feat: enhance item attributes management by adding sort order; update related models, resources, and tests for visibility and ordering of variants

This commit is contained in:
2026-08-10 13:58:35 -03:00
parent e7c8807a67
commit 8542c03466
18 changed files with 252 additions and 19 deletions

View File

@@ -45,7 +45,7 @@ class CartItemResource extends JsonResource
'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited
? null
: $variant->inventory->availableStock(),
'values' => $variant->selectionOptions(),
'values' => $variant->selectionOptions($this->catalogItem->itemAttributes),
])
->values(),
],

View File

@@ -107,6 +107,7 @@ class CartService
return $cart->fresh()->load([
'items.catalogItem.attachments',
'items.catalogItem.inventory',
'items.catalogItem.itemAttributes.attribute',
'items.catalogItem.variants.inventory',
'items.catalogItem.variants.definitions.itemAttribute.attribute.options',
'items.catalogItem.variants.eventDates',

View File

@@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
#[Fillable([
'tenant_code',
@@ -170,6 +171,15 @@ class CatalogItem extends Model
return ($this->availableStock() ?? 0) > 0;
}
/** @return Collection<int, Variant> */
public function visibleVariants(): Collection
{
return $this->variants
->filter(fn (Variant $variant): bool => $this->inventory_policy === InventoryPolicy::Unlimited
|| ($variant->inventory?->availableStock() ?? 0) > 0)
->values();
}
public function getPrice(): float
{
return (float) $this->precio;

View File

@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
'catalog_item_id',
'attribute_id',
'allow_multi_select',
'sort_order',
])]
class ItemAttribute extends Model
{
@@ -25,6 +26,7 @@ class ItemAttribute extends Model
'catalog_item_id' => 'integer',
'attribute_id' => 'integer',
'allow_multi_select' => 'boolean',
'sort_order' => 'integer',
];
}

View File

@@ -152,7 +152,7 @@ class Variant extends Model
/**
* @return Collection<string, array{value: string, label: string}|array<int, array{value: string, label: string}>>
*/
public function selectionOptions(): Collection
public function selectionOptions(?Collection $itemAttributes = null): Collection
{
$options = $this->definitions
->groupBy('item_attribute_id')
@@ -197,7 +197,37 @@ class Variant extends Model
$options->put('event_date', $eventDateOptions->all());
}
return $options;
if ($itemAttributes === null) {
$itemAttributes = $this->definitions
->map(fn (VariantDefinition $definition) => $definition->itemAttribute)
->filter()
->unique('id')
->values();
if ($this->catalogItem !== null) {
$itemAttributes = $itemAttributes
->merge($this->catalogItem->itemAttributes)
->unique('id')
->values();
}
}
$ordering = $itemAttributes->mapWithKeys(function (ItemAttribute $itemAttribute): array {
$attribute = $itemAttribute->attribute;
return $attribute === null
? []
: [$attribute->codigo => [$itemAttribute->sort_order, mb_strtolower($attribute->nombre)]];
});
return $options->sortKeysUsing(function (string $left, string $right) use ($ordering): int {
[$leftOrder, $leftLabel] = $ordering->get($left, [0, mb_strtolower($left)]);
[$rightOrder, $rightLabel] = $ordering->get($right, [0, mb_strtolower($right)]);
return $leftOrder <=> $rightOrder
?: $leftLabel <=> $rightLabel
?: $left <=> $right;
});
}
/** @return Collection<int, EventDate> */

View File

@@ -34,7 +34,7 @@ class CatalogFeaturedItemResource extends JsonResource
'validity_time_id' => $catalogItem->validity_time_id,
'validity_time' => ValidityTimeResource::make($catalogItem->validityTime),
'stock_tecnico' => $catalogItem->availableStock(),
'variants' => $catalogItem->variants
'variants' => $catalogItem->visibleVariants()
->map(fn (Variant $variant): array => [
'id' => $variant->id,
'event_date_id' => $variant->event_date_id,
@@ -46,7 +46,7 @@ class CatalogFeaturedItemResource extends JsonResource
'stock_tecnico' => $catalogItem->inventory_policy === InventoryPolicy::Unlimited
? null
: $variant->inventory->availableStock(),
'values' => $variant->selectionOptions(),
'values' => $variant->selectionOptions($catalogItem->itemAttributes),
])
->values(),
];

View File

@@ -49,7 +49,7 @@ class CatalogItemDetailResource extends JsonResource
$selectedVariant === null,
fn () => $this->imageUrls($this->attachments),
),
'variants' => $this->variants
'variants' => $this->visibleVariants()
->map(fn (Variant $variant): array => $this->variantData($variant))
->values(),
'selected_variant' => $this->when(
@@ -87,6 +87,7 @@ class CatalogItemDetailResource extends JsonResource
'id' => $attribute->id,
'codigo' => $attribute->codigo,
'nombre' => $attribute->nombre,
'sort_order' => $itemAttribute->sort_order,
'is_required' => $attribute->is_required,
'allow_multi_select' => $itemAttribute->allow_multi_select,
'metadata_schema' => $attribute->metadata_schema,
@@ -146,7 +147,10 @@ class CatalogItemDetailResource extends JsonResource
private function attributesData(): Collection
{
return $this->itemAttributes
->sortBy(fn (ItemAttribute $itemAttribute): int => $itemAttribute->attribute->type === FieldType::EventDate ? 0 : 1)
->sort(function (ItemAttribute $left, ItemAttribute $right): int {
return $left->sort_order <=> $right->sort_order
?: mb_strtolower($left->attribute->nombre) <=> mb_strtolower($right->attribute->nombre);
})
->map(fn (ItemAttribute $itemAttribute): array => $this->attributeData($itemAttribute))
->values();
}
@@ -154,7 +158,7 @@ class CatalogItemDetailResource extends JsonResource
/** @return array<string, mixed> */
private function variantData(Variant $variant): array
{
$values = $variant->selectionOptions();
$values = $variant->selectionOptions($this->itemAttributes);
$eventDates = $variant->selectedEventDates();
return [

View File

@@ -45,7 +45,7 @@ class CatalogItemResource extends JsonResource
'descripcion' => $variant->getDescription(),
'precio' => number_format($variant->getPrice(), 2, '.', ''),
'real_stock' => $variant->inventory?->real_stock,
'values' => $variant->selectionOptions(),
'values' => $variant->selectionOptions($this->itemAttributes),
'images' => $variant->attachments
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values(),

View File

@@ -30,7 +30,7 @@ class CatalogSearchItemResource extends JsonResource
'validity_time' => ValidityTimeResource::make($this->validityTime),
'image' => $attachment?->getTemporaryUrl(1440),
'stock_tecnico' => $this->availableStock(),
'variants' => $this->variants
'variants' => $this->visibleVariants()
->map(fn (Variant $variant): array => [
'id' => $variant->id,
'event_date_id' => $variant->event_date_id,
@@ -42,7 +42,7 @@ class CatalogSearchItemResource extends JsonResource
'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited
? null
: $variant->inventory?->availableStock(),
'values' => $variant->selectionOptions(),
'values' => $variant->selectionOptions($this->itemAttributes),
])
->values(),
];

View File

@@ -182,9 +182,10 @@ class CatalogService
'bundleComponents.variant.definitions.itemAttribute.attribute',
]);
$visibleVariants = $catalogItem->visibleVariants();
$selectedVariant = $variantId === null
? $catalogItem->variants->first()
: $catalogItem->variants->firstWhere('id', $variantId);
? $visibleVariants->first()
: $visibleVariants->firstWhere('id', $variantId);
if ($variantId !== null && $selectedVariant === null) {
throw new NotFoundHttpException('Variant not found for catalog item.');
@@ -227,6 +228,7 @@ class CatalogService
'attachments',
'inventory',
'validityTime',
'itemAttributes.attribute',
'variants.inventory',
'variants.attachments',
'variants.eventDate',
@@ -261,6 +263,7 @@ class CatalogService
'attachments',
'inventory',
'validityTime',
'itemAttributes.attribute',
'variants.inventory',
'variants.attachments',
'variants.eventDate',

View File

@@ -41,6 +41,7 @@ class FeaturedGroupService
'inventory',
'attachments',
'validityTime',
'itemAttributes.attribute',
'variants.inventory',
'variants.attachments',
'variants.eventDate',

View File

@@ -21,6 +21,12 @@ class FoodService
{
private const ATTRIBUTE_CODES = ['event_date', 'horario', 'servicio'];
private const ATTRIBUTE_SORT_ORDERS = [
'event_date' => 1,
'horario' => 2,
'servicio' => 3,
];
public function __construct(private readonly CatalogService $catalogService) {}
public function current(Tenant $tenant): ?CatalogItem
@@ -169,9 +175,12 @@ class FoodService
private function itemAttributes(CatalogItem $food, Collection $attributes): Collection
{
return $attributes->mapWithKeys(function (Attribute $attribute, string $code) use ($food): array {
$itemAttribute = $food->itemAttributes()->firstOrCreate(
$itemAttribute = $food->itemAttributes()->updateOrCreate(
['attribute_id' => $attribute->id],
['allow_multi_select' => false],
[
'allow_multi_select' => false,
'sort_order' => self::ATTRIBUTE_SORT_ORDERS[$code],
],
);
return [$code => $itemAttribute];