@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\RequisitionItem;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
/** RQ-01 classification, distinct from the original catalogue category and financial purchase type. */
|
||||
final class RequisitionCategories
|
||||
{
|
||||
public const LABELS = [
|
||||
'production_input' => 'Insumos de produção',
|
||||
'spare_part' => 'Peças sobressalentes',
|
||||
'capital_equipment' => 'Maquinaria',
|
||||
'office_equipment' => 'Equipamento de escritório',
|
||||
'other' => 'Outra categoria',
|
||||
];
|
||||
|
||||
public function available(): bool
|
||||
{
|
||||
return Schema::hasColumn('requisition_items', 'category_context');
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $line */
|
||||
public function incomplete(array $line): bool
|
||||
{
|
||||
$category = $line['procurement_category'] ?? null;
|
||||
$type = $line['purchase_type'] ?? null;
|
||||
|
||||
return (empty($category) && trim($line['category_reason'] ?? '') !== '')
|
||||
|| ($category === 'production_input' && empty($line['catalogue_entry_id']))
|
||||
|| ($category === 'spare_part' && ((empty($line['catalogue_entry_id']) && $type !== 'spare_part') || empty($line['spare_part_id']) || empty($line['equipment_id'])))
|
||||
|| ($category === 'capital_equipment' && $type !== 'equipment')
|
||||
|| (($category === 'capital_equipment' || (in_array($category, ['office_equipment', 'other'], true) && empty($line['catalogue_entry_id'])))
|
||||
&& mb_strlen(trim($line['category_reason'] ?? '')) < 10);
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $line
|
||||
* @return array<string,mixed>|null
|
||||
*/
|
||||
public function context(array $line, int $index, ?RequisitionItem $previous = null): ?array
|
||||
{
|
||||
if (! array_key_exists('procurement_category', $line) && ! array_key_exists('category_reason', $line) && $previous?->category_context !== null) {
|
||||
$line['procurement_category'] = $previous->category_context['category'];
|
||||
$line['category_reason'] = $previous->category_context['reason'];
|
||||
}
|
||||
$category = $line['procurement_category'] ?? null;
|
||||
$reason = trim($line['category_reason'] ?? '');
|
||||
if (empty($category) && $reason === '') {
|
||||
// Accepted legacy lines remain unclassified, never inferred from their description.
|
||||
return null;
|
||||
}
|
||||
if (! $this->available()) {
|
||||
abort(503, 'A classificação RQ-01 ainda não está instalada.');
|
||||
}
|
||||
$field = 'items.'.$index.'.procurement_category';
|
||||
if (! is_string($category) || ! array_key_exists($category, self::LABELS)) {
|
||||
throw ValidationException::withMessages([$field => 'Seleccione a categoria da necessidade.']);
|
||||
}
|
||||
$type = $line['purchase_type'];
|
||||
if ($category === 'production_input' && (empty($line['catalogue_snapshot']['stock_item_id'])
|
||||
|| empty($line['catalogue_snapshot']['code']) || ! in_array($type, ['consumable', 'other'], true))) {
|
||||
throw ValidationException::withMessages([$field => 'Insumos de produção: seleccione um material de stock no catálogo publicado.']);
|
||||
}
|
||||
if ($category === 'spare_part' && ($type !== 'spare_part' || empty($line['spare_part_id']) || empty($line['equipment_id']))) {
|
||||
throw ValidationException::withMessages([$field => 'Peças sobressalentes: escolha o tipo Peça e a associação ao equipamento.']);
|
||||
}
|
||||
if ($category === 'capital_equipment' && $type !== 'equipment') {
|
||||
throw ValidationException::withMessages([$field => 'Maquinaria: escolha o tipo Equipamento para conservar a validação técnica e o orçamento de investimento.']);
|
||||
}
|
||||
if (($category === 'capital_equipment' || (in_array($category, ['office_equipment', 'other'], true) && empty($line['catalogue_entry_id'])))
|
||||
&& mb_strlen($reason) < 10) {
|
||||
throw ValidationException::withMessages(['items.'.$index.'.category_reason' => $category === 'capital_equipment'
|
||||
? 'Descreva a justificação do investimento com pelo menos 10 caracteres.'
|
||||
: 'Justifique o uso de texto livre, sem artigo do catálogo, com pelo menos 10 caracteres.']);
|
||||
}
|
||||
|
||||
return ['version' => 1, 'category' => $category, 'reason' => $reason === '' ? null : $reason];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user