This commit is contained in:
2026-11-02 20:27:11 +02:00
parent b44378b899
commit badaada043
300 changed files with 17458 additions and 1056 deletions
+9 -1
View File
@@ -3,6 +3,7 @@
namespace App\Services;
use App\Models\RequisitionItem;
use App\Models\SparePart;
use App\Models\StockItem;
use App\Models\User;
use Illuminate\Support\Facades\DB;
@@ -37,8 +38,11 @@ class RequisitionCatalogue
$actor = app(CurrentActor::class)->lock($actor);
abort_unless($this->canManage($actor), 403);
$data = Validator::make($input, ['version' => 'required|integer|min:0', 'stock_basis' => 'required|string|size:64', 'category' => 'required|string|max:100',
'purchase_type' => 'required|in:consumable,other', 'published' => 'required|boolean', 'reason' => 'required|string|min:10|max:4000'])->validate();
'purchase_type' => 'required|in:consumable,other,spare_part', 'published' => 'required|boolean', 'reason' => 'required|string|min:10|max:4000'])->validate();
$stock = StockItem::query()->where('tenant_id', $actor->tenant_id)->lockForUpdate()->findOrFail($stockId);
if ($data['purchase_type'] === 'spare_part' && ! SparePart::query()->where('stock_item_id', $stock->id)->exists()) {
throw ValidationException::withMessages(['purchase_type' => 'Registe a peça e os equipamentos compatíveis antes de a publicar como peça.']);
}
$entry = DB::table('requisition_catalogue')->where('tenant_id', $actor->tenant_id)->where('stock_item_id', $stock->id)->lockForUpdate()->first();
if ((int) $data['version'] !== (int) ($entry->version ?? 0) || $data['stock_basis'] !== $this->basis($stock)) {
throw ValidationException::withMessages(['version' => 'O artigo ou a publicação mudou. Actualize e confira antes de guardar.']);
@@ -119,6 +123,10 @@ class RequisitionCatalogue
$snapshot = ['entry_id' => $entry->id, 'version' => $entry->version, ...$this->sourceDescriptor($entry)];
}
if ($snapshot['purchase_type'] === 'spare_part' && ! SparePart::query()->whereKey($line['spare_part_id'] ?? 0)->where('stock_item_id', $snapshot['stock_item_id'])->exists()) {
throw ValidationException::withMessages(['items' => 'A peça seleccionada não corresponde ao artigo publicado no catálogo.']);
}
return [...$line, 'catalogue_entry_id' => $snapshot['entry_id'], 'catalogue_snapshot' => $snapshot,
'description' => $snapshot['description'], 'unit' => $snapshot['unit'], 'category' => $snapshot['category'], 'purchase_type' => $snapshot['purchase_type']];
}