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()
|
->columns(3)->collapsible()->collapsed()->compact()
|
||||||
->schema([
|
->schema([
|
||||||
Forms\Components\Select::make('pay_status')->options(\App\Support\I18n::opts(WorkOrder::PAY_STATUSES))->default('unpaid')->required(),
|
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')
|
Forms\Components\Toggle::make('apply_margin')
|
||||||
->label(__('Aplică marjă internă'))
|
->label(__('Aplică marjă internă'))
|
||||||
->default(true)
|
->default(true)
|
||||||
|
|||||||
+9
-5
@@ -28,11 +28,9 @@ class PaymentsRelationManager extends RelationManager
|
|||||||
Forms\Components\TextInput::make('amount')
|
Forms\Components\TextInput::make('amount')
|
||||||
->label(__('Sumă'))
|
->label(__('Sumă'))
|
||||||
->numeric()
|
->numeric()
|
||||||
->required()
|
->minValue(0.01)
|
||||||
->default(function (RelationManager $livewire): float {
|
->step(0.01)
|
||||||
$wo = $livewire->getOwnerRecord();
|
->required(),
|
||||||
return max(0.0, (float) $wo->total - $wo->paidAmount());
|
|
||||||
}),
|
|
||||||
Forms\Components\Select::make('method')
|
Forms\Components\Select::make('method')
|
||||||
->options(\App\Support\I18n::opts(Payment::METHODS))
|
->options(\App\Support\I18n::opts(Payment::METHODS))
|
||||||
->default('cash')
|
->default('cash')
|
||||||
@@ -59,6 +57,12 @@ class PaymentsRelationManager extends RelationManager
|
|||||||
Actions\CreateAction::make()
|
Actions\CreateAction::make()
|
||||||
->label(__('Adaugă plată'))
|
->label(__('Adaugă plată'))
|
||||||
->modalHeading(__('Adaugă plată la fișă'))
|
->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 {
|
->mutateFormDataUsing(function (array $data, RelationManager $livewire): array {
|
||||||
$wo = $livewire->getOwnerRecord();
|
$wo = $livewire->getOwnerRecord();
|
||||||
$data['client_id'] = $wo->client_id;
|
$data['client_id'] = $wo->client_id;
|
||||||
|
|||||||
Reference in New Issue
Block a user