| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
-
- namespace App\Models;
-
- use App\Enums\OrderStatus;
- use Carbon\CarbonInterface;
- use Database\Factories\OrderFactory;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
-
- /**
- * One purchase from a store. Money is saved in sen (1 ringgit = 100 sen).
- *
- * The items keep their own copy of each product's name and price, so an order
- * never changes when the merchant later edits or deletes a product.
- *
- * @property int $id
- * @property int $store_id
- * @property string $public_id
- * @property int $number
- * @property OrderStatus $status
- * @property string $customer_name
- * @property string $customer_email
- * @property string $customer_phone
- * @property array{line1: string, line2: string|null, city: string, postcode: string, state: string, country: string} $shipping_address
- * @property int $subtotal
- * @property string|null $discount_code
- * @property string|null $discount_name The automatic discount used, when there was no code
- * @property int $discount_total
- * @property int $shipping_total
- * @property int $total
- * @property string $currency
- * @property string|null $payment_provider
- * @property string|null $payment_checkout_id
- * @property string|null $payment_reference
- * @property CarbonInterface|null $paid_at
- * @property CarbonInterface|null $shipped_at
- * @property string|null $shipping_courier
- * @property string|null $tracking_number
- * @property CarbonInterface|null $created_at
- * @property CarbonInterface|null $updated_at
- * @property-read Store $store
- * @property-read Collection<int, OrderItem> $items
- */
- class Order extends Model
- {
- /** @use HasFactory<OrderFactory> */
- use HasFactory;
-
- /**
- * @return BelongsTo<Store, $this>
- */
- public function store(): BelongsTo
- {
- return $this->belongsTo(Store::class);
- }
-
- /**
- * @return HasMany<OrderItem, $this>
- */
- 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<string, mixed>
- */
- 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<string, string>
- */
- protected function casts(): array
- {
- return [
- 'status' => OrderStatus::class,
- 'shipping_address' => 'array',
- 'paid_at' => 'datetime',
- 'shipped_at' => 'datetime',
- ];
- }
- }
|