fix: hide margin text + recompute lines when WO.apply_margin flips
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) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Central\Plan;
|
||||
use App\Models\Tenant\Client;
|
||||
use App\Models\Tenant\User;
|
||||
use App\Models\Tenant\Vehicle;
|
||||
use App\Models\Tenant\WorkOrder;
|
||||
use App\Models\Tenant\WorkOrderWork;
|
||||
use App\Tenancy\TenantManager;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InternalMarginRecomputeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Company $company;
|
||||
private User $mechanic;
|
||||
private WorkOrder $wo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$plan = Plan::firstOrCreate(['slug' => '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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user