feat(migrations): implement backfill for legacy refunds and add migration tests

This commit is contained in:
2026-09-14 15:13:16 -03:00
parent 7d45734ad7
commit b1e09b71ad
2 changed files with 228 additions and 1 deletions

View File

@@ -9,6 +9,8 @@ return new class extends Migration
{
public function up(): void
{
$this->backfillLegacyRefunds();
$mismatchedItem = DB::table('compra_items as purchase_items')
->leftJoin('ticket_refunds as refunds', 'refunds.purchase_item_id', '=', 'purchase_items.id')
->where('purchase_items.refunded_amount', '>', 0)
@@ -24,7 +26,7 @@ return new class extends Migration
if ($mismatchedItem !== null) {
throw new RuntimeException(
"No se puede eliminar compra_items.refunded_amount: el ítem {$mismatchedItem->id} "
.'contiene un importe histórico que no está respaldado por ticket_refunds.'
.'contiene un importe histórico que no se pudo respaldar con ticket_refunds.'
);
}
@@ -49,4 +51,78 @@ return new class extends Migration
->update(['refunded_amount' => $refund->refund_total]);
}, column: 'purchase_item_id');
}
private function backfillLegacyRefunds(): void
{
$items = DB::table('compra_items as purchase_items')
->leftJoin('ticket_refunds as refunds', 'refunds.purchase_item_id', '=', 'purchase_items.id')
->where('purchase_items.refunded_amount', '>', 0)
->groupBy(
'purchase_items.id',
'purchase_items.refunded_amount',
'purchase_items.precio_unitario',
)
->selectRaw(
'purchase_items.id, purchase_items.refunded_amount, purchase_items.precio_unitario, '
.'COALESCE(SUM(refunds.amount), 0) as refund_total'
)
->orderBy('purchase_items.id')
->get();
foreach ($items as $item) {
$legacyAmountInCents = (int) round(
((float) $item->refunded_amount - (float) $item->refund_total) * 100
);
if ($legacyAmountInCents <= 0) {
continue;
}
$tickets = DB::table('tickets as tickets')
->leftJoin('ticket_refunds as refunds', 'refunds.ticket_id', '=', 'tickets.id')
->where('tickets.source_purchase_item_id', $item->id)
->whereNotNull('tickets.refunded_at')
->whereNull('refunds.id')
->orderBy('tickets.refunded_at')
->orderBy('tickets.id')
->get(['tickets.id', 'tickets.refunded_at']);
$ticketCount = $tickets->count();
$unitPriceInCents = (int) round((float) $item->precio_unitario * 100);
if ($ticketCount === 0
|| $unitPriceInCents <= 0
|| $legacyAmountInCents > $ticketCount * $unitPriceInCents) {
continue;
}
$baseAmountInCents = intdiv($legacyAmountInCents, $ticketCount);
$remainderInCents = $legacyAmountInCents % $ticketCount;
if ($baseAmountInCents === 0) {
continue;
}
$refunds = $tickets->values()->map(function (object $ticket, int $index) use (
$item,
$baseAmountInCents,
$remainderInCents,
$unitPriceInCents,
): array {
$amountInCents = $baseAmountInCents + ($index < $remainderInCents ? 1 : 0);
return [
'ticket_id' => $ticket->id,
'purchase_item_id' => $item->id,
'created_by_user_id' => null,
'type' => $amountInCents === $unitPriceInCents ? 'total' : 'partial',
'amount' => number_format($amountInCents / 100, 2, '.', ''),
'created_at' => $ticket->refunded_at,
'updated_at' => $ticket->refunded_at,
];
})->all();
DB::table('ticket_refunds')->insert($refunds);
}
}
};