From 7769ab773740ff3d6cf08c27ab417ad949184b75 Mon Sep 17 00:00:00 2001 From: Vasyka Date: Mon, 13 Jul 2026 20:07:51 +0000 Subject: [PATCH] fix: hide margin text + recompute lines when WO.apply_margin flips MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two related fixes for the WO-level "Aplică marjă internă" toggle: == 1. Hide description text when WO toggle is off == WorksRelationManager's Total column showed a gray subtitle line "Bază salariu: 320 MDL · marjă 20%" that persisted even after apply_margin was toggled OFF at the WO level. Confusing — the user expected "off means invisible". Fix: description callback now short-circuits to null when $record->workOrder->apply_margin === false, hiding the entire text. Also hides when applied_margin_pct is 0 (nothing meaningful to show). Result: OFF at WO level → zero margin details anywhere in the Manopere tab. ON → same as before. == 2. Auto-recompute salary_base on all lines when toggle flips == Previously, salary_base was frozen at line save-time. Flipping apply_margin from on→off left existing lines with the old 20%-reduced salary_base, so payroll still used the reduced amount even though the user had visually decided "no margin". Fix: WorkOrder::updated hook detects wasChanged(['apply_margin', 'override_margin_pct']) and iterates through works(): - apply_margin=false → salary_base = total, applied_margin_pct = 0 - apply_margin=true → resolver chain (WO override → mechanic → default) saveQuietly() on each line so we don't retrigger the works() booted hooks that would recompute again. This is DIFFERENT semantic from user.internal_margin_pct changes — those DON'T rewrite history (test still passes). The distinction: - User margin change: personnel decision, must not touch closed WOs - WO apply_margin change: explicit per-Fișă decision, must affect every line on that same Fișă InternalMarginRecomputeTest (3): - Flipping WO.apply_margin off recomputes both existing lines to at-cost - Flipping back on recomputes to margined - Changing WO.override_margin_pct recomputes with new % (40 → 60% base) Suite: 306 passed (853 assertions). Was 303. +3 recompute tests. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../RelationManagers/WorksRelationManager.php | 6 +- app/Models/Tenant/WorkOrder.php | 19 ++++ tests/Feature/InternalMarginRecomputeTest.php | 89 +++++++++++++++++++ 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/InternalMarginRecomputeTest.php diff --git a/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/WorksRelationManager.php b/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/WorksRelationManager.php index a0674a4..d4ba58c 100644 --- a/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/WorksRelationManager.php +++ b/app/Filament/Tenant/Resources/WorkOrderResource/RelationManagers/WorksRelationManager.php @@ -77,9 +77,9 @@ class WorksRelationManager extends RelationManager ->alignRight() ->description(function ($record) { if (! self::marginDetailsVisible() || $record->salary_base === null) return null; - if ((float) $record->applied_margin_pct === 0.0) { - return 'Fără marjă · bază salariu = Total'; - } + // WO-level toggle off → hide margin text complet + if ($record->workOrder && $record->workOrder->apply_margin === false) return null; + if ((float) $record->applied_margin_pct === 0.0) return null; return 'Bază salariu: ' . number_format((float) $record->salary_base, 2) . ' MDL · marjă ' . rtrim(rtrim(number_format((float) $record->applied_margin_pct, 2), '0'), '.') . '%'; }), Tables\Columns\TextColumn::make('master.name')->label('Maistru')->placeholder('—'), diff --git a/app/Models/Tenant/WorkOrder.php b/app/Models/Tenant/WorkOrder.php index a59cea0..850168c 100644 --- a/app/Models/Tenant/WorkOrder.php +++ b/app/Models/Tenant/WorkOrder.php @@ -153,6 +153,25 @@ class WorkOrder extends Model implements HasMedia app(\App\Services\NotificationDispatcher::class)->workOrderReady($wo); } + // WO.apply_margin sau override_margin_pct s-a schimbat → recomputăm salary_base + // pe toate manoperele existente (spre deosebire de user.internal_margin_pct, + // decizia la nivel de Fișă e explicită și trebuie să afecteze toate liniile ei). + if ($wo->wasChanged(['apply_margin', 'override_margin_pct'])) { + $resolver = app(\App\Services\MarginResolver::class); + foreach ($wo->works()->get() as $line) { + if ($wo->apply_margin === false) { + $line->applied_margin_pct = 0; + $line->salary_base = (float) $line->total; + } else { + $mechanic = $line->master_id ? \App\Models\Tenant\User::find($line->master_id) : null; + $marginPct = $resolver->resolve($wo, $mechanic); + $line->applied_margin_pct = $marginPct; + $line->salary_base = $resolver->computeSalaryBase((float) $line->total, $marginPct); + } + $line->saveQuietly(); + } + } + // Push the assigned mechanic when a WO gets assigned to them. if ($wo->wasChanged('master_id') && $wo->master_id) { try { diff --git a/tests/Feature/InternalMarginRecomputeTest.php b/tests/Feature/InternalMarginRecomputeTest.php new file mode 100644 index 0000000..9098d63 --- /dev/null +++ b/tests/Feature/InternalMarginRecomputeTest.php @@ -0,0 +1,89 @@ + 'test'], ['name' => 'T', 'price' => 0, 'features' => []]); + $this->company = Company::create(['plan_id' => $plan->id, 'slug' => 'rec-' . uniqid(), 'name' => 'REC', 'status' => 'active']); + app(TenantManager::class)->setCurrent($this->company); + + $this->mechanic = User::create(['name' => 'Andrei', 'email' => 'a@e.com', 'password' => bcrypt('x'), 'role' => 'mechanic', 'status' => 'active', 'internal_margin_pct' => 20]); + $client = Client::create(['name' => 'C', 'phone' => '+37399000000', 'type' => 'individual', 'status' => 'active']); + $vehicle = Vehicle::create(['client_id' => $client->id, 'make' => 'BMW', 'model' => 'X5', 'plate' => 'RC-1']); + $this->wo = WorkOrder::create([ + 'number' => WorkOrder::generateNumber($this->company->id), + 'client_id' => $client->id, 'vehicle_id' => $vehicle->id, 'master_id' => $this->mechanic->id, + 'opened_at' => today(), 'status' => 'in_work', 'total' => 0, + ]); + } + + public function test_toggling_wo_apply_margin_off_recomputes_all_existing_lines(): void + { + // Create 2 lines with margin on (default) + $w1 = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'A', 'hours' => 1, 'price_per_hour' => 250]); + $w2 = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'B', 'hours' => 2, 'price_per_hour' => 200]); + // Sanity: salary_base = 80% of total + $this->assertEquals(200.00, (float) $w1->salary_base); + $this->assertEquals(320.00, (float) $w2->salary_base); + $this->assertEquals(20.00, (float) $w1->applied_margin_pct); + + // Flip the toggle OFF at WO level + $this->wo->update(['apply_margin' => false]); + + $w1->refresh(); $w2->refresh(); + + // Both lines recomputed: salary_base = total, applied_margin_pct = 0 + $this->assertEquals(250.00, (float) $w1->salary_base); + $this->assertEquals(400.00, (float) $w2->salary_base); + $this->assertEquals(0.00, (float) $w1->applied_margin_pct); + $this->assertEquals(0.00, (float) $w2->applied_margin_pct); + } + + public function test_toggling_wo_apply_margin_back_on_recomputes_with_current_margin(): void + { + $w1 = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'X', 'hours' => 1, 'price_per_hour' => 250]); + $this->wo->update(['apply_margin' => false]); + $w1->refresh(); + $this->assertEquals(250.00, (float) $w1->salary_base); + + // Flip back on + $this->wo->update(['apply_margin' => true]); + $w1->refresh(); + + $this->assertEquals(200.00, (float) $w1->salary_base); + $this->assertEquals(20.00, (float) $w1->applied_margin_pct); + } + + public function test_changing_wo_override_margin_pct_recomputes_lines(): void + { + $w = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'X', 'hours' => 1, 'price_per_hour' => 300]); + $this->assertEquals(240.00, (float) $w->salary_base); // 300 * 0.80 (mechanic's 20%) + + // Set an override at 40% on the WO + $this->wo->update(['override_margin_pct' => 40]); + $w->refresh(); + $this->assertEquals(180.00, (float) $w->salary_base); // 300 * 0.60 + $this->assertEquals(40.00, (float) $w->applied_margin_pct); + } +}