fix apply the plan docs/alrahma_api_fix_plan_school_year_only_v7
API CI/CD / Validate (composer + pint) (push) Successful in 3m10s
API CI/CD / Test (PHPUnit) (push) Failing after 6m49s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 1m0s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped

This commit is contained in:
root
2026-07-07 01:52:29 -04:00
parent 58726ee0e9
commit 031e499819
35 changed files with 5633 additions and 182 deletions
@@ -41,6 +41,33 @@ class SchoolYearControllerTest extends TestCase
$this->assertSame('promote', $response->json('data.promotion_preview.rows.0.promotion_action'));
}
public function test_name_based_school_year_management_routes_use_school_year_query(): void
{
$this->seedClosureData();
$this->actingAs(User::query()->findOrFail(1), 'api');
$this->getJson('/api/v1/school-years/summary?school_year=2025-2026')
->assertOk()
->assertJsonPath('data.school_year.name', '2025-2026');
$this->getJson('/api/v1/school-years/promotion-preview?school_year=2025-2026&to_school_year=2026-2027')
->assertOk()
->assertJsonPath('data.current_school_year', '2025-2026')
->assertJsonPath('data.target_school_year', '2026-2027');
$this->postJson('/api/v1/school-years/preview-close?school_year=2025-2026', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
])->assertOk()
->assertJsonPath('data.school_year.name', '2025-2026')
->assertJsonPath('data.parent_balances.summary.total_old_unpaid_balance', 200);
}
public function test_preview_close_excludes_zero_invoice_balance_parent_even_when_parent_account_is_stale(): void
{
$this->seedClosureData();
@@ -73,6 +100,146 @@ class SchoolYearControllerTest extends TestCase
);
}
public function test_positive_balance_is_not_hidden_by_credit_balance(): void
{
$this->seedClosureData();
DB::table('invoices')->insert([
'id' => 502,
'parent_id' => 10,
'invoice_number' => 'INV-502-CREDIT',
'total_amount' => 0,
'balance' => -200,
'paid_amount' => 200,
'has_discount' => 0,
'issue_date' => '2026-05-11',
'due_date' => '2026-06-10',
'status' => 'credit',
'description' => 'Credit balance',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
'updated_by' => 1,
'semester' => 'Fall',
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->postJson('/api/v1/school-years/preview-close?school_year=2025-2026', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
]);
$response->assertOk()
->assertJsonPath('data.parent_balances.summary.parents_with_positive_balance', 1)
->assertJsonPath('data.parent_balances.summary.total_positive_unpaid_balance', 200)
->assertJsonPath('data.parent_balances.summary.parents_with_credit_balance', 1)
->assertJsonPath('data.parent_balances.summary.total_credit_balance', 200)
->assertJsonPath('data.parent_balances.summary.net_balance_impact', 0);
$row = collect($response->json('data.parent_balances.rows'))
->first(fn (array $row): bool => (int) $row['parent_id'] === 10);
$this->assertSame(200.0, (float) $row['positive_unpaid_balance']);
$this->assertSame(200.0, (float) $row['credit_balance']);
$this->assertSame(0.0, (float) $row['net_balance']);
$this->assertSame(200.0, (float) $row['amount_to_transfer']);
$this->assertSame([500], $row['positive_invoice_ids']);
$this->assertSame([502], $row['credit_invoice_ids']);
}
public function test_credit_only_parent_is_reported_as_credit_not_unpaid_carryover(): void
{
$this->seedClosureData();
$this->seedParent(12, 'Credit', 'Only', 'credit@example.com');
DB::table('invoices')->insert([
'id' => 503,
'parent_id' => 12,
'invoice_number' => 'INV-503-CREDIT',
'total_amount' => 0,
'balance' => -50,
'paid_amount' => 50,
'has_discount' => 0,
'issue_date' => '2026-05-11',
'due_date' => '2026-06-10',
'status' => 'credit',
'description' => 'Credit only',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
'updated_by' => 1,
'semester' => 'Fall',
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->getJson('/api/v1/school-years/parent-balances?school_year=2025-2026&to_school_year=2026-2027');
$response->assertOk()
->assertJsonPath('data.summary.parents_with_positive_balance', 1)
->assertJsonPath('data.summary.parents_with_credit_balance', 1)
->assertJsonPath('data.summary.total_positive_unpaid_balance', 200)
->assertJsonPath('data.summary.total_credit_balance', 50);
$creditRow = collect($response->json('data.rows'))
->first(fn (array $row): bool => (int) $row['parent_id'] === 12);
$this->assertNotNull($creditRow);
$this->assertSame(0.0, (float) $creditRow['positive_unpaid_balance']);
$this->assertSame(50.0, (float) $creditRow['credit_balance']);
$this->assertSame(0.0, (float) $creditRow['amount_to_transfer']);
$this->assertSame([503], $creditRow['credit_invoice_ids']);
}
public function test_pending_or_draft_invoice_blocks_close_but_is_not_transferred(): void
{
$this->seedClosureData();
DB::table('invoices')->insert([
'id' => 504,
'parent_id' => 10,
'invoice_number' => 'INV-504-DRAFT',
'total_amount' => 100,
'balance' => 100,
'paid_amount' => 0,
'has_discount' => 0,
'issue_date' => '2026-05-11',
'due_date' => '2026-06-10',
'status' => 'draft',
'description' => 'Draft invoice',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
'updated_by' => 1,
'semester' => 'Fall',
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->postJson('/api/v1/school-years/preview-close?school_year=2025-2026', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
]);
$response->assertOk()
->assertJsonPath('data.validation.can_close', false)
->assertJsonPath('data.parent_balances.summary.total_positive_unpaid_balance', 200);
$this->assertStringContainsString(
'invoices are still in draft status',
implode(' ', $response->json('data.validation.errors'))
);
}
public function test_close_school_year_does_not_transfer_zero_invoice_balance_parent_even_when_parent_account_is_stale(): void
{
$this->seedClosureData();
@@ -111,6 +278,79 @@ class SchoolYearControllerTest extends TestCase
]);
}
public function test_close_does_not_mutate_old_year_parent_account(): void
{
$this->seedClosureData();
DB::table('parent_accounts')->insert([
'parent_id' => 10,
'school_year' => '2025-2026',
'opening_balance' => 55,
'current_balance' => 77,
'created_at' => now(),
'updated_at' => now(),
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$this->postJson('/api/v1/school-years/close?school_year=2025-2026', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
'confirmation' => 'CLOSE 2025-2026',
])->assertOk();
$this->assertDatabaseHas('parent_accounts', [
'parent_id' => 10,
'school_year' => '2025-2026',
'opening_balance' => 55,
'current_balance' => 77,
]);
$this->assertDatabaseHas('parent_accounts', [
'parent_id' => 10,
'school_year' => '2026-2027',
'opening_balance' => 200,
'current_balance' => 200,
]);
}
public function test_existing_balance_transfer_conflict_is_visible_in_preview_and_blocks_close(): void
{
$this->seedClosureData();
DB::table('parent_balance_transfers')->insert([
'parent_id' => 10,
'from_school_year' => '2025-2026',
'to_school_year' => '2026-2027',
'amount' => 200,
'status' => 'transferred',
'source_summary_json' => json_encode(['source_invoice_ids' => [500]]),
'created_by' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->postJson('/api/v1/school-years/preview-close?school_year=2025-2026', [
'new_school_year' => [
'name' => '2026-2027',
'start_date' => '2026-09-01',
'end_date' => '2027-06-30',
],
'transfer_unpaid_balances' => true,
]);
$response->assertOk()
->assertJsonPath('data.validation.can_close', false)
->assertJsonPath('data.parent_balances.summary.existing_transfer_conflicts', 1)
->assertJsonPath('data.parent_balances.rows.0.existing_transfer_conflict', true);
}
public function test_parent_cannot_close_school_year(): void
{
$this->seedClosureData();
@@ -231,6 +471,41 @@ class SchoolYearControllerTest extends TestCase
]);
}
public function test_admin_can_update_school_year_by_name_via_api(): void
{
$this->seedClosureData();
DB::table('school_years')->insert([
'id' => 2,
'name' => '2027-2028',
'start_date' => '2027-09-01',
'end_date' => '2028-06-30',
'status' => 'draft',
'is_current' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
$this->actingAs(User::query()->findOrFail(1), 'api');
$response = $this->patchJson('/api/v1/school-years/selected?school_year=2027-2028', [
'name' => '2028-2029',
'start_date' => '2028-09-01',
'end_date' => '2029-06-30',
'school_year' => '2027-2028',
]);
$response->assertOk()
->assertJsonPath('data.name', '2028-2029')
->assertJsonPath('data.start_date', '2028-09-01');
$this->assertDatabaseHas('school_years', [
'id' => 2,
'name' => '2028-2029',
'start_date' => '2028-09-01 00:00:00',
'end_date' => '2029-06-30 00:00:00',
]);
}
public function test_close_school_year_creates_new_active_year_enrollment_and_balance_transfer(): void
{
$this->seedClosureData();
@@ -379,6 +654,35 @@ class SchoolYearControllerTest extends TestCase
]);
}
private function seedParent(int $id, string $firstname, string $lastname, string $email): void
{
DB::table('users')->insert([
'id' => $id,
'school_id' => $id,
'firstname' => $firstname,
'lastname' => $lastname,
'cellphone' => '5555555555',
'email' => $email,
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'is_verified' => 1,
'status' => 'Active',
'is_suspended' => 0,
'failed_attempts' => 0,
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('user_roles')->insert([
'user_id' => $id,
'role_id' => 2,
]);
}
private function seedClosureData(): void
{
DB::table('configuration')->insert([