@baack-software/baack-ts-crm 0.0.0-stage → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +74 -2
- package/dist/account.d.ts +36 -0
- package/dist/account.d.ts.map +1 -0
- package/dist/account.js +48 -0
- package/dist/account.js.map +1 -0
- package/dist/backend.d.ts +104 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +328 -0
- package/dist/backend.js.map +1 -0
- package/dist/crm-client.d.ts +164 -0
- package/dist/crm-client.d.ts.map +1 -0
- package/dist/crm-client.js +252 -0
- package/dist/crm-client.js.map +1 -0
- package/dist/deal-order.d.ts +42 -0
- package/dist/deal-order.d.ts.map +1 -0
- package/dist/deal-order.js +80 -0
- package/dist/deal-order.js.map +1 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +33 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/invoices.d.ts +53 -0
- package/dist/invoices.d.ts.map +1 -0
- package/dist/invoices.js +155 -0
- package/dist/invoices.js.map +1 -0
- package/dist/leads.d.ts +29 -0
- package/dist/leads.d.ts.map +1 -0
- package/dist/leads.js +56 -0
- package/dist/leads.js.map +1 -0
- package/dist/mock/index.d.ts +43 -0
- package/dist/mock/index.d.ts.map +1 -0
- package/dist/mock/index.js +303 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/money.d.ts +29 -0
- package/dist/money.d.ts.map +1 -0
- package/dist/money.js +82 -0
- package/dist/money.js.map +1 -0
- package/dist/period.d.ts +30 -0
- package/dist/period.d.ts.map +1 -0
- package/dist/period.js +49 -0
- package/dist/period.js.map +1 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +58 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/react/Charts.d.ts +19 -0
- package/dist/react/Charts.d.ts.map +1 -0
- package/dist/react/Charts.js +38 -0
- package/dist/react/Charts.js.map +1 -0
- package/dist/react/ChoiceCombobox.d.ts +36 -0
- package/dist/react/ChoiceCombobox.d.ts.map +1 -0
- package/dist/react/ChoiceCombobox.js +105 -0
- package/dist/react/ChoiceCombobox.js.map +1 -0
- package/dist/react/CompanyAccountView.d.ts +21 -0
- package/dist/react/CompanyAccountView.d.ts.map +1 -0
- package/dist/react/CompanyAccountView.js +27 -0
- package/dist/react/CompanyAccountView.js.map +1 -0
- package/dist/react/DashboardCards.d.ts +32 -0
- package/dist/react/DashboardCards.d.ts.map +1 -0
- package/dist/react/DashboardCards.js +38 -0
- package/dist/react/DashboardCards.js.map +1 -0
- package/dist/react/DealsBoard.d.ts +49 -0
- package/dist/react/DealsBoard.d.ts.map +1 -0
- package/dist/react/DealsBoard.js +82 -0
- package/dist/react/DealsBoard.js.map +1 -0
- package/dist/react/Details.d.ts +85 -0
- package/dist/react/Details.d.ts.map +1 -0
- package/dist/react/Details.js +268 -0
- package/dist/react/Details.js.map +1 -0
- package/dist/react/Forms.d.ts +190 -0
- package/dist/react/Forms.d.ts.map +1 -0
- package/dist/react/Forms.js +494 -0
- package/dist/react/Forms.js.map +1 -0
- package/dist/react/LeadsTable.d.ts +22 -0
- package/dist/react/LeadsTable.d.ts.map +1 -0
- package/dist/react/LeadsTable.js +79 -0
- package/dist/react/LeadsTable.js.map +1 -0
- package/dist/react/Sales.d.ts +68 -0
- package/dist/react/Sales.d.ts.map +1 -0
- package/dist/react/Sales.js +270 -0
- package/dist/react/Sales.js.map +1 -0
- package/dist/react/SidePanel.d.ts +31 -0
- package/dist/react/SidePanel.d.ts.map +1 -0
- package/dist/react/SidePanel.js +49 -0
- package/dist/react/SidePanel.js.map +1 -0
- package/dist/react/format.d.ts +28 -0
- package/dist/react/format.d.ts.map +1 -0
- package/dist/react/format.js +30 -0
- package/dist/react/format.js.map +1 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +15 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/parts.d.ts +55 -0
- package/dist/react/parts.d.ts.map +1 -0
- package/dist/react/parts.js +82 -0
- package/dist/react/parts.js.map +1 -0
- package/dist/react/pipeline-choice.d.ts +19 -0
- package/dist/react/pipeline-choice.d.ts.map +1 -0
- package/dist/react/pipeline-choice.js +39 -0
- package/dist/react/pipeline-choice.js.map +1 -0
- package/dist/react/text.d.ts +232 -0
- package/dist/react/text.d.ts.map +1 -0
- package/dist/react/text.js +229 -0
- package/dist/react/text.js.map +1 -0
- package/dist/react/ui/alert.d.ts +11 -0
- package/dist/react/ui/alert.d.ts.map +1 -0
- package/dist/react/ui/alert.js +29 -0
- package/dist/react/ui/alert.js.map +1 -0
- package/dist/react/ui/badge.d.ts +10 -0
- package/dist/react/ui/badge.d.ts.map +1 -0
- package/dist/react/ui/badge.js +26 -0
- package/dist/react/ui/badge.js.map +1 -0
- package/dist/react/ui/button.d.ts +11 -0
- package/dist/react/ui/button.d.ts.map +1 -0
- package/dist/react/ui/button.js +37 -0
- package/dist/react/ui/button.js.map +1 -0
- package/dist/react/ui/card.d.ts +12 -0
- package/dist/react/ui/card.d.ts.map +1 -0
- package/dist/react/ui/card.js +26 -0
- package/dist/react/ui/card.js.map +1 -0
- package/dist/react/ui/chart.d.ts +45 -0
- package/dist/react/ui/chart.d.ts.map +1 -0
- package/dist/react/ui/chart.js +116 -0
- package/dist/react/ui/chart.js.map +1 -0
- package/dist/react/ui/input.d.ts +4 -0
- package/dist/react/ui/input.d.ts.map +1 -0
- package/dist/react/ui/input.js +8 -0
- package/dist/react/ui/input.js.map +1 -0
- package/dist/react/ui/label.d.ts +5 -0
- package/dist/react/ui/label.d.ts.map +1 -0
- package/dist/react/ui/label.js +10 -0
- package/dist/react/ui/label.js.map +1 -0
- package/dist/react/ui/progress.d.ts +5 -0
- package/dist/react/ui/progress.d.ts.map +1 -0
- package/dist/react/ui/progress.js +10 -0
- package/dist/react/ui/progress.js.map +1 -0
- package/dist/react/ui/select.d.ts +16 -0
- package/dist/react/ui/select.d.ts.map +1 -0
- package/dist/react/ui/select.js +37 -0
- package/dist/react/ui/select.js.map +1 -0
- package/dist/react/ui/separator.d.ts +5 -0
- package/dist/react/ui/separator.d.ts.map +1 -0
- package/dist/react/ui/separator.js +10 -0
- package/dist/react/ui/separator.js.map +1 -0
- package/dist/react/ui/sheet.d.ts +15 -0
- package/dist/react/ui/sheet.d.ts.map +1 -0
- package/dist/react/ui/sheet.js +38 -0
- package/dist/react/ui/sheet.js.map +1 -0
- package/dist/react/ui/skeleton.d.ts +3 -0
- package/dist/react/ui/skeleton.d.ts.map +1 -0
- package/dist/react/ui/skeleton.js +7 -0
- package/dist/react/ui/skeleton.js.map +1 -0
- package/dist/react/ui/table.d.ts +11 -0
- package/dist/react/ui/table.d.ts.map +1 -0
- package/dist/react/ui/table.js +29 -0
- package/dist/react/ui/table.js.map +1 -0
- package/dist/react/ui/tabs.d.ts +12 -0
- package/dist/react/ui/tabs.d.ts.map +1 -0
- package/dist/react/ui/tabs.js +31 -0
- package/dist/react/ui/tabs.js.map +1 -0
- package/dist/react/views.d.ts +82 -0
- package/dist/react/views.d.ts.map +1 -0
- package/dist/react/views.js +241 -0
- package/dist/react/views.js.map +1 -0
- package/dist/sales.d.ts +82 -0
- package/dist/sales.d.ts.map +1 -0
- package/dist/sales.js +87 -0
- package/dist/sales.js.map +1 -0
- package/dist/store.d.ts +159 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +305 -0
- package/dist/store.js.map +1 -0
- package/dist/target.d.ts +22 -0
- package/dist/target.d.ts.map +1 -0
- package/dist/target.js +40 -0
- package/dist/target.js.map +1 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +100 -4
- package/src/account.ts +79 -0
- package/src/backend.ts +501 -0
- package/src/crm-client.ts +393 -0
- package/src/deal-order.ts +116 -0
- package/src/errors.ts +34 -0
- package/src/index.ts +13 -0
- package/src/invoices.ts +177 -0
- package/src/leads.ts +78 -0
- package/src/mock/index.ts +398 -0
- package/src/money.ts +104 -0
- package/src/period.ts +71 -0
- package/src/pipeline.ts +82 -0
- package/src/react/Charts.tsx +143 -0
- package/src/react/ChoiceCombobox.tsx +227 -0
- package/src/react/CompanyAccountView.tsx +198 -0
- package/src/react/DashboardCards.tsx +171 -0
- package/src/react/DealsBoard.tsx +344 -0
- package/src/react/Details.tsx +870 -0
- package/src/react/Forms.tsx +1486 -0
- package/src/react/LeadsTable.tsx +234 -0
- package/src/react/Sales.tsx +827 -0
- package/src/react/SidePanel.tsx +104 -0
- package/src/react/format.ts +53 -0
- package/src/react/index.ts +14 -0
- package/src/react/parts.tsx +183 -0
- package/src/react/pipeline-choice.ts +54 -0
- package/src/react/text.ts +232 -0
- package/src/react/ui/README.md +6 -0
- package/src/react/ui/alert.tsx +75 -0
- package/src/react/ui/badge.tsx +48 -0
- package/src/react/ui/button.tsx +66 -0
- package/src/react/ui/card.tsx +102 -0
- package/src/react/ui/chart.tsx +304 -0
- package/src/react/ui/input.tsx +18 -0
- package/src/react/ui/label.tsx +23 -0
- package/src/react/ui/progress.tsx +30 -0
- package/src/react/ui/select.tsx +189 -0
- package/src/react/ui/separator.tsx +27 -0
- package/src/react/ui/sheet.tsx +145 -0
- package/src/react/ui/skeleton.tsx +13 -0
- package/src/react/ui/table.tsx +113 -0
- package/src/react/ui/tabs.tsx +89 -0
- package/src/react/views.tsx +633 -0
- package/src/sales.ts +167 -0
- package/src/store.ts +528 -0
- package/src/target.ts +54 -0
- package/src/types.ts +230 -0
|
@@ -0,0 +1,1486 @@
|
|
|
1
|
+
import { ref, type RefLike } from '@baack-software/baack-ts-tasks';
|
|
2
|
+
import { ChevronRight, Plus, X } from 'lucide-react';
|
|
3
|
+
import { useId, useState, type FormEvent, type ReactNode } from 'react';
|
|
4
|
+
import type { DealInput, InvoiceInput, LeadInput } from '../backend.ts';
|
|
5
|
+
import type { NewInvoiceOptions, NewLeadOptions } from '../crm-client.ts';
|
|
6
|
+
import { INVOICE_STATUSES, INVOICE_STATUS_LABELS } from '../invoices.ts';
|
|
7
|
+
import { LEAD_SOURCES, SOURCE_LABELS } from '../leads.ts';
|
|
8
|
+
import { formatMoney, fractionDigits, fromMinor, toMinor } from '../money.ts';
|
|
9
|
+
import { orderedStages } from '../pipeline.ts';
|
|
10
|
+
import type {
|
|
11
|
+
Deal,
|
|
12
|
+
InvoiceStatus,
|
|
13
|
+
LeadSource,
|
|
14
|
+
Money,
|
|
15
|
+
NewContact,
|
|
16
|
+
Pipeline,
|
|
17
|
+
PipelineStage,
|
|
18
|
+
Taxability,
|
|
19
|
+
} from '../types.ts';
|
|
20
|
+
import { ChoiceCombobox } from './ChoiceCombobox.tsx';
|
|
21
|
+
import { PanelCloseButton } from './SidePanel.tsx';
|
|
22
|
+
import { withText, type CrmText } from './text.ts';
|
|
23
|
+
import { Button } from './ui/button.tsx';
|
|
24
|
+
import { Card, CardAction, CardContent, CardHeader, CardTitle } from './ui/card.tsx';
|
|
25
|
+
import { Input } from './ui/input.tsx';
|
|
26
|
+
import { Label } from './ui/label.tsx';
|
|
27
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select.tsx';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A record the forms can link to, named: one of the host's customers, or one of its people. `detail`, such as a
|
|
31
|
+
* person's email address, tells apart records with the same name.
|
|
32
|
+
*/
|
|
33
|
+
export interface CrmChoice {
|
|
34
|
+
urn: string;
|
|
35
|
+
name: string;
|
|
36
|
+
detail?: string | undefined;
|
|
37
|
+
/** More to find it by when searching, such as a person's other emails and telephone numbers. */
|
|
38
|
+
keywords?: readonly string[] | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A company the forms can link a record to: the host's customers. */
|
|
42
|
+
export type CrmCompanyChoice = CrmChoice;
|
|
43
|
+
|
|
44
|
+
/** A person the forms can link a record to, as its contact or assignee: the host's people. */
|
|
45
|
+
export type CrmPersonChoice = CrmChoice;
|
|
46
|
+
|
|
47
|
+
interface FormProps {
|
|
48
|
+
/** The company whose CRM this is. */
|
|
49
|
+
owner: RefLike;
|
|
50
|
+
text?: Partial<CrmText> | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** An invoice line as the new invoice form makes it: its unit price in major units, its price and tax as money. */
|
|
54
|
+
export interface InvoiceLineDraft {
|
|
55
|
+
description: string;
|
|
56
|
+
quantity: number;
|
|
57
|
+
unitPrice: number;
|
|
58
|
+
price: Money;
|
|
59
|
+
tax: Money;
|
|
60
|
+
itemCode?: string;
|
|
61
|
+
discountRate?: number;
|
|
62
|
+
discountPrice?: Money;
|
|
63
|
+
taxability?: Taxability;
|
|
64
|
+
taxName?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Radix's select keeps "" for "no value", so "none" is chosen with this instead. */
|
|
68
|
+
const NONE = '__none__';
|
|
69
|
+
/** "Add a new ..." in a choice select. */
|
|
70
|
+
const ADD = '__add__';
|
|
71
|
+
/** A new record, made when the form is saved, shown as chosen. */
|
|
72
|
+
const PENDING = '__pending__';
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* An amount typed in major units (120.00, or 1,500.00 with separators) as minor units, or undefined when it isn't a
|
|
76
|
+
* number of at least zero.
|
|
77
|
+
*/
|
|
78
|
+
export function minorOrUndefined(amount: string, currencyCode: string): number | undefined {
|
|
79
|
+
try {
|
|
80
|
+
const minor = toMinor(amount.replaceAll(',', '').trim(), currencyCode);
|
|
81
|
+
return minor >= 0 ? minor : undefined;
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A date input's value (YYYY-MM-DD) as the API's timestamp: the start of that day, UTC. */
|
|
88
|
+
const dayTimestamp = (day: string) => (day ? `${day}T00:00:00` : null);
|
|
89
|
+
const today = (now: Date) => now.toISOString().slice(0, 10);
|
|
90
|
+
const inDays = (now: Date, days: number) => today(new Date(now.getTime() + days * 86_400_000));
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A form's frame: a card, as an open record's is, with its heading (and the side panel's close button), fields,
|
|
94
|
+
* problem and submit button. `back` adds a way back beside the submit button, for a step of a larger form.
|
|
95
|
+
*/
|
|
96
|
+
function FormFrame({
|
|
97
|
+
title,
|
|
98
|
+
submit,
|
|
99
|
+
submitting,
|
|
100
|
+
saving,
|
|
101
|
+
error,
|
|
102
|
+
onSubmit,
|
|
103
|
+
back,
|
|
104
|
+
framed = true,
|
|
105
|
+
children,
|
|
106
|
+
}: {
|
|
107
|
+
title: string;
|
|
108
|
+
submit: string;
|
|
109
|
+
submitting: string;
|
|
110
|
+
saving: boolean;
|
|
111
|
+
error: string | undefined;
|
|
112
|
+
onSubmit: () => Promise<void>;
|
|
113
|
+
back?: { label: string; onBack: () => void } | undefined;
|
|
114
|
+
/** False for a form inside another card, such as a step of an open lead: no card of its own. */
|
|
115
|
+
framed?: boolean | undefined;
|
|
116
|
+
children: (ids: { error: string | undefined }) => ReactNode;
|
|
117
|
+
}) {
|
|
118
|
+
const id = useId();
|
|
119
|
+
const errorId = error ? `${id}-error` : undefined;
|
|
120
|
+
const heading = (
|
|
121
|
+
<h2 id={`${id}-title`} className="text-base font-semibold">
|
|
122
|
+
{title}
|
|
123
|
+
</h2>
|
|
124
|
+
);
|
|
125
|
+
const form = (
|
|
126
|
+
<form
|
|
127
|
+
className="grid gap-3"
|
|
128
|
+
aria-labelledby={`${id}-title`}
|
|
129
|
+
noValidate
|
|
130
|
+
onSubmit={(event: FormEvent) => {
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
void onSubmit();
|
|
133
|
+
}}
|
|
134
|
+
>
|
|
135
|
+
{!framed && (
|
|
136
|
+
<div className="flex items-center justify-between gap-2">
|
|
137
|
+
{heading}
|
|
138
|
+
<PanelCloseButton />
|
|
139
|
+
</div>
|
|
140
|
+
)}
|
|
141
|
+
{children({ error: errorId })}
|
|
142
|
+
{error && (
|
|
143
|
+
<p id={errorId} role="alert" className="text-destructive text-sm">
|
|
144
|
+
{error}
|
|
145
|
+
</p>
|
|
146
|
+
)}
|
|
147
|
+
<div className="flex gap-2">
|
|
148
|
+
<Button type="submit" disabled={saving}>
|
|
149
|
+
{saving ? submitting : submit}
|
|
150
|
+
</Button>
|
|
151
|
+
{back && (
|
|
152
|
+
<Button type="button" variant="ghost" onClick={back.onBack}>
|
|
153
|
+
{back.label}
|
|
154
|
+
</Button>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
</form>
|
|
158
|
+
);
|
|
159
|
+
if (!framed) return form;
|
|
160
|
+
return (
|
|
161
|
+
<Card>
|
|
162
|
+
<CardHeader>
|
|
163
|
+
<CardTitle>{heading}</CardTitle>
|
|
164
|
+
<CardAction>
|
|
165
|
+
<PanelCloseButton />
|
|
166
|
+
</CardAction>
|
|
167
|
+
</CardHeader>
|
|
168
|
+
<CardContent>{form}</CardContent>
|
|
169
|
+
</Card>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function Field({
|
|
174
|
+
id,
|
|
175
|
+
label,
|
|
176
|
+
hint,
|
|
177
|
+
children,
|
|
178
|
+
}: {
|
|
179
|
+
id: string;
|
|
180
|
+
label: string;
|
|
181
|
+
hint?: string | undefined;
|
|
182
|
+
children: ReactNode;
|
|
183
|
+
}) {
|
|
184
|
+
return (
|
|
185
|
+
<div className="grid gap-1.5">
|
|
186
|
+
<Label htmlFor={id}>{label}</Label>
|
|
187
|
+
{children}
|
|
188
|
+
{hint && (
|
|
189
|
+
<p id={`${id}-hint`} className="text-muted-foreground text-xs">
|
|
190
|
+
{hint}
|
|
191
|
+
</p>
|
|
192
|
+
)}
|
|
193
|
+
</div>
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* A record to link: none, one of `choices`, or, with `add`, a new one made by the step `add` opens. A new one waiting
|
|
199
|
+
* to be made shows as chosen (`pending`, its name). A linked record missing from `choices` is still shown, named by
|
|
200
|
+
* `named` or by the end of its URN.
|
|
201
|
+
*/
|
|
202
|
+
export function ChoiceSelect({
|
|
203
|
+
id,
|
|
204
|
+
value,
|
|
205
|
+
onChange,
|
|
206
|
+
choices,
|
|
207
|
+
none,
|
|
208
|
+
add,
|
|
209
|
+
pending,
|
|
210
|
+
named,
|
|
211
|
+
disabled,
|
|
212
|
+
describedBy,
|
|
213
|
+
text,
|
|
214
|
+
}: {
|
|
215
|
+
id: string;
|
|
216
|
+
value: string;
|
|
217
|
+
onChange: (urn: string) => void;
|
|
218
|
+
choices: readonly CrmChoice[];
|
|
219
|
+
none: string;
|
|
220
|
+
add?: { label: string; onAdd: () => void } | undefined;
|
|
221
|
+
pending?: string | undefined;
|
|
222
|
+
named?: ((urn: string) => string | undefined) | undefined;
|
|
223
|
+
disabled?: boolean | undefined;
|
|
224
|
+
describedBy?: string | undefined;
|
|
225
|
+
text?: Partial<CrmText> | undefined;
|
|
226
|
+
}) {
|
|
227
|
+
const words = withText(text);
|
|
228
|
+
const missing = value && !choices.some((choice) => choice.urn === value) ? value : undefined;
|
|
229
|
+
return (
|
|
230
|
+
<Select
|
|
231
|
+
value={pending ? PENDING : value || NONE}
|
|
232
|
+
disabled={disabled ?? false}
|
|
233
|
+
onValueChange={(next) => {
|
|
234
|
+
if (next === ADD) add?.onAdd();
|
|
235
|
+
else if (next !== PENDING) onChange(next === NONE ? '' : next);
|
|
236
|
+
}}
|
|
237
|
+
>
|
|
238
|
+
<SelectTrigger id={id} className="w-full" aria-describedby={describedBy}>
|
|
239
|
+
<SelectValue />
|
|
240
|
+
</SelectTrigger>
|
|
241
|
+
<SelectContent>
|
|
242
|
+
<SelectItem value={NONE}>{none}</SelectItem>
|
|
243
|
+
{pending && <SelectItem value={PENDING}>{`${pending} (${words.toBeCreated})`}</SelectItem>}
|
|
244
|
+
{missing && <SelectItem value={missing}>{named?.(missing) ?? `...${missing.slice(-6)}`}</SelectItem>}
|
|
245
|
+
{choices.map((choice) => (
|
|
246
|
+
<SelectItem key={choice.urn} value={choice.urn}>
|
|
247
|
+
{choice.name}
|
|
248
|
+
{choice.detail && (
|
|
249
|
+
<>
|
|
250
|
+
{' '}
|
|
251
|
+
<span className="text-muted-foreground">{choice.detail}</span>
|
|
252
|
+
</>
|
|
253
|
+
)}
|
|
254
|
+
</SelectItem>
|
|
255
|
+
))}
|
|
256
|
+
{add && <SelectItem value={ADD}>{add.label}</SelectItem>}
|
|
257
|
+
</SelectContent>
|
|
258
|
+
</Select>
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The currencies offered for an amount, with the one it's already in when that's another. */
|
|
263
|
+
export const CURRENCIES: readonly string[] = ['GBP', 'EUR', 'USD'];
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* An amount of money: a currency select joined to the amount, typed in major units (1,500.00). The amount's input
|
|
267
|
+
* has `id`, so a field's label names it; the currency select is named by `currencyLabel`.
|
|
268
|
+
*/
|
|
269
|
+
export function MoneyInput({
|
|
270
|
+
id,
|
|
271
|
+
amount,
|
|
272
|
+
currencyCode,
|
|
273
|
+
onAmount,
|
|
274
|
+
onCurrency,
|
|
275
|
+
onCommit,
|
|
276
|
+
describedBy,
|
|
277
|
+
text,
|
|
278
|
+
}: {
|
|
279
|
+
id: string;
|
|
280
|
+
amount: string;
|
|
281
|
+
currencyCode: string;
|
|
282
|
+
onAmount: (amount: string) => void;
|
|
283
|
+
onCurrency: (currencyCode: string) => void;
|
|
284
|
+
/** Called when the amount loses focus or Enter is pressed, for fields that save as they change. */
|
|
285
|
+
onCommit?: (() => void) | undefined;
|
|
286
|
+
describedBy?: string | undefined;
|
|
287
|
+
text?: Partial<CrmText> | undefined;
|
|
288
|
+
}) {
|
|
289
|
+
const words = withText(text);
|
|
290
|
+
const currencies = CURRENCIES.includes(currencyCode) ? CURRENCIES : [currencyCode, ...CURRENCIES];
|
|
291
|
+
return (
|
|
292
|
+
<div className="flex">
|
|
293
|
+
<Select value={currencyCode} onValueChange={onCurrency}>
|
|
294
|
+
<SelectTrigger aria-label={words.currency} className="w-24 shrink-0 rounded-e-none border-e-0">
|
|
295
|
+
<SelectValue />
|
|
296
|
+
</SelectTrigger>
|
|
297
|
+
<SelectContent>
|
|
298
|
+
{currencies.map((code) => (
|
|
299
|
+
<SelectItem key={code} value={code}>
|
|
300
|
+
{code}
|
|
301
|
+
</SelectItem>
|
|
302
|
+
))}
|
|
303
|
+
</SelectContent>
|
|
304
|
+
</Select>
|
|
305
|
+
<Input
|
|
306
|
+
id={id}
|
|
307
|
+
inputMode="decimal"
|
|
308
|
+
className="rounded-s-none"
|
|
309
|
+
value={amount}
|
|
310
|
+
onChange={(event) => onAmount(event.target.value)}
|
|
311
|
+
onBlur={onCommit}
|
|
312
|
+
onKeyDown={(event) => {
|
|
313
|
+
if (event.key === 'Enter') onCommit?.();
|
|
314
|
+
}}
|
|
315
|
+
aria-describedby={describedBy}
|
|
316
|
+
/>
|
|
317
|
+
</div>
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Money typed into a `MoneyInput`, for saving: the money, null for no amount, or undefined when the amount isn't a
|
|
323
|
+
* number of at least zero.
|
|
324
|
+
*/
|
|
325
|
+
export function moneyOf(amount: string, currencyCode: string): Money | null | undefined {
|
|
326
|
+
if (!amount.trim()) return null;
|
|
327
|
+
const minor = minorOrUndefined(amount, currencyCode);
|
|
328
|
+
if (minor === undefined) return undefined;
|
|
329
|
+
return minor ? fromMinor(minor, currencyCode) : null;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* A new client company, as a step of a lead's form: its name, made when the lead is saved. `Use this` goes back to
|
|
334
|
+
* the lead with it chosen.
|
|
335
|
+
*/
|
|
336
|
+
export function NewCompanyStep({
|
|
337
|
+
initial = '',
|
|
338
|
+
onUse,
|
|
339
|
+
onBack,
|
|
340
|
+
framed,
|
|
341
|
+
text,
|
|
342
|
+
}: {
|
|
343
|
+
initial?: string | undefined;
|
|
344
|
+
/** False inside another card, such as an open lead's. */
|
|
345
|
+
framed?: boolean | undefined;
|
|
346
|
+
onUse: (name: string) => Promise<unknown> | void;
|
|
347
|
+
onBack: () => void;
|
|
348
|
+
text?: Partial<CrmText> | undefined;
|
|
349
|
+
}) {
|
|
350
|
+
const words = withText(text);
|
|
351
|
+
const id = useId();
|
|
352
|
+
const [name, setName] = useState(initial);
|
|
353
|
+
const { saving, error, setError, run } = useSave();
|
|
354
|
+
return (
|
|
355
|
+
<FormFrame
|
|
356
|
+
title={words.newCompany}
|
|
357
|
+
submit={words.useThis}
|
|
358
|
+
submitting={words.saving}
|
|
359
|
+
saving={saving}
|
|
360
|
+
error={error}
|
|
361
|
+
back={{ label: words.back, onBack }}
|
|
362
|
+
framed={framed}
|
|
363
|
+
onSubmit={async () => {
|
|
364
|
+
if (!name.trim()) return setError(words.companyNameRequired);
|
|
365
|
+
await run(async () => onUse(name.trim()));
|
|
366
|
+
}}
|
|
367
|
+
>
|
|
368
|
+
{({ error: errorId }) => (
|
|
369
|
+
<Field id={`${id}-name`} label={words.companyName} hint={words.newCompanyHint}>
|
|
370
|
+
<Input
|
|
371
|
+
id={`${id}-name`}
|
|
372
|
+
value={name}
|
|
373
|
+
onChange={(event) => setName(event.target.value)}
|
|
374
|
+
aria-describedby={[`${id}-name-hint`, errorId].filter(Boolean).join(' ')}
|
|
375
|
+
/>
|
|
376
|
+
</Field>
|
|
377
|
+
)}
|
|
378
|
+
</FormFrame>
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* A new person to be a lead's contact, as a step of the lead's form: their given and family name and email, made
|
|
384
|
+
* when the lead is saved. `Use this` goes back to the lead with them chosen.
|
|
385
|
+
*/
|
|
386
|
+
export function NewContactStep({
|
|
387
|
+
initial,
|
|
388
|
+
onUse,
|
|
389
|
+
onBack,
|
|
390
|
+
framed,
|
|
391
|
+
text,
|
|
392
|
+
}: {
|
|
393
|
+
initial?: NewContact | undefined;
|
|
394
|
+
/** False inside another card, such as an open lead's. */
|
|
395
|
+
framed?: boolean | undefined;
|
|
396
|
+
onUse: (contact: NewContact) => Promise<unknown> | void;
|
|
397
|
+
onBack: () => void;
|
|
398
|
+
text?: Partial<CrmText> | undefined;
|
|
399
|
+
}) {
|
|
400
|
+
const words = withText(text);
|
|
401
|
+
return (
|
|
402
|
+
<ContactForm
|
|
403
|
+
initial={initial}
|
|
404
|
+
title={words.newContact}
|
|
405
|
+
hint={words.newContactHint}
|
|
406
|
+
submit={words.useThis}
|
|
407
|
+
submitting={words.saving}
|
|
408
|
+
back={{ label: words.back, onBack }}
|
|
409
|
+
framed={framed}
|
|
410
|
+
onSave={onUse}
|
|
411
|
+
text={text}
|
|
412
|
+
/>
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* A new person, such as one of a customer company's people: their given and family name and email. `onCreate` makes
|
|
418
|
+
* them (`CrmStore.createContact`); the form stays as it was if that fails, so it can be sent again.
|
|
419
|
+
*/
|
|
420
|
+
export function NewContactForm({
|
|
421
|
+
onCreate,
|
|
422
|
+
text,
|
|
423
|
+
}: {
|
|
424
|
+
onCreate: (contact: NewContact) => Promise<unknown>;
|
|
425
|
+
text?: Partial<CrmText> | undefined;
|
|
426
|
+
}) {
|
|
427
|
+
const words = withText(text);
|
|
428
|
+
return (
|
|
429
|
+
<ContactForm
|
|
430
|
+
title={words.addPerson}
|
|
431
|
+
submit={words.addPerson}
|
|
432
|
+
submitting={words.creating}
|
|
433
|
+
// Shown in the host's card, such as a customer's people.
|
|
434
|
+
framed={false}
|
|
435
|
+
onSave={onCreate}
|
|
436
|
+
text={text}
|
|
437
|
+
/>
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function ContactForm({
|
|
442
|
+
initial,
|
|
443
|
+
title,
|
|
444
|
+
hint,
|
|
445
|
+
submit,
|
|
446
|
+
submitting,
|
|
447
|
+
back,
|
|
448
|
+
framed,
|
|
449
|
+
onSave,
|
|
450
|
+
text,
|
|
451
|
+
}: {
|
|
452
|
+
initial?: NewContact | undefined;
|
|
453
|
+
title: string;
|
|
454
|
+
hint?: string;
|
|
455
|
+
submit: string;
|
|
456
|
+
submitting: string;
|
|
457
|
+
back?: { label: string; onBack: () => void };
|
|
458
|
+
framed?: boolean | undefined;
|
|
459
|
+
onSave: (contact: NewContact) => Promise<unknown> | void;
|
|
460
|
+
text?: Partial<CrmText> | undefined;
|
|
461
|
+
}) {
|
|
462
|
+
const words = withText(text);
|
|
463
|
+
const id = useId();
|
|
464
|
+
const [givenName, setGivenName] = useState(initial?.givenName ?? '');
|
|
465
|
+
const [familyName, setFamilyName] = useState(initial?.familyName ?? '');
|
|
466
|
+
const [email, setEmail] = useState(initial?.email ?? '');
|
|
467
|
+
const [telephone, setTelephone] = useState(initial?.telephone ?? '');
|
|
468
|
+
const { saving, error, setError, run } = useSave();
|
|
469
|
+
return (
|
|
470
|
+
<FormFrame
|
|
471
|
+
title={title}
|
|
472
|
+
submit={submit}
|
|
473
|
+
submitting={submitting}
|
|
474
|
+
saving={saving}
|
|
475
|
+
error={error}
|
|
476
|
+
back={back}
|
|
477
|
+
framed={framed}
|
|
478
|
+
onSubmit={async () => {
|
|
479
|
+
if (!givenName.trim()) return setError(words.givenNameRequired);
|
|
480
|
+
const contact: NewContact = {
|
|
481
|
+
givenName: givenName.trim(),
|
|
482
|
+
...(familyName.trim() ? { familyName: familyName.trim() } : {}),
|
|
483
|
+
...(email.trim() ? { email: email.trim() } : {}),
|
|
484
|
+
...(telephone.trim() ? { telephone: telephone.trim() } : {}),
|
|
485
|
+
};
|
|
486
|
+
await run(async () => onSave(contact));
|
|
487
|
+
}}
|
|
488
|
+
>
|
|
489
|
+
{({ error: errorId }) => (
|
|
490
|
+
<>
|
|
491
|
+
{hint && <p className="text-muted-foreground text-sm">{hint}</p>}
|
|
492
|
+
<Field id={`${id}-given`} label={words.givenName}>
|
|
493
|
+
<Input
|
|
494
|
+
id={`${id}-given`}
|
|
495
|
+
autoComplete="off"
|
|
496
|
+
value={givenName}
|
|
497
|
+
onChange={(event) => setGivenName(event.target.value)}
|
|
498
|
+
aria-describedby={errorId}
|
|
499
|
+
/>
|
|
500
|
+
</Field>
|
|
501
|
+
<Field id={`${id}-family`} label={words.familyName}>
|
|
502
|
+
<Input
|
|
503
|
+
id={`${id}-family`}
|
|
504
|
+
autoComplete="off"
|
|
505
|
+
value={familyName}
|
|
506
|
+
onChange={(event) => setFamilyName(event.target.value)}
|
|
507
|
+
/>
|
|
508
|
+
</Field>
|
|
509
|
+
<Field id={`${id}-email`} label={words.email}>
|
|
510
|
+
<Input
|
|
511
|
+
id={`${id}-email`}
|
|
512
|
+
type="email"
|
|
513
|
+
autoComplete="off"
|
|
514
|
+
value={email}
|
|
515
|
+
onChange={(event) => setEmail(event.target.value)}
|
|
516
|
+
/>
|
|
517
|
+
</Field>
|
|
518
|
+
<Field id={`${id}-telephone`} label={words.telephone}>
|
|
519
|
+
<Input
|
|
520
|
+
id={`${id}-telephone`}
|
|
521
|
+
type="tel"
|
|
522
|
+
autoComplete="off"
|
|
523
|
+
value={telephone}
|
|
524
|
+
onChange={(event) => setTelephone(event.target.value)}
|
|
525
|
+
/>
|
|
526
|
+
</Field>
|
|
527
|
+
</>
|
|
528
|
+
)}
|
|
529
|
+
</FormFrame>
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** A contact's name for showing while they're waiting to be made. */
|
|
534
|
+
export const draftName = (contact: NewContact) => [contact.givenName, contact.familyName].filter(Boolean).join(' ');
|
|
535
|
+
|
|
536
|
+
/** Runs a form's save: the problem in words when it fails, the form left as it was so it can be sent again. */
|
|
537
|
+
function useSave() {
|
|
538
|
+
const [saving, setSaving] = useState(false);
|
|
539
|
+
const [error, setError] = useState<string>();
|
|
540
|
+
const run = async (save: () => Promise<unknown>) => {
|
|
541
|
+
setError(undefined);
|
|
542
|
+
setSaving(true);
|
|
543
|
+
try {
|
|
544
|
+
await save();
|
|
545
|
+
} catch (failure) {
|
|
546
|
+
setError(failure instanceof Error ? failure.message : String(failure));
|
|
547
|
+
} finally {
|
|
548
|
+
setSaving(false);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
return { saving, error, setError, run };
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* A new lead: its title, source, value, assignee, and its company and contact person (a lead holds its business by
|
|
556
|
+
* its company). Each is chosen from the host's, or added: "Add a new ..." opens a step for its details in the same
|
|
557
|
+
* form, and it's made when the lead is saved.
|
|
558
|
+
*/
|
|
559
|
+
export function NewLeadForm({
|
|
560
|
+
owner,
|
|
561
|
+
companies = [],
|
|
562
|
+
people = [],
|
|
563
|
+
team = [],
|
|
564
|
+
currencyCode = 'GBP',
|
|
565
|
+
onCreate,
|
|
566
|
+
text,
|
|
567
|
+
}: FormProps & {
|
|
568
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
569
|
+
/** People to choose as the contact: the host's customers' people. */
|
|
570
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
571
|
+
/** People to assign the lead to: the host's team. */
|
|
572
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
573
|
+
currencyCode?: string;
|
|
574
|
+
onCreate: (input: LeadInput, options: NewLeadOptions) => Promise<unknown>;
|
|
575
|
+
}) {
|
|
576
|
+
const words = withText(text);
|
|
577
|
+
const id = useId();
|
|
578
|
+
const [step, setStep] = useState<'lead' | 'company' | 'contact'>('lead');
|
|
579
|
+
const [title, setTitle] = useState('');
|
|
580
|
+
const [source, setSource] = useState<LeadSource>('WEBSITE');
|
|
581
|
+
const [value, setValue] = useState('');
|
|
582
|
+
const [currency, setCurrency] = useState(currencyCode);
|
|
583
|
+
const [company, setCompany] = useState('');
|
|
584
|
+
const [newCompany, setNewCompany] = useState<string>();
|
|
585
|
+
const [contact, setContact] = useState('');
|
|
586
|
+
const [newContact, setNewContact] = useState<NewContact>();
|
|
587
|
+
const [assignee, setAssignee] = useState('');
|
|
588
|
+
const { saving, error, setError, run } = useSave();
|
|
589
|
+
const submit = async () => {
|
|
590
|
+
const targetValue = moneyOf(value, currency);
|
|
591
|
+
if (!title.trim()) return setError(words.titleRequired);
|
|
592
|
+
if (targetValue === undefined) return setError(words.invalidAmount);
|
|
593
|
+
await run(() =>
|
|
594
|
+
onCreate(
|
|
595
|
+
{
|
|
596
|
+
owner: ref(owner),
|
|
597
|
+
title: title.trim(),
|
|
598
|
+
source,
|
|
599
|
+
status: 'NEW',
|
|
600
|
+
outcome: 'IN_PROGRESS',
|
|
601
|
+
contactCompany: company ? { urn: company } : null,
|
|
602
|
+
contactIdentity: contact ? { urn: contact } : null,
|
|
603
|
+
assignee: assignee ? { urn: assignee } : null,
|
|
604
|
+
targetValue,
|
|
605
|
+
},
|
|
606
|
+
{ ...(newCompany ? { companyName: newCompany } : {}), ...(newContact ? { contact: newContact } : {}) },
|
|
607
|
+
),
|
|
608
|
+
);
|
|
609
|
+
};
|
|
610
|
+
if (step === 'company')
|
|
611
|
+
return (
|
|
612
|
+
<NewCompanyStep
|
|
613
|
+
initial={newCompany}
|
|
614
|
+
text={text}
|
|
615
|
+
onBack={() => setStep('lead')}
|
|
616
|
+
onUse={(name) => {
|
|
617
|
+
setCompany('');
|
|
618
|
+
setNewCompany(name);
|
|
619
|
+
setStep('lead');
|
|
620
|
+
}}
|
|
621
|
+
/>
|
|
622
|
+
);
|
|
623
|
+
if (step === 'contact')
|
|
624
|
+
return (
|
|
625
|
+
<NewContactStep
|
|
626
|
+
initial={newContact}
|
|
627
|
+
text={text}
|
|
628
|
+
onBack={() => setStep('lead')}
|
|
629
|
+
onUse={(made) => {
|
|
630
|
+
setContact('');
|
|
631
|
+
setNewContact(made);
|
|
632
|
+
setStep('lead');
|
|
633
|
+
}}
|
|
634
|
+
/>
|
|
635
|
+
);
|
|
636
|
+
return (
|
|
637
|
+
<FormFrame
|
|
638
|
+
title={words.newLead}
|
|
639
|
+
submit={words.createLead}
|
|
640
|
+
submitting={words.creating}
|
|
641
|
+
saving={saving}
|
|
642
|
+
error={error}
|
|
643
|
+
onSubmit={submit}
|
|
644
|
+
>
|
|
645
|
+
{({ error: errorId }) => (
|
|
646
|
+
<>
|
|
647
|
+
<Field id={`${id}-title`} label={words.title}>
|
|
648
|
+
<Input
|
|
649
|
+
id={`${id}-title`}
|
|
650
|
+
value={title}
|
|
651
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
652
|
+
aria-describedby={errorId}
|
|
653
|
+
/>
|
|
654
|
+
</Field>
|
|
655
|
+
<Field id={`${id}-source`} label={words.source}>
|
|
656
|
+
<Select value={source} onValueChange={(next) => setSource(next as LeadSource)}>
|
|
657
|
+
<SelectTrigger id={`${id}-source`} className="w-full">
|
|
658
|
+
<SelectValue />
|
|
659
|
+
</SelectTrigger>
|
|
660
|
+
<SelectContent>
|
|
661
|
+
{LEAD_SOURCES.map((option) => (
|
|
662
|
+
<SelectItem key={option} value={option}>
|
|
663
|
+
{SOURCE_LABELS[option]}
|
|
664
|
+
</SelectItem>
|
|
665
|
+
))}
|
|
666
|
+
</SelectContent>
|
|
667
|
+
</Select>
|
|
668
|
+
</Field>
|
|
669
|
+
<Field id={`${id}-value`} label={words.value}>
|
|
670
|
+
<MoneyInput
|
|
671
|
+
id={`${id}-value`}
|
|
672
|
+
amount={value}
|
|
673
|
+
currencyCode={currency}
|
|
674
|
+
onAmount={setValue}
|
|
675
|
+
onCurrency={setCurrency}
|
|
676
|
+
text={text}
|
|
677
|
+
/>
|
|
678
|
+
</Field>
|
|
679
|
+
<Field id={`${id}-company`} label={words.company}>
|
|
680
|
+
<ChoiceSelect
|
|
681
|
+
id={`${id}-company`}
|
|
682
|
+
value={company}
|
|
683
|
+
onChange={(urn) => {
|
|
684
|
+
setCompany(urn);
|
|
685
|
+
setNewCompany(undefined);
|
|
686
|
+
}}
|
|
687
|
+
choices={companies}
|
|
688
|
+
none={words.noCompany}
|
|
689
|
+
add={{ label: words.addCompany, onAdd: () => setStep('company') }}
|
|
690
|
+
pending={newCompany}
|
|
691
|
+
text={text}
|
|
692
|
+
/>
|
|
693
|
+
</Field>
|
|
694
|
+
<Field id={`${id}-contact`} label={words.contact}>
|
|
695
|
+
<ChoiceSelect
|
|
696
|
+
id={`${id}-contact`}
|
|
697
|
+
value={contact}
|
|
698
|
+
onChange={(urn) => {
|
|
699
|
+
setContact(urn);
|
|
700
|
+
setNewContact(undefined);
|
|
701
|
+
}}
|
|
702
|
+
choices={people}
|
|
703
|
+
none={words.noContact}
|
|
704
|
+
add={{ label: words.addContact, onAdd: () => setStep('contact') }}
|
|
705
|
+
pending={newContact && draftName(newContact)}
|
|
706
|
+
text={text}
|
|
707
|
+
/>
|
|
708
|
+
</Field>
|
|
709
|
+
<Field id={`${id}-assignee`} label={words.assignee}>
|
|
710
|
+
<ChoiceSelect
|
|
711
|
+
id={`${id}-assignee`}
|
|
712
|
+
value={assignee}
|
|
713
|
+
onChange={setAssignee}
|
|
714
|
+
choices={team}
|
|
715
|
+
none={words.unassigned}
|
|
716
|
+
text={text}
|
|
717
|
+
/>
|
|
718
|
+
</Field>
|
|
719
|
+
</>
|
|
720
|
+
)}
|
|
721
|
+
</FormFrame>
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Where a deal is: its workflow (any workflow can be a pipeline) and its stage in it. Choosing a workflow moves the
|
|
727
|
+
* deal to that workflow's first stage.
|
|
728
|
+
*/
|
|
729
|
+
export function PipelineStageFields({
|
|
730
|
+
id,
|
|
731
|
+
pipelines,
|
|
732
|
+
stage,
|
|
733
|
+
onStage,
|
|
734
|
+
disabled,
|
|
735
|
+
text,
|
|
736
|
+
}: {
|
|
737
|
+
id: string;
|
|
738
|
+
pipelines: readonly Pipeline[];
|
|
739
|
+
/** The deal's stage URN, or "" for none. */
|
|
740
|
+
stage: string;
|
|
741
|
+
onStage: (stage: PipelineStage) => void;
|
|
742
|
+
disabled?: boolean | undefined;
|
|
743
|
+
text?: Partial<CrmText> | undefined;
|
|
744
|
+
}) {
|
|
745
|
+
const words = withText(text);
|
|
746
|
+
const pipeline =
|
|
747
|
+
pipelines.find((candidate) => candidate.stages?.some((option) => option.urn === stage)) ?? pipelines[0];
|
|
748
|
+
const stages = pipeline ? orderedStages(pipeline) : [];
|
|
749
|
+
return (
|
|
750
|
+
<>
|
|
751
|
+
{pipelines.length > 1 && (
|
|
752
|
+
<Field id={`${id}-workflow`} label={words.workflow}>
|
|
753
|
+
<Select
|
|
754
|
+
value={pipeline?.urn ?? ''}
|
|
755
|
+
disabled={disabled ?? false}
|
|
756
|
+
onValueChange={(urn) => {
|
|
757
|
+
const first = orderedStages(pipelines.find((candidate) => candidate.urn === urn)!)[0];
|
|
758
|
+
if (first) onStage(first);
|
|
759
|
+
}}
|
|
760
|
+
>
|
|
761
|
+
<SelectTrigger id={`${id}-workflow`} className="w-full">
|
|
762
|
+
<SelectValue />
|
|
763
|
+
</SelectTrigger>
|
|
764
|
+
<SelectContent>
|
|
765
|
+
{pipelines.map((option) => (
|
|
766
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
767
|
+
{option.name}
|
|
768
|
+
</SelectItem>
|
|
769
|
+
))}
|
|
770
|
+
</SelectContent>
|
|
771
|
+
</Select>
|
|
772
|
+
</Field>
|
|
773
|
+
)}
|
|
774
|
+
<Field id={`${id}-stage`} label={words.stage}>
|
|
775
|
+
<Select
|
|
776
|
+
value={stages.some((option) => option.urn === stage) ? stage : ''}
|
|
777
|
+
disabled={disabled ?? false}
|
|
778
|
+
onValueChange={(urn) => onStage(stages.find((option) => option.urn === urn)!)}
|
|
779
|
+
>
|
|
780
|
+
<SelectTrigger id={`${id}-stage`} className="w-full">
|
|
781
|
+
<SelectValue />
|
|
782
|
+
</SelectTrigger>
|
|
783
|
+
<SelectContent>
|
|
784
|
+
{stages.map((option) => (
|
|
785
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
786
|
+
{option.name}
|
|
787
|
+
</SelectItem>
|
|
788
|
+
))}
|
|
789
|
+
</SelectContent>
|
|
790
|
+
</Select>
|
|
791
|
+
</Field>
|
|
792
|
+
</>
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* A new deal that didn't start as a lead: its title, companies and contacts (found by searching), value, workflow and stage (the first
|
|
798
|
+
* workflow's first stage, to start with), expected close, probability and assignee.
|
|
799
|
+
*/
|
|
800
|
+
export function NewDealForm({
|
|
801
|
+
owner,
|
|
802
|
+
pipelines,
|
|
803
|
+
companies = [],
|
|
804
|
+
people = [],
|
|
805
|
+
team = [],
|
|
806
|
+
currencyCode = 'GBP',
|
|
807
|
+
now = new Date(),
|
|
808
|
+
onCreate,
|
|
809
|
+
text,
|
|
810
|
+
}: FormProps & {
|
|
811
|
+
/** The workflows a deal can be in, the first chosen to start with. */
|
|
812
|
+
pipelines: readonly Pipeline[];
|
|
813
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
814
|
+
/** People to choose as the contact: the host's customers' people. */
|
|
815
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
816
|
+
/** People to assign the deal to: the host's team. */
|
|
817
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
818
|
+
currencyCode?: string;
|
|
819
|
+
now?: Date;
|
|
820
|
+
onCreate: (input: DealInput) => Promise<unknown>;
|
|
821
|
+
}) {
|
|
822
|
+
const words = withText(text);
|
|
823
|
+
const id = useId();
|
|
824
|
+
const [title, setTitle] = useState('');
|
|
825
|
+
const [chosenCompanies, setCompanies] = useState<string[]>([]);
|
|
826
|
+
const [contacts, setContacts] = useState<string[]>([]);
|
|
827
|
+
const [assignee, setAssignee] = useState('');
|
|
828
|
+
const [value, setValue] = useState('');
|
|
829
|
+
const [currency, setCurrency] = useState(currencyCode);
|
|
830
|
+
const [stage, setStage] = useState(pipelines[0] ? (orderedStages(pipelines[0])[0]?.urn ?? '') : '');
|
|
831
|
+
const [close, setClose] = useState(inDays(now, 30));
|
|
832
|
+
const [probability, setProbability] = useState('');
|
|
833
|
+
const { saving, error, setError, run } = useSave();
|
|
834
|
+
const submit = async () => {
|
|
835
|
+
const targetValue = moneyOf(value, currency);
|
|
836
|
+
const chance = probability.trim() ? Number(probability) : undefined;
|
|
837
|
+
if (!title.trim()) return setError(words.titleRequired);
|
|
838
|
+
if (targetValue === undefined) return setError(words.invalidAmount);
|
|
839
|
+
if (chance !== undefined && !(chance >= 0 && chance <= 100)) return setError(words.invalidProbability);
|
|
840
|
+
await run(() =>
|
|
841
|
+
onCreate({
|
|
842
|
+
owner: ref(owner),
|
|
843
|
+
title: title.trim(),
|
|
844
|
+
contactCompanies: chosenCompanies.map((urn) => ({ urn })),
|
|
845
|
+
contactIdentities: contacts.map((urn) => ({ urn })),
|
|
846
|
+
workflowStage: stage ? { urn: stage } : null,
|
|
847
|
+
outcome: 'OPEN',
|
|
848
|
+
probability: chance ?? null,
|
|
849
|
+
targetValue,
|
|
850
|
+
targetCloseTimestamp: dayTimestamp(close),
|
|
851
|
+
assignee: assignee ? { urn: assignee } : null,
|
|
852
|
+
invoices: [],
|
|
853
|
+
}),
|
|
854
|
+
);
|
|
855
|
+
};
|
|
856
|
+
return (
|
|
857
|
+
<FormFrame
|
|
858
|
+
title={words.newDeal}
|
|
859
|
+
submit={words.createDeal}
|
|
860
|
+
submitting={words.creating}
|
|
861
|
+
saving={saving}
|
|
862
|
+
error={error}
|
|
863
|
+
onSubmit={submit}
|
|
864
|
+
>
|
|
865
|
+
{({ error: errorId }) => (
|
|
866
|
+
<>
|
|
867
|
+
<Field id={`${id}-title`} label={words.title}>
|
|
868
|
+
<Input
|
|
869
|
+
id={`${id}-title`}
|
|
870
|
+
value={title}
|
|
871
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
872
|
+
aria-describedby={errorId}
|
|
873
|
+
/>
|
|
874
|
+
</Field>
|
|
875
|
+
<Field id={`${id}-companies`} label={words.companies}>
|
|
876
|
+
<ChoiceCombobox
|
|
877
|
+
id={`${id}-companies`}
|
|
878
|
+
value={chosenCompanies}
|
|
879
|
+
onChange={setCompanies}
|
|
880
|
+
choices={companies}
|
|
881
|
+
placeholder={words.searchCompanies}
|
|
882
|
+
text={text}
|
|
883
|
+
/>
|
|
884
|
+
</Field>
|
|
885
|
+
<Field id={`${id}-contacts`} label={words.contacts}>
|
|
886
|
+
<ChoiceCombobox
|
|
887
|
+
id={`${id}-contacts`}
|
|
888
|
+
value={contacts}
|
|
889
|
+
onChange={setContacts}
|
|
890
|
+
choices={people}
|
|
891
|
+
placeholder={words.searchContacts}
|
|
892
|
+
text={text}
|
|
893
|
+
/>
|
|
894
|
+
</Field>
|
|
895
|
+
<Field id={`${id}-value`} label={words.value}>
|
|
896
|
+
<MoneyInput
|
|
897
|
+
id={`${id}-value`}
|
|
898
|
+
amount={value}
|
|
899
|
+
currencyCode={currency}
|
|
900
|
+
onAmount={setValue}
|
|
901
|
+
onCurrency={setCurrency}
|
|
902
|
+
text={text}
|
|
903
|
+
/>
|
|
904
|
+
</Field>
|
|
905
|
+
<PipelineStageFields
|
|
906
|
+
id={id}
|
|
907
|
+
pipelines={pipelines}
|
|
908
|
+
stage={stage}
|
|
909
|
+
onStage={(next) => setStage(next.urn)}
|
|
910
|
+
text={text}
|
|
911
|
+
/>
|
|
912
|
+
<Field id={`${id}-assignee`} label={words.assignee}>
|
|
913
|
+
<ChoiceSelect
|
|
914
|
+
id={`${id}-assignee`}
|
|
915
|
+
value={assignee}
|
|
916
|
+
onChange={setAssignee}
|
|
917
|
+
choices={team}
|
|
918
|
+
none={words.unassigned}
|
|
919
|
+
text={text}
|
|
920
|
+
/>
|
|
921
|
+
</Field>
|
|
922
|
+
<Field id={`${id}-close`} label={words.expectedClose}>
|
|
923
|
+
<Input id={`${id}-close`} type="date" value={close} onChange={(event) => setClose(event.target.value)} />
|
|
924
|
+
</Field>
|
|
925
|
+
<Field id={`${id}-probability`} label={`${words.probability} (%)`}>
|
|
926
|
+
<Input
|
|
927
|
+
id={`${id}-probability`}
|
|
928
|
+
inputMode="numeric"
|
|
929
|
+
value={probability}
|
|
930
|
+
onChange={(event) => setProbability(event.target.value)}
|
|
931
|
+
/>
|
|
932
|
+
</Field>
|
|
933
|
+
</>
|
|
934
|
+
)}
|
|
935
|
+
</FormFrame>
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
const TAXABILITY_LABELS: Record<Taxability, string> = {
|
|
940
|
+
TAXABLE: 'Taxable',
|
|
941
|
+
PARTIALLY_TAXABLE: 'Partly taxable',
|
|
942
|
+
NON_TAXABLE: 'Not taxable',
|
|
943
|
+
TAX_EXEMPT: 'Exempt',
|
|
944
|
+
NOT_APPLICABLE: 'Not applicable',
|
|
945
|
+
};
|
|
946
|
+
const TAXABILITIES = Object.keys(TAXABILITY_LABELS) as Taxability[];
|
|
947
|
+
/** Taxed at the invoice's rate; the others have no tax. */
|
|
948
|
+
const TAXED: readonly Taxability[] = ['TAXABLE', 'PARTIALLY_TAXABLE'];
|
|
949
|
+
|
|
950
|
+
interface LineRow {
|
|
951
|
+
description: string;
|
|
952
|
+
quantity: string;
|
|
953
|
+
unitPrice: string;
|
|
954
|
+
itemCode: string;
|
|
955
|
+
discountRate: string;
|
|
956
|
+
taxability: Taxability;
|
|
957
|
+
taxName: string;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
const EMPTY_LINE: LineRow = {
|
|
961
|
+
description: '',
|
|
962
|
+
quantity: '1',
|
|
963
|
+
unitPrice: '',
|
|
964
|
+
itemCode: '',
|
|
965
|
+
discountRate: '',
|
|
966
|
+
taxability: 'TAXABLE',
|
|
967
|
+
taxName: '',
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* What a line, typed in, comes to, in minor units: before discount, the discount, its net price and tax. Undefined
|
|
972
|
+
* when it isn't complete or its discount isn't 0 to 100.
|
|
973
|
+
*/
|
|
974
|
+
function priceLine(line: LineRow, currencyCode: string, rate: number) {
|
|
975
|
+
const quantity = Number(line.quantity);
|
|
976
|
+
const unit = minorOrUndefined(line.unitPrice, currencyCode);
|
|
977
|
+
const discountRate = line.discountRate.trim() ? Number(line.discountRate) : 0;
|
|
978
|
+
if (!line.description.trim() || !(quantity > 0) || !unit || !(discountRate >= 0 && discountRate <= 100))
|
|
979
|
+
return undefined;
|
|
980
|
+
const gross = Math.round(unit * quantity);
|
|
981
|
+
const discount = Math.round((gross * discountRate) / 100);
|
|
982
|
+
const price = gross - discount;
|
|
983
|
+
const tax = TAXED.includes(line.taxability) ? Math.round((price * rate) / 100) : 0;
|
|
984
|
+
return { line, quantity, unit, discountRate, discount, price, tax };
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/** The line as the API takes it: its unit price in major units, its amounts as money. */
|
|
988
|
+
function lineDraft(priced: NonNullable<ReturnType<typeof priceLine>>, code: string): InvoiceLineDraft {
|
|
989
|
+
const { line } = priced;
|
|
990
|
+
return {
|
|
991
|
+
description: line.description.trim(),
|
|
992
|
+
quantity: priced.quantity,
|
|
993
|
+
unitPrice: priced.unit / 10 ** fractionDigits(code),
|
|
994
|
+
price: fromMinor(priced.price, code),
|
|
995
|
+
tax: fromMinor(priced.tax, code),
|
|
996
|
+
taxability: line.taxability,
|
|
997
|
+
...(line.itemCode.trim() ? { itemCode: line.itemCode.trim() } : {}),
|
|
998
|
+
...(priced.discountRate
|
|
999
|
+
? { discountRate: priced.discountRate, discountPrice: fromMinor(priced.discount, code) }
|
|
1000
|
+
: {}),
|
|
1001
|
+
...(line.taxName.trim() ? { taxName: line.taxName.trim() } : {}),
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* One invoice line with all its fields, as a step of the invoice form: description, item code, quantity, unit price,
|
|
1007
|
+
* discount, tax treatment and tax name, with what it comes to. `Done` goes back to the invoice with it kept.
|
|
1008
|
+
*/
|
|
1009
|
+
function InvoiceLineStep({
|
|
1010
|
+
number,
|
|
1011
|
+
initial,
|
|
1012
|
+
currencyCode,
|
|
1013
|
+
rate,
|
|
1014
|
+
onDone,
|
|
1015
|
+
onBack,
|
|
1016
|
+
text,
|
|
1017
|
+
}: {
|
|
1018
|
+
number: number;
|
|
1019
|
+
initial: LineRow;
|
|
1020
|
+
currencyCode: string;
|
|
1021
|
+
rate: number;
|
|
1022
|
+
onDone: (line: LineRow) => void;
|
|
1023
|
+
onBack: () => void;
|
|
1024
|
+
text?: Partial<CrmText> | undefined;
|
|
1025
|
+
}) {
|
|
1026
|
+
const words = withText(text);
|
|
1027
|
+
const id = useId();
|
|
1028
|
+
const [line, setLine] = useState(initial);
|
|
1029
|
+
const { saving, error, setError, run } = useSave();
|
|
1030
|
+
const set = (field: keyof LineRow, value: string) => setLine((current) => ({ ...current, [field]: value }));
|
|
1031
|
+
const priced = priceLine(line, currencyCode, Number.isFinite(rate) ? rate : 0);
|
|
1032
|
+
const money = (minor: number) => formatMoney(fromMinor(minor, currencyCode));
|
|
1033
|
+
const input = (field: Exclude<keyof LineRow, 'taxability'>, label: string, inputMode?: 'decimal') => (
|
|
1034
|
+
<Field id={`${id}-${field}`} label={label}>
|
|
1035
|
+
<Input
|
|
1036
|
+
id={`${id}-${field}`}
|
|
1037
|
+
inputMode={inputMode}
|
|
1038
|
+
value={line[field]}
|
|
1039
|
+
onChange={(event) => set(field, event.target.value)}
|
|
1040
|
+
/>
|
|
1041
|
+
</Field>
|
|
1042
|
+
);
|
|
1043
|
+
return (
|
|
1044
|
+
<FormFrame
|
|
1045
|
+
title={`${words.line} ${number}`}
|
|
1046
|
+
submit={words.done}
|
|
1047
|
+
submitting={words.saving}
|
|
1048
|
+
saving={saving}
|
|
1049
|
+
error={error}
|
|
1050
|
+
back={{ label: words.back, onBack }}
|
|
1051
|
+
onSubmit={async () => {
|
|
1052
|
+
const discount = line.discountRate.trim() ? Number(line.discountRate) : 0;
|
|
1053
|
+
if (!(discount >= 0 && discount <= 100)) return setError(words.invalidDiscount);
|
|
1054
|
+
await run(async () => onDone(line));
|
|
1055
|
+
}}
|
|
1056
|
+
>
|
|
1057
|
+
{() => (
|
|
1058
|
+
<>
|
|
1059
|
+
{input('description', words.description)}
|
|
1060
|
+
{input('itemCode', words.itemCode)}
|
|
1061
|
+
<div className="grid grid-cols-2 gap-3">
|
|
1062
|
+
{input('quantity', words.quantity, 'decimal')}
|
|
1063
|
+
{input('unitPrice', `${words.unitPrice} (${currencyCode})`, 'decimal')}
|
|
1064
|
+
{input('discountRate', words.discountRate, 'decimal')}
|
|
1065
|
+
<Field id={`${id}-taxability`} label={words.taxability}>
|
|
1066
|
+
<Select value={line.taxability} onValueChange={(next) => set('taxability', next)}>
|
|
1067
|
+
<SelectTrigger id={`${id}-taxability`} className="w-full">
|
|
1068
|
+
<SelectValue />
|
|
1069
|
+
</SelectTrigger>
|
|
1070
|
+
<SelectContent>
|
|
1071
|
+
{TAXABILITIES.map((option) => (
|
|
1072
|
+
<SelectItem key={option} value={option}>
|
|
1073
|
+
{TAXABILITY_LABELS[option]}
|
|
1074
|
+
</SelectItem>
|
|
1075
|
+
))}
|
|
1076
|
+
</SelectContent>
|
|
1077
|
+
</Select>
|
|
1078
|
+
</Field>
|
|
1079
|
+
</div>
|
|
1080
|
+
{input('taxName', words.taxName)}
|
|
1081
|
+
<dl aria-live="polite" className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 text-sm">
|
|
1082
|
+
<dt className="text-muted-foreground">{words.price}</dt>
|
|
1083
|
+
<dd className="tabular-nums">{priced ? money(priced.price) : '-'}</dd>
|
|
1084
|
+
<dt className="text-muted-foreground">{words.tax}</dt>
|
|
1085
|
+
<dd className="tabular-nums">{priced ? money(priced.tax) : '-'}</dd>
|
|
1086
|
+
</dl>
|
|
1087
|
+
</>
|
|
1088
|
+
)}
|
|
1089
|
+
</FormFrame>
|
|
1090
|
+
);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* A party's company and person, as the invoice form picks them: an existing company or a new one by name, and an
|
|
1095
|
+
* existing person or a new one.
|
|
1096
|
+
*/
|
|
1097
|
+
interface PartyDraft {
|
|
1098
|
+
company: string;
|
|
1099
|
+
identity: string;
|
|
1100
|
+
newCompany?: string | undefined;
|
|
1101
|
+
newContact?: NewContact | undefined;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
const recipientKey = (owner: string) => `baack-crm:invoice-recipient:${owner}`;
|
|
1105
|
+
|
|
1106
|
+
/** The recipient last used for this owner's invoices, kept in the browser; else the owner itself. */
|
|
1107
|
+
function lastRecipient(owner: string): PartyDraft {
|
|
1108
|
+
try {
|
|
1109
|
+
const saved = JSON.parse(localStorage.getItem(recipientKey(owner)) ?? 'null') as Partial<PartyDraft> | null;
|
|
1110
|
+
if (saved && typeof saved.company === 'string')
|
|
1111
|
+
return { company: saved.company, identity: typeof saved.identity === 'string' ? saved.identity : '' };
|
|
1112
|
+
} catch {
|
|
1113
|
+
// No storage: the owner, as on first use.
|
|
1114
|
+
}
|
|
1115
|
+
return { company: owner, identity: '' };
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
function keepRecipient(owner: string, party: PartyDraft) {
|
|
1119
|
+
try {
|
|
1120
|
+
localStorage.setItem(recipientKey(owner), JSON.stringify({ company: party.company, identity: party.identity }));
|
|
1121
|
+
} catch {
|
|
1122
|
+
// Not kept; next time starts from the owner.
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
const partyRefs = (party: PartyDraft) => ({
|
|
1127
|
+
company: party.company ? { urn: party.company } : null,
|
|
1128
|
+
identity: party.identity ? { urn: party.identity } : null,
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* A new invoice: who pays it (the remitter) and who's paid (the recipient), each a company, a person or both, found by
|
|
1133
|
+
* searching; a company not in Baack yet can be added by name. The recipient starts as the one used last, else the
|
|
1134
|
+
* owner. An invoice can be a sale (a customer pays the owner) or a cost (the owner pays a supplier). Its amount is
|
|
1135
|
+
* lines, each opening into all its fields, or a total including tax, with a tax rate; then its dates, status, and
|
|
1136
|
+
* the deal it's for, if any.
|
|
1137
|
+
*/
|
|
1138
|
+
export function NewInvoiceForm({
|
|
1139
|
+
owner,
|
|
1140
|
+
companies = [],
|
|
1141
|
+
people = [],
|
|
1142
|
+
deals = [],
|
|
1143
|
+
currencyCode: defaultCurrency = 'GBP',
|
|
1144
|
+
now = new Date(),
|
|
1145
|
+
onCreate,
|
|
1146
|
+
text,
|
|
1147
|
+
}: FormProps & {
|
|
1148
|
+
/** Companies either party can be: the host's own companies (the owner and its child companies) and customers. */
|
|
1149
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
1150
|
+
/** People either party can be. */
|
|
1151
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
1152
|
+
/** Deals the invoice can be raised for. */
|
|
1153
|
+
deals?: readonly Deal[];
|
|
1154
|
+
currencyCode?: string;
|
|
1155
|
+
now?: Date;
|
|
1156
|
+
onCreate: (input: InvoiceInput, lines: InvoiceLineDraft[], options: NewInvoiceOptions) => Promise<unknown>;
|
|
1157
|
+
}) {
|
|
1158
|
+
const words = withText(text);
|
|
1159
|
+
const id = useId();
|
|
1160
|
+
const ownerUrn = ref(owner).urn;
|
|
1161
|
+
const [remitter, setRemitter] = useState<PartyDraft>({ company: '', identity: '' });
|
|
1162
|
+
const [recipient, setRecipient] = useState<PartyDraft>(() => lastRecipient(ownerUrn));
|
|
1163
|
+
// A party whose new company or person is being made, in a step of the form.
|
|
1164
|
+
const [adding, setAdding] = useState<{ which: 'remitter' | 'recipient'; record: 'company' | 'contact' }>();
|
|
1165
|
+
const [editing, setEditing] = useState<number>();
|
|
1166
|
+
const [deal, setDeal] = useState('');
|
|
1167
|
+
const [currencyCode, setCurrencyCode] = useState(defaultCurrency);
|
|
1168
|
+
const [status, setStatus] = useState<InvoiceStatus>('DRAFT');
|
|
1169
|
+
// Not issued yet, as drafts usually aren't: set when it's sent.
|
|
1170
|
+
const [issued, setIssued] = useState('');
|
|
1171
|
+
const [due, setDue] = useState(inDays(now, 30));
|
|
1172
|
+
const [rate, setRate] = useState('20');
|
|
1173
|
+
const [amountAs, setAmountAs] = useState<'lines' | 'total'>('lines');
|
|
1174
|
+
const [lines, setLines] = useState<LineRow[]>([EMPTY_LINE]);
|
|
1175
|
+
const [total, setTotal] = useState('');
|
|
1176
|
+
const { saving, error, setError, run } = useSave();
|
|
1177
|
+
const change = (index: number, field: keyof LineRow, value: string) =>
|
|
1178
|
+
setLines((current) => current.map((line, at) => (at === index ? { ...line, [field]: value } : line)));
|
|
1179
|
+
const hasParty = (party: PartyDraft) =>
|
|
1180
|
+
Boolean(party.company || party.identity || party.newCompany || party.newContact);
|
|
1181
|
+
const submit = async () => {
|
|
1182
|
+
const taxRate = Number(rate);
|
|
1183
|
+
const code = currencyCode;
|
|
1184
|
+
if (!hasParty(remitter)) return setError(words.remitterRequired);
|
|
1185
|
+
if (!hasParty(recipient)) return setError(words.recipientRequired);
|
|
1186
|
+
if (!(taxRate >= 0 && taxRate <= 100)) return setError(words.invalidRate);
|
|
1187
|
+
let done: InvoiceLineDraft[] = [];
|
|
1188
|
+
let subTotal: number;
|
|
1189
|
+
let tax: number;
|
|
1190
|
+
if (amountAs === 'total') {
|
|
1191
|
+
// A total includes tax: the subtotal and tax are worked out from it at the rate.
|
|
1192
|
+
const gross = minorOrUndefined(total, code);
|
|
1193
|
+
if (!gross) return setError(words.totalRequired);
|
|
1194
|
+
subTotal = Math.round(gross / (1 + taxRate / 100));
|
|
1195
|
+
tax = gross - subTotal;
|
|
1196
|
+
} else {
|
|
1197
|
+
const priced = lines.map((line) => priceLine(line, code, taxRate));
|
|
1198
|
+
if (priced.some((line) => line === undefined)) return setError(words.linesRequired);
|
|
1199
|
+
const complete = priced as NonNullable<(typeof priced)[number]>[];
|
|
1200
|
+
done = complete.map((line) => lineDraft(line, code));
|
|
1201
|
+
subTotal = complete.reduce((sum, line) => sum + line.price, 0);
|
|
1202
|
+
tax = complete.reduce((sum, line) => sum + line.tax, 0);
|
|
1203
|
+
}
|
|
1204
|
+
const linked = deals.find((candidate) => candidate.urn === deal);
|
|
1205
|
+
await run(async () => {
|
|
1206
|
+
await onCreate(
|
|
1207
|
+
{
|
|
1208
|
+
owner: ref(owner),
|
|
1209
|
+
remitter: partyRefs(remitter),
|
|
1210
|
+
recipient: partyRefs(recipient),
|
|
1211
|
+
status,
|
|
1212
|
+
currencyCode: code,
|
|
1213
|
+
// A total is given with its tax in it; lines are priced before tax.
|
|
1214
|
+
lineItemTax: !taxRate ? 'NO_TAX' : amountAs === 'total' ? 'INCLUSIVE' : 'EXCLUSIVE',
|
|
1215
|
+
subTotal: fromMinor(subTotal, code),
|
|
1216
|
+
tax: fromMinor(tax, code),
|
|
1217
|
+
total: fromMinor(subTotal + tax, code),
|
|
1218
|
+
paid: fromMinor(0, code),
|
|
1219
|
+
issuedTimestamp: dayTimestamp(issued),
|
|
1220
|
+
dueTimestamp: dayTimestamp(due),
|
|
1221
|
+
},
|
|
1222
|
+
done,
|
|
1223
|
+
{
|
|
1224
|
+
...(linked ? { deal: linked } : {}),
|
|
1225
|
+
...(remitter.newCompany ? { remitterCompanyName: remitter.newCompany } : {}),
|
|
1226
|
+
...(recipient.newCompany ? { recipientCompanyName: recipient.newCompany } : {}),
|
|
1227
|
+
...(remitter.newContact ? { remitterContact: remitter.newContact } : {}),
|
|
1228
|
+
...(recipient.newContact ? { recipientContact: recipient.newContact } : {}),
|
|
1229
|
+
},
|
|
1230
|
+
);
|
|
1231
|
+
// A new record's URN isn't known here, so only an existing recipient is kept for next time.
|
|
1232
|
+
if (!recipient.newCompany && !recipient.newContact) keepRecipient(ownerUrn, recipient);
|
|
1233
|
+
});
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
if (adding) {
|
|
1237
|
+
const party = adding.which === 'remitter' ? remitter : recipient;
|
|
1238
|
+
const setParty = adding.which === 'remitter' ? setRemitter : setRecipient;
|
|
1239
|
+
return adding.record === 'company' ? (
|
|
1240
|
+
<NewCompanyStep
|
|
1241
|
+
initial={party.newCompany ?? ''}
|
|
1242
|
+
text={text}
|
|
1243
|
+
onBack={() => setAdding(undefined)}
|
|
1244
|
+
onUse={(name) => {
|
|
1245
|
+
setParty({ ...party, company: '', newCompany: name });
|
|
1246
|
+
setAdding(undefined);
|
|
1247
|
+
}}
|
|
1248
|
+
/>
|
|
1249
|
+
) : (
|
|
1250
|
+
<NewContactStep
|
|
1251
|
+
initial={party.newContact}
|
|
1252
|
+
text={text}
|
|
1253
|
+
onBack={() => setAdding(undefined)}
|
|
1254
|
+
onUse={(contact) => {
|
|
1255
|
+
setParty({ ...party, identity: '', newContact: contact });
|
|
1256
|
+
setAdding(undefined);
|
|
1257
|
+
}}
|
|
1258
|
+
/>
|
|
1259
|
+
);
|
|
1260
|
+
}
|
|
1261
|
+
if (editing !== undefined && lines[editing]) {
|
|
1262
|
+
return (
|
|
1263
|
+
<InvoiceLineStep
|
|
1264
|
+
number={editing + 1}
|
|
1265
|
+
initial={lines[editing]}
|
|
1266
|
+
currencyCode={currencyCode}
|
|
1267
|
+
rate={Number(rate)}
|
|
1268
|
+
text={text}
|
|
1269
|
+
onBack={() => setEditing(undefined)}
|
|
1270
|
+
onDone={(line) => {
|
|
1271
|
+
setLines((current) => current.map((other, at) => (at === editing ? line : other)));
|
|
1272
|
+
setEditing(undefined);
|
|
1273
|
+
}}
|
|
1274
|
+
/>
|
|
1275
|
+
);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
const partyFields = (which: 'remitter' | 'recipient', party: PartyDraft, setParty: (next: PartyDraft) => void) => (
|
|
1279
|
+
<fieldset className="grid gap-3">
|
|
1280
|
+
<legend className="mb-1 text-sm font-medium">{which === 'remitter' ? words.remitter : words.recipient}</legend>
|
|
1281
|
+
<Field
|
|
1282
|
+
id={`${id}-${which}-company`}
|
|
1283
|
+
label={which === 'remitter' ? words.remitterCompany : words.recipientCompany}
|
|
1284
|
+
hint={which === 'recipient' ? words.recipientHint : undefined}
|
|
1285
|
+
>
|
|
1286
|
+
<ChoiceCombobox
|
|
1287
|
+
id={`${id}-${which}-company`}
|
|
1288
|
+
single
|
|
1289
|
+
value={party.company ? [party.company] : []}
|
|
1290
|
+
onChange={([company = '']) => setParty({ ...party, company, newCompany: undefined })}
|
|
1291
|
+
choices={companies}
|
|
1292
|
+
placeholder={words.searchCompanies}
|
|
1293
|
+
add={{ label: words.addCompany, onAdd: () => setAdding({ which, record: 'company' }) }}
|
|
1294
|
+
pending={
|
|
1295
|
+
party.newCompany
|
|
1296
|
+
? { name: party.newCompany, onClear: () => setParty({ ...party, newCompany: undefined }) }
|
|
1297
|
+
: undefined
|
|
1298
|
+
}
|
|
1299
|
+
describedBy={which === 'recipient' ? `${id}-${which}-company-hint` : undefined}
|
|
1300
|
+
text={text}
|
|
1301
|
+
/>
|
|
1302
|
+
</Field>
|
|
1303
|
+
<Field id={`${id}-${which}-person`} label={which === 'remitter' ? words.remitterPerson : words.recipientPerson}>
|
|
1304
|
+
<ChoiceCombobox
|
|
1305
|
+
id={`${id}-${which}-person`}
|
|
1306
|
+
single
|
|
1307
|
+
value={party.identity ? [party.identity] : []}
|
|
1308
|
+
onChange={([identity = '']) => setParty({ ...party, identity, newContact: undefined })}
|
|
1309
|
+
choices={people}
|
|
1310
|
+
placeholder={words.searchPeople}
|
|
1311
|
+
add={{ label: words.addContact, onAdd: () => setAdding({ which, record: 'contact' }) }}
|
|
1312
|
+
pending={
|
|
1313
|
+
party.newContact
|
|
1314
|
+
? { name: draftName(party.newContact), onClear: () => setParty({ ...party, newContact: undefined }) }
|
|
1315
|
+
: undefined
|
|
1316
|
+
}
|
|
1317
|
+
text={text}
|
|
1318
|
+
/>
|
|
1319
|
+
</Field>
|
|
1320
|
+
</fieldset>
|
|
1321
|
+
);
|
|
1322
|
+
|
|
1323
|
+
return (
|
|
1324
|
+
<FormFrame
|
|
1325
|
+
title={words.newInvoice}
|
|
1326
|
+
submit={words.createInvoice}
|
|
1327
|
+
submitting={words.creating}
|
|
1328
|
+
saving={saving}
|
|
1329
|
+
error={error}
|
|
1330
|
+
onSubmit={submit}
|
|
1331
|
+
>
|
|
1332
|
+
{({ error: errorId }) => (
|
|
1333
|
+
<>
|
|
1334
|
+
{partyFields('remitter', remitter, setRemitter)}
|
|
1335
|
+
{partyFields('recipient', recipient, setRecipient)}
|
|
1336
|
+
<Field id={`${id}-deal`} label={words.forDeal}>
|
|
1337
|
+
<Select value={deal || NONE} onValueChange={(next) => setDeal(next === NONE ? '' : next)}>
|
|
1338
|
+
<SelectTrigger id={`${id}-deal`} className="w-full">
|
|
1339
|
+
<SelectValue />
|
|
1340
|
+
</SelectTrigger>
|
|
1341
|
+
<SelectContent>
|
|
1342
|
+
<SelectItem value={NONE}>{words.noDeal}</SelectItem>
|
|
1343
|
+
{deals.map((option) => (
|
|
1344
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
1345
|
+
{option.title}
|
|
1346
|
+
</SelectItem>
|
|
1347
|
+
))}
|
|
1348
|
+
</SelectContent>
|
|
1349
|
+
</Select>
|
|
1350
|
+
</Field>
|
|
1351
|
+
<div className="grid grid-cols-2 gap-3">
|
|
1352
|
+
<Field id={`${id}-currency`} label={words.currency}>
|
|
1353
|
+
<Select value={currencyCode} onValueChange={setCurrencyCode}>
|
|
1354
|
+
<SelectTrigger id={`${id}-currency`} className="w-full">
|
|
1355
|
+
<SelectValue />
|
|
1356
|
+
</SelectTrigger>
|
|
1357
|
+
<SelectContent>
|
|
1358
|
+
{(CURRENCIES.includes(currencyCode) ? CURRENCIES : [currencyCode, ...CURRENCIES]).map((code) => (
|
|
1359
|
+
<SelectItem key={code} value={code}>
|
|
1360
|
+
{code}
|
|
1361
|
+
</SelectItem>
|
|
1362
|
+
))}
|
|
1363
|
+
</SelectContent>
|
|
1364
|
+
</Select>
|
|
1365
|
+
</Field>
|
|
1366
|
+
<Field id={`${id}-status`} label={words.status}>
|
|
1367
|
+
<Select value={status} onValueChange={(next) => setStatus(next as InvoiceStatus)}>
|
|
1368
|
+
<SelectTrigger id={`${id}-status`} className="w-full">
|
|
1369
|
+
<SelectValue />
|
|
1370
|
+
</SelectTrigger>
|
|
1371
|
+
<SelectContent>
|
|
1372
|
+
{INVOICE_STATUSES.map((option) => (
|
|
1373
|
+
<SelectItem key={option} value={option}>
|
|
1374
|
+
{INVOICE_STATUS_LABELS[option]}
|
|
1375
|
+
</SelectItem>
|
|
1376
|
+
))}
|
|
1377
|
+
</SelectContent>
|
|
1378
|
+
</Select>
|
|
1379
|
+
</Field>
|
|
1380
|
+
<Field id={`${id}-issued`} label={words.issuedOn}>
|
|
1381
|
+
<Input
|
|
1382
|
+
id={`${id}-issued`}
|
|
1383
|
+
type="date"
|
|
1384
|
+
value={issued}
|
|
1385
|
+
onChange={(event) => setIssued(event.target.value)}
|
|
1386
|
+
/>
|
|
1387
|
+
</Field>
|
|
1388
|
+
<Field id={`${id}-due`} label={words.dueOn}>
|
|
1389
|
+
<Input id={`${id}-due`} type="date" value={due} onChange={(event) => setDue(event.target.value)} />
|
|
1390
|
+
</Field>
|
|
1391
|
+
</div>
|
|
1392
|
+
<Field id={`${id}-amount-as`} label={words.amountAs}>
|
|
1393
|
+
<Select value={amountAs} onValueChange={(next) => setAmountAs(next as 'lines' | 'total')}>
|
|
1394
|
+
<SelectTrigger id={`${id}-amount-as`} className="w-full">
|
|
1395
|
+
<SelectValue />
|
|
1396
|
+
</SelectTrigger>
|
|
1397
|
+
<SelectContent>
|
|
1398
|
+
<SelectItem value="lines">{words.amountAsLines}</SelectItem>
|
|
1399
|
+
<SelectItem value="total">{words.amountAsTotal}</SelectItem>
|
|
1400
|
+
</SelectContent>
|
|
1401
|
+
</Select>
|
|
1402
|
+
</Field>
|
|
1403
|
+
{amountAs === 'total' && (
|
|
1404
|
+
<Field id={`${id}-total`} label={`${words.totalWithTax} (${currencyCode})`} hint={words.totalHint}>
|
|
1405
|
+
<Input
|
|
1406
|
+
id={`${id}-total`}
|
|
1407
|
+
inputMode="decimal"
|
|
1408
|
+
value={total}
|
|
1409
|
+
onChange={(event) => setTotal(event.target.value)}
|
|
1410
|
+
aria-describedby={[`${id}-total-hint`, errorId].filter(Boolean).join(' ')}
|
|
1411
|
+
/>
|
|
1412
|
+
</Field>
|
|
1413
|
+
)}
|
|
1414
|
+
<fieldset className="grid gap-2" aria-describedby={errorId} hidden={amountAs === 'total'}>
|
|
1415
|
+
<legend className="mb-1 text-sm font-medium">{words.lines}</legend>
|
|
1416
|
+
{lines.map((line, index) => (
|
|
1417
|
+
<div key={index} className="grid grid-cols-[1fr_4rem_6rem_auto_auto] items-end gap-2">
|
|
1418
|
+
<Field id={`${id}-line-${index}-description`} label={`${words.description} ${index + 1}`}>
|
|
1419
|
+
<Input
|
|
1420
|
+
id={`${id}-line-${index}-description`}
|
|
1421
|
+
value={line.description}
|
|
1422
|
+
onChange={(event) => change(index, 'description', event.target.value)}
|
|
1423
|
+
/>
|
|
1424
|
+
</Field>
|
|
1425
|
+
<Field id={`${id}-line-${index}-quantity`} label={words.quantity}>
|
|
1426
|
+
<Input
|
|
1427
|
+
id={`${id}-line-${index}-quantity`}
|
|
1428
|
+
inputMode="decimal"
|
|
1429
|
+
value={line.quantity}
|
|
1430
|
+
onChange={(event) => change(index, 'quantity', event.target.value)}
|
|
1431
|
+
/>
|
|
1432
|
+
</Field>
|
|
1433
|
+
<Field id={`${id}-line-${index}-price`} label={words.unitPrice}>
|
|
1434
|
+
<Input
|
|
1435
|
+
id={`${id}-line-${index}-price`}
|
|
1436
|
+
inputMode="decimal"
|
|
1437
|
+
value={line.unitPrice}
|
|
1438
|
+
onChange={(event) => change(index, 'unitPrice', event.target.value)}
|
|
1439
|
+
/>
|
|
1440
|
+
</Field>
|
|
1441
|
+
<Button
|
|
1442
|
+
type="button"
|
|
1443
|
+
variant="ghost"
|
|
1444
|
+
size="icon"
|
|
1445
|
+
aria-label={`${words.lineDetails}: ${words.line} ${index + 1}`}
|
|
1446
|
+
onClick={() => setEditing(index)}
|
|
1447
|
+
>
|
|
1448
|
+
<ChevronRight aria-hidden="true" />
|
|
1449
|
+
</Button>
|
|
1450
|
+
<Button
|
|
1451
|
+
type="button"
|
|
1452
|
+
variant="ghost"
|
|
1453
|
+
size="icon"
|
|
1454
|
+
aria-label={`${words.removeLine} ${index + 1}`}
|
|
1455
|
+
disabled={lines.length === 1}
|
|
1456
|
+
onClick={() => setLines((current) => current.filter((_, at) => at !== index))}
|
|
1457
|
+
>
|
|
1458
|
+
<X aria-hidden="true" />
|
|
1459
|
+
</Button>
|
|
1460
|
+
</div>
|
|
1461
|
+
))}
|
|
1462
|
+
<div>
|
|
1463
|
+
<Button
|
|
1464
|
+
type="button"
|
|
1465
|
+
variant="outline"
|
|
1466
|
+
size="sm"
|
|
1467
|
+
onClick={() => setLines((current) => [...current, EMPTY_LINE])}
|
|
1468
|
+
>
|
|
1469
|
+
<Plus aria-hidden="true" />
|
|
1470
|
+
{words.addLine}
|
|
1471
|
+
</Button>
|
|
1472
|
+
</div>
|
|
1473
|
+
</fieldset>
|
|
1474
|
+
<Field id={`${id}-rate`} label={words.taxRate}>
|
|
1475
|
+
<Input
|
|
1476
|
+
id={`${id}-rate`}
|
|
1477
|
+
inputMode="decimal"
|
|
1478
|
+
value={rate}
|
|
1479
|
+
onChange={(event) => setRate(event.target.value)}
|
|
1480
|
+
/>
|
|
1481
|
+
</Field>
|
|
1482
|
+
</>
|
|
1483
|
+
)}
|
|
1484
|
+
</FormFrame>
|
|
1485
|
+
);
|
|
1486
|
+
}
|