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
@@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundDecisionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundDecisionServiceTest extends TestCase
{
use RefreshDatabase;
public function test_approve_updates_refund_status(): void
{
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(),
]);
$service = new RefundDecisionService();
$result = $service->approve(1, 2);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('refunds', [
'id' => 1,
'status' => 'Approved',
]);
}
}
@@ -0,0 +1,51 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Models\Invoice;
use App\Services\Discounts\DiscountInvoiceService;
use App\Services\Refunds\RefundInvoiceAdjustmentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;
class RefundInvoiceAdjustmentServiceTest extends TestCase
{
use RefreshDatabase;
public function test_apply_book_charge_inserts_additional_charge(): void
{
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',
]);
$invoice = Invoice::query()->findOrFail(1);
$invoiceService = Mockery::mock(DiscountInvoiceService::class);
$invoiceService->shouldReceive('recalculateInvoice')->once()->with(1, '2025-2026');
$service = new RefundInvoiceAdjustmentService($invoiceService);
$result = $service->applyBookChargeAndUpdateInvoice($invoice, 10, [
'eligible' => true,
'book_price' => 15.00,
], 'Fall', 1);
$this->assertSame('inserted', $result['book_charge_action']);
$this->assertDatabaseHas('additional_charges', [
'invoice_id' => 1,
'title' => 'Book Fee (Non-Refundable)',
'amount' => 15.00,
]);
}
}
@@ -0,0 +1,23 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class RefundNotificationServiceTest extends TestCase
{
use RefreshDatabase;
public function test_notify_pending_logs_event(): void
{
Log::spy();
$service = new RefundNotificationService();
$service->notifyPending(10, 50.25, 'https://example.test/login');
Log::shouldHaveReceived('info')->once();
}
}
@@ -0,0 +1,57 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundNotificationService;
use App\Services\Refunds\RefundOverpaymentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundOverpaymentServiceTest extends TestCase
{
use RefreshDatabase;
public function test_recalculate_creates_overpayment_refund(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
DB::table('invoices')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_number' => 'INV-1',
'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',
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Paid',
]);
$service = new RefundOverpaymentService(new RefundNotificationService());
$result = $service->recalculate(false, null, 1);
$this->assertNotEmpty($result['created_ids']);
$this->assertDatabaseHas('refunds', [
'parent_id' => 10,
'request' => 'overpayment',
]);
}
}
@@ -0,0 +1,42 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundPayoutService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundPayoutServiceTest extends TestCase
{
use RefreshDatabase;
public function test_record_payment_marks_partial(): void
{
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_id' => 1,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 100,
'refund_paid_amount' => 0,
'status' => 'Approved',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$service = new RefundPayoutService();
$result = $service->recordPayment(1, [
'paid_amount' => 40,
'payment_method' => 'Cash',
], 2);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('refunds', [
'id' => 1,
'status' => 'Partial',
]);
}
}
@@ -0,0 +1,104 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundInvoiceAdjustmentService;
use App\Services\Refunds\RefundPolicyService;
use App\Services\Fees\FeeRefundDetailService;
use App\Services\Discounts\DiscountInvoiceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundPolicyServiceTest extends TestCase
{
use RefreshDatabase;
public function test_process_withdrawal_refund_creates_refund(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
['config_key' => 'book_price', 'config_value' => '10'],
['config_key' => 'grade_fee', 'config_value' => '9'],
['config_key' => 'first_student_fee', 'config_value' => '350'],
['config_key' => 'second_student_fee', 'config_value' => '200'],
['config_key' => 'youth_fee', 'config_value' => '180'],
]);
DB::table('classSection')->insert([
'id' => 1,
'class_id' => 1,
'class_section_id' => 101,
'class_section_name' => '3',
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('students')->insert([
'id' => 1,
'parent_id' => 10,
'firstname' => 'Kid',
'lastname' => 'One',
'school_year' => '2025-2026',
]);
DB::table('enrollments')->insert([
'id' => 1,
'student_id' => 1,
'parent_id' => 10,
'class_section_id' => 101,
'enrollment_status' => 'withdrawn',
'withdrawal_date' => '2025-01-15',
'school_year' => '2025-2026',
'semester' => 'Fall',
'admission_status' => 'accepted',
'is_withdrawn' => 1,
]);
DB::table('invoices')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_number' => 'INV-1',
'total_amount' => 350.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' => 100.00,
'total_amount' => 100.00,
'balance' => 0.00,
'payment_date' => '2025-01-10',
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Paid',
]);
$service = new RefundPolicyService(
new FeeRefundDetailService(),
new RefundInvoiceAdjustmentService(new DiscountInvoiceService())
);
$result = $service->processWithdrawalRefund(10, 1, '2025-2026', 'Fall', 1);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('refunds', [
'parent_id' => 10,
'invoice_id' => 1,
'request' => 'tuition',
]);
$this->assertDatabaseHas('additional_charges', [
'invoice_id' => 1,
'title' => 'Book Fee (Non-Refundable)',
]);
}
}
@@ -0,0 +1,49 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundQueryServiceTest extends TestCase
{
use RefreshDatabase;
public function test_list_filters_by_status(): void
{
DB::table('refunds')->insert([
[
'id' => 1,
'parent_id' => 10,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 10,
'refund_paid_amount' => 0,
'status' => 'Pending',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
],
[
'id' => 2,
'parent_id' => 11,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 20,
'refund_paid_amount' => 0,
'status' => 'Approved',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
],
]);
$service = new RefundQueryService();
$result = $service->list(['status' => 'Approved', 'per_page' => 10, 'page' => 1]);
$this->assertSame(1, $result['pagination']['total']);
$this->assertSame('Approved', $result['refunds']->items()[0]->status);
}
}
@@ -0,0 +1,82 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Fees\FeeRefundDetailService;
use App\Services\Refunds\RefundInvoiceAdjustmentService;
use App\Services\Refunds\RefundNotificationService;
use App\Services\Refunds\RefundPolicyService;
use App\Services\Refunds\RefundRequestService;
use App\Services\Refunds\RefundSummaryService;
use App\Services\Discounts\DiscountInvoiceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundRequestServiceTest extends TestCase
{
use RefreshDatabase;
public function test_request_overpayment_creates_refund(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
DB::table('users')->insert([
'id' => 10,
'firstname' => 'Parent',
'lastname' => 'User',
'email' => 'parent@example.com',
'status' => 'Active',
]);
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',
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Paid',
]);
$service = new RefundRequestService(
new RefundPolicyService(
new FeeRefundDetailService(),
new RefundInvoiceAdjustmentService(new DiscountInvoiceService())
),
new RefundSummaryService(),
new RefundNotificationService()
);
$result = $service->requestRefund([
'parent_id' => 10,
'request_type' => 'overpayment',
'amount' => 25,
], 1);
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('refunds', [
'parent_id' => 10,
'request' => 'overpayment',
]);
}
}
@@ -0,0 +1,64 @@
<?php
namespace Tests\Unit\Services\Refunds;
use App\Services\Refunds\RefundSummaryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RefundSummaryServiceTest extends TestCase
{
use RefreshDatabase;
public function test_summary_calculates_unapplied_balance(): void
{
DB::table('invoices')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_number' => 'INV-1',
'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',
'school_year' => '2025-2026',
'semester' => 'Fall',
'status' => 'Paid',
]);
DB::table('refunds')->insert([
'id' => 1,
'parent_id' => 10,
'invoice_id' => 1,
'school_year' => '2025-2026',
'semester' => 'Fall',
'refund_amount' => 20,
'refund_paid_amount' => 20,
'status' => 'Paid',
'request' => 'overpayment',
'created_at' => now(),
'updated_at' => now(),
]);
$service = new RefundSummaryService();
$summary = $service->getParentFinancialSummary(10, '2025-2026', 'Fall');
$this->assertSame(100.0, $summary['total_invoiced']);
$this->assertSame(150.0, $summary['total_paid_in']);
$this->assertSame(20.0, $summary['total_refunded']);
$this->assertSame(30.0, $summary['unapplied_balance']);
}
}