mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-09 00:24:09 +00:00
feat(sales): fresh support services and provider
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Application;
|
||||
|
||||
use App\Domains\Sales\Models\Invoice;
|
||||
|
||||
class InvoiceBalanceService
|
||||
{
|
||||
public function allocatedTotal(Invoice $invoice): int
|
||||
{
|
||||
return (int) $invoice->allocations()->sum('amount');
|
||||
}
|
||||
|
||||
public function creditedTotal(Invoice $invoice): int
|
||||
{
|
||||
return -(int) $invoice->creditNotes()->sum('total');
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate an invoice exclusively from durable credits and payment
|
||||
* allocations. Callers that change allocations must hold the invoice lock.
|
||||
*/
|
||||
public function recalculate(Invoice $invoice): void
|
||||
{
|
||||
$allocated = $this->allocatedTotal($invoice);
|
||||
$credited = $this->creditedTotal($invoice);
|
||||
$due = max(0, (int) $invoice->total - $allocated - $credited);
|
||||
|
||||
$invoice->due_amount = $due;
|
||||
$invoice->base_due_amount = (int) round($due * $invoice->exchange_rate);
|
||||
|
||||
if ($due === 0) {
|
||||
// Nothing left outstanding, so the document closes out on both
|
||||
// axes at once.
|
||||
$invoice->forceFill([
|
||||
'status' => Invoice::STATUS_COMPLETED,
|
||||
'paid_status' => Invoice::STATUS_PAID,
|
||||
]);
|
||||
$invoice->overdue = false;
|
||||
} else {
|
||||
$invoice->status = $invoice->getPreviousStatus();
|
||||
$invoice->paid_status = $allocated > 0
|
||||
? Invoice::STATUS_PARTIALLY_PAID
|
||||
: Invoice::STATUS_UNPAID;
|
||||
}
|
||||
|
||||
$invoice->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user