Revert "feat(variants): implement restoration of previously suspended variants with usable dates"

This reverts commit 3a4f6b64d2.
This commit is contained in:
2026-09-16 09:08:43 -03:00
parent 3a4f6b64d2
commit 8ad9b3a4e3
5 changed files with 21 additions and 124 deletions

View File

@@ -8,9 +8,7 @@ use App\Domains\Catalog\Models\StockReservation;
use App\Domains\Catalog\Models\StockReservationLine;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Event\Models\EventDate;
use App\Domains\Event\Services\EffectiveEventDateResolver;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class VariantReplacementService
{
@@ -72,7 +70,14 @@ class VariantReplacementService
->get();
foreach ($variants as $variant) {
$remainingDateIds = $this->usableDateIds($variant);
$remainingDateIds = $variant->selectedEventDates()
->filter(fn (EventDate $date): bool => $date->suspended_at === null
&& $date->rescheduled_to_event_date_id === null)
->pluck('id')
->map(fn ($id): int => (int) $id)
->unique()
->sort()
->values();
if ($remainingDateIds->isEmpty()) {
$variant->update(['sales_disabled_at' => now()]);
@@ -80,75 +85,22 @@ class VariantReplacementService
continue;
}
$this->replaceWithUsableDates($variant, $remainingDateIds);
}
}
public function restorePreviouslySuspendedVariants(): int
{
return DB::transaction(function (): int {
$variants = Variant::query()
->whereNotNull('sales_disabled_at')
->whereNull('replaced_by_variant_id')
->with(['eventDates', 'eventDate', 'definitions', 'allAttachments'])
->orderBy('id')
->lockForUpdate()
->get();
$restored = 0;
foreach ($variants as $variant) {
if (! $variant->selectedEventDates()->contains(
fn (EventDate $date): bool => $date->suspended_at !== null,
)) {
continue;
}
$usableDateIds = $this->usableDateIds($variant);
if ($usableDateIds->isEmpty()) {
continue;
}
$this->replaceWithUsableDates($variant, $usableDateIds);
$restored++;
$replacement = $this->findEquivalent($variant, $remainingDateIds);
if ($replacement === null) {
$replacement = $this->cloneWithDates($variant, $remainingDateIds);
} else {
$this->mergeInventoryInto($variant, $replacement);
}
return $restored;
});
}
$variant->update([
'replaced_by_variant_id' => $replacement->getKey(),
'sales_disabled_at' => now(),
]);
/** @return Collection<int, int> */
private function usableDateIds(Variant $variant): Collection
{
$resolver = app(EffectiveEventDateResolver::class);
return $variant->selectedEventDates()
->map(fn (EventDate $date): ?EventDate => $resolver->resolve($date))
->filter()
->pluck('id')
->map(fn ($id): int => (int) $id)
->unique()
->sort()
->values();
}
/** @param Collection<int, int> $dateIds */
private function replaceWithUsableDates(Variant $variant, Collection $dateIds): void
{
$replacement = $this->findEquivalent($variant, $dateIds);
if ($replacement === null) {
$replacement = $this->cloneWithDates($variant, $dateIds);
} else {
$this->mergeInventoryInto($variant, $replacement);
BundleComponent::query()
->where('component_variant_id', $variant->getKey())
->update(['component_variant_id' => $replacement->getKey()]);
}
$variant->update([
'replaced_by_variant_id' => $replacement->getKey(),
'sales_disabled_at' => $variant->sales_disabled_at ?? now(),
]);
BundleComponent::query()
->where('component_variant_id', $variant->getKey())
->update(['component_variant_id' => $replacement->getKey()]);
}
/** @param Collection<int, int> $eventDateIds */