@baack-software/baack-ts-crm 0.0.2 → 0.0.4
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/dist/backend.d.ts +29 -1
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +95 -0
- package/dist/backend.js.map +1 -1
- package/dist/crm-client.d.ts +55 -13
- package/dist/crm-client.d.ts.map +1 -1
- package/dist/crm-client.js +99 -15
- package/dist/crm-client.js.map +1 -1
- 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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/invoices.d.ts +7 -0
- package/dist/invoices.d.ts.map +1 -1
- package/dist/invoices.js +20 -2
- package/dist/invoices.js.map +1 -1
- package/dist/period.d.ts +15 -2
- package/dist/period.d.ts.map +1 -1
- package/dist/period.js +27 -1
- package/dist/period.js.map +1 -1
- package/dist/pipeline.d.ts +6 -0
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +18 -0
- package/dist/pipeline.js.map +1 -1
- 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/DealsBoard.d.ts +22 -3
- package/dist/react/DealsBoard.d.ts.map +1 -1
- package/dist/react/DealsBoard.js +53 -4
- package/dist/react/DealsBoard.js.map +1 -1
- package/dist/react/Details.d.ts +40 -7
- package/dist/react/Details.d.ts.map +1 -1
- package/dist/react/Details.js +180 -23
- package/dist/react/Details.js.map +1 -1
- package/dist/react/Forms.d.ts +148 -17
- package/dist/react/Forms.d.ts.map +1 -1
- package/dist/react/Forms.js +365 -75
- package/dist/react/Forms.js.map +1 -1
- package/dist/react/Sales.d.ts +25 -6
- package/dist/react/Sales.d.ts.map +1 -1
- package/dist/react/Sales.js +114 -30
- package/dist/react/Sales.js.map +1 -1
- package/dist/react/SidePanel.d.ts +10 -0
- package/dist/react/SidePanel.d.ts.map +1 -1
- package/dist/react/SidePanel.js +18 -3
- package/dist/react/SidePanel.js.map +1 -1
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/parts.d.ts +12 -2
- package/dist/react/parts.d.ts.map +1 -1
- package/dist/react/parts.js +25 -2
- package/dist/react/parts.js.map +1 -1
- 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 +89 -2
- package/dist/react/text.d.ts.map +1 -1
- package/dist/react/text.js +89 -2
- package/dist/react/text.js.map +1 -1
- package/dist/react/ui/badge.d.ts +1 -1
- package/dist/react/ui/button.d.ts +2 -2
- package/dist/react/views.d.ts +33 -11
- package/dist/react/views.d.ts.map +1 -1
- package/dist/react/views.js +96 -27
- package/dist/react/views.js.map +1 -1
- package/dist/sales.d.ts +28 -3
- package/dist/sales.d.ts.map +1 -1
- package/dist/sales.js +33 -10
- package/dist/sales.js.map +1 -1
- package/dist/store.d.ts +31 -12
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +82 -21
- package/dist/store.js.map +1 -1
- package/dist/target.d.ts +2 -1
- package/dist/target.d.ts.map +1 -1
- package/dist/target.js +6 -2
- package/dist/target.js.map +1 -1
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/backend.ts +131 -1
- package/src/crm-client.ts +162 -19
- package/src/deal-order.ts +116 -0
- package/src/index.ts +1 -0
- package/src/invoices.ts +22 -2
- package/src/period.ts +41 -2
- package/src/pipeline.ts +21 -0
- package/src/react/ChoiceCombobox.tsx +227 -0
- package/src/react/DealsBoard.tsx +171 -4
- package/src/react/Details.tsx +608 -31
- package/src/react/Forms.tsx +1006 -139
- package/src/react/Sales.tsx +332 -44
- package/src/react/SidePanel.tsx +22 -11
- package/src/react/index.ts +1 -0
- package/src/react/parts.tsx +56 -4
- package/src/react/pipeline-choice.ts +54 -0
- package/src/react/text.ts +90 -2
- package/src/react/views.tsx +222 -32
- package/src/sales.ts +57 -10
- package/src/store.ts +136 -27
- package/src/target.ts +5 -2
- package/src/types.ts +28 -0
package/src/react/views.tsx
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { Plus } from 'lucide-react';
|
|
2
|
-
import { useMemo, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
3
|
-
import {
|
|
2
|
+
import { useEffect, useId, useMemo, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
3
|
+
import { ref } from '@baack-software/baack-ts-tasks';
|
|
4
|
+
import { DEAL_ORDERS, filterDeals, type DealFilter, type DealOrder } from '../deal-order.ts';
|
|
5
|
+
import { periodRange, type DateRange, type ReportingPeriod } from '../period.ts';
|
|
6
|
+
import { pipelineOf } from '../pipeline.ts';
|
|
4
7
|
import { selectAccount, selectDashboard, selectSales, type CrmStore, type CrmStoreState } from '../store.ts';
|
|
5
8
|
import type { CrmConcept, Deal, DealOutcome, Invoice, Lead, PipelineStage } from '../types.ts';
|
|
6
9
|
import { LeadsBySourceCard, RevenueChart } from './Charts.tsx';
|
|
7
10
|
import { MetricCards, PipelineCard, TargetCard } from './DashboardCards.tsx';
|
|
8
|
-
import { DealsBoard } from './DealsBoard.tsx';
|
|
9
|
-
import { DealDetail, LeadDetail } from './Details.tsx';
|
|
11
|
+
import { DealFilters, DealsBoard } from './DealsBoard.tsx';
|
|
12
|
+
import { DealDetail, LeadDetail, type ContactDetailsAccess } from './Details.tsx';
|
|
10
13
|
import { CompanyAccountView } from './CompanyAccountView.tsx';
|
|
11
14
|
import type { CrmNames } from './format.ts';
|
|
12
15
|
import { LeadsTable } from './LeadsTable.tsx';
|
|
@@ -18,11 +21,21 @@ import {
|
|
|
18
21
|
SalesMetricCards,
|
|
19
22
|
invoiceTitle,
|
|
20
23
|
} from './Sales.tsx';
|
|
21
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
NewDealForm,
|
|
26
|
+
NewInvoiceForm,
|
|
27
|
+
NewLeadForm,
|
|
28
|
+
type CrmChoice,
|
|
29
|
+
type CrmCompanyChoice,
|
|
30
|
+
type CrmPersonChoice,
|
|
31
|
+
} from './Forms.tsx';
|
|
32
|
+
import { recallDeals, rememberDeals, usePipeline } from './pipeline-choice.ts';
|
|
22
33
|
import { SidePanelLayout, recordItemId } from './SidePanel.tsx';
|
|
23
|
-
import { SampleDataNotice } from './parts.tsx';
|
|
34
|
+
import { PeriodSelect, SampleDataNotice, periodText } from './parts.tsx';
|
|
24
35
|
import { withText, type CrmText } from './text.ts';
|
|
25
36
|
import { Alert, AlertDescription, AlertTitle } from './ui/alert.tsx';
|
|
37
|
+
import { Label } from './ui/label.tsx';
|
|
38
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select.tsx';
|
|
26
39
|
import { Button } from './ui/button.tsx';
|
|
27
40
|
import { Skeleton } from './ui/skeleton.tsx';
|
|
28
41
|
|
|
@@ -33,16 +46,33 @@ export function useCrmStore(store: CrmStore): CrmStoreState {
|
|
|
33
46
|
|
|
34
47
|
/** The host's names, falling back to the companies the store made (a converted lead's prospect) for theirs. */
|
|
35
48
|
export function useCrmNames(names: CrmNames | undefined, state: CrmStoreState): CrmNames {
|
|
36
|
-
const { companies } = state;
|
|
49
|
+
const { companies, people } = state;
|
|
37
50
|
return useMemo(
|
|
38
51
|
() => ({
|
|
39
52
|
...names,
|
|
40
53
|
company: (urn: string) => names?.company?.(urn) ?? companies.find((company) => company.urn === urn)?.name,
|
|
54
|
+
person: (urn: string) => names?.person?.(urn) ?? people.find((person) => person.urn === urn)?.name,
|
|
41
55
|
}),
|
|
42
|
-
[names, companies],
|
|
56
|
+
[names, companies, people],
|
|
43
57
|
);
|
|
44
58
|
}
|
|
45
59
|
|
|
60
|
+
/** The store's reading and setting of people's email and telephone, kept stable so the fields don't reload. */
|
|
61
|
+
const useContactDetails = (store: CrmStore): ContactDetailsAccess =>
|
|
62
|
+
useMemo(
|
|
63
|
+
() => ({
|
|
64
|
+
load: (identity) => store.contactDetails(identity),
|
|
65
|
+
save: (identity, details) => store.setContactDetails(identity, details),
|
|
66
|
+
}),
|
|
67
|
+
[store],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/** Choices with the records the store made while open, such as a lead's new contact, added where they're missing. */
|
|
71
|
+
const withMade = (choices: readonly CrmChoice[], made: readonly CrmChoice[]): readonly CrmChoice[] => [
|
|
72
|
+
...choices,
|
|
73
|
+
...made.filter((record) => !choices.some((choice) => choice.urn === record.urn)),
|
|
74
|
+
];
|
|
75
|
+
|
|
46
76
|
const CONCEPT_NAMES: Record<CrmConcept, string> = {
|
|
47
77
|
leads: 'Leads',
|
|
48
78
|
deals: 'Deals',
|
|
@@ -127,18 +157,21 @@ function NewButton({ panel, label, onClick }: { panel: string; label: string; on
|
|
|
127
157
|
}
|
|
128
158
|
|
|
129
159
|
/**
|
|
130
|
-
* The CRM dashboard for a period
|
|
131
|
-
* source, the pipeline, the host's tasks panel and the leads table.
|
|
160
|
+
* The CRM dashboard for a period: `range`, else the one picked (the last 30 days to start with), with figures against
|
|
161
|
+
* the period before, the target, leads by source, the pipeline, the host's tasks panel and the leads table.
|
|
132
162
|
*/
|
|
133
163
|
export function CrmDashboard({
|
|
134
164
|
store,
|
|
135
165
|
names,
|
|
136
166
|
text,
|
|
137
167
|
range,
|
|
168
|
+
ownCompanies,
|
|
138
169
|
tasks,
|
|
139
170
|
onOpenLead,
|
|
140
171
|
}: ViewProps & {
|
|
141
172
|
range?: DateRange | undefined;
|
|
173
|
+
/** The companies the business counts as itself (the owner and its child companies), to leave its costs out. */
|
|
174
|
+
ownCompanies?: readonly string[] | undefined;
|
|
142
175
|
/** The host's tasks panel, such as baack-ts-tasks' TasksAbout. */
|
|
143
176
|
tasks?: ReactNode;
|
|
144
177
|
onOpenLead?: ((lead: Lead) => void) | undefined;
|
|
@@ -146,15 +179,22 @@ export function CrmDashboard({
|
|
|
146
179
|
const state = useCrmStore(store);
|
|
147
180
|
names = useCrmNames(names, state);
|
|
148
181
|
const words = withText(text);
|
|
149
|
-
const
|
|
150
|
-
const
|
|
182
|
+
const [picked, setPicked] = useState<ReportingPeriod>('last30');
|
|
183
|
+
const period = useMemo(() => range ?? periodRange(picked), [range, picked]);
|
|
184
|
+
const ownKey = ownCompanies?.join(',');
|
|
185
|
+
const [pipeline] = usePipeline(ref(store.options.owner).urn, state.pipelines, state.deals);
|
|
186
|
+
const dashboard = useMemo(
|
|
187
|
+
() => selectDashboard(state, period, ownKey === undefined ? undefined : new Set(ownKey.split(',')), pipeline?.urn),
|
|
188
|
+
[state, period, ownKey, pipeline?.urn],
|
|
189
|
+
);
|
|
151
190
|
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
152
191
|
const concepts: CrmConcept[] = ['leads', 'deals', 'pipelines', 'remittances'];
|
|
153
192
|
return (
|
|
154
193
|
<div className="grid gap-6">
|
|
155
194
|
<Sample store={store} concepts={concepts} text={text} />
|
|
156
195
|
<Problems state={state} concepts={concepts} text={words} />
|
|
157
|
-
<
|
|
196
|
+
{!range && <PeriodSelect value={picked} onChange={setPicked} text={text} />}
|
|
197
|
+
<MetricCards dashboard={dashboard} text={range ? text : periodText(picked, text)} />
|
|
158
198
|
<RevenueChart days={dashboard.revenueByDay} currencyCode={dashboard.revenue.currencyCode} text={text} />
|
|
159
199
|
<div className={tasks ? 'grid items-start gap-6 lg:grid-cols-3' : 'grid items-start gap-6 lg:grid-cols-2'}>
|
|
160
200
|
<TargetCard target={dashboard.target} text={text} />
|
|
@@ -173,10 +213,16 @@ export function CrmLeadsView({
|
|
|
173
213
|
names,
|
|
174
214
|
text,
|
|
175
215
|
companies,
|
|
216
|
+
people = [],
|
|
217
|
+
team = [],
|
|
176
218
|
selected,
|
|
177
219
|
onSelect,
|
|
178
220
|
onConverted,
|
|
179
221
|
}: ViewProps & {
|
|
222
|
+
/** People a lead's contact can be chosen from: the host's customers' people. */
|
|
223
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
224
|
+
/** People a lead can be assigned to: the host's team. */
|
|
225
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
180
226
|
selected?: string | undefined;
|
|
181
227
|
onSelect?: ((lead: Lead | undefined) => void) | undefined;
|
|
182
228
|
/** After a lead becomes a deal, for example to open the deal. */
|
|
@@ -185,10 +231,16 @@ export function CrmLeadsView({
|
|
|
185
231
|
const state = useCrmStore(store);
|
|
186
232
|
names = useCrmNames(names, state);
|
|
187
233
|
const words = withText(text);
|
|
234
|
+
const contacts = withMade(people, state.people);
|
|
235
|
+
const contactDetails = useContactDetails(store);
|
|
236
|
+
const customers = withMade(
|
|
237
|
+
companies ?? [],
|
|
238
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
239
|
+
);
|
|
188
240
|
const [own, setOwn] = useState<string>();
|
|
189
241
|
const urn = onSelect ? selected : (own ?? selected);
|
|
190
242
|
const lead = state.leads.find((candidate) => candidate.urn === urn);
|
|
191
|
-
const pipeline = state.pipelines
|
|
243
|
+
const [pipeline] = usePipeline(ref(store.options.owner).urn, state.pipelines, state.deals);
|
|
192
244
|
const [creating, setCreating] = useState(false);
|
|
193
245
|
const select = (next: Lead | undefined) => {
|
|
194
246
|
setCreating(false);
|
|
@@ -210,7 +262,9 @@ export function CrmLeadsView({
|
|
|
210
262
|
creating ? (
|
|
211
263
|
<NewLeadForm
|
|
212
264
|
owner={store.options.owner}
|
|
213
|
-
companies={
|
|
265
|
+
companies={customers}
|
|
266
|
+
people={contacts}
|
|
267
|
+
team={team}
|
|
214
268
|
currencyCode={currencyOf(state)}
|
|
215
269
|
text={text}
|
|
216
270
|
onCreate={async (input, options) => select(await store.createLead(input, options))}
|
|
@@ -221,8 +275,13 @@ export function CrmLeadsView({
|
|
|
221
275
|
key={lead.urn}
|
|
222
276
|
lead={lead}
|
|
223
277
|
names={names}
|
|
278
|
+
companies={customers}
|
|
279
|
+
people={contacts}
|
|
280
|
+
team={team}
|
|
281
|
+
onChange={(changes, options) => store.updateLead(lead, changes, options)}
|
|
224
282
|
text={text}
|
|
225
283
|
converting={state.saving.has(lead.urn)}
|
|
284
|
+
contactDetails={contactDetails}
|
|
226
285
|
onSetOutcome={(closing, outcome, closedReason) =>
|
|
227
286
|
void store
|
|
228
287
|
.setLeadOutcome(closing, outcome, closedReason === undefined ? {} : { closedReason })
|
|
@@ -248,22 +307,74 @@ export function CrmLeadsView({
|
|
|
248
307
|
);
|
|
249
308
|
}
|
|
250
309
|
|
|
251
|
-
/**
|
|
310
|
+
/** The deals board's own part of what's remembered (see `rememberDeals`): its order. The workflow is the shared choice. */
|
|
311
|
+
interface DealsBoardMemory {
|
|
312
|
+
order?: DealOrder | undefined;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function recall(owner: string): DealsBoardMemory {
|
|
316
|
+
const saved = recallDeals(owner).order;
|
|
317
|
+
return { order: DEAL_ORDERS.find((order) => order === saved) };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Deals as a board, one workflow at a time: the one this user last chose, else the one with the most deals (see
|
|
322
|
+
* `usePipeline`). Filters
|
|
323
|
+
* and the columns' order sit above it; the opened deal shows to the right, as tasks do, and the board moves to its
|
|
324
|
+
* workflow.
|
|
325
|
+
*/
|
|
252
326
|
export function CrmDealsView({
|
|
253
327
|
store,
|
|
254
328
|
names,
|
|
255
329
|
text,
|
|
256
330
|
companies,
|
|
331
|
+
people = [],
|
|
332
|
+
team = [],
|
|
257
333
|
selected,
|
|
258
334
|
onSelect,
|
|
259
|
-
}: ViewProps & {
|
|
335
|
+
}: ViewProps & {
|
|
336
|
+
/** People a deal's contact can be chosen from: the host's customers' people. */
|
|
337
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
338
|
+
/** People a deal can be assigned to: the host's team. */
|
|
339
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
340
|
+
selected?: string | undefined;
|
|
341
|
+
onSelect?: ((deal: Deal | undefined) => void) | undefined;
|
|
342
|
+
}) {
|
|
260
343
|
const state = useCrmStore(store);
|
|
261
344
|
names = useCrmNames(names, state);
|
|
262
345
|
const words = withText(text);
|
|
346
|
+
const ids = useId();
|
|
263
347
|
const [own, setOwn] = useState<string>();
|
|
264
348
|
const urn = onSelect ? selected : (own ?? selected);
|
|
265
349
|
const deal = state.deals.find((candidate) => candidate.urn === urn);
|
|
266
|
-
const
|
|
350
|
+
const owner = ref(store.options.owner).urn;
|
|
351
|
+
const [memory, setMemory] = useState<DealsBoardMemory>(() => recall(owner));
|
|
352
|
+
const update = (changes: DealsBoardMemory) =>
|
|
353
|
+
setMemory((current) => {
|
|
354
|
+
const next = { ...current, ...changes };
|
|
355
|
+
rememberDeals(owner, next);
|
|
356
|
+
return next;
|
|
357
|
+
});
|
|
358
|
+
const [shown, setShown] = usePipeline(owner, state.pipelines, state.deals);
|
|
359
|
+
const order = memory.order ?? 'weighted';
|
|
360
|
+
const [filter, setFilter] = useState<DealFilter>({});
|
|
361
|
+
// An opened deal in another workflow, such as one followed from a customer's page, brings the board to it.
|
|
362
|
+
const opened = deal && pipelineOf(deal, state.pipelines)?.urn;
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
if (opened) setShown(opened);
|
|
365
|
+
// setShown is stable in effect: it only sets state and storage.
|
|
366
|
+
}, [opened]);
|
|
367
|
+
const pipeline = shown;
|
|
368
|
+
const visible = useMemo(
|
|
369
|
+
() => filterDeals(state.deals, filter, (urn) => names?.company?.(urn) ?? names?.person?.(urn)),
|
|
370
|
+
[state.deals, filter, names],
|
|
371
|
+
);
|
|
372
|
+
const contacts = withMade(people, state.people);
|
|
373
|
+
const contactDetails = useContactDetails(store);
|
|
374
|
+
const customers = withMade(
|
|
375
|
+
companies ?? [],
|
|
376
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
377
|
+
);
|
|
267
378
|
const [creating, setCreating] = useState(false);
|
|
268
379
|
const select = (next: Deal | undefined) => {
|
|
269
380
|
setCreating(false);
|
|
@@ -278,7 +389,42 @@ export function CrmDealsView({
|
|
|
278
389
|
<div className="grid gap-6">
|
|
279
390
|
<Sample store={store} concepts={['deals', 'pipelines']} text={text} />
|
|
280
391
|
<Problems state={state} concepts={['deals', 'pipelines']} text={words} />
|
|
281
|
-
{pipeline &&
|
|
392
|
+
{pipeline && (
|
|
393
|
+
<div className="flex flex-wrap items-end justify-between gap-3">
|
|
394
|
+
{state.pipelines.length > 1 ? (
|
|
395
|
+
<div className="grid gap-1.5">
|
|
396
|
+
<Label htmlFor={`${ids}-workflow`}>{words.workflow}</Label>
|
|
397
|
+
<Select value={pipeline.urn} onValueChange={setShown}>
|
|
398
|
+
<SelectTrigger id={`${ids}-workflow`} className="w-56">
|
|
399
|
+
<SelectValue />
|
|
400
|
+
</SelectTrigger>
|
|
401
|
+
<SelectContent>
|
|
402
|
+
{state.pipelines.map((option) => (
|
|
403
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
404
|
+
{option.name}
|
|
405
|
+
</SelectItem>
|
|
406
|
+
))}
|
|
407
|
+
</SelectContent>
|
|
408
|
+
</Select>
|
|
409
|
+
</div>
|
|
410
|
+
) : (
|
|
411
|
+
<span />
|
|
412
|
+
)}
|
|
413
|
+
<NewButton panel="new-deal" label={words.newDeal} onClick={() => setCreating(true)} />
|
|
414
|
+
</div>
|
|
415
|
+
)}
|
|
416
|
+
{pipeline && (
|
|
417
|
+
<DealFilters
|
|
418
|
+
value={filter}
|
|
419
|
+
onChange={setFilter}
|
|
420
|
+
order={order}
|
|
421
|
+
onOrder={(next) => update({ order: next })}
|
|
422
|
+
team={team}
|
|
423
|
+
companies={customers}
|
|
424
|
+
shown={visible.filter((shownDeal) => pipelineOf(shownDeal, [pipeline])).length}
|
|
425
|
+
text={text}
|
|
426
|
+
/>
|
|
427
|
+
)}
|
|
282
428
|
{pipeline && (
|
|
283
429
|
<SidePanelLayout
|
|
284
430
|
open={creating ? 'new-deal' : deal?.urn}
|
|
@@ -288,9 +434,12 @@ export function CrmDealsView({
|
|
|
288
434
|
panel={
|
|
289
435
|
creating ? (
|
|
290
436
|
<NewDealForm
|
|
437
|
+
// The workflow shown on the board first, so a new deal lands where it's being looked at.
|
|
291
438
|
owner={store.options.owner}
|
|
292
|
-
|
|
293
|
-
companies={
|
|
439
|
+
pipelines={[pipeline, ...state.pipelines.filter((other) => other.urn !== pipeline.urn)]}
|
|
440
|
+
companies={customers}
|
|
441
|
+
people={contacts}
|
|
442
|
+
team={team}
|
|
294
443
|
currencyCode={currencyOf(state)}
|
|
295
444
|
text={text}
|
|
296
445
|
onCreate={async (input) => select(await store.createDeal(input))}
|
|
@@ -299,10 +448,20 @@ export function CrmDealsView({
|
|
|
299
448
|
deal && (
|
|
300
449
|
<DealDetail
|
|
301
450
|
deal={deal}
|
|
302
|
-
|
|
451
|
+
pipelines={state.pipelines}
|
|
303
452
|
names={names}
|
|
304
|
-
|
|
453
|
+
companies={customers}
|
|
454
|
+
people={contacts}
|
|
455
|
+
team={team}
|
|
456
|
+
onMove={(moving, stage) => {
|
|
457
|
+
move(moving, stage);
|
|
458
|
+
// Moved to another workflow: the board follows it.
|
|
459
|
+
const to = state.pipelines.find((option) => option.stages.some((s) => s.urn === stage.urn));
|
|
460
|
+
if (to) setShown(to.urn);
|
|
461
|
+
}}
|
|
305
462
|
onSetOutcome={setOutcome}
|
|
463
|
+
onChange={(changes) => store.updateDeal(deal, changes)}
|
|
464
|
+
contactDetails={contactDetails}
|
|
306
465
|
saving={state.saving.has(deal.urn)}
|
|
307
466
|
text={text}
|
|
308
467
|
/>
|
|
@@ -311,8 +470,9 @@ export function CrmDealsView({
|
|
|
311
470
|
}
|
|
312
471
|
>
|
|
313
472
|
<DealsBoard
|
|
314
|
-
deals={
|
|
473
|
+
deals={visible}
|
|
315
474
|
pipeline={pipeline}
|
|
475
|
+
order={order}
|
|
316
476
|
names={names}
|
|
317
477
|
onOpen={select}
|
|
318
478
|
onMove={move}
|
|
@@ -327,19 +487,28 @@ export function CrmDealsView({
|
|
|
327
487
|
}
|
|
328
488
|
|
|
329
489
|
/**
|
|
330
|
-
* Sales for a period (
|
|
331
|
-
* before, payments over time, outstanding by age, payments by method,
|
|
332
|
-
* the right with its payments, where another can be recorded.
|
|
490
|
+
* Sales for a period (`range`, else the one picked, the last 30 days to start with): invoiced, received, outstanding,
|
|
491
|
+
* sales tax, costs and drafts against the period before, payments over time, outstanding by age, payments by method,
|
|
492
|
+
* and the invoices; the opened invoice shows to the right with its payments, where another can be recorded.
|
|
333
493
|
*/
|
|
334
494
|
export function CrmSalesView({
|
|
335
495
|
store,
|
|
336
496
|
names,
|
|
337
497
|
text,
|
|
338
498
|
companies,
|
|
499
|
+
people = [],
|
|
500
|
+
ownCompanies,
|
|
339
501
|
range,
|
|
340
502
|
selected,
|
|
341
503
|
onSelect,
|
|
342
504
|
}: ViewProps & {
|
|
505
|
+
/**
|
|
506
|
+
* The companies the business counts as itself (the owner and its child companies): invoices they pay are costs,
|
|
507
|
+
* apart from the sales figures. The owner alone when not given.
|
|
508
|
+
*/
|
|
509
|
+
ownCompanies?: readonly string[] | undefined;
|
|
510
|
+
/** People an invoice's remitter or recipient can be. */
|
|
511
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
343
512
|
range?: DateRange | undefined;
|
|
344
513
|
selected?: string | undefined;
|
|
345
514
|
onSelect?: ((invoice: Invoice | undefined) => void) | undefined;
|
|
@@ -347,12 +516,20 @@ export function CrmSalesView({
|
|
|
347
516
|
const state = useCrmStore(store);
|
|
348
517
|
names = useCrmNames(names, state);
|
|
349
518
|
const words = withText(text);
|
|
350
|
-
const
|
|
351
|
-
const
|
|
519
|
+
const [picked, setPicked] = useState<ReportingPeriod>('last30');
|
|
520
|
+
const period = useMemo(() => range ?? periodRange(picked), [range, picked]);
|
|
521
|
+
const ownKey = ownCompanies?.join(',');
|
|
522
|
+
const ownSet = useMemo(() => (ownKey === undefined ? undefined : new Set(ownKey.split(','))), [ownKey]);
|
|
523
|
+
const summary = useMemo(() => selectSales(state, period, new Date(), ownSet), [state, period, ownSet]);
|
|
352
524
|
const [own, setOwn] = useState<string>();
|
|
353
525
|
const urn = onSelect ? selected : (own ?? selected);
|
|
354
526
|
const invoice = summary.invoices.find((candidate) => candidate.urn === urn);
|
|
355
527
|
const [creating, setCreating] = useState(false);
|
|
528
|
+
// Either party of an invoice: the host's companies, with any the store made since.
|
|
529
|
+
const parties = withMade(
|
|
530
|
+
companies ?? [],
|
|
531
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
532
|
+
);
|
|
356
533
|
const select = (next: Invoice | undefined) => {
|
|
357
534
|
setCreating(false);
|
|
358
535
|
if (onSelect) onSelect(next);
|
|
@@ -364,7 +541,8 @@ export function CrmSalesView({
|
|
|
364
541
|
<div className="grid gap-6">
|
|
365
542
|
<Sample store={store} concepts={concepts} text={text} />
|
|
366
543
|
<Problems state={state} concepts={concepts} text={words} />
|
|
367
|
-
<
|
|
544
|
+
{!range && <PeriodSelect value={picked} onChange={setPicked} text={text} />}
|
|
545
|
+
<SalesMetricCards summary={summary} text={range ? text : periodText(picked, text)} />
|
|
368
546
|
<RevenueChart days={summary.byDay} currencyCode={summary.currencyCode} text={text} />
|
|
369
547
|
<div className="grid items-start gap-6 lg:grid-cols-2">
|
|
370
548
|
<OutstandingByAgeCard summary={summary} text={text} />
|
|
@@ -380,7 +558,8 @@ export function CrmSalesView({
|
|
|
380
558
|
creating ? (
|
|
381
559
|
<NewInvoiceForm
|
|
382
560
|
owner={store.options.owner}
|
|
383
|
-
companies={
|
|
561
|
+
companies={parties}
|
|
562
|
+
people={withMade(people, state.people)}
|
|
384
563
|
deals={state.deals}
|
|
385
564
|
currencyCode={currencyOf(state)}
|
|
386
565
|
text={text}
|
|
@@ -392,6 +571,10 @@ export function CrmSalesView({
|
|
|
392
571
|
invoice={invoice}
|
|
393
572
|
payments={summary.payments.filter((payment) => payment.paymentInvoice.urn === invoice.urn)}
|
|
394
573
|
names={names}
|
|
574
|
+
companies={parties}
|
|
575
|
+
people={withMade(people, state.people)}
|
|
576
|
+
onChange={(changes, options) => store.updateInvoice(invoice, changes, options)}
|
|
577
|
+
saving={state.saving.has(invoice.urn)}
|
|
395
578
|
onRecord={(input) => store.recordPayment(input)}
|
|
396
579
|
text={text}
|
|
397
580
|
/>
|
|
@@ -399,7 +582,14 @@ export function CrmSalesView({
|
|
|
399
582
|
)
|
|
400
583
|
}
|
|
401
584
|
>
|
|
402
|
-
<InvoicesTable
|
|
585
|
+
<InvoicesTable
|
|
586
|
+
invoices={summary.invoices}
|
|
587
|
+
names={names}
|
|
588
|
+
own={ownSet}
|
|
589
|
+
onOpen={select}
|
|
590
|
+
selected={invoice?.urn}
|
|
591
|
+
text={text}
|
|
592
|
+
/>
|
|
403
593
|
</SidePanelLayout>
|
|
404
594
|
</div>
|
|
405
595
|
);
|
package/src/sales.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { addMoney, type MoneyTotals } from './money.ts';
|
|
|
2
2
|
import { inRange, periodChange, previousRange, type DateRange } from './period.ts';
|
|
3
3
|
import {
|
|
4
4
|
invoiceFromRemittances,
|
|
5
|
+
isCostInvoice,
|
|
5
6
|
outstandingByAge,
|
|
6
7
|
outstandingTotals,
|
|
7
8
|
overdueTotals,
|
|
@@ -18,6 +19,17 @@ export interface ComparedTotals {
|
|
|
18
19
|
change: number | undefined;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
/** Invoices the business pays (see `isCostInvoice`), kept out of the sales figures. */
|
|
23
|
+
export interface CostsSummary {
|
|
24
|
+
/** Cost invoices issued in the period. */
|
|
25
|
+
invoiced: ComparedTotals;
|
|
26
|
+
/** Paid on cost invoices in the period, less refunds. */
|
|
27
|
+
paid: MoneyTotals;
|
|
28
|
+
/** Still to pay now on cost invoices, and how much of it is overdue. */
|
|
29
|
+
outstanding: MoneyTotals;
|
|
30
|
+
overdue: MoneyTotals;
|
|
31
|
+
}
|
|
32
|
+
|
|
21
33
|
export interface SalesSummary {
|
|
22
34
|
range: DateRange;
|
|
23
35
|
/** The currency with the most money in the period, which charts and changes use. */
|
|
@@ -31,10 +43,14 @@ export interface SalesSummary {
|
|
|
31
43
|
/** Still to pay now, and how much of it is overdue. */
|
|
32
44
|
outstanding: MoneyTotals;
|
|
33
45
|
overdue: MoneyTotals;
|
|
46
|
+
/** Draft invoices now, whenever drafted: not yet sent, so not in the other figures. */
|
|
47
|
+
drafts: { count: number; totals: MoneyTotals };
|
|
34
48
|
byDay: { day: string; totals: MoneyTotals }[];
|
|
35
49
|
byAge: { bucket: AgeBucket; totals: MoneyTotals }[];
|
|
36
50
|
byMethod: { method: RemittanceMethod; totals: MoneyTotals }[];
|
|
37
|
-
/**
|
|
51
|
+
/** Invoices the business pays, apart from the sales figures above. */
|
|
52
|
+
costs: CostsSummary;
|
|
53
|
+
/** Every invoice, sales and costs, newest first, with its payment fields worked out from its remittances. */
|
|
38
54
|
invoices: Invoice[];
|
|
39
55
|
/** Every remittance, newest first. */
|
|
40
56
|
payments: Remittance[];
|
|
@@ -75,25 +91,43 @@ const largest = (...sets: MoneyTotals[]): string | undefined => {
|
|
|
75
91
|
return Object.entries(all).sort(([, a], [, b]) => b - a)[0]?.[0];
|
|
76
92
|
};
|
|
77
93
|
|
|
78
|
-
/**
|
|
94
|
+
/** Remittances against sales invoices only, leaving out payments the business made on its costs. */
|
|
95
|
+
export function salesRemittances(
|
|
96
|
+
data: { invoices: readonly Invoice[]; remittances: readonly Remittance[] },
|
|
97
|
+
own?: ReadonlySet<string>,
|
|
98
|
+
): Remittance[] {
|
|
99
|
+
const costs = new Set(data.invoices.filter((invoice) => isCostInvoice(invoice, own)).map((invoice) => invoice.urn));
|
|
100
|
+
return data.remittances.filter((remittance) => !costs.has(remittance.paymentInvoice.urn));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Sales for a period from invoices and remittances, compared with the period before. Invoices the business pays (see
|
|
105
|
+
* `isCostInvoice`; `own` is the companies it counts as itself) are costs: summed apart, not counted as sales.
|
|
106
|
+
*/
|
|
79
107
|
export function salesSummary(
|
|
80
108
|
data: { invoices: readonly Invoice[]; remittances: readonly Remittance[] },
|
|
81
109
|
range: DateRange,
|
|
82
110
|
now = new Date(),
|
|
111
|
+
own?: ReadonlySet<string>,
|
|
83
112
|
): SalesSummary {
|
|
84
113
|
const before = previousRange(range);
|
|
85
|
-
const
|
|
114
|
+
const all = withPayments(data.invoices, data.remittances).toSorted((a, b) =>
|
|
86
115
|
(b.issuedTimestamp ?? '').localeCompare(a.issuedTimestamp ?? ''),
|
|
87
116
|
);
|
|
88
|
-
const
|
|
89
|
-
|
|
117
|
+
const costInvoices = all.filter((invoice) => isCostInvoice(invoice, own));
|
|
118
|
+
const invoices = all.filter((invoice) => !isCostInvoice(invoice, own));
|
|
119
|
+
const costUrns = new Set(costInvoices.map((invoice) => invoice.urn));
|
|
120
|
+
const costPayments = data.remittances.filter((remittance) => costUrns.has(remittance.paymentInvoice.urn));
|
|
121
|
+
const remittances = data.remittances.filter((remittance) => !costUrns.has(remittance.paymentInvoice.urn));
|
|
122
|
+
const issuedIn = (period: DateRange, list = invoices) =>
|
|
123
|
+
list.filter((invoice) => ISSUED(invoice) && inRange(invoice.issuedTimestamp, period));
|
|
90
124
|
const sum = (list: Invoice[], amount: (invoice: Invoice) => Money | null | undefined) =>
|
|
91
125
|
list.reduce<MoneyTotals>((totals, invoice) => addMoney(totals, amount(invoice)), {});
|
|
92
126
|
|
|
93
127
|
const invoicedNow = sum(issuedIn(range), (invoice) => invoice.total);
|
|
94
128
|
const invoicedBefore = sum(issuedIn(before), (invoice) => invoice.total);
|
|
95
|
-
const receivedNow = paymentsReceived(
|
|
96
|
-
const receivedBefore = paymentsReceived(
|
|
129
|
+
const receivedNow = paymentsReceived(remittances, range);
|
|
130
|
+
const receivedBefore = paymentsReceived(remittances, before);
|
|
97
131
|
const taxNow = sum(issuedIn(range), (invoice) => invoice.tax);
|
|
98
132
|
const taxBefore = sum(issuedIn(before), (invoice) => invoice.tax);
|
|
99
133
|
const currencyCode = largest(invoicedNow, receivedNow, invoicedBefore, receivedBefore);
|
|
@@ -111,10 +145,23 @@ export function salesSummary(
|
|
|
111
145
|
tax: compared(taxNow, taxBefore),
|
|
112
146
|
outstanding: outstandingTotals(invoices),
|
|
113
147
|
overdue: overdueTotals(invoices, now),
|
|
114
|
-
|
|
148
|
+
drafts: (() => {
|
|
149
|
+
const drafts = invoices.filter((invoice) => invoice.status === 'DRAFT');
|
|
150
|
+
return { count: drafts.length, totals: sum(drafts, (invoice) => invoice.total) };
|
|
151
|
+
})(),
|
|
152
|
+
byDay: paymentsByDay(remittances, range),
|
|
115
153
|
byAge: outstandingByAge(invoices, now),
|
|
116
|
-
byMethod: paymentsByMethod(
|
|
117
|
-
|
|
154
|
+
byMethod: paymentsByMethod(remittances, range),
|
|
155
|
+
costs: {
|
|
156
|
+
invoiced: compared(
|
|
157
|
+
sum(issuedIn(range, costInvoices), (invoice) => invoice.total),
|
|
158
|
+
sum(issuedIn(before, costInvoices), (invoice) => invoice.total),
|
|
159
|
+
),
|
|
160
|
+
paid: paymentsReceived(costPayments, range),
|
|
161
|
+
outstanding: outstandingTotals(costInvoices),
|
|
162
|
+
overdue: overdueTotals(costInvoices, now),
|
|
163
|
+
},
|
|
164
|
+
invoices: all,
|
|
118
165
|
payments: data.remittances.toSorted((a, b) => b.receivedTimestamp.localeCompare(a.receivedTimestamp)),
|
|
119
166
|
};
|
|
120
167
|
}
|