75 lines
4.8 KiB
PHP
75 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\CostCenter;
|
|
use App\Models\Equipment;
|
|
use App\Models\SparePart;
|
|
use App\Models\StockItem;
|
|
use App\Models\User;
|
|
use App\Models\Warehouse;
|
|
use App\Models\WarehouseRequest;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class WarehouseRequests
|
|
{
|
|
/** @param array<string,mixed> $input */
|
|
public function draft(User $actor, array $input): WarehouseRequest
|
|
{
|
|
return DB::transaction(function () use ($actor, $input) {
|
|
app(ConflictGuard::class)->lockCompany($actor);
|
|
$actor = app(CurrentActor::class)->lock($actor);
|
|
abort_unless($actor->hasFunctions(['warehouse_request_prepare']) && ! app(OrganizationAccess::class)->maintenanceOnly($actor), 403);
|
|
abort_unless(Schema::hasTable('warehouse_requests'), 503, 'Instalação de requisições ao armazém pendente.');
|
|
$data = Validator::make($input, ['operation_key' => 'required|uuid', 'warehouse_id' => 'required|integer|min:1', 'cost_center_id' => 'required|integer|min:1',
|
|
'destination' => 'required|string|min:3|max:300', 'reason' => 'required|string|min:10|max:4000', 'priority' => 'required|in:normal,urgent',
|
|
'lines' => 'required|array|min:1|max:50', 'lines.*.stock_item_id' => 'required|integer|min:1', 'lines.*.quantity' => 'required|string|regex:/^[0-9]{1,6}(\.[0-9]{1,3})?$/D',
|
|
'lines.*.use' => 'required|in:internal,equipment', 'lines.*.equipment_id' => 'nullable|integer|min:1'])->validate();
|
|
$hash = hash('sha256', json_encode($data, JSON_THROW_ON_ERROR));
|
|
$old = WarehouseRequest::query()->where('created_by', $actor->id)->where('operation_key', $data['operation_key'])->first();
|
|
if ($old !== null) {
|
|
if ($old->input_hash !== $hash) {
|
|
throw ValidationException::withMessages(['operation_key' => 'Chave utilizada com outro conteúdo.']);
|
|
}
|
|
|
|
return $old;
|
|
}
|
|
$warehouse = Warehouse::query()->whereKey($data['warehouse_id'])->firstOrFail();
|
|
$center = CostCenter::query()->where('department_id', $actor->department_id)->whereKey($data['cost_center_id'])->firstOrFail();
|
|
$lines = [];
|
|
foreach ($data['lines'] as $index => $line) {
|
|
$item = StockItem::query()->whereKey($line['stock_item_id'])->firstOrFail();
|
|
$quantity = ReceiptWorkflow::milli($line['quantity']);
|
|
if ($quantity <= 0) {
|
|
throw ValidationException::withMessages(['lines.'.$index.'.quantity' => 'A quantidade deve ser positiva.']);
|
|
}
|
|
$equipment = null;
|
|
if ($line['use'] === 'equipment') {
|
|
if (empty($line['equipment_id'])) {
|
|
throw ValidationException::withMessages(['lines.'.$index.'.equipment_id' => 'Seleccione o equipamento de destino.']);
|
|
}
|
|
$equipment = Equipment::query()->where('department_id', $actor->department_id)->whereKey($line['equipment_id'])->firstOrFail();
|
|
$part = SparePart::query()->where('stock_item_id', $item->id)->first();
|
|
if ($part !== null) {
|
|
app(EquipmentWorkflow::class)->linkedPart($part->id, $equipment->id);
|
|
}
|
|
} elseif (! empty($line['equipment_id'])) {
|
|
throw ValidationException::withMessages(['lines.'.$index.'.equipment_id' => 'Escolha utilização em equipamento para associar este destino.']);
|
|
}
|
|
$lines[] = ['item' => $item->only(['id', 'code', 'name', 'unit']), 'quantity_milli' => $quantity, 'use' => $line['use'],
|
|
'destination' => $data['destination'], 'equipment' => $equipment?->only(['id', 'code', 'description'])];
|
|
}
|
|
$record = WarehouseRequest::query()->create(['tenant_id' => $actor->tenant_id, 'created_by' => $actor->id, 'warehouse_id' => $warehouse->id,
|
|
'cost_center_id' => $center->id, 'operation_key' => $data['operation_key'], 'input_hash' => $hash, 'status' => 'draft', 'created_at' => now(),
|
|
'snapshot' => ['requester' => $actor->only(['id', 'name', 'department_id']), 'warehouse' => $warehouse->only(['id', 'code', 'name']),
|
|
'cost_center' => $center->only(['id', 'code', 'name']), 'destination' => $data['destination'], 'reason' => $data['reason'], 'priority' => $data['priority'], 'lines' => $lines]]);
|
|
app(RequisitionWorkflow::class)->audit($actor, 'warehouse_request.drafted', null, null, ['warehouse_request_id' => $record->id, 'snapshot' => $record->snapshot]);
|
|
|
|
return $record;
|
|
});
|
|
}
|
|
}
|