From 116495ce0b11ad268021dcc013ac3631f2672ed4 Mon Sep 17 00:00:00 2001 From: Vasyka Date: Tue, 8 Sep 2026 07:41:02 +0000 Subject: [PATCH] Fix payment amount editable + discount live recalculation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PaymentsRelationManager: remove broken default() closure on amount; use fillForm() on CreateAction instead (correct Filament API) so the modal pre-fills with remaining balance (total - paid) but field stays fully editable; add minValue/step for numeric UX - WorkOrderResource: discount_pct is now live(onBlur) with afterStateUpdated that recomputes works+parts+subcontract minus discount and sets the total field immediately — no Save required to see the effect Co-Authored-By: Claude Sonnet 4.6 --- .../Tenant/Resources/WorkOrderResource.php | 15 ++++++++++++++- .../RelationManagers/PaymentsRelationManager.php | 14 +++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/Filament/Tenant/Resources/WorkOrderResource.php b/app/Filament/Tenant/Resources/WorkOrderResource.php index 6063da8..bc855e6 100644 --- a/app/Filament/Tenant/Resources/WorkOrderResource.php +++ b/app/Filament/Tenant/Resources/WorkOrderResource.php @@ -149,7 +149,20 @@ class WorkOrderResource extends Resource ->columns(3)->collapsible()->collapsed()->compact() ->schema([ Forms\Components\Select::make('pay_status')->options(\App\Support\I18n::opts(WorkOrder::PAY_STATUSES))->default('unpaid')->required(), - Forms\Components\TextInput::make('discount_pct')->label(__('Discount %'))->numeric()->default(0), + Forms\Components\TextInput::make('discount_pct') + ->label(__('Discount %')) + ->numeric() + ->default(0) + ->live(onBlur: true) + ->afterStateUpdated(function ($state, callable $set, $record) { + if (! $record) return; + $works = (float) $record->works()->sum('total'); + $parts = (float) $record->parts()->sum('total'); + $sub = (float) $record->subcontractJobs() + ->where('status', '!=', 'cancelled')->sum('client_price'); + $disc = min(100, max(0, (float) $state)); + $set('total', round(($works + $parts + $sub) * (1 - $disc / 100), 2)); + }), Forms\Components\Toggle::make('apply_margin') ->label(__('Aplică marjă internă')) ->default(true) diff --git a/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/PaymentsRelationManager.php b/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/PaymentsRelationManager.php index 6621fcf..6590092 100644 --- a/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/PaymentsRelationManager.php +++ b/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/PaymentsRelationManager.php @@ -28,11 +28,9 @@ class PaymentsRelationManager extends RelationManager Forms\Components\TextInput::make('amount') ->label(__('Sumă')) ->numeric() - ->required() - ->default(function (RelationManager $livewire): float { - $wo = $livewire->getOwnerRecord(); - return max(0.0, (float) $wo->total - $wo->paidAmount()); - }), + ->minValue(0.01) + ->step(0.01) + ->required(), Forms\Components\Select::make('method') ->options(\App\Support\I18n::opts(Payment::METHODS)) ->default('cash') @@ -59,6 +57,12 @@ class PaymentsRelationManager extends RelationManager Actions\CreateAction::make() ->label(__('Adaugă plată')) ->modalHeading(__('Adaugă plată la fișă')) + ->fillForm(function (RelationManager $livewire): array { + $wo = $livewire->getOwnerRecord(); + return [ + 'amount' => max(0.0, round((float) $wo->total - $wo->paidAmount(), 2)), + ]; + }) ->mutateFormDataUsing(function (array $data, RelationManager $livewire): array { $wo = $livewire->getOwnerRecord(); $data['client_id'] = $wo->client_id;