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,143 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Tenant\Pages;
|
||||||
|
|
||||||
|
use App\Models\Tenant\Supplier;
|
||||||
|
use App\Services\ExcelInvoiceImportService;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
|
use Filament\Pages\Page;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Livewire\WithFileUploads;
|
||||||
|
|
||||||
|
class ExcelImportWizard extends Page
|
||||||
|
{
|
||||||
|
use WithFileUploads;
|
||||||
|
|
||||||
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-arrow-up-tray';
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'Import factură Excel';
|
||||||
|
|
||||||
|
protected static string|\UnitEnum|null $navigationGroup = 'Stoc & Finanțe';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 65;
|
||||||
|
|
||||||
|
protected static ?string $title = 'Import factură Excel/CSV';
|
||||||
|
|
||||||
|
protected string $view = 'filament.tenant.pages.excel-import-wizard';
|
||||||
|
|
||||||
|
public int $step = 1;
|
||||||
|
public ?int $supplierId = null;
|
||||||
|
public $upload = null;
|
||||||
|
public ?string $storedPath = null;
|
||||||
|
public array $headersPreview = ['columns' => [], 'rows' => []];
|
||||||
|
public array $mapping = [
|
||||||
|
'article_col' => 'B',
|
||||||
|
'name_col' => 'C',
|
||||||
|
'qty_col' => 'E',
|
||||||
|
'price_col' => 'F',
|
||||||
|
'brand_col' => null,
|
||||||
|
'header_row' => 1,
|
||||||
|
];
|
||||||
|
public bool $rememberMapping = true;
|
||||||
|
public array $previewRows = [];
|
||||||
|
public array $previewSummary = ['total' => 0, 'found' => 0, 'new' => 0, 'no_article' => 0];
|
||||||
|
public bool $createNew = true;
|
||||||
|
|
||||||
|
public function getMaxContentWidth(): \Filament\Support\Enums\Width
|
||||||
|
{
|
||||||
|
return \Filament\Support\Enums\Width::Full;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSupplierOptions(): array
|
||||||
|
{
|
||||||
|
return Supplier::orderBy('name')->pluck('name', 'id')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function goToStep2(): void
|
||||||
|
{
|
||||||
|
if (! $this->supplierId) {
|
||||||
|
Notification::make()->title('Selectează furnizorul')->danger()->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (! $this->upload) {
|
||||||
|
Notification::make()->title('Încarcă fișierul Excel sau CSV')->danger()->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persist the uploaded file so Livewire reuses can resolve it
|
||||||
|
$this->storedPath = $this->upload->store('imports', 'local');
|
||||||
|
|
||||||
|
// Try to load remembered mapping for this supplier
|
||||||
|
$svc = app(ExcelInvoiceImportService::class);
|
||||||
|
$supplier = Supplier::find($this->supplierId);
|
||||||
|
$remembered = $svc->rememberedMappingFor($supplier);
|
||||||
|
if ($remembered) {
|
||||||
|
$this->mapping = array_merge($this->mapping, $remembered);
|
||||||
|
}
|
||||||
|
|
||||||
|
$absPath = Storage::disk('local')->path($this->storedPath);
|
||||||
|
$this->headersPreview = $svc->headersPreview($absPath);
|
||||||
|
|
||||||
|
$this->step = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function goToStep3(): void
|
||||||
|
{
|
||||||
|
$absPath = Storage::disk('local')->path($this->storedPath);
|
||||||
|
$svc = app(ExcelInvoiceImportService::class);
|
||||||
|
$result = $svc->preview($absPath, $this->mapping);
|
||||||
|
$this->previewRows = $result['rows'];
|
||||||
|
$this->previewSummary = $result['summary'];
|
||||||
|
|
||||||
|
if (empty($this->previewRows)) {
|
||||||
|
Notification::make()->title('Nu am găsit linii valide — verifică maparea coloanelor')->warning()->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->step = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function confirmImport(): void
|
||||||
|
{
|
||||||
|
$svc = app(ExcelInvoiceImportService::class);
|
||||||
|
$supplier = Supplier::find($this->supplierId);
|
||||||
|
|
||||||
|
if ($this->rememberMapping) {
|
||||||
|
$svc->rememberMapping($supplier, $this->mapping, basename($this->storedPath ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
$purchase = $svc->import($supplier, $this->previewRows, $this->createNew);
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title("Import reușit — Purchase {$purchase->number}")
|
||||||
|
->body("{$this->previewSummary['total']} linii importate")
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
// Cleanup uploaded file
|
||||||
|
if ($this->storedPath) {
|
||||||
|
Storage::disk('local')->delete($this->storedPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->step = 4;
|
||||||
|
$this->dispatch('purchase-created', purchaseId: $purchase->id);
|
||||||
|
|
||||||
|
// Set the redirect URL on the page so the blade can show a CTA
|
||||||
|
session()->flash('purchase_id', $purchase->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reset_(): void
|
||||||
|
{
|
||||||
|
$this->step = 1;
|
||||||
|
$this->supplierId = null;
|
||||||
|
$this->upload = null;
|
||||||
|
$this->storedPath = null;
|
||||||
|
$this->headersPreview = ['columns' => [], 'rows' => []];
|
||||||
|
$this->mapping = [
|
||||||
|
'article_col' => 'B', 'name_col' => 'C', 'qty_col' => 'E',
|
||||||
|
'price_col' => 'F', 'brand_col' => null, 'header_row' => 1,
|
||||||
|
];
|
||||||
|
$this->previewRows = [];
|
||||||
|
$this->previewSummary = ['total' => 0, 'found' => 0, 'new' => 0, 'no_article' => 0];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\Tenant\WorkOrder;
|
use App\Models\Tenant\WorkOrder;
|
||||||
|
use App\Models\Tenant\WorkOrderPart;
|
||||||
|
use App\Models\Tenant\WorkOrderWork;
|
||||||
use App\Tenancy\TenantManager;
|
use App\Tenancy\TenantManager;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
@@ -11,8 +13,6 @@ class TrackingController extends Controller
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Public WO tracking page — accessed via QR code or SMS link.
|
* Public WO tracking page — accessed via QR code or SMS link.
|
||||||
* Tenant is resolved by ResolveTenant from the host, so the global
|
|
||||||
* BelongsToTenant scope already filters to the correct tenant.
|
|
||||||
*/
|
*/
|
||||||
public function show(Request $request, string $token)
|
public function show(Request $request, string $token)
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,7 @@ class TrackingController extends Controller
|
|||||||
throw new NotFoundHttpException('Tracking only available on tenant subdomain.');
|
throw new NotFoundHttpException('Tracking only available on tenant subdomain.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$wo = WorkOrder::with(['client', 'vehicle', 'master', 'media'])
|
$wo = WorkOrder::with(['client', 'vehicle', 'master', 'media', 'works', 'parts'])
|
||||||
->where('tracking_token', $token)
|
->where('tracking_token', $token)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
@@ -29,13 +29,56 @@ class TrackingController extends Controller
|
|||||||
throw new NotFoundHttpException('Fișa nu a fost găsită.');
|
throw new NotFoundHttpException('Fișa nu a fost găsită.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pendingWorks = $wo->works->filter(fn ($w) => $w->isPendingApproval());
|
||||||
|
$pendingParts = $wo->parts->filter(fn ($p) => $p->isPendingApproval());
|
||||||
|
|
||||||
return view('tracking.show', [
|
return view('tracking.show', [
|
||||||
'wo' => $wo,
|
'wo' => $wo,
|
||||||
'tenant' => $tenant,
|
'tenant' => $tenant,
|
||||||
'photos' => $wo->getMedia('photos'),
|
'photos' => $wo->getMedia('photos'),
|
||||||
|
'pendingWorks' => $pendingWorks,
|
||||||
|
'pendingParts' => $pendingParts,
|
||||||
|
'approvalStatus' => $request->session()->pull('approval_status'),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client approves or declines a pending work/part line via the unique
|
||||||
|
* approval_token. The line's approval_token IS the credential — anyone
|
||||||
|
* with the URL can act (clients won't share it).
|
||||||
|
*/
|
||||||
|
public function approve(Request $request, string $token, string $kind, string $lineToken)
|
||||||
|
{
|
||||||
|
$tenant = app(TenantManager::class)->current();
|
||||||
|
if (! $tenant) throw new NotFoundHttpException();
|
||||||
|
|
||||||
|
$wo = WorkOrder::where('tracking_token', $token)->first();
|
||||||
|
if (! $wo) throw new NotFoundHttpException();
|
||||||
|
|
||||||
|
$decision = $request->input('decision', 'approve');
|
||||||
|
|
||||||
|
$line = match ($kind) {
|
||||||
|
'work' => WorkOrderWork::where('work_order_id', $wo->id)->where('approval_token', $lineToken)->first(),
|
||||||
|
'part' => WorkOrderPart::where('work_order_id', $wo->id)->where('approval_token', $lineToken)->first(),
|
||||||
|
default => null,
|
||||||
|
};
|
||||||
|
if (! $line || ! $line->isPendingApproval()) {
|
||||||
|
$request->session()->flash('approval_status', ['kind' => 'error', 'message' => 'Linia nu mai necesită aprobare.']);
|
||||||
|
return redirect()->route('tracking.show', ['token' => $token]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($decision === 'approve') {
|
||||||
|
$line->forceFill(['approved_at' => now()])->save();
|
||||||
|
$msg = '✅ Lucrarea „' . $line->name . '" a fost aprobată. Mulțumim!';
|
||||||
|
} else {
|
||||||
|
$line->forceFill(['declined_at' => now()])->save();
|
||||||
|
$msg = '❌ Lucrarea „' . $line->name . '" a fost respinsă. Vă vom contacta.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$request->session()->flash('approval_status', ['kind' => 'success', 'message' => $msg]);
|
||||||
|
return redirect()->route('tracking.show', ['token' => $token]);
|
||||||
|
}
|
||||||
|
|
||||||
public function qr(Request $request, string $token)
|
public function qr(Request $request, string $token)
|
||||||
{
|
{
|
||||||
$tenant = app(TenantManager::class)->current();
|
$tenant = app(TenantManager::class)->current();
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Tenant;
|
||||||
|
|
||||||
|
use App\Models\Concerns\BelongsToTenant;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class SupplierInvoiceMapping extends Model
|
||||||
|
{
|
||||||
|
use BelongsToTenant;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'company_id', 'supplier_id', 'mapping_config',
|
||||||
|
'sample_file_name', 'last_used_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'mapping_config' => 'array',
|
||||||
|
'last_used_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function supplier(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Supplier::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ class WorkOrderPart extends Model
|
|||||||
'name', 'article', 'brand',
|
'name', 'article', 'brand',
|
||||||
'qty', 'unit', 'buy_price', 'sell_price',
|
'qty', 'unit', 'buy_price', 'sell_price',
|
||||||
'discount_pct', 'total', 'status', 'notes',
|
'discount_pct', 'total', 'status', 'notes',
|
||||||
|
'requires_approval', 'approved_at', 'approval_token', 'declined_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
@@ -32,8 +33,16 @@ class WorkOrderPart extends Model
|
|||||||
'sell_price' => 'decimal:2',
|
'sell_price' => 'decimal:2',
|
||||||
'discount_pct' => 'decimal:2',
|
'discount_pct' => 'decimal:2',
|
||||||
'total' => 'decimal:2',
|
'total' => 'decimal:2',
|
||||||
|
'requires_approval' => 'boolean',
|
||||||
|
'approved_at' => 'datetime',
|
||||||
|
'declined_at' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function isPendingApproval(): bool
|
||||||
|
{
|
||||||
|
return $this->requires_approval && $this->approved_at === null && $this->declined_at === null;
|
||||||
|
}
|
||||||
|
|
||||||
public function workOrder(): BelongsTo
|
public function workOrder(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(WorkOrder::class);
|
return $this->belongsTo(WorkOrder::class);
|
||||||
@@ -50,6 +59,9 @@ class WorkOrderPart extends Model
|
|||||||
$sub = (float) $row->qty * (float) $row->sell_price;
|
$sub = (float) $row->qty * (float) $row->sell_price;
|
||||||
$disc = (float) $row->discount_pct;
|
$disc = (float) $row->discount_pct;
|
||||||
$row->total = round($sub * (1 - $disc / 100), 2);
|
$row->total = round($sub * (1 - $disc / 100), 2);
|
||||||
|
if ($row->requires_approval && empty($row->approval_token)) {
|
||||||
|
$row->approval_token = \Illuminate\Support\Str::random(24);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reserve batches as soon as a catalog-linked part line is created.
|
// Reserve batches as soon as a catalog-linked part line is created.
|
||||||
|
|||||||
@@ -21,14 +21,23 @@ class WorkOrderWork extends Model
|
|||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'company_id', 'work_order_id', 'labor_id', 'master_id',
|
'company_id', 'work_order_id', 'labor_id', 'master_id',
|
||||||
'name', 'hours', 'price_per_hour', 'total', 'status', 'notes',
|
'name', 'hours', 'price_per_hour', 'total', 'status', 'notes',
|
||||||
|
'requires_approval', 'approved_at', 'approval_token', 'declined_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'hours' => 'decimal:2',
|
'hours' => 'decimal:2',
|
||||||
'price_per_hour' => 'decimal:2',
|
'price_per_hour' => 'decimal:2',
|
||||||
'total' => 'decimal:2',
|
'total' => 'decimal:2',
|
||||||
|
'requires_approval' => 'boolean',
|
||||||
|
'approved_at' => 'datetime',
|
||||||
|
'declined_at' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function isPendingApproval(): bool
|
||||||
|
{
|
||||||
|
return $this->requires_approval && $this->approved_at === null && $this->declined_at === null;
|
||||||
|
}
|
||||||
|
|
||||||
public function workOrder(): BelongsTo
|
public function workOrder(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(WorkOrder::class);
|
return $this->belongsTo(WorkOrder::class);
|
||||||
@@ -48,6 +57,9 @@ class WorkOrderWork extends Model
|
|||||||
{
|
{
|
||||||
static::saving(function (self $row) {
|
static::saving(function (self $row) {
|
||||||
$row->total = round((float) $row->hours * (float) $row->price_per_hour, 2);
|
$row->total = round((float) $row->hours * (float) $row->price_per_hour, 2);
|
||||||
|
if ($row->requires_approval && empty($row->approval_token)) {
|
||||||
|
$row->approval_token = \Illuminate\Support\Str::random(24);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
static::saved(fn (self $row) => $row->workOrder?->recalcTotal());
|
static::saved(fn (self $row) => $row->workOrder?->recalcTotal());
|
||||||
static::deleted(fn (self $row) => $row->workOrder?->recalcTotal());
|
static::deleted(fn (self $row) => $row->workOrder?->recalcTotal());
|
||||||
|
|||||||
@@ -0,0 +1,231 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
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 Illuminate\Support\Facades\DB;
|
||||||
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses XLSX / CSV files coming from suppliers and turns them into
|
||||||
|
* Purchase drafts. Mapping (column letters → field) is stored per supplier
|
||||||
|
* so the second import becomes instant.
|
||||||
|
*
|
||||||
|
* Three steps:
|
||||||
|
* 1. headersPreview($path) — first 5 rows + detected column letters
|
||||||
|
* 2. preview($path, $mapping) — parse all rows + classify each as
|
||||||
|
* found (article matches part) / new
|
||||||
|
* (article exists in file but not in DB) /
|
||||||
|
* no_article (line has data but no article col)
|
||||||
|
* 3. import($supplier, $rows) — create Purchase + PurchaseItems; auto-creates
|
||||||
|
* Parts for "new" rows when create_new = true
|
||||||
|
*/
|
||||||
|
class ExcelInvoiceImportService
|
||||||
|
{
|
||||||
|
/** Read first $maxRows of a spreadsheet for the wizard preview. */
|
||||||
|
public function headersPreview(string $absPath, int $maxRows = 5): array
|
||||||
|
{
|
||||||
|
$sheet = $this->loadSheet($absPath);
|
||||||
|
$highestColumn = $sheet->getHighestColumn();
|
||||||
|
$highestColIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
|
||||||
|
$cols = [];
|
||||||
|
for ($i = 1; $i <= min($highestColIndex, 20); $i++) {
|
||||||
|
$cols[] = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($i);
|
||||||
|
}
|
||||||
|
$rows = [];
|
||||||
|
for ($r = 1; $r <= $maxRows; $r++) {
|
||||||
|
$row = [];
|
||||||
|
foreach ($cols as $col) {
|
||||||
|
$row[$col] = (string) ($sheet->getCell("$col$r")->getValue() ?? '');
|
||||||
|
}
|
||||||
|
$rows[] = $row;
|
||||||
|
}
|
||||||
|
return ['columns' => $cols, 'rows' => $rows];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the file according to $mapping and classify each line.
|
||||||
|
*
|
||||||
|
* Mapping keys:
|
||||||
|
* article_col, name_col, qty_col, price_col, brand_col (optional),
|
||||||
|
* header_row (1-based row number to skip), sheet_name (optional).
|
||||||
|
*/
|
||||||
|
public function preview(string $absPath, array $mapping): array
|
||||||
|
{
|
||||||
|
$sheet = $this->loadSheet($absPath, $mapping['sheet_name'] ?? null);
|
||||||
|
$headerRow = (int) ($mapping['header_row'] ?? 1);
|
||||||
|
$highestRow = $sheet->getHighestRow();
|
||||||
|
|
||||||
|
$rows = [];
|
||||||
|
$articlesInFile = [];
|
||||||
|
|
||||||
|
for ($r = $headerRow + 1; $r <= $highestRow; $r++) {
|
||||||
|
$article = $this->cellString($sheet, $mapping['article_col'] ?? null, $r);
|
||||||
|
$name = $this->cellString($sheet, $mapping['name_col'] ?? null, $r);
|
||||||
|
$brand = $this->cellString($sheet, $mapping['brand_col'] ?? null, $r);
|
||||||
|
$qty = $this->cellDecimal($sheet, $mapping['qty_col'] ?? null, $r);
|
||||||
|
$price = $this->cellDecimal($sheet, $mapping['price_col'] ?? null, $r);
|
||||||
|
|
||||||
|
// Skip totally empty lines
|
||||||
|
if ($article === '' && $name === '' && $qty <= 0) continue;
|
||||||
|
|
||||||
|
$status = 'no_article';
|
||||||
|
$partId = null;
|
||||||
|
if ($article !== '') {
|
||||||
|
$articlesInFile[] = $article;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows[] = [
|
||||||
|
'row' => $r,
|
||||||
|
'article' => $article,
|
||||||
|
'name' => $name,
|
||||||
|
'brand' => $brand,
|
||||||
|
'qty' => $qty,
|
||||||
|
'price' => $price,
|
||||||
|
'status' => $status,
|
||||||
|
'part_id' => $partId,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Single batch query for all articles → status
|
||||||
|
if (! empty($articlesInFile)) {
|
||||||
|
$existing = Part::whereIn('article', array_unique($articlesInFile))->get()->keyBy('article');
|
||||||
|
foreach ($rows as &$row) {
|
||||||
|
if ($row['article'] === '') continue;
|
||||||
|
if ($existing->has($row['article'])) {
|
||||||
|
$row['status'] = 'found';
|
||||||
|
$row['part_id'] = $existing[$row['article']]->id;
|
||||||
|
} else {
|
||||||
|
$row['status'] = 'new';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'rows' => $rows,
|
||||||
|
'summary' => [
|
||||||
|
'total' => count($rows),
|
||||||
|
'found' => count(array_filter($rows, fn ($r) => $r['status'] === 'found')),
|
||||||
|
'new' => count(array_filter($rows, fn ($r) => $r['status'] === 'new')),
|
||||||
|
'no_article' => count(array_filter($rows, fn ($r) => $r['status'] === 'no_article')),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a draft Purchase + items from the rows. Rows with status='new'
|
||||||
|
* automatically get a Part created if $createNew is true.
|
||||||
|
*/
|
||||||
|
public function import(Supplier $supplier, array $rows, bool $createNew = true): Purchase
|
||||||
|
{
|
||||||
|
return DB::transaction(function () use ($supplier, $rows, $createNew) {
|
||||||
|
$purchase = Purchase::create([
|
||||||
|
'supplier_id' => $supplier->id,
|
||||||
|
'number' => $this->generatePurchaseNumber($supplier->company_id),
|
||||||
|
'order_date' => today(),
|
||||||
|
'status' => 'ordered',
|
||||||
|
'total' => 0,
|
||||||
|
'notes' => 'Auto-import Excel/CSV',
|
||||||
|
]);
|
||||||
|
$total = 0;
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$partId = $row['part_id'] ?? null;
|
||||||
|
if (! $partId && $row['status'] === 'new' && $createNew && $row['article'] !== '') {
|
||||||
|
$part = Part::create([
|
||||||
|
'name' => $row['name'] ?: $row['article'],
|
||||||
|
'article' => $row['article'],
|
||||||
|
'brand' => $row['brand'] ?? null,
|
||||||
|
'buy_price' => $row['price'],
|
||||||
|
'preferred_supplier_id' => $supplier->id,
|
||||||
|
]);
|
||||||
|
$partId = $part->id;
|
||||||
|
}
|
||||||
|
PurchaseItem::create([
|
||||||
|
'purchase_id' => $purchase->id,
|
||||||
|
'part_id' => $partId,
|
||||||
|
'name' => $row['name'] ?: $row['article'],
|
||||||
|
'article' => $row['article'],
|
||||||
|
'qty' => $row['qty'] ?: 1,
|
||||||
|
'qty_received' => 0,
|
||||||
|
'buy_price' => $row['price'],
|
||||||
|
'total' => round(($row['qty'] ?: 1) * $row['price'], 2),
|
||||||
|
]);
|
||||||
|
$total += ($row['qty'] ?: 1) * $row['price'];
|
||||||
|
}
|
||||||
|
$purchase->update(['total' => round($total, 2)]);
|
||||||
|
return $purchase;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save mapping config for a supplier so subsequent imports skip the wizard.
|
||||||
|
*/
|
||||||
|
public function rememberMapping(Supplier $supplier, array $mapping, ?string $sampleFileName = null): SupplierInvoiceMapping
|
||||||
|
{
|
||||||
|
return SupplierInvoiceMapping::updateOrCreate(
|
||||||
|
['supplier_id' => $supplier->id],
|
||||||
|
[
|
||||||
|
'mapping_config' => $mapping,
|
||||||
|
'sample_file_name' => $sampleFileName,
|
||||||
|
'last_used_at' => now(),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rememberedMappingFor(Supplier $supplier): ?array
|
||||||
|
{
|
||||||
|
$m = SupplierInvoiceMapping::where('supplier_id', $supplier->id)->first();
|
||||||
|
return $m?->mapping_config;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadSheet(string $absPath, ?string $sheetName = null)
|
||||||
|
{
|
||||||
|
$reader = IOFactory::createReaderForFile($absPath);
|
||||||
|
$reader->setReadDataOnly(true);
|
||||||
|
$spreadsheet = $reader->load($absPath);
|
||||||
|
return $sheetName ? $spreadsheet->getSheetByName($sheetName) ?? $spreadsheet->getActiveSheet() : $spreadsheet->getActiveSheet();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cellString($sheet, ?string $col, int $row): string
|
||||||
|
{
|
||||||
|
if (! $col) return '';
|
||||||
|
$value = $sheet->getCell("$col$row")->getValue();
|
||||||
|
return trim((string) ($value ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cellDecimal($sheet, ?string $col, int $row): float
|
||||||
|
{
|
||||||
|
if (! $col) return 0.0;
|
||||||
|
$value = $sheet->getCell("$col$row")->getValue();
|
||||||
|
if ($value === null || $value === '') return 0.0;
|
||||||
|
// Normalize "1 234,56" / "1,234.56" → 1234.56
|
||||||
|
// PCRE doesn't support \u{XXXX} — use \x{00A0} (non-breaking space) instead
|
||||||
|
$clean = preg_replace('/[\s\x{00A0}]/u', '', (string) $value);
|
||||||
|
$clean = str_replace(',', '.', $clean);
|
||||||
|
// remove duplicate dots if any
|
||||||
|
$parts = explode('.', $clean);
|
||||||
|
if (count($parts) > 2) {
|
||||||
|
$clean = implode('', array_slice($parts, 0, -1)) . '.' . end($parts);
|
||||||
|
}
|
||||||
|
return (float) $clean;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function generatePurchaseNumber(int $companyId): string
|
||||||
|
{
|
||||||
|
$year = date('Y');
|
||||||
|
$last = Purchase::where('company_id', $companyId)
|
||||||
|
->where('number', 'like', "P-$year-%")
|
||||||
|
->orderByDesc('id')->first();
|
||||||
|
$next = 1;
|
||||||
|
if ($last && preg_match('/P-\d{4}-(\d+)$/', $last->number, $m)) {
|
||||||
|
$next = (int) $m[1] + 1;
|
||||||
|
}
|
||||||
|
return sprintf('P-%s-%04d', $year, $next);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
"laravel/sanctum": "^4.3",
|
"laravel/sanctum": "^4.3",
|
||||||
"laravel/tinker": "^2.10.1",
|
"laravel/tinker": "^2.10.1",
|
||||||
"minishlink/web-push": "^10.0",
|
"minishlink/web-push": "^10.0",
|
||||||
|
"phpoffice/phpspreadsheet": "^5.7",
|
||||||
"resend/resend-laravel": "^1.4",
|
"resend/resend-laravel": "^1.4",
|
||||||
"spatie/laravel-activitylog": "^5.0",
|
"spatie/laravel-activitylog": "^5.0",
|
||||||
"spatie/laravel-medialibrary": "^11.22",
|
"spatie/laravel-medialibrary": "^11.22",
|
||||||
|
|||||||
Generated
+296
-1
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "82d0b6d061454a485d2a93b700e4a5a8",
|
"content-hash": "5b5b5d8a2a2a4bac8ef246a2b165992c",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "barryvdh/laravel-dompdf",
|
"name": "barryvdh/laravel-dompdf",
|
||||||
@@ -654,6 +654,85 @@
|
|||||||
],
|
],
|
||||||
"time": "2025-01-03T16:18:33+00:00"
|
"time": "2025-01-03T16:18:33+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "composer/pcre",
|
||||||
|
"version": "3.3.2",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/composer/pcre.git",
|
||||||
|
"reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/composer/pcre/zipball/b2bed4734f0cc156ee1fe9c0da2550420d99a21e",
|
||||||
|
"reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^7.4 || ^8.0"
|
||||||
|
},
|
||||||
|
"conflict": {
|
||||||
|
"phpstan/phpstan": "<1.11.10"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpstan/phpstan": "^1.12 || ^2",
|
||||||
|
"phpstan/phpstan-strict-rules": "^1 || ^2",
|
||||||
|
"phpunit/phpunit": "^8 || ^9"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"phpstan": {
|
||||||
|
"includes": [
|
||||||
|
"extension.neon"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-main": "3.x-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Composer\\Pcre\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Jordi Boggiano",
|
||||||
|
"email": "j.boggiano@seld.be",
|
||||||
|
"homepage": "http://seld.be"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "PCRE wrapping library that offers type-safe preg_* replacements.",
|
||||||
|
"keywords": [
|
||||||
|
"PCRE",
|
||||||
|
"preg",
|
||||||
|
"regex",
|
||||||
|
"regular expression"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/composer/pcre/issues",
|
||||||
|
"source": "https://github.com/composer/pcre/tree/3.3.2"
|
||||||
|
},
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://packagist.com",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://github.com/composer",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/composer/composer",
|
||||||
|
"type": "tidelift"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2024-11-12T16:29:46+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "composer/semver",
|
"name": "composer/semver",
|
||||||
"version": "3.4.4",
|
"version": "3.4.4",
|
||||||
@@ -4210,6 +4289,113 @@
|
|||||||
],
|
],
|
||||||
"time": "2026-04-11T18:38:28+00:00"
|
"time": "2026-04-11T18:38:28+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "markbaker/complex",
|
||||||
|
"version": "3.0.2",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/MarkBaker/PHPComplex.git",
|
||||||
|
"reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/MarkBaker/PHPComplex/zipball/95c56caa1cf5c766ad6d65b6344b807c1e8405b9",
|
||||||
|
"reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^7.2 || ^8.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",
|
||||||
|
"phpcompatibility/php-compatibility": "^9.3",
|
||||||
|
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
|
||||||
|
"squizlabs/php_codesniffer": "^3.7"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Complex\\": "classes/src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Mark Baker",
|
||||||
|
"email": "mark@lange.demon.co.uk"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "PHP Class for working with complex numbers",
|
||||||
|
"homepage": "https://github.com/MarkBaker/PHPComplex",
|
||||||
|
"keywords": [
|
||||||
|
"complex",
|
||||||
|
"mathematics"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/MarkBaker/PHPComplex/issues",
|
||||||
|
"source": "https://github.com/MarkBaker/PHPComplex/tree/3.0.2"
|
||||||
|
},
|
||||||
|
"time": "2022-12-06T16:21:08+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "markbaker/matrix",
|
||||||
|
"version": "3.0.1",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/MarkBaker/PHPMatrix.git",
|
||||||
|
"reference": "728434227fe21be27ff6d86621a1b13107a2562c"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/MarkBaker/PHPMatrix/zipball/728434227fe21be27ff6d86621a1b13107a2562c",
|
||||||
|
"reference": "728434227fe21be27ff6d86621a1b13107a2562c",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^7.1 || ^8.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",
|
||||||
|
"phpcompatibility/php-compatibility": "^9.3",
|
||||||
|
"phpdocumentor/phpdocumentor": "2.*",
|
||||||
|
"phploc/phploc": "^4.0",
|
||||||
|
"phpmd/phpmd": "2.*",
|
||||||
|
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
|
||||||
|
"sebastian/phpcpd": "^4.0",
|
||||||
|
"squizlabs/php_codesniffer": "^3.7"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Matrix\\": "classes/src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Mark Baker",
|
||||||
|
"email": "mark@demon-angel.eu"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "PHP Class for working with matrices",
|
||||||
|
"homepage": "https://github.com/MarkBaker/PHPMatrix",
|
||||||
|
"keywords": [
|
||||||
|
"mathematics",
|
||||||
|
"matrix",
|
||||||
|
"vector"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/MarkBaker/PHPMatrix/issues",
|
||||||
|
"source": "https://github.com/MarkBaker/PHPMatrix/tree/3.0.1"
|
||||||
|
},
|
||||||
|
"time": "2022-12-02T22:17:43+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "masterminds/html5",
|
"name": "masterminds/html5",
|
||||||
"version": "2.10.0",
|
"version": "2.10.0",
|
||||||
@@ -5190,6 +5376,115 @@
|
|||||||
},
|
},
|
||||||
"time": "2025-12-30T16:12:18+00:00"
|
"time": "2025-12-30T16:12:18+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "phpoffice/phpspreadsheet",
|
||||||
|
"version": "5.7.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/PHPOffice/PhpSpreadsheet.git",
|
||||||
|
"reference": "9f55d3b9b7bcb1084fda8340e4b7ce4ed10cd0c8"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/9f55d3b9b7bcb1084fda8340e4b7ce4ed10cd0c8",
|
||||||
|
"reference": "9f55d3b9b7bcb1084fda8340e4b7ce4ed10cd0c8",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"composer/pcre": "^1||^2||^3",
|
||||||
|
"ext-ctype": "*",
|
||||||
|
"ext-dom": "*",
|
||||||
|
"ext-fileinfo": "*",
|
||||||
|
"ext-filter": "*",
|
||||||
|
"ext-gd": "*",
|
||||||
|
"ext-iconv": "*",
|
||||||
|
"ext-libxml": "*",
|
||||||
|
"ext-mbstring": "*",
|
||||||
|
"ext-simplexml": "*",
|
||||||
|
"ext-xml": "*",
|
||||||
|
"ext-xmlreader": "*",
|
||||||
|
"ext-xmlwriter": "*",
|
||||||
|
"ext-zip": "*",
|
||||||
|
"ext-zlib": "*",
|
||||||
|
"maennchen/zipstream-php": "^2.1 || ^3.0",
|
||||||
|
"markbaker/complex": "^3.0",
|
||||||
|
"markbaker/matrix": "^3.0",
|
||||||
|
"php": "^8.1",
|
||||||
|
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": "dev-main",
|
||||||
|
"dompdf/dompdf": "^2.0 || ^3.0",
|
||||||
|
"ext-intl": "*",
|
||||||
|
"friendsofphp/php-cs-fixer": "^3.2",
|
||||||
|
"mitoteam/jpgraph": "^10.5",
|
||||||
|
"mpdf/mpdf": "^8.1.1",
|
||||||
|
"phpcompatibility/php-compatibility": "^9.3",
|
||||||
|
"phpstan/phpstan": "^1.1 || ^2.0",
|
||||||
|
"phpstan/phpstan-deprecation-rules": "^1.0 || ^2.0",
|
||||||
|
"phpstan/phpstan-phpunit": "^1.0 || ^2.0",
|
||||||
|
"phpunit/phpunit": "^10.5",
|
||||||
|
"squizlabs/php_codesniffer": "^3.7",
|
||||||
|
"tecnickcom/tcpdf": "^6.5"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"dompdf/dompdf": "Option for rendering PDF with PDF Writer",
|
||||||
|
"ext-intl": "PHP Internationalization Functions, required for NumberFormat Wizard and StringHelper::setLocale()",
|
||||||
|
"mitoteam/jpgraph": "Option for rendering charts, or including charts with PDF or HTML Writers",
|
||||||
|
"mpdf/mpdf": "Option for rendering PDF with PDF Writer",
|
||||||
|
"tecnickcom/tcpdf": "Option for rendering PDF with PDF Writer"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"PhpOffice\\PhpSpreadsheet\\": "src/PhpSpreadsheet"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Maarten Balliauw",
|
||||||
|
"homepage": "https://blog.maartenballiauw.be"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Mark Baker",
|
||||||
|
"homepage": "https://markbakeruk.net"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Franck Lefevre",
|
||||||
|
"homepage": "https://rootslabs.net"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Erik Tilt"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Adrien Crivelli"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Owen Leibman"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine",
|
||||||
|
"homepage": "https://github.com/PHPOffice/PhpSpreadsheet",
|
||||||
|
"keywords": [
|
||||||
|
"OpenXML",
|
||||||
|
"excel",
|
||||||
|
"gnumeric",
|
||||||
|
"ods",
|
||||||
|
"php",
|
||||||
|
"spreadsheet",
|
||||||
|
"xls",
|
||||||
|
"xlsx"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues",
|
||||||
|
"source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/5.7.0"
|
||||||
|
},
|
||||||
|
"time": "2026-04-20T02:42:17+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "phpoption/phpoption",
|
"name": "phpoption/phpoption",
|
||||||
"version": "1.9.5",
|
"version": "1.9.5",
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('supplier_invoice_mappings', function (Blueprint $t) {
|
||||||
|
$t->id();
|
||||||
|
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
|
||||||
|
$t->foreignId('supplier_id')->constrained('suppliers')->cascadeOnDelete();
|
||||||
|
$t->json('mapping_config');
|
||||||
|
// {article_col:"B", name_col:"C", qty_col:"E", price_col:"F",
|
||||||
|
// brand_col:"D"|null, header_row:2, sheet_name:"Товары"|null}
|
||||||
|
$t->string('sample_file_name', 200)->nullable();
|
||||||
|
$t->timestamp('last_used_at')->nullable();
|
||||||
|
$t->timestamps();
|
||||||
|
$t->unique(['company_id', 'supplier_id'], 'sim_company_supplier_uniq');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('supplier_invoice_mappings');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
foreach (['wo_works', 'wo_parts'] as $table) {
|
||||||
|
Schema::table($table, function (Blueprint $t) use ($table) {
|
||||||
|
if (! Schema::hasColumn($table, 'requires_approval')) {
|
||||||
|
$t->boolean('requires_approval')->default(false)->after('status');
|
||||||
|
}
|
||||||
|
if (! Schema::hasColumn($table, 'approved_at')) {
|
||||||
|
$t->timestamp('approved_at')->nullable()->after('requires_approval');
|
||||||
|
}
|
||||||
|
if (! Schema::hasColumn($table, 'approval_token')) {
|
||||||
|
$t->string('approval_token', 32)->nullable()->after('approved_at');
|
||||||
|
}
|
||||||
|
if (! Schema::hasColumn($table, 'declined_at')) {
|
||||||
|
$t->timestamp('declined_at')->nullable()->after('approval_token');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Index for fast token lookup
|
||||||
|
try {
|
||||||
|
Schema::table('wo_works', fn (Blueprint $t) => $t->index('approval_token', 'wow_approval_token_idx'));
|
||||||
|
} catch (\Throwable $e) {}
|
||||||
|
try {
|
||||||
|
Schema::table('wo_parts', fn (Blueprint $t) => $t->index('approval_token', 'wop_approval_token_idx'));
|
||||||
|
} catch (\Throwable $e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
foreach (['wo_works', 'wo_parts'] as $table) {
|
||||||
|
Schema::table($table, function (Blueprint $t) use ($table) {
|
||||||
|
foreach (['requires_approval', 'approved_at', 'approval_token', 'declined_at'] as $col) {
|
||||||
|
if (Schema::hasColumn($table, $col)) $t->dropColumn($col);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,228 @@
|
|||||||
|
<x-filament-panels::page>
|
||||||
|
<style>
|
||||||
|
.wiz { max-width: 1000px; margin: 0 auto; }
|
||||||
|
.wiz-steps { display: flex; gap: 8px; margin-bottom: 24px; align-items: center; }
|
||||||
|
.wiz-step { display: flex; align-items: center; gap: 8px; padding: 6px 14px; border-radius: 20px; font-size: 13px; font-weight: 500; color: #94a3b8; }
|
||||||
|
.wiz-step.active { background: #ebf5ff; color: #1d4ed8; }
|
||||||
|
.wiz-step.done { background: #dcfce7; color: #15803d; }
|
||||||
|
.wiz-step-sep { width: 24px; height: 2px; background: #e2e8f0; }
|
||||||
|
.wiz-card { background: white; border: 1px solid #e2e8f0; border-radius: 12px; padding: 24px; }
|
||||||
|
.dark .wiz-card { background: #1f2937; border-color: #374151; }
|
||||||
|
.wiz-field { margin-bottom: 16px; }
|
||||||
|
.wiz-field label { display: block; font-size: 11px; font-weight: 600; color: #718096; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }
|
||||||
|
.wiz-field input, .wiz-field select { width: 100%; padding: 10px 12px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 14px; }
|
||||||
|
.dark .wiz-field input, .dark .wiz-field select { background: #111827; color: #f1f5f9; border-color: #374151; }
|
||||||
|
.wiz-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
|
||||||
|
.wiz-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 24px; }
|
||||||
|
.wiz-btn { padding: 8px 16px; border-radius: 6px; font-weight: 500; cursor: pointer; border: 1px solid #cbd5e1; background: white; color: #1a202c; }
|
||||||
|
.wiz-btn:hover { background: #f7fafc; }
|
||||||
|
.wiz-btn-primary { background: #3b82f6; color: white; border-color: #3b82f6; }
|
||||||
|
.wiz-btn-primary:hover { background: #2563eb; }
|
||||||
|
.wiz-preview-table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
||||||
|
.wiz-preview-table th, .wiz-preview-table td { padding: 6px 8px; border: 1px solid #e2e8f0; text-align: left; }
|
||||||
|
.wiz-preview-table th { background: #f7fafc; font-weight: 600; font-size: 11px; color: #4a5568; }
|
||||||
|
.dark .wiz-preview-table th { background: #111827; color: #cbd5e1; }
|
||||||
|
.wiz-badge { padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 500; }
|
||||||
|
.wiz-badge.found { background: #dcfce7; color: #15803d; }
|
||||||
|
.wiz-badge.new { background: #fef3c7; color: #92400e; }
|
||||||
|
.wiz-badge.no_article { background: #fee2e2; color: #991b1b; }
|
||||||
|
.wiz-summary { display: flex; gap: 16px; margin-bottom: 16px; padding: 12px; background: #f7fafc; border-radius: 6px; }
|
||||||
|
.dark .wiz-summary { background: #111827; }
|
||||||
|
.wiz-summary-item { font-size: 13px; }
|
||||||
|
.wiz-summary-item strong { font-size: 20px; display: block; font-weight: 700; }
|
||||||
|
.wiz-upload { border: 2px dashed #cbd5e1; border-radius: 8px; padding: 32px; text-align: center; cursor: pointer; }
|
||||||
|
.wiz-upload:hover { border-color: #3b82f6; background: #ebf5ff; }
|
||||||
|
.dark .wiz-upload:hover { background: #1e293b; }
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div class="wiz">
|
||||||
|
<div class="wiz-steps">
|
||||||
|
<div class="wiz-step {{ $step >= 1 ? 'active' : '' }} {{ $step > 1 ? 'done' : '' }}">1. Upload</div>
|
||||||
|
<div class="wiz-step-sep"></div>
|
||||||
|
<div class="wiz-step {{ $step == 2 ? 'active' : '' }} {{ $step > 2 ? 'done' : '' }}">2. Mapare coloane</div>
|
||||||
|
<div class="wiz-step-sep"></div>
|
||||||
|
<div class="wiz-step {{ $step == 3 ? 'active' : '' }} {{ $step > 3 ? 'done' : '' }}">3. Previzualizare</div>
|
||||||
|
<div class="wiz-step-sep"></div>
|
||||||
|
<div class="wiz-step {{ $step == 4 ? 'active' : '' }}">4. Confirmare</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-card">
|
||||||
|
{{-- STEP 1: Upload --}}
|
||||||
|
@if ($step === 1)
|
||||||
|
<h2 style="font-size:18px;font-weight:600;margin-bottom:16px;">Pasul 1: Încarcă fișierul</h2>
|
||||||
|
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Furnizor *</label>
|
||||||
|
<select wire:model="supplierId">
|
||||||
|
<option value="">— alege —</option>
|
||||||
|
@foreach ($this->getSupplierOptions() as $id => $name)
|
||||||
|
<option value="{{ $id }}">{{ $name }}</option>
|
||||||
|
@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Fișier Excel (.xlsx) sau CSV *</label>
|
||||||
|
<input type="file" wire:model="upload" accept=".xlsx,.xls,.csv" />
|
||||||
|
@if ($upload)
|
||||||
|
<div style="margin-top:6px;font-size:12px;color:#4a5568;">Selectat: {{ $upload->getClientOriginalName() }}</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-actions">
|
||||||
|
<button class="wiz-btn wiz-btn-primary" wire:click="goToStep2" wire:loading.attr="disabled">Următorul →</button>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
{{-- STEP 2: Mapping --}}
|
||||||
|
@if ($step === 2)
|
||||||
|
<h2 style="font-size:18px;font-weight:600;margin-bottom:16px;">Pasul 2: Mapează coloanele</h2>
|
||||||
|
<p style="color:#4a5568;font-size:13px;margin-bottom:16px;">Spune-i sistemului ce reprezintă fiecare coloană din fișier. Vom salva configurația pentru imporțările viitoare.</p>
|
||||||
|
|
||||||
|
<div style="overflow-x:auto;margin-bottom:20px;">
|
||||||
|
<table class="wiz-preview-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
@foreach ($headersPreview['columns'] as $col)
|
||||||
|
<th>{{ $col }}</th>
|
||||||
|
@endforeach
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach ($headersPreview['rows'] as $row)
|
||||||
|
<tr>
|
||||||
|
@foreach ($headersPreview['columns'] as $col)
|
||||||
|
<td>{{ $row[$col] ?? '' }}</td>
|
||||||
|
@endforeach
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-grid">
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Coloana Articol *</label>
|
||||||
|
<select wire:model="mapping.article_col">
|
||||||
|
<option value="">—</option>
|
||||||
|
@foreach ($headersPreview['columns'] as $c)<option value="{{ $c }}">{{ $c }}</option>@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Coloana Denumire *</label>
|
||||||
|
<select wire:model="mapping.name_col">
|
||||||
|
<option value="">—</option>
|
||||||
|
@foreach ($headersPreview['columns'] as $c)<option value="{{ $c }}">{{ $c }}</option>@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Coloana Cantitate *</label>
|
||||||
|
<select wire:model="mapping.qty_col">
|
||||||
|
<option value="">—</option>
|
||||||
|
@foreach ($headersPreview['columns'] as $c)<option value="{{ $c }}">{{ $c }}</option>@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Coloana Preț *</label>
|
||||||
|
<select wire:model="mapping.price_col">
|
||||||
|
<option value="">—</option>
|
||||||
|
@foreach ($headersPreview['columns'] as $c)<option value="{{ $c }}">{{ $c }}</option>@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>Coloana Brand (opțional)</label>
|
||||||
|
<select wire:model="mapping.brand_col">
|
||||||
|
<option value="">—</option>
|
||||||
|
@foreach ($headersPreview['columns'] as $c)<option value="{{ $c }}">{{ $c }}</option>@endforeach
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label>De la rândul (sărim header) *</label>
|
||||||
|
<input type="number" min="1" wire:model="mapping.header_row" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label style="display:flex;align-items:center;gap:8px;text-transform:none;">
|
||||||
|
<input type="checkbox" wire:model="rememberMapping" />
|
||||||
|
<span>Salvează configurația pentru acest furnizor</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-actions">
|
||||||
|
<button class="wiz-btn" wire:click="$set('step', 1)">← Înapoi</button>
|
||||||
|
<button class="wiz-btn wiz-btn-primary" wire:click="goToStep3">Previzualizare →</button>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
{{-- STEP 3: Preview --}}
|
||||||
|
@if ($step === 3)
|
||||||
|
<h2 style="font-size:18px;font-weight:600;margin-bottom:16px;">Pasul 3: Previzualizare — {{ $previewSummary['total'] }} poziții</h2>
|
||||||
|
|
||||||
|
<div class="wiz-summary">
|
||||||
|
<div class="wiz-summary-item">
|
||||||
|
<strong style="color:#15803d;">{{ $previewSummary['found'] }}</strong>
|
||||||
|
Găsite (cu articol existent)
|
||||||
|
</div>
|
||||||
|
<div class="wiz-summary-item">
|
||||||
|
<strong style="color:#92400e;">{{ $previewSummary['new'] }}</strong>
|
||||||
|
Articole noi (se vor crea)
|
||||||
|
</div>
|
||||||
|
<div class="wiz-summary-item">
|
||||||
|
<strong style="color:#991b1b;">{{ $previewSummary['no_article'] }}</strong>
|
||||||
|
Fără articol (manual)
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style="max-height:400px;overflow-y:auto;margin-bottom:16px;">
|
||||||
|
<table class="wiz-preview-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Articol</th><th>Denumire</th><th>Brand</th><th>Cant.</th><th>Preț</th><th>Status</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach ($previewRows as $row)
|
||||||
|
<tr>
|
||||||
|
<td style="font-family:monospace;">{{ $row['article'] }}</td>
|
||||||
|
<td>{{ $row['name'] }}</td>
|
||||||
|
<td>{{ $row['brand'] }}</td>
|
||||||
|
<td>{{ rtrim(rtrim(number_format($row['qty'], 2), '0'), '.') }}</td>
|
||||||
|
<td>{{ number_format($row['price'], 2) }}</td>
|
||||||
|
<td><span class="wiz-badge {{ $row['status'] }}">{{ ['found' => '✅ Găsit', 'new' => '⚠️ Nou', 'no_article' => '❓ Nu găsit'][$row['status']] ?? $row['status'] }}</span></td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-field">
|
||||||
|
<label style="display:flex;align-items:center;gap:8px;text-transform:none;">
|
||||||
|
<input type="checkbox" wire:model="createNew" />
|
||||||
|
<span>Creează automat articolele noi în nomenclatură</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wiz-actions">
|
||||||
|
<button class="wiz-btn" wire:click="$set('step', 2)">← Înapoi</button>
|
||||||
|
<button class="wiz-btn wiz-btn-primary" wire:click="confirmImport">Confirmă și importă</button>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
{{-- STEP 4: Done --}}
|
||||||
|
@if ($step === 4)
|
||||||
|
<div style="text-align:center;padding:32px;">
|
||||||
|
<div style="font-size:48px;margin-bottom:16px;">✅</div>
|
||||||
|
<h2 style="font-size:20px;font-weight:600;margin-bottom:8px;">Import finalizat cu succes</h2>
|
||||||
|
<p style="color:#4a5568;">{{ $previewSummary['total'] }} poziții importate în Purchase nouă</p>
|
||||||
|
<div style="margin-top:24px;">
|
||||||
|
@if (session('purchase_id'))
|
||||||
|
<a class="wiz-btn wiz-btn-primary" href="{{ route('filament.tenant.resources.purchases.edit', ['record' => session('purchase_id')]) }}">↗ Deschide Purchase</a>
|
||||||
|
@endif
|
||||||
|
<button class="wiz-btn" wire:click="reset_">Import nou</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-filament-panels::page>
|
||||||
@@ -88,8 +88,61 @@
|
|||||||
<div class="num">Fișa #{{ $wo->number }}</div>
|
<div class="num">Fișa #{{ $wo->number }}</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.approval-banner { background:#fef3c7; border:1px solid #fcd34d; border-radius:10px; padding:14px 16px; margin-bottom:16px; }
|
||||||
|
.approval-banner h3 { font-size:14px; font-weight:700; color:#92400e; margin-bottom:8px; display:flex; align-items:center; gap:6px; }
|
||||||
|
.approval-line { background:white; border:1px solid #fde68a; border-radius:8px; padding:10px 12px; margin-bottom:8px; }
|
||||||
|
.approval-line:last-child { margin-bottom:0; }
|
||||||
|
.approval-line-title { font-weight:600; font-size:14px; color:#1a202c; }
|
||||||
|
.approval-line-amount { color:#92400e; font-weight:600; margin-top:2px; font-size:13px; }
|
||||||
|
.approval-line-actions { display:flex; gap:8px; margin-top:8px; }
|
||||||
|
.btn-approve { flex:1; padding:8px; background:#16a34a; color:white; border:none; border-radius:6px; font-weight:600; cursor:pointer; font-size:13px; }
|
||||||
|
.btn-approve:hover { background:#15803d; }
|
||||||
|
.btn-decline { flex:1; padding:8px; background:white; color:#dc2626; border:1px solid #dc2626; border-radius:6px; font-weight:600; cursor:pointer; font-size:13px; }
|
||||||
|
.btn-decline:hover { background:#fee2e2; }
|
||||||
|
.flash-success { background:#dcfce7; border:1px solid #86efac; color:#166534; padding:12px 14px; border-radius:8px; margin-bottom:16px; font-size:13px; }
|
||||||
|
.flash-error { background:#fee2e2; border:1px solid #fca5a5; color:#991b1b; padding:12px 14px; border-radius:8px; margin-bottom:16px; font-size:13px; }
|
||||||
|
</style>
|
||||||
|
|
||||||
<div class="wrap">
|
<div class="wrap">
|
||||||
|
|
||||||
|
@if (isset($approvalStatus) && $approvalStatus)
|
||||||
|
<div class="flash-{{ $approvalStatus['kind'] === 'error' ? 'error' : 'success' }}">{{ $approvalStatus['message'] }}</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ($pendingWorks->isNotEmpty() || $pendingParts->isNotEmpty())
|
||||||
|
<div class="approval-banner">
|
||||||
|
<h3>⚠ Necesită aprobarea ta</h3>
|
||||||
|
<p style="font-size:13px;color:#92400e;margin-bottom:10px;">Am descoperit lucrări suplimentare. Te rugăm să decizi mai jos.</p>
|
||||||
|
@foreach ($pendingWorks as $w)
|
||||||
|
<div class="approval-line">
|
||||||
|
<div class="approval-line-title">{{ $w->name }}</div>
|
||||||
|
<div class="approval-line-amount">{{ rtrim(rtrim(number_format($w->hours, 2), '0'), '.') }} ore · {{ number_format($w->total, 0, '.', ' ') }} MDL</div>
|
||||||
|
<div class="approval-line-actions">
|
||||||
|
<form method="POST" action="{{ route('tracking.approve', ['token' => $wo->tracking_token, 'kind' => 'work', 'lineToken' => $w->approval_token]) }}" style="flex:1;display:flex;gap:8px;">
|
||||||
|
@csrf
|
||||||
|
<button type="submit" name="decision" value="approve" class="btn-approve">✅ Aprob</button>
|
||||||
|
<button type="submit" name="decision" value="decline" class="btn-decline">❌ Nu aprob</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
|
@foreach ($pendingParts as $p)
|
||||||
|
<div class="approval-line">
|
||||||
|
<div class="approval-line-title">{{ $p->name }} @if ($p->article) <span style="font-family:monospace;color:#6b7280;font-weight:400;">· {{ $p->article }}</span>@endif</div>
|
||||||
|
<div class="approval-line-amount">{{ rtrim(rtrim(number_format($p->qty, 2), '0'), '.') }} {{ $p->unit ?? 'buc' }} · {{ number_format($p->total, 0, '.', ' ') }} MDL</div>
|
||||||
|
<div class="approval-line-actions">
|
||||||
|
<form method="POST" action="{{ route('tracking.approve', ['token' => $wo->tracking_token, 'kind' => 'part', 'lineToken' => $p->approval_token]) }}" style="flex:1;display:flex;gap:8px;">
|
||||||
|
@csrf
|
||||||
|
<button type="submit" name="decision" value="approve" class="btn-approve">✅ Aprob</button>
|
||||||
|
<button type="submit" name="decision" value="decline" class="btn-decline">❌ Nu aprob</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
<div class="card" style="text-align:center;">
|
<div class="card" style="text-align:center;">
|
||||||
<span class="status-badge">{{ $statuses[$wo->status] ?? $wo->status }}</span>
|
<span class="status-badge">{{ $statuses[$wo->status] ?? $wo->status }}</span>
|
||||||
@if ($wo->eta_at && in_array($wo->status, ['in_work', 'awaiting_parts', 'approved', 'diagnosis'], true))
|
@if ($wo->eta_at && in_array($wo->status, ['in_work', 'awaiting_parts', 'approved', 'diagnosis'], true))
|
||||||
|
|||||||
@@ -123,6 +123,11 @@ Route::get('/t/{token}', [\App\Http\Controllers\TrackingController::class, 'show
|
|||||||
Route::get('/t/{token}/qr.svg', [\App\Http\Controllers\TrackingController::class, 'qr'])
|
Route::get('/t/{token}/qr.svg', [\App\Http\Controllers\TrackingController::class, 'qr'])
|
||||||
->where('token', '[A-Za-z0-9]{16,32}')
|
->where('token', '[A-Za-z0-9]{16,32}')
|
||||||
->name('tracking.qr');
|
->name('tracking.qr');
|
||||||
|
Route::post('/t/{token}/approve/{kind}/{lineToken}', [\App\Http\Controllers\TrackingController::class, 'approve'])
|
||||||
|
->where('token', '[A-Za-z0-9]{16,32}')
|
||||||
|
->where('kind', 'work|part')
|
||||||
|
->where('lineToken', '[A-Za-z0-9]{16,32}')
|
||||||
|
->name('tracking.approve');
|
||||||
|
|
||||||
// Locale switch — POST /locale/{lang} sets session and persists to user.
|
// Locale switch — POST /locale/{lang} sets session and persists to user.
|
||||||
Route::post('/locale/{lang}', function (Request $request, string $lang) {
|
Route::post('/locale/{lang}', function (Request $request, string $lang) {
|
||||||
|
|||||||
@@ -0,0 +1,158 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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