Đóng gói cuối tuần

This commit is contained in:
2026-04-18 04:46:01 +00:00
parent 761b34916b
commit 3cef1c40df
37 changed files with 1266 additions and 301 deletions

View File

@@ -12,9 +12,8 @@ class ProjectFactory extends Factory
public function definition(): array
{
return [
'name' => 'Khu đô thị HQLand ' . $this->faker->unique()->city(),
'type' => $this->faker->randomElement(['Khu đô thị', 'Chung cư', 'Đất nền phân lô']),
'address' => $this->faker->address(),
'code' => $this->faker->unique()->regexify('[A-Z]{3}[0-9]{2}'),
'name' => 'Khu đô thị ' . $this->faker->city(),
];
}
}

View File

@@ -1,16 +1,17 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('projects', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('code')->unique(); // STH03
$table->string('name');
$table->string('type');
$table->string('address')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('projects'); }
};
};

View File

@@ -1,22 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('products', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('project_id')->constrained('projects')->cascadeOnDelete();
$table->string('product_type');
$table->string('code')->unique();
$table->decimal('area', 10, 2);
$table->decimal('price_per_unit', 15, 2)->nullable();
$table->string('product_type'); // LAND, APARTMENT, SHOPHOUSE...
// === TRƯỜNG CHUNG ===
$table->string('code')->unique(); // Khu + Lô (STH03.01)
$table->decimal('area', 12, 2);
$table->decimal('price_per_unit', 15, 2);
$table->decimal('total_price', 15, 2);
$table->jsonb('custom_data')->nullable(); // Chứa thông tin tầng, view, hướng...
// === TÀI CHÍNH BỔ SUNG ===
$table->decimal('qsdd_value', 15, 2)->default(0);
$table->decimal('foundation_temp_value', 15, 2)->default(0);
$table->decimal('contract_temp_value', 15, 2)->default(0);
// === KỸ THUẬT & XÂY DỰNG ===
$table->string('adjacent_road')->nullable();
$table->integer('frontage_count')->nullable();
$table->integer('max_floors')->nullable();
$table->decimal('building_density', 5, 2)->nullable();
$table->string('construction_status')->nullable();
// === HẠ TẦNG (NESTED) ===
$table->text('infrastructure_raw_text')->nullable();
$table->jsonb('infrastructure_status')->nullable();
// === TRẠNG THÁI ===
$table->string('status')->default('Đang mở bán');
$table->string('red_book_status')->default('Chưa có dữ liệu');
// === LINH HOẠT ===
$table->jsonb('custom_data')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('products'); }
};
};

View File

@@ -1,7 +1,9 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('customers', function (Blueprint $table) {
@@ -10,10 +12,10 @@ return new class extends Migration {
$table->string('full_name');
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->jsonb('address')->nullable(); // Lưu cấu trúc: số nhà, phường, quận...
$table->jsonb('address')->nullable();
$table->date('dob')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('customers'); }
};
};

View File

@@ -1,7 +1,9 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('contracts', function (Blueprint $table) {
@@ -14,11 +16,11 @@ return new class extends Migration {
$table->string('status')->default('Đang hiệu lực');
$table->decimal('total_value', 15, 2);
$table->decimal('paid_amount', 15, 2)->default(0);
// Cột ảo tự động tính số tiền còn lại, dev không cần query tính toán
$table->decimal('remaining_amount', 15, 2)->virtualAs('total_value - paid_amount');
$table->decimal('remaining_amount', 15, 2)->default(0);
$table->decimal('excess_amount', 15, 2)->default(0); // Tiền dư từ đợt trước
$table->jsonb('metadata')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('contracts'); }
};
};

View File

@@ -1,19 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('appendices', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
$table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
$table->string('type');
$table->integer('apply_from_order')->default(0);
$table->date('signing_date')->nullable();
$table->jsonb('custom_data')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('appendices'); }
};

View File

@@ -1,20 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('settlements', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
$table->string('type'); // "MÓNG", "THÂN", "CP THI CÔNG"
$table->decimal('temp_value', 15, 2)->default(0);
$table->decimal('final_value', 15, 2)->default(0);
$table->decimal('difference', 15, 2)->virtualAs('final_value - temp_value');
$table->string('red_book_status')->nullable();
$table->date('issue_date')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('settlements'); }
};

View File

@@ -1,22 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
Schema::create('payments', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
$table->string('payment_type');
$table->integer('installment_no')->default(1);
$table->decimal('amount', 15, 2);
$table->date('due_date')->nullable();
$table->date('paid_date')->nullable();
$table->string('status')->default('PENDING'); // PENDING, PAID, OVERDUE, CANCELLED
$table->string('receipt_number')->nullable();
$table->jsonb('metadata')->nullable();
$table->timestamps();
});
}
public function down(): void { Schema::dropIfExists('payments'); }
};

View File

@@ -0,0 +1,76 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
// Mẫu thanh toán
Schema::create('payment_templates', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('project_id')->constrained('projects')->cascadeOnDelete();
$table->string('name');
$table->boolean('is_default')->default(false);
$table->timestamps();
});
// Lịch trình thanh toán
Schema::create('payment_schedules', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
$table->foreignUuid('template_id')->constrained('payment_templates')->cascadeOnDelete();
$table->timestamps();
});
// Chi tiết từng đợt thanh toán
Schema::create('payment_schedule_items', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('template_id')->nullable()->constrained('payment_templates')->cascadeOnDelete();
$table->foreignUuid('schedule_id')->nullable()->constrained('payment_schedules')->cascadeOnDelete();
$table->integer('installment_no');
$table->decimal('amount', 15, 2)->nullable();
$table->decimal('percentage', 5, 2)->nullable();
// --- LOGIC NGÀY ĐẾN HẠN ---
$table->integer('days_after_signing')->nullable(); // Cách 1: X ngày sau ngày ký
$table->integer('days_after_previous')->nullable(); // Cách 2: X ngày sau đợt trước
$table->date('due_date')->nullable(); // Cách 3: Ngày chính xác
$table->string('type'); // QSDD, MONG, THAN...
$table->timestamps();
});
// Phiếu thu thực tế
Schema::create('payments', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
$table->foreignUuid('schedule_item_id')->nullable()->constrained('payment_schedule_items')->nullOnDelete();
$table->decimal('amount', 15, 2);
$table->date('paid_date');
$table->string('receipt_number')->nullable();
$table->string('method')->default('Chuyển khoản');
$table->jsonb('metadata')->nullable();
$table->timestamps();
});
// Tiền phạt chậm nộp
Schema::create('payment_fines', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
$table->decimal('amount', 15, 2);
$table->string('reason');
$table->date('due_date');
$table->date('paid_date')->nullable();
$table->timestamps();
});
}
public function down(): void {
Schema::dropIfExists('payment_fines');
Schema::dropIfExists('payments');
Schema::dropIfExists('payment_schedule_items');
Schema::dropIfExists('payment_schedules');
Schema::dropIfExists('payment_templates');
}
};

View File

@@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void {
// Phụ lục hợp đồng
Schema::create('appendices', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete();
$table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
$table->string('type');
$table->integer('apply_from_order'); // Kế thừa từ CN số mấy
$table->date('signing_date');
$table->jsonb('custom_data')->nullable();
$table->timestamps();
});
// Quyết toán & Sổ đỏ
Schema::create('settlements', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete();
$table->string('type'); // MONG, THAN, CP THI CONG
$table->decimal('temp_value', 15, 2);
$table->decimal('final_value', 15, 2);
$table->decimal('difference', 15, 2);
$table->string('red_book_status');
$table->date('issue_date')->nullable();
$table->timestamps();
});
}
public function down(): void {
Schema::dropIfExists('settlements');
Schema::dropIfExists('appendices');
}
};

View File

@@ -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,
]);
}
}

View 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
]);
}
}
}
}