@baack-software/baack-ts-crm 0.0.4 → 0.0.6
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/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/react/Charts.d.ts +18 -0
- package/dist/react/Charts.d.ts.map +1 -1
- package/dist/react/Charts.js +30 -3
- package/dist/react/Charts.js.map +1 -1
- package/dist/react/DashboardCards.d.ts +4 -1
- package/dist/react/DashboardCards.d.ts.map +1 -1
- package/dist/react/DashboardCards.js +5 -2
- package/dist/react/DashboardCards.js.map +1 -1
- package/dist/react/text.d.ts +8 -1
- package/dist/react/text.d.ts.map +1 -1
- package/dist/react/text.js +8 -1
- package/dist/react/text.js.map +1 -1
- package/dist/react/views.d.ts +2 -1
- package/dist/react/views.d.ts.map +1 -1
- package/dist/react/views.js +4 -3
- package/dist/react/views.js.map +1 -1
- package/dist/store.d.ts +5 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +5 -2
- package/dist/store.js.map +1 -1
- package/dist/target.d.ts +9 -7
- package/dist/target.d.ts.map +1 -1
- package/dist/target.js +9 -20
- package/dist/target.js.map +1 -1
- package/dist/timeline.d.ts +29 -0
- package/dist/timeline.d.ts.map +1 -0
- package/dist/timeline.js +44 -0
- package/dist/timeline.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/react/Charts.tsx +139 -4
- package/src/react/DashboardCards.tsx +13 -9
- package/src/react/text.ts +8 -1
- package/src/react/views.tsx +27 -3
- package/src/store.ts +14 -2
- package/src/target.ts +12 -23
- package/src/timeline.ts +72 -0
package/src/target.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { addMoney, type MoneyTotals } from './money.ts';
|
|
2
2
|
import { paymentsReceived } from './invoices.ts';
|
|
3
|
-
import { leadOutcome } from './leads.ts';
|
|
4
3
|
import { dealOutcome } from './pipeline.ts';
|
|
5
|
-
import type { Deal,
|
|
4
|
+
import type { Deal, Remittance } from './types.ts';
|
|
6
5
|
|
|
7
6
|
export interface SalesTarget {
|
|
8
|
-
/** The target per currency:
|
|
7
|
+
/** The target per currency: the target values of qualified deals still open or won. */
|
|
9
8
|
target: MoneyTotals;
|
|
10
9
|
/** Won deals' target values per currency. */
|
|
11
10
|
won: MoneyTotals;
|
|
@@ -15,33 +14,23 @@ export interface SalesTarget {
|
|
|
15
14
|
progress: Readonly<Record<string, number>>;
|
|
16
15
|
}
|
|
17
16
|
|
|
17
|
+
/** Whether a deal counts towards the sales target: every qualified deal except those lost. */
|
|
18
|
+
export const countsTowardsTarget = (deal: Deal): boolean => dealOutcome(deal) !== 'LOST';
|
|
19
|
+
|
|
18
20
|
/**
|
|
19
|
-
* The sales target and progress towards it. The target is the sum of the
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* The sales target and progress towards it. The target is the sum of the qualified deals' target values (decided
|
|
22
|
+
* 12 Nov 2026, in place of also counting leads not yet qualified): a deal converted from a lead and one created
|
|
23
|
+
* without a lead count alike, and leads don't count until they're qualified into a deal. Lost deals don't count
|
|
24
|
+
* (decided 27 Oct 2026): the target is what's still open or won.
|
|
23
25
|
* Progress is the payments received against the target (decided 24 Oct 2026): money in on the invoices raised for the
|
|
24
26
|
* deals (`Deal.invoices`), refunds taken off. Payments on invoices without a deal, such as repeat orders, don't count.
|
|
25
27
|
*/
|
|
26
|
-
export function salesTarget(
|
|
27
|
-
leads: readonly Lead[],
|
|
28
|
-
deals: readonly Deal[],
|
|
29
|
-
remittances: readonly Remittance[],
|
|
30
|
-
): SalesTarget {
|
|
31
|
-
const fromLeads = new Set(deals.flatMap((deal) => (deal.leadSource ? [deal.leadSource.urn] : [])));
|
|
28
|
+
export function salesTarget(deals: readonly Deal[], remittances: readonly Remittance[]): SalesTarget {
|
|
32
29
|
let target: MoneyTotals = {};
|
|
33
30
|
let won: MoneyTotals = {};
|
|
34
|
-
for (const deal of deals) {
|
|
35
|
-
const outcome = dealOutcome(deal);
|
|
36
|
-
if (outcome === 'LOST') continue;
|
|
31
|
+
for (const deal of deals.filter(countsTowardsTarget)) {
|
|
37
32
|
target = addMoney(target, deal.targetValue);
|
|
38
|
-
if (
|
|
39
|
-
}
|
|
40
|
-
for (const lead of leads) {
|
|
41
|
-
if (leadOutcome(lead) === 'CLOSED') continue;
|
|
42
|
-
// A lead that became one of these deals (its leadSource) is counted by its deal's value above.
|
|
43
|
-
if (fromLeads.has(lead.urn)) continue;
|
|
44
|
-
target = addMoney(target, lead.targetValue);
|
|
33
|
+
if (dealOutcome(deal) === 'WON') won = addMoney(won, deal.targetValue);
|
|
45
34
|
}
|
|
46
35
|
const invoiced = new Set(deals.flatMap((deal) => deal.invoices.map((invoice) => invoice.urn)));
|
|
47
36
|
const received = paymentsReceived(remittances.filter((remittance) => invoiced.has(remittance.paymentInvoice.urn)));
|
package/src/timeline.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { addMoney, type MoneyTotals } from './money.ts';
|
|
2
|
+
import { inRange, type DateRange } from './period.ts';
|
|
3
|
+
import type { Money } from './types.ts';
|
|
4
|
+
|
|
5
|
+
/** How a timeline groups its records: by UTC day, or by week (starting Monday) for longer periods. */
|
|
6
|
+
export type TimelineStep = 'day' | 'week';
|
|
7
|
+
|
|
8
|
+
export interface TimelinePoint {
|
|
9
|
+
/** The UTC date the day or week starts on, such as `2026-11-09`. */
|
|
10
|
+
day: string;
|
|
11
|
+
/** How many records fell in it. */
|
|
12
|
+
count: number;
|
|
13
|
+
/** Their values, per currency. */
|
|
14
|
+
value: MoneyTotals;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Timeline {
|
|
18
|
+
step: TimelineStep;
|
|
19
|
+
points: TimelinePoint[];
|
|
20
|
+
/** The currency with the most value across the points, which charts use; others aren't added in. */
|
|
21
|
+
currencyCode: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DAY = 86_400_000;
|
|
25
|
+
|
|
26
|
+
/** Ranges longer than this are grouped by week, so a quarter or a year stays readable in a small chart. */
|
|
27
|
+
const MAX_DAYS = 31;
|
|
28
|
+
|
|
29
|
+
/** The step a timeline over `range` uses. */
|
|
30
|
+
export const timelineStep = (range: DateRange): TimelineStep =>
|
|
31
|
+
range.end.getTime() - range.start.getTime() > MAX_DAYS * DAY ? 'week' : 'day';
|
|
32
|
+
|
|
33
|
+
/** The days or weeks across a range, every one included, the first starting on or before the range's start. */
|
|
34
|
+
export function timeSteps(range: DateRange, step = timelineStep(range)): DateRange[] {
|
|
35
|
+
let start = new Date(Date.UTC(range.start.getUTCFullYear(), range.start.getUTCMonth(), range.start.getUTCDate()));
|
|
36
|
+
// getUTCDay is 0 on Sunday: back up to the Monday.
|
|
37
|
+
if (step === 'week') start = new Date(start.getTime() - ((start.getUTCDay() + 6) % 7) * DAY);
|
|
38
|
+
const length = step === 'week' ? 7 * DAY : DAY;
|
|
39
|
+
const steps: DateRange[] = [];
|
|
40
|
+
for (let at = start; at < range.end; at = new Date(at.getTime() + length)) {
|
|
41
|
+
steps.push({ start: at, end: new Date(at.getTime() + length) });
|
|
42
|
+
}
|
|
43
|
+
return steps;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Records in a range counted and valued per day or week, for a chart: `when` places a record in time and `value` is
|
|
48
|
+
* what it's worth. Only records within the range count, so a first or last week shows the part inside it.
|
|
49
|
+
*/
|
|
50
|
+
export function timeline<T>(
|
|
51
|
+
records: readonly T[],
|
|
52
|
+
range: DateRange,
|
|
53
|
+
when: (record: T) => string | null | undefined,
|
|
54
|
+
value: (record: T) => Money | null | undefined,
|
|
55
|
+
): Timeline {
|
|
56
|
+
const step = timelineStep(range);
|
|
57
|
+
const within = records.filter((record) => inRange(when(record), range));
|
|
58
|
+
const points = timeSteps(range, step).map((span) => {
|
|
59
|
+
const inSpan = within.filter((record) => inRange(when(record), span));
|
|
60
|
+
return {
|
|
61
|
+
day: span.start.toISOString().slice(0, 10),
|
|
62
|
+
count: inSpan.length,
|
|
63
|
+
value: inSpan.reduce<MoneyTotals>((totals, record) => addMoney(totals, value(record)), {}),
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
const sums: Record<string, number> = {};
|
|
67
|
+
for (const point of points) {
|
|
68
|
+
for (const [currency, minor] of Object.entries(point.value)) sums[currency] = (sums[currency] ?? 0) + minor;
|
|
69
|
+
}
|
|
70
|
+
const currencyCode = Object.entries(sums).sort(([, a], [, b]) => b - a)[0]?.[0];
|
|
71
|
+
return { step, points, currencyCode };
|
|
72
|
+
}
|