@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
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { defaultPipeline } from '../pipeline.ts';
|
|
3
|
+
import type { Deal, Pipeline } from '../types.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What this user last chose in the deals, per company, kept in the browser: the workflow they work in (shared by the
|
|
7
|
+
* dashboard, converting leads and the deals board) and the board's order. One record, so each writer keeps the other's.
|
|
8
|
+
*/
|
|
9
|
+
export interface DealsMemory {
|
|
10
|
+
pipeline?: string | undefined;
|
|
11
|
+
order?: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const memoryKey = (owner: string) => `baack-crm:deals-board:${owner}`;
|
|
15
|
+
|
|
16
|
+
export function recallDeals(owner: string): DealsMemory {
|
|
17
|
+
try {
|
|
18
|
+
const saved = JSON.parse(localStorage.getItem(memoryKey(owner)) ?? '{}') as { pipeline?: unknown; order?: unknown };
|
|
19
|
+
return {
|
|
20
|
+
pipeline: typeof saved.pipeline === 'string' ? saved.pipeline : undefined,
|
|
21
|
+
order: typeof saved.order === 'string' ? saved.order : undefined,
|
|
22
|
+
};
|
|
23
|
+
} catch {
|
|
24
|
+
// No storage (private browsing, blocked, or no browser): start afresh.
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Keeps `changes`, merged into what's stored, so a change to one field never puts back an old value of another. */
|
|
30
|
+
export function rememberDeals(owner: string, changes: DealsMemory): void {
|
|
31
|
+
try {
|
|
32
|
+
localStorage.setItem(memoryKey(owner), JSON.stringify({ ...recallDeals(owner), ...changes }));
|
|
33
|
+
} catch {
|
|
34
|
+
// Not kept; the views still work.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The pipeline this user works in, for the company `owner`: the one they last chose (their team may work in a
|
|
40
|
+
* different workflow from other users'), else the default (`defaultPipeline`: the workflow with the most deals).
|
|
41
|
+
* Choosing one remembers it.
|
|
42
|
+
*/
|
|
43
|
+
export function usePipeline(
|
|
44
|
+
owner: string,
|
|
45
|
+
pipelines: readonly Pipeline[],
|
|
46
|
+
deals: readonly Deal[],
|
|
47
|
+
): [Pipeline | undefined, (urn: string) => void] {
|
|
48
|
+
const [chosen, setChosen] = useState(() => recallDeals(owner).pipeline);
|
|
49
|
+
const choose = (urn: string) => {
|
|
50
|
+
setChosen(urn);
|
|
51
|
+
rememberDeals(owner, { pipeline: urn });
|
|
52
|
+
};
|
|
53
|
+
return [defaultPipeline(pipelines, deals, chosen), choose];
|
|
54
|
+
}
|
package/src/react/text.ts
CHANGED
|
@@ -9,6 +9,9 @@ export const CRM_TEXT = {
|
|
|
9
9
|
loading: 'Loading',
|
|
10
10
|
noChange: 'No earlier figures',
|
|
11
11
|
vsPrevious: 'vs previous period',
|
|
12
|
+
vsPrevious30: 'vs the 30 days before',
|
|
13
|
+
vsLastQuarter: 'vs the same time last quarter',
|
|
14
|
+
vsLastYear: 'vs the same time last year',
|
|
12
15
|
customers: 'Customers',
|
|
13
16
|
customersHint: 'Companies with a won deal',
|
|
14
17
|
openDeals: 'Open deals',
|
|
@@ -22,6 +25,7 @@ export const CRM_TEXT = {
|
|
|
22
25
|
leadsBySource: 'Leads by source',
|
|
23
26
|
pipeline: 'Sales pipeline',
|
|
24
27
|
stage: 'Stage',
|
|
28
|
+
workflow: 'Workflow',
|
|
25
29
|
deals: 'Deals',
|
|
26
30
|
value: 'Value',
|
|
27
31
|
weighted: 'Weighted',
|
|
@@ -112,8 +116,31 @@ export const CRM_TEXT = {
|
|
|
112
116
|
title: 'Title',
|
|
113
117
|
titleRequired: 'Enter a title.',
|
|
114
118
|
noCompany: 'No company',
|
|
115
|
-
newCompany: '
|
|
116
|
-
newCompanyHint: 'Makes the company, owned by yours
|
|
119
|
+
newCompany: 'New company',
|
|
120
|
+
newCompanyHint: 'Makes the client company, owned by yours, when the lead is saved.',
|
|
121
|
+
addCompany: 'Add a new company',
|
|
122
|
+
companyNameRequired: 'Enter the company name.',
|
|
123
|
+
noContact: 'No contact',
|
|
124
|
+
newContact: 'New contact',
|
|
125
|
+
newContactHint:
|
|
126
|
+
"Makes a person owned by your company when the lead is saved, linked to the lead's company when it has one.",
|
|
127
|
+
addContact: 'Add a new person',
|
|
128
|
+
addPerson: 'Add a person',
|
|
129
|
+
givenName: 'Given name',
|
|
130
|
+
familyName: 'Family name',
|
|
131
|
+
email: 'Email',
|
|
132
|
+
givenNameRequired: 'Enter a given name.',
|
|
133
|
+
telephone: 'Telephone',
|
|
134
|
+
contactDetails: "Contact's details",
|
|
135
|
+
contactEmail: "Contact's email",
|
|
136
|
+
contactTelephone: "Contact's telephone",
|
|
137
|
+
contactDetailsUnavailable: "The contact's email and telephone couldn't be read.",
|
|
138
|
+
unassigned: 'Unassigned',
|
|
139
|
+
toBeCreated: 'new',
|
|
140
|
+
useThis: 'Use this',
|
|
141
|
+
saving: 'Saving',
|
|
142
|
+
back: 'Back',
|
|
143
|
+
saveFailed: "The change couldn't be saved.",
|
|
117
144
|
createLead: 'Create lead',
|
|
118
145
|
createDeal: 'Create deal',
|
|
119
146
|
createInvoice: 'Create invoice',
|
|
@@ -133,9 +160,70 @@ export const CRM_TEXT = {
|
|
|
133
160
|
subTotal: 'Subtotal',
|
|
134
161
|
tax: 'Tax',
|
|
135
162
|
customerRequired: 'Choose the customer.',
|
|
163
|
+
amountAs: 'Amount as',
|
|
164
|
+
amountAsLines: 'Line items',
|
|
165
|
+
amountAsTotal: 'A total',
|
|
166
|
+
totalWithTax: 'Total, including tax',
|
|
167
|
+
totalHint: 'The subtotal and tax are worked out from it at the tax rate.',
|
|
168
|
+
totalRequired: 'Enter the total, above zero, such as 1,200.00.',
|
|
136
169
|
linesRequired: 'Give each line a description, a quantity and a unit price above zero.',
|
|
137
170
|
invalidRate: 'Enter a tax rate from 0 to 100.',
|
|
138
171
|
invalidProbability: 'Enter a probability from 0 to 100.',
|
|
172
|
+
companies: 'Companies',
|
|
173
|
+
contacts: 'Contacts',
|
|
174
|
+
searchCompanies: 'Search companies by name',
|
|
175
|
+
searchContacts: 'Search people by name, email or telephone',
|
|
176
|
+
chosen: 'Chosen',
|
|
177
|
+
remove: 'Remove',
|
|
178
|
+
noMatches: 'No matches.',
|
|
179
|
+
filterDeals: 'Filter deals',
|
|
180
|
+
allOutcomes: 'All outcomes',
|
|
181
|
+
anyone: 'Anyone',
|
|
182
|
+
allCompanies: 'All companies',
|
|
183
|
+
clearFilters: 'Clear filters',
|
|
184
|
+
sortBy: 'Sort by',
|
|
185
|
+
orderWeighted: 'Weighted',
|
|
186
|
+
orderSize: 'Deal size',
|
|
187
|
+
orderUrgency: 'Urgency',
|
|
188
|
+
dealsShown: 'deals shown',
|
|
189
|
+
remitter: 'Remitter: who pays',
|
|
190
|
+
remitterCompany: 'Company that pays',
|
|
191
|
+
remitterPerson: 'Person who pays',
|
|
192
|
+
recipient: 'Recipient: who is paid',
|
|
193
|
+
recipientCompany: 'Company paid',
|
|
194
|
+
recipientPerson: 'Person who is paid',
|
|
195
|
+
recipientHint: 'Usually your company, or one of its companies; another company for an invoice you pay.',
|
|
196
|
+
remitterRequired: 'Choose the company or person who pays.',
|
|
197
|
+
recipientRequired: 'Choose the company or person who is paid.',
|
|
198
|
+
searchPeople: 'Search people by name, email or telephone',
|
|
199
|
+
noPerson: 'No person',
|
|
200
|
+
line: 'Line',
|
|
201
|
+
lineDetails: 'All fields',
|
|
202
|
+
itemCode: 'Item code',
|
|
203
|
+
discountRate: 'Discount (%)',
|
|
204
|
+
taxability: 'Tax treatment',
|
|
205
|
+
taxName: 'Tax name',
|
|
206
|
+
price: 'Price',
|
|
207
|
+
done: 'Done',
|
|
208
|
+
invalidDiscount: 'Enter a discount from 0 to 100.',
|
|
209
|
+
costs: 'Costs',
|
|
210
|
+
costsHint: 'Invoices you pay, issued in the period',
|
|
211
|
+
toPay: 'To pay',
|
|
212
|
+
party: 'Customer or supplier',
|
|
213
|
+
paidBy: 'Paid by',
|
|
214
|
+
paidTo: 'Paid to',
|
|
215
|
+
allKinds: 'Sales and costs',
|
|
216
|
+
salesOnly: 'Sales',
|
|
217
|
+
costsOnly: 'Costs',
|
|
218
|
+
cost: 'Cost',
|
|
219
|
+
drafts: 'Drafts',
|
|
220
|
+
draftsHint: 'Not yet sent, so not counted above',
|
|
221
|
+
draftInvoice: 'draft invoice',
|
|
222
|
+
draftInvoices: 'draft invoices',
|
|
223
|
+
period: 'Period',
|
|
224
|
+
periodLast30: 'Last 30 days',
|
|
225
|
+
periodQuarter: 'This quarter',
|
|
226
|
+
periodYear: 'This year',
|
|
139
227
|
} as const;
|
|
140
228
|
|
|
141
229
|
export type CrmText = { readonly [K in keyof typeof CRM_TEXT]: string };
|
package/src/react/views.tsx
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { Plus } from 'lucide-react';
|
|
2
|
-
import { useMemo, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
3
|
-
import {
|
|
2
|
+
import { useEffect, useId, useMemo, useState, useSyncExternalStore, type ReactNode } from 'react';
|
|
3
|
+
import { ref } from '@baack-software/baack-ts-tasks';
|
|
4
|
+
import { DEAL_ORDERS, filterDeals, type DealFilter, type DealOrder } from '../deal-order.ts';
|
|
5
|
+
import { periodRange, type DateRange, type ReportingPeriod } from '../period.ts';
|
|
6
|
+
import { pipelineOf } from '../pipeline.ts';
|
|
4
7
|
import { selectAccount, selectDashboard, selectSales, type CrmStore, type CrmStoreState } from '../store.ts';
|
|
5
8
|
import type { CrmConcept, Deal, DealOutcome, Invoice, Lead, PipelineStage } from '../types.ts';
|
|
6
9
|
import { LeadsBySourceCard, RevenueChart } from './Charts.tsx';
|
|
7
10
|
import { MetricCards, PipelineCard, TargetCard } from './DashboardCards.tsx';
|
|
8
|
-
import { DealsBoard } from './DealsBoard.tsx';
|
|
9
|
-
import { DealDetail, LeadDetail } from './Details.tsx';
|
|
11
|
+
import { DealFilters, DealsBoard } from './DealsBoard.tsx';
|
|
12
|
+
import { DealDetail, LeadDetail, type ContactDetailsAccess } from './Details.tsx';
|
|
10
13
|
import { CompanyAccountView } from './CompanyAccountView.tsx';
|
|
11
14
|
import type { CrmNames } from './format.ts';
|
|
12
15
|
import { LeadsTable } from './LeadsTable.tsx';
|
|
@@ -18,11 +21,21 @@ import {
|
|
|
18
21
|
SalesMetricCards,
|
|
19
22
|
invoiceTitle,
|
|
20
23
|
} from './Sales.tsx';
|
|
21
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
NewDealForm,
|
|
26
|
+
NewInvoiceForm,
|
|
27
|
+
NewLeadForm,
|
|
28
|
+
type CrmChoice,
|
|
29
|
+
type CrmCompanyChoice,
|
|
30
|
+
type CrmPersonChoice,
|
|
31
|
+
} from './Forms.tsx';
|
|
32
|
+
import { recallDeals, rememberDeals, usePipeline } from './pipeline-choice.ts';
|
|
22
33
|
import { SidePanelLayout, recordItemId } from './SidePanel.tsx';
|
|
23
|
-
import { SampleDataNotice } from './parts.tsx';
|
|
34
|
+
import { PeriodSelect, SampleDataNotice, periodText } from './parts.tsx';
|
|
24
35
|
import { withText, type CrmText } from './text.ts';
|
|
25
36
|
import { Alert, AlertDescription, AlertTitle } from './ui/alert.tsx';
|
|
37
|
+
import { Label } from './ui/label.tsx';
|
|
38
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select.tsx';
|
|
26
39
|
import { Button } from './ui/button.tsx';
|
|
27
40
|
import { Skeleton } from './ui/skeleton.tsx';
|
|
28
41
|
|
|
@@ -33,16 +46,33 @@ export function useCrmStore(store: CrmStore): CrmStoreState {
|
|
|
33
46
|
|
|
34
47
|
/** The host's names, falling back to the companies the store made (a converted lead's prospect) for theirs. */
|
|
35
48
|
export function useCrmNames(names: CrmNames | undefined, state: CrmStoreState): CrmNames {
|
|
36
|
-
const { companies } = state;
|
|
49
|
+
const { companies, people } = state;
|
|
37
50
|
return useMemo(
|
|
38
51
|
() => ({
|
|
39
52
|
...names,
|
|
40
53
|
company: (urn: string) => names?.company?.(urn) ?? companies.find((company) => company.urn === urn)?.name,
|
|
54
|
+
person: (urn: string) => names?.person?.(urn) ?? people.find((person) => person.urn === urn)?.name,
|
|
41
55
|
}),
|
|
42
|
-
[names, companies],
|
|
56
|
+
[names, companies, people],
|
|
43
57
|
);
|
|
44
58
|
}
|
|
45
59
|
|
|
60
|
+
/** The store's reading and setting of people's email and telephone, kept stable so the fields don't reload. */
|
|
61
|
+
const useContactDetails = (store: CrmStore): ContactDetailsAccess =>
|
|
62
|
+
useMemo(
|
|
63
|
+
() => ({
|
|
64
|
+
load: (identity) => store.contactDetails(identity),
|
|
65
|
+
save: (identity, details) => store.setContactDetails(identity, details),
|
|
66
|
+
}),
|
|
67
|
+
[store],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/** Choices with the records the store made while open, such as a lead's new contact, added where they're missing. */
|
|
71
|
+
const withMade = (choices: readonly CrmChoice[], made: readonly CrmChoice[]): readonly CrmChoice[] => [
|
|
72
|
+
...choices,
|
|
73
|
+
...made.filter((record) => !choices.some((choice) => choice.urn === record.urn)),
|
|
74
|
+
];
|
|
75
|
+
|
|
46
76
|
const CONCEPT_NAMES: Record<CrmConcept, string> = {
|
|
47
77
|
leads: 'Leads',
|
|
48
78
|
deals: 'Deals',
|
|
@@ -127,18 +157,21 @@ function NewButton({ panel, label, onClick }: { panel: string; label: string; on
|
|
|
127
157
|
}
|
|
128
158
|
|
|
129
159
|
/**
|
|
130
|
-
* The CRM dashboard for a period
|
|
131
|
-
* source, the pipeline, the host's tasks panel and the leads table.
|
|
160
|
+
* The CRM dashboard for a period: `range`, else the one picked (the last 30 days to start with), with figures against
|
|
161
|
+
* the period before, the target, leads by source, the pipeline, the host's tasks panel and the leads table.
|
|
132
162
|
*/
|
|
133
163
|
export function CrmDashboard({
|
|
134
164
|
store,
|
|
135
165
|
names,
|
|
136
166
|
text,
|
|
137
167
|
range,
|
|
168
|
+
ownCompanies,
|
|
138
169
|
tasks,
|
|
139
170
|
onOpenLead,
|
|
140
171
|
}: ViewProps & {
|
|
141
172
|
range?: DateRange | undefined;
|
|
173
|
+
/** The companies the business counts as itself (the owner and its child companies), to leave its costs out. */
|
|
174
|
+
ownCompanies?: readonly string[] | undefined;
|
|
142
175
|
/** The host's tasks panel, such as baack-ts-tasks' TasksAbout. */
|
|
143
176
|
tasks?: ReactNode;
|
|
144
177
|
onOpenLead?: ((lead: Lead) => void) | undefined;
|
|
@@ -146,15 +179,22 @@ export function CrmDashboard({
|
|
|
146
179
|
const state = useCrmStore(store);
|
|
147
180
|
names = useCrmNames(names, state);
|
|
148
181
|
const words = withText(text);
|
|
149
|
-
const
|
|
150
|
-
const
|
|
182
|
+
const [picked, setPicked] = useState<ReportingPeriod>('last30');
|
|
183
|
+
const period = useMemo(() => range ?? periodRange(picked), [range, picked]);
|
|
184
|
+
const ownKey = ownCompanies?.join(',');
|
|
185
|
+
const [pipeline] = usePipeline(ref(store.options.owner).urn, state.pipelines, state.deals);
|
|
186
|
+
const dashboard = useMemo(
|
|
187
|
+
() => selectDashboard(state, period, ownKey === undefined ? undefined : new Set(ownKey.split(',')), pipeline?.urn),
|
|
188
|
+
[state, period, ownKey, pipeline?.urn],
|
|
189
|
+
);
|
|
151
190
|
if (state.status === 'idle' || state.status === 'loading') return <Loading label={words.loading} />;
|
|
152
191
|
const concepts: CrmConcept[] = ['leads', 'deals', 'pipelines', 'remittances'];
|
|
153
192
|
return (
|
|
154
193
|
<div className="grid gap-6">
|
|
155
194
|
<Sample store={store} concepts={concepts} text={text} />
|
|
156
195
|
<Problems state={state} concepts={concepts} text={words} />
|
|
157
|
-
<
|
|
196
|
+
{!range && <PeriodSelect value={picked} onChange={setPicked} text={text} />}
|
|
197
|
+
<MetricCards dashboard={dashboard} text={range ? text : periodText(picked, text)} />
|
|
158
198
|
<RevenueChart days={dashboard.revenueByDay} currencyCode={dashboard.revenue.currencyCode} text={text} />
|
|
159
199
|
<div className={tasks ? 'grid items-start gap-6 lg:grid-cols-3' : 'grid items-start gap-6 lg:grid-cols-2'}>
|
|
160
200
|
<TargetCard target={dashboard.target} text={text} />
|
|
@@ -173,10 +213,16 @@ export function CrmLeadsView({
|
|
|
173
213
|
names,
|
|
174
214
|
text,
|
|
175
215
|
companies,
|
|
216
|
+
people = [],
|
|
217
|
+
team = [],
|
|
176
218
|
selected,
|
|
177
219
|
onSelect,
|
|
178
220
|
onConverted,
|
|
179
221
|
}: ViewProps & {
|
|
222
|
+
/** People a lead's contact can be chosen from: the host's customers' people. */
|
|
223
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
224
|
+
/** People a lead can be assigned to: the host's team. */
|
|
225
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
180
226
|
selected?: string | undefined;
|
|
181
227
|
onSelect?: ((lead: Lead | undefined) => void) | undefined;
|
|
182
228
|
/** After a lead becomes a deal, for example to open the deal. */
|
|
@@ -185,10 +231,16 @@ export function CrmLeadsView({
|
|
|
185
231
|
const state = useCrmStore(store);
|
|
186
232
|
names = useCrmNames(names, state);
|
|
187
233
|
const words = withText(text);
|
|
234
|
+
const contacts = withMade(people, state.people);
|
|
235
|
+
const contactDetails = useContactDetails(store);
|
|
236
|
+
const customers = withMade(
|
|
237
|
+
companies ?? [],
|
|
238
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
239
|
+
);
|
|
188
240
|
const [own, setOwn] = useState<string>();
|
|
189
241
|
const urn = onSelect ? selected : (own ?? selected);
|
|
190
242
|
const lead = state.leads.find((candidate) => candidate.urn === urn);
|
|
191
|
-
const pipeline = state.pipelines
|
|
243
|
+
const [pipeline] = usePipeline(ref(store.options.owner).urn, state.pipelines, state.deals);
|
|
192
244
|
const [creating, setCreating] = useState(false);
|
|
193
245
|
const select = (next: Lead | undefined) => {
|
|
194
246
|
setCreating(false);
|
|
@@ -210,7 +262,9 @@ export function CrmLeadsView({
|
|
|
210
262
|
creating ? (
|
|
211
263
|
<NewLeadForm
|
|
212
264
|
owner={store.options.owner}
|
|
213
|
-
companies={
|
|
265
|
+
companies={customers}
|
|
266
|
+
people={contacts}
|
|
267
|
+
team={team}
|
|
214
268
|
currencyCode={currencyOf(state)}
|
|
215
269
|
text={text}
|
|
216
270
|
onCreate={async (input, options) => select(await store.createLead(input, options))}
|
|
@@ -221,8 +275,13 @@ export function CrmLeadsView({
|
|
|
221
275
|
key={lead.urn}
|
|
222
276
|
lead={lead}
|
|
223
277
|
names={names}
|
|
278
|
+
companies={customers}
|
|
279
|
+
people={contacts}
|
|
280
|
+
team={team}
|
|
281
|
+
onChange={(changes, options) => store.updateLead(lead, changes, options)}
|
|
224
282
|
text={text}
|
|
225
283
|
converting={state.saving.has(lead.urn)}
|
|
284
|
+
contactDetails={contactDetails}
|
|
226
285
|
onSetOutcome={(closing, outcome, closedReason) =>
|
|
227
286
|
void store
|
|
228
287
|
.setLeadOutcome(closing, outcome, closedReason === undefined ? {} : { closedReason })
|
|
@@ -248,22 +307,74 @@ export function CrmLeadsView({
|
|
|
248
307
|
);
|
|
249
308
|
}
|
|
250
309
|
|
|
251
|
-
/**
|
|
310
|
+
/** The deals board's own part of what's remembered (see `rememberDeals`): its order. The workflow is the shared choice. */
|
|
311
|
+
interface DealsBoardMemory {
|
|
312
|
+
order?: DealOrder | undefined;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function recall(owner: string): DealsBoardMemory {
|
|
316
|
+
const saved = recallDeals(owner).order;
|
|
317
|
+
return { order: DEAL_ORDERS.find((order) => order === saved) };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Deals as a board, one workflow at a time: the one this user last chose, else the one with the most deals (see
|
|
322
|
+
* `usePipeline`). Filters
|
|
323
|
+
* and the columns' order sit above it; the opened deal shows to the right, as tasks do, and the board moves to its
|
|
324
|
+
* workflow.
|
|
325
|
+
*/
|
|
252
326
|
export function CrmDealsView({
|
|
253
327
|
store,
|
|
254
328
|
names,
|
|
255
329
|
text,
|
|
256
330
|
companies,
|
|
331
|
+
people = [],
|
|
332
|
+
team = [],
|
|
257
333
|
selected,
|
|
258
334
|
onSelect,
|
|
259
|
-
}: ViewProps & {
|
|
335
|
+
}: ViewProps & {
|
|
336
|
+
/** People a deal's contact can be chosen from: the host's customers' people. */
|
|
337
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
338
|
+
/** People a deal can be assigned to: the host's team. */
|
|
339
|
+
team?: readonly CrmPersonChoice[] | undefined;
|
|
340
|
+
selected?: string | undefined;
|
|
341
|
+
onSelect?: ((deal: Deal | undefined) => void) | undefined;
|
|
342
|
+
}) {
|
|
260
343
|
const state = useCrmStore(store);
|
|
261
344
|
names = useCrmNames(names, state);
|
|
262
345
|
const words = withText(text);
|
|
346
|
+
const ids = useId();
|
|
263
347
|
const [own, setOwn] = useState<string>();
|
|
264
348
|
const urn = onSelect ? selected : (own ?? selected);
|
|
265
349
|
const deal = state.deals.find((candidate) => candidate.urn === urn);
|
|
266
|
-
const
|
|
350
|
+
const owner = ref(store.options.owner).urn;
|
|
351
|
+
const [memory, setMemory] = useState<DealsBoardMemory>(() => recall(owner));
|
|
352
|
+
const update = (changes: DealsBoardMemory) =>
|
|
353
|
+
setMemory((current) => {
|
|
354
|
+
const next = { ...current, ...changes };
|
|
355
|
+
rememberDeals(owner, next);
|
|
356
|
+
return next;
|
|
357
|
+
});
|
|
358
|
+
const [shown, setShown] = usePipeline(owner, state.pipelines, state.deals);
|
|
359
|
+
const order = memory.order ?? 'weighted';
|
|
360
|
+
const [filter, setFilter] = useState<DealFilter>({});
|
|
361
|
+
// An opened deal in another workflow, such as one followed from a customer's page, brings the board to it.
|
|
362
|
+
const opened = deal && pipelineOf(deal, state.pipelines)?.urn;
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
if (opened) setShown(opened);
|
|
365
|
+
// setShown is stable in effect: it only sets state and storage.
|
|
366
|
+
}, [opened]);
|
|
367
|
+
const pipeline = shown;
|
|
368
|
+
const visible = useMemo(
|
|
369
|
+
() => filterDeals(state.deals, filter, (urn) => names?.company?.(urn) ?? names?.person?.(urn)),
|
|
370
|
+
[state.deals, filter, names],
|
|
371
|
+
);
|
|
372
|
+
const contacts = withMade(people, state.people);
|
|
373
|
+
const contactDetails = useContactDetails(store);
|
|
374
|
+
const customers = withMade(
|
|
375
|
+
companies ?? [],
|
|
376
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
377
|
+
);
|
|
267
378
|
const [creating, setCreating] = useState(false);
|
|
268
379
|
const select = (next: Deal | undefined) => {
|
|
269
380
|
setCreating(false);
|
|
@@ -278,7 +389,42 @@ export function CrmDealsView({
|
|
|
278
389
|
<div className="grid gap-6">
|
|
279
390
|
<Sample store={store} concepts={['deals', 'pipelines']} text={text} />
|
|
280
391
|
<Problems state={state} concepts={['deals', 'pipelines']} text={words} />
|
|
281
|
-
{pipeline &&
|
|
392
|
+
{pipeline && (
|
|
393
|
+
<div className="flex flex-wrap items-end justify-between gap-3">
|
|
394
|
+
{state.pipelines.length > 1 ? (
|
|
395
|
+
<div className="grid gap-1.5">
|
|
396
|
+
<Label htmlFor={`${ids}-workflow`}>{words.workflow}</Label>
|
|
397
|
+
<Select value={pipeline.urn} onValueChange={setShown}>
|
|
398
|
+
<SelectTrigger id={`${ids}-workflow`} className="w-56">
|
|
399
|
+
<SelectValue />
|
|
400
|
+
</SelectTrigger>
|
|
401
|
+
<SelectContent>
|
|
402
|
+
{state.pipelines.map((option) => (
|
|
403
|
+
<SelectItem key={option.urn} value={option.urn}>
|
|
404
|
+
{option.name}
|
|
405
|
+
</SelectItem>
|
|
406
|
+
))}
|
|
407
|
+
</SelectContent>
|
|
408
|
+
</Select>
|
|
409
|
+
</div>
|
|
410
|
+
) : (
|
|
411
|
+
<span />
|
|
412
|
+
)}
|
|
413
|
+
<NewButton panel="new-deal" label={words.newDeal} onClick={() => setCreating(true)} />
|
|
414
|
+
</div>
|
|
415
|
+
)}
|
|
416
|
+
{pipeline && (
|
|
417
|
+
<DealFilters
|
|
418
|
+
value={filter}
|
|
419
|
+
onChange={setFilter}
|
|
420
|
+
order={order}
|
|
421
|
+
onOrder={(next) => update({ order: next })}
|
|
422
|
+
team={team}
|
|
423
|
+
companies={customers}
|
|
424
|
+
shown={visible.filter((shownDeal) => pipelineOf(shownDeal, [pipeline])).length}
|
|
425
|
+
text={text}
|
|
426
|
+
/>
|
|
427
|
+
)}
|
|
282
428
|
{pipeline && (
|
|
283
429
|
<SidePanelLayout
|
|
284
430
|
open={creating ? 'new-deal' : deal?.urn}
|
|
@@ -288,9 +434,12 @@ export function CrmDealsView({
|
|
|
288
434
|
panel={
|
|
289
435
|
creating ? (
|
|
290
436
|
<NewDealForm
|
|
437
|
+
// The workflow shown on the board first, so a new deal lands where it's being looked at.
|
|
291
438
|
owner={store.options.owner}
|
|
292
|
-
|
|
293
|
-
companies={
|
|
439
|
+
pipelines={[pipeline, ...state.pipelines.filter((other) => other.urn !== pipeline.urn)]}
|
|
440
|
+
companies={customers}
|
|
441
|
+
people={contacts}
|
|
442
|
+
team={team}
|
|
294
443
|
currencyCode={currencyOf(state)}
|
|
295
444
|
text={text}
|
|
296
445
|
onCreate={async (input) => select(await store.createDeal(input))}
|
|
@@ -299,10 +448,20 @@ export function CrmDealsView({
|
|
|
299
448
|
deal && (
|
|
300
449
|
<DealDetail
|
|
301
450
|
deal={deal}
|
|
302
|
-
|
|
451
|
+
pipelines={state.pipelines}
|
|
303
452
|
names={names}
|
|
304
|
-
|
|
453
|
+
companies={customers}
|
|
454
|
+
people={contacts}
|
|
455
|
+
team={team}
|
|
456
|
+
onMove={(moving, stage) => {
|
|
457
|
+
move(moving, stage);
|
|
458
|
+
// Moved to another workflow: the board follows it.
|
|
459
|
+
const to = state.pipelines.find((option) => option.stages.some((s) => s.urn === stage.urn));
|
|
460
|
+
if (to) setShown(to.urn);
|
|
461
|
+
}}
|
|
305
462
|
onSetOutcome={setOutcome}
|
|
463
|
+
onChange={(changes) => store.updateDeal(deal, changes)}
|
|
464
|
+
contactDetails={contactDetails}
|
|
306
465
|
saving={state.saving.has(deal.urn)}
|
|
307
466
|
text={text}
|
|
308
467
|
/>
|
|
@@ -311,8 +470,9 @@ export function CrmDealsView({
|
|
|
311
470
|
}
|
|
312
471
|
>
|
|
313
472
|
<DealsBoard
|
|
314
|
-
deals={
|
|
473
|
+
deals={visible}
|
|
315
474
|
pipeline={pipeline}
|
|
475
|
+
order={order}
|
|
316
476
|
names={names}
|
|
317
477
|
onOpen={select}
|
|
318
478
|
onMove={move}
|
|
@@ -327,19 +487,28 @@ export function CrmDealsView({
|
|
|
327
487
|
}
|
|
328
488
|
|
|
329
489
|
/**
|
|
330
|
-
* Sales for a period (
|
|
331
|
-
* before, payments over time, outstanding by age, payments by method,
|
|
332
|
-
* the right with its payments, where another can be recorded.
|
|
490
|
+
* Sales for a period (`range`, else the one picked, the last 30 days to start with): invoiced, received, outstanding,
|
|
491
|
+
* sales tax, costs and drafts against the period before, payments over time, outstanding by age, payments by method,
|
|
492
|
+
* and the invoices; the opened invoice shows to the right with its payments, where another can be recorded.
|
|
333
493
|
*/
|
|
334
494
|
export function CrmSalesView({
|
|
335
495
|
store,
|
|
336
496
|
names,
|
|
337
497
|
text,
|
|
338
498
|
companies,
|
|
499
|
+
people = [],
|
|
500
|
+
ownCompanies,
|
|
339
501
|
range,
|
|
340
502
|
selected,
|
|
341
503
|
onSelect,
|
|
342
504
|
}: ViewProps & {
|
|
505
|
+
/**
|
|
506
|
+
* The companies the business counts as itself (the owner and its child companies): invoices they pay are costs,
|
|
507
|
+
* apart from the sales figures. The owner alone when not given.
|
|
508
|
+
*/
|
|
509
|
+
ownCompanies?: readonly string[] | undefined;
|
|
510
|
+
/** People an invoice's remitter or recipient can be. */
|
|
511
|
+
people?: readonly CrmPersonChoice[] | undefined;
|
|
343
512
|
range?: DateRange | undefined;
|
|
344
513
|
selected?: string | undefined;
|
|
345
514
|
onSelect?: ((invoice: Invoice | undefined) => void) | undefined;
|
|
@@ -347,12 +516,20 @@ export function CrmSalesView({
|
|
|
347
516
|
const state = useCrmStore(store);
|
|
348
517
|
names = useCrmNames(names, state);
|
|
349
518
|
const words = withText(text);
|
|
350
|
-
const
|
|
351
|
-
const
|
|
519
|
+
const [picked, setPicked] = useState<ReportingPeriod>('last30');
|
|
520
|
+
const period = useMemo(() => range ?? periodRange(picked), [range, picked]);
|
|
521
|
+
const ownKey = ownCompanies?.join(',');
|
|
522
|
+
const ownSet = useMemo(() => (ownKey === undefined ? undefined : new Set(ownKey.split(','))), [ownKey]);
|
|
523
|
+
const summary = useMemo(() => selectSales(state, period, new Date(), ownSet), [state, period, ownSet]);
|
|
352
524
|
const [own, setOwn] = useState<string>();
|
|
353
525
|
const urn = onSelect ? selected : (own ?? selected);
|
|
354
526
|
const invoice = summary.invoices.find((candidate) => candidate.urn === urn);
|
|
355
527
|
const [creating, setCreating] = useState(false);
|
|
528
|
+
// Either party of an invoice: the host's companies, with any the store made since.
|
|
529
|
+
const parties = withMade(
|
|
530
|
+
companies ?? [],
|
|
531
|
+
state.companies.map((made) => ({ urn: made.urn, name: made.name ?? words.unnamed })),
|
|
532
|
+
);
|
|
356
533
|
const select = (next: Invoice | undefined) => {
|
|
357
534
|
setCreating(false);
|
|
358
535
|
if (onSelect) onSelect(next);
|
|
@@ -364,7 +541,8 @@ export function CrmSalesView({
|
|
|
364
541
|
<div className="grid gap-6">
|
|
365
542
|
<Sample store={store} concepts={concepts} text={text} />
|
|
366
543
|
<Problems state={state} concepts={concepts} text={words} />
|
|
367
|
-
<
|
|
544
|
+
{!range && <PeriodSelect value={picked} onChange={setPicked} text={text} />}
|
|
545
|
+
<SalesMetricCards summary={summary} text={range ? text : periodText(picked, text)} />
|
|
368
546
|
<RevenueChart days={summary.byDay} currencyCode={summary.currencyCode} text={text} />
|
|
369
547
|
<div className="grid items-start gap-6 lg:grid-cols-2">
|
|
370
548
|
<OutstandingByAgeCard summary={summary} text={text} />
|
|
@@ -380,7 +558,8 @@ export function CrmSalesView({
|
|
|
380
558
|
creating ? (
|
|
381
559
|
<NewInvoiceForm
|
|
382
560
|
owner={store.options.owner}
|
|
383
|
-
companies={
|
|
561
|
+
companies={parties}
|
|
562
|
+
people={withMade(people, state.people)}
|
|
384
563
|
deals={state.deals}
|
|
385
564
|
currencyCode={currencyOf(state)}
|
|
386
565
|
text={text}
|
|
@@ -392,6 +571,10 @@ export function CrmSalesView({
|
|
|
392
571
|
invoice={invoice}
|
|
393
572
|
payments={summary.payments.filter((payment) => payment.paymentInvoice.urn === invoice.urn)}
|
|
394
573
|
names={names}
|
|
574
|
+
companies={parties}
|
|
575
|
+
people={withMade(people, state.people)}
|
|
576
|
+
onChange={(changes, options) => store.updateInvoice(invoice, changes, options)}
|
|
577
|
+
saving={state.saving.has(invoice.urn)}
|
|
395
578
|
onRecord={(input) => store.recordPayment(input)}
|
|
396
579
|
text={text}
|
|
397
580
|
/>
|
|
@@ -399,7 +582,14 @@ export function CrmSalesView({
|
|
|
399
582
|
)
|
|
400
583
|
}
|
|
401
584
|
>
|
|
402
|
-
<InvoicesTable
|
|
585
|
+
<InvoicesTable
|
|
586
|
+
invoices={summary.invoices}
|
|
587
|
+
names={names}
|
|
588
|
+
own={ownSet}
|
|
589
|
+
onOpen={select}
|
|
590
|
+
selected={invoice?.urn}
|
|
591
|
+
text={text}
|
|
592
|
+
/>
|
|
403
593
|
</SidePanelLayout>
|
|
404
594
|
</div>
|
|
405
595
|
);
|