feat(catalog): add sales_end_at field to catalog items and implement sale logic
feat(event): include catalog items in event resource and update event service test(seeder): add FiestaTradicionArrufoSeeder for event and catalog item setup config: set event timezone for local event dates
This commit is contained in:
@@ -31,6 +31,7 @@ use Illuminate\Support\Collection;
|
||||
'group_order',
|
||||
'descripcion',
|
||||
'precio',
|
||||
'sales_end_at',
|
||||
'inventory_policy',
|
||||
'inventory_subject',
|
||||
'max_units_per_user',
|
||||
@@ -75,6 +76,7 @@ class CatalogItem extends Model
|
||||
'type' => CatalogItemType::class,
|
||||
'group_order' => 'integer',
|
||||
'precio' => 'decimal:2',
|
||||
'sales_end_at' => 'datetime',
|
||||
'inventory_policy' => InventoryPolicy::class,
|
||||
'inventory_subject' => InventorySubject::class,
|
||||
'max_units_per_user' => 'integer',
|
||||
@@ -191,6 +193,10 @@ class CatalogItem extends Model
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
if (! $this->isSaleOpen()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->type === CatalogItemType::Bundle) {
|
||||
$availableStock = $this->availableStock();
|
||||
|
||||
@@ -208,6 +214,9 @@ class CatalogItem extends Model
|
||||
public function scopeWhereAvailable(Builder $query): Builder
|
||||
{
|
||||
return $query->where(function (Builder $query): void {
|
||||
$query->whereNull('catalog_items.sales_end_at')
|
||||
->orWhere('catalog_items.sales_end_at', '>', now());
|
||||
})->where(function (Builder $query): void {
|
||||
$query
|
||||
->where(function (Builder $unlimitedQuery): void {
|
||||
$unlimitedQuery
|
||||
@@ -247,10 +256,16 @@ class CatalogItem extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function isSaleOpen(): bool
|
||||
{
|
||||
return $this->sales_end_at === null || now()->lt($this->sales_end_at);
|
||||
}
|
||||
|
||||
/** @return Collection<int, Variant> */
|
||||
public function visibleVariants(?int $includedVariantId = null): Collection
|
||||
{
|
||||
return $this->variants
|
||||
->each(fn (Variant $variant) => $variant->setRelation('catalogItem', $this))
|
||||
->filter(fn (Variant $variant): bool => $variant->hasOnlyActiveEventDates()
|
||||
&& (($includedVariantId !== null && $variant->id === $includedVariantId)
|
||||
|| ($variant->isSellable() && (
|
||||
|
||||
@@ -96,6 +96,7 @@ class Variant extends Model
|
||||
{
|
||||
return $this->sales_disabled_at === null
|
||||
&& $this->replaced_by_variant_id === null
|
||||
&& $this->catalogItem->isSaleOpen()
|
||||
&& $this->hasOnlyActiveEventDates();
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,12 @@ class CatalogSelectionResolver
|
||||
throw new NotFoundHttpException('Catalog item not found for tenant.');
|
||||
}
|
||||
|
||||
if (! $catalogItem->isSaleOpen()) {
|
||||
throw ValidationException::withMessages([
|
||||
"{$fieldPrefix}.catalog_item_id" => ['La venta de este producto finalizó.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($catalogItem->isBundle()) {
|
||||
if ($variantId !== null) {
|
||||
throw ValidationException::withMessages([
|
||||
|
||||
Reference in New Issue
Block a user