feat: Phase 2F — customer CRM

Backend:
- New model: Customer (customers table) — name, nickname, phone, email, notes, is_active
- Order model: add customer_id nullable FK → customers
- New router: /api/customers/ — list (with ?search=), create, get, update, soft-delete,
  GET /{id}/orders (visit history with totals)
- Each CustomerOut includes visit_count + total_spent (computed from closed/paid orders)
- PUT /api/orders/{id}/customer — assign or unassign a customer; manager-only;
  validates customer is active; broadcasts order_updated SSE event
- GET /api/orders/{id} now returns customer_id, customer_name (with nickname), customer_phone
- Migration: CREATE TABLE customers + ALTER TABLE orders ADD COLUMN customer_id

Frontend:
- CustomersPage (/customers): searchable list (name/nickname/phone), avatar initials,
  visit count + total_spent on each row; click → detail panel slides in showing stats
  (visits, total spent, avg ticket), contact info, notes, full visit history
- OrderDetailPage: new Πελάτης card — shows assigned customer in blue if set;
  for open orders shows "+ Ανάθεση πελάτη" button → inline search dropdown → assign;
  Αφαίρεση button to unassign
- Sidebar: Users icon for Πελάτες (between Επαφές and Ρυθμίσεις)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 02:59:53 +03:00
co-authored by Claude Sonnet 4.6
parent d39382e8dd
commit e9bdf61a4d
10 changed files with 648 additions and 1 deletions
@@ -157,6 +157,8 @@ export default function OrderDetailPage({ orderId: propOrderId, readOnly = false
const [confirmAction, setConfirmAction] = useState(null) // { type, payload }
const [showPrintModal, setShowPrintModal] = useState(false)
const [payPending, setPayPending] = useState(null) // item_ids waiting for method selection
const [customerSearch, setCustomerSearch] = useState('')
const [showCustomerDropdown, setShowCustomerDropdown] = useState(false)
const { data: order, isLoading } = useQuery({
queryKey: ['order', orderId],
@@ -220,6 +222,19 @@ export default function OrderDetailPage({ orderId: propOrderId, readOnly = false
onError: () => toast.error('Σφάλμα'),
})
const { data: customersData = [] } = useQuery({
queryKey: ['customers', customerSearch],
queryFn: () => client.get('/api/customers/', { params: customerSearch ? { search: customerSearch } : {} }).then(r => r.data),
staleTime: 30_000,
enabled: showCustomerDropdown,
})
const assignCustomer = useMutation({
mutationFn: (customer_id) => client.put(`/api/orders/${orderId}/customer`, { customer_id }),
onSuccess: (_, customer_id) => { toast.success(customer_id ? 'Πελάτης ανατέθηκε' : 'Πελάτης αφαιρέθηκε'); invalidate(); setShowCustomerDropdown(false); setCustomerSearch('') },
onError: () => toast.error('Σφάλμα'),
})
const payItems = useMutation({
mutationFn: ({ item_ids, payment_method }) => client.post(`/api/orders/${orderId}/pay`, { item_ids, payment_method }),
onSuccess: () => { toast.success('Πληρώθηκε'); invalidate() },
@@ -277,6 +292,73 @@ export default function OrderDetailPage({ orderId: propOrderId, readOnly = false
</div>
</div>
{/* Customer assignment */}
<div className="card p-4">
<div className="flex items-center justify-between mb-2">
<h2 className="text-sm font-semibold text-gray-700">Πελάτης</h2>
{order.customer_id && isOpen && !readOnly && (
<button
onClick={() => assignCustomer.mutate(null)}
className="text-xs text-gray-400 hover:text-red-500"
>
Αφαίρεση
</button>
)}
</div>
{order.customer_id ? (
<div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '8px 12px', background: '#eff6ff', borderRadius: 8, border: '1px solid #bfdbfe' }}>
<span style={{ fontSize: 16 }}>👤</span>
<div>
<div style={{ fontSize: 13.5, fontWeight: 700, color: '#1d4ed8' }}>{order.customer_name || `Πελάτης #${order.customer_id}`}</div>
{order.customer_phone && <div style={{ fontSize: 11.5, color: '#3b82f6' }}>{order.customer_phone}</div>}
</div>
</div>
) : isOpen && !readOnly ? (
<div style={{ position: 'relative' }}>
{!showCustomerDropdown ? (
<button
onClick={() => setShowCustomerDropdown(true)}
style={{ fontSize: 13, color: '#6b7280', border: '1px dashed #d1d5db', borderRadius: 8, padding: '7px 14px', cursor: 'pointer', background: 'white', width: '100%', textAlign: 'left' }}
>
+ Ανάθεση πελάτη
</button>
) : (
<div>
<input
autoFocus
value={customerSearch}
onChange={e => setCustomerSearch(e.target.value)}
placeholder="Αναζήτηση πελάτη…"
style={{ width: '100%', padding: '7px 11px', border: '1.5px solid #3b82f6', borderRadius: 8, fontSize: 13, outline: 'none', boxSizing: 'border-box' }}
/>
<div style={{ position: 'absolute', top: '100%', left: 0, right: 0, background: 'white', border: '1px solid #e5e7eb', borderRadius: 8, boxShadow: '0 4px 16px rgba(0,0,0,0.10)', zIndex: 20, maxHeight: 220, overflowY: 'auto', marginTop: 4 }}>
{customersData.length === 0 && (
<div style={{ padding: '10px 14px', fontSize: 13, color: '#9ca3af' }}>Δεν βρέθηκαν πελάτες</div>
)}
{customersData.map(c => (
<button key={c.id} onClick={() => assignCustomer.mutate(c.id)}
style={{ display: 'block', width: '100%', textAlign: 'left', padding: '9px 14px', border: 'none', background: 'none', cursor: 'pointer', fontSize: 13, borderBottom: '1px solid #f3f4f6' }}
className="hover:bg-blue-50"
>
<span style={{ fontWeight: 600, color: '#111315' }}>{c.name}</span>
{c.nickname && <span style={{ color: '#9ca3af', marginLeft: 5 }}>«{c.nickname}»</span>}
{c.phone && <span style={{ color: '#6b7280', marginLeft: 8, fontSize: 12 }}>{c.phone}</span>}
</button>
))}
<button onClick={() => { setShowCustomerDropdown(false); setCustomerSearch('') }}
style={{ display: 'block', width: '100%', padding: '8px 14px', fontSize: 12, color: '#9ca3af', border: 'none', background: 'none', cursor: 'pointer', borderTop: '1px solid #f0f0ef' }}>
Ακύρωση
</button>
</div>
</div>
)}
</div>
) : (
<div style={{ fontSize: 13, color: '#d1d5db' }}>Χωρίς πελάτη</div>
)}
</div>
{/* Tabs */}
<div className="flex gap-1 border-b border-gray-200">
{[['overview', 'Επισκόπηση'], ['audit', 'Ιστορικό Συναλλαγών']].map(([key, label]) => (