From c7b1d08afa62926a66ca282b019a43c274ed66e7 Mon Sep 17 00:00:00 2001 From: phuongtc Date: Wed, 29 Apr 2026 07:03:44 +0000 Subject: [PATCH] Add Soft Delete UI (Restore/ForceDelete) + Update docs (AGENTS, ASSESSMENT, WORKFLOW) --- AGENTS.md | 2 ++ ASSESSMENT.md | 8 ++++---- WORKFLOW.md | 4 ++-- .../Contracts/Tables/ContractsTable.php | 8 +++++++- .../Customers/Tables/CustomersTable.php | 6 ++++++ .../Resources/Payments/Tables/PaymentsTable.php | 17 +++++++++++++++++ 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 78bbe72..25bac0a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -306,6 +306,8 @@ - [x] **ProjectReport Page:** Báo cáo thống kê bán hàng, thanh toán theo từng dự án - [x] **ExportDebtReport Command:** Xuất Excel báo cáo công nợ khách hàng (2 sheet: Tổng hợp + Chi tiết đợt TT) - [x] **Notification System:** `PaymentDueNotification` + `SendPaymentDueNotifications` command + `RecentNotifications` widget +- [x] **Soft Delete:** Contract, Payment, Customer + Restore/ForceDelete UI trong Filament Tables +- [x] **Payment.collected_by:** Ghi nhận ngườ thu tiền trong PaymentForm + PaymentsTable ### 5.2. Đang dở / Cần tiếp tục - [x] **Dashboard thống kê:** Đã tạo `ContractStatsOverview` + `UpcomingPaymentsTable` diff --git a/ASSESSMENT.md b/ASSESSMENT.md index 0e0bff1..ca5ad1e 100644 --- a/ASSESSMENT.md +++ b/ASSESSMENT.md @@ -36,7 +36,7 @@ | 1 | **~~MailMergeService dùng `eval()`~~ [ĐÃ SỬA]** | `safeEval()` execute string bằng `eval('return ' . $expression)` | Nếu sanitize lỗi → Remote Code Execution. Hiện filter regex chưa đủ chặt | | 2 | **~~`Contract::saved()` gọi `saveQuietly()`~~ [ĐÃ SỬA]** | Sau khi save HĐ, trigger tính toán rồi save lại | Nếu logic thay đổi → infinite loop. Hiện tại may mắn không loop vì chỉ update `calculation_log` nhưng rủi ro cao | | 3 | **~~Không có Transaction~~ [ĐÃ SỬA]** | `ImportContractsComplex` dùng `DB::beginTransaction` nhưng các service khác không | Nếu tạo HĐ thành công nhưng tạo lịch TT lỗi → dữ liệu lệch | -| 4 | **Không có Soft Delete** | Tất cả model dùng `Model::delete()` cứng | Xóa nhầm HĐ/Thu tiền → mất vĩnh viễn, không audit được | +| 4 | **~~Không có Soft Delete~~ [ĐÃ SỬA]** | Đã thêm SoftDeletes cho Contract, Payment, Customer + Restore/ForceDelete UI | Xóa nhầm → có thể khôi phục, audit được | ### 🟡 Trung bình - Ảnh hưởng trải nghiệm @@ -45,7 +45,7 @@ | 5 | **Chưa có phân quyền** | Chỉ có 1 loại user, ai cũng vào được mọi chức năng | Nhân viên thu ngân có thể xóa HĐ, sửa giá | | 6 | **Chưa có API** | Hiện chỉ có Filament Admin Panel | Không làm app mobile, không tích hợp với website bán hàng | | 7 | **ContractForm chưa hiển thị `calculation_log` đúng** | Khi create HĐ mới, `final_value_display` dùng `DiscountEngine` cũ thay vì Pipeline | Giá trị hiển thị có thể khác với giá trị lưu DB | -| 8 | **Payment chưa liên kết người thu** | `Payment` chỉ có `contract_id`, không có `collected_by` | Không biết ai thu tiền, khó trách nhiệm | +| 8 | **~~Payment chưa liên kết ngườ thu~~ [ĐÃ SỬA]** | Đã thêm `collected_by` (foreign key → users) + hiển thị trong Form/Table | Ghi nhận rõ ngườ thu tiền | | 9 | **Chưa có sổ quỹ** | Thu tiền nhưng không ghi nhận vào quỹ tiền mặt/ngân hàng | Không đối soát được thực thu với ngân hàng | ### 🟢 Thấp - Cần cải thiện lâu dài @@ -81,8 +81,8 @@ ### Giai đoạn 1: Sửa lỗi & An toàn (ĐÃ HOÀN THÀNH) 1. **~~Thay thế `eval()`~~ [DONE]** - Dùng shunting yard + bcmath trong MailMergeService 2. **~~Thêm `DB::transaction`~~ [DONE]** - `ContractScheduleService::generateFromTemplate()` đã có transaction -3. **Thêm Soft Delete** cho Contract, Payment, Customer + model `DeletedBy` để audit -4. **Thêm `collected_by`** vào bảng `payments` + hiển thị ngườ thu trong Form/Table +3. **~~Thêm Soft Delete~~ [DONE]** - Contract, Payment, Customer + Restore/ForceDelete UI +4. **~~Thêm `collected_by`~~ [DONE]** - Bảng `payments` + Form/Table hiển thị ngườ thu 5. **~~Fix `Contract::saved()`~~ [DONE]** - Dùng `self::$calculating` guard flag + `updateQuietly()` ### Giai đoạn 2: Quyền hạn & Báo cáo (2 tuần) diff --git a/WORKFLOW.md b/WORKFLOW.md index 8759e71..d8a4b25 100644 --- a/WORKFLOW.md +++ b/WORKFLOW.md @@ -451,9 +451,9 @@ User 1───* Notification (MorphMany) | # | Vấn đề | Mức độ | Ghi chú | |---|--------|--------|---------| -| 1 | **Soft Delete** | 🔴 Nghiêm trọng | Tất cả model dùng delete cứng. Xóa nhầm → mất vĩnh viễn | +| 1 | **~~Soft Delete~~ ✅** | 🟢 Đã xong | Contract, Payment, Customer có SoftDeletes + Restore/ForceDelete UI | | 2 | **Phân quyền** | 🟡 Trung bình | Chỉ 1 loại user. Ai cũng xóa/sửa được mọi thứ | -| 3 | **Payment.collected_by** | 🟡 Trung bình | Không ghi nhận ai thu tiền. Khó truy trách nhiệm | +| 3 | **~~Payment.collected_by~~ ✅** | 🟢 Đã xong | Đã thêm collected_by (FK → users) + hiển thị Form/Table | | 4 | **Sổ quỹ** | 🟡 Trung bình | Thu tiền nhưng không ghi vào quỹ TM/NH. Không đối soát được | | 5 | **CRM Pipeline** | 🟢 Thấp | Chưa quản lý Lead/Khách hàng tiềm năng | | 6 | **Báo cáo BCTC** | 🟢 Thấp | Chưa có báo cáo theo chuẩn kế toán VN | diff --git a/app/Filament/Resources/Contracts/Tables/ContractsTable.php b/app/Filament/Resources/Contracts/Tables/ContractsTable.php index f98590c..c1551c3 100644 --- a/app/Filament/Resources/Contracts/Tables/ContractsTable.php +++ b/app/Filament/Resources/Contracts/Tables/ContractsTable.php @@ -8,7 +8,10 @@ use Filament\Actions\Action; use Filament\Actions\BulkActionGroup; use Filament\Actions\DeleteBulkAction; use Filament\Actions\EditAction; +use Filament\Actions\ForceDeleteAction; +use Filament\Actions\RestoreAction; use Filament\Tables\Columns\TextColumn; +use Filament\Tables\Filters\TrashedFilter; use Filament\Tables\Table; class ContractsTable @@ -84,10 +87,13 @@ class ContractsTable ->queries( true: fn ($query) => $query->where('transfer_order', 0), false: fn ($query) => $query->where('transfer_order', '>', 0), - ) + ), + TrashedFilter::make(), ]) ->recordActions([ EditAction::make(), + RestoreAction::make(), + ForceDeleteAction::make(), Action::make('generateSchedule') ->label('Tạo lịch TT') ->icon('heroicon-o-calendar-days') diff --git a/app/Filament/Resources/Customers/Tables/CustomersTable.php b/app/Filament/Resources/Customers/Tables/CustomersTable.php index b553028..d28e9f7 100644 --- a/app/Filament/Resources/Customers/Tables/CustomersTable.php +++ b/app/Filament/Resources/Customers/Tables/CustomersTable.php @@ -5,8 +5,11 @@ namespace App\Filament\Resources\Customers\Tables; use Filament\Actions\BulkActionGroup; use Filament\Actions\DeleteBulkAction; use Filament\Actions\EditAction; +use Filament\Actions\ForceDeleteAction; +use Filament\Actions\RestoreAction; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Columns\IconColumn; +use Filament\Tables\Filters\TrashedFilter; use Filament\Tables\Table; class CustomersTable @@ -64,9 +67,12 @@ class CustomersTable 'INDIVIDUAL' => 'Cá nhân', 'COMPANY' => 'Công ty', ]), + TrashedFilter::make(), ]) ->recordActions([ EditAction::make(), + RestoreAction::make(), + ForceDeleteAction::make(), ]) ->bulkActions([ BulkActionGroup::make([ diff --git a/app/Filament/Resources/Payments/Tables/PaymentsTable.php b/app/Filament/Resources/Payments/Tables/PaymentsTable.php index c9a9d55..28dce5e 100644 --- a/app/Filament/Resources/Payments/Tables/PaymentsTable.php +++ b/app/Filament/Resources/Payments/Tables/PaymentsTable.php @@ -2,7 +2,13 @@ namespace App\Filament\Resources\Payments\Tables; +use Filament\Actions\BulkActionGroup; +use Filament\Actions\DeleteBulkAction; +use Filament\Actions\EditAction; +use Filament\Actions\ForceDeleteAction; +use Filament\Actions\RestoreAction; use Filament\Tables; +use Filament\Tables\Filters\TrashedFilter; use Filament\Tables\Table; class PaymentsTable @@ -99,6 +105,17 @@ class PaymentsTable ->when($data['from'], fn ($q) => $q->whereDate('paid_date', '>=', $data['from'])) ->when($data['to'], fn ($q) => $q->whereDate('paid_date', '<=', $data['to'])); }), + TrashedFilter::make(), + ]) + ->recordActions([ + EditAction::make(), + RestoreAction::make(), + ForceDeleteAction::make(), + ]) + ->bulkActions([ + BulkActionGroup::make([ + DeleteBulkAction::make(), + ]), ]) ->defaultSort('paid_date', 'desc'); }