diff --git a/app/Filament/Tenant/Pages/ExcelImportWizard.php b/app/Filament/Tenant/Pages/ExcelImportWizard.php new file mode 100644 index 0000000..0ce409d --- /dev/null +++ b/app/Filament/Tenant/Pages/ExcelImportWizard.php @@ -0,0 +1,143 @@ + [], '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]; + } +} diff --git a/app/Http/Controllers/TrackingController.php b/app/Http/Controllers/TrackingController.php index 8a3bf93..a741aa9 100644 --- a/app/Http/Controllers/TrackingController.php +++ b/app/Http/Controllers/TrackingController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Models\Tenant\WorkOrder; +use App\Models\Tenant\WorkOrderPart; +use App\Models\Tenant\WorkOrderWork; use App\Tenancy\TenantManager; use Illuminate\Http\Request; 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. - * 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) { @@ -21,7 +21,7 @@ class TrackingController extends Controller 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) ->first(); @@ -29,13 +29,56 @@ class TrackingController extends Controller 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', [ 'wo' => $wo, 'tenant' => $tenant, '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) { $tenant = app(TenantManager::class)->current(); diff --git a/app/Models/Tenant/SupplierInvoiceMapping.php b/app/Models/Tenant/SupplierInvoiceMapping.php new file mode 100644 index 0000000..2773c0f --- /dev/null +++ b/app/Models/Tenant/SupplierInvoiceMapping.php @@ -0,0 +1,27 @@ + 'array', + 'last_used_at' => 'datetime', + ]; + + public function supplier(): BelongsTo + { + return $this->belongsTo(Supplier::class); + } +} diff --git a/app/Models/Tenant/WorkOrderPart.php b/app/Models/Tenant/WorkOrderPart.php index 0e3ea98..45201c8 100644 --- a/app/Models/Tenant/WorkOrderPart.php +++ b/app/Models/Tenant/WorkOrderPart.php @@ -24,6 +24,7 @@ class WorkOrderPart extends Model 'name', 'article', 'brand', 'qty', 'unit', 'buy_price', 'sell_price', 'discount_pct', 'total', 'status', 'notes', + 'requires_approval', 'approved_at', 'approval_token', 'declined_at', ]; protected $casts = [ @@ -32,8 +33,16 @@ class WorkOrderPart extends Model 'sell_price' => 'decimal:2', 'discount_pct' => '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 { return $this->belongsTo(WorkOrder::class); @@ -50,6 +59,9 @@ class WorkOrderPart extends Model $sub = (float) $row->qty * (float) $row->sell_price; $disc = (float) $row->discount_pct; $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. diff --git a/app/Models/Tenant/WorkOrderWork.php b/app/Models/Tenant/WorkOrderWork.php index 2fc33e7..6935d34 100644 --- a/app/Models/Tenant/WorkOrderWork.php +++ b/app/Models/Tenant/WorkOrderWork.php @@ -21,14 +21,23 @@ class WorkOrderWork extends Model protected $fillable = [ 'company_id', 'work_order_id', 'labor_id', 'master_id', 'name', 'hours', 'price_per_hour', 'total', 'status', 'notes', + 'requires_approval', 'approved_at', 'approval_token', 'declined_at', ]; protected $casts = [ 'hours' => 'decimal:2', 'price_per_hour' => '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 { return $this->belongsTo(WorkOrder::class); @@ -48,6 +57,9 @@ class WorkOrderWork extends Model { static::saving(function (self $row) { $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::deleted(fn (self $row) => $row->workOrder?->recalcTotal()); diff --git a/app/Services/ExcelInvoiceImportService.php b/app/Services/ExcelInvoiceImportService.php new file mode 100644 index 0000000..b1a3df6 --- /dev/null +++ b/app/Services/ExcelInvoiceImportService.php @@ -0,0 +1,231 @@ +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); + } +} diff --git a/composer.json b/composer.json index 2f41fbd..8048751 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "laravel/sanctum": "^4.3", "laravel/tinker": "^2.10.1", "minishlink/web-push": "^10.0", + "phpoffice/phpspreadsheet": "^5.7", "resend/resend-laravel": "^1.4", "spatie/laravel-activitylog": "^5.0", "spatie/laravel-medialibrary": "^11.22", diff --git a/composer.lock b/composer.lock index 76e3ab6..a58c677 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "82d0b6d061454a485d2a93b700e4a5a8", + "content-hash": "5b5b5d8a2a2a4bac8ef246a2b165992c", "packages": [ { "name": "barryvdh/laravel-dompdf", @@ -654,6 +654,85 @@ ], "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", "version": "3.4.4", @@ -4210,6 +4289,113 @@ ], "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", "version": "2.10.0", @@ -5190,6 +5376,115 @@ }, "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", "version": "1.9.5", diff --git a/database/migrations/2026_06_05_000001_create_supplier_invoice_mappings.php b/database/migrations/2026_06_05_000001_create_supplier_invoice_mappings.php new file mode 100644 index 0000000..8961104 --- /dev/null +++ b/database/migrations/2026_06_05_000001_create_supplier_invoice_mappings.php @@ -0,0 +1,29 @@ +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'); + } +}; diff --git a/database/migrations/2026_06_05_000002_add_approval_to_wo_lines.php b/database/migrations/2026_06_05_000002_add_approval_to_wo_lines.php new file mode 100644 index 0000000..a53c997 --- /dev/null +++ b/database/migrations/2026_06_05_000002_add_approval_to_wo_lines.php @@ -0,0 +1,46 @@ +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); + } + }); + } + } +}; diff --git a/resources/views/filament/tenant/pages/excel-import-wizard.blade.php b/resources/views/filament/tenant/pages/excel-import-wizard.blade.php new file mode 100644 index 0000000..fff70ef --- /dev/null +++ b/resources/views/filament/tenant/pages/excel-import-wizard.blade.php @@ -0,0 +1,228 @@ + + + +
+
+
1. Upload
+
+
2. Mapare coloane
+
+
3. Previzualizare
+
+
4. Confirmare
+
+ +
+ {{-- STEP 1: Upload --}} + @if ($step === 1) +

Pasul 1: Încarcă fișierul

+ +
+ + +
+ +
+ + + @if ($upload) +
Selectat: {{ $upload->getClientOriginalName() }}
+ @endif +
+ +
+ +
+ @endif + + {{-- STEP 2: Mapping --}} + @if ($step === 2) +

Pasul 2: Mapează coloanele

+

Spune-i sistemului ce reprezintă fiecare coloană din fișier. Vom salva configurația pentru imporțările viitoare.

+ +
+ + + + @foreach ($headersPreview['columns'] as $col) + + @endforeach + + + + @foreach ($headersPreview['rows'] as $row) + + @foreach ($headersPreview['columns'] as $col) + + @endforeach + + @endforeach + +
{{ $col }}
{{ $row[$col] ?? '' }}
+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ +
+ +
+ + +
+ @endif + + {{-- STEP 3: Preview --}} + @if ($step === 3) +

Pasul 3: Previzualizare — {{ $previewSummary['total'] }} poziții

+ +
+
+ {{ $previewSummary['found'] }} + Găsite (cu articol existent) +
+
+ {{ $previewSummary['new'] }} + Articole noi (se vor crea) +
+
+ {{ $previewSummary['no_article'] }} + Fără articol (manual) +
+
+ +
+ + + + + + + + @foreach ($previewRows as $row) + + + + + + + + + @endforeach + +
ArticolDenumireBrandCant.PrețStatus
{{ $row['article'] }}{{ $row['name'] }}{{ $row['brand'] }}{{ rtrim(rtrim(number_format($row['qty'], 2), '0'), '.') }}{{ number_format($row['price'], 2) }}{{ ['found' => '✅ Găsit', 'new' => '⚠️ Nou', 'no_article' => '❓ Nu găsit'][$row['status']] ?? $row['status'] }}
+
+ +
+ +
+ +
+ + +
+ @endif + + {{-- STEP 4: Done --}} + @if ($step === 4) +
+
+

Import finalizat cu succes

+

{{ $previewSummary['total'] }} poziții importate în Purchase nouă

+
+ @if (session('purchase_id')) + ↗ Deschide Purchase + @endif + +
+
+ @endif +
+
+
diff --git a/resources/views/tracking/show.blade.php b/resources/views/tracking/show.blade.php index d87b27c..4330c01 100644 --- a/resources/views/tracking/show.blade.php +++ b/resources/views/tracking/show.blade.php @@ -88,8 +88,61 @@
Fișa #{{ $wo->number }}
+ +
+ @if (isset($approvalStatus) && $approvalStatus) +
{{ $approvalStatus['message'] }}
+ @endif + + @if ($pendingWorks->isNotEmpty() || $pendingParts->isNotEmpty()) +
+

⚠ Necesită aprobarea ta

+

Am descoperit lucrări suplimentare. Te rugăm să decizi mai jos.

+ @foreach ($pendingWorks as $w) +
+
{{ $w->name }}
+
{{ rtrim(rtrim(number_format($w->hours, 2), '0'), '.') }} ore · {{ number_format($w->total, 0, '.', ' ') }} MDL
+
+
+ @csrf + + +
+
+
+ @endforeach + @foreach ($pendingParts as $p) +
+
{{ $p->name }} @if ($p->article) · {{ $p->article }}@endif
+
{{ rtrim(rtrim(number_format($p->qty, 2), '0'), '.') }} {{ $p->unit ?? 'buc' }} · {{ number_format($p->total, 0, '.', ' ') }} MDL
+
+
+ @csrf + + +
+
+
+ @endforeach +
+ @endif +
{{ $statuses[$wo->status] ?? $wo->status }} @if ($wo->eta_at && in_array($wo->status, ['in_work', 'awaiting_parts', 'approved', 'diagnosis'], true)) diff --git a/routes/web.php b/routes/web.php index f33aed8..215ea4c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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']) ->where('token', '[A-Za-z0-9]{16,32}') ->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. Route::post('/locale/{lang}', function (Request $request, string $lang) { diff --git a/tests/Feature/ExcelInvoiceImportTest.php b/tests/Feature/ExcelInvoiceImportTest.php new file mode 100644 index 0000000..d36b73d --- /dev/null +++ b/tests/Feature/ExcelInvoiceImportTest.php @@ -0,0 +1,158 @@ + '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); + } +} diff --git a/tests/Feature/TrackingApprovalTest.php b/tests/Feature/TrackingApprovalTest.php new file mode 100644 index 0000000..d917191 --- /dev/null +++ b/tests/Feature/TrackingApprovalTest.php @@ -0,0 +1,153 @@ + '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'); + } +}