fix invoice and enrollment fees
This commit is contained in:
@@ -416,14 +416,17 @@ final class WithdrawalFinancialService
|
||||
if ($invoice !== null) {
|
||||
$this->lockInvoice((int) $invoice['id']);
|
||||
$ledger = $this->invoiceLedger->calculateInvoice((int) $invoice['id']);
|
||||
$existingTargetAdjustment = $this->db->table('invoice_lines il')
|
||||
->selectSum('il.line_amount_cents', 'total')
|
||||
->select("COALESCE(SUM(CASE WHEN il.discount_eligible = 1 THEN il.line_amount_cents ELSE 0 END), 0) AS eligible_total", false)
|
||||
->join('withdrawal_financial_calculations wfc', 'wfc.id = il.source_id AND il.source_type = \'withdrawal_calculation\'', 'inner')
|
||||
->where('il.invoice_id', (int) $invoice['id'])
|
||||
->where('wfc.enrollment_id', (int) $enrollment['id'])
|
||||
->where('il.voided_at', null)
|
||||
->get()->getRowArray();
|
||||
$existingTargetAdjustment = ['total' => 0, 'eligible_total' => 0];
|
||||
if ($this->invoiceLinesAvailable()) {
|
||||
$existingTargetAdjustment = $this->db->table('invoice_lines il')
|
||||
->selectSum('il.line_amount_cents', 'total')
|
||||
->select("COALESCE(SUM(CASE WHEN il.discount_eligible = 1 THEN il.line_amount_cents ELSE 0 END), 0) AS eligible_total", false)
|
||||
->join('withdrawal_financial_calculations wfc', 'wfc.id = il.source_id AND il.source_type = \'withdrawal_calculation\'', 'inner')
|
||||
->where('il.invoice_id', (int) $invoice['id'])
|
||||
->where('wfc.enrollment_id', (int) $enrollment['id'])
|
||||
->where('il.voided_at', null)
|
||||
->get()->getRowArray();
|
||||
}
|
||||
$baseGrossCharge = (int) ($ledger['gross_charge_cents'] ?? 0) - (int) ($existingTargetAdjustment['total'] ?? 0);
|
||||
$baseDiscountEligible = max(0, (int) ($ledger['discount_eligible_base_cents'] ?? 0) - (int) ($existingTargetAdjustment['eligible_total'] ?? 0));
|
||||
$requestedDiscount = max(0, (int) ($ledger['requested_discount_cents'] ?? 0));
|
||||
@@ -596,6 +599,10 @@ final class WithdrawalFinancialService
|
||||
/** @return list<string> */
|
||||
private function invoiceReconstructionBlockers(int $invoiceId, int $expectedFamilyTuition, int $expectedStudentCount, bool $legacyConfirmed): array
|
||||
{
|
||||
if (! $this->invoiceLinesAvailable()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$lines = $this->db->table('invoice_lines')
|
||||
->select('line_type, source_type, line_amount_cents')
|
||||
->where('invoice_id', $invoiceId)
|
||||
@@ -635,6 +642,10 @@ final class WithdrawalFinancialService
|
||||
/** @return list<array<string,mixed>> */
|
||||
private function invoiceTuitionStudentDetails(int $invoiceId): array
|
||||
{
|
||||
if (! $this->invoiceLinesAvailable()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$line = $this->db->table('invoice_lines')
|
||||
->select('metadata_json')
|
||||
->where('invoice_id', $invoiceId)
|
||||
@@ -654,6 +665,10 @@ final class WithdrawalFinancialService
|
||||
|
||||
private function appendInvoiceLines(array $invoice, array $calculation, array $books): void
|
||||
{
|
||||
if (! $this->invoiceLinesAvailable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$invoiceId = (int) $invoice['id'];
|
||||
$calculationId = (int) $calculation['id'];
|
||||
$enrollmentId = (int) $calculation['enrollment_id'];
|
||||
@@ -790,11 +805,13 @@ final class WithdrawalFinancialService
|
||||
private function supersedePostedCalculation(int $oldId, int $newId): void
|
||||
{
|
||||
$now = utc_now();
|
||||
$this->db->table('invoice_lines')->where('source_type', 'withdrawal_calculation')->where('source_id', $oldId)->where('voided_at', null)->update([
|
||||
'active_source_key' => null,
|
||||
'voided_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
if ($this->invoiceLinesAvailable()) {
|
||||
$this->db->table('invoice_lines')->where('source_type', 'withdrawal_calculation')->where('source_id', $oldId)->where('voided_at', null)->update([
|
||||
'active_source_key' => null,
|
||||
'voided_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
$this->db->table('withdrawal_financial_calculations')->where('id', $oldId)->update([
|
||||
'status' => 'superseded',
|
||||
'active_posted_key' => null,
|
||||
@@ -927,10 +944,15 @@ final class WithdrawalFinancialService
|
||||
|
||||
private function assertTables(): void
|
||||
{
|
||||
foreach (['withdrawal_financial_calculations', 'student_book_issues', 'invoice_lines', 'refunds', 'school_years'] as $table) {
|
||||
foreach (['withdrawal_financial_calculations', 'student_book_issues', 'refunds', 'school_years'] as $table) {
|
||||
if (! $this->db->tableExists($table)) {
|
||||
throw new RuntimeException('Required table is missing: ' . $table . '. Run migrations first.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function invoiceLinesAvailable(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user