153 lines
6.7 KiB
PHP
153 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\Product;
|
|
use App\Models\Customer;
|
|
use App\Models\Contract;
|
|
use App\Models\PaymentTemplate;
|
|
use App\Models\PaymentSchedule;
|
|
use App\Models\PaymentScheduleItem;
|
|
use App\Models\Payment;
|
|
use App\Models\Appendix;
|
|
use App\Models\Settlement;
|
|
use App\Enums\ProductType;
|
|
use App\Enums\PaymentType;
|
|
use Illuminate\Database\Seeder;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class TestDataSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
DB::transaction(function () {
|
|
// 1. PROJECT
|
|
$sth03 = Project::factory()->create(['code' => 'STH03', 'name' => 'Khu Riverside STH03']);
|
|
$diamond = Project::factory()->create(['code' => 'DIA21', 'name' => 'Diamond Luxury Suites']);
|
|
|
|
// 2. PAYMENT TEMPLATES
|
|
$templateStandard = PaymentTemplate::create([
|
|
'project_id' => $sth03->id,
|
|
'name' => 'Thanh toán chuẩn STH03',
|
|
'is_default' => true
|
|
]);
|
|
|
|
$this->createTemplateItems($templateStandard);
|
|
|
|
// 3. PRODUCTS
|
|
$this->seedProducts($sth03, $diamond);
|
|
|
|
// 4. CUSTOMERS
|
|
$customers = Customer::factory(15)->create();
|
|
|
|
// 5. CASE STUDY: LỊCH SỬ CHUYỂN NHƯỢNG
|
|
$this->seedTransferHistory($sth03, $customers);
|
|
|
|
// 6. CASE STUDY: DÒNG TIỀN PHỨC TẠP
|
|
$this->seedComplexPayments($sth03, $customers, $templateStandard);
|
|
});
|
|
}
|
|
|
|
private function createTemplateItems($template)
|
|
{
|
|
PaymentScheduleItem::create(['template_id' => $template->id, 'installment_no' => 1, 'percentage' => 30, 'days_after_signing' => 7, 'type' => PaymentType::QSDD]);
|
|
PaymentScheduleItem::create(['template_id' => $template->id, 'installment_no' => 2, 'percentage' => 40, 'days_after_previous' => 60, 'type' => PaymentType::MONG]);
|
|
PaymentScheduleItem::create(['template_id' => $template->id, 'installment_no' => 3, 'percentage' => 30, 'days_after_previous' => 90, 'type' => PaymentType::THAN]);
|
|
}
|
|
|
|
private function seedProducts($project1, $project2)
|
|
{
|
|
for ($i = 1; $i <= 5; $i++) {
|
|
Product::create([
|
|
'project_id' => $project1->id,
|
|
'product_type' => ProductType::LAND,
|
|
'code' => $project1->code . '.' . sprintf('%02d', $i),
|
|
'area' => 100 + $i,
|
|
'price_per_unit' => 50000000,
|
|
'total_price' => (100 + $i) * 50000000,
|
|
'qsdd_value' => ((100 + $i) * 50000000) * 0.4,
|
|
'adjacent_road' => 'Đường 16m',
|
|
'frontage_count' => 1,
|
|
'infrastructure_status' => [
|
|
'dien' => ['status' => 'Hoàn thiện', 'child' => ['tram_bien_ap' => 'Đã nghiệm thu']],
|
|
'nuoc' => ['status' => 'Đang thi công']
|
|
],
|
|
'custom_data' => ['so_lo' => 'Lô '.$i, 'huong' => 'Đông Nam'],
|
|
'status' => 'Đang mở bán'
|
|
]);
|
|
}
|
|
|
|
for ($i = 1; $i <= 5; $i++) {
|
|
Product::create([
|
|
'project_id' => $project2->id,
|
|
'product_type' => ProductType::APARTMENT,
|
|
'code' => $project2->code . '-P' . sprintf('%03d', $i),
|
|
'area' => 75,
|
|
'price_per_unit' => 40000000,
|
|
'total_price' => 75 * 40000000,
|
|
'infrastructure_status' => ['thang_may' => 'Schindler', 'pccc' => 'Đạt chuẩn'],
|
|
'custom_data' => ['block' => 'A', 'floor' => 10 + $i, 'view' => 'City View'],
|
|
'status' => 'Đang mở bán'
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function seedTransferHistory($project, $customers)
|
|
{
|
|
$product = Product::where('code', 'STH03.01')->first();
|
|
|
|
$c1 = Contract::create([
|
|
'product_id' => $product->id, 'transfer_order' => 1, 'contract_type' => 'HĐMB', 'contract_number' => 'HĐMB-GOC-88',
|
|
'signing_date' => Carbon::now()->subYears(2), 'total_value' => $product->total_price, 'paid_amount' => $product->total_price, 'status' => 'Đã chuyển nhượng'
|
|
]);
|
|
$customers[0]->contracts()->attach($c1->id, ['role' => 'CHỦ CŨ', 'transfer_order' => 1]);
|
|
|
|
// SỬA TỪ customData THÀNH custom_data
|
|
Appendix::create([
|
|
'contract_id' => $c1->id, 'product_id' => $product->id, 'type' => 'Thay đổi diện tích', 'apply_from_order' => 1, 'signing_date' => Carbon::now()->subYears(1),
|
|
'custom_data' => ['area_new' => 105]
|
|
]);
|
|
|
|
$c2 = Contract::create([
|
|
'product_id' => $product->id, 'transfer_order' => 0, 'contract_type' => 'VBCN', 'contract_number' => 'VBCN-F1-99',
|
|
'signing_date' => Carbon::now(), 'total_value' => $product->total_price * 1.15, 'paid_amount' => 500000000, 'status' => 'Đang hiệu lực'
|
|
]);
|
|
$customers[1]->contracts()->attach($c2->id, ['role' => 'CHỦ SỞ HỮU', 'transfer_order' => 0]);
|
|
|
|
$product->update(['status' => 'Đã bán']);
|
|
}
|
|
|
|
private function seedComplexPayments($project, $customers, $template)
|
|
{
|
|
$product = Product::where('code', 'STH03.02')->first();
|
|
$contract = Contract::create([
|
|
'product_id' => $product->id, 'transfer_order' => 0, 'contract_type' => 'HĐMB', 'contract_number' => 'HD-PAY-DEBUG',
|
|
'signing_date' => Carbon::now()->subDays(10), 'total_value' => $product->total_price, 'status' => 'Đang hiệu lực'
|
|
]);
|
|
$customers[5]->contracts()->attach($contract->id, ['role' => 'CHỦ SỞ HỮU', 'transfer_order' => 0]);
|
|
|
|
$schedule = PaymentSchedule::create(['contract_id' => $contract->id, 'template_id' => $template->id]);
|
|
$items = $template->items;
|
|
foreach($items as $item) {
|
|
$si = PaymentScheduleItem::create([
|
|
'schedule_id' => $schedule->id, 'installment_no' => $item->installment_no, 'percentage' => $item->percentage,
|
|
'amount' => $contract->total_value * ($item->percentage / 100), 'type' => $item->type, 'due_date' => Carbon::now()->addDays(30 * $item->installment_no)
|
|
]);
|
|
|
|
if ($item->installment_no == 1) {
|
|
$required = $si->amount;
|
|
$paid = $required * 1.2;
|
|
Payment::create(['contract_id' => $contract->id, 'schedule_item_id' => $si->id, 'amount' => $paid, 'paid_date' => Carbon::now(), 'method' => 'Chuyển khoản']);
|
|
|
|
$contract->update([
|
|
'paid_amount' => $paid,
|
|
'excess_amount' => $paid - $required,
|
|
'remaining_amount' => $contract->total_value - $paid
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|