@wirecat/cli-core 0.606.0 → 0.607.0
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/CHANGELOG.md +186 -1
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/reads.js +5 -0
- package/dist/cli/app-mcp-command.d.ts +6 -2
- package/dist/cli/app-mcp-command.js +11 -2
- package/dist/cli/csv.d.ts +10 -0
- package/dist/cli/csv.js +22 -0
- package/dist/cli/early-refusal.d.ts +10 -0
- package/dist/cli/early-refusal.js +19 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +3 -0
- package/dist/cli/profile-token.d.ts +29 -0
- package/dist/cli/profile-token.js +65 -0
- package/dist/cli/program.js +4 -2
- package/dist/crm/cli/csv.d.ts +23 -0
- package/dist/crm/cli/csv.js +135 -0
- package/dist/crm/cli/index.d.ts +1 -1
- package/dist/crm/cli/index.js +33 -21
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- package/dist/crm/writes.d.ts +6 -4
- package/dist/crm/writes.js +17 -13
- package/dist/documents.d.ts +2 -0
- package/dist/documents.js +2 -0
- package/dist/domain/business.d.ts +79 -0
- package/dist/domain/business.js +42 -0
- package/dist/domain/finance.d.ts +2 -0
- package/dist/domain/finance.js +2 -2
- package/dist/domain/google-documents.d.ts +2 -0
- package/dist/domain/google-documents.js +3 -1
- package/dist/domain/money.d.ts +2 -0
- package/dist/domain/money.js +11 -3
- package/dist/domain/providers.d.ts +36 -1
- package/dist/domain/providers.js +69 -7
- package/dist/domain/record-fields.d.ts +42 -0
- package/dist/domain/record-fields.js +122 -0
- package/dist/domain/record-types.d.ts +489 -16
- package/dist/domain/record-types.js +374 -7
- package/dist/domain/records.d.ts +96 -5
- package/dist/domain/records.js +178 -32
- package/dist/domain/ref-summaries.js +5 -0
- package/dist/domain/references.d.ts +1 -1
- package/dist/domain/references.js +5 -0
- package/dist/domain/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +14 -0
- package/dist/entities/deal.js +3 -0
- package/dist/entities/index.d.ts +12 -0
- package/dist/errors/reasons.js +61 -2
- package/dist/fetch/addresses.d.ts +5 -0
- package/dist/fetch/addresses.js +76 -0
- package/dist/fetch/page.d.ts +64 -0
- package/dist/fetch/page.js +256 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -3
- package/dist/meetings/calendar-text.d.ts +1 -1
- package/dist/meetings/calendar-text.js +4 -0
- package/dist/meetings/event-store.d.ts +2 -2
- package/dist/search/account-list.d.ts +4 -0
- package/dist/search/account-list.js +19 -8
- package/dist/search/query.js +2 -2
- package/dist/services/events-search.js +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/ingest.d.ts +56 -8
- package/dist/services/ingest.js +95 -61
- package/dist/services/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- package/dist/services/proposals/kinds/contracts.d.ts +4 -0
- package/dist/services/proposals/kinds/contracts.js +9 -0
- package/dist/services/proposals/kinds/finance.d.ts +4 -0
- package/dist/services/proposals/kinds/finance.js +14 -35
- package/dist/services/proposals/kinds/orders.js +7 -30
- package/dist/services/proposals/kinds/shared.d.ts +27 -1
- package/dist/services/proposals/kinds/shared.js +34 -0
- package/dist/services/proposals/kinds/source-review.d.ts +4 -0
- package/dist/services/proposals/kinds/source-review.js +45 -0
- package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
- package/dist/services/proposals/kinds/subscriptions.js +24 -0
- package/dist/services/proposals/kinds/transactions.d.ts +4 -0
- package/dist/services/proposals/kinds/transactions.js +10 -0
- package/dist/services/proposals/registry.js +9 -0
- package/dist/services/search-all-meetings.js +2 -2
- package/dist/services/search-all.js +4 -3
- package/dist/services/search-kind.js +3 -3
- package/dist/store/decisions.js +5 -1
- package/dist/store/index.d.ts +14 -3
- package/dist/store/index.js +3 -0
- package/dist/store/people.js +1 -1
- package/dist/store/source-review.d.ts +64 -0
- package/dist/store/source-review.js +7 -0
- package/dist/store/sqlite/accounts.d.ts +1 -1
- package/dist/store/sqlite/accounts.js +24 -1
- package/dist/store/sqlite/added-sources.d.ts +82 -0
- package/dist/store/sqlite/added-sources.js +237 -0
- package/dist/store/sqlite/business-contracts.d.ts +90 -0
- package/dist/store/sqlite/business-contracts.js +182 -0
- package/dist/store/sqlite/calendar-events.js +7 -3
- package/dist/store/sqlite/calendar-forget.js +3 -0
- package/dist/store/sqlite/calendar.d.ts +21 -1
- package/dist/store/sqlite/calendar.js +27 -7
- package/dist/store/sqlite/chat-rekey.js +7 -0
- package/dist/store/sqlite/chats.d.ts +3 -1
- package/dist/store/sqlite/chats.js +15 -1
- package/dist/store/sqlite/crm-rows.d.ts +44 -0
- package/dist/store/sqlite/crm-rows.js +195 -0
- package/dist/store/sqlite/crm.d.ts +2 -11
- package/dist/store/sqlite/crm.js +11 -166
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +5 -3
- package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
- package/dist/store/sqlite/emails.js +2 -0
- package/dist/store/sqlite/entity-types.d.ts +14 -9
- package/dist/store/sqlite/entity-types.js +4 -10
- package/dist/store/sqlite/evidence-signature.d.ts +114 -0
- package/dist/store/sqlite/evidence-signature.js +349 -0
- package/dist/store/sqlite/evidence-sources.d.ts +2 -1
- package/dist/store/sqlite/evidence-sources.js +7 -3
- package/dist/store/sqlite/facts.d.ts +7 -2
- package/dist/store/sqlite/facts.js +21 -5
- package/dist/store/sqlite/finance.d.ts +18 -105
- package/dist/store/sqlite/finance.js +100 -219
- package/dist/store/sqlite/knowledge.d.ts +8 -2
- package/dist/store/sqlite/knowledge.js +65 -71
- package/dist/store/sqlite/labelled.d.ts +3 -0
- package/dist/store/sqlite/labelled.js +11 -0
- package/dist/store/sqlite/link-kinds.d.ts +2 -0
- package/dist/store/sqlite/link-kinds.js +2 -0
- package/dist/store/sqlite/manifest.js +2 -0
- package/dist/store/sqlite/memories.d.ts +23 -9
- package/dist/store/sqlite/memories.js +41 -13
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +103 -0
- package/dist/store/sqlite/money-lines.d.ts +48 -0
- package/dist/store/sqlite/money-lines.js +76 -0
- package/dist/store/sqlite/money-transactions.d.ts +132 -0
- package/dist/store/sqlite/money-transactions.js +295 -0
- package/dist/store/sqlite/notes.d.ts +11 -2
- package/dist/store/sqlite/notes.js +20 -25
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/payment-facts.js +1 -1
- package/dist/store/sqlite/private-people.js +2 -0
- package/dist/store/sqlite/products.d.ts +78 -0
- package/dist/store/sqlite/products.js +131 -0
- package/dist/store/sqlite/project-transfer/records.js +26 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +5 -3
- package/dist/store/sqlite/proposed-actions.d.ts +1 -1
- package/dist/store/sqlite/proposed-actions.js +24 -7
- package/dist/store/sqlite/providers.d.ts +0 -4
- package/dist/store/sqlite/providers.js +20 -2
- package/dist/store/sqlite/record-matching.d.ts +80 -0
- package/dist/store/sqlite/record-matching.js +264 -0
- package/dist/store/sqlite/record-projection.d.ts +89 -0
- package/dist/store/sqlite/record-projection.js +50 -0
- package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
- package/dist/store/sqlite/record-projectors-direct.js +125 -0
- package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
- package/dist/store/sqlite/record-projectors-money.js +214 -0
- package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
- package/dist/store/sqlite/record-projectors-phone.js +212 -0
- package/dist/store/sqlite/record-projectors.d.ts +3 -44
- package/dist/store/sqlite/record-projectors.js +7 -67
- package/dist/store/sqlite/record-relations.d.ts +24 -0
- package/dist/store/sqlite/record-relations.js +92 -0
- package/dist/store/sqlite/record-remap.d.ts +12 -0
- package/dist/store/sqlite/record-remap.js +92 -0
- package/dist/store/sqlite/record-run.d.ts +71 -0
- package/dist/store/sqlite/record-run.js +179 -0
- package/dist/store/sqlite/record-settling.d.ts +9 -0
- package/dist/store/sqlite/record-settling.js +74 -0
- package/dist/store/sqlite/record-stamps.d.ts +13 -0
- package/dist/store/sqlite/record-stamps.js +30 -0
- package/dist/store/sqlite/record-writes.d.ts +43 -0
- package/dist/store/sqlite/record-writes.js +307 -0
- package/dist/store/sqlite/records.d.ts +52 -42
- package/dist/store/sqlite/records.js +123 -353
- package/dist/store/sqlite/ref-summaries.d.ts +6 -5
- package/dist/store/sqlite/ref-summaries.js +60 -8
- package/dist/store/sqlite/refunds.d.ts +31 -0
- package/dist/store/sqlite/refunds.js +112 -0
- package/dist/store/sqlite/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema.d.ts +2840 -83
- package/dist/store/sqlite/schema.js +256 -1
- package/dist/store/sqlite/source-review-marks.d.ts +54 -0
- package/dist/store/sqlite/source-review-marks.js +143 -0
- package/dist/store/sqlite/source-review.d.ts +54 -0
- package/dist/store/sqlite/source-review.js +221 -0
- package/dist/store/sqlite/stage-amounts.d.ts +14 -0
- package/dist/store/sqlite/stage-amounts.js +23 -0
- package/dist/store/sqlite/subscriptions.d.ts +135 -0
- package/dist/store/sqlite/subscriptions.js +294 -0
- package/dist/store/sqlite/text-input.d.ts +3 -0
- package/dist/store/sqlite/text-input.js +13 -0
- package/dist/store/sqlite/things.js +2 -9
- package/dist/store/sqlite/transaction.d.ts +15 -0
- package/dist/store/sqlite/transaction.js +57 -0
- package/dist/store/sqlite/wallets.d.ts +88 -0
- package/dist/store/sqlite/wallets.js +143 -0
- package/dist/store/store-errors.d.ts +1 -0
- package/dist/store/store-errors.js +45 -0
- package/dist/store/store.d.ts +16 -1
- package/dist/store/store.js +30 -6
- package/package.json +5 -2
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
|
@@ -1,41 +1,11 @@
|
|
|
1
1
|
import { type FinanceReview, type InvoiceDirection, type InvoiceStatus, type MoneyDirection } from "../../domain/finance.js";
|
|
2
2
|
import { type Money } from "../../domain/money.js";
|
|
3
|
-
import {
|
|
4
|
-
import { type
|
|
5
|
-
import { type Actor, type ProposedByFilter } from "./actors.js";
|
|
3
|
+
import type { Decider } from "../decisions.js";
|
|
4
|
+
import { type ProposedByFilter } from "./actors.js";
|
|
6
5
|
import { type Author } from "./memories.js";
|
|
6
|
+
import { type MoneyLine, type MoneyLineInput } from "./money-lines.js";
|
|
7
7
|
import type { StoreContext } from "./open.js";
|
|
8
|
-
|
|
9
|
-
export interface Party {
|
|
10
|
-
ref: string;
|
|
11
|
-
name: string | null;
|
|
12
|
-
/** The owner's own person row. */
|
|
13
|
-
owner: boolean;
|
|
14
|
-
}
|
|
15
|
-
export interface Reviewed {
|
|
16
|
-
review: FinanceReview;
|
|
17
|
-
/** `name` is a bot's handle and `kind` whether it is an agent or a rule; both `null` for a person. */
|
|
18
|
-
author: {
|
|
19
|
-
type: Actor["type"];
|
|
20
|
-
id: string;
|
|
21
|
-
name: string | null;
|
|
22
|
-
kind: "agent" | "rule" | null;
|
|
23
|
-
};
|
|
24
|
-
model: string | null;
|
|
25
|
-
confirmedBy: {
|
|
26
|
-
type: Actor["type"];
|
|
27
|
-
id: string;
|
|
28
|
-
} | null;
|
|
29
|
-
source: string;
|
|
30
|
-
/** Why an agent or an import proposed it. */
|
|
31
|
-
reason: string | null;
|
|
32
|
-
/** What it was read from: the messages, documents or notes an agent or the owner cited. */
|
|
33
|
-
evidence: string[];
|
|
34
|
-
project: string | null;
|
|
35
|
-
projectName: string | null;
|
|
36
|
-
createdAt: string;
|
|
37
|
-
updatedAt: string;
|
|
38
|
-
}
|
|
8
|
+
import { type Party, type Reviewed, type Written } from "./reviewed.js";
|
|
39
9
|
export interface Invoice extends Reviewed {
|
|
40
10
|
id: string;
|
|
41
11
|
ref: string;
|
|
@@ -47,21 +17,17 @@ export interface Invoice extends Reviewed {
|
|
|
47
17
|
counterparty: Party;
|
|
48
18
|
currency: string;
|
|
49
19
|
total: string;
|
|
50
|
-
/** Settled by confirmed payments. */
|
|
20
|
+
/** Settled by confirmed payments, net of their confirmed refunds. */
|
|
51
21
|
paid: string;
|
|
52
22
|
/** Still owed: `total − paid`. */
|
|
53
23
|
open: string;
|
|
54
24
|
issueDate: string | null;
|
|
55
25
|
dueDate: string | null;
|
|
56
26
|
status: InvoiceStatus;
|
|
27
|
+
contract: string | null;
|
|
28
|
+
subscription: string | null;
|
|
57
29
|
}
|
|
58
|
-
export
|
|
59
|
-
position: number;
|
|
60
|
-
description: string;
|
|
61
|
-
quantity: string;
|
|
62
|
-
unitPrice: string | null;
|
|
63
|
-
amount: string;
|
|
64
|
-
}
|
|
30
|
+
export type InvoiceLine = MoneyLine;
|
|
65
31
|
export interface Allocation {
|
|
66
32
|
payment: string;
|
|
67
33
|
invoice: string;
|
|
@@ -88,29 +54,14 @@ export interface Payment extends Reviewed {
|
|
|
88
54
|
/** Allocated to invoices, confirmed or not. */
|
|
89
55
|
allocated: string;
|
|
90
56
|
unallocated: string;
|
|
57
|
+
/** The payment this refunds: the other direction, the same currency, never more than it. */
|
|
58
|
+
refundOf: string | null;
|
|
59
|
+
subscription: string | null;
|
|
91
60
|
}
|
|
92
61
|
export interface PaymentDetail extends Payment {
|
|
93
62
|
allocations: Allocation[];
|
|
94
63
|
}
|
|
95
|
-
export
|
|
96
|
-
/** The owner's entry is confirmed at once; a bot's waits as proposed for the owner. */
|
|
97
|
-
by: Author;
|
|
98
|
-
project?: string;
|
|
99
|
-
/** Where a bot's row came from, such as `agent` or an importer's name; the owner's is always `owner`. */
|
|
100
|
-
source?: string;
|
|
101
|
-
/** Why a bot proposes it, kept for the reviewer. */
|
|
102
|
-
reason?: string;
|
|
103
|
-
/** References it was read from, linked as `evidence`. */
|
|
104
|
-
evidence?: string[];
|
|
105
|
-
}
|
|
106
|
-
export interface InvoiceLineInput {
|
|
107
|
-
description: string;
|
|
108
|
-
/** A decimal string; `1` when not given. */
|
|
109
|
-
quantity?: string;
|
|
110
|
-
/** Decimal strings in the invoice's currency. Give either or both: both must agree, quantity × unit price. */
|
|
111
|
-
unitPrice?: string;
|
|
112
|
-
amount?: string;
|
|
113
|
-
}
|
|
64
|
+
export type InvoiceLineInput = MoneyLineInput;
|
|
114
65
|
export interface InvoiceInput extends Written {
|
|
115
66
|
direction: InvoiceDirection;
|
|
116
67
|
number?: string;
|
|
@@ -126,6 +77,9 @@ export interface InvoiceInput extends Written {
|
|
|
126
77
|
lines: InvoiceLineInput[];
|
|
127
78
|
/** The total printed on the invoice: refused when it is not the sum of the lines. */
|
|
128
79
|
total?: string;
|
|
80
|
+
contract?: string;
|
|
81
|
+
/** The subscription whose period it bills. */
|
|
82
|
+
subscription?: string;
|
|
129
83
|
}
|
|
130
84
|
export interface PaymentInput extends Written {
|
|
131
85
|
direction: MoneyDirection;
|
|
@@ -140,6 +94,9 @@ export interface PaymentInput extends Written {
|
|
|
140
94
|
invoice: string;
|
|
141
95
|
amount: string;
|
|
142
96
|
}[];
|
|
97
|
+
/** The payment it refunds: it then goes the other way, and is allocated only to invoices that payment settles. */
|
|
98
|
+
refundOf?: string;
|
|
99
|
+
subscription?: string;
|
|
143
100
|
}
|
|
144
101
|
interface ListFilter {
|
|
145
102
|
project?: string;
|
|
@@ -207,50 +164,6 @@ export interface FinanceStore {
|
|
|
207
164
|
payable: OpenTotal[];
|
|
208
165
|
}>;
|
|
209
166
|
}
|
|
210
|
-
type Row = Record<string, unknown>;
|
|
211
|
-
/** The types `decide` settles money for; other proposal types get their own route. */
|
|
212
|
-
declare const FINANCE_TYPES: readonly ["invoice", "payment"];
|
|
213
|
-
type FinanceType = (typeof FINANCE_TYPES)[number];
|
|
214
|
-
type FinanceTable = (typeof RECORD_TYPES)[FinanceType]["table"];
|
|
215
|
-
/** One article, for the first prefix only: `an invoice: or payment:`. */
|
|
216
|
-
export declare const prefixesText: (types: readonly string[]) => string;
|
|
217
|
-
export declare const refuse: (message: string, reason: string, code?: "validation_error" | "permission_error") => never;
|
|
218
|
-
export declare const oneOf: <T extends string>(value: unknown, allowed: readonly T[], what: string) => T;
|
|
219
|
-
export declare const text: (value: unknown, what: string, limit: number) => string | null;
|
|
220
|
-
export declare const isoDate: (value: unknown, what: string) => string | null;
|
|
221
|
-
export declare const iso: (value: unknown) => string;
|
|
222
|
-
/** What invoices, payments and orders share: their parties, who wrote them, and how the owner decides them. */
|
|
223
|
-
export declare const reviewedRowsOver: (context: StoreContext) => {
|
|
224
|
-
partyOf: (type: unknown, id: unknown) => Party | null;
|
|
225
|
-
partyThing: (reference: string | undefined, what: string) => import("./things.js").Thing | undefined;
|
|
226
|
-
reviewedOf: (row: Row, type: "invoice" | "payment" | "order" | "deal") => Reviewed;
|
|
227
|
-
written: (input: Written) => {
|
|
228
|
-
owner: boolean;
|
|
229
|
-
actor: Actor;
|
|
230
|
-
model: string | null;
|
|
231
|
-
source: string;
|
|
232
|
-
review: string;
|
|
233
|
-
confirmedType: "bot" | "person" | null;
|
|
234
|
-
confirmedId: number | null;
|
|
235
|
-
metadata: string | null;
|
|
236
|
-
project: number | null;
|
|
237
|
-
evidence: string[];
|
|
238
|
-
};
|
|
239
|
-
keepEvidence: (type: "invoice" | "payment" | "order" | "deal", id: number, by: ReturnType<(input: Written) => {
|
|
240
|
-
owner: boolean;
|
|
241
|
-
actor: Actor;
|
|
242
|
-
model: string | null;
|
|
243
|
-
source: string;
|
|
244
|
-
review: string;
|
|
245
|
-
confirmedType: "bot" | "person" | null;
|
|
246
|
-
confirmedId: number | null;
|
|
247
|
-
metadata: string | null;
|
|
248
|
-
project: number | null;
|
|
249
|
-
evidence: string[];
|
|
250
|
-
}>) => void;
|
|
251
|
-
ownerOnly: (by: Author, what: string) => void;
|
|
252
|
-
keepDecision: (table: FinanceTable | "orders", id: number, by: Decider, reason?: string) => void;
|
|
253
|
-
};
|
|
254
167
|
type FinanceOperations = {
|
|
255
168
|
[K in keyof FinanceStore]: FinanceStore[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
|
|
256
169
|
};
|
|
@@ -1,139 +1,29 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { FINANCE_REVIEWS, INVOICE_DIRECTIONS, INVOICE_STATUSES, invoiceTitle, MONEY_DIRECTIONS, paymentEntryTitle, settledStatus, } from "../../domain/finance.js";
|
|
3
|
-
import { currencyOf, formatDecimal,
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { authorActor, authorOf, evidenceOf, linkEvidence, projectId, projectOf, subjectOf, } from "./memories.js";
|
|
3
|
+
import { currencyOf, formatDecimal, minorUnits, moneyLabel, moneyOf, scaleOf } from "../../domain/money.js";
|
|
4
|
+
import { proposedBySql } from "./actors.js";
|
|
5
|
+
import { authorActor, projectId } from "./memories.js";
|
|
6
|
+
import { moneyLinesOver } from "./money-lines.js";
|
|
8
7
|
import { requiredThing } from "./notes.js";
|
|
8
|
+
import { netPaidSql, refundRulesOver } from "./refunds.js";
|
|
9
|
+
import { iso, isoDate, limitOf, oneOf, prefixesText, refuse, reviewedRowsOver, text, } from "./reviewed.js";
|
|
9
10
|
import { write } from "./transaction.js";
|
|
10
11
|
/** The types `decide` settles money for; other proposal types get their own route. */
|
|
11
12
|
const FINANCE_TYPES = ["invoice", "payment"];
|
|
12
13
|
const isFinanceType = (type) => FINANCE_TYPES.includes(type);
|
|
13
|
-
const article = (word) => (/^[aeiou]/.test(word) ? "an" : "a");
|
|
14
|
-
/** One article, for the first prefix only: `an invoice: or payment:`. */
|
|
15
|
-
export const prefixesText = (types) => `${article(types[0] ?? "")} ${types.map((type) => `${type}:`).join(" or ")}`;
|
|
16
|
-
export const refuse = (message, reason, code = "validation_error") => {
|
|
17
|
-
throw new CliError(code, message, { reason });
|
|
18
|
-
};
|
|
19
|
-
export const oneOf = (value, allowed, what) => {
|
|
20
|
-
if (typeof value !== "string" || !allowed.includes(value))
|
|
21
|
-
return refuse(`${what} is one of ${allowed.join(", ")}`, "finance.field");
|
|
22
|
-
return value;
|
|
23
|
-
};
|
|
24
|
-
export const text = (value, what, limit) => {
|
|
25
|
-
if (value === undefined || value === null)
|
|
26
|
-
return null;
|
|
27
|
-
if (typeof value !== "string" || !value.trim() || value.length > limit || value.includes("\0"))
|
|
28
|
-
return refuse(`${what} takes 1–${limit} characters`, "finance.field");
|
|
29
|
-
return value.trim();
|
|
30
|
-
};
|
|
31
|
-
export const isoDate = (value, what) => {
|
|
32
|
-
if (value === undefined || value === null)
|
|
33
|
-
return null;
|
|
34
|
-
const date = typeof value === "string" ? value.trim() : "";
|
|
35
|
-
if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || new Date(`${date}T00:00:00Z`).toISOString().slice(0, 10) !== date)
|
|
36
|
-
return refuse(`${what} is a date such as 2026-11-15`, "finance.field");
|
|
37
|
-
return date;
|
|
38
|
-
};
|
|
39
|
-
export const iso = (value) => new Date(Number(value)).toISOString();
|
|
40
|
-
const evidenceList = (value) => {
|
|
41
|
-
if (value === undefined)
|
|
42
|
-
return [];
|
|
43
|
-
if (!Array.isArray(value) || value.length > 8 || value.some((one) => typeof one !== "string" || !one.trim()))
|
|
44
|
-
return refuse("evidence is a list of at most 8 references", "finance.field");
|
|
45
|
-
return [...new Set(value.map((one) => one.trim()))];
|
|
46
|
-
};
|
|
47
14
|
const MAX_LINES = 500;
|
|
48
|
-
const reasonOf = (metadata) => {
|
|
49
|
-
if (metadata == null)
|
|
50
|
-
return null;
|
|
51
|
-
try {
|
|
52
|
-
const { reason } = JSON.parse(String(metadata));
|
|
53
|
-
return typeof reason === "string" ? reason : null;
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
15
|
/** Money settled by confirmed payments only: a proposed payment promises nothing yet. */
|
|
60
|
-
const PAID = "
|
|
61
|
-
"WHERE a.invoice_id = i.id AND p.review = 'confirmed' AND p.deleted_at IS NULL)";
|
|
16
|
+
const PAID = netPaidSql("i.id");
|
|
62
17
|
const INVOICES = `SELECT i.*, ${PAID} AS paid, b.name AS author_name, b.kind AS author_kind FROM invoices i ` +
|
|
63
18
|
"LEFT JOIN bots b ON i.author_type = 'bot' AND b.id = i.author_id";
|
|
64
19
|
const PAYMENTS = "SELECT p.*, (SELECT coalesce(sum(amount), 0) FROM payment_allocations WHERE payment_id = p.id) AS allocated, " +
|
|
65
20
|
"b.name AS author_name, b.kind AS author_kind FROM payments p LEFT JOIN bots b ON p.author_type = 'bot' AND b.id = p.author_id";
|
|
66
|
-
/** What invoices, payments and orders share: their parties, who wrote them, and how the owner decides them. */
|
|
67
|
-
export const reviewedRowsOver = (context) => {
|
|
68
|
-
const { database } = context;
|
|
69
|
-
const partyOf = (type, id) => {
|
|
70
|
-
if (type == null)
|
|
71
|
-
return null;
|
|
72
|
-
const { subject, subjectName, subjectIsOwner } = subjectOf(database, { subject_type: type, subject_id: id });
|
|
73
|
-
return subject === null ? null : { ref: subject, name: subjectName, owner: subjectIsOwner };
|
|
74
|
-
};
|
|
75
|
-
const partyThing = (reference, what) => {
|
|
76
|
-
if (reference === undefined)
|
|
77
|
-
return undefined;
|
|
78
|
-
const thing = requiredThing(database, reference, what);
|
|
79
|
-
if (thing.type !== "person" && thing.type !== "organization")
|
|
80
|
-
return refuse(`${what} is a person: or organization: reference, not ${reference}`, "finance.party");
|
|
81
|
-
return thing;
|
|
82
|
-
};
|
|
83
|
-
const reviewedOf = (row, type) => ({
|
|
84
|
-
review: row.review,
|
|
85
|
-
author: authorOf(row),
|
|
86
|
-
model: row.model == null ? null : String(row.model),
|
|
87
|
-
confirmedBy: row.confirmed_by_type == null
|
|
88
|
-
? null
|
|
89
|
-
: { type: row.confirmed_by_type, id: String(row.confirmed_by_id) },
|
|
90
|
-
source: String(row.source),
|
|
91
|
-
reason: reasonOf(row.metadata),
|
|
92
|
-
evidence: evidenceOf(context, { type, id: Number(row.id) }),
|
|
93
|
-
...projectOf(database, row),
|
|
94
|
-
createdAt: iso(row.created_at),
|
|
95
|
-
updatedAt: iso(row.updated_at),
|
|
96
|
-
});
|
|
97
|
-
const written = (input) => {
|
|
98
|
-
const owner = input.by === "owner";
|
|
99
|
-
const { actor, model } = authorActor(context, input.by);
|
|
100
|
-
const source = owner ? "owner" : (text(input.source, "a source", 80) ?? "agent");
|
|
101
|
-
const reason = owner ? null : text(input.reason, "a reason", 2000);
|
|
102
|
-
return {
|
|
103
|
-
owner,
|
|
104
|
-
actor,
|
|
105
|
-
model,
|
|
106
|
-
source,
|
|
107
|
-
review: owner ? "confirmed" : "proposed",
|
|
108
|
-
confirmedType: owner ? actor.type : null,
|
|
109
|
-
confirmedId: owner ? actor.id : null,
|
|
110
|
-
metadata: reason === null ? null : JSON.stringify({ reason }),
|
|
111
|
-
project: input.project === undefined ? null : projectId(database, input.project),
|
|
112
|
-
evidence: evidenceList(input.evidence),
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
const keepEvidence = (type, id, by) => {
|
|
116
|
-
if (by.evidence.length)
|
|
117
|
-
linkEvidence(context, { type, id }, by.evidence, by.owner ? "owner" : "agent");
|
|
118
|
-
};
|
|
119
|
-
const ownerOnly = (by, what) => {
|
|
120
|
-
if (by !== "owner")
|
|
121
|
-
refuse(`only the owner ${what}`, "finance.owner-only", "permission_error");
|
|
122
|
-
};
|
|
123
|
-
const keepDecision = (table, id, by, reason) => {
|
|
124
|
-
const why = [by === "owner" ? undefined : by.reason.trim(), reason?.trim() || undefined].filter(Boolean).join(": ");
|
|
125
|
-
if (by === "owner" && !why)
|
|
126
|
-
return;
|
|
127
|
-
const decision = by === "owner" ? { by: "owner", reason: why } : { by: "agent", bot: by.bot, reason: why };
|
|
128
|
-
database
|
|
129
|
-
.prepare(`UPDATE ${table} SET metadata = json_set(coalesce(metadata, '{}'), '$.decision', json(?)) WHERE id = ?`)
|
|
130
|
-
.run(JSON.stringify(decision), id);
|
|
131
|
-
};
|
|
132
|
-
return { partyOf, partyThing, reviewedOf, written, keepEvidence, ownerOnly, keepDecision };
|
|
133
|
-
};
|
|
134
21
|
export const financeOperationsOver = (context) => {
|
|
135
22
|
const { database, now } = context;
|
|
136
|
-
const { partyOf, partyThing, reviewedOf, written, keepEvidence, ownerOnly,
|
|
23
|
+
const { partyOf, partyThing, reviewedOf, written, keepEvidence, ownerOnly, linkedId, decideRow, mayChangeParts } = reviewedRowsOver(context);
|
|
24
|
+
const refunds = refundRulesOver(context);
|
|
25
|
+
const lines = moneyLinesOver(context);
|
|
26
|
+
const refOf = (type, id) => (id == null ? null : `${type}:${id}`);
|
|
137
27
|
const invoiceOf = (row) => {
|
|
138
28
|
const currency = String(row.currency);
|
|
139
29
|
const scale = scaleOf(currency);
|
|
@@ -165,6 +55,8 @@ export const financeOperationsOver = (context) => {
|
|
|
165
55
|
issueDate: row.issue_date == null ? null : String(row.issue_date),
|
|
166
56
|
dueDate: fields.dueDate,
|
|
167
57
|
status: fields.status,
|
|
58
|
+
contract: refOf("contract", row.contract_id),
|
|
59
|
+
subscription: refOf("subscription", row.subscription_id),
|
|
168
60
|
...reviewedOf(row, "invoice"),
|
|
169
61
|
};
|
|
170
62
|
};
|
|
@@ -196,6 +88,8 @@ export const financeOperationsOver = (context) => {
|
|
|
196
88
|
reference: row.reference == null ? null : String(row.reference),
|
|
197
89
|
allocated: formatDecimal(allocated, scale),
|
|
198
90
|
unallocated: formatDecimal(amount - allocated, scale),
|
|
91
|
+
refundOf: refOf("payment", row.refund_of),
|
|
92
|
+
subscription: refOf("subscription", row.subscription_id),
|
|
199
93
|
...reviewedOf(row, "payment"),
|
|
200
94
|
};
|
|
201
95
|
};
|
|
@@ -218,18 +112,11 @@ export const financeOperationsOver = (context) => {
|
|
|
218
112
|
};
|
|
219
113
|
const invoiceDetail = (reference) => {
|
|
220
114
|
const row = rowOf(reference, "invoice");
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
.
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
position: Number(line.position),
|
|
227
|
-
description: String(line.description),
|
|
228
|
-
quantity: String(line.quantity),
|
|
229
|
-
unitPrice: line.unit_price == null ? null : formatDecimal(Number(line.unit_price), scale),
|
|
230
|
-
amount: formatDecimal(Number(line.amount), scale),
|
|
231
|
-
}));
|
|
232
|
-
return { ...invoiceOf(row), lines, allocations: allocationsWhere("invoice_id", Number(row.id)) };
|
|
115
|
+
return {
|
|
116
|
+
...invoiceOf(row),
|
|
117
|
+
lines: lines.shownLines("invoice", Number(row.id), String(row.currency)),
|
|
118
|
+
allocations: allocationsWhere("invoice_id", Number(row.id)),
|
|
119
|
+
};
|
|
233
120
|
};
|
|
234
121
|
const paymentDetail = (reference) => {
|
|
235
122
|
const row = rowOf(reference, "payment");
|
|
@@ -262,13 +149,16 @@ export const financeOperationsOver = (context) => {
|
|
|
262
149
|
if (fromPayment + units > Number(payment.amount))
|
|
263
150
|
refuse(`${paymentRef} is ${moneyLabel(Number(payment.amount), currency)} and ` +
|
|
264
151
|
`${moneyLabel(fromPayment, currency)} of it is already allocated; ${moneyLabel(units, currency)} more is too much`, "finance.over-payment");
|
|
265
|
-
|
|
266
|
-
.
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
152
|
+
if (payment.refund_of != null)
|
|
153
|
+
refunds.refundShare(payment, Number(invoice.id), units);
|
|
154
|
+
else {
|
|
155
|
+
const toInvoice = Number(database
|
|
156
|
+
.prepare(`SELECT ${netPaidSql("?", { including: "?" })} AS n`)
|
|
157
|
+
.get(Number(invoice.id), Number(payment.id))?.n);
|
|
158
|
+
if (toInvoice + units > Number(invoice.total))
|
|
159
|
+
refuse(`${invoiceRef} totals ${moneyLabel(Number(invoice.total), currency)} and ` +
|
|
160
|
+
`${moneyLabel(toInvoice, currency)} is already allocated to it; ${moneyLabel(units, currency)} more is too much`, "finance.over-invoice");
|
|
161
|
+
}
|
|
272
162
|
const at = now();
|
|
273
163
|
database
|
|
274
164
|
.prepare("INSERT INTO payment_allocations (payment_id, invoice_id, amount, author_type, author_id, created_at, updated_at) " +
|
|
@@ -280,10 +170,9 @@ export const financeOperationsOver = (context) => {
|
|
|
280
170
|
/** A proposed payment's allocations count once confirmed; by then other confirmed payments may cover the invoice. */
|
|
281
171
|
const coveredElsewhere = (paymentId) => {
|
|
282
172
|
const over = database
|
|
283
|
-
.prepare(
|
|
284
|
-
"
|
|
285
|
-
"
|
|
286
|
-
"JOIN invoices i ON i.id = a.invoice_id WHERE a.payment_id = ? AND a.amount + others > i.total LIMIT 1")
|
|
173
|
+
.prepare(`SELECT a.invoice_id, a.amount, i.total, i.currency, ${netPaidSql("a.invoice_id", { excluding: "a.payment_id" })} ` +
|
|
174
|
+
"AS others FROM payment_allocations a JOIN invoices i ON i.id = a.invoice_id " +
|
|
175
|
+
"WHERE a.payment_id = ? AND a.amount + others > i.total LIMIT 1")
|
|
287
176
|
.get(paymentId);
|
|
288
177
|
if (!over)
|
|
289
178
|
return;
|
|
@@ -297,75 +186,63 @@ export const financeOperationsOver = (context) => {
|
|
|
297
186
|
if (!isFinanceType(thing.type))
|
|
298
187
|
return refuse(`${reference} is not ${prefixesText(FINANCE_TYPES)}`, "finance.ref");
|
|
299
188
|
const type = thing.type;
|
|
300
|
-
const table = RECORD_TYPES[type].table;
|
|
301
189
|
const row = rowOf(reference, type);
|
|
302
190
|
const ref = `${type}:${row.id}`;
|
|
303
|
-
assertMayDecide({ id: ref, proposedBy: proposerOf(database, row.author_type, row.author_id), ownerOnly: true }, by, context.decisions);
|
|
304
|
-
if (row.review !== "proposed")
|
|
305
|
-
refuse(`${ref} is already ${String(row.review)}`, "proposal.wrong-state");
|
|
306
191
|
const id = Number(row.id);
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
192
|
+
const payment = type === "payment";
|
|
193
|
+
decideRow(type, row, { by, reason, confirm }, {
|
|
194
|
+
before: () => {
|
|
195
|
+
if (payment && confirm && row.refund_of != null)
|
|
196
|
+
refunds.confirmable(row);
|
|
197
|
+
else if (payment && confirm)
|
|
198
|
+
coveredElsewhere(id);
|
|
199
|
+
if (payment && !confirm) {
|
|
200
|
+
refunds.keepsRefunded(row);
|
|
201
|
+
keepsTransaction(id);
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
after: () => {
|
|
205
|
+
const touched = payment
|
|
206
|
+
? database
|
|
207
|
+
.prepare("SELECT invoice_id FROM payment_allocations WHERE payment_id = ?")
|
|
208
|
+
.all(id)
|
|
209
|
+
.map((one) => Number(one.invoice_id))
|
|
210
|
+
: [id];
|
|
211
|
+
if (!confirm && payment)
|
|
212
|
+
database.prepare("DELETE FROM payment_allocations WHERE payment_id = ?").run(id);
|
|
213
|
+
if (!confirm && !payment)
|
|
214
|
+
dropAllocations(id, "");
|
|
215
|
+
for (const invoice of touched)
|
|
216
|
+
settle(invoice);
|
|
217
|
+
},
|
|
332
218
|
});
|
|
333
|
-
return
|
|
219
|
+
return payment ? paymentDetail(ref) : invoiceDetail(ref);
|
|
334
220
|
};
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
221
|
+
/** A statement line records this payment: it stays until the link is taken back on the transaction. */
|
|
222
|
+
const keepsTransaction = (paymentId) => {
|
|
223
|
+
const linked = database
|
|
224
|
+
.prepare("SELECT id FROM financial_transactions WHERE payment_id = ? AND deleted_at IS NULL LIMIT 1")
|
|
225
|
+
.get(paymentId);
|
|
226
|
+
if (linked)
|
|
227
|
+
refuse(`payment:${paymentId} is the payment of transaction:${linked.id}; take the link back there first`, "transaction.payment");
|
|
341
228
|
};
|
|
342
|
-
|
|
229
|
+
/** Refunds' shares go first: a payment's share stays while a refund of it takes part of it back. */
|
|
230
|
+
const dropAllocations = (invoiceId, which) => {
|
|
231
|
+
for (const refund of [true, false])
|
|
232
|
+
database
|
|
233
|
+
.prepare("DELETE FROM payment_allocations WHERE invoice_id = ? AND payment_id IN " +
|
|
234
|
+
`(SELECT id FROM payments WHERE refund_of IS ${refund ? "NOT " : ""}NULL${which})`)
|
|
235
|
+
.run(invoiceId);
|
|
236
|
+
};
|
|
237
|
+
const mayAllocate = (row, by) => mayChangeParts(row, by, `only the owner allocates payment:${row.id}; a bot allocates only from a payment it proposed that is still proposed`);
|
|
343
238
|
return {
|
|
344
239
|
addInvoice: (input) => {
|
|
345
240
|
const direction = oneOf(input.direction, INVOICE_DIRECTIONS, "an invoice's direction");
|
|
346
241
|
const currency = currencyOf(input.currency);
|
|
347
242
|
if (!Array.isArray(input.lines) || input.lines.length === 0 || input.lines.length > MAX_LINES)
|
|
348
243
|
refuse(`an invoice has 1–${MAX_LINES} lines`, "finance.lines");
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
const quantity = quantityOf(line.quantity ?? "1");
|
|
352
|
-
const unitPrice = line.unitPrice === undefined ? null : minorUnits(line.unitPrice, currency, `${what}'s unit price`);
|
|
353
|
-
const computed = unitPrice === null ? null : lineUnits(quantity.thousandths, unitPrice);
|
|
354
|
-
const amount = line.amount === undefined ? computed : minorUnits(line.amount, currency, `${what}'s amount`);
|
|
355
|
-
if (amount === null)
|
|
356
|
-
return refuse(`${what} takes a unit price, an amount or both`, "finance.lines");
|
|
357
|
-
if (computed !== null && computed !== amount)
|
|
358
|
-
refuse(`${what}: ${quantity.text} × ${moneyLabel(unitPrice, currency)} is ` +
|
|
359
|
-
`${moneyLabel(computed, currency)}, not ${moneyLabel(amount, currency)}`, "finance.line-amount");
|
|
360
|
-
return {
|
|
361
|
-
description: text(line.description, `${what}'s description`, 500) ??
|
|
362
|
-
refuse(`${what} needs a description`, "finance.lines"),
|
|
363
|
-
quantity: quantity.text,
|
|
364
|
-
unitPrice: unitPrice ?? (quantity.thousandths === 1000n ? amount : null),
|
|
365
|
-
amount,
|
|
366
|
-
};
|
|
367
|
-
});
|
|
368
|
-
const total = lines.reduce((sum, line) => sum + line.amount, 0);
|
|
244
|
+
const priced = lines.linesOf("invoice", input.lines, currency);
|
|
245
|
+
const total = priced.reduce((sum, line) => sum + line.amount, 0);
|
|
369
246
|
if (!Number.isSafeInteger(total))
|
|
370
247
|
refuse("an invoice's total is too large", "finance.total");
|
|
371
248
|
if (total <= 0)
|
|
@@ -382,18 +259,17 @@ export const financeOperationsOver = (context) => {
|
|
|
382
259
|
const issuer = partyThing(input.issuer, "the issuer");
|
|
383
260
|
const counterparty = partyThing(input.counterparty, "the counterparty") ??
|
|
384
261
|
refuse("an invoice names its counterparty, a person: or organization:", "finance.party");
|
|
262
|
+
const contract = linkedId(input.contract, "contract", "finance.ref");
|
|
263
|
+
const subscription = linkedId(input.subscription, "subscription", "finance.ref");
|
|
385
264
|
const by = written(input);
|
|
386
265
|
const at = now();
|
|
387
266
|
const made = Number(database
|
|
388
267
|
.prepare("INSERT INTO invoices (direction, number, issuer_type, issuer_id, counterparty_type, counterparty_id, project_id, " +
|
|
389
268
|
"currency, total, issue_date, due_date, status, review, author_type, author_id, model, confirmed_by_type, " +
|
|
390
|
-
"confirmed_by_id, source, metadata, created_at, updated_at) " +
|
|
391
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
|
|
392
|
-
.get(direction, number, issuer?.type ?? null, issuer?.id ?? null, counterparty.type, counterparty.id, by.project, currency, total, issueDate, dueDate, input.draft ? "draft" : "issued", by.review, by.actor.type, by.actor.id, by.model, by.confirmedType, by.confirmedId, by.source, by.metadata, at, at)?.id);
|
|
393
|
-
|
|
394
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
395
|
-
for (const [index, line] of lines.entries())
|
|
396
|
-
insert.run(made, index + 1, line.description, line.quantity, line.unitPrice, line.amount, at, at);
|
|
269
|
+
"confirmed_by_id, source, metadata, created_at, updated_at, contract_id, subscription_id) " +
|
|
270
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
|
|
271
|
+
.get(direction, number, issuer?.type ?? null, issuer?.id ?? null, counterparty.type, counterparty.id, by.project, currency, total, issueDate, dueDate, input.draft ? "draft" : "issued", by.review, by.actor.type, by.actor.id, by.model, by.confirmedType, by.confirmedId, by.source, by.metadata, at, at, contract, subscription)?.id);
|
|
272
|
+
lines.insertLines("invoice", made, priced, 1, at);
|
|
397
273
|
keepEvidence("invoice", made, by);
|
|
398
274
|
return made;
|
|
399
275
|
});
|
|
@@ -433,17 +309,11 @@ export const financeOperationsOver = (context) => {
|
|
|
433
309
|
const row = rowOf(reference, "invoice");
|
|
434
310
|
if (row.status === "void")
|
|
435
311
|
refuse(`invoice:${row.id} is already void`, "finance.status");
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
"WHERE a.invoice_id = ? AND p.review = 'confirmed' AND p.deleted_at IS NULL LIMIT 1")
|
|
439
|
-
.get(Number(row.id));
|
|
440
|
-
if (allocated)
|
|
312
|
+
const paid = Number(database.prepare(`SELECT ${netPaidSql("?")} AS n`).get(Number(row.id))?.n);
|
|
313
|
+
if (paid > 0)
|
|
441
314
|
refuse(`payments are allocated to invoice:${row.id}; a paid invoice is not voided`, "finance.status");
|
|
442
315
|
write(database, () => {
|
|
443
|
-
|
|
444
|
-
.prepare("DELETE FROM payment_allocations WHERE invoice_id = ? " +
|
|
445
|
-
"AND payment_id IN (SELECT id FROM payments WHERE review = 'proposed')")
|
|
446
|
-
.run(Number(row.id));
|
|
316
|
+
dropAllocations(Number(row.id), " AND review = 'proposed'");
|
|
447
317
|
database.prepare("UPDATE invoices SET status = 'void', updated_at = ? WHERE id = ?").run(now(), Number(row.id));
|
|
448
318
|
});
|
|
449
319
|
return invoiceDetail(`invoice:${row.id}`);
|
|
@@ -463,13 +333,18 @@ export const financeOperationsOver = (context) => {
|
|
|
463
333
|
const id = write(database, () => {
|
|
464
334
|
const payer = partyThing(input.payer, "the payer");
|
|
465
335
|
const payee = partyThing(input.payee, "the payee");
|
|
336
|
+
const refundOf = linkedId(input.refundOf, "payment", "finance.refund");
|
|
337
|
+
if (refundOf !== null)
|
|
338
|
+
refunds.originalFor(refundOf, { direction, currency, amount });
|
|
339
|
+
const subscription = linkedId(input.subscription, "subscription", "finance.ref");
|
|
466
340
|
const by = written(input);
|
|
467
341
|
const at = now();
|
|
468
342
|
const made = database
|
|
469
343
|
.prepare("INSERT INTO payments (direction, payer_type, payer_id, payee_type, payee_id, amount, currency, payment_date, " +
|
|
470
344
|
"reference, project_id, review, author_type, author_id, model, confirmed_by_type, confirmed_by_id, source, " +
|
|
471
|
-
"metadata, created_at, updated_at
|
|
472
|
-
|
|
345
|
+
"metadata, created_at, updated_at, refund_of, subscription_id) " +
|
|
346
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *")
|
|
347
|
+
.get(direction, payer?.type ?? null, payer?.id ?? null, payee?.type ?? null, payee?.id ?? null, amount, currency, date, reference, by.project, by.review, by.actor.type, by.actor.id, by.model, by.confirmedType, by.confirmedId, by.source, by.metadata, at, at, refundOf, subscription);
|
|
473
348
|
keepEvidence("payment", Number(made.id), by);
|
|
474
349
|
const touched = allocations.map(({ invoice, units }) => allocateRow(made, invoice, units, by.actor));
|
|
475
350
|
for (const invoice of touched)
|
|
@@ -518,6 +393,12 @@ export const financeOperationsOver = (context) => {
|
|
|
518
393
|
refuse(`${ref} is ${row.deleted_at != null ? "deleted" : "rejected"}; only a proposed or confirmed payment's ` +
|
|
519
394
|
"allocations are taken back", "finance.unallocate");
|
|
520
395
|
const target = rowOf(invoice, "invoice");
|
|
396
|
+
if (target.status === "void")
|
|
397
|
+
refuse(`invoice:${target.id} is void; its allocations stay as they were`, "finance.unallocate");
|
|
398
|
+
if (row.refund_of == null)
|
|
399
|
+
refunds.keepsShare(Number(row.id), Number(target.id));
|
|
400
|
+
else
|
|
401
|
+
refunds.keepsInvoice(row, Number(target.id));
|
|
521
402
|
write(database, () => {
|
|
522
403
|
const gone = database
|
|
523
404
|
.prepare("DELETE FROM payment_allocations WHERE payment_id = ? AND invoice_id = ?")
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type QueryAst } from "@wirecat/cli-base/query";
|
|
2
2
|
import { type Decider } from "../decisions.js";
|
|
3
3
|
import type { AccountKey } from "../types.js";
|
|
4
|
+
import { type LabelledType } from "./labelled.js";
|
|
4
5
|
import { type MeetingProjectsStore } from "./meeting-projects.js";
|
|
5
6
|
import type { Author } from "./memories.js";
|
|
6
7
|
import { type Link } from "./notes.js";
|
|
@@ -9,6 +10,7 @@ import { type OrganizationsStore } from "./organizations.js";
|
|
|
9
10
|
import type { ProposedAction } from "./proposed-actions.js";
|
|
10
11
|
import { type KnowledgeRelation, type LinkDecision, type RelateInput, type RelationUpdate, type Scored } from "./relation-proposals.js";
|
|
11
12
|
import { type RelationReads } from "./relation-reads.js";
|
|
13
|
+
import { type SourceReviewStore } from "./source-review.js";
|
|
12
14
|
import type { TagCreationOptions } from "./tag-creation.js";
|
|
13
15
|
import { type Label, type TagKind, type TagRow } from "./tags.js";
|
|
14
16
|
import { type ThingState } from "./things.js";
|
|
@@ -101,8 +103,8 @@ export interface Reminder {
|
|
|
101
103
|
revision: number;
|
|
102
104
|
receipt: string | null;
|
|
103
105
|
}
|
|
104
|
-
export type LabelledType
|
|
105
|
-
export interface KnowledgeStore extends VocabularyStore, MeetingProjectsStore, OrganizationsStore, RelationReads {
|
|
106
|
+
export type { LabelledType } from "./labelled.js";
|
|
107
|
+
export interface KnowledgeStore extends VocabularyStore, MeetingProjectsStore, OrganizationsStore, RelationReads, SourceReviewStore {
|
|
106
108
|
taskIds(key: AccountKey, options?: {
|
|
107
109
|
state?: "open" | "done" | "dismissed";
|
|
108
110
|
limit?: number;
|
|
@@ -250,4 +252,8 @@ export interface KnowledgeStore extends VocabularyStore, MeetingProjectsStore, O
|
|
|
250
252
|
}): Promise<Reminder[]>;
|
|
251
253
|
acknowledgeReminder(key: AccountKey, id: string, receipt: string): Promise<Reminder>;
|
|
252
254
|
}
|
|
255
|
+
type Row = Record<string, unknown>;
|
|
253
256
|
export declare const knowledgeStoreOver: (context: StoreContext) => KnowledgeStore;
|
|
257
|
+
type ProjectInput = Parameters<KnowledgeStore["addProject"]>[0];
|
|
258
|
+
/** A new project row, its key made from the name when none is given; inside the caller's write. */
|
|
259
|
+
export declare const insertProject: ({ database, now }: StoreContext, { name, key, type, organization, scope, description }: ProjectInput) => Row;
|