mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 12:58:13 +00:00
fix: premature payable invoice
This commit is contained in:
@@ -826,8 +826,15 @@ export class BillingService {
|
||||
* Expire a source's currently-open invoice (its pay window closed before
|
||||
* settlement), then emit `${source}.invoice.expired`. Resolves the open invoice
|
||||
* and transitions it to EXPIRED — a terminal, non-payable status (kept out of
|
||||
* `OPEN_STATUSES`). No-op (returns null) when the source has no open invoice
|
||||
* (already paid/cancelled/expired).
|
||||
* `OPEN_STATUSES`). No-op (returns null) when the source has no invoice left to
|
||||
* retire (already paid/cancelled/expired).
|
||||
*
|
||||
* DRAFT invoices are matched too, even though they were never issued: this is
|
||||
* also the "retire the invoice this source no longer needs" path (a cancelled
|
||||
* booking, or a full-amount invoice superseded by a partial-offer one). Skipping
|
||||
* drafts would leave the stale one behind for `findPayable` to hand back — the
|
||||
* superseding invoice would then never be minted, and a cancelled booking would
|
||||
* keep a draft that a later `issuePayable` could still make payable.
|
||||
*
|
||||
* Pass the caller's transaction `manager` (e.g. the booking pay-window expiry in
|
||||
* the batch engine) to enlist in its DB transaction.
|
||||
@@ -850,7 +857,7 @@ export class BillingService {
|
||||
where: {
|
||||
source,
|
||||
sourceId,
|
||||
status: In(OPEN_STATUSES),
|
||||
status: In([Freight.InvoiceStatus.Draft, ...OPEN_STATUSES]),
|
||||
...(type ? { type } : {}),
|
||||
},
|
||||
order: { issuedAt: "DESC" },
|
||||
@@ -867,30 +874,58 @@ export class BillingService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync a source's open invoice `dueAt` to its real pay-window deadline. The
|
||||
* booking invoice is generated before the pay window opens (at booking
|
||||
* creation/approval), so its printed due date is refreshed when the batch engine
|
||||
* sets `paymentDeadline`. No-op when the source has no open invoice.
|
||||
* Issue a source's invoice and stamp its real pay-window deadline — the single
|
||||
* transition that makes a source payable.
|
||||
*
|
||||
* A source's invoice is minted DRAFT, before any pay window exists (e.g. a
|
||||
* booking invoice is generated at creation / operation-accept, long before the
|
||||
* batch engine reserves a slot). DRAFT is deliberately outside `OPEN_STATUSES`,
|
||||
* so such an invoice is not settleable and the portal renders no pay button.
|
||||
* The domain calls this at the moment the pay window actually opens (booking →
|
||||
* `reserve`, which sets SELECTED_FOR_BATCH + `paymentDeadline`), which issues
|
||||
* the draft (→ PENDING, stamping `issuedAt`) and prints the real `dueAt`.
|
||||
*
|
||||
* Idempotent: an already-issued open invoice only has its `dueAt` refreshed, so
|
||||
* a re-reserve never re-issues. No-op (returns null) when the source has no
|
||||
* draft-or-open invoice (already paid/cancelled/expired).
|
||||
*/
|
||||
async syncPayableDueDate(
|
||||
async issuePayable(
|
||||
source: Freight.InvoiceSource,
|
||||
sourceId: string,
|
||||
dueAt: Date,
|
||||
type?: string,
|
||||
manager?: EntityManager,
|
||||
): Promise<void> {
|
||||
): Promise<Invoice | null> {
|
||||
const mg = manager ?? this.dataSource.manager;
|
||||
const invoice = await mg.findOne(Invoice, {
|
||||
where: {
|
||||
source,
|
||||
sourceId,
|
||||
status: In(OPEN_STATUSES),
|
||||
status: In([Freight.InvoiceStatus.Draft, ...OPEN_STATUSES]),
|
||||
...(type ? { type } : {}),
|
||||
},
|
||||
order: { issuedAt: "DESC" },
|
||||
});
|
||||
if (!invoice) return;
|
||||
await mg.update(Invoice, { id: invoice.id }, { dueAt });
|
||||
if (!invoice) return null;
|
||||
|
||||
const issuing = invoice.status === Freight.InvoiceStatus.Draft;
|
||||
const patch = {
|
||||
dueAt,
|
||||
...(issuing
|
||||
? {
|
||||
status: Freight.InvoiceStatus.Pending,
|
||||
issuedAt: invoice.issuedAt ?? new Date(),
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
await mg.update(Invoice, { id: invoice.id }, patch);
|
||||
|
||||
if (issuing) {
|
||||
this.logger.log(
|
||||
`Issued invoice ${invoice.invoiceNumber} (${invoice.id}) for ${source}:${sourceId} — payable until ${dueAt.toISOString()}`,
|
||||
);
|
||||
}
|
||||
return { ...invoice, ...patch } as Invoice;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user