122 lines
5.2 KiB
PHP
122 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticket\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use RuntimeException;
|
|
|
|
class BackfillRefundedUnitsService
|
|
{
|
|
/** @return array<string, int> */
|
|
public function run(): array
|
|
{
|
|
$summary = DB::transaction(function (): array {
|
|
if (DB::table('inventories')->where('refunded_units', '>', 0)->exists()) {
|
|
throw new RuntimeException('El backfill requiere que refunded_units sea cero en todos los inventarios.');
|
|
}
|
|
|
|
$counts = [];
|
|
$variants = [];
|
|
$refundsSeen = 0;
|
|
$bundlesSkipped = 0;
|
|
|
|
DB::table('ticket_refunds as refunds')
|
|
->join('tickets', 'tickets.id', '=', 'refunds.ticket_id')
|
|
->join('compra_items as purchase_items', 'purchase_items.id', '=', 'refunds.purchase_item_id')
|
|
->leftJoin('catalog_items as purchase_catalog', 'purchase_catalog.id', '=', 'purchase_items.source_catalog_item_id')
|
|
->leftJoin('catalog_items as ticket_catalog', 'ticket_catalog.id', '=', 'tickets.source_catalog_item_id')
|
|
->select([
|
|
'refunds.id',
|
|
'refunds.ticket_id',
|
|
'tickets.source_variant_id',
|
|
'ticket_catalog.inventory_id',
|
|
'ticket_catalog.inventory_policy',
|
|
'ticket_catalog.type as ticket_catalog_type',
|
|
'purchase_catalog.type as purchase_catalog_type',
|
|
])
|
|
->chunkById(500, function ($refunds) use (&$counts, &$variants, &$refundsSeen, &$bundlesSkipped): void {
|
|
foreach ($refunds as $refund) {
|
|
$refundsSeen++;
|
|
if ($refund->ticket_catalog_type === 'bundle' || $refund->purchase_catalog_type === 'bundle') {
|
|
$bundlesSkipped++;
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($refund->inventory_policy === null) {
|
|
throw new RuntimeException("El reembolso {$refund->id} no tiene un producto de catálogo asociado.");
|
|
}
|
|
|
|
$inventoryId = $refund->source_variant_id === null
|
|
? $refund->inventory_id
|
|
: $this->currentVariantInventoryId((int) $refund->source_variant_id, $variants);
|
|
|
|
if ($inventoryId === null) {
|
|
throw new RuntimeException("El reembolso {$refund->id} no tiene un inventario asociado.");
|
|
}
|
|
|
|
$counts[$inventoryId]['refunded'] = ($counts[$inventoryId]['refunded'] ?? 0) + 1;
|
|
if ($refund->inventory_policy === 'tracked') {
|
|
$counts[$inventoryId]['stock'] = ($counts[$inventoryId]['stock'] ?? 0) + 1;
|
|
}
|
|
}
|
|
}, 'refunds.id', 'id');
|
|
|
|
foreach ($counts as $inventoryId => $count) {
|
|
$updates = ['refunded_units' => DB::raw('refunded_units + '.$count['refunded'])];
|
|
if (($count['stock'] ?? 0) > 0) {
|
|
$updates['real_stock'] = DB::raw('real_stock + '.$count['stock']);
|
|
}
|
|
|
|
if (DB::table('inventories')->where('id', $inventoryId)->update($updates) !== 1) {
|
|
throw new RuntimeException("No se encontró el inventario {$inventoryId} para reponerlo.");
|
|
}
|
|
}
|
|
|
|
$refundedUnitsAdded = array_sum(array_column($counts, 'refunded'));
|
|
|
|
return [
|
|
'refunds_seen' => $refundsSeen,
|
|
'refunds_applied' => $refundedUnitsAdded,
|
|
'bundles_skipped' => $bundlesSkipped,
|
|
'inventories_updated' => count($counts),
|
|
'refunded_units_added' => $refundedUnitsAdded,
|
|
'real_stock_added' => array_sum(array_column($counts, 'stock')),
|
|
];
|
|
});
|
|
|
|
Log::info('inventory.refunded_units_backfill.completed', $summary);
|
|
|
|
return $summary;
|
|
}
|
|
|
|
/** @param array<int, object|null> $variants */
|
|
private function currentVariantInventoryId(int $variantId, array &$variants): ?int
|
|
{
|
|
$visited = [];
|
|
|
|
while (true) {
|
|
if (isset($visited[$variantId])) {
|
|
throw new RuntimeException("La cadena de reemplazos de la variante {$variantId} es circular.");
|
|
}
|
|
$visited[$variantId] = true;
|
|
|
|
if (! array_key_exists($variantId, $variants)) {
|
|
$variants[$variantId] = DB::table('variantes')
|
|
->where('id', $variantId)
|
|
->first(['inventory_id', 'replaced_by_variant_id']);
|
|
}
|
|
$variant = $variants[$variantId];
|
|
if ($variant === null) {
|
|
throw new RuntimeException("No se encontró la variante {$variantId} de un ticket reembolsado.");
|
|
}
|
|
if ($variant->replaced_by_variant_id === null) {
|
|
return $variant->inventory_id === null ? null : (int) $variant->inventory_id;
|
|
}
|
|
|
|
$variantId = (int) $variant->replaced_by_variant_id;
|
|
}
|
|
}
|
|
}
|