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\Commerce\Purchase\Services;
use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Commerce\Purchase\Models\Purchase;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
@@ -69,15 +70,12 @@ class TenantTransactionResetService
'users_preserved' => DB::table('users')->where('tenant_codigo', $tenantCode)->count(),
];
DB::table('inventories')
->whereIn('id', $scope['inventory_ids'])
->update([
'real_stock' => DB::raw('real_stock + sold_units - refunded_units'),
'reserved_stock' => 0,
'entry_reserved_stock' => 0,
'sold_units' => 0,
'refunded_units' => 0,
]);
Inventory::query()
->whereKey($scope['inventory_ids'])
->orderBy('id')
->lockForUpdate()
->get()
->each(fn (Inventory $inventory) => $inventory->resetTransactionCounters());
return $summary;
});