76 lines
3.5 KiB
PHP
76 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\PurchaseOrder;
|
|
use App\Models\Requisition;
|
|
use App\Models\User;
|
|
use Brick\Math\BigInteger;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
/** Documentary association at issuance, never a retrospective legal compliance decision. */
|
|
final class ProcurementContractInsights
|
|
{
|
|
public const LABELS = ['contract' => 'Todas as linhas com contrato associado', 'partial' => 'Parte das linhas com contrato associado',
|
|
'without_contract' => 'Sem contrato associado na emissão', 'unknown' => 'Associação por determinar'];
|
|
|
|
/** @param Builder<Requisition> $requests
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function build(User $actor, Builder $requests): array
|
|
{
|
|
$coverage = array_fill_keys(['contract', 'partial', 'without_contract', 'unknown'], 0);
|
|
$currencies = [];
|
|
$orders = PurchaseOrder::query()->visibleTo($actor)->operational()->forRequisitions($requests)
|
|
->with('items:id,purchase_order_id,requisition_item_id');
|
|
foreach ($orders->lazyById(200) as $order) {
|
|
$state = $this->classification($order);
|
|
$coverage[$state]++;
|
|
$currency = $order->getAttribute('currency');
|
|
$minor = $order->getAttribute('total_minor');
|
|
if (! is_string($currency) || ! preg_match('/^[A-Z]{3}$/D', $currency) || ! preg_match('/^\d+$/D', (string) $minor)) {
|
|
// Missing amounts are not converted to a zero-valued order.
|
|
$currencies['unknown:'.$state] ??= ['currency' => null, 'classification' => $state, 'count' => 0, 'amount_minor' => null];
|
|
$currencies['unknown:'.$state]['count']++;
|
|
|
|
continue;
|
|
}
|
|
$key = $currency.':'.$state;
|
|
$currencies[$key] ??= ['currency' => $currency, 'classification' => $state, 'count' => 0, 'amount_minor' => '0'];
|
|
$currencies[$key]['count']++;
|
|
$currencies[$key]['amount_minor'] = (string) BigInteger::of($currencies[$key]['amount_minor'])->plus($minor);
|
|
}
|
|
ksort($currencies);
|
|
|
|
return ['method' => 'frozen_order_contract_association_v1', 'coverage' => $coverage, 'currencies' => array_values($currencies)];
|
|
}
|
|
|
|
private function classification(PurchaseOrder $order): string
|
|
{
|
|
$snapshot = $order->snapshot;
|
|
if (! is_string($snapshot['agreement_order_input_hash'] ?? null) || ! preg_match('/^[a-f0-9]{64}$/D', $snapshot['agreement_order_input_hash'])
|
|
|| ! is_array($snapshot['agreement_bindings'] ?? null) || $order->items->isEmpty()) {
|
|
return 'unknown';
|
|
}
|
|
$contracts = 0;
|
|
foreach ($order->items as $line) {
|
|
$binding = $snapshot['agreement_bindings'][$line->getAttribute('requisition_item_id')] ?? null;
|
|
if ($binding !== null && ! is_array($binding)) {
|
|
return 'unknown';
|
|
}
|
|
if ($binding === null || ! isset($binding['contract_id'])) {
|
|
continue;
|
|
}
|
|
if (($binding['schema'] ?? null) !== 'sgc.agreement.order_limit.v1'
|
|
|| ! is_int($binding['contract_id']) || $binding['contract_id'] < 1
|
|
|| ($binding['contract']['id'] ?? null) !== $binding['contract_id']
|
|
|| ! is_int($binding['contract']['version'] ?? null) || $binding['contract']['version'] < 1) {
|
|
return 'unknown';
|
|
}
|
|
$contracts++;
|
|
}
|
|
|
|
return $contracts === 0 ? 'without_contract' : ($contracts === $order->items->count() ? 'contract' : 'partial');
|
|
}
|
|
}
|