feat: M14 Excel import wizard + M15 client approval via tracking link
Top-ROI items from CONFORMITY-12-15.md. Together: ~40h of TZ work
delivered in one pass.
== M14 — Excel/CSV invoice import wizard ==
phpoffice/phpspreadsheet ^5.7 added as composer dep — parses both XLSX
and CSV cleanly.
ExcelInvoiceImportService (app/Services/ExcelInvoiceImportService.php):
- headersPreview($path) → first 5 rows + detected column letters
- preview($path, $mapping) → all rows classified as found/new/no_article
- import($supplier, $rows, $createNew=true) → creates Purchase + items,
auto-creates Parts for "new" rows
- rememberMapping / rememberedMappingFor($supplier) — round-trips JSON
config (article_col / name_col / qty_col / price_col / brand_col? /
header_row / sheet_name?) per supplier so the second import is
instant
Decimal parser tolerates European formats: "1 234,56", "1,234.56",
non-breaking spaces (U+00A0 NBSP common in copy-pastes from PDF).
Article matching uses single batch query (Part::whereIn) — O(1) for
the whole sheet, not O(rows).
ExcelImportWizard Filament page (/app/excel-import-wizard) — 4-step
Livewire wizard:
1. Upload + supplier select (saved mapping auto-loads if exists)
2. Column mapping with first-3-rows preview table + per-column
dropdowns
3. Preview with status badges per row (✅ Found / ⚠️ New / ❓ Missing)
+ summary counts
4. Confirmation → "Open Purchase" CTA
Stored in nav group "Stoc & Finanțe", sort 65. Width Full.
Migration: supplier_invoice_mappings (id, company_id, supplier_id UNIQUE,
mapping_config JSON, sample_file_name, last_used_at, timestamps).
Per-tenant scope via BelongsToTenant.
== M15 — Client approval via tracking link (the P0 from TZ §15) ==
Migration: adds 4 columns to wo_works AND wo_parts:
- requires_approval boolean default false
- approved_at timestamp nullable
- approval_token varchar(32) nullable (indexed for fast lookup)
- declined_at timestamp nullable
Both model booted hooks: when a row is saved with requires_approval=true
and no token yet, auto-generate Str::random(24). Models gain
isPendingApproval() helper returning true only while not yet approved
nor declined.
Public route: POST /t/{token}/approve/{kind}/{lineToken}
kind = 'work' | 'part'
body: decision = 'approve' | 'decline'
The line's approval_token IS the credential — anyone with the URL can
act. No CSRF token required since this is the unauthed public tracking
flow (the tracking_token + line approval_token combo functions as
shared-secret). Form-encoded POST with csrf_field() on the public form
keeps Laravel happy.
TrackingController::show() now eager-loads works + parts, computes
pendingWorks and pendingParts collections, passes them to the view.
TrackingController::approve() validates kind, locates the line by
(work_order_id, approval_token), idempotently marks approved_at or
declined_at, redirects back to /t/{token} with a flash status.
UI banner (tracking/show.blade.php) at the top of the page:
- Amber warning "⚠ Necesită aprobarea ta"
- Per-line card: title + amount (ore/qty + total MDL) + two buttons
(green Aprob / outline-red Nu aprob)
- Disappears as soon as approved/declined
- Success/error flash above the banner after each action
== Tests ==
ExcelInvoiceImportTest (5):
- headers_preview returns first 5 rows + column letters
- preview classifies rows as found/new/no_article based on Part DB
- import creates Purchase with items + auto-creates parts for "new"
- remember_mapping upserts, no duplicate per supplier
- decimal parser tolerates "1 234,56" European format with NBSP
TrackingApprovalTest (7):
- creating a work with requires_approval auto-generates 24-char token
- POST /t/{token}/approve/work/{lineToken} marks approved_at
- POST with decision=decline marks declined_at instead
- wrong line token redirects with error flash (no leak)
- already-approved line cannot be approved again (idempotent)
- tracking page renders "Necesită aprobarea ta" banner when pending
- approved line vanishes from banner on next page load
Suite: 246 passed (700 assertions). Was 234.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Central\Company;
|
||||
use App\Models\Central\Plan;
|
||||
use App\Models\Tenant\Client;
|
||||
use App\Models\Tenant\Vehicle;
|
||||
use App\Models\Tenant\WorkOrder;
|
||||
use App\Models\Tenant\WorkOrderPart;
|
||||
use App\Models\Tenant\WorkOrderWork;
|
||||
use App\Tenancy\TenantManager;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TrackingApprovalTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Company $company;
|
||||
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' => 'trk-' . uniqid(), 'name' => 'Trk Co', 'status' => 'active']);
|
||||
app(TenantManager::class)->setCurrent($this->company);
|
||||
$client = Client::create(['name' => 'Cli', 'phone' => '+37399123456', 'type' => 'individual', 'status' => 'active']);
|
||||
$vehicle = Vehicle::create(['client_id' => $client->id, 'make' => 'BMW', 'model' => 'X5', 'plate' => 'TRK-1']);
|
||||
$this->wo = WorkOrder::create([
|
||||
'number' => WorkOrder::generateNumber($this->company->id),
|
||||
'client_id' => $client->id, 'vehicle_id' => $vehicle->id,
|
||||
'opened_at' => today(), 'status' => 'in_work', 'total' => 5000,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_creating_work_with_requires_approval_generates_token(): void
|
||||
{
|
||||
$work = WorkOrderWork::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'Înlocuire amortizor față stâng',
|
||||
'hours' => 1.5, 'price_per_hour' => 420, 'status' => 'todo',
|
||||
'requires_approval' => true,
|
||||
]);
|
||||
|
||||
$this->assertNotEmpty($work->approval_token);
|
||||
$this->assertEquals(24, strlen($work->approval_token));
|
||||
$this->assertTrue($work->isPendingApproval());
|
||||
}
|
||||
|
||||
public function test_approve_endpoint_marks_line_approved(): void
|
||||
{
|
||||
$work = WorkOrderWork::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'Extra labor', 'hours' => 1, 'price_per_hour' => 400,
|
||||
'status' => 'todo', 'requires_approval' => true,
|
||||
]);
|
||||
$token = $this->wo->tracking_token;
|
||||
$lineToken = $work->approval_token;
|
||||
|
||||
$resp = $this->post("/t/{$token}/approve/work/{$lineToken}", ['decision' => 'approve']);
|
||||
$resp->assertRedirect(route('tracking.show', ['token' => $token]));
|
||||
|
||||
$work->refresh();
|
||||
$this->assertNotNull($work->approved_at);
|
||||
$this->assertFalse($work->isPendingApproval());
|
||||
}
|
||||
|
||||
public function test_decline_endpoint_marks_line_declined(): void
|
||||
{
|
||||
$part = WorkOrderPart::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'Filtru extra', 'article' => 'EX-1',
|
||||
'qty' => 1, 'sell_price' => 200, 'status' => 'needed',
|
||||
'requires_approval' => true,
|
||||
]);
|
||||
$token = $this->wo->tracking_token;
|
||||
|
||||
$resp = $this->post("/t/{$token}/approve/part/{$part->approval_token}", ['decision' => 'decline']);
|
||||
$resp->assertRedirect();
|
||||
|
||||
$part->refresh();
|
||||
$this->assertNull($part->approved_at);
|
||||
$this->assertNotNull($part->declined_at);
|
||||
$this->assertFalse($part->isPendingApproval());
|
||||
}
|
||||
|
||||
public function test_wrong_line_token_returns_redirect_with_error(): void
|
||||
{
|
||||
WorkOrderWork::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'X', 'hours' => 1, 'price_per_hour' => 200,
|
||||
'status' => 'todo', 'requires_approval' => true,
|
||||
]);
|
||||
$token = $this->wo->tracking_token;
|
||||
|
||||
$resp = $this->post("/t/{$token}/approve/work/wrongtoken12345678901234");
|
||||
$resp->assertRedirect();
|
||||
// Session flashed with error
|
||||
$resp->assertSessionHas('approval_status');
|
||||
$status = session('approval_status');
|
||||
$this->assertEquals('error', $status['kind']);
|
||||
}
|
||||
|
||||
public function test_already_approved_line_cannot_be_approved_again(): void
|
||||
{
|
||||
$work = WorkOrderWork::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'Already approved', 'hours' => 1, 'price_per_hour' => 200,
|
||||
'status' => 'todo', 'requires_approval' => true,
|
||||
]);
|
||||
$work->update(['approved_at' => now()->subHour()]);
|
||||
$originalApproval = $work->approved_at;
|
||||
$token = $this->wo->tracking_token;
|
||||
|
||||
// Second attempt should be a no-op (line no longer pending)
|
||||
$this->post("/t/{$token}/approve/work/{$work->approval_token}", ['decision' => 'approve']);
|
||||
|
||||
$work->refresh();
|
||||
$this->assertEquals($originalApproval->toIso8601String(), $work->approved_at->toIso8601String());
|
||||
}
|
||||
|
||||
public function test_tracking_show_includes_pending_approval_banner(): void
|
||||
{
|
||||
WorkOrderWork::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'Înlocuire amortizor', 'hours' => 1.5, 'price_per_hour' => 420,
|
||||
'status' => 'todo', 'requires_approval' => true,
|
||||
]);
|
||||
|
||||
$token = $this->wo->tracking_token;
|
||||
$resp = $this->get("/t/{$token}");
|
||||
$resp->assertOk();
|
||||
$resp->assertSee('Necesită aprobarea ta');
|
||||
$resp->assertSee('Înlocuire amortizor');
|
||||
$resp->assertSee('Aprob');
|
||||
}
|
||||
|
||||
public function test_approved_line_does_not_appear_in_banner(): void
|
||||
{
|
||||
$work = WorkOrderWork::create([
|
||||
'work_order_id' => $this->wo->id,
|
||||
'name' => 'Should not show', 'hours' => 1, 'price_per_hour' => 200,
|
||||
'status' => 'todo', 'requires_approval' => true,
|
||||
]);
|
||||
$work->update(['approved_at' => now()]);
|
||||
|
||||
$resp = $this->get("/t/{$this->wo->tracking_token}");
|
||||
$resp->assertOk();
|
||||
$resp->assertDontSee('Should not show');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user