@baack-software/baack-ts-crm 0.0.0-stage → 0.0.2
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 +76 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +233 -0
- package/dist/backend.js.map +1 -0
- package/dist/crm-client.d.ts +122 -0
- package/dist/crm-client.d.ts.map +1 -0
- package/dist/crm-client.js +168 -0
- package/dist/crm-client.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 +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/invoices.d.ts +46 -0
- package/dist/invoices.d.ts.map +1 -0
- package/dist/invoices.js +137 -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 +17 -0
- package/dist/period.d.ts.map +1 -0
- package/dist/period.js +23 -0
- package/dist/period.js.map +1 -0
- package/dist/pipeline.d.ts +30 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +40 -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/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 +30 -0
- package/dist/react/DealsBoard.d.ts.map +1 -0
- package/dist/react/DealsBoard.js +33 -0
- package/dist/react/DealsBoard.js.map +1 -0
- package/dist/react/Details.d.ts +52 -0
- package/dist/react/Details.d.ts.map +1 -0
- package/dist/react/Details.js +111 -0
- package/dist/react/Details.js.map +1 -0
- package/dist/react/Forms.d.ts +59 -0
- package/dist/react/Forms.d.ts.map +1 -0
- package/dist/react/Forms.js +204 -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 +49 -0
- package/dist/react/Sales.d.ts.map +1 -0
- package/dist/react/Sales.js +186 -0
- package/dist/react/Sales.js.map +1 -0
- package/dist/react/SidePanel.d.ts +21 -0
- package/dist/react/SidePanel.d.ts.map +1 -0
- package/dist/react/SidePanel.js +37 -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 +14 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +14 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/parts.d.ts +45 -0
- package/dist/react/parts.d.ts.map +1 -0
- package/dist/react/parts.js +59 -0
- package/dist/react/parts.js.map +1 -0
- package/dist/react/text.d.ts +145 -0
- package/dist/react/text.d.ts.map +1 -0
- package/dist/react/text.js +142 -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 +60 -0
- package/dist/react/views.d.ts.map +1 -0
- package/dist/react/views.js +172 -0
- package/dist/react/views.js.map +1 -0
- package/dist/sales.d.ts +57 -0
- package/dist/sales.d.ts.map +1 -0
- package/dist/sales.js +64 -0
- package/dist/sales.js.map +1 -0
- package/dist/store.d.ts +140 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +244 -0
- package/dist/store.js.map +1 -0
- package/dist/target.d.ts +21 -0
- package/dist/target.d.ts.map +1 -0
- package/dist/target.js +36 -0
- package/dist/target.js.map +1 -0
- package/dist/types.d.ts +180 -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 +371 -0
- package/src/crm-client.ts +250 -0
- package/src/errors.ts +34 -0
- package/src/index.ts +12 -0
- package/src/invoices.ts +157 -0
- package/src/leads.ts +78 -0
- package/src/mock/index.ts +398 -0
- package/src/money.ts +104 -0
- package/src/period.ts +32 -0
- package/src/pipeline.ts +61 -0
- package/src/react/Charts.tsx +143 -0
- package/src/react/CompanyAccountView.tsx +198 -0
- package/src/react/DashboardCards.tsx +171 -0
- package/src/react/DealsBoard.tsx +177 -0
- package/src/react/Details.tsx +293 -0
- package/src/react/Forms.tsx +619 -0
- package/src/react/LeadsTable.tsx +234 -0
- package/src/react/Sales.tsx +539 -0
- package/src/react/SidePanel.tsx +95 -0
- package/src/react/format.ts +53 -0
- package/src/react/index.ts +13 -0
- package/src/react/parts.tsx +131 -0
- package/src/react/text.ts +144 -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 +443 -0
- package/src/sales.ts +120 -0
- package/src/store.ts +419 -0
- package/src/target.ts +51 -0
- package/src/types.ts +202 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
import { ref, type RefLike } from '@baack-software/baack-ts-tasks';
|
|
2
|
+
import { 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 { NewLeadOptions } from '../crm-client.ts';
|
|
6
|
+
import { LEAD_SOURCES, SOURCE_LABELS } from '../leads.ts';
|
|
7
|
+
import { fractionDigits, fromMinor, toMinor } from '../money.ts';
|
|
8
|
+
import { orderedStages } from '../pipeline.ts';
|
|
9
|
+
import type { Deal, InvoiceStatus, LeadSource, Money, Pipeline } from '../types.ts';
|
|
10
|
+
import { withText, type CrmText } from './text.ts';
|
|
11
|
+
import { Button } from './ui/button.tsx';
|
|
12
|
+
import { Input } from './ui/input.tsx';
|
|
13
|
+
import { Label } from './ui/label.tsx';
|
|
14
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select.tsx';
|
|
15
|
+
|
|
16
|
+
/** A company the forms can link a record to: the host's customers, named. */
|
|
17
|
+
export interface CrmCompanyChoice {
|
|
18
|
+
urn: string;
|
|
19
|
+
name: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface FormProps {
|
|
23
|
+
/** The company whose CRM this is. */
|
|
24
|
+
owner: RefLike;
|
|
25
|
+
text?: Partial<CrmText> | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** An invoice line as the new invoice form makes it: its unit price in major units, its price and tax as money. */
|
|
29
|
+
export interface InvoiceLineDraft {
|
|
30
|
+
description: string;
|
|
31
|
+
quantity: number;
|
|
32
|
+
unitPrice: number;
|
|
33
|
+
price: Money;
|
|
34
|
+
tax: Money;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Radix's select keeps "" for "no value", so "none" is chosen with this instead. */
|
|
38
|
+
const NONE = '__none__';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* An amount typed in major units (120.00, or 1,500.00 with separators) as minor units, or undefined when it isn't a
|
|
42
|
+
* number of at least zero.
|
|
43
|
+
*/
|
|
44
|
+
function minorOrUndefined(amount: string, currencyCode: string): number | undefined {
|
|
45
|
+
try {
|
|
46
|
+
const minor = toMinor(amount.replaceAll(',', '').trim(), currencyCode);
|
|
47
|
+
return minor >= 0 ? minor : undefined;
|
|
48
|
+
} catch {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A date input's value (YYYY-MM-DD) as the API's timestamp: the start of that day, UTC. */
|
|
54
|
+
const dayTimestamp = (day: string) => (day ? `${day}T00:00:00` : null);
|
|
55
|
+
const today = (now: Date) => now.toISOString().slice(0, 10);
|
|
56
|
+
const inDays = (now: Date, days: number) => today(new Date(now.getTime() + days * 86_400_000));
|
|
57
|
+
|
|
58
|
+
/** A form's frame: its heading, fields, problem and submit button, as the record payment form is laid out. */
|
|
59
|
+
function FormFrame({
|
|
60
|
+
title,
|
|
61
|
+
submit,
|
|
62
|
+
submitting,
|
|
63
|
+
saving,
|
|
64
|
+
error,
|
|
65
|
+
onSubmit,
|
|
66
|
+
children,
|
|
67
|
+
}: {
|
|
68
|
+
title: string;
|
|
69
|
+
submit: string;
|
|
70
|
+
submitting: string;
|
|
71
|
+
saving: boolean;
|
|
72
|
+
error: string | undefined;
|
|
73
|
+
onSubmit: () => Promise<void>;
|
|
74
|
+
children: (ids: { error: string | undefined }) => ReactNode;
|
|
75
|
+
}) {
|
|
76
|
+
const id = useId();
|
|
77
|
+
const errorId = error ? `${id}-error` : undefined;
|
|
78
|
+
return (
|
|
79
|
+
<form
|
|
80
|
+
className="grid gap-3"
|
|
81
|
+
aria-labelledby={`${id}-title`}
|
|
82
|
+
noValidate
|
|
83
|
+
onSubmit={(event: FormEvent) => {
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
void onSubmit();
|
|
86
|
+
}}
|
|
87
|
+
>
|
|
88
|
+
<h2 id={`${id}-title`} className="text-base font-semibold">
|
|
89
|
+
{title}
|
|
90
|
+
</h2>
|
|
91
|
+
{children({ error: errorId })}
|
|
92
|
+
{error && (
|
|
93
|
+
<p id={errorId} role="alert" className="text-destructive text-sm">
|
|
94
|
+
{error}
|
|
95
|
+
</p>
|
|
96
|
+
)}
|
|
97
|
+
<div>
|
|
98
|
+
<Button type="submit" disabled={saving}>
|
|
99
|
+
{saving ? submitting : submit}
|
|
100
|
+
</Button>
|
|
101
|
+
</div>
|
|
102
|
+
</form>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function Field({ id, label, hint, children }: { id: string; label: string; hint?: string; children: ReactNode }) {
|
|
107
|
+
return (
|
|
108
|
+
<div className="grid gap-1.5">
|
|
109
|
+
<Label htmlFor={id}>{label}</Label>
|
|
110
|
+
{children}
|
|
111
|
+
{hint && (
|
|
112
|
+
<p id={`${id}-hint`} className="text-muted-foreground text-xs">
|
|
113
|
+
{hint}
|
|
114
|
+
</p>
|
|
115
|
+
)}
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function CompanySelect({
|
|
121
|
+
id,
|
|
122
|
+
value,
|
|
123
|
+
onChange,
|
|
124
|
+
companies,
|
|
125
|
+
none,
|
|
126
|
+
}: {
|
|
127
|
+
id: string;
|
|
128
|
+
value: string;
|
|
129
|
+
onChange: (urn: string) => void;
|
|
130
|
+
companies: readonly CrmCompanyChoice[];
|
|
131
|
+
none: string;
|
|
132
|
+
}) {
|
|
133
|
+
return (
|
|
134
|
+
<Select value={value || NONE} onValueChange={(next) => onChange(next === NONE ? '' : next)}>
|
|
135
|
+
<SelectTrigger id={id} className="w-full">
|
|
136
|
+
<SelectValue />
|
|
137
|
+
</SelectTrigger>
|
|
138
|
+
<SelectContent>
|
|
139
|
+
<SelectItem value={NONE}>{none}</SelectItem>
|
|
140
|
+
{companies.map((company) => (
|
|
141
|
+
<SelectItem key={company.urn} value={company.urn}>
|
|
142
|
+
{company.name}
|
|
143
|
+
</SelectItem>
|
|
144
|
+
))}
|
|
145
|
+
</SelectContent>
|
|
146
|
+
</Select>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Runs a form's save: the problem in words when it fails, the form left as it was so it can be sent again. */
|
|
151
|
+
function useSave() {
|
|
152
|
+
const [saving, setSaving] = useState(false);
|
|
153
|
+
const [error, setError] = useState<string>();
|
|
154
|
+
const run = async (save: () => Promise<unknown>) => {
|
|
155
|
+
setError(undefined);
|
|
156
|
+
setSaving(true);
|
|
157
|
+
try {
|
|
158
|
+
await save();
|
|
159
|
+
} catch (failure) {
|
|
160
|
+
setError(failure instanceof Error ? failure.message : String(failure));
|
|
161
|
+
} finally {
|
|
162
|
+
setSaving(false);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
return { saving, error, setError, run };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A new lead: its title, source, value, and its company, chosen from the host's companies or made from a new name (a
|
|
170
|
+
* lead holds its business by its company).
|
|
171
|
+
*/
|
|
172
|
+
export function NewLeadForm({
|
|
173
|
+
owner,
|
|
174
|
+
companies = [],
|
|
175
|
+
currencyCode = 'GBP',
|
|
176
|
+
onCreate,
|
|
177
|
+
text,
|
|
178
|
+
}: FormProps & {
|
|
179
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
180
|
+
currencyCode?: string;
|
|
181
|
+
onCreate: (input: LeadInput, options: NewLeadOptions) => Promise<unknown>;
|
|
182
|
+
}) {
|
|
183
|
+
const words = withText(text);
|
|
184
|
+
const id = useId();
|
|
185
|
+
const [title, setTitle] = useState('');
|
|
186
|
+
const [source, setSource] = useState<LeadSource>('WEBSITE');
|
|
187
|
+
const [value, setValue] = useState('');
|
|
188
|
+
const [company, setCompany] = useState('');
|
|
189
|
+
const [newCompany, setNewCompany] = useState('');
|
|
190
|
+
const { saving, error, setError, run } = useSave();
|
|
191
|
+
const submit = async () => {
|
|
192
|
+
const minor = value.trim() ? minorOrUndefined(value, currencyCode) : 0;
|
|
193
|
+
if (!title.trim()) return setError(words.titleRequired);
|
|
194
|
+
if (minor === undefined) return setError(words.invalidAmount);
|
|
195
|
+
const named = company ? '' : newCompany.trim();
|
|
196
|
+
await run(() =>
|
|
197
|
+
onCreate(
|
|
198
|
+
{
|
|
199
|
+
owner: ref(owner),
|
|
200
|
+
title: title.trim(),
|
|
201
|
+
source,
|
|
202
|
+
status: 'NEW',
|
|
203
|
+
outcome: 'IN_PROGRESS',
|
|
204
|
+
contactCompany: company ? { urn: company } : null,
|
|
205
|
+
targetValue: minor ? fromMinor(minor, currencyCode) : null,
|
|
206
|
+
},
|
|
207
|
+
named ? { companyName: named } : {},
|
|
208
|
+
),
|
|
209
|
+
);
|
|
210
|
+
};
|
|
211
|
+
return (
|
|
212
|
+
<FormFrame
|
|
213
|
+
title={words.newLead}
|
|
214
|
+
submit={words.createLead}
|
|
215
|
+
submitting={words.creating}
|
|
216
|
+
saving={saving}
|
|
217
|
+
error={error}
|
|
218
|
+
onSubmit={submit}
|
|
219
|
+
>
|
|
220
|
+
{({ error: errorId }) => (
|
|
221
|
+
<>
|
|
222
|
+
<Field id={`${id}-title`} label={words.title}>
|
|
223
|
+
<Input
|
|
224
|
+
id={`${id}-title`}
|
|
225
|
+
value={title}
|
|
226
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
227
|
+
aria-describedby={errorId}
|
|
228
|
+
/>
|
|
229
|
+
</Field>
|
|
230
|
+
<Field id={`${id}-source`} label={words.source}>
|
|
231
|
+
<Select value={source} onValueChange={(next) => setSource(next as LeadSource)}>
|
|
232
|
+
<SelectTrigger id={`${id}-source`} className="w-full">
|
|
233
|
+
<SelectValue />
|
|
234
|
+
</SelectTrigger>
|
|
235
|
+
<SelectContent>
|
|
236
|
+
{LEAD_SOURCES.map((option) => (
|
|
237
|
+
<SelectItem key={option} value={option}>
|
|
238
|
+
{SOURCE_LABELS[option]}
|
|
239
|
+
</SelectItem>
|
|
240
|
+
))}
|
|
241
|
+
</SelectContent>
|
|
242
|
+
</Select>
|
|
243
|
+
</Field>
|
|
244
|
+
<Field id={`${id}-value`} label={`${words.value} (${currencyCode})`}>
|
|
245
|
+
<Input
|
|
246
|
+
id={`${id}-value`}
|
|
247
|
+
inputMode="decimal"
|
|
248
|
+
value={value}
|
|
249
|
+
onChange={(event) => setValue(event.target.value)}
|
|
250
|
+
/>
|
|
251
|
+
</Field>
|
|
252
|
+
<Field id={`${id}-company`} label={words.company}>
|
|
253
|
+
<CompanySelect
|
|
254
|
+
id={`${id}-company`}
|
|
255
|
+
value={company}
|
|
256
|
+
onChange={setCompany}
|
|
257
|
+
companies={companies}
|
|
258
|
+
none={words.noCompany}
|
|
259
|
+
/>
|
|
260
|
+
</Field>
|
|
261
|
+
{!company && (
|
|
262
|
+
<Field id={`${id}-new-company`} label={words.newCompany} hint={words.newCompanyHint}>
|
|
263
|
+
<Input
|
|
264
|
+
id={`${id}-new-company`}
|
|
265
|
+
value={newCompany}
|
|
266
|
+
onChange={(event) => setNewCompany(event.target.value)}
|
|
267
|
+
aria-describedby={`${id}-new-company-hint`}
|
|
268
|
+
/>
|
|
269
|
+
</Field>
|
|
270
|
+
)}
|
|
271
|
+
</>
|
|
272
|
+
)}
|
|
273
|
+
</FormFrame>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* A new deal that didn't start as a lead: its title, company, value, stage in the pipeline (the first, to start with),
|
|
279
|
+
* expected close and probability.
|
|
280
|
+
*/
|
|
281
|
+
export function NewDealForm({
|
|
282
|
+
owner,
|
|
283
|
+
pipeline,
|
|
284
|
+
companies = [],
|
|
285
|
+
currencyCode = 'GBP',
|
|
286
|
+
now = new Date(),
|
|
287
|
+
onCreate,
|
|
288
|
+
text,
|
|
289
|
+
}: FormProps & {
|
|
290
|
+
pipeline: Pipeline;
|
|
291
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
292
|
+
currencyCode?: string;
|
|
293
|
+
now?: Date;
|
|
294
|
+
onCreate: (input: DealInput) => Promise<unknown>;
|
|
295
|
+
}) {
|
|
296
|
+
const words = withText(text);
|
|
297
|
+
const id = useId();
|
|
298
|
+
const stages = orderedStages(pipeline);
|
|
299
|
+
const [title, setTitle] = useState('');
|
|
300
|
+
const [company, setCompany] = useState('');
|
|
301
|
+
const [value, setValue] = useState('');
|
|
302
|
+
const [stage, setStage] = useState(stages[0]?.urn ?? '');
|
|
303
|
+
const [close, setClose] = useState(inDays(now, 30));
|
|
304
|
+
const [probability, setProbability] = useState('');
|
|
305
|
+
const { saving, error, setError, run } = useSave();
|
|
306
|
+
const submit = async () => {
|
|
307
|
+
const minor = value.trim() ? minorOrUndefined(value, currencyCode) : 0;
|
|
308
|
+
const chance = probability.trim() ? Number(probability) : undefined;
|
|
309
|
+
if (!title.trim()) return setError(words.titleRequired);
|
|
310
|
+
if (minor === undefined) return setError(words.invalidAmount);
|
|
311
|
+
if (chance !== undefined && !(chance >= 0 && chance <= 100)) return setError(words.invalidProbability);
|
|
312
|
+
await run(() =>
|
|
313
|
+
onCreate({
|
|
314
|
+
owner: ref(owner),
|
|
315
|
+
title: title.trim(),
|
|
316
|
+
contactCompanies: company ? [{ urn: company }] : [],
|
|
317
|
+
contactIdentities: [],
|
|
318
|
+
workflowStage: stage ? { urn: stage } : null,
|
|
319
|
+
outcome: 'OPEN',
|
|
320
|
+
probability: chance ?? null,
|
|
321
|
+
targetValue: minor ? fromMinor(minor, currencyCode) : null,
|
|
322
|
+
targetCloseTimestamp: dayTimestamp(close),
|
|
323
|
+
invoices: [],
|
|
324
|
+
}),
|
|
325
|
+
);
|
|
326
|
+
};
|
|
327
|
+
return (
|
|
328
|
+
<FormFrame
|
|
329
|
+
title={words.newDeal}
|
|
330
|
+
submit={words.createDeal}
|
|
331
|
+
submitting={words.creating}
|
|
332
|
+
saving={saving}
|
|
333
|
+
error={error}
|
|
334
|
+
onSubmit={submit}
|
|
335
|
+
>
|
|
336
|
+
{({ error: errorId }) => (
|
|
337
|
+
<>
|
|
338
|
+
<Field id={`${id}-title`} label={words.title}>
|
|
339
|
+
<Input
|
|
340
|
+
id={`${id}-title`}
|
|
341
|
+
value={title}
|
|
342
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
343
|
+
aria-describedby={errorId}
|
|
344
|
+
/>
|
|
345
|
+
</Field>
|
|
346
|
+
<Field id={`${id}-company`} label={words.company}>
|
|
347
|
+
<CompanySelect
|
|
348
|
+
id={`${id}-company`}
|
|
349
|
+
value={company}
|
|
350
|
+
onChange={setCompany}
|
|
351
|
+
companies={companies}
|
|
352
|
+
none={words.noCompany}
|
|
353
|
+
/>
|
|
354
|
+
</Field>
|
|
355
|
+
<Field id={`${id}-value`} label={`${words.value} (${currencyCode})`}>
|
|
356
|
+
<Input
|
|
357
|
+
id={`${id}-value`}
|
|
358
|
+
inputMode="decimal"
|
|
359
|
+
value={value}
|
|
360
|
+
onChange={(event) => setValue(event.target.value)}
|
|
361
|
+
/>
|
|
362
|
+
</Field>
|
|
363
|
+
<Field id={`${id}-stage`} label={words.stage}>
|
|
364
|
+
<Select value={stage} onValueChange={setStage}>
|
|
365
|
+
<SelectTrigger id={`${id}-stage`} className="w-full">
|
|
366
|
+
<SelectValue />
|
|
367
|
+
</SelectTrigger>
|
|
368
|
+
<SelectContent>
|
|
369
|
+
{stages.map((option) => (
|
|
370
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
371
|
+
{option.name}
|
|
372
|
+
</SelectItem>
|
|
373
|
+
))}
|
|
374
|
+
</SelectContent>
|
|
375
|
+
</Select>
|
|
376
|
+
</Field>
|
|
377
|
+
<Field id={`${id}-close`} label={words.expectedClose}>
|
|
378
|
+
<Input id={`${id}-close`} type="date" value={close} onChange={(event) => setClose(event.target.value)} />
|
|
379
|
+
</Field>
|
|
380
|
+
<Field id={`${id}-probability`} label={`${words.probability} (%)`}>
|
|
381
|
+
<Input
|
|
382
|
+
id={`${id}-probability`}
|
|
383
|
+
inputMode="numeric"
|
|
384
|
+
value={probability}
|
|
385
|
+
onChange={(event) => setProbability(event.target.value)}
|
|
386
|
+
/>
|
|
387
|
+
</Field>
|
|
388
|
+
</>
|
|
389
|
+
)}
|
|
390
|
+
</FormFrame>
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const INVOICE_STATUSES: readonly InvoiceStatus[] = ['DRAFT', 'AUTHORISED'];
|
|
395
|
+
|
|
396
|
+
interface LineRow {
|
|
397
|
+
description: string;
|
|
398
|
+
quantity: string;
|
|
399
|
+
unitPrice: string;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const EMPTY_LINE: LineRow = { description: '', quantity: '1', unitPrice: '' };
|
|
403
|
+
|
|
404
|
+
/** What a line, typed in, comes to: its net price and tax in minor units, or undefined when it isn't complete. */
|
|
405
|
+
function priceLine(line: LineRow, currencyCode: string, rate: number) {
|
|
406
|
+
const quantity = Number(line.quantity);
|
|
407
|
+
const unit = minorOrUndefined(line.unitPrice, currencyCode);
|
|
408
|
+
if (!line.description.trim() || !(quantity > 0) || !unit) return undefined;
|
|
409
|
+
const price = Math.round(unit * quantity);
|
|
410
|
+
return { description: line.description.trim(), quantity, unit, price, tax: Math.round((price * rate) / 100) };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* A new invoice to a customer: its lines (description, quantity, unit price) and a tax rate, which give its subtotal,
|
|
415
|
+
* tax and total (the API keeps an invoice's amounts as given), its dates and status, and the deal it's for, if any.
|
|
416
|
+
*/
|
|
417
|
+
export function NewInvoiceForm({
|
|
418
|
+
owner,
|
|
419
|
+
companies = [],
|
|
420
|
+
deals = [],
|
|
421
|
+
currencyCode: defaultCurrency = 'GBP',
|
|
422
|
+
now = new Date(),
|
|
423
|
+
onCreate,
|
|
424
|
+
text,
|
|
425
|
+
}: FormProps & {
|
|
426
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
427
|
+
/** Deals the invoice can be raised for. */
|
|
428
|
+
deals?: readonly Deal[];
|
|
429
|
+
currencyCode?: string;
|
|
430
|
+
now?: Date;
|
|
431
|
+
onCreate: (input: InvoiceInput, lines: InvoiceLineDraft[], options: { deal?: Deal }) => Promise<unknown>;
|
|
432
|
+
}) {
|
|
433
|
+
const words = withText(text);
|
|
434
|
+
const id = useId();
|
|
435
|
+
const [customer, setCustomer] = useState('');
|
|
436
|
+
const [deal, setDeal] = useState('');
|
|
437
|
+
const [currencyCode, setCurrencyCode] = useState(defaultCurrency);
|
|
438
|
+
const [status, setStatus] = useState<InvoiceStatus>('DRAFT');
|
|
439
|
+
const [issued, setIssued] = useState(today(now));
|
|
440
|
+
const [due, setDue] = useState(inDays(now, 30));
|
|
441
|
+
const [rate, setRate] = useState('20');
|
|
442
|
+
const [lines, setLines] = useState<LineRow[]>([EMPTY_LINE]);
|
|
443
|
+
const { saving, error, setError, run } = useSave();
|
|
444
|
+
const change = (index: number, field: keyof LineRow, value: string) =>
|
|
445
|
+
setLines((current) => current.map((line, at) => (at === index ? { ...line, [field]: value } : line)));
|
|
446
|
+
const submit = async () => {
|
|
447
|
+
const taxRate = Number(rate);
|
|
448
|
+
const code = currencyCode.trim().toUpperCase();
|
|
449
|
+
if (!customer) return setError(words.customerRequired);
|
|
450
|
+
if (!(taxRate >= 0 && taxRate <= 100)) return setError(words.invalidRate);
|
|
451
|
+
const priced = lines.map((line) => priceLine(line, code, taxRate));
|
|
452
|
+
if (priced.some((line) => line === undefined)) return setError(words.linesRequired);
|
|
453
|
+
const done = priced as NonNullable<(typeof priced)[number]>[];
|
|
454
|
+
const subTotal = done.reduce((sum, line) => sum + line.price, 0);
|
|
455
|
+
const tax = done.reduce((sum, line) => sum + line.tax, 0);
|
|
456
|
+
const party = { company: { urn: customer }, identity: null };
|
|
457
|
+
const linked = deals.find((candidate) => candidate.urn === deal);
|
|
458
|
+
await run(() =>
|
|
459
|
+
onCreate(
|
|
460
|
+
{
|
|
461
|
+
owner: ref(owner),
|
|
462
|
+
recipient: party,
|
|
463
|
+
remitter: party,
|
|
464
|
+
status,
|
|
465
|
+
currencyCode: code,
|
|
466
|
+
lineItemTax: taxRate ? 'EXCLUSIVE' : 'NO_TAX',
|
|
467
|
+
subTotal: fromMinor(subTotal, code),
|
|
468
|
+
tax: fromMinor(tax, code),
|
|
469
|
+
total: fromMinor(subTotal + tax, code),
|
|
470
|
+
paid: fromMinor(0, code),
|
|
471
|
+
issuedTimestamp: status === 'DRAFT' ? null : dayTimestamp(issued),
|
|
472
|
+
dueTimestamp: dayTimestamp(due),
|
|
473
|
+
},
|
|
474
|
+
done.map((line) => ({
|
|
475
|
+
description: line.description,
|
|
476
|
+
quantity: line.quantity,
|
|
477
|
+
// A line's unit price is in major units; its price and tax are money.
|
|
478
|
+
unitPrice: line.unit / 10 ** fractionDigits(code),
|
|
479
|
+
price: fromMinor(line.price, code),
|
|
480
|
+
tax: fromMinor(line.tax, code),
|
|
481
|
+
})),
|
|
482
|
+
linked ? { deal: linked } : {},
|
|
483
|
+
),
|
|
484
|
+
);
|
|
485
|
+
};
|
|
486
|
+
return (
|
|
487
|
+
<FormFrame
|
|
488
|
+
title={words.newInvoice}
|
|
489
|
+
submit={words.createInvoice}
|
|
490
|
+
submitting={words.creating}
|
|
491
|
+
saving={saving}
|
|
492
|
+
error={error}
|
|
493
|
+
onSubmit={submit}
|
|
494
|
+
>
|
|
495
|
+
{({ error: errorId }) => (
|
|
496
|
+
<>
|
|
497
|
+
<Field id={`${id}-customer`} label={words.customer}>
|
|
498
|
+
<CompanySelect
|
|
499
|
+
id={`${id}-customer`}
|
|
500
|
+
value={customer}
|
|
501
|
+
onChange={setCustomer}
|
|
502
|
+
companies={companies}
|
|
503
|
+
none={words.noCompany}
|
|
504
|
+
/>
|
|
505
|
+
</Field>
|
|
506
|
+
<Field id={`${id}-deal`} label={words.forDeal}>
|
|
507
|
+
<Select value={deal || NONE} onValueChange={(next) => setDeal(next === NONE ? '' : next)}>
|
|
508
|
+
<SelectTrigger id={`${id}-deal`} className="w-full">
|
|
509
|
+
<SelectValue />
|
|
510
|
+
</SelectTrigger>
|
|
511
|
+
<SelectContent>
|
|
512
|
+
<SelectItem value={NONE}>{words.noDeal}</SelectItem>
|
|
513
|
+
{deals.map((option) => (
|
|
514
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
515
|
+
{option.title}
|
|
516
|
+
</SelectItem>
|
|
517
|
+
))}
|
|
518
|
+
</SelectContent>
|
|
519
|
+
</Select>
|
|
520
|
+
</Field>
|
|
521
|
+
<div className="grid grid-cols-2 gap-3">
|
|
522
|
+
<Field id={`${id}-currency`} label={words.currency}>
|
|
523
|
+
<Input
|
|
524
|
+
id={`${id}-currency`}
|
|
525
|
+
value={currencyCode}
|
|
526
|
+
maxLength={3}
|
|
527
|
+
onChange={(event) => setCurrencyCode(event.target.value)}
|
|
528
|
+
/>
|
|
529
|
+
</Field>
|
|
530
|
+
<Field id={`${id}-status`} label={words.status}>
|
|
531
|
+
<Select value={status} onValueChange={(next) => setStatus(next as InvoiceStatus)}>
|
|
532
|
+
<SelectTrigger id={`${id}-status`} className="w-full">
|
|
533
|
+
<SelectValue />
|
|
534
|
+
</SelectTrigger>
|
|
535
|
+
<SelectContent>
|
|
536
|
+
{INVOICE_STATUSES.map((option) => (
|
|
537
|
+
<SelectItem key={option} value={option}>
|
|
538
|
+
{option === 'DRAFT' ? 'Draft' : words.awaitingPayment}
|
|
539
|
+
</SelectItem>
|
|
540
|
+
))}
|
|
541
|
+
</SelectContent>
|
|
542
|
+
</Select>
|
|
543
|
+
</Field>
|
|
544
|
+
<Field id={`${id}-issued`} label={words.issuedOn}>
|
|
545
|
+
<Input
|
|
546
|
+
id={`${id}-issued`}
|
|
547
|
+
type="date"
|
|
548
|
+
value={issued}
|
|
549
|
+
onChange={(event) => setIssued(event.target.value)}
|
|
550
|
+
/>
|
|
551
|
+
</Field>
|
|
552
|
+
<Field id={`${id}-due`} label={words.dueOn}>
|
|
553
|
+
<Input id={`${id}-due`} type="date" value={due} onChange={(event) => setDue(event.target.value)} />
|
|
554
|
+
</Field>
|
|
555
|
+
</div>
|
|
556
|
+
<fieldset className="grid gap-2" aria-describedby={errorId}>
|
|
557
|
+
<legend className="mb-1 text-sm font-medium">{words.lines}</legend>
|
|
558
|
+
{lines.map((line, index) => (
|
|
559
|
+
<div key={index} className="grid grid-cols-[1fr_4rem_6rem_auto] items-end gap-2">
|
|
560
|
+
<Field id={`${id}-line-${index}-description`} label={`${words.description} ${index + 1}`}>
|
|
561
|
+
<Input
|
|
562
|
+
id={`${id}-line-${index}-description`}
|
|
563
|
+
value={line.description}
|
|
564
|
+
onChange={(event) => change(index, 'description', event.target.value)}
|
|
565
|
+
/>
|
|
566
|
+
</Field>
|
|
567
|
+
<Field id={`${id}-line-${index}-quantity`} label={words.quantity}>
|
|
568
|
+
<Input
|
|
569
|
+
id={`${id}-line-${index}-quantity`}
|
|
570
|
+
inputMode="decimal"
|
|
571
|
+
value={line.quantity}
|
|
572
|
+
onChange={(event) => change(index, 'quantity', event.target.value)}
|
|
573
|
+
/>
|
|
574
|
+
</Field>
|
|
575
|
+
<Field id={`${id}-line-${index}-price`} label={words.unitPrice}>
|
|
576
|
+
<Input
|
|
577
|
+
id={`${id}-line-${index}-price`}
|
|
578
|
+
inputMode="decimal"
|
|
579
|
+
value={line.unitPrice}
|
|
580
|
+
onChange={(event) => change(index, 'unitPrice', event.target.value)}
|
|
581
|
+
/>
|
|
582
|
+
</Field>
|
|
583
|
+
<Button
|
|
584
|
+
type="button"
|
|
585
|
+
variant="ghost"
|
|
586
|
+
size="icon"
|
|
587
|
+
aria-label={`${words.removeLine} ${index + 1}`}
|
|
588
|
+
disabled={lines.length === 1}
|
|
589
|
+
onClick={() => setLines((current) => current.filter((_, at) => at !== index))}
|
|
590
|
+
>
|
|
591
|
+
<X aria-hidden="true" />
|
|
592
|
+
</Button>
|
|
593
|
+
</div>
|
|
594
|
+
))}
|
|
595
|
+
<div>
|
|
596
|
+
<Button
|
|
597
|
+
type="button"
|
|
598
|
+
variant="outline"
|
|
599
|
+
size="sm"
|
|
600
|
+
onClick={() => setLines((current) => [...current, EMPTY_LINE])}
|
|
601
|
+
>
|
|
602
|
+
<Plus aria-hidden="true" />
|
|
603
|
+
{words.addLine}
|
|
604
|
+
</Button>
|
|
605
|
+
</div>
|
|
606
|
+
</fieldset>
|
|
607
|
+
<Field id={`${id}-rate`} label={words.taxRate}>
|
|
608
|
+
<Input
|
|
609
|
+
id={`${id}-rate`}
|
|
610
|
+
inputMode="decimal"
|
|
611
|
+
value={rate}
|
|
612
|
+
onChange={(event) => setRate(event.target.value)}
|
|
613
|
+
/>
|
|
614
|
+
</Field>
|
|
615
|
+
</>
|
|
616
|
+
)}
|
|
617
|
+
</FormFrame>
|
|
618
|
+
);
|
|
619
|
+
}
|