@arcforgelabs/flow 2026.10.32-beta.1 → 2026.10.33

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.
Files changed (59) hide show
  1. package/README.md +3 -1
  2. package/npm-shrinkwrap.json +2 -2
  3. package/package.json +2 -1
  4. package/server/api/invoicing.js +69 -26
  5. package/server/app.js +4 -0
  6. package/server/core/automations.js +17 -3
  7. package/server/core/invoices.js +2 -2
  8. package/server/core/invoicing-rules.js +64 -5
  9. package/server/core/job-board.js +15 -4
  10. package/server/core/ledger.js +245 -8
  11. package/server/core/pipeline.js +30 -22
  12. package/server/core/read-before-act.js +96 -0
  13. package/server/core/reconcile.js +350 -0
  14. package/server/core/references.js +99 -0
  15. package/server/core/sending.js +52 -8
  16. package/server/db/migrations/0016_invoice_reference.sql +1 -0
  17. package/server/db/migrations/0017_sync_basis.sql +1 -0
  18. package/server/db/migrations/meta/0016_snapshot.json +3861 -0
  19. package/server/db/migrations/meta/0017_snapshot.json +3867 -0
  20. package/server/db/migrations/meta/_journal.json +14 -0
  21. package/server/db/schema.js +5 -0
  22. package/server/index.js +5 -0
  23. package/server/integrations/port.js +4 -0
  24. package/server/integrations/seazona/act.js +48 -0
  25. package/server/integrations/seazona/feed.js +77 -50
  26. package/server/integrations/seazona/governor.js +28 -2
  27. package/server/integrations/seazona/index.js +69 -3
  28. package/server/integrations/seazona/lanes.js +82 -9
  29. package/server/integrations/seazona/ledger.js +14 -9
  30. package/server/integrations/seazona/migrations/0005_record_reread.sql +1 -0
  31. package/server/integrations/seazona/migrations/0006_sweep_findings.sql +11 -0
  32. package/server/integrations/seazona/migrations/meta/0005_snapshot.json +1329 -0
  33. package/server/integrations/seazona/migrations/meta/0006_snapshot.json +1403 -0
  34. package/server/integrations/seazona/migrations/meta/_journal.json +14 -0
  35. package/server/integrations/seazona/patients.js +30 -0
  36. package/server/integrations/seazona/reconcile.js +40 -0
  37. package/server/integrations/seazona/scheduler.js +32 -4
  38. package/server/integrations/seazona/schema.js +21 -0
  39. package/server/integrations/seazona/settings.js +12 -0
  40. package/server/integrations/seazona/store.js +24 -2
  41. package/server/integrations/seazona/sweep.js +574 -0
  42. package/server/integrations/seazona/tables.js +1 -0
  43. package/server/integrations/xero/client.js +12 -0
  44. package/server/integrations/xero/import.js +9 -3
  45. package/server/integrations/xero/index.js +7 -1
  46. package/server/integrations/xero/migrations/0007_xero_webhooks.sql +30 -0
  47. package/server/integrations/xero/migrations/meta/_journal.json +7 -0
  48. package/server/integrations/xero/mirror.js +16 -8
  49. package/server/integrations/xero/routes.js +20 -0
  50. package/server/integrations/xero/settings.js +3 -0
  51. package/server/integrations/xero/webhooks.js +346 -0
  52. package/server/ui/app.js +15 -7
  53. package/server/ui/flow.css +56 -7
  54. package/server/ui/invoice-notes.js +26 -0
  55. package/server/ui/invoicing-settings.js +3 -1
  56. package/server/ui/invoicing.js +231 -68
  57. package/server/ui/job-board.js +2 -1
  58. package/server/ui/jobs.js +19 -1
  59. package/server/ui/routes.js +1 -1
@@ -0,0 +1,350 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { and, desc, eq, gte, inArray, lte, sql } from 'drizzle-orm';
3
+ import { backgroundJobs, billingObligations, invoices, reviewItems } from '../db/schema.js';
4
+ import { HISTORY_ACTOR } from './history.js';
5
+ import { soleOrganisation } from './pipeline.js';
6
+ import { deadHoldSince, deadSince, enqueueJob, runNextJob } from './queue.js';
7
+ import { openReview, resolveReviewFor } from './review.js';
8
+
9
+ // The daily reconcile (issue 129, docs/RECONCILE.md). Flow checks each record
10
+ // as it moves; once a day it also checks the whole. For each of the last
11
+ // `days` days it counts and totals the jobs in three places, from Flow's own
12
+ // tables only (no call to any other system):
13
+ //
14
+ // 1. the work system's copy (each adapter's `billableJobs`);
15
+ // 2. the billing obligation ledger;
16
+ // 3. the Flow invoices the ledger made, and the accounting system's copy of
17
+ // the invoices linked to them (`findInvoices`).
18
+ //
19
+ // A day where they do not agree opens one review item (`reconcile.day`) with
20
+ // the counts, the totals and up to 20 record ids for each difference. A
21
+ // difference is an alert only: the reconcile never changes a record. The item
22
+ // closes by itself when a later run agrees.
23
+ //
24
+ // A difference first seen is checked again an hour later, and opens an item
25
+ // only when it is still there: a job the pipeline has not reached yet is not
26
+ // a difference.
27
+
28
+ export const RECONCILE_KIND = 'reconcile.daily';
29
+ export const RECONCILE_REVIEW_KIND = 'reconcile.day';
30
+ export const ACTOR = 'system:reconcile';
31
+ export const RECONCILE_DAYS = 60;
32
+ export const EVIDENCE_IDS = 20;
33
+ const RUN_EVERY_MS = 24 * 60 * 60 * 1000;
34
+ const RECHECK_MS = 60 * 60 * 1000;
35
+ const TICK_MS = 10 * 60 * 1000;
36
+ const LEASE_MS = 15 * 60 * 1000;
37
+ const FIND_BATCH = 400;
38
+ // Accounting states where the ledger holds a link to an invoice that exists.
39
+ const LINKED = ['drafted', 'authorised', 'part_paid', 'paid'];
40
+
41
+ const isoDay = (ms) => new Date(ms).toISOString().slice(0, 10);
42
+ const withGst = (cents) => Math.round(cents * 1.1);
43
+
44
+ // A stable id for one day's review item, so a repeat finding refreshes it.
45
+ export function dayReviewId(organisationId, day) {
46
+ const hex = createHash('sha256').update(`${RECONCILE_REVIEW_KIND}:${organisationId}:${day}`).digest('hex');
47
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-5${hex.slice(13, 16)}-8${hex.slice(17, 20)}-${hex.slice(20, 32)}`;
48
+ }
49
+
50
+ function emptyDay(day) {
51
+ return {
52
+ day,
53
+ work: { count: 0, subtotalCents: 0, unpriced: 0 },
54
+ ledger: { count: 0, subtotalCents: 0, totalCents: 0 },
55
+ flowInvoices: { count: 0, totalCents: 0 },
56
+ accounting: { linked: 0, found: 0, totalCents: 0 },
57
+ differences: {
58
+ missingInLedger: [], missingInWork: [], missingInAccounting: [], accountingTotalDiffers: [],
59
+ },
60
+ };
61
+ }
62
+
63
+ const differenceCount = (entry) => Object.values(entry.differences).reduce((n, list) => n + list.length, 0);
64
+
65
+ // The evidence a review item keeps: the counts, the totals, and up to
66
+ // EVIDENCE_IDS ids for each difference with the full count beside them.
67
+ function evidenceFor(entry) {
68
+ const differences = {};
69
+ for (const [key, list] of Object.entries(entry.differences)) {
70
+ if (list.length) differences[key] = { count: list.length, ids: list.slice(0, EVIDENCE_IDS).map((d) => d.id) };
71
+ }
72
+ return { ...entry, differences };
73
+ }
74
+
75
+ const moneyAtRisk = (entry) => Object.values(entry.differences).flat()
76
+ .reduce((cents, d) => cents + Math.abs(d.cents ?? 0), 0);
77
+
78
+ // One pass over Flow's own tables. Answers one entry per day, oldest first.
79
+ export async function reconcileDays(db, organisationId, { integrations, from, to }) {
80
+ const byDay = new Map();
81
+ const dayOf = (day) => {
82
+ if (!byDay.has(day)) byDay.set(day, emptyDay(day));
83
+ return byDay.get(day);
84
+ };
85
+
86
+ // 1. The work systems' copies.
87
+ const workAdapters = integrations?.with('billableJobs') ?? [];
88
+ const work = new Map(); // `${system}:${ref}` -> job
89
+ for (const adapter of workAdapters) {
90
+ const { jobs } = await adapter.capabilities.billableJobs({ organisationId, from, to });
91
+ for (const job of jobs) {
92
+ if (!job.day || job.day < from || job.day > to) continue;
93
+ work.set(`${adapter.id}:${job.ref}`, { ...job, system: adapter.id });
94
+ const entry = dayOf(job.day);
95
+ entry.work.count += 1;
96
+ if (Number.isInteger(job.subtotalCents)) entry.work.subtotalCents += job.subtotalCents;
97
+ else entry.work.unpriced += 1;
98
+ }
99
+ }
100
+
101
+ // 2. The ledger. A job only a history import reported is left out: the
102
+ // import keeps its own reconciliation record (docs/SEAZONA.md).
103
+ const obligations = await db.select({
104
+ id: billingObligations.id,
105
+ sourceSystem: billingObligations.sourceSystem,
106
+ sourceJobRef: billingObligations.sourceJobRef,
107
+ jobDate: billingObligations.jobDate,
108
+ subtotalCents: billingObligations.subtotalCents,
109
+ totalCents: billingObligations.totalCents,
110
+ accountingState: billingObligations.accountingState,
111
+ accountingRef: billingObligations.accountingRef,
112
+ invoiceId: billingObligations.invoiceId,
113
+ invoiceStatus: invoices.status,
114
+ invoiceTotalCents: invoices.totalCents,
115
+ })
116
+ .from(billingObligations)
117
+ .leftJoin(invoices, and(eq(invoices.organisationId, billingObligations.organisationId), eq(invoices.id, billingObligations.invoiceId)))
118
+ .where(and(
119
+ eq(billingObligations.organisationId, organisationId),
120
+ gte(billingObligations.jobDate, from),
121
+ lte(billingObligations.jobDate, to),
122
+ sql`exists (select 1 from obligation_events e
123
+ where e.organisation_id = ${billingObligations.organisationId} and e.obligation_id = ${billingObligations.id}
124
+ and e.kind in ('observed', 'source_changed') and e.actor <> ${HISTORY_ACTOR})`,
125
+ ));
126
+
127
+ const reported = new Set(workAdapters.map((a) => a.id));
128
+ const inLedger = new Set();
129
+ for (const o of obligations) {
130
+ const entry = dayOf(o.jobDate);
131
+ entry.ledger.count += 1;
132
+ entry.ledger.subtotalCents += o.subtotalCents;
133
+ entry.ledger.totalCents += o.totalCents;
134
+ if (o.invoiceId) {
135
+ entry.flowInvoices.count += 1;
136
+ entry.flowInvoices.totalCents += o.invoiceTotalCents ?? 0;
137
+ }
138
+ // Only a work system that is on can say a job is gone from its copy.
139
+ if (reported.has(o.sourceSystem)) {
140
+ const key = `${o.sourceSystem}:${o.sourceJobRef}`;
141
+ inLedger.add(key);
142
+ if (!work.has(key)) entry.differences.missingInWork.push({ id: o.id, cents: o.totalCents });
143
+ }
144
+ }
145
+ for (const [key, job] of work) {
146
+ if (!inLedger.has(key)) dayOf(job.day).differences.missingInLedger.push({ id: job.ref, cents: job.subtotalCents ?? 0 });
147
+ }
148
+
149
+ // 3. The accounting system's copy of each linked invoice.
150
+ const [accounting] = integrations?.with('findInvoices') ?? [];
151
+ const linked = obligations.filter((o) => o.accountingRef && LINKED.includes(o.accountingState));
152
+ if (accounting) {
153
+ const copies = new Map();
154
+ for (let i = 0; i < linked.length; i += FIND_BATCH) {
155
+ const invoiceIds = linked.slice(i, i + FIND_BATCH).map((o) => o.accountingRef);
156
+ const { invoices: found } = await accounting.capabilities.findInvoices({ organisationId, invoiceIds });
157
+ for (const invoice of found) copies.set(String(invoice.id), invoice);
158
+ }
159
+ for (const o of linked) {
160
+ const entry = dayOf(o.jobDate);
161
+ entry.accounting.linked += 1;
162
+ const copy = copies.get(String(o.accountingRef));
163
+ if (!copy) {
164
+ entry.differences.missingInAccounting.push({ id: o.id, cents: o.totalCents });
165
+ continue;
166
+ }
167
+ entry.accounting.found += 1;
168
+ entry.accounting.totalCents += copy.totalCents ?? 0;
169
+ // An invoice Flow issued must carry Flow's total. One Flow only matched
170
+ // may carry the job's total with or without GST, as the matcher allows.
171
+ const expected = o.invoiceId && o.invoiceStatus === 'issued'
172
+ ? [o.invoiceTotalCents]
173
+ : [o.totalCents, o.subtotalCents, withGst(o.subtotalCents)];
174
+ if (copy.totalCents !== null && !expected.includes(copy.totalCents)) {
175
+ entry.differences.accountingTotalDiffers.push({ id: o.id, cents: copy.totalCents - expected[0] });
176
+ }
177
+ }
178
+ }
179
+
180
+ return {
181
+ systems: { work: workAdapters.map((a) => a.id), accounting: accounting?.id ?? null },
182
+ days: [...byDay.values()].sort((a, b) => a.day.localeCompare(b.day)),
183
+ };
184
+ }
185
+
186
+ async function openDayItems(db, organisationId) {
187
+ const rows = await db.select({ flowId: reviewItems.flowId, evidence: reviewItems.evidence }).from(reviewItems).where(and(
188
+ eq(reviewItems.organisationId, organisationId),
189
+ eq(reviewItems.kind, RECONCILE_REVIEW_KIND),
190
+ eq(reviewItems.state, 'open'),
191
+ ));
192
+ return new Map(rows.map((r) => [r.evidence?.day, r.flowId]).filter(([day]) => day));
193
+ }
194
+
195
+ // One run: reconcile, open or refresh an item for each day that still
196
+ // differs, and close the item of each day that now agrees. `unconfirmed` are
197
+ // the days the last run found different for the first time. Answers the
198
+ // summary the job keeps (GET /health shows it).
199
+ export async function runReconcile(db, organisationId, {
200
+ integrations, now = Date.now(), days = RECONCILE_DAYS, unconfirmed = [],
201
+ }) {
202
+ const to = isoDay(now);
203
+ const open = await openDayItems(db, organisationId);
204
+ // An open item older than the window is checked too, so it can close.
205
+ const oldest = [...open.keys()].reduce((min, day) => (day < min ? day : min), isoDay(now - (days - 1) * 86_400_000));
206
+ const { systems, days: entries } = await reconcileDays(db, organisationId, { integrations, from: oldest, to });
207
+ const differing = entries.filter((e) => differenceCount(e) > 0);
208
+ const recheck = new Set(unconfirmed);
209
+ const firstSeen = [];
210
+ let opened = 0;
211
+ for (const entry of differing) {
212
+ if (!open.has(entry.day) && !recheck.has(entry.day)) {
213
+ firstSeen.push(entry.day);
214
+ continue;
215
+ }
216
+ await openReview(db, organisationId, {
217
+ kind: RECONCILE_REVIEW_KIND,
218
+ entity: 'day',
219
+ flowId: dayReviewId(organisationId, entry.day),
220
+ systems: [...systems.work, ...(systems.accounting ? [systems.accounting] : [])],
221
+ evidence: evidenceFor(entry),
222
+ moneyAtRiskCents: moneyAtRisk(entry),
223
+ });
224
+ opened += 1;
225
+ }
226
+ const differs = new Set(differing.map((e) => e.day));
227
+ let closed = 0;
228
+ for (const [day, flowId] of open) {
229
+ if (differs.has(day)) continue;
230
+ await resolveReviewFor(db, organisationId, {
231
+ kind: RECONCILE_REVIEW_KIND, entity: 'day', flowId, resolution: 'a later run agreed', actor: ACTOR,
232
+ });
233
+ closed += 1;
234
+ }
235
+ return {
236
+ from: oldest,
237
+ to,
238
+ systems,
239
+ daysChecked: entries.length,
240
+ daysDifferent: differing.length,
241
+ openItems: (await openDayItems(db, organisationId)).size,
242
+ opened,
243
+ closed,
244
+ unconfirmed: firstSeen,
245
+ };
246
+ }
247
+
248
+ async function lastRun(db, organisationId) {
249
+ const [row] = await db.select().from(backgroundJobs).where(and(
250
+ eq(backgroundJobs.organisationId, organisationId),
251
+ eq(backgroundJobs.kind, RECONCILE_KIND),
252
+ inArray(backgroundJobs.state, ['succeeded', 'failed', 'dead']),
253
+ )).orderBy(desc(backgroundJobs.updatedAt)).limit(1);
254
+ return row ?? null;
255
+ }
256
+
257
+ // The last run and its result, for GET /health: counts only, no record ids.
258
+ export async function reconcileHealth(db, { now = Date.now() } = {}) {
259
+ const organisationId = await soleOrganisation(db);
260
+ if (!organisationId) return null;
261
+ const row = await lastRun(db, organisationId);
262
+ if (!row) return { lastRunAt: null };
263
+ const result = row.state === 'succeeded' ? row.result ?? {} : {};
264
+ return {
265
+ lastRunAt: row.updatedAt.toISOString(),
266
+ state: row.state,
267
+ ...(row.state === 'succeeded' ? {
268
+ agrees: result.daysDifferent === 0,
269
+ daysChecked: result.daysChecked ?? null,
270
+ daysDifferent: result.daysDifferent ?? null,
271
+ openItems: result.openItems ?? null,
272
+ late: now - row.updatedAt.getTime() > RUN_EVERY_MS + 2 * TICK_MS + LEASE_MS,
273
+ } : { error: row.lastError }),
274
+ };
275
+ }
276
+
277
+ // Is a run due? Once a day, and an hour after a run that found a new difference.
278
+ export function reconcileDue(last, nowMs) {
279
+ if (!last) return true;
280
+ const age = nowMs - last.updatedAt.getTime();
281
+ if (last.state !== 'succeeded') return age >= RECHECK_MS;
282
+ if ((last.result?.unconfirmed ?? []).length) return age >= RECHECK_MS;
283
+ return age >= RUN_EVERY_MS;
284
+ }
285
+
286
+ // The worker: every ten minutes it asks whether a run is due, and runs it on
287
+ // the job queue, so two Flow processes never run it at once. It runs only
288
+ // while an integration that feeds it is on.
289
+ export function createReconcileWorker({
290
+ db, integrations, log = console, timers = { setTimeout, clearTimeout }, now = Date.now, everyMs = TICK_MS, firstDelayMs = 60_000,
291
+ }) {
292
+ const workerId = `reconcile-${randomUUID()}`;
293
+ const startedAtMs = now();
294
+ let timer = null;
295
+ let running = null;
296
+ let stopped = true;
297
+
298
+ function schedule(delayMs) {
299
+ if (stopped) return;
300
+ timer = timers.setTimeout(tick, delayMs);
301
+ timer?.unref?.();
302
+ }
303
+
304
+ async function tick() {
305
+ timer = null;
306
+ if (stopped) return;
307
+ running = (async () => {
308
+ try {
309
+ if (!integrations?.with('billableJobs').length && !integrations?.with('findInvoices').length) return;
310
+ const organisationId = await soleOrganisation(db);
311
+ if (!organisationId) return;
312
+ const last = await lastRun(db, organisationId);
313
+ if (!reconcileDue(last, now())) return;
314
+ if (await deadSince(db, organisationId, { kind: RECONCILE_KIND, dedupeKey: 'all', since: deadHoldSince(startedAtMs, now()) })) return;
315
+ const unconfirmed = last?.state === 'succeeded' ? last.result?.unconfirmed ?? [] : [];
316
+ await enqueueJob(db, organisationId, { kind: RECONCILE_KIND, dedupeKey: 'all', maxAttempts: 3 });
317
+ const ran = await runNextJob(db, {
318
+ workerId,
319
+ leaseMs: LEASE_MS,
320
+ handlers: { [RECONCILE_KIND]: () => runReconcile(db, organisationId, { integrations, now: now(), unconfirmed }) },
321
+ });
322
+ if (!ran) return;
323
+ if (ran.outcome === 'dead') log.error?.('[reconcile] the daily reconcile gave up after its last attempt');
324
+ else if (ran.outcome === 'succeeded') {
325
+ const { daysChecked, daysDifferent, opened, closed } = ran.job.result;
326
+ (daysDifferent ? log.warn : log.info)?.(`[reconcile] ${daysChecked} days checked, ${daysDifferent} differ; ${opened} items open or refreshed, ${closed} closed`);
327
+ } else log.warn?.(`[reconcile] ${ran.outcome}: ${ran.job.lastError}`);
328
+ } catch (error) {
329
+ log.error?.(`[reconcile] ${error?.code ?? 'error'}: ${error?.message ?? error}`);
330
+ }
331
+ })();
332
+ await running;
333
+ running = null;
334
+ schedule(everyMs);
335
+ }
336
+
337
+ return {
338
+ start() {
339
+ stopped = false;
340
+ schedule(firstDelayMs);
341
+ },
342
+ async stop() {
343
+ stopped = true;
344
+ if (timer) timers.clearTimeout(timer);
345
+ timer = null;
346
+ await running;
347
+ },
348
+ tick,
349
+ };
350
+ }
@@ -0,0 +1,99 @@
1
+ import { and, eq, inArray, isNotNull } from 'drizzle-orm';
2
+ import { billingObligations, invoices } from '../db/schema.js';
3
+
4
+ // The invoice Reference (docs/INVOICING_RULES.md, "Reference"). The lab writes it as a
5
+ // rule in Invoice rules, from tokens: {practitioner} is the job's
6
+ // practitioner, {patient} the patient name the work system gives (only when
7
+ // its integration keeps patient names). Flow
8
+ // never puts a work system's ids in the Reference: the customer reads it.
9
+
10
+ export const REFERENCE_TOKENS = Object.freeze(['practitioner', 'patient']);
11
+ export const DEFAULT_REFERENCE = '{practitioner} - {patient}';
12
+ const TOKEN = /\{([a-z]+)\}/g;
13
+ const MAX = 255;
14
+
15
+ // The template's parts: text and tokens, in order.
16
+ function parts(template) {
17
+ const out = [];
18
+ let at = 0;
19
+ for (const match of String(template ?? '').matchAll(TOKEN)) {
20
+ out.push({ text: template.slice(at, match.index) }, { token: match[1] });
21
+ at = match.index + match[0].length;
22
+ }
23
+ out.push({ text: String(template ?? '').slice(at) });
24
+ return out;
25
+ }
26
+
27
+ // Unknown tokens are refused, so a typing error shows when it is saved.
28
+ export function checkReferenceTemplate(template) {
29
+ const unknown = parts(template).filter((p) => p.token && !REFERENCE_TOKENS.includes(p.token)).map((p) => `{${p.token}}`);
30
+ return unknown.length ? `unknown token ${unknown[0]}: use ${REFERENCE_TOKENS.map((t) => `{${t}}`).join(', ')}` : null;
31
+ }
32
+
33
+ // The Reference for some fields. Text between two tokens is a spacer: it
34
+ // shows only when the tokens on both sides have a value, so
35
+ // "{practitioner} - {patient}" with no patient is the practitioner alone.
36
+ // Text before the first token and after the last always shows. Null when no
37
+ // token has a value.
38
+ export function renderReference(template, fields) {
39
+ const list = parts(template);
40
+ let out = list[0].text;
41
+ let filled = false;
42
+ for (let i = 1; i < list.length; i += 2) {
43
+ const value = String(fields[list[i].token] ?? '').trim().replace(/\s+/g, ' ');
44
+ if (!value) continue;
45
+ if (filled) out += list[i - 1].text;
46
+ out += value;
47
+ filled = true;
48
+ }
49
+ if (!filled) return null;
50
+ return `${out}${list.at(-1).text}`.trim().slice(0, MAX) || null;
51
+ }
52
+
53
+ // The fields of one job. `work` is the work system adapter; it gives the
54
+ // patient name when it keeps one. With `read`, it may ask the work system for
55
+ // a name it has not read yet.
56
+ export async function jobReferenceFields(db, organisationId, obligation, { work = null, read = false } = {}) {
57
+ let found = {};
58
+ if (work?.id === obligation.sourceSystem && typeof work.capabilities.jobReferenceFields === 'function') {
59
+ // A work system that cannot answer gives no name; the rule still works.
60
+ found = (await work.capabilities.jobReferenceFields({ organisationId, sourceJobRef: obligation.sourceJobRef, read }).catch(() => null)) ?? {};
61
+ }
62
+ return { fields: { practitioner: obligation.practitioner, patient: found.patient ?? null }, asked: Boolean(found.asked) };
63
+ }
64
+
65
+ // The Reference of a new draft for a job. `template` is the organisation's
66
+ // rule (invoicing settings, referenceTemplate).
67
+ export async function jobReference(db, organisationId, obligation, { template, ...options }) {
68
+ return renderReference(template, (await jobReferenceFields(db, organisationId, obligation, options)).fields);
69
+ }
70
+
71
+ // Drafts Flow made from a job keep the Reference the rule gives, as the rule
72
+ // or the job's fields change. A Reference a person typed is never changed:
73
+ // only one that is empty, a work system id (as earlier releases wrote), or
74
+ // the rule's own text without a field it now has, is updated. The status of
75
+ // the draft does not change. `reads` caps how many jobs may ask the work
76
+ // system for a name in one pass. Answers { updated }.
77
+ export async function refreshReferences(db, organisationId, { template, work = null, reads = 10 }) {
78
+ const rows = await db.select({ obligation: billingObligations, invoiceId: invoices.id, reference: invoices.reference })
79
+ .from(billingObligations)
80
+ .innerJoin(invoices, eq(invoices.id, billingObligations.invoiceId))
81
+ .where(and(eq(billingObligations.organisationId, organisationId), isNotNull(billingObligations.invoiceId), eq(invoices.status, 'draft')));
82
+ let updated = 0;
83
+ let asked = 0;
84
+ for (const { obligation, invoiceId, reference } of rows) {
85
+ const found = await jobReferenceFields(db, organisationId, obligation, { work, read: asked < reads });
86
+ const { fields } = found;
87
+ if (found.asked) asked += 1;
88
+ const wanted = renderReference(template, fields);
89
+ if ((wanted ?? null) === (reference ?? null)) continue;
90
+ const ours = [null, '', obligation.caseRef, obligation.sourceJobRef,
91
+ renderReference(template, { ...fields, patient: null }), renderReference(DEFAULT_REFERENCE, { ...fields, patient: null }),
92
+ renderReference(DEFAULT_REFERENCE, fields)];
93
+ if (!ours.includes(reference)) continue;
94
+ await db.update(invoices).set({ reference: wanted })
95
+ .where(and(eq(invoices.organisationId, organisationId), inArray(invoices.id, [invoiceId]), eq(invoices.status, 'draft')));
96
+ updated += 1;
97
+ }
98
+ return { updated };
99
+ }
@@ -1,9 +1,10 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import { and, asc, desc, eq, inArray, sql } from 'drizzle-orm';
3
- import { invoiceLines, invoices } from '../db/schema.js';
3
+ import { billingObligations, invoiceLines, invoices } from '../db/schema.js';
4
4
  import { FlowError } from './errors.js';
5
5
  import { issueInvoice } from './invoices.js';
6
6
  import { soleOrganisation } from './pipeline.js';
7
+ import { needsRead, readAccountingFirst, readBeforeAct, READS_PER_PASS } from './read-before-act.js';
7
8
  import { deadHoldSince, deadSince, enqueueJob, runNextJob } from './queue.js';
8
9
 
9
10
  // Drafts, Problem, Ready and Send all ready (docs/INVOICE_SENDING.md).
@@ -13,8 +14,8 @@ import { deadHoldSince, deadSince, enqueueJob, runNextJob } from './queue.js';
13
14
  // charge, an account) on every line. When an accounting
14
15
  // system is connected, its own checks are added (for Xero: the customer
15
16
  // matches one Xero contact, every item is in Xero). A draft with a problem is
16
- // in Problem. A person marks a draft without one ready; any later change to
17
- // the draft sends it back to be checked again.
17
+ // in Problem. A person marks a draft without one ready. A saved change keeps
18
+ // it ready; only Move to Drafts (markNotReady) takes it back.
18
19
  //
19
20
  // Send all ready issues every ready draft (each gets its number) and, when an
20
21
  // accounting system is connected, queues it to go there. The send worker
@@ -60,6 +61,15 @@ async function checkDrafts(db, organisationId, drafts, accounting) {
60
61
  const found = draftProblems(draft, linesOf.get(draft.id) ?? []);
61
62
  if (found.length) problems.set(draft.id, found);
62
63
  }
64
+ // A draft whose job the ledger sent to review (the job changed after the
65
+ // draft was made, or the work system deleted it) waits for a person.
66
+ const reviewed = await db.select({ invoiceId: billingObligations.invoiceId, reason: billingObligations.holdReason })
67
+ .from(billingObligations)
68
+ .where(and(eq(billingObligations.organisationId, organisationId), inArray(billingObligations.invoiceId, ids), eq(billingObligations.holdState, 'review')));
69
+ for (const { invoiceId, reason } of reviewed) {
70
+ const found = problem('job_review', `The job needs a look before this invoice goes: ${reason ?? 'it was sent to review'}.`);
71
+ problems.set(invoiceId, [...(problems.get(invoiceId) ?? []), found]);
72
+ }
63
73
  if (accounting) {
64
74
  const outside = await accounting.capabilities.checkDrafts({ organisationId, invoiceIds: ids });
65
75
  for (const [id, found] of outside ?? []) {
@@ -146,12 +156,46 @@ async function assertCanSend(accounting, organisationId) {
146
156
  }
147
157
  }
148
158
 
159
+ // Read before act (issue #141): the jobs behind these drafts whose work
160
+ // system report is old are read again first. Answers the drafts that may go
161
+ // now; a draft whose job went to review, or could not be read, waits.
162
+ async function readJobsFirst(db, organisationId, invoiceIds, { work, now, personId }) {
163
+ if (!invoiceIds.length || typeof work?.capabilities?.readWorkItem !== 'function') return { go: invoiceIds, waiting: [] };
164
+ const jobs = await db.select().from(billingObligations)
165
+ .where(and(eq(billingObligations.organisationId, organisationId), inArray(billingObligations.invoiceId, invoiceIds)));
166
+ const old = jobs.filter((job) => needsRead(job, { work, now }));
167
+ const outcomes = await readBeforeAct(db, organisationId, old.slice(0, READS_PER_PASS), { work, now, actor: `person:${personId}` });
168
+ const held = new Set(old.slice(READS_PER_PASS).map((job) => job.invoiceId));
169
+ for (const { outcome, obligation } of outcomes.values()) {
170
+ if (outcome === 'waiting' || outcome === 'review') held.add(obligation.invoiceId);
171
+ }
172
+ return { go: invoiceIds.filter((id) => !held.has(id)), waiting: invoiceIds.filter((id) => held.has(id)) };
173
+ }
174
+
149
175
  // Send all ready: issues each ready draft, then queues it for the accounting
150
- // system. Answers { issued, queued, remaining }.
151
- export async function sendAllReady(db, organisationId, { accounting = null, personId, issueDate }) {
176
+ // system. With `invoiceIds`, only those drafts, and each must be ready (the
177
+ // Send button on one invoice). `work` is the work integration, for the read
178
+ // before act. Answers { issued, queued, remaining, waiting }.
179
+ export async function sendAllReady(db, organisationId, { accounting = null, work = null, personId, issueDate, invoiceIds = null, now = new Date() }) {
152
180
  await assertCanSend(accounting, organisationId);
153
- const { ready } = await invoiceViews(db, organisationId, { accounting });
154
- const batch = ready.slice(0, SEND_BATCH);
181
+ // Issue #45: the accounting copy reads its latest changes first, so the
182
+ // draft checks below see an invoice someone made there meanwhile.
183
+ let views = await invoiceViews(db, organisationId, { accounting });
184
+ const accountingRead = views.ready.length ? await readAccountingFirst(db, organisationId, { accounting, now }) : { outcome: 'fresh' };
185
+ if (accountingRead.changed) views = await invoiceViews(db, organisationId, { accounting });
186
+ let { ready } = views;
187
+ if (invoiceIds) {
188
+ const wanted = new Set(invoiceIds);
189
+ ready = ready.filter((id) => wanted.has(id));
190
+ if (ready.length !== wanted.size) throw new FlowError('not_ready', 'Only a ready invoice can be sent. Mark it ready first.');
191
+ }
192
+ const { go, waiting } = accountingRead.outcome === 'waiting'
193
+ ? { go: [], waiting: ready.slice(0, SEND_BATCH) }
194
+ : await readJobsFirst(db, organisationId, ready.slice(0, SEND_BATCH), { work, now, personId });
195
+ if (invoiceIds && waiting.length) {
196
+ throw new FlowError('not_ready', 'Flow could not confirm the job with the work system or the accounting system just now, or the job changed. Nothing was sent. Look at the invoice, then try again.');
197
+ }
198
+ const batch = go;
155
199
  let issued = 0;
156
200
  for (const invoiceId of batch) {
157
201
  // Queued first: the worker sends only issued invoices, so a stop between
@@ -167,7 +211,7 @@ export async function sendAllReady(db, organisationId, { accounting = null, pers
167
211
  }
168
212
  issued += 1;
169
213
  }
170
- return { issued, queued: accounting ? issued : 0, remaining: ready.length - batch.length };
214
+ return { issued, queued: accounting ? issued : 0, remaining: Math.max(0, ready.length - SEND_BATCH), waiting: waiting.length };
171
215
  }
172
216
 
173
217
  // Send again: issued invoices that failed go back on the queue. With no ids,
@@ -0,0 +1 @@
1
+ ALTER TABLE "invoicing_settings" ADD COLUMN IF NOT EXISTS "reference_template" text DEFAULT '{practitioner} - {patient}' NOT NULL;
@@ -0,0 +1 @@
1
+ ALTER TABLE "billing_obligations" ADD COLUMN IF NOT EXISTS "draft_basis" jsonb;