@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
|
@@ -1091,5 +1091,108 @@ export const GENERATED = [
|
|
|
1091
1091
|
"statements": [
|
|
1092
1092
|
"CREATE UNIQUE INDEX `links_relationship_key` ON `links` (`kind`,`from_type`,`from_id`,`to_type`,`to_id`,ifnull(\"anchor\", ''),ifnull(\"role\", ''),ifnull(\"account_id\", 0)) WHERE kind IN ('member-of', 'related-to', 'reports-to', 'customer-of', 'partner-of') AND to_id IS NOT NULL;"
|
|
1093
1093
|
]
|
|
1094
|
+
},
|
|
1095
|
+
{
|
|
1096
|
+
"name": "20261108010753_version-57-business",
|
|
1097
|
+
"statements": [
|
|
1098
|
+
"CREATE TABLE `catalog_items` (\n\t`id` integer PRIMARY KEY,\n\t`kind` text NOT NULL,\n\t`name` text NOT NULL,\n\t`name_folded` text NOT NULL,\n\t`sku` text,\n\t`supplier_type` text,\n\t`supplier_id` integer,\n\t`unit` text,\n\t`unit_price` integer,\n\t`currency` text,\n\t`description` text,\n\t`archived_date` text,\n\t`author_type` text NOT NULL,\n\t`author_id` integer NOT NULL,\n\t`model` text,\n\t`source` text NOT NULL,\n\t`metadata` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\t`deleted_at` integer\n);",
|
|
1099
|
+
"CREATE TABLE `contracts` (\n\t`id` integer PRIMARY KEY,\n\t`name` text NOT NULL,\n\t`number` text,\n\t`kind` text NOT NULL,\n\t`status` text NOT NULL,\n\t`signed_date` text,\n\t`effective_date` text,\n\t`expiration_date` text,\n\t`auto_renew` integer DEFAULT 0 NOT NULL,\n\t`renewal_count` integer,\n\t`renewal_unit` text,\n\t`notice_days` integer,\n\t`terms` text,\n\t`value` integer,\n\t`currency` text,\n\t`project_id` integer,\n\t`deal_id` integer,\n\t`review` text NOT NULL,\n\t`author_type` text NOT NULL,\n\t`author_id` integer NOT NULL,\n\t`model` text,\n\t`confirmed_by_type` text,\n\t`confirmed_by_id` integer,\n\t`source` text NOT NULL,\n\t`metadata` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\t`deleted_at` integer,\n\tCONSTRAINT `fk_contracts_project_id_projects_id_fk` FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`),\n\tCONSTRAINT `fk_contracts_deal_id_deals_id_fk` FOREIGN KEY (`deal_id`) REFERENCES `deals`(`id`)\n);",
|
|
1100
|
+
"CREATE TABLE `financial_accounts` (\n\t`id` integer PRIMARY KEY,\n\t`name` text NOT NULL,\n\t`name_folded` text NOT NULL,\n\t`kind` text NOT NULL,\n\t`currency` text NOT NULL,\n\t`institution` text,\n\t`number_hint` text,\n\t`holder_type` text,\n\t`holder_id` integer,\n\t`opening_balance` integer,\n\t`opening_date` text,\n\t`closed_date` text,\n\t`author_type` text NOT NULL,\n\t`author_id` integer NOT NULL,\n\t`model` text,\n\t`source` text NOT NULL,\n\t`metadata` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\t`deleted_at` integer\n);",
|
|
1101
|
+
"CREATE TABLE `financial_transactions` (\n\t`id` integer PRIMARY KEY,\n\t`financial_account_id` integer NOT NULL,\n\t`direction` text NOT NULL,\n\t`amount` integer NOT NULL,\n\t`currency` text NOT NULL,\n\t`status` text NOT NULL,\n\t`statement_date` text NOT NULL,\n\t`booked_date` text,\n\t`description` text,\n\t`counterparty_name` text,\n\t`counterparty_type` text,\n\t`counterparty_id` integer,\n\t`reference` text,\n\t`payment_id` integer,\n\t`project_id` integer,\n\t`review` text NOT NULL,\n\t`author_type` text NOT NULL,\n\t`author_id` integer NOT NULL,\n\t`model` text,\n\t`confirmed_by_type` text,\n\t`confirmed_by_id` integer,\n\t`source` text NOT NULL,\n\t`metadata` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\t`deleted_at` integer,\n\tCONSTRAINT `fk_financial_transactions_financial_account_id_financial_accounts_id_fk` FOREIGN KEY (`financial_account_id`) REFERENCES `financial_accounts`(`id`),\n\tCONSTRAINT `fk_financial_transactions_payment_id_payments_id_fk` FOREIGN KEY (`payment_id`) REFERENCES `payments`(`id`),\n\tCONSTRAINT `fk_financial_transactions_project_id_projects_id_fk` FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`)\n);",
|
|
1102
|
+
"CREATE TABLE `order_lines` (\n\t`id` integer PRIMARY KEY,\n\t`order_id` integer NOT NULL,\n\t`position` integer NOT NULL,\n\t`description` text NOT NULL,\n\t`quantity` text NOT NULL,\n\t`unit_price` integer,\n\t`amount` integer NOT NULL,\n\t`catalog_item_id` integer,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\tCONSTRAINT `fk_order_lines_order_id_orders_id_fk` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`),\n\tCONSTRAINT `fk_order_lines_catalog_item_id_catalog_items_id_fk` FOREIGN KEY (`catalog_item_id`) REFERENCES `catalog_items`(`id`),\n\tCONSTRAINT `order_lines_order_id_position_unique` UNIQUE(`order_id`,`position`)\n);",
|
|
1103
|
+
"CREATE TABLE `subscription_prices` (\n\t`id` integer PRIMARY KEY,\n\t`subscription_id` integer NOT NULL,\n\t`amount` integer NOT NULL,\n\t`currency` text NOT NULL,\n\t`effective_from` text NOT NULL,\n\t`review` text NOT NULL,\n\t`author_type` text NOT NULL,\n\t`author_id` integer NOT NULL,\n\t`model` text,\n\t`confirmed_by_type` text,\n\t`confirmed_by_id` integer,\n\t`source` text NOT NULL,\n\t`metadata` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\tCONSTRAINT `fk_subscription_prices_subscription_id_subscriptions_id_fk` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions`(`id`)\n);",
|
|
1104
|
+
"CREATE TABLE `subscriptions` (\n\t`id` integer PRIMARY KEY,\n\t`name` text NOT NULL,\n\t`direction` text NOT NULL,\n\t`supplier_type` text NOT NULL,\n\t`supplier_id` integer NOT NULL,\n\t`customer_type` text NOT NULL,\n\t`customer_id` integer NOT NULL,\n\t`catalog_item_id` integer,\n\t`contract_id` integer,\n\t`project_id` integer,\n\t`status` text NOT NULL,\n\t`interval_count` integer NOT NULL,\n\t`interval_unit` text NOT NULL,\n\t`start_date` text NOT NULL,\n\t`trial_end_date` text,\n\t`next_renewal_date` text,\n\t`cancelled_date` text,\n\t`end_date` text,\n\t`review` text NOT NULL,\n\t`author_type` text NOT NULL,\n\t`author_id` integer NOT NULL,\n\t`model` text,\n\t`confirmed_by_type` text,\n\t`confirmed_by_id` integer,\n\t`source` text NOT NULL,\n\t`metadata` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\t`deleted_at` integer,\n\tCONSTRAINT `fk_subscriptions_catalog_item_id_catalog_items_id_fk` FOREIGN KEY (`catalog_item_id`) REFERENCES `catalog_items`(`id`),\n\tCONSTRAINT `fk_subscriptions_contract_id_contracts_id_fk` FOREIGN KEY (`contract_id`) REFERENCES `contracts`(`id`),\n\tCONSTRAINT `fk_subscriptions_project_id_projects_id_fk` FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`)\n);",
|
|
1105
|
+
"CREATE TABLE `transaction_splits` (\n\t`id` integer PRIMARY KEY,\n\t`transaction_id` integer NOT NULL,\n\t`position` integer NOT NULL,\n\t`category` text NOT NULL,\n\t`category_folded` text NOT NULL,\n\t`amount` integer NOT NULL,\n\t`project_id` integer,\n\t`note` text,\n\t`created_at` integer NOT NULL,\n\t`updated_at` integer NOT NULL,\n\tCONSTRAINT `fk_transaction_splits_transaction_id_financial_transactions_id_fk` FOREIGN KEY (`transaction_id`) REFERENCES `financial_transactions`(`id`),\n\tCONSTRAINT `fk_transaction_splits_project_id_projects_id_fk` FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`),\n\tCONSTRAINT `transaction_splits_transaction_id_position_unique` UNIQUE(`transaction_id`,`position`)\n);",
|
|
1106
|
+
"ALTER TABLE `invoice_lines` ADD `catalog_item_id` integer REFERENCES catalog_items(id);",
|
|
1107
|
+
"ALTER TABLE `invoices` ADD `contract_id` integer REFERENCES contracts(id);",
|
|
1108
|
+
"ALTER TABLE `invoices` ADD `subscription_id` integer REFERENCES subscriptions(id);",
|
|
1109
|
+
"ALTER TABLE `orders` ADD `contract_id` integer REFERENCES contracts(id);",
|
|
1110
|
+
"ALTER TABLE `orders` ADD `deal_id` integer REFERENCES deals(id);",
|
|
1111
|
+
"ALTER TABLE `orders` ADD `direction` text DEFAULT 'purchase' NOT NULL;",
|
|
1112
|
+
"ALTER TABLE `payments` ADD `refund_of` integer REFERENCES payments(id);",
|
|
1113
|
+
"ALTER TABLE `payments` ADD `subscription_id` integer REFERENCES subscriptions(id);",
|
|
1114
|
+
"ALTER TABLE `record_relations` ADD `as_role` text;",
|
|
1115
|
+
"ALTER TABLE `records` ADD `created_entity` integer DEFAULT 0 NOT NULL;",
|
|
1116
|
+
"CREATE UNIQUE INDEX `catalog_items_by_supplier_type_supplier_id_sku` ON `catalog_items` (`supplier_type`,`supplier_id`,`sku`) WHERE supplier_id IS NOT NULL AND sku IS NOT NULL AND deleted_at IS NULL;",
|
|
1117
|
+
"CREATE UNIQUE INDEX `catalog_items_by_sku` ON `catalog_items` (`sku`) WHERE supplier_id IS NULL AND sku IS NOT NULL AND deleted_at IS NULL;",
|
|
1118
|
+
"CREATE INDEX `catalog_items_by_name_folded` ON `catalog_items` (`name_folded`);",
|
|
1119
|
+
"CREATE INDEX `catalog_items_by_author_type_author_id` ON `catalog_items` (`author_type`,`author_id`);",
|
|
1120
|
+
"CREATE INDEX `contracts_by_project_id` ON `contracts` (`project_id`);",
|
|
1121
|
+
"CREATE INDEX `contracts_by_deal_id` ON `contracts` (`deal_id`);",
|
|
1122
|
+
"CREATE INDEX `contracts_by_author_type_author_id` ON `contracts` (`author_type`,`author_id`);",
|
|
1123
|
+
"CREATE INDEX `contracts_by_confirmed_by_type_confirmed_by_id` ON `contracts` (`confirmed_by_type`,`confirmed_by_id`);",
|
|
1124
|
+
"CREATE INDEX `financial_accounts_by_name_folded` ON `financial_accounts` (`name_folded`);",
|
|
1125
|
+
"CREATE INDEX `financial_accounts_by_holder_type_holder_id` ON `financial_accounts` (`holder_type`,`holder_id`);",
|
|
1126
|
+
"CREATE INDEX `financial_accounts_by_author_type_author_id` ON `financial_accounts` (`author_type`,`author_id`);",
|
|
1127
|
+
"CREATE INDEX `financial_transactions_by_financial_account_id_booked_date` ON `financial_transactions` (`financial_account_id`,`booked_date`);",
|
|
1128
|
+
"CREATE UNIQUE INDEX `financial_transactions_by_payment_id` ON `financial_transactions` (`payment_id`) WHERE payment_id IS NOT NULL AND deleted_at IS NULL;",
|
|
1129
|
+
"CREATE INDEX `financial_transactions_by_counterparty_type_counterparty_id` ON `financial_transactions` (`counterparty_type`,`counterparty_id`);",
|
|
1130
|
+
"CREATE INDEX `financial_transactions_by_project_id` ON `financial_transactions` (`project_id`);",
|
|
1131
|
+
"CREATE INDEX `financial_transactions_by_author_type_author_id` ON `financial_transactions` (`author_type`,`author_id`);",
|
|
1132
|
+
"CREATE INDEX `financial_transactions_by_confirmed_by_type_confirmed_by_id` ON `financial_transactions` (`confirmed_by_type`,`confirmed_by_id`);",
|
|
1133
|
+
"CREATE INDEX `identities_by_phone` ON `identities` (`phone`) WHERE phone IS NOT NULL;",
|
|
1134
|
+
"CREATE INDEX `invoice_lines_by_catalog_item_id` ON `invoice_lines` (`catalog_item_id`);",
|
|
1135
|
+
"CREATE INDEX `invoices_by_contract_id` ON `invoices` (`contract_id`);",
|
|
1136
|
+
"CREATE INDEX `invoices_by_subscription_id` ON `invoices` (`subscription_id`);",
|
|
1137
|
+
"CREATE INDEX `order_lines_by_catalog_item_id` ON `order_lines` (`catalog_item_id`);",
|
|
1138
|
+
"CREATE INDEX `orders_by_contract_id` ON `orders` (`contract_id`);",
|
|
1139
|
+
"CREATE INDEX `orders_by_deal_id` ON `orders` (`deal_id`);",
|
|
1140
|
+
"CREATE INDEX `payments_by_refund_of` ON `payments` (`refund_of`);",
|
|
1141
|
+
"CREATE INDEX `payments_by_subscription_id` ON `payments` (`subscription_id`);",
|
|
1142
|
+
"CREATE INDEX `records_by_account_id_batch_id` ON `records` (`account_id`,`batch_id`);",
|
|
1143
|
+
"CREATE UNIQUE INDEX `subscription_prices_by_subscription_id_effective_from` ON `subscription_prices` (`subscription_id`,`effective_from`) WHERE review <> 'rejected';",
|
|
1144
|
+
"CREATE INDEX `subscription_prices_by_author_type_author_id` ON `subscription_prices` (`author_type`,`author_id`);",
|
|
1145
|
+
"CREATE INDEX `subscription_prices_by_confirmed_by_type_confirmed_by_id` ON `subscription_prices` (`confirmed_by_type`,`confirmed_by_id`);",
|
|
1146
|
+
"CREATE INDEX `subscriptions_by_supplier_type_supplier_id` ON `subscriptions` (`supplier_type`,`supplier_id`);",
|
|
1147
|
+
"CREATE INDEX `subscriptions_by_customer_type_customer_id` ON `subscriptions` (`customer_type`,`customer_id`);",
|
|
1148
|
+
"CREATE INDEX `subscriptions_by_catalog_item_id` ON `subscriptions` (`catalog_item_id`);",
|
|
1149
|
+
"CREATE INDEX `subscriptions_by_contract_id` ON `subscriptions` (`contract_id`);",
|
|
1150
|
+
"CREATE INDEX `subscriptions_by_project_id` ON `subscriptions` (`project_id`);",
|
|
1151
|
+
"CREATE INDEX `subscriptions_by_author_type_author_id` ON `subscriptions` (`author_type`,`author_id`);",
|
|
1152
|
+
"CREATE INDEX `subscriptions_by_confirmed_by_type_confirmed_by_id` ON `subscriptions` (`confirmed_by_type`,`confirmed_by_id`);",
|
|
1153
|
+
"CREATE INDEX `transaction_splits_by_category_folded` ON `transaction_splits` (`category_folded`);",
|
|
1154
|
+
"CREATE INDEX `transaction_splits_by_project_id` ON `transaction_splits` (`project_id`);"
|
|
1155
|
+
]
|
|
1156
|
+
},
|
|
1157
|
+
{
|
|
1158
|
+
"name": "20261108010754_version-57-business-rules",
|
|
1159
|
+
"statements": [
|
|
1160
|
+
"-- The store's code checks these first and says why; the triggers keep a stray write from another build or a hand\n-- edit from storing business data that does not add up. Paid is net: a confirmed refund's allocation counts against\n-- the payment it refunds, so an invoice refunded in full is owed again.\nCREATE TRIGGER catalog_items_valid_bi BEFORE INSERT ON catalog_items\n WHEN new.kind NOT IN ('product', 'service') OR new.unit_price < 0\n OR (new.unit_price IS NULL) <> (new.currency IS NULL) BEGIN\n SELECT RAISE(ABORT, 'catalog_items: kind product or service, a price of 0 or more with its currency');\nEND;",
|
|
1161
|
+
"CREATE TRIGGER catalog_items_valid_bu BEFORE UPDATE ON catalog_items\n WHEN new.kind NOT IN ('product', 'service') OR new.unit_price < 0\n OR (new.unit_price IS NULL) <> (new.currency IS NULL) BEGIN\n SELECT RAISE(ABORT, 'catalog_items: kind product or service, a price of 0 or more with its currency');\nEND;",
|
|
1162
|
+
"CREATE TRIGGER order_lines_valid_bi BEFORE INSERT ON order_lines\n WHEN new.amount < 0 OR new.unit_price < 0 BEGIN\n SELECT RAISE(ABORT, 'order_lines: an amount and a unit price of 0 or more');\nEND;",
|
|
1163
|
+
"CREATE TRIGGER order_lines_valid_bu BEFORE UPDATE ON order_lines\n WHEN new.amount < 0 OR new.unit_price < 0 OR new.order_id IS NOT old.order_id BEGIN\n SELECT RAISE(ABORT, 'order_lines: an amount and a unit price of 0 or more, on the same order');\nEND;",
|
|
1164
|
+
"DROP TRIGGER orders_valid_bi;",
|
|
1165
|
+
"CREATE TRIGGER orders_valid_bi BEFORE INSERT ON orders\n WHEN new.customer_type NOT IN ('person', 'organization')\n OR new.purpose NOT IN ('personal', 'business', 'client', 'other')\n OR new.status NOT IN ('placed', 'shipped', 'delivered', 'cancelled', 'returned')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR new.direction NOT IN ('purchase', 'sale')\n OR (new.total IS NULL) <> (new.currency IS NULL) OR new.total < 0 BEGIN\n SELECT RAISE(ABORT, 'orders: a person or organization customer, a known purpose, status, review and direction, an amount with its currency');\nEND;",
|
|
1166
|
+
"DROP TRIGGER orders_valid_bu;",
|
|
1167
|
+
"-- A line is written before the order's total moves to the new sum, so the sum is checked when the order changes.\nCREATE TRIGGER orders_valid_bu BEFORE UPDATE ON orders\n WHEN new.customer_type NOT IN ('person', 'organization')\n OR new.purpose NOT IN ('personal', 'business', 'client', 'other')\n OR new.status NOT IN ('placed', 'shipped', 'delivered', 'cancelled', 'returned')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR new.direction NOT IN ('purchase', 'sale')\n OR (new.total IS NULL) <> (new.currency IS NULL) OR new.total < 0\n OR (EXISTS (SELECT 1 FROM order_lines WHERE order_id = new.id)\n AND (new.currency IS NULL OR new.total IS NOT (SELECT sum(amount) FROM order_lines WHERE order_id = new.id))) BEGIN\n SELECT RAISE(ABORT, 'orders: a known customer, purpose, status, review and direction; with lines, a currency and their sum as the total');\nEND;",
|
|
1168
|
+
"CREATE TRIGGER contracts_valid_bi BEFORE INSERT ON contracts\n WHEN new.kind NOT IN ('sale', 'purchase', 'service', 'lease', 'employment', 'nda', 'license', 'other')\n OR new.status NOT IN ('draft', 'active', 'ended', 'terminated')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected') OR new.auto_renew NOT IN (0, 1)\n OR (new.renewal_count IS NULL) <> (new.renewal_unit IS NULL) OR new.renewal_count < 1\n OR new.renewal_unit NOT IN ('day', 'week', 'month', 'year') OR new.notice_days < 0\n OR new.effective_date > new.expiration_date\n OR (new.value IS NULL) <> (new.currency IS NULL) OR new.value < 0 BEGIN\n SELECT RAISE(ABORT, 'contracts: a known kind, status and review, dates in order, a renewal and a value each complete');\nEND;",
|
|
1169
|
+
"CREATE TRIGGER contracts_valid_bu BEFORE UPDATE ON contracts\n WHEN new.kind NOT IN ('sale', 'purchase', 'service', 'lease', 'employment', 'nda', 'license', 'other')\n OR new.status NOT IN ('draft', 'active', 'ended', 'terminated')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected') OR new.auto_renew NOT IN (0, 1)\n OR (new.renewal_count IS NULL) <> (new.renewal_unit IS NULL) OR new.renewal_count < 1\n OR new.renewal_unit NOT IN ('day', 'week', 'month', 'year') OR new.notice_days < 0\n OR new.effective_date > new.expiration_date\n OR (new.value IS NULL) <> (new.currency IS NULL) OR new.value < 0 BEGIN\n SELECT RAISE(ABORT, 'contracts: a known kind, status and review, dates in order, a renewal and a value each complete');\nEND;",
|
|
1170
|
+
"CREATE TRIGGER subscriptions_valid_bi BEFORE INSERT ON subscriptions\n WHEN new.direction NOT IN ('receivable', 'payable')\n OR new.supplier_type NOT IN ('person', 'organization') OR new.customer_type NOT IN ('person', 'organization')\n OR new.status NOT IN ('trial', 'active', 'paused', 'cancelled', 'ended')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR new.interval_count < 1 OR new.interval_unit NOT IN ('day', 'week', 'month', 'year')\n OR new.trial_end_date < new.start_date OR new.end_date < new.start_date\n OR (new.status = 'trial' AND new.trial_end_date IS NULL)\n OR (new.status = 'cancelled' AND new.cancelled_date IS NULL) BEGIN\n SELECT RAISE(ABORT, 'subscriptions: a known direction, parties, status and review, an interval, dates in order');\nEND;",
|
|
1171
|
+
"CREATE TRIGGER subscriptions_valid_bu BEFORE UPDATE ON subscriptions\n WHEN new.direction NOT IN ('receivable', 'payable')\n OR new.supplier_type NOT IN ('person', 'organization') OR new.customer_type NOT IN ('person', 'organization')\n OR new.status NOT IN ('trial', 'active', 'paused', 'cancelled', 'ended')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR new.interval_count < 1 OR new.interval_unit NOT IN ('day', 'week', 'month', 'year')\n OR new.trial_end_date < new.start_date OR new.end_date < new.start_date\n OR (new.status = 'trial' AND new.trial_end_date IS NULL)\n OR (new.status = 'cancelled' AND new.cancelled_date IS NULL) BEGIN\n SELECT RAISE(ABORT, 'subscriptions: a known direction, parties, status and review, an interval, dates in order');\nEND;",
|
|
1172
|
+
"CREATE TRIGGER subscription_prices_valid_bi BEFORE INSERT ON subscription_prices\n WHEN new.amount < 0 OR new.review NOT IN ('proposed', 'confirmed', 'rejected') BEGIN\n SELECT RAISE(ABORT, 'subscription_prices: an amount of 0 or more and a known review');\nEND;",
|
|
1173
|
+
"-- A price is history: a correction is a rejection and a new row, so only its decision changes.\nCREATE TRIGGER subscription_prices_fixed_bu BEFORE UPDATE ON subscription_prices\n WHEN new.amount IS NOT old.amount OR new.currency IS NOT old.currency\n OR new.effective_from IS NOT old.effective_from OR new.subscription_id IS NOT old.subscription_id\n OR new.author_type IS NOT old.author_type OR new.author_id IS NOT old.author_id\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR (old.review = 'confirmed' AND new.review <> 'confirmed') BEGIN\n SELECT RAISE(ABORT, 'subscription_prices: a price is never changed; reject it and add a new one');\nEND;",
|
|
1174
|
+
"CREATE TRIGGER subscription_prices_kept_bd BEFORE DELETE ON subscription_prices BEGIN\n SELECT RAISE(ABORT, 'subscription_prices: a price is history and is never deleted');\nEND;",
|
|
1175
|
+
"CREATE TRIGGER financial_accounts_valid_bi BEFORE INSERT ON financial_accounts\n WHEN new.kind NOT IN ('bank', 'card', 'cash', 'wallet', 'other')\n OR (new.opening_balance IS NULL) <> (new.opening_date IS NULL) OR length(new.number_hint) > 4\n OR new.holder_type NOT IN ('person', 'organization') BEGIN\n SELECT RAISE(ABORT, 'financial_accounts: a known kind, an opening balance with its date, at most 4 characters of the number');\nEND;",
|
|
1176
|
+
"CREATE TRIGGER financial_accounts_valid_bu BEFORE UPDATE ON financial_accounts\n WHEN new.kind NOT IN ('bank', 'card', 'cash', 'wallet', 'other')\n OR (new.opening_balance IS NULL) <> (new.opening_date IS NULL) OR length(new.number_hint) > 4\n OR new.holder_type NOT IN ('person', 'organization')\n OR (new.currency IS NOT old.currency\n AND EXISTS (SELECT 1 FROM financial_transactions WHERE financial_account_id = new.id)) BEGIN\n SELECT RAISE(ABORT, 'financial_accounts: a known kind, an opening balance with its date, the currency kept once it has transactions');\nEND;",
|
|
1177
|
+
"CREATE TRIGGER financial_transactions_valid_bi BEFORE INSERT ON financial_transactions\n WHEN new.direction NOT IN ('in', 'out') OR new.amount <= 0 OR new.status NOT IN ('pending', 'booked')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR new.currency IS NOT (SELECT currency FROM financial_accounts WHERE id = new.financial_account_id)\n OR (new.status = 'booked') <> (new.booked_date IS NOT NULL)\n OR (new.payment_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM payments p WHERE p.id = new.payment_id\n AND p.currency = new.currency AND p.direction = new.direction AND p.amount = new.amount)) BEGIN\n SELECT RAISE(ABORT, 'financial_transactions: a known direction, status and review, above 0 in its account''s currency, booked with a date, its payment of the same amount');\nEND;",
|
|
1178
|
+
"CREATE TRIGGER financial_transactions_valid_bu BEFORE UPDATE ON financial_transactions\n WHEN new.direction NOT IN ('in', 'out') OR new.amount <= 0 OR new.status NOT IN ('pending', 'booked')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR new.currency IS NOT (SELECT currency FROM financial_accounts WHERE id = new.financial_account_id)\n OR (new.status = 'booked') <> (new.booked_date IS NOT NULL)\n OR (new.payment_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM payments p WHERE p.id = new.payment_id\n AND p.currency = new.currency AND p.direction = new.direction AND p.amount = new.amount))\n OR new.amount < (SELECT coalesce(sum(amount), 0) FROM transaction_splits WHERE transaction_id = new.id) BEGIN\n SELECT RAISE(ABORT, 'financial_transactions: a known direction, status and review, booked with a date, its payment of the same amount, at or above its splits');\nEND;",
|
|
1179
|
+
"CREATE TRIGGER transaction_splits_valid_bi BEFORE INSERT ON transaction_splits\n WHEN new.amount <= 0\n OR (SELECT coalesce(sum(amount), 0) FROM transaction_splits WHERE transaction_id = new.transaction_id) + new.amount\n > (SELECT amount FROM financial_transactions WHERE id = new.transaction_id) BEGIN\n SELECT RAISE(ABORT, 'transaction_splits: above 0, and the splits within the transaction''s amount');\nEND;",
|
|
1180
|
+
"CREATE TRIGGER transaction_splits_valid_bu BEFORE UPDATE ON transaction_splits\n WHEN new.amount <= 0 OR new.transaction_id IS NOT old.transaction_id\n OR (SELECT coalesce(sum(amount), 0) FROM transaction_splits WHERE transaction_id = new.transaction_id AND id <> old.id)\n + new.amount > (SELECT amount FROM financial_transactions WHERE id = new.transaction_id) BEGIN\n SELECT RAISE(ABORT, 'transaction_splits: above 0, on the same transaction, and the splits within its amount');\nEND;",
|
|
1181
|
+
"CREATE TRIGGER payments_refund_bi BEFORE INSERT ON payments\n WHEN new.refund_of IS NOT NULL AND (\n NOT EXISTS (SELECT 1 FROM payments o WHERE o.id = new.refund_of AND o.refund_of IS NULL\n AND o.direction <> new.direction AND o.currency = new.currency AND o.review <> 'rejected' AND o.deleted_at IS NULL)\n OR (SELECT coalesce(sum(r.amount), 0) FROM payments r WHERE r.refund_of = new.refund_of\n AND r.review = 'confirmed' AND r.deleted_at IS NULL) + new.amount\n > (SELECT amount FROM payments WHERE id = new.refund_of)) BEGIN\n SELECT RAISE(ABORT, 'payments: a refund goes the other way in the same currency, of a live payment that is not a refund, within its amount');\nEND;",
|
|
1182
|
+
"CREATE TRIGGER payments_refund_bu BEFORE UPDATE ON payments\n WHEN new.refund_of IS NOT old.refund_of\n OR (new.refund_of IS NOT NULL AND new.review <> 'rejected' AND new.deleted_at IS NULL AND (\n NOT EXISTS (SELECT 1 FROM payments o WHERE o.id = new.refund_of AND o.refund_of IS NULL\n AND o.direction <> new.direction AND o.currency = new.currency AND o.review <> 'rejected' AND o.deleted_at IS NULL)\n OR (SELECT coalesce(sum(r.amount), 0) FROM payments r WHERE r.refund_of = new.refund_of AND r.id <> new.id\n AND r.review = 'confirmed' AND r.deleted_at IS NULL) + new.amount\n > (SELECT amount FROM payments WHERE id = new.refund_of)))\n OR ((new.review = 'rejected' OR new.deleted_at IS NOT NULL) AND old.review <> 'rejected' AND old.deleted_at IS NULL\n AND EXISTS (SELECT 1 FROM payments r WHERE r.refund_of = new.id AND r.review <> 'rejected' AND r.deleted_at IS NULL))\n OR (new.refund_of IS NULL AND new.amount < (SELECT coalesce(sum(r.amount), 0) FROM payments r\n WHERE r.refund_of = new.id AND r.review = 'confirmed' AND r.deleted_at IS NULL)) BEGIN\n SELECT RAISE(ABORT, 'payments: a refund keeps its payment, goes the other way and stays within it; a refunded payment is not rejected or deleted');\nEND;",
|
|
1183
|
+
"-- A confirmed refund that stops counting gives its shares back to their invoices: never beyond a total.\nCREATE TRIGGER payments_refund_leaves_bu BEFORE UPDATE ON payments\n WHEN new.refund_of IS NOT NULL AND old.review = 'confirmed' AND old.deleted_at IS NULL\n AND (new.review <> 'confirmed' OR new.deleted_at IS NOT NULL)\n AND EXISTS (SELECT 1 FROM payment_allocations a JOIN invoices i ON i.id = a.invoice_id WHERE a.payment_id = new.id\n AND (SELECT coalesce(sum(CASE WHEN np.refund_of IS NULL THEN na.amount ELSE -na.amount END), 0) FROM payment_allocations na JOIN payments np ON np.id = na.payment_id WHERE na.invoice_id = i.id AND np.review = 'confirmed' AND np.deleted_at IS NULL) + a.amount > i.total) BEGIN\n SELECT RAISE(ABORT, 'payments: a refund leaving the confirmed ones would pay an invoice beyond its total');\nEND;",
|
|
1184
|
+
"CREATE TRIGGER payments_linked_bu BEFORE UPDATE ON payments\n WHEN (new.amount IS NOT old.amount OR new.currency IS NOT old.currency OR new.direction IS NOT old.direction\n OR (new.review = 'rejected' AND old.review <> 'rejected') OR (new.deleted_at IS NOT NULL AND old.deleted_at IS NULL))\n AND EXISTS (SELECT 1 FROM financial_transactions WHERE payment_id = new.id AND deleted_at IS NULL) BEGIN\n SELECT RAISE(ABORT, 'payments: a transaction records this payment; take the link back first');\nEND;",
|
|
1185
|
+
"DROP TRIGGER invoices_valid_bu;",
|
|
1186
|
+
"CREATE TRIGGER invoices_valid_bu BEFORE UPDATE ON invoices\n WHEN new.direction NOT IN ('receivable', 'payable') OR new.total < 0\n OR new.status NOT IN ('draft', 'issued', 'partly_paid', 'paid', 'void')\n OR new.review NOT IN ('proposed', 'confirmed', 'rejected')\n OR (new.currency IS NOT old.currency AND EXISTS (SELECT 1 FROM payment_allocations WHERE invoice_id = new.id))\n OR new.total < (SELECT coalesce(sum(CASE WHEN p.refund_of IS NULL THEN a.amount ELSE -a.amount END), 0)\n FROM payment_allocations a JOIN payments p ON p.id = a.payment_id\n WHERE a.invoice_id = new.id AND p.review = 'confirmed' AND p.deleted_at IS NULL) BEGIN\n SELECT RAISE(ABORT, 'invoices: the total stays at or above what is paid on it, in the same currency');\nEND;",
|
|
1187
|
+
"DROP TRIGGER payments_confirm_bu;",
|
|
1188
|
+
"CREATE TRIGGER payments_confirm_bu BEFORE UPDATE OF review ON payments\n WHEN new.review = 'confirmed' AND old.review IS NOT 'confirmed' AND (\n (new.refund_of IS NULL AND EXISTS (\n SELECT 1 FROM payment_allocations a JOIN invoices i ON i.id = a.invoice_id WHERE a.payment_id = new.id\n AND a.amount + (SELECT coalesce(sum(CASE WHEN p.refund_of IS NULL THEN o.amount ELSE -o.amount END), 0)\n FROM payment_allocations o JOIN payments p ON p.id = o.payment_id\n WHERE o.invoice_id = a.invoice_id AND o.payment_id <> new.id AND p.review = 'confirmed' AND p.deleted_at IS NULL)\n > i.total))\n OR (new.refund_of IS NOT NULL AND NOT EXISTS (\n SELECT 1 FROM payments o WHERE o.id = new.refund_of AND o.review = 'confirmed' AND o.deleted_at IS NULL))\n OR (new.refund_of IS NOT NULL AND EXISTS (\n SELECT 1 FROM payment_allocations a WHERE a.payment_id = new.id\n AND a.amount + (SELECT coalesce(sum(o.amount), 0) FROM payment_allocations o JOIN payments r ON r.id = o.payment_id\n WHERE o.invoice_id = a.invoice_id AND r.refund_of = new.refund_of AND r.id <> new.id\n AND r.review = 'confirmed' AND r.deleted_at IS NULL)\n > coalesce((SELECT amount FROM payment_allocations WHERE payment_id = new.refund_of AND invoice_id = a.invoice_id), 0)))) BEGIN\n SELECT RAISE(ABORT, 'payments: confirming it would settle more than an invoice''s total, or take back more than its payment gave, or come before its payment');\nEND;",
|
|
1189
|
+
"DROP TRIGGER payment_allocations_valid_bi;",
|
|
1190
|
+
"-- An ordinary payment counts against what the invoice still owes; a refund's share counts against what the payment\n-- it refunds gave that invoice, so net paid never goes below 0.\nCREATE TRIGGER payment_allocations_valid_bi BEFORE INSERT ON payment_allocations\n WHEN new.amount <= 0\n OR (SELECT currency FROM payments WHERE id = new.payment_id)\n IS NOT (SELECT currency FROM invoices WHERE id = new.invoice_id)\n OR (SELECT coalesce(sum(amount), 0) FROM payment_allocations WHERE payment_id = new.payment_id) + new.amount\n > (SELECT amount FROM payments WHERE id = new.payment_id)\n OR ((SELECT refund_of FROM payments WHERE id = new.payment_id) IS NULL\n AND (SELECT coalesce(sum(CASE WHEN p.refund_of IS NULL THEN a.amount ELSE -a.amount END), 0)\n FROM payment_allocations a JOIN payments p ON p.id = a.payment_id WHERE a.invoice_id = new.invoice_id\n AND ((p.review = 'confirmed' AND p.deleted_at IS NULL) OR a.payment_id = new.payment_id)) + new.amount\n > (SELECT total FROM invoices WHERE id = new.invoice_id))\n OR ((SELECT refund_of FROM payments WHERE id = new.payment_id) IS NOT NULL\n AND (SELECT coalesce(sum(a.amount), 0) FROM payment_allocations a JOIN payments r ON r.id = a.payment_id\n WHERE a.invoice_id = new.invoice_id AND r.refund_of = (SELECT refund_of FROM payments WHERE id = new.payment_id)\n AND ((r.review = 'confirmed' AND r.deleted_at IS NULL) OR a.payment_id = new.payment_id)) + new.amount\n > coalesce((SELECT amount FROM payment_allocations WHERE invoice_id = new.invoice_id\n AND payment_id = (SELECT refund_of FROM payments WHERE id = new.payment_id)), 0)) BEGIN\n SELECT RAISE(ABORT, 'payment_allocations: above 0, one currency, within the payment and within the invoice or the refunded share');\nEND;",
|
|
1191
|
+
"DROP TRIGGER payment_allocations_valid_bu;",
|
|
1192
|
+
"CREATE TRIGGER payment_allocations_valid_bu BEFORE UPDATE ON payment_allocations\n WHEN new.amount <= 0 OR new.payment_id IS NOT old.payment_id OR new.invoice_id IS NOT old.invoice_id\n OR (SELECT coalesce(sum(amount), 0) FROM payment_allocations WHERE payment_id = new.payment_id AND id <> old.id)\n + new.amount > (SELECT amount FROM payments WHERE id = new.payment_id)\n OR ((SELECT refund_of FROM payments WHERE id = new.payment_id) IS NULL\n AND (SELECT coalesce(sum(CASE WHEN p.refund_of IS NULL THEN a.amount ELSE -a.amount END), 0)\n FROM payment_allocations a JOIN payments p ON p.id = a.payment_id WHERE a.invoice_id = new.invoice_id\n AND a.id <> old.id AND ((p.review = 'confirmed' AND p.deleted_at IS NULL) OR a.payment_id = new.payment_id))\n + new.amount > (SELECT total FROM invoices WHERE id = new.invoice_id))\n OR ((SELECT refund_of FROM payments WHERE id = new.payment_id) IS NOT NULL\n AND (SELECT coalesce(sum(a.amount), 0) FROM payment_allocations a JOIN payments r ON r.id = a.payment_id\n WHERE a.invoice_id = new.invoice_id AND a.id <> old.id\n AND r.refund_of = (SELECT refund_of FROM payments WHERE id = new.payment_id)\n AND ((r.review = 'confirmed' AND r.deleted_at IS NULL) OR a.payment_id = new.payment_id)) + new.amount\n > coalesce((SELECT amount FROM payment_allocations WHERE invoice_id = new.invoice_id\n AND payment_id = (SELECT refund_of FROM payments WHERE id = new.payment_id)), 0))\n OR ((SELECT refund_of FROM payments WHERE id = new.payment_id) IS NULL\n AND new.amount < (SELECT coalesce(sum(a.amount), 0) FROM payment_allocations a JOIN payments r ON r.id = a.payment_id\n WHERE r.refund_of = new.payment_id AND a.invoice_id = new.invoice_id))\n OR (new.amount < old.amount\n AND (SELECT refund_of IS NOT NULL AND review = 'confirmed' AND deleted_at IS NULL FROM payments WHERE id = new.payment_id)\n AND (SELECT coalesce(sum(CASE WHEN np.refund_of IS NULL THEN na.amount ELSE -na.amount END), 0)\n FROM payment_allocations na JOIN payments np ON np.id = na.payment_id\n WHERE na.invoice_id = new.invoice_id AND np.review = 'confirmed' AND np.deleted_at IS NULL)\n + old.amount - new.amount > (SELECT total FROM invoices WHERE id = new.invoice_id)) BEGIN\n SELECT RAISE(ABORT, 'payment_allocations: above 0, within the payment, the invoice or the refunded share, at or above its refunds, sides unchanged');\nEND;",
|
|
1193
|
+
"CREATE TRIGGER payment_allocations_refunded_bd BEFORE DELETE ON payment_allocations\n WHEN (SELECT review FROM invoices WHERE id = old.invoice_id) <> 'rejected' AND EXISTS (SELECT 1 FROM payment_allocations a JOIN payments r ON r.id = a.payment_id\n WHERE r.refund_of = old.payment_id AND a.invoice_id = old.invoice_id) BEGIN\n SELECT RAISE(ABORT, 'payment_allocations: a refund of this payment is allocated to the invoice; take it back first');\nEND;",
|
|
1194
|
+
"CREATE TRIGGER payment_allocations_refund_bd BEFORE DELETE ON payment_allocations\n WHEN (SELECT refund_of IS NOT NULL AND review = 'confirmed' AND deleted_at IS NULL FROM payments WHERE id = old.payment_id)\n AND (SELECT review FROM invoices WHERE id = old.invoice_id) <> 'rejected'\n AND (SELECT coalesce(sum(CASE WHEN np.refund_of IS NULL THEN na.amount ELSE -na.amount END), 0) FROM payment_allocations na JOIN payments np ON np.id = na.payment_id WHERE na.invoice_id = old.invoice_id AND np.review = 'confirmed' AND np.deleted_at IS NULL) + old.amount\n > (SELECT total FROM invoices WHERE id = old.invoice_id) BEGIN\n SELECT RAISE(ABORT, 'payment_allocations: taking a refund''s share back would pay the invoice beyond its total');\nEND;",
|
|
1195
|
+
"-- A void invoice keeps what was settled on it; a rejected one lets its allocations go, as reject sets review first.\nCREATE TRIGGER payment_allocations_void_bd BEFORE DELETE ON payment_allocations\n WHEN (SELECT status = 'void' AND review <> 'rejected' FROM invoices WHERE id = old.invoice_id) BEGIN\n SELECT RAISE(ABORT, 'payment_allocations: a void invoice keeps its allocations');\nEND;"
|
|
1196
|
+
]
|
|
1094
1197
|
}
|
|
1095
1198
|
];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { StoreContext } from "./open.js";
|
|
2
|
+
export interface MoneyLineInput {
|
|
3
|
+
/** Taken from the product when left out. */
|
|
4
|
+
description?: string;
|
|
5
|
+
/** A decimal string; `1` when not given. */
|
|
6
|
+
quantity?: string;
|
|
7
|
+
/** Decimal strings in the parent's currency. Give either or both: both must agree, quantity × unit price. */
|
|
8
|
+
unitPrice?: string;
|
|
9
|
+
amount?: string;
|
|
10
|
+
/** `product:<id>`: its name and price are copied onto the line, and a later price change leaves the line as it is. */
|
|
11
|
+
product?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface MoneyLine {
|
|
14
|
+
position: number;
|
|
15
|
+
description: string;
|
|
16
|
+
quantity: string;
|
|
17
|
+
unitPrice: string | null;
|
|
18
|
+
amount: string;
|
|
19
|
+
product: string | null;
|
|
20
|
+
}
|
|
21
|
+
export interface PricedLine {
|
|
22
|
+
description: string;
|
|
23
|
+
quantity: string;
|
|
24
|
+
unitPrice: number | null;
|
|
25
|
+
amount: number;
|
|
26
|
+
productId: number | null;
|
|
27
|
+
}
|
|
28
|
+
/** Where each parent keeps its lines, and the prefix of the reasons its line errors carry. */
|
|
29
|
+
declare const PARENTS: {
|
|
30
|
+
readonly invoice: {
|
|
31
|
+
readonly table: "invoice_lines";
|
|
32
|
+
readonly column: "invoice_id";
|
|
33
|
+
readonly reasons: "finance";
|
|
34
|
+
};
|
|
35
|
+
readonly order: {
|
|
36
|
+
readonly table: "order_lines";
|
|
37
|
+
readonly column: "order_id";
|
|
38
|
+
readonly reasons: "order";
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export type LineParent = keyof typeof PARENTS;
|
|
42
|
+
/** Invoice and order lines: quantity × unit price, in the parent's currency, a product's name and price copied. */
|
|
43
|
+
export declare const moneyLinesOver: ({ database }: StoreContext) => {
|
|
44
|
+
linesOf: (parent: LineParent, lines: MoneyLineInput[], currency: string) => PricedLine[];
|
|
45
|
+
insertLines: (parent: LineParent, id: number, lines: PricedLine[], from: number, at: number) => void;
|
|
46
|
+
shownLines: (parent: LineParent, id: number, currency: string) => MoneyLine[];
|
|
47
|
+
};
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { formatDecimal, lineUnits, minorUnits, moneyLabel, quantityOf, scaleOf } from "../../domain/money.js";
|
|
2
|
+
import { requiredThing } from "./notes.js";
|
|
3
|
+
import { refuse, text } from "./reviewed.js";
|
|
4
|
+
/** Where each parent keeps its lines, and the prefix of the reasons its line errors carry. */
|
|
5
|
+
const PARENTS = {
|
|
6
|
+
invoice: { table: "invoice_lines", column: "invoice_id", reasons: "finance" },
|
|
7
|
+
order: { table: "order_lines", column: "order_id", reasons: "order" },
|
|
8
|
+
};
|
|
9
|
+
/** Invoice and order lines: quantity × unit price, in the parent's currency, a product's name and price copied. */
|
|
10
|
+
export const moneyLinesOver = ({ database }) => {
|
|
11
|
+
const productOf = (reference, what, code) => {
|
|
12
|
+
const thing = requiredThing(database, reference, `${what}'s product`);
|
|
13
|
+
if (thing.type !== "product")
|
|
14
|
+
return refuse(`${what}'s product is a product: reference, not ${reference}`, code("lines"));
|
|
15
|
+
const row = database.prepare("SELECT * FROM catalog_items WHERE id = ?").get(thing.id);
|
|
16
|
+
if (row.deleted_at != null)
|
|
17
|
+
refuse(`product:${row.id} is deleted`, code("lines"));
|
|
18
|
+
return row;
|
|
19
|
+
};
|
|
20
|
+
const priced = (parent, line, at, currency) => {
|
|
21
|
+
const code = (name) => `${PARENTS[parent].reasons}.${name}`;
|
|
22
|
+
const what = `line ${at + 1}`;
|
|
23
|
+
const product = line.product === undefined ? null : productOf(line.product, what, code);
|
|
24
|
+
const quantity = quantityOf(line.quantity ?? "1");
|
|
25
|
+
const copied = product !== null && line.unitPrice === undefined && line.amount === undefined;
|
|
26
|
+
if (copied && product.unit_price != null && product.currency !== currency)
|
|
27
|
+
refuse(`product:${product.id} is priced in ${String(product.currency)}; give ${what}'s price in ${currency}`, code("currency"));
|
|
28
|
+
const unitPrice = copied
|
|
29
|
+
? product.unit_price == null
|
|
30
|
+
? null
|
|
31
|
+
: Number(product.unit_price)
|
|
32
|
+
: line.unitPrice === undefined
|
|
33
|
+
? null
|
|
34
|
+
: minorUnits(line.unitPrice, currency, `${what}'s unit price`);
|
|
35
|
+
const computed = unitPrice === null ? null : lineUnits(quantity.thousandths, unitPrice);
|
|
36
|
+
const amount = line.amount === undefined ? computed : minorUnits(line.amount, currency, `${what}'s amount`);
|
|
37
|
+
if (amount === null)
|
|
38
|
+
return refuse(`${what} takes a unit price, an amount or both`, code("lines"));
|
|
39
|
+
if (computed !== null && computed !== amount)
|
|
40
|
+
refuse(`${what}: ${quantity.text} × ${moneyLabel(unitPrice, currency)} is ` +
|
|
41
|
+
`${moneyLabel(computed, currency)}, not ${moneyLabel(amount, currency)}`, code("line-amount"));
|
|
42
|
+
const description = text(line.description ?? (product === null ? undefined : String(product.name)), `${what}'s description`, 500, code("field")) ?? refuse(`${what} needs a description`, code("lines"));
|
|
43
|
+
return {
|
|
44
|
+
description,
|
|
45
|
+
quantity: quantity.text,
|
|
46
|
+
unitPrice: unitPrice ?? (quantity.thousandths === 1000n ? amount : null),
|
|
47
|
+
amount,
|
|
48
|
+
productId: product === null ? null : Number(product.id),
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const linesOf = (parent, lines, currency) => lines.map((line, at) => priced(parent, line, at, currency));
|
|
52
|
+
/** Writes lines from position `from` on. */
|
|
53
|
+
const insertLines = (parent, id, lines, from, at) => {
|
|
54
|
+
const { table, column } = PARENTS[parent];
|
|
55
|
+
const insert = database.prepare(`INSERT INTO ${table} (${column}, position, description, quantity, unit_price, amount, catalog_item_id, ` +
|
|
56
|
+
"created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
57
|
+
for (const [index, line] of lines.entries())
|
|
58
|
+
insert.run(id, from + index, line.description, line.quantity, line.unitPrice, line.amount, line.productId, at, at);
|
|
59
|
+
};
|
|
60
|
+
const shownLines = (parent, id, currency) => {
|
|
61
|
+
const { table, column } = PARENTS[parent];
|
|
62
|
+
const scale = scaleOf(currency);
|
|
63
|
+
return database
|
|
64
|
+
.prepare(`SELECT * FROM ${table} WHERE ${column} = ? ORDER BY position`)
|
|
65
|
+
.all(id)
|
|
66
|
+
.map((line) => ({
|
|
67
|
+
position: Number(line.position),
|
|
68
|
+
description: String(line.description),
|
|
69
|
+
quantity: String(line.quantity),
|
|
70
|
+
unitPrice: line.unit_price == null ? null : formatDecimal(Number(line.unit_price), scale),
|
|
71
|
+
amount: formatDecimal(Number(line.amount), scale),
|
|
72
|
+
product: line.catalog_item_id == null ? null : `product:${line.catalog_item_id}`,
|
|
73
|
+
}));
|
|
74
|
+
};
|
|
75
|
+
return { linesOf, insertLines, shownLines };
|
|
76
|
+
};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { type TransactionStatus } from "../../domain/business.js";
|
|
2
|
+
import { type FinanceReview, type MoneyDirection } from "../../domain/finance.js";
|
|
3
|
+
import { type Money } from "../../domain/money.js";
|
|
4
|
+
import type { Decider } from "../decisions.js";
|
|
5
|
+
import { type ProposedByFilter } from "./actors.js";
|
|
6
|
+
import { type Author } from "./memories.js";
|
|
7
|
+
import type { StoreContext } from "./open.js";
|
|
8
|
+
import { type Party, type Reviewed, type Written } from "./reviewed.js";
|
|
9
|
+
export interface TransactionSplit {
|
|
10
|
+
position: number;
|
|
11
|
+
category: string;
|
|
12
|
+
amount: string;
|
|
13
|
+
project: string | null;
|
|
14
|
+
note: string | null;
|
|
15
|
+
}
|
|
16
|
+
export interface MoneyTransaction extends Reviewed {
|
|
17
|
+
id: string;
|
|
18
|
+
ref: string;
|
|
19
|
+
/** One line, as a list or the review inbox prints it. */
|
|
20
|
+
title: string;
|
|
21
|
+
wallet: string;
|
|
22
|
+
walletName: string;
|
|
23
|
+
direction: MoneyDirection;
|
|
24
|
+
amount: string;
|
|
25
|
+
currency: string;
|
|
26
|
+
status: TransactionStatus;
|
|
27
|
+
/** `YYYY-MM-DD`: the day the statement shows. */
|
|
28
|
+
statementDate: string;
|
|
29
|
+
/** Set once booked. */
|
|
30
|
+
bookedDate: string | null;
|
|
31
|
+
description: string | null;
|
|
32
|
+
/** As the statement names the other side. */
|
|
33
|
+
counterpartyName: string | null;
|
|
34
|
+
counterparty: Party | null;
|
|
35
|
+
reference: string | null;
|
|
36
|
+
/** The payment it settles, of the same currency, direction and amount. */
|
|
37
|
+
payment: string | null;
|
|
38
|
+
splits: TransactionSplit[];
|
|
39
|
+
/** What no split categorizes. */
|
|
40
|
+
uncategorized: string;
|
|
41
|
+
}
|
|
42
|
+
export interface SplitInput {
|
|
43
|
+
/** A decimal string in the transaction's currency. */
|
|
44
|
+
amount: string;
|
|
45
|
+
category: string;
|
|
46
|
+
project?: string;
|
|
47
|
+
note?: string;
|
|
48
|
+
}
|
|
49
|
+
export interface TransactionInput extends Written {
|
|
50
|
+
wallet: string;
|
|
51
|
+
direction: MoneyDirection;
|
|
52
|
+
/** A decimal string above 0, in the wallet's currency. */
|
|
53
|
+
amount: string;
|
|
54
|
+
/** The wallet's; refused when it differs. */
|
|
55
|
+
currency?: string;
|
|
56
|
+
/** `booked` when left out. */
|
|
57
|
+
status?: TransactionStatus;
|
|
58
|
+
statementDate: string;
|
|
59
|
+
/** A booked transaction's day; its `statementDate` when left out. */
|
|
60
|
+
bookedDate?: string;
|
|
61
|
+
description?: string;
|
|
62
|
+
counterpartyName?: string;
|
|
63
|
+
counterparty?: string;
|
|
64
|
+
reference?: string;
|
|
65
|
+
payment?: string;
|
|
66
|
+
splits?: SplitInput[];
|
|
67
|
+
}
|
|
68
|
+
/** The owner changes any transaction; a bot only one it proposed that is still proposed. */
|
|
69
|
+
export interface TransactionUpdate {
|
|
70
|
+
by: Author;
|
|
71
|
+
direction?: MoneyDirection;
|
|
72
|
+
/** Refused below its splits, or when a linked payment no longer matches. */
|
|
73
|
+
amount?: string;
|
|
74
|
+
status?: TransactionStatus;
|
|
75
|
+
statementDate?: string;
|
|
76
|
+
bookedDate?: string;
|
|
77
|
+
description?: string | null;
|
|
78
|
+
counterpartyName?: string | null;
|
|
79
|
+
/** `null` takes the link back. */
|
|
80
|
+
payment?: string | null;
|
|
81
|
+
counterparty?: string | null;
|
|
82
|
+
reference?: string | null;
|
|
83
|
+
}
|
|
84
|
+
export interface TransactionFilter {
|
|
85
|
+
wallet?: string;
|
|
86
|
+
direction?: MoneyDirection;
|
|
87
|
+
status?: TransactionStatus;
|
|
88
|
+
/** A split's category, in any letter case. */
|
|
89
|
+
category?: string;
|
|
90
|
+
project?: string;
|
|
91
|
+
review?: FinanceReview;
|
|
92
|
+
proposedBy?: ProposedByFilter;
|
|
93
|
+
/** Only those with an amount no split categorizes. */
|
|
94
|
+
uncategorized?: boolean;
|
|
95
|
+
/** 1–500, 100 by default; latest statement day first. */
|
|
96
|
+
limit?: number;
|
|
97
|
+
}
|
|
98
|
+
export interface CategoryTotal extends Money {
|
|
99
|
+
/** `null` for what no split categorizes. */
|
|
100
|
+
category: string | null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* What statements say moved on the owner's wallets (`transaction:<id>`). An agent's or an import's waits as proposed;
|
|
104
|
+
* a payment is linked only when currency, direction and amount agree, one transaction per payment.
|
|
105
|
+
*/
|
|
106
|
+
export interface TransactionsStore {
|
|
107
|
+
add(input: TransactionInput): Promise<MoneyTransaction>;
|
|
108
|
+
get(reference: string): Promise<MoneyTransaction>;
|
|
109
|
+
list(filter?: TransactionFilter): Promise<MoneyTransaction[]>;
|
|
110
|
+
update(reference: string, input: TransactionUpdate): Promise<MoneyTransaction>;
|
|
111
|
+
addSplit(reference: string, input: SplitInput & {
|
|
112
|
+
by: Author;
|
|
113
|
+
}): Promise<MoneyTransaction>;
|
|
114
|
+
removeSplit(reference: string, input: {
|
|
115
|
+
position: number;
|
|
116
|
+
by: Author;
|
|
117
|
+
}): Promise<MoneyTransaction>;
|
|
118
|
+
/** Totals per category and currency over confirmed, booked transactions; nothing is converted. */
|
|
119
|
+
categories(filter?: {
|
|
120
|
+
wallet?: string;
|
|
121
|
+
from?: string;
|
|
122
|
+
until?: string;
|
|
123
|
+
}): Promise<CategoryTotal[]>;
|
|
124
|
+
confirm(reference: string, by: Decider, reason?: string): Promise<MoneyTransaction>;
|
|
125
|
+
reject(reference: string, by: Decider, reason?: string): Promise<MoneyTransaction>;
|
|
126
|
+
}
|
|
127
|
+
type TransactionOperations = {
|
|
128
|
+
[K in keyof TransactionsStore]: TransactionsStore[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
|
|
129
|
+
};
|
|
130
|
+
export declare const transactionOperationsOver: (context: StoreContext) => TransactionOperations;
|
|
131
|
+
export declare const transactionsStoreOver: (context: StoreContext) => TransactionsStore;
|
|
132
|
+
export {};
|