From 49aa20a634fa4e551d0b63d611a85cc1ae87f69b Mon Sep 17 00:00:00 2001 From: phuongtc Date: Tue, 28 Apr 2026 03:57:18 +0000 Subject: [PATCH] Hoan thien core finance v2 - Calculation Pipeline, Form Templates --- AGENTS.md | 40 +++ NEXT_SESSION.md | 161 +++------- .../Contracts/Schemas/ContractForm.php | 35 ++- .../FormTemplates/FormTemplateResource.php | 42 +++ .../Pages/CreateFormTemplate.php | 11 + .../FormTemplates/Pages/EditFormTemplate.php | 11 + .../FormTemplates/Pages/ListFormTemplates.php | 19 ++ .../Schemas/FormTemplateForm.php | 148 ++++++++++ .../Tables/FormTemplatesTable.php | 56 ++++ app/Models/Contract.php | 39 ++- app/Models/FormField.php | 23 ++ app/Models/FormPrintLog.php | 29 ++ app/Models/FormTemplate.php | 28 ++ .../Calculation/CalculationPipeline.php | 33 +++ .../Calculation/CalculationResult.php | 51 ++++ app/Services/Calculation/CalculationStep.php | 83 ++++++ .../Calculation/PriceCalculationService.php | 110 +++++++ app/Services/Calculation/RoundingRule.php | 21 ++ app/Services/Forms/MailMergeService.php | 169 +++++++++++ ...13900_add_calculation_log_to_contracts.php | 19 ++ ...28_020000_create_form_templates_tables.php | 52 ++++ prisma.md | 276 ------------------ prisma2.md | 241 --------------- promptdata.md | 221 -------------- 24 files changed, 1043 insertions(+), 875 deletions(-) create mode 100644 app/Filament/Resources/FormTemplates/FormTemplateResource.php create mode 100644 app/Filament/Resources/FormTemplates/Pages/CreateFormTemplate.php create mode 100644 app/Filament/Resources/FormTemplates/Pages/EditFormTemplate.php create mode 100644 app/Filament/Resources/FormTemplates/Pages/ListFormTemplates.php create mode 100644 app/Filament/Resources/FormTemplates/Schemas/FormTemplateForm.php create mode 100644 app/Filament/Resources/FormTemplates/Tables/FormTemplatesTable.php create mode 100644 app/Models/FormField.php create mode 100644 app/Models/FormPrintLog.php create mode 100644 app/Models/FormTemplate.php create mode 100644 app/Services/Calculation/CalculationPipeline.php create mode 100644 app/Services/Calculation/CalculationResult.php create mode 100644 app/Services/Calculation/CalculationStep.php create mode 100644 app/Services/Calculation/PriceCalculationService.php create mode 100644 app/Services/Calculation/RoundingRule.php create mode 100644 app/Services/Forms/MailMergeService.php create mode 100644 database/migrations/2026_04_28_013900_add_calculation_log_to_contracts.php create mode 100644 database/migrations/2026_04_28_020000_create_form_templates_tables.php delete mode 100644 prisma.md delete mode 100644 prisma2.md delete mode 100644 promptdata.md diff --git a/AGENTS.md b/AGENTS.md index ec67ded..c4c907b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -174,6 +174,39 @@ --- +### 3.5. Form Templates (Biểu mẫu in ấn) +**Kiến trúc:** Mail Merge Engine - Word-style template với merge fields. + +**Models:** `FormTemplate`, `FormField`, `FormPrintLog` + +**FormTemplate:** +- `name`, `code`, `target_model` (Contract/Product/Customer) +- `html_template`: Nội dung HTML với placeholder `{{ma_truong}}` +- `paper_size`: A4/A5/Letter + +**FormField (Merge Fields):** +- `code`: Tên biến trong template (ví dụ: `ten_khach_hang`) +- `source_type`: `db_column` | `db_relation` | `formula` | `input` | `static` +- `source_config`: JSON cấu hình (tên cột, công thức, relation path...) +- `format`: `text` | `number` | `currency` | `date` | `percent` +- `decimal_places`: Số chữ số thập phân + +**Cách hoạt động:** +1. Admin tạo template HTML, chèn `{{ten_khach_hang}}` +2. Định nghĩa FormField: `ten_khach_hang` lấy từ `contract.customers.0.full_name` +3. Khi in: `MailMergeService::render()` evaluate tất cả fields → thay vào template +4. Snapshot được lưu vào `FormPrintLog` + +**Filament Resource:** +- `FormTemplateResource` → CRUD biểu mẫu với Repeater fields + +**Services:** +- `MailMergeService::evaluateFields()` - Tính toán giá trị tất cả fields +- `MailMergeService::render()` - Render HTML cuối cùng +- `MailMergeService::savePrintLog()` - Lưu snapshot + rendered HTML + +--- + ## 4. CÁC COMMAND IMPORT DỮ LIỆU ### `import:products-excel {file=sanpham.xlsx}` @@ -227,6 +260,8 @@ - [x] **Appendix Resource:** Form + Table đầy đủ - [x] **Settlement Resource:** Form + Table đầy đủ - [x] **Discount Engine:** Tính toán tự động chiết khấu + hiển thị `final_value` trong ContractForm +- [x] **Calculation Pipeline:** Kiến trúc tính toán tường minh (Step-by-step) với làm tròn tại mỗi bước +- [x] **Form Templates:** Mail Merge Engine cho phiếu tính giá, HĐ, phụ lục - Admin tự tạo template ### 5.2. Đang dở / Cần tiếp tục - [x] **Dashboard thống kê:** Đã tạo `ContractStatsOverview` + `UpcomingPaymentsTable` @@ -290,15 +325,20 @@ DB_HOST=127.0.0.1 php artisan migrate ### Migrations mới - `database/migrations/2026_04_24_083000_add_payment_template_id_to_contracts.php` +- `database/migrations/2026_04_28_013900_add_calculation_log_to_contracts.php` +- `database/migrations/2026_04_28_020000_create_form_templates_tables.php` ### Services mới - `app/Services/DiscountEngine.php` - Tính toán chiết khấu +- `app/Services/Calculation/` - Calculation Pipeline (RoundingRule, CalculationStep, CalculationResult, CalculationPipeline, PriceCalculationService) +- `app/Services/Forms/MailMergeService.php` - Engine xử lý biểu mẫu in ấn - `app/Console/Commands/GenerateContractSchedules.php` - Command tạo lịch hàng loạt ### Filament Resources mới - `app/Filament/Resources/PaymentFines/` (Resource + Form + Table + Pages) - `app/Filament/Resources/Appendices/` (Resource + Form + Table + Pages) - `app/Filament/Resources/Settlements/` (Resource + Form + Table + Pages) +- `app/Filament/Resources/FormTemplates/` (Resource + Form + Table + Pages) ### Widgets mới - `app/Filament/Widgets/ContractStatsOverview.php` - Dashboard tổng quan tài chính diff --git a/NEXT_SESSION.md b/NEXT_SESSION.md index 4d01055..b441a6c 100644 --- a/NEXT_SESSION.md +++ b/NEXT_SESSION.md @@ -7,152 +7,75 @@ ## ⚠️ THÔNG BÁO QUAN TRỌNG -Có **26 file thay đổi CHƯA COMMIT**. Bạn cần commit hoặc stash trước khi chuyển máy, nếu không sẽ mất toàn bộ công việc vừa làm! +Có **rất nhiều file thay đổi CHƯA COMMIT**. Bạn cần commit trước khi chuyển máy! ```bash -# Cách 1: Commit ngay (KHUYẾN NGHỊ) git add -A -git commit -m "Hoan thien core finance v2" - -# Cách 2: Hoặc stash để commit sau -git stash -u +git commit -m "Hoan thien core finance v2 - Calculation Pipeline, Form Templates" +git push origin main ``` --- -## 1. NHỮNG GÌ VỪA HOÀN THÀNH (Phiên hiện tại) +## 1. NHỮNG GÌ VỪA HOÀN THÀNH -### ✅ Fix lỗi quan trọng -- **EditAction not found:** Đã sửa namespace `Filament\Actions\EditAction` (không phải `Filament\Tables\Actions\EditAction`) -- **User quyền truy cập:** Thêm `FilamentUser` interface vào `User.php` +### ✅ Kiến trúc mới: Calculation Pipeline +- Tính toán giá BĐS tường minh, step-by-step với làm tròn tại mỗi bước +- `RoundingRule`: NONE, UNIT (đồng), THOUSAND, MILLION +- `CalculationStep`: Định nghĩa từng bước (tên, công thức, làm tròn, ghi đè) +- `CalculationResult`: Lưu snapshot + price_sheet cho phiếu tính giá +- `PriceCalculationService`: Pipeline chuyên BĐS (QSDĐ + Móng → Subtotal → CK → Net → VAT → Total) +- `Contract::calculation_log`: JSONB lưu toàn bộ quá trình tính toán -### ✅ ContractForm + Tự động tạo lịch -- Migration mới: `payment_template_id` trong bảng `contracts` -- `payment_template_id` đã lưu vào DB, không còn `dehydrated(false)` -- Tự động tạo lịch thanh toán khi tạo HĐ mới +### ✅ Module mới: Form Templates (Biểu mẫu in ấn) +- **Mail Merge Engine:** Admin tự tạo template HTML, chèn `{{ma_truong}}` +- **FormField:** Định nghĩa nguồn dữ liệu (db_column, db_relation, formula, input, static) +- **FormPrintLog:** Lưu snapshot khi in +- **FormTemplateResource:** CRUD trong Filament với RichEditor WYSIWYG + Repeater fields +- Layout: 3 section xếp dọc (Thông tin → Trường dữ liệu → Nội dung mẫu in) -### ✅ PaymentForm Validation -- Số tiền thu không vượt quá công nợ đợt TT / công nợ HĐ -- Helper text hiển thị công nợ còn lại -- Fix lỗi khi edit payment (kiểm tra `instanceof Payment`) - -### ✅ PaymentsTable -- Thêm cột: Loại đợt, Trạng thái đối soát (Đủ/Thiếu/Thừa), Còn thiếu - -### ✅ ContractsTable -- Thêm cột: `paid_amount`, `remaining_amount` -- `ContractResource` giờ delegate về `ContractsTable` Schemas - -### ✅ Hiệu năng -- Fix N+1 query ở `PaymentScheduleItem::getPaidAmountAttribute()` (kiểm tra `relationLoaded`) - -### ✅ Command mới -- `php artisan contracts:generate-schedules {--force}` - Tạo lịch hàng loạt cho 139 HĐ - -### ✅ Resources mới -- `PaymentFineResource` - Quản lý tiền phạt -- `AppendixResource` - Quản lý phụ lục HĐ -- `SettlementResource` - Quản lý quyết toán & sổ đỏ - -### ✅ Discount Engine -- `DiscountEngine::calculate()` - Tính chiết khấu tự động -- Accessor `final_value` trong Contract model -- Hiển thị giá trị sau chiết khấu cả khi create và edit - -### ✅ Dashboard -- `ContractStatsOverview` - 5 chỉ số tài chính tổng quan -- `UpcomingPaymentsTable` - Danh sách đợt TT sắp đến hạn (30 ngày) +### ✅ Các fix trước đó +- EditAction namespace, User FilamentUser, ContractForm tạo lịch tự động +- Payment validation, PaymentsTable đối soát, ContractsTable công nợ +- PaymentFine/Appendix/Settlement Resources, Dashboard widgets --- -## 2. CẤU HÌNH DATABASE (QUAN TRỌNG) +## 2. CẤU HÌNH DATABASE -### Database chính (Production) -- **Connection:** pgsql -- **Host:** 127.0.0.1 (từ host machine) -- **Database:** laravel -- **Username/Password:** sail / password +### Chạy migrate trên production (NẾU CHƯA CHẠY) +```bash +DB_HOST=127.0.0.1 php artisan migrate --force +``` -### Database test -- **Database:** laravel_testing (đã tạo, migrations đã chạy) -- **Chạy test:** `DB_HOST=127.0.0.1 ./vendor/bin/pest` - -### Lệnh chạy Artisan -- `DB_HOST=127.0.0.1 php artisan tinker` -- `DB_HOST=127.0.0.1 php artisan migrate` (KHÔNG dùng `migrate:fresh`!) +Các migration quan trọng: +- `2026_04_24_083000_add_payment_template_id_to_contracts` +- `2026_04_28_013900_add_calculation_log_to_contracts` +- `2026_04_28_020000_create_form_templates_tables` --- -## 3. CÁC FILE CHƯA COMMIT - -### Modified (17 file) -``` -AGENTS.md -app/Console/Commands/ImportContractsComplex.php -app/Filament/Resources/Contracts/ContractResource.php -app/Filament/Resources/Contracts/Pages/CreateContract.php -app/Filament/Resources/Contracts/Schemas/ContractForm.php -app/Filament/Resources/Contracts/Tables/ContractsTable.php -app/Filament/Resources/Payments/PaymentResource.php -app/Filament/Resources/Payments/Schemas/PaymentForm.php -app/Filament/Resources/Payments/Tables/PaymentsTable.php -app/Models/Contract.php -app/Models/PaymentScheduleItem.php -app/Models/User.php -app/Providers/Filament/AdminPanelProvider.php -``` - -### Untracked mới (9 file/folder) -``` -app/Console/Commands/GenerateContractSchedules.php -app/Filament/Resources/Appendices/ -app/Filament/Resources/PaymentFines/ -app/Filament/Resources/Settlements/ -app/Filament/Widgets/ -app/Services/DiscountEngine.php -database/migrations/2026_04_24_083000_add_payment_template_id_to_contracts.php -tests/Feature/ContractResourceRenderTest.php -``` - ---- - -## 4. VIỆC CẦN LÀM TIẾP THEO (Checklist) - -### 🟡 Trung bình ưu tiên -- [ ] **Notification:** Cảnh báo đợt thanh toán sắp đến hạn (30/7/3 ngày) -- [ ] **Export Excel:** Xuất báo cáo công nợ khách hàng -- [ ] **Báo cáo theo Dự án:** Thống kê bán hàng, thanh toán theo dự án - -### 🟢 Thấp ưu tiên -- [ ] **Audit Log:** Lưu lịch sử sửa HĐ, thu tiền -- [ ] **Queue:** Generate schedules qua queue nếu >1000 HĐ -- [ ] **Email/SMS:** Tự động nhắc thanh toán -- [ ] **Advanced Filter:** Tìm HĐ theo khoảng giá trị, ngày ký - ---- - -## 5. CÂU LỆNH TEST QUAN TRỌNG +## 3. TEST ```bash -# Test toàn bộ -DB_HOST=127.0.0.1 ./vendor/bin/pest - -# Test cụ thể -DB_HOST=127.0.0.1 ./vendor/bin/pest --filter="ContractFinanceFlowTest" - -# Test render (kiểm tra không bị lỗi class not found) -DB_HOST=127.0.0.1 ./vendor/bin/pest --filter="ContractResourceRenderTest" +DB_HOST=127.0.0.1 ./vendor/bin/pest --filter="ContractFinanceFlowTest|ContractResourceRenderTest" ``` -**Kết quả hiện tại:** 9 tests passed, 0 failed. - --- -## 6. TÀI KHOẢN ĐĂNG NHẬP +## 4. VIỆC CẦN LÀM TIẾP THEO + +- [ ] **Notification:** Cảnh báo đợt thanh toán sắp đến hạn +- [ ] **Export Excel:** Báo cáo công nợ khách hàng +- [ ] **In ấn thực tế:** Tích hợp MailMergeService với action "In" trong ContractResource + +--- + +## 5. TÀI KHOẢN - **Email:** admin@phuongtc.com - **Password:** 1Qazxsw2@!321 --- -*Lưu ý: Commit ngay lập tức trước khi tắt máy hoặc chuyển sang máy khác!* +*Commit ngay lập tức trước khi tắt máy!* diff --git a/app/Filament/Resources/Contracts/Schemas/ContractForm.php b/app/Filament/Resources/Contracts/Schemas/ContractForm.php index 4d8e837..fa10db3 100644 --- a/app/Filament/Resources/Contracts/Schemas/ContractForm.php +++ b/app/Filament/Resources/Contracts/Schemas/ContractForm.php @@ -138,22 +138,33 @@ class ContractForm ->label('Bảng chi tiết chiết khấu (Dạng Key-Value)') ->columnSpanFull(), - Placeholder::make('final_value_display') - ->label('Giá trị sau chiết khấu') + Placeholder::make('price_sheet') + ->label('Phiếu tính giá') ->columnSpanFull() - ->content(function ($record, $get) { - $totalValue = $record ? (float) $record->total_value : (float) ($get('total_value') ?? 0); - $discountDetails = $record ? $record->discount_details : ($get('discount_details') ?? []); - - if ($totalValue <= 0) { - return new HtmlString("
Chưa có giá trị hợp đồng để tính chiết khấu.
"); + ->content(function ($record) { + if (! $record || ! $record->calculation_log) { + return new HtmlString("
Chưa có dữ liệu tính toán. Vui lòng lưu hợp đồng để tạo phiếu tính giá.
"); } - $result = \App\Services\DiscountEngine::calculate($totalValue, $discountDetails); - $final = number_format($result['final_value']); - $discount = number_format($result['discount_amount']); + $steps = $record->calculation_log['price_sheet'] ?? []; + if (empty($steps)) { + return '-'; + } - return new HtmlString("
{$final} VNĐ
Đã chiết khấu: {$discount} VNĐ
"); + $html = '
'; + $html .= ''; + $html .= ''; + + foreach ($steps as $step) { + $desc = $step['description']; + $value = number_format($step['value']); + $isOverride = $step['is_overridden'] ? ' (ghi đè)' : ''; + $style = str_contains(strtolower($desc), 'tổng') ? 'font-weight: bold; border-top: 1px solid #e5e7eb;' : ''; + $html .= ""; + } + + $html .= '
Diễn giảiGiá trị (VNĐ)
{$desc}{$isOverride}{$value}
'; + return new HtmlString($html); }), ]), diff --git a/app/Filament/Resources/FormTemplates/FormTemplateResource.php b/app/Filament/Resources/FormTemplates/FormTemplateResource.php new file mode 100644 index 0000000..0426b0a --- /dev/null +++ b/app/Filament/Resources/FormTemplates/FormTemplateResource.php @@ -0,0 +1,42 @@ +value; + protected static ?int $navigationSort = 10; + + protected static ?string $modelLabel = 'Biểu mẫu'; + protected static ?string $pluralModelLabel = 'Biểu mẫu in ấn'; + + public static function form(Schema $schema): Schema + { + return FormTemplateForm::configure($schema); + } + + public static function table(Table $table): Table + { + return FormTemplatesTable::configure($table); + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListFormTemplates::route('/'), + 'create' => Pages\CreateFormTemplate::route('/create'), + 'edit' => Pages\EditFormTemplate::route('/{record}/edit'), + ]; + } +} diff --git a/app/Filament/Resources/FormTemplates/Pages/CreateFormTemplate.php b/app/Filament/Resources/FormTemplates/Pages/CreateFormTemplate.php new file mode 100644 index 0000000..d669221 --- /dev/null +++ b/app/Filament/Resources/FormTemplates/Pages/CreateFormTemplate.php @@ -0,0 +1,11 @@ +components([ + // BLOCK 1: Thông tin cơ bản + Section::make('Thông tin biểu mẫu') + ->schema([ + Grid::make(3) + ->schema([ + TextInput::make('name') + ->label('Tên biểu mẫu') + ->required(), + + TextInput::make('code') + ->label('Mã biểu mẫu') + ->required() + ->unique(ignoreRecord: true), + + Select::make('target_model') + ->label('Áp dụng cho') + ->options([ + 'App\Models\Contract' => 'Hợp đồng', + 'App\Models\Product' => 'Sản phẩm', + 'App\Models\Customer' => 'Khách hàng', + ]) + ->required(), + ]), + + Grid::make(3) + ->schema([ + Select::make('paper_size') + ->label('Khổ giấy') + ->options([ + 'A4' => 'A4', + 'A5' => 'A5', + 'Letter' => 'Letter', + ]) + ->default('A4') + ->required(), + ]), + ]), + + // BLOCK 2: Danh sách trường dữ liệu + Section::make('Danh sách trường dữ liệu (Merge Fields)') + ->schema([ + Repeater::make('fields') + ->relationship('fields') + ->schema([ + Grid::make(3) + ->schema([ + TextInput::make('code') + ->label('Mã trường') + ->required() + ->placeholder('ten_khach_hang'), + + TextInput::make('label') + ->label('Tên hiển thị') + ->required() + ->placeholder('Tên khách hàng'), + + Select::make('source_type') + ->label('Nguồn dữ liệu') + ->options([ + 'db_column' => 'Cột trong DB', + 'db_relation' => 'Quan hệ (relation)', + 'formula' => 'Công thức tính toán', + 'input' => 'Nhập tay khi in', + 'static' => 'Giá trị cố định', + ]) + ->required() + ->live(), + ]), + + Grid::make(3) + ->schema([ + KeyValue::make('source_config') + ->label('Cấu hình nguồn') + ->keyLabel('Tham số') + ->valueLabel('Giá trị') + ->helperText(fn ($get) => match ($get('source_type')) { + 'db_column' => 'Ví dụ: column => land_value', + 'db_relation' => 'Ví dụ: relation => customers, column => full_name, index => 0', + 'formula' => 'Ví dụ: expression => land_value + foundation_value', + 'input' => 'Ví dụ: default => (giá trị mặc định)', + 'static' => 'Ví dụ: value => Hà Nội', + default => 'Nhập cấu hình phù hợp với loại nguồn dữ liệu', + }), + + Select::make('format') + ->label('Định dạng hiển thị') + ->options([ + 'text' => 'Văn bản', + 'number' => 'Số', + 'currency' => 'Tiền tệ (VNĐ)', + 'date' => 'Ngày tháng', + 'percent' => 'Phần trăm', + ]) + ->default('text'), + + TextInput::make('decimal_places') + ->label('Số thập phân') + ->numeric() + ->default(0) + ->visible(fn ($get) => in_array($get('format'), ['number', 'currency', 'percent'])), + ]), + + TextInput::make('display_order') + ->label('Thứ tự') + ->numeric() + ->default(0) + ->hidden(), + ]) + ->addActionLabel('Thêm trường dữ liệu') + ->reorderable() + ->orderColumn('display_order') + ->defaultItems(0) + ->collapsible() + ->columnSpanFull(), + ]), + + // BLOCK 3: Nội dung mẫu in - FULL WIDTH, TO RỘNG + Section::make('Nội dung mẫu in') + ->schema([ + RichEditor::make('html_template') + ->label('') + ->required() + ->placeholder('Soạn thảo nội dung biểu mẫu...') + ->helperText('Chèn trường dữ liệu bằng cú pháp {{ma_truong}}. Ví dụ: Tên khách hàng: {{ten_khach_hang}}') + ->columnSpanFull(), + ]), + ]); + } +} diff --git a/app/Filament/Resources/FormTemplates/Tables/FormTemplatesTable.php b/app/Filament/Resources/FormTemplates/Tables/FormTemplatesTable.php new file mode 100644 index 0000000..e076abf --- /dev/null +++ b/app/Filament/Resources/FormTemplates/Tables/FormTemplatesTable.php @@ -0,0 +1,56 @@ +columns([ + Tables\Columns\TextColumn::make('name') + ->label('Tên biểu mẫu') + ->searchable() + ->sortable(), + + Tables\Columns\TextColumn::make('code') + ->label('Mã') + ->searchable() + ->copyable(), + + Tables\Columns\TextColumn::make('target_model') + ->label('Áp dụng cho') + ->formatStateUsing(fn ($state) => match ($state) { + 'App\Models\Contract' => 'Hợp đồng', + 'App\Models\Product' => 'Sản phẩm', + 'App\Models\Customer' => 'Khách hàng', + default => $state, + }), + + Tables\Columns\TextColumn::make('paper_size') + ->label('Khổ giấy') + ->badge(), + + Tables\Columns\IconColumn::make('is_active') + ->label('Hoạt động') + ->boolean(), + + Tables\Columns\TextColumn::make('fields_count') + ->label('Số trường') + ->counts('fields'), + ]) + ->filters([ + Tables\Filters\SelectFilter::make('target_model') + ->label('Áp dụng cho') + ->options([ + 'App\Models\Contract' => 'Hợp đồng', + 'App\Models\Product' => 'Sản phẩm', + 'App\Models\Customer' => 'Khách hàng', + ]), + ]) + ->defaultSort('name'); + } +} diff --git a/app/Models/Contract.php b/app/Models/Contract.php index 69dca84..d3ddb36 100644 --- a/app/Models/Contract.php +++ b/app/Models/Contract.php @@ -16,6 +16,7 @@ class Contract extends Model protected $casts = [ 'metadata' => 'array', 'discount_details' => 'array', + 'calculation_log' => 'array', 'total_value' => 'decimal:2', 'land_value' => 'decimal:2', 'foundation_value' => 'decimal:2', @@ -79,16 +80,28 @@ class Contract extends Model } /** - * Giá trị sau chiết khấu. + * Giá trị sau chiết khấu (qua PriceCalculationService). */ public function getFinalValueAttribute(): float { - $result = \App\Services\DiscountEngine::calculate( - (float) $this->total_value, - $this->discount_details - ); + if ($this->calculation_log) { + return (float) ($this->calculation_log['final_values']['total_payment'] ?? 0); + } - return $result['final_value']; + // Fallback: tính nhanh nếu chưa có calculation_log + $result = \App\Services\Calculation\PriceCalculationService::calculateForContract($this); + return (float) ($result->get('total_payment') ?? 0); + } + + /** + * Lấy phiếu tính giá chi tiết. + */ + public function getPriceSheetAttribute(): ?array + { + if ($this->calculation_log) { + return $this->calculation_log['price_sheet'] ?? null; + } + return null; } protected static function booted() @@ -110,5 +123,19 @@ class Contract extends Model $contract->remaining_amount = (float) ($contract->total_value ?? 0) - (float) ($contract->paid_amount ?? 0); }); + + static::saved(function ($contract) { + // Tự động tính toán và lưu snapshot sau khi lưu + if ($contract->land_value || $contract->foundation_value) { + $result = \App\Services\Calculation\PriceCalculationService::calculateForContract($contract); + $contract->calculation_log = [ + 'steps' => $result->getSteps(), + 'final_values' => $result->getValues(), + 'price_sheet' => $result->toPriceSheet(), + 'calculated_at' => now()->toDateTimeString(), + ]; + $contract->saveQuietly(); + } + }); } } diff --git a/app/Models/FormField.php b/app/Models/FormField.php new file mode 100644 index 0000000..3016bc6 --- /dev/null +++ b/app/Models/FormField.php @@ -0,0 +1,23 @@ + 'array', + ]; + + public function template() + { + return $this->belongsTo(FormTemplate::class); + } +} diff --git a/app/Models/FormPrintLog.php b/app/Models/FormPrintLog.php new file mode 100644 index 0000000..7a02058 --- /dev/null +++ b/app/Models/FormPrintLog.php @@ -0,0 +1,29 @@ + 'array', + 'printed_at' => 'datetime', + ]; + + public function template() + { + return $this->belongsTo(FormTemplate::class); + } + + public function printedBy() + { + return $this->belongsTo(User::class, 'printed_by'); + } +} diff --git a/app/Models/FormTemplate.php b/app/Models/FormTemplate.php new file mode 100644 index 0000000..48289b6 --- /dev/null +++ b/app/Models/FormTemplate.php @@ -0,0 +1,28 @@ + 'boolean', + ]; + + public function fields() + { + return $this->hasMany(FormField::class, 'template_id')->orderBy('display_order'); + } + + public function printLogs() + { + return $this->hasMany(FormPrintLog::class, 'template_id'); + } +} diff --git a/app/Services/Calculation/CalculationPipeline.php b/app/Services/Calculation/CalculationPipeline.php new file mode 100644 index 0000000..5137ad7 --- /dev/null +++ b/app/Services/Calculation/CalculationPipeline.php @@ -0,0 +1,33 @@ +steps[] = $step; + return $this; + } + + public function execute(array $initialData): CalculationResult + { + $data = $initialData; + $result = new CalculationResult(); + + foreach ($this->steps as $step) { + $stepResult = $step->execute($data); + $result->addStep($stepResult); + $data[$step->outputKey()] = $stepResult['rounded_value']; + } + + return $result; + } + + public function getSteps(): array + { + return $this->steps; + } +} diff --git a/app/Services/Calculation/CalculationResult.php b/app/Services/Calculation/CalculationResult.php new file mode 100644 index 0000000..7b1af12 --- /dev/null +++ b/app/Services/Calculation/CalculationResult.php @@ -0,0 +1,51 @@ +steps[] = $stepResult; + $this->values[$stepResult['output_key']] = $stepResult['rounded_value']; + } + + public function get(string $key): ?int + { + return $this->values[$key] ?? null; + } + + public function getSteps(): array + { + return $this->steps; + } + + public function getValues(): array + { + return $this->values; + } + + public function toArray(): array + { + return [ + 'steps' => $this->steps, + 'final_values' => $this->values, + ]; + } + + public function toPriceSheet(): array + { + $sheet = []; + foreach ($this->steps as $step) { + $sheet[] = [ + 'description' => $step['name'], + 'value' => $step['rounded_value'], + 'is_overridden' => $step['is_overridden'], + ]; + } + return $sheet; + } +} diff --git a/app/Services/Calculation/CalculationStep.php b/app/Services/Calculation/CalculationStep.php new file mode 100644 index 0000000..c414646 --- /dev/null +++ b/app/Services/Calculation/CalculationStep.php @@ -0,0 +1,83 @@ +name = $name; + $this->outputKey = $outputKey; + $this->formula = $formula; + $this->roundingRule = $roundingRule; + $this->dependencies = $dependencies; + } + + public function name(): string + { + return $this->name; + } + + public function outputKey(): string + { + return $this->outputKey; + } + + public function dependencies(): array + { + return $this->dependencies; + } + + public function override(int $value): static + { + $this->overrideValue = $value; + $this->isOverridden = true; + return $this; + } + + public function isOverridden(): bool + { + return $this->isOverridden; + } + + public function execute(array $data): array + { + if ($this->isOverridden) { + return [ + 'name' => $this->name, + 'output_key' => $this->outputKey, + 'formula_raw' => null, + 'calculated_value' => null, + 'rounded_value' => $this->overrideValue, + 'is_overridden' => true, + 'dependencies' => $this->dependencies, + ]; + } + + $rawValue = call_user_func($this->formula, $data); + $roundedValue = $this->roundingRule->apply($rawValue); + + return [ + 'name' => $this->name, + 'output_key' => $this->outputKey, + 'formula_raw' => $rawValue, + 'calculated_value' => $rawValue, + 'rounded_value' => $roundedValue, + 'is_overridden' => false, + 'dependencies' => $this->dependencies, + ]; + } +} diff --git a/app/Services/Calculation/PriceCalculationService.php b/app/Services/Calculation/PriceCalculationService.php new file mode 100644 index 0000000..71db9d9 --- /dev/null +++ b/app/Services/Calculation/PriceCalculationService.php @@ -0,0 +1,110 @@ +addStep(new CalculationStep( + name: 'Giá trị QSDĐ', + outputKey: 'land_value', + formula: fn ($data) => (float) ($data['land_value'] ?? 0), + roundingRule: RoundingRule::UNIT, + dependencies: [] + )); + + $pipeline->addStep(new CalculationStep( + name: 'Giá trị Móng', + outputKey: 'foundation_value', + formula: fn ($data) => (float) ($data['foundation_value'] ?? 0), + roundingRule: RoundingRule::UNIT, + dependencies: [] + )); + + $pipeline->addStep(new CalculationStep( + name: 'Tổng giá trị trước chiết khấu', + outputKey: 'subtotal', + formula: fn ($data) => $data['land_value'] + $data['foundation_value'], + roundingRule: RoundingRule::UNIT, + dependencies: ['land_value', 'foundation_value'] + )); + + // Bước 2: Chiết khấu + $pipeline->addStep(new CalculationStep( + name: 'Chiết khấu', + outputKey: 'discount_amount', + formula: function ($data) { + $details = $data['discount_details'] ?? []; + if (!empty($details['total_amount'])) { + return (float) $details['total_amount']; + } + if (!empty($details['total_percentage'])) { + return $data['subtotal'] * ((float) $details['total_percentage'] / 100); + } + return 0; + }, + roundingRule: RoundingRule::UNIT, + dependencies: ['subtotal', 'discount_details'] + )); + + // Bước 3: Sau chiết khấu + $pipeline->addStep(new CalculationStep( + name: 'Giá trị sau chiết khấu', + outputKey: 'net_value', + formula: fn ($data) => $data['subtotal'] - $data['discount_amount'], + roundingRule: RoundingRule::UNIT, + dependencies: ['subtotal', 'discount_amount'] + )); + + // Bước 4: VAT (nếu có) + $pipeline->addStep(new CalculationStep( + name: 'Thuế VAT', + outputKey: 'vat_amount', + formula: function ($data) { + $vatRate = (float) ($data['vat_rate'] ?? 0); + return $data['net_value'] * ($vatRate / 100); + }, + roundingRule: RoundingRule::UNIT, + dependencies: ['net_value', 'vat_rate'] + )); + + // Bước 5: Tổng thanh toán + $pipeline->addStep(new CalculationStep( + name: 'Tổng thanh toán', + outputKey: 'total_payment', + formula: fn ($data) => $data['net_value'] + $data['vat_amount'], + roundingRule: RoundingRule::UNIT, + dependencies: ['net_value', 'vat_amount'] + )); + + return $pipeline; + } + + public static function calculateForContract(Contract $contract, array $overrides = []): CalculationResult + { + $pipeline = self::forContract($contract); + $data = [ + 'land_value' => (float) $contract->land_value, + 'foundation_value' => (float) $contract->foundation_value, + 'discount_details' => $contract->discount_details ?? [], + 'vat_rate' => (float) ($contract->metadata['vat_rate'] ?? 0), + ]; + + // Áp dụng ghi đè nếu có + if (!empty($overrides)) { + foreach ($pipeline->getSteps() as $step) { + if (isset($overrides[$step->outputKey()])) { + $step->override((int) $overrides[$step->outputKey()]); + } + } + } + + return $pipeline->execute($data); + } +} diff --git a/app/Services/Calculation/RoundingRule.php b/app/Services/Calculation/RoundingRule.php new file mode 100644 index 0000000..f65e7bd --- /dev/null +++ b/app/Services/Calculation/RoundingRule.php @@ -0,0 +1,21 @@ + (int) $value, + self::UNIT => (int) round($value), + self::THOUSAND => (int) (round($value / 1000) * 1000), + self::MILLION => (int) (round($value / 1000000) * 1000000), + }; + } +} diff --git a/app/Services/Forms/MailMergeService.php b/app/Services/Forms/MailMergeService.php new file mode 100644 index 0000000..5b71c27 --- /dev/null +++ b/app/Services/Forms/MailMergeService.php @@ -0,0 +1,169 @@ +fields as $field) { + $values[$field->code] = self::evaluateSingleField($field, $record, $values); + } + + return $values; + } + + protected static function evaluateSingleField($field, Model $record, array $evaluatedValues): mixed + { + $config = $field->source_config ?? []; + + return match ($field->source_type) { + 'db_column' => self::getDbColumnValue($record, $config['column'] ?? null), + 'db_relation' => self::getRelationValue($record, $config), + 'formula' => self::evaluateFormula($config['expression'] ?? '', $evaluatedValues), + 'input' => $config['default'] ?? '', + 'static' => $config['value'] ?? '', + default => '', + }; + } + + protected static function getDbColumnValue(Model $record, ?string $column): mixed + { + if (! $column) return ''; + return $record->{$column} ?? ''; + } + + protected static function getRelationValue(Model $record, array $config): mixed + { + $relation = $config['relation'] ?? null; + $column = $config['column'] ?? null; + $index = $config['index'] ?? null; + + if (! $relation || ! $column) return ''; + + $related = $record->{$relation}; + + if (is_null($related)) return ''; + + if ($related instanceof \Illuminate\Database\Eloquent\Collection) { + if ($index !== null) { + $item = $related->skip($index)->first(); + return $item?->{$column} ?? ''; + } + return $related->pluck($column)->implode(', '); + } + + return $related->{$column} ?? ''; + } + + protected static function evaluateFormula(string $expression, array $values): float + { + if (empty($expression)) return 0; + + // Thay thế tên biến bằng giá trị + $evalExpression = $expression; + foreach ($values as $key => $value) { + if (is_numeric($value)) { + $evalExpression = str_replace($key, (float) $value, $evalExpression); + } + } + + // Chỉ cho phép số và các phép toán cơ bản + $evalExpression = preg_replace('/[^0-9.\+\-\*\/\(\)\s]/', '', $evalExpression); + + if (empty($evalExpression)) return 0; + + try { + // Eval an toàn với chỉ phép toán + $result = self::safeEval($evalExpression); + return (float) $result; + } catch (\Throwable $e) { + return 0; + } + } + + protected static function safeEval(string $expression): float + { + // Loại bỏ các hàm nguy hiểm, chỉ giữ phép toán + $expression = preg_replace('/[^0-9.\+\-\*\/\(\)\s]/', '', $expression); + + if (empty($expression) || preg_match('/[a-zA-Z]/', $expression)) { + throw new \InvalidArgumentException('Invalid expression'); + } + + // Dùng bc math nếu có, hoặc eval đơn giản + return (float) eval('return ' . $expression . ';'); + } + + /** + * Format value theo kiểu field. + */ + public static function formatValue(mixed $value, string $format, int $decimals = 0): string + { + return match ($format) { + 'number' => number_format((float) $value, $decimals, ',', '.'), + 'currency' => number_format((float) $value, 0, ',', '.') . ' VNĐ', + 'percent' => number_format((float) $value, $decimals, ',', '.') . '%', + 'date' => $value ? \Carbon\Carbon::parse($value)->format('d/m/Y') : '', + default => (string) $value, + }; + } + + /** + * Render template with evaluated values. + */ + public static function render(FormTemplate $template, Model $record): array + { + $rawValues = self::evaluateFields($template, $record); + + $formattedValues = []; + foreach ($template->fields as $field) { + $code = $field->code; + $rawValue = $rawValues[$code] ?? ''; + $formattedValues[$code] = self::formatValue( + $rawValue, + $field->format, + $field->decimal_places + ); + } + + $html = $template->html_template; + foreach ($formattedValues as $code => $value) { + $html = str_replace('{{' . $code . '}}', (string) $value, $html); + } + + return [ + 'html' => $html, + 'raw_values' => $rawValues, + 'formatted_values' => $formattedValues, + ]; + } + + /** + * Save print log with snapshot. + */ + public static function savePrintLog(FormTemplate $template, Model $record, array $renderResult, int $userId): FormPrintLog + { + return FormPrintLog::create([ + 'template_id' => $template->id, + 'target_model' => get_class($record), + 'target_id' => $record->id, + 'target_number' => $record->contract_number ?? $record->code ?? null, + 'snapshot_data' => [ + 'raw_values' => $renderResult['raw_values'], + 'formatted_values' => $renderResult['formatted_values'], + ], + 'rendered_html' => $renderResult['html'], + 'printed_by' => $userId, + 'printed_at' => now(), + ]); + } +} diff --git a/database/migrations/2026_04_28_013900_add_calculation_log_to_contracts.php b/database/migrations/2026_04_28_013900_add_calculation_log_to_contracts.php new file mode 100644 index 0000000..a7f631e --- /dev/null +++ b/database/migrations/2026_04_28_013900_add_calculation_log_to_contracts.php @@ -0,0 +1,19 @@ +jsonb('calculation_log')->nullable()->comment('Snapshot tính toán giá - phiếu tính giá'); + }); + } + + public function down(): void { + Schema::table('contracts', function (Blueprint $table) { + $table->dropColumn('calculation_log'); + }); + } +}; diff --git a/database/migrations/2026_04_28_020000_create_form_templates_tables.php b/database/migrations/2026_04_28_020000_create_form_templates_tables.php new file mode 100644 index 0000000..e15e73e --- /dev/null +++ b/database/migrations/2026_04_28_020000_create_form_templates_tables.php @@ -0,0 +1,52 @@ +uuid('id')->primary(); + $table->string('name'); + $table->string('code')->unique(); + $table->string('target_model'); // App\Models\Contract, App\Models\Product... + $table->text('html_template'); + $table->string('paper_size')->default('A4'); + $table->boolean('is_active')->default(true); + $table->timestamps(); + }); + + Schema::create('form_fields', function (Blueprint $table) { + $table->uuid('id')->primary(); + $table->foreignUuid('template_id')->constrained('form_templates')->cascadeOnDelete(); + $table->string('code'); // ten_bien trong {{ten_bien}} + $table->string('label'); + $table->string('source_type'); // db_column, db_relation, formula, input, static + $table->jsonb('source_config'); + $table->string('format')->default('text'); // text, number, currency, date, percent + $table->integer('decimal_places')->default(0); + $table->integer('display_order')->default(0); + $table->timestamps(); + }); + + Schema::create('form_print_logs', function (Blueprint $table) { + $table->uuid('id')->primary(); + $table->foreignUuid('template_id')->constrained('form_templates'); + $table->string('target_model'); + $table->uuid('target_id'); + $table->string('target_number')->nullable(); // contract_number, product_code... + $table->jsonb('snapshot_data'); // snapshot tat ca field values + $table->text('rendered_html'); + $table->foreignId('printed_by')->constrained('users'); + $table->timestamp('printed_at'); + $table->timestamps(); + }); + } + + public function down(): void { + Schema::dropIfExists('form_print_logs'); + Schema::dropIfExists('form_fields'); + Schema::dropIfExists('form_templates'); + } +}; diff --git a/prisma.md b/prisma.md deleted file mode 100644 index 45a7755..0000000 --- a/prisma.md +++ /dev/null @@ -1,276 +0,0 @@ -**Dành cho AI Agent & Lập trình** - -Dưới đây là **file Prisma Schema đầy đủ, thống nhất và chi tiết nhất** dựa trên tất cả các yêu cầu bạn đã nêu từ đầu đến giờ (bao gồm file Excel, logic chuyển nhượng, tình trạng hạ tầng nested, PaymentSchedule với template, dư/thiếu tiền, v.v.). - -prisma - -``` -// ============================================= -// PRISMA SCHEMA - HỆ THỐNG QUẢN LÝ BẤT ĐỘNG SẢN -// Phiên bản: 2.3 -// Ngày: 18/04/2026 -// Mục đích: Hoàn chỉnh, self-host, hỗ trợ nhiều loại sản phẩm, -// lịch sử chuyển nhượng, phụ lục kế thừa, -// tình trạng hạ tầng nested, và dòng tiền chi tiết. -// ============================================= - -datasource db { - provider = "postgresql" - url = env("DATABASE_URL") -} - -generator client { - provider = "prisma-client-js" -} - -// ============================================= -// 1. PROJECT (Khu / Dự án) -// ============================================= -model Project { - id String @id @default(uuid()) - code String @unique // ví dụ: STH03 - name String - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - - products Product[] - templates PaymentTemplate[] -} - -// ============================================= -// 2. PRODUCT (Sản phẩm - Đất nền, Căn hộ...) -// ============================================= -model Product { - id String @id @default(uuid()) - projectId String - - productType ProductType - - // === TRƯỜNG CHUNG TỪ FILE sanpham.xlsx === - code String @unique // Khu + Lô (STH03.01) - area Decimal - pricePerUnit Decimal - totalPrice Decimal - qsddValue Decimal - foundationTempValue Decimal - contractTempValue Decimal - adjacentRoad String? - frontageCount Int? - maxFloors Int? - buildingDensity Decimal? - constructionStatus String? - - // === TÌNH TRẠNG HẠ TẦNG (NESTED JSONB) === - infrastructureRawText String? // Giữ nguyên text gốc để backup - infrastructureStatus Json // Cấu trúc nested (hỗ trợ child-of-child) - - // === TRẠNG THÁI SỔ ĐỎ === - redBookStatus String @default("Chưa có dữ liệu") - - // === TRƯỜNG LINH HOẠT CHO SẢN PHẨM MỚI === - customData Json // Block, tầng, hướng, sổ hồng riêng, giấy phép XD... - - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - - project Project @relation(fields: [projectId], references: [id]) - contracts Contract[] - settlements Settlement[] -} - -// ============================================= -// ENUM LOẠI SẢN PHẨM -// ============================================= -enum ProductType { - LAND - APARTMENT - SHOPHOUSE - OFFICE - CONDOTEL - VILLA - // Thêm loại mới ở đây -} - -// ============================================= -// 3. CONTRACT & CHUYỂN NHƯỢNG (Giữ nguyên logic Excel) -// ============================================= -model Contract { - id String @id @default(uuid()) - productId String - transferOrder Int // 0 = KH HIỆN TẠI, 1 = HĐ GỐC, 2+ = VBCN - contractType String // HĐGV, HĐMB, VBCN - contractNumber String - signingDate DateTime - totalValue Decimal - paidAmount Decimal - remainingAmount Decimal - metadata Json? - - product Product @relation(fields: [productId], references: [id]) - customers ContractCustomer[] - appendices Appendix[] - payments Payment[] - schedule PaymentSchedule? - fines PaymentFine[] - - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt -} - -model Customer { - id String @id @default(uuid()) - cmndCccd String @unique - fullName String - phone String - email String? - address Json? - dob DateTime? - - contracts ContractCustomer[] -} - -model ContractCustomer { - id String @id @default(uuid()) - contractId String - customerId String - role String // "CHỦ SH 1", "CHỦ SH 2", "CHỦ SH 3" - transferOrder Int - - contract Contract @relation(fields: [contractId], references: [id]) - customer Customer @relation(fields: [customerId], references: [id]) -} - -// ============================================= -// 4. APPENDIX (Phụ lục) -// ============================================= -model Appendix { - id String @id @default(uuid()) - contractId String - productId String - type String - applyFromOrder Int // Kế thừa từ CN # - signingDate DateTime - customData Json? - - contract Contract @relation(fields: [contractId], references: [id]) -} - -// ============================================= -// 5. SETTLEMENT (Quyết toán & Sổ đỏ) -// ============================================= -model Settlement { - id String @id @default(uuid()) - productId String - type String // MÓNG, THÂN, CP THI CÔNG - tempValue Decimal - finalValue Decimal - difference Decimal - redBookStatus String - issueDate DateTime? - - product Product @relation(fields: [productId], references: [id]) -} - -// ============================================= -// 6. PAYMENT MODULE - DÒNG TIỀN (THEO YÊU CẦU) -// ============================================= -model PaymentTemplate { - id String @id @default(uuid()) - projectId String - name String // "Thanh toán chuẩn 30-30-40", "Trả một lần" - isDefault Boolean @default(false) - - project Project @relation(fields: [projectId], references: [id]) - items PaymentScheduleItem[] - schedules PaymentSchedule[] - - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt -} - -model PaymentScheduleItem { - id String @id @default(uuid()) - templateId String? - scheduleId String? - - installmentNo Int - amount Decimal? - percentage Decimal? // Ưu tiên % nếu có - - // Hai cách tính ngày đến hạn - daysAfterSigning Int? // Số ngày sau ngày ký hợp đồng - dueDate DateTime? // Ngày chính xác - - type PaymentType - - template PaymentTemplate? @relation(fields: [templateId], references: [id]) - schedule PaymentSchedule? @relation(fields: [scheduleId], references: [id]) - payments Payment[] - - createdAt DateTime @default(now()) -} - -model PaymentSchedule { - id String @id @default(uuid()) - contractId String - templateId String // Template đã chọn khi tạo hợp đồng - - contract Contract @relation(fields: [contractId], references: [id]) - template PaymentTemplate @relation(fields: [templateId], references: [id]) - items PaymentScheduleItem[] - - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt -} - -model Payment { - id String @id @default(uuid()) - contractId String - scheduleItemId String? - - amount Decimal - paidDate DateTime - receiptNumber String? - method String - metadata Json? - - contract Contract @relation(fields: [contractId], references: [id]) - scheduleItem PaymentScheduleItem? @relation(fields: [scheduleItemId], references: [id]) - - createdAt DateTime @default(now()) -} - -model PaymentFine { - id String @id @default(uuid()) - contractId String - amount Decimal - reason String - dueDate DateTime - paidDate DateTime? - - contract Contract @relation(fields: [contractId], references: [id]) -} - -// ============================================= -// ENUMS -// ============================================= -enum PaymentType { - QSDD - MONG - THAN - CHI_PHI_TC - CK - PHAT - OTHER -} -``` - ---- - -**Hướng dẫn sử dụng cho AI Agent / Lập trình:** - -- **Model chính**: Product, Contract, PaymentTemplate, PaymentScheduleItem, PaymentSchedule, Payment. -- **Logic tạo hợp đồng**: Khi tạo Contract → clone PaymentTemplate (mặc định hoặc do nhân viên chọn) thành PaymentSchedule + các PaymentScheduleItem. -- **Dư/thiếu tiền**: Xử lý ở tầng application logic (khi tạo Payment). -- **Tình trạng hạ tầng**: Sử dụng infrastructureStatus JSONB (nested). -- **Dynamic fields**: Sử dụng customData JSONB. diff --git a/prisma2.md b/prisma2.md deleted file mode 100644 index ace2aca..0000000 --- a/prisma2.md +++ /dev/null @@ -1,241 +0,0 @@ -// ============================================= -// PRISMA SCHEMA - HQLAND MANAGEMENT SYSTEM -// Phiên bản: 2.4 (Cập nhật từ Database thực tế) -// Ngày: 18/04/2026 -// Ghi chú: Đồng bộ hóa 100% với Migrations hiện tại. -// ============================================= - -datasource db { - provider = "postgresql" - url = env("DATABASE_URL") -} - -generator client { - provider = "prisma-client-js" -} - -// ============================================= -// 1. PROJECT (Dự án) -// ============================================= -model Project { - id String @id @default(uuid()) - code String @unique // ví dụ: STH03 - name String - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - products Product[] - templates PaymentTemplate[] -} - -// ============================================= -// 2. PRODUCT (Sản phẩm) -// ============================================= -model Product { - id String @id @default(uuid()) - projectId String @map("project_id") - - productType String @map("product_type") // LAND, APARTMENT... - - code String @unique - area Decimal @db.Decimal(12, 2) - pricePerUnit Decimal @db.Decimal(15, 2) @map("price_per_unit") - totalPrice Decimal @db.Decimal(15, 2) @map("total_price") - - // Giá trị tài chính bổ sung - qsddValue Decimal @default(0) @db.Decimal(15, 2) @map("qsdd_value") - foundationTempValue Decimal @default(0) @db.Decimal(15, 2) @map("foundation_temp_value") - contractTempValue Decimal @default(0) @db.Decimal(15, 2) @map("contract_temp_value") - - // Thông số kỹ thuật - adjacentRoad String? @map("adjacent_road") - frontageCount Int? @map("frontage_count") - maxFloors Int? @map("max_floors") - buildingDensity Decimal? @db.Decimal(5, 2) @map("building_density") - constructionStatus String? @map("construction_status") - - // Hạ tầng & Dữ liệu động - infrastructureRawText String? @map("infrastructure_raw_text") - infrastructureStatus Json? @map("infrastructure_status") - customData Json? @map("custom_data") - - // Trạng thái - status String @default("Đang mở bán") - redBookStatus String @default("Chưa có dữ liệu") @map("red_book_status") - - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - project Project @relation(fields: [projectId], references: [id]) - contracts Contract[] - settlements Settlement[] -} - -// ============================================= -// 3. CONTRACT & CUSTOMER -// ============================================= -model Contract { - id String @id @default(uuid()) - productId String @map("product_id") - transferOrder Int @default(0) @map("transfer_order") - contractType String @default("HĐMB") @map("contract_type") - contractNumber String @unique @map("contract_number") - signingDate DateTime? @map("signing_date") - status String @default("Đang hiệu lực") - - totalValue Decimal @db.Decimal(15, 2) @map("total_value") - paidAmount Decimal @default(0) @db.Decimal(15, 2) @map("paid_amount") - remainingAmount Decimal @default(0) @db.Decimal(15, 2) @map("remaining_amount") - excessAmount Decimal @default(0) @db.Decimal(15, 2) @map("excess_amount") // Tiền dư - - metadata Json? - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - product Product @relation(fields: [productId], references: [id]) - customers ContractCustomer[] - appendices Appendix[] - payments Payment[] - schedule PaymentSchedule? - fines PaymentFine[] -} - -model Customer { - id String @id @default(uuid()) - cmndCccd String @unique @map("cmnd_cccd") - fullName String @map("full_name") - phone String? - email String? - address Json? - dob DateTime? - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - contracts ContractCustomer[] -} - -model ContractCustomer { - id BigInt @id @default(autoincrement()) - contractId String @map("contract_id") - customerId String @map("customer_id") - role String @default("CHỦ SH 1") - transferOrder Int @default(0) @map("transfer_order") - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - contract Contract @relation(fields: [contractId], references: [id]) - customer Customer @relation(fields: [customerId], references: [id]) -} - -// ============================================= -// 4. FINANCE MODULE -// ============================================= -model PaymentTemplate { - id String @id @default(uuid()) - projectId String @map("project_id") - name String - isDefault Boolean @default(false) @map("is_default") - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - project Project @relation(fields: [projectId], references: [id]) - items PaymentScheduleItem[] - schedules PaymentSchedule[] -} - -model PaymentSchedule { - id String @id @default(uuid()) - contractId String @unique @map("contract_id") - templateId String @map("template_id") - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - contract Contract @relation(fields: [contractId], references: [id]) - template PaymentTemplate @relation(fields: [templateId], references: [id]) - items PaymentScheduleItem[] -} - -model PaymentScheduleItem { - id String @id @default(uuid()) - templateId String? @map("template_id") - scheduleId String? @map("schedule_id") - - installmentNo Int @map("installment_no") - amount Decimal? @db.Decimal(15, 2) - percentage Decimal? @db.Decimal(5, 2) - - // Logic ngày đến hạn - daysAfterSigning Int? @map("days_after_signing") - daysAfterPrevious Int? @map("days_after_previous") - dueDate DateTime? @map("due_date") - - type String // QSDD, MONG, THAN... - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - template PaymentTemplate? @relation(fields: [templateId], references: [id]) - schedule PaymentSchedule? @relation(fields: [scheduleId], references: [id]) - payments Payment[] -} - -model Payment { - id String @id @default(uuid()) - contractId String @map("contract_id") - scheduleItemId String? @map("schedule_item_id") - - amount Decimal @db.Decimal(15, 2) - paidDate DateTime @map("paid_date") - receiptNumber String? @map("receipt_number") - method String @default("Chuyển khoản") - metadata Json? - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - contract Contract @relation(fields: [contractId], references: [id]) - scheduleItem PaymentScheduleItem? @relation(fields: [scheduleItemId], references: [id]) -} - -model PaymentFine { - id String @id @default(uuid()) - contractId String @map("contract_id") - amount Decimal @db.Decimal(15, 2) - reason String - dueDate DateTime @map("due_date") - paidDate DateTime? @map("paid_date") - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - contract Contract @relation(fields: [contractId], references: [id]) -} - -// ============================================= -// 5. APPENDIX & SETTLEMENT -// ============================================= -model Appendix { - id String @id @default(uuid()) - contractId String @map("contract_id") - productId String @map("product_id") - type String - applyFromOrder Int @map("apply_from_order") - signingDate DateTime @map("signing_date") - customData Json? @map("custom_data") - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - contract Contract @relation(fields: [contractId], references: [id]) -} - -model Settlement { - id String @id @default(uuid()) - productId String @map("product_id") - type String // MONG, THAN, CP THI CONG - tempValue Decimal @db.Decimal(15, 2) @map("temp_value") - finalValue Decimal @db.Decimal(15, 2) @map("final_value") - difference Decimal @db.Decimal(15, 2) - redBookStatus String @map("red_book_status") - issueDate DateTime? @map("issue_date") - createdAt DateTime @default(now()) @map("created_at") - updatedAt DateTime @updatedAt @map("updated_at") - - product Product @relation(fields: [productId], references: [id]) -} diff --git a/promptdata.md b/promptdata.md deleted file mode 100644 index 0b37124..0000000 --- a/promptdata.md +++ /dev/null @@ -1,221 +0,0 @@ -**Đóng vai:** Bạn là chuyên gia kiến trúc phần mềm Laravel 13.5 và PostgreSQL. **Nhiệm vụ:** Xây dựng hệ thống Database Migration (8 bảng) cho dự án HQLand dựa trên thiết kế V3. - -**Thực hiện chính xác các bước sau:** - -**Bước 1:** Tìm trong `database/migrations/`. Xóa TẤT CẢ các file migration hiện có. (Bắt đầu từ một nền tảng sạch). - -**Bước 2: Tạo file `2025_01_01_000000_create_projects_table.php`** - -PHP - -``` -uuid('id')->primary(); - $table->string('name'); - $table->string('type'); - $table->string('address')->nullable(); - $table->timestamps(); - }); - } - public function down(): void { Schema::dropIfExists('projects'); } -}; -``` - -**Bước 3: Tạo file `2025_01_02_000000_create_products_table.php`** - -PHP - -``` -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->decimal('total_price', 15, 2); - $table->jsonb('custom_data')->nullable(); // Chứa thông tin tầng, view, hướng... - $table->string('status')->default('Đang mở bán'); - $table->string('red_book_status')->default('Chưa có dữ liệu'); - $table->timestamps(); - }); - } - public function down(): void { Schema::dropIfExists('products'); } -}; -``` - -**Bước 4: Tạo file `2025_01_03_000000_create_customers_table.php`** - -PHP - -``` -uuid('id')->primary(); - $table->string('cmnd_cccd')->unique(); - $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->date('dob')->nullable(); - $table->timestamps(); - }); - } - public function down(): void { Schema::dropIfExists('customers'); } -}; -``` - -**Bước 5: Tạo file `2025_01_04_000000_create_contracts_table.php`** - -PHP - -``` -uuid('id')->primary(); - $table->foreignUuid('product_id')->constrained('products')->cascadeOnDelete(); - $table->integer('transfer_order')->default(0); - $table->string('contract_type')->default('HĐMB'); - $table->string('contract_number')->unique(); - $table->date('signing_date')->nullable(); - $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->jsonb('metadata')->nullable(); - $table->timestamps(); - }); - } - public function down(): void { Schema::dropIfExists('contracts'); } -}; -``` - -**Bước 6: Tạo file `2025_01_05_000000_create_contract_customers_table.php` (Bảng trung gian)** - -PHP - -``` -uuid('id')->primary(); - $table->foreignUuid('contract_id')->constrained('contracts')->cascadeOnDelete(); - $table->foreignUuid('customer_id')->constrained('customers')->cascadeOnDelete(); - $table->string('role')->default('CHỦ SH 1'); // Đồng sở hữu - $table->integer('transfer_order')->default(0); - $table->timestamps(); - }); - } - public function down(): void { Schema::dropIfExists('contract_customers'); } -}; -``` - -**Bước 7: Tạo file `2025_01_06_000000_create_appendices_table.php` (Phụ lục)** - -PHP - -``` -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'); } -}; -``` - -**Bước 8: Tạo file `2025_01_07_000000_create_settlements_table.php` (Quyết toán)** - -PHP - -``` -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'); } -}; -``` - -**Bước 9: Tạo file `2025_01_08_000000_create_payments_table.php` (Thanh toán / Biên lai)** - -PHP - -``` -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'); } -}; -``` - -