AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Order.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 int $shipping_total
  28. * @property int $total
  29. * @property string $currency
  30. * @property string|null $payment_provider
  31. * @property string|null $payment_checkout_id
  32. * @property string|null $payment_reference
  33. * @property CarbonInterface|null $paid_at
  34. * @property CarbonInterface|null $shipped_at
  35. * @property string|null $shipping_courier
  36. * @property string|null $tracking_number
  37. * @property CarbonInterface|null $created_at
  38. * @property CarbonInterface|null $updated_at
  39. * @property-read Store $store
  40. * @property-read Collection<int, OrderItem> $items
  41. */
  42. class Order extends Model
  43. {
  44. /** @use HasFactory<OrderFactory> */
  45. use HasFactory;
  46. /**
  47. * @return BelongsTo<Store, $this>
  48. */
  49. public function store(): BelongsTo
  50. {
  51. return $this->belongsTo(Store::class);
  52. }
  53. /**
  54. * @return HasMany<OrderItem, $this>
  55. */
  56. public function items(): HasMany
  57. {
  58. return $this->hasMany(OrderItem::class);
  59. }
  60. public function isPaid(): bool
  61. {
  62. return $this->status === OrderStatus::Paid;
  63. }
  64. /**
  65. * The order as shown to people. Money is in ringgit here.
  66. *
  67. * @return array<string, mixed>
  68. */
  69. public function toDisplay(): array
  70. {
  71. return [
  72. 'id' => $this->id,
  73. 'public_id' => $this->public_id,
  74. 'number' => $this->number,
  75. 'status' => $this->status->value,
  76. 'status_label' => $this->status->label(),
  77. 'customer_name' => $this->customer_name,
  78. 'customer_email' => $this->customer_email,
  79. 'customer_phone' => $this->customer_phone,
  80. 'shipping_address' => $this->shipping_address,
  81. 'subtotal' => $this->subtotal / 100,
  82. 'shipping_total' => $this->shipping_total / 100,
  83. 'total' => $this->total / 100,
  84. 'paid_at' => $this->paid_at?->toIso8601String(),
  85. 'shipped_at' => $this->shipped_at?->toIso8601String(),
  86. 'shipping_courier' => $this->shipping_courier,
  87. 'tracking_number' => $this->tracking_number,
  88. 'created_at' => $this->created_at?->toIso8601String(),
  89. 'items' => $this->items->map(fn (OrderItem $item) => [
  90. 'id' => $item->id,
  91. 'name' => $item->name,
  92. 'unit_price' => $item->unit_price / 100,
  93. 'quantity' => $item->quantity,
  94. 'line_total' => $item->line_total / 100,
  95. ])->all(),
  96. ];
  97. }
  98. /**
  99. * @return array<string, string>
  100. */
  101. protected function casts(): array
  102. {
  103. return [
  104. 'status' => OrderStatus::class,
  105. 'shipping_address' => 'array',
  106. 'paid_at' => 'datetime',
  107. 'shipped_at' => 'datetime',
  108. ];
  109. }
  110. }