Xu ly giao dien San Pham
This commit is contained in:
106
tests/Feature/ContractFinanceFlowTest.php
Normal file
106
tests/Feature/ContractFinanceFlowTest.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Filament\Resources\Contracts\ContractResource;
|
||||
use App\Models\Contract;
|
||||
use App\Models\Customer;
|
||||
use App\Models\PaymentSchedule;
|
||||
use App\Models\PaymentScheduleItem;
|
||||
use App\Models\PaymentTemplate;
|
||||
use App\Models\Product;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->actingAs(User::factory()->create());
|
||||
});
|
||||
|
||||
it('can create a contract and automatically generate a payment schedule from template', function () {
|
||||
// 1. Chuẩn bị dữ liệu mẫu
|
||||
$project = Project::factory()->create(['code' => 'TEST-PROJ', 'name' => 'Dự án Kiểm thử']);
|
||||
$product = Product::factory()->create([
|
||||
'project_id' => $project->id,
|
||||
'code' => 'STH03.TEST',
|
||||
'total_price' => 1000000000, // 1 tỷ
|
||||
]);
|
||||
$customer = Customer::factory()->create(['full_name' => 'Khách Hàng Test']);
|
||||
|
||||
// 2. Tạo Mẫu thanh toán (3 đợt: 30% - 30% - 40%)
|
||||
$template = PaymentTemplate::create([
|
||||
'project_id' => $project->id,
|
||||
'name' => 'Mẫu thanh toán 30-30-40',
|
||||
'is_default' => true,
|
||||
]);
|
||||
|
||||
// Đợt 1: 30% ngay khi ký (0 ngày sau signing)
|
||||
PaymentScheduleItem::create([
|
||||
'template_id' => $template->id,
|
||||
'installment_no' => 1,
|
||||
'percentage' => 30,
|
||||
'days_after_signing' => 0,
|
||||
'type' => 'QSDD',
|
||||
]);
|
||||
|
||||
// Đợt 2: 30% sau 30 ngày kể từ đợt trước
|
||||
PaymentScheduleItem::create([
|
||||
'template_id' => $template->id,
|
||||
'installment_no' => 2,
|
||||
'percentage' => 30,
|
||||
'days_after_previous' => 30,
|
||||
'type' => 'MONG',
|
||||
]);
|
||||
|
||||
// 3. Thực hiện tạo Hợp đồng qua Livewire Page
|
||||
$signingDate = Carbon::now();
|
||||
|
||||
\Livewire\Livewire::test(\App\Filament\Resources\Contracts\Pages\CreateContract::class)
|
||||
->fillForm([
|
||||
'product_id' => $product->id,
|
||||
'contract_number' => 'HD-TEST-UUID',
|
||||
'contract_type' => 'HĐMB',
|
||||
'signing_date' => $signingDate->format('Y-m-d'),
|
||||
'total_value' => 1000000000,
|
||||
'payment_template_id' => $template->id, // Chọn mẫu thanh toán
|
||||
'customers' => [$customer->id], // Đồng sở hữu
|
||||
])
|
||||
->call('create')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
// 4. KIỂM TRA KẾT QUẢ TRONG DATABASE
|
||||
|
||||
// A. Hợp đồng phải tồn tại
|
||||
$contract = Contract::where('contract_number', 'HD-TEST-UUID')->first();
|
||||
expect($contract)->not->toBeNull();
|
||||
expect($contract->id)->toBeUuid(); // Kiểm tra khóa chính là UUID
|
||||
|
||||
// B. Kiểm tra bảng trung gian contract_customers (Phải có UUID)
|
||||
$this->assertDatabaseHas('contract_customers', [
|
||||
'contract_id' => $contract->id,
|
||||
'customer_id' => $customer->id,
|
||||
]);
|
||||
|
||||
$pivot = \DB::table('contract_customers')->where('contract_id', $contract->id)->first();
|
||||
expect($pivot->id)->toBeUuid(); // KIỂM TRA QUAN TRỌNG: ID bảng trung gian phải là UUID
|
||||
|
||||
// C. Kiểm tra Lịch trình thanh toán (Payment Schedule)
|
||||
$schedule = PaymentSchedule::where('contract_id', $contract->id)->first();
|
||||
expect($schedule)->not->toBeNull();
|
||||
expect($schedule->template_id)->toBe($template->id);
|
||||
|
||||
// D. Kiểm tra các đợt thanh toán con (Items)
|
||||
$scheduleItems = PaymentScheduleItem::where('schedule_id', $schedule->id)
|
||||
->orderBy('installment_no')
|
||||
->get();
|
||||
|
||||
expect($scheduleItems)->toHaveCount(2);
|
||||
|
||||
// Kiểm tra đợt 1 (30% = 300 triệu)
|
||||
expect((float)$scheduleItems[0]->amount)->toBe(300000000.0);
|
||||
expect($scheduleItems[0]->due_date->format('Y-m-d'))->toBe($signingDate->format('Y-m-d'));
|
||||
|
||||
// Kiểm tra đợt 2 (30% = 300 triệu, sau 30 ngày)
|
||||
expect((float)$scheduleItems[1]->amount)->toBe(300000000.0);
|
||||
expect($scheduleItems[1]->due_date->format('Y-m-d'))->toBe($signingDate->copy()->addDays(30)->format('Y-m-d'));
|
||||
});
|
||||
Reference in New Issue
Block a user