refactor: menyesuaikan nama relasi dari orderItems menjadi items
This commit is contained in:
parent
9894ab6de1
commit
5427260c5a
@ -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();
|
||||
|
||||
|
||||
@ -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 = [
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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');
|
||||
}
|
||||
|
||||
@ -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');
|
||||
}
|
||||
|
||||
@ -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');
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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')
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -206,7 +206,7 @@
|
||||
</flux:table.columns>
|
||||
|
||||
<flux:table.rows>
|
||||
@forelse($orderItems as $item)
|
||||
@forelse($items as $item)
|
||||
<flux:table.row>
|
||||
<flux:table.cell>
|
||||
<flux:heading>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user