$items */ class Order extends Model { /** @use HasFactory */ use HasFactory; /** * @return BelongsTo */ public function store(): BelongsTo { return $this->belongsTo(Store::class); } /** * @return HasMany */ public function items(): HasMany { return $this->hasMany(OrderItem::class); } public function isPaid(): bool { return $this->status === OrderStatus::Paid; } /** * The order as shown to people. Money is in ringgit here. * * @return array */ public function toDisplay(): array { return [ 'id' => $this->id, 'public_id' => $this->public_id, 'number' => $this->number, 'status' => $this->status->value, 'status_label' => $this->status->label(), 'customer_name' => $this->customer_name, 'customer_email' => $this->customer_email, 'customer_phone' => $this->customer_phone, 'shipping_address' => $this->shipping_address, 'subtotal' => $this->subtotal / 100, 'discount_code' => $this->discount_code, 'discount_name' => $this->discount_name, 'discount_total' => $this->discount_total / 100, 'shipping_total' => $this->shipping_total / 100, 'total' => $this->total / 100, 'paid_at' => $this->paid_at?->toIso8601String(), 'shipped_at' => $this->shipped_at?->toIso8601String(), 'shipping_courier' => $this->shipping_courier, 'tracking_number' => $this->tracking_number, 'created_at' => $this->created_at?->toIso8601String(), 'items' => $this->items->map(fn (OrderItem $item) => [ 'id' => $item->id, 'name' => $item->name, 'unit_price' => $item->unit_price / 100, 'quantity' => $item->quantity, 'line_total' => $item->line_total / 100, // For a bundle, what to pack for each one: ["2 × Sambal Hijau (250g)", ...]. 'contents' => array_map(fn (array $part) => $part['label'], $item->bundle_contents ?? []), ])->all(), ]; } /** * @return array */ protected function casts(): array { return [ 'status' => OrderStatus::class, 'shipping_address' => 'array', 'paid_at' => 'datetime', 'shipped_at' => 'datetime', ]; } }