AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Order.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_account The merchant's Stripe account the order was paid into (null: our own account)
  36. * @property string|null $payment_reference
  37. * @property CarbonInterface|null $paid_at
  38. * @property CarbonInterface|null $shipped_at
  39. * @property string|null $shipping_courier
  40. * @property string|null $tracking_number
  41. * @property string|null $shipment_number EasyParcel's number for the booked parcel, such as ES-2602-4VW9E
  42. * @property string|null $shipment_order_number EasyParcel's order number, such as EI-2602-4P2SK
  43. * @property int|null $shipment_cost What the merchant paid EasyParcel, in sen
  44. * @property string|null $label_url The courier label (AWB) to print
  45. * @property string|null $tracking_url
  46. * @property CarbonInterface|null $created_at
  47. * @property CarbonInterface|null $updated_at
  48. * @property-read Store $store
  49. * @property-read Collection<int, OrderItem> $items
  50. */
  51. class Order extends Model
  52. {
  53. /** @use HasFactory<OrderFactory> */
  54. use HasFactory;
  55. /**
  56. * @return BelongsTo<Store, $this>
  57. */
  58. public function store(): BelongsTo
  59. {
  60. return $this->belongsTo(Store::class);
  61. }
  62. /**
  63. * @return HasMany<OrderItem, $this>
  64. */
  65. public function items(): HasMany
  66. {
  67. return $this->hasMany(OrderItem::class);
  68. }
  69. public function isPaid(): bool
  70. {
  71. return $this->status === OrderStatus::Paid;
  72. }
  73. /**
  74. * The order as shown to people. Money is in ringgit here.
  75. *
  76. * @return array<string, mixed>
  77. */
  78. public function toDisplay(): array
  79. {
  80. return [
  81. 'id' => $this->id,
  82. 'public_id' => $this->public_id,
  83. 'number' => $this->number,
  84. 'status' => $this->status->value,
  85. 'status_label' => $this->status->label(),
  86. 'customer_name' => $this->customer_name,
  87. 'customer_email' => $this->customer_email,
  88. 'customer_phone' => $this->customer_phone,
  89. 'shipping_address' => $this->shipping_address,
  90. 'subtotal' => $this->subtotal / 100,
  91. 'discount_code' => $this->discount_code,
  92. 'discount_name' => $this->discount_name,
  93. 'discount_total' => $this->discount_total / 100,
  94. 'shipping_total' => $this->shipping_total / 100,
  95. 'total' => $this->total / 100,
  96. 'paid_at' => $this->paid_at?->toIso8601String(),
  97. 'shipped_at' => $this->shipped_at?->toIso8601String(),
  98. 'shipping_courier' => $this->shipping_courier,
  99. 'tracking_number' => $this->tracking_number,
  100. 'shipment' => $this->shipment_number === null ? null : [
  101. 'number' => $this->shipment_number,
  102. 'cost' => $this->shipment_cost !== null ? $this->shipment_cost / 100 : null,
  103. 'label_url' => $this->label_url ?: null,
  104. 'tracking_url' => $this->tracking_url ?: null,
  105. ],
  106. 'created_at' => $this->created_at?->toIso8601String(),
  107. 'items' => $this->items->map(fn (OrderItem $item) => [
  108. 'id' => $item->id,
  109. 'name' => $item->name,
  110. 'unit_price' => $item->unit_price / 100,
  111. 'quantity' => $item->quantity,
  112. 'line_total' => $item->line_total / 100,
  113. // For a bundle, what to pack for each one: ["2 × Sambal Hijau (250g)", ...].
  114. 'contents' => array_map(fn (array $part) => $part['label'], $item->bundle_contents ?? []),
  115. ])->all(),
  116. ];
  117. }
  118. /**
  119. * @return array<string, string>
  120. */
  121. protected function casts(): array
  122. {
  123. return [
  124. 'status' => OrderStatus::class,
  125. 'shipping_address' => 'array',
  126. 'paid_at' => 'datetime',
  127. 'shipped_at' => 'datetime',
  128. ];
  129. }
  130. }