AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Order.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\OrderStatus;
  4. use Carbon\CarbonInterface;
  5. use Database\Factories\OrderFactory;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. /**
  12. * One purchase from a store. Money is saved in sen (1 ringgit = 100 sen).
  13. *
  14. * The items keep their own copy of each product's name and price, so an order
  15. * never changes when the merchant later edits or deletes a product.
  16. *
  17. * @property int $id
  18. * @property int $store_id
  19. * @property string $public_id
  20. * @property int $number
  21. * @property OrderStatus $status
  22. * @property string $customer_name
  23. * @property string $customer_email
  24. * @property string $customer_phone
  25. * @property array{line1: string, line2: string|null, city: string, postcode: string, state: string, country: string} $shipping_address
  26. * @property int $subtotal
  27. * @property string|null $discount_code
  28. * @property int $discount_total
  29. * @property int $shipping_total
  30. * @property int $total
  31. * @property string $currency
  32. * @property string|null $payment_provider
  33. * @property string|null $payment_checkout_id
  34. * @property string|null $payment_reference
  35. * @property CarbonInterface|null $paid_at
  36. * @property CarbonInterface|null $shipped_at
  37. * @property string|null $shipping_courier
  38. * @property string|null $tracking_number
  39. * @property CarbonInterface|null $created_at
  40. * @property CarbonInterface|null $updated_at
  41. * @property-read Store $store
  42. * @property-read Collection<int, OrderItem> $items
  43. */
  44. class Order extends Model
  45. {
  46. /** @use HasFactory<OrderFactory> */
  47. use HasFactory;
  48. /**
  49. * @return BelongsTo<Store, $this>
  50. */
  51. public function store(): BelongsTo
  52. {
  53. return $this->belongsTo(Store::class);
  54. }
  55. /**
  56. * @return HasMany<OrderItem, $this>
  57. */
  58. public function items(): HasMany
  59. {
  60. return $this->hasMany(OrderItem::class);
  61. }
  62. public function isPaid(): bool
  63. {
  64. return $this->status === OrderStatus::Paid;
  65. }
  66. /**
  67. * The order as shown to people. Money is in ringgit here.
  68. *
  69. * @return array<string, mixed>
  70. */
  71. public function toDisplay(): array
  72. {
  73. return [
  74. 'id' => $this->id,
  75. 'public_id' => $this->public_id,
  76. 'number' => $this->number,
  77. 'status' => $this->status->value,
  78. 'status_label' => $this->status->label(),
  79. 'customer_name' => $this->customer_name,
  80. 'customer_email' => $this->customer_email,
  81. 'customer_phone' => $this->customer_phone,
  82. 'shipping_address' => $this->shipping_address,
  83. 'subtotal' => $this->subtotal / 100,
  84. 'discount_code' => $this->discount_code,
  85. 'discount_total' => $this->discount_total / 100,
  86. 'shipping_total' => $this->shipping_total / 100,
  87. 'total' => $this->total / 100,
  88. 'paid_at' => $this->paid_at?->toIso8601String(),
  89. 'shipped_at' => $this->shipped_at?->toIso8601String(),
  90. 'shipping_courier' => $this->shipping_courier,
  91. 'tracking_number' => $this->tracking_number,
  92. 'created_at' => $this->created_at?->toIso8601String(),
  93. 'items' => $this->items->map(fn (OrderItem $item) => [
  94. 'id' => $item->id,
  95. 'name' => $item->name,
  96. 'unit_price' => $item->unit_price / 100,
  97. 'quantity' => $item->quantity,
  98. 'line_total' => $item->line_total / 100,
  99. // For a bundle, what to pack for each one: ["2 × Sambal Hijau (250g)", ...].
  100. 'contents' => array_map(fn (array $part) => $part['label'], $item->bundle_contents ?? []),
  101. ])->all(),
  102. ];
  103. }
  104. /**
  105. * @return array<string, string>
  106. */
  107. protected function casts(): array
  108. {
  109. return [
  110. 'status' => OrderStatus::class,
  111. 'shipping_address' => 'array',
  112. 'paid_at' => 'datetime',
  113. 'shipped_at' => 'datetime',
  114. ];
  115. }
  116. }