fix invoice and enrollment fees
This commit is contained in:
@@ -2,25 +2,22 @@
|
||||
|
||||
namespace App\Libraries;
|
||||
|
||||
use App\Models\InvoiceLineModel;
|
||||
use App\Models\InvoiceModel;
|
||||
|
||||
class InvoiceIssuanceService
|
||||
{
|
||||
private \CodeIgniter\Database\BaseConnection $db;
|
||||
private InvoiceModel $invoiceModel;
|
||||
private InvoiceLineModel $invoiceLineModel;
|
||||
private InvoiceLedgerService $invoiceLedgerService;
|
||||
|
||||
public function __construct(
|
||||
?\CodeIgniter\Database\BaseConnection $db = null,
|
||||
?InvoiceModel $invoiceModel = null,
|
||||
?InvoiceLineModel $invoiceLineModel = null,
|
||||
$invoiceLineModel = null,
|
||||
?InvoiceLedgerService $invoiceLedgerService = null
|
||||
) {
|
||||
$this->db = $db ?? db_connect();
|
||||
$this->invoiceModel = $invoiceModel ?? new InvoiceModel();
|
||||
$this->invoiceLineModel = $invoiceLineModel ?? new InvoiceLineModel();
|
||||
$this->invoiceLedgerService = $invoiceLedgerService ?? new InvoiceLedgerService();
|
||||
}
|
||||
|
||||
@@ -45,29 +42,6 @@ class InvoiceIssuanceService
|
||||
|
||||
$this->db->query('SELECT id FROM invoices WHERE id = ? FOR UPDATE', [(int)$invoiceId]);
|
||||
|
||||
$inserted = $this->invoiceLedgerService->issueInitialInvoiceLines(
|
||||
(int)$invoiceId,
|
||||
$command->tuitionAmount,
|
||||
$command->eventAmount,
|
||||
$command->metadata
|
||||
);
|
||||
if ($inserted <= 0) {
|
||||
throw new FinancialPersistenceException('INVOICE_ISSUE_NO_LINES');
|
||||
}
|
||||
|
||||
$lineTotals = $this->db->table('invoice_lines')
|
||||
->select('COUNT(*) AS line_count, COALESCE(SUM(line_amount_cents),0) AS total_cents')
|
||||
->where('invoice_id', (int)$invoiceId)
|
||||
->where('voided_at IS NULL', null, false)
|
||||
->get()
|
||||
->getRowArray();
|
||||
if ((int)($lineTotals['line_count'] ?? 0) !== $inserted) {
|
||||
throw new FinancialPersistenceException('INVOICE_ISSUE_LINE_COUNT_MISMATCH');
|
||||
}
|
||||
if ((int)($lineTotals['total_cents'] ?? 0) === 0) {
|
||||
throw new FinancialPersistenceException('INVOICE_ISSUE_ZERO_LINE_TOTAL');
|
||||
}
|
||||
|
||||
$this->requireWrite($this->invoiceModel->update((int)$invoiceId, [
|
||||
'status' => FinancialStatus::INVOICE_ISSUED,
|
||||
'updated_at' => utc_now(),
|
||||
|
||||
Reference in New Issue
Block a user