add refund and event logic

This commit is contained in:
root
2026-03-10 23:12:49 -04:00
parent ba1206e314
commit f6be51576c
67 changed files with 4808 additions and 1000 deletions
@@ -3,6 +3,8 @@
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;
@@ -63,6 +65,17 @@ class FeeCalculationControllerTest extends TestCase
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure([
'ok',
'refund' => [
'refund_amount',
'total_paid',
'refund_deadline',
'school_end_date',
'weeks_of_study',
'withdrawn_students',
],
]);
$this->assertGreaterThan(0, (float) $response->json('refund.refund_amount'));
}
@@ -83,9 +96,110 @@ class FeeCalculationControllerTest extends TestCase
$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([
@@ -0,0 +1,335 @@
<?php
namespace Tests\Feature\Api\V1\Finance;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class RefundControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_requires_authentication(): void
{
$response = $this->getJson('/api/v1/finance/refunds');
$response->assertStatus(401);
}
public function test_index_returns_paginated_refunds(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('refunds')->insert([
[
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 50,
'refund_paid_amount' => 0,
'status' => 'Pending',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
],
]);
$response = $this->getJson('/api/v1/finance/refunds');
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure([
'ok',
'refunds',
'pagination' => [
'current_page',
'per_page',
'total',
'total_pages',
],
]);
}
public function test_show_returns_refund(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 50,
'refund_paid_amount' => 0,
'status' => 'Pending',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->getJson('/api/v1/finance/refunds/1');
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure(['ok', 'refund' => ['id', 'status', 'refund_amount']]);
}
public function test_store_overpayment_creates_refund(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
DB::table('invoices')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_number' => 'INV-1',
'total_amount' => 100.00,
'balance' => 100.00,
'paid_amount' => 0.00,
'status' => 'Unpaid',
'school_year' => '2025-2026',
'semester' => 'Fall',
'issue_date' => '2025-01-05',
]);
DB::table('payments')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_id' => 1,
'paid_amount' => 150.00,
'total_amount' => 150.00,
'balance' => 0.00,
'payment_date' => '2025-01-10',
'payment_method' => 'manual',
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Paid',
]);
$response = $this->postJson('/api/v1/finance/refunds', [
'parent_id' => 10,
'request_type' => 'overpayment',
'amount' => 25,
]);
$response->assertStatus(201);
$response->assertJsonPath('ok', true);
$this->assertDatabaseHas('refunds', [
'parent_id' => 10,
'request' => 'overpayment',
]);
}
public function test_store_validates_payload(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/finance/refunds', [
'parent_id' => 0,
'request_type' => 'overpayment',
]);
$response->assertStatus(422);
$response->assertJsonStructure(['message', 'errors']);
}
public function test_update_approves_refund(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 50,
'refund_paid_amount' => 0,
'status' => 'Pending',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->patchJson('/api/v1/finance/refunds/1', [
'status' => 'Approved',
'reason' => 'ok',
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertDatabaseHas('refunds', [
'id' => 1,
'status' => 'Approved',
]);
}
public function test_reject_requires_reason(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 50,
'refund_paid_amount' => 0,
'status' => 'Pending',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->postJson('/api/v1/finance/refunds/1/reject', []);
$response->assertStatus(422);
$response->assertJsonStructure(['message', 'errors']);
}
public function test_record_payment_updates_refund(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 100,
'refund_paid_amount' => 0,
'status' => 'Approved',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->postJson('/api/v1/finance/refunds/1/payments', [
'paid_amount' => 40,
'payment_method' => 'Cash',
]);
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertDatabaseHas('refunds', [
'id' => 1,
'status' => 'Partial',
]);
}
public function test_recalculate_overpayments_creates_refund(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$this->seedConfig();
DB::table('invoices')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_number' => 'INV-2',
'total_amount' => 100.00,
'balance' => 0.00,
'paid_amount' => 100.00,
'status' => 'Paid',
'school_year' => '2025-2026',
'semester' => 'Fall',
'issue_date' => '2025-01-05',
]);
DB::table('payments')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_id' => 1,
'paid_amount' => 150.00,
'total_amount' => 150.00,
'balance' => 0.00,
'payment_date' => '2025-01-10',
'payment_method' => 'manual',
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Paid',
]);
$response = $this->postJson('/api/v1/finance/refunds/recalculate-overpayments', []);
$response->assertOk();
$response->assertJsonPath('ok', true);
$this->assertDatabaseHas('refunds', [
'parent_id' => 10,
'request' => 'overpayment',
]);
}
public function test_destroy_cancels_refund(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 50,
'refund_paid_amount' => 0,
'status' => 'Pending',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->deleteJson('/api/v1/finance/refunds/1');
$response->assertOk();
$this->assertDatabaseHas('refunds', [
'id' => 1,
'status' => 'Canceled',
]);
}
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);
}
}
@@ -0,0 +1,361 @@
<?php
namespace Tests\Feature\Api\V1\Settings;
use App\Models\User;
use App\Services\Events\EventManagementService;
use App\Services\Invoices\InvoiceGenerationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class EventControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_requires_authentication(): void
{
$response = $this->getJson('/api/v1/settings/events');
$response->assertStatus(401);
}
public function test_index_returns_events(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Open House',
'event_category' => 'workshops',
'amount' => 10,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->getJson('/api/v1/settings/events');
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure([
'ok',
'events',
'pagination',
'active_count',
'categories',
]);
}
public function test_store_creates_event(): void
{
$this->mock(InvoiceGenerationService::class, function ($mock) {
$mock->shouldReceive('generateInvoice')->andReturn(['ok' => true]);
});
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('students')->insert([
'id' => 1,
'parent_id' => 10,
'firstname' => 'Kid',
'lastname' => 'One',
]);
DB::table('enrollments')->insert([
'id' => 1,
'student_id' => 1,
'parent_id' => 10,
'class_section_id' => 101,
'enrollment_status' => 'enrolled',
'school_year' => '2025-2026',
]);
$response = $this->postJson('/api/v1/settings/events', [
'event_name' => 'Field Trip',
'event_category' => 'field trips',
'description' => 'Visit museum',
'amount' => 25,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
$response->assertStatus(201);
$response->assertJsonPath('ok', true);
$this->assertDatabaseHas('events', [
'event_name' => 'Field Trip',
'school_year' => '2025-2026',
]);
$this->assertDatabaseHas('event_charges', [
'event_id' => 1,
'student_id' => 1,
]);
}
public function test_store_requires_authentication(): void
{
$response = $this->postJson('/api/v1/settings/events', [
'event_name' => 'Field Trip',
'event_category' => 'field trips',
'amount' => 25,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
$response->assertStatus(401);
}
public function test_store_validates_payload(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/settings/events', [
'event_name' => '',
'event_category' => 'invalid',
]);
$response->assertStatus(422);
$response->assertJsonStructure(['message', 'errors']);
}
public function test_store_handles_service_exception(): void
{
$this->mock(EventManagementService::class, function ($mock) {
$mock->shouldReceive('create')->andThrow(new \RuntimeException('boom'));
});
$user = $this->createUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/settings/events', [
'event_name' => 'Field Trip',
'event_category' => 'field trips',
'description' => 'Visit museum',
'amount' => 25,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
$response->assertStatus(500);
$response->assertJsonPath('ok', false);
}
public function test_update_updates_event(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Open House',
'event_category' => 'workshops',
'amount' => 10,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->patchJson('/api/v1/settings/events/1', [
'event_name' => 'Updated',
]);
$response->assertOk();
$this->assertDatabaseHas('events', [
'id' => 1,
'event_name' => 'Updated',
]);
}
public function test_destroy_deletes_event(): void
{
$this->mock(InvoiceGenerationService::class, function ($mock) {
$mock->shouldReceive('generateInvoice')->andReturn(['ok' => true]);
});
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Open House',
'event_category' => 'workshops',
'amount' => 10,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('event_charges')->insert([
'id' => 1,
'event_id' => 1,
'parent_id' => 10,
'student_id' => 1,
'participation' => 'yes',
'charged' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$response = $this->deleteJson('/api/v1/settings/events/1');
$response->assertOk();
$this->assertDatabaseMissing('events', ['id' => 1]);
}
public function test_charges_list_returns_data(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('users')->insert([
'id' => 10,
'firstname' => 'Parent',
'lastname' => 'User',
'email' => 'p@example.com',
'status' => 'Active',
]);
DB::table('students')->insert([
'id' => 1,
'parent_id' => 10,
'firstname' => 'Kid',
'lastname' => 'One',
]);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Open House',
'event_category' => 'workshops',
'amount' => 10,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('event_charges')->insert([
'id' => 1,
'event_id' => 1,
'parent_id' => 10,
'student_id' => 1,
'participation' => 'yes',
'charged' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$response = $this->getJson('/api/v1/settings/events/charges/list');
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure(['ok', 'charges', 'pagination']);
}
public function test_update_charges_updates_participation(): void
{
$this->mock(InvoiceGenerationService::class, function ($mock) {
$mock->shouldReceive('generateInvoice')->andReturn(['ok' => true]);
});
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('events')->insert([
'id' => 1,
'event_name' => 'Open House',
'event_category' => 'workshops',
'amount' => 10,
'expiration_date' => '2025-02-01',
'semester' => 'Fall',
'school_year' => '2025-2026',
'created_at' => now(),
'updated_at' => now(),
]);
$response = $this->postJson('/api/v1/settings/events/1/charges', [
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'participation' => [
1 => 'yes',
],
]);
$response->assertOk();
$this->assertDatabaseHas('event_charges', [
'event_id' => 1,
'student_id' => 1,
'parent_id' => 10,
]);
}
public function test_students_with_charges_returns_students(): void
{
$user = $this->createUser();
Sanctum::actingAs($user);
DB::table('students')->insert([
'id' => 1,
'parent_id' => 10,
'firstname' => 'Kid',
'lastname' => 'One',
]);
DB::table('event_charges')->insert([
'id' => 1,
'event_id' => 1,
'parent_id' => 10,
'student_id' => 1,
'participation' => 'yes',
'charged' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
]);
$response = $this->getJson('/api/v1/settings/events/charges/students?parent_id=10&school_year=2025-2026&semester=Fall');
$response->assertOk();
$response->assertJsonPath('ok', true);
$response->assertJsonStructure(['ok', 'students']);
}
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);
}
}