Fix payment amount editable + discount live recalculation
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
+9
-5
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user