Hoan thien core finance v2
This commit is contained in:
@@ -5,13 +5,12 @@ namespace App\Filament\Resources\Contracts;
|
||||
use App\Filament\Resources\Contracts\Pages;
|
||||
use App\Models\Contract;
|
||||
use App\Enums\NavigationGroup;
|
||||
use App\Services\ContractScheduleService;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use App\Filament\Resources\Contracts\ContractResource\RelationManagers\ScheduleItemsRelationManager;
|
||||
use App\Filament\Resources\Contracts\Schemas\ContractForm;
|
||||
use App\Filament\Resources\Contracts\Tables\ContractsTable;
|
||||
|
||||
class ContractResource extends Resource
|
||||
{
|
||||
@@ -30,33 +29,7 @@ class ContractResource extends Resource
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('contract_number')->label('Số HĐ')->searchable(),
|
||||
Tables\Columns\TextColumn::make('product.code')->label('Sản phẩm'),
|
||||
Tables\Columns\TextColumn::make('total_value')->label('Giá trị')->money('VND'),
|
||||
Tables\Columns\TextColumn::make('paid_amount')->label('Đã thu')->money('VND'),
|
||||
Tables\Columns\TextColumn::make('remaining_amount')->label('Còn lại')->money('VND'),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\Action::make('generateSchedule')
|
||||
->label('Tạo lịch TT')
|
||||
->icon('heroicon-o-calendar-days')
|
||||
->color('warning')
|
||||
->requiresConfirmation()
|
||||
->modalHeading('Tạo lịch thanh toán')
|
||||
->modalDescription('Hành động này sẽ xóa lịch thanh toán cũ (nếu có) và tạo lại từ mẫu của dự án.')
|
||||
->action(function (Contract $record) {
|
||||
try {
|
||||
ContractScheduleService::generateFromTemplate($record);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
// Filament sẽ tự động hiển thị lỗi nếu throw ra trong action
|
||||
throw $e;
|
||||
}
|
||||
})
|
||||
->visible(fn (Contract $record) => $record->signing_date !== null),
|
||||
]);
|
||||
return ContractsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array { return [ScheduleItemsRelationManager::class]; }
|
||||
|
||||
@@ -13,10 +13,9 @@ class CreateContract extends CreateRecord
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$contract = $this->record;
|
||||
$templateId = $this->data['payment_template_id'] ?? null;
|
||||
|
||||
if ($templateId) {
|
||||
$template = \App\Models\PaymentTemplate::find($templateId);
|
||||
if ($contract->payment_template_id) {
|
||||
$template = $contract->paymentTemplate;
|
||||
if ($template) {
|
||||
ContractScheduleService::generateFromTemplate($contract, $template);
|
||||
}
|
||||
|
||||
@@ -137,6 +137,24 @@ class ContractForm
|
||||
KeyValue::make('discount_details')
|
||||
->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')
|
||||
->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("<div style='font-size: 0.9rem; color: #9ca3af;'>Chưa có giá trị hợp đồng để tính chiết khấu.</div>");
|
||||
}
|
||||
|
||||
$result = \App\Services\DiscountEngine::calculate($totalValue, $discountDetails);
|
||||
$final = number_format($result['final_value']);
|
||||
$discount = number_format($result['discount_amount']);
|
||||
|
||||
return new HtmlString("<div style='font-size: 1.1rem; font-weight: bold; color: #16a34a;'>{$final} VNĐ</div><div style='font-size: 0.8rem; color: #9ca3af;'>Đã chiết khấu: {$discount} VNĐ</div>");
|
||||
}),
|
||||
]),
|
||||
|
||||
Section::make('Thông tin quản lý & Khách hàng')
|
||||
@@ -166,8 +184,8 @@ class ContractForm
|
||||
->placeholder('Chọn mẫu để tự động tạo lịch trình...')
|
||||
->options(PaymentTemplate::pluck('name', 'id'))
|
||||
->searchable()
|
||||
->dehydrated(false)
|
||||
->helperText('Lưu ý: Chỉ chọn nếu bạn muốn khởi tạo lại lịch trình thanh toán.'),
|
||||
->hiddenOn('edit')
|
||||
->helperText('Hệ thống sẽ tự động tạo lịch thanh toán sau khi lưu hợp đồng.'),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
namespace App\Filament\Resources\Contracts\Tables;
|
||||
|
||||
use App\Models\Contract;
|
||||
use App\Services\ContractScheduleService;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Columns\BadgeColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class ContractsTable
|
||||
@@ -55,6 +57,19 @@ class ContractsTable
|
||||
'Đã hủy' => 'danger',
|
||||
default => 'gray',
|
||||
}),
|
||||
|
||||
TextColumn::make('paid_amount')
|
||||
->label('Đã thu')
|
||||
->money('VND')
|
||||
->sortable()
|
||||
->toggleable(),
|
||||
|
||||
TextColumn::make('remaining_amount')
|
||||
->label('Còn lại')
|
||||
->money('VND')
|
||||
->sortable()
|
||||
->color('danger')
|
||||
->toggleable(),
|
||||
])
|
||||
->filters([
|
||||
\Filament\Tables\Filters\SelectFilter::make('status')
|
||||
@@ -73,6 +88,17 @@ class ContractsTable
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
Action::make('generateSchedule')
|
||||
->label('Tạo lịch TT')
|
||||
->icon('heroicon-o-calendar-days')
|
||||
->color('warning')
|
||||
->requiresConfirmation()
|
||||
->modalHeading('Tạo lịch thanh toán')
|
||||
->modalDescription('Hành động này sẽ xóa lịch thanh toán cũ (nếu có) và tạo lại từ mẫu của dự án.')
|
||||
->action(function (Contract $record) {
|
||||
ContractScheduleService::generateFromTemplate($record);
|
||||
})
|
||||
->visible(fn (Contract $record) => $record->signing_date !== null),
|
||||
])
|
||||
->bulkActions([
|
||||
BulkActionGroup::make([
|
||||
|
||||
Reference in New Issue
Block a user