AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Order.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 string|null $discount_name The automatic discount used, when there was no code
  29. * @property int $discount_total
  30. * @property int $shipping_total
  31. * @property int $total
  32. * @property string $currency
  33. * @property string|null $payment_provider
  34. * @property string|null $payment_checkout_id
  35. * @property string|null $payment_url The provider's payment page, so the shopper can come back to it
  36. * @property CarbonInterface|null $payment_expires_at When the payment page stops taking payments
  37. * @property string|null $payment_account The merchant's Stripe account the order was paid into (null: our own account)
  38. * @property string|null $payment_reference
  39. * @property CarbonInterface|null $payment_checked_at When the double-check last asked the provider (PaymentDoubleCheck)
  40. * @property CarbonInterface|null $payment_reminded_at When the shopper was emailed "Complete your payment"
  41. * @property CarbonInterface|null $paid_at
  42. * @property CarbonInterface|null $shipped_at
  43. * @property string|null $shipping_courier
  44. * @property string|null $tracking_number
  45. * @property string|null $shipment_number EasyParcel's number for the booked parcel, such as ES-2602-4VW9E
  46. * @property string|null $shipment_order_number EasyParcel's order number, such as EI-2602-4P2SK
  47. * @property int|null $shipment_cost What the merchant paid EasyParcel, in sen
  48. * @property string|null $label_url The courier label (AWB) to print
  49. * @property string|null $tracking_url
  50. * @property CarbonInterface|null $created_at
  51. * @property CarbonInterface|null $updated_at
  52. * @property-read Store $store
  53. * @property-read Collection<int, OrderItem> $items
  54. */
  55. class Order extends Model
  56. {
  57. /** @use HasFactory<OrderFactory> */
  58. use HasFactory;
  59. /**
  60. * @return BelongsTo<Store, $this>
  61. */
  62. public function store(): BelongsTo
  63. {
  64. return $this->belongsTo(Store::class);
  65. }
  66. /**
  67. * @return HasMany<OrderItem, $this>
  68. */
  69. public function items(): HasMany
  70. {
  71. return $this->hasMany(OrderItem::class);
  72. }
  73. public function isPaid(): bool
  74. {
  75. return $this->status === OrderStatus::Paid;
  76. }
  77. /**
  78. * Whether the shopper can still go back to the payment page and pay: not paid yet, and the page has not expired.
  79. */
  80. public function canStillPay(): bool
  81. {
  82. return $this->status === OrderStatus::Pending
  83. && $this->payment_url !== null
  84. && $this->payment_expires_at?->isFuture() === true;
  85. }
  86. /**
  87. * The order as shown to people. Money is in ringgit here.
  88. *
  89. * @return array<string, mixed>
  90. */
  91. public function toDisplay(): array
  92. {
  93. return [
  94. 'id' => $this->id,
  95. 'public_id' => $this->public_id,
  96. 'number' => $this->number,
  97. 'status' => $this->status->value,
  98. 'status_label' => $this->status->label(),
  99. 'customer_name' => $this->customer_name,
  100. 'customer_email' => $this->customer_email,
  101. 'customer_phone' => $this->customer_phone,
  102. 'shipping_address' => $this->shipping_address,
  103. 'subtotal' => $this->subtotal / 100,
  104. 'discount_code' => $this->discount_code,
  105. 'discount_name' => $this->discount_name,
  106. 'discount_total' => $this->discount_total / 100,
  107. 'shipping_total' => $this->shipping_total / 100,
  108. 'total' => $this->total / 100,
  109. 'paid_at' => $this->paid_at?->toIso8601String(),
  110. // Until when the shopper can pay, and when they were reminded by email. Null for older orders.
  111. 'payment_expires_at' => $this->payment_expires_at?->toIso8601String(),
  112. 'payment_reminded_at' => $this->payment_reminded_at?->toIso8601String(),
  113. 'shipped_at' => $this->shipped_at?->toIso8601String(),
  114. 'shipping_courier' => $this->shipping_courier,
  115. 'tracking_number' => $this->tracking_number,
  116. 'shipment' => $this->shipment_number === null ? null : [
  117. 'number' => $this->shipment_number,
  118. 'cost' => $this->shipment_cost !== null ? $this->shipment_cost / 100 : null,
  119. 'label_url' => $this->label_url ?: null,
  120. 'tracking_url' => $this->tracking_url ?: null,
  121. ],
  122. 'created_at' => $this->created_at?->toIso8601String(),
  123. 'items' => $this->items->map(fn (OrderItem $item) => [
  124. 'id' => $item->id,
  125. 'name' => $item->name,
  126. 'unit_price' => $item->unit_price / 100,
  127. 'quantity' => $item->quantity,
  128. 'line_total' => $item->line_total / 100,
  129. // For a bundle, what to pack for each one: ["2 × Sambal Hijau (250g)", ...].
  130. 'contents' => array_map(fn (array $part) => $part['label'], $item->bundle_contents ?? []),
  131. ])->all(),
  132. ];
  133. }
  134. /**
  135. * @return array<string, string>
  136. */
  137. protected function casts(): array
  138. {
  139. return [
  140. 'status' => OrderStatus::class,
  141. 'payment_expires_at' => 'datetime',
  142. 'payment_checked_at' => 'datetime',
  143. 'payment_reminded_at' => 'datetime',
  144. 'shipping_address' => 'array',
  145. 'paid_at' => 'datetime',
  146. 'shipped_at' => 'datetime',
  147. ];
  148. }
  149. }