update api and add more features

This commit is contained in:
root
2026-06-04 02:24:41 -04:00
parent fa6c9519a0
commit 4e33882ac7
131 changed files with 34596 additions and 340 deletions
@@ -0,0 +1,238 @@
<?php
namespace Tests\Unit\Services\Billing;
use App\Services\Billing\BalanceCalculationService;
use App\Services\Billing\BillingTotalsService;
use App\Services\Fees\FeeConfigService;
use App\Services\Fees\FeeGradeService;
use App\Services\Fees\FeeStudentFeeService;
use App\Services\Fees\TuitionCalculationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class BalanceCalculationServiceTest extends TestCase
{
use RefreshDatabase;
public function test_calculate_family_account_applies_balance_formula(): void
{
$this->seedFeeConfig();
$this->seedClassSection(101, '2');
$this->seedClassSection(102, '5');
$parentId = 10;
$invoiceId = $this->seedInvoice($parentId, 1000.00);
DB::table('payments')->insert([
'parent_id' => $parentId,
'invoice_id' => $invoiceId,
'paid_amount' => 500.00,
'total_amount' => 500.00,
'balance' => 0.00,
'number_of_installments' => 1,
'payment_date' => '2025-01-10',
'school_year' => '2025-2026',
'status' => 'Paid',
]);
DB::table('discount_usages')->insert([
'voucher_id' => 1,
'invoice_id' => $invoiceId,
'parent_id' => $parentId,
'discount_amount' => 80.00,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
DB::table('additional_charges')->insert([
'parent_id' => $parentId,
'invoice_id' => $invoiceId,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Books',
'amount' => 100.00,
'status' => 'applied',
]);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Field Trip',
'amount' => 25.00,
'expiration_date' => '2025-06-01',
]);
DB::table('event_charges')->insert([
'event_id' => 1,
'parent_id' => $parentId,
'student_id' => 2,
'charged' => 50.00,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
DB::table('refunds')->insert([
'parent_id' => $parentId,
'school_year' => '2025-2026',
'invoice_id' => $invoiceId,
'refund_amount' => 30.00,
'refund_paid_amount' => 30.00,
'status' => 'Paid',
'semester' => 'Fall',
]);
$service = $this->makeService();
$result = $service->calculateFamilyAccount([
[
'student_id' => 1,
'class_section_id' => 101,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
[
'student_id' => 2,
'class_section_id' => 102,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
], $parentId);
$this->assertSame($parentId, $result['parent_id']);
$this->assertSame(550.0, $result['charges']['tuition']);
$this->assertSame(100.0, $result['charges']['extra_charges']);
$this->assertSame(50.0, $result['charges']['event_charges']);
$this->assertSame(700.0, $result['charges']['total']);
$this->assertSame(500.0, $result['payments']);
$this->assertSame(80.0, $result['credits']['total']);
$this->assertSame(30.0, $result['refunds_applied']);
$this->assertSame(90.0, $result['remaining_balance']);
$this->assertSame(0.0, $result['account_credit']);
$this->assertCount(2, $result['students']);
}
public function test_overpayment_records_account_credit(): void
{
$this->seedFeeConfig();
$this->seedClassSection(201, '3');
$parentId = 20;
$invoiceId = $this->seedInvoice($parentId, 500.00);
DB::table('payments')->insert([
'parent_id' => $parentId,
'invoice_id' => $invoiceId,
'paid_amount' => 500.00,
'total_amount' => 500.00,
'balance' => 0.00,
'number_of_installments' => 1,
'payment_date' => '2025-01-10',
'school_year' => '2025-2026',
'status' => 'Paid',
]);
$service = $this->makeService();
$result = $service->calculateFamilyAccount([
[
'student_id' => 5,
'class_section_id' => 201,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
], $parentId);
$this->assertSame(350.0, $result['charges']['tuition']);
$this->assertSame(0.0, $result['remaining_balance']);
$this->assertSame(150.0, $result['account_credit']);
}
public function test_student_rows_include_event_charges(): void
{
$this->seedFeeConfig();
$this->seedClassSection(301, '4');
$parentId = 30;
DB::table('events')->insert([
'id' => 2,
'event_name' => 'Camp',
'amount' => 40.00,
'expiration_date' => '2025-06-01',
]);
DB::table('event_charges')->insert([
'event_id' => 2,
'parent_id' => $parentId,
'student_id' => 7,
'charged' => 40.00,
'school_year' => '2025-2026',
]);
$service = $this->makeService();
$result = $service->calculateFamilyAccount([
[
'student_id' => 7,
'firstname' => 'Sam',
'lastname' => 'Lee',
'class_section_id' => 301,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
], $parentId);
$student = $result['students'][0];
$this->assertSame(7, $student['student_id']);
$this->assertSame(350.0, $student['tuition']);
$this->assertSame(40.0, $student['event_charges']);
$this->assertSame(390.0, $student['total_charges']);
}
private function makeService(): BalanceCalculationService
{
$config = new FeeConfigService();
$tuition = new TuitionCalculationService(
$config,
new FeeStudentFeeService(new FeeGradeService(), $config)
);
return new BalanceCalculationService($config, $tuition, new BillingTotalsService());
}
private function seedFeeConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'first_student_fee', 'config_value' => '350'],
['config_key' => 'second_student_fee', 'config_value' => '200'],
['config_key' => 'youth_fee', 'config_value' => '180'],
]);
}
private function seedClassSection(int $sectionId, string $gradeName): void
{
DB::table('classSection')->insert([
'class_id' => $sectionId,
'class_section_id' => $sectionId,
'class_section_name' => $gradeName,
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
}
private function seedInvoice(int $parentId, float $total): int
{
return (int) DB::table('invoices')->insertGetId([
'parent_id' => $parentId,
'invoice_number' => 'INV-' . $parentId,
'total_amount' => $total,
'balance' => 0.00,
'paid_amount' => 0.00,
'issue_date' => '2025-01-01',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
}
}
@@ -17,6 +17,7 @@ class BillingTotalsServiceTest extends TestCase
'id' => 1,
'event_name' => 'Field Trip',
'amount' => 25.00,
'expiration_date' => '2025-06-01',
]);
DB::table('event_charges')->insert([
@@ -58,4 +59,78 @@ class BillingTotalsServiceTest extends TestCase
$this->assertSame(25.0, $eventSubtotal);
$this->assertSame(10.0, $additionalSubtotal);
}
public function test_additional_subtotal_by_parent_sums_applied_charges(): void
{
DB::table('additional_charges')->insert([
[
'parent_id' => 10,
'invoice_id' => 99,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Uniform',
'amount' => 40.00,
'status' => 'applied',
],
[
'parent_id' => 10,
'invoice_id' => 99,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'deduct',
'title' => 'Credit',
'amount' => 10.00,
'status' => 'applied',
],
[
'parent_id' => 10,
'invoice_id' => 99,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Pending',
'amount' => 99.00,
'status' => 'pending',
],
]);
$service = new BillingTotalsService();
$this->assertSame(30.0, $service->additionalSubtotalByParent(10, '2025-2026'));
}
public function test_event_subtotals_by_student_groups_amounts(): void
{
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Trip',
'amount' => 20.00,
'expiration_date' => '2025-06-01',
]);
DB::table('event_charges')->insert([
[
'event_id' => 1,
'parent_id' => 5,
'student_id' => 1,
'charged' => 20.00,
'school_year' => '2025-2026',
],
[
'event_id' => 1,
'parent_id' => 5,
'student_id' => 2,
'charged' => 15.00,
'school_year' => '2025-2026',
],
]);
$service = new BillingTotalsService();
$byStudent = $service->eventSubtotalsByStudent(5, '2025-2026');
$this->assertSame(20.0, $byStudent[1]);
$this->assertSame(15.0, $byStudent[2]);
$this->assertSame(35.0, $service->eventSubtotal(5, '2025-2026'));
}
}
@@ -0,0 +1,228 @@
<?php
namespace Tests\Unit\Services\Billing;
use App\Models\AdditionalCharge;
use App\Models\EventCharges;
use App\Services\Billing\ChargeService;
use App\Services\ExtraCharges\ExtraChargesChargeService;
use App\Services\ExtraCharges\ExtraChargesContextService;
use App\Services\ExtraCharges\ExtraChargesInvoiceService;
use App\Services\Invoices\InvoiceGenerationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;
class ChargeServiceTest extends TestCase
{
use RefreshDatabase;
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_create_extra_charge_blocks_duplicate(): void
{
$this->seedConfig();
DB::table('additional_charges')->insert([
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Books',
'amount' => 25.00,
'status' => 'pending',
]);
$service = $this->makeService();
$result = $service->createExtraCharge([
'parent_id' => 10,
'title' => 'Books',
'amount' => 25,
'charge_type' => 'add',
]);
$this->assertFalse($result['ok']);
$this->assertSame('duplicate_charge', $result['error']);
}
public function test_create_event_charge_blocks_duplicate(): void
{
$this->seedConfig();
DB::table('event_charges')->insert([
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Field Trip',
'participation' => 'yes',
'charged' => 25.00,
'amount' => 25.00,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$service = $this->makeService();
$result = $service->createEventCharge([
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Field Trip',
'amount' => 25,
]);
$this->assertFalse($result['ok']);
$this->assertSame('duplicate_charge', $result['error']);
}
public function test_create_event_charge_succeeds(): void
{
$this->seedConfig();
$service = $this->makeService();
$result = $service->createEventCharge([
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Camp',
'amount' => 40,
'created_by' => 1,
]);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('event_charges', [
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Camp',
'participation' => 'yes',
]);
}
public function test_cancel_event_charge_zeros_charge_and_creates_credit(): void
{
$this->seedConfig();
$chargeId = DB::table('event_charges')->insertGetId([
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Trip',
'participation' => 'yes',
'charged' => 30.00,
'amount' => 30.00,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$service = $this->makeService();
$result = $service->cancelEventCharge((int) $chargeId, true);
$this->assertTrue($result['ok']);
$this->assertSame('canceled', $result['cancellation_status']);
$charge = EventCharges::query()->find($chargeId);
$this->assertSame('no', $charge->participation);
$this->assertSame(0.0, (float) $charge->charged);
$this->assertDatabaseHas('additional_charges', [
'parent_id' => 10,
'charge_type' => 'deduct',
'status' => 'pending',
]);
}
public function test_apply_extra_charge_marks_applied(): void
{
$this->seedConfig();
$invoiceId = DB::table('invoices')->insertGetId([
'parent_id' => 10,
'invoice_number' => 'INV-10',
'total_amount' => 100.00,
'balance' => 100.00,
'paid_amount' => 0.00,
'issue_date' => '2025-01-01',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$chargeId = DB::table('additional_charges')->insertGetId([
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Lab fee',
'amount' => 15.00,
'status' => 'pending',
]);
$service = $this->makeService();
$result = $service->applyExtraCharge((int) $chargeId, (int) $invoiceId);
$this->assertTrue($result['ok']);
$charge = AdditionalCharge::query()->find($chargeId);
$this->assertSame('applied', $charge->status);
$this->assertSame($invoiceId, (int) $charge->invoice_id);
}
public function test_list_for_parent_returns_both_charge_types(): void
{
$this->seedConfig();
DB::table('additional_charges')->insert([
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Uniform',
'amount' => 20.00,
'status' => 'pending',
]);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Trip',
'amount' => 25.00,
'expiration_date' => '2025-06-01',
]);
DB::table('event_charges')->insert([
'event_id' => 1,
'parent_id' => 10,
'student_id' => 1,
'participation' => 'yes',
'charged' => 25.00,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$service = $this->makeService();
$list = $service->listForParent(10, '2025-2026');
$this->assertCount(1, $list['extra_charges']);
$this->assertCount(1, $list['event_charges']);
$this->assertSame('extra', $list['extra_charges'][0]['charge_type']);
$this->assertSame('event', $list['event_charges'][0]['charge_type']);
}
private function makeService(): ChargeService
{
$invoiceGen = Mockery::mock(InvoiceGenerationService::class);
$invoiceGen->shouldReceive('generateInvoice')->andReturn(['ok' => true]);
return new ChargeService(
app(ExtraChargesChargeService::class),
app(ExtraChargesInvoiceService::class),
app(ExtraChargesContextService::class),
$invoiceGen
);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
}