433 lines
13 KiB
PHP
433 lines
13 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\V1\Finance;
|
|
|
|
use App\Models\User;
|
|
use App\Services\Fees\FeeRefundCalculatorService;
|
|
use App\Services\Fees\FeeStudentFeeService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class FeeCalculationControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_refund_endpoint_returns_amount(): 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' => 200.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/refund', [
|
|
'parent_id' => 10,
|
|
'students' => [
|
|
[
|
|
'student_id' => 1,
|
|
'class_section_id' => 101,
|
|
'enrollment_status' => 'withdrawn',
|
|
'admission_status' => 'accepted',
|
|
'withdrawal_date' => '2025-01-15',
|
|
],
|
|
],
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('ok', true);
|
|
$response->assertJsonStructure([
|
|
'ok',
|
|
'refund' => [
|
|
'refund_amount',
|
|
'total_paid',
|
|
'refund_deadline',
|
|
'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
|
|
{
|
|
$user = $this->createUser();
|
|
Sanctum::actingAs($user);
|
|
|
|
$this->seedConfig();
|
|
$this->seedClassSection();
|
|
|
|
$response = $this->postJson('/api/v1/finance/fees/tuition-total', [
|
|
'students' => [
|
|
['class_section_id' => 101],
|
|
['class_section_id' => 101],
|
|
],
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('ok', true);
|
|
$response->assertJsonStructure([
|
|
'ok',
|
|
'tuition' => [
|
|
'total_tuition',
|
|
],
|
|
]);
|
|
$this->assertGreaterThan(0, (float) $response->json('tuition.total_tuition'));
|
|
}
|
|
|
|
public function test_refund_endpoint_requires_authentication(): void
|
|
{
|
|
$response = $this->postJson('/api/v1/finance/fees/refund', [
|
|
'parent_id' => 10,
|
|
'students' => [],
|
|
]);
|
|
|
|
$response->assertStatus(401);
|
|
}
|
|
|
|
public function test_tuition_total_endpoint_requires_authentication(): void
|
|
{
|
|
$response = $this->postJson('/api/v1/finance/fees/tuition-total', [
|
|
'students' => [],
|
|
]);
|
|
|
|
$response->assertStatus(401);
|
|
}
|
|
|
|
public function test_refund_endpoint_validates_payload(): void
|
|
{
|
|
$user = $this->createUser();
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->postJson('/api/v1/finance/fees/refund', [
|
|
'parent_id' => 0,
|
|
'students' => [
|
|
['class_section_id' => null],
|
|
],
|
|
]);
|
|
|
|
$response->assertStatus(422);
|
|
$response->assertJsonStructure(['message', 'errors']);
|
|
}
|
|
|
|
public function test_tuition_total_endpoint_validates_payload(): void
|
|
{
|
|
$user = $this->createUser();
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->postJson('/api/v1/finance/fees/tuition-total', [
|
|
'students' => [
|
|
['class_section_id' => null],
|
|
],
|
|
]);
|
|
|
|
$response->assertStatus(422);
|
|
$response->assertJsonStructure(['message', 'errors']);
|
|
}
|
|
|
|
public function test_refund_endpoint_handles_service_failure(): void
|
|
{
|
|
$user = $this->createUser();
|
|
Sanctum::actingAs($user);
|
|
|
|
$this->mock(FeeRefundCalculatorService::class, function ($mock) {
|
|
$mock->shouldReceive('calculateRefund')->andThrow(new \RuntimeException('boom'));
|
|
});
|
|
|
|
$response = $this->postJson('/api/v1/finance/fees/refund', [
|
|
'parent_id' => 10,
|
|
'students' => [
|
|
[
|
|
'student_id' => 1,
|
|
'class_section_id' => 101,
|
|
'enrollment_status' => 'withdrawn',
|
|
'admission_status' => 'accepted',
|
|
'withdrawal_date' => '2025-01-15',
|
|
],
|
|
],
|
|
]);
|
|
|
|
$response->assertStatus(500);
|
|
$response->assertJsonPath('ok', false);
|
|
}
|
|
|
|
public function test_tuition_total_endpoint_handles_service_failure(): void
|
|
{
|
|
$user = $this->createUser();
|
|
Sanctum::actingAs($user);
|
|
|
|
$this->mock(FeeStudentFeeService::class, function ($mock) {
|
|
$mock->shouldReceive('totalTuitionFee')->andThrow(new \RuntimeException('boom'));
|
|
});
|
|
|
|
$response = $this->postJson('/api/v1/finance/fees/tuition-total', [
|
|
'students' => [
|
|
['class_section_id' => 101],
|
|
],
|
|
]);
|
|
|
|
$response->assertStatus(500);
|
|
$response->assertJsonPath('ok', false);
|
|
}
|
|
|
|
private function seedConfig(): 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(): void
|
|
{
|
|
DB::table('classSection')->insert([
|
|
'id' => 1,
|
|
'class_id' => 1,
|
|
'class_section_id' => 101,
|
|
'class_section_name' => '3',
|
|
'semester' => 'Fall',
|
|
'school_year' => '2025-2026',
|
|
]);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|