|
|
@@ -0,0 +1,120 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Unit;
|
|
|
+
|
|
|
+use App\Models\City;
|
|
|
+use App\Models\Country;
|
|
|
+use App\Models\State;
|
|
|
+use App\Models\Student;
|
|
|
+use App\Models\StudentContractInstallment;
|
|
|
+use App\Models\Unit;
|
|
|
+use App\Services\StudentContractService;
|
|
|
+use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
+use Tests\TestCase;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Taxa de Contratação / Entrada: abate do Total do Curso antes de dividir as
|
|
|
+ * parcelas restantes, e gera sua própria parcela (REF. ENTRADA), do mesmo
|
|
|
+ * jeito que a matrícula já funciona.
|
|
|
+ */
|
|
|
+class StudentContractDownPaymentTest extends TestCase
|
|
|
+{
|
|
|
+ use RefreshDatabase;
|
|
|
+
|
|
|
+ private Unit $unit;
|
|
|
+
|
|
|
+ private StudentContractService $service;
|
|
|
+
|
|
|
+ protected function setUp(): void
|
|
|
+ {
|
|
|
+ parent::setUp();
|
|
|
+
|
|
|
+ $this->unit = $this->makeUnit();
|
|
|
+ $this->service = new StudentContractService();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_down_payment_reduces_total_installments_basis_and_creates_its_own_installment(): void
|
|
|
+ {
|
|
|
+ $student = Student::create([
|
|
|
+ 'name' => 'Aluno Teste',
|
|
|
+ 'unit_id' => $this->unit->id,
|
|
|
+ 'status' => 'active',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $contract = $this->service->create([
|
|
|
+ 'student_id' => $student->id,
|
|
|
+ 'unit_id' => $this->unit->id,
|
|
|
+ 'status' => 'pending',
|
|
|
+ 'total_value' => 5843.00,
|
|
|
+ 'total_installments' => 13,
|
|
|
+ 'total_due_date' => '2026-10-10',
|
|
|
+ 'down_payment_value' => 500.00,
|
|
|
+ 'down_payment_date' => '2026-09-15',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $installments = StudentContractInstallment::where('student_contract_id', $contract->id)
|
|
|
+ ->orderBy('type')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $downPayment = $installments->firstWhere('type', 'down_payment');
|
|
|
+
|
|
|
+ $this->assertNotNull($downPayment);
|
|
|
+ $this->assertSame('REF. ENTRADA', $downPayment->history);
|
|
|
+ $this->assertSame(1, $downPayment->total_installments);
|
|
|
+ $this->assertEquals(500.00, $downPayment->value);
|
|
|
+ $this->assertSame('2026-09-15', $downPayment->due_date->format('Y-m-d'));
|
|
|
+
|
|
|
+ $totalInstallments = $installments->where('type', 'total');
|
|
|
+
|
|
|
+ $this->assertCount(13, $totalInstallments);
|
|
|
+ // (5843 - 500) / 13 = 411.00
|
|
|
+ $this->assertEquals(411.00, $totalInstallments->first()->value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_without_down_payment_total_installments_use_full_value(): void
|
|
|
+ {
|
|
|
+ $student = Student::create([
|
|
|
+ 'name' => 'Aluno Sem Entrada',
|
|
|
+ 'unit_id' => $this->unit->id,
|
|
|
+ 'status' => 'active',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $contract = $this->service->create([
|
|
|
+ 'student_id' => $student->id,
|
|
|
+ 'unit_id' => $this->unit->id,
|
|
|
+ 'status' => 'pending',
|
|
|
+ 'total_value' => 1300.00,
|
|
|
+ 'total_installments' => 13,
|
|
|
+ 'total_due_date' => '2026-10-10',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $installments = StudentContractInstallment::where('student_contract_id', $contract->id)->get();
|
|
|
+
|
|
|
+ $this->assertNull($installments->firstWhere('type', 'down_payment'));
|
|
|
+
|
|
|
+ $totalInstallments = $installments->where('type', 'total');
|
|
|
+
|
|
|
+ $this->assertCount(13, $totalInstallments);
|
|
|
+ $this->assertEquals(100.00, $totalInstallments->first()->value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function makeUnit(): Unit
|
|
|
+ {
|
|
|
+ $country = Country::create(['name' => 'Brasil', 'code' => 'BR']);
|
|
|
+ $state = State::create(['name' => 'Paraná', 'code' => 'PR', 'country_id' => $country->id]);
|
|
|
+ $city = City::create(['name' => 'Maringá', 'country_id' => $country->id, 'state_id' => $state->id]);
|
|
|
+
|
|
|
+ return Unit::create([
|
|
|
+ 'fantasy_name' => 'Unidade Teste',
|
|
|
+ 'social_reason' => 'Unidade Teste LTDA',
|
|
|
+ 'cnpj' => '00000000000191',
|
|
|
+ 'street' => 'Rua Teste',
|
|
|
+ 'neighborhood' => 'Centro',
|
|
|
+ 'postal_code' => '87000000',
|
|
|
+ 'city_id' => $city->id,
|
|
|
+ 'state_id' => $state->id,
|
|
|
+ 'email' => 'unidade@teste.com',
|
|
|
+ 'name_responsible' => 'Responsável Teste',
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|