@@ -34,11 +34,13 @@ class ProcurementPaymentInsights
|
||||
$categoryCoverage = ['allocated' => 0, 'unallocated' => 0, 'indeterminate' => 0];
|
||||
$categories = [];
|
||||
$unassigned = [];
|
||||
$seen = [];
|
||||
foreach ($invoices->select(['id', 'supplier_id', 'snapshot'])->lazyById(200) as $invoice) {
|
||||
$coverage['invoices']++;
|
||||
$rows = [];
|
||||
try {
|
||||
foreach ($adapter->readPaymentHistory($actor, $invoice->id) as $payment) {
|
||||
$facts = [...$adapter->readPaymentHistory($actor, $invoice->id), ...app(CommonPaymentFacts::class)->forInvoice($actor, $invoice->id)];
|
||||
foreach ($facts as $payment) {
|
||||
if ($payment['status'] !== 'confirmed') {
|
||||
continue;
|
||||
}
|
||||
@@ -63,19 +65,30 @@ class ProcurementPaymentInsights
|
||||
continue;
|
||||
}
|
||||
foreach ($rows as $payment) {
|
||||
$identity = ($payment['source_type'] ?? 'external_payment') === 'common_allocation'
|
||||
? 'common:'.$payment['common_payment_id'] : 'native:'.$payment['id'];
|
||||
$currency = $payment['currency'];
|
||||
$currencies[$currency] ??= ['currency' => $currency, 'count' => 0, 'amount_minor' => '0'];
|
||||
$currencies[$currency]['count']++;
|
||||
if (! isset($seen['currency'][$currency][$identity])) {
|
||||
$currencies[$currency]['count']++;
|
||||
$seen['currency'][$currency][$identity] = true;
|
||||
}
|
||||
$currencies[$currency]['amount_minor'] = (string) BigInteger::of($currencies[$currency]['amount_minor'])->plus($payment['amount_minor']);
|
||||
$key = $invoice->getAttribute('supplier_id').':'.$currency;
|
||||
$name = $invoice->snapshot['supplier']['name'] ?? null;
|
||||
$suppliers[$key] ??= ['supplier_id' => (int) $invoice->getAttribute('supplier_id'),
|
||||
'supplier_name' => is_string($name) && trim($name) !== '' ? $name : 'Fornecedor #'.$invoice->getAttribute('supplier_id'),
|
||||
'currency' => $currency, 'count' => 0, 'amount_minor' => '0'];
|
||||
$suppliers[$key]['count']++;
|
||||
if (! isset($seen['supplier'][$key][$identity])) {
|
||||
$suppliers[$key]['count']++;
|
||||
$seen['supplier'][$key][$identity] = true;
|
||||
}
|
||||
$suppliers[$key]['amount_minor'] = (string) BigInteger::of($suppliers[$key]['amount_minor'])->plus($payment['amount_minor']);
|
||||
$attribution = app(ExpenseAllocations::class)->projection($actor, $payment['id']);
|
||||
$categoryCoverage[$attribution['state']]++;
|
||||
$attribution = app(ExpenseAllocations::class)->projection($actor, $payment['id'], $payment['source_type'] ?? 'external_payment');
|
||||
if (! isset($seen['category_state'][$attribution['state']][$identity])) {
|
||||
$categoryCoverage[$attribution['state']]++;
|
||||
$seen['category_state'][$attribution['state']][$identity] = true;
|
||||
}
|
||||
if ($attribution['state'] === 'allocated') {
|
||||
foreach ($attribution['allocations'] as $allocation) {
|
||||
if ($allocation['amount_minor'] === '0') {
|
||||
@@ -83,16 +96,25 @@ class ProcurementPaymentInsights
|
||||
}
|
||||
$categoryKey = json_encode([$allocation['category'], $currency], JSON_THROW_ON_ERROR);
|
||||
$categories[$categoryKey] ??= ['category' => $allocation['category'], 'currency' => $currency, 'count' => 0, 'amount_minor' => '0'];
|
||||
$categories[$categoryKey]['count']++;
|
||||
if (! isset($seen['category'][$categoryKey][$identity])) {
|
||||
$categories[$categoryKey]['count']++;
|
||||
$seen['category'][$categoryKey][$identity] = true;
|
||||
}
|
||||
$categories[$categoryKey]['amount_minor'] = (string) BigInteger::of($categories[$categoryKey]['amount_minor'])->plus($allocation['amount_minor']);
|
||||
}
|
||||
} else {
|
||||
$unassignedKey = $currency.':'.$attribution['state'];
|
||||
$unassigned[$unassignedKey] ??= ['state' => $attribution['state'], 'currency' => $currency, 'count' => 0, 'amount_minor' => '0'];
|
||||
$unassigned[$unassignedKey]['count']++;
|
||||
if (! isset($seen['unassigned'][$unassignedKey][$identity])) {
|
||||
$unassigned[$unassignedKey]['count']++;
|
||||
$seen['unassigned'][$unassignedKey][$identity] = true;
|
||||
}
|
||||
$unassigned[$unassignedKey]['amount_minor'] = (string) BigInteger::of($unassigned[$unassignedKey]['amount_minor'])->plus($payment['amount_minor']);
|
||||
}
|
||||
$coverage['confirmed_payments']++;
|
||||
if (! isset($seen['payment'][$identity])) {
|
||||
$coverage['confirmed_payments']++;
|
||||
$seen['payment'][$identity] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
ksort($currencies);
|
||||
|
||||
Reference in New Issue
Block a user