diff --git a/app/Livewire/Forms/Studio/Manage/OrderForm.php b/app/Livewire/Forms/Studio/Manage/OrderForm.php index 035454c..657f9d3 100644 --- a/app/Livewire/Forms/Studio/Manage/OrderForm.php +++ b/app/Livewire/Forms/Studio/Manage/OrderForm.php @@ -75,17 +75,17 @@ public function store() { $this->validate(); - $orderItems = $this->loadOrderItems(); + $items = $this->loadOrderItems(); // Generate a unique invoice number based on today's paid order count $todayCount = Order::whereDate('created_at', now())->paid()->count() + 1; $invoiceNumber = 'INV'.now()->format('Ymd').str_pad($todayCount, 4, '0', STR_PAD_LEFT); // Calculate total cost of goods sold (COGS) — internal purchase cost - $cogs = $orderItems->sum(fn ($item) => $item->cogs * $item->quantity); + $cogs = $items->sum(fn ($item) => $item->cogs * $item->quantity); // Calculate subtotal before any global discount - $subtotal = $orderItems->sum(fn ($item) => $item->unit_price * $item->quantity); + $subtotal = $items->sum(fn ($item) => $item->unit_price * $item->quantity); $discount = replaceCurrency($this->discount); @@ -94,7 +94,7 @@ public function store() // Wrap the entire operation in a database transaction // Ensures atomicity — if any step fails, all changes are rolled back - DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $orderItems) { + DB::transaction(function () use ($invoiceNumber, $cogs, $subtotal, $discount, $total, $items) { $order = Order::create([ 'outlet_id' => $this->outlet_id, 'user_id' => auth()->id(), @@ -110,7 +110,7 @@ public function store() ]); // Attach all order items to the newly created order - $orderItems->each(function ($item) use ($order) { + $items->each(function ($item) use ($order) { $item->order_id = $order->id; $item->save(); diff --git a/app/Livewire/Studio/Loyalty/Tier.php b/app/Livewire/Studio/Loyalty/Tier.php index 2fd35dc..5140f7a 100644 --- a/app/Livewire/Studio/Loyalty/Tier.php +++ b/app/Livewire/Studio/Loyalty/Tier.php @@ -29,13 +29,13 @@ class Tier extends Component public function mount() { - $this->tiers = TierModel::with(['memberships.user.customer.orders.orderItems', 'memberships.user.customer.orders.payments']) + $this->tiers = TierModel::with(['memberships.user.customer.orders.items', 'memberships.user.customer.orders.payments']) ->withCount('memberships') ->orderBy('min_points', 'asc') ->get() ->map(function ($tier) { $topMembership = $tier->memberships() - ->with(['user.customer.orders.orderItems', 'user.customer.orders.payments']) + ->with(['user.customer.orders.items', 'user.customer.orders.payments']) ->orderByDesc('total_points') ->first(); @@ -44,7 +44,7 @@ public function mount() $orders = $topMembership->user->customer->orders; $totalOrders = $orders->count(); - $totalItems = $orders->flatMap(fn ($order) => $order->orderItems)->sum('quantity'); + $totalItems = $orders->flatMap(fn ($order) => $order->items)->sum('quantity'); $totalAmount = $orders->flatMap(fn ($order) => $order->payments)->sum('amount'); $topMemberStats = [ diff --git a/app/Livewire/Studio/Manage/Order/Create.php b/app/Livewire/Studio/Manage/Order/Create.php index c986bf5..d4a3da5 100644 --- a/app/Livewire/Studio/Manage/Order/Create.php +++ b/app/Livewire/Studio/Manage/Order/Create.php @@ -41,7 +41,7 @@ class Create extends Component public string $total = ''; - public $orderItems; + public $items; public function mount() { @@ -58,7 +58,7 @@ public function mount() $this->products = Product::orderBy('name')->pluck('name', 'id')->toArray(); - $this->orderItems = $this->loadOrderItems(); + $this->items = $this->loadOrderItems(); $this->subtotal = $this->getSubTotal(); @@ -69,7 +69,7 @@ public function save() { $this->canOrAbort('create order'); - if ($this->orderItems->isEmpty()) { + if ($this->items->isEmpty()) { $this->toast('Keranjang tidak boleh kosong.', 'Gagal', 'danger'); return; diff --git a/app/Models/Bottle.php b/app/Models/Bottle.php index faeb3b8..f550092 100644 --- a/app/Models/Bottle.php +++ b/app/Models/Bottle.php @@ -47,7 +47,7 @@ public function purchaseItems(): MorphMany return $this->morphMany(PurchaseItem::class, 'purchasable'); } - public function orderItems(): MorphMany + public function items(): MorphMany { return $this->morphMany(PurchaseItem::class, 'orderable'); } diff --git a/app/Models/Perfume.php b/app/Models/Perfume.php index fae7979..2c788e1 100644 --- a/app/Models/Perfume.php +++ b/app/Models/Perfume.php @@ -57,7 +57,7 @@ public function purchaseItems(): MorphMany return $this->morphMany(PurchaseItem::class, 'purchasable'); } - public function orderItems(): MorphMany + public function items(): MorphMany { return $this->morphMany(PurchaseItem::class, 'orderable'); } diff --git a/app/Models/Product.php b/app/Models/Product.php index 813ff71..355d722 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -44,7 +44,7 @@ public function purchaseItems(): MorphMany return $this->morphMany(PurchaseItem::class, 'purchasable'); } - public function orderItems(): MorphMany + public function items(): MorphMany { return $this->morphMany(PurchaseItem::class, 'orderable'); } diff --git a/app/Models/User.php b/app/Models/User.php index 48099d8..e89b5be 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -87,7 +87,7 @@ public function orders(): HasMany return $this->hasMany(Order::class); } - public function orderItems(): HasMany + public function items(): HasMany { return $this->hasMany(OrderItem::class); } diff --git a/app/Traits/Order/WithCalculateTotal.php b/app/Traits/Order/WithCalculateTotal.php index 743665f..beb36b4 100644 --- a/app/Traits/Order/WithCalculateTotal.php +++ b/app/Traits/Order/WithCalculateTotal.php @@ -6,11 +6,11 @@ trait WithCalculateTotal { public function getSubTotal() { - return $this->orderItems->sum(fn ($item) => $item->unit_price * $item->quantity); + return $this->items->sum(fn ($item) => $item->unit_price * $item->quantity); } public function getTotal() { - return $this->orderItems->sum(fn ($item) => $item->unit_price * $item->quantity - $item->discount); + return $this->items->sum(fn ($item) => $item->unit_price * $item->quantity - $item->discount); } } diff --git a/app/Traits/Order/WithManageItem.php b/app/Traits/Order/WithManageItem.php index b69a730..7cc85a5 100644 --- a/app/Traits/Order/WithManageItem.php +++ b/app/Traits/Order/WithManageItem.php @@ -14,7 +14,7 @@ trait WithManageItem */ protected function addOrUpdateOrderItem($model, int $quantity) { - $existing = $this->orderItems->firstWhere(fn ($i) => $i->orderable_type === get_class($model) && $i->orderable_id === $model->id); + $existing = $this->items->firstWhere(fn ($i) => $i->orderable_type === get_class($model) && $i->orderable_id === $model->id); if ($existing) { $existing->increment('quantity', $quantity); @@ -29,10 +29,10 @@ protected function addOrUpdateOrderItem($model, int $quantity) 'unit_price' => $model->sale_price ?? 0, ]); - $this->orderItems->push($existing); + $this->items->push($existing); } - $this->orderItems = $this->orderItems->map(fn ($i) => $i->id === $existing->id ? $existing : $i); + $this->items = $this->items->map(fn ($i) => $i->id === $existing->id ? $existing : $i); $this->subtotal = $this->getSubtotal(); $this->total = $this->getTotal(); @@ -40,7 +40,7 @@ protected function addOrUpdateOrderItem($model, int $quantity) public function deleteItem(OrderItem $item) { - $this->orderItems = $this->orderItems->reject(fn ($i) => $i->id === $item->id); + $this->items = $this->items->reject(fn ($i) => $i->id === $item->id); $item->delete(); diff --git a/app/Traits/Order/WithOrderItem.php b/app/Traits/Order/WithOrderItem.php index c9459da..6c8ff97 100644 --- a/app/Traits/Order/WithOrderItem.php +++ b/app/Traits/Order/WithOrderItem.php @@ -8,7 +8,7 @@ trait WithOrderItem { public function loadOrderItems() { - return $this->orderItems = OrderItem::query() + return $this->items = OrderItem::query() ->currentUser() ->where('user_id', auth()->id()) ->whereNull('order_id') diff --git a/app/Traits/Order/WithUpdateItem.php b/app/Traits/Order/WithUpdateItem.php index 6ca5f48..9f1f865 100644 --- a/app/Traits/Order/WithUpdateItem.php +++ b/app/Traits/Order/WithUpdateItem.php @@ -39,7 +39,7 @@ public function updateItem() $item->refresh(); - $this->orderItems = $this->orderItems->map(fn ($i) => $i->id === $item->id ? $item : $i); + $this->items = $this->items->map(fn ($i) => $i->id === $item->id ? $item : $i); $this->subtotal = $this->getSubTotal(); diff --git a/resources/views/livewire/studio/manage/order/form.blade.php b/resources/views/livewire/studio/manage/order/form.blade.php index 2325c47..5f15f03 100644 --- a/resources/views/livewire/studio/manage/order/form.blade.php +++ b/resources/views/livewire/studio/manage/order/form.blade.php @@ -206,7 +206,7 @@ - @forelse($orderItems as $item) + @forelse($items as $item)