@voyant-travel/finance 0.119.5
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/LICENSE +201 -0
- package/README.md +192 -0
- package/dist/action-ledger-drift.d.ts +29 -0
- package/dist/action-ledger-drift.d.ts.map +1 -0
- package/dist/action-ledger-drift.js +166 -0
- package/dist/booking-tax.d.ts +124 -0
- package/dist/booking-tax.d.ts.map +1 -0
- package/dist/booking-tax.js +264 -0
- package/dist/checkout-routes.d.ts +1154 -0
- package/dist/checkout-routes.d.ts.map +1 -0
- package/dist/checkout-routes.js +116 -0
- package/dist/checkout-service-plan.d.ts +137 -0
- package/dist/checkout-service-plan.d.ts.map +1 -0
- package/dist/checkout-service-plan.js +119 -0
- package/dist/checkout-service.d.ts +9 -0
- package/dist/checkout-service.d.ts.map +1 -0
- package/dist/checkout-service.js +324 -0
- package/dist/checkout-validation.d.ts +1682 -0
- package/dist/checkout-validation.d.ts.map +1 -0
- package/dist/checkout-validation.js +228 -0
- package/dist/document-download.d.ts +3 -0
- package/dist/document-download.d.ts.map +1 -0
- package/dist/document-download.js +1 -0
- package/dist/fx-money.d.ts +17 -0
- package/dist/fx-money.d.ts.map +1 -0
- package/dist/fx-money.js +194 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -0
- package/dist/invoice-fx.d.ts +134 -0
- package/dist/invoice-fx.d.ts.map +1 -0
- package/dist/invoice-fx.js +240 -0
- package/dist/invoice-number-errors.d.ts +2 -0
- package/dist/invoice-number-errors.d.ts.map +1 -0
- package/dist/invoice-number-errors.js +58 -0
- package/dist/markets-ref.d.ts +149 -0
- package/dist/markets-ref.d.ts.map +1 -0
- package/dist/markets-ref.js +17 -0
- package/dist/payment-link.d.ts +23 -0
- package/dist/payment-link.d.ts.map +1 -0
- package/dist/payment-link.js +30 -0
- package/dist/payment-policy.d.ts +113 -0
- package/dist/payment-policy.d.ts.map +1 -0
- package/dist/payment-policy.js +193 -0
- package/dist/route-runtime.d.ts +22 -0
- package/dist/route-runtime.d.ts.map +1 -0
- package/dist/route-runtime.js +18 -0
- package/dist/routes-action-ledger.d.ts +181 -0
- package/dist/routes-action-ledger.d.ts.map +1 -0
- package/dist/routes-action-ledger.js +142 -0
- package/dist/routes-booking-billing.d.ts +852 -0
- package/dist/routes-booking-billing.d.ts.map +1 -0
- package/dist/routes-booking-billing.js +223 -0
- package/dist/routes-booking-create.d.ts +3 -0
- package/dist/routes-booking-create.d.ts.map +1 -0
- package/dist/routes-booking-create.js +194 -0
- package/dist/routes-booking-reads.d.ts +46 -0
- package/dist/routes-booking-reads.d.ts.map +1 -0
- package/dist/routes-booking-reads.js +20 -0
- package/dist/routes-documents.d.ts +195 -0
- package/dist/routes-documents.d.ts.map +1 -0
- package/dist/routes-documents.js +93 -0
- package/dist/routes-invoice-core.d.ts +794 -0
- package/dist/routes-invoice-core.d.ts.map +1 -0
- package/dist/routes-invoice-core.js +238 -0
- package/dist/routes-invoice-documents.d.ts +401 -0
- package/dist/routes-invoice-documents.d.ts.map +1 -0
- package/dist/routes-invoice-documents.js +91 -0
- package/dist/routes-invoice-issue.d.ts +384 -0
- package/dist/routes-invoice-issue.d.ts.map +1 -0
- package/dist/routes-invoice-issue.js +208 -0
- package/dist/routes-payment-processing.d.ts +1193 -0
- package/dist/routes-payment-processing.d.ts.map +1 -0
- package/dist/routes-payment-processing.js +238 -0
- package/dist/routes-payments.d.ts +309 -0
- package/dist/routes-payments.d.ts.map +1 -0
- package/dist/routes-payments.js +94 -0
- package/dist/routes-public.d.ts +1948 -0
- package/dist/routes-public.d.ts.map +1 -0
- package/dist/routes-public.js +275 -0
- package/dist/routes-reference-data.d.ts +977 -0
- package/dist/routes-reference-data.d.ts.map +1 -0
- package/dist/routes-reference-data.js +191 -0
- package/dist/routes-reports.d.ts +344 -0
- package/dist/routes-reports.d.ts.map +1 -0
- package/dist/routes-reports.js +93 -0
- package/dist/routes-runtime.d.ts +71 -0
- package/dist/routes-runtime.d.ts.map +1 -0
- package/dist/routes-runtime.js +59 -0
- package/dist/routes-settlement.d.ts +67 -0
- package/dist/routes-settlement.d.ts.map +1 -0
- package/dist/routes-settlement.js +23 -0
- package/dist/routes-shared.d.ts +35 -0
- package/dist/routes-shared.d.ts.map +1 -0
- package/dist/routes-shared.js +10 -0
- package/dist/routes-supplier-invoices.d.ts +778 -0
- package/dist/routes-supplier-invoices.d.ts.map +1 -0
- package/dist/routes-supplier-invoices.js +159 -0
- package/dist/routes-vouchers.d.ts +228 -0
- package/dist/routes-vouchers.d.ts.map +1 -0
- package/dist/routes-vouchers.js +54 -0
- package/dist/routes.d.ts +5577 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +44 -0
- package/dist/schema/booking-billing.d.ts +1006 -0
- package/dist/schema/booking-billing.d.ts.map +1 -0
- package/dist/schema/booking-billing.js +106 -0
- package/dist/schema/enums.d.ts +48 -0
- package/dist/schema/enums.d.ts.map +1 -0
- package/dist/schema/enums.js +237 -0
- package/dist/schema/invoice-documents.d.ts +1245 -0
- package/dist/schema/invoice-documents.d.ts.map +1 -0
- package/dist/schema/invoice-documents.js +140 -0
- package/dist/schema/payment-instruments.d.ts +418 -0
- package/dist/schema/payment-instruments.d.ts.map +1 -0
- package/dist/schema/payment-instruments.js +45 -0
- package/dist/schema/payment-processing.d.ts +563 -0
- package/dist/schema/payment-processing.d.ts.map +1 -0
- package/dist/schema/payment-processing.js +65 -0
- package/dist/schema/payment-sessions.d.ts +728 -0
- package/dist/schema/payment-sessions.d.ts.map +1 -0
- package/dist/schema/payment-sessions.js +79 -0
- package/dist/schema/receivables.d.ts +1474 -0
- package/dist/schema/receivables.d.ts.map +1 -0
- package/dist/schema/receivables.js +179 -0
- package/dist/schema/relations.d.ts +82 -0
- package/dist/schema/relations.d.ts.map +1 -0
- package/dist/schema/relations.js +144 -0
- package/dist/schema/supplier-invoices.d.ts +1619 -0
- package/dist/schema/supplier-invoices.d.ts.map +1 -0
- package/dist/schema/supplier-invoices.js +228 -0
- package/dist/schema/tax.d.ts +712 -0
- package/dist/schema/tax.d.ts.map +1 -0
- package/dist/schema/tax.js +98 -0
- package/dist/schema/vouchers.d.ts +444 -0
- package/dist/schema/vouchers.d.ts.map +1 -0
- package/dist/schema/vouchers.js +64 -0
- package/dist/schema.d.ts +12 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +11 -0
- package/dist/service-accountant-shares.d.ts +106 -0
- package/dist/service-accountant-shares.d.ts.map +1 -0
- package/dist/service-accountant-shares.js +331 -0
- package/dist/service-action-ledger-accounting.d.ts +104 -0
- package/dist/service-action-ledger-accounting.d.ts.map +1 -0
- package/dist/service-action-ledger-accounting.js +386 -0
- package/dist/service-action-ledger-booking-payments.d.ts +48 -0
- package/dist/service-action-ledger-booking-payments.d.ts.map +1 -0
- package/dist/service-action-ledger-booking-payments.js +178 -0
- package/dist/service-action-ledger-bookings.d.ts +44 -0
- package/dist/service-action-ledger-bookings.d.ts.map +1 -0
- package/dist/service-action-ledger-bookings.js +81 -0
- package/dist/service-action-ledger-payment-authorizations.d.ts +48 -0
- package/dist/service-action-ledger-payment-authorizations.d.ts.map +1 -0
- package/dist/service-action-ledger-payment-authorizations.js +209 -0
- package/dist/service-action-ledger-payment-sessions.d.ts +83 -0
- package/dist/service-action-ledger-payment-sessions.d.ts.map +1 -0
- package/dist/service-action-ledger-payment-sessions.js +294 -0
- package/dist/service-action-ledger-supplier-invoices.d.ts +27 -0
- package/dist/service-action-ledger-supplier-invoices.d.ts.map +1 -0
- package/dist/service-action-ledger-supplier-invoices.js +111 -0
- package/dist/service-action-ledger-supplier-payments.d.ts +21 -0
- package/dist/service-action-ledger-supplier-payments.d.ts.map +1 -0
- package/dist/service-action-ledger-supplier-payments.js +97 -0
- package/dist/service-action-ledger.d.ts +7 -0
- package/dist/service-action-ledger.d.ts.map +1 -0
- package/dist/service-action-ledger.js +6 -0
- package/dist/service-aggregates.d.ts +96 -0
- package/dist/service-aggregates.d.ts.map +1 -0
- package/dist/service-aggregates.js +294 -0
- package/dist/service-booking-billing.d.ts +2322 -0
- package/dist/service-booking-billing.d.ts.map +1 -0
- package/dist/service-booking-billing.js +8 -0
- package/dist/service-booking-create.d.ts +410 -0
- package/dist/service-booking-create.d.ts.map +1 -0
- package/dist/service-booking-create.js +1256 -0
- package/dist/service-booking-guarantees.d.ts +725 -0
- package/dist/service-booking-guarantees.d.ts.map +1 -0
- package/dist/service-booking-guarantees.js +153 -0
- package/dist/service-booking-item-billing.d.ts +1062 -0
- package/dist/service-booking-item-billing.d.ts.map +1 -0
- package/dist/service-booking-item-billing.js +77 -0
- package/dist/service-booking-payment-schedules.d.ts +557 -0
- package/dist/service-booking-payment-schedules.d.ts.map +1 -0
- package/dist/service-booking-payment-schedules.js +372 -0
- package/dist/service-bookings-dual-create.d.ts +308 -0
- package/dist/service-bookings-dual-create.d.ts.map +1 -0
- package/dist/service-bookings-dual-create.js +131 -0
- package/dist/service-boundary-sql.d.ts +6 -0
- package/dist/service-boundary-sql.d.ts.map +1 -0
- package/dist/service-boundary-sql.js +15 -0
- package/dist/service-cost-categories.d.ts +26 -0
- package/dist/service-cost-categories.d.ts.map +1 -0
- package/dist/service-cost-categories.js +76 -0
- package/dist/service-documents.d.ts +80 -0
- package/dist/service-documents.d.ts.map +1 -0
- package/dist/service-documents.js +228 -0
- package/dist/service-invoice-artifacts.d.ts +246 -0
- package/dist/service-invoice-artifacts.d.ts.map +1 -0
- package/dist/service-invoice-artifacts.js +277 -0
- package/dist/service-invoice-core.d.ts +405 -0
- package/dist/service-invoice-core.d.ts.map +1 -0
- package/dist/service-invoice-core.js +290 -0
- package/dist/service-invoice-credit-notes.d.ts +973 -0
- package/dist/service-invoice-credit-notes.d.ts.map +1 -0
- package/dist/service-invoice-credit-notes.js +142 -0
- package/dist/service-invoice-from-booking.d.ts +41 -0
- package/dist/service-invoice-from-booking.d.ts.map +1 -0
- package/dist/service-invoice-from-booking.js +267 -0
- package/dist/service-invoice-line-items.d.ts +432 -0
- package/dist/service-invoice-line-items.d.ts.map +1 -0
- package/dist/service-invoice-line-items.js +102 -0
- package/dist/service-invoice-numbering.d.ts +227 -0
- package/dist/service-invoice-numbering.d.ts.map +1 -0
- package/dist/service-invoice-numbering.js +260 -0
- package/dist/service-invoice-payments.d.ts +673 -0
- package/dist/service-invoice-payments.d.ts.map +1 -0
- package/dist/service-invoice-payments.js +398 -0
- package/dist/service-invoices.d.ts +2501 -0
- package/dist/service-invoices.d.ts.map +1 -0
- package/dist/service-invoices.js +12 -0
- package/dist/service-issue.d.ts +207 -0
- package/dist/service-issue.d.ts.map +1 -0
- package/dist/service-issue.js +431 -0
- package/dist/service-payment-authorizations.d.ts +164 -0
- package/dist/service-payment-authorizations.d.ts.map +1 -0
- package/dist/service-payment-authorizations.js +227 -0
- package/dist/service-payment-instruments.d.ts +116 -0
- package/dist/service-payment-instruments.d.ts.map +1 -0
- package/dist/service-payment-instruments.js +99 -0
- package/dist/service-payment-processing.d.ts +676 -0
- package/dist/service-payment-processing.d.ts.map +1 -0
- package/dist/service-payment-processing.js +10 -0
- package/dist/service-payment-session-completion.d.ts +48 -0
- package/dist/service-payment-session-completion.d.ts.map +1 -0
- package/dist/service-payment-session-completion.js +238 -0
- package/dist/service-payment-sessions.d.ts +361 -0
- package/dist/service-payment-sessions.d.ts.map +1 -0
- package/dist/service-payment-sessions.js +280 -0
- package/dist/service-profitability.d.ts +114 -0
- package/dist/service-profitability.d.ts.map +1 -0
- package/dist/service-profitability.js +794 -0
- package/dist/service-public.d.ts +553 -0
- package/dist/service-public.d.ts.map +1 -0
- package/dist/service-public.js +583 -0
- package/dist/service-reference-data.d.ts +272 -0
- package/dist/service-reference-data.d.ts.map +1 -0
- package/dist/service-reference-data.js +280 -0
- package/dist/service-rendition-wait.d.ts +38 -0
- package/dist/service-rendition-wait.d.ts.map +1 -0
- package/dist/service-rendition-wait.js +67 -0
- package/dist/service-reports.d.ts +37 -0
- package/dist/service-reports.d.ts.map +1 -0
- package/dist/service-reports.js +62 -0
- package/dist/service-settlement.d.ts +46 -0
- package/dist/service-settlement.d.ts.map +1 -0
- package/dist/service-settlement.js +185 -0
- package/dist/service-shared.d.ts +541 -0
- package/dist/service-shared.d.ts.map +1 -0
- package/dist/service-shared.js +764 -0
- package/dist/service-supplier-invoices.d.ts +871 -0
- package/dist/service-supplier-invoices.d.ts.map +1 -0
- package/dist/service-supplier-invoices.js +744 -0
- package/dist/service-supplier-payments.d.ts +69 -0
- package/dist/service-supplier-payments.d.ts.map +1 -0
- package/dist/service-supplier-payments.js +136 -0
- package/dist/service-vouchers-migration.d.ts +44 -0
- package/dist/service-vouchers-migration.d.ts.map +1 -0
- package/dist/service-vouchers-migration.js +148 -0
- package/dist/service-vouchers.d.ts +157 -0
- package/dist/service-vouchers.d.ts.map +1 -0
- package/dist/service-vouchers.js +191 -0
- package/dist/service.d.ts +6490 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +29 -0
- package/dist/validation-billing.d.ts +2 -0
- package/dist/validation-billing.d.ts.map +1 -0
- package/dist/validation-billing.js +1 -0
- package/dist/validation-payments.d.ts +2 -0
- package/dist/validation-payments.d.ts.map +1 -0
- package/dist/validation-payments.js +1 -0
- package/dist/validation-public.d.ts +2 -0
- package/dist/validation-public.d.ts.map +1 -0
- package/dist/validation-public.js +1 -0
- package/dist/validation-shared.d.ts +2 -0
- package/dist/validation-shared.d.ts.map +1 -0
- package/dist/validation-shared.js +1 -0
- package/dist/validation-vouchers.d.ts +2 -0
- package/dist/validation-vouchers.d.ts.map +1 -0
- package/dist/validation-vouchers.js +1 -0
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +1 -0
- package/package.json +121 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import type { CreateInvoiceInput, FinanceServiceRuntime, InvoiceListQuery, PostgresJsDatabase, UpdateInvoiceInput, VoidInvoiceInput } from "./service-shared.js";
|
|
2
|
+
export declare const financeInvoiceCoreService: {
|
|
3
|
+
listInvoices(db: PostgresJsDatabase, query: InvoiceListQuery): Promise<{
|
|
4
|
+
data: {
|
|
5
|
+
bookingPaymentScheduleIds: string[];
|
|
6
|
+
id: string;
|
|
7
|
+
invoiceNumber: string;
|
|
8
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
9
|
+
convertedFromInvoiceId: string | null;
|
|
10
|
+
seriesId: string | null;
|
|
11
|
+
sequence: number | null;
|
|
12
|
+
templateId: string | null;
|
|
13
|
+
taxRegimeId: string | null;
|
|
14
|
+
language: string | null;
|
|
15
|
+
bookingId: string;
|
|
16
|
+
personId: string | null;
|
|
17
|
+
organizationId: string | null;
|
|
18
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
19
|
+
currency: string;
|
|
20
|
+
baseCurrency: string | null;
|
|
21
|
+
fxRateSetId: string | null;
|
|
22
|
+
subtotalCents: number;
|
|
23
|
+
baseSubtotalCents: number | null;
|
|
24
|
+
taxCents: number;
|
|
25
|
+
baseTaxCents: number | null;
|
|
26
|
+
totalCents: number;
|
|
27
|
+
baseTotalCents: number | null;
|
|
28
|
+
paidCents: number;
|
|
29
|
+
basePaidCents: number | null;
|
|
30
|
+
balanceDueCents: number;
|
|
31
|
+
baseBalanceDueCents: number | null;
|
|
32
|
+
commissionPercent: number | null;
|
|
33
|
+
commissionAmountCents: number | null;
|
|
34
|
+
issueDate: string;
|
|
35
|
+
dueDate: string;
|
|
36
|
+
notes: string | null;
|
|
37
|
+
voidedAt: Date | null;
|
|
38
|
+
voidReason: string | null;
|
|
39
|
+
createdAt: Date;
|
|
40
|
+
updatedAt: Date;
|
|
41
|
+
}[];
|
|
42
|
+
total: number;
|
|
43
|
+
limit: number;
|
|
44
|
+
offset: number;
|
|
45
|
+
}>;
|
|
46
|
+
createInvoice(db: PostgresJsDatabase, data: CreateInvoiceInput): Promise<{
|
|
47
|
+
subtotalCents: number;
|
|
48
|
+
currency: string;
|
|
49
|
+
id: string;
|
|
50
|
+
createdAt: Date;
|
|
51
|
+
updatedAt: Date;
|
|
52
|
+
taxRegimeId: string | null;
|
|
53
|
+
taxCents: number;
|
|
54
|
+
totalCents: number;
|
|
55
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
56
|
+
bookingId: string;
|
|
57
|
+
notes: string | null;
|
|
58
|
+
templateId: string | null;
|
|
59
|
+
dueDate: string;
|
|
60
|
+
invoiceNumber: string;
|
|
61
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
62
|
+
convertedFromInvoiceId: string | null;
|
|
63
|
+
seriesId: string | null;
|
|
64
|
+
sequence: number | null;
|
|
65
|
+
language: string | null;
|
|
66
|
+
personId: string | null;
|
|
67
|
+
organizationId: string | null;
|
|
68
|
+
baseCurrency: string | null;
|
|
69
|
+
fxRateSetId: string | null;
|
|
70
|
+
baseSubtotalCents: number | null;
|
|
71
|
+
baseTaxCents: number | null;
|
|
72
|
+
baseTotalCents: number | null;
|
|
73
|
+
paidCents: number;
|
|
74
|
+
basePaidCents: number | null;
|
|
75
|
+
balanceDueCents: number;
|
|
76
|
+
baseBalanceDueCents: number | null;
|
|
77
|
+
commissionPercent: number | null;
|
|
78
|
+
commissionAmountCents: number | null;
|
|
79
|
+
issueDate: string;
|
|
80
|
+
voidedAt: Date | null;
|
|
81
|
+
voidReason: string | null;
|
|
82
|
+
} | undefined>;
|
|
83
|
+
getInvoiceById(db: PostgresJsDatabase, id: string): Promise<{
|
|
84
|
+
convertedToInvoiceId: string | null;
|
|
85
|
+
convertedToInvoiceNumber: string | null;
|
|
86
|
+
id: string;
|
|
87
|
+
invoiceNumber: string;
|
|
88
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
89
|
+
convertedFromInvoiceId: string | null;
|
|
90
|
+
seriesId: string | null;
|
|
91
|
+
sequence: number | null;
|
|
92
|
+
templateId: string | null;
|
|
93
|
+
taxRegimeId: string | null;
|
|
94
|
+
language: string | null;
|
|
95
|
+
bookingId: string;
|
|
96
|
+
personId: string | null;
|
|
97
|
+
organizationId: string | null;
|
|
98
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
99
|
+
currency: string;
|
|
100
|
+
baseCurrency: string | null;
|
|
101
|
+
fxRateSetId: string | null;
|
|
102
|
+
subtotalCents: number;
|
|
103
|
+
baseSubtotalCents: number | null;
|
|
104
|
+
taxCents: number;
|
|
105
|
+
baseTaxCents: number | null;
|
|
106
|
+
totalCents: number;
|
|
107
|
+
baseTotalCents: number | null;
|
|
108
|
+
paidCents: number;
|
|
109
|
+
basePaidCents: number | null;
|
|
110
|
+
balanceDueCents: number;
|
|
111
|
+
baseBalanceDueCents: number | null;
|
|
112
|
+
commissionPercent: number | null;
|
|
113
|
+
commissionAmountCents: number | null;
|
|
114
|
+
issueDate: string;
|
|
115
|
+
dueDate: string;
|
|
116
|
+
notes: string | null;
|
|
117
|
+
voidedAt: Date | null;
|
|
118
|
+
voidReason: string | null;
|
|
119
|
+
createdAt: Date;
|
|
120
|
+
updatedAt: Date;
|
|
121
|
+
} | null>;
|
|
122
|
+
updateInvoice(db: PostgresJsDatabase, id: string, data: UpdateInvoiceInput, runtime?: FinanceServiceRuntime): Promise<{
|
|
123
|
+
id: string;
|
|
124
|
+
invoiceNumber: string;
|
|
125
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
126
|
+
convertedFromInvoiceId: string | null;
|
|
127
|
+
seriesId: string | null;
|
|
128
|
+
sequence: number | null;
|
|
129
|
+
templateId: string | null;
|
|
130
|
+
taxRegimeId: string | null;
|
|
131
|
+
language: string | null;
|
|
132
|
+
bookingId: string;
|
|
133
|
+
personId: string | null;
|
|
134
|
+
organizationId: string | null;
|
|
135
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
136
|
+
currency: string;
|
|
137
|
+
baseCurrency: string | null;
|
|
138
|
+
fxRateSetId: string | null;
|
|
139
|
+
subtotalCents: number;
|
|
140
|
+
baseSubtotalCents: number | null;
|
|
141
|
+
taxCents: number;
|
|
142
|
+
baseTaxCents: number | null;
|
|
143
|
+
totalCents: number;
|
|
144
|
+
baseTotalCents: number | null;
|
|
145
|
+
paidCents: number;
|
|
146
|
+
basePaidCents: number | null;
|
|
147
|
+
balanceDueCents: number;
|
|
148
|
+
baseBalanceDueCents: number | null;
|
|
149
|
+
commissionPercent: number | null;
|
|
150
|
+
commissionAmountCents: number | null;
|
|
151
|
+
issueDate: string;
|
|
152
|
+
dueDate: string;
|
|
153
|
+
notes: string | null;
|
|
154
|
+
voidedAt: Date | null;
|
|
155
|
+
voidReason: string | null;
|
|
156
|
+
createdAt: Date;
|
|
157
|
+
updatedAt: Date;
|
|
158
|
+
} | null>;
|
|
159
|
+
deleteInvoice(db: PostgresJsDatabase, id: string, runtime?: FinanceServiceRuntime): Promise<{
|
|
160
|
+
status: "not_found";
|
|
161
|
+
} | {
|
|
162
|
+
status: "not_draft";
|
|
163
|
+
} | {
|
|
164
|
+
status: "deleted";
|
|
165
|
+
}>;
|
|
166
|
+
voidInvoice(db: PostgresJsDatabase, id: string, input?: VoidInvoiceInput, runtime?: FinanceServiceRuntime): Promise<{
|
|
167
|
+
status: "not_found";
|
|
168
|
+
invoice?: undefined;
|
|
169
|
+
} | {
|
|
170
|
+
status: "already_void";
|
|
171
|
+
invoice: {
|
|
172
|
+
id: string;
|
|
173
|
+
invoiceNumber: string;
|
|
174
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
175
|
+
convertedFromInvoiceId: string | null;
|
|
176
|
+
seriesId: string | null;
|
|
177
|
+
sequence: number | null;
|
|
178
|
+
templateId: string | null;
|
|
179
|
+
taxRegimeId: string | null;
|
|
180
|
+
language: string | null;
|
|
181
|
+
bookingId: string;
|
|
182
|
+
personId: string | null;
|
|
183
|
+
organizationId: string | null;
|
|
184
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
185
|
+
currency: string;
|
|
186
|
+
baseCurrency: string | null;
|
|
187
|
+
fxRateSetId: string | null;
|
|
188
|
+
subtotalCents: number;
|
|
189
|
+
baseSubtotalCents: number | null;
|
|
190
|
+
taxCents: number;
|
|
191
|
+
baseTaxCents: number | null;
|
|
192
|
+
totalCents: number;
|
|
193
|
+
baseTotalCents: number | null;
|
|
194
|
+
paidCents: number;
|
|
195
|
+
basePaidCents: number | null;
|
|
196
|
+
balanceDueCents: number;
|
|
197
|
+
baseBalanceDueCents: number | null;
|
|
198
|
+
commissionPercent: number | null;
|
|
199
|
+
commissionAmountCents: number | null;
|
|
200
|
+
issueDate: string;
|
|
201
|
+
dueDate: string;
|
|
202
|
+
notes: string | null;
|
|
203
|
+
voidedAt: Date | null;
|
|
204
|
+
voidReason: string | null;
|
|
205
|
+
createdAt: Date;
|
|
206
|
+
updatedAt: Date;
|
|
207
|
+
};
|
|
208
|
+
} | {
|
|
209
|
+
status: "draft";
|
|
210
|
+
invoice: {
|
|
211
|
+
id: string;
|
|
212
|
+
invoiceNumber: string;
|
|
213
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
214
|
+
convertedFromInvoiceId: string | null;
|
|
215
|
+
seriesId: string | null;
|
|
216
|
+
sequence: number | null;
|
|
217
|
+
templateId: string | null;
|
|
218
|
+
taxRegimeId: string | null;
|
|
219
|
+
language: string | null;
|
|
220
|
+
bookingId: string;
|
|
221
|
+
personId: string | null;
|
|
222
|
+
organizationId: string | null;
|
|
223
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
224
|
+
currency: string;
|
|
225
|
+
baseCurrency: string | null;
|
|
226
|
+
fxRateSetId: string | null;
|
|
227
|
+
subtotalCents: number;
|
|
228
|
+
baseSubtotalCents: number | null;
|
|
229
|
+
taxCents: number;
|
|
230
|
+
baseTaxCents: number | null;
|
|
231
|
+
totalCents: number;
|
|
232
|
+
baseTotalCents: number | null;
|
|
233
|
+
paidCents: number;
|
|
234
|
+
basePaidCents: number | null;
|
|
235
|
+
balanceDueCents: number;
|
|
236
|
+
baseBalanceDueCents: number | null;
|
|
237
|
+
commissionPercent: number | null;
|
|
238
|
+
commissionAmountCents: number | null;
|
|
239
|
+
issueDate: string;
|
|
240
|
+
dueDate: string;
|
|
241
|
+
notes: string | null;
|
|
242
|
+
voidedAt: Date | null;
|
|
243
|
+
voidReason: string | null;
|
|
244
|
+
createdAt: Date;
|
|
245
|
+
updatedAt: Date;
|
|
246
|
+
};
|
|
247
|
+
} | {
|
|
248
|
+
status: "invalid_status";
|
|
249
|
+
invoice: {
|
|
250
|
+
id: string;
|
|
251
|
+
invoiceNumber: string;
|
|
252
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
253
|
+
convertedFromInvoiceId: string | null;
|
|
254
|
+
seriesId: string | null;
|
|
255
|
+
sequence: number | null;
|
|
256
|
+
templateId: string | null;
|
|
257
|
+
taxRegimeId: string | null;
|
|
258
|
+
language: string | null;
|
|
259
|
+
bookingId: string;
|
|
260
|
+
personId: string | null;
|
|
261
|
+
organizationId: string | null;
|
|
262
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
263
|
+
currency: string;
|
|
264
|
+
baseCurrency: string | null;
|
|
265
|
+
fxRateSetId: string | null;
|
|
266
|
+
subtotalCents: number;
|
|
267
|
+
baseSubtotalCents: number | null;
|
|
268
|
+
taxCents: number;
|
|
269
|
+
baseTaxCents: number | null;
|
|
270
|
+
totalCents: number;
|
|
271
|
+
baseTotalCents: number | null;
|
|
272
|
+
paidCents: number;
|
|
273
|
+
basePaidCents: number | null;
|
|
274
|
+
balanceDueCents: number;
|
|
275
|
+
baseBalanceDueCents: number | null;
|
|
276
|
+
commissionPercent: number | null;
|
|
277
|
+
commissionAmountCents: number | null;
|
|
278
|
+
issueDate: string;
|
|
279
|
+
dueDate: string;
|
|
280
|
+
notes: string | null;
|
|
281
|
+
voidedAt: Date | null;
|
|
282
|
+
voidReason: string | null;
|
|
283
|
+
createdAt: Date;
|
|
284
|
+
updatedAt: Date;
|
|
285
|
+
};
|
|
286
|
+
} | {
|
|
287
|
+
status: "has_payments";
|
|
288
|
+
invoice: {
|
|
289
|
+
id: string;
|
|
290
|
+
invoiceNumber: string;
|
|
291
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
292
|
+
convertedFromInvoiceId: string | null;
|
|
293
|
+
seriesId: string | null;
|
|
294
|
+
sequence: number | null;
|
|
295
|
+
templateId: string | null;
|
|
296
|
+
taxRegimeId: string | null;
|
|
297
|
+
language: string | null;
|
|
298
|
+
bookingId: string;
|
|
299
|
+
personId: string | null;
|
|
300
|
+
organizationId: string | null;
|
|
301
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
302
|
+
currency: string;
|
|
303
|
+
baseCurrency: string | null;
|
|
304
|
+
fxRateSetId: string | null;
|
|
305
|
+
subtotalCents: number;
|
|
306
|
+
baseSubtotalCents: number | null;
|
|
307
|
+
taxCents: number;
|
|
308
|
+
baseTaxCents: number | null;
|
|
309
|
+
totalCents: number;
|
|
310
|
+
baseTotalCents: number | null;
|
|
311
|
+
paidCents: number;
|
|
312
|
+
basePaidCents: number | null;
|
|
313
|
+
balanceDueCents: number;
|
|
314
|
+
baseBalanceDueCents: number | null;
|
|
315
|
+
commissionPercent: number | null;
|
|
316
|
+
commissionAmountCents: number | null;
|
|
317
|
+
issueDate: string;
|
|
318
|
+
dueDate: string;
|
|
319
|
+
notes: string | null;
|
|
320
|
+
voidedAt: Date | null;
|
|
321
|
+
voidReason: string | null;
|
|
322
|
+
createdAt: Date;
|
|
323
|
+
updatedAt: Date;
|
|
324
|
+
};
|
|
325
|
+
} | {
|
|
326
|
+
status: "has_credit_notes";
|
|
327
|
+
invoice: {
|
|
328
|
+
id: string;
|
|
329
|
+
invoiceNumber: string;
|
|
330
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
331
|
+
convertedFromInvoiceId: string | null;
|
|
332
|
+
seriesId: string | null;
|
|
333
|
+
sequence: number | null;
|
|
334
|
+
templateId: string | null;
|
|
335
|
+
taxRegimeId: string | null;
|
|
336
|
+
language: string | null;
|
|
337
|
+
bookingId: string;
|
|
338
|
+
personId: string | null;
|
|
339
|
+
organizationId: string | null;
|
|
340
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
341
|
+
currency: string;
|
|
342
|
+
baseCurrency: string | null;
|
|
343
|
+
fxRateSetId: string | null;
|
|
344
|
+
subtotalCents: number;
|
|
345
|
+
baseSubtotalCents: number | null;
|
|
346
|
+
taxCents: number;
|
|
347
|
+
baseTaxCents: number | null;
|
|
348
|
+
totalCents: number;
|
|
349
|
+
baseTotalCents: number | null;
|
|
350
|
+
paidCents: number;
|
|
351
|
+
basePaidCents: number | null;
|
|
352
|
+
balanceDueCents: number;
|
|
353
|
+
baseBalanceDueCents: number | null;
|
|
354
|
+
commissionPercent: number | null;
|
|
355
|
+
commissionAmountCents: number | null;
|
|
356
|
+
issueDate: string;
|
|
357
|
+
dueDate: string;
|
|
358
|
+
notes: string | null;
|
|
359
|
+
voidedAt: Date | null;
|
|
360
|
+
voidReason: string | null;
|
|
361
|
+
createdAt: Date;
|
|
362
|
+
updatedAt: Date;
|
|
363
|
+
};
|
|
364
|
+
} | {
|
|
365
|
+
status: "voided";
|
|
366
|
+
invoice: {
|
|
367
|
+
id: string;
|
|
368
|
+
invoiceNumber: string;
|
|
369
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
370
|
+
convertedFromInvoiceId: string | null;
|
|
371
|
+
seriesId: string | null;
|
|
372
|
+
sequence: number | null;
|
|
373
|
+
templateId: string | null;
|
|
374
|
+
taxRegimeId: string | null;
|
|
375
|
+
language: string | null;
|
|
376
|
+
bookingId: string;
|
|
377
|
+
personId: string | null;
|
|
378
|
+
organizationId: string | null;
|
|
379
|
+
status: "void" | "paid" | "draft" | "pending_external_allocation" | "issued" | "partially_paid" | "overdue";
|
|
380
|
+
currency: string;
|
|
381
|
+
baseCurrency: string | null;
|
|
382
|
+
fxRateSetId: string | null;
|
|
383
|
+
subtotalCents: number;
|
|
384
|
+
baseSubtotalCents: number | null;
|
|
385
|
+
taxCents: number;
|
|
386
|
+
baseTaxCents: number | null;
|
|
387
|
+
totalCents: number;
|
|
388
|
+
baseTotalCents: number | null;
|
|
389
|
+
paidCents: number;
|
|
390
|
+
basePaidCents: number | null;
|
|
391
|
+
balanceDueCents: number;
|
|
392
|
+
baseBalanceDueCents: number | null;
|
|
393
|
+
commissionPercent: number | null;
|
|
394
|
+
commissionAmountCents: number | null;
|
|
395
|
+
issueDate: string;
|
|
396
|
+
dueDate: string;
|
|
397
|
+
notes: string | null;
|
|
398
|
+
voidedAt: Date | null;
|
|
399
|
+
voidReason: string | null;
|
|
400
|
+
createdAt: Date;
|
|
401
|
+
updatedAt: Date;
|
|
402
|
+
};
|
|
403
|
+
}>;
|
|
404
|
+
};
|
|
405
|
+
//# sourceMappingURL=service-invoice-core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service-invoice-core.d.ts","sourceRoot":"","sources":["../src/service-invoice-core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,qBAAqB,EACrB,gBAAgB,EAEhB,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,qBAAqB,CAAA;AA4B5B,eAAO,MAAM,yBAAyB;qBACb,kBAAkB,SAAS,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAmH1C,kBAAkB,QAAQ,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBAI3C,kBAAkB,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAqBjD,kBAAkB,MAClB,MAAM,QACJ,kBAAkB,YACf,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAwCR,kBAAkB,MAAM,MAAM,YAAW,qBAAqB;;;;;;;oBA8ChF,kBAAkB,MAClB,MAAM,UACH,gBAAgB,YACd,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0IjC,CAAA"}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { and, appendActionLedgerMutation, asc, buildInvoiceDeleteActionLedgerInput, buildInvoiceUpdateActionLedgerInput, creditNotes, desc, eq, gte, InvoiceNumberConflictError, ilike, inArray, invoiceExternalRefs, invoiceLineItems, invoiceNumberSeries, invoices, isInvoiceNumberUniqueConstraintError, isNotNull, lte, ne, or, payments, readStringMetadata, sql, } from "./service-shared.js";
|
|
2
|
+
export const financeInvoiceCoreService = {
|
|
3
|
+
async listInvoices(db, query) {
|
|
4
|
+
const conditions = [];
|
|
5
|
+
if (query.status) {
|
|
6
|
+
conditions.push(eq(invoices.status, query.status));
|
|
7
|
+
}
|
|
8
|
+
if (query.bookingId) {
|
|
9
|
+
conditions.push(eq(invoices.bookingId, query.bookingId));
|
|
10
|
+
}
|
|
11
|
+
if (query.personId) {
|
|
12
|
+
conditions.push(eq(invoices.personId, query.personId));
|
|
13
|
+
}
|
|
14
|
+
if (query.organizationId) {
|
|
15
|
+
conditions.push(eq(invoices.organizationId, query.organizationId));
|
|
16
|
+
}
|
|
17
|
+
if (query.currency) {
|
|
18
|
+
conditions.push(eq(invoices.currency, query.currency));
|
|
19
|
+
}
|
|
20
|
+
if (query.dueDateFrom) {
|
|
21
|
+
conditions.push(gte(invoices.dueDate, query.dueDateFrom));
|
|
22
|
+
}
|
|
23
|
+
if (query.dueDateTo) {
|
|
24
|
+
conditions.push(lte(invoices.dueDate, query.dueDateTo));
|
|
25
|
+
}
|
|
26
|
+
if (query.search) {
|
|
27
|
+
const term = `%${query.search}%`;
|
|
28
|
+
conditions.push(or(ilike(invoices.invoiceNumber, term), ilike(invoices.notes, term)));
|
|
29
|
+
}
|
|
30
|
+
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
|
31
|
+
const sortColumn = (() => {
|
|
32
|
+
switch (query.sortBy) {
|
|
33
|
+
case "invoiceNumber":
|
|
34
|
+
return invoices.invoiceNumber;
|
|
35
|
+
case "status":
|
|
36
|
+
return invoices.status;
|
|
37
|
+
case "totalCents":
|
|
38
|
+
return invoices.totalCents;
|
|
39
|
+
case "paidCents":
|
|
40
|
+
return invoices.paidCents;
|
|
41
|
+
case "balanceDueCents":
|
|
42
|
+
return invoices.balanceDueCents;
|
|
43
|
+
case "issueDate":
|
|
44
|
+
return invoices.issueDate;
|
|
45
|
+
case "dueDate":
|
|
46
|
+
return invoices.dueDate;
|
|
47
|
+
default:
|
|
48
|
+
return invoices.createdAt;
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
51
|
+
const sortFn = query.sortDir === "asc" ? asc : desc;
|
|
52
|
+
const [rows, countResult] = await Promise.all([
|
|
53
|
+
db
|
|
54
|
+
.select()
|
|
55
|
+
.from(invoices)
|
|
56
|
+
.where(where)
|
|
57
|
+
.limit(query.limit)
|
|
58
|
+
.offset(query.offset)
|
|
59
|
+
.orderBy(sortFn(sortColumn), desc(invoices.createdAt)),
|
|
60
|
+
db.select({ count: sql `count(*)::int` }).from(invoices).where(where),
|
|
61
|
+
]);
|
|
62
|
+
// For each returned invoice, surface the distinct
|
|
63
|
+
// `bookingPaymentScheduleId`s referenced by its line items so the
|
|
64
|
+
// booking-detail payment-schedule table can link rows to invoices
|
|
65
|
+
// without a second roundtrip. Returns `[]` when an invoice covers
|
|
66
|
+
// booking items directly (no schedule link).
|
|
67
|
+
const invoiceIds = rows.map((row) => row.id);
|
|
68
|
+
const scheduleLinks = invoiceIds.length
|
|
69
|
+
? await db
|
|
70
|
+
.select({
|
|
71
|
+
invoiceId: invoiceLineItems.invoiceId,
|
|
72
|
+
bookingPaymentScheduleId: invoiceLineItems.bookingPaymentScheduleId,
|
|
73
|
+
})
|
|
74
|
+
.from(invoiceLineItems)
|
|
75
|
+
.where(and(inArray(invoiceLineItems.invoiceId, invoiceIds), isNotNull(invoiceLineItems.bookingPaymentScheduleId)))
|
|
76
|
+
: [];
|
|
77
|
+
const scheduleIdsByInvoice = new Map();
|
|
78
|
+
for (const link of scheduleLinks) {
|
|
79
|
+
const scheduleId = link.bookingPaymentScheduleId;
|
|
80
|
+
if (!scheduleId)
|
|
81
|
+
continue;
|
|
82
|
+
const existing = scheduleIdsByInvoice.get(link.invoiceId);
|
|
83
|
+
if (!existing) {
|
|
84
|
+
scheduleIdsByInvoice.set(link.invoiceId, [scheduleId]);
|
|
85
|
+
}
|
|
86
|
+
else if (!existing.includes(scheduleId)) {
|
|
87
|
+
existing.push(scheduleId);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const data = rows.map((row) => ({
|
|
91
|
+
...row,
|
|
92
|
+
bookingPaymentScheduleIds: scheduleIdsByInvoice.get(row.id) ?? [],
|
|
93
|
+
}));
|
|
94
|
+
return {
|
|
95
|
+
data,
|
|
96
|
+
total: countResult[0]?.count ?? 0,
|
|
97
|
+
limit: query.limit,
|
|
98
|
+
offset: query.offset,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
async createInvoice(db, data) {
|
|
102
|
+
const [row] = await db.insert(invoices).values(data).returning();
|
|
103
|
+
return row;
|
|
104
|
+
},
|
|
105
|
+
async getInvoiceById(db, id) {
|
|
106
|
+
const [row] = await db.select().from(invoices).where(eq(invoices.id, id)).limit(1);
|
|
107
|
+
if (!row)
|
|
108
|
+
return null;
|
|
109
|
+
// Surface the proforma → final-invoice link so the UI can show
|
|
110
|
+
// "Invoiced" instead of "Void" for proformas that were converted.
|
|
111
|
+
// The reverse direction (`convertedFromInvoiceId`) already lives on
|
|
112
|
+
// the row; this looks up the inverse via the unique
|
|
113
|
+
// `idx_invoices_converted_from` index.
|
|
114
|
+
const [convertedTo] = await db
|
|
115
|
+
.select({ id: invoices.id, invoiceNumber: invoices.invoiceNumber })
|
|
116
|
+
.from(invoices)
|
|
117
|
+
.where(and(eq(invoices.convertedFromInvoiceId, id), ne(invoices.status, "void")))
|
|
118
|
+
.limit(1);
|
|
119
|
+
return {
|
|
120
|
+
...row,
|
|
121
|
+
convertedToInvoiceId: convertedTo?.id ?? null,
|
|
122
|
+
convertedToInvoiceNumber: convertedTo?.invoiceNumber ?? null,
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
async updateInvoice(db, id, data, runtime = {}) {
|
|
126
|
+
const updateInvoiceRow = (writer) => writer
|
|
127
|
+
.update(invoices)
|
|
128
|
+
.set({ ...data, updatedAt: new Date() })
|
|
129
|
+
.where(eq(invoices.id, id))
|
|
130
|
+
.returning();
|
|
131
|
+
try {
|
|
132
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
133
|
+
if (actionLedgerContext) {
|
|
134
|
+
return await db.transaction(async (tx) => {
|
|
135
|
+
const [row] = await updateInvoiceRow(tx);
|
|
136
|
+
if (row) {
|
|
137
|
+
await appendActionLedgerMutation(tx, buildInvoiceUpdateActionLedgerInput(actionLedgerContext, { invoice: row, changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
138
|
+
}
|
|
139
|
+
return row ?? null;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const [row] = await updateInvoiceRow(db);
|
|
143
|
+
return row ?? null;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (data.invoiceNumber && isInvoiceNumberUniqueConstraintError(error)) {
|
|
147
|
+
throw new InvoiceNumberConflictError(data.invoiceNumber);
|
|
148
|
+
}
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
async deleteInvoice(db, id, runtime = {}) {
|
|
153
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
154
|
+
if (actionLedgerContext) {
|
|
155
|
+
return db.transaction(async (tx) => {
|
|
156
|
+
const [existing] = await tx.select().from(invoices).where(eq(invoices.id, id)).limit(1);
|
|
157
|
+
if (!existing) {
|
|
158
|
+
return { status: "not_found" };
|
|
159
|
+
}
|
|
160
|
+
if (existing.status !== "draft") {
|
|
161
|
+
return { status: "not_draft" };
|
|
162
|
+
}
|
|
163
|
+
await tx.delete(invoices).where(eq(invoices.id, id));
|
|
164
|
+
await appendActionLedgerMutation(tx, buildInvoiceDeleteActionLedgerInput(actionLedgerContext, { invoice: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
165
|
+
return { status: "deleted" };
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const [existing] = await db
|
|
169
|
+
.select({ id: invoices.id, status: invoices.status })
|
|
170
|
+
.from(invoices)
|
|
171
|
+
.where(eq(invoices.id, id))
|
|
172
|
+
.limit(1);
|
|
173
|
+
if (!existing) {
|
|
174
|
+
return { status: "not_found" };
|
|
175
|
+
}
|
|
176
|
+
if (existing.status !== "draft") {
|
|
177
|
+
return { status: "not_draft" };
|
|
178
|
+
}
|
|
179
|
+
await db.delete(invoices).where(eq(invoices.id, id));
|
|
180
|
+
return { status: "deleted" };
|
|
181
|
+
},
|
|
182
|
+
async voidInvoice(db, id, input = {}, runtime = {}) {
|
|
183
|
+
const reason = input.reason?.trim() || null;
|
|
184
|
+
const voidedAt = new Date();
|
|
185
|
+
const result = await db.transaction(async (tx) => {
|
|
186
|
+
const [existing] = await tx.select().from(invoices).where(eq(invoices.id, id)).limit(1);
|
|
187
|
+
if (!existing) {
|
|
188
|
+
return { status: "not_found" };
|
|
189
|
+
}
|
|
190
|
+
if (existing.status === "void") {
|
|
191
|
+
return { status: "already_void", invoice: existing };
|
|
192
|
+
}
|
|
193
|
+
if (existing.status === "draft") {
|
|
194
|
+
return { status: "draft", invoice: existing };
|
|
195
|
+
}
|
|
196
|
+
const voidableStatuses = new Set([
|
|
197
|
+
"pending_external_allocation",
|
|
198
|
+
"issued",
|
|
199
|
+
"partially_paid",
|
|
200
|
+
"overdue",
|
|
201
|
+
]);
|
|
202
|
+
if (!voidableStatuses.has(existing.status)) {
|
|
203
|
+
return { status: "invalid_status", invoice: existing };
|
|
204
|
+
}
|
|
205
|
+
const [payment] = await tx
|
|
206
|
+
.select({ id: payments.id })
|
|
207
|
+
.from(payments)
|
|
208
|
+
.where(eq(payments.invoiceId, id))
|
|
209
|
+
.limit(1);
|
|
210
|
+
if (payment) {
|
|
211
|
+
return { status: "has_payments", invoice: existing };
|
|
212
|
+
}
|
|
213
|
+
const [creditNote] = await tx
|
|
214
|
+
.select({ id: creditNotes.id })
|
|
215
|
+
.from(creditNotes)
|
|
216
|
+
.where(eq(creditNotes.invoiceId, id))
|
|
217
|
+
.limit(1);
|
|
218
|
+
if (creditNote) {
|
|
219
|
+
return { status: "has_credit_notes", invoice: existing };
|
|
220
|
+
}
|
|
221
|
+
const changes = {
|
|
222
|
+
status: "void",
|
|
223
|
+
voidedAt,
|
|
224
|
+
voidReason: reason,
|
|
225
|
+
balanceDueCents: 0,
|
|
226
|
+
baseBalanceDueCents: existing.baseBalanceDueCents == null ? null : 0,
|
|
227
|
+
updatedAt: voidedAt,
|
|
228
|
+
};
|
|
229
|
+
const actionLedgerChanges = {
|
|
230
|
+
status: "void",
|
|
231
|
+
balanceDueCents: changes.balanceDueCents,
|
|
232
|
+
baseBalanceDueCents: changes.baseBalanceDueCents,
|
|
233
|
+
};
|
|
234
|
+
const [invoice] = await tx
|
|
235
|
+
.update(invoices)
|
|
236
|
+
.set(changes)
|
|
237
|
+
.where(eq(invoices.id, id))
|
|
238
|
+
.returning();
|
|
239
|
+
if (!invoice) {
|
|
240
|
+
return { status: "not_found" };
|
|
241
|
+
}
|
|
242
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
243
|
+
if (actionLedgerContext) {
|
|
244
|
+
await appendActionLedgerMutation(tx, buildInvoiceUpdateActionLedgerInput(actionLedgerContext, { invoice, changes: actionLedgerChanges }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
245
|
+
}
|
|
246
|
+
return { status: "voided", invoice };
|
|
247
|
+
});
|
|
248
|
+
if (result.status === "voided" && runtime.eventBus) {
|
|
249
|
+
const [smartbillRef] = await db
|
|
250
|
+
.select()
|
|
251
|
+
.from(invoiceExternalRefs)
|
|
252
|
+
.where(and(eq(invoiceExternalRefs.invoiceId, result.invoice.id), eq(invoiceExternalRefs.provider, "smartbill")))
|
|
253
|
+
.orderBy(desc(invoiceExternalRefs.createdAt))
|
|
254
|
+
.limit(1);
|
|
255
|
+
const [externalRef] = smartbillRef
|
|
256
|
+
? [smartbillRef]
|
|
257
|
+
: await db
|
|
258
|
+
.select()
|
|
259
|
+
.from(invoiceExternalRefs)
|
|
260
|
+
.where(eq(invoiceExternalRefs.invoiceId, result.invoice.id))
|
|
261
|
+
.orderBy(desc(invoiceExternalRefs.createdAt))
|
|
262
|
+
.limit(1);
|
|
263
|
+
const [series] = result.invoice.seriesId
|
|
264
|
+
? await db
|
|
265
|
+
.select({ name: invoiceNumberSeries.name })
|
|
266
|
+
.from(invoiceNumberSeries)
|
|
267
|
+
.where(eq(invoiceNumberSeries.id, result.invoice.seriesId))
|
|
268
|
+
.limit(1)
|
|
269
|
+
: [];
|
|
270
|
+
const event = {
|
|
271
|
+
invoiceId: result.invoice.id,
|
|
272
|
+
invoiceNumber: result.invoice.invoiceNumber,
|
|
273
|
+
invoiceType: result.invoice.invoiceType,
|
|
274
|
+
bookingId: result.invoice.bookingId,
|
|
275
|
+
totalCents: result.invoice.totalCents,
|
|
276
|
+
currency: result.invoice.currency,
|
|
277
|
+
reason,
|
|
278
|
+
voidedAt: result.invoice.voidedAt?.toISOString() ?? voidedAt.toISOString(),
|
|
279
|
+
externalProvider: externalRef?.provider ?? null,
|
|
280
|
+
externalNumber: externalRef?.externalNumber ?? null,
|
|
281
|
+
externalSeriesName: readStringMetadata(externalRef?.metadata, "seriesName") ??
|
|
282
|
+
readStringMetadata(externalRef?.metadata, "series") ??
|
|
283
|
+
series?.name ??
|
|
284
|
+
null,
|
|
285
|
+
};
|
|
286
|
+
await runtime.eventBus.emit("invoice.voided", event);
|
|
287
|
+
}
|
|
288
|
+
return result;
|
|
289
|
+
},
|
|
290
|
+
};
|