Đóng gói cuối tuần
This commit is contained in:
@@ -3,10 +3,6 @@
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Project;
|
||||
use App\Models\Product;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Contract;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@@ -15,95 +11,21 @@ class DatabaseSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
// 1. Xóa sạch dữ liệu cũ
|
||||
// 1. Dọn dẹp an toàn
|
||||
Schema::disableForeignKeyConstraints();
|
||||
Contract::query()->delete();
|
||||
Customer::query()->delete();
|
||||
Product::query()->delete();
|
||||
Project::query()->delete();
|
||||
User::query()->delete();
|
||||
Schema::enableForeignKeyConstraints();
|
||||
|
||||
// 2. Tạo tài khoản Admin mặc định
|
||||
User::updateOrCreate(
|
||||
['email' => 'admin@phuongtc.com'],
|
||||
[
|
||||
'name' => 'Administrator',
|
||||
'password' => Hash::make('1Qazxsw2@!321'),
|
||||
]
|
||||
);
|
||||
|
||||
// 3. Tạo 1 Dự án cố định và 1 Sản phẩm cố định STH-6535 để test
|
||||
$specialProject = Project::factory()->create(['name' => 'Dự án HQLand Center']);
|
||||
$specialProduct = Product::factory()->create([
|
||||
'project_id' => $specialProject->id,
|
||||
'code' => 'STH-6535',
|
||||
'status' => 'Đang mở bán'
|
||||
// 2. Tạo tài khoản quản trị Admin
|
||||
User::create([
|
||||
'name' => 'chanphuong',
|
||||
'email' => 'admin@phuongtc.com',
|
||||
'password' => Hash::make('1Qazxsw2@!321'),
|
||||
]);
|
||||
|
||||
// 4. Tạo thêm các Dự án và Sản phẩm ngẫu nhiên khác
|
||||
Project::factory(2)
|
||||
->has(Product::factory()->count(15), 'products')
|
||||
->create();
|
||||
|
||||
// 5. Tạo 20 Khách hàng
|
||||
Customer::factory(20)->create();
|
||||
|
||||
// 6. Tạo dữ liệu Lịch sử chuyển nhượng cho 10 sản phẩm (bao gồm cả STH-6535)
|
||||
$transferProducts = Product::limit(10)->get();
|
||||
$allCustomers = Customer::all();
|
||||
|
||||
foreach ($transferProducts as $product) {
|
||||
$baseValue = $product->total_price;
|
||||
|
||||
// --- Lần 1: Hợp đồng gốc (Mua từ CĐT) ---
|
||||
$contract1 = Contract::factory()->create([
|
||||
'product_id' => $product->id,
|
||||
'contract_type' => 'HĐMB',
|
||||
'transfer_order' => 1,
|
||||
'total_value' => $baseValue,
|
||||
'status' => 'Đã chuyển nhượng',
|
||||
'signing_date' => now()->subYears(2),
|
||||
]);
|
||||
$allCustomers->random()->contracts()->attach($contract1->id, ['role' => 'CHỦ CŨ', 'transfer_order' => 1]);
|
||||
|
||||
// --- Lần 2: Chuyển nhượng F1 ---
|
||||
$valueF1 = $baseValue * 1.1;
|
||||
$contract2 = Contract::factory()->create([
|
||||
'product_id' => $product->id,
|
||||
'contract_type' => 'VBCN',
|
||||
'transfer_order' => 2,
|
||||
'total_value' => $valueF1,
|
||||
'status' => 'Đã chuyển nhượng',
|
||||
'signing_date' => now()->subYear(),
|
||||
]);
|
||||
$allCustomers->random()->contracts()->attach($contract2->id, ['role' => 'CHỦ CŨ', 'transfer_order' => 2]);
|
||||
|
||||
// --- Lần 3: Chủ hiện tại (Sở hữu cuối cùng) ---
|
||||
$valueFinal = $valueF1 * 1.1;
|
||||
$contract3 = Contract::factory()->create([
|
||||
'product_id' => $product->id,
|
||||
'contract_type' => 'VBCN',
|
||||
'transfer_order' => 0,
|
||||
'total_value' => $valueFinal,
|
||||
'status' => 'Đang hiệu lực',
|
||||
'signing_date' => now(),
|
||||
]);
|
||||
$allCustomers->random()->contracts()->attach($contract3->id, ['role' => 'CHỦ SỞ HỮU', 'transfer_order' => 0]);
|
||||
|
||||
// Cập nhật trạng thái sản phẩm cuối cùng
|
||||
$product->update(['status' => 'Đã bán', 'total_price' => $valueFinal]);
|
||||
}
|
||||
|
||||
// 7. Tạo thêm 5 hợp đồng lẻ cho các sản phẩm còn lại để đa dạng hóa
|
||||
$remainingProducts = Product::where('status', 'Đang mở bán')->inRandomOrder()->limit(5)->get();
|
||||
foreach ($remainingProducts as $product) {
|
||||
$contract = Contract::factory()->create([
|
||||
'product_id' => $product->id,
|
||||
'transfer_order' => 0,
|
||||
'total_value' => $product->total_price,
|
||||
]);
|
||||
$allCustomers->random()->contracts()->attach($contract->id, ['role' => 'CHỦ SỞ HỮU', 'transfer_order' => 0]);
|
||||
$product->update(['status' => 'Đã bán']);
|
||||
}
|
||||
// 3. Gọi bộ nạp dữ liệu Test chuyên sâu
|
||||
$this->call([
|
||||
TestDataSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
152
database/seeders/TestDataSeeder.php
Normal file
152
database/seeders/TestDataSeeder.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?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
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user