update api and add more features
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\V1\Promotions;
|
||||
|
||||
use App\Models\StudentPromotionRecord;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ParentPromotionControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_overview_returns_actionable_records_for_parent(): void
|
||||
{
|
||||
$parent = $this->seedParent();
|
||||
$studentId = $this->seedStudent($parent->id);
|
||||
$this->seedPromotionRecord($studentId, $parent->id);
|
||||
|
||||
Sanctum::actingAs($parent);
|
||||
$response = $this->getJson('/api/v1/parents/promotions');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('ok', true);
|
||||
$response->assertJsonPath('records.0.parent_action_required', true);
|
||||
$response->assertJsonPath('records.0.promoted_level', 'Grade 4');
|
||||
}
|
||||
|
||||
public function test_parent_completes_full_enrollment_flow(): void
|
||||
{
|
||||
$parent = $this->seedParent();
|
||||
$studentId = $this->seedStudent($parent->id);
|
||||
$promotionId = $this->seedPromotionRecord($studentId, $parent->id);
|
||||
|
||||
Sanctum::actingAs($parent);
|
||||
|
||||
// Start enrollment
|
||||
$start = $this->postJson('/api/v1/parents/promotions/' . $promotionId . '/start');
|
||||
$start->assertOk();
|
||||
$start->assertJsonPath('data.promotion_status', StudentPromotionRecord::STATUS_ENROLLMENT_STARTED);
|
||||
|
||||
// Complete the checklist incrementally
|
||||
$update = $this->patchJson('/api/v1/parents/promotions/' . $promotionId . '/steps', [
|
||||
'info_confirmed' => true,
|
||||
'documents_uploaded' => true,
|
||||
'agreement_accepted' => true,
|
||||
]);
|
||||
$update->assertOk();
|
||||
$update->assertJsonPath('data.promotion_status', StudentPromotionRecord::STATUS_ENROLLMENT_STARTED);
|
||||
|
||||
// Final step finalises promotion
|
||||
$finish = $this->patchJson('/api/v1/parents/promotions/' . $promotionId . '/steps', [
|
||||
'payment_completed' => true,
|
||||
]);
|
||||
$finish->assertOk();
|
||||
$finish->assertJsonPath('data.promotion_status', StudentPromotionRecord::STATUS_PROMOTED_AND_ENROLLED);
|
||||
|
||||
$this->assertDatabaseHas('enrollments', [
|
||||
'student_id' => $studentId,
|
||||
'school_year' => '2026-2027',
|
||||
'enrollment_status' => 'enrolled',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_parent_cannot_view_other_parents_promotion(): void
|
||||
{
|
||||
$parentA = $this->seedParent('A', 'a@example.com');
|
||||
$parentB = $this->seedParent('B', 'b@example.com');
|
||||
$studentId = $this->seedStudent($parentB->id);
|
||||
$promotionId = $this->seedPromotionRecord($studentId, $parentB->id);
|
||||
|
||||
Sanctum::actingAs($parentA);
|
||||
$response = $this->getJson('/api/v1/parents/promotions/' . $promotionId);
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_unauthenticated_request_is_unauthorized(): void
|
||||
{
|
||||
$response = $this->getJson('/api/v1/parents/promotions');
|
||||
$response->assertStatus(401);
|
||||
}
|
||||
|
||||
private function seedParent(string $name = 'Parent', string $email = 'parent@example.com'): User
|
||||
{
|
||||
DB::table('roles')->insertOrIgnore([
|
||||
['id' => 2, 'name' => 'parent', 'slug' => 'parent', 'is_active' => 1],
|
||||
]);
|
||||
|
||||
$userId = DB::table('users')->insertGetId([
|
||||
'firstname' => $name,
|
||||
'lastname' => 'User',
|
||||
'cellphone' => '5550000000',
|
||||
'email' => $email,
|
||||
'address_street' => '1 Way',
|
||||
'city' => 'C',
|
||||
'state' => 'S',
|
||||
'zip' => '00000',
|
||||
'accept_school_policy' => 1,
|
||||
'password' => bcrypt('secret'),
|
||||
'user_type' => 'primary',
|
||||
'school_year' => '2025-2026',
|
||||
'semester' => 'Fall',
|
||||
'status' => 'Active',
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => $userId,
|
||||
'role_id' => 2,
|
||||
]);
|
||||
|
||||
return User::query()->findOrFail($userId);
|
||||
}
|
||||
|
||||
private function seedStudent(int $parentId): int
|
||||
{
|
||||
return (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',
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedPromotionRecord(int $studentId, int $parentId): int
|
||||
{
|
||||
$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,
|
||||
'current_level_name' => 'Grade 3',
|
||||
'promoted_level_name' => 'Grade 4',
|
||||
'enrollment_deadline' => now()->addDays(30)->toDateString(),
|
||||
]);
|
||||
return (int) $record->getKey();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user