diff --git a/app/Traits/Order/WithManageItem.php b/app/Traits/Order/WithManageItem.php index f780d9d..a80e5a7 100644 --- a/app/Traits/Order/WithManageItem.php +++ b/app/Traits/Order/WithManageItem.php @@ -15,7 +15,24 @@ trait WithManageItem */ protected function addOrUpdateOrderItem($model, int $quantity, ?string $quality = null): void { - $existing = $this->items->firstWhere(fn ($i) => $i->orderable_type === get_class($model) && $i->orderable_id === $model->id && $i->quality === $quality); + // If the item is a Perfume, we generally want separate entries (especially for custom sizes or distinct refills) + // Adjust logic: Only search for existing if it's NOT a Perfume. + // Or if user specifically wants Bottles/Products to merge but Perfumes to split: + + $existing = null; + + // Check instance type to decide whether to merge + // "if item is perfume, then do not increment quantity" + // "if item is bottle or product, then increment quantity" + $isPerfume = $model instanceof \App\Models\Perfume; + + if (! $isPerfume) { + $existing = $this->items->firstWhere( + fn ($i) => $i->orderable_type === get_class($model) && + $i->orderable_id === $model->id && + $i->quality === $quality + ); + } if ($existing) { $existing->increment('quantity', $quantity); diff --git a/resources/views/livewire/studio/manage/order/form.blade.php b/resources/views/livewire/studio/manage/order/form.blade.php index c0ca266..773be1e 100644 --- a/resources/views/livewire/studio/manage/order/form.blade.php +++ b/resources/views/livewire/studio/manage/order/form.blade.php @@ -246,7 +246,7 @@ @if ($item->quality && $item->quality !== 'Custom') {{ $item->quality }} @else - {{ $item->quality === 'Custom' ? $item->quantity : formatCurrencyNumber($item->quantity, '') }} + {{ $item->quality === 'Custom' ? formatCurrencyNumber($item->quantity, '') . ' ml' : formatCurrencyNumber($item->quantity, '') }} x {{ formatCurrencyNumber($item->unit_price, 'Rp') }} @endif diff --git a/tests/Feature/Studio/Manage/Order/CreateTest.php b/tests/Feature/Studio/Manage/Order/CreateTest.php index 54f6fec..e04ab6b 100644 --- a/tests/Feature/Studio/Manage/Order/CreateTest.php +++ b/tests/Feature/Studio/Manage/Order/CreateTest.php @@ -191,11 +191,13 @@ 'quantity' => 30, ]); - // Bottle should have aggregated to 2 (since bottle has no quality separation) + // Bottle SHOULD aggregated to 2 (since bottle isn't a perfume and logic allows increment) $this->assertDatabaseHas('order_items', [ 'orderable_id' => $bottle->id, 'quantity' => 2, ]); + + $this->assertDatabaseCount('order_items', 3); // 2 perfumes (diff quality) + 1 bottle (aggregated) }); it('can update item quantity in the cart', function () {