@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,443 @@
|
|
|
1
|
+
import { Plus } from 'lucide-react';
|
|
2
|
+
import { useMemo, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
3
|
+
import { lastDays, type DateRange } from '../period.ts';
|
|
4
|
+
import { selectAccount, selectDashboard, selectSales, type CrmStore, type CrmStoreState } from '../store.ts';
|
|
5
|
+
import type { CrmConcept, Deal, DealOutcome, Invoice, Lead, PipelineStage } from '../types.ts';
|
|
6
|
+
import { LeadsBySourceCard, RevenueChart } from './Charts.tsx';
|
|
7
|
+
import { MetricCards, PipelineCard, TargetCard } from './DashboardCards.tsx';
|
|
8
|
+
import { DealsBoard } from './DealsBoard.tsx';
|
|
9
|
+
import { DealDetail, LeadDetail } from './Details.tsx';
|
|
10
|
+
import { CompanyAccountView } from './CompanyAccountView.tsx';
|
|
11
|
+
import type { CrmNames } from './format.ts';
|
|
12
|
+
import { LeadsTable } from './LeadsTable.tsx';
|
|
13
|
+
import {
|
|
14
|
+
InvoiceDetail,
|
|
15
|
+
InvoicesTable,
|
|
16
|
+
OutstandingByAgeCard,
|
|
17
|
+
PaymentMethodsCard,
|
|
18
|
+
SalesMetricCards,
|
|
19
|
+
invoiceTitle,
|
|
20
|
+
} from './Sales.tsx';
|
|
21
|
+
import { NewDealForm, NewInvoiceForm, NewLeadForm, type CrmCompanyChoice } from './Forms.tsx';
|
|
22
|
+
import { SidePanelLayout, recordItemId } from './SidePanel.tsx';
|
|
23
|
+
import { SampleDataNotice } from './parts.tsx';
|
|
24
|
+
import { withText, type CrmText } from './text.ts';
|
|
25
|
+
import { Alert, AlertDescription, AlertTitle } from './ui/alert.tsx';
|
|
26
|
+
import { Button } from './ui/button.tsx';
|
|
27
|
+
import { Skeleton } from './ui/skeleton.tsx';
|
|
28
|
+
|
|
29
|
+
/** The store's state, re-rendering on every change. */
|
|
30
|
+
export function useCrmStore(store: CrmStore): CrmStoreState {
|
|
31
|
+
return useSyncExternalStore(store.subscribe, store.getState, store.getState);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The host's names, falling back to the companies the store made (a converted lead's prospect) for theirs. */
|
|
35
|
+
export function useCrmNames(names: CrmNames | undefined, state: CrmStoreState): CrmNames {
|
|
36
|
+
const { companies } = state;
|
|
37
|
+
return useMemo(
|
|
38
|
+
() => ({
|
|
39
|
+
...names,
|
|
40
|
+
company: (urn: string) => names?.company?.(urn) ?? companies.find((company) => company.urn === urn)?.name,
|
|
41
|
+
}),
|
|
42
|
+
[names, companies],
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const CONCEPT_NAMES: Record<CrmConcept, string> = {
|
|
47
|
+
leads: 'Leads',
|
|
48
|
+
deals: 'Deals',
|
|
49
|
+
pipelines: 'Pipelines',
|
|
50
|
+
invoices: 'Invoices',
|
|
51
|
+
remittances: 'Payments',
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The notice naming which of a view's concepts come from sample data. */
|
|
55
|
+
function Sample({
|
|
56
|
+
store,
|
|
57
|
+
concepts,
|
|
58
|
+
text,
|
|
59
|
+
}: {
|
|
60
|
+
store: CrmStore;
|
|
61
|
+
concepts: CrmConcept[];
|
|
62
|
+
text?: Partial<CrmText> | undefined;
|
|
63
|
+
}) {
|
|
64
|
+
const sample = concepts.filter((concept) => store.client.isSample(concept)).map((concept) => CONCEPT_NAMES[concept]);
|
|
65
|
+
return <SampleDataNotice concepts={sample} text={text} />;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function Loading({ label }: { label: string }) {
|
|
69
|
+
return (
|
|
70
|
+
<div role="status" aria-label={label} className="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
|
|
71
|
+
{Array.from({ length: 4 }, (_, n) => (
|
|
72
|
+
<Skeleton key={n} className="h-28 rounded-xl" />
|
|
73
|
+
))}
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Says which concepts couldn't be loaded, and why: not in the API yet, or an error. */
|
|
79
|
+
function Problems({ state, concepts, text }: { state: CrmStoreState; concepts: CrmConcept[]; text: CrmText }) {
|
|
80
|
+
const problems = concepts.filter((concept) => ['unavailable', 'error'].includes(state.concepts[concept].status));
|
|
81
|
+
if (problems.length === 0) return null;
|
|
82
|
+
return (
|
|
83
|
+
<Alert>
|
|
84
|
+
<AlertTitle>{text.unavailable}</AlertTitle>
|
|
85
|
+
<AlertDescription>
|
|
86
|
+
{problems.map((concept) => {
|
|
87
|
+
const { status, error } = state.concepts[concept];
|
|
88
|
+
return (
|
|
89
|
+
<p key={concept}>
|
|
90
|
+
{CONCEPT_NAMES[concept]}:{' '}
|
|
91
|
+
{status === 'unavailable' ? text.sampleDataHint : error instanceof Error ? error.message : String(error)}
|
|
92
|
+
</p>
|
|
93
|
+
);
|
|
94
|
+
})}
|
|
95
|
+
</AlertDescription>
|
|
96
|
+
</Alert>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface ViewProps {
|
|
101
|
+
store: CrmStore;
|
|
102
|
+
names?: CrmNames | undefined;
|
|
103
|
+
text?: Partial<CrmText> | undefined;
|
|
104
|
+
/** The companies new records can be linked to: the host's customers, named. */
|
|
105
|
+
companies?: readonly CrmCompanyChoice[] | undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The currency new records start in: the one the company's records use, else GBP. */
|
|
109
|
+
const currencyOf = (state: CrmStoreState): string =>
|
|
110
|
+
state.invoices[0]?.currencyCode ??
|
|
111
|
+
[...state.deals, ...state.leads].find((record) => record.targetValue)?.targetValue?.currencyCode ??
|
|
112
|
+
'GBP';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* "New ...": opens the form in the side panel, in place of a record. Its id is the panel's, so closing the form
|
|
116
|
+
* returns focus to it.
|
|
117
|
+
*/
|
|
118
|
+
function NewButton({ panel, label, onClick }: { panel: string; label: string; onClick: () => void }) {
|
|
119
|
+
return (
|
|
120
|
+
<div className="flex justify-end">
|
|
121
|
+
<Button id={recordItemId(panel)} onClick={onClick}>
|
|
122
|
+
<Plus aria-hidden="true" />
|
|
123
|
+
{label}
|
|
124
|
+
</Button>
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The CRM dashboard for a period (default the last 28 days): figures against the period before, the target, leads by
|
|
131
|
+
* source, the pipeline, the host's tasks panel and the leads table.
|
|
132
|
+
*/
|
|
133
|
+
export function CrmDashboard({
|
|
134
|
+
store,
|
|
135
|
+
names,
|
|
136
|
+
text,
|
|
137
|
+
range,
|
|
138
|
+
tasks,
|
|
139
|
+
onOpenLead,
|
|
140
|
+
}: ViewProps & {
|
|
141
|
+
range?: DateRange | undefined;
|
|
142
|
+
/** The host's tasks panel, such as baack-ts-tasks' TasksAbout. */
|
|
143
|
+
tasks?: ReactNode;
|
|
144
|
+
onOpenLead?: ((lead: Lead) => void) | undefined;
|
|
145
|
+
}) {
|
|
146
|
+
const state = useCrmStore(store);
|
|
147
|
+
names = useCrmNames(names, state);
|
|
148
|
+
const words = withText(text);
|
|
149
|
+
const period = useMemo(() => range ?? lastDays(28), [range]);
|
|
150
|
+
const dashboard = useMemo(() => selectDashboard(state, period), [state, period]);
|
|
151
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
152
|
+
const concepts: CrmConcept[] = ['leads', 'deals', 'pipelines', 'remittances'];
|
|
153
|
+
return (
|
|
154
|
+
<div className="grid gap-6">
|
|
155
|
+
<Sample store={store} concepts={concepts} text={text} />
|
|
156
|
+
<Problems state={state} concepts={concepts} text={words} />
|
|
157
|
+
<MetricCards dashboard={dashboard} text={text} />
|
|
158
|
+
<RevenueChart days={dashboard.revenueByDay} currencyCode={dashboard.revenue.currencyCode} text={text} />
|
|
159
|
+
<div className={tasks ? 'grid items-start gap-6 lg:grid-cols-3' : 'grid items-start gap-6 lg:grid-cols-2'}>
|
|
160
|
+
<TargetCard target={dashboard.target} text={text} />
|
|
161
|
+
<LeadsBySourceCard counts={dashboard.leadsBySource} text={text} />
|
|
162
|
+
{tasks ?? null}
|
|
163
|
+
</div>
|
|
164
|
+
<PipelineCard stages={dashboard.pipeline} text={text} />
|
|
165
|
+
<LeadsTable leads={state.leads} names={names} onOpen={onOpenLead} text={text} />
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Leads in a table; the opened lead shows to the right, as tasks do, where it can be converted into a deal. */
|
|
171
|
+
export function CrmLeadsView({
|
|
172
|
+
store,
|
|
173
|
+
names,
|
|
174
|
+
text,
|
|
175
|
+
companies,
|
|
176
|
+
selected,
|
|
177
|
+
onSelect,
|
|
178
|
+
onConverted,
|
|
179
|
+
}: ViewProps & {
|
|
180
|
+
selected?: string | undefined;
|
|
181
|
+
onSelect?: ((lead: Lead | undefined) => void) | undefined;
|
|
182
|
+
/** After a lead becomes a deal, for example to open the deal. */
|
|
183
|
+
onConverted?: ((deal: Deal) => void) | undefined;
|
|
184
|
+
}) {
|
|
185
|
+
const state = useCrmStore(store);
|
|
186
|
+
names = useCrmNames(names, state);
|
|
187
|
+
const words = withText(text);
|
|
188
|
+
const [own, setOwn] = useState<string>();
|
|
189
|
+
const urn = onSelect ? selected : (own ?? selected);
|
|
190
|
+
const lead = state.leads.find((candidate) => candidate.urn === urn);
|
|
191
|
+
const pipeline = state.pipelines[0];
|
|
192
|
+
const [creating, setCreating] = useState(false);
|
|
193
|
+
const select = (next: Lead | undefined) => {
|
|
194
|
+
setCreating(false);
|
|
195
|
+
if (onSelect) onSelect(next);
|
|
196
|
+
else setOwn(next?.urn);
|
|
197
|
+
};
|
|
198
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
199
|
+
return (
|
|
200
|
+
<div className="grid gap-6">
|
|
201
|
+
<Sample store={store} concepts={['leads']} text={text} />
|
|
202
|
+
<Problems state={state} concepts={['leads']} text={words} />
|
|
203
|
+
<NewButton panel="new-lead" label={words.newLead} onClick={() => setCreating(true)} />
|
|
204
|
+
<SidePanelLayout
|
|
205
|
+
open={creating ? 'new-lead' : lead?.urn}
|
|
206
|
+
title={creating ? words.newLead : (lead?.title ?? words.lead)}
|
|
207
|
+
onClose={() => select(undefined)}
|
|
208
|
+
text={text}
|
|
209
|
+
panel={
|
|
210
|
+
creating ? (
|
|
211
|
+
<NewLeadForm
|
|
212
|
+
owner={store.options.owner}
|
|
213
|
+
companies={companies}
|
|
214
|
+
currencyCode={currencyOf(state)}
|
|
215
|
+
text={text}
|
|
216
|
+
onCreate={async (input, options) => select(await store.createLead(input, options))}
|
|
217
|
+
/>
|
|
218
|
+
) : (
|
|
219
|
+
lead && (
|
|
220
|
+
<LeadDetail
|
|
221
|
+
key={lead.urn}
|
|
222
|
+
lead={lead}
|
|
223
|
+
names={names}
|
|
224
|
+
text={text}
|
|
225
|
+
converting={state.saving.has(lead.urn)}
|
|
226
|
+
onSetOutcome={(closing, outcome, closedReason) =>
|
|
227
|
+
void store
|
|
228
|
+
.setLeadOutcome(closing, outcome, closedReason === undefined ? {} : { closedReason })
|
|
229
|
+
.catch(() => undefined)
|
|
230
|
+
}
|
|
231
|
+
onConvert={
|
|
232
|
+
pipeline
|
|
233
|
+
? async (open, choice) => {
|
|
234
|
+
// A refused conversion is rolled back and kept as the store's writeError.
|
|
235
|
+
const result = await store.convertLead(open, { pipeline, ...choice }).catch(() => undefined);
|
|
236
|
+
if (result) onConverted?.(result.deal);
|
|
237
|
+
}
|
|
238
|
+
: undefined
|
|
239
|
+
}
|
|
240
|
+
/>
|
|
241
|
+
)
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
>
|
|
245
|
+
<LeadsTable leads={state.leads} names={names} onOpen={select} selected={lead?.urn} text={text} />
|
|
246
|
+
</SidePanelLayout>
|
|
247
|
+
</div>
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Deals as a board on the first pipeline; the opened deal shows to the right, as tasks do. */
|
|
252
|
+
export function CrmDealsView({
|
|
253
|
+
store,
|
|
254
|
+
names,
|
|
255
|
+
text,
|
|
256
|
+
companies,
|
|
257
|
+
selected,
|
|
258
|
+
onSelect,
|
|
259
|
+
}: ViewProps & { selected?: string | undefined; onSelect?: ((deal: Deal | undefined) => void) | undefined }) {
|
|
260
|
+
const state = useCrmStore(store);
|
|
261
|
+
names = useCrmNames(names, state);
|
|
262
|
+
const words = withText(text);
|
|
263
|
+
const [own, setOwn] = useState<string>();
|
|
264
|
+
const urn = onSelect ? selected : (own ?? selected);
|
|
265
|
+
const deal = state.deals.find((candidate) => candidate.urn === urn);
|
|
266
|
+
const pipeline = state.pipelines[0];
|
|
267
|
+
const [creating, setCreating] = useState(false);
|
|
268
|
+
const select = (next: Deal | undefined) => {
|
|
269
|
+
setCreating(false);
|
|
270
|
+
if (onSelect) onSelect(next);
|
|
271
|
+
else setOwn(next?.urn);
|
|
272
|
+
};
|
|
273
|
+
const move = (moving: Deal, stage: PipelineStage) => void store.moveDeal(moving, stage).catch(() => undefined);
|
|
274
|
+
const setOutcome = (closing: Deal, outcome: DealOutcome, lostReason?: string) =>
|
|
275
|
+
void store.setOutcome(closing, outcome, lostReason === undefined ? {} : { lostReason }).catch(() => undefined);
|
|
276
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
277
|
+
return (
|
|
278
|
+
<div className="grid gap-6">
|
|
279
|
+
<Sample store={store} concepts={['deals', 'pipelines']} text={text} />
|
|
280
|
+
<Problems state={state} concepts={['deals', 'pipelines']} text={words} />
|
|
281
|
+
{pipeline && <NewButton panel="new-deal" label={words.newDeal} onClick={() => setCreating(true)} />}
|
|
282
|
+
{pipeline && (
|
|
283
|
+
<SidePanelLayout
|
|
284
|
+
open={creating ? 'new-deal' : deal?.urn}
|
|
285
|
+
title={creating ? words.newDeal : (deal?.title ?? words.deals)}
|
|
286
|
+
onClose={() => select(undefined)}
|
|
287
|
+
text={text}
|
|
288
|
+
panel={
|
|
289
|
+
creating ? (
|
|
290
|
+
<NewDealForm
|
|
291
|
+
owner={store.options.owner}
|
|
292
|
+
pipeline={pipeline}
|
|
293
|
+
companies={companies}
|
|
294
|
+
currencyCode={currencyOf(state)}
|
|
295
|
+
text={text}
|
|
296
|
+
onCreate={async (input) => select(await store.createDeal(input))}
|
|
297
|
+
/>
|
|
298
|
+
) : (
|
|
299
|
+
deal && (
|
|
300
|
+
<DealDetail
|
|
301
|
+
deal={deal}
|
|
302
|
+
pipeline={pipeline}
|
|
303
|
+
names={names}
|
|
304
|
+
onMove={move}
|
|
305
|
+
onSetOutcome={setOutcome}
|
|
306
|
+
saving={state.saving.has(deal.urn)}
|
|
307
|
+
text={text}
|
|
308
|
+
/>
|
|
309
|
+
)
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
>
|
|
313
|
+
<DealsBoard
|
|
314
|
+
deals={state.deals}
|
|
315
|
+
pipeline={pipeline}
|
|
316
|
+
names={names}
|
|
317
|
+
onOpen={select}
|
|
318
|
+
onMove={move}
|
|
319
|
+
selected={deal?.urn}
|
|
320
|
+
saving={state.saving}
|
|
321
|
+
text={text}
|
|
322
|
+
/>
|
|
323
|
+
</SidePanelLayout>
|
|
324
|
+
)}
|
|
325
|
+
</div>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Sales for a period (default the last 28 days): invoiced, received, outstanding and sales tax against the period
|
|
331
|
+
* before, payments over time, outstanding by age, payments by method, and the invoices; the opened invoice shows to
|
|
332
|
+
* the right with its payments, where another can be recorded.
|
|
333
|
+
*/
|
|
334
|
+
export function CrmSalesView({
|
|
335
|
+
store,
|
|
336
|
+
names,
|
|
337
|
+
text,
|
|
338
|
+
companies,
|
|
339
|
+
range,
|
|
340
|
+
selected,
|
|
341
|
+
onSelect,
|
|
342
|
+
}: ViewProps & {
|
|
343
|
+
range?: DateRange | undefined;
|
|
344
|
+
selected?: string | undefined;
|
|
345
|
+
onSelect?: ((invoice: Invoice | undefined) => void) | undefined;
|
|
346
|
+
}) {
|
|
347
|
+
const state = useCrmStore(store);
|
|
348
|
+
names = useCrmNames(names, state);
|
|
349
|
+
const words = withText(text);
|
|
350
|
+
const period = useMemo(() => range ?? lastDays(28), [range]);
|
|
351
|
+
const summary = useMemo(() => selectSales(state, period), [state, period]);
|
|
352
|
+
const [own, setOwn] = useState<string>();
|
|
353
|
+
const urn = onSelect ? selected : (own ?? selected);
|
|
354
|
+
const invoice = summary.invoices.find((candidate) => candidate.urn === urn);
|
|
355
|
+
const [creating, setCreating] = useState(false);
|
|
356
|
+
const select = (next: Invoice | undefined) => {
|
|
357
|
+
setCreating(false);
|
|
358
|
+
if (onSelect) onSelect(next);
|
|
359
|
+
else setOwn(next?.urn);
|
|
360
|
+
};
|
|
361
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
362
|
+
const concepts: CrmConcept[] = ['invoices', 'remittances'];
|
|
363
|
+
return (
|
|
364
|
+
<div className="grid gap-6">
|
|
365
|
+
<Sample store={store} concepts={concepts} text={text} />
|
|
366
|
+
<Problems state={state} concepts={concepts} text={words} />
|
|
367
|
+
<SalesMetricCards summary={summary} text={text} />
|
|
368
|
+
<RevenueChart days={summary.byDay} currencyCode={summary.currencyCode} text={text} />
|
|
369
|
+
<div className="grid items-start gap-6 lg:grid-cols-2">
|
|
370
|
+
<OutstandingByAgeCard summary={summary} text={text} />
|
|
371
|
+
<PaymentMethodsCard summary={summary} text={text} />
|
|
372
|
+
</div>
|
|
373
|
+
<NewButton panel="new-invoice" label={words.newInvoice} onClick={() => setCreating(true)} />
|
|
374
|
+
<SidePanelLayout
|
|
375
|
+
open={creating ? 'new-invoice' : invoice?.urn}
|
|
376
|
+
title={creating ? words.newInvoice : invoice ? invoiceTitle(invoice) : words.invoice}
|
|
377
|
+
onClose={() => select(undefined)}
|
|
378
|
+
text={text}
|
|
379
|
+
panel={
|
|
380
|
+
creating ? (
|
|
381
|
+
<NewInvoiceForm
|
|
382
|
+
owner={store.options.owner}
|
|
383
|
+
companies={companies}
|
|
384
|
+
deals={state.deals}
|
|
385
|
+
currencyCode={currencyOf(state)}
|
|
386
|
+
text={text}
|
|
387
|
+
onCreate={async (input, lines, options) => select(await store.createInvoice(input, lines, options))}
|
|
388
|
+
/>
|
|
389
|
+
) : (
|
|
390
|
+
invoice && (
|
|
391
|
+
<InvoiceDetail
|
|
392
|
+
invoice={invoice}
|
|
393
|
+
payments={summary.payments.filter((payment) => payment.paymentInvoice.urn === invoice.urn)}
|
|
394
|
+
names={names}
|
|
395
|
+
onRecord={(input) => store.recordPayment(input)}
|
|
396
|
+
text={text}
|
|
397
|
+
/>
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
}
|
|
401
|
+
>
|
|
402
|
+
<InvoicesTable invoices={summary.invoices} names={names} onOpen={select} selected={invoice?.urn} text={text} />
|
|
403
|
+
</SidePanelLayout>
|
|
404
|
+
</div>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** One customer company's account from the store, with the host's people and tasks panels. */
|
|
409
|
+
export function CrmCompanyAccount({
|
|
410
|
+
store,
|
|
411
|
+
text,
|
|
412
|
+
company,
|
|
413
|
+
people,
|
|
414
|
+
tasks,
|
|
415
|
+
onOpenDeal,
|
|
416
|
+
onOpenLead,
|
|
417
|
+
}: ViewProps & {
|
|
418
|
+
company: string;
|
|
419
|
+
people?: ReactNode;
|
|
420
|
+
tasks?: ReactNode;
|
|
421
|
+
onOpenDeal?: ((deal: Deal) => void) | undefined;
|
|
422
|
+
onOpenLead?: ((lead: Lead) => void) | undefined;
|
|
423
|
+
}) {
|
|
424
|
+
const state = useCrmStore(store);
|
|
425
|
+
const words = withText(text);
|
|
426
|
+
const account = useMemo(() => selectAccount(state, company), [state, company]);
|
|
427
|
+
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
428
|
+
return (
|
|
429
|
+
<div className="grid gap-6">
|
|
430
|
+
<Sample store={store} concepts={['leads', 'deals', 'invoices', 'remittances']} text={text} />
|
|
431
|
+
<Problems state={state} concepts={['leads', 'deals', 'invoices', 'remittances']} text={words} />
|
|
432
|
+
<CompanyAccountView
|
|
433
|
+
account={account}
|
|
434
|
+
pipelines={state.pipelines}
|
|
435
|
+
onOpenDeal={onOpenDeal}
|
|
436
|
+
onOpenLead={onOpenLead}
|
|
437
|
+
people={people}
|
|
438
|
+
tasks={tasks}
|
|
439
|
+
text={text}
|
|
440
|
+
/>
|
|
441
|
+
</div>
|
|
442
|
+
);
|
|
443
|
+
}
|
package/src/sales.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { addMoney, type MoneyTotals } from './money.ts';
|
|
2
|
+
import { inRange, periodChange, previousRange, type DateRange } from './period.ts';
|
|
3
|
+
import {
|
|
4
|
+
invoiceFromRemittances,
|
|
5
|
+
outstandingByAge,
|
|
6
|
+
outstandingTotals,
|
|
7
|
+
overdueTotals,
|
|
8
|
+
paymentsByDay,
|
|
9
|
+
paymentsReceived,
|
|
10
|
+
type AgeBucket,
|
|
11
|
+
} from './invoices.ts';
|
|
12
|
+
import type { Invoice, Money, Remittance, RemittanceMethod } from './types.ts';
|
|
13
|
+
|
|
14
|
+
/** Totals now and in the period before, with the change in the main currency (undefined with nothing before). */
|
|
15
|
+
export interface ComparedTotals {
|
|
16
|
+
current: MoneyTotals;
|
|
17
|
+
previous: MoneyTotals;
|
|
18
|
+
change: number | undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SalesSummary {
|
|
22
|
+
range: DateRange;
|
|
23
|
+
/** The currency with the most money in the period, which charts and changes use. */
|
|
24
|
+
currencyCode: string | undefined;
|
|
25
|
+
/** Invoices issued in the period (not drafts or void). */
|
|
26
|
+
invoiced: ComparedTotals;
|
|
27
|
+
/** Payments received less refunds. */
|
|
28
|
+
received: ComparedTotals;
|
|
29
|
+
/** Sales tax on the invoices issued in the period. */
|
|
30
|
+
tax: ComparedTotals;
|
|
31
|
+
/** Still to pay now, and how much of it is overdue. */
|
|
32
|
+
outstanding: MoneyTotals;
|
|
33
|
+
overdue: MoneyTotals;
|
|
34
|
+
byDay: { day: string; totals: MoneyTotals }[];
|
|
35
|
+
byAge: { bucket: AgeBucket; totals: MoneyTotals }[];
|
|
36
|
+
byMethod: { method: RemittanceMethod; totals: MoneyTotals }[];
|
|
37
|
+
/** Every invoice, newest first, with its payment fields worked out from its remittances. */
|
|
38
|
+
invoices: Invoice[];
|
|
39
|
+
/** Every remittance, newest first. */
|
|
40
|
+
payments: Remittance[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ISSUED = (invoice: Invoice) => invoice.status !== 'DRAFT' && invoice.status !== 'VOIDED';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Invoices with `paid`, `refunded`, `status` and `fullyPaidTimestamp` worked out from their remittances
|
|
47
|
+
* (`invoiceFromRemittances`), so recording a payment shows on its invoice at once. Invoices without remittances
|
|
48
|
+
* keep what the API gave.
|
|
49
|
+
*/
|
|
50
|
+
export function withPayments(invoices: readonly Invoice[], remittances: readonly Remittance[]): Invoice[] {
|
|
51
|
+
const paidFor = new Set(remittances.map((remittance) => remittance.paymentInvoice.urn));
|
|
52
|
+
return invoices.map((invoice) =>
|
|
53
|
+
paidFor.has(invoice.urn) ? { ...invoice, ...invoiceFromRemittances(invoice, remittances) } : invoice,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Money received per payment method, refunds taken off, in a range. */
|
|
58
|
+
export function paymentsByMethod(
|
|
59
|
+
remittances: readonly Remittance[],
|
|
60
|
+
range?: DateRange,
|
|
61
|
+
): { method: RemittanceMethod; totals: MoneyTotals }[] {
|
|
62
|
+
const byMethod = new Map<RemittanceMethod, Remittance[]>();
|
|
63
|
+
for (const remittance of remittances) {
|
|
64
|
+
const method = remittance.remittanceMethod;
|
|
65
|
+
byMethod.set(method, [...(byMethod.get(method) ?? []), remittance]);
|
|
66
|
+
}
|
|
67
|
+
return [...byMethod].map(([method, list]) => ({ method, totals: paymentsReceived(list, range) }));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The currency with the most money across some totals. */
|
|
71
|
+
const largest = (...sets: MoneyTotals[]): string | undefined => {
|
|
72
|
+
const all: Record<string, number> = {};
|
|
73
|
+
for (const set of sets)
|
|
74
|
+
for (const [currency, minor] of Object.entries(set)) all[currency] = (all[currency] ?? 0) + Math.abs(minor);
|
|
75
|
+
return Object.entries(all).sort(([, a], [, b]) => b - a)[0]?.[0];
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Sales for a period from invoices and remittances, compared with the period before. */
|
|
79
|
+
export function salesSummary(
|
|
80
|
+
data: { invoices: readonly Invoice[]; remittances: readonly Remittance[] },
|
|
81
|
+
range: DateRange,
|
|
82
|
+
now = new Date(),
|
|
83
|
+
): SalesSummary {
|
|
84
|
+
const before = previousRange(range);
|
|
85
|
+
const invoices = withPayments(data.invoices, data.remittances).toSorted((a, b) =>
|
|
86
|
+
(b.issuedTimestamp ?? '').localeCompare(a.issuedTimestamp ?? ''),
|
|
87
|
+
);
|
|
88
|
+
const issuedIn = (period: DateRange) =>
|
|
89
|
+
invoices.filter((invoice) => ISSUED(invoice) && inRange(invoice.issuedTimestamp, period));
|
|
90
|
+
const sum = (list: Invoice[], amount: (invoice: Invoice) => Money | null | undefined) =>
|
|
91
|
+
list.reduce<MoneyTotals>((totals, invoice) => addMoney(totals, amount(invoice)), {});
|
|
92
|
+
|
|
93
|
+
const invoicedNow = sum(issuedIn(range), (invoice) => invoice.total);
|
|
94
|
+
const invoicedBefore = sum(issuedIn(before), (invoice) => invoice.total);
|
|
95
|
+
const receivedNow = paymentsReceived(data.remittances, range);
|
|
96
|
+
const receivedBefore = paymentsReceived(data.remittances, before);
|
|
97
|
+
const taxNow = sum(issuedIn(range), (invoice) => invoice.tax);
|
|
98
|
+
const taxBefore = sum(issuedIn(before), (invoice) => invoice.tax);
|
|
99
|
+
const currencyCode = largest(invoicedNow, receivedNow, invoicedBefore, receivedBefore);
|
|
100
|
+
const compared = (current: MoneyTotals, previous: MoneyTotals): ComparedTotals => ({
|
|
101
|
+
current,
|
|
102
|
+
previous,
|
|
103
|
+
change: currencyCode ? periodChange(current[currencyCode] ?? 0, previous[currencyCode] ?? 0) : undefined,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
range,
|
|
108
|
+
currencyCode,
|
|
109
|
+
invoiced: compared(invoicedNow, invoicedBefore),
|
|
110
|
+
received: compared(receivedNow, receivedBefore),
|
|
111
|
+
tax: compared(taxNow, taxBefore),
|
|
112
|
+
outstanding: outstandingTotals(invoices),
|
|
113
|
+
overdue: overdueTotals(invoices, now),
|
|
114
|
+
byDay: paymentsByDay(data.remittances, range),
|
|
115
|
+
byAge: outstandingByAge(invoices, now),
|
|
116
|
+
byMethod: paymentsByMethod(data.remittances, range),
|
|
117
|
+
invoices,
|
|
118
|
+
payments: data.remittances.toSorted((a, b) => b.receivedTimestamp.localeCompare(a.receivedTimestamp)),
|
|
119
|
+
};
|
|
120
|
+
}
|