@happyvertical/smrt-commerce 0.62.4 → 0.62.6
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/AGENTS.md +1 -0
- package/agents/accounting-connection.md +76 -0
- package/agents/accounting-live-gate.md +40 -0
- package/agents/accounting-mapping.md +60 -0
- package/agents/accounting-payment-sync.md +76 -0
- package/agents/ordinary-accounting.md +3 -0
- package/dist/accounting-connection.d.ts +179 -0
- package/dist/accounting-connection.d.ts.map +1 -0
- package/dist/accounting-live-gate.d.ts +50 -0
- package/dist/accounting-live-gate.d.ts.map +1 -0
- package/dist/accounting-mapping.d.ts +189 -0
- package/dist/accounting-mapping.d.ts.map +1 -0
- package/dist/accounting-payment-sync.d.ts +233 -0
- package/dist/accounting-payment-sync.d.ts.map +1 -0
- package/dist/chunks/{InvoiceCollection-Cc-fJrHy.js → InvoiceCollection-xooQnUwq.js} +10 -10
- package/dist/chunks/{InvoiceCollection-Cc-fJrHy.js.map → InvoiceCollection-xooQnUwq.js.map} +1 -1
- package/dist/chunks/{allocation-integrity-CvXQm3wK.js → allocation-integrity-C4SZJcao.js} +6 -6
- package/dist/chunks/{allocation-integrity-CvXQm3wK.js.map → allocation-integrity-C4SZJcao.js.map} +1 -1
- package/dist/chunks/{contract-money-BoKHNfV6.js → contract-money-ATacxU6t.js} +4 -7
- package/dist/chunks/{contract-money-BoKHNfV6.js.map → contract-money-ATacxU6t.js.map} +1 -1
- package/dist/chunks/{invoice-posting-DlR_-TG1.js → invoice-posting-CDfxug3Q.js} +4 -5
- package/dist/chunks/{invoice-posting-DlR_-TG1.js.map → invoice-posting-CDfxug3Q.js.map} +1 -1
- package/dist/chunks/retention-accounting-BA4wXUQc.js +8710 -0
- package/dist/chunks/retention-accounting-BA4wXUQc.js.map +1 -0
- package/dist/chunks/{tenant-money-barrier-Bmu4YiID.js → tenant-money-barrier-BdyXIVT5.js} +3 -3
- package/dist/chunks/{tenant-money-barrier-Bmu4YiID.js.map → tenant-money-barrier-BdyXIVT5.js.map} +1 -1
- package/dist/collections/AccountingConnectionCollection.d.ts +27 -0
- package/dist/collections/AccountingConnectionCollection.d.ts.map +1 -0
- package/dist/collections/AccountingMappingCollection.d.ts +24 -0
- package/dist/collections/AccountingMappingCollection.d.ts.map +1 -0
- package/dist/collections/AccountingSyncRecordCollections.d.ts +27 -0
- package/dist/collections/AccountingSyncRecordCollections.d.ts.map +1 -0
- package/dist/collections/index.d.ts +3 -0
- package/dist/collections/index.d.ts.map +1 -1
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1532 -21
- package/dist/index.js.map +1 -1
- package/dist/invoice-accounting-sync.d.ts +6 -0
- package/dist/invoice-accounting-sync.d.ts.map +1 -1
- package/dist/invoices.js +1 -1
- package/dist/manifest.json +6303 -3740
- package/dist/models/AccountingConnection.d.ts +101 -0
- package/dist/models/AccountingConnection.d.ts.map +1 -0
- package/dist/models/AccountingMapping.d.ts +85 -0
- package/dist/models/AccountingMapping.d.ts.map +1 -0
- package/dist/models/AccountingSyncRecords.d.ts +95 -0
- package/dist/models/AccountingSyncRecords.d.ts.map +1 -0
- package/dist/models/index.d.ts +3 -0
- package/dist/models/index.d.ts.map +1 -1
- package/dist/ordinary-accounting.d.ts +1 -0
- package/dist/ordinary-accounting.d.ts.map +1 -1
- package/dist/recipes.d.ts +1 -1
- package/dist/retention-accounting.d.ts +1 -0
- package/dist/retention-accounting.d.ts.map +1 -1
- package/dist/smrt-knowledge.json +1327 -99
- package/dist/smrt-knowledge.okf.md +55 -23
- package/package.json +15 -15
- package/dist/chunks/ContractLineItemCollection-BKHy-nR8.js +0 -260
- package/dist/chunks/ContractLineItemCollection-BKHy-nR8.js.map +0 -1
- package/dist/chunks/DocumentTaxCollection-BRebznXF.js +0 -4649
- package/dist/chunks/DocumentTaxCollection-BRebznXF.js.map +0 -1
- package/dist/chunks/TaxCodeCollection-BBqCugs5.js +0 -155
- package/dist/chunks/TaxCodeCollection-BBqCugs5.js.map +0 -1
- package/dist/chunks/TaxComponentCollection-BHJjbQxq.js +0 -134
- package/dist/chunks/TaxComponentCollection-BHJjbQxq.js.map +0 -1
- package/dist/chunks/credit-allocation-yspz0Lab.js +0 -121
- package/dist/chunks/credit-allocation-yspz0Lab.js.map +0 -1
- package/dist/chunks/retention-accounting-D0KzSw3f.js +0 -3069
- package/dist/chunks/retention-accounting-D0KzSw3f.js.map +0 -1
- package/dist/chunks/retention-accounting-internal-DNxLLVvK.js +0 -30
- package/dist/chunks/retention-accounting-internal-DNxLLVvK.js.map +0 -1
- package/dist/chunks/units-8ElbjVZK.js +0 -60
- package/dist/chunks/units-8ElbjVZK.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retention-accounting-BA4wXUQc.js","names":["field","field","revision","saved","input","netMinor","taxMinor","grossMinor","field","request","projection","step"],"sources":["../../src/models/AccountingConnection.ts","../../src/collections/AccountingConnectionCollection.ts","../../src/accounting-live-gate.ts","../../src/models/InvoiceLineItem.ts","../../src/collections/InvoiceLineItemCollection.ts","../../src/historical-funds-internal.ts","../../src/credit-allocation.ts","../../src/models/PaymentAllocation.ts","../../src/collections/PaymentAllocationCollection.ts","../../src/connected-payments-internal.ts","../../src/ordinary-accounting-internal.ts","../../src/retention-accounting-internal.ts","../../src/models/Payment.ts","../../src/collections/PaymentCollection.ts","../../src/svelte/invoices/currency.ts","../../src/billing/units.ts","../../src/accounting-snapshot.ts","../../src/party-contacts.ts","../../src/models/Customer.ts","../../src/collections/CustomerCollection.ts","../../src/models/HistoricalSalesLine.ts","../../src/collections/HistoricalSalesLineCollection.ts","../../src/models/TaxCode.ts","../../src/collections/TaxCodeCollection.ts","../../src/models/TaxComponent.ts","../../src/collections/TaxComponentCollection.ts","../../src/models/Vendor.ts","../../src/models/Contract.ts","../../src/collections/PurchaseContractCollection.ts","../../src/collections/SalesContractCollection.ts","../../src/collections/ContractCollection.ts","../../src/contract-billing-schedule.ts","../../src/invoice-source-binding.ts","../../src/document-money.ts","../../src/historical-sales-validation.ts","../../src/historical-sales-money.ts","../../src/historical-sales-reports.ts","../../src/models/DocumentTax.ts","../../src/models/ContractLineItem.ts","../../src/collections/ContractLineItemCollection.ts","../../src/collections/VendorCollection.ts","../../src/received-purchase-money.ts","../../src/collections/DocumentTaxCollection.ts","../../src/quickbooks-pilot-evidence.ts","../../src/invoice-accounting-sync.ts","../../src/svelte/invoices/calculations.ts","../../src/accounting-steps-internal.ts","../../src/ordinary-accounting.ts","../../src/retention-accounting.ts"],"sourcesContent":["/**\n * AccountingConnection — a tenant's connection to one accounting provider\n * (QuickBooks Online) (#5136).\n *\n * Tenant-level, not user-owned: one row per `(tenant, provider)`. The OAuth\n * refresh token is never a column; it lives in smrt-secrets. The row keeps the\n * company (`realmId`), the environment, a connection status, the last error\n * code, and the per-connection refresh lease that serializes token rotation.\n *\n * Commands live in `accounting-connection.ts`; this model only guards its own\n * identity. See `agents/accounting-connection.md`.\n *\n * @packageDocumentation\n */\n\nimport {\n field,\n type SmrtDeleteOptions,\n SmrtObject,\n type SmrtObjectOptions,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\n\n/** Accounting providers a connection can name. */\nexport type AccountingConnectionProvider = 'quickbooks';\n\n/** Provider mode. Sandbox and production companies never mix. */\nexport type AccountingConnectionEnvironment = 'sandbox' | 'production';\n\n/**\n * Connection standing.\n *\n * - `connected`: tokens are held and the last refresh succeeded.\n * - `needs_reconnect`: the provider rejected the grant (`invalid_grant`:\n * revoked consent or an expired refresh token); the user must reconnect.\n * - `disconnected`: revoked at the provider and the secret deleted; the row and\n * its sync history stay.\n */\nexport type AccountingConnectionStatus =\n | 'connected'\n | 'needs_reconnect'\n | 'disconnected';\n\n/** Constructor options for {@link AccountingConnection}. */\nexport interface AccountingConnectionOptions extends SmrtObjectOptions {\n tenantId?: string;\n provider?: AccountingConnectionProvider;\n realmId?: string;\n environment?: AccountingConnectionEnvironment;\n status?: AccountingConnectionStatus;\n}\n\nconst PROVIDERS: readonly AccountingConnectionProvider[] = ['quickbooks'];\nconst ENVIRONMENTS: readonly AccountingConnectionEnvironment[] = [\n 'sandbox',\n 'production',\n];\nconst STATUSES: readonly AccountingConnectionStatus[] = [\n 'connected',\n 'needs_reconnect',\n 'disconnected',\n];\n\n/** Rows being written by an accounting-connection command. */\nconst commandWriteInProgress = new WeakSet<AccountingConnection>();\n\n/** @internal Run a write owned by the accounting-connection commands. */\nexport async function writeAccountingConnection<T>(\n connection: AccountingConnection,\n write: () => Promise<T>,\n): Promise<T> {\n commandWriteInProgress.add(connection);\n try {\n return await write();\n } finally {\n commandWriteInProgress.delete(connection);\n }\n}\n\n/**\n * A tenant's connection to an accounting provider.\n *\n * Unique on `(tenantId, provider)`: one connection per tenant and provider.\n * Reconnecting reuses the row (the company may change); disconnecting keeps it.\n * The generated surface is read-only: the row is created, rebound, refreshed\n * and disconnected only through the accounting-connection commands, and never\n * deleted.\n */\n@TenantScoped({ mode: 'required' })\n@smrt({\n tableName: 'accounting_connections',\n indexes: [\n {\n name: 'accounting_connections_tenant_provider',\n columns: ['tenantId', 'provider'],\n unique: true,\n },\n ],\n api: { include: ['list', 'get'] },\n mcp: { include: ['list', 'get'] },\n cli: {\n include: ['list', 'get', 'isConnected', 'isLiveEnabled'],\n skipApiCheck: true,\n },\n})\nexport class AccountingConnection extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /** Tenant that owns the connection. */\n @tenantId()\n tenantId: string = '';\n\n /** Accounting provider. */\n provider: AccountingConnectionProvider = 'quickbooks';\n\n /** Provider company id (QuickBooks `realmId`); empty before the first connect. */\n realmId: string = '';\n\n /** Provider mode of the connected company. */\n environment: AccountingConnectionEnvironment = 'sandbox';\n\n /** Standing; see {@link AccountingConnectionStatus}. */\n status: AccountingConnectionStatus = 'disconnected';\n\n /** Short error code of the last failure (never provider text or tokens). */\n lastError: string = '';\n\n /** When {@link lastError} was recorded. */\n @field({ type: 'datetime', nullable: true })\n lastErrorAt: Date | null = null;\n\n /** Who completed the connect (an opaque user reference), if known. */\n connectedBy: string = '';\n\n /** When the current connection was completed. */\n @field({ type: 'datetime', nullable: true })\n connectedAt: Date | null = null;\n\n /** When the connection was last revoked and its secret deleted. */\n @field({ type: 'datetime', nullable: true })\n disconnectedAt: Date | null = null;\n\n /** Incremented on every persisted token rotation; never the token itself. */\n tokenVersion: number = 0;\n\n /** When tokens were last refreshed and persisted. */\n @field({ type: 'datetime', nullable: true })\n lastRefreshedAt: Date | null = null;\n\n /**\n * The company live (production) accounting writes were deliberately enabled\n * for, or empty. Production sync runs only while this equals\n * {@link realmId} on a connected production connection (#5138).\n */\n liveEnabledRealmId: string = '';\n\n /** When live writes were enabled. */\n @field({ type: 'datetime', nullable: true })\n liveEnabledAt: Date | null = null;\n\n /** Who enabled live writes (an opaque user reference). */\n liveEnabledBy: string = '';\n\n /** Reference to the evidence that justified enabling live writes. */\n liveEnabledEvidenceRef: string = '';\n\n /** Owner of the refresh lease (random token), or empty. Command-owned. */\n @field({ sensitive: true })\n leaseToken: string = '';\n\n /** ISO instant the refresh lease lapses, or empty. Command-owned. */\n @field({ sensitive: true })\n leaseExpiresAt: string = '';\n\n constructor(options: AccountingConnectionOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.provider !== undefined) this.provider = options.provider;\n if (options.realmId !== undefined) this.realmId = options.realmId;\n if (options.environment !== undefined)\n this.environment = options.environment;\n if (options.status !== undefined) this.status = options.status;\n }\n\n /** Whether production writes are enabled for the connected company. */\n isLiveEnabled(): boolean {\n return (\n this.status === 'connected' &&\n this.environment === 'production' &&\n this.realmId !== '' &&\n this.liveEnabledRealmId === this.realmId\n );\n }\n\n /** Whether the connection can currently make provider calls. */\n isConnected(): boolean {\n return this.status === 'connected';\n }\n\n /** Refuses writes outside the commands and any change of tenant or provider. */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n if (!commandWriteInProgress.has(this))\n throw new Error(\n 'AccountingConnection is written only by the accounting-connection commands',\n );\n if (!this.tenantId)\n throw new Error('AccountingConnection requires a tenant');\n if (!PROVIDERS.includes(this.provider))\n throw new Error('AccountingConnection provider is invalid');\n if (!ENVIRONMENTS.includes(this.environment))\n throw new Error(\n 'AccountingConnection environment must be sandbox or production',\n );\n if (!STATUSES.includes(this.status))\n throw new Error('AccountingConnection status is invalid');\n if (this.status === 'connected' && !this.realmId)\n throw new Error('A connected AccountingConnection requires a realmId');\n if (this.id) {\n const prior = await this.getCanonicalPersistedRow({ id: this.id });\n if (prior) {\n const identity: Array<[string, unknown, unknown]> = [\n ['tenantId', String(prior.tenant_id ?? ''), this.tenantId],\n ['provider', String(prior.provider ?? ''), this.provider],\n ];\n for (const [name, before, after] of identity)\n if (String(before).toLowerCase() !== String(after).toLowerCase())\n throw new Error(`AccountingConnection ${name} cannot change`);\n }\n }\n return super.save(options);\n }\n\n /** Never deleted: it owns sync history. Use the disconnect command. */\n override async delete(_options?: SmrtDeleteOptions): Promise<void> {\n throw new Error(\n 'AccountingConnection cannot be deleted; disconnect it instead',\n );\n }\n}\n\nexport default AccountingConnection;\n","/**\n * AccountingConnectionCollection - tenant accounting connections (#5136).\n * Writes go through the accounting-connection commands; see\n * `agents/accounting-connection.md`.\n * @packageDocumentation\n */\n\nimport { SmrtCollection } from '@happyvertical/smrt-core';\nimport {\n AccountingConnection,\n type AccountingConnectionProvider,\n} from '../models/AccountingConnection.js';\n\nexport class AccountingConnectionCollection extends SmrtCollection<AccountingConnection> {\n static readonly _itemClass = AccountingConnection;\n\n /**\n * @internal An unsaved connection for the accounting-connection commands,\n * which own every write (`save()` refuses any other caller).\n */\n async build(\n options: Parameters<SmrtCollection<AccountingConnection>['create']>[0],\n ): Promise<AccountingConnection> {\n return this.createUnsaved(options);\n }\n\n /**\n * The tenant's connection for a provider, or `null`.\n *\n * @param args - Tenant and provider\n * @returns The connection or null\n */\n async findForTenant(args: {\n tenantId: string;\n provider: AccountingConnectionProvider;\n }): Promise<AccountingConnection | null> {\n const rows = await this.list({\n where: { tenantId: args.tenantId, provider: args.provider },\n limit: 2,\n });\n if (rows.length > 1)\n throw new Error(\n 'Tenant has more than one accounting connection per provider',\n );\n return rows[0] ?? null;\n }\n}\n","/**\n * Live-mode gate on accounting writes (#5138).\n *\n * The frozen invoice, credit and refund sync functions accepted any provider\n * with `environment: 'production'`; live use was blocked only by convention.\n * Production writes now need a deliberate, persisted enablement on the tenant's\n * {@link AccountingConnection} (`enableLiveAccounting`) for the same company,\n * and a connected production connection. Sandbox stays the default and needs\n * nothing. Without it a production write throws\n * {@link AccountingLiveGateError} before any state is reserved or any provider\n * call is made, so state is unchanged.\n *\n * See `agents/accounting-live-gate.md`.\n *\n * @packageDocumentation\n */\n\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { withTenant } from '@happyvertical/smrt-tenancy';\nimport { AccountingConnectionCollection } from './collections/AccountingConnectionCollection.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\n\n/** Why the gate refused a write. */\nexport type AccountingLiveGateErrorCode =\n | 'LIVE_ACCOUNTING_NOT_ENABLED'\n | 'ACCOUNTING_ENVIRONMENT_MISMATCH';\n\n/** Typed refusal: a production accounting write is not enabled. */\nexport class AccountingLiveGateError extends Error {\n readonly code: AccountingLiveGateErrorCode;\n\n constructor(code: AccountingLiveGateErrorCode, message: string) {\n super(message);\n this.name = 'AccountingLiveGateError';\n this.code = code;\n }\n}\n\n/** What a sync function is about to write to. */\nexport interface AccountingWriteTarget {\n readonly db: DatabaseInterface;\n readonly tenantId: string;\n /** The local document, used to read the realm and environment it is bound to. */\n readonly invoiceId?: string;\n /** The provider; its own `environment` is checked when it exposes one. */\n readonly provider?: object;\n /** The realm and environment the caller's mapping targets, when it has one. */\n readonly scope?: {\n readonly realmId: string;\n readonly environment: 'sandbox' | 'production';\n };\n}\n\nfunction providerEnvironment(provider: object | undefined): string | undefined {\n const value = (provider as { environment?: unknown } | undefined)\n ?.environment;\n return typeof value === 'string' ? value : undefined;\n}\n\n/**\n * The tenant's connection, or `null` when it has none. A database that has not\n * migrated the connection table yet has no connection by definition: sandbox\n * writes carry on and production stays refused.\n */\nasync function findConnection(target: AccountingWriteTarget) {\n try {\n return await withTenant({ tenantId: target.tenantId }, async () =>\n (\n await AccountingConnectionCollection.create({ db: target.db })\n ).findForTenant({ tenantId: target.tenantId, provider: 'quickbooks' }),\n );\n } catch (error) {\n if ((error as { code?: unknown } | null)?.code === 'DB_SCHEMA_MISSING')\n return null;\n throw error;\n }\n}\n\n/**\n * Refuse a production accounting write unless live mode is enabled for the\n * tenant's connected company. Call before reserving state or calling the\n * provider. Resolves for sandbox targets.\n *\n * The target environment is every one of: the caller's mapping scope, the\n * provider's own `environment`, and the environment the document was already\n * synced under. They must agree, so a sandbox-labelled mapping cannot reach a\n * production provider; any production among them requires the gate.\n */\nexport async function assertAccountingWriteAllowed(\n target: AccountingWriteTarget,\n): Promise<void> {\n const environments = new Set<string>();\n let realmId = target.scope?.realmId;\n if (target.scope) environments.add(target.scope.environment);\n const fromProvider = providerEnvironment(target.provider);\n if (fromProvider) environments.add(fromProvider);\n if (target.invoiceId) {\n const invoice = await withTenant({ tenantId: target.tenantId }, async () =>\n (await InvoiceCollection.create({ db: target.db })).get(\n { id: target.invoiceId },\n { cache: false },\n ),\n );\n if (invoice?.accountingSyncStateJson) {\n try {\n const state = JSON.parse(invoice.accountingSyncStateJson) as {\n identity?: { environment?: string; realmId?: string };\n };\n if (state.identity?.environment)\n environments.add(state.identity.environment);\n realmId = realmId ?? state.identity?.realmId;\n } catch {\n // Malformed state is the sync function's refusal to make, not ours.\n }\n }\n }\n if (environments.size > 1)\n throw new AccountingLiveGateError(\n 'ACCOUNTING_ENVIRONMENT_MISMATCH',\n 'The provider, mapping and synced document name different accounting environments',\n );\n const connection = await findConnection(target);\n if (!environments.has('production')) {\n // A sandbox label must not reach a company the tenant connected as\n // production: the label is the caller's claim, the connection is the record.\n if (\n target.scope &&\n connection &&\n connection.status !== 'disconnected' &&\n connection.realmId === target.scope.realmId &&\n connection.environment !== target.scope.environment\n )\n throw new AccountingLiveGateError(\n 'ACCOUNTING_ENVIRONMENT_MISMATCH',\n 'The mapping names a different environment than the tenant connection for this company',\n );\n return;\n }\n if (\n !connection ||\n !connection.isLiveEnabled() ||\n (realmId !== undefined && connection.realmId !== realmId)\n )\n throw new AccountingLiveGateError(\n 'LIVE_ACCOUNTING_NOT_ENABLED',\n 'Production accounting writes are not enabled for this tenant and company',\n );\n}\n","/**\n * InvoiceLineItem model - individual items on an invoice\n * @packageDocumentation\n */\n\nimport {\n crossPackageRef,\n field,\n foreignKey,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport {\n DOCUMENT_MONEY_READ_REBIND,\n DOCUMENT_MONEY_WRITE,\n type DocumentMoneyReadRebindCapability,\n type DocumentMoneyWriteCapability,\n} from '../document-money-internal.js';\nimport { MoneyValidationError } from '../money.js';\nimport type {\n InvoiceCreditCalculationBasis,\n InvoiceLineItemOptions,\n InvoiceTaxComponentSnapshot,\n} from '../types/index.js';\n\n/**\n * InvoiceLineItem represents a single line item on an invoice.\n *\n * Line items contain details of what is being billed, including\n * quantity, pricing, and optional source tracking (e.g., from ad campaigns).\n *\n * @example\n * ```typescript\n * const lineItem = await lineItems.create({\n * invoiceId: invoice.id,\n * description: 'Display Advertising - Summer Campaign',\n * quantity: 50000, // impressions\n * unitPrice: 1, // 1 cent per impression (minor units)\n * taxRate: 0.05,\n * sourceType: 'campaign',\n * sourceId: 'campaign-uuid',\n * periodStart: new Date('2025-06-01'),\n * periodEnd: new Date('2025-06-30')\n * });\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n api: {\n include: ['list', 'get', 'create', 'update', 'delete'],\n writable: [\n 'invoiceId',\n 'description',\n 'sku',\n 'productId',\n 'quantity',\n 'unitPrice',\n 'discount',\n 'discountType',\n 'discountRate',\n 'taxRate',\n 'taxMode',\n 'taxCodeId',\n 'sourceType',\n 'sourceId',\n 'periodStart',\n 'periodEnd',\n 'revenueAccountId',\n 'sortOrder',\n ],\n },\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class InvoiceLineItem extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /** @internal Collection read guard returns this snapshot on its root DB. */\n rebindMoneyRead(\n capability: DocumentMoneyReadRebindCapability,\n db: DatabaseInterface,\n ): this {\n if (capability !== DOCUMENT_MONEY_READ_REBIND)\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Invoice line read rebinding requires the owning capability',\n );\n this._db = db;\n this.options.db = db;\n return this;\n }\n\n /**\n * Tenant ID for multi-tenant isolation\n * Nullable to support both tenant-scoped and global invoice line items\n */\n @tenantId({ nullable: true })\n tenantId: string | null = null;\n\n /**\n * Parent invoice\n */\n @field({ description: 'The invoice this line belongs to.' })\n @foreignKey('Invoice', { role: 'items' })\n invoiceId: string = '';\n\n @foreignKey('InvoiceLineItem', { nullable: true })\n creditedLineId: string | null = null;\n\n @field({\n description:\n 'Frozen source basis used to cap and reproduce an issued credit line.',\n })\n creditCalculationBasisJson = '';\n\n get creditCalculationBasis(): InvoiceCreditCalculationBasis | null {\n try {\n const value: unknown = JSON.parse(this.creditCalculationBasisJson);\n if (!value || typeof value !== 'object' || !('kind' in value))\n return null;\n const basis = value as Record<string, unknown>;\n if (\n (basis.kind !== 'quantity' && basis.kind !== 'net_amount') ||\n typeof basis.sourceQuantity !== 'string' ||\n !Number.isSafeInteger(basis.sourceUnitPrice) ||\n (basis.sourceDiscountType !== 'none' &&\n basis.sourceDiscountType !== 'percent' &&\n basis.sourceDiscountType !== 'amount') ||\n typeof basis.sourceDiscountRate !== 'string' ||\n !Number.isSafeInteger(basis.sourceDiscountMinor) ||\n (basis.kind === 'quantity' && typeof basis.quantity !== 'string') ||\n (basis.kind === 'net_amount' && !Number.isSafeInteger(basis.netMinor))\n )\n return null;\n return basis as unknown as InvoiceCreditCalculationBasis;\n } catch {\n return null;\n }\n }\n\n set creditCalculationBasis(value: InvoiceCreditCalculationBasis | null) {\n this.creditCalculationBasisJson = value ? JSON.stringify(value) : '';\n }\n\n /**\n * Item description\n */\n @field({ description: 'What is being billed on this line.' })\n description: string = '';\n\n /**\n * SKU or item code\n */\n sku: string = '';\n\n @crossPackageRef('@happyvertical/smrt-products:Product', { nullable: true })\n productId: string | null = null;\n\n /** Nonnegative decimal quantity (hours, weight, units), up to six fractional digits. */\n @field({ description: 'How many.' })\n quantity: number = 1.0;\n\n /**\n * Unit price before discount, in **integer minor units** (cents, satoshis),\n * in the document's pricing mode: tax-inclusive when its `lineAmountType`\n * is `inclusive`.\n *\n * Money is exact, so it is stored as minor units and never as a float —\n * `$19.99` is `1999`. An integer literal is what maps this to an INTEGER\n * column (#2361).\n */\n @field({\n description:\n \"The price of one, before discount; includes tax when the document's prices include tax.\",\n ui: { format: 'money' },\n })\n unitPrice: number = 0;\n\n /**\n * Discount amount (flat, not percentage), in integer minor units (#2361).\n */\n @field({ ui: { format: 'money' } })\n discount: number = 0;\n\n discountType: 'none' | 'percent' | 'amount' = 'none';\n\n @field({ type: 'decimal' })\n discountRate = '0.0';\n\n /**\n * Tax rate as a decimal fraction (e.g., 0.05 for 5%).\n *\n * A genuine rate, not money: it is inherently fractional, so the `0.0`\n * initializer is load-bearing and maps it to a DECIMAL column. INTEGER would\n * truncate every rate to 0 (#2361).\n */\n @field({\n description:\n 'The stored fractional tax rate for this line (0.05 means 5%).',\n })\n taxRate: number = 0.0;\n\n taxMode: 'inherit' | 'override' = 'inherit';\n\n @foreignKey('TaxCode', { nullable: true })\n taxCodeId: string | null = null;\n\n taxCodeCode: string | null = null;\n\n taxCodeName: string | null = null;\n\n taxComponentsJson: string | null = null;\n\n get taxComponents(): InvoiceTaxComponentSnapshot[] {\n if (this.taxComponentsJson === null) return [];\n try {\n const value: unknown = JSON.parse(this.taxComponentsJson);\n if (!Array.isArray(value)) return [];\n return value.filter(\n (entry): entry is InvoiceTaxComponentSnapshot =>\n !!entry &&\n typeof entry === 'object' &&\n typeof (entry as InvoiceTaxComponentSnapshot).taxComponentId ===\n 'string' &&\n typeof (entry as InvoiceTaxComponentSnapshot).name === 'string' &&\n typeof (entry as InvoiceTaxComponentSnapshot).percent === 'string' &&\n Number.isSafeInteger((entry as InvoiceTaxComponentSnapshot).taxMinor),\n );\n } catch {\n return [];\n }\n }\n\n set taxComponents(value: InvoiceTaxComponentSnapshot[]) {\n this.taxComponentsJson = JSON.stringify(value);\n }\n\n /** Exact sum of frozen component taxes, in integer minor units. */\n @field({ ui: { format: 'money' } })\n taxAmount = 0;\n\n /** Stable document snapshot identity; ordinary lifecycle saves do not change it. */\n moneySnapshotId = '';\n\n /**\n * Calculated line amount, in integer minor units (#2361): net of discount\n * and before tax for `exclusive` / `none` documents, and tax-inclusive for\n * `inclusive` ones; {@link taxAmount} holds the tax.\n */\n @field({\n description:\n \"The line amount: net of discount, and including tax only when the document's prices include tax.\",\n ui: { format: 'money' },\n })\n amount: number = 0;\n\n // ============================================================================\n // Source Tracking\n // ============================================================================\n\n /**\n * Type of source ('campaign' | 'contract' | 'manual' | etc.)\n */\n sourceType: string = '';\n\n /**\n * ID of the source (e.g., campaign ID, contract ID)\n */\n sourceId: string = '';\n\n /** Command-owned pointer to the source package's existing reservation. */\n @field({ type: 'text', nullable: true, readonly: true })\n sourceReservationId: string | null = null;\n\n /**\n * Service period start (for time-based billing)\n */\n periodStart: Date | null = null;\n\n /**\n * Service period end (for time-based billing)\n */\n periodEnd: Date | null = null;\n\n // ============================================================================\n // Accounting\n // ============================================================================\n\n /**\n * Revenue account ID (cross-package ref to smrt-ledgers)\n * Used for revenue recognition to specific accounts\n */\n @crossPackageRef('@happyvertical/smrt-ledgers:Account', { nullable: true })\n revenueAccountId: string | null = null;\n\n /**\n * Sort order within the invoice\n */\n sortOrder: number = 0;\n\n constructor(options: InvoiceLineItemOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.invoiceId !== undefined) this.invoiceId = options.invoiceId;\n if (options.creditedLineId !== undefined)\n this.creditedLineId = options.creditedLineId;\n if (options.creditCalculationBasisJson !== undefined)\n this.creditCalculationBasisJson = options.creditCalculationBasisJson;\n if (options.creditCalculationBasis !== undefined)\n this.creditCalculationBasis = structuredClone(\n options.creditCalculationBasis,\n );\n if (options.description !== undefined)\n this.description = options.description;\n if (options.sku !== undefined) this.sku = options.sku;\n if (options.productId !== undefined) this.productId = options.productId;\n if (options.quantity !== undefined) this.quantity = options.quantity;\n if (options.unitPrice !== undefined) this.unitPrice = options.unitPrice;\n if (options.discount !== undefined) this.discount = options.discount;\n if (options.discountType !== undefined)\n this.discountType = options.discountType;\n if (options.discountRate !== undefined)\n this.discountRate = options.discountRate;\n if (options.taxRate !== undefined) this.taxRate = options.taxRate;\n if (options.taxMode !== undefined) this.taxMode = options.taxMode;\n if (options.taxCodeId !== undefined) this.taxCodeId = options.taxCodeId;\n if (options.taxCodeCode !== undefined)\n this.taxCodeCode = options.taxCodeCode;\n if (options.taxCodeName !== undefined)\n this.taxCodeName = options.taxCodeName;\n if (options.taxComponentsJson !== undefined)\n this.taxComponentsJson = options.taxComponentsJson;\n if (options.taxComponents !== undefined)\n this.taxComponents = structuredClone(options.taxComponents);\n if (options.taxAmount !== undefined) this.taxAmount = options.taxAmount;\n if (options.moneySnapshotId !== undefined)\n this.moneySnapshotId = options.moneySnapshotId;\n if (options.amount !== undefined) this.amount = options.amount;\n if (options.sourceType !== undefined) this.sourceType = options.sourceType;\n if (options.sourceId !== undefined) this.sourceId = options.sourceId;\n if (options.sourceReservationId !== undefined)\n this.sourceReservationId = options.sourceReservationId;\n if (options.periodStart !== undefined)\n this.periodStart = options.periodStart;\n if (options.periodEnd !== undefined) this.periodEnd = options.periodEnd;\n if (options.revenueAccountId !== undefined)\n this.revenueAccountId = options.revenueAccountId;\n if (options.sortOrder !== undefined) this.sortOrder = options.sortOrder;\n }\n\n /** Refuse an old-format row read from the database. */\n override async initialize(): Promise<this> {\n await super.initialize();\n // A row read from the database must not be old-format money data.\n if (this.isPersisted) {\n const { assertHydratedMoneyRowCurrent } = await import(\n '../upgrade/detect.js'\n );\n await assertHydratedMoneyRowCurrent(this);\n }\n return this;\n }\n\n /** Persist through the invoice money aggregate. */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n const { saveInvoiceLineItem } = await import('../document-money.js');\n return (await saveInvoiceLineItem(this, options)) as this;\n }\n\n /** @internal Aggregate persistence only. */\n async saveDerived(\n capability: DocumentMoneyWriteCapability,\n options?: SmrtSaveOptions,\n ): Promise<this> {\n if (capability !== DOCUMENT_MONEY_WRITE)\n throw new Error('Invoice line money write capability is required');\n // Money is integer minor units: refuse a major-unit price before it can\n // reach the database (#2401), ahead of the aggregate recompute.\n for (const [field, value] of [\n ['unitPrice', this.unitPrice],\n ['discount', this.discount],\n ] as const)\n if (!Number.isSafeInteger(value))\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n `Invoice line ${field} ${value} must be a safe integer number of minor units`,\n );\n return (await super.save(options)) as this;\n }\n\n override async delete(\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n const { deleteInvoiceLineItem } = await import('../document-money.js');\n await deleteInvoiceLineItem(this, options);\n }\n\n /** @internal Aggregate persistence only. */\n async deleteDerived(\n capability: DocumentMoneyWriteCapability,\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n if (capability !== DOCUMENT_MONEY_WRITE)\n throw new Error('Invoice line money write capability is required');\n await super.delete(options);\n }\n\n /**\n * Check if line item has source tracking\n */\n hasSource(): boolean {\n return !!this.sourceType && !!this.sourceId;\n }\n\n /**\n * Check if line item has a service period\n */\n hasPeriod(): boolean {\n return !!this.periodStart && !!this.periodEnd;\n }\n}\n\nexport default InvoiceLineItem;\n","/**\n * InvoiceLineItemCollection - Collection manager for InvoiceLineItem objects\n * @packageDocumentation\n */\n\nimport {\n SmrtCollection,\n type SmrtListOptions,\n type SmrtObject,\n type SmrtSelectedRow,\n type SmrtSelectField,\n type SmrtStiReadScope,\n type SmrtWhereClause,\n} from '@happyvertical/smrt-core';\nimport {\n getTenantId,\n queryGlobal,\n queryWithGlobals,\n} from '@happyvertical/smrt-tenancy';\nimport { DOCUMENT_MONEY_READ_REBIND } from '../document-money-internal.js';\nimport { InvoiceLineItem } from '../models/InvoiceLineItem.js';\nimport { withTenantMoneyCollectionRead } from '../tenant-money-barrier.js';\nimport { assertCommerceMoneySchemaCurrent } from '../upgrade/detect.js';\n\nexport class InvoiceLineItemCollection extends SmrtCollection<InvoiceLineItem> {\n static readonly _itemClass = InvoiceLineItem;\n\n private async withMoneyRead<T>(operation: () => Promise<T>): Promise<T> {\n const rootDb = this.db;\n const tenantId = getTenantId();\n if (!tenantId) {\n // Without a tenant, still refuse a database with old money storage.\n await assertCommerceMoneySchemaCurrent(rootDb);\n return operation();\n }\n const result = await withTenantMoneyCollectionRead(\n rootDb,\n tenantId,\n (boundDb) => this.withDatabase(boundDb, operation),\n );\n if (result instanceof InvoiceLineItem)\n result.rebindMoneyRead(DOCUMENT_MONEY_READ_REBIND, rootDb);\n else if (Array.isArray(result))\n for (const item of result)\n if (item instanceof InvoiceLineItem)\n item.rebindMoneyRead(DOCUMENT_MONEY_READ_REBIND, rootDb);\n return result;\n }\n\n override async get(\n filter: string | SmrtWhereClause<InvoiceLineItem>,\n options: Parameters<SmrtCollection<InvoiceLineItem>['get']>[1] = {},\n ): Promise<InvoiceLineItem | null> {\n return this.withMoneyRead(() => super.get(filter, options));\n }\n\n override async list<\n const Select extends readonly SmrtSelectField<InvoiceLineItem>[],\n >(\n options: Omit<SmrtListOptions<InvoiceLineItem>, 'select' | 'stiScope'> & {\n select: Select;\n include?: never;\n stiScope: SmrtStiReadScope;\n },\n ): Promise<Record<string, unknown>[]>;\n override async list<\n const Select extends readonly SmrtSelectField<InvoiceLineItem>[],\n >(\n options: Omit<SmrtListOptions<InvoiceLineItem>, 'select' | 'stiScope'> & {\n select: Select;\n include?: never;\n stiScope?: undefined;\n },\n ): Promise<SmrtSelectedRow<InvoiceLineItem, Select>[]>;\n override async list(\n options: Omit<SmrtListOptions<InvoiceLineItem>, 'select' | 'stiScope'> & {\n select?: undefined;\n stiScope: SmrtStiReadScope;\n },\n ): Promise<SmrtObject[]>;\n override async list(options?: undefined): Promise<InvoiceLineItem[]>;\n override async list(\n options: Omit<SmrtListOptions<InvoiceLineItem>, 'select' | 'stiScope'> & {\n select?: undefined;\n stiScope?: undefined;\n },\n ): Promise<InvoiceLineItem[]>;\n override async list(\n options:\n | (Omit<SmrtListOptions<InvoiceLineItem>, 'select'> & {\n select?: undefined;\n })\n | undefined,\n ): Promise<SmrtObject[]>;\n override async list(\n options: SmrtListOptions<InvoiceLineItem> | undefined,\n ): Promise<SmrtObject[] | Record<string, unknown>[]>;\n override async list(\n options: SmrtListOptions<InvoiceLineItem> = {},\n ): Promise<SmrtObject[] | Record<string, unknown>[]> {\n return this.withMoneyRead(() => super.list(options));\n }\n\n /**\n * Find line items by invoice\n *\n * @param invoiceId - Invoice ID\n * @returns Array of line items, sorted by sortOrder\n */\n async findByInvoice(invoiceId: string): Promise<InvoiceLineItem[]> {\n return await this.list({\n where: { invoiceId },\n orderBy: 'sortOrder ASC',\n });\n }\n\n /**\n * Find line items by source\n *\n * @param sourceType - Source type (e.g., 'campaign', 'contract')\n * @param sourceId - Source ID\n * @returns Array of line items\n */\n async findBySource(\n sourceType: string,\n sourceId: string,\n ): Promise<InvoiceLineItem[]> {\n return await this.list({\n where: { sourceType, sourceId },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find line items by source type only\n *\n * @param sourceType - Source type (e.g., 'campaign')\n * @returns Array of line items\n */\n async findBySourceType(sourceType: string): Promise<InvoiceLineItem[]> {\n return await this.list({\n where: { sourceType },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Calculate total amount for an invoice\n *\n * @param invoiceId - Invoice ID\n * @returns Total amount\n */\n async getTotalForInvoice(invoiceId: string): Promise<number> {\n const lineItems = await this.findByInvoice(invoiceId);\n return lineItems.reduce((sum, item) => sum + item.amount, 0);\n }\n\n /**\n * Get the next sort order for an invoice\n *\n * @param invoiceId - Invoice ID\n * @returns Next sort order number\n */\n async getNextSortOrder(invoiceId: string): Promise<number> {\n const lineItems = await this.findByInvoice(invoiceId);\n if (lineItems.length === 0) return 0;\n\n const maxSortOrder = Math.max(...lineItems.map((item) => item.sortOrder));\n return maxSortOrder + 1;\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all invoice line items belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of invoice line items for the tenant\n */\n async findByTenant(tenantId: string): Promise<InvoiceLineItem[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global invoice line items (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global invoice line items\n */\n async findGlobal(): Promise<InvoiceLineItem[]> {\n return queryGlobal<InvoiceLineItem>(this);\n }\n\n /**\n * Find invoice line items for a tenant including global line items.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global invoice line items\n */\n async findWithGlobals(tenantId: string): Promise<InvoiceLineItem[]> {\n return queryWithGlobals<InvoiceLineItem>(\n this,\n tenantId,\n 'InvoiceLineItem.findWithGlobals',\n );\n }\n}\n","/** Shared historical/live invoice capacity. This module is private to the owning package. */\nimport { isPostgresDatabase } from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { HistoricalCustomerFundsAllocationCollection } from './collections/HistoricalCustomerFundsCollections.js';\n\nasync function exists(db: DatabaseInterface) {\n return isPostgresDatabase(db)\n ? (\n await db.query('SELECT to_regclass(?) AS relation', [\n 'historical_customer_funds_allocations',\n ])\n ).rows[0]?.relation != null\n : db.tableExists('historical_customer_funds_allocations');\n}\nexport async function historicalInvoiceAllocationTotal(\n db: DatabaseInterface,\n invoiceId: string,\n effect?: 'payment' | 'refund',\n): Promise<number> {\n if (effect === 'refund')\n return historicalCreditRefundCommittedTotal(db, invoiceId);\n if (!(await exists(db)))\n return historicalCreditRefundCommittedTotal(db, invoiceId);\n const tenantId = getTenantId();\n if (!tenantId) {\n if (\n (\n await db.query(\n 'SELECT id FROM historical_customer_funds_allocations WHERE invoice_id=? LIMIT 1',\n invoiceId,\n )\n ).rows.length\n )\n throw new Error(\n 'Historical allocation capacity requires the explicit active tenant',\n );\n return historicalCreditRefundCommittedTotal(db, invoiceId);\n }\n const rows = await (\n await HistoricalCustomerFundsAllocationCollection.create({ db })\n ).list({\n where: { tenantId, invoiceId, effect: 'payment' },\n limit: 100001,\n });\n if (rows.length > 100000)\n throw new Error(\n 'Historical allocation capacity exceeds the bounded source scope',\n );\n const total = rows\n .filter((row) => row.effect === 'payment')\n .reduce((sum, row) => sum + BigInt(row.amountMinor), 0n);\n if (total > BigInt(Number.MAX_SAFE_INTEGER))\n throw new Error('Historical allocation capacity is unsafe');\n const combined =\n total +\n BigInt(\n effect === 'payment'\n ? 0\n : await historicalCreditRefundCommittedTotal(db, invoiceId),\n );\n if (combined > BigInt(Number.MAX_SAFE_INTEGER))\n throw new Error('Historical combined capacity is unsafe');\n return Number(combined);\n}\n\n/** Original cash refunds reserve capacity before their separately preserved allocation arrives. */\nexport async function historicalCreditRefundCommittedTotal(\n db: DatabaseInterface,\n creditNoteId: string,\n): Promise<number> {\n const available = isPostgresDatabase(db)\n ? (\n await db.query('SELECT to_regclass(?) AS relation', [\n 'historical_customer_funds',\n ])\n ).rows[0]?.relation != null\n : await db.tableExists('historical_customer_funds');\n if (!available) return 0;\n const tenantId = getTenantId();\n if (!tenantId) {\n if (\n (\n await db.query(\n 'SELECT id FROM historical_customer_funds WHERE source_credit_note_id=? LIMIT 1',\n creditNoteId,\n )\n ).rows.length\n )\n throw new Error(\n 'Historical refund capacity requires the explicit active tenant',\n );\n return 0;\n }\n const { HistoricalCustomerFundsCollection } = await import(\n './collections/HistoricalCustomerFundsCollections.js'\n );\n const rows = await (\n await HistoricalCustomerFundsCollection.create({ db })\n ).list({\n where: { tenantId, sourceCreditNoteId: creditNoteId, kind: 'refund' },\n limit: 100001,\n });\n if (rows.length > 100000)\n throw new Error(\n 'Historical credit refund source scope exceeds its bounded limit',\n );\n const total = rows.reduce((sum, row) => sum + BigInt(row.amountMinor), 0n);\n if (total > BigInt(Number.MAX_SAFE_INTEGER))\n throw new Error('Historical refund source total is unsafe');\n return Number(total);\n}\n","/** Pure allocation of positive credit requests against frozen invoice lines. */\n\nimport { MoneyValidationError } from './money.js';\nimport { roundSignedRational } from './money-rational.js';\nimport type { InvoiceCreditCalculationBasis } from './types/index.js';\n\nconst QUANTITY_SCALE = 1_000_000n;\n\nfunction quantityUnits(value: string, label: string): bigint {\n if (!/^(?:0|[1-9]\\d*)(?:\\.\\d{1,6})?$/.test(value))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `${label} must be a nonnegative decimal with at most six places`,\n );\n const [whole, fraction = ''] = value.split('.');\n return BigInt(whole) * QUANTITY_SCALE + BigInt(fraction.padEnd(6, '0'));\n}\n\nfunction safeSigned(value: bigint, label: string): number {\n if (\n value < BigInt(Number.MIN_SAFE_INTEGER) ||\n value > BigInt(Number.MAX_SAFE_INTEGER)\n )\n throw new MoneyValidationError('UNSAFE_MONEY', `${label} is unsafe`);\n return Number(value);\n}\n\nfunction magnitude(value: bigint): bigint {\n return value < 0n ? -value : value;\n}\n\nexport interface FrozenCreditSourceLine {\n id: string;\n quantity: string;\n netMinor: number;\n discountMinor: number;\n unitPrice: number;\n discountType: 'none' | 'percent' | 'amount';\n discountRate: string;\n}\n\nexport interface IssuedCreditConsumption {\n creditedLineId: string;\n quantity: string;\n netMinor: number;\n discountMinor: number;\n}\n\nexport type FrozenCreditRequest =\n | { creditedLineId: string; quantity: string }\n | { creditedLineId: string; netMinor: number };\n\nexport interface FrozenCreditAllocation {\n creditedLineId: string;\n basis: InvoiceCreditCalculationBasis;\n netMinor: number | null;\n discountMinor: number;\n /** Signed tax-exclusive source capacity remaining before this allocation. */\n remainingNetMinor: number;\n exhaustsLine: boolean;\n}\n\nexport interface FrozenCreditAllocationResult {\n allocations: FrozenCreditAllocation[];\n exhaustsDocument: boolean;\n}\n\nexport interface AvailableRefundInput {\n sourceTotalMinor: number;\n sourcePaidMinor: number;\n issuedCreditMinor: number;\n settledRefundMinor: number;\n}\n\n/** Exact unapplied credit available to refund across one source invoice. */\nexport function calculateAvailableRefund(input: AvailableRefundInput): number {\n for (const [label, value] of Object.entries(input)) {\n if (!Number.isSafeInteger(value) || value < 0)\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n `${label} must be a nonnegative safe integer`,\n );\n }\n const outstanding = Math.max(\n 0,\n input.sourceTotalMinor - input.sourcePaidMinor,\n );\n return Math.max(\n 0,\n Math.max(0, input.issuedCreditMinor - outstanding) -\n input.settledRefundMinor,\n );\n}\n\n/** Validate caps and normalize flat-discount/final-residual allocation. */\nexport function allocateFrozenCredit(\n sourceLines: readonly FrozenCreditSourceLine[],\n issued: readonly IssuedCreditConsumption[],\n requests: readonly FrozenCreditRequest[],\n): FrozenCreditAllocationResult {\n for (const source of sourceLines) {\n if (\n !source.id ||\n !Number.isSafeInteger(source.netMinor) ||\n !Number.isSafeInteger(source.discountMinor) ||\n !Number.isSafeInteger(source.unitPrice)\n )\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n 'Frozen source money must use safe integer minor units',\n );\n if (quantityUnits(source.quantity, 'Source quantity') <= 0n)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen source quantity must be positive',\n );\n }\n const sources = new Map(sourceLines.map((line) => [line.id, line]));\n if (sources.size !== sourceLines.length || !requests.length)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Credit allocation requires distinct source lines and requests',\n );\n const requestedIds = new Set<string>();\n const consumed = new Map<\n string,\n { quantity: bigint; net: bigint; discount: bigint }\n >();\n for (const row of issued) {\n if (\n !row.creditedLineId ||\n !Number.isInteger(row.netMinor) ||\n !Number.isInteger(row.discountMinor)\n )\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n 'Issued credit money must use safe integer minor units',\n );\n if (\n !Number.isSafeInteger(row.netMinor) ||\n !Number.isSafeInteger(row.discountMinor)\n )\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n 'Issued credit money must use safe integer minor units',\n );\n if (!sources.has(row.creditedLineId))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Issued credit references unknown line ${row.creditedLineId}`,\n );\n const current = consumed.get(row.creditedLineId) ?? {\n quantity: 0n,\n net: 0n,\n discount: 0n,\n };\n const issuedQuantity = quantityUnits(\n row.quantity,\n 'Issued credit quantity',\n );\n current.quantity += issuedQuantity;\n current.net += BigInt(row.netMinor);\n current.discount += BigInt(row.discountMinor);\n consumed.set(row.creditedLineId, current);\n }\n const allocations = [...requests]\n .sort((left, right) =>\n left.creditedLineId < right.creditedLineId ? -1 : 1,\n )\n .map((request): FrozenCreditAllocation => {\n if (requestedIds.has(request.creditedLineId))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'A credit may reference a source line only once',\n );\n requestedIds.add(request.creditedLineId);\n const source = sources.get(request.creditedLineId);\n if (!source)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Unknown credited line ${request.creditedLineId}`,\n );\n const used = consumed.get(source.id) ?? {\n quantity: 0n,\n net: 0n,\n discount: 0n,\n };\n const sourceQuantity = quantityUnits(source.quantity, 'Source quantity');\n const remainingQuantity = sourceQuantity - used.quantity;\n const sourceNet = BigInt(source.netMinor);\n const sourceDiscount = BigInt(source.discountMinor);\n const sign = sourceNet < 0n ? -1n : 1n;\n if (\n (used.net !== 0n && (used.net < 0n ? -1n : 1n) !== sign) ||\n (used.discount !== 0n &&\n sourceDiscount !== 0n &&\n (used.discount < 0n ? -1n : 1n) !== (sourceDiscount < 0n ? -1n : 1n))\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Issued credit signs must match the frozen source line',\n );\n const remainingNet = magnitude(sourceNet) - magnitude(used.net);\n const remainingDiscount =\n magnitude(sourceDiscount) - magnitude(used.discount);\n if (remainingQuantity < 0n || remainingNet < 0n || remainingDiscount < 0n)\n throw new MoneyValidationError(\n 'CREDIT_EXCEEDS_LINE',\n 'Persisted credits already exceed the frozen source line',\n );\n const snapshot = {\n sourceQuantity: source.quantity,\n sourceUnitPrice: source.unitPrice,\n sourceDiscountType: source.discountType,\n sourceDiscountRate: String(source.discountRate),\n sourceDiscountMinor: source.discountMinor,\n } as const;\n if ('quantity' in request) {\n const quantity = quantityUnits(request.quantity, 'Credit quantity');\n if (quantity <= 0n || quantity > remainingQuantity)\n throw new MoneyValidationError(\n 'CREDIT_EXCEEDS_LINE',\n 'Credit quantity exceeds the remaining frozen quantity',\n );\n const exhaustsLine = quantity === remainingQuantity;\n const discountSign = sourceDiscount < 0n ? -1n : 1n;\n const cumulativeDiscount =\n source.discountType === 'none'\n ? 0n\n : roundSignedRational(\n sourceDiscount * (used.quantity + quantity),\n sourceQuantity,\n );\n const incrementalDiscount = cumulativeDiscount - used.discount;\n const boundedIncrementalDiscount =\n incrementalDiscount === 0n ||\n (incrementalDiscount < 0n ? -1n : 1n) !== discountSign\n ? 0n\n : discountSign *\n (magnitude(incrementalDiscount) < remainingDiscount\n ? magnitude(incrementalDiscount)\n : remainingDiscount);\n const discount =\n source.discountType === 'none'\n ? 0n\n : exhaustsLine\n ? discountSign * remainingDiscount\n : boundedIncrementalDiscount;\n return {\n creditedLineId: source.id,\n basis: { kind: 'quantity', quantity: request.quantity, ...snapshot },\n netMinor: null,\n discountMinor: safeSigned(discount, 'Credit discount'),\n remainingNetMinor: safeSigned(\n sign * remainingNet,\n 'Remaining credit net amount',\n ),\n exhaustsLine,\n };\n }\n if (!Number.isSafeInteger(request.netMinor) || request.netMinor <= 0)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Credit net amount must be a positive safe integer',\n );\n const amount = BigInt(request.netMinor);\n if (amount > remainingNet)\n throw new MoneyValidationError(\n 'CREDIT_EXCEEDS_LINE',\n 'Credit net amount exceeds the remaining frozen net amount',\n );\n return {\n creditedLineId: source.id,\n basis: { kind: 'net_amount', netMinor: request.netMinor, ...snapshot },\n netMinor: safeSigned(sign * amount, 'Credit net amount'),\n discountMinor: 0,\n remainingNetMinor: safeSigned(\n sign * remainingNet,\n 'Remaining credit net amount',\n ),\n exhaustsLine: amount === remainingNet,\n };\n });\n const exhausted = new Set(\n allocations\n .filter((row) => row.exhaustsLine)\n .map((row) => row.creditedLineId),\n );\n return {\n allocations,\n exhaustsDocument: sourceLines.every((line) => {\n const used = consumed.get(line.id);\n return (\n magnitude(BigInt(line.netMinor)) - magnitude(used?.net ?? 0n) === 0n ||\n exhausted.has(line.id)\n );\n }),\n };\n}\n","/**\n * PaymentAllocation model - maps payments to invoices\n * @packageDocumentation\n */\n\nimport {\n field,\n foreignKey,\n type SmrtDeleteOptions,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport { allocationOwner, allocationTotal } from '../allocation-integrity.js';\nimport { calculateAvailableRefund } from '../credit-allocation.js';\nimport {\n assertCustomerFundsWrite,\n customerFundsState,\n} from '../customer-funds-internal.js';\nimport {\n historicalCreditRefundCommittedTotal,\n historicalInvoiceAllocationTotal,\n} from '../historical-funds-internal.js';\nimport { assertIntegerMinorUnits } from '../money.js';\nimport type { PaymentAllocationOptions } from '../types/index.js';\n\n/*\n * There is deliberately no `ALLOCATION_EPSILON` here any more (#2401).\n *\n * `Payment.amount`, `Invoice.totalAmount` and this row's `amount` are all\n * integer minor units, so \"does this allocation over-apply the payment\" is an\n * exact integer question. The old `0.01` tolerance let an allocation exceed its\n * payment by up to a cent — real money, not rounding fuzz — and only existed\n * because the payment side was a float.\n */\n\n/**\n * PaymentAllocation tracks how payments are applied to invoices.\n *\n * This enables:\n * - Partial payments (one payment partially covering an invoice)\n * - Split payments (one payment split across multiple invoices)\n * - Payment history per invoice\n *\n * Saves atomically enforce parent existence, matching tenant/customer/currency,\n * positive safe-integer amounts and both balance caps. Use\n * `PaymentAllocationCollection.allocatePayment()` for atomic splits and replay.\n * Direct saves retain caller-owned invoice status refresh.\n *\n * @example\n * ```typescript\n * import { PaymentAllocationCollection } from '@happyvertical/smrt-commerce';\n *\n * // Get the collection\n * const allocations = await PaymentAllocationCollection.create(options);\n *\n * // Check available funds before allocating (all amounts are minor units)\n * const available = await allocations.getUnallocatedFromPayment(\n * payment.id,\n * payment.amount\n * );\n *\n * if (available >= amountToAllocate) {\n * const allocation = await allocations.create({\n * paymentId: payment.id,\n * invoiceId: invoice.id,\n * amount: amountToAllocate,\n * allocatedBy: 'user-uuid'\n * });\n *\n * // Reload the revision advanced by the allocation, then update status.\n * await invoice.loadFromId();\n * const totalAllocated = await allocations.getTotalAllocatedToInvoice(invoice.id);\n * invoice.updatePaymentStatus(totalAllocated);\n * await invoice.save();\n * }\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n // NOTE: `create` and `delete` are intentionally NOT exposed over the\n // generated REST/MCP surface (S5 audit #1390). Allocation rows directly\n // determine an invoice's amountPaid/status and a payment's remaining funds;\n // a generated `create` lets a caller forge an allocation that over-applies a\n // payment, and a generated `delete` lets a caller silently un-apply funds —\n // both falsify balances with no authz. Allocations must be created/removed\n // through application code that re-derives invoice status and respects the\n // payment-amount cap (the cap is also enforced in `save()` as defence in\n // depth).\n //\n // CLI parity (round 6, codex ROOT INSIGHT): the CLI is an independently\n // configured write surface. `cli: true` would still generate create/update/\n // delete commands that forge or un-apply allocations, re-opening the exact\n // vector closed on api/mcp. The CLI is locked to the same read-only surface.\n api: { include: ['list', 'get'] },\n mcp: { include: ['list', 'get'] },\n cli: { include: ['list', 'get'] },\n})\nexport class PaymentAllocation extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /**\n * Tenant ID for multi-tenant isolation\n * Nullable to support both tenant-scoped and global payment allocations\n */\n @tenantId({ nullable: true })\n tenantId: string | null = null;\n\n /**\n * Payment being allocated\n */\n @field({ description: 'The payment being applied.' })\n @foreignKey('Payment')\n paymentId: string = '';\n\n /**\n * Invoice receiving the allocation\n */\n @field({ description: 'The invoice it settles.' })\n @foreignKey('Invoice')\n invoiceId: string = '';\n\n /**\n * Amount allocated from payment to invoice, in **integer minor units**\n * (cents, satoshis).\n *\n * Matches `Invoice.amountPaid` (derived by summing these rows) and\n * `Payment.amount` (which `save()` caps against). All three converted\n * together in #2401 — the cap is a subtraction across the pair, so a\n * dollars-vs-cents split there would reject every allocation as\n * over-applying.\n */\n @field({\n description: 'How much of the payment goes to this invoice.',\n ui: { format: 'money' },\n })\n amount: number = 0;\n\n /**\n * When the allocation was made\n */\n @field({ description: 'When it was applied.' })\n allocatedAt: Date = new Date();\n\n /**\n * User/agent who made the allocation\n */\n allocatedBy: string = '';\n\n /**\n * Notes about the allocation\n */\n @field({ description: 'Anything worth remembering about this allocation.' })\n notes: string = '';\n\n constructor(options: PaymentAllocationOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.paymentId !== undefined) this.paymentId = options.paymentId;\n if (options.invoiceId !== undefined) this.invoiceId = options.invoiceId;\n if (options.amount !== undefined) this.amount = options.amount;\n if (options.allocatedAt !== undefined)\n this.allocatedAt = options.allocatedAt;\n if (options.allocatedBy !== undefined)\n this.allocatedBy = options.allocatedBy;\n if (options.notes !== undefined) this.notes = options.notes;\n }\n\n /** Save and validate against both claimed parent rows in one transaction. */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n assertIntegerMinorUnits('PaymentAllocation', this.id ?? '<new>', [\n ['amount', this.amount],\n ]);\n if (this.amount <= 0)\n throw new Error('PaymentAllocation amount must be a positive number');\n if (!this.paymentId || !this.invoiceId)\n throw new Error('PaymentAllocation requires payment and invoice');\n return this.withTransaction(async () => {\n const { PaymentCollection } = await import(\n '../collections/PaymentCollection.js'\n );\n const { InvoiceCollection } = await import(\n '../collections/InvoiceCollection.js'\n );\n const { PaymentAllocationCollection } = await import(\n '../collections/PaymentAllocationCollection.js'\n );\n const payments = await PaymentCollection.create({ db: this.db });\n const invoices = await InvoiceCollection.create({ db: this.db });\n const allocations = await PaymentAllocationCollection.create({\n db: this.db,\n });\n const prior = this.id\n ? await allocations.get({ id: this.id }, { cache: false })\n : null;\n if (\n prior &&\n (prior.paymentId !== this.paymentId ||\n prior.invoiceId !== this.invoiceId ||\n prior.tenantId !== allocationOwner(this.tenantId))\n ) {\n throw new Error('PaymentAllocation parent identity is immutable');\n }\n const payment = await payments.get(\n { id: this.paymentId },\n { cache: false },\n );\n if (!payment)\n throw new Error('PaymentAllocation referenced Payment does not exist');\n if (\n payment.retentionAccountingBindingJson ||\n payment.ordinaryAccountingBindingJson\n )\n throw new Error('Provider-recorded refund allocation is immutable');\n if (customerFundsState(payment.customerFundsJson))\n assertCustomerFundsWrite(this);\n if (!payment.updated_at)\n throw new Error('Allocation parent has no persisted revision');\n await payment.claimRevision(payment.updated_at);\n const invoice = await invoices.get(\n { id: this.invoiceId },\n { cache: false },\n );\n if (!invoice)\n throw new Error('PaymentAllocation referenced Invoice does not exist');\n if (!invoice.updated_at)\n throw new Error('Allocation parent has no persisted revision');\n await invoice.claimRevision(invoice.updated_at);\n const owner = allocationOwner(this.tenantId);\n if (\n payment.tenantId !== owner ||\n invoice.tenantId !== owner ||\n (payment.customerId || null) !== (invoice.customerId || null) ||\n payment.currency !== invoice.currency\n ) {\n throw new Error(\n 'PaymentAllocation requires matching tenant, customer and currency',\n );\n }\n if (\n (payment.kind === 'refund' && invoice.kind !== 'credit_note') ||\n (payment.kind === 'payment' && invoice.kind !== 'invoice')\n )\n throw new Error(\n 'PaymentAllocation requires payments on invoices and refunds on credit notes',\n );\n const paymentTotal =\n allocationTotal(\n await allocations.findByPayment(this.paymentId),\n this.id,\n ) + BigInt(this.amount);\n const invoiceTotal =\n allocationTotal(\n await allocations.findByInvoice(this.invoiceId),\n this.id,\n ) +\n BigInt(this.amount) +\n BigInt(await historicalInvoiceAllocationTotal(this.db, this.invoiceId));\n if (paymentTotal > BigInt(payment.amount))\n throw new Error('PaymentAllocation would over-apply payment');\n const invoiceCap =\n invoice.kind === 'invoice'\n ? invoice.getPayableFacts().allocationCapMinor\n : invoice.totalAmount;\n if (invoiceTotal > BigInt(Math.max(0, invoiceCap)))\n throw new Error('PaymentAllocation would over-pay invoice');\n if (payment.kind === 'refund') {\n if (payment.status !== 'completed')\n throw new Error('A refund must be settled before it is allocated');\n if (!invoice.creditedInvoiceId)\n throw new Error('Refund credit note has no credited invoice');\n const source = await invoices.get(\n { id: invoice.creditedInvoiceId },\n { cache: false },\n );\n if (!source || source.tenantId !== owner)\n throw new Error('Refund source invoice does not exist in its tenant');\n if (!source.updated_at)\n throw new Error('Refund source invoice has no persisted revision');\n await source.claimRevision(source.updated_at);\n const creditRows = await invoices.list({\n where: { tenantId: owner, creditedInvoiceId: source.id },\n limit: 100001,\n });\n if (creditRows.length > 100000)\n throw new Error(\n 'Refund credit scope exceeds its bounded source limit',\n );\n const credits = creditRows.filter(\n (credit) =>\n credit.kind === 'credit_note' &&\n credit.status !== 'draft' &&\n credit.status !== 'cancelled',\n );\n const issuedCredits = credits.reduce(\n (sum, credit) => sum + credit.totalAmount,\n 0,\n );\n let settledRefunds = 0;\n for (const credit of credits) {\n if (!credit.id) continue;\n settledRefunds += await historicalCreditRefundCommittedTotal(\n this.db,\n credit.id,\n );\n for (const allocation of await allocations.findByInvoice(credit.id)) {\n if (allocation.id === this.id) continue;\n const refund = await payments.get(\n { id: allocation.paymentId },\n { cache: false },\n );\n if (refund?.kind === 'refund' && refund.status === 'completed')\n settledRefunds += allocation.amount;\n }\n }\n const availableRefund = calculateAvailableRefund({\n sourceTotalMinor: source.totalAmount,\n sourcePaidMinor: source.amountPaid,\n issuedCreditMinor: issuedCredits,\n settledRefundMinor: settledRefunds,\n });\n if (this.amount > availableRefund)\n throw new Error(\n `Refund allocation exceeds available unapplied credit (${availableRefund})`,\n );\n }\n return (await super.save(options)) as this;\n });\n }\n /** Managed application reversal must also post its inverse journal and retain evidence. */\n override async delete(options: SmrtDeleteOptions = {}): Promise<void> {\n return this.withTransaction(async () => {\n const persisted = this.id\n ? await this.db.get(this.tableName, { id: this.id })\n : null;\n const { PaymentCollection } = await import(\n '../collections/PaymentCollection.js'\n );\n const payment = await (\n await PaymentCollection.create({ db: this.db })\n ).get(\n { id: String(persisted?.payment_id ?? this.paymentId) },\n { cache: false },\n );\n // Share the Payment revision boundary with provider refund reservation.\n // A stale unbound read must fail before deleting its settlement evidence.\n if (payment) {\n if (!payment.updated_at)\n throw new Error('Allocation parent has no persisted revision');\n await payment.claimRevision(payment.updated_at);\n }\n if (\n payment?.retentionAccountingBindingJson ||\n payment?.ordinaryAccountingBindingJson\n )\n throw new Error(\n 'Provider-recorded refund allocation cannot be deleted',\n );\n if (payment?.customerFundsJson) assertCustomerFundsWrite(this);\n return super.delete(options);\n });\n }\n}\n\nexport default PaymentAllocation;\n","/**\n * PaymentAllocationCollection - Collection manager for PaymentAllocation objects\n * @packageDocumentation\n */\n\nimport { SmrtCollection } from '@happyvertical/smrt-core';\nimport { queryGlobal, queryWithGlobals } from '@happyvertical/smrt-tenancy';\nimport { customerFundsState } from '../customer-funds-internal.js';\nimport { historicalInvoiceAllocationTotal } from '../historical-funds-internal.js';\nimport { PaymentAllocation } from '../models/PaymentAllocation.js';\n\nexport class PaymentAllocationCollection extends SmrtCollection<PaymentAllocation> {\n static readonly _itemClass = PaymentAllocation;\n\n /**\n * Apply a payment to several invoices atomically. Callers supply stable UUID\n * allocation ids: exact replays reuse rows; reusing an id with other money\n * or parents fails. Actor authorization remains the application's job.\n */\n async allocatePayment(input: {\n paymentId: string;\n allocations: { id: string; invoiceId: string; amount: number }[];\n allocatedBy?: string;\n }): Promise<PaymentAllocation[]> {\n if (\n !input.allocations.length ||\n new Set(input.allocations.map((item) => item.id)).size !==\n input.allocations.length\n ) {\n throw new Error(\n 'Allocation batch requires distinct stable allocation ids',\n );\n }\n const { PaymentCollection } = await import('./PaymentCollection.js');\n const { InvoiceCollection } = await import('./InvoiceCollection.js');\n const { InvoiceStatus } = await import('../types/index.js');\n const payments = await PaymentCollection.create({ db: this.db });\n const payment = await payments.get(\n { id: input.paymentId },\n { cache: false },\n );\n if (!payment) throw new Error('Payment does not exist');\n const committed = await payment.withTransaction(async () => {\n if (!payment.updated_at)\n throw new Error('Allocation parent has no persisted revision');\n await payment.claimRevision(payment.updated_at);\n const allocations = await PaymentAllocationCollection.create({\n db: payment.db,\n });\n const invoices = await InvoiceCollection.create({ db: payment.db });\n const result: PaymentAllocation[] = [];\n for (const item of [...input.allocations].sort(\n (a, b) =>\n a.invoiceId.localeCompare(b.invoiceId) || a.id.localeCompare(b.id),\n )) {\n if (!item.id) throw new Error('Allocation requires a stable id');\n const existing = await allocations.get(\n { id: item.id },\n { cache: false },\n );\n if (existing) {\n if (\n existing.paymentId !== input.paymentId ||\n existing.invoiceId !== item.invoiceId ||\n existing.amount !== item.amount ||\n existing.tenantId !== payment.tenantId\n ) {\n throw new Error(\n 'Allocation replay does not match existing allocation',\n );\n }\n result.push(existing);\n } else {\n result.push(\n await allocations.create({\n ...item,\n paymentId: input.paymentId,\n tenantId: payment.tenantId,\n allocatedBy: input.allocatedBy ?? '',\n _insertOnly: true,\n }),\n );\n }\n }\n for (const invoiceId of [\n ...new Set(input.allocations.map((item) => item.invoiceId)),\n ].sort()) {\n const invoice = await invoices.get({ id: invoiceId }, { cache: false });\n if (!invoice) throw new Error('Invoice does not exist');\n if (invoice.status !== InvoiceStatus.WRITTEN_OFF)\n invoice.updatePaymentStatus(\n await allocations.getTotalAllocatedToInvoice(invoiceId),\n );\n await invoice.save();\n }\n return result;\n });\n // Child objects created on the transaction cannot escape with its closed\n // executor. Hydrate results on the caller's executor (possibly an outer tx).\n return Promise.all(\n committed.map(async ({ id }) => {\n const allocation = await this.get({ id: String(id) }, { cache: false });\n if (!allocation)\n throw new Error('Committed allocation no longer exists');\n return allocation;\n }),\n );\n }\n\n /** Remove one allocation atomically and rederive its invoice/credit status. */\n async removeAllocation(id: string): Promise<void> {\n const allocation = await this.get({ id }, { cache: false });\n if (!allocation) return;\n const { PaymentCollection } = await import('./PaymentCollection.js');\n const { InvoiceCollection } = await import('./InvoiceCollection.js');\n const payment = await (await PaymentCollection.create({ db: this.db })).get(\n { id: allocation.paymentId },\n { cache: false },\n );\n if (!payment) throw new Error('Allocation payment does not exist');\n await payment.withTransaction(async () => {\n const rows = await PaymentAllocationCollection.create({ db: payment.db });\n const current = await rows.get({ id }, { cache: false });\n if (!current) return;\n const invoices = await InvoiceCollection.create({ db: payment.db });\n const invoice = await invoices.get(\n { id: current.invoiceId },\n { cache: false },\n );\n if (!invoice) throw new Error('Allocation invoice does not exist');\n await current.delete();\n invoice.updatePaymentStatus(\n await rows.getTotalAllocatedToInvoice(current.invoiceId),\n );\n await invoice.save();\n });\n }\n\n /**\n * Find allocations by payment\n *\n * @param paymentId - Payment ID\n * @returns Array of allocations\n */\n private async allFor(\n field: 'paymentId' | 'invoiceId',\n id: string,\n ): Promise<PaymentAllocation[]> {\n const rows: PaymentAllocation[] = [];\n for (let offset = 0; ; offset += 1000) {\n const page = await this.list({\n where: { [field]: id },\n orderBy: ['allocatedAt DESC', 'id ASC'],\n offset,\n limit: 1000,\n });\n rows.push(...page);\n if (page.length < 1000) return rows;\n }\n }\n async findByPayment(paymentId: string): Promise<PaymentAllocation[]> {\n return this.allFor('paymentId', paymentId);\n }\n /** Every committed row participates in payment and historical combined capacity. */\n async findByInvoice(invoiceId: string): Promise<PaymentAllocation[]> {\n return this.allFor('invoiceId', invoiceId);\n }\n\n /**\n * Get total amount allocated to an invoice\n *\n * @param invoiceId - Invoice ID\n * @returns Total allocated amount\n */\n async getTotalAllocatedToInvoice(invoiceId: string): Promise<number> {\n const allocations = await this.findByInvoice(invoiceId);\n const total = allocations.reduce(\n (sum, alloc) => sum + BigInt(alloc.amount),\n BigInt(await historicalInvoiceAllocationTotal(this.db, invoiceId)),\n );\n if (total > BigInt(Number.MAX_SAFE_INTEGER))\n throw new Error('Combined invoice allocation total is unsafe');\n return Number(total);\n }\n\n /**\n * Get total amount allocated from a payment\n *\n * @param paymentId - Payment ID\n * @returns Total allocated amount\n */\n async getTotalAllocatedFromPayment(paymentId: string): Promise<number> {\n const allocations = await this.findByPayment(paymentId);\n return allocations.reduce((sum, alloc) => sum + alloc.amount, 0);\n }\n\n /**\n * Get remaining unallocated amount from a payment.\n *\n * @param paymentId - Payment ID\n * @param paymentAmount - Total payment amount\n * @returns Unallocated amount\n * @throws Error if payment is over-allocated (indicates data integrity issue)\n */\n async getUnallocatedFromPayment(\n paymentId: string,\n paymentAmount: number,\n ): Promise<number> {\n const allocated = await this.getTotalAllocatedFromPayment(paymentId);\n const { PaymentCollection } = await import('./PaymentCollection.js');\n const payment = await (await PaymentCollection.create({ db: this.db })).get(\n { id: paymentId },\n { cache: false },\n );\n const managed = customerFundsState(payment?.customerFundsJson);\n if (\n managed &&\n (managed.kind !== 'receipt' || payment?.amount !== paymentAmount)\n )\n throw new Error(\n 'Customer funds require their authoritative receipt amount',\n );\n const refunded =\n managed?.operations\n .filter((op) => op.kind === 'refund')\n .reduce((sum, op) => sum + BigInt(op.amountMinor), 0n) ?? 0n;\n const remaining = Number(\n BigInt(paymentAmount) - BigInt(allocated) - refunded,\n );\n\n if (remaining < 0) {\n throw new Error(\n `Over-allocated payment detected for paymentId=${paymentId}: ` +\n `allocated amount (${allocated}) exceeds payment amount (${paymentAmount})`,\n );\n }\n\n return remaining;\n }\n\n /**\n * Find allocation by payment and invoice\n *\n * @param paymentId - Payment ID\n * @param invoiceId - Invoice ID\n * @returns Allocation or null\n */\n async findByPaymentAndInvoice(\n paymentId: string,\n invoiceId: string,\n ): Promise<PaymentAllocation | null> {\n const results = await this.list({\n where: { paymentId, invoiceId },\n limit: 1,\n });\n return results[0] || null;\n }\n\n /**\n * Find allocations by allocator\n *\n * @param allocatedBy - User/agent ID who made the allocation\n * @returns Array of allocations\n */\n async findByAllocator(allocatedBy: string): Promise<PaymentAllocation[]> {\n return await this.list({\n where: { allocatedBy },\n orderBy: 'allocatedAt DESC',\n });\n }\n\n /**\n * Find allocations in date range\n *\n * @param startDate - Start date\n * @param endDate - End date\n * @returns Array of allocations\n */\n async findByDateRange(\n startDate: Date,\n endDate: Date,\n ): Promise<PaymentAllocation[]> {\n return await this.list({\n where: {\n 'allocatedAt >=': startDate.toISOString(),\n 'allocatedAt <=': endDate.toISOString(),\n },\n orderBy: 'allocatedAt DESC',\n });\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all payment allocations belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of payment allocations for the tenant\n */\n async findByTenant(tenantId: string): Promise<PaymentAllocation[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global payment allocations (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global payment allocations\n */\n async findGlobal(): Promise<PaymentAllocation[]> {\n return queryGlobal<PaymentAllocation>(this);\n }\n\n /**\n * Find payment allocations for a tenant including global allocations.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global payment allocations\n */\n async findWithGlobals(tenantId: string): Promise<PaymentAllocation[]> {\n return queryWithGlobals<PaymentAllocation>(\n this,\n tenantId,\n 'PaymentAllocation.findWithGlobals',\n );\n }\n}\n","/**\n * Internal write authority for the connected-payments commands (#3831); not a\n * public bypass. Connected `Payment` rows, connected `PaymentIntent` creation\n * and option writes are refused unless they run inside a command for the\n * row's tenant, so no generated REST, MCP or CLI surface can forge them.\n */\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\nconst authority = new AsyncLocalStorage<{ tenantId: string }>();\n\n/** @internal Run a connected-payments command write for one tenant. */\nexport function withConnectedPaymentAuthority<T>(\n tenantId: string,\n operation: () => Promise<T>,\n): Promise<T> {\n return authority.run({ tenantId: tenantId.toLowerCase() }, operation);\n}\n\n/** @internal Whether a connected-payments command for this tenant is running. */\nexport function hasConnectedPaymentAuthority(\n tenantId: string | null | undefined,\n): boolean {\n const current = authority.getStore();\n return (\n !!current &&\n typeof tenantId === 'string' &&\n current.tenantId === tenantId.toLowerCase()\n );\n}\n","/** Internal authority for the ordinary-invoice coordinator; not a public bypass. */\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\nconst authority = new AsyncLocalStorage<{\n tenantId: string;\n invoiceId: string;\n}>();\nexport function withOrdinaryAccountingAuthority<T>(\n tenantId: string,\n invoiceId: string,\n operation: () => Promise<T>,\n): Promise<T> {\n return authority.run({ tenantId, invoiceId }, operation);\n}\nexport function hasOrdinaryAccountingAuthority(\n tenantId: string | null | undefined,\n invoiceId: string | null | undefined,\n): boolean {\n const current = authority.getStore();\n return (\n !!current &&\n current.tenantId === tenantId &&\n current.invoiceId === invoiceId\n );\n}\n","/** Internal authority for the retained-invoice coordinator; not a public bypass. */\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\nconst authority = new AsyncLocalStorage<{\n tenantId: string;\n invoiceId: string;\n}>();\nexport function withRetentionAccountingAuthority<T>(\n tenantId: string,\n invoiceId: string,\n operation: () => Promise<T>,\n): Promise<T> {\n return authority.run({ tenantId, invoiceId }, operation);\n}\nexport function hasRetentionAccountingAuthority(\n tenantId: string | null | undefined,\n invoiceId: string | null | undefined,\n): boolean {\n const current = authority.getStore();\n return (\n !!current &&\n current.tenantId === tenantId &&\n current.invoiceId === invoiceId\n );\n}\n","import { hasConnectedPaymentAuthority } from '../connected-payments-internal.js';\nimport { hasOrdinaryAccountingAuthority } from '../ordinary-accounting-internal.js';\nimport { hasRetentionAccountingAuthority } from '../retention-accounting-internal.js';\n/**\n * Payment model - tracks payments with ledger integration\n * @packageDocumentation\n */\n\nimport {\n crossPackageRef,\n field,\n foreignKey,\n type SmrtDeleteOptions,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport type { Journal } from '@happyvertical/smrt-ledgers';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport {\n assertAllocationParent,\n claimAllocationParent,\n} from '../allocation-integrity.js';\nimport {\n assertCustomerFundsWrite,\n customerFundsState,\n} from '../customer-funds-internal.js';\nimport { assertIntegerMinorUnits } from '../money.js';\nimport {\n type PaymentKind,\n PaymentMethod,\n type PaymentOptions,\n PaymentStatus,\n type RecordPaymentOptions,\n} from '../types/index.js';\n\n/**\n * Legal status transitions for a Payment, keyed by the prior persisted status.\n * A status mapping to itself (no-op re-save) is always permitted and handled\n * separately. Transitioning *into* COMPLETED is additionally gated on the\n * verified `recordPayment()` settlement path (see {@link Payment.save}) — it is\n * never reachable by raw mass-assignment even though it appears here as a\n * structurally legal edge out of PENDING.\n *\n * Forward path: PENDING → COMPLETED, with FAILED / CANCELLED as alternate\n * terminal exits from PENDING and REFUNDED reachable from COMPLETED.\n */\nconst PAYMENT_STATUS_TRANSITIONS: Record<PaymentStatus, PaymentStatus[]> = {\n [PaymentStatus.PENDING]: [\n PaymentStatus.COMPLETED,\n PaymentStatus.FAILED,\n PaymentStatus.CANCELLED,\n ],\n // A completed payment can only be reversed via refund.\n [PaymentStatus.COMPLETED]: [PaymentStatus.REFUNDED],\n // Terminal states.\n [PaymentStatus.FAILED]: [],\n [PaymentStatus.REFUNDED]: [],\n [PaymentStatus.CANCELLED]: [],\n};\n\n/**\n * Module-scoped record of the status each Payment instance was loaded with,\n * so the save-time transition guard can compare the prior persisted status\n * against the one being written without adding a persisted column. WeakMap\n * keeps it out of the schema and GCs with the instance — same pattern as the\n * other commerce models.\n */\nconst loadedPaymentStatus = new WeakMap<Payment, PaymentStatus>();\n\n/**\n * Marks instances whose transition into COMPLETED is being driven by the\n * verified `recordPayment()` settlement path (which posts a balanced journal\n * before flipping the status). The save-time guard consults this set so that\n * COMPLETED can only be reached through that path, never via raw\n * mass-assignment on the generated update route. Cleared after the save runs.\n */\nconst settlementInProgress = new WeakSet<Payment>();\n\n/** Prevent concurrent temporary executor bindings on the same instance. */\nconst recordingInProgress = new WeakSet<Payment>();\n\n/**\n * Payment represents a financial transaction against a contract.\n *\n * Payments can be integrated with smrt-ledgers to automatically\n * create balanced journal entries for proper accounting.\n *\n * @example\n * ```typescript\n * // Create a payment\n * const payment = await payments.create({\n * contractId: order.id,\n * customerId: customer.id,\n * amount: 1500.00,\n * method: PaymentMethod.CREDIT_CARD,\n * transactionId: 'stripe_pi_123456'\n * });\n *\n * // Record with ledger integration\n * await payment.recordPayment({\n * ledgerId: ledger.id,\n * receivablesAccountId: arAccount.id,\n * cashAccountId: bankAccount.id\n * });\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n display: {\n list: ['reference', 'customerId', 'status', 'amount', 'paidAt'],\n label: 'reference',\n },\n // ROOT FIX (S5 audit #1390 round 4): restrict the generated create/update\n // write surface so privileged / settlement-derived fields can NEVER be set\n // by a generated REST/MCP route — closing the status-mass-assignment vector\n // at the surface rather than relying solely on the save()-time guard.\n //\n // `writable` is an allowlist: only these fields survive applyWritablePolicy()\n // on BOTH create and update (#1540). Deliberately EXCLUDED:\n // - `status` — a COMPLETED Payment is settlement proof downstream\n // (PaymentIntent's PAID verification trusts it). It is only ever set by\n // the verified `recordPayment()` settlement path, never by a caller. This\n // closes the \"forged COMPLETED Payment via api.create\" vector (codex HIGH#1).\n // - `journalId` / `paidAt` — settlement-derived, written by recordPayment().\n // - `syncedAt` — provider-sync bookkeeping, written by the sync path.\n api: {\n include: ['list', 'get', 'create', 'update'],\n writable: [\n 'kind',\n 'contractId',\n 'customerId',\n 'amount',\n 'currency',\n 'method',\n 'transactionId',\n 'reference',\n 'notes',\n 'externalId',\n 'externalProvider',\n 'backendId',\n 'backendTxRef',\n 'nativeAmount',\n 'nativeCurrency',\n 'usdAtQuote',\n 'usdAtConfirmation',\n ],\n },\n // NOTE: `recordPayment` is intentionally NOT exposed over MCP. It posts a\n // balanced journal into smrt-ledgers (moves money in the books) and flips\n // the payment to COMPLETED — not safe as an unguarded MCP tool that carries\n // no authz. Financial mutations must go through application code that\n // enforces permissions. The MCP create/update tools honor the same\n // `api.writable` allowlist above, so `status` is unsettable over MCP too.\n // See S5 audit #1390.\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class Payment extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /** Incoming settlement or outgoing customer refund. Amount stays positive. */\n kind: PaymentKind = 'payment';\n\n /** Command-owned customer advance policy, source lineage and append-only application evidence. */\n @field({ type: 'text' })\n customerFundsJson = '';\n\n /** Immutable provider-refund source binding owned by retention commands. */\n @field({ sensitive: true })\n retentionAccountingBindingJson = '';\n\n /** Immutable ordinary-provider refund reservation; command-owned. */\n @field({ sensitive: true })\n ordinaryAccountingBindingJson = '';\n\n /**\n * Tenant ID for multi-tenant isolation\n * Nullable to support both tenant-scoped and global payments\n */\n @tenantId({ nullable: true })\n tenantId: string | null = null;\n\n /**\n * Contract this payment is for\n */\n @field({ description: 'The order or agreement this payment is for, if any.' })\n @foreignKey('SalesContract', { validateSubtype: true })\n contractId: string | null = null;\n\n /**\n * Customer who made the payment\n */\n @field({ description: 'The customer who paid.' })\n @foreignKey('Customer')\n customerId: string | null = null;\n\n /**\n * Payment amount, in **integer minor units** (cents, satoshis).\n *\n * The `= 0` initializer is load-bearing: SMRT maps an integer literal to\n * INTEGER and a decimal literal to DECIMAL. This is the figure\n * `PaymentAllocation.save()` caps allocations against and `Invoice.amountPaid`\n * is summed from, so it must be in the same exact units as both (#2401).\n */\n @field({ description: 'How much was paid.', ui: { format: 'money' } })\n amount: number = 0;\n\n /**\n * Currency code (ISO 4217)\n */\n @field({\n description: 'The currency of the payment.',\n ui: { widget: 'currency' },\n })\n currency: string = 'USD';\n\n /**\n * Payment method used\n */\n @field({\n description: 'How it was paid: cash, check, card, bank transfer or other.',\n })\n method: PaymentMethod = PaymentMethod.BANK_TRANSFER;\n\n /**\n * Current payment status\n */\n @field({\n description:\n 'Whether the payment is pending, completed, failed or refunded.',\n })\n status: PaymentStatus = PaymentStatus.PENDING;\n\n /**\n * External transaction ID (from payment processor)\n */\n transactionId: string = '';\n\n /**\n * Internal reference number\n */\n @field({ description: 'A reference note, such as a check number.' })\n reference: string = '';\n\n /**\n * Link to smrt-ledgers Journal (created by recordPayment)\n */\n @crossPackageRef('@happyvertical/smrt-ledgers:Journal')\n journalId: string = '';\n\n /**\n * When the payment was completed\n */\n @field({ description: 'When the money was received.' })\n paidAt: Date | null = null;\n\n /**\n * Notes about the payment\n */\n @field({\n description: 'Anything worth remembering about this payment.',\n ui: { widget: 'textarea' },\n })\n notes: string = '';\n\n // ============================================================================\n // Provider Sync\n // ============================================================================\n\n /**\n * External ID in accounting provider (e.g., QBO payment ID)\n */\n externalId: string = '';\n\n /**\n * Accounting provider name ('quickbooks' | 'stripe' | 'paypal' | etc.)\n */\n externalProvider: string = '';\n\n /**\n * When payment was last synced to provider\n */\n syncedAt: Date | null = null;\n\n // ============================================================================\n // Payment Backend Identity\n // ============================================================================\n //\n // When a `PaymentBackend` adapter (Stripe, x402, BTC RPC, etc.) brokers the\n // payment rather than a human recording it manually, we capture the\n // backend identity, its native-currency amount, and the USD valuation at\n // both quote and confirmation time. The classic `amount` / `currency`\n // pair stays as the canonical settlement number; the backend fields\n // describe *how* the funds arrived and let downstream accounting close\n // the loop on volatile-currency drift.\n //\n // All fields default to empty / zero so existing Payment consumers that\n // pre-date the marketplace adapter machinery are unaffected.\n\n /**\n * Stable identifier of the `PaymentBackend` adapter that served this\n * payment — e.g. `base-usdc`, `solana-usdc`, `btc`, `stripe`, `paypal`.\n *\n * Distinct from {@link externalProvider}: `backendId` names the SMRT\n * payment-rail adapter, while `externalProvider` names a downstream\n * accounting destination (QuickBooks, Stripe-the-accounting-source).\n * The same Stripe payment will have `backendId: 'stripe'` AND\n * `externalProvider: 'stripe'`; a crypto payment synced to QBO will\n * have `backendId: 'base-usdc'` and `externalProvider: 'quickbooks'`.\n */\n backendId: string = '';\n\n /**\n * Chain transaction hash or backend aggregator's reference id. For\n * on-chain payments this is the tx hash (`0x...` on EVM, base58 sig on\n * Solana, txid on Bitcoin); for fiat-rail backends it's the gateway's\n * own reference. Kept distinct from {@link transactionId} so consumers\n * that already populate `transactionId` with a provider-internal id\n * don't have to overload it.\n */\n backendTxRef: string = '';\n\n /**\n * The amount the backend actually moved, in the native currency's own\n * **integer minor units** — satoshis for BTC, cents for a fiat rail. For\n * stablecoin rails this typically equals `amount`; for volatile-currency\n * rails it's the satoshi/wei figure the chain recorded, independent of any\n * USD valuation.\n *\n * Fresh PostgreSQL and DuckDB INTEGER columns materialize as BIGINT. SMRT\n * rejects values outside JavaScript's safe-integer range during hydration;\n * existing PostgreSQL `int4` columns require the explicit widening in #2424.\n */\n nativeAmount: number = 0;\n\n /**\n * Code identifying the native currency `nativeAmount` is denominated\n * in. Mirrors the `backendId` namespacing convention — `USDC-base`,\n * `BTC`, `ETH`, `USD-stripe`. Empty string means \"use {@link currency}\"\n * (i.e. the payment was already quoted and settled in the same\n * currency).\n */\n nativeCurrency: string = '';\n\n /**\n * USD valuation of the payment at the moment the price was quoted to\n * the buyer (typically the moment a `PaymentIntent` was issued), in\n * **integer minor units** (US cents). A default of `0` means no USD-quote\n * snapshot was taken (the payment was already USD-denominated or the\n * backend doesn't require drift accounting).\n *\n * This is money describing the same funds as {@link Payment.amount}, so it\n * converts with it — a dollars-here / cents-there split would make\n * {@link Payment.usdDrift} meaningless (#2401).\n */\n usdAtQuote: number = 0;\n\n /**\n * USD valuation of the payment at the moment it was confirmed on the\n * backend (chain confirmation, gateway settlement, etc.), in integer minor\n * units (US cents). The delta between {@link usdAtQuote} and\n * `usdAtConfirmation` is the USD drift the operator absorbs (positive or\n * negative) when accepting payment in a volatile native currency (#2401).\n */\n usdAtConfirmation: number = 0;\n\n /**\n * Connected provider account (for example a Stripe `acct_...`) a direct\n * charge was made on, so the funds landed with the tenant rather than the\n * platform (#3831). Empty for platform-account and non-Connect payments.\n * Server-managed: written only by the connected-payments commands.\n */\n connectedAccountId: string = '';\n\n /** Immutable host-supplied agreement/fee-policy version at checkout pricing. */\n connectedFeePolicyRef: string = '';\n\n /**\n * Platform application fee the provider withheld from this direct charge,\n * in integer minor units of {@link currency} (#3831). Settled at source, so\n * {@link recordPayment} books it as a fee expense, never as a payable.\n * Server-managed.\n */\n applicationFeeCents: number = 0;\n\n /**\n * For a `refund` of a connected charge, the `Payment` it refunds (#3831).\n * Server-managed.\n */\n refundOfPaymentId: string = '';\n\n /**\n * For a `refund` of a connected charge, the provider's refund state:\n * `requested` (recorded before the provider is asked), `pending` until the\n * provider confirms it, then `succeeded` (journaled) or `failed` (#3831).\n * Server-managed.\n */\n connectedRefundState: '' | 'requested' | 'pending' | 'succeeded' | 'failed' =\n '';\n\n /**\n * For a `refund` of a connected charge, whether the platform's application\n * fee was refunded with it (#3831). Server-managed and frozen.\n */\n applicationFeeRefunded: boolean = false;\n\n constructor(options: PaymentOptions = {}) {\n super(options);\n if (options.kind !== undefined) this.kind = options.kind;\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.contractId !== undefined) this.contractId = options.contractId;\n if (options.customerId !== undefined) this.customerId = options.customerId;\n if (options.amount !== undefined) this.amount = options.amount;\n if (options.currency !== undefined) this.currency = options.currency;\n if (options.method !== undefined) this.method = options.method;\n if (options.status !== undefined) this.status = options.status;\n if (options.transactionId !== undefined)\n this.transactionId = options.transactionId;\n if (options.reference !== undefined) this.reference = options.reference;\n if (options.customerFundsJson !== undefined)\n this.customerFundsJson = options.customerFundsJson;\n if (options.journalId !== undefined) this.journalId = options.journalId;\n if (options.paidAt !== undefined) this.paidAt = options.paidAt;\n if (options.notes !== undefined) this.notes = options.notes;\n if (options.externalId !== undefined) this.externalId = options.externalId;\n if (options.externalProvider !== undefined)\n this.externalProvider = options.externalProvider;\n if (options.syncedAt !== undefined) this.syncedAt = options.syncedAt;\n if (options.backendId !== undefined) this.backendId = options.backendId;\n if (options.backendTxRef !== undefined)\n this.backendTxRef = options.backendTxRef;\n if (options.nativeAmount !== undefined)\n this.nativeAmount = options.nativeAmount;\n if (options.nativeCurrency !== undefined)\n this.nativeCurrency = options.nativeCurrency;\n if (options.usdAtQuote !== undefined) this.usdAtQuote = options.usdAtQuote;\n if (options.usdAtConfirmation !== undefined)\n this.usdAtConfirmation = options.usdAtConfirmation;\n if (options.connectedAccountId !== undefined)\n this.connectedAccountId = options.connectedAccountId;\n if (options.connectedFeePolicyRef !== undefined)\n this.connectedFeePolicyRef = options.connectedFeePolicyRef;\n if (options.applicationFeeCents !== undefined)\n this.applicationFeeCents = options.applicationFeeCents;\n if (options.refundOfPaymentId !== undefined)\n this.refundOfPaymentId = options.refundOfPaymentId;\n if (options.connectedRefundState !== undefined)\n this.connectedRefundState = options.connectedRefundState;\n if (options.applicationFeeRefunded !== undefined)\n this.applicationFeeRefunded = options.applicationFeeRefunded;\n }\n\n /**\n * Capture the persisted status the row was loaded with, so the save-time\n * transition guard can reject illegal status flips made via raw field\n * assignment (mass-assignment on the generated update route, a stale caller,\n * etc.). Freshly-constructed (not-yet-saved) payments have no prior status.\n */\n override async initialize(): Promise<this> {\n await super.initialize();\n if (await this.isSaved()) {\n loadedPaymentStatus.set(this, this.status);\n }\n return this;\n }\n\n /**\n * Save-time state-machine guard (S5 audit #1390).\n *\n * `status` is mass-assignable on the generated update/create routes, and a\n * COMPLETED Payment is treated as settlement proof downstream — e.g.\n * {@link PaymentIntent}'s PAID verification trusts a COMPLETED Payment row.\n * A forged `status: 'completed'` (with arbitrary amounts and no journal)\n * would therefore satisfy that check without any money having moved.\n *\n * This guard enforces two things:\n * - **Transitions must be legal** per {@link PAYMENT_STATUS_TRANSITIONS}.\n * - **Reaching COMPLETED requires the verified settlement path — on\n * creation AND on update.** Only `recordPayment()` (which posts a\n * balanced journal and links `journalId`) may write a Payment into\n * COMPLETED; it announces itself via {@link settlementInProgress}. A raw\n * `status: 'completed'` is rejected whether the row is brand-new (a\n * GENESIS `create({ status: 'completed' })`) or already persisted — that\n * is the exact path a forged COMPLETED would take to satisfy\n * PaymentIntent's PAID verification without any money having moved (codex\n * HIGH#1, #1390 round 5). There is deliberately NO import/fixture\n * carve-out: a COMPLETED Payment is settlement proof downstream, so it is\n * only ever reachable through `recordPayment()`, on every surface\n * (REST/MCP/CLI/direct). Fixtures/migrations that need a completed payment\n * must drive it through `recordPayment()` (or start non-COMPLETED).\n */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n return this.withTransaction(async () => {\n if (this.kind !== 'payment' && this.kind !== 'refund')\n throw new Error('Payment kind must be payment or refund');\n // The claim is the compare-and-swap against the caller's revision (it\n // advances the stored revision), so the guarded save below must expect\n // the revision the claim just wrote, not the caller's pre-claim one.\n const callerRevision = options?.expectedUpdatedAt;\n await claimAllocationParent(this, 'payment', callerRevision);\n if (callerRevision !== undefined && this.updated_at)\n options = { ...options, expectedUpdatedAt: this.updated_at };\n // Whole minor units on every surface (#2401). `amount` is what\n // `PaymentAllocation.save()` and `Payout.assertCappedBySourcePayment()` cap\n // against with exact integer comparisons, so a fractional major-unit write\n // here would silently break both.\n assertIntegerMinorUnits('Payment', this.id || '<new>', [\n ['amount', this.amount],\n ['nativeAmount', this.nativeAmount],\n ['usdAtQuote', this.usdAtQuote],\n ['usdAtConfirmation', this.usdAtConfirmation],\n ['applicationFeeCents', this.applicationFeeCents],\n ]);\n if (\n this.applicationFeeCents < 0 ||\n this.applicationFeeCents > this.amount\n )\n throw new Error(\n 'Payment applicationFeeCents must be between 0 and the payment amount',\n );\n if (this.applicationFeeCents > 0 && !this.connectedAccountId)\n throw new Error(\n 'Payment applicationFeeCents requires a connectedAccountId',\n );\n\n const priorRow = await this.loadPersistedRow();\n if (this.customerFundsJson || priorRow?.customer_funds_json)\n assertCustomerFundsWrite(this);\n const ordinaryBinding =\n this.ordinaryAccountingBindingJson ||\n String(priorRow?.ordinary_accounting_binding_json ?? '');\n if (\n ordinaryBinding &&\n !hasOrdinaryAccountingAuthority(\n this.tenantId,\n JSON.parse(ordinaryBinding).invoiceId,\n )\n )\n throw new Error(\n 'Provider-recorded refund is immutable; use an explicit reversal workflow',\n );\n const retainedBinding =\n this.retentionAccountingBindingJson ||\n String(priorRow?.retention_accounting_binding_json ?? '');\n if (\n retainedBinding &&\n !hasRetentionAccountingAuthority(\n this.tenantId,\n JSON.parse(retainedBinding).invoiceId,\n )\n )\n throw new Error(\n 'Provider-recorded refund is immutable; use an explicit reversal workflow',\n );\n // A connected charge or refund (#3831) is written only by the\n // connected-payments commands: its identity and amounts never change\n // after the first write, even while PENDING (a pending refund can wait\n // days for the provider), so a generated update cannot repoint it.\n // Every write of a connected row (creation, refund state, provider\n // reference, status) must come from a connected-payments command for\n // its tenant: generated surfaces (including CLI `--from-file`) cannot\n // forge or advance one.\n if (\n (this.connectedAccountId ||\n String(priorRow?.connected_account_id ?? '')) &&\n !hasConnectedPaymentAuthority(this.tenantId)\n )\n throw new Error(\n 'Connected payments and refunds are written only by the connected-payments commands',\n );\n if (priorRow && String(priorRow.connected_account_id ?? ''))\n this.assertConnectedIdentityUnchanged(priorRow);\n const prior =\n priorRow && priorRow.status != null\n ? (priorRow.status as PaymentStatus)\n : loadedPaymentStatus.get(this);\n this.assertStatusTransition(prior);\n\n // Reaching COMPLETED is only legal via the verified settlement path,\n // regardless of whether this is a create (no persisted prior) or an\n // update of an existing row. A GENESIS create with status: COMPLETED would\n // otherwise forge settlement proof, so we close it here at the model level\n // — this covers EVERY surface (REST/MCP/CLI/direct), independent of any\n // writable-allowlist gap on a given surface.\n const reachingCompleted =\n this.status === PaymentStatus.COMPLETED &&\n prior !== PaymentStatus.COMPLETED;\n if (reachingCompleted && !settlementInProgress.has(this)) {\n throw new Error(\n `Payment ${this.id || '<new>'}: cannot set status to COMPLETED via ` +\n 'raw assignment — a Payment is only completed through ' +\n 'recordPayment(), which posts a balanced settlement journal. Use ' +\n 'recordPayment() instead of setting status directly.',\n );\n }\n\n // Freeze the settled monetary fields once the row is already COMPLETED in\n // the database (S5 audit #1390 round 6 — codex ROOT INSIGHT; mirrors the\n // PaymentIntent PAID backing-field freeze). A COMPLETED Payment is\n // settlement proof: its amount was reconciled against a balanced journal\n // and is trusted downstream — PaymentIntent's PAID verification binds the\n // winning option's `nativeAmount` to `Payment.amount` / `nativeAmount`. The\n // only legal exit from COMPLETED is COMPLETED → REFUNDED, and a refund must\n // NOT alter the settled figures. Closing this at the model level covers\n // EVERY surface (REST/MCP/CLI/direct), independent of the per-surface\n // writable allowlist (`amount`/`currency`/`nativeAmount`/... are writable\n // on a *new* PENDING payment, but must be immutable once settled).\n if (\n priorRow &&\n (priorRow.status === PaymentStatus.COMPLETED ||\n priorRow.status === PaymentStatus.REFUNDED)\n ) {\n this.assertSettledAmountsUnchanged(priorRow);\n }\n\n try {\n await assertAllocationParent(this, 'payment');\n const result = (await super.save(options)) as this;\n loadedPaymentStatus.set(this, this.status);\n return result;\n } finally {\n settlementInProgress.delete(this);\n }\n });\n }\n\n /** Managed receipts and refunds retain immutable cash provenance. */\n override async delete(options: SmrtDeleteOptions = {}): Promise<void> {\n const persisted = await this.loadPersistedRow();\n if (\n this.retentionAccountingBindingJson ||\n persisted?.retention_accounting_binding_json ||\n this.ordinaryAccountingBindingJson ||\n persisted?.ordinary_accounting_binding_json\n )\n throw new Error('Provider-recorded refund history cannot be deleted');\n if (this.customerFundsJson || persisted?.customer_funds_json)\n throw new Error('Customer funds cash history cannot be deleted');\n if (this.connectedAccountId || persisted?.connected_account_id)\n throw new Error('Connected payment and refund history cannot be deleted');\n return super.delete(options);\n }\n\n /** See the connected-row guard in {@link save}. */\n private assertConnectedIdentityUnchanged(\n priorRow: Record<string, unknown>,\n ): void {\n const frozen: Array<[string, unknown, unknown]> = [\n ['kind', String(priorRow.kind ?? 'payment'), this.kind],\n [\n 'connectedFeePolicyRef',\n String(priorRow.connected_fee_policy_ref ?? ''),\n this.connectedFeePolicyRef,\n ],\n ['amount', Number(priorRow.amount ?? 0), Number(this.amount ?? 0)],\n ['currency', String(priorRow.currency ?? ''), this.currency],\n ['backendId', String(priorRow.backend_id ?? ''), this.backendId],\n [\n // A refund's provider reference is set once, in the same write that\n // records the provider's answer (connectedRefundState leaves\n // `requested`, which no generated surface can write).\n 'backendTxRef',\n !priorRow.backend_tx_ref &&\n priorRow.connected_refund_state === 'requested' &&\n this.connectedRefundState !== 'requested'\n ? this.backendTxRef\n : String(priorRow.backend_tx_ref ?? ''),\n this.backendTxRef,\n ],\n [\n 'transactionId',\n String(priorRow.transaction_id ?? ''),\n this.transactionId,\n ],\n [\n 'connectedAccountId',\n String(priorRow.connected_account_id ?? ''),\n this.connectedAccountId,\n ],\n [\n 'applicationFeeCents',\n Number(priorRow.application_fee_cents ?? 0),\n Number(this.applicationFeeCents ?? 0),\n ],\n [\n 'refundOfPaymentId',\n String(priorRow.refund_of_payment_id ?? ''),\n this.refundOfPaymentId,\n ],\n [\n 'applicationFeeRefunded',\n Boolean(priorRow.application_fee_refunded),\n Boolean(this.applicationFeeRefunded),\n ],\n ];\n for (const [field, prior, next] of frozen)\n if (prior !== next)\n throw new Error(\n `Payment ${this.id}: '${field}' of a connected payment or refund cannot change`,\n );\n }\n\n /**\n * Reject an illegal status flip. Compares the about-to-be-written status\n * against the status the row was loaded with. No-op transitions (status\n * unchanged) and brand-new rows (no prior) are always allowed — the\n * COMPLETED-specific settlement requirement is enforced separately in\n * {@link save}.\n */\n private assertStatusTransition(prior: PaymentStatus | undefined): void {\n if (prior === undefined) return; // new row — any starting status (subject to the COMPLETED gate)\n if (prior === this.status) return; // no-op re-save\n const allowed = PAYMENT_STATUS_TRANSITIONS[prior] ?? [];\n if (!allowed.includes(this.status)) {\n throw new Error(\n `Payment ${this.id}: illegal status transition '${prior}' → '${this.status}'. ` +\n 'Use the guarded helpers (recordPayment / markFailed / cancel).',\n );\n }\n }\n\n /**\n * Load the AUTHORITATIVE persisted row (S5 audit #1390 round 4, codex HIGH#1;\n * extended round 6 to carry the full row, not just `status`). The WeakMap is\n * only populated when {@link initialize} loaded the row from the DB; it is\n * empty for an instance built via\n * `collection.create({ id: <existing>, _skipLoad: true })` — the upsert path\n * that lets a caller write onto an existing row without hydrating it. Trusting\n * an empty WeakMap there would treat the write as a brand-new row and skip the\n * transition guard entirely (a poisonable prior-state).\n *\n * So when this instance carries an `id`, read the persisted row straight from\n * the database and use it as the prior — a create-onto-existing is an update.\n * Returns `undefined` when no row exists (truly new), so callers fall back to\n * the WeakMap (which is also empty then). Settled-amount columns are\n * snake_case (`native_amount`, etc.); the `status` column is single-word.\n */\n private async loadPersistedRow(): Promise<\n Record<string, unknown> | undefined\n > {\n if (!this.id) return undefined;\n try {\n const row = await this.getCanonicalPersistedRow({ id: this.id });\n return row ?? undefined;\n } catch {\n // DB not ready / table absent — treat as new (in-memory fallback in save).\n return undefined;\n }\n }\n\n /**\n * Reject any change to the settled monetary fields of an already-COMPLETED\n * Payment (S5 audit #1390 round 6 — codex ROOT INSIGHT). Mirrors the\n * PaymentIntent PAID backing-field freeze. A COMPLETED Payment's economic\n * identity is settled: the `amount` was posted into a balanced journal and is\n * the figure PaymentIntent's PAID verification reconciles against, so it (and\n * the native/USD valuation fields that describe the same money) must not\n * drift. The only legal status move out of COMPLETED is REFUNDED, which\n * reverses the funds without rewriting how much they were. Compared against\n * the AUTHORITATIVE persisted row so the freeze holds on every surface, even\n * for an un-hydrated create-onto-existing upsert.\n */\n private assertSettledAmountsUnchanged(\n priorRow: Record<string, unknown>,\n ): void {\n const frozen: Array<[string, number | string, number | string]> = [\n ['journalId', String(priorRow.journal_id ?? ''), this.journalId ?? ''],\n [\n 'paidAt',\n priorRow.paid_at == null\n ? ''\n : new Date(priorRow.paid_at as string | number).getTime(),\n this.paidAt == null ? '' : new Date(this.paidAt).getTime(),\n ],\n ['kind', String(priorRow.kind ?? 'payment'), this.kind],\n [\n 'connectedFeePolicyRef',\n String(priorRow.connected_fee_policy_ref ?? ''),\n this.connectedFeePolicyRef,\n ],\n ['amount', Number(priorRow.amount ?? 0), Number(this.amount ?? 0)],\n [\n 'currency',\n String(priorRow.currency ?? ''),\n String(this.currency ?? ''),\n ],\n [\n 'nativeAmount',\n Number(priorRow.native_amount ?? 0),\n Number(this.nativeAmount ?? 0),\n ],\n [\n 'nativeCurrency',\n String(priorRow.native_currency ?? ''),\n String(this.nativeCurrency ?? ''),\n ],\n [\n 'usdAtQuote',\n Number(priorRow.usd_at_quote ?? 0),\n Number(this.usdAtQuote ?? 0),\n ],\n [\n 'usdAtConfirmation',\n Number(priorRow.usd_at_confirmation ?? 0),\n Number(this.usdAtConfirmation ?? 0),\n ],\n [\n 'connectedAccountId',\n String(priorRow.connected_account_id ?? ''),\n String(this.connectedAccountId ?? ''),\n ],\n [\n 'applicationFeeCents',\n Number(priorRow.application_fee_cents ?? 0),\n Number(this.applicationFeeCents ?? 0),\n ],\n [\n 'refundOfPaymentId',\n String(priorRow.refund_of_payment_id ?? ''),\n String(this.refundOfPaymentId ?? ''),\n ],\n [\n 'connectedRefundState',\n String(priorRow.connected_refund_state ?? ''),\n String(this.connectedRefundState ?? ''),\n ],\n ];\n for (const [field, prior, next] of frozen) {\n if (prior !== next) {\n throw new Error(\n `Payment ${this.id}: '${field}' is frozen once the payment is ` +\n `COMPLETED (was '${prior}', got '${next}'). A settled payment's ` +\n 'monetary fields cannot be mutated — issue a refund instead.',\n );\n }\n }\n }\n\n /**\n * USD drift between quote time and confirmation time — what the\n * operator gained (positive) or lost (negative) by accepting a\n * volatile-currency payment. Returns `0` when either side of the\n * comparison is missing or zero, so callers don't have to special-case\n * fiat-rail / stablecoin payments.\n */\n usdDrift(): number {\n if (!this.usdAtQuote || !this.usdAtConfirmation) return 0;\n return this.usdAtConfirmation - this.usdAtQuote;\n }\n\n /**\n * Check if payment is pending\n */\n isPending(): boolean {\n return this.status === PaymentStatus.PENDING;\n }\n\n /**\n * Check if payment is completed\n */\n isCompleted(): boolean {\n return this.status === PaymentStatus.COMPLETED;\n }\n\n /**\n * Check if payment is refunded\n */\n isRefunded(): boolean {\n return this.status === PaymentStatus.REFUNDED;\n }\n\n /**\n * Record the payment and create a balanced journal entry.\n *\n * Creates a journal entry in smrt-ledgers:\n * - Debit: Cash/Bank account (assets increase)\n * - Credit: Accounts Receivable (receivables decrease)\n *\n * Both sides carry `this.amount`, so the journal posted from commerce is\n * denominated in **integer minor units** like the rest of this package\n * (#2401). smrt-ledgers is unit-agnostic — its `BALANCE_EPSILON` only checks\n * that debits equal credits — and integer entries balance exactly, so this is\n * strictly tighter than the major-unit journals commerce used to post.\n *\n * @param options - Ledger and account configuration\n * @returns The created journal\n *\n * @example\n * ```typescript\n * const journal = await payment.recordPayment({\n * ledgerId: ledger.id,\n * receivablesAccountId: arAccount.id,\n * cashAccountId: bankAccount.id\n * });\n * console.log(`Created journal: ${journal.number}`);\n * ```\n */\n async recordPayment(options: RecordPaymentOptions): Promise<Journal> {\n if (recordingInProgress.has(this)) {\n throw new Error('Payment recording is already in progress');\n }\n if (this.status === PaymentStatus.COMPLETED) {\n throw new Error('Payment already recorded');\n }\n const previous = {\n status: this.status,\n journalId: this.journalId,\n paidAt: this.paidAt,\n loadedStatus: loadedPaymentStatus.get(this),\n };\n recordingInProgress.add(this);\n try {\n await this.withTransaction(async () => {\n if (!this.isPersisted) await this.save();\n // The revision claim is a write fence, including under PostgreSQL\n // repeatable-read snapshots. A stale contender must reload and retry\n // instead of posting from an earlier pending-payment snapshot.\n if (!this.updated_at) {\n throw new Error(\n 'Payment revision is missing; reload before recording',\n );\n }\n await this.claimRevision(this.updated_at);\n // In-memory status can be edited without saving (including markFailed).\n // Only the claimed persisted row decides whether settlement happened.\n const persisted = await this.getCanonicalPersistedRow({ id: this.id });\n if (!persisted) throw new Error('Claimed payment could not be loaded');\n if (persisted.status === PaymentStatus.COMPLETED) {\n throw new Error('Payment already recorded');\n }\n await this.postPaymentInTransaction(options);\n });\n } catch (error) {\n this.status = previous.status;\n this.journalId = previous.journalId;\n this.paidAt = previous.paidAt;\n if (previous.loadedStatus === undefined) loadedPaymentStatus.delete(this);\n else loadedPaymentStatus.set(this, previous.loadedStatus);\n throw error;\n } finally {\n recordingInProgress.delete(this);\n settlementInProgress.delete(this);\n }\n\n // Return a journal on the caller's executor, not the now-closed transaction.\n const { JournalCollection } = await import('@happyvertical/smrt-ledgers');\n const journals = await JournalCollection.create({\n ...this.options,\n db: this.db,\n });\n const journal = await journals.get({ id: this.journalId });\n if (!journal)\n throw new Error('Recorded payment journal could not be loaded');\n return journal;\n }\n\n /** Settle an outgoing refund through the same guarded journal transaction. */\n async recordRefund(options: RecordPaymentOptions): Promise<Journal> {\n if (this.kind !== 'refund')\n throw new Error('recordRefund requires Payment.kind=refund');\n return this.recordPayment(options);\n }\n\n /** Post and complete only inside recordPayment's claimed transaction. */\n private async postPaymentInTransaction(\n options: RecordPaymentOptions,\n ): Promise<Journal> {\n if (this.status === PaymentStatus.COMPLETED) {\n throw new Error('Payment already recorded');\n }\n\n if (this.amount <= 0) {\n throw new Error('Payment amount must be positive');\n }\n\n // A connected direct charge arrives net of the platform's application\n // fee (#3831): the provider withheld it at source, so it is an expense\n // already paid, never a payable to the platform.\n const applicationFee =\n this.kind === 'payment' ? this.applicationFeeCents : 0;\n if (applicationFee > 0 && !options.applicationFeeAccountId)\n throw new Error(\n 'Payment carries an application fee; recordPayment requires applicationFeeAccountId',\n );\n\n const funds = customerFundsState(this.customerFundsJson);\n const explicitDate = options.effectiveOn;\n if (\n explicitDate !== undefined &&\n (typeof explicitDate !== 'string' ||\n !/^\\d{4}-\\d{2}-\\d{2}$/.test(explicitDate) ||\n !Number.isFinite(Date.parse(explicitDate)) ||\n new Date(explicitDate).toISOString().slice(0, 10) !== explicitDate)\n )\n throw new Error('Settlement effectiveOn must be a calendar date');\n if (funds) {\n if (explicitDate !== undefined && explicitDate !== funds.effectiveOn)\n throw new Error('Settlement date differs from reviewed customer funds');\n assertCustomerFundsWrite(this);\n if (\n options.cashAccountId !== funds.policy.cashAccountId ||\n options.receivablesAccountId !== funds.policy.receivablesAccountId\n )\n throw new Error(\n 'Customer funds settlement accounts differ from reviewed policy',\n );\n }\n const creditAccountId =\n funds?.policy.mode === 'deposit-liability'\n ? funds.policy.liabilityAccountId!\n : options.receivablesAccountId;\n const effectiveOn = funds?.effectiveOn ?? explicitDate;\n const effectiveDate = effectiveOn\n ? new Date(`${effectiveOn}T12:00:00Z`)\n : new Date();\n // Dynamic import to avoid hard dependency on smrt-ledgers\n const { JournalCollection } = await import('@happyvertical/smrt-ledgers');\n\n // Create the journal collection\n const journalCollection = await JournalCollection.create(this.options);\n\n // Create a new journal for this payment\n const journal = await journalCollection.create({\n date: effectiveDate,\n description:\n this.kind === 'refund'\n ? `Customer refund for contract ${this.contractId}`\n : `Payment received for contract ${this.contractId}`,\n sourceModule: 'smrt-commerce',\n sourceRef: this.id,\n });\n await journal.save();\n\n // Incoming payments debit cash/credit AR; refunds reverse that movement.\n await journal.addEntry({\n accountId: options.cashAccountId,\n currency: this.currency,\n debit: this.kind === 'payment' ? this.amount - applicationFee : 0,\n credit: this.kind === 'refund' ? this.amount : 0,\n memo: `${this.kind === 'refund' ? 'Refund' : 'Payment'} ${this.reference || this.id}`,\n });\n if (applicationFee > 0)\n await journal.addEntry({\n accountId: options.applicationFeeAccountId!,\n currency: this.currency,\n debit: applicationFee,\n credit: 0,\n memo: `Application fee ${this.reference || this.id}`,\n });\n\n // Credit Accounts Receivable (receivables decrease)\n await journal.addEntry({\n accountId: creditAccountId,\n currency: this.currency,\n debit: this.kind === 'refund' ? this.amount : 0,\n credit: this.kind === 'payment' ? this.amount : 0,\n memo: `${this.kind === 'refund' ? 'Refund' : 'Payment'} ${this.reference || this.id}`,\n });\n\n // Post the journal (validates balance and finalizes)\n await journal.post();\n\n // Update payment record. Announce the verified settlement to the save-time\n // guard so the COMPLETED transition is accepted (the guard rejects any\n // other route into COMPLETED). A posted journal always carries an id;\n // fail fast rather than persist an empty-string FK if that invariant breaks.\n if (!journal.id) {\n throw new Error(\n 'Payment.recordPayment: journal was posted but has no id',\n );\n }\n this.journalId = journal.id;\n this.status = PaymentStatus.COMPLETED;\n this.paidAt = effectiveDate;\n settlementInProgress.add(this);\n await this.save();\n\n return journal;\n }\n\n /**\n * Get the linked journal entry (if recorded)\n */\n async getJournal(): Promise<Journal | null> {\n if (!this.journalId) return null;\n\n try {\n const { JournalCollection } = await import('@happyvertical/smrt-ledgers');\n const collection = await JournalCollection.create(this.options);\n return await collection.get({ id: this.journalId });\n } catch {\n // smrt-ledgers not available\n return null;\n }\n }\n\n /**\n * Mark payment as failed\n */\n markFailed(reason?: string): void {\n this.status = PaymentStatus.FAILED;\n if (reason) {\n this.notes = `${this.notes ? `${this.notes}\\n` : ''}Failed: ${reason}`;\n }\n }\n\n /**\n * Cancel the payment\n */\n cancel(): void {\n if (this.status === PaymentStatus.COMPLETED) {\n throw new Error('Cannot cancel a completed payment. Use refund instead.');\n }\n this.status = PaymentStatus.CANCELLED;\n }\n}\n\nexport default Payment;\n","/**\n * PaymentCollection - Collection manager for Payment objects\n * @packageDocumentation\n */\n\nimport { SmrtCollection } from '@happyvertical/smrt-core';\nimport { queryGlobal, queryWithGlobals } from '@happyvertical/smrt-tenancy';\nimport { Payment } from '../models/Payment.js';\nimport { type PaymentMethod, PaymentStatus } from '../types/index.js';\n\nexport class PaymentCollection extends SmrtCollection<Payment> {\n static readonly _itemClass = Payment;\n\n /**\n * Find payments by contract\n *\n * @param contractId - Contract ID\n * @returns Array of payments\n */\n async findByContract(contractId: string): Promise<Payment[]> {\n return await this.list({\n where: { contractId },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find payments by customer\n *\n * @param customerId - Customer ID\n * @returns Array of payments\n */\n async findByCustomer(customerId: string): Promise<Payment[]> {\n return await this.list({\n where: { customerId },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find payments by status\n *\n * @param status - Payment status\n * @returns Array of payments\n */\n async findByStatus(status: PaymentStatus): Promise<Payment[]> {\n return await this.list({\n where: { status },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find payments by method\n *\n * @param method - Payment method\n * @returns Array of payments\n */\n async findByMethod(method: PaymentMethod): Promise<Payment[]> {\n return await this.list({\n where: { method },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find all pending payments\n *\n * @returns Array of pending payments\n */\n async findPending(): Promise<Payment[]> {\n return await this.findByStatus(PaymentStatus.PENDING);\n }\n\n /**\n * Find all completed payments\n *\n * @returns Array of completed payments\n */\n async findCompleted(): Promise<Payment[]> {\n return await this.findByStatus(PaymentStatus.COMPLETED);\n }\n\n /**\n * Find payment by transaction ID\n *\n * @param transactionId - External transaction ID\n * @returns Payment or null\n */\n async findByTransactionId(transactionId: string): Promise<Payment | null> {\n const results = await this.list({\n where: { transactionId },\n limit: 1,\n });\n return results[0] || null;\n }\n\n /**\n * Find payments in date range\n *\n * @param startDate - Start date\n * @param endDate - End date\n * @returns Array of payments\n */\n async findByDateRange(startDate: Date, endDate: Date): Promise<Payment[]> {\n return await this.list({\n where: {\n 'paidAt >=': startDate.toISOString(),\n 'paidAt <=': endDate.toISOString(),\n },\n orderBy: 'paidAt DESC',\n });\n }\n\n /**\n * Calculate total payments for a contract\n *\n * @param contractId - Contract ID\n * @returns Total amount paid\n */\n async getTotalForContract(contractId: string): Promise<number> {\n const payments = await this.list({\n where: {\n contractId,\n status: PaymentStatus.COMPLETED,\n },\n });\n\n return payments.reduce((sum, payment) => sum + payment.amount, 0);\n }\n\n /**\n * Find payments linked to a ledger journal\n *\n * @param journalId - Journal ID from smrt-ledgers\n * @returns Payment or null\n */\n async findByJournal(journalId: string): Promise<Payment | null> {\n const results = await this.list({\n where: { journalId },\n limit: 1,\n });\n return results[0] || null;\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all payments belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of payments for the tenant\n */\n async findByTenant(tenantId: string): Promise<Payment[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global payments (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global payments\n */\n async findGlobal(): Promise<Payment[]> {\n return queryGlobal<Payment>(this);\n }\n\n /**\n * Find payments for a tenant including global payments.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global payments\n */\n async findWithGlobals(tenantId: string): Promise<Payment[]> {\n return queryWithGlobals<Payment>(this, tenantId, 'Payment.findWithGlobals');\n }\n}\n","/** Svelte-free currency precision shared by invoice previews and billing. */\n\nimport { ISO_4217_MINOR_UNITS } from '@happyvertical/smrt-ui/currency';\n\nconst CURRENCY_PATTERN = /^[A-Z]{3}$/;\n\n/** Upper-case, validate, and return an ISO 4217-shaped currency code. */\nexport function normalizeCurrency(currency: string): string {\n const code = String(currency ?? '')\n .trim()\n .toUpperCase();\n if (!CURRENCY_PATTERN.test(code)) {\n throw new Error(`Currency '${currency}' must be a three-letter code.`);\n }\n return code;\n}\n\n/**\n * The ISO 4217 minor-unit exponent of a currency (2 for USD and RSD, 0 for\n * JPY, 3 for IQD). Unknown codes and currencies without a minor unit fail\n * closed because assigning either an inferred or invented exponent changes\n * the represented amount.\n */\nexport function currencyMinorUnitExponent(currency: string): number {\n const code = normalizeCurrency(currency);\n const exponent = ISO_4217_MINOR_UNITS.get(code);\n if (exponent === undefined)\n throw new Error(`Unsupported currency '${code}'.`);\n if (exponent === null)\n throw new Error(`Currency '${code}' does not define a minor unit.`);\n return exponent;\n}\n","/**\n * Money-unit and identity helpers for billing-period close (#3060).\n *\n * Commerce stores money as integer minor units. `@happyvertical/accounting`\n * takes and returns currency *major* units and converts to the provider's own\n * representation itself, so the only conversion this package performs is the\n * ISO 4217 one: minor units ÷ 10^exponent.\n */\n\nimport {\n currencyMinorUnitExponent,\n normalizeCurrency,\n} from '../svelte/invoices/currency.js';\n\nexport {\n currencyMinorUnitExponent,\n normalizeCurrency,\n} from '../svelte/invoices/currency.js';\n\n/** Integer minor units → major units (`1999` USD → `19.99`). */\nexport function minorToMajorUnits(amount: number, currency: string): number {\n if (!Number.isSafeInteger(amount)) {\n throw new Error(\n `Amount ${amount} must be a safe integer number of minor units.`,\n );\n }\n return amount / 10 ** currencyMinorUnitExponent(currency);\n}\n\n/**\n * Major units → integer minor units. Rejects a value that is not a whole\n * number of minor units instead of rounding it: a provider amount that does\n * not round-trip exactly means the two sides disagree about the currency.\n */\nexport function majorToMinorUnits(amount: number, currency: string): number {\n if (!Number.isFinite(amount)) {\n throw new Error(`Provider amount ${amount} is not a finite number.`);\n }\n const scaled = amount * 10 ** currencyMinorUnitExponent(currency);\n const minor = Math.round(scaled);\n if (Math.abs(scaled - minor) > 1e-6 || !Number.isSafeInteger(minor)) {\n throw new Error(\n `Provider amount ${amount} ${normalizeCurrency(currency)} is not a whole number of minor units.`,\n );\n }\n return minor;\n}\n\n/**\n * A stable UUID derived from its parts (SHA-256, version/variant bits set).\n * Deterministic ids are what make every period-close write replay-safe: a\n * retry recomputes the same identity instead of creating a second row.\n */\nexport async function deterministicId(parts: string[]): Promise<string> {\n const bytes = new TextEncoder().encode(JSON.stringify(parts));\n const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));\n const uuid = digest.slice(0, 16);\n uuid[6] = (uuid[6] & 0x0f) | 0x50;\n uuid[8] = (uuid[8] & 0x3f) | 0x80;\n const hex = Array.from(uuid, (byte) => byte.toString(16).padStart(2, '0'));\n const joined = hex.join('');\n return [\n joined.slice(0, 8),\n joined.slice(8, 12),\n joined.slice(12, 16),\n joined.slice(16, 20),\n joined.slice(20),\n ].join('-');\n}\n\nconst UUID_PATTERN =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n\n/** Validate and lower-case a tenant UUID. */\nexport function canonicalTenantId(id: string, label = 'Tenant ID'): string {\n if (typeof id !== 'string' || !UUID_PATTERN.test(id)) {\n throw new Error(`${label} must be a UUID.`);\n }\n return id.toLowerCase();\n}\n\n/** Whether `id` is a UUID (any case), such as a billing record id. */\nexport function isUuid(id: string): boolean {\n return typeof id === 'string' && UUID_PATTERN.test(id);\n}\n\n/** Whether `id` is a tenant UUID (any case). */\nexport function isTenantId(id: string): boolean {\n return typeof id === 'string' && UUID_PATTERN.test(id);\n}\n\n/** Lower-case a possibly-empty tenant id for comparison. */\nexport function tenantKey(id: string | null | undefined): string {\n return id ? String(id).toLowerCase() : '';\n}\n","/** Pure projection of frozen commerce money snapshots into accounting SDK inputs. */\n\nimport {\n type AccountingProvider,\n type CreditMemoInput,\n type InvoiceInput,\n type InvoiceLineItemInput,\n requireCreditMemoOperations,\n type SyncResult,\n type TaxComponentSnapshot,\n} from '@happyvertical/accounting';\n\nimport { minorToMajorUnits } from './billing/units.js';\nimport {\n type LineAmountType,\n MoneyValidationError,\n type TaxRoundingScope,\n} from './money.js';\n\nexport type FrozenAccountingDocumentKind = 'invoice' | 'creditMemo';\n\n/** Realm-specific references are configuration, never inferred from a tax name or rate. */\nexport interface FrozenQuickBooksTaxMapping {\n readonly lines: Readonly<\n Record<\n string,\n {\n readonly itemRef: string;\n readonly taxCodeRef: string;\n /** Caller-verified configuration of the referenced realm TaxCode. */\n readonly taxComponents?: readonly {\n readonly taxRateRef: string;\n readonly compound: boolean;\n }[];\n }\n >\n >;\n readonly components: Readonly<\n Record<string, { readonly quickbooksTaxRateRef: string }>\n >;\n}\n\n/** Frozen component fact persisted with a line; all values are minor units except percent. */\nexport interface FrozenAccountingTaxComponent {\n readonly taxComponentId: string;\n readonly name: string;\n /** Exact percent text: `5` means 5 percent. */\n readonly percent: string;\n readonly taxMinor: number;\n readonly compound?: boolean;\n readonly taxableMinor?: number;\n}\n\n/** A frozen commerce line. `unitPriceMinor` and `discountMinor` are pre-discount document-mode values. */\nexport interface FrozenAccountingLineSnapshot {\n readonly id: string;\n readonly description: string;\n readonly sku?: string;\n readonly quantity: number;\n /** Pre-discount unit price: net for exclusive, gross for inclusive. */\n readonly unitPriceMinor: number;\n /** Pre-discount reduction: net for exclusive, gross for inclusive. */\n readonly discountMinor: number;\n /** Persisted post-discount net amount. */\n readonly amountMinor: number;\n readonly taxMinor: number;\n readonly taxComponents: readonly FrozenAccountingTaxComponent[];\n}\n\n/** Immutable facts required to project one approved document. */\nexport interface FrozenAccountingDocumentSnapshot {\n readonly kind: FrozenAccountingDocumentKind;\n readonly id: string;\n readonly number: string;\n readonly customerId: string;\n readonly customerExternalId?: string;\n readonly issueDate: Date;\n readonly dueDate?: Date;\n readonly currency: string;\n readonly lineAmountType: LineAmountType;\n readonly taxRoundingScope: TaxRoundingScope;\n readonly subtotalMinor: number;\n readonly taxAmountMinor: number;\n /** Header-only provider tax has no frozen component and cannot be sent to QBO. */\n readonly providerTaxMinor: number;\n readonly totalAmountMinor: number;\n readonly lineItems: readonly FrozenAccountingLineSnapshot[];\n /** Required only for a positive credit memo. */\n readonly creditedInvoiceExternalId?: string;\n readonly externalId?: string;\n readonly reference?: string;\n readonly memo?: string;\n}\n\n/** Public SDK types retained as named aliases for commerce callers. */\nexport type AccountingTaxComponentInput = TaxComponentSnapshot;\nexport type AccountingSnapshotLineInput = InvoiceLineItemInput;\nexport type AccountingSnapshotInvoiceInput = InvoiceInput;\nexport type AccountingSnapshotCreditMemoInput = CreditMemoInput;\nexport type AccountingSnapshotInput = InvoiceInput | CreditMemoInput;\n\n/**\n * Map and submit a positive credit memo only when the provider exposes that\n * capability. Mapping runs before the provider is touched, so a malformed\n * frozen snapshot cannot trigger a network write.\n */\nexport async function pushFrozenCreditMemo(\n provider: Pick<AccountingProvider, 'type' | 'creditMemos'>,\n snapshot: FrozenAccountingDocumentSnapshot,\n mapping: FrozenQuickBooksTaxMapping,\n): Promise<SyncResult> {\n if (snapshot.kind !== 'creditMemo') {\n throw new Error('Credit-memo submission requires a creditMemo snapshot');\n }\n const input = mapFrozenAccountingSnapshot(snapshot, mapping);\n if (!('creditMemoNumber' in input)) {\n throw new Error('Credit-memo submission did not produce a credit memo');\n }\n return requireCreditMemoOperations(provider).push(input);\n}\n\nfunction requireText(value: string | undefined, label: string): string {\n if (!value || value.trim() !== value) {\n throw new Error(`${label} must be a nonempty trimmed string`);\n }\n return value;\n}\n\nfunction requireSafeMinor(value: number, label: string): number {\n if (!Number.isSafeInteger(value) || value < 0) {\n throw new Error(`${label} must be a nonnegative safe integer minor amount`);\n }\n return value;\n}\n\nfunction requirePositiveQuantity(quantity: number, lineId: string): number {\n if (!Number.isFinite(quantity) || quantity <= 0) {\n throw new Error(\n `Line '${lineId}' quantity must be a positive finite number`,\n );\n }\n return quantity;\n}\n\n/** Convert exact percent text to the SDK's fraction text without float arithmetic. */\nfunction percentToFraction(percent: string, componentId: string): string {\n if (!/^(?:0|[1-9]\\d{0,2})(?:\\.\\d{1,6})?$/.test(percent)) {\n throw new Error(\n `Tax component '${componentId}' percent must be exact decimal text from 0 through 100`,\n );\n }\n const [whole, fraction = ''] = percent.split('.');\n const scale = 10n ** BigInt(fraction.length);\n const numerator = BigInt(whole) * scale + BigInt(fraction || '0');\n const denominator = 100n * scale;\n if (numerator > denominator) {\n throw new Error(`Tax component '${componentId}' percent exceeds 100`);\n }\n if (numerator === 0n) return '0';\n if (numerator === denominator) return '1';\n const padded = numerator\n .toString()\n .padStart(denominator.toString().length - 1, '0');\n return `0.${padded}`.replace(/0+$/, '');\n}\n\nfunction globalTaxCalculation(\n lineAmountType: LineAmountType,\n): NonNullable<InvoiceInput['quickbooksMapping']>['globalTaxCalculation'] {\n switch (lineAmountType) {\n case 'exclusive':\n return 'TaxExcluded';\n case 'inclusive':\n return 'TaxInclusive';\n case 'none':\n return 'NotApplicable';\n }\n}\n\nfunction mapLine(\n line: FrozenAccountingLineSnapshot,\n currency: string,\n lineAmountType: LineAmountType,\n mapping: FrozenQuickBooksTaxMapping,\n): AccountingSnapshotLineInput {\n requireText(line.id, 'Line ID');\n const lineMapping = mapping.lines[line.id];\n if (!lineMapping) {\n throw new Error(\n `Missing explicit QuickBooks mapping for line '${line.id}'`,\n );\n }\n requireText(\n lineMapping.itemRef,\n `QuickBooks item reference for line '${line.id}'`,\n );\n requireText(\n lineMapping.taxCodeRef,\n `QuickBooks tax-code reference for line '${line.id}'`,\n );\n\n const amountMinor = requireSafeMinor(\n line.amountMinor,\n `Line '${line.id}' amount`,\n );\n const taxMinor = requireSafeMinor(line.taxMinor, `Line '${line.id}' tax`);\n const componentIds = new Set<string>();\n let componentTotalMinor = 0;\n const taxComponents = line.taxComponents.map((component) => {\n if (\n component.compound !== undefined &&\n typeof component.compound !== 'boolean'\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen component compound flag must be boolean',\n );\n if (component.compound && !lineMapping.taxComponents)\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_UNSUPPORTED',\n 'Compound accounting sync requires caller-verified ordered realm tax-code mapping',\n );\n if (component.compound && component.taxableMinor === undefined)\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_UNSUPPORTED',\n 'Compound accounting sync requires the frozen component taxable basis',\n );\n const basisMinor =\n component.taxableMinor === undefined\n ? undefined\n : requireSafeMinor(\n component.taxableMinor,\n `Line '${line.id}' component taxable basis`,\n );\n if (\n basisMinor !== undefined &&\n basisMinor !==\n amountMinor + (component.compound ? componentTotalMinor : 0)\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_UNSUPPORTED',\n 'Frozen component taxable basis does not match ordered source facts',\n );\n requireText(component.taxComponentId, `Line '${line.id}' tax component ID`);\n if (componentIds.has(component.taxComponentId)) {\n throw new Error(\n `Line '${line.id}' repeats tax component '${component.taxComponentId}'`,\n );\n }\n componentIds.add(component.taxComponentId);\n const taxAmountMinor = requireSafeMinor(\n component.taxMinor,\n `Line '${line.id}' component '${component.taxComponentId}' tax`,\n );\n componentTotalMinor += taxAmountMinor;\n const componentMapping = mapping.components[component.taxComponentId];\n if (!componentMapping) {\n throw new Error(\n `Missing explicit QuickBooks tax-rate reference for component '${component.taxComponentId}'`,\n );\n }\n requireText(\n componentMapping.quickbooksTaxRateRef,\n `QuickBooks tax-rate reference for component '${component.taxComponentId}'`,\n );\n return {\n taxCode: component.taxComponentId,\n rate: percentToFraction(component.percent, component.taxComponentId),\n quickbooksTaxRateRef: componentMapping.quickbooksTaxRateRef,\n taxAmount: minorToMajorUnits(taxAmountMinor, currency),\n ...(component.compound ? { compound: true } : {}),\n ...(basisMinor !== undefined\n ? { taxableAmount: minorToMajorUnits(basisMinor, currency) }\n : {}),\n };\n });\n if (\n lineMapping.taxComponents &&\n (lineMapping.taxComponents.length !== taxComponents.length ||\n lineMapping.taxComponents.some(\n (part, index) =>\n part.taxRateRef !== taxComponents[index].quickbooksTaxRateRef ||\n typeof part.compound !== 'boolean' ||\n part.compound !== (taxComponents[index].compound ?? false),\n ))\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_UNSUPPORTED',\n 'Ordered realm tax-code mapping does not match frozen components',\n );\n if (componentTotalMinor !== taxMinor) {\n throw new Error(\n `Line '${line.id}' frozen component tax ${componentTotalMinor} does not equal line tax ${taxMinor}`,\n );\n }\n if (taxMinor > 0 && taxComponents.length === 0) {\n throw new Error(`Line '${line.id}' tax requires frozen tax components`);\n }\n\n const discountMinor = requireSafeMinor(\n line.discountMinor,\n `Line '${line.id}' discount`,\n );\n const unitPriceMinor = requireSafeMinor(\n line.unitPriceMinor,\n `Line '${line.id}' unit price`,\n );\n const result = {\n description: requireText(line.description, `Line '${line.id}' description`),\n sku: line.sku || undefined,\n quantity: requirePositiveQuantity(line.quantity, line.id),\n unitPrice: minorToMajorUnits(unitPriceMinor, currency),\n ...(discountMinor === 0\n ? {}\n : { discount: minorToMajorUnits(discountMinor, currency) }),\n amount: minorToMajorUnits(amountMinor, currency),\n taxAmount: minorToMajorUnits(taxMinor, currency),\n taxComponents,\n quickbooksMapping: {\n itemRef: lineMapping.itemRef,\n taxCodeRef: lineMapping.taxCodeRef,\n ...(lineMapping.taxComponents\n ? {\n taxComponents: lineMapping.taxComponents.map((part) => ({\n ...part,\n })),\n }\n : {}),\n },\n };\n if (lineAmountType === 'inclusive') {\n // This is an exact representation of two stored facts, never a tax calculation.\n return {\n ...result,\n taxInclusiveAmount: minorToMajorUnits(amountMinor + taxMinor, currency),\n };\n }\n return result;\n}\n\n/**\n * Project an approved frozen snapshot into the companion SDK's request shape.\n * It does not calculate discounts, taxes, rates, or document allocation.\n */\nexport function mapFrozenAccountingSnapshot(\n snapshot: FrozenAccountingDocumentSnapshot,\n mapping: FrozenQuickBooksTaxMapping,\n): AccountingSnapshotInput {\n const currency = requireText(snapshot.currency, 'Currency');\n requireText(snapshot.id, 'Document ID');\n requireText(snapshot.number, 'Document number');\n requireText(snapshot.customerId, 'Customer ID');\n if (\n !(snapshot.issueDate instanceof Date) ||\n Number.isNaN(snapshot.issueDate.valueOf())\n ) {\n throw new Error('Issue date must be a valid Date');\n }\n if (!Array.isArray(snapshot.lineItems) || snapshot.lineItems.length === 0) {\n throw new Error('Frozen document requires at least one line item');\n }\n const subtotalMinor = requireSafeMinor(snapshot.subtotalMinor, 'Subtotal');\n const taxAmountMinor = requireSafeMinor(\n snapshot.taxAmountMinor,\n 'Tax amount',\n );\n const providerTaxMinor = requireSafeMinor(\n snapshot.providerTaxMinor,\n 'Provider tax',\n );\n const totalAmountMinor = requireSafeMinor(snapshot.totalAmountMinor, 'Total');\n if (providerTaxMinor !== 0) {\n throw new Error(\n 'Frozen provider tax cannot be mapped to QuickBooks without a stored component snapshot and explicit tax-rate reference',\n );\n }\n if (subtotalMinor + taxAmountMinor !== totalAmountMinor) {\n throw new Error('Frozen subtotal plus tax must equal frozen total');\n }\n\n const lineItems = snapshot.lineItems.map((line) =>\n mapLine(line, currency, snapshot.lineAmountType, mapping),\n );\n const linesSubtotalMinor = snapshot.lineItems.reduce(\n (sum, line) => sum + line.amountMinor,\n 0,\n );\n const linesTaxMinor = snapshot.lineItems.reduce(\n (sum, line) => sum + line.taxMinor,\n 0,\n );\n if (linesSubtotalMinor !== subtotalMinor) {\n throw new Error('Frozen line net amounts do not equal frozen subtotal');\n }\n if (linesTaxMinor !== taxAmountMinor) {\n throw new Error('Frozen line tax amounts do not equal frozen tax total');\n }\n\n const base = {\n id: snapshot.id,\n externalId: snapshot.externalId,\n customerId: snapshot.customerId,\n customerExternalId: snapshot.customerExternalId,\n issueDate: snapshot.issueDate,\n currency,\n subtotal: minorToMajorUnits(subtotalMinor, currency),\n taxAmount: minorToMajorUnits(taxAmountMinor, currency),\n totalAmount: minorToMajorUnits(totalAmountMinor, currency),\n lineItems,\n quickbooksMapping: {\n globalTaxCalculation: globalTaxCalculation(snapshot.lineAmountType),\n },\n reference: snapshot.reference,\n memo: snapshot.memo,\n } as const;\n\n if (snapshot.kind === 'invoice') {\n if (\n !(snapshot.dueDate instanceof Date) ||\n Number.isNaN(snapshot.dueDate.valueOf())\n ) {\n throw new Error('Invoice due date must be a valid Date');\n }\n return {\n ...base,\n invoiceNumber: snapshot.number,\n dueDate: snapshot.dueDate,\n };\n }\n const creditedInvoiceExternalId = requireText(\n snapshot.creditedInvoiceExternalId,\n 'Credited invoice external ID',\n );\n if (subtotalMinor === 0 || totalAmountMinor === 0) {\n throw new Error('Credit memo amounts must be positive');\n }\n return {\n ...base,\n creditMemoNumber: snapshot.number,\n creditedInvoiceExternalId,\n };\n}\n","/**\n * Contact rows for customers and vendors (#3501).\n *\n * `CustomerForm` and `VendorForm` (`./svelte`) post contacts as repeated\n * field names in DOM order, one value per row for each of `contactId`,\n * `contactName`, `contactLabel`, `contactEmail`, `contactPhone`, and\n * `contactAddress` (all overridable through the forms' `fieldNames`). The\n * helpers here read that payload, apply the native add/remove intents, and\n * write the rows to `Customer.contacts` / `Vendor.contacts`.\n *\n * @packageDocumentation\n */\n\nimport type { CustomerCollection } from './collections/CustomerCollection.js';\nimport type { VendorCollection } from './collections/VendorCollection.js';\nimport type { Customer } from './models/Customer.js';\nimport type { Vendor } from './models/Vendor.js';\nimport type { PartyContact } from './types/index.js';\n\n/** One submitted contact row, exactly as posted (untrimmed strings). */\nexport type PartyContactInput = Partial<PartyContact>;\n\n/** The text fields of a contact, which are also its error keys. */\nexport type PartyContactField = Exclude<keyof PartyContact, 'id'>;\n\n/** HTML names of the repeated contact fields; defaults match the forms. */\nexport interface PartyContactFieldNames {\n contactId: string;\n contactName: string;\n contactLabel: string;\n contactEmail: string;\n contactPhone: string;\n contactAddress: string;\n /** Hidden presence marker: the number of contact rows the form rendered. */\n contactCount: string;\n}\n\n/** Default contact field names, matching `DEFAULT_PARTY_FIELD_NAMES` in `./svelte`. */\nexport const DEFAULT_PARTY_CONTACT_FIELD_NAMES: PartyContactFieldNames = {\n contactId: 'contactId',\n contactName: 'contactName',\n contactLabel: 'contactLabel',\n contactEmail: 'contactEmail',\n contactPhone: 'contactPhone',\n contactAddress: 'contactAddress',\n contactCount: 'contactCount',\n};\n\n/** Maximum lengths, matching the form controls' `maxlength`. */\nexport const PARTY_CONTACT_MAX_LENGTHS: Readonly<\n Record<PartyContactField, number>\n> = {\n name: 160,\n label: 80,\n email: 320,\n phone: 80,\n address: 500,\n};\n\n/**\n * Per-row validation error codes, keyed by field. `unknown_contact` (on\n * `id`) means the posted id is not one of the party's saved contacts — a\n * stale or altered form; reload the party and show its current rows.\n */\nexport type PartyContactErrorCode =\n | 'too_long'\n | 'invalid_email'\n | 'unknown_contact';\n\n/** Errors for one submitted row; indexes match the submitted rows. */\nexport type PartyContactRowErrors = Partial<\n Record<PartyContactField | 'id', PartyContactErrorCode>\n>;\n\nconst FIELDS: readonly PartyContactField[] = [\n 'name',\n 'label',\n 'email',\n 'phone',\n 'address',\n];\n\n/** Thrown by {@link savePartyContacts} when any row is invalid; nothing is written. */\nexport class PartyContactValidationError extends Error {\n /** Errors aligned with the submitted rows; `undefined` for a valid row. */\n readonly errors: Array<PartyContactRowErrors | undefined>;\n\n constructor(errors: Array<PartyContactRowErrors | undefined>) {\n super('One or more contact rows are invalid');\n this.name = 'PartyContactValidationError';\n this.errors = errors;\n }\n}\n\n/**\n * Thrown by {@link readPartyContactRows} (and so {@link savePartyContacts}\n * given a form) when the posted contact payload cannot be trusted:\n * `missing` — no `contactCount` marker, so the editable contact block did\n * not render or was posted under other field names (treating that as \"no\n * contacts\" would delete them all); `misaligned` — the repeated fields or\n * the marker disagree on the row count. Nothing is written.\n */\nexport class PartyContactPayloadError extends Error {\n readonly reason: 'missing' | 'misaligned';\n\n constructor(reason: 'missing' | 'misaligned', message: string) {\n super(message);\n this.name = 'PartyContactPayloadError';\n this.reason = reason;\n }\n}\n\n/** Thrown by {@link savePartyContacts} when the party is not visible in the current tenant. */\nexport class PartyContactPartyNotFoundError extends Error {\n constructor(partyId: string) {\n super(`Customer or vendor ${partyId} was not found`);\n this.name = 'PartyContactPartyNotFoundError';\n }\n}\n\nfunction text(value: unknown): string {\n return typeof value === 'string' ? value : '';\n}\n\n/**\n * Normalize stored contacts (an array, a JSON string, or nothing) into\n * well-formed {@link PartyContact} rows. Non-object entries and non-string\n * values are dropped; a row without an id is given one.\n */\nexport function normalizePartyContacts(value: unknown): PartyContact[] {\n let input = value;\n if (typeof input === 'string') {\n try {\n input = JSON.parse(input);\n } catch {\n return [];\n }\n }\n if (!Array.isArray(input)) return [];\n const contacts: PartyContact[] = [];\n for (const entry of input) {\n if (!entry || typeof entry !== 'object') continue;\n const row = entry as Record<string, unknown>;\n contacts.push({\n id: text(row.id) || globalThis.crypto.randomUUID(),\n name: text(row.name),\n label: text(row.label),\n email: text(row.email),\n phone: text(row.phone),\n address: text(row.address),\n });\n }\n return contacts;\n}\n\n/** Minimal form-data reader; `FormData` and `URLSearchParams` both satisfy it. */\nexport interface PartyContactFormSource {\n getAll(name: string): unknown[];\n}\n\n/**\n * Read the contact rows a party form posted, in DOM order. The editable\n * contact block always posts a `contactCount` marker with its row count, and\n * every row renders all six fields. A missing marker (the block did not\n * render, or `fieldNames` differ from the form's) or any disagreeing count\n * throws {@link PartyContactPayloadError}, so zero rows is only ever read\n * when the form really posted an empty contact list.\n */\nexport function readPartyContactRows(\n form: PartyContactFormSource,\n fieldNames: Partial<PartyContactFieldNames> = {},\n): PartyContactInput[] {\n const names = { ...DEFAULT_PARTY_CONTACT_FIELD_NAMES, ...fieldNames };\n const column = (name: string) => form.getAll(name).map(text);\n const columns = {\n id: column(names.contactId),\n name: column(names.contactName),\n label: column(names.contactLabel),\n email: column(names.contactEmail),\n phone: column(names.contactPhone),\n address: column(names.contactAddress),\n };\n const markers = column(names.contactCount);\n if (markers.length !== 1)\n throw new PartyContactPayloadError(\n markers.length === 0 ? 'missing' : 'misaligned',\n markers.length === 0\n ? `Contact payload has no ${names.contactCount} marker; the contact fields were not posted under these names`\n : `Contact payload has ${markers.length} ${names.contactCount} markers`,\n );\n const count = columns.id.length;\n if (markers[0] !== String(count))\n throw new PartyContactPayloadError(\n 'misaligned',\n `Contact payload is misaligned: ${names.contactCount} is ${markers[0]} but ${count} contact ids were posted`,\n );\n for (const [key, values] of Object.entries(columns)) {\n if (values.length !== count)\n throw new PartyContactPayloadError(\n 'misaligned',\n `Contact payload is misaligned: ${count} contact ids but ${values.length} ${key} values`,\n );\n }\n return columns.id.map((id, index) => ({\n id,\n name: columns.name[index],\n label: columns.label[index],\n email: columns.email[index],\n phone: columns.phone[index],\n address: columns.address[index],\n }));\n}\n\n/** Submitter values for the forms' native contact row actions. */\nexport interface PartyContactIntents {\n /** Defaults to the forms' `addContact`. */\n addIntent?: string;\n /** Parses a remove submitter into a row index; defaults to `removeContact:<index>`. */\n parseRemoveIntent?: (intent: string) => number | undefined;\n}\n\nfunction defaultParseRemoveIntent(intent: string): number | undefined {\n const match = /^removeContact:(\\d+)$/.exec(intent);\n return match ? Number(match[1]) : undefined;\n}\n\n/**\n * Apply an add or remove submitter to the posted rows and return the rows\n * to render back into the form. Nothing is persisted: a row action is a\n * round trip, and only the save intent writes. `handled` is `false` for any\n * other intent (including save), with the rows unchanged; when it is `true`,\n * render the returned rows and do not save.\n */\nexport function applyPartyContactIntent(\n rows: readonly PartyContactInput[],\n intent: string,\n intents: PartyContactIntents = {},\n): { contacts: PartyContactInput[]; handled: boolean } {\n if (intent === (intents.addIntent ?? 'addContact'))\n return { contacts: [...rows, {}], handled: true };\n const index = (intents.parseRemoveIntent ?? defaultParseRemoveIntent)(intent);\n // A stale index (the row is already gone) is still a remove: it must not\n // fall through to a save.\n if (index !== undefined)\n return {\n contacts: rows.filter((_, position) => position !== index),\n handled: true,\n };\n return { contacts: [...rows], handled: false };\n}\n\n// Deliberately permissive: one \"@\" with text on both sides and no spaces.\n// Deliverability is the caller's concern.\nconst EMAIL = /^[^\\s@]+@[^\\s@]+$/;\n\n/**\n * Validate posted rows without persisting them. Returns the trimmed rows\n * that would be written (blank rows dropped) and, when any row is invalid,\n * errors aligned with the submitted rows. Ids are checked against the\n * party's saved contacts only by {@link savePartyContacts}.\n */\nexport function validatePartyContactRows(rows: readonly PartyContactInput[]): {\n contacts: PartyContactInput[];\n errors?: Array<PartyContactRowErrors | undefined>;\n} {\n const contacts: PartyContactInput[] = [];\n const errors: Array<PartyContactRowErrors | undefined> = [];\n let invalid = false;\n rows.forEach((row, index) => {\n const trimmed: PartyContactInput = { id: text(row.id).trim() };\n let rowErrors: PartyContactRowErrors | undefined;\n for (const field of FIELDS) {\n const value = text(row[field]).trim();\n trimmed[field] = value;\n if (value.length > PARTY_CONTACT_MAX_LENGTHS[field])\n rowErrors = { ...rowErrors, [field]: 'too_long' };\n }\n if (trimmed.email && !rowErrors?.email && !EMAIL.test(trimmed.email))\n rowErrors = { ...rowErrors, email: 'invalid_email' };\n errors[index] = rowErrors;\n if (rowErrors) invalid = true;\n if (FIELDS.some((field) => trimmed[field])) contacts.push(trimmed);\n });\n return invalid ? { contacts, errors } : { contacts };\n}\n\n/** Options for {@link savePartyContacts}. */\nexport interface SavePartyContactsOptions {\n /** Field names used to read a posted form; must match the form's `fieldNames`. */\n fieldNames?: Partial<PartyContactFieldNames>;\n /**\n * When set, the party must belong to this tenant. Use it to bind a form's\n * posted tenant to the loaded row in addition to the active tenant\n * context, which already scopes the lookup.\n */\n expectedTenantId?: string | null;\n}\n\n/**\n * Replace a customer's or vendor's contacts with the rows its form posted.\n *\n * Pass the posted `FormData` (with `options.fieldNames` when the form\n * overrides them): it is read with {@link readPartyContactRows}, so a payload\n * without the contact block's presence marker throws\n * {@link PartyContactPayloadError} instead of removing every contact. An\n * array is taken as rows the caller already read through that function.\n *\n * The party is loaded through `collection.get()`, so the active tenant\n * context scopes it: a party in another tenant is not found. Rows are\n * validated ({@link validatePartyContactRows}), blank rows dropped, kept\n * rows matched to saved contacts by id (new rows get a fresh id), and rows\n * that were not posted are removed. A posted id the party does not have is\n * an `unknown_contact` error. On any error nothing is written and a\n * {@link PartyContactValidationError} is thrown.\n *\n * This is a read-modify-write of one row: concurrent saves of the same party\n * are last-writer-wins, like any other field edit.\n *\n * @returns The saved contacts, in submitted order.\n */\nexport async function savePartyContacts(\n collection: CustomerCollection | VendorCollection,\n partyId: string,\n posted: PartyContactFormSource | readonly PartyContactInput[],\n options: SavePartyContactsOptions = {},\n): Promise<PartyContact[]> {\n // Reading the form here (rather than trusting a caller's array) keeps the\n // presence-marker check on the write path.\n const rows = Array.isArray(posted)\n ? (posted as readonly PartyContactInput[])\n : readPartyContactRows(\n posted as PartyContactFormSource,\n options.fieldNames,\n );\n const party: Customer | Vendor | null = await collection.get({\n id: partyId,\n });\n if (\n !party ||\n (options.expectedTenantId !== undefined &&\n party.tenantId !== options.expectedTenantId)\n )\n throw new PartyContactPartyNotFoundError(partyId);\n\n const validation = validatePartyContactRows(rows);\n const { contacts } = validation;\n const errors = validation.errors ?? rows.map(() => undefined);\n const saved = new Set(\n normalizePartyContacts(party.contacts).map((c) => c.id),\n );\n let invalid = validation.errors !== undefined;\n rows.forEach((row, index) => {\n const id = text(row.id).trim();\n if (id && !saved.has(id)) {\n errors[index] = { ...errors[index], id: 'unknown_contact' };\n invalid = true;\n }\n });\n if (invalid) throw new PartyContactValidationError(errors);\n\n const seen = new Set<string>();\n party.contacts = contacts.map((row) => {\n // A duplicated posted id (two rows claiming one contact) keeps the id\n // on the first row only, so stored ids stay unique.\n const id =\n row.id && !seen.has(row.id) ? row.id : globalThis.crypto.randomUUID();\n seen.add(id);\n return {\n id,\n name: row.name ?? '',\n label: row.label ?? '',\n email: row.email ?? '',\n phone: row.phone ?? '',\n address: row.address ?? '',\n };\n });\n await party.save();\n return party.contacts;\n}\n","/**\n * Customer model - links to Profile with customer-specific data\n * @packageDocumentation\n */\n\nimport {\n crossPackageRef,\n DatabaseError,\n field,\n foreignKey,\n method,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport { normalizePartyContacts } from '../party-contacts.js';\nimport {\n type Address,\n type CustomerOptions,\n CustomerStatus,\n CustomerType,\n type PartyContact,\n} from '../types/index.js';\n\n/**\n * Customer represents the customer role for a Profile.\n *\n * One Profile can have multiple Customer records (e.g., different business contexts).\n * Customer stores customer-specific data like credit limits and payment terms.\n *\n * @example\n * ```typescript\n * const customer = await customers.create({\n * profileId: 'profile-uuid',\n * creditLimit: 10000,\n * paymentTerms: 'Net 30'\n * });\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n writeThrough: {\n profileId: {\n fields: ['name', 'email'],\n virtualFields: {\n phone: {\n type: 'text',\n description: 'A phone number for reaching this person.',\n ui: { widget: 'phone' },\n },\n },\n },\n },\n display: {\n list: ['status', 'email', 'phone'],\n label: { via: 'profileId', field: 'name', fallback: 'Unnamed customer' },\n },\n api: { include: ['list', 'get', 'create', 'update'] },\n mcp: { include: ['list', 'get', 'create', 'update'] },\n cli: { skipApiCheck: true },\n})\nexport class Customer extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /**\n * Tenant ID for multi-tenant isolation\n * Nullable to support both tenant-scoped and global customers\n */\n @tenantId({ nullable: true })\n tenantId: string | null = null;\n\n /**\n * Reference to smrt-profiles Profile\n * Plain string for cross-package reference\n */\n @crossPackageRef('@happyvertical/smrt-profiles:Profile', {\n description: 'Choose an existing contact, or enter a new name above.',\n })\n profileId: string = '';\n\n /**\n * Maximum credit extended to this customer, in **integer minor units**\n * (cents, satoshis).\n *\n * Converts with the rest of commerce because\n * {@link Customer.hasAvailableCredit} compares it directly against a money\n * amount; a dollars-vs-cents split there would approve every request off by\n * a factor of 100 (#2401).\n */\n @field({\n description:\n 'The most this customer may owe you at one time. Leave it at zero if you do not set limits.',\n ui: { format: 'money' },\n })\n creditLimit: number = 0;\n\n /**\n * Payment terms (e.g., \"Net 30\", \"Due on receipt\", \"2/10 Net 30\")\n */\n @field({\n description:\n 'How long the customer has to pay, written the way you would say it, for example \"Net 30\".',\n })\n paymentTerms: string = '';\n\n /**\n * Whether customer is exempt from tax\n */\n @field({\n description: 'Turn this on if you do not charge this customer sales tax.',\n })\n taxExempt: boolean = false;\n\n /**\n * Tax identification number\n *\n * Sensitive (#1540): PII excluded from generated API/MCP responses and\n * rejected as a `where` filter key.\n */\n @field({\n sensitive: true,\n description:\n 'The tax or VAT number of the customer, kept on file for tax-exempt sales.',\n })\n taxId: string = '';\n\n /**\n * Tax code normally used for this customer's sales.\n *\n * An exempt customer always resolves to the tenant's exempt code before\n * this preference is considered.\n */\n @foreignKey('TaxCode', {\n description: 'The tax code normally used when selling to this customer.',\n })\n defaultTaxCodeId: string = '';\n\n /**\n * Default shipping address\n */\n @field({\n description:\n 'Where you usually send goods to this customer, so you do not retype it on every order.',\n })\n defaultShippingAddress: Address = {};\n\n /**\n * Default billing address\n */\n @field({ description: 'Where you send invoices to this customer.' })\n defaultBillingAddress: Address = {};\n\n /**\n * Customer status\n */\n @field({\n default: 'active',\n description:\n 'Whether you are still dealing with this customer: active, inactive or suspended.',\n })\n status: CustomerStatus = CustomerStatus.ACTIVE;\n\n /**\n * Customer classification — DTC, wholesale, or retail.\n *\n * Drives channel access (wholesale portal vs. storefront vs. POS), default\n * price tier, and credit terms. Defaults to DTC for backwards compatibility.\n */\n @field({\n description:\n 'The kind of customer: a direct shopper, a wholesale buyer or a retail customer.',\n })\n customerType: CustomerType = CustomerType.DTC;\n\n /**\n * Internal notes about this customer\n */\n @field({\n description: 'Anything worth remembering about this customer.',\n ui: { widget: 'textarea' },\n })\n notes: string = '';\n\n /**\n * People to reach at this customer, stored as a JSON column (#3501). Write\n * form rows through `savePartyContacts()`; every save normalizes the shape.\n */\n @field({\n description:\n 'The people you deal with at this customer, with their role and how to reach them.',\n })\n contacts: PartyContact[] = [];\n\n constructor(options: CustomerOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.profileId !== undefined) this.profileId = options.profileId;\n if (options.creditLimit !== undefined)\n this.creditLimit = options.creditLimit;\n if (options.paymentTerms !== undefined)\n this.paymentTerms = options.paymentTerms;\n if (options.taxExempt !== undefined) this.taxExempt = options.taxExempt;\n if (options.taxId !== undefined) this.taxId = options.taxId;\n if (options.defaultTaxCodeId !== undefined)\n this.defaultTaxCodeId = options.defaultTaxCodeId;\n if (options.defaultShippingAddress !== undefined)\n this.defaultShippingAddress = options.defaultShippingAddress;\n if (options.defaultBillingAddress !== undefined)\n this.defaultBillingAddress = options.defaultBillingAddress;\n if (options.status !== undefined) this.status = options.status;\n // Treat `null` the same as `undefined` here. Customers loaded from\n // rows written before the `customer_type` column existed will\n // hydrate with the field as SQL `NULL`; without this guard the\n // SmrtObject loader would overwrite the `CustomerType.DTC`\n // initializer with `null`, breaking the documented\n // backwards-compatible default. Explicit string values (including\n // any future custom flavour) still take precedence.\n if (options.customerType !== undefined && options.customerType !== null) {\n this.customerType = options.customerType;\n }\n if (options.notes !== undefined) this.notes = options.notes;\n if (options.contacts !== undefined)\n this.contacts = normalizePartyContacts(options.contacts);\n }\n\n /**\n * Re-normalize contacts after the framework's option-override pass, which\n * re-applies raw option values (including a stored JSON string).\n */\n /** Virtual fields owned by the linked Profile, never Customer columns. */\n\n protected override async writeThroughDefaults(\n via: string,\n ): Promise<Record<string, unknown>> {\n if (via !== 'profileId') return super.writeThroughDefaults(via);\n const { ProfileTypeCollection } = await import(\n '@happyvertical/smrt-profiles'\n );\n const types = await ProfileTypeCollection.create({ db: this.db });\n const person = await types.getOrCreateBySlug('person', {\n name: 'Person',\n description: 'Individual person profile',\n });\n return { typeId: person.id };\n }\n\n @method({ expose: false })\n static override async resolveWriteThroughVirtualProjection(\n via: string,\n options: import('@happyvertical/smrt-core').SmrtObjectOptions,\n ): Promise<Record<string, { sql: string; values: unknown[] }>> {\n if (via !== 'profileId')\n return super.resolveWriteThroughVirtualProjection(via, options);\n const {\n ProfileCollection,\n ProfileMetadataCollection,\n ProfileMetafieldCollection,\n } = await import('@happyvertical/smrt-profiles');\n const metadata = await ProfileMetadataCollection.create(options);\n const metafields = await ProfileMetafieldCollection.create(options);\n // The public column projections validate field policy and run each owning\n // collection's authorization and tenant interceptors, without fetching rows.\n try {\n const fields = await metafields.buildProjectionQuery({\n where: { slug: 'phone' },\n select: ['id', 'tenantId'],\n });\n const rows = await metadata.buildProjectionQuery({\n select: ['profileId', 'metafieldId', 'tenantId', 'value'],\n });\n const fieldSql = fields.sql.replace(\n /\\$(\\d+)/g,\n (_, index: string) => `$${Number(index) + rows.values.length}`,\n );\n return {\n phone: {\n sql: `SELECT m.\"profileId\" AS \"id\", m.\"tenantId\", m.\"value\" FROM (${rows.sql}) m JOIN (${fieldSql}) f ON f.\"id\" = m.\"metafieldId\" AND (f.\"tenantId\" = m.\"tenantId\" OR f.\"tenantId\" IS NULL)`,\n values: [...rows.values, ...fields.values],\n },\n };\n } catch (error) {\n if (\n !(error instanceof DatabaseError) ||\n error.code !== 'DB_SCHEMA_MISSING' ||\n ![metadata.tableName, metafields.tableName].includes(\n String(error.details?.tableName),\n )\n )\n throw error;\n // Phone storage is optional for an existing contact reader. Compose an\n // empty public Profile projection so native UUID types stay intact;\n // core's existing scalar COALESCE supplies the empty phone value.\n const profiles = await ProfileCollection.create(options);\n const empty = await profiles.buildProjectionQuery({\n select: ['id', 'tenantId'],\n });\n return {\n phone: {\n sql: `SELECT p.\"id\", p.\"tenantId\", '' AS \"value\" FROM (${empty.sql}) p WHERE 1 = 0`,\n values: empty.values,\n },\n };\n }\n }\n\n protected override async readWriteThroughVirtual(\n via: string,\n target: SmrtObject,\n ): Promise<Record<string, unknown>> {\n if (via !== 'profileId') return super.readWriteThroughVirtual(via, target);\n const profile = target as import('@happyvertical/smrt-profiles').Profile;\n return { phone: (await profile.getMetadata()).phone ?? '' };\n }\n\n protected override async saveWriteThroughVirtual(\n via: string,\n target: SmrtObject,\n values: Record<string, unknown>,\n ): Promise<void> {\n if (via !== 'profileId')\n return super.saveWriteThroughVirtual(via, target, values);\n if (!Object.hasOwn(values, 'phone')) return;\n if (typeof values.phone !== 'string') throw new Error('Phone must be text');\n const profile = target as import('@happyvertical/smrt-profiles').Profile;\n // Empty optional controls mean no phone. Metadata values remain required.\n if (!values.phone.trim()) {\n if (Object.hasOwn(await profile.getMetadata(), 'phone'))\n await profile.removeMetadata('phone');\n return;\n }\n const { ProfileMetafieldCollection } = await import(\n '@happyvertical/smrt-profiles'\n );\n const metafields = await ProfileMetafieldCollection.create({ db: this.db });\n await metafields.getOrUpsert(\n { slug: 'phone', tenantId: this.tenantId },\n {\n name: 'Phone',\n validation: { type: 'string' },\n },\n );\n await profile.addMetadata('phone', values.phone);\n }\n\n override async initialize(): Promise<this> {\n await super.initialize();\n this.contacts = normalizePartyContacts(this.contacts);\n return this;\n }\n\n /** Save with a final contacts normalization against direct assignment. */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n this.contacts = normalizePartyContacts(this.contacts);\n return super.save(options) as Promise<this>;\n }\n\n /**\n * Convenience predicate: is this a wholesale customer?\n */\n isWholesale(): boolean {\n return this.customerType === CustomerType.WHOLESALE;\n }\n\n /**\n * Check if customer is active\n */\n isActive(): boolean {\n return this.status === CustomerStatus.ACTIVE;\n }\n\n /**\n * Check if customer has available credit\n */\n hasAvailableCredit(amount: number): boolean {\n // In a full implementation, this would check against outstanding balances\n return this.creditLimit >= amount;\n }\n\n /**\n * Get the profile ID for external lookup.\n *\n * To get the actual Profile object, use the ProfileCollection from smrt-profiles:\n * ```typescript\n * const profiles = await ProfileCollection.create(options);\n * const profile = await profiles.get({ id: customer.profileId });\n * ```\n *\n * @returns The profile ID or empty string if not set\n */\n getProfileId(): string {\n return this.profileId;\n }\n}\n\nexport default Customer;\n","/**\n * CustomerCollection - Collection manager for Customer objects\n * @packageDocumentation\n */\n\nimport {\n type AuditContext,\n SmrtCollection,\n type SmrtCreateInput,\n} from '@happyvertical/smrt-core';\nimport { queryGlobal, queryWithGlobals } from '@happyvertical/smrt-tenancy';\nimport { Customer } from '../models/Customer.js';\nimport { type CustomerOptions, CustomerStatus } from '../types/index.js';\n\nexport class CustomerCollection extends SmrtCollection<Customer> {\n static readonly _itemClass = Customer;\n\n /** The ordinary create/update lifecycle also accepts the declared linked contact fields. */\n override async create(\n options: SmrtCreateInput<Customer> &\n Pick<CustomerOptions, 'name' | 'email' | 'phone'>,\n context?: Partial<AuditContext>,\n ): Promise<Customer> {\n return super.create(options, context);\n }\n override async update(\n id: string,\n values: Partial<\n SmrtCreateInput<Customer> &\n Pick<CustomerOptions, 'name' | 'email' | 'phone'>\n >,\n context?: Partial<AuditContext>,\n ): Promise<Customer | null> {\n return super.update(id, values, context);\n }\n\n /**\n * Find customers by profile ID\n *\n * @param profileId - Profile ID from smrt-profiles\n * @returns Array of customers linked to this profile\n */\n async findByProfile(profileId: string): Promise<Customer[]> {\n return await this.list({\n where: { profileId },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find all active customers\n *\n * @returns Array of active customers\n */\n async findActive(): Promise<Customer[]> {\n return await this.list({\n where: { status: CustomerStatus.ACTIVE },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find customers by status\n *\n * @param status - Customer status\n * @returns Array of customers\n */\n async findByStatus(status: CustomerStatus): Promise<Customer[]> {\n return await this.list({\n where: { status },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Get or create a customer for a profile\n *\n * @param profileId - Profile ID\n * @param defaults - Default values if creating\n * @returns Customer\n */\n async getOrCreateForProfile(\n profileId: string,\n defaults: Partial<{\n creditLimit: number;\n paymentTerms: string;\n }> = {},\n ): Promise<Customer> {\n const existing = await this.findByProfile(profileId);\n if (existing.length > 0) {\n return existing[0];\n }\n\n const customer = await this.create({\n profileId,\n creditLimit: defaults.creditLimit ?? 0,\n paymentTerms: defaults.paymentTerms ?? '',\n status: CustomerStatus.ACTIVE,\n });\n await customer.save();\n return customer;\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all customers belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of customers for the tenant\n */\n async findByTenant(tenantId: string): Promise<Customer[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global customers (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global customers\n */\n async findGlobal(): Promise<Customer[]> {\n return queryGlobal<Customer>(this);\n }\n\n /**\n * Find customers for a tenant including global customers.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global customers\n */\n async findWithGlobals(tenantId: string): Promise<Customer[]> {\n return queryWithGlobals<Customer>(\n this,\n tenantId,\n 'Customer.findWithGlobals',\n );\n }\n}\n","import {\n field,\n foreignKey,\n SmrtObject,\n type SmrtObjectOptions,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport { assertHistoricalCommerceWrite } from '../historical-commerce-internal.js';\nimport type { HistoricalSalesLineInput } from '../historical-sales-types.js';\nexport interface HistoricalSalesLineOptions\n extends SmrtObjectOptions,\n Partial<HistoricalSalesLineInput> {\n tenantId?: string;\n invoiceId?: string;\n sourceKeyHash?: string;\n sourceFieldsJson?: string;\n moneySnapshotId?: string;\n}\n/** An immutable source line with explicit unknowns, never a calculated InvoiceLineItem. */\n@TenantScoped({ mode: 'required' })\n@smrt({\n tableName: 'historical_sales_lines',\n indexes: [\n {\n name: 'historical_sales_line_source_key',\n columns: ['tenantId', 'invoiceId', 'sourceKeyHash'],\n unique: true,\n },\n ],\n api: { include: ['get', 'list'] },\n cli: { include: ['get', 'list'] },\n mcp: { include: ['get', 'list'] },\n})\nexport class HistoricalSalesLine extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @tenantId() tenantId = '';\n @foreignKey('Invoice') invoiceId = '';\n @foreignKey('HistoricalSalesLine', { nullable: true }) creditedLineId:\n | string\n | null = null;\n sourceKey = '';\n sourceKeyHash = '';\n description = '';\n @field({ ui: { format: 'money' } }) netMinor = 0;\n @field({ type: 'integer', nullable: true, ui: { format: 'money' } }) taxMinor:\n | number\n | null = null;\n @field({ type: 'integer', nullable: true, ui: { format: 'money' } })\n grossMinor: number | null = null;\n @field({ type: 'text', nullable: true }) quantityText: string | null = null;\n @field({ type: 'text', nullable: true }) unitRateText: string | null = null;\n sourceFieldsJson = '{}';\n moneySnapshotId = '';\n constructor(options: HistoricalSalesLineOptions = {}) {\n super(options);\n for (const key of [\n 'tenantId',\n 'invoiceId',\n 'creditedLineId',\n 'sourceKey',\n 'sourceKeyHash',\n 'description',\n 'netMinor',\n 'taxMinor',\n 'grossMinor',\n 'quantityText',\n 'unitRateText',\n 'sourceFieldsJson',\n 'moneySnapshotId',\n ] as const)\n if (options[key] !== undefined)\n Object.assign(this, { [key]: options[key] });\n if (options.sourceFields !== undefined)\n this.sourceFieldsJson = JSON.stringify(options.sourceFields);\n }\n override async save(options?: SmrtSaveOptions): Promise<this> {\n assertHistoricalCommerceWrite(this);\n if (\n !this.tenantId ||\n !this.invoiceId ||\n !this.sourceKey ||\n !this.sourceKeyHash ||\n !this.moneySnapshotId\n )\n throw new Error('Historical source line identity is incomplete');\n if (this.id && (await this.db.get(this.tableName, { id: this.id })))\n throw new Error('Historical sales source lines are immutable');\n return super.save(options);\n }\n override async delete(): Promise<void> {\n throw new Error('Historical sales source lines cannot be deleted');\n }\n}\n","import { SmrtCollection } from '@happyvertical/smrt-core';\nimport { HistoricalSalesLine } from '../models/HistoricalSalesLine.js';\nexport class HistoricalSalesLineCollection extends SmrtCollection<HistoricalSalesLine> {\n static readonly _itemClass = HistoricalSalesLine;\n}\n","import {\n field,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport { MoneyValidationError } from '../money.js';\nimport type { TaxCodeOptions, TaxCodePart } from '../types/index.js';\n\nexport type TaxCodeKind = 'taxable' | 'zero-rated' | 'exempt' | 'out-of-scope';\nexport type TaxCodeAppliesTo = 'sales' | 'purchases' | 'both';\n\n/** A reusable tax code whose ordered component list is safe to edit. */\n@TenantScoped({ mode: 'required' })\n@smrt({\n conflictColumns: ['tenant_id', 'code'],\n api: {\n include: ['list', 'get', 'create', 'update'],\n writable: ['code', 'name', 'kind', 'appliesTo', 'components', 'active'],\n },\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class TaxCode extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @tenantId()\n tenantId = '';\n\n @field({ description: 'A stable short code, for example gst-pst-bc.' })\n code = '';\n\n @field({ description: 'The name people see for this tax code.' })\n name = '';\n\n @field({ description: 'How the tax code is reported.' })\n kind: TaxCodeKind = 'taxable';\n\n @field({\n description: 'Whether the code applies to sales, purchases or both.',\n })\n appliesTo: TaxCodeAppliesTo = 'both';\n\n /** Ordered component IDs; compound includes every preceding component in this code. */\n /** Stored JSON is guarded through the typed accessor below. */\n componentsJson = '[]';\n\n @field({\n description: 'The ordered tax components in this code.',\n transient: true,\n })\n get components(): TaxCodePart[] {\n try {\n const value: unknown = JSON.parse(this.componentsJson);\n if (!Array.isArray(value)) return [];\n return value.filter(\n (part): part is TaxCodePart =>\n !!part &&\n typeof part === 'object' &&\n typeof (part as TaxCodePart).taxComponentId === 'string' &&\n typeof (part as TaxCodePart).compound === 'boolean',\n );\n } catch {\n return [];\n }\n }\n\n set components(value: TaxCodePart[] | string) {\n this.componentsJson =\n typeof value === 'string' ? value : JSON.stringify(value);\n }\n\n @field({\n description: 'Whether users may select this code for new documents.',\n })\n active = true;\n\n constructor(options: TaxCodeOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId ?? '';\n if (options.code !== undefined) this.code = options.code;\n if (options.name !== undefined) this.name = options.name;\n if (options.kind !== undefined) this.kind = options.kind;\n if (options.appliesTo !== undefined) this.appliesTo = options.appliesTo;\n if (options.components !== undefined)\n this.components = structuredClone(options.components);\n if (options.active !== undefined) this.active = options.active;\n }\n\n override async save(options?: SmrtSaveOptions): Promise<this> {\n if (!this.code.trim() || !this.name.trim())\n throw new Error('Tax code requires a nonempty code and name');\n if (\n !['taxable', 'zero-rated', 'exempt', 'out-of-scope'].includes(this.kind)\n )\n throw new Error('Tax code kind is invalid');\n if (!['sales', 'purchases', 'both'].includes(this.appliesTo))\n throw new Error('Tax code appliesTo is invalid');\n const parts = this.parsedComponents();\n if (!parts)\n throw new Error('Tax code components must be a valid JSON array');\n const nonTaxable = this.kind !== 'taxable';\n if (nonTaxable && parts.length) {\n throw new MoneyValidationError(\n 'TAX_CODE_NON_TAXABLE_COMPONENTS',\n `${this.kind} tax codes cannot have components.`,\n );\n }\n if (!nonTaxable && !parts.length) {\n throw new MoneyValidationError(\n 'TAX_CODE_TAXABLE_EMPTY',\n 'Taxable tax codes require at least one component.',\n );\n }\n const seen = new Set<string>();\n for (const component of parts) {\n if (seen.has(component.taxComponentId)) {\n throw new MoneyValidationError(\n 'TAX_CODE_DUPLICATE_COMPONENT',\n 'A tax code cannot contain the same component twice.',\n );\n }\n seen.add(component.taxComponentId);\n }\n return this.withTransaction(async () => {\n const { TaxComponentCollection } = await import(\n '../collections/TaxComponentCollection.js'\n );\n const components = await TaxComponentCollection.create({ db: this.db });\n for (const part of parts) {\n const component = await components.get(\n { id: part.taxComponentId },\n { cache: false },\n );\n if (!component) {\n throw new Error('Tax code components must belong to the same tenant');\n }\n if (!component.updated_at)\n throw new Error('Tax code component is missing its revision');\n await component.claimRevision(component.updated_at);\n if (!this.tenantId) this.tenantId = component.tenantId;\n if (component.tenantId !== this.tenantId)\n throw new Error('Tax code components must belong to the same tenant');\n }\n return (await super.save(options)) as this;\n });\n }\n\n private parsedComponents(): TaxCodePart[] | null {\n try {\n const value: unknown = JSON.parse(this.componentsJson);\n if (!Array.isArray(value)) return null;\n if (\n value.some(\n (part) =>\n !part ||\n typeof part !== 'object' ||\n typeof (part as TaxCodePart).taxComponentId !== 'string' ||\n !(part as TaxCodePart).taxComponentId.trim() ||\n typeof (part as TaxCodePart).compound !== 'boolean',\n )\n )\n return null;\n return value as TaxCodePart[];\n } catch {\n return null;\n }\n }\n}\n","import { SmrtCollection } from '@happyvertical/smrt-core';\nimport { TaxCode } from '../models/TaxCode.js';\n\nexport class TaxCodeCollection extends SmrtCollection<TaxCode> {\n static readonly _itemClass = TaxCode;\n\n /**\n * Find a code by its stable `code`. Pass `tenantId` whenever it is known:\n * without an active tenancy filter the lookup is otherwise unscoped.\n */\n async findByCode(code: string, tenantId?: string): Promise<TaxCode | null> {\n return (\n (\n await this.list({\n where: tenantId ? { code, tenantId } : { code },\n limit: 1,\n })\n )[0] ?? null\n );\n }\n}\n","import {\n crossPackageRef,\n field,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { AccountCollection } from '@happyvertical/smrt-ledgers';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport { MoneyValidationError } from '../money.js';\nimport type { TaxComponentOptions } from '../types/index.js';\n\nconst PERCENT = /^(\\d+)(?:\\.(\\d{1,6}))?$/;\n\nfunction isPercent(value: string): boolean {\n const match = PERCENT.exec(value);\n if (!match) return false;\n const scaled =\n BigInt(match[1]) * 1_000_000n + BigInt((match[2] ?? '').padEnd(6, '0'));\n return scaled <= 100_000_000n;\n}\n\nfunction canonicalPercent(value: string): string {\n const [whole, fraction = ''] = String(value).split('.');\n const normalizedWhole = whole.replace(/^0+(?=\\d)/, '') || '0';\n const normalizedFraction = fraction.replace(/0+$/, '');\n return normalizedFraction\n ? `${normalizedWhole}.${normalizedFraction}`\n : normalizedWhole;\n}\n\n/**\n * One reusable tax rate component. Codes may combine components, but the\n * component's percent is immutable after it has been saved: archive it and\n * repoint editable codes to a replacement rate instead of rewriting history.\n */\n@TenantScoped({ mode: 'required' })\n@smrt({\n api: {\n include: ['list', 'get', 'create', 'update'],\n writable: [\n 'name',\n 'percent',\n 'liabilityAccountId',\n 'recoverable',\n 'active',\n ],\n },\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class TaxComponent extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @tenantId()\n tenantId = '';\n\n @field({ description: 'The name of this tax component, for example GST.' })\n name = '';\n\n /** Percent, not a fraction: 7.125 means 7.125%. Stored as DECIMAL. */\n @field({\n type: 'decimal',\n description: 'The tax percentage, for example 7.125 for 7.125 percent.',\n })\n percent = '0.0';\n\n @crossPackageRef('@happyvertical/smrt-ledgers:Account', {\n description: 'The liability account that receives this tax component.',\n })\n liabilityAccountId = '';\n\n @field({\n description:\n 'Whether purchases using this component may recover the full input tax. Partial recovery is not supported.',\n })\n recoverable = false;\n\n @field({ description: 'Whether new tax codes and overrides may select it.' })\n active = true;\n\n constructor(options: TaxComponentOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId ?? '';\n if (options.name !== undefined) this.name = options.name;\n if (options.percent !== undefined) this.percent = options.percent;\n if (options.liabilityAccountId !== undefined)\n this.liabilityAccountId = options.liabilityAccountId;\n if (options.recoverable !== undefined)\n this.recoverable = options.recoverable;\n if (options.active !== undefined) this.active = options.active;\n }\n\n override async initialize(): Promise<this> {\n await super.initialize();\n this.percent = canonicalPercent(String(this.percent));\n return this;\n }\n\n override async save(options?: SmrtSaveOptions): Promise<this> {\n if (!this.name.trim())\n throw new Error('Tax component requires a nonempty name');\n if (!isPercent(this.percent)) {\n throw new MoneyValidationError(\n 'PERCENT_OUT_OF_RANGE',\n 'Tax component percent must be between 0 and 100 with at most six decimal places.',\n );\n }\n this.percent = canonicalPercent(this.percent);\n return this.withTransaction(async () => {\n const { TaxComponentCollection } = await import(\n '../collections/TaxComponentCollection.js'\n );\n const components = await TaxComponentCollection.create({ db: this.db });\n const persisted = this.id\n ? await components.get({ id: this.id }, { cache: false })\n : null;\n if (persisted && canonicalPercent(persisted.percent) !== this.percent) {\n const callerRevision = options?.expectedUpdatedAt;\n const referenced = await this.isReferenced(persisted, callerRevision);\n // claimRevision advanced the row's version; the pending save must use\n // that version rather than its stale caller snapshot.\n this.updated_at = persisted.updated_at;\n if (callerRevision !== undefined && persisted.updated_at)\n options = { ...options, expectedUpdatedAt: persisted.updated_at };\n if (referenced) {\n throw new MoneyValidationError(\n 'PERCENT_OUT_OF_RANGE',\n 'A referenced tax component rate is immutable; archive it and repoint its tax codes.',\n );\n }\n }\n if (this.liabilityAccountId) {\n const accounts = await AccountCollection.create({ db: this.db });\n const account = await accounts.get(\n { id: this.liabilityAccountId },\n { cache: false },\n );\n if (!account || account.tenantId !== this.tenantId) {\n throw new Error(\n 'Tax component liability account must belong to its tenant',\n );\n }\n }\n return (await super.save(options)) as this;\n });\n }\n\n private async isReferenced(\n persisted: TaxComponent,\n expectedUpdatedAt?: Date | string,\n ): Promise<boolean> {\n if (!this.id) return false;\n if (!persisted.updated_at)\n throw new Error('Referenced tax component is missing its revision');\n // Claim before inspecting references. TaxCode.save() claims the same row\n // before attaching it, so a concurrent rate edit must reload and retry.\n await persisted.claimRevision(expectedUpdatedAt ?? persisted.updated_at);\n const { TaxCodeCollection } = await import(\n '../collections/TaxCodeCollection.js'\n );\n const { DocumentTaxCollection } = await import(\n '../collections/DocumentTaxCollection.js'\n );\n const codes = await TaxCodeCollection.create({ db: this.db });\n const usedByCode = (\n await codes.list({ where: { tenantId: this.tenantId } })\n ).some((code) =>\n code.components.some((component) => component.taxComponentId === this.id),\n );\n if (usedByCode) return true;\n const documentTaxes = await DocumentTaxCollection.create({ db: this.db });\n return (\n (\n await documentTaxes.list({\n where: { tenantId: this.tenantId, taxComponentId: this.id },\n limit: 1,\n })\n ).length > 0\n );\n }\n}\n","import { SmrtCollection } from '@happyvertical/smrt-core';\nimport { TaxComponent } from '../models/TaxComponent.js';\n\nexport class TaxComponentCollection extends SmrtCollection<TaxComponent> {\n static readonly _itemClass = TaxComponent;\n}\n","/**\n * Vendor model - links to Profile with vendor-specific data\n * @packageDocumentation\n */\n\nimport {\n crossPackageRef,\n field,\n foreignKey,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport { normalizePartyContacts } from '../party-contacts.js';\nimport {\n type PartyContact,\n type VendorOptions,\n VendorStatus,\n} from '../types/index.js';\n\n/**\n * Vendor represents the vendor/supplier role for a Profile.\n *\n * One Profile can have both Customer and Vendor records - e.g., a company\n * you both buy from and sell to.\n *\n * @example\n * ```typescript\n * const vendor = await vendors.create({\n * profileId: 'profile-uuid',\n * leadTimeDays: 14,\n * minimumOrderAmount: 500,\n * paymentTerms: 'Net 60'\n * });\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n api: { include: ['list', 'get', 'create', 'update'] },\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class Vendor extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /**\n * Tenant ID for multi-tenant isolation\n * Nullable to support both tenant-scoped and global vendors\n */\n @tenantId({ nullable: true })\n tenantId: string | null = null;\n\n /**\n * Reference to smrt-profiles Profile\n * Plain string for cross-package reference\n */\n @crossPackageRef('@happyvertical/smrt-profiles:Profile', {\n description:\n 'Links this vendor to their profile record, where their name and contact details are kept.',\n })\n profileId: string = '';\n\n /**\n * Typical lead time in days for orders\n */\n @field({\n description:\n 'How many days the vendor usually takes between your order and delivery.',\n })\n leadTimeDays: number = 0;\n\n /**\n * Minimum order amount required, in **integer minor units** (cents,\n * satoshis). Compared against `Contract.totalAmount`, which is also minor\n * units (#2401).\n */\n @field({\n description: 'The smallest order the vendor will accept.',\n ui: { format: 'money' },\n })\n minimumOrderAmount: number = 0;\n\n /**\n * Payment terms for this vendor (e.g., \"Net 60\")\n */\n @field({\n description:\n 'How long you have to pay this vendor, written the way you would say it, for example \"Net 30\".',\n })\n paymentTerms: string = '';\n\n /**\n * Default currency for transactions with this vendor\n */\n @field({\n description: 'The currency this vendor bills you in.',\n ui: { widget: 'currency' },\n })\n currency: string = 'USD';\n\n /**\n * Supplier registration number.\n *\n * Sensitive for the same reason as a customer's tax ID: it is identifying\n * business information and must not enter generated list/search surfaces.\n */\n @field({\n sensitive: true,\n description: 'The vendor tax or VAT registration number kept on file.',\n })\n taxId: string = '';\n\n /** Tax code normally used for purchases from this vendor. */\n @foreignKey('TaxCode', {\n description: 'The tax code normally used when buying from this vendor.',\n })\n defaultTaxCodeId: string = '';\n\n /**\n * Default contact email for orders\n */\n @field({\n description: 'The email address you normally use to reach this vendor.',\n ui: { widget: 'email' },\n })\n defaultContactEmail: string = '';\n\n /**\n * Default contact phone for orders\n */\n @field({\n description: 'The phone number you normally use to reach this vendor.',\n ui: { widget: 'phone' },\n })\n defaultContactPhone: string = '';\n\n /**\n * Vendor status\n */\n @field({\n description:\n 'Whether you still buy from this vendor: active, inactive or suspended.',\n })\n status: VendorStatus = VendorStatus.ACTIVE;\n\n /**\n * Internal notes about this vendor\n */\n @field({\n description: 'Anything worth remembering about this vendor.',\n ui: { widget: 'textarea' },\n })\n notes: string = '';\n\n /**\n * Per-currency payout destinations for this vendor.\n *\n * Stored as a JSON column. Keys are payout-rail-qualified currency codes —\n * the code must encode both the asset and the rail (e.g. `USDC-base`,\n * `USDC-solana`, `BTC`, `USD-stripe`, `USD-wire`). Values are whatever the\n * receiving rail needs as a destination (EVM address, BTC address, Stripe\n * Connect account id, IBAN, etc.).\n *\n * MVP shape: a flat map on the vendor row. A vendor with no entry for a\n * given currency cannot be paid in that currency — filtering is the\n * consumer's responsibility (the upstream `PaymentIntent` builder filters\n * `paymentOptions` at quote time against this map).\n *\n * Use {@link getPayoutAddress} for a `Map.get`-style lookup that returns\n * `undefined` for missing entries; direct property access is also fine.\n *\n * Sensitive (#1540): payout destinations are excluded from generated API/MCP\n * responses and rejected as a `where` filter key.\n */\n @field({\n sensitive: true,\n description: 'Where payments to this vendor should be sent.',\n })\n payoutAddresses: Record<string, string> = {};\n\n /**\n * People to reach at this vendor, stored as a JSON column (#3501). Write\n * form rows through `savePartyContacts()`; every save normalizes the shape.\n */\n @field({\n description:\n 'The people you deal with at this vendor, with their role and how to reach them.',\n })\n contacts: PartyContact[] = [];\n\n constructor(options: VendorOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.profileId !== undefined) this.profileId = options.profileId;\n if (options.leadTimeDays !== undefined)\n this.leadTimeDays = options.leadTimeDays;\n if (options.minimumOrderAmount !== undefined)\n this.minimumOrderAmount = options.minimumOrderAmount;\n if (options.paymentTerms !== undefined)\n this.paymentTerms = options.paymentTerms;\n if (options.currency !== undefined) this.currency = options.currency;\n if (options.taxId !== undefined) this.taxId = options.taxId;\n if (options.defaultTaxCodeId !== undefined)\n this.defaultTaxCodeId = options.defaultTaxCodeId;\n if (options.defaultContactEmail !== undefined)\n this.defaultContactEmail = options.defaultContactEmail;\n if (options.defaultContactPhone !== undefined)\n this.defaultContactPhone = options.defaultContactPhone;\n if (options.status !== undefined) this.status = options.status;\n if (options.notes !== undefined) this.notes = options.notes;\n if (options.payoutAddresses !== undefined) {\n this.payoutAddresses = Vendor.normalizePayoutAddresses(\n options.payoutAddresses,\n );\n }\n if (options.contacts !== undefined)\n this.contacts = normalizePartyContacts(options.contacts);\n }\n\n /**\n * The framework's {@link SmrtObject.initialize} re-applies `options` keys on\n * top of constructor-set values so option data always wins over field\n * initializers. That means our constructor's normalization runs *before*\n * `initializePropertiesFromOptions` overwrites `payoutAddresses` with the\n * raw cloned value. Re-normalize once super has finished so consumers\n * who pass a string, a partially-typed map, or `null` end up with a\n * clean `Record<string, string>` before any save / read happens.\n */\n override async initialize(): Promise<this> {\n await super.initialize();\n this.payoutAddresses = Vendor.normalizePayoutAddresses(\n this.payoutAddresses as unknown,\n );\n this.contacts = normalizePartyContacts(this.contacts);\n return this;\n }\n\n /**\n * Save with a final normalization pass so direct field assignments\n * (`vendor.payoutAddresses = somethingFunny`) can't smuggle non-string\n * values into the persisted row.\n */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n this.payoutAddresses = Vendor.normalizePayoutAddresses(\n this.payoutAddresses as unknown,\n );\n this.contacts = normalizePartyContacts(this.contacts);\n return super.save(options) as Promise<this>;\n }\n\n /**\n * Check if vendor is active\n */\n isActive(): boolean {\n return this.status === VendorStatus.ACTIVE;\n }\n\n /**\n * Check if order meets minimum requirements\n */\n meetsMinimumOrder(amount: number): boolean {\n return amount >= this.minimumOrderAmount;\n }\n\n /**\n * Get the profile ID for external lookup.\n *\n * To get the actual Profile object, use the ProfileCollection from smrt-profiles:\n * ```typescript\n * const profiles = await ProfileCollection.create(options);\n * const profile = await profiles.get({ id: vendor.profileId });\n * ```\n *\n * @returns The profile ID or empty string if not set\n */\n getProfileId(): string {\n return this.profileId;\n }\n\n /**\n * Look up the payout destination for a single currency. Returns\n * `undefined` if the vendor has no entry for that currency — caller's\n * job to decide whether that means \"skip the option\" or \"raise a\n * configuration error\". The lookup is case-sensitive; the contract is\n * that the currency code passed in matches exactly what was stored\n * (`USDC-base`, not `usdc-base`).\n */\n getPayoutAddress(currency: string): string | undefined {\n const map = this.payoutAddresses;\n if (!map || typeof map !== 'object') return undefined;\n const value = (map as Record<string, unknown>)[currency];\n return typeof value === 'string' ? value : undefined;\n }\n\n /**\n * Set the payout destination for a single currency. Mutates the live\n * map and returns `this` for chaining. Use {@link clearPayoutAddress}\n * to remove a single entry.\n */\n setPayoutAddress(currency: string, destination: string): this {\n this.payoutAddresses = {\n ...(this.payoutAddresses ?? {}),\n [currency]: destination,\n };\n return this;\n }\n\n /**\n * Remove a single currency entry from the payout map. No-op if the\n * currency was already absent.\n */\n clearPayoutAddress(currency: string): this {\n if (!this.payoutAddresses || !(currency in this.payoutAddresses)) {\n return this;\n }\n const next = { ...this.payoutAddresses };\n delete next[currency];\n this.payoutAddresses = next;\n return this;\n }\n\n /**\n * Normalize an input value (object, pre-serialized JSON string, or\n * `null`/`undefined`) into a plain `Record<string, string>`. Used by\n * the constructor so consumers can pass either shape into\n * `VendorCollection.create({ payoutAddresses: ... })`.\n *\n * Non-string values inside an input object are silently dropped — a\n * `Record<string, string>` invariant is what consumers downstream\n * (`PaymentIntent` builders, payout SDK adapters) rely on.\n */\n private static normalizePayoutAddresses(\n value: unknown,\n ): Record<string, string> {\n if (value == null) return {};\n let parsed: unknown = value;\n if (typeof value === 'string') {\n try {\n parsed = JSON.parse(value);\n } catch {\n return {};\n }\n }\n if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {\n return {};\n }\n const out: Record<string, string> = {};\n for (const [key, v] of Object.entries(parsed as Record<string, unknown>)) {\n if (typeof v === 'string') out[key] = v;\n }\n return out;\n }\n}\n\nexport default Vendor;\n","/**\n * Contract model with STI for different contract types\n * @packageDocumentation\n */\n\nimport {\n field,\n foreignKey,\n type Meta,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport {\n DOCUMENT_MONEY_READ_REBIND,\n DOCUMENT_MONEY_WRITE,\n type DocumentMoneyReadRebindCapability,\n type DocumentMoneyWriteCapability,\n} from '../document-money-internal.js';\nimport { assertIntegerMinorUnits } from '../money.js';\nimport {\n type ContractOptions,\n ContractStatus,\n ContractType,\n type LicenseSaleOptions,\n type PurchaseContractOptions,\n type PurchaseOrderOptions,\n type SalesContractOptions,\n type TaxRegistrationOptions,\n} from '../types/index.js';\nimport { Customer } from './Customer.js';\nimport { Vendor } from './Vendor.js';\n\n/**\n * Legal status transitions for a Contract (and all its STI subtypes), keyed by\n * the prior persisted status. A status re-saved unchanged (no-op) and a\n * brand-new row are always permitted; this map governs *changes* only.\n *\n * Flow: DRAFT → SENT → (ACCEPTED | DECLINED); ACCEPTED → (COMPLETED |\n * CANCELLED). An estimate/order may also be cancelled directly from DRAFT/SENT.\n * DECLINED / COMPLETED / CANCELLED are terminal. This guards against raw\n * mass-assignment forcing, e.g., a DECLINED contract back to ACCEPTED or a\n * COMPLETED one back to DRAFT. See S5 audit #1390.\n */\nconst CONTRACT_STATUS_TRANSITIONS: Record<ContractStatus, ContractStatus[]> = {\n [ContractStatus.DRAFT]: [\n ContractStatus.SENT,\n ContractStatus.ACCEPTED,\n ContractStatus.DECLINED,\n ContractStatus.CANCELLED,\n ],\n [ContractStatus.SENT]: [\n ContractStatus.ACCEPTED,\n ContractStatus.DECLINED,\n ContractStatus.CANCELLED,\n ],\n [ContractStatus.ACCEPTED]: [\n ContractStatus.COMPLETED,\n ContractStatus.CANCELLED,\n ],\n [ContractStatus.DECLINED]: [],\n [ContractStatus.COMPLETED]: [],\n [ContractStatus.CANCELLED]: [],\n};\n\n/**\n * Module-scoped record of the status each Contract instance was loaded with,\n * for the save-time transition guard. WeakMap keeps it out of the schema and\n * GCs with the instance — same pattern as the other commerce models.\n */\nconst loadedContractStatus = new WeakMap<Contract, ContractStatus>();\n\n/** Whether a Contract belongs to the purchase STI branch, including descendants. */\nexport function classifyContractPurchase(contract: Contract): boolean {\n return 'vendorId' in contract;\n}\n\n/** Frozen snapshots are reportable except cancelled contracts and supplier quotes. */\nexport function isContractSnapshotReportEligible(contract: Contract): boolean {\n return (\n !(contract instanceof SupplierQuote) &&\n contract.status !== ContractStatus.DRAFT &&\n contract.status !== ContractStatus.CANCELLED\n );\n}\n\n/**\n * Contract is the base class for all commercial agreements.\n *\n * Uses Single Table Inheritance (STI) for ten leaf types. SalesContract owns\n * the customer link for Estimate, Order, WholesaleOrder, Cart, Lease,\n * Agreement, and LicenseSale. PurchaseContract owns the vendor link for\n * SupplierQuote, PurchaseOrder and ProductionOrder. Both branches share `contracts`.\n *\n * @example\n * ```typescript\n * // Create a customer order\n * const order = await contracts.create({\n * _meta_type: 'Order',\n * customerId: customer.id,\n * totalAmount: 150000, // $1,500.00 in cents\n * status: ContractStatus.DRAFT\n * });\n *\n * // Create a purchase order to vendor\n * const po = await contracts.create({\n * _meta_type: 'PurchaseOrder',\n * vendorId: vendor.id,\n * totalAmount: 500000, // $5,000.00 in cents\n * reference: 'PO-2024-001'\n * });\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n display: {\n label: 'reference',\n list: [\n 'reference',\n 'customerId',\n 'vendorId',\n 'status',\n 'totalAmount',\n 'issueDate',\n 'dueDate',\n ],\n },\n tableStrategy: 'sti',\n api: { include: ['list', 'get', 'create', 'update'] },\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class Contract extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /** @internal Collection read guard returns this snapshot on its root DB. */\n rebindMoneyRead(\n capability: DocumentMoneyReadRebindCapability,\n db: DatabaseInterface,\n ): this {\n if (capability !== DOCUMENT_MONEY_READ_REBIND)\n throw new Error('Contract read rebinding requires the owning capability');\n this._db = db;\n this.options.db = db;\n return this;\n }\n\n /**\n * Tenant that owns this money document and scopes its tax reads\n */\n @tenantId({ nullable: false })\n @field({ required: true, requiredFor: 'tenant-scope' })\n tenantId: string | null = null;\n\n /**\n * Contract type discriminator (STI)\n */\n @field({\n description:\n 'The kind of contract: order, estimate, lease, agreement and so on.',\n })\n contractType: ContractType = ContractType.ORDER;\n\n /**\n * Current status of the contract\n */\n @field({\n description:\n 'Where the order or agreement stands, for example draft, sent, accepted or completed.',\n })\n status: ContractStatus = ContractStatus.DRAFT;\n\n /**\n * Subtotal before tax, in **integer minor units** (cents, satoshis).\n *\n * The `= 0` initializer is load-bearing: SMRT maps an integer literal to\n * INTEGER and a decimal literal to DECIMAL. Money is exact, so it is stored\n * as minor units and never as a float — `$19.99` is `1999`, not `19.99`\n * (#2401).\n */\n @field({ description: 'The amount before tax.', ui: { format: 'money' } })\n subtotal: number = 0;\n\n /** Informational sum of resolved line discounts, in integer minor units. */\n @field({ ui: { format: 'money' } })\n discountTotal: number = 0;\n\n /**\n * Tax amount, in integer minor units (#2401).\n */\n @field({ description: 'The tax charged.', ui: { format: 'money' } })\n taxAmount: number = 0;\n\n /**\n * Total amount including tax, in integer minor units (#2401).\n */\n @field({\n description: 'The full amount, tax included.',\n ui: { format: 'money' },\n })\n totalAmount: number = 0;\n\n /**\n * Currency code (ISO 4217)\n */\n @field({\n description: 'The currency of the amounts.',\n ui: { widget: 'currency' },\n })\n currency: string = 'USD';\n\n /** Calculated pricing or immutable source-declared purchase evidence. */\n @field({\n description:\n 'Whether amounts were calculated or recorded from reviewed original paperwork.',\n })\n moneyAuthority: 'calculated' | 'received' = 'calculated';\n\n /** Guarded received evidence; write through the public received purchase/sales contract commands only. */\n receivedPurchaseMoneyJson = '';\n\n /**\n * Append-only approved milestone schedule and issued claim evidence.\n * `''` means no schedule; the column defaults to it, and a NULL stored by an\n * earlier writer hydrates as `''` (#4155).\n */\n @field({ type: 'text', default: '' })\n billingScheduleJson = '';\n\n /** Pricing interpretation fixed before the Contract leaves draft. */\n lineAmountType: 'exclusive' | 'inclusive' | 'none' = 'exclusive';\n\n /** Component rounding scope fixed before the Contract leaves draft. */\n taxRoundingScope: 'line' | 'document' = 'line';\n\n /** Immutable seller registration snapshot once the Contract leaves draft. */\n sellerTaxRegistrationsJson = '[]';\n\n get sellerTaxRegistrations(): TaxRegistrationOptions[] {\n try {\n const value: unknown = JSON.parse(this.sellerTaxRegistrationsJson);\n if (!Array.isArray(value)) return [];\n return value.filter(\n (entry): entry is TaxRegistrationOptions =>\n !!entry &&\n typeof entry === 'object' &&\n typeof (entry as TaxRegistrationOptions).taxComponentId ===\n 'string' &&\n typeof (entry as TaxRegistrationOptions).number === 'string' &&\n typeof (entry as TaxRegistrationOptions).label === 'string',\n );\n } catch {\n return [];\n }\n }\n\n set sellerTaxRegistrations(value: TaxRegistrationOptions[]) {\n this.sellerTaxRegistrationsJson = JSON.stringify(value);\n }\n\n /** Immutable customer or vendor tax identifier snapshot after issue. */\n partyTaxId = '';\n\n /** Stable issue/freeze identity, independent of lifecycle updated_at. */\n moneySnapshotId = '';\n\n /**\n * Date contract was issued\n */\n @field({\n description: 'The date it was issued or placed.',\n // A business date: the natural time axis of contract reports (#4100).\n ui: { format: 'date' },\n })\n issueDate: Date = new Date();\n\n /**\n * Payment due date\n */\n @field({ description: 'The date payment or delivery is due.' })\n dueDate: Date | null = null;\n\n /**\n * Expiry date (for estimates/quotes)\n */\n @field({ description: 'The last day the price or offer is good for.' })\n expiryDate: Date | null = null;\n\n /**\n * External reference number (PO number, quote number, etc.)\n */\n @field({\n description:\n 'Your own number or note for finding this later, such as a purchase order or quote number.',\n })\n reference: string = '';\n\n /**\n * Internal notes\n */\n @field({\n description: 'Anything worth remembering about this.',\n ui: { widget: 'textarea' },\n })\n notes: string = '';\n\n /**\n * Terms and conditions\n */\n @field({\n description:\n 'The conditions agreed, such as deposits, payment or delivery conditions.',\n ui: { widget: 'textarea' },\n })\n terms: string = '';\n\n /**\n * Sales channel that owns this contract.\n *\n * Plain string so consumers can model their own channels without an enum\n * change (e.g. `dtc-web`, `wholesale-b2b`, `pos-store-1`, `marketplace-faire`).\n * Empty string means \"unattributed / legacy\".\n */\n @field({\n description:\n 'Where it came from, for example your shop, a marketplace or a phone call.',\n })\n channelId: string = '';\n\n /**\n * STI discriminator field\n */\n static readonly _stiField = 'contractType';\n\n constructor(options: ContractOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.contractType !== undefined)\n this.contractType = options.contractType;\n if (options.status !== undefined) this.status = options.status;\n if (options.billingScheduleJson !== undefined)\n this.billingScheduleJson = options.billingScheduleJson;\n if (options.moneyAuthority !== undefined)\n this.moneyAuthority = options.moneyAuthority;\n if (options.receivedPurchaseMoneyJson !== undefined)\n this.receivedPurchaseMoneyJson = options.receivedPurchaseMoneyJson;\n if (options.subtotal !== undefined) this.subtotal = options.subtotal;\n if (options.discountTotal !== undefined)\n this.discountTotal = options.discountTotal;\n if (options.taxAmount !== undefined) this.taxAmount = options.taxAmount;\n if (options.totalAmount !== undefined)\n this.totalAmount = options.totalAmount;\n if (options.currency !== undefined) this.currency = options.currency;\n if (options.lineAmountType !== undefined)\n this.lineAmountType = options.lineAmountType;\n if (options.taxRoundingScope !== undefined)\n this.taxRoundingScope = options.taxRoundingScope;\n if (options.sellerTaxRegistrationsJson !== undefined)\n this.sellerTaxRegistrationsJson = options.sellerTaxRegistrationsJson;\n if (options.sellerTaxRegistrations !== undefined)\n this.sellerTaxRegistrations = structuredClone(\n options.sellerTaxRegistrations,\n );\n if (options.partyTaxId !== undefined) this.partyTaxId = options.partyTaxId;\n if (options.moneySnapshotId !== undefined)\n this.moneySnapshotId = options.moneySnapshotId;\n if (options.issueDate !== undefined) this.issueDate = options.issueDate;\n if (options.dueDate !== undefined) this.dueDate = options.dueDate;\n if (options.expiryDate !== undefined) this.expiryDate = options.expiryDate;\n if (options.reference !== undefined) this.reference = options.reference;\n if (options.notes !== undefined) this.notes = options.notes;\n if (options.terms !== undefined) this.terms = options.terms;\n if (options.channelId !== undefined) this.channelId = options.channelId;\n }\n\n /**\n * Check if contract is in draft state\n */\n isDraft(): boolean {\n return this.status === ContractStatus.DRAFT;\n }\n\n /**\n * Check if contract is accepted\n */\n isAccepted(): boolean {\n return this.status === ContractStatus.ACCEPTED;\n }\n\n /**\n * Check if contract is completed\n */\n isCompleted(): boolean {\n return this.status === ContractStatus.COMPLETED;\n }\n\n /**\n * Check if contract is expired (for estimates)\n */\n isExpired(): boolean {\n if (!this.expiryDate) return false;\n return new Date() > this.expiryDate;\n }\n\n /**\n * Check if contract is overdue\n */\n isOverdue(): boolean {\n if (!this.dueDate) return false;\n if (this.status === ContractStatus.COMPLETED) return false;\n return new Date() > this.dueDate;\n }\n\n /**\n * Calculate and update totals from line items\n */\n async recalculateTotals(): Promise<void> {\n const { recalculateContractTotals } = await import('../contract-money.js');\n await recalculateContractTotals(this);\n }\n\n /**\n * Capture the status the row was loaded with so the save-time transition\n * guard can reject illegal status flips made via raw field assignment\n * (mass-assignment on the generated update route, a stale caller, etc.).\n * Only persisted rows carry a prior status.\n */\n override async initialize(): Promise<this> {\n await super.initialize();\n // No schedule is `''`; a row written before the column had a default\n // holds NULL, which means the same thing (#4155).\n this.billingScheduleJson ??= '';\n // A row read from the database must not be old-format money data.\n if (this.isPersisted) {\n const { assertHydratedMoneyRowCurrent } = await import(\n '../upgrade/detect.js'\n );\n await assertHydratedMoneyRowCurrent(this);\n }\n if (await this.isSaved()) {\n loadedContractStatus.set(this, this.status);\n }\n return this;\n }\n\n /**\n * Validate the amounts and the status transition before persisting, then\n * save. Inherited by every STI subtype (Estimate/Order/Lease/.../LicenseSale),\n * so a forged `status` on any of them is caught here. LicenseSale layers its\n * own rights-immutability guard on top via `super.save(options)`. See S5 audit\n * #1390.\n *\n * `subtotal` / `taxAmount` / `totalAmount` are mass-assignable on the\n * generated create/update routes, so the minor-units check runs on every\n * write surface (#2401).\n */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n const { saveContract } = await import('../contract-money.js');\n return (await saveContract(this, options)) as this;\n }\n\n /** Received purchase monetary history cannot be deleted. */\n override async delete(\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n const { deleteContract } = await import('../contract-money.js');\n await deleteContract(this, options);\n }\n\n /** @internal Contract aggregate persistence only. */\n async deleteDerived(\n capability: DocumentMoneyWriteCapability,\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n if (capability !== DOCUMENT_MONEY_WRITE)\n throw new Error('Contract money write capability is required');\n await super.delete(options);\n }\n\n /** @internal Contract aggregate persistence only. */\n async saveDerived(\n capability: DocumentMoneyWriteCapability,\n options?: SmrtSaveOptions,\n ): Promise<this> {\n if (capability !== DOCUMENT_MONEY_WRITE)\n throw new Error('Contract money write capability is required');\n assertIntegerMinorUnits('Contract', this.reference || this.id || '<new>', [\n ['subtotal', this.subtotal],\n ['discountTotal', this.discountTotal],\n ['taxAmount', this.taxAmount],\n ['totalAmount', this.totalAmount],\n ]);\n const prior = await this.resolvePriorStatus();\n this.assertContractStatusTransition(prior);\n const result = (await super.save(options)) as this;\n loadedContractStatus.set(this, this.status);\n return result;\n }\n\n /**\n * Reject an illegal status flip done via raw assignment. No-op transitions\n * and brand-new rows are always allowed.\n */\n protected assertContractStatusTransition(\n prior: ContractStatus | undefined,\n ): void {\n if (prior === undefined) return; // new row — any starting status is fine\n if (prior === this.status) return; // no-op re-save\n const allowed = CONTRACT_STATUS_TRANSITIONS[prior] ?? [];\n if (!allowed.includes(this.status)) {\n throw new Error(\n `Contract ${this.reference || this.id}: illegal status transition ` +\n `'${prior}' → '${this.status}'.`,\n );\n }\n }\n\n /**\n * Resolve the AUTHORITATIVE prior status (S5 audit #1390 round 5, codex\n * MEDIUM#2). The WeakMap is only populated when {@link initialize} loaded the\n * row from the DB; it is empty for an instance built via\n * `collection.create({ id: <existing>, _skipLoad: true })` — the upsert path\n * that lets a caller write onto an existing row without hydrating it. Trusting\n * an empty WeakMap there would treat the write as a brand-new row and skip the\n * transition guard entirely (a poisonable prior-state). Mirrors the\n * authoritative-prior-load applied to Payment/PaymentIntent/Invoice/Payout.\n *\n * So when this instance carries an `id`, read the persisted row straight from\n * the database and treat its `status` as the prior — a create-onto-existing is\n * an update. Only when no row exists in the DB (truly new) do we fall back to\n * the WeakMap (which is also empty then), i.e. `undefined` = genuinely new.\n * The raw row's `status` column is single-word (no snake_case transform).\n */\n protected async resolvePriorStatus(): Promise<ContractStatus | undefined> {\n if (this.id) {\n try {\n const row = await this.db.get(this.tableName, { id: this.id });\n if (row && row.status != null) {\n return row.status as ContractStatus;\n }\n } catch {\n // DB not ready / table absent — fall through to the in-memory record.\n }\n }\n return loadedContractStatus.get(this);\n }\n}\n\n// Every STI child below MUST repeat the `@TenantScoped` decoration. The\n// tenancy registry keys off the concrete className passed by the\n// collection, not the inheritance chain — `OrderCollection.list()` sends\n// `'Order'` to the interceptor, which then misses the `'Contract'`\n// registration and skips tenant auto-filter/auto-populate. Without an\n// explicit `@TenantScoped` on each subclass, saves end up with\n// `tenantId: null` (the row becomes global / invisible to tenant-scoped\n// queries) and lists return rows across tenants.\n\n/** Customer-side contracts, including orders, estimates, and licenses. */\n@TenantScoped({ mode: 'optional' })\n@smrt({ abstract: true })\nexport abstract class SalesContract extends Contract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Customer, {\n description: 'The customer a sales order or agreement is for.',\n })\n customerId: string | null = null;\n\n constructor(options: SalesContractOptions = {}) {\n super(options);\n if (options.customerId !== undefined) this.customerId = options.customerId;\n }\n}\n\n/** Supplier-side contracts, including supplier quotes, purchase and production orders. */\n@TenantScoped({ mode: 'optional' })\n@smrt({ abstract: true })\nexport abstract class PurchaseContract extends Contract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Vendor, {\n description: 'The vendor supplying this quote or purchase contract.',\n })\n vendorId: string | null = null;\n\n constructor(options: PurchaseContractOptions = {}) {\n super(options);\n if (options.vendorId !== undefined) this.vendorId = options.vendorId;\n }\n}\n\n/**\n * Estimate - Quote or proposal for a customer\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class Estimate extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Customer)\n override customerId: string | null = null;\n override contractType = ContractType.ESTIMATE;\n\n constructor(options: SalesContractOptions = {}) {\n super(options);\n if (options.customerId !== undefined) this.customerId = options.customerId;\n }\n}\n\n/**\n * Order - Customer purchase order\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class Order extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Customer)\n override customerId: string | null = null;\n override contractType = ContractType.ORDER;\n\n /** Accepted Estimate this Order was converted from; empty for a direct Order. */\n @field({\n description: 'The accepted estimate this order was converted from, if any.',\n })\n sourceEstimateId: Meta<string> = '';\n\n /** Idempotency identity of the conversion that created this Order. */\n @field({\n description:\n 'The request identity of the conversion that created this order.',\n })\n conversionRequestId: Meta<string> = '';\n\n /** Host-owned evidence reference for the customer's acceptance of the Estimate. */\n @field({\n description:\n 'Where the proof of the customer accepting the estimate is kept.',\n })\n acceptanceEvidenceRef: Meta<string> = '';\n\n constructor(options: SalesContractOptions = {}) {\n super(options);\n if (options.customerId !== undefined) this.customerId = options.customerId;\n }\n}\n\n/**\n * Lease - Rental or lease agreement\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class Lease extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Customer)\n override customerId: string | null = null;\n override contractType = ContractType.LEASE;\n\n constructor(options: SalesContractOptions = {}) {\n super(options);\n if (options.customerId !== undefined) this.customerId = options.customerId;\n }\n}\n\n/**\n * Agreement - Service or maintenance agreement\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n display: {\n label: 'reference',\n list: ['reference', 'customerId', 'status', 'totalAmount', 'createdAt'],\n },\n})\nexport class Agreement extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Customer)\n override customerId: string | null = null;\n override contractType = ContractType.AGREEMENT;\n\n constructor(options: SalesContractOptions = {}) {\n super(options);\n if (options.customerId !== undefined) this.customerId = options.customerId;\n }\n}\n\n/** Supplier quotation received from a vendor; acceptance does not create an order. */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class SupplierQuote extends PurchaseContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Vendor)\n override vendorId: string | null = null;\n override contractType = ContractType.SUPPLIER_QUOTE;\n\n constructor(options: PurchaseContractOptions = {}) {\n super(options);\n if (options.vendorId !== undefined) this.vendorId = options.vendorId;\n }\n}\n\n/**\n * PurchaseOrder - Order sent to vendor/supplier\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class PurchaseOrder extends PurchaseContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Vendor)\n override vendorId: string | null = null;\n override contractType = ContractType.PURCHASE_ORDER;\n\n /** Expected delivery calendar date (YYYY-MM-DD), or empty when unspecified. */\n @field({\n description:\n 'The date you expect the delivery, as YYYY-MM-DD. Leave blank if it is not known.',\n })\n expectedDate: Meta<string> = '';\n\n /** Delivery address/instructions snapshot, independent of the vendor address. */\n @field({\n description:\n 'Where this order should be delivered, including any delivery instructions.',\n })\n shipTo: Meta<string> = '';\n\n /** Construct a purchase order with its optional delivery header. */\n constructor(options: PurchaseOrderOptions = {}) {\n super(options);\n if (options.vendorId !== undefined) this.vendorId = options.vendorId;\n if (options.expectedDate !== undefined)\n this.expectedDate = options.expectedDate;\n if (options.shipTo !== undefined) this.shipTo = options.shipTo;\n }\n}\n\n/**\n * WholesaleOrder - B2B order placed by a wholesale customer.\n *\n * Behaves like an Order but is conventionally created against a customer with\n * `customerType: 'wholesale'`, uses NET-30/60 payment terms, and is delivered\n * via the wholesale-portal channel rather than retail checkout.\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class WholesaleOrder extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @foreignKey(Customer)\n override customerId: string | null = null;\n override contractType = ContractType.WHOLESALE_ORDER;\n\n constructor(options: SalesContractOptions = {}) {\n super(options);\n if (options.customerId !== undefined) this.customerId = options.customerId;\n }\n}\n\n/**\n * ProductionOrder - instruction to commission goods from an internal or\n * external factory.\n *\n * The manufacturing equivalent of a {@link PurchaseOrder} — but instead of\n * buying finished inventory, you're paying to *make* it. A ProductionOrder\n * consumes raw materials per a Bill of Materials (see\n * `@happyvertical/smrt-manufacturing`) and produces finished SKU stock when\n * posted (see `@happyvertical/smrt-inventory`).\n *\n * Carries two STI-meta fields that `ProductionService.consumeMaterials` /\n * `runProduction` read from the loaded row to resolve which BOM to walk:\n *\n * - `productId` — plain string ref to the upstream `Product` (or any\n * STI subtype, e.g. apparel `Style`). Used to look up the *active*\n * BOM via `BomService.findActiveForProduct` when no explicit\n * revision was pinned at the time the order was placed.\n * - `bomId` — optional pin to a specific BOM revision. When set, the\n * manufacturing service consumes against that exact revision even\n * if a newer BOM has become active in the meantime (keeps a\n * posted run from silently switching recipes mid-flight).\n *\n * Both fields are `Meta<string>` so the scanner routes them into the\n * shared `_meta_data` JSON column on the Contract STI table instead of\n * widening the base schema with manufacturing-specific columns.\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class ProductionOrder extends PurchaseContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n override contractType = ContractType.PRODUCTION_ORDER;\n\n /**\n * Plain string reference to the upstream product the order is asked\n * to make. Cross-package id; never `@foreignKey()`. Required by\n * `ProductionService.consumeMaterials` unless `bomId` is supplied.\n */\n productId: Meta<string> = '';\n\n /**\n * Optional explicit BOM revision the order locked to at posting\n * time. When set, manufacturing uses this exact row even if the\n * product's active BOM has since changed. When empty, falls back\n * to the active BOM for `productId`.\n */\n bomId: Meta<string> = '';\n}\n\n/**\n * Cart - transient order-in-progress for a shopper.\n *\n * Persisted as a Contract so it can hold the same line items, totals, and\n * customer reference as a real Order, then convert in place (the application\n * promotes the row from `_meta_type: Cart` to `_meta_type: Order` at checkout\n * rather than copying data between tables).\n *\n * A Cart starts in {@link ContractStatus.DRAFT} via the base `Contract.status`\n * default — the subclass doesn't need its own override. Application code\n * is responsible for assigning the appropriate Order status at checkout;\n * the framework doesn't enforce a state machine on the cart → order transition.\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class Cart extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n override contractType = ContractType.CART;\n}\n\n/**\n * Snapshot of the rights granted by a {@link LicenseSale}. The fields are\n * intentionally typed (not a generic JSON blob) so they survive schema\n * migrations and downstream consumers (PDF templating, hash-registry\n * publishers) can rely on the shape without re-parsing.\n *\n * Each field is opaque text — the values that make sense vary by\n * industry. A stock-media marketplace might use:\n *\n * - `medium`: `web,print,social`\n * - `distributionScope`: `worldwide` / `territorial`\n * - `exclusivity`: `exclusive` / `non-exclusive`\n * - `duration`: `perpetual` / `12-months` / `event-only`\n * - `territory`: ISO-3166 list or `worldwide`\n *\n * Booleans (`sublicensing`, `derivatives`) are typed as such; everything\n * else is a string so the model doesn't dictate vocabulary.\n */\nexport interface LicenseRightsSnapshot {\n medium: string;\n distributionScope: string;\n exclusivity: string;\n duration: string;\n territory: string;\n sublicensing: boolean;\n derivatives: boolean;\n}\n\n/**\n * Module-scoped WeakMap of \"rights snapshot the row was loaded with\".\n *\n * Used by {@link LicenseSale}'s immutability guard. We can't store the\n * captured snapshot as an instance field because the AST scanner would\n * pick it up as a persisted column; we can't store it as a `Meta<T>`\n * because then it'd round-trip with the row and the comparison would\n * always tautologically succeed. A WeakMap keyed by the instance is\n * the cleanest scope: no schema interaction, garbage-collected with\n * the instance.\n */\nconst issuedRightsSnapshot = new WeakMap<LicenseSale, string>();\n\n/**\n * LicenseSale — industry-neutral licensing primitive.\n *\n * Sits alongside the other Contract STI subtypes (Estimate, Order,\n * Lease, Agreement, PurchaseOrder, WholesaleOrder, ProductionOrder,\n * Cart) and represents a single sale of rights for a fee. A\n * stock-media marketplace selling a one-time license, a music-\n * licensing platform issuing a sync license, a code-asset marketplace\n * granting a redistribution right — all use the same primitive.\n *\n * Immutability invariant: once a `LicenseSale` is saved with\n * {@link ContractStatus.ACCEPTED}, its rights snapshot becomes\n * frozen. Re-saving with mutated rights throws. The only legal\n * transition out of ACCEPTED is `revoke()` (which moves status to\n * CANCELLED without changing the rights). If a different snapshot\n * is genuinely needed, the operator's contract is to issue a *new*\n * LicenseSale row and let the old one stand as historical record.\n *\n * All subtype-specific fields are declared as `Meta<T>` so the\n * scanner routes them into the shared `_meta_data` JSON column on\n * the `contracts` STI table — no migration of the base schema needed\n * to land this subtype.\n */\n@TenantScoped({ mode: 'optional' })\n@smrt()\nexport class LicenseSale extends SalesContract { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n override contractType = ContractType.LICENSE_SALE;\n\n /**\n * Plain string reference to the upstream `Sku` (from\n * `@happyvertical/smrt-products`) that this license was sold against.\n * Cross-package id; never `@foreignKey()`.\n */\n skuId: Meta<string> = '';\n\n /**\n * Plain string reference to the `Payment` that satisfied the\n * purchase. Cross-model reference matches the rest of the\n * package's convention.\n */\n paymentId: Meta<string> = '';\n\n /**\n * The licensee's email address — the primary identity carried\n * forward into PDF generation and any downstream hash-registry\n * publication. Required; without it, downstream rights cannot be\n * attributed.\n */\n licenseeEmail: Meta<string> = '';\n\n /**\n * Optional legal-entity name of the licensee for purchases that\n * warrant it (typically high-tier B2B licenses). Empty string for\n * individual-buyer licenses where the email *is* the legal identity.\n */\n licenseeLegalEntity: Meta<string> = '';\n\n /**\n * Optional ISO-3166 jurisdiction code of the licensee, captured for\n * tax / disclosure / governing-law purposes. Empty when not\n * required.\n */\n licenseeJurisdiction: Meta<string> = '';\n\n // -------- Rights snapshot (immutable once issued) --------\n\n /** Permitted media — `web,print,social`, `broadcast`, etc. */\n rightsMedium: Meta<string> = '';\n\n /** Distribution scope — `worldwide`, `territorial`, `private`, etc. */\n rightsDistributionScope: Meta<string> = '';\n\n /** Exclusivity — `exclusive` / `non-exclusive` / `co-exclusive`. */\n rightsExclusivity: Meta<string> = '';\n\n /** Duration — `perpetual` / `12-months` / `event-only`, etc. */\n rightsDuration: Meta<string> = '';\n\n /** Territory — ISO-3166 list, `worldwide`, region name, etc. */\n rightsTerritory: Meta<string> = '';\n\n /** Whether the licensee may sublicense the rights. */\n rightsSublicensing: Meta<boolean> = false;\n\n /** Whether the licensee may create derivative works. */\n rightsDerivatives: Meta<boolean> = false;\n\n // -------- Artefacts --------\n\n /** URL of the signed PDF (typically a cloud-storage public URL). */\n pdfUrl: Meta<string> = '';\n\n /**\n * SHA-256 of the signed PDF bytes (hex string, no `0x` prefix).\n * Lets downstream consumers verify the PDF the licensee holds is\n * the one this row was issued for, without re-fetching.\n */\n pdfHash: Meta<string> = '';\n\n /**\n * Optional reference to an on-chain hash-registry entry — set when\n * the LicenseSale's `pdfHash` has been anchored on a public chain\n * for tamper-evidence. Format is whatever the hash-registry\n * service emits (e.g. `chain:tx-hash:log-index`). Empty for\n * deployments that don't publish hashes on-chain.\n */\n onChainHashRegistryRef: Meta<string> = '';\n\n constructor(options: LicenseSaleOptions = {}) {\n super(options);\n if (options.skuId !== undefined) this.skuId = options.skuId;\n if (options.paymentId !== undefined) this.paymentId = options.paymentId;\n if (options.licenseeEmail !== undefined)\n this.licenseeEmail = options.licenseeEmail;\n if (options.licenseeLegalEntity !== undefined)\n this.licenseeLegalEntity = options.licenseeLegalEntity;\n if (options.licenseeJurisdiction !== undefined)\n this.licenseeJurisdiction = options.licenseeJurisdiction;\n if (options.rightsMedium !== undefined)\n this.rightsMedium = options.rightsMedium;\n if (options.rightsDistributionScope !== undefined)\n this.rightsDistributionScope = options.rightsDistributionScope;\n if (options.rightsExclusivity !== undefined)\n this.rightsExclusivity = options.rightsExclusivity;\n if (options.rightsDuration !== undefined)\n this.rightsDuration = options.rightsDuration;\n if (options.rightsTerritory !== undefined)\n this.rightsTerritory = options.rightsTerritory;\n if (options.rightsSublicensing !== undefined)\n this.rightsSublicensing = options.rightsSublicensing;\n if (options.rightsDerivatives !== undefined)\n this.rightsDerivatives = options.rightsDerivatives;\n if (options.pdfUrl !== undefined) this.pdfUrl = options.pdfUrl;\n if (options.pdfHash !== undefined) this.pdfHash = options.pdfHash;\n if (options.onChainHashRegistryRef !== undefined)\n this.onChainHashRegistryRef = options.onChainHashRegistryRef;\n }\n\n /**\n * Once `super.initialize()` has applied the framework's option-\n * override pass, capture the rights snapshot if (and only if) the\n * row arrived already issued. That's how the immutability guard\n * tells \"freshly drafting a license\" (snapshot still mutable) apart\n * from \"loaded an already-issued row\" (snapshot frozen).\n */\n override async initialize(): Promise<this> {\n await super.initialize();\n if (this.status === ContractStatus.ACCEPTED && (await this.isSaved())) {\n issuedRightsSnapshot.set(this, this.serializeRightsSnapshot());\n }\n return this;\n }\n\n /**\n * Return the current rights snapshot as a typed object. Read-only\n * for callers — mutation should go through assignment to the\n * individual `rights*` fields, gated by the issued-immutability\n * guard.\n */\n getRightsSnapshot(): LicenseRightsSnapshot {\n return {\n medium: this.rightsMedium,\n distributionScope: this.rightsDistributionScope,\n exclusivity: this.rightsExclusivity,\n duration: this.rightsDuration,\n territory: this.rightsTerritory,\n sublicensing: this.rightsSublicensing,\n derivatives: this.rightsDerivatives,\n };\n }\n\n /**\n * Revoke an issued license — moves status to `CANCELLED` without\n * touching the rights snapshot (which stays frozen). Throws if\n * called on a non-issued license; a draft license should be\n * deleted, not revoked.\n */\n revoke(): void {\n if (this.status !== ContractStatus.ACCEPTED) {\n throw new Error(\n `LicenseSale ${this.id ?? '<new>'}: cannot revoke from status '${this.status}' — only ACCEPTED licenses can be revoked`,\n );\n }\n this.status = ContractStatus.CANCELLED;\n }\n\n /**\n * Save with the immutability check: if the row was loaded with\n * `ACCEPTED` status, the rights snapshot at save time must match\n * the snapshot at load time. Anything else means an out-of-band\n * mutation crept in and we surface it instead of letting the row\n * drift silently.\n *\n * Newly-drafted licenses (where the row was constructed without\n * `ACCEPTED` status) capture their snapshot at the moment the\n * caller transitions to `ACCEPTED` and saves — `validateImmutable`\n * picks that up post-save on the next call.\n */\n override async save(options?: SmrtSaveOptions): Promise<this> {\n this.validateImmutable();\n const result = (await super.save(options)) as this;\n // If this save transitioned the row to ACCEPTED, freeze the\n // snapshot for future saves.\n if (\n this.status === ContractStatus.ACCEPTED &&\n !issuedRightsSnapshot.has(this)\n ) {\n issuedRightsSnapshot.set(this, this.serializeRightsSnapshot());\n }\n return result;\n }\n\n /**\n * Compare the current rights against the captured snapshot\n * (populated either at load time by `initialize` or at first\n * issuance by `save`). Throws on mismatch.\n */\n private validateImmutable(): void {\n const captured = issuedRightsSnapshot.get(this);\n if (!captured) return;\n const current = this.serializeRightsSnapshot();\n if (captured !== current) {\n throw new Error(\n `LicenseSale ${this.id ?? '<new>'}: rights snapshot is immutable once issued (status=ACCEPTED). ` +\n 'Use revoke() to cancel the existing license and issue a new one with the updated rights.',\n );\n }\n }\n\n private serializeRightsSnapshot(): string {\n // Stable key ordering so a re-serialization after a no-op\n // assignment doesn't trip the equality check.\n return JSON.stringify({\n medium: this.rightsMedium,\n distributionScope: this.rightsDistributionScope,\n exclusivity: this.rightsExclusivity,\n duration: this.rightsDuration,\n territory: this.rightsTerritory,\n sublicensing: this.rightsSublicensing,\n derivatives: this.rightsDerivatives,\n });\n }\n}\n\nexport default Contract;\n","import { SmrtCollection } from '@happyvertical/smrt-core';\nimport { PurchaseContract } from '../models/Contract.js';\n\n/** Read supplier-side contracts across every purchase STI subtype. */\nexport class PurchaseContractCollection extends SmrtCollection<PurchaseContract> {\n static readonly _itemClass = PurchaseContract;\n\n async findByVendor(vendorId: string): Promise<PurchaseContract[]> {\n return this.list({\n where: { vendorId },\n orderBy: 'created_at DESC',\n });\n }\n}\n","import { SmrtCollection } from '@happyvertical/smrt-core';\nimport { SalesContract } from '../models/Contract.js';\n\n/** Read customer-side contracts across every sales STI subtype. */\nexport class SalesContractCollection extends SmrtCollection<SalesContract> {\n static readonly _itemClass = SalesContract;\n\n async findByCustomer(customerId: string): Promise<SalesContract[]> {\n return this.list({\n where: { customerId },\n orderBy: 'created_at DESC',\n });\n }\n}\n","/**\n * ContractCollection - Collection manager for Contract objects (STI)\n * @packageDocumentation\n */\n\nimport {\n SmrtCollection,\n type SmrtListOptions,\n type SmrtObject,\n type SmrtSelectedRow,\n type SmrtSelectField,\n type SmrtStiReadScope,\n type SmrtWhereClause,\n} from '@happyvertical/smrt-core';\nimport {\n getTenantId,\n queryGlobal,\n queryWithGlobals,\n} from '@happyvertical/smrt-tenancy';\nimport { DOCUMENT_MONEY_READ_REBIND } from '../document-money-internal.js';\nimport {\n Contract,\n isContractSnapshotReportEligible,\n} from '../models/Contract.js';\nimport { withTenantMoneyCollectionRead } from '../tenant-money-barrier.js';\nimport { ContractStatus, ContractType } from '../types/index.js';\nimport { assertCommerceMoneySchemaCurrent } from '../upgrade/detect.js';\nimport { PurchaseContractCollection } from './PurchaseContractCollection.js';\nimport { SalesContractCollection } from './SalesContractCollection.js';\n\nexport class ContractCollection extends SmrtCollection<Contract> {\n static readonly _itemClass = Contract;\n\n private async withMoneyRead<T>(operation: () => Promise<T>): Promise<T> {\n const rootDb = this.db;\n const tenantId = getTenantId();\n if (!tenantId) {\n // Without a tenant, still refuse a database with old money storage.\n await assertCommerceMoneySchemaCurrent(rootDb);\n return operation();\n }\n const result = await withTenantMoneyCollectionRead(\n rootDb,\n tenantId,\n (boundDb) => this.withDatabase(boundDb, operation),\n );\n if (result instanceof Contract)\n result.rebindMoneyRead(DOCUMENT_MONEY_READ_REBIND, rootDb);\n else if (Array.isArray(result))\n for (const item of result)\n if (item instanceof Contract)\n item.rebindMoneyRead(DOCUMENT_MONEY_READ_REBIND, rootDb);\n return result;\n }\n\n override async get(\n filter: string | SmrtWhereClause<Contract>,\n options: Parameters<SmrtCollection<Contract>['get']>[1] = {},\n ): Promise<Contract | null> {\n return this.withMoneyRead(() => super.get(filter, options));\n }\n\n override async list<\n const Select extends readonly SmrtSelectField<Contract>[],\n >(\n options: Omit<SmrtListOptions<Contract>, 'select' | 'stiScope'> & {\n select: Select;\n include?: never;\n stiScope: SmrtStiReadScope;\n },\n ): Promise<Record<string, unknown>[]>;\n override async list<\n const Select extends readonly SmrtSelectField<Contract>[],\n >(\n options: Omit<SmrtListOptions<Contract>, 'select' | 'stiScope'> & {\n select: Select;\n include?: never;\n stiScope?: undefined;\n },\n ): Promise<SmrtSelectedRow<Contract, Select>[]>;\n override async list(\n options: Omit<SmrtListOptions<Contract>, 'select' | 'stiScope'> & {\n select?: undefined;\n stiScope: SmrtStiReadScope;\n },\n ): Promise<SmrtObject[]>;\n override async list(options?: undefined): Promise<Contract[]>;\n override async list(\n options: Omit<SmrtListOptions<Contract>, 'select' | 'stiScope'> & {\n select?: undefined;\n stiScope?: undefined;\n },\n ): Promise<Contract[]>;\n override async list(\n options:\n | (Omit<SmrtListOptions<Contract>, 'select'> & { select?: undefined })\n | undefined,\n ): Promise<SmrtObject[]>;\n override async list(\n options: SmrtListOptions<Contract> | undefined,\n ): Promise<SmrtObject[] | Record<string, unknown>[]>;\n override async list(\n options: SmrtListOptions<Contract> = {},\n ): Promise<SmrtObject[] | Record<string, unknown>[]> {\n return this.withMoneyRead(() => super.list(options));\n }\n\n /**\n * Find contracts by customer\n *\n * @param customerId - Customer ID\n * @returns Array of contracts\n * @deprecated Use SalesContractCollection.findByCustomer.\n */\n async findByCustomer(customerId: string): Promise<Contract[]> {\n const sales = await SalesContractCollection.create({\n ...this.options,\n db: this.db,\n });\n return sales.findByCustomer(customerId);\n }\n\n /**\n * Find contracts by vendor\n *\n * @param vendorId - Vendor ID\n * @returns Array of contracts\n * @deprecated Use PurchaseContractCollection.findByVendor.\n */\n async findByVendor(vendorId: string): Promise<Contract[]> {\n const purchases = await PurchaseContractCollection.create({\n ...this.options,\n db: this.db,\n });\n return purchases.findByVendor(vendorId);\n }\n\n /**\n * Hydrate a bounded tenant/date report candidate set once, retaining exact\n * STI descendants so callers can classify custom purchase types safely.\n */\n async listSnapshotReportContracts(options: {\n tenantId: string;\n from: Date;\n to: Date;\n }): Promise<Contract[]> {\n const contracts = await this.list({\n where: {\n tenantId: options.tenantId,\n 'issueDate >=': options.from.toISOString(),\n 'issueDate <=': options.to.toISOString(),\n },\n orderBy: 'issueDate ASC',\n });\n return contracts.filter(isContractSnapshotReportEligible);\n }\n\n /**\n * Find contracts by type (STI)\n *\n * @param contractType - Contract type\n * @returns Array of contracts\n */\n async findByType(contractType: ContractType): Promise<Contract[]> {\n return await this.list({\n where: { contractType },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find contracts by status\n *\n * @param status - Contract status\n * @returns Array of contracts\n */\n async findByStatus(status: ContractStatus): Promise<Contract[]> {\n return await this.list({\n where: { status },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find all draft contracts\n *\n * @returns Array of draft contracts\n */\n async findDrafts(): Promise<Contract[]> {\n return await this.findByStatus(ContractStatus.DRAFT);\n }\n\n /**\n * Find all accepted contracts\n *\n * @returns Array of accepted contracts\n */\n async findAccepted(): Promise<Contract[]> {\n return await this.findByStatus(ContractStatus.ACCEPTED);\n }\n\n /**\n * Find overdue contracts\n *\n * @returns Array of overdue contracts\n */\n async findOverdue(): Promise<Contract[]> {\n const now = new Date().toISOString();\n // Get all contracts past due date that aren't completed or cancelled\n const all = await this.list({\n where: {\n 'dueDate <': now,\n },\n orderBy: 'dueDate ASC',\n });\n return all.filter(\n (c) =>\n c.status !== ContractStatus.COMPLETED &&\n c.status !== ContractStatus.CANCELLED,\n );\n }\n\n /**\n * Find expired estimates/quotes\n *\n * @returns Array of expired estimates\n */\n async findExpired(): Promise<Contract[]> {\n const now = new Date().toISOString();\n // Get estimates past expiry date that aren't accepted, declined, or cancelled\n const estimates = await this.list({\n where: {\n contractType: ContractType.ESTIMATE,\n 'expiryDate <': now,\n },\n orderBy: 'expiryDate ASC',\n });\n return estimates.filter(\n (c) =>\n c.status !== ContractStatus.ACCEPTED &&\n c.status !== ContractStatus.DECLINED &&\n c.status !== ContractStatus.CANCELLED,\n );\n }\n\n /**\n * Find contracts in date range\n *\n * @param startDate - Start date\n * @param endDate - End date\n * @returns Array of contracts\n */\n async findByDateRange(startDate: Date, endDate: Date): Promise<Contract[]> {\n return await this.list({\n where: {\n 'issueDate >=': startDate.toISOString(),\n 'issueDate <=': endDate.toISOString(),\n },\n orderBy: 'issueDate DESC',\n });\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all contracts belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of contracts for the tenant\n */\n async findByTenant(tenantId: string): Promise<Contract[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global contracts (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global contracts\n */\n async findGlobal(): Promise<Contract[]> {\n return queryGlobal<Contract>(this);\n }\n\n /**\n * Find contracts for a tenant including global contracts.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global contracts\n */\n async findWithGlobals(tenantId: string): Promise<Contract[]> {\n return queryWithGlobals<Contract>(\n this,\n tenantId,\n 'Contract.findWithGlobals',\n );\n }\n}\n","/** Approved contract billing capacity; construction authorization belongs to the host. */\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { ContractCollection } from './collections/ContractCollection.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport {\n DOCUMENT_MONEY_WRITE,\n lockDocumentMoneyParents,\n} from './document-money-internal.js';\nimport { Agreement, type Contract, Order } from './models/Contract.js';\nimport type { Invoice } from './models/Invoice.js';\nimport { MoneyValidationError } from './money.js';\nimport { withTenantMoneyRuntime } from './tenant-money-barrier.js';\nimport { ContractStatus, InvoiceStatus } from './types/index.js';\n\n/** Exact billing entitlement in the schedule's explicit net or gross basis. */\nexport interface ContractBillingMilestone {\n key: string;\n label: string;\n amountMinor: number;\n}\n/** Immutable approved revision; ceiling is derived from original money plus amendments. */\nexport interface ContractBillingScheduleRevision {\n revision: number;\n requestId: string;\n approvalEvidenceRef: string;\n taxPolicyEvidenceRef: string;\n currency: string;\n basis: 'net' | 'gross';\n amendmentMinor: number;\n approvedTotalMinor: number;\n milestones: ContractBillingMilestone[];\n}\n/** Exact invoice amounts the host approved, independently of a net-only billing ceiling. */\nexport interface ContractMilestoneInvoiceMoney {\n netMinor: number;\n taxMinor: number;\n grossMinor: number;\n}\n/** Immutable issued claim; settlement, cancellation and credits never implicitly reopen it. */\nexport interface ContractMilestoneClaim {\n requestId: string;\n revision: number;\n milestoneKey: string;\n invoiceId: string;\n moneySnapshotId: string;\n approvedMoney: ContractMilestoneInvoiceMoney;\n invoiceApprovalEvidenceRef: string;\n}\n/** Persisted append-only schedule history and billing claims. */\nexport interface ContractBillingSchedule {\n version: 1;\n originalAmountMinor: number;\n capacityPolicy: 'issued-consumed';\n revisions: ContractBillingScheduleRevision[];\n claims: ContractMilestoneClaim[];\n}\ninterface ScheduleCommand {\n db: DatabaseInterface;\n tenantId: string;\n contractId: string;\n requestId: string;\n}\n/** The host must authorize approval and retain the referenced policy/evidence. */\nexport interface ApproveContractBillingScheduleInput extends ScheduleCommand {\n expectedRevision: number;\n approvalEvidenceRef: string;\n taxPolicyEvidenceRef: string;\n currency: string;\n basis: 'net' | 'gross';\n /** Zero for the original schedule; explicit signed approved value change thereafter. */\n amendmentMinor: number;\n milestones: ContractBillingMilestone[];\n}\n/** Issue an existing prepared draft under exact host-approved money and capacity. */\nexport interface IssueContractMilestoneInvoiceInput extends ScheduleCommand {\n revision: number;\n milestoneKey: string;\n invoiceId: string;\n approvedMoney: ContractMilestoneInvoiceMoney;\n invoiceApprovalEvidenceRef: string;\n}\nfunction invalid(message: string): never {\n throw new MoneyValidationError('INVALID_MONEY_INPUT', message);\n}\nfunction text(value: unknown, label: string): string {\n if (typeof value !== 'string' || !value || value.trim() !== value)\n invalid(`${label} must be nonempty trimmed text`);\n return value;\n}\nfunction integer(value: unknown, label: string, signed = false): number {\n if (\n typeof value !== 'number' ||\n !Number.isSafeInteger(value) ||\n (!signed && value < 0)\n )\n invalid(`${label} must be a ${signed ? '' : 'nonnegative '}safe integer`);\n return value;\n}\nfunction safe(value: bigint): number {\n const result = Number(value);\n return integer(result, 'Computed amount');\n}\nfunction context(input: ScheduleCommand): void {\n text(input.tenantId, 'Tenant');\n text(input.contractId, 'Contract');\n text(input.requestId, 'Request');\n if (getTenantId() !== input.tenantId)\n invalid('Schedule command requires its active tenant context');\n}\nfunction milestones(\n value: ContractBillingMilestone[],\n): ContractBillingMilestone[] {\n if (!Array.isArray(value) || value.length === 0)\n invalid('A schedule requires milestones');\n const keys = new Set<string>();\n return value.map((entry) => {\n if (!entry || typeof entry !== 'object') invalid('Invalid milestone');\n const key = text(entry.key, 'Milestone key');\n if (keys.has(key)) invalid('Milestone keys must be unique');\n keys.add(key);\n return {\n key,\n label: text(entry.label, 'Milestone label'),\n amountMinor: integer(entry.amountMinor, 'Milestone amount'),\n };\n });\n}\nfunction money(\n value: ContractMilestoneInvoiceMoney,\n): ContractMilestoneInvoiceMoney {\n if (!value || typeof value !== 'object')\n invalid('Approved invoice money is required');\n const netMinor = integer(value.netMinor, 'Approved net');\n const taxMinor = integer(value.taxMinor, 'Approved tax');\n const grossMinor = integer(value.grossMinor, 'Approved gross');\n if (BigInt(netMinor) + BigInt(taxMinor) !== BigInt(grossMinor))\n invalid('Approved invoice money must reconcile');\n return { netMinor, taxMinor, grossMinor };\n}\n/** Allocate integer relative weights with the exact rounding remainder on the final milestone. */\nexport function allocateContractMilestones(\n totalMinor: number,\n weights: readonly number[],\n): number[] {\n integer(totalMinor, 'Total');\n if (!Array.isArray(weights) || !weights.length)\n invalid('Milestone weights are required');\n const values = weights.map((value) => {\n integer(value, 'Weight');\n if (!value) invalid('Weights must be positive');\n return BigInt(value);\n });\n const sum = values.reduce((a, b) => a + b, 0n);\n let remaining = BigInt(totalMinor);\n return values.map((value, index) => {\n const amount =\n index === values.length - 1\n ? remaining\n : (BigInt(totalMinor) * value) / sum;\n remaining -= amount;\n return safe(amount);\n });\n}\n/** Read immutable schedule evidence; a detached result cannot mutate its Contract. */\nexport function getContractBillingSchedule(\n contract: Contract,\n): ContractBillingSchedule | null {\n if (!contract.billingScheduleJson) return null;\n let parsed: ContractBillingSchedule;\n try {\n parsed = JSON.parse(contract.billingScheduleJson);\n } catch {\n invalid('Stored billing schedule is invalid');\n }\n if (\n parsed.version !== 1 ||\n parsed.capacityPolicy !== 'issued-consumed' ||\n !Array.isArray(parsed.revisions) ||\n !parsed.revisions.length ||\n !Array.isArray(parsed.claims)\n )\n invalid('Stored billing schedule is invalid');\n return parsed;\n}\nasync function loadContract(\n db: DatabaseInterface,\n tenantId: string,\n contractId: string,\n): Promise<Order | Agreement> {\n const collection = await ContractCollection.create({ db });\n await lockDocumentMoneyParents([{ id: contractId, tenantId, collection }]);\n const contract = await collection.get({ id: contractId }, { cache: false });\n if (\n !(contract instanceof Order || contract instanceof Agreement) ||\n contract.tenantId !== tenantId ||\n !contract.customerId ||\n contract.status !== ContractStatus.ACCEPTED ||\n !contract.moneySnapshotId\n )\n invalid(\n 'Schedule requires an accepted customer contract in the active tenant',\n );\n return contract;\n}\n/** Append an approved schedule; original frozen Contract money remains authoritative and unchanged. */\nexport async function approveContractBillingSchedule(\n input: ApproveContractBillingScheduleInput,\n): Promise<ContractBillingScheduleRevision> {\n context(input);\n const expectedRevision = integer(input.expectedRevision, 'Expected revision');\n const proposal = {\n requestId: input.requestId,\n approvalEvidenceRef: text(input.approvalEvidenceRef, 'Approval evidence'),\n taxPolicyEvidenceRef: text(\n input.taxPolicyEvidenceRef,\n 'Tax policy evidence',\n ),\n currency: text(input.currency, 'Currency'),\n basis: input.basis,\n amendmentMinor: integer(input.amendmentMinor, 'Amendment', true),\n milestones: milestones(input.milestones),\n };\n if (proposal.basis !== 'net' && proposal.basis !== 'gross')\n invalid('Schedule basis must explicitly be net or gross');\n return withTenantMoneyRuntime(input.db, input.tenantId, async ({ db }) => {\n const contract = await loadContract(db, input.tenantId, input.contractId);\n const stored = getContractBillingSchedule(contract);\n const replay = stored?.revisions.find(\n (entry) => entry.requestId === input.requestId,\n );\n if (replay) {\n const { revision, approvedTotalMinor: _total, ...old } = replay;\n if (\n revision !== expectedRevision + 1 ||\n JSON.stringify(old) !== JSON.stringify(proposal)\n )\n invalid('Schedule request changed');\n return replay;\n }\n if (stored?.claims.some((entry) => entry.requestId === input.requestId))\n invalid('Request identity already used');\n const previous = stored?.revisions.at(-1);\n if ((previous?.revision ?? 0) !== expectedRevision)\n invalid('Schedule revision changed');\n if (\n contract.currency !== proposal.currency ||\n (previous &&\n (previous.currency !== proposal.currency ||\n previous.basis !== proposal.basis))\n )\n invalid('Schedule currency and basis must match original approval');\n const originalAmountMinor =\n stored?.originalAmountMinor ??\n (proposal.basis === 'net' ? contract.subtotal : contract.totalAmount);\n integer(originalAmountMinor, 'Original contract amount');\n if (!stored) {\n if (proposal.amendmentMinor !== 0)\n invalid('Initial schedule cannot amend original contract value');\n const invoices = await (\n await InvoiceCollection.create({ db })\n ).findByContract(input.contractId);\n if (\n invoices.some(\n (invoice) => invoice.kind === 'invoice' && !!invoice.moneySnapshotId,\n )\n )\n invalid(\n 'Existing issued contract invoices prevent initial schedule approval',\n );\n }\n const approvedTotalMinor = safe(\n BigInt(previous?.approvedTotalMinor ?? originalAmountMinor) +\n BigInt(proposal.amendmentMinor),\n );\n if (\n proposal.milestones.reduce(\n (sum, entry) => sum + BigInt(entry.amountMinor),\n 0n,\n ) !== BigInt(approvedTotalMinor)\n )\n invalid(\n 'Milestone total must equal original contract plus approved amendments',\n );\n for (const claim of stored?.claims ?? []) {\n const prior = stored?.revisions\n .find((entry) => entry.revision === claim.revision)\n ?.milestones.find((entry) => entry.key === claim.milestoneKey);\n const next = proposal.milestones.find(\n (entry) => entry.key === claim.milestoneKey,\n );\n if (!prior || !next || JSON.stringify(prior) !== JSON.stringify(next))\n invalid('An issued milestone cannot be removed or changed');\n }\n const revision: ContractBillingScheduleRevision = {\n revision: expectedRevision + 1,\n ...proposal,\n approvedTotalMinor,\n };\n const state: ContractBillingSchedule = stored ?? {\n version: 1,\n originalAmountMinor,\n capacityPolicy: 'issued-consumed',\n revisions: [],\n claims: [],\n };\n state.revisions.push(revision);\n contract.billingScheduleJson = JSON.stringify(state);\n await contract.saveDerived(DOCUMENT_MONEY_WRITE);\n return revision;\n });\n}\n\n// An executor-bound, one-invoice capability. Never exported from the package.\nconst issuePermissions = new WeakMap<\n object,\n Map<\n string,\n {\n contractId: string;\n amountMinor: number;\n basis: 'net' | 'gross';\n approvedMoney: ContractMilestoneInvoiceMoney;\n }\n >\n>();\n/** @internal Called by the ordinary money aggregate before any new invoice freezes. */\nexport async function assertContractScheduleInvoiceFreeze(\n db: DatabaseInterface,\n invoice: Invoice,\n): Promise<void> {\n if (invoice.kind !== 'invoice' || !invoice.contractId) return;\n const contract = await (await ContractCollection.create({ db })).get(\n { id: invoice.contractId },\n { cache: false },\n );\n if (!contract) return;\n if (contract.tenantId !== invoice.tenantId)\n invalid('Invoice contract must belong to its active tenant');\n const state = getContractBillingSchedule(contract);\n if (!state) return;\n // Abandoning an unissued draft creates no billing entitlement.\n if (\n invoice.status === InvoiceStatus.CANCELLED &&\n !invoice.moneySnapshotId &&\n !invoice.arJournalId\n )\n return;\n if (!(contract instanceof Order || contract instanceof Agreement))\n invalid('Scheduled contract must be a customer contract');\n const permission = invoice.id\n ? issuePermissions.get(db as object)?.get(invoice.id)\n : undefined;\n if (!permission || permission.contractId !== invoice.contractId)\n invalid(\n 'Scheduled contract invoices must use issueContractMilestoneInvoice',\n );\n if (\n invoice.customerId !== contract.customerId ||\n invoice.currency !== contract.currency\n )\n invalid('Milestone invoice must match contract customer and currency');\n const actual = {\n netMinor: invoice.subtotal,\n taxMinor: invoice.taxAmount,\n grossMinor: invoice.totalAmount,\n };\n if (\n JSON.stringify(actual) !== JSON.stringify(permission.approvedMoney) ||\n (permission.basis === 'net' ? invoice.subtotal : invoice.totalAmount) !==\n permission.amountMinor\n )\n invalid('Milestone invoice money differs from approved amounts');\n}\n/** Atomically issue/freeze one prepared draft and consume exactly one approved milestone. */\nexport async function issueContractMilestoneInvoice(\n input: IssueContractMilestoneInvoiceInput,\n): Promise<ContractMilestoneClaim> {\n context(input);\n const request = {\n requestId: input.requestId,\n revision: integer(input.revision, 'Revision'),\n milestoneKey: text(input.milestoneKey, 'Milestone key'),\n invoiceId: text(input.invoiceId, 'Invoice'),\n approvedMoney: money(input.approvedMoney),\n invoiceApprovalEvidenceRef: text(\n input.invoiceApprovalEvidenceRef,\n 'Invoice approval evidence',\n ),\n };\n return withTenantMoneyRuntime(input.db, input.tenantId, async ({ db }) => {\n const contract = await loadContract(db, input.tenantId, input.contractId);\n const state = getContractBillingSchedule(contract);\n if (!state) invalid('Contract has no approved billing schedule');\n const replay = state.claims.find(\n (entry) => entry.requestId === request.requestId,\n );\n if (replay) {\n const { moneySnapshotId: _snapshot, ...prior } = replay;\n if (JSON.stringify(prior) !== JSON.stringify(request))\n invalid('Milestone issue request changed');\n return replay;\n }\n if (state.revisions.some((entry) => entry.requestId === request.requestId))\n invalid('Request identity already used');\n const revision = state.revisions.at(-1);\n if (!revision || revision.revision !== request.revision)\n invalid('Schedule revision changed');\n const milestone = revision.milestones.find(\n (entry) => entry.key === request.milestoneKey,\n );\n if (!milestone) invalid('Milestone does not exist in approved revision');\n if (\n state.claims.some(\n (entry) =>\n entry.milestoneKey === request.milestoneKey ||\n entry.invoiceId === request.invoiceId,\n )\n )\n invalid('Milestone or invoice already consumed');\n const invoices = await InvoiceCollection.create({ db });\n await lockDocumentMoneyParents([\n { id: request.invoiceId, tenantId: input.tenantId, collection: invoices },\n ]);\n const invoice = await invoices.get(\n { id: request.invoiceId },\n { cache: false },\n );\n if (\n !invoice ||\n invoice.tenantId !== input.tenantId ||\n invoice.kind !== 'invoice' ||\n invoice.status !== InvoiceStatus.DRAFT ||\n invoice.moneySnapshotId ||\n invoice.contractId !== input.contractId ||\n invoice.customerId !== contract.customerId ||\n invoice.currency !== revision.currency\n )\n invalid(\n 'Milestone requires a matching unissued draft in the active tenant',\n );\n const permissions = issuePermissions.get(db as object) ?? new Map();\n permissions.set(request.invoiceId, {\n contractId: input.contractId,\n amountMinor: milestone.amountMinor,\n basis: revision.basis,\n approvedMoney: request.approvedMoney,\n });\n issuePermissions.set(db as object, permissions);\n try {\n invoice.markSent();\n await invoice.save();\n } finally {\n permissions.delete(request.invoiceId);\n if (!permissions.size) issuePermissions.delete(db as object);\n }\n if (!invoice.moneySnapshotId) invalid('Milestone invoice was not frozen');\n const claim: ContractMilestoneClaim = {\n ...request,\n moneySnapshotId: invoice.moneySnapshotId,\n };\n state.claims.push(claim);\n contract.billingScheduleJson = JSON.stringify(state);\n await contract.saveDerived(DOCUMENT_MONEY_WRITE);\n return claim;\n });\n}\n","/** Generic invoice binding to an owning package's existing reservation. No money capacity lives here. */\nimport { ObjectRegistry } from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { InvoiceLineItemCollection } from './collections/InvoiceLineItemCollection.js';\nimport type { Invoice } from './models/Invoice.js';\nimport type { InvoiceLineItem } from './models/InvoiceLineItem.js';\nimport { MoneyValidationError } from './money.js';\n\n/** A trusted package registers one protected source namespace and verifies its own authoritative reservation. */\nexport type InvoiceSourceBindingHandler = (input: {\n executor: DatabaseInterface;\n invoice: Invoice;\n line: InvoiceLineItem;\n reservationId: string;\n phase: 'bind' | 'release' | 'validate';\n}) => Promise<void>;\nconst handlers = new Map<string, InvoiceSourceBindingHandler>();\nconst writes = new WeakMap<InvoiceLineItem, string | null>();\nfunction invalid(message: string): never {\n throw new MoneyValidationError('FROZEN_DOCUMENT', message);\n}\n/** Register from trusted package code at import time, never from request data. Protected names are qualified. */\nexport function registerInvoiceSourceBinding(\n sourceType: string,\n handler: InvoiceSourceBindingHandler,\n): void {\n if (\n !/^billable:@[a-z0-9-]+\\/[a-z0-9-]+:[A-Za-z][A-Za-z0-9]*$/.test(sourceType)\n )\n invalid('Billable source type must be package qualified');\n if (handlers.has(sourceType) && handlers.get(sourceType) !== handler)\n invalid('Billable source handler already registered');\n handlers.set(sourceType, handler);\n}\nasync function handlerFor(type: string): Promise<InvoiceSourceBindingHandler> {\n if (!handlers.has(type) && type.startsWith('billable:'))\n await ObjectRegistry.tryLoadFromExternalPackage(type.slice(9));\n const handler = handlers.get(type);\n if (!handler) invalid('Protected invoice source handler is unavailable');\n return handler;\n}\n/** Enforce old and proposed identity; clearing the namespace never clears a persisted binding. */\nexport async function assertInvoiceLineSource(\n executor: DatabaseInterface,\n invoice: Invoice,\n line: InvoiceLineItem,\n persisted: InvoiceLineItem | null,\n issuing = false,\n): Promise<void> {\n const old = persisted?.sourceReservationId ?? null;\n const next = line.sourceReservationId ?? null;\n const authorized = writes.has(line) && writes.get(line) === next;\n if (old !== next && !authorized)\n invalid('Invoice source reservation is command-owned');\n if (\n old &&\n (line.sourceType !== persisted?.sourceType ||\n line.sourceId !== persisted?.sourceId)\n )\n invalid('Bound invoice source identity cannot change');\n if (old && line.sourceType !== persisted?.sourceType)\n invalid('Bound source namespace cannot change');\n if (line.sourceType.startsWith('billable:') || old || next) {\n const handler = await handlerFor(line.sourceType);\n if (issuing && !next)\n invalid('Protected invoice source must be reserved before issue');\n if (next)\n await handler({\n executor,\n invoice,\n line,\n reservationId: next,\n phase: authorized ? 'bind' : 'validate',\n });\n }\n}\n/** Guard physical line or cascading invoice deletion independently of caller-mutated fields. */\nexport function assertInvoiceSourceDelete(\n lines: readonly InvoiceLineItem[],\n): void {\n if (lines.some((line) => line.sourceReservationId))\n invalid('Release invoice source reservations before deletion');\n}\n/** Attach an already-authorized owning-package reservation. It must identify exactly this tenant/invoice/line. */\nexport async function bindInvoiceLineSource(input: {\n db: DatabaseInterface;\n tenantId: string;\n lineId: string;\n reservationId: string;\n}): Promise<void> {\n if (\n !input.tenantId ||\n getTenantId() !== input.tenantId ||\n !input.reservationId\n )\n invalid('Active tenant and reservation required');\n const line = await (\n await InvoiceLineItemCollection.create({ db: input.db })\n ).get({ id: input.lineId, tenantId: input.tenantId }, { cache: false });\n if (!line || line.tenantId !== input.tenantId)\n invalid('Owned invoice line required');\n await line.withTransaction(async (bound) => {\n const invoice = await (\n await InvoiceCollection.create({ db: bound.db })\n ).get({ id: bound.invoiceId, tenantId: input.tenantId }, { cache: false });\n if (!invoice || invoice.status !== 'draft')\n invalid('Unissued owned invoice required');\n if (\n bound.sourceReservationId &&\n bound.sourceReservationId !== input.reservationId\n )\n invalid('Invoice line already bound');\n await (await handlerFor(bound.sourceType))({\n executor: bound.db,\n invoice,\n line: bound,\n reservationId: input.reservationId,\n phase: 'bind',\n });\n bound.sourceReservationId = input.reservationId;\n writes.set(bound, input.reservationId);\n try {\n await bound.save();\n } finally {\n writes.delete(bound);\n }\n });\n}\n/** Withdraw only after the owning package released its draft claim, in the same caller transaction. */\nexport async function unbindInvoiceLineSource(input: {\n db: DatabaseInterface;\n tenantId: string;\n lineId: string;\n reservationId: string;\n}): Promise<void> {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n invalid('Active tenant required');\n const line = await (\n await InvoiceLineItemCollection.create({ db: input.db })\n ).get({ id: input.lineId, tenantId: input.tenantId }, { cache: false });\n if (!line || line.sourceReservationId !== input.reservationId)\n invalid('Owned bound invoice line required');\n await line.withTransaction(async (bound) => {\n const invoice = await (\n await InvoiceCollection.create({ db: bound.db })\n ).get({ id: bound.invoiceId, tenantId: input.tenantId }, { cache: false });\n if (!invoice || invoice.status !== 'draft')\n invalid('Issued invoice source cannot be unbound');\n await (await handlerFor(bound.sourceType))({\n executor: bound.db,\n invoice,\n line: bound,\n reservationId: input.reservationId,\n phase: 'release',\n });\n bound.sourceReservationId = null;\n writes.set(bound, null);\n try {\n await bound.save();\n } finally {\n writes.delete(bound);\n }\n });\n}\n","import { ObjectRegistry } from '@happyvertical/smrt-core';\nimport { assertContractScheduleInvoiceFreeze } from './contract-billing-schedule.js';\nimport {\n getHistoricalSalesMoney,\n readHistoricalSalesLines,\n} from './historical-sales-money.js';\nimport type { HistoricalSalesMoneySnapshot } from './historical-sales-types.js';\nimport {\n assertInvoiceLineSource,\n assertInvoiceSourceDelete,\n} from './invoice-source-binding.js';\n/** Transactional Invoice money aggregate persistence. */\n\nimport type { SmrtSaveOptions } from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { CustomerCollection } from './collections/CustomerCollection.js';\nimport {\n DocumentTaxCollection,\n type DocumentTaxWriteInput,\n} from './collections/DocumentTaxCollection.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { InvoiceLineItemCollection } from './collections/InvoiceLineItemCollection.js';\nimport { TaxCodeCollection } from './collections/TaxCodeCollection.js';\nimport { TaxComponentCollection } from './collections/TaxComponentCollection.js';\nimport {\n allocateFrozenCredit,\n type FrozenCreditAllocation,\n type IssuedCreditConsumption,\n} from './credit-allocation.js';\nimport {\n DOCUMENT_MONEY_WRITE,\n lockDocumentMoneyParents,\n withDocumentMoneyObjectTransaction,\n withDocumentMoneyTransaction,\n} from './document-money-internal.js';\nimport type { DocumentTax } from './models/DocumentTax.js';\nimport type { Invoice } from './models/Invoice.js';\nimport { InvoiceLineItem } from './models/InvoiceLineItem.js';\nimport type { TaxSettings } from './models/TaxSettings.js';\nimport {\n calculateMoneyDocument,\n type MoneyDiscountInput,\n MoneyValidationError,\n type ResolvableTaxCodeInput,\n resolveLineTaxCode,\n} from './money.js';\nimport {\n lockTenantMoneyRuntime,\n withTenantMoneyRuntime,\n} from './tenant-money-barrier.js';\nimport type { InvoiceTaxComponentSnapshot } from './types/index.js';\nimport { InvoiceStatus } from './types/index.js';\n\ntype ProductTaxDefaults = {\n tenantId: string | null;\n defaultSalesTaxCodeId: string;\n};\n\ntype DeleteOptions = { expectedUpdatedAt?: Date | string };\n\nexport interface PreviewInvoiceMoneyInput {\n db: DatabaseInterface;\n tenantId: string;\n invoiceId: string;\n draft?: {\n header?: Partial<\n Pick<\n Invoice,\n | 'currency'\n | 'lineAmountType'\n | 'taxRoundingScope'\n | 'providerTaxAmount'\n | 'customerId'\n >\n >;\n lines?: Array<\n Partial<\n Pick<\n InvoiceLineItem,\n | 'id'\n | 'description'\n | 'productId'\n | 'quantity'\n | 'unitPrice'\n | 'discountType'\n | 'discountRate'\n | 'discount'\n | 'taxMode'\n | 'taxCodeId'\n >\n >\n >;\n };\n}\n\nexport interface PreviewInvoiceMoneyResult {\n source: 'calculated';\n header: {\n currency: string;\n lineAmountType: Invoice['lineAmountType'];\n taxRoundingScope: Invoice['taxRoundingScope'];\n subtotalMinor: number;\n discountTotalMinor: number;\n taxAmountMinor: number;\n providerTaxAmountMinor: number;\n totalAmountMinor: number;\n };\n lines: Array<{\n id: string;\n grossMinor: number;\n discountMinor: number;\n amountMinor: number;\n taxMinor: number;\n taxCodeId: string | null;\n taxCodeCode: string | null;\n taxCodeName: string | null;\n taxComponents: InvoiceLineItem['taxComponents'];\n }>;\n taxes: DocumentTaxWriteInput[];\n}\n\nexport interface GetInvoiceMoneyFactsInput {\n db: DatabaseInterface;\n tenantId: string;\n invoiceId: string;\n}\n\nexport interface InvoiceMoneyFacts {\n historical?: {\n snapshot: HistoricalSalesMoneySnapshot;\n lines: Array<{ sourceKey: string; lineId: string }>;\n };\n source: 'stored';\n payable?: ReturnType<Invoice['getPayableFacts']>;\n invoiceId: string;\n moneySnapshotId: string;\n kind: Invoice['kind'];\n status: Invoice['status'];\n header: PreviewInvoiceMoneyResult['header'];\n lines: PreviewInvoiceMoneyResult['lines'];\n taxes: DocumentTaxWriteInput[];\n}\n\nconst FROZEN_INVOICE_FIELDS = [\n 'moneyAuthority',\n 'historicalSalesMoneyJson',\n 'historicalSourceKeyHash',\n 'kind',\n 'creditedInvoiceId',\n 'tenantId',\n 'customerId',\n 'contractId',\n 'invoiceNumber',\n 'reference',\n 'issueDate',\n 'dueDate',\n 'subtotal',\n 'discountTotal',\n 'taxAmount',\n 'providerTaxAmount',\n 'totalAmount',\n 'currency',\n 'lineAmountType',\n 'taxRoundingScope',\n 'sellerTaxRegistrationsJson',\n 'partyTaxId',\n 'moneySnapshotId',\n 'notes',\n 'customerNotes',\n 'terms',\n] as const;\n\nconst FROZEN_LINE_FIELDS = [\n 'tenantId',\n 'invoiceId',\n 'creditedLineId',\n 'creditCalculationBasisJson',\n 'description',\n 'sku',\n 'productId',\n 'quantity',\n 'unitPrice',\n 'discountType',\n 'discountRate',\n 'discount',\n 'taxMode',\n 'taxCodeId',\n 'taxCodeCode',\n 'taxCodeName',\n 'taxComponentsJson',\n 'taxAmount',\n 'taxRate',\n 'amount',\n 'sourceType',\n 'sourceId',\n 'sourceReservationId',\n 'periodStart',\n 'periodEnd',\n 'revenueAccountId',\n 'sortOrder',\n 'moneySnapshotId',\n] as const;\n\n/** Frozen-field identity: an absent optional reference (`''`) equals a stored NULL. */\nfunction comparable(value: unknown): unknown {\n if (value instanceof Date) return value.toISOString();\n return value === '' || value === undefined ? null : value;\n}\n\nfunction assertFieldsUnchanged<T extends object>(\n proposed: T,\n persisted: T,\n fields: readonly (keyof T)[],\n label: string,\n): void {\n for (const field of fields) {\n if (comparable(proposed[field]) !== comparable(persisted[field])) {\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n `${label} ${String(field)} is frozen`,\n );\n }\n }\n}\n\nfunction assertTenantId(value: string | null): asserts value is string {\n if (!value || value.trim() !== value)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice money requires a tenant-owned invoice',\n );\n}\n\nfunction requireId(value: string | null | undefined, label: string): string {\n if (!value)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `${label} requires a persisted ID`,\n );\n return value;\n}\n\n/** Lock the tenant's settings row; every money document belongs to a tenant. */\nasync function lockRuntimeSettings(\n db: DatabaseInterface,\n tenantId: string | null,\n): Promise<TaxSettings> {\n if (!tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Commerce money documents require a tenant',\n );\n return lockTenantMoneyRuntime(db, tenantId);\n}\n\nfunction isFrozen(invoice: Invoice): boolean {\n return (\n invoice.status !== InvoiceStatus.DRAFT ||\n !!invoice.arJournalId ||\n !!invoice.moneySnapshotId\n );\n}\n\nfunction aggregateSignature(invoice: Invoice): string {\n return JSON.stringify([\n invoice.lineAmountType,\n invoice.taxRoundingScope,\n invoice.subtotal,\n invoice.discountTotal,\n invoice.taxAmount,\n invoice.totalAmount,\n ]);\n}\n\nasync function loadInvoice(\n db: DatabaseInterface,\n id: string,\n): Promise<{ collection: InvoiceCollection; invoice: Invoice }> {\n const collection = await InvoiceCollection.create({ db });\n const invoice = await collection.get({ id }, { cache: false });\n if (!invoice)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Invoice ${id} was not found`,\n );\n return { collection, invoice };\n}\n\nasync function findInvoice(\n db: DatabaseInterface,\n id: string,\n): Promise<Invoice | null> {\n const collection = await InvoiceCollection.create({ db });\n return collection.get({ id }, { cache: false });\n}\n\n/** @internal Acquire the invoice money lock and reload persisted authority. */\nexport async function lockAndReloadInvoice(\n db: DatabaseInterface,\n invoiceId: string,\n tenantId: string,\n): Promise<{ collection: InvoiceCollection; invoice: Invoice }> {\n const first = await loadInvoice(db, invoiceId);\n await lockDocumentMoneyParents([\n { id: invoiceId, tenantId, collection: first.collection },\n ]);\n const invoice = await first.collection.get(\n { id: invoiceId },\n { cache: false },\n );\n if (!invoice || invoice.tenantId !== tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice tenant identity changed while acquiring its money lock',\n );\n return { collection: first.collection, invoice };\n}\n\nasync function lockAndReloadMoneyInvoice(\n db: DatabaseInterface,\n invoiceId: string,\n tenantId: string,\n creditedInvoiceId: string | null,\n): Promise<{\n collection: InvoiceCollection;\n invoice: Invoice;\n source: Invoice | null;\n}> {\n const collection = await InvoiceCollection.create({ db });\n const ids = [invoiceId, ...(creditedInvoiceId ? [creditedInvoiceId] : [])];\n const loaded = await Promise.all(\n ids.map((id) => collection.get({ id }, { cache: false })),\n );\n if (loaded.some((row) => !row || row.tenantId !== tenantId))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice money parents must exist in the active tenant',\n );\n await lockDocumentMoneyParents(\n ids.map((id) => ({ id, tenantId, collection })),\n );\n const invoice = await collection.get({ id: invoiceId }, { cache: false });\n const source = creditedInvoiceId\n ? await collection.get({ id: creditedInvoiceId }, { cache: false })\n : null;\n if (\n !invoice ||\n invoice.tenantId !== tenantId ||\n (source && source.tenantId !== tenantId)\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice money parent identity changed while acquiring its lock',\n );\n return { collection, invoice, source };\n}\n\nfunction discountInput(line: InvoiceLineItem): MoneyDiscountInput {\n if (line.discountType === 'percent')\n return { kind: 'percent', rate: String(line.discountRate) };\n if (line.discountType === 'amount' || line.discount !== 0)\n return { kind: 'amount', minor: line.discount };\n if (line.discountType !== 'none')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Unknown discount type: ${line.discountType}`,\n );\n return { kind: 'none' };\n}\n\ntype CreditPreparation = {\n allocationByLineId: Map<string, FrozenCreditAllocation>;\n sourceByLineId: Map<string, InvoiceLineItem>;\n issuedLines: InvoiceLineItem[];\n issuedParentById: Map<string, Invoice>;\n sourceInvoice: Invoice;\n sourceTaxes: DocumentTax[];\n exhaustsDocument: boolean;\n};\n\nfunction issuedCredit(invoice: Invoice): boolean {\n return (\n invoice.kind === 'credit_note' &&\n invoice.status !== InvoiceStatus.DRAFT &&\n invoice.status !== InvoiceStatus.CANCELLED\n );\n}\n\n/** @internal Refresh one economic credit effect, including historical issued credits. */\nexport async function refreshIssuedCreditBalance(\n db: DatabaseInterface,\n source: Invoice,\n): Promise<void> {\n const sourceId = requireId(source.id, 'Credited invoice');\n const credits = await (await InvoiceCollection.create({ db })).list({\n where: { tenantId: source.tenantId, creditedInvoiceId: sourceId },\n limit: 100001,\n });\n if (credits.length > 100000)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Issued credit scope exceeds its bounded source limit',\n );\n const issued = credits.filter(issuedCredit).sort((left, right) => {\n const date = left.issueDate.getTime() - right.issueDate.getTime();\n if (date) return date;\n return String(left.id) < String(right.id)\n ? -1\n : String(left.id) > String(right.id)\n ? 1\n : 0;\n });\n const total = issued.reduce((sum, credit) => sum + credit.totalAmount, 0);\n if (!Number.isSafeInteger(total) || total < 0)\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n 'Issued credit total is not a nonnegative safe integer',\n );\n let remainingSource = Math.max(0, source.totalAmount - source.amountPaid);\n const managed = new Map<string, number>();\n if (\n source.retentionAccountingStateJson ||\n source.ordinaryAccountingStateJson\n ) {\n const providerState = JSON.parse(\n source.retentionAccountingStateJson || source.ordinaryAccountingStateJson,\n );\n for (const entry of providerState.credits) {\n const credit = issued.find(\n (candidate) => candidate.id === entry.creditId,\n );\n const retainedMinor = source.retentionAccountingStateJson\n ? entry.retainedMinor\n : 0;\n const frozen = retainedMinor + entry.appliedMinor;\n if (\n !credit ||\n managed.has(entry.creditId) ||\n !Number.isSafeInteger(retainedMinor) ||\n retainedMinor < 0 ||\n !Number.isSafeInteger(entry.appliedMinor) ||\n entry.appliedMinor < 0 ||\n !Number.isSafeInteger(frozen) ||\n frozen > credit.totalAmount ||\n frozen !== credit.creditedAmount ||\n frozen > remainingSource\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Provider-managed credit allocation cannot change without an explicit reversal contract',\n );\n managed.set(entry.creditId, frozen);\n remainingSource -= frozen;\n }\n }\n // Provider applications are immutable authority, not a fresh allocation choice.\n // Reserve them before assigning residual capacity; neither database order nor\n // a later same-day credit's UUID may take an already committed application.\n for (const credit of [\n ...issued,\n ...credits.filter((entry) => !issued.includes(entry)),\n ]) {\n const included = issued.includes(credit);\n const frozen = managed.get(requireId(credit.id, 'Credit invoice'));\n const applied =\n frozen ?? (included ? Math.min(credit.totalAmount, remainingSource) : 0);\n if (included && frozen === undefined) remainingSource -= applied;\n if (credit.creditedAmount !== applied) {\n credit.creditedAmount = applied;\n credit.updatePaymentStatus(credit.amountPaid);\n // A zero application is not an issuance reversal. SENT can be recorded\n // without delivery timestamps, so payment status alone cannot restore it.\n if (included && credit.status === InvoiceStatus.DRAFT)\n credit.status = InvoiceStatus.SENT;\n await credit.saveDerived(DOCUMENT_MONEY_WRITE);\n }\n }\n if (\n (source.retentionAccountingStateJson ||\n source.ordinaryAccountingStateJson) &&\n total !== source.creditedAmount\n ) {\n const state = JSON.parse(\n source.retentionAccountingStateJson || source.ordinaryAccountingStateJson,\n );\n if (\n state.status !== 'ready' ||\n state.steps.some(\n (step: { status: string }) => step.status !== 'confirmed',\n )\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Provider retention operation must reconcile before changing source credits',\n );\n }\n source.creditedAmount = total;\n source.updatePaymentStatus(source.amountPaid);\n await source.saveDerived(DOCUMENT_MONEY_WRITE);\n}\n\nfunction lineNet(line: InvoiceLineItem, parent: Invoice): number {\n return parent.lineAmountType === 'inclusive'\n ? line.amount - line.taxAmount\n : line.amount;\n}\n\nasync function prepareCreditLines(\n db: DatabaseInterface,\n invoice: Invoice,\n lines: InvoiceLineItem[],\n): Promise<CreditPreparation> {\n if (!invoice.creditedInvoiceId)\n throw new MoneyValidationError(\n 'CREDIT_TARGET_NOT_ISSUED',\n 'A credit note requires a credited invoice',\n );\n const sourceInvoice = await findInvoice(db, invoice.creditedInvoiceId);\n if (\n sourceInvoice?.kind !== 'invoice' ||\n sourceInvoice.tenantId !== invoice.tenantId ||\n sourceInvoice.status === InvoiceStatus.DRAFT ||\n sourceInvoice.status === InvoiceStatus.CANCELLED ||\n !sourceInvoice.moneySnapshotId\n )\n throw new MoneyValidationError(\n 'CREDIT_TARGET_NOT_ISSUED',\n 'Credit target must be an issued, non-cancelled invoice in the same tenant',\n );\n if (sourceInvoice.moneyAuthority === 'historical')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Historical source credits require exact original monetary facts and concrete historical source line lineage, not calculated quantity or inferred tax',\n );\n if (sourceInvoice.currency !== invoice.currency)\n throw new MoneyValidationError(\n 'CURRENCY_MISMATCH',\n 'Credit note currency must match its credited invoice',\n );\n if (sourceInvoice.customerId !== invoice.customerId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Credit note customer must match its credited invoice',\n );\n invoice.lineAmountType = sourceInvoice.lineAmountType;\n invoice.taxRoundingScope = sourceInvoice.taxRoundingScope;\n const lineCollection = await InvoiceLineItemCollection.create({ db });\n const sourceLines = await lineCollection.findByInvoice(\n requireId(sourceInvoice.id, 'Credited invoice'),\n );\n const sourceTaxes = await (\n await DocumentTaxCollection.create({ db })\n ).findByDocument(\n requireId(sourceInvoice.tenantId, 'Credited invoice tenant'),\n 'invoice',\n requireId(sourceInvoice.id, 'Credited invoice'),\n );\n const sourceByLineId = new Map(\n sourceLines.map((line) => [requireId(line.id, 'Credited line'), line]),\n );\n const invoices = await InvoiceCollection.create({ db });\n const priorCredits = (\n await invoices.list({\n where: {\n tenantId: invoice.tenantId,\n creditedInvoiceId: invoice.creditedInvoiceId,\n },\n })\n ).filter((row) => row.id !== invoice.id && issuedCredit(row));\n const issuedLines: InvoiceLineItem[] = [];\n const issuedParentById = new Map(\n priorCredits.map((row) => [requireId(row.id, 'Issued credit note'), row]),\n );\n const consumptions: IssuedCreditConsumption[] = [];\n for (const credit of priorCredits) {\n for (const line of await lineCollection.findByInvoice(\n requireId(credit.id, 'Issued credit note'),\n )) {\n if (!line.creditedLineId) continue;\n issuedLines.push(line);\n const basis = line.creditCalculationBasis;\n consumptions.push({\n creditedLineId: line.creditedLineId,\n quantity: basis?.kind === 'quantity' ? basis.quantity : '0',\n netMinor: lineNet(line, credit),\n discountMinor: line.discount,\n });\n }\n }\n const targeted = lines.filter((line) => line.creditedLineId);\n const allocation = targeted.length\n ? allocateFrozenCredit(\n sourceLines.map((line) => ({\n id: requireId(line.id, 'Credited line'),\n quantity: String(line.quantity),\n netMinor: lineNet(line, sourceInvoice),\n discountMinor: line.discount,\n unitPrice: line.unitPrice,\n discountType: line.discountType,\n discountRate: line.discountRate,\n })),\n consumptions,\n targeted.map((line) => {\n const basis = line.creditCalculationBasis;\n return basis?.kind === 'net_amount'\n ? {\n creditedLineId: requireId(line.creditedLineId, 'Credited line'),\n netMinor: basis.netMinor,\n }\n : {\n creditedLineId: requireId(line.creditedLineId, 'Credited line'),\n quantity:\n basis?.kind === 'quantity'\n ? basis.quantity\n : String(line.quantity),\n };\n }),\n )\n : { allocations: [], exhaustsDocument: false };\n const allocationByLineId = new Map<string, FrozenCreditAllocation>();\n const targetBySource = new Map(\n targeted.map((line) => [\n requireId(line.creditedLineId, 'Credited line'),\n line,\n ]),\n );\n for (const row of allocation.allocations) {\n const line = targetBySource.get(row.creditedLineId);\n if (!line)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Credit allocation lost its target line',\n );\n line.creditCalculationBasis = row.basis;\n allocationByLineId.set(requireId(line.id, 'Credit line'), row);\n }\n return {\n allocationByLineId,\n sourceByLineId,\n issuedLines,\n issuedParentById,\n sourceInvoice,\n sourceTaxes,\n exhaustsDocument: allocation.exhaustsDocument,\n };\n}\n\nasync function recomputeInvoiceAggregate(\n db: DatabaseInterface,\n invoice: Invoice,\n settings: TaxSettings,\n freezeSnapshotId = '',\n persist = true,\n proposedLines?: InvoiceLineItem[],\n): Promise<{ lines: InvoiceLineItem[]; taxRows: DocumentTaxWriteInput[] }> {\n assertTenantId(invoice.tenantId);\n const tenantId = invoice.tenantId;\n const linesCollection = await InvoiceLineItemCollection.create({ db });\n const codesCollection = await TaxCodeCollection.create({ db });\n const componentsCollection = await TaxComponentCollection.create({ db });\n const invoiceId = requireId(invoice.id, 'Invoice calculation');\n const lines =\n proposedLines ?? (await linesCollection.findByInvoice(invoiceId));\n const credit =\n invoice.kind === 'credit_note'\n ? await prepareCreditLines(db, invoice, lines)\n : null;\n const codes = await codesCollection.list({ where: { tenantId } });\n const components = await componentsCollection.list({ where: { tenantId } });\n const componentById = new Map(\n components.map((row) => [requireId(row.id, 'Tax component'), row]),\n );\n const codeById = new Map(\n codes.map((row) => [requireId(row.id, 'Tax code'), row]),\n );\n const codeInputs: Record<string, ResolvableTaxCodeInput> = {};\n for (const code of codes) {\n const codeId = requireId(code.id, 'Tax code');\n codeInputs[codeId] = {\n id: codeId,\n kind: code.kind,\n active: code.active,\n appliesTo: code.appliesTo,\n components: code.components.map((part) => {\n const component = componentById.get(part.taxComponentId);\n if (!component)\n throw new MoneyValidationError(\n 'TAX_CODE_NOT_FOUND',\n `Tax component ${part.taxComponentId} was not loaded`,\n );\n return {\n taxComponentId: requireId(component.id, 'Tax component'),\n percent: component.percent,\n compound: part.compound,\n };\n }),\n };\n }\n if (credit) {\n for (const source of credit.sourceByLineId.values()) {\n if (!source.taxCodeId) continue;\n const frozenKind = credit.sourceTaxes.find(\n (row) => row.taxCodeId === source.taxCodeId,\n )?.kind;\n codeInputs[source.taxCodeId] = {\n id: source.taxCodeId,\n kind:\n frozenKind ??\n (source.taxComponents.length ? 'taxable' : 'zero-rated'),\n active: true,\n appliesTo: 'sales',\n components: source.taxComponents.map((component) => ({\n taxComponentId: component.taxComponentId,\n percent: component.percent,\n compound: component.compound ?? false,\n })),\n };\n }\n }\n const exempt = await codesCollection.findByCode('exempt', tenantId);\n const outOfScope = await codesCollection.findByCode('out-of-scope', tenantId);\n const customers = await CustomerCollection.create({ db });\n const customer = invoice.customerId\n ? await customers.get({ id: invoice.customerId }, { cache: false })\n : null;\n if (customer && customer.tenantId !== tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice customer must belong to its tenant',\n );\n\n // Products owns catalog reads. Load it lazily, on the aggregate transaction,\n // so commerce keeps the same runtime-only package edge used for ledgers.\n const productIds = [\n ...new Set(\n lines\n .filter((line) => !line.creditedLineId)\n .map((line) => line.productId)\n .filter((productId): productId is string => !!productId),\n ),\n ];\n const productById = new Map<string, ProductTaxDefaults>();\n if (productIds.length > 0) {\n const { ProductCollection } = await import(\n '@happyvertical/smrt-products/collections'\n );\n const products = await ProductCollection.create({ db });\n for (const productId of productIds) {\n const product = await products.get({ id: productId }, { cache: false });\n if (!product)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Invoice product ${productId} was not found`,\n );\n if (product.tenantId !== null && product.tenantId !== tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Invoice product ${productId} must be global or belong to its tenant`,\n );\n productById.set(productId, product);\n }\n }\n\n type ResolvedMetadata = {\n id: string;\n code: string | null;\n name: string | null;\n kind: 'taxable' | 'zero-rated' | 'exempt' | 'out-of-scope';\n components: InvoiceTaxComponentSnapshot[];\n };\n const resolvedCodes = new Map<string, ResolvedMetadata | null>();\n const calculated = calculateMoneyDocument({\n currency: invoice.currency,\n lineAmountType: invoice.lineAmountType,\n roundingScope: invoice.taxRoundingScope,\n lines: lines.map((line) => {\n if (line.tenantId !== tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Invoice line ${line.id} must belong to its parent tenant`,\n );\n const lineId = requireId(line.id, 'Invoice line');\n if (line.creditedLineId) {\n if (!credit)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Only a credit note may reference a credited line',\n );\n const source = credit.sourceByLineId.get(line.creditedLineId);\n const allocation = credit.allocationByLineId.get(lineId);\n if (!source || !allocation)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Credited line is not part of the frozen source invoice',\n );\n const taxCode = source.taxCodeId\n ? (codeInputs[source.taxCodeId] ?? null)\n : null;\n resolvedCodes.set(\n lineId,\n source.taxCodeId\n ? {\n id: source.taxCodeId,\n code: source.taxCodeCode,\n name: source.taxCodeName,\n kind: taxCode?.kind ?? 'zero-rated',\n components: structuredClone(source.taxComponents),\n }\n : null,\n );\n line.description ||= source.description;\n line.sku ||= source.sku;\n line.productId = source.productId;\n line.revenueAccountId = source.revenueAccountId;\n if (allocation.basis.kind === 'quantity') {\n line.quantity = Number(allocation.basis.quantity);\n line.unitPrice = allocation.basis.sourceUnitPrice;\n line.discountType = allocation.discountMinor ? 'amount' : 'none';\n line.discountRate = '0';\n } else {\n line.quantity = 1;\n line.unitPrice = allocation.netMinor ?? allocation.basis.netMinor;\n line.discountType = 'none';\n line.discountRate = '0';\n }\n line.discount = allocation.discountMinor;\n return {\n id: lineId,\n quantity: line.quantity,\n unitPrice: line.unitPrice,\n netAmountMinor:\n allocation.basis.kind === 'net_amount'\n ? (allocation.netMinor ?? allocation.basis.netMinor)\n : undefined,\n discount: allocation.discountMinor\n ? ({ kind: 'amount', minor: allocation.discountMinor } as const)\n : ({ kind: 'none' } as const),\n taxCode,\n };\n }\n if (invoice.kind === 'invoice' && line.creditCalculationBasis)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Ordinary invoice lines cannot carry a credit calculation basis',\n );\n if (invoice.kind === 'credit_note' && line.taxMode !== 'override')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Free-form credit lines require an explicit tax-code override',\n );\n const resolved = resolveLineTaxCode(\n {\n lineAmountType: invoice.lineAmountType,\n lineOverride: line.taxMode === 'override' ? line.taxCodeId : null,\n },\n {\n direction: 'sales',\n codes: codeInputs,\n customerTaxExempt: customer?.taxExempt ?? false,\n exemptCodeId: exempt?.id,\n productCodeId: line.productId\n ? productById.get(line.productId)?.defaultSalesTaxCodeId || null\n : null,\n partyCodeId: customer?.defaultTaxCodeId,\n appDefaultCodeId: settings.defaultSalesTaxCodeId,\n outOfScopeCodeId: outOfScope?.id,\n },\n );\n resolvedCodes.set(\n lineId,\n resolved.taxCode\n ? (() => {\n const code = codeById.get(resolved.taxCode.id);\n if (!code)\n throw new MoneyValidationError(\n 'TAX_CODE_NOT_FOUND',\n `Tax code ${resolved.taxCode.id} was not loaded`,\n );\n return {\n id: requireId(code.id, 'Tax code'),\n code: code.code,\n name: code.name,\n kind: code.kind,\n components: code.components.map((part) => {\n const component = componentById.get(part.taxComponentId);\n if (!component)\n throw new MoneyValidationError(\n 'TAX_CODE_NOT_FOUND',\n `Tax component ${part.taxComponentId} was not loaded`,\n );\n return {\n taxComponentId: part.taxComponentId,\n name: component.name,\n percent: component.percent,\n ...(part.compound ? { compound: true } : {}),\n taxMinor: 0,\n };\n }),\n };\n })()\n : null,\n );\n return {\n id: requireId(line.id, 'Invoice line'),\n quantity: line.quantity,\n unitPrice: line.unitPrice,\n discount: discountInput(line),\n taxCode: resolved.taxCode,\n };\n }),\n });\n\n // When every original line is exhausted, consume the exact remaining frozen\n // component and taxable residual. This preserves document-rounding cents\n // without consulting today's rates; free-form goodwill lines remain separate.\n if (credit?.exhaustsDocument) {\n const resultById = new Map(calculated.lines.map((row) => [row.id, row]));\n const targeted = lines.filter((line) => !!line.creditedLineId);\n for (const line of targeted) {\n const source = credit.sourceByLineId.get(\n requireId(line.creditedLineId, 'Credited line'),\n );\n const result = resultById.get(requireId(line.id, 'Credit line'));\n if (!source || !result) continue;\n const prior = credit.issuedLines.filter(\n (candidate) => candidate.creditedLineId === source.id,\n );\n const priorNet = prior.reduce((sum, candidate) => {\n const parent = credit.issuedParentById.get(candidate.invoiceId);\n return sum + (parent ? lineNet(candidate, parent) : 0);\n }, 0);\n const desiredNet = lineNet(source, credit.sourceInvoice) - priorNet;\n const netDelta = desiredNet - result.netMinor;\n result.netMinor = desiredNet;\n result.amountMinor += netDelta;\n for (const sourceComponent of source.taxComponents) {\n const component = result.components.find(\n (candidate) =>\n candidate.taxComponentId === sourceComponent.taxComponentId,\n );\n if (!component) continue;\n const priorTax = prior.reduce(\n (sum, candidate) =>\n sum +\n (candidate.taxComponents.find(\n (part) => part.taxComponentId === sourceComponent.taxComponentId,\n )?.taxMinor ?? 0),\n 0,\n );\n const desiredTax = sourceComponent.taxMinor - priorTax;\n const taxDelta = desiredTax - component.taxMinor;\n component.taxMinor = desiredTax;\n if (sourceComponent.compound) {\n component.compound = true;\n component.taxableMinor =\n (sourceComponent.taxableMinor ?? 0) -\n prior.reduce(\n (sum, candidate) =>\n sum +\n (candidate.taxComponents.find(\n (part) =>\n part.taxComponentId === sourceComponent.taxComponentId,\n )?.taxableMinor ?? 0),\n 0,\n );\n }\n result.taxMinor += taxDelta;\n if (invoice.lineAmountType === 'inclusive')\n result.amountMinor += taxDelta;\n }\n }\n const rebuilt = new Map<\n string,\n {\n taxCodeId: string | null;\n taxComponentId: string | null;\n taxableMinor: number;\n netMinor?: number;\n taxMinor: number;\n }\n >();\n for (const result of calculated.lines) {\n const metadata = resolvedCodes.get(result.id) ?? null;\n const parts = result.components.length\n ? result.components\n : [\n {\n taxComponentId: null,\n taxMinor: 0,\n compound: false,\n taxableMinor: undefined,\n },\n ];\n for (const part of parts) {\n const key = `${metadata?.id ?? ''}\\u0000${part.taxComponentId ?? ''}`;\n const row = rebuilt.get(key) ?? {\n taxCodeId: metadata?.id ?? null,\n taxComponentId: part.taxComponentId,\n taxableMinor: 0,\n taxMinor: 0,\n };\n row.taxableMinor += part.taxableMinor ?? result.netMinor;\n if (part.compound) row.netMinor = (row.netMinor ?? 0) + result.netMinor;\n row.taxMinor += part.taxMinor;\n rebuilt.set(key, row);\n }\n }\n calculated.taxes = [...rebuilt.values()].sort((left, right) => {\n const a = `${left.taxCodeId ?? ''}\\u0000${left.taxComponentId ?? ''}`;\n const b = `${right.taxCodeId ?? ''}\\u0000${right.taxComponentId ?? ''}`;\n return a < b ? -1 : a > b ? 1 : 0;\n });\n calculated.subtotalMinor = calculated.lines.reduce(\n (sum, row) => sum + row.netMinor,\n 0,\n );\n calculated.taxMinor = calculated.lines.reduce(\n (sum, row) => sum + row.taxMinor,\n 0,\n );\n calculated.totalMinor = calculated.subtotalMinor + calculated.taxMinor;\n }\n\n if (credit) {\n const resultById = new Map(calculated.lines.map((row) => [row.id, row]));\n for (const [lineId, allocation] of credit.allocationByLineId) {\n if (allocation.basis.kind !== 'quantity') continue;\n const result = resultById.get(lineId);\n const hasAmountBasisConsumption = credit.issuedLines.some(\n (line) =>\n line.creditedLineId === allocation.creditedLineId &&\n line.creditCalculationBasis?.kind === 'net_amount',\n );\n if (\n result &&\n Math.abs(result.netMinor) > Math.abs(allocation.remainingNetMinor) &&\n (!allocation.exhaustsLine || hasAmountBasisConsumption)\n )\n throw new MoneyValidationError(\n 'CREDIT_EXCEEDS_LINE',\n 'Credit quantity exceeds the remaining frozen net amount',\n );\n }\n }\n\n const lineById = new Map(\n lines.map((line) => [requireId(line.id, 'Invoice line'), line]),\n );\n for (const result of calculated.lines) {\n const line = lineById.get(result.id);\n if (!line)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Calculated line ${result.id} was not loaded`,\n );\n const code = resolvedCodes.get(requireId(line.id, 'Invoice line')) ?? null;\n line.discount = result.discountMinor;\n line.amount = result.amountMinor;\n line.taxAmount = result.taxMinor;\n line.taxCodeId = code?.id ?? null;\n line.taxCodeCode = code?.code ?? null;\n line.taxCodeName = code?.name ?? null;\n line.taxComponents = result.components.map((entry) => {\n const component = code?.components.find(\n (candidate) => candidate.taxComponentId === entry.taxComponentId,\n );\n if (!component)\n throw new MoneyValidationError(\n 'TAX_CODE_NOT_FOUND',\n `Calculated component ${entry.taxComponentId} was not loaded`,\n );\n return {\n taxComponentId: component.taxComponentId,\n name: component.name,\n percent: component.percent,\n taxMinor: entry.taxMinor,\n ...(entry.compound\n ? { compound: true, taxableMinor: entry.taxableMinor }\n : {}),\n };\n });\n line.taxRate = result.components.some((part) => part.compound)\n ? result.netMinor === 0\n ? 0\n : result.taxMinor / result.netMinor\n : code\n ? code.components.reduce(\n (sum, part) => sum + Number(part.percent) / 100,\n 0,\n )\n : 0;\n if (freezeSnapshotId) line.moneySnapshotId = freezeSnapshotId;\n if (persist) await line.saveDerived(DOCUMENT_MONEY_WRITE);\n }\n\n const taxRows = calculated.taxes.map((tax): DocumentTaxWriteInput => {\n const code = tax.taxCodeId\n ? ([...resolvedCodes.values()].find(\n (candidate) => candidate?.id === tax.taxCodeId,\n ) ?? null)\n : null;\n const snapshotComponent = tax.taxComponentId\n ? code?.components.find(\n (candidate) => candidate.taxComponentId === tax.taxComponentId,\n )\n : null;\n const currentComponent = tax.taxComponentId\n ? componentById.get(tax.taxComponentId)\n : null;\n const sourceTax = credit?.sourceTaxes.find(\n (row) =>\n row.taxCodeId === tax.taxCodeId &&\n row.taxComponentId === tax.taxComponentId,\n );\n if (tax.taxCodeId && !code)\n throw new MoneyValidationError(\n 'TAX_CODE_NOT_FOUND',\n `Document tax code ${tax.taxCodeId} was not loaded`,\n );\n if (tax.taxComponentId && !snapshotComponent)\n throw new MoneyValidationError(\n 'TAX_CODE_NOT_FOUND',\n `Document tax component ${tax.taxComponentId} was not loaded`,\n );\n return {\n tenantId,\n documentType: invoice.kind,\n documentId: invoiceId,\n currency: invoice.currency,\n taxCodeId: code ? requireId(code.id, 'Tax code') : null,\n taxCodeCode: code?.code ?? null,\n kind: code?.kind ?? null,\n taxComponentId: snapshotComponent?.taxComponentId ?? null,\n taxComponentName: snapshotComponent?.name ?? null,\n percent: snapshotComponent?.percent ?? null,\n liabilityAccountId:\n sourceTax?.liabilityAccountId ||\n currentComponent?.liabilityAccountId ||\n null,\n taxableMinor: tax.taxableMinor,\n netMinor: tax.netMinor ?? null,\n taxMinor: tax.taxMinor,\n recoverableMinor: 0,\n };\n });\n if (persist) {\n const documentTaxes = await DocumentTaxCollection.create({ db });\n await documentTaxes.replaceDerivedForDocument(DOCUMENT_MONEY_WRITE, {\n tenantId,\n documentType: invoice.kind,\n documentId: invoiceId,\n rows: taxRows,\n });\n }\n invoice.subtotal = calculated.subtotalMinor;\n invoice.discountTotal = calculated.discountTotalMinor;\n invoice.taxAmount = calculated.taxMinor + invoice.providerTaxAmount;\n invoice.totalAmount = calculated.totalMinor + invoice.providerTaxAmount;\n return { lines, taxRows };\n}\n\nasync function previewInvoiceMoneyOnScope(\n input: PreviewInvoiceMoneyInput,\n settings: TaxSettings,\n): Promise<PreviewInvoiceMoneyResult> {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice preview requires its active principal tenant',\n );\n const { invoice } = await loadInvoice(input.db, input.invoiceId);\n if (invoice.tenantId !== input.tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice preview cannot cross tenant boundaries',\n );\n if (invoice.status !== InvoiceStatus.DRAFT || invoice.moneySnapshotId)\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Frozen invoice money cannot be previewed as a proposed draft',\n );\n const proposedHeader = input.draft?.header;\n if (proposedHeader)\n for (const field of [\n 'currency',\n 'lineAmountType',\n 'taxRoundingScope',\n 'providerTaxAmount',\n 'customerId',\n ] as const)\n if (proposedHeader[field] !== undefined)\n invoice[field] = proposedHeader[field] as never;\n const storedLines = await (\n await InvoiceLineItemCollection.create({ db: input.db })\n ).findByInvoice(input.invoiceId);\n const storedById = new Map(storedLines.map((line) => [line.id, line]));\n const providedIds =\n input.draft?.lines\n ?.map((line) => line.id)\n .filter((id): id is string => !!id) ?? [];\n if (new Set(providedIds).size !== providedIds.length)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice preview line ids must be distinct',\n );\n const unknownId = providedIds.find((id) => !storedById.has(id));\n if (unknownId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Invoice preview line ${unknownId} was not found on the invoice`,\n );\n const proposedLines = input.draft?.lines\n ? input.draft.lines.map((proposed) => {\n const line = proposed.id ? storedById.get(proposed.id) : undefined;\n const result =\n line ??\n new (ObjectRegistry.resolveEffectiveConstructor(InvoiceLineItem))();\n for (const field of [\n 'description',\n 'productId',\n 'quantity',\n 'unitPrice',\n 'discountType',\n 'discountRate',\n 'discount',\n 'taxMode',\n 'taxCodeId',\n ] as const)\n if (proposed[field] !== undefined)\n result[field] = proposed[field] as never;\n result.id ||= crypto.randomUUID();\n result.invoiceId = input.invoiceId;\n result.tenantId = input.tenantId;\n return result;\n })\n : storedLines;\n const calculated = await recomputeInvoiceAggregate(\n input.db,\n invoice,\n settings,\n '',\n false,\n proposedLines,\n );\n return {\n source: 'calculated',\n header: {\n currency: invoice.currency,\n lineAmountType: invoice.lineAmountType,\n taxRoundingScope: invoice.taxRoundingScope,\n subtotalMinor: invoice.subtotal,\n discountTotalMinor: invoice.discountTotal,\n taxAmountMinor: invoice.taxAmount,\n providerTaxAmountMinor: invoice.providerTaxAmount,\n totalAmountMinor: invoice.totalAmount,\n },\n lines: calculated.lines.map((line) => ({\n id: requireId(line.id, 'Preview line'),\n grossMinor: line.amount + line.discount,\n discountMinor: line.discount,\n amountMinor: line.amount,\n taxMinor: line.taxAmount,\n taxCodeId: line.taxCodeId,\n taxCodeCode: line.taxCodeCode,\n taxCodeName: line.taxCodeName,\n taxComponents: structuredClone(line.taxComponents),\n })),\n taxes: calculated.taxRows,\n };\n}\n\n/** Calculate a proposed draft invoice in memory through the owning resolver. */\nexport async function previewInvoiceMoney(\n input: PreviewInvoiceMoneyInput,\n): Promise<PreviewInvoiceMoneyResult> {\n return withTenantMoneyRuntime(input.db, input.tenantId, async (scope) =>\n previewInvoiceMoneyOnScope({ ...input, db: scope.db }, scope.settings),\n );\n}\n\nasync function getInvoiceMoneyFactsOnScope(\n input: GetInvoiceMoneyFactsInput,\n): Promise<InvoiceMoneyFacts> {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice money facts require their active principal tenant',\n );\n const { invoice } = await loadInvoice(input.db, input.invoiceId);\n if (invoice.tenantId !== input.tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice money facts cannot cross tenant boundaries',\n );\n const lines = await (\n await InvoiceLineItemCollection.create({ db: input.db })\n ).findByInvoice(input.invoiceId);\n const taxes = await (\n await DocumentTaxCollection.create({ db: input.db })\n ).findByDocument(input.tenantId, invoice.kind, input.invoiceId);\n const historical = getHistoricalSalesMoney(invoice);\n return {\n source: 'stored',\n ...(historical\n ? {\n historical: {\n snapshot: historical,\n lines: (await readHistoricalSalesLines(input.db, invoice)).map(\n (line) => ({\n sourceKey: line.sourceKey,\n lineId: String(line.id),\n }),\n ),\n },\n }\n : {}),\n ...(invoice.retentionStateJson\n ? { payable: invoice.getPayableFacts() }\n : {}),\n invoiceId: input.invoiceId,\n moneySnapshotId: invoice.moneySnapshotId,\n kind: invoice.kind,\n status: invoice.status,\n header: {\n currency: invoice.currency,\n lineAmountType: invoice.lineAmountType,\n taxRoundingScope: invoice.taxRoundingScope,\n subtotalMinor: invoice.subtotal,\n discountTotalMinor: invoice.discountTotal,\n taxAmountMinor: invoice.taxAmount,\n providerTaxAmountMinor: invoice.providerTaxAmount,\n totalAmountMinor: invoice.totalAmount,\n },\n lines: lines.map((line) => ({\n id: requireId(line.id, 'Invoice line'),\n grossMinor: line.amount + line.discount,\n discountMinor: line.discount,\n amountMinor: line.amount,\n taxMinor: line.taxAmount,\n taxCodeId: line.taxCodeId,\n taxCodeCode: line.taxCodeCode,\n taxCodeName: line.taxCodeName,\n taxComponents: structuredClone(line.taxComponents),\n })),\n taxes: taxes.map((row) => ({\n tenantId: requireId(row.tenantId, 'Document tax tenant'),\n documentType: row.documentType,\n documentId: row.documentId,\n currency: row.currency,\n taxCodeId: row.taxCodeId,\n taxCodeCode: row.taxCodeCode,\n kind: row.kind,\n taxComponentId: row.taxComponentId,\n taxComponentName: row.taxComponentName,\n percent: row.percent,\n liabilityAccountId: row.liabilityAccountId,\n taxableMinor: row.taxableMinor,\n ...(row.netMinor !== null ? { netMinor: row.netMinor } : {}),\n taxMinor: row.taxMinor,\n recoverableMinor: row.recoverableMinor,\n })),\n };\n}\n\n/** Read authoritative persisted money facts without resolution or recalculation. */\nexport async function getInvoiceMoneyFacts(\n input: GetInvoiceMoneyFactsInput,\n): Promise<InvoiceMoneyFacts> {\n return withTenantMoneyRuntime(input.db, input.tenantId, async (scope) =>\n getInvoiceMoneyFactsOnScope({ ...input, db: scope.db }),\n );\n}\n\n/** Persist an invoice and recompute its complete money aggregate. */\nexport async function saveInvoice(\n input: Invoice,\n options?: SmrtSaveOptions,\n): Promise<Invoice> {\n const priorRevision = input.updated_at;\n try {\n return await withDocumentMoneyTransaction(input.db, async (tx) =>\n input.withDatabase(tx, async (invoice) => {\n if (invoice.kind !== 'invoice' && invoice.kind !== 'credit_note')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice kind must be invoice or credit_note',\n );\n if (invoice.kind === 'invoice' && invoice.creditedInvoiceId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Only a credit note may reference a credited invoice',\n );\n if (invoice.kind === 'credit_note' && !invoice.creditedInvoiceId)\n throw new MoneyValidationError(\n 'CREDIT_TARGET_NOT_ISSUED',\n 'A credit note requires a credited invoice',\n );\n const candidateTenantId = getTenantId() ?? invoice.tenantId ?? null;\n const settings = await lockRuntimeSettings(tx, candidateTenantId);\n const existing = invoice.id ? await findInvoice(tx, invoice.id) : null;\n const tenantId = existing?.tenantId ?? candidateTenantId;\n if (\n invoice.retentionAccountingStateJson !==\n (existing?.retentionAccountingStateJson ?? '') ||\n invoice.ordinaryAccountingStateJson !==\n (existing?.ordinaryAccountingStateJson ?? '')\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Retention accounting state is managed by provider commands',\n );\n if (\n invoice.moneyAuthority !==\n (existing?.moneyAuthority ?? 'calculated') ||\n invoice.historicalSalesMoneyJson !==\n (existing?.historicalSalesMoneyJson ?? '') ||\n invoice.historicalSourceKeyHash !==\n (existing?.historicalSourceKeyHash ?? null)\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Historical source authority is managed only by historical sales commands',\n );\n if (existing?.moneyAuthority === 'historical')\n getHistoricalSalesMoney(existing);\n\n if (invoice.retentionStateJson !== (existing?.retentionStateJson ?? ''))\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Retention state is managed by retention commands',\n );\n if (\n candidateTenantId &&\n existing?.tenantId &&\n existing.tenantId !== candidateTenantId\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice tenant does not match its active money barrier',\n );\n assertTenantId(tenantId);\n if (!existing) {\n invoice.tenantId = tenantId;\n invoice.creditedAmount = 0;\n let source: Invoice | null = null;\n if (invoice.kind === 'credit_note') {\n source = await findInvoice(\n tx,\n requireId(invoice.creditedInvoiceId, 'Credited invoice'),\n );\n if (!source || source.tenantId !== tenantId)\n throw new MoneyValidationError(\n 'CREDIT_TARGET_NOT_ISSUED',\n 'Credit target must exist in the active tenant',\n );\n await lockDocumentMoneyParents([\n {\n id: requireId(source.id, 'Credited invoice'),\n tenantId,\n collection: await InvoiceCollection.create({ db: tx }),\n },\n ]);\n if (\n source.kind !== 'invoice' ||\n source.status === InvoiceStatus.DRAFT ||\n source.status === InvoiceStatus.CANCELLED ||\n !source.moneySnapshotId\n )\n throw new MoneyValidationError(\n 'CREDIT_TARGET_NOT_ISSUED',\n 'Credit target must be an issued, non-cancelled invoice',\n );\n if (\n invoice.currency !== source.currency ||\n invoice.customerId !== source.customerId\n )\n throw new MoneyValidationError(\n invoice.currency !== source.currency\n ? 'CURRENCY_MISMATCH'\n : 'INVALID_MONEY_INPUT',\n 'Credit note currency and customer must match its source invoice',\n );\n }\n invoice.lineAmountType =\n invoice.kind === 'credit_note' && source\n ? source.lineAmountType\n : settings.defaultLineAmountType;\n invoice.taxRoundingScope =\n invoice.kind === 'credit_note' && source\n ? source.taxRoundingScope\n : settings.taxRoundingScope;\n invoice.subtotal = 0;\n invoice.discountTotal = 0;\n invoice.taxAmount = invoice.providerTaxAmount;\n invoice.totalAmount = invoice.providerTaxAmount;\n if (invoice.status !== InvoiceStatus.DRAFT || invoice.arJournalId) {\n await assertContractScheduleInvoiceFreeze(tx, invoice);\n const customers = await CustomerCollection.create({ db: tx });\n const customer = invoice.customerId\n ? await customers.get(\n { id: invoice.customerId },\n { cache: false },\n )\n : null;\n invoice.moneySnapshotId = crypto.randomUUID();\n invoice.sellerTaxRegistrations = structuredClone(\n settings.taxRegistrations,\n );\n invoice.partyTaxId = customer?.taxId ?? '';\n }\n return invoice.saveDerived(DOCUMENT_MONEY_WRITE, options);\n }\n if (invoice.tenantId !== existing.tenantId)\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Invoice tenant identity cannot change',\n );\n if (\n invoice.kind !== existing.kind ||\n invoice.creditedInvoiceId !== existing.creditedInvoiceId\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Invoice kind and credited invoice identity are immutable',\n );\n const existingId = requireId(existing.id, 'Invoice');\n const locked = await lockAndReloadMoneyInvoice(\n tx,\n existingId,\n tenantId,\n existing.creditedInvoiceId,\n );\n invoice.creditedAmount = locked.invoice.creditedAmount;\n if (isFrozen(locked.invoice)) {\n if (\n invoice.arReversalJournalId !== locked.invoice.arReversalJournalId\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Invoice reversal journal identity is server-managed',\n );\n if (\n locked.invoice.arJournalId &&\n invoice.arJournalId !== locked.invoice.arJournalId\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Invoice revenue journal identity is frozen once assigned',\n );\n if (\n invoice.status === InvoiceStatus.CANCELLED &&\n locked.invoice.status !== InvoiceStatus.CANCELLED &&\n locked.invoice.kind === 'invoice' &&\n locked.invoice.isPaid()\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'A paid invoice cannot be cancelled',\n );\n if (\n invoice.status === InvoiceStatus.CANCELLED &&\n locked.invoice.status !== InvoiceStatus.CANCELLED &&\n locked.invoice.kind === 'credit_note' &&\n locked.invoice.amountPaid > 0\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'A refunded credit note cannot be cancelled',\n );\n if (\n invoice.status === InvoiceStatus.CANCELLED &&\n locked.invoice.status !== InvoiceStatus.CANCELLED &&\n locked.invoice.arJournalId &&\n !locked.invoice.arReversalJournalId\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'A posted invoice requires an atomic reversal before cancellation',\n );\n assertFieldsUnchanged(\n invoice,\n locked.invoice,\n FROZEN_INVOICE_FIELDS,\n 'Invoice',\n );\n const saved = await invoice.saveDerived(\n DOCUMENT_MONEY_WRITE,\n options,\n );\n if (locked.source)\n await refreshIssuedCreditBalance(tx, locked.source);\n return saved;\n }\n const freezing =\n invoice.status !== InvoiceStatus.DRAFT || !!invoice.arJournalId;\n const snapshotId = freezing\n ? locked.invoice.moneySnapshotId || crypto.randomUUID()\n : '';\n await recomputeInvoiceAggregate(tx, invoice, settings, snapshotId);\n if (freezing) {\n const sourceLines = await (\n await InvoiceLineItemCollection.create({ db: tx })\n ).findByInvoice(existingId);\n for (const line of sourceLines)\n await assertInvoiceLineSource(tx, invoice, line, line, true);\n await assertContractScheduleInvoiceFreeze(tx, invoice);\n const customers = await CustomerCollection.create({ db: tx });\n const customer = invoice.customerId\n ? await customers.get({ id: invoice.customerId }, { cache: false })\n : null;\n invoice.sellerTaxRegistrations = structuredClone(\n settings.taxRegistrations,\n );\n invoice.partyTaxId = customer?.taxId ?? '';\n invoice.moneySnapshotId = snapshotId;\n }\n const saved = await invoice.saveDerived(DOCUMENT_MONEY_WRITE, options);\n if (locked.source) await refreshIssuedCreditBalance(tx, locked.source);\n return saved;\n }),\n );\n } catch (error) {\n input.updated_at = priorRevision;\n throw error;\n }\n}\n\n/** Persist a line and recompute its complete invoice aggregate. */\nexport async function saveInvoiceLineItem(\n input: InvoiceLineItem,\n options?: SmrtSaveOptions,\n): Promise<InvoiceLineItem> {\n return withDocumentMoneyObjectTransaction(input, async (line, tx) => {\n const candidateTenantId = getTenantId() ?? line.tenantId ?? null;\n const settings = await lockRuntimeSettings(tx, candidateTenantId);\n let persisted = line.id\n ? await (await InvoiceLineItemCollection.create({ db: tx })).get(\n { id: line.id },\n { cache: false },\n )\n : null;\n const parentId = persisted?.invoiceId ?? line.invoiceId;\n const first = await loadInvoice(tx, parentId);\n if (candidateTenantId && first.invoice.tenantId !== candidateTenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice line tenant does not match its active money barrier',\n );\n assertTenantId(first.invoice.tenantId);\n const { invoice, source } = await lockAndReloadMoneyInvoice(\n tx,\n parentId,\n first.invoice.tenantId,\n first.invoice.creditedInvoiceId,\n );\n // Binding may have committed while this save waited for its invoice lock.\n persisted = line.id\n ? await (await InvoiceLineItemCollection.create({ db: tx })).get(\n { id: line.id },\n { cache: false },\n )\n : null;\n if (persisted && line.invoiceId !== persisted.invoiceId)\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Invoice line cannot move between parents',\n );\n line.invoiceId = requireId(invoice.id, 'Invoice');\n line.tenantId = invoice.tenantId;\n if (invoice.kind === 'invoice' && line.creditedLineId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Ordinary invoice lines cannot reference credited lines',\n );\n if (\n invoice.kind === 'credit_note' &&\n line.creditedLineId &&\n (!source || source.id !== invoice.creditedInvoiceId)\n )\n throw new MoneyValidationError(\n 'CREDIT_TARGET_NOT_ISSUED',\n 'Credit source changed while saving its line',\n );\n await assertInvoiceLineSource(tx, invoice, line, persisted);\n if (isFrozen(invoice)) {\n if (!persisted)\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Cannot add a line to a frozen invoice',\n );\n assertFieldsUnchanged(\n line,\n persisted,\n FROZEN_LINE_FIELDS,\n 'Invoice line',\n );\n return line.saveDerived(DOCUMENT_MONEY_WRITE, options);\n }\n await line.saveDerived(DOCUMENT_MONEY_WRITE, options);\n const priorAggregate = aggregateSignature(invoice);\n await recomputeInvoiceAggregate(tx, invoice, settings);\n if (aggregateSignature(invoice) !== priorAggregate) {\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n }\n const refreshed = await (\n await InvoiceLineItemCollection.create({ db: tx })\n ).get({ id: requireId(line.id, 'Invoice line') }, { cache: false });\n if (refreshed) {\n for (const field of [\n 'discount',\n 'amount',\n 'taxAmount',\n 'taxRate',\n 'taxCodeId',\n 'taxCodeCode',\n 'taxCodeName',\n 'taxComponentsJson',\n 'moneySnapshotId',\n 'updated_at',\n ] as const) {\n line[field] = refreshed[field] as never;\n }\n }\n return line;\n });\n}\n\nexport async function deleteInvoiceLineItem(\n input: InvoiceLineItem,\n options: DeleteOptions,\n): Promise<void> {\n await withDocumentMoneyTransaction(input.db, async (tx) =>\n input.withDatabase(tx, async (line) => {\n const candidateTenantId = getTenantId() ?? line.tenantId ?? null;\n const settings = await lockRuntimeSettings(tx, candidateTenantId);\n const persisted = line.id\n ? await (await InvoiceLineItemCollection.create({ db: tx })).get(\n { id: line.id },\n { cache: false },\n )\n : null;\n if (!persisted) return;\n const first = await loadInvoice(tx, persisted.invoiceId);\n if (candidateTenantId && first.invoice.tenantId !== candidateTenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice line tenant does not match its active money barrier',\n );\n assertTenantId(first.invoice.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n persisted.invoiceId,\n first.invoice.tenantId,\n );\n const current = await (\n await InvoiceLineItemCollection.create({ db: tx })\n ).get({ id: persisted.id }, { cache: false });\n if (!current) return;\n assertInvoiceSourceDelete([current]);\n if (isFrozen(invoice))\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Cannot delete a line from a frozen invoice',\n );\n await line.deleteDerived(DOCUMENT_MONEY_WRITE, options);\n const priorAggregate = aggregateSignature(invoice);\n await recomputeInvoiceAggregate(tx, invoice, settings);\n if (aggregateSignature(invoice) !== priorAggregate) {\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n }\n }),\n );\n}\n\nexport async function deleteInvoice(\n input: Invoice,\n options: DeleteOptions,\n): Promise<void> {\n await withDocumentMoneyTransaction(input.db, async (tx) =>\n input.withDatabase(tx, async (invoice) => {\n const candidateTenantId = getTenantId() ?? invoice.tenantId ?? null;\n const settings = await lockRuntimeSettings(tx, candidateTenantId);\n const persisted = invoice.id ? await findInvoice(tx, invoice.id) : null;\n if (!persisted) return;\n if (candidateTenantId && persisted.tenantId !== candidateTenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Invoice tenant does not match its active money barrier',\n );\n if (isFrozen(persisted))\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Cannot delete a frozen invoice',\n );\n assertTenantId(persisted.tenantId);\n const persistedId = requireId(persisted.id, 'Invoice');\n await lockAndReloadInvoice(tx, persistedId, persisted.tenantId);\n assertInvoiceSourceDelete(\n await (\n await InvoiceLineItemCollection.create({ db: tx })\n ).findByInvoice(persistedId),\n );\n const documentTaxes = await DocumentTaxCollection.create({ db: tx });\n await documentTaxes.replaceDerivedForDocument(DOCUMENT_MONEY_WRITE, {\n tenantId: persisted.tenantId,\n documentType: 'invoice',\n documentId: persistedId,\n rows: [],\n });\n await invoice.deleteDerived(DOCUMENT_MONEY_WRITE, options);\n }),\n );\n}\n","import { ISO_4217_MINOR_UNITS } from '@happyvertical/smrt-ui/currency';\nimport type {\n HistoricalSalesMoneyFacts,\n HistoricalSalesRetention,\n HistoricalSalesSource,\n} from './historical-sales-types.js';\nimport { MoneyValidationError } from './money.js';\n\nfunction invalid(): never {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Historical sales require exact supported source facts and explicit unknown values',\n );\n}\nexport function historicalText(value: unknown, max = 2000): string {\n if (\n typeof value !== 'string' ||\n !value.trim() ||\n value.trim() !== value ||\n value.length > max\n )\n invalid();\n return value;\n}\nexport function historicalCalendarDate(value: unknown): string {\n const date = historicalText(value);\n if (\n !/^\\d{4}-\\d{2}-\\d{2}$/.test(date) ||\n !Number.isFinite(Date.parse(date)) ||\n new Date(date).toISOString().slice(0, 10) !== date\n )\n invalid();\n return date;\n}\nfunction record(value: unknown): Record<string, unknown> {\n if (!value || typeof value !== 'object' || Array.isArray(value)) invalid();\n return value as Record<string, unknown>;\n}\nfunction fields(input: unknown, allowed: readonly string[]) {\n const value = record(input);\n if (Object.keys(value).some((key) => !allowed.includes(key))) invalid();\n return value;\n}\nfunction integer(value: unknown): number {\n if (typeof value !== 'number' || !Number.isSafeInteger(value)) invalid();\n return value;\n}\nfunction nullableInteger(value: unknown): number | null {\n return value === null ? null : integer(value);\n}\nfunction nullableText(value: unknown): string | null {\n return value === null ? null : historicalText(value);\n}\nfunction sum(values: number[]): number {\n const result = values.reduce((total, value) => total + BigInt(value), 0n);\n if (\n result > BigInt(Number.MAX_SAFE_INTEGER) ||\n result < BigInt(Number.MIN_SAFE_INTEGER)\n )\n invalid();\n return Number(result);\n}\nfunction sameAmount(net: number, tax: number, gross: number) {\n if (sum([net, tax]) !== gross) invalid();\n}\nexport function historicalSalesMagnitudes(facts: HistoricalSalesMoneyFacts) {\n const sign =\n facts.kind === 'credit_note' && facts.polarity === 'signed' ? -1 : 1;\n const result = {\n netMinor: facts.netMinor * sign,\n taxMinor: facts.taxMinor * sign,\n grossMinor: facts.grossMinor * sign,\n };\n if (Object.values(result).some((value) => value < 0)) invalid();\n return result;\n}\nexport function validateHistoricalSalesSource(\n input: HistoricalSalesSource,\n): HistoricalSalesSource {\n const value = fields(input, [\n 'namespace',\n 'key',\n 'version',\n 'issuedOn',\n 'dueOn',\n 'timeZone',\n 'approval',\n 'retention',\n ]);\n const retention =\n value.retention == null\n ? value.retention\n : (() => {\n const r = fields(value.retention, [\n 'retainedMinor',\n 'creditPolicy',\n 'policyRef',\n 'effectiveOn',\n 'evidenceRef',\n 'originalActorRef',\n 'releases',\n ]);\n if (\n r.creditPolicy !== 'retained-first' ||\n !Array.isArray(r.releases) ||\n r.releases.length > 10000\n )\n invalid();\n const retainedMinor = integer(r.retainedMinor);\n if (retainedMinor <= 0) invalid();\n const effectiveOn = historicalCalendarDate(r.effectiveOn);\n const keys = new Set<string>();\n const releases = r.releases.map(\n (input): HistoricalSalesRetention['releases'][number] => {\n const event = fields(input, [\n 'sourceKey',\n 'amountMinor',\n 'effectiveOn',\n 'evidenceRef',\n 'originalActorRef',\n 'sameDayCreditOrder',\n ]);\n const sourceKey = historicalText(event.sourceKey),\n amountMinor = integer(event.amountMinor),\n on = historicalCalendarDate(event.effectiveOn);\n if (keys.has(sourceKey) || amountMinor <= 0 || on < effectiveOn)\n invalid();\n keys.add(sourceKey);\n return {\n ...(event.sameDayCreditOrder === undefined\n ? {}\n : {\n sameDayCreditOrder:\n event.sameDayCreditOrder === null ||\n event.sameDayCreditOrder === 'credit-before-release' ||\n event.sameDayCreditOrder === 'release-before-credit'\n ? event.sameDayCreditOrder\n : invalid(),\n }),\n sourceKey,\n amountMinor,\n effectiveOn: on,\n evidenceRef: historicalText(event.evidenceRef),\n originalActorRef: nullableText(event.originalActorRef),\n };\n },\n );\n if (sum(releases.map((event) => event.amountMinor)) > retainedMinor)\n invalid();\n return {\n retainedMinor,\n creditPolicy: 'retained-first' as const,\n policyRef: historicalText(r.policyRef),\n effectiveOn,\n evidenceRef: historicalText(r.evidenceRef),\n originalActorRef: nullableText(r.originalActorRef),\n releases,\n };\n })();\n if (\n retention &&\n retention.effectiveOn !== historicalCalendarDate(value.issuedOn)\n )\n invalid();\n const timeZone = historicalText(value.timeZone);\n try {\n new Intl.DateTimeFormat('en', { timeZone }).format(0);\n } catch {\n invalid();\n }\n const approval =\n value.approval === null\n ? null\n : fields(value.approval, ['actorRef', 'approvedOn']);\n return {\n ...(retention === undefined ? {} : { retention }),\n namespace: historicalText(value.namespace),\n key: historicalText(value.key),\n version: historicalText(value.version),\n issuedOn: historicalCalendarDate(value.issuedOn),\n dueOn: value.dueOn === null ? null : historicalCalendarDate(value.dueOn),\n timeZone,\n approval:\n approval === null\n ? null\n : {\n actorRef: historicalText(approval.actorRef),\n approvedOn:\n approval.approvedOn === null\n ? null\n : historicalCalendarDate(approval.approvedOn),\n },\n };\n}\n/** Validate original facts without a tax calculator, source normalization, or implicit polarity. */\nexport function validateHistoricalSalesMoneyFacts(\n input: HistoricalSalesMoneyFacts,\n): HistoricalSalesMoneyFacts {\n const value = fields(input, [\n 'currency',\n 'kind',\n 'polarity',\n 'netMinor',\n 'taxMinor',\n 'grossMinor',\n 'taxComponents',\n 'detail',\n ]);\n const currency = historicalText(value.currency);\n if (\n !ISO_4217_MINOR_UNITS.has(currency) ||\n ISO_4217_MINOR_UNITS.get(currency) === null ||\n !['invoice', 'credit_note'].includes(value.kind as string) ||\n !['signed', 'magnitudes'].includes(value.polarity as string)\n )\n invalid();\n const netMinor = integer(value.netMinor),\n taxMinor = integer(value.taxMinor),\n grossMinor = integer(value.grossMinor);\n sameAmount(netMinor, taxMinor, grossMinor);\n const seenComponents = new Set<string>();\n const taxComponents =\n value.taxComponents === null\n ? null\n : (() => {\n if (\n !Array.isArray(value.taxComponents) ||\n value.taxComponents.length > 1000\n )\n invalid();\n return value.taxComponents.map((inputComponent) => {\n const component = fields(inputComponent, [\n 'sourceKey',\n 'label',\n 'amountMinor',\n 'classification',\n 'taxCodeId',\n 'taxComponentId',\n 'taxableMinor',\n 'netMinor',\n 'liabilityAccountId',\n ]),\n sourceKey = historicalText(component.sourceKey);\n if (seenComponents.has(sourceKey)) invalid();\n seenComponents.add(sourceKey);\n if (\n component.classification !== null &&\n !['taxable', 'zero-rated', 'exempt', 'out-of-scope'].includes(\n component.classification as string,\n )\n )\n invalid();\n const classification = component.classification as\n | 'taxable'\n | 'zero-rated'\n | 'exempt'\n | 'out-of-scope'\n | null;\n const amountMinor = integer(component.amountMinor),\n taxableMinor = nullableInteger(component.taxableMinor);\n if (\n classification !== null &&\n classification !== 'taxable' &&\n amountMinor !== 0\n )\n invalid();\n return {\n sourceKey,\n label: historicalText(component.label),\n amountMinor,\n classification,\n taxCodeId: nullableText(component.taxCodeId),\n taxComponentId: nullableText(component.taxComponentId),\n taxableMinor,\n netMinor: nullableInteger(component.netMinor),\n liabilityAccountId: nullableText(component.liabilityAccountId),\n };\n });\n })();\n if (\n taxComponents !== null &&\n sum(taxComponents.map((component) => component.amountMinor)) !== taxMinor\n )\n invalid();\n const detailInput = fields(\n value.detail,\n record(value.detail).kind === 'header-only' ? ['kind'] : ['kind', 'lines'],\n );\n let detail: HistoricalSalesMoneyFacts['detail'];\n if (detailInput.kind === 'header-only') detail = { kind: 'header-only' };\n else if (detailInput.kind === 'lines') {\n if (\n !Array.isArray(detailInput.lines) ||\n !detailInput.lines.length ||\n detailInput.lines.length > 100000\n )\n invalid();\n const seen = new Set<string>();\n const lines = detailInput.lines.map((inputLine) => {\n const line = fields(inputLine, [\n 'sourceKey',\n 'creditedLineId',\n 'description',\n 'netMinor',\n 'taxMinor',\n 'grossMinor',\n 'quantityText',\n 'unitRateText',\n 'sourceFields',\n ]),\n sourceKey = historicalText(line.sourceKey);\n if (seen.has(sourceKey)) invalid();\n seen.add(sourceKey);\n const sourceFields = record(line.sourceFields);\n if (\n Object.keys(sourceFields).length > 100 ||\n Object.entries(sourceFields).some(\n ([key, cell]) =>\n key.length > 200 || typeof cell !== 'string' || cell.length > 10000,\n )\n )\n invalid();\n const netMinor = integer(line.netMinor),\n taxMinor = nullableInteger(line.taxMinor),\n grossMinor = nullableInteger(line.grossMinor);\n if (taxMinor !== null && grossMinor !== null)\n sameAmount(netMinor, taxMinor, grossMinor);\n if (\n typeof line.description !== 'string' ||\n line.description.length > 10000\n )\n invalid();\n return {\n sourceKey,\n description: line.description,\n creditedLineId: nullableText(line.creditedLineId),\n netMinor,\n taxMinor,\n grossMinor,\n quantityText: nullableText(line.quantityText),\n unitRateText: nullableText(line.unitRateText),\n sourceFields: Object.fromEntries(\n Object.entries(sourceFields),\n ) as Record<string, string>,\n };\n });\n if (\n sum(lines.map((line) => line.netMinor)) !== netMinor ||\n (lines.every((line) => line.taxMinor !== null) &&\n sum(lines.map((line) => line.taxMinor as number)) !== taxMinor) ||\n (lines.every((line) => line.grossMinor !== null) &&\n sum(lines.map((line) => line.grossMinor as number)) !== grossMinor)\n )\n invalid();\n detail = { kind: 'lines', lines };\n } else invalid();\n const result: HistoricalSalesMoneyFacts = {\n currency,\n kind: value.kind as HistoricalSalesMoneyFacts['kind'],\n polarity: value.polarity as HistoricalSalesMoneyFacts['polarity'],\n netMinor,\n taxMinor,\n grossMinor,\n taxComponents,\n detail,\n };\n historicalSalesMagnitudes(result);\n return result;\n}\n","/** Exact original issued sales authority. Host review is distinct from original issue/approval. */\nimport { createHash } from 'node:crypto';\nimport { ObjectRegistry } from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { CustomerCollection } from './collections/CustomerCollection.js';\nimport { HistoricalSalesLineCollection } from './collections/HistoricalSalesLineCollection.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { InvoiceLineItemCollection } from './collections/InvoiceLineItemCollection.js';\nimport { TaxCodeCollection } from './collections/TaxCodeCollection.js';\nimport { TaxComponentCollection } from './collections/TaxComponentCollection.js';\nimport { assertContractScheduleInvoiceFreeze } from './contract-billing-schedule.js';\nimport {\n lockAndReloadInvoice,\n refreshIssuedCreditBalance,\n} from './document-money.js';\nimport { DOCUMENT_MONEY_WRITE } from './document-money-internal.js';\nimport { withHistoricalCommerceWrite } from './historical-commerce-internal.js';\nimport type {\n HistoricalSalesMoneySnapshot,\n RecordHistoricalSalesMoneyInput,\n} from './historical-sales-types.js';\nimport {\n historicalSalesMagnitudes,\n historicalText,\n validateHistoricalSalesMoneyFacts,\n validateHistoricalSalesSource,\n} from './historical-sales-validation.js';\nimport { HistoricalSalesLine } from './models/HistoricalSalesLine.js';\nimport type { Invoice } from './models/Invoice.js';\nimport { MoneyValidationError } from './money.js';\nimport {\n type InvoiceRetentionState,\n parseInvoiceRetention,\n} from './retention-money.js';\nimport { withTenantMoneyRuntime } from './tenant-money-barrier.js';\nimport { InvoiceStatus } from './types/index.js';\n\nexport type * from './historical-sales-types.js';\nexport { validateHistoricalSalesMoneyFacts } from './historical-sales-validation.js';\n\nfunction hash(value: unknown): string {\n return createHash('sha256').update(JSON.stringify(value)).digest('hex');\n}\nfunction identity(value: unknown): string {\n const h = hash(value);\n return `${h.slice(0, 8)}-${h.slice(8, 12)}-5${h.slice(13, 16)}-a${h.slice(17, 20)}-${h.slice(20, 32)}`;\n}\nfunction invalid(message: string): never {\n throw new MoneyValidationError('INVALID_MONEY_INPUT', message);\n}\nexport function historicalSalesSourceHash(\n source: HistoricalSalesMoneySnapshot['source'],\n): string {\n return hash([source.namespace, source.key]);\n}\nfunction snapshotIdentity(\n invoice: Invoice,\n snapshot: HistoricalSalesMoneySnapshot,\n) {\n return identity({\n tenantId: invoice.tenantId,\n invoiceId: invoice.id,\n customerId: invoice.customerId,\n invoiceNumber: invoice.invoiceNumber,\n snapshot,\n });\n}\nfunction originalRetentionState(\n snapshot: HistoricalSalesMoneySnapshot,\n): InvoiceRetentionState | null {\n const r = snapshot.source.retention;\n if (!r) return null;\n return {\n version: 1,\n currency: snapshot.facts.currency,\n creditPolicy: r.creditPolicy,\n retainedMinor: r.retainedMinor,\n requestId: `historical-retention:${snapshot.requestId}`,\n policyVersion: r.policyRef,\n evidenceRef: r.evidenceRef,\n actorId: snapshot.review.actorId,\n reason: 'Original source retention',\n recordedAt: snapshot.recordedAt,\n effectiveOn: r.effectiveOn,\n releases: r.releases.map((event) => ({\n requestId: `historical-release:${event.sourceKey}`,\n policyVersion: r.policyRef,\n evidenceRef: event.evidenceRef,\n actorId: snapshot.review.actorId,\n reason: 'Original source release',\n recordedAt: snapshot.recordedAt,\n effectiveOn: event.effectiveOn,\n ...(event.sameDayCreditOrder === undefined\n ? {}\n : { sameDayCreditOrder: event.sameDayCreditOrder }),\n amountMinor: event.amountMinor,\n })),\n };\n}\n/** Stored source header evidence; never recalculates tax or source line allocations. */\nexport function getHistoricalSalesMoney(\n invoice: Invoice,\n): HistoricalSalesMoneySnapshot | null {\n if (\n invoice.moneyAuthority === 'calculated' &&\n !invoice.historicalSalesMoneyJson &&\n invoice.historicalSourceKeyHash === null\n )\n return null;\n if (\n invoice.moneyAuthority !== 'historical' ||\n !invoice.historicalSalesMoneyJson\n )\n invalid('Historical invoice authority is incomplete');\n const value = JSON.parse(\n invoice.historicalSalesMoneyJson,\n ) as HistoricalSalesMoneySnapshot;\n if (value.version !== 1)\n invalid('Historical invoice snapshot version is unsupported');\n const snapshot: HistoricalSalesMoneySnapshot = {\n version: 1,\n requestId: historicalText(value.requestId),\n evidenceRef: historicalText(value.evidenceRef),\n review: {\n actorId: historicalText(value.review?.actorId),\n policyRef: historicalText(value.review?.policyRef),\n },\n recordedAt: historicalText(value.recordedAt),\n source: validateHistoricalSalesSource(value.source),\n facts: validateHistoricalSalesMoneyFacts(value.facts),\n creditedSource:\n value.creditedSource === null\n ? null\n : {\n invoiceId: historicalText(value.creditedSource?.invoiceId),\n moneySnapshotId: historicalText(\n value.creditedSource?.moneySnapshotId,\n ),\n },\n };\n const amounts = historicalSalesMagnitudes(snapshot.facts);\n if (\n invoice.currency !== snapshot.facts.currency ||\n invoice.kind !== snapshot.facts.kind ||\n invoice.subtotal !== amounts.netMinor ||\n invoice.taxAmount !== amounts.taxMinor ||\n invoice.totalAmount !== amounts.grossMinor ||\n invoice.discountTotal !== 0 ||\n invoice.providerTaxAmount !== 0 ||\n invoice.historicalSourceKeyHash !==\n historicalSalesSourceHash(snapshot.source) ||\n invoice.moneySnapshotId !== snapshotIdentity(invoice, snapshot)\n )\n invalid(\n 'Historical invoice no longer matches its immutable original monetary authority',\n );\n if (\n invoice.issueDate.toISOString().slice(0, 10) !== snapshot.source.issuedOn ||\n (invoice.dueDate?.toISOString().slice(0, 10) ?? null) !==\n snapshot.source.dueOn\n )\n invalid('Historical source calendar dates changed');\n if (\n snapshot.facts.kind === 'credit_note'\n ? invoice.creditedInvoiceId !== snapshot.creditedSource?.invoiceId\n : snapshot.creditedSource !== null\n )\n invalid('Historical credit source lineage changed');\n const expectedRetention = originalRetentionState(snapshot),\n retained = parseInvoiceRetention(invoice.retentionStateJson);\n if (expectedRetention) {\n if (!retained) invalid('Original retention authority is missing');\n for (const key of Object.keys(\n expectedRetention,\n ) as (keyof InvoiceRetentionState)[]) {\n if (key === 'releases') continue;\n if (retained[key] !== expectedRetention[key])\n invalid('Original retention source projection changed');\n }\n if (\n JSON.stringify(\n retained.releases.slice(0, expectedRetention.releases.length),\n ) !== JSON.stringify(expectedRetention.releases)\n )\n invalid('Original retention release projection changed');\n } else if (retained)\n invalid('Historical invoice has no original retention authority');\n return structuredClone(snapshot);\n}\nfunction lineIdentity(invoiceId: string, sourceKey: string) {\n return identity({ kind: 'historical-sales-line:v1', invoiceId, sourceKey });\n}\n/** Read actual immutable source children for typed consumer crosswalks. */\nexport async function readHistoricalSalesLines(\n db: DatabaseInterface,\n invoice: Invoice,\n) {\n const snapshot = getHistoricalSalesMoney(invoice);\n if (!snapshot) return [];\n const lines = await (await HistoricalSalesLineCollection.create({ db })).list(\n {\n where: { tenantId: invoice.tenantId, invoiceId: invoice.id },\n limit: 100001,\n },\n );\n const expected =\n snapshot.facts.detail.kind === 'lines' ? snapshot.facts.detail.lines : [];\n if (lines.length !== expected.length)\n invalid('Historical invoice source child identities are incomplete');\n const byKey = new Map(lines.map((line) => [line.sourceKey, line]));\n if (byKey.size !== lines.length)\n invalid('Historical source child identity is ambiguous');\n return expected.map((source) => {\n const line = byKey.get(source.sourceKey);\n if (\n !line ||\n line.id !== lineIdentity(String(invoice.id), source.sourceKey) ||\n line.sourceKeyHash !== hash(source.sourceKey) ||\n line.moneySnapshotId !== invoice.moneySnapshotId ||\n line.description !== source.description ||\n line.creditedLineId !== source.creditedLineId ||\n line.netMinor !== source.netMinor ||\n line.taxMinor !== source.taxMinor ||\n line.grossMinor !== source.grossMinor ||\n line.quantityText !== source.quantityText ||\n line.unitRateText !== source.unitRateText ||\n line.sourceFieldsJson !== JSON.stringify(source.sourceFields)\n )\n invalid(\n 'Historical invoice source child no longer matches its original facts',\n );\n return line;\n });\n}\nasync function assertSourceLineCapacity(\n db: DatabaseInterface,\n invoices: InvoiceCollection,\n source: Invoice | null,\n snapshot: HistoricalSalesMoneySnapshot,\n) {\n if (snapshot.facts.detail.kind !== 'lines') return;\n const linked = snapshot.facts.detail.lines.filter(\n (line) => line.creditedLineId !== null,\n );\n if (!linked.length) return;\n if (!source || snapshot.facts.kind !== 'credit_note')\n invalid(\n 'Only an exact historical credit may link original historical lines',\n );\n const sourceLines = new Map(\n (await readHistoricalSalesLines(db, source)).map((line) => [line.id, line]),\n );\n const collection = await HistoricalSalesLineCollection.create({ db });\n const requestSign = snapshot.facts.polarity === 'signed' ? -1 : 1;\n const pending = new Map<\n string,\n { netMinor: bigint; taxMinor: bigint; grossMinor: bigint }\n >();\n for (const request of linked) {\n const original = sourceLines.get(request.creditedLineId ?? '');\n if (!original)\n invalid(\n 'Historical credit line does not belong to its exact original source invoice',\n );\n const inRequest = pending.get(String(original.id)) ?? {\n netMinor: 0n,\n taxMinor: 0n,\n grossMinor: 0n,\n };\n const previous = await collection.list({\n where: { tenantId: source.tenantId, creditedLineId: original.id },\n limit: 100001,\n });\n if (previous.length > 100000)\n invalid(\n 'Historical source line capacity exceeds the bounded credit scope',\n );\n const consumed = { netMinor: 0n, taxMinor: 0n, grossMinor: 0n };\n for (const line of previous) {\n const parent = await invoices.get(\n { id: line.invoiceId, tenantId: source.tenantId },\n { cache: false },\n );\n if (\n !parent ||\n parent.kind !== 'credit_note' ||\n parent.creditedInvoiceId !== source.id\n )\n invalid('Historical source line has invalid credit lineage');\n if (\n parent.status === InvoiceStatus.DRAFT ||\n parent.status === InvoiceStatus.CANCELLED\n )\n continue;\n const prior = getHistoricalSalesMoney(parent);\n if (!prior)\n invalid('Historical source line has no original monetary authority');\n const sign = prior.facts.polarity === 'signed' ? -1 : 1;\n for (const field of ['netMinor', 'taxMinor', 'grossMinor'] as const) {\n if (line[field] !== null)\n consumed[field] += BigInt(line[field]) * BigInt(sign);\n }\n }\n for (const field of ['netMinor', 'taxMinor', 'grossMinor'] as const) {\n const originalAmount = original[field],\n requestedAmount = request[field];\n if (originalAmount === null || requestedAmount === null) continue;\n const magnitude = originalAmount < 0 ? -1n : 1n;\n const originalMagnitude = BigInt(originalAmount) * magnitude;\n const requestMagnitude =\n BigInt(requestedAmount) * BigInt(requestSign) * magnitude;\n if (\n requestMagnitude < 0n ||\n (consumed[field] + inRequest[field]) * magnitude + requestMagnitude >\n originalMagnitude\n )\n invalid(\n 'Historical credit exceeds an exact original source line monetary capacity',\n );\n }\n for (const field of ['netMinor', 'taxMinor', 'grossMinor'] as const)\n if (request[field] !== null)\n inRequest[field] += BigInt(request[field]) * BigInt(requestSign);\n pending.set(String(original.id), inRequest);\n }\n}\nfunction stableRequest(snapshot: HistoricalSalesMoneySnapshot) {\n return {\n source: snapshot.source,\n facts: snapshot.facts,\n evidenceRef: snapshot.evidenceRef,\n policyRef: snapshot.review.policyRef,\n creditedSource: snapshot.creditedSource,\n };\n}\n/** Host-authorized import only. Source issue is retained evidence, never a new send/post. */\nexport async function recordHistoricalSalesMoney(\n input: RecordHistoricalSalesMoneyInput,\n) {\n if (getTenantId() !== input.tenantId)\n invalid('Historical sales require the explicit active tenant');\n const facts = validateHistoricalSalesMoneyFacts(input.facts),\n source = validateHistoricalSalesSource(input.source);\n const snapshot: HistoricalSalesMoneySnapshot = {\n version: 1,\n requestId: historicalText(input.requestId),\n evidenceRef: historicalText(input.evidenceRef),\n review: {\n actorId: historicalText(input.review?.actorId),\n policyRef: historicalText(input.review?.policyRef),\n },\n recordedAt: new Date().toISOString(),\n source,\n facts,\n creditedSource: input.creditedSource\n ? {\n invoiceId: historicalText(input.creditedSource.invoiceId),\n moneySnapshotId: historicalText(input.creditedSource.moneySnapshotId),\n }\n : null,\n };\n if ((facts.kind === 'credit_note') !== Boolean(snapshot.creditedSource))\n invalid(\n 'Historical credits require the exact original issued invoice snapshot',\n );\n return withTenantMoneyRuntime(input.db, input.tenantId, async ({ db }) => {\n const { invoice } = await lockAndReloadInvoice(\n db,\n historicalText(input.invoiceId),\n input.tenantId,\n );\n if (invoice.tenantId !== input.tenantId)\n invalid('Historical invoice is not owned by the active tenant');\n const existing = getHistoricalSalesMoney(invoice);\n if (existing) {\n if (hash(stableRequest(existing)) !== hash(stableRequest(snapshot)))\n invalid(\n 'Historical invoice retry differs from its original facts or reviewed policy',\n );\n return {\n snapshot: existing,\n invoiceId: String(invoice.id),\n moneySnapshotId: invoice.moneySnapshotId,\n lines: (await readHistoricalSalesLines(db, invoice)).map((line) => ({\n sourceKey: line.sourceKey,\n lineId: String(line.id),\n })),\n };\n }\n if (\n invoice.status !== InvoiceStatus.DRAFT ||\n invoice.moneySnapshotId ||\n invoice.arJournalId ||\n invoice.amountPaid ||\n invoice.creditedAmount ||\n invoice.retentionStateJson ||\n invoice.externalId ||\n invoice.externalProvider ||\n invoice.providerTaxAmount\n )\n invalid(\n 'Historical source money requires an empty unfrozen invoice without current financial effects',\n );\n if (invoice.kind !== facts.kind || invoice.currency !== facts.currency)\n invalid('Historical source kind/currency must match its draft invoice');\n const customer = invoice.customerId\n ? await (await CustomerCollection.create({ db })).get(\n { id: invoice.customerId, tenantId: input.tenantId },\n { cache: false },\n )\n : null;\n if (!customer)\n invalid('Historical sales require a customer owned by the active tenant');\n if (\n (\n await (\n await InvoiceLineItemCollection.create({ db })\n ).findByInvoice(String(invoice.id))\n ).length\n )\n invalid(\n 'Historical source money cannot mix with calculated invoice lines',\n );\n const invoices = await InvoiceCollection.create({ db });\n const duplicate = (\n await invoices.list({\n where: {\n tenantId: input.tenantId,\n historicalSourceKeyHash: historicalSalesSourceHash(source),\n },\n limit: 1,\n })\n )[0];\n if (duplicate && duplicate.id !== invoice.id)\n invalid(\n 'Historical source identity already belongs to another canonical invoice',\n );\n for (const component of facts.taxComponents ?? []) {\n const code = component.taxCodeId\n ? await (await TaxCodeCollection.create({ db })).get(\n { id: component.taxCodeId, tenantId: input.tenantId },\n { cache: false },\n )\n : null;\n if (\n component.taxCodeId &&\n (!code ||\n (component.classification !== null &&\n code.kind !== component.classification))\n )\n invalid(\n 'Historical source tax code is not owned by its tenant with the reviewed classification',\n );\n if (component.taxComponentId) {\n const tax = await (await TaxComponentCollection.create({ db })).get(\n { id: component.taxComponentId, tenantId: input.tenantId },\n { cache: false },\n );\n if (\n !tax ||\n (code &&\n !code.components.some((part) => part.taxComponentId === tax.id))\n )\n invalid(\n 'Historical source tax component is not owned by its declared tenant/code',\n );\n }\n if (component.liabilityAccountId) {\n const { AccountCollection } = await import(\n '@happyvertical/smrt-ledgers'\n );\n const account = await (await AccountCollection.create({ db })).get(\n { id: component.liabilityAccountId, tenantId: input.tenantId },\n { cache: false },\n );\n if (!account || account.type !== 'liability')\n invalid(\n 'Historical source tax liability account is not owned by its tenant with the required type',\n );\n }\n }\n let credited: Invoice | null = null;\n const amounts = historicalSalesMagnitudes(facts);\n if (snapshot.creditedSource) {\n credited = (\n await lockAndReloadInvoice(\n db,\n snapshot.creditedSource.invoiceId,\n input.tenantId,\n )\n ).invoice;\n if (\n credited.kind !== 'invoice' ||\n credited.tenantId !== input.tenantId ||\n credited.customerId !== invoice.customerId ||\n credited.currency !== invoice.currency ||\n !credited.moneySnapshotId ||\n credited.moneySnapshotId !== snapshot.creditedSource.moneySnapshotId ||\n credited.status === InvoiceStatus.DRAFT ||\n credited.status === InvoiceStatus.CANCELLED ||\n invoice.creditedInvoiceId !== credited.id\n )\n invalid(\n 'Historical credit original invoice/customer/currency/snapshot does not match',\n );\n const prior = (\n await invoices\n .list({\n where: { tenantId: input.tenantId, creditedInvoiceId: credited.id },\n limit: 100001,\n })\n .then((rows) => {\n if (rows.length > 100000)\n invalid(\n 'Historical credit scope exceeds its bounded source limit',\n );\n return rows;\n })\n ).filter(\n (row) =>\n row.id !== invoice.id &&\n row.status !== InvoiceStatus.DRAFT &&\n row.status !== InvoiceStatus.CANCELLED,\n );\n if (prior.length > 100000)\n invalid('Historical credit scope exceeds its bounded source limit');\n const retention = parseInvoiceRetention(credited.retentionStateJson);\n if (retention) {\n const credits = [\n ...prior.map((row) => ({\n on:\n getHistoricalSalesMoney(row)?.source.issuedOn ??\n row.issueDate.toISOString().slice(0, 10),\n amount: row.totalAmount,\n })),\n { on: source.issuedOn, amount: amounts.grossMinor },\n ];\n let remaining = retention.retainedMinor;\n const events = [\n ...credits.map((event) => ({\n ...event,\n kind: 'credit' as const,\n rank: 1,\n })),\n ...retention.releases.map((release) => {\n const on = release.effectiveOn ?? release.recordedAt.slice(0, 10);\n if (\n credits.some((event) => event.on === on) &&\n !release.sameDayCreditOrder\n )\n invalid(\n 'Same-day original retention release/credit requires explicit reviewed ordering',\n );\n return {\n on,\n amount: release.amountMinor,\n kind: 'release' as const,\n rank:\n release.sameDayCreditOrder === 'release-before-credit' ? 0 : 2,\n };\n }),\n ].sort(\n (left, right) =>\n left.on.localeCompare(right.on) || left.rank - right.rank,\n );\n for (const event of events) {\n if (event.kind === 'credit')\n remaining = Math.max(0, remaining - event.amount);\n else {\n if (event.amount > remaining)\n invalid(\n 'Original retention release exceeds retained-first capacity at its source date',\n );\n remaining -= event.amount;\n }\n }\n }\n for (const [field, value] of [\n ['subtotal', amounts.netMinor],\n ['taxAmount', amounts.taxMinor],\n ['totalAmount', amounts.grossMinor],\n ] as const) {\n const consumed = prior.reduce(\n (sum, row) => sum + BigInt(row[field]),\n 0n,\n );\n if (consumed + BigInt(value) > BigInt(credited[field]))\n invalid(\n 'Historical credit exceeds the remaining exact original net/tax/gross capacity',\n );\n }\n }\n await assertSourceLineCapacity(db, invoices, credited, snapshot);\n if (\n source.retention &&\n (facts.kind !== 'invoice' ||\n source.retention.retainedMinor > amounts.grossMinor)\n )\n invalid('Historical retention must match original invoice gross');\n const originalRetention = originalRetentionState(snapshot);\n invoice.retentionStateJson = originalRetention\n ? JSON.stringify(originalRetention)\n : '';\n invoice.moneyAuthority = 'historical';\n invoice.historicalSalesMoneyJson = JSON.stringify(snapshot);\n invoice.historicalSourceKeyHash = historicalSalesSourceHash(source);\n invoice.subtotal = amounts.netMinor;\n invoice.discountTotal = 0;\n invoice.taxAmount = amounts.taxMinor;\n invoice.providerTaxAmount = 0;\n invoice.totalAmount = amounts.grossMinor;\n invoice.issueDate = new Date(`${source.issuedOn}T00:00:00Z`);\n invoice.dueDate =\n source.dueOn === null ? null : new Date(`${source.dueOn}T00:00:00Z`);\n invoice.moneySnapshotId = snapshotIdentity(invoice, snapshot);\n invoice.status = InvoiceStatus.SENT;\n await assertContractScheduleInvoiceFreeze(db, invoice);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n if (facts.detail.kind === 'lines')\n for (const line of facts.detail.lines) {\n const child = await new (ObjectRegistry.resolveEffectiveConstructor(\n HistoricalSalesLine,\n ))({\n db,\n id: lineIdentity(String(invoice.id), line.sourceKey),\n tenantId: input.tenantId,\n invoiceId: String(invoice.id),\n ...line,\n sourceKeyHash: hash(line.sourceKey),\n moneySnapshotId: invoice.moneySnapshotId,\n }).initialize();\n await withHistoricalCommerceWrite(child, () => child.save());\n }\n if (credited) await refreshIssuedCreditBalance(db, credited);\n return {\n snapshot: structuredClone(snapshot),\n invoiceId: String(invoice.id),\n moneySnapshotId: invoice.moneySnapshotId,\n lines: (await readHistoricalSalesLines(db, invoice)).map((line) => ({\n sourceKey: line.sourceKey,\n lineId: String(line.id),\n })),\n };\n });\n}\n","/** Original header totals remain readable even when detailed tax classifications are unknown. */\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport type {\n DocumentTaxReportFact,\n DocumentTaxReportFactsOptions,\n} from './collections/DocumentTaxCollection.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { getHistoricalSalesMoney } from './historical-sales-money.js';\nimport { historicalSalesMagnitudes } from './historical-sales-validation.js';\nimport type { Invoice } from './models/Invoice.js';\nimport { withTenantMoneyRuntime } from './tenant-money-barrier.js';\n\nexport interface HistoricalSalesHeaderReportFact {\n invoiceId: string;\n moneySnapshotId: string;\n customerId: string;\n invoiceNumber: string;\n kind: Invoice['kind'];\n issueDate: string;\n currency: string;\n netMinor: number;\n taxMinor: number;\n grossMinor: number;\n taxBreakdownKnown: boolean;\n}\nexport class HistoricalSalesTaxClassificationRequired extends Error {\n readonly code = 'HISTORICAL_SALES_TAX_CLASSIFICATION_REQUIRED';\n constructor(readonly invoiceId: string) {\n super(\n `Historical invoice ${invoiceId} retains exact header totals, but this tax breakdown requires explicit reviewed source classification, tax identity and taxable basis. Read the historical header facts or resolve the source assessment; never infer a rate or line allocation.`,\n );\n this.name = 'HistoricalSalesTaxClassificationRequired';\n }\n}\nfunction classified(invoice: Invoice) {\n const snapshot = getHistoricalSalesMoney(invoice);\n if (!snapshot) return false;\n const components = snapshot.facts.taxComponents;\n return Boolean(\n components?.length &&\n components.every(\n (component) =>\n component.classification !== null &&\n component.taxCodeId &&\n component.taxableMinor !== null &&\n component.netMinor !== null &&\n (component.classification !== 'taxable' || component.taxComponentId),\n ),\n );\n}\n/** @internal Project explicit source detail only; a known header is not a component classification. */\nexport function historicalSalesTaxReportFacts(\n invoice: Invoice,\n): DocumentTaxReportFact[] {\n const snapshot = getHistoricalSalesMoney(invoice);\n if (!snapshot) return [];\n if (snapshot.facts.netMinor === 0 && snapshot.facts.taxMinor === 0) return [];\n if (!classified(invoice))\n throw new HistoricalSalesTaxClassificationRequired(String(invoice.id));\n const sign =\n snapshot.facts.kind === 'credit_note' &&\n snapshot.facts.polarity === 'signed'\n ? -1\n : 1;\n return (snapshot.facts.taxComponents ?? []).map((component) => {\n if (\n component.classification === null ||\n !component.taxCodeId ||\n component.taxableMinor === null\n )\n throw new HistoricalSalesTaxClassificationRequired(String(invoice.id));\n return {\n tenantId: String(invoice.tenantId),\n documentType: invoice.kind,\n documentId: String(invoice.id),\n documentNumber: invoice.invoiceNumber,\n partyId: invoice.customerId,\n documentKind: invoice.kind,\n issueDate: invoice.issueDate.toISOString(),\n status: invoice.status,\n direction: 'sales',\n currency: invoice.currency,\n taxCodeId: component.taxCodeId,\n taxCodeKind: component.classification,\n taxableMinor: component.taxableMinor * sign,\n ...(component.netMinor === null\n ? {}\n : { netMinor: component.netMinor * sign }),\n taxMinor: component.amountMinor * sign,\n recoverableMinor: 0,\n taxComponentId: component.taxComponentId,\n componentName: component.label,\n liabilityAccountId: component.liabilityAccountId,\n commitmentId: null,\n commitmentLineId: null,\n };\n });\n}\n/** @internal Invoice source rows for the same public report scope; never silently truncate. */\nexport async function historicalSalesReportInvoices(\n db: DatabaseInterface,\n options: DocumentTaxReportFactsOptions,\n) {\n const rows = await (await InvoiceCollection.create({ db })).list({\n where: {\n tenantId: options.tenantId,\n moneyAuthority: 'historical',\n 'issueDate >=': options.from.toISOString(),\n 'issueDate <=': options.to.toISOString(),\n 'status not in': ['draft', 'cancelled'],\n ...(options.currency ? { currency: options.currency } : {}),\n },\n limit: 100001,\n });\n if (rows.length > 100000)\n throw new Error(\n 'Historical sales report scope exceeds the bounded document limit; select a smaller scope',\n );\n return rows;\n}\n/** Exact generic header reporting does not require a fabricated tax breakdown. */\nexport async function listHistoricalSalesHeaderFacts(\n input: DocumentTaxReportFactsOptions & { db: DatabaseInterface },\n): Promise<HistoricalSalesHeaderReportFact[]> {\n if (getTenantId() !== input.tenantId)\n throw new Error(\n 'Historical sales reporting requires the explicit active tenant',\n );\n if (\n !(input.from instanceof Date) ||\n !(input.to instanceof Date) ||\n !Number.isFinite(input.from.getTime()) ||\n !Number.isFinite(input.to.getTime()) ||\n input.from > input.to\n )\n throw new Error(\n 'Historical sales report requires an explicit valid period',\n );\n return withTenantMoneyRuntime(input.db, input.tenantId, async ({ db }) =>\n (await historicalSalesReportInvoices(db, input)).map((invoice) => {\n const snapshot = getHistoricalSalesMoney(invoice);\n if (!snapshot)\n throw new Error('Historical source monetary authority is unavailable');\n return {\n invoiceId: String(invoice.id),\n moneySnapshotId: invoice.moneySnapshotId,\n customerId: invoice.customerId,\n invoiceNumber: invoice.invoiceNumber,\n kind: invoice.kind,\n issueDate: invoice.issueDate.toISOString(),\n currency: invoice.currency,\n ...historicalSalesMagnitudes(snapshot.facts),\n taxBreakdownKnown: classified(invoice),\n };\n }),\n );\n}\n","/** Persisted per-code/component tax totals for a commerce document. */\n\nimport {\n field,\n SmrtObject,\n type SmrtObjectOptions,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport {\n DOCUMENT_MONEY_WRITE,\n type DocumentMoneyWriteCapability,\n} from '../document-money-internal.js';\nimport { MoneyValidationError, type TaxCodeKind } from '../money.js';\n\nexport type CommerceDocumentType = 'invoice' | 'contract' | 'credit_note';\n\nexport interface DocumentTaxOptions extends SmrtObjectOptions {\n tenantId?: string;\n documentType?: CommerceDocumentType;\n documentId?: string;\n currency?: string;\n taxCodeId?: string | null;\n taxCodeCode?: string | null;\n kind?: TaxCodeKind | null;\n taxComponentId?: string | null;\n taxComponentName?: string | null;\n percent?: string | null;\n liabilityAccountId?: string | null;\n taxableMinor?: number;\n netMinor?: number | null;\n taxMinor?: number;\n recoverableMinor?: number;\n}\n@TenantScoped({ mode: 'required' })\n@smrt({\n tableName: 'document_taxes',\n conflictColumns: [\n 'document_type',\n 'document_id',\n 'tax_code_id',\n 'tax_component_id',\n ],\n api: { include: ['list', 'get'] },\n mcp: { include: ['list', 'get'] },\n cli: { include: ['list', 'get'] },\n})\nexport class DocumentTax extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n @tenantId()\n tenantId = '';\n\n @field({ required: true })\n documentType: CommerceDocumentType = 'invoice';\n\n @field({ required: true, requiredFor: 'identity' })\n documentId = '';\n\n @field({ required: true })\n currency = 'USD';\n\n @field({ nullable: true })\n taxCodeId: string | null = null;\n\n @field({ nullable: true })\n taxCodeCode: string | null = null;\n\n @field({ nullable: true })\n kind: TaxCodeKind | null = null;\n\n @field({ nullable: true })\n taxComponentId: string | null = null;\n\n @field({ nullable: true })\n taxComponentName: string | null = null;\n\n @field({ type: 'decimal', nullable: true })\n percent: string | null = null;\n\n @field({ nullable: true })\n liabilityAccountId: string | null = null;\n\n @field({\n description:\n 'Actual component taxable basis, including prior compound taxes.',\n ui: { format: 'money' },\n })\n taxableMinor = 0;\n\n @field({\n type: 'integer',\n nullable: true,\n description: 'Net basis when different from the component taxable basis.',\n ui: { format: 'money' },\n })\n netMinor: number | null = null;\n\n @field({\n description: 'Tax total in integer minor units.',\n ui: { format: 'money' },\n })\n taxMinor = 0;\n\n @field({\n description: 'Recoverable purchase tax in integer minor units.',\n ui: { format: 'money' },\n })\n recoverableMinor = 0;\n\n constructor(options: DocumentTaxOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.documentType !== undefined)\n this.documentType = options.documentType;\n if (options.documentId !== undefined) this.documentId = options.documentId;\n if (options.currency !== undefined) this.currency = options.currency;\n if (options.taxCodeId !== undefined) this.taxCodeId = options.taxCodeId;\n if (options.taxCodeCode !== undefined)\n this.taxCodeCode = options.taxCodeCode;\n if (options.kind !== undefined) this.kind = options.kind;\n if (options.taxComponentId !== undefined)\n this.taxComponentId = options.taxComponentId;\n if (options.taxComponentName !== undefined)\n this.taxComponentName = options.taxComponentName;\n if (options.percent !== undefined) this.percent = options.percent;\n if (options.liabilityAccountId !== undefined)\n this.liabilityAccountId = options.liabilityAccountId;\n if (options.taxableMinor !== undefined)\n this.taxableMinor = options.taxableMinor;\n if (options.netMinor !== undefined) this.netMinor = options.netMinor;\n if (options.taxMinor !== undefined) this.taxMinor = options.taxMinor;\n if (options.recoverableMinor !== undefined)\n this.recoverableMinor = options.recoverableMinor;\n }\n\n private assertValidDerivedRow(): void {\n if (!['invoice', 'contract', 'credit_note'].includes(this.documentType)) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `Unknown document tax type: ${this.documentType}`,\n );\n }\n for (const [label, value] of [\n ['tenantId', this.tenantId],\n ['documentId', this.documentId],\n ['currency', this.currency],\n ] as const) {\n if (!value || value.trim() !== value) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `DocumentTax ${label} must be a nonempty value`,\n );\n }\n }\n for (const [label, value] of [\n ['taxableMinor', this.taxableMinor],\n ['netMinor', this.netMinor ?? this.taxableMinor],\n ['taxMinor', this.taxMinor],\n ['recoverableMinor', this.recoverableMinor],\n ] as const) {\n if (!Number.isSafeInteger(value)) {\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n `DocumentTax ${label} must be safe integer minor units`,\n );\n }\n }\n if (this.taxCodeId === null) {\n if (\n this.taxComponentId !== null ||\n this.taxCodeCode !== null ||\n this.kind !== null ||\n this.taxComponentName !== null ||\n this.percent !== null ||\n this.liabilityAccountId !== null ||\n this.taxMinor !== 0 ||\n this.recoverableMinor !== 0\n ) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'An unassigned DocumentTax row cannot carry code/component snapshots or tax',\n );\n }\n return;\n }\n if (!this.taxCodeCode || !this.kind) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Assigned DocumentTax rows require code and kind snapshots',\n );\n }\n if (this.taxComponentId === null) {\n if (\n this.kind === 'taxable' ||\n this.taxComponentName !== null ||\n this.percent !== null ||\n this.liabilityAccountId !== null ||\n this.taxMinor !== 0 ||\n this.recoverableMinor !== 0\n ) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Component-less DocumentTax rows must be non-taxable zero-tax snapshots',\n );\n }\n return;\n }\n if (this.kind !== 'taxable' || !this.taxComponentName || !this.percent) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Component DocumentTax rows require taxable component snapshots',\n );\n }\n if (\n this.recoverableMinor !== 0 &&\n this.recoverableMinor !== this.taxMinor\n ) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'DocumentTax recoverableMinor must be zero or the component tax total',\n );\n }\n }\n\n override async save(_options?: SmrtSaveOptions): Promise<this> {\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'DocumentTax rows are derived and cannot be saved directly',\n );\n }\n\n override async delete(\n _options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'DocumentTax rows are derived and cannot be deleted directly',\n );\n }\n\n /** @internal Aggregate persistence only. */\n async saveDerived(\n capability: DocumentMoneyWriteCapability,\n options?: SmrtSaveOptions,\n ): Promise<this> {\n if (capability !== DOCUMENT_MONEY_WRITE) {\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'DocumentTax write capability is required',\n );\n }\n this.assertValidDerivedRow();\n return (await super.save(options)) as this;\n }\n\n /** @internal Aggregate persistence only. */\n async deleteDerived(\n capability: DocumentMoneyWriteCapability,\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n if (capability !== DOCUMENT_MONEY_WRITE) {\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'DocumentTax write capability is required',\n );\n }\n await super.delete(options);\n }\n}\n","/**\n * ContractLineItem model - individual items on a contract\n * @packageDocumentation\n */\n\nimport {\n crossPackageRef,\n field,\n foreignKey,\n SmrtObject,\n type SmrtSaveOptions,\n smrt,\n} from '@happyvertical/smrt-core';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { TenantScoped, tenantId } from '@happyvertical/smrt-tenancy';\nimport {\n DOCUMENT_MONEY_READ_REBIND,\n DOCUMENT_MONEY_WRITE,\n type DocumentMoneyReadRebindCapability,\n type DocumentMoneyWriteCapability,\n} from '../document-money-internal.js';\nimport { MoneyValidationError } from '../money.js';\nimport type {\n ContractLineItemOptions,\n ContractTaxComponentSnapshot,\n} from '../types/index.js';\n\n/**\n * ContractLineItem represents a single line item on a contract.\n *\n * Line items contain the details of what is being sold/purchased,\n * including quantity, pricing, and optional subscription details.\n *\n * @example\n * ```typescript\n * const lineItem = await lineItems.create({\n * contractId: order.id,\n * description: 'Widget Pro',\n * quantity: 10,\n * unitPrice: 4999, // $49.99 in cents\n * taxRate: 0.08\n * });\n * ```\n */\n@TenantScoped({ mode: 'optional' })\n@smrt({\n tableStrategy: 'sti',\n api: { include: ['list', 'get', 'create', 'update', 'delete'] },\n mcp: { include: ['list', 'get'] },\n cli: { skipApiCheck: true },\n})\nexport class ContractLineItem extends SmrtObject { static __smrtPackage__ = \"@happyvertical/smrt-commerce\";\n /** @internal Collection read guard returns this snapshot on its root DB. */\n rebindMoneyRead(\n capability: DocumentMoneyReadRebindCapability,\n db: DatabaseInterface,\n ): this {\n if (capability !== DOCUMENT_MONEY_READ_REBIND)\n throw new Error(\n 'Contract line read rebinding requires the owning capability',\n );\n this._db = db;\n this.options.db = db;\n return this;\n }\n\n /**\n * Tenant ID for multi-tenant isolation\n * Nullable to support both tenant-scoped and global line items\n */\n @tenantId({ nullable: true })\n tenantId: string | null = null;\n\n /**\n * Parent contract\n */\n @field({ description: 'The order, quote or agreement this line belongs to.' })\n @foreignKey('Contract', { role: 'items' })\n contractId: string = '';\n\n /**\n * Item description\n */\n @field({ description: 'What is being sold or bought on this line.' })\n description: string = '';\n\n /**\n * Quantity ordered. Decimal on purpose — contracts price fractional\n * quantities (hours, weight, bandwidth), as do invoice line items.\n */\n @field({ description: 'How many.' })\n quantity: number = 1.0;\n\n /**\n * Unit price before discount, in **integer minor units** (cents, satoshis),\n * in the document's pricing mode: tax-inclusive when its `lineAmountType`\n * is `inclusive`.\n *\n * The `= 0` initializer is load-bearing: SMRT maps an integer literal to\n * INTEGER and a decimal literal to DECIMAL. Money is exact, so `$49.99` is\n * `4999` (#2401).\n */\n @field({\n description:\n \"The price of one, before discount; includes tax when the document's prices include tax.\",\n ui: { format: 'money' },\n })\n unitPrice: number = 0;\n\n /**\n * Discount amount (flat, not percentage), in integer minor units (#2401).\n */\n @field({ ui: { format: 'money' } })\n discount: number = 0;\n\n discountType: 'none' | 'percent' | 'amount' = 'none';\n\n @field({ type: 'decimal' })\n discountRate = '0.0';\n\n /**\n * Tax rate as decimal (e.g., 0.08 for 8%). A rate is inherently fractional,\n * so it stays DECIMAL — INTEGER would truncate every meaningful value.\n */\n @field({\n description:\n 'The stored fractional tax rate for this line (0.05 means 5%).',\n })\n taxRate: number = 0.0;\n\n taxMode: 'inherit' | 'override' = 'inherit';\n\n @foreignKey('TaxCode', { nullable: true })\n taxCodeId: string | null = null;\n\n taxCodeCode: string | null = null;\n\n taxCodeName: string | null = null;\n\n taxComponentsJson: string | null = null;\n\n get taxComponents(): ContractTaxComponentSnapshot[] {\n if (this.taxComponentsJson === null) return [];\n try {\n const value: unknown = JSON.parse(this.taxComponentsJson);\n if (!Array.isArray(value)) return [];\n return value.filter(\n (entry): entry is ContractTaxComponentSnapshot =>\n !!entry &&\n typeof entry === 'object' &&\n typeof (entry as ContractTaxComponentSnapshot).taxComponentId ===\n 'string' &&\n typeof (entry as ContractTaxComponentSnapshot).name === 'string' &&\n typeof (entry as ContractTaxComponentSnapshot).percent === 'string' &&\n Number.isSafeInteger(\n (entry as ContractTaxComponentSnapshot).taxMinor,\n ),\n );\n } catch {\n return [];\n }\n }\n\n set taxComponents(value: ContractTaxComponentSnapshot[]) {\n this.taxComponentsJson = JSON.stringify(value);\n }\n\n /** Exact sum of frozen component taxes, in integer minor units. */\n @field({ ui: { format: 'money' } })\n taxAmount = 0;\n\n /** Stable issue/freeze identity, independent of lifecycle updated_at. */\n moneySnapshotId = '';\n\n /**\n * Line amount in integer minor units: net of discount and before tax for\n * `exclusive` / `none` documents, and tax-inclusive for `inclusive` ones;\n * {@link taxAmount} holds the tax. Derived by the Contract money aggregate\n * on every save.\n */\n @field({\n description:\n \"The line amount: net of discount, and including tax only when the document's prices include tax.\",\n ui: { format: 'money' },\n })\n amount: number = 0;\n\n /**\n * Optional product reference (cross-package, plain string)\n */\n @field({\n description: 'The product this line is for, if it is one you list.',\n })\n @crossPackageRef('@happyvertical/smrt-products:Product')\n productId: string = '';\n\n /**\n * SKU or item code\n */\n sku: string = '';\n\n /**\n * Start date (for lease/subscription items)\n */\n startDate: Date | null = null;\n\n /**\n * End date (for lease/subscription items)\n */\n endDate: Date | null = null;\n\n /**\n * Billing period (for subscriptions: 'monthly', 'yearly', etc.)\n */\n billingPeriod: string = '';\n\n /**\n * Sort order within the contract\n */\n sortOrder: number = 0;\n\n constructor(options: ContractLineItemOptions = {}) {\n super(options);\n if (options.tenantId !== undefined) this.tenantId = options.tenantId;\n if (options.contractId !== undefined) this.contractId = options.contractId;\n if (options.description !== undefined)\n this.description = options.description;\n if (options.quantity !== undefined) this.quantity = options.quantity;\n if (options.unitPrice !== undefined) this.unitPrice = options.unitPrice;\n if (options.discount !== undefined) this.discount = options.discount;\n if (options.discountType !== undefined)\n this.discountType = options.discountType;\n if (options.discountRate !== undefined)\n this.discountRate = options.discountRate;\n if (options.taxRate !== undefined) this.taxRate = options.taxRate;\n if (options.taxMode !== undefined) this.taxMode = options.taxMode;\n if (options.taxCodeId !== undefined) this.taxCodeId = options.taxCodeId;\n if (options.taxCodeCode !== undefined)\n this.taxCodeCode = options.taxCodeCode;\n if (options.taxCodeName !== undefined)\n this.taxCodeName = options.taxCodeName;\n if (options.taxComponentsJson !== undefined)\n this.taxComponentsJson = options.taxComponentsJson;\n if (options.taxComponents !== undefined)\n this.taxComponents = structuredClone(options.taxComponents);\n if (options.taxAmount !== undefined) this.taxAmount = options.taxAmount;\n if (options.moneySnapshotId !== undefined)\n this.moneySnapshotId = options.moneySnapshotId;\n if (options.amount !== undefined) this.amount = options.amount;\n if (options.productId !== undefined) this.productId = options.productId;\n if (options.sku !== undefined) this.sku = options.sku;\n if (options.startDate !== undefined) this.startDate = options.startDate;\n if (options.endDate !== undefined) this.endDate = options.endDate;\n if (options.billingPeriod !== undefined)\n this.billingPeriod = options.billingPeriod;\n if (options.sortOrder !== undefined) this.sortOrder = options.sortOrder;\n }\n\n override async initialize(): Promise<this> {\n await super.initialize();\n // A row read from the database must not be old-format money data.\n if (this.isPersisted) {\n const { assertHydratedMoneyRowCurrent } = await import(\n '../upgrade/detect.js'\n );\n await assertHydratedMoneyRowCurrent(this);\n }\n return this;\n }\n\n override async save(options?: SmrtSaveOptions): Promise<this> {\n const { saveContractLineItem } = await import('../contract-money.js');\n return (await saveContractLineItem(this, options)) as this;\n }\n\n /** @internal Contract aggregate persistence only. */\n async saveDerived(\n capability: DocumentMoneyWriteCapability,\n options?: SmrtSaveOptions,\n ): Promise<this> {\n if (capability !== DOCUMENT_MONEY_WRITE)\n throw new Error('Contract line money write capability is required');\n // Money is integer minor units: refuse a major-unit price before it can\n // reach the database (#2401), ahead of the aggregate recompute.\n for (const [field, value] of [\n ['unitPrice', this.unitPrice],\n ['discount', this.discount],\n ] as const)\n if (!Number.isSafeInteger(value))\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n `Contract line ${field} ${value} must be a safe integer number of minor units`,\n );\n return (await super.save(options)) as this;\n }\n\n override async delete(\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n const { deleteContractLineItem } = await import('../contract-money.js');\n await deleteContractLineItem(this, options);\n }\n\n /** @internal Contract aggregate persistence only. */\n async deleteDerived(\n capability: DocumentMoneyWriteCapability,\n options: { expectedUpdatedAt?: Date | string } = {},\n ): Promise<void> {\n if (capability !== DOCUMENT_MONEY_WRITE)\n throw new Error('Contract line money write capability is required');\n await super.delete(options);\n }\n\n /**\n * Check if this is a subscription/recurring item\n */\n isRecurring(): boolean {\n return !!this.billingPeriod && !!this.startDate;\n }\n\n /**\n * Check if subscription is active\n */\n isSubscriptionActive(): boolean {\n if (!this.isRecurring()) return false;\n if (!this.startDate) return false;\n\n const now = new Date();\n if (now < this.startDate) return false;\n if (this.endDate && now > this.endDate) return false;\n\n return true;\n }\n}\n\nexport default ContractLineItem;\n","/**\n * ContractLineItemCollection - Collection manager for ContractLineItem objects\n * @packageDocumentation\n */\n\nimport {\n SmrtCollection,\n type SmrtListOptions,\n type SmrtObject,\n type SmrtSelectedRow,\n type SmrtSelectField,\n type SmrtStiReadScope,\n type SmrtWhereClause,\n} from '@happyvertical/smrt-core';\nimport {\n getTenantId,\n queryGlobal,\n queryWithGlobals,\n} from '@happyvertical/smrt-tenancy';\nimport { DOCUMENT_MONEY_READ_REBIND } from '../document-money-internal.js';\nimport { ContractLineItem } from '../models/ContractLineItem.js';\nimport { withTenantMoneyCollectionRead } from '../tenant-money-barrier.js';\nimport { assertCommerceMoneySchemaCurrent } from '../upgrade/detect.js';\n\nexport class ContractLineItemCollection extends SmrtCollection<ContractLineItem> {\n static readonly _itemClass = ContractLineItem;\n\n private async withMoneyRead<T>(operation: () => Promise<T>): Promise<T> {\n const rootDb = this.db;\n const tenantId = getTenantId();\n if (!tenantId) {\n // Without a tenant, still refuse a database with old money storage.\n await assertCommerceMoneySchemaCurrent(rootDb);\n return operation();\n }\n const result = await withTenantMoneyCollectionRead(\n rootDb,\n tenantId,\n (boundDb) => this.withDatabase(boundDb, operation),\n );\n if (result instanceof ContractLineItem)\n result.rebindMoneyRead(DOCUMENT_MONEY_READ_REBIND, rootDb);\n else if (Array.isArray(result))\n for (const item of result)\n if (item instanceof ContractLineItem)\n item.rebindMoneyRead(DOCUMENT_MONEY_READ_REBIND, rootDb);\n return result;\n }\n\n override async get(\n filter: string | SmrtWhereClause<ContractLineItem>,\n options: Parameters<SmrtCollection<ContractLineItem>['get']>[1] = {},\n ): Promise<ContractLineItem | null> {\n return this.withMoneyRead(() => super.get(filter, options));\n }\n\n override async list<\n const Select extends readonly SmrtSelectField<ContractLineItem>[],\n >(\n options: Omit<SmrtListOptions<ContractLineItem>, 'select' | 'stiScope'> & {\n select: Select;\n include?: never;\n stiScope: SmrtStiReadScope;\n },\n ): Promise<Record<string, unknown>[]>;\n override async list<\n const Select extends readonly SmrtSelectField<ContractLineItem>[],\n >(\n options: Omit<SmrtListOptions<ContractLineItem>, 'select' | 'stiScope'> & {\n select: Select;\n include?: never;\n stiScope?: undefined;\n },\n ): Promise<SmrtSelectedRow<ContractLineItem, Select>[]>;\n override async list(\n options: Omit<SmrtListOptions<ContractLineItem>, 'select' | 'stiScope'> & {\n select?: undefined;\n stiScope: SmrtStiReadScope;\n },\n ): Promise<SmrtObject[]>;\n override async list(options?: undefined): Promise<ContractLineItem[]>;\n override async list(\n options: Omit<SmrtListOptions<ContractLineItem>, 'select' | 'stiScope'> & {\n select?: undefined;\n stiScope?: undefined;\n },\n ): Promise<ContractLineItem[]>;\n override async list(\n options:\n | (Omit<SmrtListOptions<ContractLineItem>, 'select'> & {\n select?: undefined;\n })\n | undefined,\n ): Promise<SmrtObject[]>;\n override async list(\n options: SmrtListOptions<ContractLineItem> | undefined,\n ): Promise<SmrtObject[] | Record<string, unknown>[]>;\n override async list(\n options: SmrtListOptions<ContractLineItem> = {},\n ): Promise<SmrtObject[] | Record<string, unknown>[]> {\n return this.withMoneyRead(() => super.list(options));\n }\n\n /**\n * Find contract line items by their parent contract.\n *\n * @param contractId - Contract ID\n * @returns Array of contract line items, sorted by sortOrder\n */\n async findByContract(contractId: string): Promise<ContractLineItem[]> {\n return await this.list({\n where: { contractId },\n orderBy: 'sortOrder ASC',\n });\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all contract line items belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of contract line items for the tenant\n */\n async findByTenant(tenantId: string): Promise<ContractLineItem[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global contract line items (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global contract line items\n */\n async findGlobal(): Promise<ContractLineItem[]> {\n return queryGlobal<ContractLineItem>(this);\n }\n\n /**\n * Find contract line items for a tenant including global line items.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global contract line items\n */\n async findWithGlobals(tenantId: string): Promise<ContractLineItem[]> {\n return queryWithGlobals<ContractLineItem>(\n this,\n tenantId,\n 'ContractLineItem.findWithGlobals',\n );\n }\n}\n","/**\n * VendorCollection - Collection manager for Vendor objects\n * @packageDocumentation\n */\n\nimport { SmrtCollection } from '@happyvertical/smrt-core';\nimport { queryGlobal, queryWithGlobals } from '@happyvertical/smrt-tenancy';\nimport { Vendor } from '../models/Vendor.js';\nimport { VendorStatus } from '../types/index.js';\n\nexport class VendorCollection extends SmrtCollection<Vendor> {\n static readonly _itemClass = Vendor;\n\n /**\n * Find vendors by profile ID\n *\n * @param profileId - Profile ID from smrt-profiles\n * @returns Array of vendors linked to this profile\n */\n async findByProfile(profileId: string): Promise<Vendor[]> {\n return await this.list({\n where: { profileId },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find all active vendors\n *\n * @returns Array of active vendors\n */\n async findActive(): Promise<Vendor[]> {\n return await this.list({\n where: { status: VendorStatus.ACTIVE },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Find vendors by status\n *\n * @param status - Vendor status\n * @returns Array of vendors\n */\n async findByStatus(status: VendorStatus): Promise<Vendor[]> {\n return await this.list({\n where: { status },\n orderBy: 'created_at DESC',\n });\n }\n\n /**\n * Get or create a vendor for a profile\n *\n * @param profileId - Profile ID\n * @param defaults - Default values if creating\n * @returns Vendor\n */\n async getOrCreateForProfile(\n profileId: string,\n defaults: Partial<{\n leadTimeDays: number;\n minimumOrderAmount: number;\n paymentTerms: string;\n }> = {},\n ): Promise<Vendor> {\n const existing = await this.findByProfile(profileId);\n if (existing.length > 0) {\n return existing[0];\n }\n\n const vendor = await this.create({\n profileId,\n leadTimeDays: defaults.leadTimeDays ?? 0,\n minimumOrderAmount: defaults.minimumOrderAmount ?? 0,\n paymentTerms: defaults.paymentTerms ?? '',\n status: VendorStatus.ACTIVE,\n });\n await vendor.save();\n return vendor;\n }\n\n // ============================================================================\n // Tenant Helper Methods\n // ============================================================================\n\n /**\n * Find all vendors belonging to a specific tenant\n *\n * @param tenantId - Tenant ID\n * @returns Array of vendors for the tenant\n */\n async findByTenant(tenantId: string): Promise<Vendor[]> {\n return this.list({ where: { tenantId } });\n }\n\n /**\n * Find all global vendors (not associated with any tenant).\n *\n * Routes through the shared tenant-global helper so it does not throw under\n * an active tenant context (an explicit `tenant_id IS NULL` filter would be\n * flagged as an isolation violation). (#1600)\n *\n * @returns Array of global vendors\n */\n async findGlobal(): Promise<Vendor[]> {\n return queryGlobal<Vendor>(this);\n }\n\n /**\n * Find vendors for a tenant including global vendors.\n *\n * Fails closed if an active tenant context requests a different tenant's\n * rows; the admin/system path keeps the cross-tenant capability. (#1600)\n *\n * @param tenantId - Tenant ID\n * @returns Array of tenant-specific and global vendors\n */\n async findWithGlobals(tenantId: string): Promise<Vendor[]> {\n return queryWithGlobals<Vendor>(this, tenantId, 'Vendor.findWithGlobals');\n }\n}\n","/** Exact monetary evidence received from a supplier, independent of calculated pricing. */\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { ISO_4217_MINOR_UNITS } from '@happyvertical/smrt-ui/currency';\nimport { ContractCollection } from './collections/ContractCollection.js';\nimport { ContractLineItemCollection } from './collections/ContractLineItemCollection.js';\nimport { CustomerCollection } from './collections/CustomerCollection.js';\nimport { TaxCodeCollection } from './collections/TaxCodeCollection.js';\nimport { TaxComponentCollection } from './collections/TaxComponentCollection.js';\nimport { VendorCollection } from './collections/VendorCollection.js';\nimport {\n DOCUMENT_MONEY_WRITE,\n lockDocumentMoneyParents,\n} from './document-money-internal.js';\nimport {\n Agreement,\n type Contract,\n Order,\n PurchaseOrder,\n SupplierQuote,\n} from './models/Contract.js';\nimport { MoneyValidationError, type TaxCodeKind } from './money.js';\nimport { withTenantMoneyRuntime } from './tenant-money-barrier.js';\nimport { ContractStatus } from './types/index.js';\n\n/** A supplier tax component. Null explicitly retains an unknown fact. */\nexport interface ReceivedPurchaseTaxComponent {\n sourceKey: string;\n label: string;\n amountMinor: number;\n classification: TaxCodeKind | null;\n /** Explicit report identity/basis; omission retains unknown, never implies a rate. */\n taxCodeId?: string | null;\n taxComponentId?: string | null;\n taxableMinor?: number | null;\n /** Explicit net allocation when supplier taxable basis includes prior taxes. */\n netMinor?: number | null;\n recoverableMinor: number | null;\n}\n/** Complete source line money; optional source decimal text is evidence, not a calculator input. */\nexport interface ReceivedPurchaseMoneyLine {\n sourceKey: string;\n description: string;\n netMinor: number;\n taxMinor: number;\n grossMinor: number;\n quantityText?: string;\n unitRateText?: string;\n}\n/** Monetary evidence without a fabricated rate, quantity, or line allocation. */\nexport interface ReceivedPurchaseMoneyFacts {\n currency: string;\n netMinor: number;\n taxMinor: number;\n grossMinor: number;\n taxComponents: ReceivedPurchaseTaxComponent[] | null;\n detail:\n | { kind: 'header-only' }\n | { kind: 'lines'; lines: ReceivedPurchaseMoneyLine[] };\n}\n/** Immutable request and supplier evidence identity retained with the amounts. */\nexport interface ReceivedPurchaseMoneySnapshot {\n version: 1;\n requestId: string;\n evidenceRef: string;\n facts: ReceivedPurchaseMoneyFacts;\n sourceQuote: ReceivedPurchaseQuoteSource | null;\n}\nexport interface ReceivedPurchaseQuoteSource {\n contractId: string;\n relationship: 'copy' | 'negotiated';\n}\n/** Public command input. The host authorizes the human before calling this command. */\nexport interface RecordReceivedPurchaseMoneyInput {\n db: DatabaseInterface;\n tenantId: string;\n contractId: string;\n requestId: string;\n evidenceRef: string;\n facts: ReceivedPurchaseMoneyFacts;\n sourceQuote?: ReceivedPurchaseQuoteSource;\n}\n\n/** Complete received sales agreement evidence; recording is not revenue/tax recognition. */\nexport type ReceivedSalesContractMoneyFacts = ReceivedPurchaseMoneyFacts;\nexport type ReceivedSalesContractMoneySnapshot = ReceivedPurchaseMoneySnapshot;\nexport type RecordReceivedSalesContractMoneyInput = Omit<\n RecordReceivedPurchaseMoneyInput,\n 'sourceQuote'\n>;\n\n/** Record supplied complete facts on one empty draft Order/Agreement. */\nexport async function recordReceivedSalesContractMoney(\n input: RecordReceivedSalesContractMoneyInput,\n): Promise<ReceivedSalesContractMoneySnapshot> {\n if (Object.hasOwn(input, 'sourceQuote'))\n invalid('Sales agreement evidence cannot reference a supplier quote');\n if (\n input.facts.taxComponents?.some(\n (component) => component.recoverableMinor !== null,\n )\n )\n invalid(\n 'Purchase tax recovery is not applicable to sales agreement evidence; retain null',\n );\n return recordReceivedContractMoney(input, 'sales');\n}\n\n/** Read exact sales agreement evidence without calculating or recognizing an invoice. */\nexport function getReceivedSalesContractMoney(\n contract: Contract,\n): ReceivedSalesContractMoneySnapshot | null {\n if (!(contract instanceof Order || contract instanceof Agreement))\n return null;\n return getReceivedPurchaseMoney(contract);\n}\n\nfunction invalid(message: string): never {\n throw new MoneyValidationError('INVALID_MONEY_INPUT', message);\n}\nfunction object(value: unknown, label: string): Record<string, unknown> {\n if (!value || typeof value !== 'object' || Array.isArray(value))\n invalid(`${label} must be an object`);\n return value as Record<string, unknown>;\n}\nfunction text(value: unknown, label: string): string {\n if (typeof value !== 'string' || !value || value.trim() !== value)\n invalid(`${label} must be nonempty trimmed text`);\n return value;\n}\nfunction minor(value: unknown, label: string): number {\n if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0)\n invalid(`${label} must be a nonnegative safe integer`);\n return value;\n}\nfunction amounts(value: Record<string, unknown>, label: string) {\n const netMinor = minor(value.netMinor, `${label} net`);\n const taxMinor = minor(value.taxMinor, `${label} tax`);\n const grossMinor = minor(value.grossMinor, `${label} gross`);\n if (BigInt(netMinor) + BigInt(taxMinor) !== BigInt(grossMinor))\n invalid(`${label} net plus tax must equal gross`);\n return { netMinor, taxMinor, grossMinor };\n}\nfunction unique(keys: string[], label: string): void {\n if (new Set(keys).size !== keys.length)\n invalid(`${label} source keys must be unique`);\n}\n\n/** Validate and canonicalize exact received money, retaining unknowns as null. */\nexport function validateReceivedPurchaseMoneyFacts(\n value: unknown,\n): ReceivedPurchaseMoneyFacts {\n const input = object(value, 'Received purchase facts');\n const currency = text(input.currency, 'Currency');\n if (\n !/^[A-Z]{3}$/.test(currency) ||\n !ISO_4217_MINOR_UNITS.has(currency) ||\n ISO_4217_MINOR_UNITS.get(currency) === null\n )\n invalid('Currency must be a supported uppercase ISO 4217 code');\n const totals = amounts(input, 'Document');\n let taxComponents: ReceivedPurchaseTaxComponent[] | null = null;\n if (input.taxComponents !== null) {\n if (!Array.isArray(input.taxComponents))\n invalid('Tax components must be an array or explicit null');\n taxComponents = input.taxComponents.map((raw) => {\n const component = object(raw, 'Tax component');\n const amountMinor = minor(component.amountMinor, 'Component amount');\n const recoverableMinor =\n component.recoverableMinor === null\n ? null\n : minor(component.recoverableMinor, 'Recoverable amount');\n if (recoverableMinor !== null && recoverableMinor > amountMinor)\n invalid('Recoverable tax cannot exceed component tax');\n const classification = component.classification;\n if (\n classification !== null &&\n !['taxable', 'zero-rated', 'exempt', 'out-of-scope'].includes(\n String(classification),\n )\n )\n invalid('Unknown tax classification');\n const taxableMinor =\n component.taxableMinor == null\n ? null\n : minor(component.taxableMinor, 'Taxable basis');\n const netMinor =\n component.netMinor == null\n ? null\n : minor(component.netMinor, 'Component net basis');\n if (netMinor !== null && netMinor > totals.netMinor)\n invalid('Component net basis exceeds document net');\n if (\n classification !== null &&\n classification !== 'taxable' &&\n amountMinor !== 0\n )\n invalid('A non-taxable classification cannot contain tax');\n return {\n sourceKey: text(component.sourceKey, 'Component source key'),\n label: text(component.label, 'Component label'),\n amountMinor,\n classification: classification as TaxCodeKind | null,\n taxCodeId:\n component.taxCodeId == null\n ? null\n : text(component.taxCodeId, 'Tax code identity'),\n taxComponentId:\n component.taxComponentId == null\n ? null\n : text(component.taxComponentId, 'Tax component identity'),\n taxableMinor,\n ...(netMinor !== null ? { netMinor } : {}),\n recoverableMinor,\n };\n });\n unique(\n taxComponents.map((item) => item.sourceKey),\n 'Tax component',\n );\n if (\n taxComponents.reduce(\n (sum, item) => sum + BigInt(item.amountMinor),\n 0n,\n ) !== BigInt(totals.taxMinor)\n )\n invalid('Component tax sum must equal document tax');\n }\n const rawDetail = object(input.detail, 'Source detail');\n let detail: ReceivedPurchaseMoneyFacts['detail'];\n if (rawDetail.kind === 'header-only') {\n if ('lines' in rawDetail)\n invalid('Header-only evidence cannot contain monetary lines');\n detail = { kind: 'header-only' };\n } else if (rawDetail.kind === 'lines') {\n if (!Array.isArray(rawDetail.lines) || !rawDetail.lines.length)\n invalid('Complete source lines are required');\n const lines = rawDetail.lines.map((raw) => {\n const line = object(raw, 'Source line');\n const result: ReceivedPurchaseMoneyLine = {\n sourceKey: text(line.sourceKey, 'Line source key'),\n description: text(line.description, 'Line description'),\n ...amounts(line, 'Source line'),\n };\n for (const key of ['quantityText', 'unitRateText'] as const) {\n if (line[key] === undefined) continue;\n const decimal = text(line[key], key);\n if (!/^(?:0|[1-9]\\d*)(?:\\.\\d+)?$/.test(decimal))\n invalid(`${key} must be nonnegative decimal source text`);\n result[key] = decimal;\n }\n return result;\n });\n unique(\n lines.map((item) => item.sourceKey),\n 'Line',\n );\n for (const key of ['netMinor', 'taxMinor', 'grossMinor'] as const) {\n if (\n lines.reduce((sum, line) => sum + BigInt(line[key]), 0n) !==\n BigInt(totals[key])\n )\n invalid(`Source line ${key} sum must equal document amount`);\n }\n detail = { kind: 'lines', lines };\n } else invalid('Unknown received source detail kind');\n return { currency, ...totals, taxComponents, detail };\n}\n\nfunction quoteSource(value: unknown): ReceivedPurchaseQuoteSource | null {\n if (value == null) return null;\n const input = object(value, 'Source quote');\n if (input.relationship !== 'copy' && input.relationship !== 'negotiated')\n invalid('Unknown source quote relationship');\n return {\n contractId: text(input.contractId, 'Source quote identity'),\n relationship: input.relationship,\n };\n}\n\n/** Read validated received evidence; calculated contracts return null. */\nexport function getReceivedPurchaseMoney(\n contract: Pick<Contract, 'moneyAuthority' | 'receivedPurchaseMoneyJson'>,\n): ReceivedPurchaseMoneySnapshot | null {\n if (contract.moneyAuthority === 'calculated') {\n if (contract.receivedPurchaseMoneyJson)\n invalid('Calculated money cannot contain received evidence');\n return null;\n }\n if (contract.moneyAuthority !== 'received')\n invalid('Unknown contract money authority');\n let parsed: unknown;\n try {\n parsed = JSON.parse(contract.receivedPurchaseMoneyJson);\n } catch {\n invalid('Stored received money is invalid');\n }\n const input = object(parsed, 'Received snapshot');\n if (input.version !== 1) invalid('Unknown received money version');\n return {\n version: 1,\n requestId: text(input.requestId, 'Request identity'),\n evidenceRef: text(input.evidenceRef, 'Evidence reference'),\n facts: validateReceivedPurchaseMoneyFacts(input.facts),\n sourceQuote: quoteSource(input.sourceQuote),\n };\n}\n\n/** Record immutable source facts on one empty draft SupplierQuote or PurchaseOrder, retry safely. */\nexport async function recordReceivedPurchaseMoney(\n input: RecordReceivedPurchaseMoneyInput,\n): Promise<ReceivedPurchaseMoneySnapshot> {\n return recordReceivedContractMoney(input, 'purchase');\n}\n\nasync function recordReceivedContractMoney(\n input: RecordReceivedPurchaseMoneyInput,\n direction: 'purchase' | 'sales',\n): Promise<ReceivedPurchaseMoneySnapshot> {\n const tenantId = text(input.tenantId, 'Tenant');\n if (getTenantId() !== tenantId)\n invalid('Received contract money requires its active tenant context');\n const contractId = text(input.contractId, 'Contract');\n const snapshot: ReceivedPurchaseMoneySnapshot = {\n version: 1,\n requestId: text(input.requestId, 'Request identity'),\n evidenceRef: text(input.evidenceRef, 'Evidence reference'),\n facts: validateReceivedPurchaseMoneyFacts(input.facts),\n sourceQuote: quoteSource(input.sourceQuote),\n };\n const serialized = JSON.stringify(snapshot);\n return withTenantMoneyRuntime(input.db, tenantId, async ({ db }) => {\n const contracts = await ContractCollection.create({ db });\n await lockDocumentMoneyParents([\n { id: contractId, tenantId, collection: contracts },\n ...(snapshot.sourceQuote\n ? [\n {\n id: snapshot.sourceQuote.contractId,\n tenantId,\n collection: contracts,\n },\n ]\n : []),\n ]);\n const contract = await contracts.get({ id: contractId }, { cache: false });\n const validParty =\n direction === 'purchase'\n ? (contract instanceof SupplierQuote ||\n contract instanceof PurchaseOrder) &&\n contract.vendorId\n : (contract instanceof Order || contract instanceof Agreement) &&\n contract.customerId;\n if (!contract || contract.tenantId !== tenantId || !validParty)\n invalid(\n direction === 'purchase'\n ? 'Received money requires a supplier quote or purchase order in the active tenant'\n : 'Received money requires an Order or Agreement with a customer in the active tenant',\n );\n const stored = getReceivedPurchaseMoney(contract);\n if (stored) {\n if (JSON.stringify(stored) !== serialized)\n invalid(\n 'Received money request changed; create a separate contract revision',\n );\n return stored;\n }\n if (\n direction === 'purchase' &&\n (contract instanceof SupplierQuote || contract instanceof PurchaseOrder)\n ) {\n const vendor = await (await VendorCollection.create({ db })).get(\n { id: contract.vendorId },\n { cache: false },\n );\n if (!vendor || vendor.tenantId !== tenantId)\n invalid('Supplier must belong to the active tenant');\n } else if (contract instanceof Order || contract instanceof Agreement) {\n const customer = await (await CustomerCollection.create({ db })).get(\n { id: contract.customerId },\n { cache: false },\n );\n if (!customer || customer.tenantId !== tenantId)\n invalid('Customer must belong to the active tenant');\n }\n if (snapshot.sourceQuote) {\n if (!(contract instanceof PurchaseOrder))\n invalid('Only a purchase order can reference a source quote');\n const source = await contracts.get(\n { id: snapshot.sourceQuote.contractId },\n { cache: false },\n );\n if (\n !(source instanceof SupplierQuote) ||\n source.tenantId !== tenantId ||\n source.vendorId !== contract.vendorId ||\n source.currency !== contract.currency ||\n source.status !== ContractStatus.ACCEPTED ||\n !source.moneySnapshotId\n )\n invalid(\n 'Source quote must be accepted with matching tenant, vendor and currency',\n );\n const sourceMoney = getReceivedPurchaseMoney(source);\n if (!sourceMoney)\n invalid('Source quote must have received monetary evidence');\n if (\n snapshot.sourceQuote.relationship === 'copy' &&\n JSON.stringify(sourceMoney.facts) !== JSON.stringify(snapshot.facts)\n )\n invalid('Copied quote monetary facts must match exactly');\n if (\n snapshot.sourceQuote.relationship === 'negotiated' &&\n snapshot.evidenceRef === sourceMoney.evidenceRef\n )\n invalid(\n 'Negotiated award requires separate approved monetary evidence',\n );\n }\n for (const component of snapshot.facts.taxComponents ?? []) {\n if (component.taxCodeId) {\n const code = await (await TaxCodeCollection.create({ db })).get(\n { id: component.taxCodeId },\n { cache: false },\n );\n if (!code || code.tenantId !== tenantId)\n invalid('Tax report code must belong to the active tenant');\n if (\n component.classification !== null &&\n code.kind !== component.classification\n )\n invalid('Explicit tax classification must match its selected code');\n if (\n component.taxComponentId &&\n !code.components.some(\n (entry) => entry.taxComponentId === component.taxComponentId,\n )\n )\n invalid('Tax component must belong to its selected tax code');\n }\n if (component.taxComponentId) {\n const tax = await (await TaxComponentCollection.create({ db })).get(\n { id: component.taxComponentId },\n { cache: false },\n );\n if (!tax || tax.tenantId !== tenantId)\n invalid('Tax report component must belong to the active tenant');\n }\n }\n if (contract.status !== ContractStatus.DRAFT || contract.moneySnapshotId)\n invalid('Received money requires an unfrozen draft');\n if (contract.currency !== snapshot.facts.currency)\n invalid('Received money currency must match the contract');\n const lines = await (\n await ContractLineItemCollection.create({ db })\n ).findByContract(contractId);\n if (lines.length)\n invalid('Received money cannot mix with calculated contract lines');\n contract.moneyAuthority = 'received';\n contract.receivedPurchaseMoneyJson = serialized;\n contract.subtotal = snapshot.facts.netMinor;\n contract.discountTotal = 0;\n contract.taxAmount = snapshot.facts.taxMinor;\n contract.totalAmount = snapshot.facts.grossMinor;\n await contract.saveDerived(DOCUMENT_MONEY_WRITE);\n return snapshot;\n });\n}\n\n/** A received order cannot silently disappear from tax reporting when facts are unknown. */\nexport class ReceivedPurchaseTaxClassificationRequired extends Error {\n readonly code = 'RECEIVED_PURCHASE_TAX_CLASSIFICATION_REQUIRED';\n constructor(readonly contractId: string) {\n super(\n `Purchase order ${contractId} needs explicit tax classification, tax-code/component identity, taxable basis and recoverability in its received evidence before tax reporting. Record a reviewed replacement revision; do not infer tax rates.`,\n );\n this.name = 'ReceivedPurchaseTaxClassificationRequired';\n }\n}\n\n/** @internal Project explicitly classified source facts without inventing DocumentTax rates. */\nexport function receivedPurchaseReportFacts(\n contract: Contract,\n): import('./collections/DocumentTaxCollection.js').DocumentTaxReportFact[] {\n const snapshot = getReceivedPurchaseMoney(contract);\n if (!snapshot || !(contract instanceof PurchaseOrder)) return [];\n const id = text(contract.id, 'Purchase order identity');\n const components = snapshot.facts.taxComponents;\n if (!components?.length)\n throw new ReceivedPurchaseTaxClassificationRequired(id);\n return components.map((component) => {\n if (\n component.classification === null ||\n !component.taxCodeId ||\n component.taxableMinor == null ||\n component.recoverableMinor === null ||\n (component.taxableMinor > snapshot.facts.netMinor &&\n component.netMinor == null) ||\n (component.classification === 'taxable' && !component.taxComponentId)\n )\n throw new ReceivedPurchaseTaxClassificationRequired(id);\n return {\n tenantId: text(contract.tenantId, 'Tenant'),\n documentType: 'contract',\n documentId: id,\n documentNumber: contract.reference.trim() || id,\n partyId: contract.vendorId,\n documentKind: 'contract',\n issueDate: contract.issueDate.toISOString(),\n status: contract.status,\n direction: 'purchase',\n currency: snapshot.facts.currency,\n taxCodeId: component.taxCodeId,\n taxCodeKind: component.classification,\n taxableMinor: component.taxableMinor,\n ...(component.netMinor != null ? { netMinor: component.netMinor } : {}),\n taxMinor: component.amountMinor,\n recoverableMinor: component.recoverableMinor,\n taxComponentId: component.taxComponentId ?? null,\n componentName: component.label,\n liabilityAccountId: null,\n commitmentId: id,\n commitmentLineId: null,\n };\n });\n}\n","/** Owning collection for derived document tax totals. */\n\nimport { SmrtCollection } from '@happyvertical/smrt-core';\nimport { withTenant } from '@happyvertical/smrt-tenancy';\nimport {\n DOCUMENT_MONEY_WRITE,\n type DocumentMoneyWriteCapability,\n} from '../document-money-internal.js';\nimport {\n historicalSalesReportInvoices,\n historicalSalesTaxReportFacts,\n} from '../historical-sales-reports.js';\nimport { classifyContractPurchase } from '../models/Contract.js';\nimport {\n type CommerceDocumentType,\n DocumentTax,\n type DocumentTaxOptions,\n} from '../models/DocumentTax.js';\nimport { MoneyValidationError, type TaxCodeKind } from '../money.js';\nimport { receivedPurchaseReportFacts } from '../received-purchase-money.js';\nimport { ContractCollection } from './ContractCollection.js';\n\nexport type DocumentTaxWriteInput = Required<\n Pick<\n DocumentTaxOptions,\n | 'tenantId'\n | 'documentType'\n | 'documentId'\n | 'currency'\n | 'taxCodeId'\n | 'taxCodeCode'\n | 'kind'\n | 'taxComponentId'\n | 'taxComponentName'\n | 'percent'\n | 'liabilityAccountId'\n | 'taxableMinor'\n | 'taxMinor'\n | 'recoverableMinor'\n >\n> & { netMinor?: number | null };\n\n/** Normalized immutable document-tax snapshots for tax-summary materialization. */\nexport interface DocumentTaxReportFact {\n tenantId: string;\n documentType: CommerceDocumentType;\n documentId: string;\n /** A document number/reference supplied to drilldown consumers. */\n documentNumber: string;\n /** Tenant-scoped party identity for a host to resolve as its party display. */\n partyId: string | null;\n /** Positive credit snapshots are negated once by the pure report aggregation. */\n documentKind: CommerceDocumentType;\n issueDate: string;\n status: string;\n direction: 'sales' | 'purchase';\n currency: string;\n taxCodeId: string | null;\n /** Null is an unassigned/unknown snapshot and must remain visible as a warning. */\n taxCodeKind: TaxCodeKind | null;\n taxableMinor: number;\n netMinor?: number;\n taxMinor: number;\n recoverableMinor: number;\n taxComponentId: string | null;\n componentName: string | null;\n liabilityAccountId: string | null;\n /** Purchase Contracts are the expense commitment identity; other documents are unlinked. */\n commitmentId: string | null;\n /** Document-tax snapshots are contract-level, so they retain no line allocation. */\n commitmentLineId: string | null;\n}\n\n/** @deprecated Use DocumentTaxReportFact. Kept for report-adapter compatibility. */\nexport type InvoiceTaxReportFact = DocumentTaxReportFact;\n\nexport interface DocumentTaxReportFactsOptions {\n tenantId: string;\n from: Date;\n to: Date;\n currency?: string;\n}\n\n/** @deprecated Use DocumentTaxReportFactsOptions. */\nexport type InvoiceTaxReportFactsOptions = DocumentTaxReportFactsOptions;\n\nconst TAX_CODE_KINDS = new Set<TaxCodeKind>([\n 'taxable',\n 'zero-rated',\n 'exempt',\n 'out-of-scope',\n]);\n\nfunction requiredText(value: unknown, label: string): string {\n if (typeof value !== 'string' || !value.trim())\n throw new Error(`Invoice tax report ${label} must be a nonempty string.`);\n return value;\n}\n\nfunction storedMinor(value: unknown, label: string): number {\n const parsed =\n typeof value === 'bigint'\n ? Number(value)\n : typeof value === 'number'\n ? value\n : typeof value === 'string' && /^-?\\d+$/.test(value)\n ? Number(value)\n : Number.NaN;\n if (!Number.isSafeInteger(parsed))\n throw new RangeError(\n `Invoice tax report ${label} must be a safe integer minor-unit value.`,\n );\n return parsed;\n}\n\nfunction reportDate(value: unknown, label: string): string {\n const date = new Date(String(value));\n if (Number.isNaN(date.getTime()))\n throw new Error(`Invoice tax report ${label} must be a valid date.`);\n return date.toISOString();\n}\n\nfunction invoiceDocumentType(value: unknown): 'invoice' | 'credit_note' {\n if (value === 'invoice' || value === 'credit_note') return value;\n throw new Error(\n 'Invoice tax report document type must be invoice or credit_note.',\n );\n}\n\nfunction invoiceKind(value: unknown): 'invoice' | 'credit_note' {\n if (value === 'invoice' || value === 'credit_note') return value;\n throw new Error('Invoice tax report kind must be invoice or credit_note.');\n}\n\nfunction contractDocumentType(value: unknown): 'contract' {\n if (value === 'contract') return value;\n throw new Error('Contract tax report document type must be contract.');\n}\n\nfunction taxCodeKind(value: unknown): TaxCodeKind | null {\n return typeof value === 'string' && TAX_CODE_KINDS.has(value as TaxCodeKind)\n ? (value as TaxCodeKind)\n : null;\n}\n\nfunction assertReportFactOptions(options: DocumentTaxReportFactsOptions): void {\n if (!options.tenantId?.trim())\n throw new Error('Invoice tax report facts require a tenant.');\n if (!(options.from instanceof Date) || Number.isNaN(options.from.getTime()))\n throw new Error('Invoice tax report facts require a valid start date.');\n if (!(options.to instanceof Date) || Number.isNaN(options.to.getTime()))\n throw new Error('Invoice tax report facts require a valid end date.');\n if (options.from > options.to)\n throw new Error('Invoice tax report facts require an ordered date range.');\n if (options.currency !== undefined && !options.currency.trim())\n throw new Error(\n 'Invoice tax report currency must be nonempty when supplied.',\n );\n}\n\nfunction compareCodeUnits(left: string, right: string): number {\n return left < right ? -1 : left > right ? 1 : 0;\n}\n\nfunction identity(\n row: Pick<DocumentTaxWriteInput, 'taxCodeId' | 'taxComponentId'>,\n): string {\n return `${row.taxCodeId === null ? '0:' : `1:${row.taxCodeId}`}\\u0000${\n row.taxComponentId === null ? '0:' : `1:${row.taxComponentId}`\n }`;\n}\n\nconst BUSINESS_FIELDS = [\n 'tenantId',\n 'documentType',\n 'documentId',\n 'currency',\n 'taxCodeId',\n 'taxCodeCode',\n 'kind',\n 'taxComponentId',\n 'taxComponentName',\n 'percent',\n 'liabilityAccountId',\n 'taxableMinor',\n 'netMinor',\n 'taxMinor',\n 'recoverableMinor',\n] as const;\n\nfunction unchanged(row: DocumentTax, input: DocumentTaxWriteInput): boolean {\n return BUSINESS_FIELDS.every((field) => {\n const persisted = row[field];\n const proposed = input[field];\n if (field === 'percent' && persisted !== null && proposed !== null) {\n return String(persisted) === String(proposed);\n }\n return (persisted ?? null) === (proposed ?? null);\n });\n}\n\nexport class DocumentTaxCollection extends SmrtCollection<DocumentTax> {\n static readonly _itemClass = DocumentTax;\n\n /**\n * Read immutable commerce document-tax snapshots for one tenant.\n *\n * Invoice/credit rows join their shared storage by identity and tenant.\n * Purchase Contract rows first use the Contract owner's bounded hydrated\n * candidate API so custom PurchaseContract descendants retain their STI\n * classification. The query then joins only those candidate ids by identity\n * and tenant. Stored snapshots remain authoritative; this never recalculates\n * money or resolves present-day tax configuration.\n */\n async listReportFacts(\n options: DocumentTaxReportFactsOptions,\n ): Promise<DocumentTaxReportFact[]> {\n assertReportFactOptions(options);\n const [invoiceFacts, purchaseFacts] = await Promise.all([\n this.listInvoiceReportFacts(options),\n this.listPurchaseContractReportFacts(options),\n ]);\n return [...invoiceFacts, ...purchaseFacts].sort(\n (left, right) =>\n left.issueDate.localeCompare(right.issueDate) ||\n left.documentId.localeCompare(right.documentId) ||\n String(left.taxCodeId).localeCompare(String(right.taxCodeId)) ||\n String(left.taxComponentId).localeCompare(String(right.taxComponentId)),\n );\n }\n\n private async listInvoiceReportFacts(\n options: DocumentTaxReportFactsOptions,\n ): Promise<DocumentTaxReportFact[]> {\n const currencyClause = options.currency ? ' AND d.currency = ?' : '';\n const result = await this.db.query(\n `SELECT d.tenant_id, d.document_type, d.document_id, d.currency, d.tax_code_id, d.kind,\n d.tax_component_id, d.tax_component_name, d.liability_account_id,\n d.taxable_minor, d.net_minor, d.tax_minor, d.recoverable_minor,\n i.kind AS invoice_kind, i.invoice_number, i.customer_id, i.issue_date, i.status\n FROM document_taxes d\n JOIN invoices i\n ON CAST(i.id AS TEXT) = CAST(d.document_id AS TEXT)\n AND i.tenant_id = d.tenant_id\n WHERE CAST(d.tenant_id AS TEXT) = ?\n AND CAST(i.tenant_id AS TEXT) = ?\n AND (\n (d.document_type = 'invoice' AND i.kind = 'invoice')\n OR (d.document_type = 'credit_note' AND i.kind = 'credit_note')\n )\n AND i.status NOT IN ('draft', 'cancelled')\n AND i.issue_date >= ?\n AND i.issue_date <= ?${currencyClause}\n ORDER BY i.issue_date ASC, i.id ASC, d.tax_code_id ASC, d.tax_component_id ASC`,\n options.tenantId,\n options.tenantId,\n options.from.toISOString(),\n options.to.toISOString(),\n ...(options.currency ? [options.currency] : []),\n );\n const ordinary = result.rows.map(\n (row): DocumentTaxReportFact => ({\n tenantId: requiredText(row.tenant_id, 'tenant id'),\n documentType: invoiceDocumentType(row.document_type),\n documentId: requiredText(row.document_id, 'document id'),\n documentNumber: requiredText(row.invoice_number, 'invoice number'),\n partyId:\n typeof row.customer_id === 'string' && row.customer_id.trim()\n ? row.customer_id\n : null,\n documentKind: invoiceKind(row.invoice_kind),\n issueDate: reportDate(row.issue_date, 'issue date'),\n status: requiredText(row.status, 'invoice status'),\n direction: 'sales' as const,\n currency: requiredText(row.currency, 'currency'),\n taxCodeId: row.tax_code_id == null ? null : String(row.tax_code_id),\n taxCodeKind: taxCodeKind(row.kind),\n taxableMinor: storedMinor(row.taxable_minor, 'taxable minor'),\n ...(row.net_minor != null\n ? { netMinor: storedMinor(row.net_minor, 'net minor') }\n : {}),\n taxMinor: storedMinor(row.tax_minor, 'tax minor'),\n recoverableMinor: storedMinor(\n row.recoverable_minor,\n 'recoverable minor',\n ),\n taxComponentId:\n row.tax_component_id == null ? null : String(row.tax_component_id),\n componentName:\n row.tax_component_name == null\n ? null\n : String(row.tax_component_name),\n liabilityAccountId:\n row.liability_account_id == null\n ? null\n : String(row.liability_account_id),\n commitmentId: null,\n commitmentLineId: null,\n }),\n );\n return [\n ...ordinary,\n ...(await historicalSalesReportInvoices(this.db, options)).flatMap(\n historicalSalesTaxReportFacts,\n ),\n ];\n }\n\n private async listPurchaseContractReportFacts(\n options: DocumentTaxReportFactsOptions,\n ): Promise<DocumentTaxReportFact[]> {\n const contracts = await withTenant({ tenantId: options.tenantId }, () =>\n ContractCollection.create({ db: this.db }).then((collection) =>\n collection.listSnapshotReportContracts({\n tenantId: options.tenantId,\n from: options.from,\n to: options.to,\n }),\n ),\n );\n const candidates = contracts.filter(\n (contract) =>\n classifyContractPurchase(contract) &&\n (!options.currency || contract.currency === options.currency),\n );\n const receivedFacts = candidates\n .filter((contract) => contract.moneyAuthority === 'received')\n .flatMap(receivedPurchaseReportFacts);\n const purchases = candidates.filter(\n (contract) => contract.moneyAuthority !== 'received',\n );\n const ids = purchases.flatMap((contract) =>\n contract.id ? [contract.id] : [],\n );\n if (ids.length === 0) return receivedFacts;\n const byId = new Map(\n purchases.flatMap((contract) =>\n contract.id ? ([[contract.id, contract]] as const) : [],\n ),\n );\n const placeholders = ids.map(() => '?').join(', ');\n const currencyClause = options.currency ? ' AND d.currency = ?' : '';\n const result = await this.db.query(\n `SELECT d.tenant_id, d.document_type, d.document_id, d.currency, d.tax_code_id, d.kind,\n d.tax_component_id, d.tax_component_name, d.liability_account_id,\n d.taxable_minor, d.net_minor, d.tax_minor, d.recoverable_minor\n FROM document_taxes d\n JOIN contracts c\n ON CAST(c.id AS TEXT) = CAST(d.document_id AS TEXT)\n AND c.tenant_id = d.tenant_id\n WHERE CAST(d.tenant_id AS TEXT) = ?\n AND CAST(c.tenant_id AS TEXT) = ?\n AND d.document_type = 'contract'\n AND c.status NOT IN ('draft', 'cancelled')\n AND CAST(d.document_id AS TEXT) IN (${placeholders})${currencyClause}\n ORDER BY c.issue_date ASC, c.id ASC, d.tax_code_id ASC, d.tax_component_id ASC`,\n options.tenantId,\n options.tenantId,\n ...ids,\n ...(options.currency ? [options.currency] : []),\n );\n const calculatedFacts = result.rows.flatMap(\n (row): DocumentTaxReportFact[] => {\n const documentId = requiredText(row.document_id, 'contract id');\n const contract = byId.get(documentId);\n if (!contract) return [];\n return [\n {\n tenantId: requiredText(row.tenant_id, 'tenant id'),\n documentType: contractDocumentType(row.document_type),\n documentId,\n documentNumber: contract.reference.trim() || documentId,\n partyId:\n 'vendorId' in contract &&\n typeof contract.vendorId === 'string' &&\n contract.vendorId.trim()\n ? contract.vendorId\n : null,\n documentKind: 'contract',\n issueDate: reportDate(contract.issueDate, 'contract issue date'),\n status: requiredText(contract.status, 'contract status'),\n direction: 'purchase',\n currency: requiredText(row.currency, 'currency'),\n taxCodeId: row.tax_code_id == null ? null : String(row.tax_code_id),\n taxCodeKind: taxCodeKind(row.kind),\n taxableMinor: storedMinor(row.taxable_minor, 'taxable minor'),\n ...(row.net_minor != null\n ? { netMinor: storedMinor(row.net_minor, 'net minor') }\n : {}),\n taxMinor: storedMinor(row.tax_minor, 'tax minor'),\n recoverableMinor: storedMinor(\n row.recoverable_minor,\n 'recoverable minor',\n ),\n taxComponentId:\n row.tax_component_id == null\n ? null\n : String(row.tax_component_id),\n componentName:\n row.tax_component_name == null\n ? null\n : String(row.tax_component_name),\n liabilityAccountId:\n row.liability_account_id == null\n ? null\n : String(row.liability_account_id),\n // Expenses reference their PurchaseContract id as commitmentId.\n commitmentId: documentId,\n commitmentLineId: null,\n },\n ];\n },\n );\n return [...calculatedFacts, ...receivedFacts];\n }\n\n async findByDocument(\n tenantId: string,\n documentType: CommerceDocumentType,\n documentId: string,\n ): Promise<DocumentTax[]> {\n return this.list({\n where: { tenantId, documentType, documentId },\n orderBy: ['taxCodeId ASC', 'taxComponentId ASC'],\n });\n }\n\n /**\n * Replace one draft document's derived rows on the caller's transaction.\n * Unchanged rows are not saved, preserving their exact persisted bytes.\n *\n * @internal Aggregate persistence only.\n */\n async replaceDerivedForDocument(\n capability: DocumentMoneyWriteCapability,\n input: {\n tenantId: string;\n documentType: CommerceDocumentType;\n documentId: string;\n rows: readonly DocumentTaxWriteInput[];\n },\n ): Promise<DocumentTax[]> {\n if (capability !== DOCUMENT_MONEY_WRITE) {\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'DocumentTax write capability is required',\n );\n }\n const desired = new Map<string, DocumentTaxWriteInput>();\n for (const row of input.rows) {\n if (\n row.tenantId !== input.tenantId ||\n row.documentType !== input.documentType ||\n row.documentId !== input.documentId\n ) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'DocumentTax replacement rows must share tenant and document identity',\n );\n }\n const key = identity(row);\n if (desired.has(key)) {\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'DocumentTax replacement contains a duplicate code/component identity',\n );\n }\n desired.set(key, row);\n }\n\n const existing = await this.findByDocument(\n input.tenantId,\n input.documentType,\n input.documentId,\n );\n const byIdentity = new Map(existing.map((row) => [identity(row), row]));\n const result: DocumentTax[] = [];\n for (const [key, rowInput] of [...desired].sort(([left], [right]) =>\n compareCodeUnits(left, right),\n )) {\n const current = byIdentity.get(key);\n if (current) {\n byIdentity.delete(key);\n if (!unchanged(current, rowInput)) {\n Object.assign(current, rowInput);\n await current.saveDerived(capability);\n }\n result.push(current);\n continue;\n }\n const created = await this.createUnsaved(rowInput);\n await created.saveDerived(capability);\n result.push(created);\n }\n for (const stale of [...byIdentity.values()].sort((left, right) =>\n compareCodeUnits(String(left.id), String(right.id)),\n )) {\n await stale.deleteDerived(capability, {\n expectedUpdatedAt: stale.updated_at ?? undefined,\n });\n }\n return result;\n }\n}\n","import { deterministicId } from './billing/units.js';\nimport { MoneyValidationError } from './money.js';\n\n/** Host-authorized acceptance of a specific frozen local invoice snapshot. */\nexport interface QuickBooksPilotAcceptance {\n readonly obligationId: string;\n readonly quoteVersionId: string;\n readonly moneySnapshotId: string;\n}\n\nexport interface QuickBooksPilotScope {\n readonly tenantId: string;\n readonly realmId: string;\n readonly environment: 'sandbox';\n}\n\nexport interface QuickBooksPilotInvoiceEvidence extends QuickBooksPilotScope {\n readonly externalId: string;\n readonly customerExternalId: string;\n readonly currency: string;\n readonly subtotalMinor: number;\n readonly discountMinor: number;\n readonly taxMinor: number;\n readonly totalMinor: number;\n /** Provider calendar date, YYYY-MM-DD. */\n readonly dueDate: string | null;\n readonly balanceMinor: number;\n readonly voided: boolean;\n /** True only after the adapter has read all relevant linked transaction facts. */\n readonly complete: true;\n readonly transactions: readonly QuickBooksPilotTransaction[];\n}\n\nexport interface QuickBooksPilotTransaction {\n readonly id: string;\n readonly kind: 'payment' | 'credit' | 'refund';\n /** Exact amount attributable to this invoice, not the entire transaction. */\n readonly amountMinor: number;\n}\n\nexport interface QuickBooksPilotProjection {\n readonly balanceMinor: number;\n readonly paidMinor: number;\n readonly creditedMinor: number;\n readonly refundedMinor: number;\n readonly state:\n | 'unpaid'\n | 'partial'\n | 'paid'\n | 'credited'\n | 'refunded'\n | 'voided';\n readonly transactions: readonly QuickBooksPilotTransaction[];\n readonly observedAt: string;\n}\n\nexport function pilotDisagreement(message: string): never {\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n message,\n );\n}\n\n/** Use this ID when preparing the one canonical local invoice for the obligation. */\nexport function quickBooksPilotInvoiceId(\n tenantId: string,\n obligationId: string,\n): Promise<string> {\n return deterministicId(['quickbooks-pilot-invoice', tenantId, obligationId]);\n}\n\nexport function projectQuickBooksPilotEvidence(\n evidence: QuickBooksPilotInvoiceEvidence,\n): QuickBooksPilotProjection {\n const { balanceMinor, totalMinor, voided, transactions } = evidence;\n if (\n evidence.complete !== true ||\n typeof voided !== 'boolean' ||\n !Number.isSafeInteger(balanceMinor) ||\n balanceMinor < 0 ||\n balanceMinor > totalMinor ||\n !Array.isArray(transactions)\n )\n pilotDisagreement('QuickBooks invoice readback is incomplete or invalid');\n\n const seen = new Set<string>();\n const facts: QuickBooksPilotTransaction[] = [];\n const sums = { payment: 0, credit: 0, refund: 0 };\n for (const entry of evidence.transactions) {\n if (!entry || typeof entry !== 'object')\n pilotDisagreement('QuickBooks linked transaction evidence is invalid');\n const { id, kind, amountMinor } = entry;\n if (\n !['payment', 'credit', 'refund'].includes(kind) ||\n typeof id !== 'string' ||\n !id ||\n id.trim() !== id ||\n !Number.isSafeInteger(amountMinor) ||\n amountMinor <= 0 ||\n seen.has(JSON.stringify([kind, id]))\n )\n pilotDisagreement(\n 'QuickBooks linked transaction evidence is invalid or duplicated',\n );\n seen.add(JSON.stringify([kind, id]));\n facts.push({ id, kind, amountMinor });\n sums[kind] += amountMinor;\n if (!Number.isSafeInteger(sums[kind]))\n pilotDisagreement(\n 'QuickBooks linked transaction totals exceed safe minor units',\n );\n }\n\n const paidMinor = sums.payment - sums.refund;\n if (\n paidMinor < 0 ||\n (voided\n ? balanceMinor !== 0 || paidMinor !== 0 || sums.credit !== 0\n : BigInt(paidMinor) + BigInt(sums.credit) + BigInt(balanceMinor) !==\n BigInt(totalMinor))\n )\n pilotDisagreement(\n 'QuickBooks balance disagrees with linked transaction evidence',\n );\n\n let state: QuickBooksPilotProjection['state'] = 'unpaid';\n if (voided) state = 'voided';\n else if (sums.refund > 0) state = 'refunded';\n else if (balanceMinor === 0 && sums.credit > 0) state = 'credited';\n else if (balanceMinor === 0) state = 'paid';\n else if (paidMinor > 0 || sums.credit > 0) state = 'partial';\n\n return {\n balanceMinor,\n paidMinor,\n creditedMinor: sums.credit,\n refundedMinor: sums.refund,\n state,\n transactions: facts,\n observedAt: new Date().toISOString(),\n };\n}\n","import { hasOrdinaryAccountingAuthority } from './ordinary-accounting-internal.js';\nimport { hasRetentionAccountingAuthority } from './retention-accounting-internal.js';\n/** Frozen Invoice projection and guarded accounting-provider synchronization. */\n\nimport { createHash, randomUUID } from 'node:crypto';\nimport type {\n AccountingProvider,\n InvoiceInput,\n QuickBooksCreditMemoRequest,\n QuickBooksInvoiceRequest,\n SyncResult,\n} from '@happyvertical/accounting';\nimport {\n prepareQuickBooksCreditMemoRequest,\n prepareQuickBooksInvoiceRequest,\n requireCreditMemoOperations,\n} from '@happyvertical/accounting';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { assertAccountingWriteAllowed } from './accounting-live-gate.js';\nimport {\n type AccountingSnapshotInput,\n type FrozenAccountingDocumentSnapshot,\n type FrozenQuickBooksTaxMapping,\n mapFrozenAccountingSnapshot,\n} from './accounting-snapshot.js';\nimport { DocumentTaxCollection } from './collections/DocumentTaxCollection.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { InvoiceLineItemCollection } from './collections/InvoiceLineItemCollection.js';\nimport { lockAndReloadInvoice } from './document-money.js';\nimport {\n DOCUMENT_MONEY_WRITE,\n withDocumentMoneyTransaction,\n} from './document-money-internal.js';\nimport type { Invoice } from './models/Invoice.js';\nimport type { InvoiceLineItem } from './models/InvoiceLineItem.js';\nimport { MoneyValidationError } from './money.js';\nimport {\n pilotDisagreement,\n projectQuickBooksPilotEvidence,\n type QuickBooksPilotAcceptance,\n type QuickBooksPilotInvoiceEvidence,\n type QuickBooksPilotProjection,\n type QuickBooksPilotScope,\n quickBooksPilotInvoiceId,\n} from './quickbooks-pilot-evidence.js';\nimport { lockTenantMoneyRuntime } from './tenant-money-barrier.js';\nimport { type AccountingInvoiceInput, InvoiceStatus } from './types/index.js';\n\nexport interface QuickBooksFrozenInvoiceMapping\n extends FrozenQuickBooksTaxMapping {\n readonly realmId: string;\n readonly environment: 'sandbox' | 'production';\n /**\n * The provider customer id for this realm and environment (from the\n * tenant's accounting mapping, `buildFrozenInvoiceMapping`). When set it\n * replaces the flat, unscoped `Invoice.customerExternalId`.\n */\n readonly customerRef?: string;\n}\n\nexport interface FrozenInvoiceAccountingOptions {\n readonly mapping: QuickBooksFrozenInvoiceMapping;\n}\n\nexport interface FrozenInvoiceAccountingInput\n extends FrozenInvoiceAccountingOptions {\n readonly db: DatabaseInterface;\n readonly tenantId: string;\n readonly invoiceId: string;\n}\n\nexport interface FrozenInvoiceAccountingSnapshot {\n readonly document: FrozenAccountingDocumentSnapshot;\n readonly moneySnapshotId: string;\n readonly creditedInvoiceScope?: Readonly<{\n provider: 'quickbooks';\n realmId: string;\n environment: 'sandbox' | 'production';\n }>;\n}\n\ntype FrozenSyncStatus = 'in_flight' | 'unknown' | 'rejected' | 'synced';\n\ninterface FrozenSyncIdentity {\n provider: 'quickbooks';\n documentKind: 'invoice' | 'creditMemo';\n realmId: string;\n environment: 'sandbox' | 'production';\n invoiceId: string;\n moneySnapshotId: string;\n requestId: string;\n payloadHash: string;\n creditedInvoiceExternalId?: string;\n}\n\ninterface FrozenSyncState {\n version: 1;\n status: FrozenSyncStatus;\n identity: FrozenSyncIdentity;\n attemptId: string;\n startedAt: string;\n externalId?: string;\n syncedAt?: string;\n pilot?: QuickBooksPilotAcceptance;\n pilotProjection?: QuickBooksPilotProjection;\n}\n\nexport interface FrozenAccountingSyncResult {\n readonly status: 'synced' | 'unchanged';\n readonly externalId: string;\n readonly syncedAt: Date;\n readonly moneySnapshotId: string;\n}\n\nexport interface SyncFrozenInvoiceToAccountingInput\n extends FrozenInvoiceAccountingInput {\n readonly provider: Pick<\n AccountingProvider,\n 'type' | 'invoices' | 'creditMemos'\n >;\n}\n\nexport interface ReconcileFrozenInvoiceAccountingInput\n extends FrozenInvoiceAccountingInput {\n /** The request descriptor observed before checking QuickBooks. */\n readonly requestId: string;\n /** The unresolved local owner observed with that request descriptor. */\n readonly attemptId: string;\n /**\n * Required only for an interrupted `in_flight` record. The trusted server\n * caller must first establish that the original process/request owner can no\n * longer complete. A provider lookup reporting absence is not sufficient.\n */\n readonly confirmInterruptedOwnerStopped?: true;\n /**\n * The authoritative result of looking up that request in QuickBooks.\n * `absent` means QuickBooks confirmed that no document was created.\n */\n readonly outcome:\n | {\n readonly status: 'found';\n readonly externalId: string;\n readonly syncedAt: Date;\n }\n | { readonly status: 'absent' };\n}\n\nexport interface FrozenAccountingReconciliationReady {\n readonly status: 'ready';\n readonly moneySnapshotId: string;\n readonly requestId: string;\n}\n\ntype FrozenInvoicePushInput = Omit<\n SyncFrozenInvoiceToAccountingInput,\n 'provider'\n> & {\n readonly provider: Omit<\n SyncFrozenInvoiceToAccountingInput['provider'],\n 'invoices'\n > & {\n readonly invoices: Pick<AccountingProvider['invoices'], 'push'>;\n };\n};\n\ninterface PreparedSync {\n kind: 'push';\n documentKind: 'invoice' | 'creditMemo';\n accountingInput: AccountingSnapshotInput;\n invoice: Invoice;\n identity: FrozenSyncIdentity;\n attemptId: string;\n}\n\ninterface UnchangedSync {\n kind: 'unchanged';\n result: FrozenAccountingSyncResult;\n}\n\nfunction requiredText(value: string, label: string): string {\n if (!value || value.trim() !== value)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n `${label} must be a nonempty trimmed string`,\n );\n return value;\n}\n\nfunction assertPrincipalTenant(tenantId: string): void {\n requiredText(tenantId, 'Accounting tenant');\n if (getTenantId() !== tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Accounting sync requires its active principal tenant',\n );\n}\n\nfunction unsupported(message: string): never {\n throw new MoneyValidationError('ACCOUNTING_SYNC_UNSUPPORTED', message);\n}\n\nfunction parseSyncState(value: string): FrozenSyncState | null {\n if (!value) return null;\n try {\n const parsed: unknown = JSON.parse(value);\n if (!parsed || typeof parsed !== 'object') throw new Error('not object');\n const state = parsed as Partial<FrozenSyncState>;\n const identity = state.identity as Partial<FrozenSyncIdentity> | undefined;\n if (\n state.version !== 1 ||\n !['in_flight', 'unknown', 'rejected', 'synced'].includes(\n String(state.status),\n ) ||\n !identity ||\n identity.provider !== 'quickbooks' ||\n typeof identity.realmId !== 'string' ||\n (identity.environment !== 'sandbox' &&\n identity.environment !== 'production') ||\n typeof identity.invoiceId !== 'string' ||\n typeof identity.moneySnapshotId !== 'string' ||\n typeof identity.requestId !== 'string' ||\n typeof identity.payloadHash !== 'string' ||\n (identity.documentKind !== undefined &&\n identity.documentKind !== 'invoice' &&\n identity.documentKind !== 'creditMemo') ||\n (identity.creditedInvoiceExternalId !== undefined &&\n typeof identity.creditedInvoiceExternalId !== 'string') ||\n typeof state.attemptId !== 'string' ||\n typeof state.startedAt !== 'string'\n )\n throw new Error('invalid shape');\n return {\n ...(state as FrozenSyncState),\n identity: {\n ...(identity as FrozenSyncIdentity),\n documentKind: identity.documentKind ?? 'invoice',\n },\n };\n } catch {\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Stored accounting sync state is malformed and requires reconciliation',\n );\n }\n}\n\nfunction sameIdentity(\n left: FrozenSyncIdentity,\n right: FrozenSyncIdentity,\n): boolean {\n return (\n left.provider === right.provider &&\n left.documentKind === right.documentKind &&\n left.realmId === right.realmId &&\n left.environment === right.environment &&\n left.invoiceId === right.invoiceId &&\n left.moneySnapshotId === right.moneySnapshotId &&\n left.requestId === right.requestId &&\n left.payloadHash === right.payloadHash &&\n left.creditedInvoiceExternalId === right.creditedInvoiceExternalId\n );\n}\n\nfunction sameSyncScope(\n identity: FrozenSyncIdentity,\n snapshot: FrozenInvoiceAccountingSnapshot,\n mapping: QuickBooksFrozenInvoiceMapping,\n): boolean {\n return (\n identity.provider === 'quickbooks' &&\n identity.documentKind === snapshot.document.kind &&\n identity.realmId === mapping.realmId &&\n identity.environment === mapping.environment &&\n identity.invoiceId === snapshot.document.id &&\n identity.moneySnapshotId === snapshot.moneySnapshotId\n );\n}\n\nfunction sum(values: readonly number[]): number {\n let result = 0n;\n for (const value of values) {\n if (!Number.isSafeInteger(value))\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n 'Frozen accounting facts require safe integer minor units',\n );\n result += BigInt(value);\n }\n if (\n result > BigInt(Number.MAX_SAFE_INTEGER) ||\n result < BigInt(Number.MIN_SAFE_INTEGER)\n )\n throw new MoneyValidationError(\n 'UNSAFE_MONEY',\n 'Frozen accounting totals exceed safe integer minor units',\n );\n return Number(result);\n}\n\nfunction componentTotals(\n lines: readonly InvoiceLineItem[],\n): Map<string, number> {\n const totals = new Map<string, number>();\n for (const line of lines) {\n for (const component of line.taxComponents) {\n totals.set(\n component.taxComponentId,\n sum([totals.get(component.taxComponentId) ?? 0, component.taxMinor]),\n );\n }\n }\n return totals;\n}\n\nasync function loadSnapshot(\n db: DatabaseInterface,\n tenantId: string,\n invoice: Invoice,\n): Promise<FrozenInvoiceAccountingSnapshot> {\n if (invoice.moneyAuthority === 'historical')\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_UNSUPPORTED',\n 'Historical sales source money requires an explicit reviewed provider representation; no quantities or tax breakdown will be invented',\n );\n\n if (invoice.tenantId !== tenantId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Accounting invoice cannot cross tenant boundaries',\n );\n if (invoice.status === InvoiceStatus.DRAFT || !invoice.moneySnapshotId)\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Accounting sync requires an issued frozen invoice',\n );\n if (\n (invoice.getPayableFacts().retainedRemainingMinor > 0 ||\n invoice.retentionAccountingStateJson) &&\n !hasRetentionAccountingAuthority(invoice.tenantId, invoice.id)\n )\n unsupported(\n 'Unreleased retention requires a provider payable representation',\n );\n if (invoice.status === InvoiceStatus.CANCELLED)\n unsupported('Cancelled documents cannot be created by accounting sync');\n\n const lines = await (\n await InvoiceLineItemCollection.create({ db })\n ).findByInvoice(requiredText(invoice.id ?? '', 'Accounting invoice ID'));\n if (lines.length === 0)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen accounting invoice requires at least one line',\n );\n for (const line of lines) {\n if (\n line.tenantId !== tenantId ||\n line.moneySnapshotId !== invoice.moneySnapshotId\n )\n throw new MoneyValidationError(\n 'FROZEN_DOCUMENT',\n 'Accounting line snapshot does not match its frozen invoice',\n );\n }\n const taxes = await (\n await DocumentTaxCollection.create({ db })\n ).findByDocument(tenantId, invoice.kind, invoice.id ?? '');\n const lineTax = sum(lines.map((line) => line.taxAmount));\n const storedTax = sum(taxes.map((row) => row.taxMinor));\n if (\n lineTax + invoice.providerTaxAmount !== invoice.taxAmount ||\n storedTax !== lineTax\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen line, DocumentTax, and invoice tax totals do not reconcile',\n );\n const lineComponents = componentTotals(lines);\n const storedComponents = new Map<string, number>();\n for (const row of taxes) {\n if (!row.taxComponentId) continue;\n storedComponents.set(\n row.taxComponentId,\n sum([storedComponents.get(row.taxComponentId) ?? 0, row.taxMinor]),\n );\n }\n if (\n [...new Set([...lineComponents.keys(), ...storedComponents.keys()])].some(\n (id) => (lineComponents.get(id) ?? 0) !== (storedComponents.get(id) ?? 0),\n )\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen line components do not reconcile to DocumentTax',\n );\n\n let creditedInvoiceExternalId: string | undefined;\n let creditedInvoiceScope:\n | FrozenInvoiceAccountingSnapshot['creditedInvoiceScope']\n | undefined;\n if (invoice.kind === 'credit_note') {\n if (!invoice.creditedInvoiceId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen credit note requires its credited invoice',\n );\n const source = await (await InvoiceCollection.create({ db })).get(\n { id: invoice.creditedInvoiceId },\n { cache: false },\n );\n if (!source || source.tenantId !== tenantId || !source.externalId)\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen credit note requires the source invoice external ID',\n );\n if (\n source.retentionAccountingStateJson &&\n !hasRetentionAccountingAuthority(source.tenantId, source.id)\n )\n unsupported(\n 'Provider-retained credit requires the retained-first accounting adjustment command',\n );\n if (\n source.ordinaryAccountingStateJson &&\n !hasOrdinaryAccountingAuthority(source.tenantId, source.id)\n )\n unsupported(\n 'Provider ordinary credit requires its owning accounting adjustment command',\n );\n const sourceState = parseSyncState(source.accountingSyncStateJson);\n if (\n sourceState?.status !== 'synced' ||\n sourceState.externalId !== source.externalId\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Frozen credit note requires a reconciled source invoice accounting identity',\n );\n creditedInvoiceExternalId = source.externalId;\n creditedInvoiceScope = {\n provider: sourceState.identity.provider,\n realmId: sourceState.identity.realmId,\n environment: sourceState.identity.environment,\n };\n }\n\n return {\n moneySnapshotId: invoice.moneySnapshotId,\n creditedInvoiceScope,\n document: {\n kind: invoice.kind === 'invoice' ? 'invoice' : 'creditMemo',\n id: invoice.id ?? '',\n number: invoice.invoiceNumber,\n customerId: invoice.customerId,\n customerExternalId: invoice.customerExternalId || undefined,\n issueDate: invoice.issueDate,\n dueDate: invoice.dueDate ?? undefined,\n currency: invoice.currency,\n lineAmountType: invoice.lineAmountType,\n taxRoundingScope: invoice.taxRoundingScope,\n subtotalMinor: invoice.subtotal,\n taxAmountMinor: invoice.taxAmount,\n providerTaxMinor: invoice.providerTaxAmount,\n totalAmountMinor: invoice.totalAmount,\n lineItems: lines.map((line) => ({\n id: requiredText(line.id ?? '', 'Accounting line ID'),\n description: line.description,\n sku: line.sku || undefined,\n quantity: line.quantity,\n unitPriceMinor: line.unitPrice,\n discountMinor: line.discount,\n amountMinor:\n invoice.lineAmountType === 'inclusive'\n ? line.amount - line.taxAmount\n : line.amount,\n taxMinor: line.taxAmount,\n taxComponents: structuredClone(line.taxComponents),\n })),\n creditedInvoiceExternalId,\n externalId: invoice.externalId || undefined,\n reference: invoice.reference || undefined,\n memo: invoice.customerNotes || undefined,\n },\n };\n}\n\nfunction validateMapping(mapping: QuickBooksFrozenInvoiceMapping): void {\n requiredText(mapping.realmId, 'QuickBooks realm ID');\n if (mapping.environment !== 'sandbox' && mapping.environment !== 'production')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'QuickBooks environment must be sandbox or production',\n );\n if (!mapping.lines || typeof mapping.lines !== 'object')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'QuickBooks line mapping is required',\n );\n if (!mapping.components || typeof mapping.components !== 'object')\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'QuickBooks component mapping is required',\n );\n}\n\nfunction toSdkInput(\n snapshot: FrozenInvoiceAccountingSnapshot,\n mapping: QuickBooksFrozenInvoiceMapping,\n): AccountingSnapshotInput {\n validateMapping(mapping);\n if (\n snapshot.document.kind === 'creditMemo' &&\n (snapshot.creditedInvoiceScope?.provider !== 'quickbooks' ||\n snapshot.creditedInvoiceScope.realmId !== mapping.realmId ||\n snapshot.creditedInvoiceScope.environment !== mapping.environment)\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Credit note and credited invoice must use the same accounting provider, realm, and environment',\n );\n if (\n snapshot.document.lineItems.some(\n (line) =>\n line.quantity < 0 || line.amountMinor < 0 || line.unitPriceMinor < 0,\n )\n )\n unsupported(\n 'Signed-line accounting sync requires a public accounting SDK signed-line representation',\n );\n return mapFrozenAccountingSnapshot(\n mapping.customerRef\n ? {\n ...snapshot.document,\n customerExternalId: mapping.customerRef,\n }\n : snapshot.document,\n mapping,\n );\n}\n\n/** Load and validate one authoritative issued snapshot without provider I/O. */\nexport async function loadFrozenInvoiceAccountingSnapshot(\n input: Omit<FrozenInvoiceAccountingInput, 'mapping'>,\n): Promise<FrozenInvoiceAccountingSnapshot> {\n assertPrincipalTenant(input.tenantId);\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n return loadSnapshot(tx, input.tenantId, invoice);\n });\n}\n\n/** Convert authoritative persisted facts into the released SDK invoice shape. */\nexport async function toFrozenInvoiceAccountingInput(\n input: FrozenInvoiceAccountingInput,\n): Promise<AccountingInvoiceInput> {\n const snapshot = await loadFrozenInvoiceAccountingSnapshot(input);\n return toSdkInput(snapshot, input.mapping) as AccountingInvoiceInput;\n}\n\nfunction prepareRequest(\n snapshot: FrozenInvoiceAccountingSnapshot,\n mapping: QuickBooksFrozenInvoiceMapping,\n sdkInput: AccountingSnapshotInput,\n): {\n accountingInput: AccountingSnapshotInput;\n request: QuickBooksInvoiceRequest | QuickBooksCreditMemoRequest;\n} {\n const identity = {\n requestId: requestIdFor(snapshot, mapping),\n realmId: mapping.realmId,\n environment: mapping.environment,\n } as const;\n if (snapshot.document.kind === 'invoice') {\n if (!('invoiceNumber' in sdkInput))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen invoice mapping produced the wrong accounting document kind',\n );\n const request = prepareQuickBooksInvoiceRequest(sdkInput, identity);\n return {\n request,\n accountingInput: {\n ...sdkInput,\n idempotencyKey: request.requestId,\n quickbooksRequest: request,\n },\n };\n }\n if (!('creditMemoNumber' in sdkInput))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Frozen credit-note mapping produced the wrong accounting document kind',\n );\n const request = prepareQuickBooksCreditMemoRequest(sdkInput, identity);\n return {\n request,\n accountingInput: {\n ...sdkInput,\n quickbooksCreditMemoRequest: request,\n },\n };\n}\n\nfunction requestIdFor(\n snapshot: FrozenInvoiceAccountingSnapshot,\n mapping: QuickBooksFrozenInvoiceMapping,\n): string {\n const digest = createHash('sha256')\n .update(\n JSON.stringify([\n 'quickbooks',\n mapping.realmId,\n mapping.environment,\n snapshot.document.id,\n snapshot.moneySnapshotId,\n ]),\n )\n .digest('hex');\n return `smrt_${digest.slice(0, 40)}`;\n}\n\nasync function prepareSync(\n input: FrozenInvoicePushInput,\n pilot?: QuickBooksPilotAcceptance,\n): Promise<PreparedSync | UnchangedSync> {\n if (input.provider.type !== 'quickbooks')\n unsupported(\n `Frozen accounting sync does not support provider '${input.provider.type}'`,\n );\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n if (\n invoice.ordinaryAccountingStateJson &&\n !hasOrdinaryAccountingAuthority(invoice.tenantId, invoice.id)\n )\n unsupported(\n 'Provider ordinary invoice requires its owning accounting command',\n );\n const snapshot = await loadSnapshot(tx, input.tenantId, invoice);\n const sdkInput = toSdkInput(snapshot, input.mapping);\n const prior = parseSyncState(invoice.accountingSyncStateJson);\n checkPilotAcceptance(invoice, prior, pilot);\n if (pilot) assertPilotInvoice(invoice);\n if (\n prior?.status === 'synced' &&\n sameSyncScope(prior.identity, snapshot, input.mapping)\n ) {\n if (!prior.externalId || !prior.syncedAt)\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Completed accounting sync state is incomplete',\n );\n return {\n kind: 'unchanged',\n result: {\n status: 'unchanged',\n externalId: prior.externalId,\n syncedAt: new Date(prior.syncedAt),\n moneySnapshotId: prior.identity.moneySnapshotId,\n },\n };\n }\n if (prior?.status === 'in_flight')\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_IN_PROGRESS',\n 'Another accounting sync owns this invoice',\n );\n if (prior?.status === 'unknown')\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'The prior accounting write outcome is unknown and must be reconciled',\n );\n if (prior?.status === 'synced' || invoice.externalId)\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Invoice accounting identity changed across provider, realm, environment, or snapshot',\n );\n if (snapshot.document.kind === 'creditMemo') {\n try {\n requireCreditMemoOperations(input.provider);\n } catch (error) {\n unsupported(error instanceof Error ? error.message : String(error));\n }\n }\n const { accountingInput, request } = prepareRequest(\n snapshot,\n input.mapping,\n sdkInput,\n );\n const identity = frozenIdentity(snapshot, request);\n if (prior && !sameIdentity(prior.identity, identity))\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Invoice accounting identity changed across provider, realm, environment, or snapshot',\n );\n\n const attemptId = randomUUID();\n invoice.accountingSyncStateJson = JSON.stringify({\n version: 1,\n status: 'in_flight',\n identity,\n attemptId,\n startedAt: new Date().toISOString(),\n pilot,\n } satisfies FrozenSyncState);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n return {\n kind: 'push',\n documentKind: snapshot.document.kind,\n accountingInput,\n invoice,\n identity,\n attemptId,\n };\n });\n}\n\nasync function recordFailure(\n input: FrozenInvoicePushInput,\n prepared: PreparedSync,\n error: unknown,\n pushReturned: boolean,\n): Promise<void> {\n const rejected =\n !pushReturned &&\n !!error &&\n typeof error === 'object' &&\n (error as { code?: unknown }).code === 'QBO_WRITE_ERROR' &&\n (error as { outcome?: unknown }).outcome === 'rejected';\n await withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n const state = parseSyncState(invoice.accountingSyncStateJson);\n if (\n !state ||\n state.attemptId !== prepared.attemptId ||\n !sameIdentity(state.identity, prepared.identity)\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Accounting sync ownership changed after provider failure',\n );\n invoice.accountingSyncStateJson = JSON.stringify({\n ...state,\n status: rejected ? 'rejected' : 'unknown',\n } satisfies FrozenSyncState);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n });\n}\n\nasync function saveSyncedInvoice(\n invoice: Invoice,\n state: FrozenSyncState,\n externalId: string,\n syncedAt: Date,\n): Promise<FrozenAccountingSyncResult> {\n invoice.externalId = externalId;\n invoice.externalProvider = state.identity.provider;\n invoice.syncedAt = syncedAt;\n invoice.accountingSyncStateJson = JSON.stringify({\n ...state,\n status: 'synced',\n externalId,\n syncedAt: syncedAt.toISOString(),\n } satisfies FrozenSyncState);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n return {\n status: 'synced',\n externalId,\n syncedAt,\n moneySnapshotId: state.identity.moneySnapshotId,\n };\n}\n\nasync function recordSuccess(\n input: FrozenInvoicePushInput,\n prepared: PreparedSync,\n result: SyncResult,\n pilotProjection?: QuickBooksPilotProjection,\n): Promise<FrozenAccountingSyncResult> {\n requiredText(result.externalId, 'Accounting external ID');\n if (\n !(result.syncedAt instanceof Date) ||\n Number.isNaN(result.syncedAt.valueOf())\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Accounting provider returned an invalid sync timestamp',\n );\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n const state = parseSyncState(invoice.accountingSyncStateJson);\n if (\n state?.status !== 'in_flight' ||\n state.attemptId !== prepared.attemptId ||\n !sameIdentity(state.identity, prepared.identity) ||\n invoice.moneySnapshotId !== prepared.identity.moneySnapshotId ||\n (state.pilot &&\n invoice.customerExternalId !== prepared.invoice.customerExternalId)\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Accounting sync ownership or frozen snapshot changed after provider write',\n );\n if (state.pilot)\n await assertUnusedPilotExternalId(\n tx,\n invoice,\n state.identity,\n result.externalId,\n );\n return saveSyncedInvoice(\n invoice,\n { ...state, pilotProjection },\n result.externalId,\n result.syncedAt,\n );\n });\n}\n\n/**\n * Push one frozen invoice exactly once per provider/realm/environment/snapshot.\n * Local and remote writes are intentionally separate phases. An unknown remote\n * outcome is persisted and requires reconciliation instead of a blind replay.\n */\nexport async function syncFrozenInvoiceToAccounting(\n input: SyncFrozenInvoiceToAccountingInput,\n): Promise<FrozenAccountingSyncResult> {\n return syncFrozenInvoice(input);\n}\n\nasync function syncFrozenInvoice(\n input: FrozenInvoicePushInput,\n pilot?: {\n acceptance: QuickBooksPilotAcceptance;\n verify(\n invoice: Invoice,\n result: SyncResult,\n ): Promise<QuickBooksPilotProjection>;\n },\n): Promise<FrozenAccountingSyncResult> {\n assertPrincipalTenant(input.tenantId);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n scope: input.mapping,\n });\n const prepared = await prepareSync(input, pilot?.acceptance);\n if (prepared.kind === 'unchanged') return prepared.result;\n let result: SyncResult;\n let pushReturned = false;\n let projection: QuickBooksPilotProjection | undefined;\n try {\n if (prepared.documentKind === 'invoice') {\n if (!('invoiceNumber' in prepared.accountingInput))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Prepared invoice has the wrong accounting document kind',\n );\n result = await input.provider.invoices.push(prepared.accountingInput);\n } else {\n if (!('creditMemoNumber' in prepared.accountingInput))\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Prepared credit note has the wrong accounting document kind',\n );\n result = await requireCreditMemoOperations(input.provider).push(\n prepared.accountingInput,\n );\n }\n pushReturned = true;\n projection = await pilot?.verify(prepared.invoice, result);\n } catch (error) {\n await recordFailure(input, prepared, error, pushReturned);\n throw error;\n }\n return recordSuccess(input, prepared, result, projection);\n}\n\n/**\n * Record an authoritative QuickBooks lookup after an unknown/interrupted push.\n * The caller must first query the exact persisted request descriptor and must\n * only report `absent` when the provider confirms that no document exists.\n */\nexport async function reconcileFrozenInvoiceAccountingSync(\n input: ReconcileFrozenInvoiceAccountingInput,\n): Promise<FrozenAccountingSyncResult | FrozenAccountingReconciliationReady> {\n assertPrincipalTenant(input.tenantId);\n requiredText(input.requestId, 'Accounting request ID');\n requiredText(input.attemptId, 'Accounting attempt ID');\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n const snapshot = await loadSnapshot(tx, input.tenantId, invoice);\n const sdkInput = toSdkInput(snapshot, input.mapping);\n const { request } = prepareRequest(snapshot, input.mapping, sdkInput);\n const identity = frozenIdentity(snapshot, request);\n const state = parseSyncState(invoice.accountingSyncStateJson);\n checkPilotAcceptance(invoice, state);\n if (\n !state ||\n (state.status !== 'unknown' && state.status !== 'in_flight') ||\n state.identity.requestId !== input.requestId ||\n state.attemptId !== input.attemptId ||\n !sameIdentity(state.identity, identity)\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n 'Accounting reconciliation does not match the unresolved frozen request',\n );\n if (\n state.status === 'in_flight' &&\n input.confirmInterruptedOwnerStopped !== true\n )\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_IN_PROGRESS',\n 'Accounting reconciliation cannot release a live sync owner',\n );\n\n if (input.outcome.status === 'absent') {\n invoice.accountingSyncStateJson = JSON.stringify({\n ...state,\n status: 'rejected',\n } satisfies FrozenSyncState);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n return {\n status: 'ready',\n moneySnapshotId: identity.moneySnapshotId,\n requestId: identity.requestId,\n };\n }\n\n const externalId = requiredText(\n input.outcome.externalId,\n 'Accounting external ID',\n );\n if (\n !(input.outcome.syncedAt instanceof Date) ||\n Number.isNaN(input.outcome.syncedAt.valueOf())\n )\n throw new MoneyValidationError(\n 'INVALID_MONEY_INPUT',\n 'Accounting reconciliation requires a valid sync timestamp',\n );\n return saveSyncedInvoice(\n invoice,\n state,\n externalId,\n input.outcome.syncedAt,\n );\n });\n}\n\n/** Read the persisted coordination state for diagnostics/reconciliation. */\nexport function readFrozenAccountingSyncState(invoice: Invoice): Readonly<{\n status: FrozenSyncStatus;\n provider: string;\n realmId: string;\n environment: string;\n moneySnapshotId: string;\n requestId: string;\n attemptId: string;\n startedAt: string;\n documentKind: 'invoice' | 'creditMemo';\n request: QuickBooksInvoiceRequest | QuickBooksCreditMemoRequest;\n pilotProjection?: QuickBooksPilotProjection;\n}> | null {\n const state = parseSyncState(invoice.accountingSyncStateJson);\n if (!state) return null;\n return {\n status: state.status,\n provider: state.identity.provider,\n realmId: state.identity.realmId,\n environment: state.identity.environment,\n documentKind: state.identity.documentKind,\n moneySnapshotId: state.identity.moneySnapshotId,\n requestId: state.identity.requestId,\n attemptId: state.attemptId,\n startedAt: state.startedAt,\n ...(state.pilotProjection\n ? { pilotProjection: state.pilotProjection }\n : {}),\n request: {\n requestId: state.identity.requestId,\n realmId: state.identity.realmId,\n environment: state.identity.environment,\n payloadHash: state.identity.payloadHash,\n ...(state.identity.documentKind === 'creditMemo'\n ? {\n creditedInvoiceExternalId: requiredText(\n state.identity.creditedInvoiceExternalId ?? '',\n 'Credited invoice external ID',\n ),\n }\n : {}),\n },\n };\n}\n\nfunction frozenIdentity(\n snapshot: FrozenInvoiceAccountingSnapshot,\n request: QuickBooksInvoiceRequest | QuickBooksCreditMemoRequest,\n): FrozenSyncIdentity {\n return {\n provider: 'quickbooks',\n documentKind: snapshot.document.kind,\n realmId: request.realmId,\n environment: request.environment,\n invoiceId: snapshot.document.id,\n moneySnapshotId: snapshot.moneySnapshotId,\n requestId: request.requestId,\n payloadHash: request.payloadHash,\n creditedInvoiceExternalId:\n 'creditedInvoiceExternalId' in request\n ? request.creditedInvoiceExternalId\n : undefined,\n };\n}\n\nfunction checkPilotAcceptance(\n invoice: Invoice,\n state: FrozenSyncState | null,\n acceptance?: QuickBooksPilotAcceptance,\n): void {\n if (acceptance && acceptance.moneySnapshotId !== invoice.moneySnapshotId)\n pilotDisagreement(\n 'QuickBooks pilot acceptance does not match the frozen invoice',\n );\n if (\n state &&\n (state.pilot?.obligationId !== acceptance?.obligationId ||\n state.pilot?.quoteVersionId !== acceptance?.quoteVersionId ||\n state.pilot?.moneySnapshotId !== acceptance?.moneySnapshotId)\n )\n pilotDisagreement(\n 'QuickBooks pilot acceptance differs from the existing reservation',\n );\n}\n\nasync function assertUnusedPilotExternalId(\n db: DatabaseInterface,\n invoice: Invoice,\n identity: FrozenSyncIdentity,\n externalId: string,\n): Promise<void> {\n const invoices = await InvoiceCollection.create({ db });\n const matches = await invoices.list({\n where: { externalId, tenantId: invoice.tenantId },\n });\n for (const other of matches) {\n if (other.id === invoice.id) continue;\n const binding = parseSyncState(other.accountingSyncStateJson);\n if (\n !binding ||\n (binding.identity.realmId === identity.realmId &&\n binding.identity.environment === identity.environment)\n )\n pilotDisagreement(\n 'QuickBooks invoice is already bound to another local invoice',\n );\n }\n}\n\n/** The pilot has a fixture transport only. No credential or SDK factory is accepted. */\nexport interface QuickBooksPilotFixture {\n readonly mode: 'fixture';\n readonly scope: QuickBooksPilotScope;\n pushInvoice(input: InvoiceInput): Promise<SyncResult>;\n readInvoice(externalId: string): Promise<QuickBooksPilotInvoiceEvidence>;\n findInvoiceForRequest?(request: QuickBooksInvoiceRequest): Promise<{\n readonly requestId: string;\n readonly payloadHash: string;\n readonly invoice: QuickBooksPilotInvoiceEvidence;\n } | null>;\n}\n\nexport type QuickBooksPilotCommand = FrozenInvoiceAccountingInput & {\n readonly acceptance: QuickBooksPilotAcceptance;\n readonly fixture: QuickBooksPilotFixture;\n} & (\n | { readonly action: 'issue' | 'poll' }\n | { readonly action: 'link'; readonly externalId: string }\n | {\n readonly action: 'recover';\n readonly confirmInterruptedOwnerStopped?: true;\n }\n );\n\nfunction assertPilotInvoice(invoice: Invoice): void {\n if (\n invoice.kind !== 'invoice' ||\n invoice.totalAmount <= 0 ||\n !invoice.customerExternalId ||\n invoice.ordinaryAccountingStateJson ||\n invoice.retentionAccountingStateJson\n )\n unsupported(\n 'QuickBooks pilot requires an ordinary positive invoice and a mapped customer',\n );\n}\n\nfunction checkPilotEvidence(\n evidence: QuickBooksPilotInvoiceEvidence,\n scope: QuickBooksPilotScope,\n externalId: string,\n invoice: Pick<\n Invoice,\n | 'customerExternalId'\n | 'currency'\n | 'subtotal'\n | 'discountTotal'\n | 'taxAmount'\n | 'totalAmount'\n | 'dueDate'\n >,\n): QuickBooksPilotProjection {\n if (\n !evidence ||\n evidence.tenantId !== scope.tenantId ||\n evidence.realmId !== scope.realmId ||\n evidence.environment !== scope.environment ||\n evidence.externalId !== externalId ||\n !invoice.customerExternalId ||\n evidence.customerExternalId !== invoice.customerExternalId ||\n evidence.currency !== invoice.currency ||\n evidence.subtotalMinor !== invoice.subtotal ||\n evidence.discountMinor !== invoice.discountTotal ||\n evidence.taxMinor !== invoice.taxAmount ||\n evidence.totalMinor !== invoice.totalAmount ||\n evidence.dueDate !== (invoice.dueDate?.toISOString().slice(0, 10) ?? null)\n )\n pilotDisagreement('QuickBooks invoice identity or frozen money differs');\n return projectQuickBooksPilotEvidence(evidence);\n}\n\n/**\n * Issue, link, recover or poll one accepted invoice using deterministic fixture\n * evidence. The trusted host authorizes the quote acceptance and manual link.\n * Every invocation revalidates the active tenant and fixture scope.\n */\nexport async function runQuickBooksPilot(\n input: QuickBooksPilotCommand,\n): Promise<QuickBooksPilotProjection> {\n assertPrincipalTenant(input.tenantId);\n const { acceptance, fixture, mapping } = input;\n if (!['issue', 'poll', 'link', 'recover'].includes(input.action))\n unsupported('Unsupported QuickBooks pilot action');\n if (\n fixture?.mode !== 'fixture' ||\n fixture.scope?.environment !== 'sandbox' ||\n mapping.environment !== 'sandbox' ||\n fixture.scope.tenantId !== input.tenantId ||\n fixture.scope.realmId !== mapping.realmId\n )\n unsupported(\n 'QuickBooks pilot requires an explicitly scoped sandbox fixture',\n );\n requiredText(mapping.realmId, 'QuickBooks realm');\n requiredText(acceptance.quoteVersionId, 'Accepted quote version');\n requiredText(acceptance.moneySnapshotId, 'Accepted money snapshot');\n const obligationId = requiredText(\n acceptance.obligationId,\n 'Accepted obligation',\n );\n if (\n input.invoiceId !==\n (await quickBooksPilotInvoiceId(input.tenantId, obligationId))\n )\n pilotDisagreement(\n 'QuickBooks pilot requires the canonical obligation invoice',\n );\n const scope = { ...fixture.scope };\n\n if (input.action === 'issue') {\n // The existing owner persists the descriptor before push and records any\n // unverifiable readback after push as an unresolved create.\n let projection: QuickBooksPilotProjection | undefined;\n await syncFrozenInvoice(\n {\n ...input,\n provider: {\n type: 'quickbooks',\n invoices: { push: (invoice) => fixture.pushInvoice(invoice) },\n },\n },\n {\n acceptance,\n async verify(invoice, result) {\n if (result.action !== 'created')\n pilotDisagreement('QuickBooks pilot did not return a new invoice');\n const id = requiredText(result.externalId, 'QuickBooks invoice ID');\n projection = checkPilotEvidence(\n await fixture.readInvoice(id),\n scope,\n id,\n invoice,\n );\n return projection;\n },\n },\n );\n if (projection) return projection;\n }\n\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n const state = parseSyncState(invoice.accountingSyncStateJson);\n checkPilotAcceptance(invoice, state, acceptance);\n assertPilotInvoice(invoice);\n const snapshot = await loadSnapshot(tx, input.tenantId, invoice);\n // Validate the original create descriptor even after a binding exists.\n const sdkInput = toSdkInput(\n {\n ...snapshot,\n document: { ...snapshot.document, externalId: undefined },\n },\n mapping,\n );\n const { request } = prepareRequest(snapshot, mapping, sdkInput);\n const identity = frozenIdentity(snapshot, request);\n if (state && !sameIdentity(state.identity, identity))\n pilotDisagreement('QuickBooks pilot binding or frozen request changed');\n\n if (state?.status === 'synced') {\n const id = requiredText(state.externalId ?? '', 'QuickBooks invoice ID');\n if (\n invoice.externalId !== id ||\n invoice.externalProvider !== 'quickbooks'\n )\n pilotDisagreement('QuickBooks pilot provider binding changed');\n if (input.action === 'link' && input.externalId !== id)\n pilotDisagreement('QuickBooks invoice already has a different binding');\n const projection = checkPilotEvidence(\n await fixture.readInvoice(id),\n scope,\n id,\n invoice,\n );\n invoice.accountingSyncStateJson = JSON.stringify({\n ...state,\n pilotProjection: projection,\n } satisfies FrozenSyncState);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n return projection;\n }\n\n let externalId: string;\n let evidence: QuickBooksPilotInvoiceEvidence;\n if (\n input.action === 'link' &&\n (!state || state.status === 'rejected') &&\n !invoice.externalId\n ) {\n externalId = requiredText(input.externalId, 'QuickBooks invoice ID');\n evidence = await fixture.readInvoice(externalId);\n } else if (\n input.action === 'recover' &&\n (state?.status === 'unknown' ||\n (state?.status === 'in_flight' &&\n input.confirmInterruptedOwnerStopped === true))\n ) {\n const match = await fixture.findInvoiceForRequest?.(request);\n if (\n !match ||\n match.requestId !== identity.requestId ||\n match.payloadHash !== identity.payloadHash\n )\n pilotDisagreement(\n 'QuickBooks create remains unresolved without verified request identity',\n );\n evidence = match.invoice;\n externalId = requiredText(evidence.externalId, 'QuickBooks invoice ID');\n } else {\n pilotDisagreement(\n 'QuickBooks pilot has no verified invoice binding for this action',\n );\n }\n\n const projection = checkPilotEvidence(evidence, scope, externalId, invoice);\n await assertUnusedPilotExternalId(tx, invoice, identity, externalId);\n await saveSyncedInvoice(\n invoice,\n {\n version: 1,\n status: 'synced',\n identity,\n attemptId: state?.attemptId ?? randomUUID(),\n startedAt: state?.startedAt ?? projection.observedAt,\n pilot: acceptance,\n pilotProjection: projection,\n },\n externalId,\n new Date(projection.observedAt),\n );\n return projection;\n });\n}\n","/** Exact, transport-neutral invoice line calculation shared by browser and server. */\nimport {\n calculateMoneyDocument,\n type MoneyLineResult,\n type MoneyTaxCodeInput,\n MoneyValidationError,\n} from '../../money.js';\n\n/** All monetary outputs are safe integer minor units. */\nexport interface InvoiceCalculatedAmounts {\n /** Rounded quantity times unit price before discounts. */\n grossMinor: number;\n /** Discount in minor units before tax; it carries the gross sign. */\n discountMinor: number;\n /** Gross less discount. */\n subtotalMinor: number;\n /** Rounded tax on the discounted subtotal. */\n taxMinor: number;\n /** Subtotal plus tax. */\n totalMinor: number;\n}\n\ninterface Decimal {\n coefficient: bigint;\n scale: bigint;\n}\n\n/** Existing model monetary inputs, independent of currency display precision. */\nexport interface InvoiceMinorLineInput {\n /** Nonnegative quantity with at most six fractional digits. */\n quantity: number;\n /** Signed integer minor units; negative prices support existing credit lines. */\n unitPrice: number;\n /** Flat discount in signed integer minor units; it carries the gross sign. */\n discount: number;\n /** Resolved tax fraction, at most eight fractional digits, in [0, 1]. */\n taxRate: number;\n}\n\nfunction numberDecimal(value: number, precision: number): Decimal {\n if (!Number.isFinite(value) || value < 0 || value > Number.MAX_SAFE_INTEGER)\n throw new RangeError(\n 'Invoice rate or quantity is outside the supported range.',\n );\n // The shortest canonical decimal preserves the caller's Number. toFixed can\n // invent different decimal digits that round back to that same binary value.\n const [mantissa, exponent = '0'] = value.toString().split('e');\n const [whole, fraction = ''] = mantissa.split('.');\n const places = fraction.length - Number(exponent);\n if (places > precision)\n throw new RangeError(\n 'Invoice rate or quantity exceeds the supported decimal precision.',\n );\n const coefficient = BigInt(whole + fraction);\n return places < 0\n ? { coefficient: coefficient * 10n ** BigInt(-places), scale: 1n }\n : { coefficient, scale: 10n ** BigInt(places) };\n}\n\nfunction decimalText(value: Decimal): string {\n if (value.scale === 1n) return value.coefficient.toString();\n const places = value.scale.toString().length - 1;\n const digits = value.coefficient.toString().padStart(places + 1, '0');\n const split = digits.length - places;\n return `${digits.slice(0, split)}.${digits.slice(split)}`\n .replace(/0+$/, '')\n .replace(/\\.$/, '');\n}\n\n/** An in-memory single-rate tax code for editor previews of a percent rate. */\nfunction rateTaxCode(percent: string): MoneyTaxCodeInput {\n const normalized =\n percent\n .replace(/^0+(?=\\d)/, '')\n .replace(/(\\.\\d*?)0+$/, '$1')\n .replace(/\\.$/, '') || '0';\n return normalized === '0'\n ? { id: 'rate-zero', kind: 'zero-rated', components: [] }\n : {\n id: `rate-${normalized}`,\n kind: 'taxable',\n components: [\n {\n taxComponentId: `rate-${normalized}`,\n percent: normalized,\n compound: false,\n },\n ],\n };\n}\n\n/**\n * Exact model calculation, including existing negative-price credit lines.\n * Throws before persistence for unsafe money, invalid quantity, or invalid tax.\n */\nexport function calculateInvoiceMinorLine(\n input: InvoiceMinorLineInput,\n): InvoiceCalculatedAmounts {\n for (const amount of [input.unitPrice, input.discount])\n if (!Number.isSafeInteger(amount))\n throw new RangeError('Invoice money must be safe integer minor units.');\n if (input.taxRate > 1)\n throw new RangeError('Invoice tax rate is outside the supported range.');\n numberDecimal(input.quantity, 6);\n const tax = numberDecimal(input.taxRate, 8);\n const percent = decimalText({\n coefficient: tax.coefficient * 100n,\n scale: tax.scale,\n });\n let result: MoneyLineResult;\n try {\n result = calculateMoneyDocument({\n currency: 'USD',\n lineAmountType: 'exclusive',\n roundingScope: 'line',\n lines: [\n {\n id: 'line',\n quantity: input.quantity,\n unitPrice: input.unitPrice,\n discount:\n input.discount === 0\n ? { kind: 'none' }\n : { kind: 'amount', minor: input.discount },\n taxCode: rateTaxCode(percent),\n },\n ],\n }).lines[0];\n } catch (cause) {\n if (cause instanceof MoneyValidationError)\n throw new RangeError(cause.message, { cause });\n throw cause;\n }\n return {\n grossMinor: result.grossMinor,\n discountMinor: result.discountMinor,\n subtotalMinor: result.netMinor,\n taxMinor: result.taxMinor,\n totalMinor: result.netMinor + result.taxMinor,\n };\n}\n","/** Shared durable provider step owner; internal, never a caller-writable state API. */\nimport { randomUUID } from 'node:crypto';\nimport type {\n AccountingProvider,\n QuickBooksReceivableReadback,\n QuickBooksReceivableRequest,\n} from '@happyvertical/accounting';\nimport type { Invoice } from './models/Invoice.js';\nimport { MoneyValidationError } from './money.js';\n\ninterface ReconciliationEvidence {\n outcome: 'found' | 'absent';\n evidenceRef?: string;\n externalId?: string;\n recordedAt: string;\n interruptedOwnerStopped: boolean;\n}\nexport interface AccountingStep {\n key: string;\n status: 'in_flight' | 'unknown' | 'rejected' | 'confirmed';\n attemptId: string;\n request: QuickBooksReceivableRequest;\n startedAt: string;\n result?: QuickBooksReceivableReadback;\n reconciliation?: ReconciliationEvidence;\n priorAttempts?: {\n attemptId: string;\n status: string;\n startedAt: string;\n reconciliation?: ReconciliationEvidence;\n }[];\n}\n\nexport interface AccountingStepState {\n steps: AccountingStep[];\n}\nexport interface AccountingStepEngine<S extends AccountingStepState> {\n locked<T>(\n operation: (invoice: Invoice, state: S | null) => Promise<T>,\n ): Promise<T>;\n save(invoice: Invoice, state: S): Promise<void>;\n provider: NonNullable<AccountingProvider['receivableTransfers']>;\n}\nexport interface AccountingStepReconciliation {\n stepKey: string;\n requestId: string;\n attemptId: string;\n confirmInterruptedOwnerStopped?: true;\n outcome:\n | { status: 'found'; externalId: string }\n | { status: 'absent'; evidenceRef: string };\n}\nfunction reject(message: string): never {\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n message,\n );\n}\nfunction text(value: string, label: string) {\n if (typeof value !== 'string' || !value || value.trim() !== value)\n reject(`${label} is required`);\n}\nexport async function executeAccountingStep<S extends AccountingStepState>(\n engine: AccountingStepEngine<S>,\n key: string,\n make: (invoice: Invoice, state: S) => QuickBooksReceivableRequest,\n): Promise<QuickBooksReceivableReadback> {\n const prepared = await engine.locked(async (invoice, state) => {\n if (!state) reject('Retention provider contract is missing');\n const expected = make(invoice, state),\n prior = state.steps.find((s) => s.key === key);\n if (prior && prior.request.payloadHash !== expected.payloadHash)\n reject('Retention operation identity reused with changed facts');\n if (prior?.status === 'confirmed') {\n if (!prior.result) reject('Confirmed retention step has no readback');\n return { complete: prior.result } as const;\n }\n if (prior && prior.status !== 'rejected')\n reject('Retention operation has an unresolved remote owner');\n if (state.steps.some((s) => s.key !== key && s.status !== 'confirmed'))\n reject('Another retention operation requires reconciliation');\n const step: AccountingStep = {\n key,\n status: 'in_flight',\n attemptId: randomUUID(),\n request: expected,\n startedAt: new Date().toISOString(),\n priorAttempts: prior\n ? [\n ...(prior.priorAttempts ?? []),\n {\n attemptId: prior.attemptId,\n status: prior.status,\n startedAt: prior.startedAt,\n reconciliation: prior.reconciliation,\n },\n ]\n : [],\n };\n if (prior) state.steps[state.steps.indexOf(prior)] = step;\n else state.steps.push(step);\n await engine.save(invoice, state);\n return { step } as const;\n });\n if ('complete' in prepared && prepared.complete) return prepared.complete;\n if (!prepared.step) reject('Prepared retention step is missing');\n const step = prepared.step;\n let result: QuickBooksReceivableReadback;\n try {\n result = await engine.provider.execute(step.request);\n } catch (error) {\n await engine.locked(async (invoice, state) => {\n const current = state?.steps.find((s) => s.key === key);\n if (\n !state ||\n current?.attemptId !== step.attemptId ||\n current.status !== 'in_flight'\n )\n reject('Retention step ownership changed after remote failure');\n current.status =\n (error as { outcome?: string })?.outcome === 'rejected'\n ? 'rejected'\n : 'unknown';\n await engine.save(invoice, state);\n });\n throw error;\n }\n return engine.locked(async (invoice, state) => {\n const current = state?.steps.find((s) => s.key === key);\n if (\n !state ||\n current?.attemptId !== step.attemptId ||\n current.status !== 'in_flight' ||\n result.payloadHash !== step.request.payloadHash\n )\n reject('Retention step ownership or readback changed after write');\n current.status = 'confirmed';\n current.result = structuredClone(result);\n await engine.save(invoice, state);\n return result;\n });\n}\nexport async function reconcileAccountingStep<S extends AccountingStepState>(\n engine: AccountingStepEngine<S>,\n input: AccountingStepReconciliation,\n): Promise<S> {\n const step = await engine.locked(async (_invoice, state) => {\n const found = state?.steps.find((s) => s.key === input.stepKey);\n if (\n !found ||\n found.request.requestId !== input.requestId ||\n found.attemptId !== input.attemptId ||\n !['in_flight', 'unknown'].includes(found.status)\n )\n reject('Observed retention reconciliation owner differs');\n if (found.status === 'in_flight' && !input.confirmInterruptedOwnerStopped)\n reject('Interrupted retention owner must be proven stopped');\n return structuredClone(found);\n });\n const result =\n input.outcome.status === 'found'\n ? await engine.provider.read(step.request, input.outcome.externalId)\n : null;\n if (input.outcome.status === 'absent')\n text(input.outcome.evidenceRef, 'Authoritative provider absence evidence');\n return engine.locked(async (invoice, state) => {\n const current = state?.steps.find((s) => s.key === input.stepKey);\n if (\n !state ||\n current?.attemptId !== step.attemptId ||\n current.status !== step.status\n )\n reject('Retention reconciliation owner changed');\n current.reconciliation = {\n outcome: input.outcome.status,\n recordedAt: new Date().toISOString(),\n interruptedOwnerStopped: input.confirmInterruptedOwnerStopped === true,\n ...(input.outcome.status === 'found'\n ? { externalId: input.outcome.externalId }\n : { evidenceRef: input.outcome.evidenceRef }),\n };\n if (result) {\n if (result.payloadHash !== step.request.payloadHash)\n reject('Retention reconciliation readback differs');\n current.result = result;\n current.status = 'confirmed';\n } else current.status = 'rejected';\n await engine.save(invoice, state);\n return state;\n });\n}\n","/** Ordinary canonical receivables: exact provider credit application and already-settled refund recording. */\n\nimport { createHash } from 'node:crypto';\nimport {\n type AccountingProvider,\n type CreditMemoInput,\n prepareQuickBooksInvoiceRequest,\n prepareQuickBooksReceivableRequest,\n type QuickBooksOrdinaryReceivableMapping,\n type QuickBooksReceivableRequest,\n} from '@happyvertical/accounting';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { assertAccountingWriteAllowed } from './accounting-live-gate.js';\nimport {\n type AccountingStep,\n type AccountingStepReconciliation,\n executeAccountingStep,\n reconcileAccountingStep,\n} from './accounting-steps-internal.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { PaymentAllocationCollection } from './collections/PaymentAllocationCollection.js';\nimport { PaymentCollection } from './collections/PaymentCollection.js';\nimport { lockAndReloadInvoice } from './document-money.js';\nimport {\n DOCUMENT_MONEY_WRITE,\n isMoneyTransaction,\n withDocumentMoneyTransaction,\n} from './document-money-internal.js';\nimport {\n loadFrozenInvoiceAccountingSnapshot,\n type QuickBooksFrozenInvoiceMapping,\n type ReconcileFrozenInvoiceAccountingInput,\n readFrozenAccountingSyncState,\n reconcileFrozenInvoiceAccountingSync,\n syncFrozenInvoiceToAccounting,\n toFrozenInvoiceAccountingInput,\n} from './invoice-accounting-sync.js';\nimport type { Invoice } from './models/Invoice.js';\nimport { MoneyValidationError } from './money.js';\nimport { withOrdinaryAccountingAuthority } from './ordinary-accounting-internal.js';\nimport { lockTenantMoneyRuntime } from './tenant-money-barrier.js';\nimport { InvoiceStatus } from './types/index.js';\n\ntype Provider = Pick<\n AccountingProvider,\n 'type' | 'invoices' | 'creditMemos' | 'receivableTransfers'\n>;\ninterface Scope {\n db: DatabaseInterface;\n tenantId: string;\n invoiceId: string;\n provider: Provider;\n}\ninterface RefundRecording {\n paymentId: string;\n creditId: string;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n amountMinor: number;\n paymentFingerprint: string;\n committed: boolean;\n}\ninterface CreditAdjustment {\n creditId: string;\n moneySnapshotId: string;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n grossMinor: number;\n appliedMinor: number;\n mapping: QuickBooksFrozenInvoiceMapping;\n committed: boolean;\n}\n\nexport interface OrdinaryInvoiceAccountingState {\n version: 1;\n kind: 'ordinary';\n invoiceId: string;\n moneySnapshotId: string;\n externalInvoiceId: string | null;\n invoiceMapping: QuickBooksFrozenInvoiceMapping;\n requestId: string;\n evidenceRef: string;\n currency: string;\n grossMinor: number;\n mapping: QuickBooksOrdinaryReceivableMapping;\n status: 'pending' | 'ready';\n steps: AccountingStep[];\n credits: CreditAdjustment[];\n refunds: RefundRecording[];\n}\nconst hash = (value: unknown) =>\n createHash('sha256').update(JSON.stringify(value)).digest('hex');\nfunction reject(message: string): never {\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n message,\n );\n}\nfunction text(value: string, label: string) {\n if (typeof value !== 'string' || !value || value.trim() !== value)\n reject(`${label} is required`);\n}\nfunction date(value: string, label: string) {\n if (\n !/^\\d{4}-\\d{2}-\\d{2}$/.test(value) ||\n !Number.isFinite(Date.parse(value)) ||\n new Date(value).toISOString().slice(0, 10) !== value\n )\n reject(`${label} is invalid`);\n}\nfunction principal(input: Scope) {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n reject('Ordinary accounting requires its active tenant');\n if (isMoneyTransaction(input.db))\n reject(\n 'Provider writes cannot run inside a caller-owned money transaction',\n );\n if (\n input.provider.type !== 'quickbooks' ||\n !input.provider.receivableTransfers\n )\n reject('Provider lacks reviewed receivable operations');\n}\nfunction syncedIdentity(\n invoice: Invoice,\n mapping: QuickBooksOrdinaryReceivableMapping,\n) {\n const sync = readFrozenAccountingSyncState(invoice);\n if (\n !sync ||\n sync.status !== 'synced' ||\n sync.documentKind !== 'invoice' ||\n sync.provider !== 'quickbooks' ||\n sync.moneySnapshotId !== invoice.moneySnapshotId ||\n sync.realmId !== mapping.realmId ||\n sync.environment !== mapping.environment ||\n invoice.externalProvider !== 'quickbooks' ||\n !invoice.externalId\n )\n reject(\n 'Ordinary accounting requires its reconciled frozen invoice export identity',\n );\n}\nexport function readOrdinaryInvoiceAccountingState(\n invoice: Invoice,\n): OrdinaryInvoiceAccountingState | null {\n if (!invoice.ordinaryAccountingStateJson) return null;\n let state: OrdinaryInvoiceAccountingState;\n try {\n state = JSON.parse(invoice.ordinaryAccountingStateJson);\n } catch {\n return reject('Malformed ordinary accounting state');\n }\n if (\n !state ||\n !state.mapping ||\n state.version !== 1 ||\n state.kind !== 'ordinary' ||\n state.mapping.kind !== 'ordinary' ||\n state.invoiceId !== invoice.id ||\n state.moneySnapshotId !== invoice.moneySnapshotId ||\n (state.externalInvoiceId !== null &&\n state.externalInvoiceId !== invoice.externalId) ||\n state.currency !== invoice.currency ||\n state.grossMinor !== invoice.totalAmount ||\n state.mapping.customerRef !== invoice.customerExternalId ||\n !Array.isArray(state.steps) ||\n !Array.isArray(state.credits) ||\n !Array.isArray(state.refunds) ||\n invoice.retentionStateJson ||\n invoice.retentionAccountingStateJson ||\n invoice.moneyAuthority !== 'calculated'\n )\n reject('Ordinary provider source identity changed');\n if (state.externalInvoiceId !== null) syncedIdentity(invoice, state.mapping);\n return state;\n}\nasync function locked<T>(\n input: Scope,\n operation: (\n invoice: Invoice,\n state: OrdinaryInvoiceAccountingState | null,\n ) => Promise<T>,\n): Promise<T> {\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n return operation(invoice, readOrdinaryInvoiceAccountingState(invoice));\n });\n}\nasync function save(invoice: Invoice, state: OrdinaryInvoiceAccountingState) {\n invoice.ordinaryAccountingStateJson = JSON.stringify(state);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n}\nfunction refreshStatus(state: OrdinaryInvoiceAccountingState) {\n state.status =\n state.steps.every((s) => s.status === 'confirmed') &&\n state.credits.every((c) => c.committed) &&\n state.refunds.every((r) => r.committed)\n ? 'ready'\n : 'pending';\n}\nfunction requireReady(\n state: OrdinaryInvoiceAccountingState | null,\n): OrdinaryInvoiceAccountingState {\n if (\n !state ||\n state.status !== 'ready' ||\n state.steps.some((s) => s.status !== 'confirmed')\n )\n reject('Ordinary provider operations require reconciliation');\n return state;\n}\nfunction requestId(state: OrdinaryInvoiceAccountingState, key: string) {\n return `smrt_ord_${hash([state.invoiceId, state.moneySnapshotId, state.mapping.realmId, state.mapping.environment, key]).slice(0, 40)}`;\n}\nfunction executeStep(\n input: Scope,\n key: string,\n make: (\n invoice: Invoice,\n state: OrdinaryInvoiceAccountingState,\n ) => QuickBooksReceivableRequest,\n) {\n return executeAccountingStep(\n {\n locked: (operation) => locked(input, operation),\n save,\n provider: input.provider.receivableTransfers!,\n },\n key,\n make,\n );\n}\nexport interface SyncOrdinaryInvoiceToAccountingInput extends Scope {\n requestId: string;\n evidenceRef: string;\n mapping: QuickBooksOrdinaryReceivableMapping;\n invoiceMapping: QuickBooksFrozenInvoiceMapping;\n}\nfunction validateOrdinarySource(\n invoice: Invoice,\n input: SyncOrdinaryInvoiceToAccountingInput,\n) {\n if (\n invoice.kind !== 'invoice' ||\n !invoice.moneySnapshotId ||\n invoice.retentionStateJson ||\n invoice.retentionAccountingStateJson ||\n invoice.moneyAuthority !== 'calculated' ||\n [\n InvoiceStatus.DRAFT,\n InvoiceStatus.CANCELLED,\n InvoiceStatus.WRITTEN_OFF,\n ].includes(invoice.status) ||\n invoice.collectionMethod ||\n invoice.providerTaxAmount ||\n invoice.currency !== input.mapping.currency ||\n invoice.customerExternalId !== input.mapping.customerRef ||\n input.invoiceMapping.realmId !== input.mapping.realmId ||\n input.invoiceMapping.environment !== input.mapping.environment\n )\n reject(\n 'Ordinary accounting requires its issued, nonretained canonical frozen source',\n );\n}\n/** Retain exact reviewed mapping before the first possible remote invoice write. */\nexport async function syncOrdinaryInvoiceToAccounting(\n input: SyncOrdinaryInvoiceToAccountingInput,\n): Promise<OrdinaryInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n scope: input.invoiceMapping,\n });\n text(input.requestId, 'Ordinary request');\n text(input.evidenceRef, 'Ordinary evidence');\n if (input.mapping.kind !== 'ordinary') reject('Ordinary mapping is required');\n await locked(input, async (invoice) => {\n validateOrdinarySource(invoice, input);\n await toFrozenInvoiceAccountingInput({\n db: invoice.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n mapping: input.invoiceMapping,\n });\n });\n await input.provider.receivableTransfers!.verifyMapping(input.mapping);\n await locked(input, async (invoice, state) => {\n validateOrdinarySource(invoice, input);\n // A generic exporter may already own this document. Compare its exact\n // prepared request while holding the same lock before retaining a mapping.\n const prior = readFrozenAccountingSyncState(invoice);\n if (prior) {\n const sdkInput = await toFrozenInvoiceAccountingInput({\n db: invoice.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n mapping: input.invoiceMapping,\n });\n // Reconstruct the original creation payload, not an update to its result.\n const request = prepareQuickBooksInvoiceRequest(\n { ...sdkInput, externalId: undefined },\n {\n requestId: prior.requestId,\n realmId: input.invoiceMapping.realmId,\n environment: input.invoiceMapping.environment,\n },\n );\n if (\n prior.provider !== 'quickbooks' ||\n prior.documentKind !== 'invoice' ||\n prior.moneySnapshotId !== invoice.moneySnapshotId ||\n prior.realmId !== request.realmId ||\n prior.environment !== request.environment ||\n prior.request.payloadHash !== request.payloadHash\n )\n reject(\n 'Ordinary invoice mapping differs from its frozen export request',\n );\n }\n if (state) {\n if (\n hash([\n state.requestId,\n state.evidenceRef,\n state.mapping,\n state.invoiceMapping,\n ]) !==\n hash([\n input.requestId,\n input.evidenceRef,\n input.mapping,\n input.invoiceMapping,\n ])\n )\n reject('Ordinary accounting initialization changed');\n return;\n }\n const next: OrdinaryInvoiceAccountingState = {\n version: 1,\n kind: 'ordinary',\n invoiceId: input.invoiceId,\n moneySnapshotId: invoice.moneySnapshotId,\n externalInvoiceId: null,\n invoiceMapping: structuredClone(input.invoiceMapping),\n requestId: input.requestId,\n evidenceRef: input.evidenceRef,\n currency: invoice.currency,\n grossMinor: invoice.totalAmount,\n mapping: structuredClone(input.mapping),\n status: 'pending',\n steps: [],\n credits: [],\n refunds: [],\n };\n await save(invoice, next);\n });\n const result = await withOrdinaryAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () =>\n syncFrozenInvoiceToAccounting({\n ...input,\n mapping: input.invoiceMapping,\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Ordinary provider reservation disappeared');\n syncedIdentity(invoice, state.mapping);\n state.externalInvoiceId = result.externalId;\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\nexport async function reconcileOrdinaryBaseInvoice(\n input: ReconcileFrozenInvoiceAccountingInput & { provider: Provider },\n) {\n principal(input);\n await locked(input, async (_invoice, state) => {\n if (!state || hash(state.invoiceMapping) !== hash(input.mapping))\n reject('Ordinary base mapping differs');\n });\n return reconcileFrozenInvoiceAccountingSync(input);\n}\nexport interface ReconcileOrdinaryAccountingStepInput\n extends Scope,\n AccountingStepReconciliation {}\nexport async function reconcileOrdinaryAccountingStep(\n input: ReconcileOrdinaryAccountingStepInput,\n): Promise<OrdinaryInvoiceAccountingState> {\n principal(input);\n return reconcileAccountingStep(\n {\n locked: (operation) => locked(input, operation),\n save,\n provider: input.provider.receivableTransfers!,\n },\n input,\n );\n}\nexport interface SyncOrdinaryCreditToAccountingInput extends Scope {\n creditInvoiceId: string;\n creditMapping: QuickBooksFrozenInvoiceMapping;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n}\n/** Apply the reserved ordinary credit amount to its original due balance. */\nexport async function syncOrdinaryCreditToAccounting(\n input: SyncOrdinaryCreditToAccountingInput,\n): Promise<OrdinaryInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n scope: input.creditMapping,\n });\n text(input.requestId, 'Credit adjustment request');\n text(input.evidenceRef, 'Credit adjustment evidence');\n date(input.effectiveOn, 'Credit adjustment date');\n const reserved = await locked(input, async (invoice, state) => {\n if (!state) reject('Ordinary provider contract is missing');\n const credit = await (\n await InvoiceCollection.create({ db: invoice.db })\n ).get({ id: input.creditInvoiceId }, { cache: false });\n if (\n !credit ||\n credit.tenantId !== input.tenantId ||\n credit.customerId !== invoice.customerId ||\n credit.creditedInvoiceId !== invoice.id ||\n credit.kind !== 'credit_note' ||\n !credit.moneySnapshotId ||\n [InvoiceStatus.DRAFT, InvoiceStatus.CANCELLED].includes(credit.status) ||\n credit.currency !== state.currency ||\n credit.customerExternalId !== state.mapping.customerRef\n )\n reject(\n 'Credit adjustment requires its issued same-customer source credit',\n );\n if (\n input.creditMapping.realmId !== state.mapping.realmId ||\n input.creditMapping.environment !== state.mapping.environment\n )\n reject('Credit adjustment provider scope differs');\n const prior = state.credits.find(\n (c) =>\n c.creditId === input.creditInvoiceId || c.requestId === input.requestId,\n );\n if (prior) {\n if (\n prior.creditId !== input.creditInvoiceId ||\n prior.requestId !== input.requestId ||\n prior.moneySnapshotId !== credit.moneySnapshotId ||\n prior.grossMinor !== credit.totalAmount ||\n prior.evidenceRef !== input.evidenceRef ||\n prior.effectiveOn !== input.effectiveOn ||\n hash(prior.mapping) !== hash(input.creditMapping)\n )\n reject('Credit adjustment identity reused with changed facts');\n return structuredClone(prior);\n }\n requireReady(state);\n const previousGross = state.credits.reduce(\n (sum, c) => sum + c.grossMinor,\n 0,\n );\n if (previousGross + credit.totalAmount > invoice.creditedAmount)\n reject('Provider credit exceeds issued source credit capacity');\n const next: CreditAdjustment = {\n creditId: input.creditInvoiceId,\n moneySnapshotId: credit.moneySnapshotId,\n requestId: input.requestId,\n evidenceRef: input.evidenceRef,\n effectiveOn: input.effectiveOn,\n grossMinor: credit.totalAmount,\n appliedMinor: credit.creditedAmount,\n mapping: structuredClone(input.creditMapping),\n committed: false,\n };\n state.credits.push(next);\n state.status = 'pending';\n await save(invoice, state);\n return next;\n });\n const creditResult = await withOrdinaryAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () =>\n syncFrozenInvoiceToAccounting({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.creditInvoiceId,\n provider: input.provider,\n mapping: input.creditMapping,\n }),\n );\n const prefix = `credit:${input.requestId}`;\n const creditSource = (state: OrdinaryInvoiceAccountingState) => ({\n type: 'CreditMemo' as const,\n id: creditResult.externalId,\n customerRef: state.mapping.customerRef,\n evidenceRef: reserved.moneySnapshotId,\n });\n if (reserved.appliedMinor > 0)\n await executeStep(input, `${prefix}:due-application`, (invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'application',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, `${prefix}:due-application`),\n effectiveOn: reserved.effectiveOn,\n evidenceRef: reserved.evidenceRef,\n amountMinor: reserved.appliedMinor,\n debit: { type: 'Invoice', id: invoice.externalId },\n credit: { type: 'CreditMemo', id: creditResult.externalId },\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Ordinary provider state is missing');\n const credit = state.credits.find(\n (c) => c.creditId === input.creditInvoiceId,\n );\n if (!credit) reject('Credit reservation disappeared');\n credit.committed = true;\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\n\nexport interface SyncOrdinaryRefundToAccountingInput extends Scope {\n paymentId: string;\n creditInvoiceId: string;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n}\n/** Record an already settled local refund and consume its existing remote credit. No bank I/O. */\nexport async function syncOrdinaryRefundToAccounting(\n input: SyncOrdinaryRefundToAccountingInput,\n): Promise<OrdinaryInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n });\n text(input.requestId, 'Refund recording request');\n text(input.evidenceRef, 'Refund settlement evidence');\n date(input.effectiveOn, 'Refund effective date');\n const reservation = await locked(input, async (invoice, state) => {\n if (!state) reject('Ordinary provider contract is missing');\n const credit = state.credits.find(\n (c) => c.creditId === input.creditInvoiceId && c.committed,\n );\n if (!credit) reject('Refund requires its reconciled provider credit');\n const payments = await PaymentCollection.create({ db: invoice.db }),\n payment = await payments.get({ id: input.paymentId }, { cache: false });\n const creditInvoice = await (\n await InvoiceCollection.create({ db: invoice.db })\n ).get({ id: input.creditInvoiceId }, { cache: false });\n if (\n !payment ||\n !creditInvoice ||\n payment.tenantId !== input.tenantId ||\n creditInvoice.tenantId !== input.tenantId ||\n creditInvoice.customerId !== invoice.customerId ||\n creditInvoice.currency !== state.currency ||\n creditInvoice.creditedInvoiceId !== invoice.id ||\n payment.customerId !== invoice.customerId ||\n payment.currency !== state.currency ||\n payment.kind !== 'refund' ||\n payment.status !== 'completed' ||\n !payment.journalId ||\n !payment.paidAt ||\n new Date(payment.paidAt).toISOString().slice(0, 10) !==\n input.effectiveOn ||\n !creditInvoice.externalId ||\n creditInvoice.moneySnapshotId !== credit.moneySnapshotId\n )\n reject(\n 'Provider refund requires its completed same-customer local cash journal and source credit',\n );\n const allocations = await (\n await PaymentAllocationCollection.create({ db: invoice.db })\n ).findByPayment(input.paymentId);\n if (\n allocations.length !== 1 ||\n allocations[0].invoiceId !== input.creditInvoiceId ||\n allocations[0].amount !== payment.amount\n )\n reject(\n 'Refund recording requires one exact full source-credit allocation',\n );\n const fingerprint = hash([\n payment.id,\n payment.customerId,\n payment.currency,\n payment.amount,\n payment.journalId,\n new Date(payment.paidAt).toISOString(),\n allocations[0].id,\n allocations[0].amount,\n credit.moneySnapshotId,\n ]);\n const next: RefundRecording = {\n paymentId: input.paymentId,\n creditId: input.creditInvoiceId,\n requestId: input.requestId,\n evidenceRef: input.evidenceRef,\n effectiveOn: input.effectiveOn,\n amountMinor: payment.amount,\n paymentFingerprint: fingerprint,\n committed: false,\n };\n const prior = state.refunds.find(\n (r) => r.paymentId === input.paymentId || r.requestId === input.requestId,\n );\n if (prior) {\n if (hash({ ...prior, committed: false }) !== hash(next))\n reject('Refund recording identity reused with changed facts');\n return { ...prior, externalCreditId: creditInvoice.externalId };\n }\n requireReady(state);\n if (!state.mapping.cashAccountRef)\n reject('Refund provider bank account must be explicitly reviewed');\n const already = state.refunds\n .filter((r) => r.creditId === input.creditInvoiceId)\n .reduce((sum, r) => sum + r.amountMinor, 0);\n if (already + payment.amount > credit.grossMinor - credit.appliedMinor)\n reject('Refund exceeds unapplied provider credit capacity');\n if (\n payment.ordinaryAccountingBindingJson ||\n payment.retentionAccountingBindingJson\n )\n reject('Refund is already bound to a provider operation');\n payment.ordinaryAccountingBindingJson = JSON.stringify({\n invoiceId: input.invoiceId,\n tenantId: input.tenantId,\n requestId: input.requestId,\n paymentFingerprint: fingerprint,\n });\n await withOrdinaryAccountingAuthority(input.tenantId, input.invoiceId, () =>\n payment.save(),\n );\n state.refunds.push(next);\n state.status = 'pending';\n await save(invoice, state);\n return { ...next, externalCreditId: creditInvoice.externalId };\n });\n const prefix = `refund:${input.requestId}`;\n const creditSource = (state: OrdinaryInvoiceAccountingState) => ({\n type: 'CreditMemo' as const,\n id: reservation.externalCreditId,\n customerRef: state.mapping.customerRef,\n evidenceRef: reservation.paymentFingerprint,\n });\n const journal = await executeStep(input, prefix, (_invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'cash-refund',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, prefix),\n effectiveOn: reservation.effectiveOn,\n evidenceRef: reservation.evidenceRef,\n amountMinor: reservation.amountMinor,\n }),\n );\n await executeStep(input, `${prefix}:application`, (_invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'application',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, `${prefix}:application`),\n effectiveOn: reservation.effectiveOn,\n evidenceRef: reservation.evidenceRef,\n amountMinor: reservation.amountMinor,\n debit: { type: 'JournalEntry', id: journal.externalId },\n credit: { type: 'CreditMemo', id: reservation.externalCreditId },\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Ordinary provider state disappeared');\n const refund = state.refunds.find((r) => r.requestId === input.requestId);\n if (!refund) reject('Refund reservation disappeared');\n refund.committed = true;\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\n\nexport interface ReconcileOrdinaryCreditAccountingInput extends Scope {\n creditInvoiceId: string;\n requestId: string;\n attemptId: string;\n confirmInterruptedOwnerStopped?: true;\n outcome: ReconcileFrozenInvoiceAccountingInput['outcome'];\n}\n/** Resolve an interrupted credit-memo export before its ordinary applications resume. */\nexport async function reconcileOrdinaryCreditAccounting(\n input: ReconcileOrdinaryCreditAccountingInput,\n) {\n principal(input);\n const mapping = await locked(input, async (_invoice, state) => {\n const reserved = state?.credits.find(\n (credit) => credit.creditId === input.creditInvoiceId,\n );\n if (!reserved)\n reject('Ordinary credit has no approved provider reservation');\n return structuredClone(reserved.mapping);\n });\n return withOrdinaryAccountingAuthority(input.tenantId, input.invoiceId, () =>\n reconcileFrozenInvoiceAccountingSync({\n ...input,\n invoiceId: input.creditInvoiceId,\n mapping,\n }),\n );\n}\n\nexport interface OrdinaryCreditReviewScope {\n db: DatabaseInterface;\n tenantId: string;\n invoiceId: string;\n creditInvoiceId: string;\n}\nasync function reviewOrdinaryCredit<T>(\n input: OrdinaryCreditReviewScope,\n read: (scope: {\n db: DatabaseInterface;\n tenantId: string;\n invoiceId: string;\n }) => Promise<T>,\n): Promise<T> {\n if (getTenantId() !== input.tenantId)\n reject('Ordinary review requires its active tenant');\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n if (!readOrdinaryInvoiceAccountingState(invoice))\n reject('Ordinary provider contract is missing');\n const credit = await (await InvoiceCollection.create({ db: tx })).get(\n { id: input.creditInvoiceId },\n { cache: false },\n );\n if (\n !credit ||\n credit.tenantId !== input.tenantId ||\n credit.creditedInvoiceId !== invoice.id ||\n credit.kind !== 'credit_note' ||\n credit.customerId !== invoice.customerId ||\n credit.currency !== invoice.currency ||\n credit.moneyAuthority !== 'calculated'\n )\n reject('Ordinary credit review requires exact source lineage');\n return withOrdinaryAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () =>\n read({\n db: tx,\n tenantId: input.tenantId,\n invoiceId: input.creditInvoiceId,\n }),\n );\n });\n}\nexport function loadOrdinaryCreditAccountingSnapshot(\n input: OrdinaryCreditReviewScope,\n) {\n return reviewOrdinaryCredit(input, loadFrozenInvoiceAccountingSnapshot);\n}\nexport function toOrdinaryCreditAccountingInput(\n input: OrdinaryCreditReviewScope & {\n mapping: QuickBooksFrozenInvoiceMapping;\n },\n): Promise<CreditMemoInput> {\n return reviewOrdinaryCredit(\n input,\n async (scope) =>\n (await toFrozenInvoiceAccountingInput({\n ...scope,\n mapping: input.mapping,\n })) as unknown as CreditMemoInput,\n );\n}\n","/** Durable retained-invoice provider orchestration. Every remote write is a separate recoverable step. */\n\nimport { createHash } from 'node:crypto';\nimport {\n type AccountingProvider,\n type CreditMemoInput,\n prepareQuickBooksReceivableRequest,\n type QuickBooksReceivableMapping,\n type QuickBooksReceivableReadback,\n type QuickBooksReceivableRequest,\n} from '@happyvertical/accounting';\nimport type { DatabaseInterface } from '@happyvertical/smrt-core/migrations';\nimport { getTenantId } from '@happyvertical/smrt-tenancy';\nimport { assertAccountingWriteAllowed } from './accounting-live-gate.js';\nimport { mapFrozenAccountingSnapshot } from './accounting-snapshot.js';\nimport {\n type AccountingStep,\n executeAccountingStep,\n reconcileAccountingStep,\n} from './accounting-steps-internal.js';\nimport { InvoiceCollection } from './collections/InvoiceCollection.js';\nimport { PaymentAllocationCollection } from './collections/PaymentAllocationCollection.js';\nimport { PaymentCollection } from './collections/PaymentCollection.js';\nimport { lockAndReloadInvoice } from './document-money.js';\nimport {\n DOCUMENT_MONEY_WRITE,\n isMoneyTransaction,\n withDocumentMoneyTransaction,\n} from './document-money-internal.js';\nimport {\n type FrozenInvoiceAccountingInput,\n loadFrozenInvoiceAccountingSnapshot,\n type QuickBooksFrozenInvoiceMapping,\n type ReconcileFrozenInvoiceAccountingInput,\n reconcileFrozenInvoiceAccountingSync,\n syncFrozenInvoiceToAccounting,\n toFrozenInvoiceAccountingInput,\n} from './invoice-accounting-sync.js';\nimport type { Invoice } from './models/Invoice.js';\nimport { MoneyValidationError } from './money.js';\nimport { withRetentionAccountingAuthority } from './retention-accounting-internal.js';\nimport {\n calculateInvoicePayable,\n type InvoiceRetentionEvidence,\n parseInvoiceRetention,\n validateRetentionEvidence,\n} from './retention-money.js';\nimport { lockTenantMoneyRuntime } from './tenant-money-barrier.js';\nimport { type AccountingInvoiceInput, InvoiceStatus } from './types/index.js';\n\ntype Provider = Pick<\n AccountingProvider,\n 'type' | 'invoices' | 'creditMemos' | 'receivableTransfers'\n>;\ninterface Scope {\n db: DatabaseInterface;\n tenantId: string;\n invoiceId: string;\n provider: Provider;\n}\ninterface RefundRecording {\n paymentId: string;\n creditId: string;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n amountMinor: number;\n paymentFingerprint: string;\n committed: boolean;\n}\ninterface CreditAdjustment {\n creditId: string;\n moneySnapshotId: string;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n grossMinor: number;\n retainedMinor: number;\n appliedMinor: number;\n mapping: QuickBooksFrozenInvoiceMapping;\n committed: boolean;\n}\ninterface Release extends InvoiceRetentionEvidence {\n amountMinor: number;\n effectiveOn: string;\n dueOn: string;\n stepKey: string;\n committed: boolean;\n}\nexport interface RetainedInvoiceAccountingState {\n version: 1;\n invoiceId: string;\n moneySnapshotId: string;\n requestId: string;\n evidenceRef: string;\n currency: string;\n grossMinor: number;\n retainedMinor: number;\n retentionAgreementHash: string;\n effectiveOn: string;\n mapping: QuickBooksReceivableMapping;\n invoiceMapping: QuickBooksFrozenInvoiceMapping;\n status: 'pending' | 'ready';\n steps: AccountingStep[];\n releases: Release[];\n credits: CreditAdjustment[];\n refunds: RefundRecording[];\n}\nexport interface SyncRetainedInvoiceToAccountingInput extends Scope {\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n mapping: QuickBooksReceivableMapping;\n invoiceMapping: QuickBooksFrozenInvoiceMapping;\n}\nexport interface ReleaseRetainedInvoiceToAccountingInput\n extends Scope,\n InvoiceRetentionEvidence {\n amountMinor: number;\n effectiveOn: string;\n dueOn: string;\n expectedUpdatedAt: string;\n}\nconst hash = (value: unknown) =>\n createHash('sha256').update(JSON.stringify(value)).digest('hex');\nfunction reject(message: string): never {\n throw new MoneyValidationError(\n 'ACCOUNTING_SYNC_RECONCILIATION_REQUIRED',\n message,\n );\n}\nfunction text(value: string, label: string) {\n if (typeof value !== 'string' || !value || value.trim() !== value)\n reject(`${label} is required`);\n}\nfunction date(value: string, label: string) {\n if (\n !/^\\d{4}-\\d{2}-\\d{2}$/.test(value) ||\n !Number.isFinite(Date.parse(value)) ||\n new Date(value).toISOString().slice(0, 10) !== value\n )\n reject(`${label} is invalid`);\n}\nfunction principal(input: Scope) {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n reject('Retention accounting requires its active tenant');\n if (isMoneyTransaction(input.db))\n reject(\n 'Provider writes cannot run inside a caller-owned money transaction',\n );\n if (\n input.provider.type !== 'quickbooks' ||\n !input.provider.receivableTransfers\n )\n reject('Provider lacks the reviewed receivable transfer capability');\n}\nexport function readRetainedInvoiceAccountingState(\n invoice: Invoice,\n): RetainedInvoiceAccountingState | null {\n if (!invoice.retentionAccountingStateJson) return null;\n let state: RetainedInvoiceAccountingState;\n try {\n state = JSON.parse(invoice.retentionAccountingStateJson);\n } catch {\n return reject('Malformed retained accounting state');\n }\n if (\n state.version !== 1 ||\n state.invoiceId !== invoice.id ||\n state.moneySnapshotId !== invoice.moneySnapshotId ||\n state.currency !== invoice.currency ||\n state.grossMinor !== invoice.totalAmount ||\n state.mapping.customerRef !== invoice.customerExternalId ||\n !Array.isArray(state.steps) ||\n !Array.isArray(state.releases) ||\n !Array.isArray(state.credits) ||\n !Array.isArray(state.refunds)\n )\n reject('Retention provider source identity changed');\n const retention = parseInvoiceRetention(invoice.retentionStateJson);\n if (!retention) reject('Provider retention agreement is missing');\n const { releases: _releases, ...agreement } = retention;\n if (state.retentionAgreementHash !== hash(agreement))\n reject('Provider retention agreement changed');\n return state;\n}\n/** Read-only review of the exact retained document; generic export stays blocked.\n * The host supplies authorization, and every read stays on the caller's executor.\n */\nexport async function loadRetainedInvoiceAccountingSnapshot(\n input: Omit<FrozenInvoiceAccountingInput, 'mapping'>,\n) {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n reject('Retained accounting review requires its active tenant');\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n const retention = parseInvoiceRetention(invoice.retentionStateJson);\n if (\n !retention ||\n retention.retainedMinor <= 0 ||\n invoice.kind !== 'invoice' ||\n invoice.moneyAuthority === 'historical'\n )\n reject(\n 'Retained accounting review requires a calculated retained invoice',\n );\n readRetainedInvoiceAccountingState(invoice);\n return withRetentionAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () => loadFrozenInvoiceAccountingSnapshot({ ...input, db: tx }),\n );\n });\n}\n/** Validate reviewed realm item/tax mapping without creating a provider operation. */\nexport async function toRetainedInvoiceAccountingInput(\n input: FrozenInvoiceAccountingInput,\n) {\n const snapshot = await loadRetainedInvoiceAccountingSnapshot(input);\n return mapFrozenAccountingSnapshot(\n snapshot.document,\n input.mapping,\n ) as AccountingInvoiceInput;\n}\ntype RetainedCreditReviewScope = Omit<\n FrozenInvoiceAccountingInput,\n 'mapping'\n> & {\n creditInvoiceId: string;\n};\nasync function reviewRetainedCredit<T>(\n input: RetainedCreditReviewScope,\n read: (scope: Omit<FrozenInvoiceAccountingInput, 'mapping'>) => Promise<T>,\n): Promise<T> {\n if (!input.tenantId || getTenantId() !== input.tenantId)\n reject('Retained credit review requires its active tenant');\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n // The existing owning reader proves the original calculated retained invoice.\n const original = await loadRetainedInvoiceAccountingSnapshot({\n ...input,\n db: tx,\n });\n const { invoice: credit } = await lockAndReloadInvoice(\n tx,\n input.creditInvoiceId,\n input.tenantId,\n );\n if (\n credit.kind !== 'credit_note' ||\n credit.creditedInvoiceId !== input.invoiceId ||\n credit.customerId !== original.document.customerId ||\n credit.currency !== original.document.currency ||\n credit.moneyAuthority === 'historical'\n )\n reject(\n 'Retained credit review requires its exact calculated source lineage',\n );\n return withRetentionAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () =>\n read({\n db: tx,\n tenantId: input.tenantId,\n invoiceId: input.creditInvoiceId,\n }),\n );\n });\n}\n/** Read an issued retained-first credit without permitting generic credit export. */\nexport function loadRetainedCreditAccountingSnapshot(\n input: RetainedCreditReviewScope,\n) {\n return reviewRetainedCredit(input, loadFrozenInvoiceAccountingSnapshot);\n}\n/** Map a retained-first credit using the existing frozen reader's realm/lineage checks. */\nexport function toRetainedCreditAccountingInput(\n input: RetainedCreditReviewScope & {\n mapping: QuickBooksFrozenInvoiceMapping;\n },\n): Promise<CreditMemoInput> {\n return reviewRetainedCredit(input, async (scope) => {\n // The owning review above proves credit_note; the legacy generic converter's\n // return declaration names InvoiceInput for both runtime document variants.\n return (await toFrozenInvoiceAccountingInput({\n ...scope,\n mapping: input.mapping,\n })) as unknown as CreditMemoInput;\n });\n}\n\nasync function locked<T>(\n input: Scope,\n operation: (\n invoice: Invoice,\n state: RetainedInvoiceAccountingState | null,\n ) => Promise<T>,\n): Promise<T> {\n return withDocumentMoneyTransaction(input.db, async (tx) => {\n await lockTenantMoneyRuntime(tx, input.tenantId);\n const { invoice } = await lockAndReloadInvoice(\n tx,\n input.invoiceId,\n input.tenantId,\n );\n return operation(invoice, readRetainedInvoiceAccountingState(invoice));\n });\n}\nasync function save(invoice: Invoice, state: RetainedInvoiceAccountingState) {\n invoice.retentionAccountingStateJson = JSON.stringify(state);\n await invoice.saveDerived(DOCUMENT_MONEY_WRITE);\n}\nfunction refreshStatus(state: RetainedInvoiceAccountingState) {\n state.status =\n state.steps.every((step) => step.status === 'confirmed') &&\n state.releases.every((event) => event.committed) &&\n state.credits.every((event) => event.committed) &&\n state.refunds.every((event) => event.committed)\n ? 'ready'\n : 'pending';\n}\nfunction requireReady(\n state: RetainedInvoiceAccountingState | null,\n): RetainedInvoiceAccountingState {\n if (\n !state ||\n state.status !== 'ready' ||\n state.steps.some((s) => s.status !== 'confirmed')\n )\n reject(\n 'Retention provider bundle requires reconciliation before a new operation',\n );\n return state;\n}\nfunction requestId(state: RetainedInvoiceAccountingState, key: string) {\n return `smrt_ret_${hash([state.invoiceId, state.moneySnapshotId, state.mapping.realmId, state.mapping.environment, key]).slice(0, 40)}`;\n}\nfunction source(invoice: Invoice, state: RetainedInvoiceAccountingState) {\n if (!invoice.externalId || invoice.externalProvider !== 'quickbooks')\n reject('Retention needs a reconciled provider invoice');\n return {\n type: 'Invoice' as const,\n id: invoice.externalId,\n customerRef: state.mapping.customerRef,\n evidenceRef: state.moneySnapshotId,\n };\n}\nasync function executeStep(\n input: Scope,\n key: string,\n make: (\n invoice: Invoice,\n state: RetainedInvoiceAccountingState,\n ) => QuickBooksReceivableRequest,\n) {\n return executeAccountingStep(\n {\n locked: (operation) => locked(input, operation),\n save,\n provider: input.provider.receivableTransfers!,\n },\n key,\n make,\n );\n}\n/** Export unchanged gross/tax, then reclassify and apply its exact retained amount. */\nexport async function syncRetainedInvoiceToAccounting(\n input: SyncRetainedInvoiceToAccountingInput,\n): Promise<RetainedInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n scope: input.invoiceMapping,\n });\n text(input.requestId, 'Retention request');\n text(input.evidenceRef, 'Retention evidence');\n date(input.effectiveOn, 'Retention effective date');\n await input.provider.receivableTransfers!.verifyMapping(input.mapping);\n await locked(input, async (invoice, state) => {\n if (\n input.mapping.realmId !== input.invoiceMapping.realmId ||\n input.mapping.environment !== input.invoiceMapping.environment ||\n input.mapping.currency !== invoice.currency ||\n input.mapping.customerRef !== invoice.customerExternalId\n )\n reject(\n 'Retention mapping differs from frozen invoice provider authority',\n );\n if (state) {\n if (\n hash([\n state.requestId,\n state.evidenceRef,\n state.effectiveOn,\n state.mapping,\n state.invoiceMapping,\n ]) !==\n hash([\n input.requestId,\n input.evidenceRef,\n input.effectiveOn,\n input.mapping,\n input.invoiceMapping,\n ])\n )\n reject('Retention provider contract is immutable');\n return;\n }\n if ('moneyAuthority' in invoice && invoice.moneyAuthority === 'historical')\n reject(\n 'Historical source-owned invoices require an explicit reconciled provider authority contract; original journals cannot be duplicated',\n );\n const retention = parseInvoiceRetention(invoice.retentionStateJson);\n if (\n !retention ||\n retention.retainedMinor <= 0 ||\n retention.releases.length ||\n invoice.creditedAmount ||\n invoice.amountPaid ||\n !invoice.moneySnapshotId ||\n invoice.kind !== 'invoice' ||\n [\n InvoiceStatus.DRAFT,\n InvoiceStatus.CANCELLED,\n InvoiceStatus.WRITTEN_OFF,\n ].includes(invoice.status) ||\n invoice.externalId ||\n invoice.externalProvider ||\n invoice.accountingSyncStateJson ||\n invoice.collectionMethod ||\n invoice.providerTaxAmount\n )\n reject(\n 'Initial provider retention requires an issued, uncredited, unpaid, unreleased host-owned retained invoice',\n );\n const { releases: _releases, ...agreement } = retention;\n state = {\n version: 1,\n invoiceId: input.invoiceId,\n moneySnapshotId: invoice.moneySnapshotId,\n requestId: input.requestId,\n evidenceRef: input.evidenceRef,\n effectiveOn: input.effectiveOn,\n currency: invoice.currency,\n grossMinor: invoice.totalAmount,\n retainedMinor: retention.retainedMinor,\n retentionAgreementHash: hash(agreement),\n mapping: structuredClone(input.mapping),\n invoiceMapping: structuredClone(input.invoiceMapping),\n status: 'pending',\n steps: [],\n releases: [],\n credits: [],\n refunds: [],\n };\n await save(invoice, state);\n });\n await withRetentionAccountingAuthority(input.tenantId, input.invoiceId, () =>\n syncFrozenInvoiceToAccounting({ ...input, mapping: input.invoiceMapping }),\n );\n const withheld = await executeStep(input, 'withhold', (invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'withhold',\n mapping: state.mapping,\n source: source(invoice, state),\n requestId: requestId(state, 'withhold'),\n effectiveOn: state.effectiveOn,\n evidenceRef: state.evidenceRef,\n amountMinor: state.retainedMinor,\n }),\n );\n await executeStep(input, 'withhold-application', (invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'application',\n mapping: state.mapping,\n source: source(invoice, state),\n requestId: requestId(state, 'withhold-application'),\n effectiveOn: state.effectiveOn,\n evidenceRef: state.evidenceRef,\n amountMinor: state.retainedMinor,\n debit: { type: 'Invoice', id: invoice.externalId },\n credit: { type: 'JournalEntry', id: withheld.externalId },\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Missing retained provider state');\n if (\n state.steps.some((step) => step.status !== 'confirmed') ||\n state.releases.some((release) => !release.committed)\n )\n reject('Another retention operation is unfinished');\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\n/** Release date is provider TxnDate; dueOn remains explicit host collection policy. */\nexport async function releaseRetainedInvoiceToAccounting(\n input: ReleaseRetainedInvoiceToAccountingInput,\n): Promise<RetainedInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n });\n validateRetentionEvidence(input);\n date(input.effectiveOn, 'Release effective date');\n date(input.dueOn, 'Release due date');\n if (!Number.isSafeInteger(input.amountMinor) || input.amountMinor <= 0)\n reject('Release amount or due date is invalid');\n const key = `release:${input.requestId}`;\n await locked(input, async (invoice, state) => {\n if (!state) reject('Retention provider contract is missing');\n const prior = state.releases.find((r) => r.requestId === input.requestId);\n const release: Release = {\n requestId: input.requestId,\n policyVersion: input.policyVersion,\n evidenceRef: input.evidenceRef,\n actorId: input.actorId,\n reason: input.reason,\n amountMinor: input.amountMinor,\n effectiveOn: input.effectiveOn,\n dueOn: input.dueOn,\n stepKey: key,\n committed: false,\n };\n if (prior) {\n if (hash({ ...prior, committed: false }) !== hash(release))\n reject('Release request identity reused with changed facts');\n return;\n }\n requireReady(state);\n if (\n !invoice.updated_at ||\n new Date(invoice.updated_at).toISOString() !==\n new Date(input.expectedUpdatedAt).toISOString()\n )\n reject('Stale retention release revision');\n if (\n invoice.creditedAmount !==\n state.credits\n .filter((c) => c.committed)\n .reduce((sum, c) => sum + c.grossMinor, 0)\n )\n reject(\n 'Provider retention credit adjustment must reconcile before release',\n );\n if (\n input.amountMinor >\n calculateInvoicePayable(invoice).retainedRemainingMinor\n )\n reject('Release exceeds retained capacity');\n state.releases.push(release);\n state.status = 'pending';\n await save(invoice, state);\n });\n await executeStep(input, key, (invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'release',\n mapping: state.mapping,\n source: source(invoice, state),\n requestId: requestId(state, key),\n effectiveOn: input.effectiveOn,\n evidenceRef: input.evidenceRef,\n amountMinor: input.amountMinor,\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Retention provider contract is missing');\n const release = state.releases.find((r) => r.requestId === input.requestId);\n if (!release) reject('Release reservation disappeared');\n if (!release.committed) {\n if (\n invoice.creditedAmount !==\n state.credits\n .filter((c) => c.committed)\n .reduce((sum, c) => sum + c.grossMinor, 0) ||\n input.amountMinor >\n calculateInvoicePayable(invoice).retainedRemainingMinor\n )\n reject(\n 'Source capacity changed after provider release; reconciliation required',\n );\n const retention = parseInvoiceRetention(invoice.retentionStateJson)!;\n retention.releases.push({\n requestId: release.requestId,\n policyVersion: release.policyVersion,\n evidenceRef: release.evidenceRef,\n actorId: release.actorId,\n reason: release.reason,\n amountMinor: release.amountMinor,\n effectiveOn: release.effectiveOn,\n recordedAt: new Date().toISOString(),\n });\n invoice.retentionStateJson = JSON.stringify(retention);\n release.committed = true;\n }\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\nexport interface ReconcileRetainedAccountingStepInput extends Scope {\n stepKey: string;\n requestId: string;\n attemptId: string;\n confirmInterruptedOwnerStopped?: true;\n outcome:\n | { status: 'found'; externalId: string }\n | { status: 'absent'; evidenceRef: string };\n}\n/** Unknown remote outcomes never reset on a guessed absence or an unobserved owner. */\nexport async function reconcileRetainedAccountingStep(\n input: ReconcileRetainedAccountingStepInput,\n): Promise<RetainedInvoiceAccountingState> {\n principal(input);\n return reconcileAccountingStep(\n {\n locked: (operation) => locked(input, operation),\n save,\n provider: input.provider.receivableTransfers!,\n },\n input,\n );\n}\n/** Reconcile the existing frozen invoice step using its established owner proof. */\nexport async function reconcileRetainedBaseInvoice(\n input: ReconcileFrozenInvoiceAccountingInput & { provider: Provider },\n) {\n principal(input);\n await locked(input, async (_invoice, state) => {\n if (!state || hash(state.invoiceMapping) !== hash(input.mapping))\n reject('Retention base mapping differs');\n });\n return withRetentionAccountingAuthority(input.tenantId, input.invoiceId, () =>\n reconcileFrozenInvoiceAccountingSync(input),\n );\n}\n\nexport interface SyncRetainedCreditToAccountingInput extends Scope {\n creditInvoiceId: string;\n creditMapping: QuickBooksFrozenInvoiceMapping;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n}\n/** Apply retained-first credit to the retained asset, then to the original due balance. */\nexport async function syncRetainedCreditToAccounting(\n input: SyncRetainedCreditToAccountingInput,\n): Promise<RetainedInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n scope: input.creditMapping,\n });\n text(input.requestId, 'Credit adjustment request');\n text(input.evidenceRef, 'Credit adjustment evidence');\n date(input.effectiveOn, 'Credit adjustment date');\n const reserved = await locked(input, async (invoice, state) => {\n if (!state) reject('Retention provider contract is missing');\n const credit = await (\n await InvoiceCollection.create({ db: invoice.db })\n ).get({ id: input.creditInvoiceId }, { cache: false });\n if (\n !credit ||\n credit.tenantId !== input.tenantId ||\n credit.creditedInvoiceId !== invoice.id ||\n credit.kind !== 'credit_note' ||\n !credit.moneySnapshotId ||\n [InvoiceStatus.DRAFT, InvoiceStatus.CANCELLED].includes(credit.status) ||\n credit.currency !== state.currency ||\n credit.customerExternalId !== state.mapping.customerRef\n )\n reject(\n 'Credit adjustment requires its issued same-customer source credit',\n );\n if (\n input.creditMapping.realmId !== state.mapping.realmId ||\n input.creditMapping.environment !== state.mapping.environment\n )\n reject('Credit adjustment provider scope differs');\n const prior = state.credits.find(\n (c) =>\n c.creditId === input.creditInvoiceId || c.requestId === input.requestId,\n );\n if (prior) {\n if (\n prior.creditId !== input.creditInvoiceId ||\n prior.requestId !== input.requestId ||\n prior.moneySnapshotId !== credit.moneySnapshotId ||\n prior.grossMinor !== credit.totalAmount ||\n prior.evidenceRef !== input.evidenceRef ||\n prior.effectiveOn !== input.effectiveOn ||\n hash(prior.mapping) !== hash(input.creditMapping)\n )\n reject('Credit adjustment identity reused with changed facts');\n return structuredClone(prior);\n }\n requireReady(state);\n const previousGross = state.credits.reduce(\n (sum, c) => sum + c.grossMinor,\n 0,\n );\n if (previousGross + credit.totalAmount > invoice.creditedAmount)\n reject('Provider credit exceeds issued source credit capacity');\n const remainingRetained =\n state.retainedMinor -\n state.releases\n .filter((r) => r.committed)\n .reduce((sum, r) => sum + r.amountMinor, 0) -\n state.credits.reduce((sum, c) => sum + c.retainedMinor, 0);\n const retainedMinor = Math.min(remainingRetained, credit.totalAmount);\n if (retainedMinor < 0 || retainedMinor > credit.creditedAmount)\n reject('Retained-first credit exceeds its approved source allocation');\n const next: CreditAdjustment = {\n creditId: input.creditInvoiceId,\n moneySnapshotId: credit.moneySnapshotId,\n requestId: input.requestId,\n evidenceRef: input.evidenceRef,\n effectiveOn: input.effectiveOn,\n grossMinor: credit.totalAmount,\n retainedMinor,\n appliedMinor: credit.creditedAmount - retainedMinor,\n mapping: structuredClone(input.creditMapping),\n committed: false,\n };\n state.credits.push(next);\n state.status = 'pending';\n await save(invoice, state);\n return next;\n });\n const creditResult = await withRetentionAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () =>\n syncFrozenInvoiceToAccounting({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.creditInvoiceId,\n provider: input.provider,\n mapping: input.creditMapping,\n }),\n );\n const prefix = `credit:${input.requestId}`;\n const creditSource = (state: RetainedInvoiceAccountingState) => ({\n type: 'CreditMemo' as const,\n id: creditResult.externalId,\n customerRef: state.mapping.customerRef,\n evidenceRef: reserved.moneySnapshotId,\n });\n if (reserved.retainedMinor > 0) {\n const cancelled = await executeStep(\n input,\n `${prefix}:retention`,\n (_invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'release',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, `${prefix}:retention`),\n effectiveOn: reserved.effectiveOn,\n evidenceRef: reserved.evidenceRef,\n amountMinor: reserved.retainedMinor,\n }),\n );\n await executeStep(\n input,\n `${prefix}:retention-application`,\n (_invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'application',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, `${prefix}:retention-application`),\n effectiveOn: reserved.effectiveOn,\n evidenceRef: reserved.evidenceRef,\n amountMinor: reserved.retainedMinor,\n debit: { type: 'JournalEntry', id: cancelled.externalId },\n credit: { type: 'CreditMemo', id: creditResult.externalId },\n }),\n );\n }\n if (reserved.appliedMinor > 0)\n await executeStep(input, `${prefix}:due-application`, (invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'application',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, `${prefix}:due-application`),\n effectiveOn: reserved.effectiveOn,\n evidenceRef: reserved.evidenceRef,\n amountMinor: reserved.appliedMinor,\n debit: { type: 'Invoice', id: invoice.externalId },\n credit: { type: 'CreditMemo', id: creditResult.externalId },\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Retention provider state is missing');\n const credit = state.credits.find(\n (c) => c.creditId === input.creditInvoiceId,\n );\n if (!credit) reject('Credit reservation disappeared');\n credit.committed = true;\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\n\nexport interface SyncRetainedRefundToAccountingInput extends Scope {\n paymentId: string;\n creditInvoiceId: string;\n requestId: string;\n evidenceRef: string;\n effectiveOn: string;\n}\n/** Record an already settled local refund and consume its existing remote credit. No bank I/O. */\nexport async function syncRetainedRefundToAccounting(\n input: SyncRetainedRefundToAccountingInput,\n): Promise<RetainedInvoiceAccountingState> {\n principal(input);\n await assertAccountingWriteAllowed({\n db: input.db,\n tenantId: input.tenantId,\n invoiceId: input.invoiceId,\n provider: input.provider,\n });\n text(input.requestId, 'Refund recording request');\n text(input.evidenceRef, 'Refund settlement evidence');\n date(input.effectiveOn, 'Refund effective date');\n const reservation = await locked(input, async (invoice, state) => {\n if (!state) reject('Retention provider contract is missing');\n const credit = state.credits.find(\n (c) => c.creditId === input.creditInvoiceId && c.committed,\n );\n if (!credit) reject('Refund requires its reconciled provider credit');\n const payments = await PaymentCollection.create({ db: invoice.db }),\n payment = await payments.get({ id: input.paymentId }, { cache: false });\n const creditInvoice = await (\n await InvoiceCollection.create({ db: invoice.db })\n ).get({ id: input.creditInvoiceId }, { cache: false });\n if (\n !payment ||\n !creditInvoice ||\n payment.tenantId !== input.tenantId ||\n payment.customerId !== invoice.customerId ||\n payment.currency !== state.currency ||\n payment.kind !== 'refund' ||\n payment.status !== 'completed' ||\n !payment.journalId ||\n !payment.paidAt ||\n !creditInvoice.externalId ||\n creditInvoice.moneySnapshotId !== credit.moneySnapshotId\n )\n reject(\n 'Provider refund requires its completed same-customer local cash journal and source credit',\n );\n const allocations = await (\n await PaymentAllocationCollection.create({ db: invoice.db })\n ).findByPayment(input.paymentId);\n if (\n allocations.length !== 1 ||\n allocations[0].invoiceId !== input.creditInvoiceId ||\n allocations[0].amount !== payment.amount\n )\n reject(\n 'Refund recording requires one exact full source-credit allocation',\n );\n const fingerprint = hash([\n payment.id,\n payment.customerId,\n payment.currency,\n payment.amount,\n payment.journalId,\n new Date(payment.paidAt).toISOString(),\n allocations[0].id,\n allocations[0].amount,\n credit.moneySnapshotId,\n ]);\n const next: RefundRecording = {\n paymentId: input.paymentId,\n creditId: input.creditInvoiceId,\n requestId: input.requestId,\n evidenceRef: input.evidenceRef,\n effectiveOn: input.effectiveOn,\n amountMinor: payment.amount,\n paymentFingerprint: fingerprint,\n committed: false,\n };\n const prior = state.refunds.find(\n (r) => r.paymentId === input.paymentId || r.requestId === input.requestId,\n );\n if (prior) {\n if (hash({ ...prior, committed: false }) !== hash(next))\n reject('Refund recording identity reused with changed facts');\n return { ...prior, externalCreditId: creditInvoice.externalId };\n }\n requireReady(state);\n if (!state.mapping.cashAccountRef)\n reject('Refund provider bank account must be explicitly reviewed');\n const already = state.refunds\n .filter((r) => r.creditId === input.creditInvoiceId)\n .reduce((sum, r) => sum + r.amountMinor, 0);\n if (\n already + payment.amount >\n credit.grossMinor - credit.retainedMinor - credit.appliedMinor\n )\n reject('Refund exceeds unapplied provider credit capacity');\n if (payment.retentionAccountingBindingJson)\n reject('Refund is already bound to a provider operation');\n payment.retentionAccountingBindingJson = JSON.stringify({\n invoiceId: input.invoiceId,\n tenantId: input.tenantId,\n requestId: input.requestId,\n paymentFingerprint: fingerprint,\n });\n await withRetentionAccountingAuthority(\n input.tenantId,\n input.invoiceId,\n () => payment.save(),\n );\n state.refunds.push(next);\n state.status = 'pending';\n await save(invoice, state);\n return { ...next, externalCreditId: creditInvoice.externalId };\n });\n const prefix = `refund:${input.requestId}`;\n const creditSource = (state: RetainedInvoiceAccountingState) => ({\n type: 'CreditMemo' as const,\n id: reservation.externalCreditId,\n customerRef: state.mapping.customerRef,\n evidenceRef: reservation.paymentFingerprint,\n });\n const journal = await executeStep(input, prefix, (_invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'cash-refund',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, prefix),\n effectiveOn: reservation.effectiveOn,\n evidenceRef: reservation.evidenceRef,\n amountMinor: reservation.amountMinor,\n }),\n );\n await executeStep(input, `${prefix}:application`, (_invoice, state) =>\n prepareQuickBooksReceivableRequest({\n kind: 'application',\n mapping: state.mapping,\n source: creditSource(state),\n requestId: requestId(state, `${prefix}:application`),\n effectiveOn: reservation.effectiveOn,\n evidenceRef: reservation.evidenceRef,\n amountMinor: reservation.amountMinor,\n debit: { type: 'JournalEntry', id: journal.externalId },\n credit: { type: 'CreditMemo', id: reservation.externalCreditId },\n }),\n );\n return locked(input, async (invoice, state) => {\n if (!state) reject('Retention provider state disappeared');\n const refund = state.refunds.find((r) => r.requestId === input.requestId);\n if (!refund) reject('Refund reservation disappeared');\n refund.committed = true;\n refreshStatus(state);\n await save(invoice, state);\n return state;\n });\n}\n\nexport interface ReconcileRetainedCreditAccountingInput extends Scope {\n creditInvoiceId: string;\n requestId: string;\n attemptId: string;\n confirmInterruptedOwnerStopped?: true;\n outcome: ReconcileFrozenInvoiceAccountingInput['outcome'];\n}\n/** Resolve an interrupted credit-memo export before its retained-first applications resume. */\nexport async function reconcileRetainedCreditAccounting(\n input: ReconcileRetainedCreditAccountingInput,\n) {\n principal(input);\n const mapping = await locked(input, async (_invoice, state) => {\n const reserved = state?.credits.find(\n (credit) => credit.creditId === input.creditInvoiceId,\n );\n if (!reserved)\n reject('Retained credit has no approved provider reservation');\n return structuredClone(reserved.mapping);\n });\n return withRetentionAccountingAuthority(input.tenantId, input.invoiceId, () =>\n reconcileFrozenInvoiceAccountingSync({\n ...input,\n invoiceId: input.creditInvoiceId,\n mapping,\n }),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDA,IAAM,YAAqD,CAAC,YAAY;AACxE,IAAM,eAA2D,CAC/D,WACA,YACF;AACA,IAAM,WAAkD;CACtD;CACA;CACA;AACF;AAGA,IAAM,yCAAyB,IAAI,QAA8B;AAGjE,eAAsB,0BACpB,YACA,OACY;CACZ,uBAAuB,IAAI,UAAU;CACrC,IAAI;EACF,OAAO,MAAM,MAAM;CACrB,UAAE;EACA,uBAAuB,OAAO,UAAU;CAC1C;AACF;AA4BO,IAAM,uBAAN,cAAmC,WAAW;CAGnD,WAAmB;;CAGnB,WAA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