add family logic

This commit is contained in:
root
2026-03-10 17:52:47 -04:00
parent a82f7aedbc
commit 17d73e2f92
37 changed files with 2611 additions and 2 deletions
@@ -0,0 +1,51 @@
<?php
namespace Tests\Unit\Services\Families;
use App\Services\Families\FamilyFinanceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class FamilyFinanceServiceTest extends TestCase
{
use RefreshDatabase;
public function test_load_financials_for_parents_returns_summary(): void
{
DB::table('invoices')->insert([
'parent_id' => 1,
'invoice_number' => 'INV-1',
'status' => 'open',
'total_amount' => 100,
'paid_amount' => 20,
'balance' => 80,
'issue_date' => now()->toDateString(),
'due_date' => now()->addDays(5)->toDateString(),
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('payments')->insert([
'parent_id' => 1,
'invoice_id' => 1,
'total_amount' => 100,
'paid_amount' => 20,
'balance' => 80,
'number_of_installments' => 1,
'payment_method' => 'cash',
'payment_date' => now()->toDateString(),
'status' => 'completed',
'created_at' => now(),
'updated_at' => now(),
]);
$service = new FamilyFinanceService();
$result = $service->loadFinancialsForParents([1]);
$this->assertSame(1, $result['summary']['invoices_count']);
$this->assertSame(100.0, $result['summary']['total_amount']);
$this->assertNotEmpty($result['invoices']);
$this->assertNotEmpty($result['payments']);
}
}
@@ -0,0 +1,114 @@
<?php
namespace Tests\Unit\Services\Families;
use App\Services\Families\FamilyMutationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class FamilyMutationServiceTest extends TestCase
{
use RefreshDatabase;
public function test_bootstrap_creates_family(): void
{
$this->seedUser(1);
$this->seedStudent(10, 1);
$service = new FamilyMutationService();
$result = $service->bootstrapFamilies();
$this->assertSame(1, $result['families_created']);
$this->assertDatabaseHas('families', ['family_code' => 'FAM-1']);
$this->assertDatabaseHas('family_students', ['student_id' => 10]);
}
public function test_attach_second_by_email_creates_guardian(): void
{
$this->seedUser(1);
$this->seedStudent(10, 1);
DB::table('families')->insert([
'family_code' => 'FAM-1',
'household_name' => 'Family 1',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
$service = new FamilyMutationService();
$result = $service->attachSecondByEmail(10, 'secondary@example.com', 'Second', 'Parent');
$this->assertTrue($result['ok']);
$this->assertDatabaseHas('users', ['email' => 'secondary@example.com']);
$this->assertDatabaseHas('family_guardians', ['user_id' => $result['user_id']]);
}
public function test_set_primary_home_updates_flag(): void
{
$this->seedUser(1);
$this->seedStudent(10, 1);
$familyId = DB::table('families')->insertGetId([
'family_code' => 'FAM-1',
'household_name' => 'Family 1',
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('family_students')->insert([
'family_id' => $familyId,
'student_id' => 10,
'is_primary_home' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
$service = new FamilyMutationService();
$service->setPrimaryHome($familyId, 10, true);
$this->assertDatabaseHas('family_students', [
'family_id' => $familyId,
'student_id' => 10,
'is_primary_home' => 1,
]);
}
private function seedUser(int $id): void
{
DB::table('users')->insert([
'id' => $id,
'firstname' => 'Parent',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'parent' . $id . '@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
}
private function seedStudent(int $id, int $parentId): void
{
DB::table('students')->insert([
'id' => $id,
'school_id' => 'SCH1',
'firstname' => 'Student',
'lastname' => 'One',
'age' => 10,
'gender' => 'Male',
'photo_consent' => 1,
'parent_id' => $parentId,
'year_of_registration' => '2025-2026',
'semester' => 'Fall',
'school_year' => '2025-2026',
'is_active' => 1,
]);
}
}
@@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\Services\Families;
use App\Services\Email\EmailDispatchService;
use App\Services\Families\FamilyNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class FamilyNotificationServiceTest extends TestCase
{
use RefreshDatabase;
public function test_send_compose_email_delegates_to_dispatcher(): void
{
$this->mock(EmailDispatchService::class, function ($mock) {
$mock->shouldReceive('send')
->once()
->andReturn(true);
});
$service = $this->app->make(FamilyNotificationService::class);
$result = $service->sendComposeEmail(
'recipient@example.com',
'Subject',
'<p>Body</p>',
null,
null,
null
);
$this->assertTrue($result);
}
}
@@ -0,0 +1,137 @@
<?php
namespace Tests\Unit\Services\Families;
use App\Services\Families\FamilyFinanceService;
use App\Services\Families\FamilyQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class FamilyQueryServiceTest extends TestCase
{
use RefreshDatabase;
public function test_admin_index_returns_families(): void
{
$this->seedConfig();
$studentId = $this->seedStudent(10, 1);
$familyId = $this->seedFamily('FAM-1');
$this->seedFamilyStudent($familyId, $studentId);
$this->seedFamilyGuardian($familyId, 1);
$service = new FamilyQueryService(new FamilyFinanceService());
$result = $service->adminIndexData($studentId, null, '2025-2026');
$this->assertNotEmpty($result['families']);
$this->assertSame($studentId, $result['student']['id']);
}
public function test_search_suggestions_returns_items(): void
{
$this->seedStudent(10, 1);
$service = new FamilyQueryService(new FamilyFinanceService());
$items = $service->searchSuggestions('Student', 5);
$this->assertNotEmpty($items);
$this->assertSame('student', $items[0]['type']);
}
public function test_family_card_returns_payload(): void
{
$this->seedConfig();
$studentId = $this->seedStudent(10, 1);
$familyId = $this->seedFamily('FAM-1');
$this->seedFamilyStudent($familyId, $studentId);
$this->seedFamilyGuardian($familyId, 1);
$service = new FamilyQueryService(new FamilyFinanceService());
$family = $service->familyCard(null, null, $familyId, '2025-2026');
$this->assertNotNull($family);
$this->assertSame($familyId, $family['id']);
}
private function seedConfig(): void
{
DB::table('configuration')->insert([
['config_key' => 'school_year', 'config_value' => '2025-2026'],
['config_key' => 'semester', 'config_value' => 'Fall'],
]);
}
private function seedStudent(int $id, int $parentId): int
{
DB::table('users')->insert([
'id' => $parentId,
'firstname' => 'Parent',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'parent' . $parentId . '@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
DB::table('students')->insert([
'id' => $id,
'school_id' => 'SCH1',
'firstname' => 'Student',
'lastname' => 'One',
'age' => 10,
'gender' => 'Male',
'photo_consent' => 1,
'parent_id' => $parentId,
'year_of_registration' => '2025-2026',
'semester' => 'Fall',
'school_year' => '2025-2026',
'is_active' => 1,
]);
return $id;
}
private function seedFamily(string $code): int
{
return DB::table('families')->insertGetId([
'family_code' => $code,
'household_name' => 'Family ' . $code,
'is_active' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedFamilyStudent(int $familyId, int $studentId): void
{
DB::table('family_students')->insert([
'family_id' => $familyId,
'student_id' => $studentId,
'is_primary_home' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
}
private function seedFamilyGuardian(int $familyId, int $userId): void
{
DB::table('family_guardians')->insert([
'family_id' => $familyId,
'user_id' => $userId,
'relation' => 'primary',
'is_primary' => 1,
'receive_emails' => 1,
'receive_sms' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
}