refactor(inventory): record stock operations consistently

This commit is contained in:
2026-09-25 10:15:19 -03:00
parent 6c56b338c1
commit 863e911120
8 changed files with 54 additions and 48 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Domains\Ticketing\Ticket\Services;
use App\Domains\Commerce\Catalog\Models\Inventory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use RuntimeException;
@@ -64,14 +65,9 @@ class BackfillRefundedUnitsService
}, '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.");
}
$inventory = Inventory::query()->lockForUpdate()->find($inventoryId)
?? throw new RuntimeException("No se encontró el inventario {$inventoryId} para reponerlo.");
$inventory->refundStock($count['refunded']);
}
$refundedUnitsAdded = array_sum(array_column($counts, 'refunded'));
@@ -82,7 +78,8 @@ class BackfillRefundedUnitsService
'bundles_skipped' => $bundlesSkipped,
'inventories_updated' => count($counts),
'refunded_units_added' => $refundedUnitsAdded,
'real_stock_added' => array_sum(array_column($counts, 'stock')),
'real_stock_added' => 0,
'available_stock_added' => $refundedUnitsAdded,
];
});