0e3119a6e2
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>
159 lines
6.1 KiB
PHP
159 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Central\Company;
|
|
use App\Models\Central\Plan;
|
|
use App\Models\Tenant\Part;
|
|
use App\Models\Tenant\Purchase;
|
|
use App\Models\Tenant\PurchaseItem;
|
|
use App\Models\Tenant\Supplier;
|
|
use App\Models\Tenant\SupplierInvoiceMapping;
|
|
use App\Services\ExcelInvoiceImportService;
|
|
use App\Tenancy\TenantManager;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\TestCase;
|
|
|
|
class ExcelInvoiceImportTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Company $company;
|
|
private Supplier $supplier;
|
|
|
|
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' => 'exc-' . uniqid(), 'name' => 'Exc Co', 'status' => 'active']);
|
|
app(TenantManager::class)->setCurrent($this->company);
|
|
$this->supplier = Supplier::create(['name' => 'Rossko MD', 'phone' => '+37322000000']);
|
|
}
|
|
|
|
private function makeXlsx(array $rows, string $headerName = 'Articol'): string
|
|
{
|
|
$spreadsheet = new Spreadsheet;
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setCellValue('A1', '');
|
|
$sheet->setCellValue('B1', $headerName);
|
|
$sheet->setCellValue('C1', 'Denumire');
|
|
$sheet->setCellValue('D1', 'Brand');
|
|
$sheet->setCellValue('E1', 'Cant');
|
|
$sheet->setCellValue('F1', 'Preț');
|
|
$r = 2;
|
|
foreach ($rows as $row) {
|
|
$sheet->setCellValue("B$r", $row[0]);
|
|
$sheet->setCellValue("C$r", $row[1]);
|
|
$sheet->setCellValue("D$r", $row[2]);
|
|
$sheet->setCellValue("E$r", $row[3]);
|
|
$sheet->setCellValue("F$r", $row[4]);
|
|
$r++;
|
|
}
|
|
$tmp = tempnam(sys_get_temp_dir(), 'inv') . '.xlsx';
|
|
(new Xlsx($spreadsheet))->save($tmp);
|
|
return $tmp;
|
|
}
|
|
|
|
public function test_headers_preview_returns_first_5_rows_and_column_letters(): void
|
|
{
|
|
$path = $this->makeXlsx([
|
|
['W71221', 'Filtru ulei Mann', 'Mann', 10, 61.00],
|
|
['GDB1550', 'Plăcuțe TRW', 'TRW', 4, 280.00],
|
|
]);
|
|
|
|
$svc = app(ExcelInvoiceImportService::class);
|
|
$preview = $svc->headersPreview($path);
|
|
|
|
$this->assertContains('B', $preview['columns']);
|
|
$this->assertContains('F', $preview['columns']);
|
|
$this->assertEquals('Articol', $preview['rows'][0]['B']);
|
|
$this->assertEquals('Filtru ulei Mann', $preview['rows'][1]['C']);
|
|
unlink($path);
|
|
}
|
|
|
|
public function test_preview_classifies_rows_as_found_or_new(): void
|
|
{
|
|
// Existing part in DB
|
|
Part::create(['name' => 'Filtru ulei Mann W712/83', 'article' => 'W71221', 'buy_price' => 60, 'sell_price' => 85]);
|
|
|
|
$path = $this->makeXlsx([
|
|
['W71221', 'Filtru ulei', 'Mann', 10, 61.00],
|
|
['NEW-001', 'Articol nou', 'Generic', 5, 30.00],
|
|
['', 'Fără cod', '', 1, 100.00],
|
|
]);
|
|
|
|
$result = app(ExcelInvoiceImportService::class)->preview($path, [
|
|
'article_col' => 'B', 'name_col' => 'C', 'brand_col' => 'D',
|
|
'qty_col' => 'E', 'price_col' => 'F', 'header_row' => 1,
|
|
]);
|
|
|
|
$this->assertEquals(3, $result['summary']['total']);
|
|
$this->assertEquals(1, $result['summary']['found']);
|
|
$this->assertEquals(1, $result['summary']['new']);
|
|
$this->assertEquals(1, $result['summary']['no_article']);
|
|
|
|
$this->assertEquals('found', $result['rows'][0]['status']);
|
|
$this->assertEquals('new', $result['rows'][1]['status']);
|
|
$this->assertEquals('no_article', $result['rows'][2]['status']);
|
|
unlink($path);
|
|
}
|
|
|
|
public function test_import_creates_purchase_with_items_and_auto_creates_new_parts(): void
|
|
{
|
|
Part::create(['name' => 'Existing', 'article' => 'EX-1', 'buy_price' => 50]);
|
|
$svc = app(ExcelInvoiceImportService::class);
|
|
|
|
$path = $this->makeXlsx([
|
|
['EX-1', 'Existing', 'B1', 2, 50],
|
|
['NEW-A', 'New thing', 'B2', 3, 120],
|
|
]);
|
|
|
|
$preview = $svc->preview($path, [
|
|
'article_col' => 'B', 'name_col' => 'C', 'brand_col' => 'D',
|
|
'qty_col' => 'E', 'price_col' => 'F', 'header_row' => 1,
|
|
]);
|
|
$purchase = $svc->import($this->supplier, $preview['rows'], createNew: true);
|
|
|
|
$this->assertNotNull($purchase);
|
|
$this->assertEquals(2, PurchaseItem::where('purchase_id', $purchase->id)->count());
|
|
// 2*50 + 3*120 = 460
|
|
$this->assertEqualsWithDelta(460.0, (float) $purchase->total, 0.01);
|
|
|
|
// NEW-A should have been auto-created as a Part
|
|
$this->assertNotNull(Part::where('article', 'NEW-A')->first());
|
|
unlink($path);
|
|
}
|
|
|
|
public function test_remember_mapping_persists_and_round_trips(): void
|
|
{
|
|
$mapping = ['article_col' => 'B', 'name_col' => 'C', 'qty_col' => 'E', 'price_col' => 'F', 'header_row' => 2];
|
|
$svc = app(ExcelInvoiceImportService::class);
|
|
|
|
$svc->rememberMapping($this->supplier, $mapping, 'rossko-march.xlsx');
|
|
|
|
$loaded = $svc->rememberedMappingFor($this->supplier);
|
|
$this->assertEquals('B', $loaded['article_col']);
|
|
$this->assertEquals(2, $loaded['header_row']);
|
|
|
|
// Update — should upsert, not duplicate
|
|
$mapping['header_row'] = 3;
|
|
$svc->rememberMapping($this->supplier, $mapping);
|
|
$this->assertEquals(1, SupplierInvoiceMapping::where('supplier_id', $this->supplier->id)->count());
|
|
$this->assertEquals(3, $svc->rememberedMappingFor($this->supplier)['header_row']);
|
|
}
|
|
|
|
public function test_decimal_parser_tolerates_european_format(): void
|
|
{
|
|
$path = $this->makeXlsx([
|
|
['EU-1', 'European', 'Brand', '2', '1 234,56'],
|
|
]);
|
|
$preview = app(ExcelInvoiceImportService::class)->preview($path, [
|
|
'article_col' => 'B', 'name_col' => 'C', 'qty_col' => 'E', 'price_col' => 'F', 'header_row' => 1,
|
|
]);
|
|
$this->assertEqualsWithDelta(1234.56, $preview['rows'][0]['price'], 0.01);
|
|
unlink($path);
|
|
}
|
|
}
|