@@ -39,7 +39,8 @@ final class ImportTariffCodes
|
||||
'Confirme a classificação pautal actual de cada artigo importado antes de emitir a ordem.');
|
||||
$result[$item->id] = ['record_id' => $record->id, 'version' => $record->version,
|
||||
'requisition_item_id' => $item->id, 'item' => $record->snapshot['item'], 'data' => $record->data,
|
||||
'source' => $record->snapshot['master'] ?? null, 'confirmed_by' => $record->snapshot['confirmed_by'], 'reason' => $record->reason];
|
||||
'source' => $record->snapshot['master'] ?? null, 'legal_rule' => $record->snapshot['legal_rule'] ?? null,
|
||||
'confirmed_by' => $record->snapshot['confirmed_by'], 'reason' => $record->reason];
|
||||
}
|
||||
ksort($result, SORT_NUMERIC);
|
||||
|
||||
@@ -72,7 +73,11 @@ final class ImportTariffCodes
|
||||
$stockItems->through(fn ($item) => [...$item->only(['id', 'code', 'name', 'unit']),
|
||||
'tariff' => app(SupplyPlanning::class)->latest('stock_tariff')->where('stock_item_id', $item->id)->first(['id', 'version', 'data'])]);
|
||||
|
||||
return ['requisition' => $request->only(['id', 'number', 'status']), 'items' => $request->items()->orderBy('id')->get(['id', 'description', 'unit', 'quantity']),
|
||||
$latest->each(function ($record) {
|
||||
$record->setAttribute('legal_changed', isset($record->snapshot['legal_rule']) && app(ImportLegalRules::class)->changed($record->snapshot['legal_rule']));
|
||||
});
|
||||
|
||||
return ['legalReady' => app(ImportLegalRules::class)->ready(), 'requisition' => $request->only(['id', 'number', 'status']), 'items' => $request->items()->orderBy('id')->get(['id', 'description', 'unit', 'quantity']),
|
||||
'latest' => $latest, 'history' => $history, 'stockItems' => $stockItems, 'canPropose' => ($actor->id === $request->requester_id && app(OrganizationAccess::class)->canRequest($actor)) || $actor->hasFunctions([PurchasingRole::Procurement]),
|
||||
'canConfirm' => $actor->hasFunctions([PurchasingRole::Procurement]),
|
||||
'masters' => app(SupplyPlanning::class)->latest('stock_tariff')->when(! $materialsVisible, fn ($q) => $q->whereRaw('1 = 0'))->whereIn('stock_item_id', StockItem::query()->select('id'))
|
||||
@@ -89,7 +94,10 @@ final class ImportTariffCodes
|
||||
'version' => 'required|integer|min:0', 'reason' => 'required|string|min:10|max:4000',
|
||||
'stock_item_id' => 'required_if:action,master|nullable|integer|min:1', 'requisition_item_id' => 'required_unless:action,master|nullable|integer|min:1',
|
||||
'master_id' => 'nullable|integer|min:1', 'code' => 'required_unless:action,propose|nullable|string|max:40|not_regex:/[\x00-\x1F\x7F]/',
|
||||
'source_reference' => 'required_unless:action,propose|nullable|string|max:2000'])->validate();
|
||||
'source_reference' => 'required_unless:action,propose|nullable|string|max:2000',
|
||||
'legal_rule_id' => 'nullable|integer|min:1', 'legal_match_code' => 'required_with:legal_rule_id|nullable|string|max:40',
|
||||
'legal_origin' => 'nullable|string|max:120', 'legal_sector' => 'nullable|string|max:160',
|
||||
'legal_date' => 'required_with:legal_rule_id|nullable|date_format:Y-m-d'])->validate();
|
||||
$data['operation_key'] = strtolower($data['operation_key']);
|
||||
$hash = hash('sha256', json_encode([$request->id, $data], JSON_THROW_ON_ERROR));
|
||||
|
||||
@@ -133,10 +141,23 @@ final class ImportTariffCodes
|
||||
abort_unless($code === $master->data['code'] && $source === $master->data['source_reference'], 409, 'Aplique explicitamente o código e a fonte do cadastro ou conserve uma proposta manual.');
|
||||
$snapshot['master'] = ['id' => $master->id, 'version' => $master->version, 'item' => $master->snapshot['item']];
|
||||
}
|
||||
if (! empty($data['legal_rule_id'])) {
|
||||
abort_unless($buyer && empty($data['master_id']), 403);
|
||||
$legal = app(ImportLegalRules::class)->freeze((int) $data['legal_rule_id'], $data['legal_match_code'],
|
||||
$data['legal_origin'] ?? '', $data['legal_sector'] ?? '', $data['legal_date']);
|
||||
abort_unless($code === $legal['data']['suggested_code'] && $source === $legal['data']['official_reference'].' — '.$legal['data']['citation'], 409,
|
||||
'Aplique explicitamente o código e a citação da regra seleccionada.');
|
||||
$snapshot['legal_rule'] = $legal;
|
||||
}
|
||||
$state = 'proposed';
|
||||
} else {
|
||||
abort_unless($old !== null && $old->state === 'proposed' && empty($data['master_id']) && $code !== '' && $source !== '', 409, 'Seleccione uma proposta actual e indique código e fonte da confirmação.');
|
||||
abort_unless($old->snapshot['item'] === $snapshot['item'], 409, 'O artigo mudou. Prepare uma proposta actual.');
|
||||
if (isset($old->snapshot['legal_rule'])) {
|
||||
$legal = $old->snapshot['legal_rule'];
|
||||
app(ImportLegalRules::class)->freeze($legal['id'], $legal['context']['code'], $legal['context']['origin_country'],
|
||||
$legal['context']['sector'], $legal['context']['date']);
|
||||
}
|
||||
$payload = [...$old->data, 'confirmed_code' => $code, 'confirmation_source' => $source,
|
||||
'deviation' => $code !== $old->data['proposed_code'], 'confirmed_by' => $actor->id];
|
||||
$snapshot = [...$old->snapshot, 'confirmed_by' => ['id' => $actor->id, 'name' => $actor->name]];
|
||||
|
||||
Reference in New Issue
Block a user