Merge branch 'refactor/inventory_stock' of https://gitea.quo.ar/tbianchini/shopit-back into refactor/inventory_stock

This commit is contained in:
2026-09-29 08:23:09 -03:00
parent c5aeb19735
commit 96378a6c2e
596 changed files with 239 additions and 7622 deletions

View File

@@ -228,13 +228,13 @@ class AdminAppTicketService
'amount' => number_format($refundAmount, 2, '.', ''),
]);
$this->restoreInventory($ticket, $purchaseItem, $createdBy);
$this->restoreInventory($ticket, $purchaseItem);
return $ticket->refresh()->load(self::RELATIONS);
});
}
private function restoreInventory(Ticket $ticket, PurchaseItem $purchaseItem, ?User $createdBy): void
private function restoreInventory(Ticket $ticket, PurchaseItem $purchaseItem): void
{
$catalogItem = $ticket->sourceCatalogItem;
if ($catalogItem === null) {
@@ -272,7 +272,11 @@ class AdminAppTicketService
throw ValidationException::withMessages(['ticket' => 'No se encontró el inventario del ticket.']);
}
$inventory->refundStock(1, $createdBy);
if ($catalogItem->inventory_policy === InventoryPolicy::Tracked) {
$inventory->real_stock++;
}
$inventory->refunded_units++;
$inventory->save();
}
private function refundedAmountForPurchaseItem(PurchaseItem $purchaseItem): float

View File

@@ -2,7 +2,6 @@
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;
@@ -65,9 +64,14 @@ class BackfillRefundedUnitsService
}, 'refunds.id', 'id');
foreach ($counts as $inventoryId => $count) {
$inventory = Inventory::query()->lockForUpdate()->find($inventoryId)
?? throw new RuntimeException("No se encontró el inventario {$inventoryId} para reponerlo.");
$inventory->refundStock($count['refunded']);
$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'));
@@ -78,8 +82,7 @@ class BackfillRefundedUnitsService
'bundles_skipped' => $bundlesSkipped,
'inventories_updated' => count($counts),
'refunded_units_added' => $refundedUnitsAdded,
'real_stock_added' => 0,
'available_stock_added' => $refundedUnitsAdded,
'real_stock_added' => array_sum(array_column($counts, 'stock')),
];
});