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,166 @@
<?php
namespace Tests\Feature\Api\V1\Finance;
use App\Models\User;
use App\Services\Invoices\InvoiceGenerationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Mockery;
use Tests\TestCase;
class ChargeControllerTest extends TestCase
{
use RefreshDatabase;
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
protected function setUp(): void
{
parent::setUp();
$this->mock(InvoiceGenerationService::class, function ($mock) {
$mock->shouldReceive('generateInvoice')->andReturn(['ok' => true]);
});
}
public function test_list_charges_for_parent(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
DB::table('additional_charges')->insert([
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Books',
'amount' => 20.00,
'status' => 'pending',
]);
$response = $this->getJson('/api/v1/finance/fees/charges?parent_id=10');
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertCount(1, $response->json('charges.extra_charges'));
}
public function test_store_extra_charge_returns_409_on_duplicate(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
DB::table('additional_charges')->insert([
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Books',
'amount' => 20.00,
'status' => 'pending',
]);
$response = $this->postJson('/api/v1/finance/fees/charges/extra', [
'parent_id' => 10,
'title' => 'Books',
'amount' => 20,
'charge_type' => 'add',
]);
$response->assertStatus(409);
$response->assertJsonPath('ok', false);
$response->assertJsonPath('charge.error', 'duplicate_charge');
}
public function test_store_event_charge_creates_row(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
$response = $this->postJson('/api/v1/finance/fees/charges/event', [
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Science Fair',
'amount' => 15,
]);
$response->assertStatus(201);
$response->assertJsonPath('ok', true);
$this->assertDatabaseHas('event_charges', [
'parent_id' => 10,
'student_id' => 1,
'event_name' => 'Science Fair',
]);
}
public function test_cancel_extra_charge_voids_row(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
$id = DB::table('additional_charges')->insertGetId([
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'charge_type' => 'add',
'title' => 'Fee',
'amount' => 10.00,
'status' => 'pending',
]);
$response = $this->postJson("/api/v1/finance/fees/charges/extra/{$id}/cancel");
$response->assertOk();
$this->assertDatabaseHas('additional_charges', ['id' => $id, 'status' => 'void']);
}
public function test_charges_endpoints_require_authentication(): void
{
$response = $this->getJson('/api/v1/finance/fees/charges?parent_id=10');
$response->assertStatus(401);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function createUser(): User
{
DB::table('users')->insert([
'id' => 1,
'school_id' => 1,
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '5555555555',
'email' => 'admin@example.com',
'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',
]);
return User::query()->findOrFail(1);
}
}
@@ -74,9 +74,188 @@ class FeeCalculationControllerTest extends TestCase
'school_end_date',
'weeks_of_study',
'withdrawn_students',
'students' => [
'*' => [
'student_id',
'grade',
'grade_level',
'fee_category',
'tuition_fee',
'withdrawal_date',
'weeks_remaining',
'refund_amount',
'eligible',
'reason',
],
],
],
]);
$this->assertGreaterThan(0, (float) $response->json('refund.refund_amount'));
$this->assertGreaterThan(0, count($response->json('refund.students') ?? []));
}
public function test_tuition_breakdown_endpoint_returns_per_student_detail(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
$this->seedClassSection();
$response = $this->postJson('/api/v1/finance/fees/tuition-breakdown', [
'students' => [
[
'student_id' => 1,
'firstname' => 'Alpha',
'lastname' => 'Last',
'class_section_id' => 101,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
[
'student_id' => 2,
'firstname' => 'Beta',
'lastname' => 'Last',
'class_section_id' => 101,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
],
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure([
'ok',
'tuition' => [
'school_year',
'family_total',
'billable_count',
'non_billable_count',
'fees' => ['first_student_fee', 'second_student_fee', 'youth_fee'],
'students' => [
'*' => [
'student_id',
'student_name',
'grade',
'grade_level',
'enrollment_status',
'admission_status',
'fee_category',
'tuition_fee',
'discount_amount',
'final_tuition_amount',
],
],
],
]);
$this->assertSame(550.0, (float) $response->json('tuition.family_total'));
$this->assertSame(2, (int) $response->json('tuition.billable_count'));
}
public function test_tuition_breakdown_endpoint_requires_authentication(): void
{
$response = $this->postJson('/api/v1/finance/fees/tuition-breakdown', [
'students' => [
['class_section_id' => 101],
],
]);
$response->assertStatus(401);
}
public function test_tuition_breakdown_endpoint_validates_payload(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/finance/fees/tuition-breakdown', [
'students' => [],
]);
$response->assertStatus(422);
$response->assertJsonStructure(['message', 'errors']);
}
public function test_family_balance_endpoint_returns_account_summary(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
$this->seedClassSection();
DB::table('invoices')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_number' => 'INV-1',
'total_amount' => 500.00,
'balance' => 0.00,
'paid_amount' => 200.00,
'has_discount' => 0,
'issue_date' => '2025-01-05',
'status' => 'paid',
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
DB::table('payments')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_id' => 1,
'paid_amount' => 200.00,
'total_amount' => 200.00,
'balance' => 0.00,
'number_of_installments' => 1,
'payment_date' => '2025-01-10',
'payment_method' => 'manual',
'school_year' => '2025-2026',
'status' => 'Paid',
]);
$response = $this->postJson('/api/v1/finance/fees/family-balance', [
'parent_id' => 10,
'students' => [
[
'student_id' => 1,
'class_section_id' => 101,
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
],
],
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure([
'ok',
'account' => [
'parent_id',
'school_year',
'charges' => ['tuition', 'extra_charges', 'event_charges', 'late_fees', 'total'],
'credits' => ['discounts', 'total'],
'payments',
'refunds_applied',
'remaining_balance',
'account_credit',
'students',
],
]);
$this->assertSame(350.0, (float) $response->json('account.charges.tuition'));
$this->assertSame(150.0, (float) $response->json('account.remaining_balance'));
}
public function test_family_balance_endpoint_requires_authentication(): void
{
$response = $this->postJson('/api/v1/finance/fees/family-balance', [
'parent_id' => 10,
'students' => [
['class_section_id' => 101],
],
]);
$response->assertStatus(401);
}
public function test_tuition_total_endpoint_returns_total(): void