refactor: menyesuaikan nama relasi dari orderItems menjadi items

This commit is contained in:
Yoga Pangestu 2025-11-01 18:46:09 +07:00
parent 9894ab6de1
commit 5427260c5a
12 changed files with 24 additions and 24 deletions

View File

@ -75,17 +75,17 @@ public function store()
{ {
$this->validate(); $this->validate();
$orderItems = $this->loadOrderItems(); $items = $this->loadOrderItems();
// Generate a unique invoice number based on today's paid order count // Generate a unique invoice number based on today's paid order count
$todayCount = Order::whereDate('created_at', now())->paid()->count() + 1; $todayCount = Order::whereDate('created_at', now())->paid()->count() + 1;
$invoiceNumber = 'INV'.now()->format('Ymd').str_pad($todayCount, 4, '0', STR_PAD_LEFT); $invoiceNumber = 'INV'.now()->format('Ymd').str_pad($todayCount, 4, '0', STR_PAD_LEFT);
// Calculate total cost of goods sold (COGS) — internal purchase cost // 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 // 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); $discount = replaceCurrency($this->discount);
@ -94,7 +94,7 @@ public function store()
// Wrap the entire operation in a database transaction // Wrap the entire operation in a database transaction
// Ensures atomicity — if any step fails, all changes are rolled back // 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([ $order = Order::create([
'outlet_id' => $this->outlet_id, 'outlet_id' => $this->outlet_id,
'user_id' => auth()->id(), 'user_id' => auth()->id(),
@ -110,7 +110,7 @@ public function store()
]); ]);
// Attach all order items to the newly created order // 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->order_id = $order->id;
$item->save(); $item->save();

View File

@ -29,13 +29,13 @@ class Tier extends Component
public function mount() 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') ->withCount('memberships')
->orderBy('min_points', 'asc') ->orderBy('min_points', 'asc')
->get() ->get()
->map(function ($tier) { ->map(function ($tier) {
$topMembership = $tier->memberships() $topMembership = $tier->memberships()
->with(['user.customer.orders.orderItems', 'user.customer.orders.payments']) ->with(['user.customer.orders.items', 'user.customer.orders.payments'])
->orderByDesc('total_points') ->orderByDesc('total_points')
->first(); ->first();
@ -44,7 +44,7 @@ public function mount()
$orders = $topMembership->user->customer->orders; $orders = $topMembership->user->customer->orders;
$totalOrders = $orders->count(); $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'); $totalAmount = $orders->flatMap(fn ($order) => $order->payments)->sum('amount');
$topMemberStats = [ $topMemberStats = [

View File

@ -41,7 +41,7 @@ class Create extends Component
public string $total = ''; public string $total = '';
public $orderItems; public $items;
public function mount() public function mount()
{ {
@ -58,7 +58,7 @@ public function mount()
$this->products = Product::orderBy('name')->pluck('name', 'id')->toArray(); $this->products = Product::orderBy('name')->pluck('name', 'id')->toArray();
$this->orderItems = $this->loadOrderItems(); $this->items = $this->loadOrderItems();
$this->subtotal = $this->getSubTotal(); $this->subtotal = $this->getSubTotal();
@ -69,7 +69,7 @@ public function save()
{ {
$this->canOrAbort('create order'); $this->canOrAbort('create order');
if ($this->orderItems->isEmpty()) { if ($this->items->isEmpty()) {
$this->toast('Keranjang tidak boleh kosong.', 'Gagal', 'danger'); $this->toast('Keranjang tidak boleh kosong.', 'Gagal', 'danger');
return; return;

View File

@ -47,7 +47,7 @@ public function purchaseItems(): MorphMany
return $this->morphMany(PurchaseItem::class, 'purchasable'); return $this->morphMany(PurchaseItem::class, 'purchasable');
} }
public function orderItems(): MorphMany public function items(): MorphMany
{ {
return $this->morphMany(PurchaseItem::class, 'orderable'); return $this->morphMany(PurchaseItem::class, 'orderable');
} }

View File

@ -57,7 +57,7 @@ public function purchaseItems(): MorphMany
return $this->morphMany(PurchaseItem::class, 'purchasable'); return $this->morphMany(PurchaseItem::class, 'purchasable');
} }
public function orderItems(): MorphMany public function items(): MorphMany
{ {
return $this->morphMany(PurchaseItem::class, 'orderable'); return $this->morphMany(PurchaseItem::class, 'orderable');
} }

View File

@ -44,7 +44,7 @@ public function purchaseItems(): MorphMany
return $this->morphMany(PurchaseItem::class, 'purchasable'); return $this->morphMany(PurchaseItem::class, 'purchasable');
} }
public function orderItems(): MorphMany public function items(): MorphMany
{ {
return $this->morphMany(PurchaseItem::class, 'orderable'); return $this->morphMany(PurchaseItem::class, 'orderable');
} }

View File

@ -87,7 +87,7 @@ public function orders(): HasMany
return $this->hasMany(Order::class); return $this->hasMany(Order::class);
} }
public function orderItems(): HasMany public function items(): HasMany
{ {
return $this->hasMany(OrderItem::class); return $this->hasMany(OrderItem::class);
} }

View File

@ -6,11 +6,11 @@ trait WithCalculateTotal
{ {
public function getSubTotal() 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() 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);
} }
} }

View File

@ -14,7 +14,7 @@ trait WithManageItem
*/ */
protected function addOrUpdateOrderItem($model, int $quantity) 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) { if ($existing) {
$existing->increment('quantity', $quantity); $existing->increment('quantity', $quantity);
@ -29,10 +29,10 @@ protected function addOrUpdateOrderItem($model, int $quantity)
'unit_price' => $model->sale_price ?? 0, '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->subtotal = $this->getSubtotal();
$this->total = $this->getTotal(); $this->total = $this->getTotal();
@ -40,7 +40,7 @@ protected function addOrUpdateOrderItem($model, int $quantity)
public function deleteItem(OrderItem $item) 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(); $item->delete();

View File

@ -8,7 +8,7 @@ trait WithOrderItem
{ {
public function loadOrderItems() public function loadOrderItems()
{ {
return $this->orderItems = OrderItem::query() return $this->items = OrderItem::query()
->currentUser() ->currentUser()
->where('user_id', auth()->id()) ->where('user_id', auth()->id())
->whereNull('order_id') ->whereNull('order_id')

View File

@ -39,7 +39,7 @@ public function updateItem()
$item->refresh(); $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(); $this->subtotal = $this->getSubTotal();

View File

@ -206,7 +206,7 @@
</flux:table.columns> </flux:table.columns>
<flux:table.rows> <flux:table.rows>
@forelse($orderItems as $item) @forelse($items as $item)
<flux:table.row> <flux:table.row>
<flux:table.cell> <flux:table.cell>
<flux:heading> <flux:heading>