CustomerOrderItem.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Webkul\BagistoApi\Models;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. use Illuminate\Database\Eloquent\Relations\MorphTo;
  10. /**
  11. * Customer Order Item — nested API resource (no standalone endpoints).
  12. *
  13. * Exposed only as a relationship of CustomerOrder.
  14. * Points to the `order_items` table with explicit field casts.
  15. */
  16. #[ApiResource(
  17. shortName: 'CustomerOrderItem',
  18. operations: [],
  19. graphQlOperations: [],
  20. normalizationContext: ['attributes' => ['id','qty_ordered', 'qty_shipped', 'qty_invoiced', 'qty_canceled', 'qty_refunded', 'sku', 'name', 'price', 'base_price', 'total', 'base_total', 'type', 'product', 'children']],
  21. )]
  22. class CustomerOrderItem extends Model
  23. {
  24. /** @var string */
  25. protected $table = 'order_items';
  26. /** @var array */
  27. protected $appends = ['qtyOrdered', 'qtyShipped', 'qtyInvoiced', 'qtyCanceled', 'qtyRefunded'];
  28. /** @var array */
  29. protected $casts = [
  30. 'id' => 'int',
  31. 'sku' => 'string',
  32. 'type' => 'string',
  33. 'name' => 'string',
  34. 'coupon_code' => 'string',
  35. 'weight' => 'float',
  36. 'total_weight' => 'float',
  37. 'qty_ordered' => 'int',
  38. 'qty_shipped' => 'int',
  39. 'qty_invoiced' => 'int',
  40. 'qty_canceled' => 'int',
  41. 'qty_refunded' => 'int',
  42. 'price' => 'float',
  43. 'base_price' => 'float',
  44. 'total' => 'float',
  45. 'base_total' => 'float',
  46. 'total_invoiced' => 'float',
  47. 'base_total_invoiced' => 'float',
  48. 'amount_refunded' => 'float',
  49. 'base_amount_refunded' => 'float',
  50. 'discount_percent' => 'float',
  51. 'discount_amount' => 'float',
  52. 'base_discount_amount' => 'float',
  53. 'discount_invoiced' => 'float',
  54. 'base_discount_invoiced' => 'float',
  55. 'discount_refunded' => 'float',
  56. 'base_discount_refunded' => 'float',
  57. 'tax_percent' => 'float',
  58. 'tax_amount' => 'float',
  59. 'base_tax_amount' => 'float',
  60. 'tax_amount_invoiced' => 'float',
  61. 'base_tax_amount_invoiced' => 'float',
  62. 'tax_amount_refunded' => 'float',
  63. 'base_tax_amount_refunded' => 'float',
  64. 'price_incl_tax' => 'float',
  65. 'base_price_incl_tax' => 'float',
  66. 'total_incl_tax' => 'float',
  67. 'base_total_incl_tax' => 'float',
  68. 'product_id' => 'int',
  69. 'product_type' => 'string',
  70. 'order_id' => 'int',
  71. 'tax_category_id' => 'int',
  72. 'parent_id' => 'int',
  73. 'created_at' => 'datetime',
  74. 'updated_at' => 'datetime',
  75. ];
  76. /**
  77. * API Platform identifier
  78. */
  79. #[ApiProperty(identifier: true, writable: false)]
  80. public function getId(): int
  81. {
  82. return $this->id;
  83. }
  84. #[ApiProperty(writable: false)]
  85. public function getSku(): ?string
  86. {
  87. return $this->sku;
  88. }
  89. #[ApiProperty(writable: false)]
  90. public function getName(): ?string
  91. {
  92. return $this->name;
  93. }
  94. #[ApiProperty(writable: false)]
  95. public function getQtyOrdered(): ?int
  96. {
  97. return $this->qty_ordered;
  98. }
  99. #[ApiProperty(writable: false)]
  100. public function getQtyShipped(): ?int
  101. {
  102. return $this->qty_shipped;
  103. }
  104. #[ApiProperty(writable: false)]
  105. public function getQtyInvoiced(): ?int
  106. {
  107. return $this->qty_invoiced;
  108. }
  109. #[ApiProperty(writable: false)]
  110. public function getQtyCanceled(): ?int
  111. {
  112. return $this->qty_canceled;
  113. }
  114. #[ApiProperty(writable: false)]
  115. public function getQtyRefunded(): ?int
  116. {
  117. return $this->qty_refunded;
  118. }
  119. #[ApiProperty(writable: false)]
  120. public function getType(): ?string
  121. {
  122. return $this->type;
  123. }
  124. #[ApiProperty(writable: false)]
  125. public function getPrice(): ?float
  126. {
  127. return $this->price;
  128. }
  129. #[ApiProperty(writable: false)]
  130. public function getBasePrice(): ?float
  131. {
  132. return $this->base_price;
  133. }
  134. #[ApiProperty(writable: false)]
  135. public function getTotal(): ?float
  136. {
  137. return $this->total;
  138. }
  139. #[ApiProperty(writable: false)]
  140. public function getBaseTotal(): ?float
  141. {
  142. return $this->base_total;
  143. }
  144. /**
  145. * Polymorphic product relationship.
  146. *
  147. * Mirrors OrderItem::product() — uses the order_items table's
  148. * product_id and product_type columns. The BagistoApi Product model
  149. * overrides getMorphClass() to return the base class name so that
  150. * MorphTo resolution works seamlessly.
  151. */
  152. public function product(): MorphTo
  153. {
  154. return $this->morphTo();
  155. }
  156. /**
  157. * Get the child item record associated with the order item.
  158. */
  159. public function child(): HasOne
  160. {
  161. return $this->hasOne(static::class, 'parent_id');
  162. }
  163. /**
  164. * Get the parent item record associated with the order item.
  165. */
  166. public function parent(): BelongsTo
  167. {
  168. return $this->belongsTo(static::class, 'parent_id');
  169. }
  170. /**
  171. * Get the children items (configurable product variants).
  172. */
  173. public function children(): HasMany
  174. {
  175. return $this->hasMany(static::class, 'parent_id');
  176. }
  177. /**
  178. * Override toArray to ensure qty fields are always included in serialization
  179. */
  180. public function toArray(): array
  181. {
  182. $array = parent::toArray();
  183. // Ensure qty fields are always present
  184. $array['qty_ordered'] = $this->qty_ordered;
  185. $array['qty_shipped'] = $this->qty_shipped;
  186. $array['qty_invoiced'] = $this->qty_invoiced;
  187. $array['qty_canceled'] = $this->qty_canceled;
  188. $array['qty_refunded'] = $this->qty_refunded;
  189. return $array;
  190. }
  191. }