@composable-os/cli 0.1.0-beta.10 → 0.1.0-beta.13

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 (173) hide show
  1. package/dist/{accounts-X4CKM3HL.js → accounts-GOCNY6WI.js} +17 -14
  2. package/dist/app-RPRZMLU5.js +43 -0
  3. package/dist/app-upload-YJVEKZC6.js +198 -0
  4. package/dist/{blocks-YWDHEBOK.js → blocks-DE4RRWPJ.js} +207 -25
  5. package/dist/{chunk-VY64DMQO.js → chunk-2B6YUYZV.js} +23576 -11674
  6. package/dist/{run-RT2HVWMK.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/chunk-5D7BDF4Y.js +141 -0
  9. package/dist/{chunk-WO6D3XP3.js → chunk-6CM3WZG5.js} +13 -2470
  10. package/dist/chunk-CIBJGB5H.js +962 -0
  11. package/dist/chunk-EPDTPGZJ.js +330 -0
  12. package/dist/{chunk-LLULPZAJ.js → chunk-HAYF4G5V.js} +66 -23
  13. package/dist/chunk-HCG6T53D.js +61904 -0
  14. package/dist/chunk-JX4IFWNT.js +330 -0
  15. package/dist/{chunk-A4NTSYWV.js → chunk-KFHKNXGH.js} +786 -386
  16. package/dist/chunk-KMFMD52Z.js +13276 -0
  17. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  18. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  19. package/dist/{chunk-LJFLENFC.js → chunk-Q6Y45DY6.js} +25 -2
  20. package/dist/{chunk-Y7JZFSTQ.js → chunk-QJZGYJVV.js} +823 -44
  21. package/dist/{chunk-6VXHPKGG.js → chunk-QQ3YSL5F.js} +172 -16
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-IFG4FRW5.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-CP6BXI6F.js → chunk-U72NEGCT.js} +8818 -3829
  26. package/dist/{chunk-WC5LHBBA.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/chunk-Z7OVQ6BC.js +713 -0
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-2DFENR62.js → cli-OKQHHSOE.js} +17 -23
  30. package/dist/{cli-YIOVSKOD.js → cli-VYSD2S4Z.js} +231 -82
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-PRU3VCQQ.js → client-repo-NNUCHQ2Y.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-2NC7LSYE.js → fields-GTHE6TTS.js} +27 -3
  35. package/dist/gateway-DPPEQZT4.js +25 -0
  36. package/dist/grants-6JY4XZO7.js +27 -0
  37. package/dist/invoke-MRONRZ5Q.js +33 -0
  38. package/dist/moves-UAUCMWL5.js +36 -0
  39. package/dist/{order-PUWD65BC.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-WIQQ7ITU.js → person-YOZTFV4Q.js} +11 -11
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0610_waitlists.sql +54 -0
  43. package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
  44. package/dist/platform/migrations/0650_person_links.sql +90 -0
  45. package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
  46. package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
  47. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  48. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  49. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  50. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  51. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  52. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  53. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  54. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  55. package/dist/platform/migrations/0815_checklists.sql +237 -0
  56. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  57. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  58. package/dist/platform/migrations/0850_agreements.sql +271 -0
  59. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  60. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  61. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  62. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  63. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  64. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  65. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  66. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  67. package/dist/platform/migrations/0911_texts.sql +60 -0
  68. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  69. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  70. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  71. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  72. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  73. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  74. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  75. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  76. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  77. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  78. package/dist/platform/migrations/0970_published_text.sql +59 -0
  79. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  80. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  81. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  82. package/dist/platform/migrations/1030_invoices.sql +747 -0
  83. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  84. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  85. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  86. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  87. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  88. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  89. package/dist/platform/migrations/1130_devices.sql +162 -0
  90. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  91. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  92. package/dist/platform/migrations/1151_payouts.sql +202 -0
  93. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  94. package/dist/platform/migrations/1170_journeys.sql +318 -0
  95. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  96. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  97. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  98. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  99. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  100. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  101. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  102. package/dist/platform/migrations/1271_leave.sql +249 -0
  103. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  104. package/dist/platform/migrations/1330_accounting.sql +260 -0
  105. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  106. package/dist/platform/migrations/1390_locations.sql +77 -0
  107. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  108. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  109. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  110. package/dist/platform/sdk/acknowledge.ts +100 -0
  111. package/dist/platform/sdk/agreements.ts +185 -0
  112. package/dist/platform/sdk/allowance.ts +98 -8
  113. package/dist/platform/sdk/automation-check.ts +120 -9
  114. package/dist/platform/sdk/automation.ts +78 -10
  115. package/dist/platform/sdk/behaviour.ts +26 -6
  116. package/dist/platform/sdk/calendar-entry.ts +258 -0
  117. package/dist/platform/sdk/capacity.ts +103 -2
  118. package/dist/platform/sdk/checklists.ts +118 -0
  119. package/dist/platform/sdk/contract.ts +514 -27
  120. package/dist/platform/sdk/discount.ts +408 -0
  121. package/dist/platform/sdk/document.ts +4 -2
  122. package/dist/platform/sdk/field-types.ts +5 -0
  123. package/dist/platform/sdk/formula/compile.ts +10 -0
  124. package/dist/platform/sdk/index.ts +17 -0
  125. package/dist/platform/sdk/intake.ts +380 -0
  126. package/dist/platform/sdk/journey-signals.ts +215 -0
  127. package/dist/platform/sdk/manifest.ts +1313 -66
  128. package/dist/platform/sdk/mine.ts +161 -0
  129. package/dist/platform/sdk/occupies.ts +192 -0
  130. package/dist/platform/sdk/questions.ts +681 -0
  131. package/dist/platform/sdk/recurring.ts +90 -9
  132. package/dist/platform/sdk/restricted-fields.ts +129 -0
  133. package/dist/platform/sdk/rich-text.ts +112 -0
  134. package/dist/platform/sdk/routine-mail.ts +1272 -0
  135. package/dist/platform/sdk/runtime.ts +25 -12
  136. package/dist/platform/sdk/screens.ts +91 -0
  137. package/dist/platform/sdk/sellable.ts +595 -1
  138. package/dist/platform/sdk/slug.ts +7 -2
  139. package/dist/platform/sdk/staffed.ts +474 -0
  140. package/dist/platform/sdk/states.ts +220 -64
  141. package/dist/platform/sdk/stock.ts +332 -0
  142. package/dist/platform/sdk/team.ts +83 -0
  143. package/dist/platform/sdk/testing.ts +70 -6
  144. package/dist/platform/sdk/worked-on.ts +134 -0
  145. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  146. package/dist/run-7ID64RA6.js +65 -0
  147. package/dist/run-FH7OGGTV.js +131 -0
  148. package/dist/run-OYZIAP6M.js +3363 -0
  149. package/dist/{run-Y7T4TVOL.js → run-QOFRRSZV.js} +41 -60
  150. package/dist/{run-PZUCSDDL.js → run-TJAEWR5U.js} +21 -51
  151. package/dist/{src-XTOD7EXW.js → src-52VERUPK.js} +312 -4
  152. package/dist/src-PHRELWN5.js +40 -0
  153. package/dist/visibility-SZCINPP4.js +58 -0
  154. package/package.json +2 -2
  155. package/dist/chunk-6GDEDOV7.js +0 -157
  156. package/dist/chunk-6UPUF4ML.js +0 -29998
  157. package/dist/chunk-7CQGMVDO.js +0 -63
  158. package/dist/chunk-GYSGVT6R.js +0 -284
  159. package/dist/chunk-JJHHNV46.js +0 -227
  160. package/dist/chunk-K6T42YCH.js +0 -704
  161. package/dist/chunk-M7A6FR2Z.js +0 -2586
  162. package/dist/chunk-O2UIZHND.js +0 -249
  163. package/dist/chunk-Q4N55M2Z.js +0 -552
  164. package/dist/chunk-QGGJ2G6Z.js +0 -349
  165. package/dist/chunk-R4W5XAUD.js +0 -837
  166. package/dist/chunk-TQFGGSA4.js +0 -748
  167. package/dist/chunk-YJJHF65I.js +0 -759
  168. package/dist/chunk-Z3RMMB6S.js +0 -668
  169. package/dist/grants-QXMKYMGB.js +0 -32
  170. package/dist/invoke-I3NNNXVL.js +0 -39
  171. package/dist/plan-on-sale-NB774CQL.js +0 -26
  172. package/dist/src-GYS6NBQB.js +0 -44
  173. package/dist/visibility-DLGHU4YN.js +0 -33
@@ -0,0 +1,747 @@
1
+ -- Migration 1030: quotes, invoices and credit notes, their lines, the numbering series and the
2
+ -- document event log (stage 35 step 2, ADRs 0730 and 0732).
3
+ --
4
+ -- Quotes, invoices and credit notes are base records in core, like Order. A draft has no number
5
+ -- and staff (and the assistant, through the gate) change it freely. Sending a quote, or issuing an
6
+ -- invoice or a credit note, gives it the next number of its series in that batch; from then on the
7
+ -- database refuses any change to what it says: the bill-to, the lines, the totals, the tax, the
8
+ -- number, the dates it was given and the snapshot of the business. Only its state and the host's
9
+ -- bookkeeping fields (reminders, part payments allowed, the due date) move afterwards, and only by
10
+ -- host code. A draft is deleted freely; an issued document is never deleted (the retention purge,
11
+ -- stage 35 step 11, is the one way, and amends the triggers below when it is built).
12
+ --
13
+ -- Every amount is an integer in the currency's minor unit. Tax is worked out by the host from the
14
+ -- business's own rates (ADR 0732): each line's tax rounded half up, the document's tax the sum of
15
+ -- its lines. A quantity is held in thousandths (hours and metres), so no amount is a float.
16
+ --
17
+ -- D1 runs with recursive triggers off, so the rows an INSERT OR REPLACE deletes fire no delete
18
+ -- trigger: every table here refuses an insert whose key already exists (a REPLACE is an insert),
19
+ -- so a REPLACE cannot get round the triggers that refuse an update or a delete (as migration 0850).
20
+ --
21
+ -- The bill-to's ids are Person and Organisation ids that an erase sets to null (ON DELETE SET
22
+ -- NULL): the only change to an issued document's bill-to the triggers let through. What was
23
+ -- printed (bill_to_*) stays.
24
+
25
+ -- One numbering series each for quotes, invoices and credit notes. A human admin sets the prefix
26
+ -- and the next number (invoice.settings.update), which only goes up. Every change moves `version`
27
+ -- by exactly one: an admin's change names the version it read, so one made against a series that
28
+ -- issued a number meanwhile is refused (compare-and-set); the issuing batch bumps it in SQL.
29
+ CREATE TABLE core_document_counter (
30
+ series TEXT PRIMARY KEY CHECK (series IN ('quote', 'invoice', 'credit_note')),
31
+ prefix TEXT NOT NULL CHECK (length(prefix) <= 12),
32
+ next_number INTEGER NOT NULL CHECK (next_number BETWEEN 1 AND 99999999),
33
+ version INTEGER NOT NULL DEFAULT 0,
34
+ updated_at TEXT,
35
+ updated_by TEXT
36
+ );
37
+ INSERT INTO core_document_counter (series, prefix, next_number) VALUES
38
+ ('quote', 'Q-', 1), ('invoice', 'INV-', 1), ('credit_note', 'CN-', 1);
39
+
40
+ CREATE TRIGGER core_document_counter_forward
41
+ BEFORE UPDATE ON core_document_counter
42
+ WHEN NEW.series IS NOT OLD.series OR NEW.next_number < OLD.next_number
43
+ OR NEW.version IS NOT OLD.version + 1
44
+ BEGIN
45
+ SELECT RAISE(ABORT, 'core_document_counter_back');
46
+ END;
47
+
48
+ -- Its three rows are the ones above: no other insert (a REPLACE included, which would reset one).
49
+ CREATE TRIGGER core_document_counter_no_insert
50
+ BEFORE INSERT ON core_document_counter
51
+ BEGIN
52
+ SELECT RAISE(ABORT, 'core_document_counter_back');
53
+ END;
54
+
55
+ CREATE TRIGGER core_document_counter_no_delete
56
+ BEFORE DELETE ON core_document_counter
57
+ BEGIN
58
+ SELECT RAISE(ABORT, 'core_document_counter_back');
59
+ END;
60
+
61
+ -- A quote: an offer at stated prices, valid until a date. `sent` is its issued state: it has its
62
+ -- number from then on and says what it said. accepted, declined, expired and withdrawn are final.
63
+ CREATE TABLE core_quote (
64
+ -- qt_… (ids.ts).
65
+ id TEXT PRIMARY KEY NOT NULL,
66
+ state TEXT NOT NULL DEFAULT 'draft'
67
+ CHECK (state IN ('draft', 'sent', 'accepted', 'declined', 'expired', 'withdrawn')),
68
+ -- The series' prefix and number at send (Q-0041), and the number alone.
69
+ number TEXT,
70
+ number_seq INTEGER,
71
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
72
+ -- Whether the lines' unit prices were typed without tax (added on top) or with it (inside).
73
+ tax_mode TEXT NOT NULL CHECK (tax_mode IN ('exclusive', 'inclusive')),
74
+ -- Whom it is addressed to: a Person, or an Organisation and its contact Person (person_id).
75
+ person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
76
+ organisation_id TEXT REFERENCES core_organisation (id) ON DELETE SET NULL,
77
+ -- The bill-to as printed, fixed at send (null on a draft).
78
+ bill_to_name TEXT,
79
+ bill_to_address TEXT,
80
+ bill_to_tax_number TEXT,
81
+ bill_to_email TEXT,
82
+ site_address TEXT,
83
+ -- Any record the quote is about (a deal, a booking, later a job): its object and id.
84
+ about_object TEXT,
85
+ about_id TEXT,
86
+ title TEXT,
87
+ notes TEXT,
88
+ valid_until TEXT,
89
+ deposit_kind TEXT NOT NULL DEFAULT 'none' CHECK (deposit_kind IN ('none', 'amount', 'percent')),
90
+ -- An amount in minor units, or a percentage in hundredths of a percent (2500 = 25%).
91
+ deposit_value INTEGER,
92
+ net_total INTEGER NOT NULL DEFAULT 0 CHECK (net_total >= 0),
93
+ tax_total INTEGER NOT NULL DEFAULT 0 CHECK (tax_total >= 0),
94
+ total INTEGER NOT NULL DEFAULT 0 CHECK (total >= 0),
95
+ -- The totals by rate, as JSON [{key, label, rate, net, tax, total}].
96
+ tax_by_rate TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(tax_by_rate)),
97
+ replaces_quote_id TEXT REFERENCES core_quote (id),
98
+ acceptance_id TEXT,
99
+ sent_at TEXT,
100
+ accepted_at TEXT,
101
+ declined_at TEXT,
102
+ decline_reason TEXT,
103
+ withdrawn_at TEXT,
104
+ expired_at TEXT,
105
+ created_at TEXT NOT NULL,
106
+ created_by TEXT NOT NULL,
107
+ updated_at TEXT NOT NULL,
108
+ updated_by TEXT NOT NULL,
109
+ -- Counts the quote's writes: a write made from an older read is refused (compare-and-set).
110
+ version INTEGER NOT NULL DEFAULT 0,
111
+ CHECK (total = net_total + tax_total),
112
+ CHECK (deposit_kind = 'none' OR deposit_value > 0),
113
+ CHECK (deposit_kind != 'percent' OR deposit_value <= 10000),
114
+ CHECK ((about_object IS NULL) = (about_id IS NULL))
115
+ );
116
+ CREATE UNIQUE INDEX core_quote_number_uq ON core_quote (number_seq) WHERE number_seq IS NOT NULL;
117
+ CREATE INDEX core_quote_person_idx ON core_quote (person_id, created_at);
118
+ CREATE INDEX core_quote_organisation_idx ON core_quote (organisation_id, created_at)
119
+ WHERE organisation_id IS NOT NULL;
120
+ CREATE INDEX core_quote_state_idx ON core_quote (state, created_at);
121
+
122
+ -- An invoice: a numbered request for payment, with its own order from issue (stage 35 step 3).
123
+ CREATE TABLE core_invoice (
124
+ -- inv_… (ids.ts).
125
+ id TEXT PRIMARY KEY NOT NULL,
126
+ state TEXT NOT NULL DEFAULT 'draft'
127
+ CHECK (state IN ('draft', 'issued', 'sent', 'part_paid', 'paid', 'credited', 'void')),
128
+ kind TEXT NOT NULL DEFAULT 'standard' CHECK (kind IN ('standard', 'deposit', 'final')),
129
+ number TEXT,
130
+ number_seq INTEGER,
131
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
132
+ tax_mode TEXT NOT NULL CHECK (tax_mode IN ('exclusive', 'inclusive')),
133
+ person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
134
+ organisation_id TEXT REFERENCES core_organisation (id) ON DELETE SET NULL,
135
+ bill_to_name TEXT,
136
+ bill_to_address TEXT,
137
+ bill_to_tax_number TEXT,
138
+ bill_to_email TEXT,
139
+ site_address TEXT,
140
+ about_object TEXT,
141
+ about_id TEXT,
142
+ title TEXT,
143
+ notes TEXT,
144
+ -- The quote it was made from (a deposit, a progress claim, the final invoice).
145
+ quote_id TEXT REFERENCES core_quote (id),
146
+ -- The order issuing started (stage 35 step 3).
147
+ order_id TEXT,
148
+ payment_terms_days INTEGER NOT NULL CHECK (payment_terms_days BETWEEN 0 AND 365),
149
+ issued_on TEXT,
150
+ -- Set by staff on a draft, or worked out at issue from the terms; host code may move it later
151
+ -- (invoice.setDueDate), so it is not fixed by the trigger below.
152
+ due_on TEXT,
153
+ issued_at TEXT,
154
+ sent_at TEXT,
155
+ allow_part_payments INTEGER NOT NULL DEFAULT 0 CHECK (allow_part_payments IN (0, 1)),
156
+ reminders_paused INTEGER NOT NULL DEFAULT 0 CHECK (reminders_paused IN (0, 1)),
157
+ reminders_paused_reason TEXT,
158
+ net_total INTEGER NOT NULL DEFAULT 0 CHECK (net_total >= 0),
159
+ tax_total INTEGER NOT NULL DEFAULT 0 CHECK (tax_total >= 0),
160
+ total INTEGER NOT NULL DEFAULT 0 CHECK (total >= 0),
161
+ tax_by_rate TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(tax_by_rate)),
162
+ -- The business's details, bank details and footer as at issue (JSON; null on a draft).
163
+ business TEXT CHECK (business IS NULL OR json_valid(business)),
164
+ created_at TEXT NOT NULL,
165
+ created_by TEXT NOT NULL,
166
+ updated_at TEXT NOT NULL,
167
+ updated_by TEXT NOT NULL,
168
+ version INTEGER NOT NULL DEFAULT 0,
169
+ CHECK (total = net_total + tax_total),
170
+ CHECK ((about_object IS NULL) = (about_id IS NULL))
171
+ );
172
+ CREATE UNIQUE INDEX core_invoice_number_uq ON core_invoice (number_seq) WHERE number_seq IS NOT NULL;
173
+ CREATE INDEX core_invoice_person_idx ON core_invoice (person_id, created_at);
174
+ CREATE INDEX core_invoice_organisation_idx ON core_invoice (organisation_id, created_at)
175
+ WHERE organisation_id IS NOT NULL;
176
+ CREATE INDEX core_invoice_state_idx ON core_invoice (state, created_at);
177
+ CREATE INDEX core_invoice_quote_idx ON core_invoice (quote_id) WHERE quote_id IS NOT NULL;
178
+
179
+ -- A credit note: a numbered document that reduces what an invoice asks for (stage 35 step 5).
180
+ CREATE TABLE core_credit_note (
181
+ -- cn_… (ids.ts).
182
+ id TEXT PRIMARY KEY NOT NULL,
183
+ state TEXT NOT NULL DEFAULT 'draft' CHECK (state IN ('draft', 'issued')),
184
+ number TEXT,
185
+ number_seq INTEGER,
186
+ invoice_id TEXT NOT NULL REFERENCES core_invoice (id),
187
+ currency TEXT NOT NULL CHECK (length(currency) = 3 AND currency = upper(currency)),
188
+ tax_mode TEXT NOT NULL CHECK (tax_mode IN ('exclusive', 'inclusive')),
189
+ person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
190
+ organisation_id TEXT REFERENCES core_organisation (id) ON DELETE SET NULL,
191
+ bill_to_name TEXT,
192
+ bill_to_address TEXT,
193
+ bill_to_tax_number TEXT,
194
+ bill_to_email TEXT,
195
+ reason TEXT,
196
+ -- Reduce what is owed, give back what was paid, or both in that order.
197
+ settle TEXT NOT NULL DEFAULT 'reduce' CHECK (settle IN ('reduce', 'refund', 'both')),
198
+ issued_on TEXT,
199
+ issued_at TEXT,
200
+ net_total INTEGER NOT NULL DEFAULT 0 CHECK (net_total >= 0),
201
+ tax_total INTEGER NOT NULL DEFAULT 0 CHECK (tax_total >= 0),
202
+ total INTEGER NOT NULL DEFAULT 0 CHECK (total >= 0),
203
+ tax_by_rate TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(tax_by_rate)),
204
+ business TEXT CHECK (business IS NULL OR json_valid(business)),
205
+ created_at TEXT NOT NULL,
206
+ created_by TEXT NOT NULL,
207
+ updated_at TEXT NOT NULL,
208
+ updated_by TEXT NOT NULL,
209
+ version INTEGER NOT NULL DEFAULT 0,
210
+ CHECK (total = net_total + tax_total)
211
+ );
212
+ CREATE UNIQUE INDEX core_credit_note_number_uq ON core_credit_note (number_seq)
213
+ WHERE number_seq IS NOT NULL;
214
+ CREATE INDEX core_credit_note_invoice_idx ON core_credit_note (invoice_id);
215
+
216
+ -- Lines, one table per document. A line comes from a record of a sellable object, priced by the
217
+ -- host when added (`record`), or is typed by staff (`custom`); a deduction ("less deposit paid",
218
+ -- invoices only) is written only by the host and names the deposit invoice it deducts. A staff
219
+ -- discount on a line lowers its net, and the tax is worked out on what is left. The rate's key,
220
+ -- its rate (hundredths of a percent: 1000 = 10%) and its label are kept on the line as used.
221
+ CREATE TABLE core_quote_line (
222
+ -- qtl_… (ids.ts).
223
+ id TEXT PRIMARY KEY NOT NULL,
224
+ quote_id TEXT NOT NULL REFERENCES core_quote (id),
225
+ position INTEGER NOT NULL CHECK (position >= 0),
226
+ description TEXT NOT NULL,
227
+ quantity_milli INTEGER NOT NULL CHECK (quantity_milli BETWEEN 1 AND 10000000),
228
+ unit TEXT,
229
+ unit_price INTEGER NOT NULL CHECK (unit_price >= 0),
230
+ tax_key TEXT,
231
+ tax_label TEXT,
232
+ tax_rate INTEGER NOT NULL DEFAULT 0 CHECK (tax_rate BETWEEN 0 AND 10000),
233
+ discount_kind TEXT CHECK (discount_kind IS NULL OR discount_kind IN ('amount', 'percent')),
234
+ discount_value INTEGER,
235
+ discount_reason TEXT,
236
+ discount INTEGER NOT NULL DEFAULT 0 CHECK (discount >= 0),
237
+ line_net INTEGER NOT NULL CHECK (line_net >= 0),
238
+ line_tax INTEGER NOT NULL CHECK (line_tax >= 0),
239
+ line_total INTEGER NOT NULL CHECK (line_total >= 0),
240
+ source TEXT NOT NULL CHECK (source IN ('record', 'custom')),
241
+ source_object TEXT,
242
+ source_id TEXT,
243
+ source_category TEXT,
244
+ -- A record line: the record's price as the host read it (with tax, ADR 0479), per unit.
245
+ source_price INTEGER,
246
+ created_at TEXT NOT NULL,
247
+ CHECK (line_total = line_net + line_tax),
248
+ CHECK ((source = 'record') = (source_object IS NOT NULL AND source_id IS NOT NULL)),
249
+ CHECK ((tax_key IS NULL) = (tax_rate = 0 AND tax_label IS NULL)),
250
+ CHECK ((discount_kind IS NULL) = (discount_value IS NULL))
251
+ );
252
+ CREATE INDEX core_quote_line_quote_idx ON core_quote_line (quote_id, position);
253
+ CREATE INDEX core_quote_line_tax_idx ON core_quote_line (tax_key) WHERE tax_key IS NOT NULL;
254
+
255
+ CREATE TABLE core_invoice_line (
256
+ -- inl_… (ids.ts).
257
+ id TEXT PRIMARY KEY NOT NULL,
258
+ invoice_id TEXT NOT NULL REFERENCES core_invoice (id),
259
+ position INTEGER NOT NULL CHECK (position >= 0),
260
+ description TEXT NOT NULL,
261
+ quantity_milli INTEGER NOT NULL CHECK (quantity_milli BETWEEN 1 AND 10000000),
262
+ unit TEXT,
263
+ unit_price INTEGER NOT NULL CHECK (unit_price >= 0),
264
+ tax_key TEXT,
265
+ tax_label TEXT,
266
+ tax_rate INTEGER NOT NULL DEFAULT 0 CHECK (tax_rate BETWEEN 0 AND 10000),
267
+ discount_kind TEXT CHECK (discount_kind IS NULL OR discount_kind IN ('amount', 'percent')),
268
+ discount_value INTEGER,
269
+ discount_reason TEXT,
270
+ discount INTEGER NOT NULL DEFAULT 0 CHECK (discount >= 0),
271
+ line_net INTEGER NOT NULL CHECK (line_net >= 0),
272
+ line_tax INTEGER NOT NULL CHECK (line_tax >= 0),
273
+ line_total INTEGER NOT NULL CHECK (line_total >= 0),
274
+ source TEXT NOT NULL CHECK (source IN ('record', 'custom', 'deduction')),
275
+ source_object TEXT,
276
+ source_id TEXT,
277
+ source_category TEXT,
278
+ source_price INTEGER,
279
+ -- A deduction line: the deposit invoice whose payment it takes off.
280
+ deducts_invoice_id TEXT REFERENCES core_invoice (id),
281
+ created_at TEXT NOT NULL,
282
+ CHECK (line_total = line_net + line_tax),
283
+ CHECK ((source = 'record') = (source_object IS NOT NULL AND source_id IS NOT NULL)),
284
+ CHECK ((source = 'deduction') = (deducts_invoice_id IS NOT NULL)),
285
+ CHECK ((tax_key IS NULL) = (tax_rate = 0 AND tax_label IS NULL)),
286
+ CHECK ((discount_kind IS NULL) = (discount_value IS NULL))
287
+ );
288
+ CREATE INDEX core_invoice_line_invoice_idx ON core_invoice_line (invoice_id, position);
289
+ CREATE INDEX core_invoice_line_tax_idx ON core_invoice_line (tax_key) WHERE tax_key IS NOT NULL;
290
+
291
+ CREATE TABLE core_credit_note_line (
292
+ -- cnl_… (ids.ts).
293
+ id TEXT PRIMARY KEY NOT NULL,
294
+ credit_note_id TEXT NOT NULL REFERENCES core_credit_note (id),
295
+ position INTEGER NOT NULL CHECK (position >= 0),
296
+ description TEXT NOT NULL,
297
+ quantity_milli INTEGER NOT NULL CHECK (quantity_milli BETWEEN 1 AND 10000000),
298
+ unit TEXT,
299
+ unit_price INTEGER NOT NULL CHECK (unit_price >= 0),
300
+ tax_key TEXT,
301
+ tax_label TEXT,
302
+ tax_rate INTEGER NOT NULL DEFAULT 0 CHECK (tax_rate BETWEEN 0 AND 10000),
303
+ line_net INTEGER NOT NULL CHECK (line_net >= 0),
304
+ line_tax INTEGER NOT NULL CHECK (line_tax >= 0),
305
+ line_total INTEGER NOT NULL CHECK (line_total >= 0),
306
+ -- The invoice line it credits (part of its quantity), or null for an amount at a rate.
307
+ credits_line_id TEXT REFERENCES core_invoice_line (id),
308
+ created_at TEXT NOT NULL,
309
+ CHECK (line_total = line_net + line_tax),
310
+ CHECK ((tax_key IS NULL) = (tax_rate = 0 AND tax_label IS NULL))
311
+ );
312
+ CREATE INDEX core_credit_note_line_note_idx ON core_credit_note_line (credit_note_id, position);
313
+ CREATE INDEX core_credit_note_line_tax_idx ON core_credit_note_line (tax_key)
314
+ WHERE tax_key IS NOT NULL;
315
+
316
+ -- The document event log (decision 11): one row per issue, send, payment applied or reversed,
317
+ -- credit, refund confirmed, paid back and void, written by host code in the batch of the event.
318
+ -- Finance reports (stage 52) and Accounting sync (stage 61) read it by `seq`, which only grows
319
+ -- (AUTOINCREMENT never reuses one). Nothing changes or deletes a row.
320
+ CREATE TABLE core_invoice_event (
321
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
322
+ document_kind TEXT NOT NULL CHECK (document_kind IN ('quote', 'invoice', 'credit_note')),
323
+ document_id TEXT NOT NULL,
324
+ -- The invoice the event concerns (a credit note's invoice), null for a quote's.
325
+ invoice_id TEXT,
326
+ event TEXT NOT NULL CHECK (event IN ('issued', 'sent', 'payment_applied', 'payment_reversed',
327
+ 'credited', 'refund_confirmed', 'paid_back', 'voided')),
328
+ payment_id TEXT,
329
+ method TEXT,
330
+ amount INTEGER,
331
+ currency TEXT,
332
+ at TEXT NOT NULL,
333
+ -- The staff account that caused it, or null for the host (a verified payment, the cron).
334
+ by_user TEXT
335
+ );
336
+ CREATE INDEX core_invoice_event_document_idx ON core_invoice_event (document_id, seq);
337
+
338
+ -- Never over an existing row (a REPLACE), and never below the newest one: a reader that has read
339
+ -- up to a seq never misses a row written after it.
340
+ CREATE TRIGGER core_invoice_event_insert
341
+ BEFORE INSERT ON core_invoice_event
342
+ WHEN EXISTS (SELECT 1 FROM core_invoice_event e WHERE e.seq = NEW.seq)
343
+ BEGIN
344
+ SELECT RAISE(ABORT, 'core_invoice_event_immutable');
345
+ END;
346
+
347
+ CREATE TRIGGER core_invoice_event_in_order
348
+ AFTER INSERT ON core_invoice_event
349
+ WHEN NEW.seq < (SELECT MAX(seq) FROM core_invoice_event)
350
+ BEGIN
351
+ SELECT RAISE(ABORT, 'core_invoice_event_immutable');
352
+ END;
353
+
354
+ CREATE TRIGGER core_invoice_event_no_update
355
+ BEFORE UPDATE ON core_invoice_event
356
+ BEGIN
357
+ SELECT RAISE(ABORT, 'core_invoice_event_immutable');
358
+ END;
359
+
360
+ CREATE TRIGGER core_invoice_event_no_delete
361
+ BEFORE DELETE ON core_invoice_event
362
+ BEGIN
363
+ SELECT RAISE(ABORT, 'core_invoice_event_immutable');
364
+ END;
365
+
366
+ -- --- Quotes ---------------------------------------------------------------------------------
367
+
368
+ -- A new quote is a draft with no number, never over an existing id.
369
+ CREATE TRIGGER core_quote_new
370
+ BEFORE INSERT ON core_quote
371
+ BEGIN
372
+ SELECT CASE
373
+ WHEN EXISTS (SELECT 1 FROM core_quote q WHERE q.id = NEW.id)
374
+ THEN RAISE(ABORT, 'core_document_immutable')
375
+ WHEN NEW.state != 'draft' OR NEW.number IS NOT NULL OR NEW.number_seq IS NOT NULL
376
+ OR NEW.version != 0
377
+ THEN RAISE(ABORT, 'core_document_new')
378
+ END;
379
+ END;
380
+
381
+ -- Every write names the version it read (compare-and-set), except an erase's ON DELETE SET NULL
382
+ -- of the bill-to's ids, which changes nothing else. Once sent, nothing it says changes; it never
383
+ -- goes back to a draft, and a final state stays; a number is given once, the next of the series,
384
+ -- in the batch that bumped the series, and never one another document holds.
385
+ CREATE TRIGGER core_quote_update
386
+ BEFORE UPDATE ON core_quote
387
+ BEGIN
388
+ SELECT CASE
389
+ WHEN NEW.id IS NOT OLD.id
390
+ THEN RAISE(ABORT, 'core_document_immutable')
391
+ -- An erase's ON DELETE SET NULL: the bill-to's ids set to null, and nothing else at all.
392
+ WHEN NEW.version IS OLD.version
393
+ AND ((NEW.person_id IS NULL AND OLD.person_id IS NOT NULL)
394
+ OR (NEW.organisation_id IS NULL AND OLD.organisation_id IS NOT NULL))
395
+ AND (NEW.person_id IS NULL OR NEW.person_id IS OLD.person_id)
396
+ AND (NEW.organisation_id IS NULL OR NEW.organisation_id IS OLD.organisation_id)
397
+ AND
398
+ NEW.state IS OLD.state AND NEW.number IS OLD.number AND NEW.number_seq IS OLD.number_seq AND
399
+ NEW.currency IS OLD.currency AND NEW.tax_mode IS OLD.tax_mode AND
400
+ NEW.bill_to_name IS OLD.bill_to_name AND NEW.bill_to_address IS OLD.bill_to_address AND
401
+ NEW.bill_to_tax_number IS OLD.bill_to_tax_number AND
402
+ NEW.bill_to_email IS OLD.bill_to_email AND NEW.site_address IS OLD.site_address AND
403
+ NEW.about_object IS OLD.about_object AND NEW.about_id IS OLD.about_id AND
404
+ NEW.title IS OLD.title AND NEW.notes IS OLD.notes AND NEW.valid_until IS OLD.valid_until AND
405
+ NEW.deposit_kind IS OLD.deposit_kind AND NEW.deposit_value IS OLD.deposit_value AND
406
+ NEW.net_total IS OLD.net_total AND NEW.tax_total IS OLD.tax_total AND
407
+ NEW.total IS OLD.total AND NEW.tax_by_rate IS OLD.tax_by_rate AND
408
+ NEW.replaces_quote_id IS OLD.replaces_quote_id AND
409
+ NEW.acceptance_id IS OLD.acceptance_id AND NEW.sent_at IS OLD.sent_at AND
410
+ NEW.accepted_at IS OLD.accepted_at AND NEW.declined_at IS OLD.declined_at AND
411
+ NEW.decline_reason IS OLD.decline_reason AND NEW.withdrawn_at IS OLD.withdrawn_at AND
412
+ NEW.expired_at IS OLD.expired_at AND NEW.created_at IS OLD.created_at AND
413
+ NEW.created_by IS OLD.created_by AND NEW.updated_at IS OLD.updated_at AND
414
+ NEW.updated_by IS OLD.updated_by AND NEW.version IS OLD.version
415
+ THEN NULL
416
+ WHEN NEW.version IS NOT OLD.version + 1
417
+ THEN RAISE(ABORT, 'core_document_stale')
418
+ WHEN OLD.state != 'draft' AND (NEW.state = 'draft' OR
419
+ NEW.number IS NOT OLD.number OR NEW.number_seq IS NOT OLD.number_seq OR
420
+ NEW.currency IS NOT OLD.currency OR NEW.tax_mode IS NOT OLD.tax_mode OR
421
+ NEW.person_id IS NOT OLD.person_id OR NEW.organisation_id IS NOT OLD.organisation_id OR
422
+ NEW.bill_to_name IS NOT OLD.bill_to_name OR
423
+ NEW.bill_to_address IS NOT OLD.bill_to_address OR
424
+ NEW.bill_to_tax_number IS NOT OLD.bill_to_tax_number OR
425
+ NEW.bill_to_email IS NOT OLD.bill_to_email OR NEW.site_address IS NOT OLD.site_address OR
426
+ NEW.about_object IS NOT OLD.about_object OR NEW.about_id IS NOT OLD.about_id OR
427
+ NEW.title IS NOT OLD.title OR NEW.notes IS NOT OLD.notes OR
428
+ NEW.valid_until IS NOT OLD.valid_until OR NEW.deposit_kind IS NOT OLD.deposit_kind OR
429
+ NEW.deposit_value IS NOT OLD.deposit_value OR NEW.net_total IS NOT OLD.net_total OR
430
+ NEW.tax_total IS NOT OLD.tax_total OR NEW.total IS NOT OLD.total OR
431
+ NEW.tax_by_rate IS NOT OLD.tax_by_rate OR
432
+ NEW.replaces_quote_id IS NOT OLD.replaces_quote_id OR NEW.sent_at IS NOT OLD.sent_at OR
433
+ NEW.created_at IS NOT OLD.created_at OR NEW.created_by IS NOT OLD.created_by)
434
+ THEN RAISE(ABORT, 'core_document_immutable')
435
+ WHEN OLD.state IN ('accepted', 'declined', 'expired', 'withdrawn') AND NEW.state IS NOT OLD.state
436
+ THEN RAISE(ABORT, 'core_document_immutable')
437
+ -- A draft has no number; anything else has the one its series gave, and only that one.
438
+ WHEN NEW.state = 'draft' AND (NEW.number IS NOT NULL OR NEW.number_seq IS NOT NULL)
439
+ THEN RAISE(ABORT, 'core_document_number')
440
+ WHEN NEW.state != 'draft' AND (NEW.number IS NULL OR NEW.number_seq IS NULL)
441
+ THEN RAISE(ABORT, 'core_document_number')
442
+ WHEN NEW.number_seq IS NOT OLD.number_seq AND (OLD.number_seq IS NOT NULL
443
+ OR NOT EXISTS (SELECT 1 FROM core_document_counter c WHERE c.series = 'quote'
444
+ AND NEW.number_seq = c.next_number - 1
445
+ AND NEW.number IS c.prefix || printf('%04d', NEW.number_seq))
446
+ -- An UPDATE OR REPLACE onto another document's number would delete it unseen.
447
+ OR EXISTS (SELECT 1 FROM core_quote d WHERE d.number_seq = NEW.number_seq AND d.id != NEW.id))
448
+ THEN RAISE(ABORT, 'core_document_number')
449
+ WHEN NEW.number IS NOT OLD.number AND NEW.number_seq IS OLD.number_seq
450
+ THEN RAISE(ABORT, 'core_document_number')
451
+ END;
452
+ END;
453
+
454
+ CREATE TRIGGER core_quote_no_delete
455
+ BEFORE DELETE ON core_quote
456
+ WHEN OLD.state != 'draft'
457
+ BEGIN
458
+ SELECT RAISE(ABORT, 'core_document_immutable');
459
+ END;
460
+
461
+ -- A quote's lines change only while it is a draft, and never move to another quote.
462
+ CREATE TRIGGER core_quote_line_insert
463
+ BEFORE INSERT ON core_quote_line
464
+ BEGIN
465
+ SELECT CASE
466
+ WHEN EXISTS (SELECT 1 FROM core_quote_line l WHERE l.id = NEW.id)
467
+ OR NOT EXISTS (SELECT 1 FROM core_quote q WHERE q.id = NEW.quote_id AND q.state = 'draft')
468
+ THEN RAISE(ABORT, 'core_document_immutable')
469
+ END;
470
+ END;
471
+
472
+ CREATE TRIGGER core_quote_line_update
473
+ BEFORE UPDATE ON core_quote_line
474
+ WHEN NEW.id IS NOT OLD.id OR NEW.quote_id IS NOT OLD.quote_id
475
+ OR NOT EXISTS (SELECT 1 FROM core_quote q WHERE q.id = OLD.quote_id AND q.state = 'draft')
476
+ BEGIN
477
+ SELECT RAISE(ABORT, 'core_document_immutable');
478
+ END;
479
+
480
+ CREATE TRIGGER core_quote_line_delete
481
+ BEFORE DELETE ON core_quote_line
482
+ WHEN EXISTS (SELECT 1 FROM core_quote q WHERE q.id = OLD.quote_id AND q.state != 'draft')
483
+ BEGIN
484
+ SELECT RAISE(ABORT, 'core_document_immutable');
485
+ END;
486
+
487
+ -- --- Invoices -------------------------------------------------------------------------------
488
+
489
+ CREATE TRIGGER core_invoice_new
490
+ BEFORE INSERT ON core_invoice
491
+ BEGIN
492
+ SELECT CASE
493
+ WHEN EXISTS (SELECT 1 FROM core_invoice i WHERE i.id = NEW.id)
494
+ THEN RAISE(ABORT, 'core_document_immutable')
495
+ WHEN NEW.state != 'draft' OR NEW.number IS NOT NULL OR NEW.number_seq IS NOT NULL
496
+ OR NEW.version != 0
497
+ THEN RAISE(ABORT, 'core_document_new')
498
+ END;
499
+ END;
500
+
501
+ CREATE TRIGGER core_invoice_update
502
+ BEFORE UPDATE ON core_invoice
503
+ BEGIN
504
+ SELECT CASE
505
+ WHEN NEW.id IS NOT OLD.id
506
+ THEN RAISE(ABORT, 'core_document_immutable')
507
+ -- An erase's ON DELETE SET NULL: the bill-to's ids set to null, and nothing else at all.
508
+ WHEN NEW.version IS OLD.version
509
+ AND ((NEW.person_id IS NULL AND OLD.person_id IS NOT NULL)
510
+ OR (NEW.organisation_id IS NULL AND OLD.organisation_id IS NOT NULL))
511
+ AND (NEW.person_id IS NULL OR NEW.person_id IS OLD.person_id)
512
+ AND (NEW.organisation_id IS NULL OR NEW.organisation_id IS OLD.organisation_id)
513
+ AND
514
+ NEW.state IS OLD.state AND NEW.kind IS OLD.kind AND NEW.number IS OLD.number AND
515
+ NEW.number_seq IS OLD.number_seq AND NEW.currency IS OLD.currency AND
516
+ NEW.tax_mode IS OLD.tax_mode AND NEW.bill_to_name IS OLD.bill_to_name AND
517
+ NEW.bill_to_address IS OLD.bill_to_address AND
518
+ NEW.bill_to_tax_number IS OLD.bill_to_tax_number AND
519
+ NEW.bill_to_email IS OLD.bill_to_email AND NEW.site_address IS OLD.site_address AND
520
+ NEW.about_object IS OLD.about_object AND NEW.about_id IS OLD.about_id AND
521
+ NEW.title IS OLD.title AND NEW.notes IS OLD.notes AND NEW.quote_id IS OLD.quote_id AND
522
+ NEW.order_id IS OLD.order_id AND NEW.payment_terms_days IS OLD.payment_terms_days AND
523
+ NEW.issued_on IS OLD.issued_on AND NEW.due_on IS OLD.due_on AND
524
+ NEW.issued_at IS OLD.issued_at AND NEW.sent_at IS OLD.sent_at AND
525
+ NEW.allow_part_payments IS OLD.allow_part_payments AND
526
+ NEW.reminders_paused IS OLD.reminders_paused AND
527
+ NEW.reminders_paused_reason IS OLD.reminders_paused_reason AND
528
+ NEW.net_total IS OLD.net_total AND NEW.tax_total IS OLD.tax_total AND
529
+ NEW.total IS OLD.total AND NEW.tax_by_rate IS OLD.tax_by_rate AND
530
+ NEW.business IS OLD.business AND NEW.created_at IS OLD.created_at AND
531
+ NEW.created_by IS OLD.created_by AND NEW.updated_at IS OLD.updated_at AND
532
+ NEW.updated_by IS OLD.updated_by AND NEW.version IS OLD.version
533
+ THEN NULL
534
+ WHEN NEW.version IS NOT OLD.version + 1
535
+ THEN RAISE(ABORT, 'core_document_stale')
536
+ WHEN OLD.state != 'draft' AND (NEW.state = 'draft' OR
537
+ NEW.kind IS NOT OLD.kind OR NEW.number IS NOT OLD.number OR
538
+ NEW.number_seq IS NOT OLD.number_seq OR NEW.currency IS NOT OLD.currency OR
539
+ NEW.tax_mode IS NOT OLD.tax_mode OR NEW.person_id IS NOT OLD.person_id OR
540
+ NEW.organisation_id IS NOT OLD.organisation_id OR
541
+ NEW.bill_to_name IS NOT OLD.bill_to_name OR
542
+ NEW.bill_to_address IS NOT OLD.bill_to_address OR
543
+ NEW.bill_to_tax_number IS NOT OLD.bill_to_tax_number OR
544
+ NEW.bill_to_email IS NOT OLD.bill_to_email OR NEW.site_address IS NOT OLD.site_address OR
545
+ NEW.about_object IS NOT OLD.about_object OR NEW.about_id IS NOT OLD.about_id OR
546
+ NEW.title IS NOT OLD.title OR NEW.notes IS NOT OLD.notes OR
547
+ NEW.quote_id IS NOT OLD.quote_id OR NEW.order_id IS NOT OLD.order_id OR
548
+ NEW.payment_terms_days IS NOT OLD.payment_terms_days OR
549
+ NEW.issued_on IS NOT OLD.issued_on OR NEW.issued_at IS NOT OLD.issued_at OR
550
+ NEW.net_total IS NOT OLD.net_total OR NEW.tax_total IS NOT OLD.tax_total OR
551
+ NEW.total IS NOT OLD.total OR NEW.tax_by_rate IS NOT OLD.tax_by_rate OR
552
+ NEW.business IS NOT OLD.business OR NEW.created_at IS NOT OLD.created_at OR
553
+ NEW.created_by IS NOT OLD.created_by)
554
+ THEN RAISE(ABORT, 'core_document_immutable')
555
+ WHEN OLD.state IN ('credited', 'void') AND NEW.state IS NOT OLD.state
556
+ THEN RAISE(ABORT, 'core_document_immutable')
557
+ -- A draft has no number; anything else has the one its series gave, and only that one.
558
+ WHEN NEW.state = 'draft' AND (NEW.number IS NOT NULL OR NEW.number_seq IS NOT NULL)
559
+ THEN RAISE(ABORT, 'core_document_number')
560
+ WHEN NEW.state != 'draft' AND (NEW.number IS NULL OR NEW.number_seq IS NULL)
561
+ THEN RAISE(ABORT, 'core_document_number')
562
+ WHEN NEW.number_seq IS NOT OLD.number_seq AND (OLD.number_seq IS NOT NULL
563
+ OR NOT EXISTS (SELECT 1 FROM core_document_counter c WHERE c.series = 'invoice'
564
+ AND NEW.number_seq = c.next_number - 1
565
+ AND NEW.number IS c.prefix || printf('%04d', NEW.number_seq))
566
+ -- An UPDATE OR REPLACE onto another document's number would delete it unseen.
567
+ OR EXISTS (SELECT 1 FROM core_invoice d WHERE d.number_seq = NEW.number_seq AND d.id != NEW.id))
568
+ THEN RAISE(ABORT, 'core_document_number')
569
+ WHEN NEW.number IS NOT OLD.number AND NEW.number_seq IS OLD.number_seq
570
+ THEN RAISE(ABORT, 'core_document_number')
571
+ END;
572
+ END;
573
+
574
+ CREATE TRIGGER core_invoice_no_delete
575
+ BEFORE DELETE ON core_invoice
576
+ WHEN OLD.state != 'draft'
577
+ BEGIN
578
+ SELECT RAISE(ABORT, 'core_document_immutable');
579
+ END;
580
+
581
+ CREATE TRIGGER core_invoice_line_insert
582
+ BEFORE INSERT ON core_invoice_line
583
+ BEGIN
584
+ SELECT CASE
585
+ WHEN EXISTS (SELECT 1 FROM core_invoice_line l WHERE l.id = NEW.id)
586
+ OR NOT EXISTS (
587
+ SELECT 1 FROM core_invoice i WHERE i.id = NEW.invoice_id AND i.state = 'draft'
588
+ )
589
+ THEN RAISE(ABORT, 'core_document_immutable')
590
+ END;
591
+ END;
592
+
593
+ CREATE TRIGGER core_invoice_line_update
594
+ BEFORE UPDATE ON core_invoice_line
595
+ WHEN NEW.id IS NOT OLD.id OR NEW.invoice_id IS NOT OLD.invoice_id
596
+ OR NOT EXISTS (SELECT 1 FROM core_invoice i WHERE i.id = OLD.invoice_id AND i.state = 'draft')
597
+ BEGIN
598
+ SELECT RAISE(ABORT, 'core_document_immutable');
599
+ END;
600
+
601
+ CREATE TRIGGER core_invoice_line_delete
602
+ BEFORE DELETE ON core_invoice_line
603
+ WHEN EXISTS (SELECT 1 FROM core_invoice i WHERE i.id = OLD.invoice_id AND i.state != 'draft')
604
+ BEGIN
605
+ SELECT RAISE(ABORT, 'core_document_immutable');
606
+ END;
607
+
608
+ -- --- Credit notes ---------------------------------------------------------------------------
609
+
610
+ CREATE TRIGGER core_credit_note_new
611
+ BEFORE INSERT ON core_credit_note
612
+ BEGIN
613
+ SELECT CASE
614
+ WHEN EXISTS (SELECT 1 FROM core_credit_note n WHERE n.id = NEW.id)
615
+ THEN RAISE(ABORT, 'core_document_immutable')
616
+ WHEN NEW.state != 'draft' OR NEW.number IS NOT NULL OR NEW.number_seq IS NOT NULL
617
+ OR NEW.version != 0
618
+ THEN RAISE(ABORT, 'core_document_new')
619
+ END;
620
+ END;
621
+
622
+ CREATE TRIGGER core_credit_note_update
623
+ BEFORE UPDATE ON core_credit_note
624
+ BEGIN
625
+ SELECT CASE
626
+ WHEN NEW.id IS NOT OLD.id
627
+ THEN RAISE(ABORT, 'core_document_immutable')
628
+ -- An erase's ON DELETE SET NULL: the bill-to's ids set to null, and nothing else at all.
629
+ WHEN NEW.version IS OLD.version
630
+ AND ((NEW.person_id IS NULL AND OLD.person_id IS NOT NULL)
631
+ OR (NEW.organisation_id IS NULL AND OLD.organisation_id IS NOT NULL))
632
+ AND (NEW.person_id IS NULL OR NEW.person_id IS OLD.person_id)
633
+ AND (NEW.organisation_id IS NULL OR NEW.organisation_id IS OLD.organisation_id)
634
+ AND
635
+ NEW.state IS OLD.state AND NEW.number IS OLD.number AND NEW.number_seq IS OLD.number_seq AND
636
+ NEW.invoice_id IS OLD.invoice_id AND NEW.currency IS OLD.currency AND
637
+ NEW.tax_mode IS OLD.tax_mode AND NEW.bill_to_name IS OLD.bill_to_name AND
638
+ NEW.bill_to_address IS OLD.bill_to_address AND
639
+ NEW.bill_to_tax_number IS OLD.bill_to_tax_number AND
640
+ NEW.bill_to_email IS OLD.bill_to_email AND NEW.reason IS OLD.reason AND
641
+ NEW.settle IS OLD.settle AND NEW.issued_on IS OLD.issued_on AND
642
+ NEW.issued_at IS OLD.issued_at AND NEW.net_total IS OLD.net_total AND
643
+ NEW.tax_total IS OLD.tax_total AND NEW.total IS OLD.total AND
644
+ NEW.tax_by_rate IS OLD.tax_by_rate AND NEW.business IS OLD.business AND
645
+ NEW.created_at IS OLD.created_at AND NEW.created_by IS OLD.created_by AND
646
+ NEW.updated_at IS OLD.updated_at AND NEW.updated_by IS OLD.updated_by AND
647
+ NEW.version IS OLD.version
648
+ THEN NULL
649
+ WHEN NEW.version IS NOT OLD.version + 1
650
+ THEN RAISE(ABORT, 'core_document_stale')
651
+ WHEN OLD.state != 'draft' AND (NEW.state = 'draft' OR
652
+ NEW.number IS NOT OLD.number OR NEW.number_seq IS NOT OLD.number_seq OR
653
+ NEW.invoice_id IS NOT OLD.invoice_id OR NEW.currency IS NOT OLD.currency OR
654
+ NEW.tax_mode IS NOT OLD.tax_mode OR NEW.person_id IS NOT OLD.person_id OR
655
+ NEW.organisation_id IS NOT OLD.organisation_id OR
656
+ NEW.bill_to_name IS NOT OLD.bill_to_name OR
657
+ NEW.bill_to_address IS NOT OLD.bill_to_address OR
658
+ NEW.bill_to_tax_number IS NOT OLD.bill_to_tax_number OR
659
+ NEW.bill_to_email IS NOT OLD.bill_to_email OR NEW.reason IS NOT OLD.reason OR
660
+ NEW.settle IS NOT OLD.settle OR NEW.issued_on IS NOT OLD.issued_on OR
661
+ NEW.issued_at IS NOT OLD.issued_at OR NEW.net_total IS NOT OLD.net_total OR
662
+ NEW.tax_total IS NOT OLD.tax_total OR NEW.total IS NOT OLD.total OR
663
+ NEW.tax_by_rate IS NOT OLD.tax_by_rate OR NEW.business IS NOT OLD.business OR
664
+ NEW.created_at IS NOT OLD.created_at OR NEW.created_by IS NOT OLD.created_by)
665
+ THEN RAISE(ABORT, 'core_document_immutable')
666
+ -- A draft has no number; anything else has the one its series gave, and only that one.
667
+ WHEN NEW.state = 'draft' AND (NEW.number IS NOT NULL OR NEW.number_seq IS NOT NULL)
668
+ THEN RAISE(ABORT, 'core_document_number')
669
+ WHEN NEW.state != 'draft' AND (NEW.number IS NULL OR NEW.number_seq IS NULL)
670
+ THEN RAISE(ABORT, 'core_document_number')
671
+ WHEN NEW.number_seq IS NOT OLD.number_seq AND (OLD.number_seq IS NOT NULL
672
+ OR NOT EXISTS (SELECT 1 FROM core_document_counter c WHERE c.series = 'credit_note'
673
+ AND NEW.number_seq = c.next_number - 1
674
+ AND NEW.number IS c.prefix || printf('%04d', NEW.number_seq))
675
+ -- An UPDATE OR REPLACE onto another document's number would delete it unseen.
676
+ OR EXISTS (SELECT 1 FROM core_credit_note d WHERE d.number_seq = NEW.number_seq AND d.id != NEW.id))
677
+ THEN RAISE(ABORT, 'core_document_number')
678
+ WHEN NEW.number IS NOT OLD.number AND NEW.number_seq IS OLD.number_seq
679
+ THEN RAISE(ABORT, 'core_document_number')
680
+ END;
681
+ END;
682
+
683
+ CREATE TRIGGER core_credit_note_no_delete
684
+ BEFORE DELETE ON core_credit_note
685
+ WHEN OLD.state != 'draft'
686
+ BEGIN
687
+ SELECT RAISE(ABORT, 'core_document_immutable');
688
+ END;
689
+
690
+ CREATE TRIGGER core_credit_note_line_insert
691
+ BEFORE INSERT ON core_credit_note_line
692
+ BEGIN
693
+ SELECT CASE
694
+ WHEN EXISTS (SELECT 1 FROM core_credit_note_line l WHERE l.id = NEW.id)
695
+ OR NOT EXISTS (
696
+ SELECT 1 FROM core_credit_note n WHERE n.id = NEW.credit_note_id AND n.state = 'draft'
697
+ )
698
+ THEN RAISE(ABORT, 'core_document_immutable')
699
+ END;
700
+ END;
701
+
702
+ CREATE TRIGGER core_credit_note_line_update
703
+ BEFORE UPDATE ON core_credit_note_line
704
+ WHEN NEW.id IS NOT OLD.id OR NEW.credit_note_id IS NOT OLD.credit_note_id
705
+ OR NOT EXISTS (
706
+ SELECT 1 FROM core_credit_note n WHERE n.id = OLD.credit_note_id AND n.state = 'draft'
707
+ )
708
+ BEGIN
709
+ SELECT RAISE(ABORT, 'core_document_immutable');
710
+ END;
711
+
712
+ CREATE TRIGGER core_credit_note_line_delete
713
+ BEFORE DELETE ON core_credit_note_line
714
+ WHEN EXISTS (
715
+ SELECT 1 FROM core_credit_note n WHERE n.id = OLD.credit_note_id AND n.state != 'draft'
716
+ )
717
+ BEGIN
718
+ SELECT RAISE(ABORT, 'core_document_immutable');
719
+ END;
720
+
721
+ -- --- What dashboards and query.sql read -----------------------------------------------------
722
+ --
723
+ -- Numbers, states, dates, amounts and the bill-to's ids: never an address, a tax number, an
724
+ -- email, the title or the notes, and never the snapshot of the business (its bank details).
725
+ CREATE VIEW rec_quote AS
726
+ SELECT
727
+ id, number, state, currency, tax_mode, person_id, organisation_id, about_object, about_id,
728
+ valid_until, deposit_kind, deposit_value, net_total, tax_total, total, replaces_quote_id,
729
+ sent_at, accepted_at, declined_at, withdrawn_at, expired_at, created_at, updated_at
730
+ FROM core_quote;
731
+
732
+ CREATE VIEW rec_invoice AS
733
+ SELECT
734
+ id, number, state, kind, currency, tax_mode, person_id, organisation_id, about_object, about_id,
735
+ quote_id, order_id, payment_terms_days, issued_on, due_on, issued_at, sent_at,
736
+ allow_part_payments, reminders_paused, net_total, tax_total, total, created_at, updated_at
737
+ FROM core_invoice;
738
+
739
+ CREATE VIEW rec_credit_note AS
740
+ SELECT
741
+ id, number, state, invoice_id, currency, tax_mode, person_id, organisation_id, settle,
742
+ issued_on, issued_at, net_total, tax_total, total, created_at, updated_at
743
+ FROM core_credit_note;
744
+
745
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
746
+ UPDATE core_meta SET value = '1030'
747
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 1030;