feat(hq-web): Client 360 — record header, KPI row, tabbed sections, forms extracted
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>feat/client-detail-redesign
parent
9aa376fa2b
commit
c5a524bab5
@ -0,0 +1,291 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
import { fromRupees } from '@sims/domain'
|
||||||
|
import { Button, Field, Notice, Toolbar } from '@sims/ui'
|
||||||
|
import {
|
||||||
|
createAmc, createInteraction, createRecurringPlan, patchClientModule, recordPayment,
|
||||||
|
CLIENT_MODULE_STATUSES, KIND_LABEL, PAYMENT_MODES,
|
||||||
|
type ClientModule, type ClientModuleStatus, type Employee, type InteractionType,
|
||||||
|
type Kind, type Module, type PaymentMode,
|
||||||
|
} from '../api'
|
||||||
|
|
||||||
|
const today = () => new Date().toISOString().slice(0, 10)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Owner/manager only (server re-checks): pick the employee who owns this account,
|
||||||
|
* so a lead with no quote is still routable to someone's queue. Writes client.owner_id
|
||||||
|
* via the audited PATCH /clients/:id/owner. Inactive employees are hidden from the
|
||||||
|
* picker, but a current owner who was since deactivated still renders.
|
||||||
|
*/
|
||||||
|
export function AccountOwnerSelect(props: {
|
||||||
|
ownerId?: string; employees: Employee[]; onChange: (ownerId: string | null) => void
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<label style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
|
||||||
|
<span style={{ color: 'var(--muted, #6b7280)', fontSize: 13 }}>Account owner</span>
|
||||||
|
<select
|
||||||
|
className="wf" value={props.ownerId ?? ''}
|
||||||
|
onChange={(e) => props.onChange(e.target.value !== '' ? e.target.value : null)}
|
||||||
|
>
|
||||||
|
<option value="">— unassigned —</option>
|
||||||
|
{props.employees
|
||||||
|
.filter((emp) => emp.active || emp.id === props.ownerId)
|
||||||
|
.map((emp) => <option key={emp.id} value={emp.id}>{emp.displayName}</option>)}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Owner-only: create a recurring plan on one of the client's modules. */
|
||||||
|
export function NewRecurringForm(props: {
|
||||||
|
clientId: string; options: { id: string; label: string }[]; onDone: () => void
|
||||||
|
}) {
|
||||||
|
const [f, setF] = useState({ clientModuleId: '', cadence: 'monthly', amountRs: '', nextRun: today(), policy: 'manual' })
|
||||||
|
const [error, setError] = useState<string | undefined>()
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
const set = (k: keyof typeof f) => (e: { target: { value: string } }) =>
|
||||||
|
setF((p) => ({ ...p, [k]: e.target.value }))
|
||||||
|
|
||||||
|
const save = () => {
|
||||||
|
if (f.clientModuleId === '') { setError('Pick a subscribed module'); return }
|
||||||
|
const amount = f.amountRs === '' ? undefined : Number(f.amountRs)
|
||||||
|
if (amount !== undefined && (!Number.isFinite(amount) || amount <= 0)) {
|
||||||
|
setError('Amount must be a positive rupee value (or blank for the price book)'); return
|
||||||
|
}
|
||||||
|
setError(undefined)
|
||||||
|
setSaving(true)
|
||||||
|
createRecurringPlan(props.clientId, {
|
||||||
|
clientModuleId: f.clientModuleId, cadence: f.cadence, nextRun: f.nextRun, policy: f.policy,
|
||||||
|
...(amount !== undefined ? { amountPaise: fromRupees(amount) } : {}),
|
||||||
|
})
|
||||||
|
.then(() => { setF({ clientModuleId: '', cadence: 'monthly', amountRs: '', nextRun: today(), policy: 'manual' }); props.onDone() })
|
||||||
|
.catch((e: Error) => setError(e.message))
|
||||||
|
.finally(() => setSaving(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="wf-card">
|
||||||
|
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', alignItems: 'flex-end' }}>
|
||||||
|
<Field label="Module">
|
||||||
|
<select className="wf" value={f.clientModuleId} onChange={set('clientModuleId')}>
|
||||||
|
<option value="">Module…</option>
|
||||||
|
{props.options.map((o) => <option key={o.id} value={o.id}>{o.label}</option>)}
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="Cadence">
|
||||||
|
<select className="wf" value={f.cadence} onChange={set('cadence')}>
|
||||||
|
<option value="monthly">monthly</option>
|
||||||
|
<option value="yearly">yearly</option>
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="Amount (₹, optional)"><input className="wf num" style={{ width: 120 }} value={f.amountRs} onChange={set('amountRs')} /></Field>
|
||||||
|
<Field label="Next run"><input type="date" className="wf" value={f.nextRun} onChange={set('nextRun')} /></Field>
|
||||||
|
<Field label="Policy">
|
||||||
|
<select className="wf" value={f.policy} onChange={set('policy')}>
|
||||||
|
<option value="manual">manual</option>
|
||||||
|
<option value="auto">auto</option>
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Button tone="primary" onClick={save}>{saving ? 'Saving…' : 'New recurring plan'}</Button>
|
||||||
|
</div>
|
||||||
|
{error !== undefined && <Notice tone="err">{error}</Notice>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Owner-only: create an AMC contract — amount entered in rupees, stored in paise. */
|
||||||
|
export function NewAmcForm(props: { clientId: string; onDone: () => void }) {
|
||||||
|
const [f, setF] = useState({ coverage: '', periodFrom: today(), periodTo: '', amountRs: '', reminderDays: '30' })
|
||||||
|
const [error, setError] = useState<string | undefined>()
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
const set = (k: keyof typeof f) => (e: { target: { value: string } }) =>
|
||||||
|
setF((p) => ({ ...p, [k]: e.target.value }))
|
||||||
|
|
||||||
|
const save = () => {
|
||||||
|
const amount = Number(f.amountRs)
|
||||||
|
if (!Number.isFinite(amount) || amount <= 0) { setError('Enter the contract amount in rupees'); return }
|
||||||
|
if (f.periodTo === '') { setError('Pick the period end date'); return }
|
||||||
|
const days = Number(f.reminderDays)
|
||||||
|
if (!Number.isInteger(days) || days < 0) { setError('Reminder days must be a non-negative integer'); return }
|
||||||
|
setError(undefined)
|
||||||
|
setSaving(true)
|
||||||
|
createAmc(props.clientId, {
|
||||||
|
coverage: f.coverage, periodFrom: f.periodFrom, periodTo: f.periodTo,
|
||||||
|
amountPaise: fromRupees(amount), renewalReminderDays: days,
|
||||||
|
})
|
||||||
|
.then(() => { setF({ coverage: '', periodFrom: today(), periodTo: '', amountRs: '', reminderDays: '30' }); props.onDone() })
|
||||||
|
.catch((e: Error) => setError(e.message))
|
||||||
|
.finally(() => setSaving(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="wf-card">
|
||||||
|
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', alignItems: 'flex-end' }}>
|
||||||
|
<Field label="Coverage"><input className="wf" value={f.coverage} onChange={set('coverage')} /></Field>
|
||||||
|
<Field label="Period from"><input type="date" className="wf" value={f.periodFrom} onChange={set('periodFrom')} /></Field>
|
||||||
|
<Field label="Period to"><input type="date" className="wf" value={f.periodTo} onChange={set('periodTo')} /></Field>
|
||||||
|
<Field label="Amount (₹)"><input className="wf num" style={{ width: 120 }} value={f.amountRs} onChange={set('amountRs')} /></Field>
|
||||||
|
<Field label="Reminder days"><input className="wf num" style={{ width: 80 }} value={f.reminderDays} onChange={set('reminderDays')} /></Field>
|
||||||
|
<Button tone="primary" onClick={save}>{saving ? 'Saving…' : 'New AMC'}</Button>
|
||||||
|
</div>
|
||||||
|
{error !== undefined && <Notice tone="err">{error}</Notice>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** All roles: log a call / visit / training …; a follow-up date feeds the daily scan. */
|
||||||
|
export function LogInteractionForm(props: { clientId: string; types: InteractionType[]; onDone: () => void }) {
|
||||||
|
const [f, setF] = useState({ typeCode: '', onDate: today(), notes: '', outcome: '', followUpOn: '' })
|
||||||
|
const [error, setError] = useState<string | undefined>()
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
const set = (k: keyof typeof f) => (e: { target: { value: string } }) =>
|
||||||
|
setF((p) => ({ ...p, [k]: e.target.value }))
|
||||||
|
|
||||||
|
const save = () => {
|
||||||
|
if (f.typeCode === '') { setError('Pick an interaction type'); return }
|
||||||
|
setError(undefined)
|
||||||
|
setSaving(true)
|
||||||
|
createInteraction(props.clientId, {
|
||||||
|
typeCode: f.typeCode, onDate: f.onDate, notes: f.notes,
|
||||||
|
...(f.outcome !== '' ? { outcome: f.outcome } : {}),
|
||||||
|
...(f.followUpOn !== '' ? { followUpOn: f.followUpOn } : {}),
|
||||||
|
})
|
||||||
|
.then(() => { setF({ typeCode: '', onDate: today(), notes: '', outcome: '', followUpOn: '' }); props.onDone() })
|
||||||
|
.catch((e: Error) => setError(e.message))
|
||||||
|
.finally(() => setSaving(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="wf-card">
|
||||||
|
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', alignItems: 'flex-end' }}>
|
||||||
|
<Field label="Type">
|
||||||
|
<select className="wf" value={f.typeCode} onChange={set('typeCode')}>
|
||||||
|
<option value="">Type…</option>
|
||||||
|
{props.types.map((t) => <option key={t.code} value={t.code}>{t.label}</option>)}
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="On"><input type="date" className="wf" value={f.onDate} onChange={set('onDate')} /></Field>
|
||||||
|
<Field label="Outcome">
|
||||||
|
<select className="wf" value={f.outcome} onChange={set('outcome')}>
|
||||||
|
<option value="">—</option>
|
||||||
|
<option value="positive">positive</option>
|
||||||
|
<option value="neutral">neutral</option>
|
||||||
|
<option value="negative">negative</option>
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="Follow-up on"><input type="date" className="wf" value={f.followUpOn} onChange={set('followUpOn')} /></Field>
|
||||||
|
<Button tone="primary" onClick={save}>{saving ? 'Logging…' : 'Log interaction'}</Button>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginTop: 8 }}>
|
||||||
|
<Field label="Notes"><textarea className="wf" rows={2} style={{ width: '100%' }} value={f.notes} onChange={set('notes')} /></Field>
|
||||||
|
</div>
|
||||||
|
{error !== undefined && <Notice tone="err">{error}</Notice>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function AssignModuleForm(props: { modules: Module[]; onAssign: (moduleId: string, kind: Kind) => void }) {
|
||||||
|
const [moduleId, setModuleId] = useState('')
|
||||||
|
const mod = props.modules.find((m) => m.id === moduleId)
|
||||||
|
const [kind, setKind] = useState<Kind | ''>('')
|
||||||
|
return (
|
||||||
|
<Toolbar>
|
||||||
|
<select
|
||||||
|
className="wf" value={moduleId}
|
||||||
|
onChange={(e) => { setModuleId(e.target.value); setKind('') }}
|
||||||
|
>
|
||||||
|
<option value="">Assign module…</option>
|
||||||
|
{props.modules.filter((m) => m.active).map((m) => <option key={m.id} value={m.id}>{m.name}</option>)}
|
||||||
|
</select>
|
||||||
|
{mod !== undefined && (
|
||||||
|
<select className="wf" value={kind} onChange={(e) => setKind(e.target.value as Kind)}>
|
||||||
|
<option value="">Kind…</option>
|
||||||
|
{mod.allowedKinds.map((k) => <option key={k} value={k}>{KIND_LABEL[k]}</option>)}
|
||||||
|
</select>
|
||||||
|
)}
|
||||||
|
<Button
|
||||||
|
tone="primary"
|
||||||
|
onClick={() => { if (moduleId !== '' && kind !== '') { props.onAssign(moduleId, kind); setModuleId(''); setKind('') } }}
|
||||||
|
>
|
||||||
|
Assign
|
||||||
|
</Button>
|
||||||
|
</Toolbar>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ClientModuleStatusSelect(props: {
|
||||||
|
cm: ClientModule; onPatched: () => void; onError: (msg: string) => void
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<select
|
||||||
|
className="wf" value={props.cm.status}
|
||||||
|
onChange={(e) => {
|
||||||
|
patchClientModule(props.cm.id, { status: e.target.value as ClientModuleStatus })
|
||||||
|
.then(props.onPatched).catch((err: Error) => props.onError(err.message))
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{CLIENT_MODULE_STATUSES.map((s) => <option key={s} value={s}>{s}</option>)}
|
||||||
|
</select>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function RowDate(props: {
|
||||||
|
cm: ClientModule; field: 'installedOn' | 'completedOn' | 'trainedOn'
|
||||||
|
onPatched: () => void; onError: (msg: string) => void
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<input
|
||||||
|
type="date" className="wf" style={{ width: 140 }}
|
||||||
|
value={props.cm[props.field] ?? ''}
|
||||||
|
onChange={(e) => {
|
||||||
|
patchClientModule(props.cm.id, { [props.field]: e.target.value !== '' ? e.target.value : null })
|
||||||
|
.then(props.onPatched).catch((err: Error) => props.onError(err.message))
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record a payment against the client — amounts entered in rupees, stored in
|
||||||
|
* integer paise via fromRupees. Shared with DocumentView's "Record payment".
|
||||||
|
*/
|
||||||
|
export function PaymentForm(props: { clientId: string; onDone: () => void }) {
|
||||||
|
const [f, setF] = useState({ amountRs: '', tdsRs: '', mode: 'bank' as PaymentMode, reference: '', receivedOn: today() })
|
||||||
|
const [error, setError] = useState<string | undefined>()
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
const set = (k: keyof typeof f) => (e: { target: { value: string } }) =>
|
||||||
|
setF((p) => ({ ...p, [k]: e.target.value }))
|
||||||
|
|
||||||
|
const save = () => {
|
||||||
|
const amount = Number(f.amountRs)
|
||||||
|
if (!Number.isFinite(amount) || amount <= 0) { setError('Enter a payment amount in rupees'); return }
|
||||||
|
const tds = f.tdsRs === '' ? 0 : Number(f.tdsRs)
|
||||||
|
if (!Number.isFinite(tds) || tds < 0) { setError('TDS must be a non-negative rupee amount'); return }
|
||||||
|
setError(undefined)
|
||||||
|
setSaving(true)
|
||||||
|
recordPayment({
|
||||||
|
clientId: props.clientId, receivedOn: f.receivedOn, mode: f.mode, reference: f.reference,
|
||||||
|
amountPaise: fromRupees(amount), tdsPaise: fromRupees(tds),
|
||||||
|
})
|
||||||
|
.then(() => { setF({ amountRs: '', tdsRs: '', mode: 'bank', reference: '', receivedOn: today() }); props.onDone() })
|
||||||
|
.catch((e: Error) => setError(e.message))
|
||||||
|
.finally(() => setSaving(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="wf-card">
|
||||||
|
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', alignItems: 'flex-end' }}>
|
||||||
|
<Field label="Amount (₹)"><input className="wf num" style={{ width: 120 }} value={f.amountRs} onChange={set('amountRs')} /></Field>
|
||||||
|
<Field label="TDS (₹)"><input className="wf num" style={{ width: 100 }} value={f.tdsRs} onChange={set('tdsRs')} /></Field>
|
||||||
|
<Field label="Mode">
|
||||||
|
<select className="wf" value={f.mode} onChange={set('mode')}>
|
||||||
|
{PAYMENT_MODES.map((m) => <option key={m} value={m}>{m}</option>)}
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="Reference"><input className="wf" value={f.reference} onChange={set('reference')} /></Field>
|
||||||
|
<Field label="Received on"><input type="date" className="wf" value={f.receivedOn} onChange={set('receivedOn')} /></Field>
|
||||||
|
<Button tone="primary" onClick={save}>{saving ? 'Recording…' : 'Record payment'}</Button>
|
||||||
|
</div>
|
||||||
|
{error !== undefined && <Notice tone="err">{error}</Notice>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue