@@ -27,7 +27,7 @@ class StockWorkflow
|
||||
}
|
||||
|
||||
/** @param array{kind: string, reference: string, idempotency_key: string, responsible_id: int, warehouse_id: int, stock_item_id: int, destination_warehouse_id?: int, quantity: string, reason: string, destination?: string, count_id?: int, ledger_version?: int, equipment_id?: int,lot_counts?:list<array<string,mixed>>,lot_version?:string} $data */
|
||||
public function record(User $actor, array $data): StockOperation
|
||||
public function record(User $actor, array $data, ?SalesStockSource $sale = null): StockOperation
|
||||
{
|
||||
if (! in_array($data['kind'], ['issue', 'transfer', 'count', 'adjustment'], true)) {
|
||||
throw ValidationException::withMessages(['kind' => 'Esta operação exige o seu próprio percurso de preparação e aprovação.']);
|
||||
@@ -35,11 +35,17 @@ class StockWorkflow
|
||||
$roles = $data['kind'] === 'adjustment' ? [PurchasingRole::Warehouse, 'inventory_adjust_review'] : [PurchasingRole::Warehouse];
|
||||
abort_unless($actor->tenant_id !== null && $actor->hasFunctions($roles) && auth()->id() === $actor->id, 403);
|
||||
|
||||
return DB::transaction(function () use ($actor, $data) {
|
||||
return DB::transaction(function () use ($actor, $data, $sale) {
|
||||
// Same first lock as receipts: includes empty balances and concurrent receipts.
|
||||
app(ConflictGuard::class)->lockCompany($actor);
|
||||
$actor = app(CurrentActor::class)->lock($actor);
|
||||
abort_unless($actor->hasFunctions($data['kind'] === 'adjustment' ? [PurchasingRole::Warehouse, 'inventory_adjust_review'] : [PurchasingRole::Warehouse]), 403);
|
||||
if ($sale !== null) {
|
||||
$sale->verify($actor);
|
||||
abort_unless($data['kind'] === 'issue' && $data['stock_item_id'] === $sale->snapshot['stock_item_id']
|
||||
&& ReceiptWorkflow::milli($data['quantity']) === $sale->snapshot['quantity_milli'], 409);
|
||||
$data['sales_invoice'] = $sale->snapshot;
|
||||
}
|
||||
$hash = hash('sha256', json_encode($data, JSON_THROW_ON_ERROR));
|
||||
$existing = StockOperation::query()->where('idempotency_key', $data['idempotency_key'])->first();
|
||||
if ($existing) {
|
||||
@@ -57,7 +63,7 @@ class StockWorkflow
|
||||
$item = StockItem::query()->findOrFail($data['stock_item_id']);
|
||||
$equipment = $data['kind'] === 'issue' && isset($data['equipment_id']) ? Equipment::query()->findOrFail($data['equipment_id']) : null;
|
||||
$part = $data['kind'] === 'issue' ? SparePart::query()->where('stock_item_id', $item->id)->first() : null;
|
||||
if ($part) {
|
||||
if ($part && $sale === null) {
|
||||
if (! $equipment) {
|
||||
throw ValidationException::withMessages(['equipment_id' => 'Seleccione o equipamento de destino associado à peça.']);
|
||||
}
|
||||
@@ -125,7 +131,7 @@ class StockWorkflow
|
||||
'equipment_id' => $equipment?->id, 'spare_part_id' => $part?->id,
|
||||
'payload_hash' => $hash, 'destination' => $data['destination'] ?? null, 'reason' => $data['reason'],
|
||||
'quantity_milli' => $quantity, 'theoretical_milli' => $balance['quantity'], 'ledger_version' => $balance['version'], 'recorded_at' => now(),
|
||||
'snapshot' => [...$lotSnapshot, 'actor' => $actor->only(['id', 'name']), 'responsible' => $responsible->only(['id', 'name']), 'stock_item' => $item->only(['id', 'code', 'name', 'unit']), 'warehouse' => $warehouse->only(['id', 'code', 'name']), 'destination_warehouse' => $destination?->only(['id', 'code', 'name']), 'equipment' => $equipment?->toArray(), 'spare_part' => $part?->toArray(), 'difference_milli' => $quantity - $balance['quantity'], 'budget_checked' => false, 'development_only' => app(StandaloneRuntime::class)->syntheticOnly()],
|
||||
'snapshot' => [...$lotSnapshot, ...($sale === null ? [] : ['sales_invoice' => $sale->snapshot]), 'actor' => $actor->only(['id', 'name']), 'responsible' => $responsible->only(['id', 'name']), 'stock_item' => $item->only(['id', 'code', 'name', 'unit']), 'warehouse' => $warehouse->only(['id', 'code', 'name']), 'destination_warehouse' => $destination?->only(['id', 'code', 'name']), 'equipment' => $equipment?->toArray(), 'spare_part' => $part?->toArray(), 'difference_milli' => $quantity - $balance['quantity'], 'budget_checked' => false, 'development_only' => app(StandaloneRuntime::class)->syntheticOnly()],
|
||||
]);
|
||||
if ($adjustment !== null) {
|
||||
foreach ([['decrease_milli', -1, 'adjustment_out'], ['increase_milli', 1, 'adjustment_in']] as [$key, $sign, $type]) {
|
||||
|
||||
Reference in New Issue
Block a user