Xu ly giao dien San Pham

This commit is contained in:
2026-04-19 23:50:21 +00:00
parent 10eac55520
commit 91ff4a5e4d
18 changed files with 744 additions and 196 deletions

View File

@@ -2,6 +2,8 @@
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Project;
use App\Models\Product;
use App\Models\Customer;
@@ -9,144 +11,124 @@ 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 Illuminate\Support\Facades\Hash;
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']);
// 1. Tạo Tài khoản Admin
User::updateOrCreate(
['email' => 'admin@phuongtc.com'],
[
'name' => 'chanphuong',
'password' => Hash::make('1Qazxsw2@!321'),
]
);
// 2. PAYMENT TEMPLATES
$templateStandard = PaymentTemplate::create([
'project_id' => $sth03->id,
'name' => 'Thanh toán chuẩn STH03',
'is_default' => true
]);
// 2. Tạo Dự án
$project = Project::updateOrCreate(
['code' => 'STH03'],
[
'name' => 'Khu đô thị Mỹ Gia - Gói 3',
'type' => 'Khu đô thị'
]
);
$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]
// 3. Tạo Mẫu thanh toán
$template = PaymentTemplate::create([
'project_id' => $project->id,
'name' => 'Mẫu chuẩn Đất nền 30-40-30'
]);
$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'
$project->update(['payment_template_id' => $template->id]);
// 4. Tạo các đợt mẫu
PaymentScheduleItem::create([
'template_id' => $template->id,
'installment_no' => 1,
'type' => PaymentType::MONG,
'percentage' => 30,
'days_after_signing' => 0,
]);
$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'
PaymentScheduleItem::create([
'template_id' => $template->id,
'installment_no' => 2,
'type' => PaymentType::THAN,
'percentage' => 40,
'days_after_previous' => 60,
]);
PaymentScheduleItem::create([
'template_id' => $template->id,
'installment_no' => 3,
'type' => PaymentType::OTHER,
'percentage' => 30,
'days_after_previous' => 90,
]);
$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)
]);
// 5. Tạo Sản phẩm
Product::create([
'project_id' => $project->id,
'product_type' => ProductType::LAND,
'code' => 'STH03.01',
'area' => 100,
'price_per_unit' => 25000000,
'total_price' => 2500000000,
'status' => 'Đang mở bán',
]);
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
]);
}
}
$productSold = Product::create([
'project_id' => $project->id,
'product_type' => ProductType::LAND,
'code' => 'STH03.02',
'area' => 100,
'price_per_unit' => 25000000,
'total_price' => 2500000000,
'status' => 'Đã bán',
]);
// 6. Khách hàng & Hợp đồng chuyển nhượng
// Sử dụng cột cmnd_cccd thay cho id_number
$customerA = Customer::create(['full_name' => 'Nguyễn Văn A', 'phone' => '0901234567', 'cmnd_cccd' => '123456789']);
$customerB = Customer::create(['full_name' => 'Trần Thị B', 'phone' => '0907654321', 'cmnd_cccd' => '987654321']);
// F1
$contractF1 = Contract::create([
'product_id' => $productSold->id,
'contract_number' => 'HĐMB-STH03.02-F1',
'signing_date' => Carbon::now()->subMonths(6),
'total_value' => 2500000000,
'transfer_order' => 1,
'status' => 'Đã thanh lý (Chuyển nhượng)',
]);
$contractF1->customers()->attach($customerA->id);
// F2
$contractF2 = Contract::create([
'product_id' => $productSold->id,
'contract_number' => 'HĐMB-STH03.02-F2',
'signing_date' => Carbon::now()->subDays(10),
'total_value' => 2600000000,
'transfer_order' => 2,
'status' => 'Đang hiệu lực',
]);
$contractF2->customers()->attach($customerB->id);
$schedule = PaymentSchedule::create([
'contract_id' => $contractF2->id,
'template_id' => $template->id,
]);
PaymentScheduleItem::create([
'schedule_id' => $schedule->id,
'installment_no' => 1,
'type' => PaymentType::MONG,
'percentage' => 30,
'amount' => 780000000,
'due_date' => Carbon::now()->subDays(10),
]);
}
}