Skip to content

Commit a91b6b2

Browse files
committed
feat: add invoice revision handling for repricing
1 parent 776398b commit a91b6b2

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

addon/components/order-invoice.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ export default class OrderInvoiceComponent extends Component {
9292
const results = yield this.store.query('ledger-invoice', {
9393
order_uuid: order.id,
9494
with: 'items',
95-
limit: 1,
95+
sort: '-created_at',
96+
limit: 10,
9697
});
9798

98-
this.invoice = results.firstObject ?? null;
99+
const invoices = results.toArray();
100+
this.invoice = invoices.find((invoice) => !['void', 'cancelled'].includes(invoice.status)) ?? invoices[0] ?? null;
99101
} catch {
100102
this.invoice = null;
101103
}

server/src/Observers/PurchaseRateObserver.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Fleetbase\Ledger\Observers;
44

55
use Fleetbase\Ledger\Services\InvoiceService;
6+
use Fleetbase\Ledger\Models\Invoice;
67
use Illuminate\Support\Facades\Log;
78

89
/**
@@ -56,10 +57,7 @@ public function created($purchaseRate): void
5657
return;
5758
}
5859

59-
// Skip if an invoice already exists for this order (idempotency).
60-
if ($this->invoiceAlreadyExists($order->uuid)) {
61-
return;
62-
}
60+
$this->supersedeDraftInvoices($order->uuid);
6361

6462
// Resolve currency: prefer the service quote's currency, fall back to
6563
// the company's country-derived currency, then USD.
@@ -124,10 +122,14 @@ private function resolveOrder($purchaseRate): ?object
124122
}
125123

126124
/**
127-
* Check whether a Ledger invoice already exists for the given order UUID.
125+
* Void prior draft invoices so the newly created invoice becomes the active revision.
128126
*/
129-
private function invoiceAlreadyExists(string $orderUuid): bool
127+
private function supersedeDraftInvoices(string $orderUuid): void
130128
{
131-
return \Fleetbase\Ledger\Models\Invoice::where('order_uuid', $orderUuid)->exists();
129+
Invoice::where('order_uuid', $orderUuid)
130+
->where('status', 'draft')
131+
->update([
132+
'status' => 'void',
133+
]);
132134
}
133135
}

0 commit comments

Comments
 (0)