@baack-software/baack-ts-crm 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +14 -2
- 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 +19 -10
- 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/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
|
}
|
package/src/store.ts
CHANGED
|
@@ -2,23 +2,33 @@ import type { Company } from '@baack-software/baack-ts-client';
|
|
|
2
2
|
import { urnOf, type RefLike } from '@baack-software/baack-ts-tasks';
|
|
3
3
|
import { companyAccount, type CompanyAccount } from './account.ts';
|
|
4
4
|
import type { DealInput, InvoiceInput, LeadInput, RemittanceInput } from './backend.ts';
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
ConvertOptions,
|
|
7
|
+
CrmClient,
|
|
8
|
+
InvoicePartyOptions,
|
|
9
|
+
InvoiceSaved,
|
|
10
|
+
NewInvoiceOptions,
|
|
11
|
+
NewLeadOptions,
|
|
12
|
+
} from './crm-client.ts';
|
|
6
13
|
import { CrmUnavailableError } from './errors.ts';
|
|
7
14
|
import { leadsBySource, type SourceCount } from './leads.ts';
|
|
8
15
|
import { addMoney, type MoneyTotals } from './money.ts';
|
|
9
16
|
import { inRange, periodChange, previousRange, type DateRange } from './period.ts';
|
|
10
|
-
import { dealCompany, dealOutcome, pipelineByStage, type StageSummary } from './pipeline.ts';
|
|
17
|
+
import { dealCompany, dealOutcome, defaultPipeline, pipelineByStage, type StageSummary } from './pipeline.ts';
|
|
11
18
|
import { paymentsByDay, paymentsReceived } from './invoices.ts';
|
|
12
|
-
import { salesSummary, type SalesSummary } from './sales.ts';
|
|
19
|
+
import { salesRemittances, salesSummary, type SalesSummary } from './sales.ts';
|
|
13
20
|
import { salesTarget, type SalesTarget } from './target.ts';
|
|
14
21
|
import {
|
|
15
22
|
CRM_CONCEPTS,
|
|
23
|
+
type ContactDetails,
|
|
16
24
|
type CrmConcept,
|
|
25
|
+
type CrmPerson,
|
|
17
26
|
type Deal,
|
|
18
27
|
type DealOutcome,
|
|
19
28
|
type Invoice,
|
|
20
29
|
type Lead,
|
|
21
30
|
type LeadOutcome,
|
|
31
|
+
type NewContact,
|
|
22
32
|
type Pipeline,
|
|
23
33
|
type PipelineStage,
|
|
24
34
|
type Remittance,
|
|
@@ -49,13 +59,15 @@ export interface CrmStoreState {
|
|
|
49
59
|
remittances: readonly Remittance[];
|
|
50
60
|
/** Client companies made while the store was open, such as a converted lead's prospect, with their names. */
|
|
51
61
|
companies: readonly Company[];
|
|
62
|
+
/** People made while the store was open, such as a lead's new contact, with their names. */
|
|
63
|
+
people: readonly CrmPerson[];
|
|
52
64
|
/** URNs of records with a write in flight. */
|
|
53
65
|
saving: ReadonlySet<string>;
|
|
54
66
|
/** The last write the server refused, until the next write. */
|
|
55
67
|
writeError?: unknown;
|
|
56
68
|
}
|
|
57
69
|
|
|
58
|
-
type Records = Pick<CrmStoreState, CrmConcept | 'companies'>;
|
|
70
|
+
type Records = Pick<CrmStoreState, CrmConcept | 'companies' | 'people'>;
|
|
59
71
|
|
|
60
72
|
const idle = Object.fromEntries(CRM_CONCEPTS.map((concept) => [concept, { status: 'idle' }])) as Record<
|
|
61
73
|
CrmConcept,
|
|
@@ -71,11 +83,27 @@ const EMPTY: CrmStoreState = {
|
|
|
71
83
|
invoices: [],
|
|
72
84
|
remittances: [],
|
|
73
85
|
companies: [],
|
|
86
|
+
people: [],
|
|
74
87
|
saving: new Set(),
|
|
75
88
|
};
|
|
76
89
|
|
|
77
90
|
let pendingCount = 0;
|
|
78
91
|
|
|
92
|
+
/** The companies and people saving an invoice made, added to those held. */
|
|
93
|
+
const madeForInvoice = ({ companies, people }: Records, result: Omit<InvoiceSaved, 'invoice'>): Partial<Records> => ({
|
|
94
|
+
...(result.companies.length ? { companies: [...companies, ...result.companies] } : {}),
|
|
95
|
+
...(result.people.length ? { people: [...people, ...result.people] } : {}),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/** The company and person a lead's save made, added to those the store holds. */
|
|
99
|
+
const made = (
|
|
100
|
+
{ companies, people }: Records,
|
|
101
|
+
result: { company?: Company | undefined; person?: CrmPerson | undefined },
|
|
102
|
+
): Partial<Records> => ({
|
|
103
|
+
...(result.company ? { companies: [...companies, result.company] } : {}),
|
|
104
|
+
...(result.person ? { people: [...people, result.person] } : {}),
|
|
105
|
+
});
|
|
106
|
+
|
|
79
107
|
/**
|
|
80
108
|
* Holds a company's CRM records for a UI: loads every concept, applies changes straight away and rolls them back if
|
|
81
109
|
* the server refuses, and notifies subscribers. `subscribe` and `getState` fit React's `useSyncExternalStore`.
|
|
@@ -168,13 +196,13 @@ export class CrmStore {
|
|
|
168
196
|
}
|
|
169
197
|
|
|
170
198
|
#records(): Records {
|
|
171
|
-
const { leads, deals, pipelines, invoices, remittances, companies } = this.#state;
|
|
172
|
-
return { leads, deals, pipelines, invoices, remittances, companies };
|
|
199
|
+
const { leads, deals, pipelines, invoices, remittances, companies, people } = this.#state;
|
|
200
|
+
return { leads, deals, pipelines, invoices, remittances, companies, people };
|
|
173
201
|
}
|
|
174
202
|
|
|
175
203
|
/**
|
|
176
|
-
* Creates a lead, shown at the top straight away. With `companyName`, the prospect's company
|
|
177
|
-
* (see `CrmClient.createLead`).
|
|
204
|
+
* Creates a lead, shown at the top straight away. With `companyName` or `contact`, the prospect's company or contact
|
|
205
|
+
* is made first and linked (see `CrmClient.createLead`).
|
|
178
206
|
*/
|
|
179
207
|
async createLead(input: LeadInput, options: NewLeadOptions = {}): Promise<Lead> {
|
|
180
208
|
const temporary = `pending-${++pendingCount}`;
|
|
@@ -183,22 +211,48 @@ export class CrmStore {
|
|
|
183
211
|
[temporary],
|
|
184
212
|
({ leads }) => ({ leads: [placeholder, ...leads] }),
|
|
185
213
|
() => this.client.createLead(input, options),
|
|
186
|
-
(
|
|
187
|
-
leads: leads.map((candidate) => (candidate.urn === temporary ? result.lead : candidate)),
|
|
188
|
-
...(
|
|
214
|
+
(records, result) => ({
|
|
215
|
+
leads: records.leads.map((candidate) => (candidate.urn === temporary ? result.lead : candidate)),
|
|
216
|
+
...made(records, result),
|
|
189
217
|
}),
|
|
190
218
|
);
|
|
191
219
|
return lead;
|
|
192
220
|
}
|
|
193
221
|
|
|
194
|
-
/**
|
|
195
|
-
|
|
222
|
+
/**
|
|
223
|
+
* Changes a lead, shown at once. With `companyName` or `contact`, its new company or contact is made first, as for
|
|
224
|
+
* a new lead (see `CrmClient.updateLead`).
|
|
225
|
+
*/
|
|
226
|
+
async updateLead(lead: RefLike, changes: Partial<LeadInput>, options: NewLeadOptions = {}): Promise<Lead> {
|
|
196
227
|
const urn = urnOf(lead);
|
|
197
|
-
|
|
228
|
+
const current = this.#state.leads.find((candidate) => candidate.urn === urn);
|
|
229
|
+
if (!current) throw new Error(`No lead ${urn} in the store`);
|
|
230
|
+
const result = await this.#write(
|
|
198
231
|
[urn],
|
|
199
|
-
({ leads }) => ({ leads: replace(leads, urn, (
|
|
200
|
-
() => this.client.
|
|
201
|
-
(
|
|
232
|
+
({ leads }) => ({ leads: replace(leads, urn, (shown) => ({ ...shown, ...changes })) }),
|
|
233
|
+
() => this.client.updateLead(current, changes, options),
|
|
234
|
+
(records, saved) => ({ leads: replace(records.leads, urn, () => saved.lead), ...made(records, saved) }),
|
|
235
|
+
);
|
|
236
|
+
return result.lead;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** A person's email and telephone, read when asked for: they aren't held in the store's state. */
|
|
240
|
+
contactDetails(identity: RefLike): Promise<ContactDetails> {
|
|
241
|
+
return this.client.contactDetails(identity);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Sets a person's email and telephone; fields left out are kept. */
|
|
245
|
+
setContactDetails(identity: RefLike, details: Partial<ContactDetails>): Promise<ContactDetails> {
|
|
246
|
+
return this.client.setContactDetails(identity, details);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Makes a person owned by the store's company: a customer's person when `company` is given. */
|
|
250
|
+
createContact(contact: NewContact, company?: RefLike | null): Promise<CrmPerson> {
|
|
251
|
+
return this.#write(
|
|
252
|
+
[],
|
|
253
|
+
() => ({}),
|
|
254
|
+
() => this.client.createContact(this.options.owner, contact, company),
|
|
255
|
+
({ people }, person) => ({ people: [...people, person] }),
|
|
202
256
|
);
|
|
203
257
|
}
|
|
204
258
|
|
|
@@ -230,6 +284,46 @@ export class CrmStore {
|
|
|
230
284
|
);
|
|
231
285
|
}
|
|
232
286
|
|
|
287
|
+
/** Changes a deal's fields, shown at once. Moving it between stages is `moveDeal`; its outcome, `setOutcome`. */
|
|
288
|
+
updateDeal(deal: RefLike, changes: Partial<DealInput>): Promise<Deal> {
|
|
289
|
+
const urn = urnOf(deal);
|
|
290
|
+
return this.#write(
|
|
291
|
+
[urn],
|
|
292
|
+
({ deals }) => ({ deals: replace(deals, urn, (current) => ({ ...current, ...changes })) }),
|
|
293
|
+
() => this.client.deals.update(urn, changes),
|
|
294
|
+
({ deals }, stored) => ({ deals: replace(deals, urn, () => stored) }),
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Changes an invoice's own fields (status, parties, dates), shown at once, making a party's new company and person
|
|
300
|
+
* first when `options` asks (see `CrmClient.updateInvoice`). Its lines and payments aren't changed by an update, so
|
|
301
|
+
* they're kept as they were when the server's answer leaves them out or has none.
|
|
302
|
+
*/
|
|
303
|
+
async updateInvoice(
|
|
304
|
+
invoice: RefLike,
|
|
305
|
+
changes: Partial<InvoiceInput>,
|
|
306
|
+
options: InvoicePartyOptions = {},
|
|
307
|
+
): Promise<Invoice> {
|
|
308
|
+
const urn = urnOf(invoice);
|
|
309
|
+
const current = this.#state.invoices.find((candidate) => candidate.urn === urn);
|
|
310
|
+
if (!current) throw new Error(`No invoice ${urn} in the store`);
|
|
311
|
+
const result = await this.#write(
|
|
312
|
+
[urn],
|
|
313
|
+
({ invoices }) => ({ invoices: replace(invoices, urn, (shown) => ({ ...shown, ...changes })) }),
|
|
314
|
+
() => this.client.updateInvoice(current, changes, options),
|
|
315
|
+
(records, saved) => ({
|
|
316
|
+
invoices: replace(records.invoices, urn, (shown) => {
|
|
317
|
+
const lines = saved.invoice.lines?.length ? saved.invoice.lines : shown.lines;
|
|
318
|
+
const remittances = saved.invoice.remittances?.length ? saved.invoice.remittances : shown.remittances;
|
|
319
|
+
return { ...saved.invoice, ...(lines ? { lines } : {}), ...(remittances ? { remittances } : {}) };
|
|
320
|
+
}),
|
|
321
|
+
...madeForInvoice(records, saved),
|
|
322
|
+
}),
|
|
323
|
+
);
|
|
324
|
+
return result.invoice;
|
|
325
|
+
}
|
|
326
|
+
|
|
233
327
|
/** Wins, loses or reopens a deal, shown at once (see `CrmClient.setOutcome`). */
|
|
234
328
|
setOutcome(deal: RefLike, outcome: DealOutcome, options: { lostReason?: string } = {}): Promise<Deal> {
|
|
235
329
|
const urn = urnOf(deal);
|
|
@@ -275,15 +369,16 @@ export class CrmStore {
|
|
|
275
369
|
async createInvoice(
|
|
276
370
|
input: InvoiceInput,
|
|
277
371
|
lines: Parameters<CrmClient['createInvoice']>[1] = [],
|
|
278
|
-
options:
|
|
372
|
+
options: NewInvoiceOptions = {},
|
|
279
373
|
): Promise<Invoice> {
|
|
280
374
|
const { invoice } = await this.#write(
|
|
281
375
|
options.deal ? [options.deal.urn] : [],
|
|
282
376
|
() => ({}),
|
|
283
377
|
() => this.client.createInvoice(input, lines, options),
|
|
284
|
-
(
|
|
285
|
-
invoices: [result.invoice, ...invoices],
|
|
286
|
-
...(result.deal ? { deals: replace(deals, result.deal.urn, () => result.deal!) } : {}),
|
|
378
|
+
(records, result) => ({
|
|
379
|
+
invoices: [result.invoice, ...records.invoices],
|
|
380
|
+
...(result.deal ? { deals: replace(records.deals, result.deal.urn, () => result.deal!) } : {}),
|
|
381
|
+
...madeForInvoice(records, result),
|
|
287
382
|
}),
|
|
288
383
|
);
|
|
289
384
|
return invoice;
|
|
@@ -347,9 +442,18 @@ export const mainCurrency = (totals: MoneyTotals): string | undefined =>
|
|
|
347
442
|
Object.entries(totals).sort(([, a], [, b]) => b - a)[0]?.[0];
|
|
348
443
|
|
|
349
444
|
/** Everything a CRM dashboard shows for a period, compared with the period before. */
|
|
350
|
-
export function selectDashboard(
|
|
445
|
+
export function selectDashboard(
|
|
446
|
+
state: CrmStoreState,
|
|
447
|
+
range: DateRange,
|
|
448
|
+
own?: ReadonlySet<string>,
|
|
449
|
+
/** The user's chosen pipeline, for the pipeline card (see `defaultPipeline`). */
|
|
450
|
+
chosenPipeline?: string,
|
|
451
|
+
): Dashboard {
|
|
351
452
|
const before = previousRange(range);
|
|
352
|
-
const { leads, deals, pipelines
|
|
453
|
+
const { leads, deals, pipelines } = state;
|
|
454
|
+
const shownPipeline = defaultPipeline(pipelines, deals, chosenPipeline);
|
|
455
|
+
// Revenue is what customers paid: payments the business made on its costs aren't counted.
|
|
456
|
+
const remittances = salesRemittances(state, own);
|
|
353
457
|
|
|
354
458
|
const firstWins = new Map<string, string>();
|
|
355
459
|
let openValue: MoneyTotals = {};
|
|
@@ -404,7 +508,7 @@ export function selectDashboard(state: CrmStoreState, range: DateRange): Dashboa
|
|
|
404
508
|
),
|
|
405
509
|
target: salesTarget(leads, deals, remittances),
|
|
406
510
|
leadsBySource: leadsBySource(leads),
|
|
407
|
-
pipeline:
|
|
511
|
+
pipeline: shownPipeline ? pipelineByStage(deals, shownPipeline) : [],
|
|
408
512
|
};
|
|
409
513
|
}
|
|
410
514
|
|
|
@@ -413,7 +517,12 @@ export function selectAccount(state: CrmStoreState, company: RefLike, now = new
|
|
|
413
517
|
return companyAccount(urnOf(company), state, now);
|
|
414
518
|
}
|
|
415
519
|
|
|
416
|
-
/** Sales for a period from the store's invoices and payments (see `salesSummary`). */
|
|
417
|
-
export function selectSales(
|
|
418
|
-
|
|
520
|
+
/** Sales for a period from the store's invoices and payments, costs apart (see `salesSummary`). */
|
|
521
|
+
export function selectSales(
|
|
522
|
+
state: CrmStoreState,
|
|
523
|
+
range: DateRange,
|
|
524
|
+
now = new Date(),
|
|
525
|
+
own?: ReadonlySet<string>,
|
|
526
|
+
): SalesSummary {
|
|
527
|
+
return salesSummary(state, range, now, own);
|
|
419
528
|
}
|
package/src/target.ts
CHANGED
|
@@ -18,7 +18,8 @@ export interface SalesTarget {
|
|
|
18
18
|
/**
|
|
19
19
|
* The sales target and progress towards it. The target is the sum of the leads' target values, using the deal's target
|
|
20
20
|
* value instead once a lead is qualified into a deal. Deals created without a lead count too, as they're qualified
|
|
21
|
-
* already; unqualified leads don't
|
|
21
|
+
* already; unqualified leads don't, and neither do lost deals or the leads they came from (decided 27 Oct 2026, in
|
|
22
|
+
* place of keeping lost deals in): the target is what's still open or won.
|
|
22
23
|
* Progress is the payments received against the target (decided 24 Oct 2026): money in on the invoices raised for the
|
|
23
24
|
* deals (`Deal.invoices`), refunds taken off. Payments on invoices without a deal, such as repeat orders, don't count.
|
|
24
25
|
*/
|
|
@@ -31,8 +32,10 @@ export function salesTarget(
|
|
|
31
32
|
let target: MoneyTotals = {};
|
|
32
33
|
let won: MoneyTotals = {};
|
|
33
34
|
for (const deal of deals) {
|
|
35
|
+
const outcome = dealOutcome(deal);
|
|
36
|
+
if (outcome === 'LOST') continue;
|
|
34
37
|
target = addMoney(target, deal.targetValue);
|
|
35
|
-
if (
|
|
38
|
+
if (outcome === 'WON') won = addMoney(won, deal.targetValue);
|
|
36
39
|
}
|
|
37
40
|
for (const lead of leads) {
|
|
38
41
|
if (leadOutcome(lead) === 'CLOSED') continue;
|
package/src/types.ts
CHANGED
|
@@ -117,6 +117,9 @@ export type InvoiceStatus = 'DRAFT' | 'IN_REVIEW' | 'AUTHORISED' | 'PENDING' | '
|
|
|
117
117
|
/** Whether an invoice's line prices include tax. */
|
|
118
118
|
export type LineItemTax = 'EXCLUSIVE' | 'INCLUSIVE' | 'NO_TAX';
|
|
119
119
|
|
|
120
|
+
/** How an invoice line is taxed. */
|
|
121
|
+
export type Taxability = 'TAXABLE' | 'NON_TAXABLE' | 'TAX_EXEMPT' | 'PARTIALLY_TAXABLE' | 'NOT_APPLICABLE';
|
|
122
|
+
|
|
120
123
|
/** Who pays or receives an invoice: a company, an identity, or both (a person at a company). */
|
|
121
124
|
export interface InvoiceParty {
|
|
122
125
|
company?: Ref | null;
|
|
@@ -136,6 +139,7 @@ export interface InvoiceLine {
|
|
|
136
139
|
tax?: Money | null;
|
|
137
140
|
discountPrice?: Money | null;
|
|
138
141
|
discountRate?: number | null;
|
|
142
|
+
taxability?: Taxability | null;
|
|
139
143
|
taxName?: string | null;
|
|
140
144
|
}
|
|
141
145
|
|
|
@@ -200,3 +204,27 @@ export interface Remittance {
|
|
|
200
204
|
export type CrmConcept = 'leads' | 'deals' | 'pipelines' | 'invoices' | 'remittances';
|
|
201
205
|
|
|
202
206
|
export const CRM_CONCEPTS: readonly CrmConcept[] = ['leads', 'deals', 'pipelines', 'invoices', 'remittances'];
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A new person, as the lead and customer forms make them: an identity with a person, the person's name (held
|
|
210
|
+
* securely, as person names are) and an email address.
|
|
211
|
+
*/
|
|
212
|
+
export interface NewContact {
|
|
213
|
+
givenName: string;
|
|
214
|
+
familyName?: string | undefined;
|
|
215
|
+
email?: string | undefined;
|
|
216
|
+
telephone?: string | undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** How to reach a person: their email address and telephone number, each empty when they have none. */
|
|
220
|
+
export interface ContactDetails {
|
|
221
|
+
email: string;
|
|
222
|
+
telephone: string;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** A person made through the CRM, named for showing: the identity, and the company it was made for, if any. */
|
|
226
|
+
export interface CrmPerson {
|
|
227
|
+
urn: string;
|
|
228
|
+
name: string;
|
|
229
|
+
company?: Ref | null;
|
|
230
|
+
}
|