update api and add more features
This commit is contained in:
@@ -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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -16,54 +16,12 @@ class FeeRefundCalculatorServiceTest extends TestCase
|
||||
|
||||
public function test_calculate_refund_caps_at_total_paid(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
|
||||
['config_key' => 'weeks_study', 'config_value' => '8'],
|
||||
['config_key' => 'last_school_day', 'config_value' => '2025-06-01'],
|
||||
['config_key' => 'first_student_fee', 'config_value' => '350'],
|
||||
['config_key' => 'second_student_fee', 'config_value' => '200'],
|
||||
['config_key' => 'youth_fee', 'config_value' => '180'],
|
||||
]);
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(101, '3');
|
||||
|
||||
DB::table('classSection')->insert([
|
||||
'id' => 1,
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '3',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
]);
|
||||
$this->seedInvoiceAndPayment(99, 100.00);
|
||||
|
||||
DB::table('invoices')->insert([
|
||||
'id' => 1,
|
||||
'parent_id' => 99,
|
||||
'invoice_number' => 'INV-1',
|
||||
'total_amount' => 100.00,
|
||||
'balance' => 0.00,
|
||||
'paid_amount' => 100.00,
|
||||
'issue_date' => '2025-01-01',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
|
||||
DB::table('payments')->insert([
|
||||
'id' => 1,
|
||||
'parent_id' => 99,
|
||||
'invoice_id' => 1,
|
||||
'paid_amount' => 100.00,
|
||||
'total_amount' => 100.00,
|
||||
'balance' => 0.00,
|
||||
'number_of_installments' => 1,
|
||||
'payment_date' => '2025-01-10',
|
||||
'school_year' => '2025-2026',
|
||||
'status' => 'Paid',
|
||||
]);
|
||||
|
||||
$service = new FeeRefundCalculatorService(
|
||||
new FeeConfigService(),
|
||||
new FeeStudentFeeService(new FeeGradeService(), new FeeConfigService())
|
||||
);
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
@@ -76,5 +34,249 @@ class FeeRefundCalculatorServiceTest extends TestCase
|
||||
], 99);
|
||||
|
||||
$this->assertSame(100.0, $result['refund_amount']);
|
||||
$this->assertSame(1, $result['withdrawn_students']);
|
||||
$this->assertCount(1, $result['students']);
|
||||
$this->assertTrue($result['students'][0]['eligible']);
|
||||
$this->assertSame(350.0, $result['students'][0]['tuition_fee']);
|
||||
}
|
||||
|
||||
public function test_returns_zero_when_parent_has_no_payments(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(101, '3');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
'withdrawal_date' => '2025-01-15',
|
||||
],
|
||||
], 12345);
|
||||
|
||||
$this->assertSame(0.0, $result['refund_amount']);
|
||||
$this->assertSame(0, $result['withdrawn_students']);
|
||||
$this->assertSame([], $result['students']);
|
||||
}
|
||||
|
||||
public function test_returns_zero_when_no_withdrawn_students(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(101, '3');
|
||||
$this->seedInvoiceAndPayment(50, 500.00);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
], 50);
|
||||
|
||||
$this->assertSame(0.0, $result['refund_amount']);
|
||||
$this->assertSame(0, $result['withdrawn_students']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan section 8 regression: each withdrawn student must carry the
|
||||
* tuition fee that was assigned during fee calculation, otherwise the
|
||||
* refund loop silently produces $0 for any student whose id does not
|
||||
* round-trip through the lookup map.
|
||||
*/
|
||||
public function test_withdrawn_student_breakdown_carries_assigned_tuition_fee(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(201, '4');
|
||||
$this->seedClassSection(202, '5');
|
||||
$this->seedInvoiceAndPayment(77, 1000.00);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
'student_id' => 10,
|
||||
'class_section_id' => 201,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 11,
|
||||
'class_section_id' => 202,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
'withdrawal_date' => '2025-01-15',
|
||||
],
|
||||
], 77);
|
||||
|
||||
$this->assertCount(1, $result['students']);
|
||||
$withdrawn = $result['students'][0];
|
||||
$this->assertSame(11, $withdrawn['student_id']);
|
||||
// First regular = $350 (grade 4), additional = $200 (grade 5) -> the
|
||||
// withdrawn 5th-grader is the additional regular student and must
|
||||
// refund using $200, not $0 or a stale lookup value.
|
||||
$this->assertSame(200.0, $withdrawn['tuition_fee']);
|
||||
$this->assertSame('additional_regular', $withdrawn['fee_category']);
|
||||
$this->assertGreaterThan(0, $withdrawn['refund_amount']);
|
||||
$this->assertTrue($withdrawn['eligible']);
|
||||
}
|
||||
|
||||
public function test_multi_student_family_only_refunds_withdrawn_students(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(301, '2');
|
||||
$this->seedClassSection(302, '6');
|
||||
$this->seedClassSection(303, '10');
|
||||
$this->seedInvoiceAndPayment(88, 1000.00);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
'student_id' => 21,
|
||||
'class_section_id' => 301,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 22,
|
||||
'class_section_id' => 302,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
'withdrawal_date' => '2025-01-15',
|
||||
],
|
||||
[
|
||||
'student_id' => 23,
|
||||
'class_section_id' => 303,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
'withdrawal_date' => '2025-01-15',
|
||||
],
|
||||
], 88);
|
||||
|
||||
$this->assertCount(2, $result['students']);
|
||||
$this->assertSame(2, $result['withdrawn_students']);
|
||||
|
||||
$byStudent = [];
|
||||
foreach ($result['students'] as $row) {
|
||||
$byStudent[$row['student_id']] = $row;
|
||||
}
|
||||
|
||||
$this->assertSame('additional_regular', $byStudent[22]['fee_category']);
|
||||
$this->assertSame(200.0, $byStudent[22]['tuition_fee']);
|
||||
|
||||
$this->assertSame('youth', $byStudent[23]['fee_category']);
|
||||
$this->assertSame(180.0, $byStudent[23]['tuition_fee']);
|
||||
|
||||
$sumPerStudent = round(array_sum(array_column($result['students'], 'refund_amount')), 2);
|
||||
$this->assertSame($result['refund_amount'], $sumPerStudent);
|
||||
}
|
||||
|
||||
public function test_withdrawal_after_deadline_is_not_eligible(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(401, '3');
|
||||
$this->seedInvoiceAndPayment(33, 500.00);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'class_section_id' => 401,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
'withdrawal_date' => '2025-03-01',
|
||||
],
|
||||
], 33);
|
||||
|
||||
$this->assertSame(0.0, $result['refund_amount']);
|
||||
$this->assertCount(1, $result['students']);
|
||||
$this->assertFalse($result['students'][0]['eligible']);
|
||||
$this->assertSame('after_refund_deadline', $result['students'][0]['reason']);
|
||||
}
|
||||
|
||||
public function test_missing_withdrawal_date_is_skipped_with_reason(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(501, '3');
|
||||
$this->seedInvoiceAndPayment(44, 500.00);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculateRefund([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'class_section_id' => 501,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
], 44);
|
||||
|
||||
$this->assertSame(0.0, $result['refund_amount']);
|
||||
$this->assertSame('missing_withdrawal_date', $result['students'][0]['reason']);
|
||||
}
|
||||
|
||||
private function makeService(): FeeRefundCalculatorService
|
||||
{
|
||||
return new FeeRefundCalculatorService(
|
||||
new FeeConfigService(),
|
||||
new FeeStudentFeeService(new FeeGradeService(), new FeeConfigService())
|
||||
);
|
||||
}
|
||||
|
||||
private function seedFeeConfig(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
|
||||
['config_key' => 'weeks_study', 'config_value' => '8'],
|
||||
['config_key' => 'last_school_day', 'config_value' => '2025-06-01'],
|
||||
['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 seedInvoiceAndPayment(int $parentId, float $amount): void
|
||||
{
|
||||
$invoiceId = DB::table('invoices')->insertGetId([
|
||||
'parent_id' => $parentId,
|
||||
'invoice_number' => 'INV-' . $parentId,
|
||||
'total_amount' => $amount,
|
||||
'balance' => 0.00,
|
||||
'paid_amount' => $amount,
|
||||
'issue_date' => '2025-01-01',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
|
||||
DB::table('payments')->insert([
|
||||
'parent_id' => $parentId,
|
||||
'invoice_id' => $invoiceId,
|
||||
'paid_amount' => $amount,
|
||||
'total_amount' => $amount,
|
||||
'balance' => 0.00,
|
||||
'number_of_installments' => 1,
|
||||
'payment_date' => '2025-01-10',
|
||||
'school_year' => '2025-2026',
|
||||
'status' => 'Paid',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,32 +15,11 @@ class FeeStudentFeeServiceTest extends TestCase
|
||||
|
||||
public function test_total_tuition_fee_applies_tiers_and_youth_fee(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'first_student_fee', 'config_value' => '350'],
|
||||
['config_key' => 'second_student_fee', 'config_value' => '200'],
|
||||
['config_key' => 'youth_fee', 'config_value' => '180'],
|
||||
]);
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(101, '3');
|
||||
$this->seedClassSection(102, '10');
|
||||
|
||||
DB::table('classSection')->insert([
|
||||
[
|
||||
'id' => 1,
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 101,
|
||||
'class_section_name' => '3',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'class_id' => 2,
|
||||
'class_section_id' => 102,
|
||||
'class_section_name' => '10',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
],
|
||||
]);
|
||||
|
||||
$service = new FeeStudentFeeService(new FeeGradeService(), new FeeConfigService());
|
||||
$service = $this->makeService();
|
||||
|
||||
$total = $service->totalTuitionFee([
|
||||
['class_section_id' => 101],
|
||||
@@ -49,4 +28,136 @@ class FeeStudentFeeServiceTest extends TestCase
|
||||
|
||||
$this->assertSame(530.0, $total);
|
||||
}
|
||||
|
||||
public function test_assign_fees_stores_tuition_fee_and_fee_category_on_each_student(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(101, '2');
|
||||
$this->seedClassSection(102, '5');
|
||||
$this->seedClassSection(103, '10');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$assigned = $service->assignFees([
|
||||
['student_id' => 1, 'class_section_id' => 101],
|
||||
['student_id' => 2, 'class_section_id' => 102],
|
||||
['student_id' => 3, 'class_section_id' => 103],
|
||||
]);
|
||||
|
||||
$byId = [];
|
||||
foreach ($assigned as $row) {
|
||||
$byId[$row['student_id']] = $row;
|
||||
}
|
||||
|
||||
$this->assertSame(350.0, $byId[1]['tuition_fee']);
|
||||
$this->assertSame('first_regular', $byId[1]['fee_category']);
|
||||
|
||||
$this->assertSame(200.0, $byId[2]['tuition_fee']);
|
||||
$this->assertSame('additional_regular', $byId[2]['fee_category']);
|
||||
|
||||
$this->assertSame(180.0, $byId[3]['tuition_fee']);
|
||||
$this->assertSame('youth', $byId[3]['fee_category']);
|
||||
}
|
||||
|
||||
public function test_calculate_breakdown_includes_billable_and_non_billable_students(): void
|
||||
{
|
||||
$this->seedFeeConfig();
|
||||
$this->seedClassSection(201, '3');
|
||||
$this->seedClassSection(202, '4');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$breakdown = $service->calculateBreakdown([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'firstname' => 'Ada',
|
||||
'lastname' => 'L',
|
||||
'class_section_id' => 201,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 2,
|
||||
'firstname' => 'Bo',
|
||||
'lastname' => 'L',
|
||||
'class_section_id' => 202,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 3,
|
||||
'firstname' => 'Cy',
|
||||
'lastname' => 'L',
|
||||
'class_section_id' => 201,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'pending',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(350.0, $breakdown['family_total']);
|
||||
$this->assertSame(1, $breakdown['billable_count']);
|
||||
$this->assertSame(2, $breakdown['non_billable_count']);
|
||||
$this->assertCount(3, $breakdown['students']);
|
||||
|
||||
$byId = [];
|
||||
foreach ($breakdown['students'] as $row) {
|
||||
$byId[$row['student_id']] = $row;
|
||||
}
|
||||
|
||||
$this->assertSame('first_regular', $byId[1]['fee_category']);
|
||||
$this->assertSame(350.0, $byId[1]['tuition_fee']);
|
||||
|
||||
$this->assertSame('not_billable', $byId[2]['fee_category']);
|
||||
$this->assertSame(0.0, $byId[2]['tuition_fee']);
|
||||
|
||||
$this->assertSame('not_billable', $byId[3]['fee_category']);
|
||||
}
|
||||
|
||||
public function test_is_billable_and_is_withdrawn_classifiers(): void
|
||||
{
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertTrue($service->isBillable([
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
]));
|
||||
$this->assertTrue($service->isBillable([
|
||||
'enrollment_status' => 'payment pending',
|
||||
'admission_status' => 'accepted',
|
||||
]));
|
||||
$this->assertFalse($service->isBillable([
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'pending',
|
||||
]));
|
||||
|
||||
$this->assertTrue($service->isWithdrawn(['enrollment_status' => 'withdrawn']));
|
||||
$this->assertTrue($service->isWithdrawn(['enrollment_status' => 'refund pending']));
|
||||
$this->assertTrue($service->isWithdrawn(['enrollment_status' => 'withdraw under review']));
|
||||
$this->assertFalse($service->isWithdrawn(['enrollment_status' => 'enrolled']));
|
||||
}
|
||||
|
||||
private function makeService(): FeeStudentFeeService
|
||||
{
|
||||
return new FeeStudentFeeService(new FeeGradeService(), new FeeConfigService());
|
||||
}
|
||||
|
||||
private function seedFeeConfig(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Fees;
|
||||
|
||||
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 TuitionCalculationServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_calculate_returns_family_total_and_per_student_breakdown(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$this->seedClassSection(101, '2');
|
||||
$this->seedClassSection(102, '5');
|
||||
$this->seedClassSection(103, '11');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculate([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'firstname' => 'Anna',
|
||||
'lastname' => 'Doe',
|
||||
'class_section_id' => 101,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 2,
|
||||
'firstname' => 'Ben',
|
||||
'lastname' => 'Doe',
|
||||
'class_section_id' => 102,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 3,
|
||||
'firstname' => 'Cara',
|
||||
'lastname' => 'Doe',
|
||||
'class_section_id' => 103,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame('2025-2026', $result['school_year']);
|
||||
$this->assertSame(3, $result['billable_count']);
|
||||
$this->assertSame(0, $result['non_billable_count']);
|
||||
$this->assertSame(730.0, $result['family_total']);
|
||||
|
||||
$byStudent = [];
|
||||
foreach ($result['students'] as $row) {
|
||||
$byStudent[$row['student_id']] = $row;
|
||||
}
|
||||
|
||||
$this->assertSame('first_regular', $byStudent[1]['fee_category']);
|
||||
$this->assertSame(350.0, $byStudent[1]['tuition_fee']);
|
||||
$this->assertSame('Anna Doe', $byStudent[1]['student_name']);
|
||||
|
||||
$this->assertSame('additional_regular', $byStudent[2]['fee_category']);
|
||||
$this->assertSame(200.0, $byStudent[2]['tuition_fee']);
|
||||
|
||||
$this->assertSame('youth', $byStudent[3]['fee_category']);
|
||||
$this->assertSame(180.0, $byStudent[3]['tuition_fee']);
|
||||
}
|
||||
|
||||
public function test_calculate_excludes_non_billable_students_from_family_total(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$this->seedClassSection(201, '3');
|
||||
$this->seedClassSection(202, '4');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculate([
|
||||
[
|
||||
'student_id' => 10,
|
||||
'class_section_id' => 201,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
[
|
||||
'student_id' => 11,
|
||||
'class_section_id' => 202,
|
||||
'enrollment_status' => 'withdrawn',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(1, $result['billable_count']);
|
||||
$this->assertSame(1, $result['non_billable_count']);
|
||||
$this->assertSame(350.0, $result['family_total']);
|
||||
|
||||
$byStudent = [];
|
||||
foreach ($result['students'] as $row) {
|
||||
$byStudent[$row['student_id']] = $row;
|
||||
}
|
||||
|
||||
$this->assertSame('not_billable', $byStudent[11]['fee_category']);
|
||||
$this->assertSame(0.0, $byStudent[11]['tuition_fee']);
|
||||
}
|
||||
|
||||
public function test_calculate_applies_per_student_discount(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$this->seedClassSection(301, '2');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$result = $service->calculate([
|
||||
[
|
||||
'student_id' => 99,
|
||||
'class_section_id' => 301,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
'discount_amount' => 100.0,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(250.0, $result['family_total']);
|
||||
$this->assertSame(100.0, $result['students'][0]['discount_amount']);
|
||||
$this->assertSame(250.0, $result['students'][0]['final_tuition_amount']);
|
||||
}
|
||||
|
||||
public function test_family_total_helper(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$this->seedClassSection(401, '3');
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertSame(350.0, $service->familyTotal([
|
||||
[
|
||||
'student_id' => 1,
|
||||
'class_section_id' => 401,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
private function makeService(): TuitionCalculationService
|
||||
{
|
||||
$config = new FeeConfigService();
|
||||
return new TuitionCalculationService(
|
||||
$config,
|
||||
new FeeStudentFeeService(new FeeGradeService(), $config)
|
||||
);
|
||||
}
|
||||
|
||||
private function seedConfig(): 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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ class PaymentEventChargesServiceTest extends TestCase
|
||||
'updated_by' => null,
|
||||
]);
|
||||
|
||||
$service = new PaymentEventChargesService();
|
||||
$service = app(PaymentEventChargesService::class);
|
||||
$students = $service->getEnrolledStudents(10, '2025-2026');
|
||||
|
||||
$this->assertCount(1, $students);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Promotions;
|
||||
|
||||
use App\Models\LevelProgression;
|
||||
use App\Services\Promotions\LevelProgressionService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LevelProgressionServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_seed_defaults_creates_baseline_levels(): void
|
||||
{
|
||||
$service = new LevelProgressionService();
|
||||
$created = $service->seedDefaults();
|
||||
$this->assertGreaterThan(0, $created);
|
||||
$this->assertSame($created, LevelProgression::query()->count());
|
||||
$this->assertDatabaseHas('level_progressions', [
|
||||
'current_level_name' => 'Grade 3',
|
||||
'next_level_name' => 'Grade 4',
|
||||
]);
|
||||
$this->assertDatabaseHas('level_progressions', [
|
||||
'current_level_name' => 'Youth',
|
||||
'is_terminal' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_resolve_by_class_id_uses_mapping_when_present(): void
|
||||
{
|
||||
DB::table('classes')->insert([
|
||||
['id' => 7, 'class_name' => 'Grade 3', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
|
||||
['id' => 8, 'class_name' => 'Grade 4', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
|
||||
]);
|
||||
|
||||
$service = new LevelProgressionService();
|
||||
$service->seedDefaults();
|
||||
|
||||
$resolved = $service->resolveByCurrentClassId(7);
|
||||
$this->assertNotNull($resolved);
|
||||
$this->assertSame('Grade 3', $resolved['current_level_name']);
|
||||
$this->assertSame('Grade 4', $resolved['next_level_name']);
|
||||
$this->assertSame(8, $resolved['next_level_id']);
|
||||
$this->assertFalse($resolved['is_terminal']);
|
||||
}
|
||||
|
||||
public function test_resolve_falls_back_to_class_id_plus_one_when_no_mapping(): void
|
||||
{
|
||||
DB::table('classes')->insert([
|
||||
['id' => 100, 'class_name' => 'Custom A', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
|
||||
['id' => 101, 'class_name' => 'Custom B', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
|
||||
]);
|
||||
|
||||
$service = new LevelProgressionService();
|
||||
$resolved = $service->resolveByCurrentClassId(100);
|
||||
|
||||
$this->assertNotNull($resolved);
|
||||
$this->assertSame('Custom A', $resolved['current_level_name']);
|
||||
$this->assertSame('Custom B', $resolved['next_level_name']);
|
||||
$this->assertSame(101, $resolved['next_level_id']);
|
||||
}
|
||||
|
||||
public function test_upsert_mapping_updates_existing_row(): void
|
||||
{
|
||||
$service = new LevelProgressionService();
|
||||
$service->upsertMapping([
|
||||
'current_level_name' => 'Grade 5',
|
||||
'next_level_name' => 'Grade 6',
|
||||
'order_index' => 70,
|
||||
]);
|
||||
$service->upsertMapping([
|
||||
'current_level_name' => 'Grade 5',
|
||||
'next_level_name' => 'Grade 6 (Updated)',
|
||||
'order_index' => 75,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('level_progressions', [
|
||||
'current_level_name' => 'Grade 5',
|
||||
'next_level_name' => 'Grade 6 (Updated)',
|
||||
'order_index' => 75,
|
||||
]);
|
||||
$this->assertSame(1, LevelProgression::query()->where('current_level_name', 'Grade 5')->count());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Promotions;
|
||||
|
||||
use App\Models\StudentPromotionRecord;
|
||||
use App\Services\Promotions\LevelProgressionService;
|
||||
use App\Services\Promotions\PromotionAuditService;
|
||||
use App\Services\Promotions\PromotionEligibilityService;
|
||||
use App\Services\Promotions\PromotionStatusService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PromotionEligibilityServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_passing_student_becomes_awaiting_parent_enrollment(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$studentId = $this->seedStudent();
|
||||
$this->seedClassChain();
|
||||
$this->seedAssignment($studentId, 701, '2025-2026');
|
||||
$this->seedScores($studentId, '2025-2026', 80.0, 75.0);
|
||||
|
||||
$service = $this->makeService();
|
||||
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
|
||||
|
||||
$this->assertNotNull($record);
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_AWAITING_PARENT, $record->promotion_status);
|
||||
$this->assertTrue((bool) $record->passed_current_level);
|
||||
$this->assertSame(77.5, (float) $record->final_average);
|
||||
$this->assertSame('Grade 3', $record->current_level_name);
|
||||
$this->assertSame('Grade 4', $record->promoted_level_name);
|
||||
$this->assertSame('2026-2027', $record->next_school_year);
|
||||
}
|
||||
|
||||
public function test_failing_student_becomes_repeated(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$studentId = $this->seedStudent();
|
||||
$this->seedClassChain();
|
||||
$this->seedAssignment($studentId, 701, '2025-2026');
|
||||
$this->seedScores($studentId, '2025-2026', 50.0, 55.0);
|
||||
|
||||
$service = $this->makeService();
|
||||
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
|
||||
|
||||
$this->assertNotNull($record);
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_REPEATED, $record->promotion_status);
|
||||
$this->assertFalse((bool) $record->passed_current_level);
|
||||
}
|
||||
|
||||
public function test_missing_scores_marks_on_hold(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$studentId = $this->seedStudent();
|
||||
$this->seedClassChain();
|
||||
$this->seedAssignment($studentId, 701, '2025-2026');
|
||||
|
||||
$service = $this->makeService();
|
||||
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
|
||||
|
||||
$this->assertNotNull($record);
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_ON_HOLD, $record->promotion_status);
|
||||
$this->assertNull($record->passed_current_level);
|
||||
}
|
||||
|
||||
public function test_terminal_level_is_graduated(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$studentId = $this->seedStudent();
|
||||
$this->seedClassChain();
|
||||
$this->seedAssignment($studentId, 901, '2025-2026');
|
||||
$this->seedScores($studentId, '2025-2026', 90.0, 90.0);
|
||||
|
||||
// Mark Youth as terminal
|
||||
\App\Models\LevelProgression::query()->where('current_level_name', 'Youth')->update(['is_terminal' => 1]);
|
||||
|
||||
$service = $this->makeService();
|
||||
$record = $service->evaluateStudent($studentId, '2025-2026', 1);
|
||||
|
||||
$this->assertNotNull($record);
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_GRADUATED, $record->promotion_status);
|
||||
}
|
||||
|
||||
public function test_audit_log_records_status_changes(): void
|
||||
{
|
||||
$this->seedConfig();
|
||||
$studentId = $this->seedStudent();
|
||||
$this->seedClassChain();
|
||||
$this->seedAssignment($studentId, 701, '2025-2026');
|
||||
$this->seedScores($studentId, '2025-2026', 80.0, 80.0);
|
||||
|
||||
$service = $this->makeService();
|
||||
$service->evaluateStudent($studentId, '2025-2026', 99);
|
||||
|
||||
$this->assertDatabaseHas('promotion_audit_log', [
|
||||
'student_id' => $studentId,
|
||||
'action_type' => 'eligibility_evaluated',
|
||||
'user_id' => 99,
|
||||
]);
|
||||
$this->assertDatabaseHas('promotion_audit_log', [
|
||||
'student_id' => $studentId,
|
||||
'action_type' => 'record_created',
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeService(): PromotionEligibilityService
|
||||
{
|
||||
$progression = new LevelProgressionService();
|
||||
$audit = new PromotionAuditService();
|
||||
$status = new PromotionStatusService($audit);
|
||||
return new PromotionEligibilityService($progression, $status, $audit);
|
||||
}
|
||||
|
||||
private function seedConfig(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedStudent(int $parentId = 5): int
|
||||
{
|
||||
return DB::table('students')->insertGetId([
|
||||
'school_id' => 'S-100',
|
||||
'firstname' => 'Sara',
|
||||
'lastname' => 'Test',
|
||||
'parent_id' => $parentId,
|
||||
'year_of_registration' => '2025',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedClassChain(): void
|
||||
{
|
||||
DB::table('classes')->insert([
|
||||
['id' => 7, 'class_name' => 'Grade 3', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
|
||||
['id' => 8, 'class_name' => 'Grade 4', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
|
||||
['id' => 13, 'class_name' => 'Youth', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 30],
|
||||
]);
|
||||
DB::table('classSection')->insert([
|
||||
['class_id' => 7, 'class_section_id' => 701, 'class_section_name' => '3A'],
|
||||
['class_id' => 13, 'class_section_id' => 901, 'class_section_name' => 'Youth A'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedAssignment(int $studentId, int $classSectionId, string $schoolYear): void
|
||||
{
|
||||
DB::table('student_class')->insert([
|
||||
'student_id' => $studentId,
|
||||
'class_section_id' => $classSectionId,
|
||||
'school_year' => $schoolYear,
|
||||
'semester' => 'Fall',
|
||||
'is_event_only' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedScores(int $studentId, string $schoolYear, float $fall, float $spring): void
|
||||
{
|
||||
DB::table('semester_scores')->insert([
|
||||
['student_id' => $studentId, 'class_section_id' => 0, 'school_year' => $schoolYear, 'semester' => 'fall', 'semester_score' => $fall],
|
||||
['student_id' => $studentId, 'class_section_id' => 0, 'school_year' => $schoolYear, 'semester' => 'spring', 'semester_score' => $spring],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Promotions;
|
||||
|
||||
use App\Models\StudentPromotionRecord;
|
||||
use App\Services\Promotions\PromotionAuditService;
|
||||
use App\Services\Promotions\PromotionEnrollmentService;
|
||||
use App\Services\Promotions\PromotionStatusService;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PromotionEnrollmentServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_completing_checklist_finalises_promotion_and_creates_enrollment(): void
|
||||
{
|
||||
[$record, $service, $parentId] = $this->bootstrap();
|
||||
|
||||
$service->updateSteps($record, $parentId, [
|
||||
'info_confirmed' => true,
|
||||
'documents_uploaded' => true,
|
||||
'agreement_accepted' => true,
|
||||
], $parentId);
|
||||
|
||||
$service->updateSteps($record, $parentId, [
|
||||
'payment_completed' => true,
|
||||
], $parentId);
|
||||
|
||||
$record->refresh();
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, $record->promotion_status);
|
||||
$this->assertSame(StudentPromotionRecord::ENROLLMENT_COMPLETED, $record->enrollment_status);
|
||||
$this->assertNotNull($record->promotion_finalized_at);
|
||||
$this->assertDatabaseHas('enrollments', [
|
||||
'student_id' => $record->student_id,
|
||||
'school_year' => $record->next_school_year,
|
||||
'enrollment_status' => 'enrolled',
|
||||
'admission_status' => 'accepted',
|
||||
]);
|
||||
$this->assertDatabaseHas('promotion_audit_log', [
|
||||
'promotion_id' => $record->getKey(),
|
||||
'action_type' => 'promotion_finalized',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_partial_checklist_keeps_status_in_progress(): void
|
||||
{
|
||||
[$record, $service, $parentId] = $this->bootstrap();
|
||||
|
||||
$service->updateSteps($record, $parentId, [
|
||||
'info_confirmed' => true,
|
||||
'documents_uploaded' => true,
|
||||
], $parentId);
|
||||
|
||||
$record->refresh();
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_ENROLLMENT_STARTED, $record->promotion_status);
|
||||
$this->assertSame(StudentPromotionRecord::ENROLLMENT_IN_PROGRESS, $record->enrollment_status);
|
||||
$this->assertNull($record->promotion_finalized_at);
|
||||
}
|
||||
|
||||
public function test_submit_requires_complete_checklist(): void
|
||||
{
|
||||
[$record, $service, $parentId] = $this->bootstrap();
|
||||
|
||||
$service->updateSteps($record, $parentId, [
|
||||
'info_confirmed' => true,
|
||||
], $parentId);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$service->submitEnrollment($record->refresh(), $parentId, $parentId);
|
||||
}
|
||||
|
||||
public function test_other_parent_cannot_act_on_record(): void
|
||||
{
|
||||
[$record, $service] = $this->bootstrap();
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$service->startEnrollment($record, 9999, 9999);
|
||||
}
|
||||
|
||||
public function test_expired_records_become_not_enrolled(): void
|
||||
{
|
||||
[$record, $service] = $this->bootstrap();
|
||||
$record->enrollment_deadline = now()->subDays(2)->toDateString();
|
||||
$record->save();
|
||||
|
||||
$result = $service->markExpiredDeadlines(CarbonImmutable::now(), 99);
|
||||
|
||||
$this->assertSame(1, $result['expired']);
|
||||
$record->refresh();
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_NOT_ENROLLED, $record->promotion_status);
|
||||
$this->assertSame(StudentPromotionRecord::ENROLLMENT_EXPIRED, $record->enrollment_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0:StudentPromotionRecord, 1:PromotionEnrollmentService, 2:int}
|
||||
*/
|
||||
private function bootstrap(): array
|
||||
{
|
||||
$audit = new PromotionAuditService();
|
||||
$service = new PromotionEnrollmentService(new PromotionStatusService($audit), $audit);
|
||||
|
||||
$parentId = (int) DB::table('users')->insertGetId([
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'One',
|
||||
'email' => 'parent@example.com',
|
||||
'cellphone' => '5551234567',
|
||||
'address_street' => '1 Way',
|
||||
'city' => 'C',
|
||||
'state' => 'S',
|
||||
'zip' => '00000',
|
||||
'accept_school_policy' => 1,
|
||||
'password' => bcrypt('x'),
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'status' => 'Active',
|
||||
]);
|
||||
|
||||
$studentId = (int) DB::table('students')->insertGetId([
|
||||
'school_id' => 'S-1',
|
||||
'firstname' => 'Sara',
|
||||
'lastname' => 'Test',
|
||||
'parent_id' => $parentId,
|
||||
'year_of_registration' => '2025',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
|
||||
$record = StudentPromotionRecord::query()->create([
|
||||
'student_id' => $studentId,
|
||||
'parent_id' => $parentId,
|
||||
'current_school_year' => '2025-2026',
|
||||
'next_school_year' => '2026-2027',
|
||||
'promotion_status' => StudentPromotionRecord::STATUS_AWAITING_PARENT,
|
||||
'enrollment_required' => true,
|
||||
'enrollment_status' => StudentPromotionRecord::ENROLLMENT_NOT_STARTED,
|
||||
'promoted_level_name' => 'Grade 4',
|
||||
'enrollment_deadline' => now()->addDays(30)->toDateString(),
|
||||
]);
|
||||
|
||||
return [$record, $service, $parentId];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Promotions;
|
||||
|
||||
use App\Models\StudentPromotionRecord;
|
||||
use App\Services\Promotions\PromotionAuditService;
|
||||
use App\Services\Promotions\PromotionStatusService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PromotionStatusServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_allowed_transition_updates_status_and_audit_log(): void
|
||||
{
|
||||
$record = $this->makeRecord(StudentPromotionRecord::STATUS_AWAITING_PARENT);
|
||||
$service = $this->makeService();
|
||||
|
||||
$updated = $service->transition(
|
||||
$record,
|
||||
StudentPromotionRecord::STATUS_ENROLLMENT_STARTED,
|
||||
42,
|
||||
'parent began enrollment'
|
||||
);
|
||||
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_ENROLLMENT_STARTED, $updated->promotion_status);
|
||||
$this->assertDatabaseHas('promotion_audit_log', [
|
||||
'promotion_id' => $record->getKey(),
|
||||
'action_type' => 'status_changed',
|
||||
'old_value' => StudentPromotionRecord::STATUS_AWAITING_PARENT,
|
||||
'new_value' => StudentPromotionRecord::STATUS_ENROLLMENT_STARTED,
|
||||
'user_id' => 42,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_disallowed_transition_throws(): void
|
||||
{
|
||||
$record = $this->makeRecord(StudentPromotionRecord::STATUS_NOT_REVIEWED);
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$service->transition($record, StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, 1);
|
||||
}
|
||||
|
||||
public function test_force_status_bypasses_transition_map(): void
|
||||
{
|
||||
$record = $this->makeRecord(StudentPromotionRecord::STATUS_NOT_REVIEWED);
|
||||
$service = $this->makeService();
|
||||
|
||||
$updated = $service->forceStatus($record, StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, 7, 'manual fix');
|
||||
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED, $updated->promotion_status);
|
||||
$this->assertDatabaseHas('promotion_audit_log', [
|
||||
'promotion_id' => $record->getKey(),
|
||||
'action_type' => 'manual_override',
|
||||
'field' => 'promotion_status',
|
||||
'old_value' => StudentPromotionRecord::STATUS_NOT_REVIEWED,
|
||||
'new_value' => StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_no_op_transition_returns_record_unchanged(): void
|
||||
{
|
||||
$record = $this->makeRecord(StudentPromotionRecord::STATUS_AWAITING_PARENT);
|
||||
$service = $this->makeService();
|
||||
|
||||
$updated = $service->transition(
|
||||
$record,
|
||||
StudentPromotionRecord::STATUS_AWAITING_PARENT,
|
||||
1
|
||||
);
|
||||
|
||||
$this->assertSame(StudentPromotionRecord::STATUS_AWAITING_PARENT, $updated->promotion_status);
|
||||
$this->assertDatabaseMissing('promotion_audit_log', [
|
||||
'promotion_id' => $record->getKey(),
|
||||
'action_type' => 'status_changed',
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeService(): PromotionStatusService
|
||||
{
|
||||
return new PromotionStatusService(new PromotionAuditService());
|
||||
}
|
||||
|
||||
private function makeRecord(string $status): StudentPromotionRecord
|
||||
{
|
||||
$studentId = DB::table('students')->insertGetId([
|
||||
'school_id' => 'S-1',
|
||||
'firstname' => 'Test',
|
||||
'lastname' => 'Student',
|
||||
'parent_id' => 1,
|
||||
'year_of_registration' => '2025',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
]);
|
||||
|
||||
return StudentPromotionRecord::query()->create([
|
||||
'student_id' => $studentId,
|
||||
'parent_id' => 1,
|
||||
'current_school_year' => '2025-2026',
|
||||
'next_school_year' => '2026-2027',
|
||||
'promotion_status' => $status,
|
||||
'enrollment_required' => true,
|
||||
'enrollment_status' => StudentPromotionRecord::ENROLLMENT_NOT_STARTED,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user