@baack-software/baack-ts-crm 0.0.0-stage → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +74 -2
- package/dist/account.d.ts +36 -0
- package/dist/account.d.ts.map +1 -0
- package/dist/account.js +48 -0
- package/dist/account.js.map +1 -0
- package/dist/backend.d.ts +104 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +328 -0
- package/dist/backend.js.map +1 -0
- package/dist/crm-client.d.ts +164 -0
- package/dist/crm-client.d.ts.map +1 -0
- package/dist/crm-client.js +252 -0
- package/dist/crm-client.js.map +1 -0
- package/dist/deal-order.d.ts +42 -0
- package/dist/deal-order.d.ts.map +1 -0
- package/dist/deal-order.js +80 -0
- package/dist/deal-order.js.map +1 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +33 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/invoices.d.ts +53 -0
- package/dist/invoices.d.ts.map +1 -0
- package/dist/invoices.js +155 -0
- package/dist/invoices.js.map +1 -0
- package/dist/leads.d.ts +29 -0
- package/dist/leads.d.ts.map +1 -0
- package/dist/leads.js +56 -0
- package/dist/leads.js.map +1 -0
- package/dist/mock/index.d.ts +43 -0
- package/dist/mock/index.d.ts.map +1 -0
- package/dist/mock/index.js +303 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/money.d.ts +29 -0
- package/dist/money.d.ts.map +1 -0
- package/dist/money.js +82 -0
- package/dist/money.js.map +1 -0
- package/dist/period.d.ts +30 -0
- package/dist/period.d.ts.map +1 -0
- package/dist/period.js +49 -0
- package/dist/period.js.map +1 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +58 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/react/Charts.d.ts +19 -0
- package/dist/react/Charts.d.ts.map +1 -0
- package/dist/react/Charts.js +38 -0
- package/dist/react/Charts.js.map +1 -0
- package/dist/react/ChoiceCombobox.d.ts +36 -0
- package/dist/react/ChoiceCombobox.d.ts.map +1 -0
- package/dist/react/ChoiceCombobox.js +105 -0
- package/dist/react/ChoiceCombobox.js.map +1 -0
- package/dist/react/CompanyAccountView.d.ts +21 -0
- package/dist/react/CompanyAccountView.d.ts.map +1 -0
- package/dist/react/CompanyAccountView.js +27 -0
- package/dist/react/CompanyAccountView.js.map +1 -0
- package/dist/react/DashboardCards.d.ts +32 -0
- package/dist/react/DashboardCards.d.ts.map +1 -0
- package/dist/react/DashboardCards.js +38 -0
- package/dist/react/DashboardCards.js.map +1 -0
- package/dist/react/DealsBoard.d.ts +49 -0
- package/dist/react/DealsBoard.d.ts.map +1 -0
- package/dist/react/DealsBoard.js +82 -0
- package/dist/react/DealsBoard.js.map +1 -0
- package/dist/react/Details.d.ts +85 -0
- package/dist/react/Details.d.ts.map +1 -0
- package/dist/react/Details.js +268 -0
- package/dist/react/Details.js.map +1 -0
- package/dist/react/Forms.d.ts +190 -0
- package/dist/react/Forms.d.ts.map +1 -0
- package/dist/react/Forms.js +494 -0
- package/dist/react/Forms.js.map +1 -0
- package/dist/react/LeadsTable.d.ts +22 -0
- package/dist/react/LeadsTable.d.ts.map +1 -0
- package/dist/react/LeadsTable.js +79 -0
- package/dist/react/LeadsTable.js.map +1 -0
- package/dist/react/Sales.d.ts +68 -0
- package/dist/react/Sales.d.ts.map +1 -0
- package/dist/react/Sales.js +270 -0
- package/dist/react/Sales.js.map +1 -0
- package/dist/react/SidePanel.d.ts +31 -0
- package/dist/react/SidePanel.d.ts.map +1 -0
- package/dist/react/SidePanel.js +49 -0
- package/dist/react/SidePanel.js.map +1 -0
- package/dist/react/format.d.ts +28 -0
- package/dist/react/format.d.ts.map +1 -0
- package/dist/react/format.js +30 -0
- package/dist/react/format.js.map +1 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +15 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/parts.d.ts +55 -0
- package/dist/react/parts.d.ts.map +1 -0
- package/dist/react/parts.js +82 -0
- package/dist/react/parts.js.map +1 -0
- package/dist/react/pipeline-choice.d.ts +19 -0
- package/dist/react/pipeline-choice.d.ts.map +1 -0
- package/dist/react/pipeline-choice.js +39 -0
- package/dist/react/pipeline-choice.js.map +1 -0
- package/dist/react/text.d.ts +232 -0
- package/dist/react/text.d.ts.map +1 -0
- package/dist/react/text.js +229 -0
- package/dist/react/text.js.map +1 -0
- package/dist/react/ui/alert.d.ts +11 -0
- package/dist/react/ui/alert.d.ts.map +1 -0
- package/dist/react/ui/alert.js +29 -0
- package/dist/react/ui/alert.js.map +1 -0
- package/dist/react/ui/badge.d.ts +10 -0
- package/dist/react/ui/badge.d.ts.map +1 -0
- package/dist/react/ui/badge.js +26 -0
- package/dist/react/ui/badge.js.map +1 -0
- package/dist/react/ui/button.d.ts +11 -0
- package/dist/react/ui/button.d.ts.map +1 -0
- package/dist/react/ui/button.js +37 -0
- package/dist/react/ui/button.js.map +1 -0
- package/dist/react/ui/card.d.ts +12 -0
- package/dist/react/ui/card.d.ts.map +1 -0
- package/dist/react/ui/card.js +26 -0
- package/dist/react/ui/card.js.map +1 -0
- package/dist/react/ui/chart.d.ts +45 -0
- package/dist/react/ui/chart.d.ts.map +1 -0
- package/dist/react/ui/chart.js +116 -0
- package/dist/react/ui/chart.js.map +1 -0
- package/dist/react/ui/input.d.ts +4 -0
- package/dist/react/ui/input.d.ts.map +1 -0
- package/dist/react/ui/input.js +8 -0
- package/dist/react/ui/input.js.map +1 -0
- package/dist/react/ui/label.d.ts +5 -0
- package/dist/react/ui/label.d.ts.map +1 -0
- package/dist/react/ui/label.js +10 -0
- package/dist/react/ui/label.js.map +1 -0
- package/dist/react/ui/progress.d.ts +5 -0
- package/dist/react/ui/progress.d.ts.map +1 -0
- package/dist/react/ui/progress.js +10 -0
- package/dist/react/ui/progress.js.map +1 -0
- package/dist/react/ui/select.d.ts +16 -0
- package/dist/react/ui/select.d.ts.map +1 -0
- package/dist/react/ui/select.js +37 -0
- package/dist/react/ui/select.js.map +1 -0
- package/dist/react/ui/separator.d.ts +5 -0
- package/dist/react/ui/separator.d.ts.map +1 -0
- package/dist/react/ui/separator.js +10 -0
- package/dist/react/ui/separator.js.map +1 -0
- package/dist/react/ui/sheet.d.ts +15 -0
- package/dist/react/ui/sheet.d.ts.map +1 -0
- package/dist/react/ui/sheet.js +38 -0
- package/dist/react/ui/sheet.js.map +1 -0
- package/dist/react/ui/skeleton.d.ts +3 -0
- package/dist/react/ui/skeleton.d.ts.map +1 -0
- package/dist/react/ui/skeleton.js +7 -0
- package/dist/react/ui/skeleton.js.map +1 -0
- package/dist/react/ui/table.d.ts +11 -0
- package/dist/react/ui/table.d.ts.map +1 -0
- package/dist/react/ui/table.js +29 -0
- package/dist/react/ui/table.js.map +1 -0
- package/dist/react/ui/tabs.d.ts +12 -0
- package/dist/react/ui/tabs.d.ts.map +1 -0
- package/dist/react/ui/tabs.js +31 -0
- package/dist/react/ui/tabs.js.map +1 -0
- package/dist/react/views.d.ts +82 -0
- package/dist/react/views.d.ts.map +1 -0
- package/dist/react/views.js +241 -0
- package/dist/react/views.js.map +1 -0
- package/dist/sales.d.ts +82 -0
- package/dist/sales.d.ts.map +1 -0
- package/dist/sales.js +87 -0
- package/dist/sales.js.map +1 -0
- package/dist/store.d.ts +159 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +305 -0
- package/dist/store.js.map +1 -0
- package/dist/target.d.ts +22 -0
- package/dist/target.d.ts.map +1 -0
- package/dist/target.js +40 -0
- package/dist/target.js.map +1 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +100 -4
- package/src/account.ts +79 -0
- package/src/backend.ts +501 -0
- package/src/crm-client.ts +393 -0
- package/src/deal-order.ts +116 -0
- package/src/errors.ts +34 -0
- package/src/index.ts +13 -0
- package/src/invoices.ts +177 -0
- package/src/leads.ts +78 -0
- package/src/mock/index.ts +398 -0
- package/src/money.ts +104 -0
- package/src/period.ts +71 -0
- package/src/pipeline.ts +82 -0
- package/src/react/Charts.tsx +143 -0
- package/src/react/ChoiceCombobox.tsx +227 -0
- package/src/react/CompanyAccountView.tsx +198 -0
- package/src/react/DashboardCards.tsx +171 -0
- package/src/react/DealsBoard.tsx +344 -0
- package/src/react/Details.tsx +870 -0
- package/src/react/Forms.tsx +1486 -0
- package/src/react/LeadsTable.tsx +234 -0
- package/src/react/Sales.tsx +827 -0
- package/src/react/SidePanel.tsx +104 -0
- package/src/react/format.ts +53 -0
- package/src/react/index.ts +14 -0
- package/src/react/parts.tsx +183 -0
- package/src/react/pipeline-choice.ts +54 -0
- package/src/react/text.ts +232 -0
- package/src/react/ui/README.md +6 -0
- package/src/react/ui/alert.tsx +75 -0
- package/src/react/ui/badge.tsx +48 -0
- package/src/react/ui/button.tsx +66 -0
- package/src/react/ui/card.tsx +102 -0
- package/src/react/ui/chart.tsx +304 -0
- package/src/react/ui/input.tsx +18 -0
- package/src/react/ui/label.tsx +23 -0
- package/src/react/ui/progress.tsx +30 -0
- package/src/react/ui/select.tsx +189 -0
- package/src/react/ui/separator.tsx +27 -0
- package/src/react/ui/sheet.tsx +145 -0
- package/src/react/ui/skeleton.tsx +13 -0
- package/src/react/ui/table.tsx +113 -0
- package/src/react/ui/tabs.tsx +89 -0
- package/src/react/views.tsx +633 -0
- package/src/sales.ts +167 -0
- package/src/store.ts +528 -0
- package/src/target.ts +54 -0
- package/src/types.ts +230 -0
package/src/invoices.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { parseTimestamp, toTimestamp } from '@baack-software/baack-ts-tasks';
|
|
2
|
+
import { addMoney, fromMinor, minorOf, type MoneyTotals } from './money.ts';
|
|
3
|
+
import { inRange, type DateRange } from './period.ts';
|
|
4
|
+
import type { Invoice, InvoiceStatus, Money, Remittance } from './types.ts';
|
|
5
|
+
|
|
6
|
+
/** Statuses of an invoice that has been sent and is waiting to be paid. */
|
|
7
|
+
export const AWAITING_PAYMENT: readonly InvoiceStatus[] = ['AUTHORISED', 'PENDING'];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Whether an invoice is a cost: paid by the business (one of `own`, the companies it counts as itself, such as the
|
|
11
|
+
* owner and its child companies) rather than paid to it. Without `own`, only the invoice's owner counts.
|
|
12
|
+
*/
|
|
13
|
+
export function isCostInvoice(invoice: Invoice, own?: ReadonlySet<string>): boolean {
|
|
14
|
+
const payer = invoice.remitter?.company?.urn;
|
|
15
|
+
return payer !== undefined && (own ? own.has(payer) : payer === invoice.owner.urn);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Every invoice status the API has, in the order an invoice usually moves through them. */
|
|
19
|
+
export const INVOICE_STATUSES: readonly InvoiceStatus[] = [
|
|
20
|
+
'DRAFT',
|
|
21
|
+
'IN_REVIEW',
|
|
22
|
+
'AUTHORISED',
|
|
23
|
+
'PENDING',
|
|
24
|
+
'PAID',
|
|
25
|
+
'REFUNDED',
|
|
26
|
+
'VOIDED',
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
export const INVOICE_STATUS_LABELS: Readonly<Record<InvoiceStatus, string>> = {
|
|
30
|
+
DRAFT: 'Draft',
|
|
31
|
+
IN_REVIEW: 'In review',
|
|
32
|
+
AUTHORISED: 'Authorised',
|
|
33
|
+
PENDING: 'Pending payment',
|
|
34
|
+
VOIDED: 'Void',
|
|
35
|
+
PAID: 'Paid',
|
|
36
|
+
REFUNDED: 'Refunded',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** What's still to pay on an invoice awaiting payment: its total less what's been paid. Undefined otherwise. */
|
|
40
|
+
export function outstanding(invoice: Invoice): Money | undefined {
|
|
41
|
+
if (!AWAITING_PAYMENT.includes(invoice.status)) return undefined;
|
|
42
|
+
const { currencyCode } = invoice;
|
|
43
|
+
const due = minorOf(invoice.total) - minorOf(invoice.paid);
|
|
44
|
+
return due > 0 ? fromMinor(due, currencyCode) : undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Whether an invoice is past its due date with something still to pay. */
|
|
48
|
+
export function isOverdueInvoice(invoice: Invoice, now = new Date()): boolean {
|
|
49
|
+
const due = parseTimestamp(invoice.dueTimestamp);
|
|
50
|
+
return due !== undefined && due < now && outstanding(invoice) !== undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What's still to pay across invoices, per currency. */
|
|
54
|
+
export function outstandingTotals(invoices: readonly Invoice[]): MoneyTotals {
|
|
55
|
+
return invoices.reduce<MoneyTotals>((totals, invoice) => addMoney(totals, outstanding(invoice)), {});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What's overdue across invoices, per currency. */
|
|
59
|
+
export function overdueTotals(invoices: readonly Invoice[], now = new Date()): MoneyTotals {
|
|
60
|
+
return invoices
|
|
61
|
+
.filter((invoice) => isOverdueInvoice(invoice, now))
|
|
62
|
+
.reduce<MoneyTotals>((totals, invoice) => addMoney(totals, outstanding(invoice)), {});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type AgeBucket = 'CURRENT' | 'DAYS_1_30' | 'DAYS_31_60' | 'DAYS_61_90' | 'DAYS_OVER_90';
|
|
66
|
+
|
|
67
|
+
export const AGE_BUCKETS: readonly AgeBucket[] = ['CURRENT', 'DAYS_1_30', 'DAYS_31_60', 'DAYS_61_90', 'DAYS_OVER_90'];
|
|
68
|
+
|
|
69
|
+
export const AGE_BUCKET_LABELS: Readonly<Record<AgeBucket, string>> = {
|
|
70
|
+
CURRENT: 'Not yet due',
|
|
71
|
+
DAYS_1_30: '1 to 30 days overdue',
|
|
72
|
+
DAYS_31_60: '31 to 60 days overdue',
|
|
73
|
+
DAYS_61_90: '61 to 90 days overdue',
|
|
74
|
+
DAYS_OVER_90: 'Over 90 days overdue',
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** How long an invoice has been overdue, as a bucket for an aged debt view. */
|
|
78
|
+
export function ageBucket(invoice: Invoice, now = new Date()): AgeBucket {
|
|
79
|
+
const due = parseTimestamp(invoice.dueTimestamp);
|
|
80
|
+
const days = due ? Math.floor((now.getTime() - due.getTime()) / 86_400_000) : 0;
|
|
81
|
+
if (days <= 0) return 'CURRENT';
|
|
82
|
+
if (days <= 30) return 'DAYS_1_30';
|
|
83
|
+
if (days <= 60) return 'DAYS_31_60';
|
|
84
|
+
if (days <= 90) return 'DAYS_61_90';
|
|
85
|
+
return 'DAYS_OVER_90';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** What's still to pay, per age bucket and currency, every bucket in order. */
|
|
89
|
+
export function outstandingByAge(
|
|
90
|
+
invoices: readonly Invoice[],
|
|
91
|
+
now = new Date(),
|
|
92
|
+
): { bucket: AgeBucket; totals: MoneyTotals }[] {
|
|
93
|
+
const byBucket = new Map<AgeBucket, MoneyTotals>();
|
|
94
|
+
for (const invoice of invoices) {
|
|
95
|
+
const due = outstanding(invoice);
|
|
96
|
+
if (!due) continue;
|
|
97
|
+
const bucket = ageBucket(invoice, now);
|
|
98
|
+
byBucket.set(bucket, addMoney(byBucket.get(bucket) ?? {}, due));
|
|
99
|
+
}
|
|
100
|
+
return AGE_BUCKETS.map((bucket) => ({ bucket, totals: byBucket.get(bucket) ?? {} }));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const counts = (remittance: Remittance) => remittance.remittanceStatus === 'RECEIVED';
|
|
104
|
+
|
|
105
|
+
/** Money received less refunds paid, per currency, optionally only within a date range. */
|
|
106
|
+
export function paymentsReceived(remittances: readonly Remittance[], range?: DateRange): MoneyTotals {
|
|
107
|
+
let totals: MoneyTotals = {};
|
|
108
|
+
for (const remittance of remittances) {
|
|
109
|
+
if (!counts(remittance) || (range && !inRange(remittance.receivedTimestamp, range))) continue;
|
|
110
|
+
totals = addMoney(totals, remittance.value, remittance.remittanceDirection === 'REFUND' ? -1 : 1);
|
|
111
|
+
}
|
|
112
|
+
return totals;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Payments received per UTC day in a range, every day included, for a chart. */
|
|
116
|
+
export function paymentsByDay(
|
|
117
|
+
remittances: readonly Remittance[],
|
|
118
|
+
range: DateRange,
|
|
119
|
+
): { day: string; totals: MoneyTotals }[] {
|
|
120
|
+
const days: { day: string; totals: MoneyTotals }[] = [];
|
|
121
|
+
const start = new Date(Date.UTC(range.start.getUTCFullYear(), range.start.getUTCMonth(), range.start.getUTCDate()));
|
|
122
|
+
for (let day = start; day < range.end; day = new Date(day.getTime() + 86_400_000)) {
|
|
123
|
+
const next = new Date(day.getTime() + 86_400_000);
|
|
124
|
+
days.push({
|
|
125
|
+
day: day.toISOString().slice(0, 10),
|
|
126
|
+
totals: paymentsReceived(remittances, { start: day, end: next }),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return days;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** An invoice's payment fields as they follow from its remittances. */
|
|
133
|
+
export interface InvoicePayments {
|
|
134
|
+
paid: Money;
|
|
135
|
+
refunded: Money;
|
|
136
|
+
status: InvoiceStatus;
|
|
137
|
+
fullyPaidTimestamp: string | null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* An invoice's `paid`, `refunded`, `status` and `fullyPaidTimestamp` worked out from its remittances, which the API
|
|
142
|
+
* lists on the invoice but doesn't total. An invoice's own amounts (`total`, `tax`) are as it was given: the API
|
|
143
|
+
* doesn't add them up from its lines, so an invoice can total something other than its lines' sum. Only received remittances count. An invoice is paid once payments reach its total (fully paid when the
|
|
144
|
+
* payment that got there arrived), and refunded once refunds match the payments; otherwise its status is kept.
|
|
145
|
+
*/
|
|
146
|
+
export function invoiceFromRemittances(invoice: Invoice, remittances: readonly Remittance[]): InvoicePayments {
|
|
147
|
+
const total = minorOf(invoice.total);
|
|
148
|
+
const mine = remittances
|
|
149
|
+
.filter((remittance) => remittance.paymentInvoice.urn === invoice.urn && counts(remittance))
|
|
150
|
+
.toSorted((a, b) => a.receivedTimestamp.localeCompare(b.receivedTimestamp));
|
|
151
|
+
let paid = 0;
|
|
152
|
+
let refunded = 0;
|
|
153
|
+
let fullyPaid: string | null = null;
|
|
154
|
+
for (const remittance of mine) {
|
|
155
|
+
const minor = remittance.value.unitValue;
|
|
156
|
+
if (remittance.remittanceDirection === 'REFUND') refunded += minor;
|
|
157
|
+
else {
|
|
158
|
+
paid += minor;
|
|
159
|
+
if (fullyPaid === null && paid >= total) fullyPaid = normalise(remittance.receivedTimestamp);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
let status = invoice.status;
|
|
163
|
+
if (paid > 0 && refunded >= paid) status = 'REFUNDED';
|
|
164
|
+
else if (fullyPaid) status = 'PAID';
|
|
165
|
+
return {
|
|
166
|
+
paid: fromMinor(paid, invoice.currencyCode),
|
|
167
|
+
refunded: fromMinor(refunded, invoice.currencyCode),
|
|
168
|
+
status,
|
|
169
|
+
fullyPaidTimestamp: fullyPaid,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A Baack timestamp written the one way, with milliseconds. */
|
|
174
|
+
function normalise(timestamp: string): string {
|
|
175
|
+
const date = parseTimestamp(timestamp);
|
|
176
|
+
return date ? toTimestamp(date) : timestamp;
|
|
177
|
+
}
|
package/src/leads.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Lead, LeadOutcome, LeadSource, LeadStatus } from './types.ts';
|
|
2
|
+
|
|
3
|
+
export const LEAD_SOURCES: readonly LeadSource[] = [
|
|
4
|
+
'WEBSITE',
|
|
5
|
+
'REFERRAL',
|
|
6
|
+
'SOCIAL',
|
|
7
|
+
'EMAIL',
|
|
8
|
+
'CALL',
|
|
9
|
+
'ADS',
|
|
10
|
+
'EVENT',
|
|
11
|
+
'UNKNOWN',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
export const SOURCE_LABELS: Readonly<Record<LeadSource, string>> = {
|
|
15
|
+
WEBSITE: 'Website',
|
|
16
|
+
REFERRAL: 'Referral',
|
|
17
|
+
SOCIAL: 'Social',
|
|
18
|
+
EMAIL: 'Email',
|
|
19
|
+
CALL: 'Call',
|
|
20
|
+
ADS: 'Ads',
|
|
21
|
+
EVENT: 'Event',
|
|
22
|
+
UNKNOWN: 'Other',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Lead statuses in the order a lead moves through them. */
|
|
26
|
+
export const LEAD_STATUSES: readonly LeadStatus[] = ['NEW', 'CONTACTED', 'QUALIFIED'];
|
|
27
|
+
|
|
28
|
+
export const LEAD_STATUS_LABELS: Readonly<Record<LeadStatus, string>> = {
|
|
29
|
+
NEW: 'New',
|
|
30
|
+
CONTACTED: 'Contacted',
|
|
31
|
+
QUALIFIED: 'Qualified',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const LEAD_OUTCOME_LABELS: Readonly<Record<LeadOutcome, string>> = {
|
|
35
|
+
IN_PROGRESS: 'In progress',
|
|
36
|
+
CONVERTED: 'Converted',
|
|
37
|
+
CLOSED: 'Closed',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** How a lead ended: its own outcome, in progress when it has none. */
|
|
41
|
+
export const leadOutcome = (lead: Lead): LeadOutcome => lead.outcome ?? 'IN_PROGRESS';
|
|
42
|
+
|
|
43
|
+
/** Whether a lead still needs work: not yet converted into a deal or closed as unqualified. */
|
|
44
|
+
export const isOpenLead = (lead: Lead): boolean => leadOutcome(lead) === 'IN_PROGRESS';
|
|
45
|
+
|
|
46
|
+
/** Where a lead stands, in one word for tables and filters: its outcome once closed, otherwise its status. */
|
|
47
|
+
export type LeadState = LeadStatus | Exclude<LeadOutcome, 'IN_PROGRESS'>;
|
|
48
|
+
|
|
49
|
+
export const LEAD_STATES: readonly LeadState[] = [...LEAD_STATUSES, 'CONVERTED', 'CLOSED'];
|
|
50
|
+
|
|
51
|
+
export const LEAD_STATE_LABELS: Readonly<Record<LeadState, string>> = {
|
|
52
|
+
...LEAD_STATUS_LABELS,
|
|
53
|
+
CONVERTED: LEAD_OUTCOME_LABELS.CONVERTED,
|
|
54
|
+
CLOSED: LEAD_OUTCOME_LABELS.CLOSED,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export function leadState(lead: Lead): LeadState {
|
|
58
|
+
const outcome = leadOutcome(lead);
|
|
59
|
+
return outcome === 'IN_PROGRESS' ? lead.status : outcome;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The deal a lead was converted into: the deal whose `leadSource` is the lead. */
|
|
63
|
+
export const dealFromLead = <D extends { leadSource?: { urn: string } | null }>(
|
|
64
|
+
lead: Pick<Lead, 'urn'>,
|
|
65
|
+
deals: readonly D[],
|
|
66
|
+
): D | undefined => deals.find((deal) => deal.leadSource?.urn === lead.urn);
|
|
67
|
+
|
|
68
|
+
export interface SourceCount {
|
|
69
|
+
source: LeadSource;
|
|
70
|
+
count: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** How many leads came from each source, every source in order, including those with none. */
|
|
74
|
+
export function leadsBySource(leads: readonly Lead[]): SourceCount[] {
|
|
75
|
+
const counts = new Map<LeadSource, number>();
|
|
76
|
+
for (const lead of leads) counts.set(lead.source, (counts.get(lead.source) ?? 0) + 1);
|
|
77
|
+
return LEAD_SOURCES.map((source) => ({ source, count: counts.get(source) ?? 0 }));
|
|
78
|
+
}
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { ref, toTimestamp, urnOf, type Ref, type RefLike } from '@baack-software/baack-ts-tasks';
|
|
2
|
+
import type { LeadState } from '../leads.ts';
|
|
3
|
+
import { fractionDigits } from '../money.ts';
|
|
4
|
+
import type { CrmData } from '../backend.ts';
|
|
5
|
+
import type {
|
|
6
|
+
Deal,
|
|
7
|
+
DealOutcome,
|
|
8
|
+
Invoice,
|
|
9
|
+
InvoiceStatus,
|
|
10
|
+
Lead,
|
|
11
|
+
LeadSource,
|
|
12
|
+
RemittanceMethod,
|
|
13
|
+
Pipeline,
|
|
14
|
+
PipelineStage,
|
|
15
|
+
Remittance,
|
|
16
|
+
} from '../types.ts';
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* Sample CRM records for demos and for reviewing views before the API has leads, deals and payments. The business is a
|
|
20
|
+
* coastal clothing and sea swimming brand selling wholesale to shops, clubs and hotels. Everything is dated relative
|
|
21
|
+
* to `now`, so the sample always looks current, and URNs are fixed, so the same options give the same records.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** A company the sample can attach records to, with its name for display. */
|
|
25
|
+
export interface SampleCompany {
|
|
26
|
+
urn: string;
|
|
27
|
+
name: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A person the sample can use as a contact, with the company they belong to. */
|
|
31
|
+
export interface SamplePerson {
|
|
32
|
+
urn: string;
|
|
33
|
+
company?: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface SampleOptions {
|
|
37
|
+
/** The company whose CRM this is. */
|
|
38
|
+
owner: RefLike;
|
|
39
|
+
/**
|
|
40
|
+
* Real customer companies to attach the sample's deals, leads and invoices to, in turn. Without them the sample uses
|
|
41
|
+
* its own companies (`SAMPLE_COMPANIES`), which aren't in Baack.
|
|
42
|
+
*/
|
|
43
|
+
companies?: readonly SampleCompany[];
|
|
44
|
+
/** Real people to use as contacts: each company's first person. */
|
|
45
|
+
people?: readonly SamplePerson[];
|
|
46
|
+
/** Team members to assign leads and deals to, in turn. */
|
|
47
|
+
team?: readonly RefLike[];
|
|
48
|
+
now?: Date;
|
|
49
|
+
currencyCode?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface SampleCrm extends CrmData {
|
|
53
|
+
/** The companies the records point at, with names. */
|
|
54
|
+
companies: SampleCompany[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A fixed URN for sample record `n` of a kind, recognisable by its `5a3e` start. */
|
|
58
|
+
export function sampleUrn(kind: number, n: number): string {
|
|
59
|
+
return `5a3e0000-0000-4000-8000-${kind}${String(n).padStart(11, '0')}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Whether a URN belongs to sample data. */
|
|
63
|
+
export const isSampleUrn = (urn: string): boolean => urn.startsWith('5a3e0000-');
|
|
64
|
+
|
|
65
|
+
/** The sample's own customer companies, used when no real ones are given. */
|
|
66
|
+
export const SAMPLE_COMPANIES: readonly SampleCompany[] = [
|
|
67
|
+
'Porthmeor Surf Co',
|
|
68
|
+
'Gyllyngvase Swim Club',
|
|
69
|
+
'Harbour View Hotel',
|
|
70
|
+
'Atlantic Outfitters',
|
|
71
|
+
'Penwith Wild Swimmers',
|
|
72
|
+
'Bretagne Nautique',
|
|
73
|
+
].map((name, n) => ({ urn: sampleUrn(6, n + 1), name }));
|
|
74
|
+
|
|
75
|
+
const PIPELINE_URN = sampleUrn(3, 0);
|
|
76
|
+
|
|
77
|
+
// A plain workflow: its stages are only progress. Whether a deal is won or lost, and how likely, is on the deal.
|
|
78
|
+
const STAGES: PipelineStage[] = ['Lead', 'Qualified', 'Proposal', 'Negotiation', 'Closed'].map((name, order) => ({
|
|
79
|
+
urn: sampleUrn(3, order + 1),
|
|
80
|
+
name,
|
|
81
|
+
order,
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
/** The sample pipeline's stage by name. */
|
|
85
|
+
const stage = (name: string): PipelineStage => STAGES.find((candidate) => candidate.name === name)!;
|
|
86
|
+
|
|
87
|
+
/** Where the sample's deals sit: the stage, how each stands, and the chance it's won while open. */
|
|
88
|
+
const PLACES: Record<string, { stage: string; outcome: DealOutcome; probability: number | null }> = {
|
|
89
|
+
Lead: { stage: 'Lead', outcome: 'OPEN', probability: 10 },
|
|
90
|
+
Qualified: { stage: 'Qualified', outcome: 'OPEN', probability: 25 },
|
|
91
|
+
Proposal: { stage: 'Proposal', outcome: 'OPEN', probability: 50 },
|
|
92
|
+
Negotiation: { stage: 'Negotiation', outcome: 'OPEN', probability: 75 },
|
|
93
|
+
'Closed won': { stage: 'Closed', outcome: 'WON', probability: null },
|
|
94
|
+
'Closed lost': { stage: 'Closed', outcome: 'LOST', probability: null },
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
type LeadRow = [
|
|
98
|
+
title: string,
|
|
99
|
+
company: number | string,
|
|
100
|
+
source: LeadSource,
|
|
101
|
+
// Its status, or how it ended (converted, or closed as unqualified).
|
|
102
|
+
state: LeadState,
|
|
103
|
+
value: number,
|
|
104
|
+
age: number,
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// Company is an index into the companies, or the name of a prospect's company: a sample company, made for the lead.
|
|
108
|
+
const LEADS: LeadRow[] = [
|
|
109
|
+
['Robes for the spring club season', 1, 'REFERRAL', 'CONVERTED', 2400, 40],
|
|
110
|
+
['Hotel spa robes and towels', 2, 'WEBSITE', 'CONVERTED', 5200, 35],
|
|
111
|
+
['Changing ponchos for summer stock', 0, 'EVENT', 'CONVERTED', 3800, 60],
|
|
112
|
+
['Club swim caps with logo', 4, 'SOCIAL', 'QUALIFIED', 900, 9],
|
|
113
|
+
['Wholesale account for a new shop', 'Sennen Cove Supplies', 'EMAIL', 'NEW', 1500, 2],
|
|
114
|
+
['Event kit for a charity swim', 'Swim the Fal', 'SOCIAL', 'CONTACTED', 1200, 6],
|
|
115
|
+
['Rental fleet of changing robes', 'Kynance Kayaks', 'CALL', 'CONTACTED', 2700, 12],
|
|
116
|
+
['Staff uniforms for a beach cafe', 'Little Shore Cafe', 'WEBSITE', 'NEW', 650, 1],
|
|
117
|
+
['French stockist for the winter range', 5, 'ADS', 'CONVERTED', 6400, 50],
|
|
118
|
+
['Gift shop range for a museum', 'Maritime Museum Shop', 'REFERRAL', 'CLOSED', 800, 25],
|
|
119
|
+
['Swim school starter packs', 'Tidal Pool Swim School', 'ADS', 'NEW', 1100, 3],
|
|
120
|
+
['Corporate gifts for a retreat', 'Saltings Retreats', 'EMAIL', 'CONTACTED', 1900, 15],
|
|
121
|
+
['Repeat order of winter beanies', 3, 'UNKNOWN', 'CONVERTED', 1300, 70],
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
type DealRow = [
|
|
125
|
+
title: string,
|
|
126
|
+
company: number,
|
|
127
|
+
stage: string,
|
|
128
|
+
value: number,
|
|
129
|
+
age: number,
|
|
130
|
+
closeIn: number,
|
|
131
|
+
lead?: number | undefined,
|
|
132
|
+
lostReason?: string,
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
const DEALS: DealRow[] = [
|
|
136
|
+
['Spring club robes', 1, 'Proposal', 2600, 30, 14, 0],
|
|
137
|
+
['Spa robes and towels', 2, 'Negotiation', 5200, 28, 7, 1],
|
|
138
|
+
['Summer ponchos', 0, 'Closed won', 3650, 55, -20, 2],
|
|
139
|
+
['Winter range stockist', 5, 'Qualified', 6400, 45, 30, 8],
|
|
140
|
+
['Winter beanies repeat', 3, 'Closed won', 1300, 65, -40, 12],
|
|
141
|
+
['Autumn hoodies', 0, 'Closed lost', 2100, 80, -45, undefined, 'Went with a cheaper supplier'],
|
|
142
|
+
['Club towels', 4, 'Lead', 750, 4, 45],
|
|
143
|
+
['Branded tote bags', 3, 'Proposal', 980, 18, 21],
|
|
144
|
+
['Shop fit-out display stock', 0, 'Closed won', 4200, 90, -60],
|
|
145
|
+
['Lifeguard jackets', 2, 'Negotiation', 3100, 20, 10],
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
type InvoiceRow = [deal: number, status: InvoiceStatus, issuedAgo: number, dueIn: number, paidShare: number];
|
|
149
|
+
|
|
150
|
+
const INVOICES: InvoiceRow[] = [
|
|
151
|
+
[2, 'PAID', 18, 12, 1],
|
|
152
|
+
[4, 'PAID', 38, -8, 1],
|
|
153
|
+
[8, 'AUTHORISED', 50, -20, 0.5],
|
|
154
|
+
[8, 'AUTHORISED', 20, 10, 0],
|
|
155
|
+
[2, 'AUTHORISED', 45, -15, 0],
|
|
156
|
+
[1, 'DRAFT', 0, 30, 0],
|
|
157
|
+
[4, 'REFUNDED', 60, -30, 1],
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
type RepeatRow = [description: string, company: number, amount: number, issuedAgo: number, paidAfter?: number];
|
|
161
|
+
|
|
162
|
+
// Smaller orders between deals: what each customer buys again, when it was issued, and how many days later it was paid.
|
|
163
|
+
const REPEAT_ORDERS: RepeatRow[] = [
|
|
164
|
+
['Restock: changing robes', 0, 640, 54, 6],
|
|
165
|
+
['Restock: swim caps', 1, 180, 49, 3],
|
|
166
|
+
['Restock: beanies', 3, 420, 45, 9],
|
|
167
|
+
['Spa towels top-up', 2, 760, 41, 12],
|
|
168
|
+
['Restock: changing robes', 0, 880, 36, 5],
|
|
169
|
+
['Club hoodies', 4, 540, 33, 4],
|
|
170
|
+
['Restock: dry bags', 3, 310, 28, 7],
|
|
171
|
+
['Restock: swim caps', 1, 220, 24, 2],
|
|
172
|
+
['Winter range samples', 5, 950, 20, 10],
|
|
173
|
+
['Restock: changing robes', 0, 720, 16, 4],
|
|
174
|
+
['Spa towels top-up', 2, 690, 12, 6],
|
|
175
|
+
['Restock: beanies', 3, 460, 9, 3],
|
|
176
|
+
['Club hoodies', 4, 610, 6, 2],
|
|
177
|
+
['Restock: dry bags', 3, 290, 4],
|
|
178
|
+
['Restock: swim caps', 1, 240, 2],
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
const METHODS: RemittanceMethod[] = ['BANK_TRANSFER', 'CARD_PAYMENT', 'DIRECT_DEBIT', 'BANK_TRANSFER', 'E_WALLET'];
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* A sample CRM for `owner`: a pipeline, leads, deals, invoices and payments. Pass real `companies`, `people` and
|
|
185
|
+
* `team` to attach the records to them, so a console page can mix real people and tasks with sample deals.
|
|
186
|
+
*/
|
|
187
|
+
export function sampleCrm(options: SampleOptions): SampleCrm {
|
|
188
|
+
const { now = new Date(), currencyCode = 'GBP' } = options;
|
|
189
|
+
const owner: Ref = ref(options.owner);
|
|
190
|
+
const companies = options.companies?.length ? [...options.companies] : [...SAMPLE_COMPANIES];
|
|
191
|
+
const team = (options.team ?? []).map(urnOf);
|
|
192
|
+
const ago = (days: number) => toTimestamp(new Date(now.getTime() - days * 86_400_000));
|
|
193
|
+
// The tables below are in major units (pounds); records hold minor units, as the API does.
|
|
194
|
+
const scale = 10 ** fractionDigits(currencyCode);
|
|
195
|
+
const u = (amount: number) => Math.round(amount * scale);
|
|
196
|
+
const money = (amount: number) => ({ unitValue: u(amount), currencyCode });
|
|
197
|
+
const companyAt = (index: number) => companies[index % companies.length]!;
|
|
198
|
+
const contactAt = (company: string) => options.people?.find((person) => person.company === company);
|
|
199
|
+
const assignee = (n: number) => (team.length ? { urn: team[n % team.length]! } : null);
|
|
200
|
+
|
|
201
|
+
const pipeline: Pipeline = {
|
|
202
|
+
urn: PIPELINE_URN,
|
|
203
|
+
url: `/n/v1/workflow/${PIPELINE_URN}`,
|
|
204
|
+
name: 'Sales pipeline',
|
|
205
|
+
owner: owner as NonNullable<Pipeline['owner']>,
|
|
206
|
+
stages: STAGES,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const deals: Deal[] = DEALS.map(([title, company, stageName, value, age, closeIn, lead, lostReason], n) => {
|
|
210
|
+
const urn = sampleUrn(2, n + 1);
|
|
211
|
+
const at = companyAt(company);
|
|
212
|
+
const contact = contactAt(at.urn);
|
|
213
|
+
const place = PLACES[stageName]!;
|
|
214
|
+
const closed = place.outcome !== 'OPEN';
|
|
215
|
+
return {
|
|
216
|
+
urn,
|
|
217
|
+
url: `/n/v1/deal/${urn}`,
|
|
218
|
+
owner,
|
|
219
|
+
title,
|
|
220
|
+
contactCompanies: [{ urn: at.urn }],
|
|
221
|
+
contactIdentities: contact ? [{ urn: contact.urn }] : [],
|
|
222
|
+
workflowStage: { urn: stage(place.stage).urn },
|
|
223
|
+
outcome: place.outcome,
|
|
224
|
+
probability: place.probability,
|
|
225
|
+
targetValue: money(value),
|
|
226
|
+
targetCloseTimestamp: ago(-closeIn),
|
|
227
|
+
closeTimestamp: closed ? ago(-closeIn) : null,
|
|
228
|
+
lostReason: lostReason ?? null,
|
|
229
|
+
assignee: assignee(n),
|
|
230
|
+
leadSource: lead === undefined ? null : { urn: sampleUrn(1, lead + 1) },
|
|
231
|
+
invoices: [],
|
|
232
|
+
createdTimestamp: ago(age),
|
|
233
|
+
updatedTimestamp: ago(Math.max(0, Math.min(age, closed ? -closeIn : 1))),
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Prospects' companies, made for the leads naming them: in the sample's companies, so their names show.
|
|
238
|
+
const prospects: SampleCompany[] = [];
|
|
239
|
+
const leads: Lead[] = LEADS.map(([title, company, source, state, value, age], n) => {
|
|
240
|
+
const urn = sampleUrn(1, n + 1);
|
|
241
|
+
let at = typeof company === 'number' ? companyAt(company) : undefined;
|
|
242
|
+
if (typeof company === 'string') {
|
|
243
|
+
at = { urn: sampleUrn(8, prospects.length + 1), name: company };
|
|
244
|
+
prospects.push(at);
|
|
245
|
+
}
|
|
246
|
+
const contact = at ? contactAt(at.urn) : undefined;
|
|
247
|
+
return {
|
|
248
|
+
urn,
|
|
249
|
+
url: `/n/v1/lead/${urn}`,
|
|
250
|
+
owner,
|
|
251
|
+
title,
|
|
252
|
+
contactIdentity: contact ? { urn: contact.urn } : null,
|
|
253
|
+
contactCompany: at ? { urn: at.urn } : null,
|
|
254
|
+
source,
|
|
255
|
+
status: state === 'CONVERTED' ? 'QUALIFIED' : state === 'CLOSED' ? 'CONTACTED' : state,
|
|
256
|
+
outcome: state === 'CONVERTED' || state === 'CLOSED' ? state : 'IN_PROGRESS',
|
|
257
|
+
closedReason: state === 'CLOSED' ? 'Wants own-brand stock we do not make' : null,
|
|
258
|
+
targetValue: money(value),
|
|
259
|
+
assignee: assignee(n + 1),
|
|
260
|
+
createdTimestamp: ago(age),
|
|
261
|
+
updatedTimestamp: ago(Math.max(0, age - 2)),
|
|
262
|
+
};
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const invoices: Invoice[] = [];
|
|
266
|
+
const remittances: Remittance[] = [];
|
|
267
|
+
INVOICES.forEach(([dealIndex, status, issuedAgo, dueIn, paidShare], n) => {
|
|
268
|
+
const deal = deals[dealIndex]!;
|
|
269
|
+
const urn = sampleUrn(4, n + 1);
|
|
270
|
+
const total = (deal.targetValue!.unitValue / scale) * (n === 2 || n === 3 ? 0.5 : 1);
|
|
271
|
+
const subTotal = total / 1.2;
|
|
272
|
+
const paid = total * paidShare;
|
|
273
|
+
const party = { company: deal.contactCompanies[0] ?? null, identity: deal.contactIdentities[0] ?? null };
|
|
274
|
+
deal.invoices.push({ urn });
|
|
275
|
+
invoices.push({
|
|
276
|
+
urn,
|
|
277
|
+
url: `/n/v1/paymentinvoice/${urn}`,
|
|
278
|
+
owner,
|
|
279
|
+
recipient: party,
|
|
280
|
+
remitter: party,
|
|
281
|
+
status,
|
|
282
|
+
currencyCode,
|
|
283
|
+
subTotal: money(subTotal),
|
|
284
|
+
tax: money(total - subTotal),
|
|
285
|
+
total: money(total),
|
|
286
|
+
paid: money(paid),
|
|
287
|
+
refunded: money(status === 'REFUNDED' ? paid : 0),
|
|
288
|
+
issuedTimestamp: status === 'DRAFT' ? null : ago(issuedAgo),
|
|
289
|
+
dueTimestamp: ago(-dueIn),
|
|
290
|
+
fullyPaidTimestamp: paidShare === 1 ? ago(Math.max(0, issuedAgo - 10)) : null,
|
|
291
|
+
lines: [
|
|
292
|
+
{
|
|
293
|
+
urn: sampleUrn(7, n + 1),
|
|
294
|
+
lineNumber: 1,
|
|
295
|
+
description: deal.title,
|
|
296
|
+
quantity: 1,
|
|
297
|
+
unitPrice: Number(subTotal.toFixed(2)),
|
|
298
|
+
price: money(subTotal),
|
|
299
|
+
tax: money(total - subTotal),
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
updatedTimestamp: ago(Math.max(0, issuedAgo - 10)),
|
|
303
|
+
});
|
|
304
|
+
if (paid > 0) {
|
|
305
|
+
remittances.push({
|
|
306
|
+
urn: sampleUrn(5, remittances.length + 1),
|
|
307
|
+
paymentInvoice: { urn },
|
|
308
|
+
remittanceDirection: 'PAYMENT',
|
|
309
|
+
remittanceStatus: 'RECEIVED',
|
|
310
|
+
value: money(paid),
|
|
311
|
+
remittanceMethod: METHODS[n % METHODS.length]!,
|
|
312
|
+
reference: `TW-${1040 + n}`,
|
|
313
|
+
remittingCompany: party.company,
|
|
314
|
+
remittingIdentity: party.identity,
|
|
315
|
+
receivedTimestamp: ago(Math.max(0, issuedAgo - 10)),
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (status === 'REFUNDED') {
|
|
319
|
+
remittances.push({
|
|
320
|
+
urn: sampleUrn(5, remittances.length + 1),
|
|
321
|
+
paymentInvoice: { urn },
|
|
322
|
+
remittanceDirection: 'REFUND',
|
|
323
|
+
remittanceStatus: 'RECEIVED',
|
|
324
|
+
value: money(paid),
|
|
325
|
+
remittanceMethod: 'BANK_TRANSFER',
|
|
326
|
+
reference: `TW-R${1040 + n}`,
|
|
327
|
+
remittingCompany: party.company,
|
|
328
|
+
remittingIdentity: party.identity,
|
|
329
|
+
receivedTimestamp: ago(Math.max(0, issuedAgo - 20)),
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
// Repeat orders from customers, paid soon after they're issued, so payments run through the last eight weeks.
|
|
334
|
+
REPEAT_ORDERS.forEach(([description, company, amount, issuedAgo, paidAfter], n) => {
|
|
335
|
+
const urn = sampleUrn(4, INVOICES.length + n + 1);
|
|
336
|
+
const at = companyAt(company);
|
|
337
|
+
const contact = contactAt(at.urn);
|
|
338
|
+
const party = { company: { urn: at.urn }, identity: contact ? { urn: contact.urn } : null };
|
|
339
|
+
const paid = paidAfter !== undefined && issuedAgo - paidAfter >= 0;
|
|
340
|
+
const subTotal = amount / 1.2;
|
|
341
|
+
invoices.push({
|
|
342
|
+
urn,
|
|
343
|
+
url: `/n/v1/paymentinvoice/${urn}`,
|
|
344
|
+
owner,
|
|
345
|
+
recipient: party,
|
|
346
|
+
remitter: party,
|
|
347
|
+
status: paid ? 'PAID' : 'AUTHORISED',
|
|
348
|
+
currencyCode,
|
|
349
|
+
subTotal: money(subTotal),
|
|
350
|
+
tax: money(amount - subTotal),
|
|
351
|
+
total: money(amount),
|
|
352
|
+
paid: money(paid ? amount : 0),
|
|
353
|
+
refunded: money(0),
|
|
354
|
+
issuedTimestamp: ago(issuedAgo),
|
|
355
|
+
dueTimestamp: ago(issuedAgo - 30),
|
|
356
|
+
fullyPaidTimestamp: paid ? ago(issuedAgo - paidAfter) : null,
|
|
357
|
+
lines: [
|
|
358
|
+
{
|
|
359
|
+
urn: sampleUrn(7, INVOICES.length + n + 1),
|
|
360
|
+
lineNumber: 1,
|
|
361
|
+
description,
|
|
362
|
+
quantity: 1,
|
|
363
|
+
price: money(subTotal),
|
|
364
|
+
},
|
|
365
|
+
],
|
|
366
|
+
updatedTimestamp: ago(paid ? issuedAgo - paidAfter : issuedAgo),
|
|
367
|
+
});
|
|
368
|
+
if (paid) {
|
|
369
|
+
remittances.push({
|
|
370
|
+
urn: sampleUrn(5, remittances.length + 1),
|
|
371
|
+
paymentInvoice: { urn },
|
|
372
|
+
remittanceDirection: 'PAYMENT',
|
|
373
|
+
remittanceStatus: 'RECEIVED',
|
|
374
|
+
value: money(amount),
|
|
375
|
+
remittanceMethod: METHODS[(n + 1) % METHODS.length]!,
|
|
376
|
+
reference: `TW-${1100 + n}`,
|
|
377
|
+
remittingCompany: party.company,
|
|
378
|
+
remittingIdentity: party.identity,
|
|
379
|
+
receivedTimestamp: ago(issuedAgo - paidAfter),
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
// A card payment that failed, against the invoice still awaiting payment.
|
|
384
|
+
remittances.push({
|
|
385
|
+
urn: sampleUrn(5, remittances.length + 1),
|
|
386
|
+
paymentInvoice: { urn: invoices[4]!.urn },
|
|
387
|
+
remittanceDirection: 'PAYMENT',
|
|
388
|
+
remittanceStatus: 'FAILED',
|
|
389
|
+
value: invoices[4]!.total,
|
|
390
|
+
remittanceMethod: 'CARD_PAYMENT',
|
|
391
|
+
reference: 'TW-1044-DECLINED',
|
|
392
|
+
remittingCompany: invoices[4]!.remitter?.company ?? null,
|
|
393
|
+
remittingIdentity: invoices[4]!.remitter?.identity ?? null,
|
|
394
|
+
receivedTimestamp: ago(5),
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
return { companies: [...companies, ...prospects], pipelines: [pipeline], leads, deals, invoices, remittances };
|
|
398
|
+
}
|