Hoan thien core finance v2

This commit is contained in:
2026-04-25 04:04:14 +00:00
parent 86216ef872
commit 002c9a8b99
39 changed files with 1308 additions and 89 deletions

124
AGENTS.md
View File

@@ -120,9 +120,13 @@
**Quan hệ:**
- `product()`, `customers()` (belongsToMany qua contract_customers)
- `paymentTemplate()` (belongsTo PaymentTemplate)
- `appendices()`, `paymentSchedule()`, `scheduleItems()` (HasManyThrough)
- `payments()`, `paymentFines()`
**Accessor:**
- `final_value`: Giá trị sau chiết khấu (tính từ `DiscountEngine`)
**Filament Resources:**
- `ContractResource``ContractForm` + `ContractsTable`
- Action "Tạo lịch TT" trong Table (gọi `ContractScheduleService`)
@@ -159,8 +163,14 @@
**Filament Resources:**
- `PaymentResource``PaymentForm` + `PaymentsTable`
- Form chọn Contract → chọn Đợt thanh toán (cascade)
- Table có filter theo phương thức và ngày thu
- Form chọn Contract → chọn Đợt thanh toán (cascade)
- Validation số tiền không vượt quá công nợ đợt / công nợ HĐ
- Table có filter theo phương thức và ngày thu
- Cột đối soát: Đủ / Thiếu / Thừa (tính tổng payments của đợt)
- Cột còn thiếu tiền theo đợt
- `PaymentFineResource` → Quản lý tiền phạt chậm thanh toán
- `AppendixResource` → Quản lý phụ lục hợp đồng
- `SettlementResource` → Quản lý quyết toán & sổ đỏ
---
@@ -177,6 +187,11 @@
- Parse ngày tháng Excel (số serial hoặc chuỗi)
- Tự động tạo mẫu Công ty + Ngườ đại diện (Công ty TNHH BĐS Thịnh Vượng)
### `contracts:generate-schedules {--force}`
- Tự động tạo lịch thanh toán cho các hợp đồng chưa có lịch
- Ưu tiên `contract.payment_template_id`, fallback lấy từ `product.project.paymentTemplate`
- Option `--force` để tạo lại lịch cho HĐ đã có schedule
### `import:contracts-complex {hopdong=hopdong.xlsx} {hdkh=Hd_kh.xlsx}`
- Logic "Bắc cầu" 2 file:
1. `hopdong.xlsx`: Dữ liệu tài chính (theo Số HĐMB)
@@ -204,21 +219,25 @@
- [x] PaymentResource (Form + Table)
- [x] Test: ContractFinanceFlowTest (PASS)
- [x] Cấu hình PHPUnit dùng PostgreSQL testing database
- [x] **Fix ContractForm:** `payment_template_id` đã lưu vào DB, tự động tạo lịch khi tạo HĐ mới
- [x] **PaymentForm validation:** Không cho phép thu quá công nợ đợt / HĐ
- [x] **PaymentsTable:** Thêm cột Loại đợt, Trạng thái đối soát, Còn thiếu
- [x] **Command generate schedule hàng loạt:** `php artisan contracts:generate-schedules`
- [x] **PaymentFine Resource:** Form + Table đầy đủ
- [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
### 5.2. Đang dở / Cần tiếp tục
- [ ] **ContractForm:** `payment_template_id` đang `dehydrated(false)` - chưa tự động tạo lịch khi tạo hợp đồng mới từ form (hiện chỉ có trong CreateContract page sau khi submit)
- [ ] **PaymentsTable:** Chưa có cột trạng thái đối soát (so sánh với schedule_item amount)
- [ ] **Module Chiết khấu (Discounts):** Chưa có engine tính toán tự động dựa trên `discount_details`
- [ ] **PaymentFine:** Model đã có nhưng chưa có Resource/Form
- [ ] **Appendix & Settlement:** Chưa có Filament Resources
- [ ] **Báo cáo:** Chưa có Dashboard thống kê
- [ ] **Tự động hóa lịch trình cho 139 HĐ:** Cần command hoặc action để generate schedule hàng loạt
- [x] **Dashboard thống kê:** Đã tạo `ContractStatsOverview` + `UpcomingPaymentsTable`
- [ ] **Notification:** Cảnh báo đợt thanh toán sắp đến hạn (chưa có hệ thống notification)
### 5.3. Vấn đề kỹ thuật cần xử lý
- [ ] `payment_template_id` trong ContractForm cần hook `afterCreate` hoặc đổi thành dehydrated + xử lý trong CreateContract
- [ ] PaymentsTable nên hiển thị `scheduleItem.type` trng thái đối soát
- [ ] ContractTable có thể thêm cột `paid_amount` / `remaining_amount` (đã có trong Resource nhưng chưa commit staged)
- [ ] Cần kiểm tra logic `updateOrCreate` trong ImportContractsComplex với nhiều khách hàng cùng 1 hợp đồng
### 5.3. Vấn đề kỹ thuật ĐÃ XỬ LÝ
- [x] ContractTable đã thêm cột `paid_amount` / `remaining_amount`, chuyển sang dùng `ContractsTable` Schemas
- [x] Logic `syncWithoutDetaching` trong ImportContractsComplex đảm bảo nhiều KH cùng 1 HĐ không bị ghi đè
- [x] Fix N+1 query ở `PaymentScheduleItem::getPaidAmountAttribute()` (dùng `relationLoaded`)
- [x] Fix PaymentForm validation khi edit (`instanceof Payment` thay vì truthy check)
- [x] Fix ContractForm `final_value_display` hiển thị được cả khi create (dùng `$get` state)
---
@@ -267,47 +286,48 @@ DB_HOST=127.0.0.1 php artisan migrate
---
## 8. DANH SÁCH FILE ĐÃ THAY ĐỔI (Git Status)
## 8. DANH SÁCH FILE MỚI / THAY ĐỔI TRONG PHIÊN NÀY
### Staged (Sẵn sàng commit)
- `HQLAND_PROJECT_BLUEPRINT.md`
- `analyze_contracts.php`, `analyze_excel.php`, `analyze_khachhang.php`
- `app/Console/Commands/ImportContractsComplex.php`
- `app/Console/Commands/ImportCustomersExcel.php`
- `app/Console/Commands/ImportProductsExcel.php`
- `app/Filament/Resources/Contracts/Schemas/ContractForm.php`
- `app/Filament/Resources/Contracts/Tables/ContractsTable.php`
- `app/Filament/Resources/Customers/CustomerResource.php`
- `app/Filament/Resources/Customers/Schemas/CustomerForm.php`
- `app/Filament/Resources/Customers/Tables/CustomersTable.php`
- `app/Filament/Resources/Products/Schemas/ProductForm.php`
- `app/Models/Contract.php`
- `app/Models/Customer.php`
- `composer.json`, `composer.lock`
- `database/migrations/2026_04_23_081206_update_customers_table_for_real_estate.php`
- `database/migrations/2026_04_23_094837_expand_contracts_table_for_finance.php`
- `tests/Feature/ContractFinanceFlowTest.php`
- `tests/Feature/ProductResourceTest.php`
### Migrations mới
- `database/migrations/2026_04_24_083000_add_payment_template_id_to_contracts.php`
### Unstaged (Đang chỉnh sửa, chưa xong)
- `.gitignore`
- `analyze_contracts.php`
- `app/Filament/Resources/Contracts/ContractResource.php` (thêm action Tạo lịch TT)
- `app/Filament/Resources/Contracts/Pages/CreateContract.php` (refactor dùng Service)
- `app/Filament/Resources/Contracts/Schemas/ContractForm.php`
- `app/Filament/Resources/Projects/ProjectResource.php` (refactor sang Schemas)
- `app/Filament/Resources/Projects/Schemas/ProjectForm.php`
- `app/Models/Contract.php` (booted logic tài chính)
- `app/Providers/AppServiceProvider.php` (đăng ký PaymentObserver)
- `composer.json` (xóa script tạo SQLite)
- `config/database.php` (default về pgsql)
- `database/factories/CustomerFactory.php`
- `phpunit.xml` (cấu hình PostgreSQL testing)
### Services mới
- `app/Services/DiscountEngine.php` - Tính toán chiết khấu
- `app/Console/Commands/GenerateContractSchedules.php` - Command tạo lịch hàng loạt
### Untracked (File mới chưa add)
- `app/Filament/Resources/Payments/` (PaymentResource, Form, Table, Pages)
- `app/Observers/PaymentObserver.php`
- `app/Services/ContractScheduleService.php`
### 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)
### Widgets mới
- `app/Filament/Widgets/ContractStatsOverview.php` - Dashboard tổng quan tài chính
- `app/Filament/Widgets/UpcomingPaymentsTable.php` - Danh sách đợt TT sắp đến hạn
### Models sửa đổi
- `app/Models/Contract.php` - Thêm `paymentTemplate()`, accessor `final_value`
- `app/Models/PaymentScheduleItem.php` - Thêm accessor `paid_amount`, `remaining_amount`
- `app/Models/User.php` - Thêm `FilamentUser` interface để user có quyền truy cập panel
### Forms/Tables sửa đổi
- `app/Filament/Resources/Contracts/ContractResource.php` - Fix action `EditAction` namespace (`Filament\Actions\EditAction`)
- `app/Filament/Resources/Contracts/Schemas/ContractForm.php` - Fix `payment_template_id`, thêm `final_value_display`
- `app/Filament/Resources/Contracts/Pages/CreateContract.php` - Refactor dùng `$contract->payment_template_id`
- `app/Filament/Resources/Payments/Schemas/PaymentForm.php` - Thêm validation amount + helper text công nợ
- `app/Filament/Resources/Payments/Tables/PaymentsTable.php` - Thêm cột Loại đợt, Đối soát, Còn thiếu
- `app/Filament/Resources/Payments/PaymentResource.php` - Thêm eager load `scheduleItem.payments`
### Config/Provider sửa đổi
- `app/Providers/Filament/AdminPanelProvider.php` - Đăng ký widgets mới
- `phpunit.xml` - Cấu hình PostgreSQL testing (DB_HOST, DB_DATABASE, etc.)
- `config/database.php` - Default `pgsql`
- `composer.json` - Xóa script tạo SQLite
---
## 9. FILE HỖ TRỢ CHUYỂN MÁY
- `NEXT_SESSION.md` - Checklist và hướng dẫn nhanh cho phiên làm việc tiếp theo
- `COMMIT_GUIDE.md` - Hướng dẫn commit toàn bộ thay đổi chưa commit
---