OrderController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace Webkul\Shop\Http\Controllers\API;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\JsonResponse;
  5. use Illuminate\Http\Request;
  6. use Longyi\RewardPoints\Helpers\ApiResponse;
  7. use Webkul\Sales\Models\Order;
  8. use Webkul\Sales\Repositories\OrderRepository;
  9. class OrderController extends APIController
  10. {
  11. /**
  12. * Create a new controller instance.
  13. */
  14. public function __construct(protected OrderRepository $orderRepository) {}
  15. /**
  16. * 获取当前客户订单列表(分页)
  17. *
  18. * @param Request $request
  19. * @return JsonResponse
  20. */
  21. public function index(Request $request): JsonResponse
  22. {
  23. $customer = auth()->guard('customer')->user();
  24. if (! $customer) {
  25. return ApiResponse::unauthorized();
  26. }
  27. $page = (int) $request->input('page', 1);
  28. $limit = (int) $request->input('limit', 10);
  29. $limit = min(max($limit, 1), 100);
  30. $status = $request->input('status');
  31. $query = Order::query()
  32. ->where('customer_id', $customer->id)
  33. ->orderBy('created_at', 'desc');
  34. if ($status) {
  35. $query->where('status', $status);
  36. }
  37. $paginator = $query->paginate($limit, ['*'], 'page', $page);
  38. $orders = $paginator->through(function ($order) {
  39. return $this->formatOrderListItem($order);
  40. });
  41. return ApiResponse::paginated($orders->values(), $paginator);
  42. }
  43. /**
  44. * 获取订单详情
  45. *
  46. * @param int $id
  47. * @return JsonResponse
  48. */
  49. public function show(int $id): JsonResponse
  50. {
  51. $customer = auth()->guard('customer')->user();
  52. if (! $customer) {
  53. return ApiResponse::unauthorized();
  54. }
  55. $order = Order::query()
  56. ->with([
  57. 'items' => function ($q) {
  58. $q->whereNull('parent_id');
  59. },
  60. 'items.child',
  61. 'items.additional',
  62. 'billing_address',
  63. 'shipping_address',
  64. 'payment',
  65. 'invoices',
  66. 'invoices.items',
  67. 'shipments',
  68. 'shipments.items',
  69. 'refunds',
  70. 'comments',
  71. ])
  72. ->where('customer_id', $customer->id)
  73. ->where('id', $id)
  74. ->first();
  75. if (! $order) {
  76. return ApiResponse::notFound('Order not found');
  77. }
  78. return ApiResponse::success($this->formatOrderDetail($order));
  79. }
  80. /**
  81. * 格式化订单列表项
  82. */
  83. protected function formatOrderListItem(Order $order): array
  84. {
  85. return [
  86. 'id' => $order->id,
  87. 'increment_id' => $order->increment_id,
  88. 'status' => $order->status,
  89. 'status_label' => $this->getStatusLabel($order->status),
  90. 'grand_total' => (float) $order->grand_total,
  91. 'order_currency_code' => $order->order_currency_code,
  92. 'item_count' => $order->items->sum('qty_ordered'),
  93. 'items' => $order->items->map(fn ($item) => [
  94. 'id' => $item->id,
  95. 'name' => $item->name,
  96. 'sku' => $item->sku,
  97. 'qty_ordered' => $item->qty_ordered,
  98. 'price' => (float) $item->price,
  99. 'total' => (float) $item->total,
  100. 'product_id' => $item->product_id,
  101. ])->values(),
  102. 'created_at' => $order->created_at ? Carbon::parse($order->created_at)->toDateTimeString() : null,
  103. 'can_cancel' => $order->canCancel(),
  104. 'can_reorder' => $order->canReorder(),
  105. ];
  106. }
  107. /**
  108. * 格式化订单详情
  109. */
  110. protected function formatOrderDetail(Order $order): array
  111. {
  112. return [
  113. 'id' => $order->id,
  114. 'increment_id' => $order->increment_id,
  115. 'status' => $order->status,
  116. 'status_label' => $this->getStatusLabel($order->status),
  117. 'customer_email' => $order->customer_email,
  118. 'customer_first_name' => $order->customer_first_name,
  119. 'customer_last_name' => $order->customer_last_name,
  120. 'channel_name' => $order->channel_name,
  121. 'order_currency_code' => $order->order_currency_code,
  122. 'sub_total' => (float) $order->sub_total,
  123. 'shipping_amount' => (float) $order->shipping_amount,
  124. 'tax_amount' => (float) ($order->tax_amount ?? 0),
  125. 'discount_amount' => (float) ($order->discount_amount ?? 0),
  126. 'grand_total' => (float) $order->grand_total,
  127. 'total_qty_ordered'=> $order->total_qty_ordered,
  128. 'shipping_method' => $order->shipping_method,
  129. 'shipping_title' => $order->shipping_title,
  130. 'billing_address' => $this->formatAddress($order->billing_address),
  131. 'shipping_address' => $this->formatAddress($order->shipping_address),
  132. 'payment' => $order->payment ? [
  133. 'method' => $order->payment->method,
  134. 'method_title' => $order->payment->method_title,
  135. ] : null,
  136. 'items' => $order->items->map(fn ($item) => [
  137. 'id' => $item->id,
  138. 'name' => $item->name,
  139. 'sku' => $item->sku,
  140. 'type' => $item->type,
  141. 'qty_ordered' => $item->qty_ordered,
  142. 'qty_shipped' => (int) ($item->qty_shipped ?? 0),
  143. 'qty_invoiced' => (int) ($item->qty_invoiced ?? 0),
  144. 'qty_canceled' => (int) ($item->qty_canceled ?? 0),
  145. 'qty_refunded' => (int) ($item->qty_refunded ?? 0),
  146. 'price' => (float) $item->price,
  147. 'total' => (float) $item->total,
  148. 'weight' => (float) ($item->weight ?? 0),
  149. 'product_id' => $item->product_id,
  150. 'additional' => $item->additional ? $item->additional->toArray() : null,
  151. ])->values(),
  152. 'invoices' => $order->invoices->map(fn ($invoice) => [
  153. 'id' => $invoice->id,
  154. 'increment_id' => $invoice->increment_id ?? ('#' . $invoice->id),
  155. 'state' => $invoice->state,
  156. 'email_sent' => $invoice->email_sent,
  157. 'total_qty' => $invoice->total_qty,
  158. 'sub_total' => (float) $invoice->sub_total,
  159. 'grand_total' => (float) $invoice->grand_total,
  160. 'created_at' => $invoice->created_at ? Carbon::parse($invoice->created_at)->toDateTimeString() : null,
  161. 'items' => $invoice->items->map(fn ($invItem) => [
  162. 'name' => $invItem->name,
  163. 'qty' => $invItem->qty,
  164. 'price' => (float) $invItem->price,
  165. 'total' => (float) $invItem->total,
  166. ])->values(),
  167. ])->values(),
  168. 'shipments' => $order->shipments->map(fn ($shipment) => [
  169. 'id' => $shipment->id,
  170. 'increment_id' => $shipment->increment_id ?? ('#' . $shipment->id),
  171. 'status' => $shipment->status,
  172. 'total_qty' => $shipment->total_qty,
  173. 'total_weight' => (float) $shipment->total_weight,
  174. 'carrier_code' => $shipment->carrier_code,
  175. 'carrier_title'=> $shipment->carrier_title,
  176. 'track_number' => $shipment->track_number,
  177. 'created_at' => $shipment->created_at ? Carbon::parse($shipment->created_at)->toDateTimeString() : null,
  178. 'items' => $shipment->items->map(fn ($shipItem) => [
  179. 'name' => $shipItem->name,
  180. 'qty' => $shipItem->qty,
  181. ])->values(),
  182. ])->values(),
  183. 'refunds' => $order->refunds->map(fn ($refund) => [
  184. 'id' => $refund->id,
  185. 'increment_id' => $refund->increment_id ?? ('#' . $refund->id),
  186. 'state' => $refund->state,
  187. 'total_qty' => $refund->total_qty,
  188. 'grand_total' => (float) $refund->grand_total,
  189. 'created_at' => $refund->created_at ? Carbon::parse($refund->created_at)->toDateTimeString() : null,
  190. ])->values(),
  191. 'comments' => $order->comments->map(fn ($comment) => [
  192. 'comment' => $comment->comment,
  193. 'customer_notified' => $comment->customer_notified,
  194. 'created_at' => $comment->created_at ? Carbon::parse($comment->created_at)->toDateTimeString() : null,
  195. ])->values(),
  196. 'can_cancel' => $order->canCancel(),
  197. 'can_reorder' => $order->canReorder(),
  198. 'created_at' => $order->created_at ? Carbon::parse($order->created_at)->toDateTimeString() : null,
  199. 'updated_at' => $order->updated_at ? Carbon::parse($order->updated_at)->toDateTimeString() : null,
  200. ];
  201. }
  202. /**
  203. * 格式化地址
  204. */
  205. protected function formatAddress($address): ?array
  206. {
  207. if (! $address) {
  208. return null;
  209. }
  210. return [
  211. 'first_name' => $address->first_name,
  212. 'last_name' => $address->last_name,
  213. 'email' => $address->email,
  214. 'phone' => $address->phone,
  215. 'address1' => $address->address1,
  216. 'address2' => $address->address2,
  217. 'city' => $address->city,
  218. 'state' => $address->state,
  219. 'country' => $address->country,
  220. 'postcode' => $address->postcode,
  221. 'company' => $address->company_name,
  222. ];
  223. }
  224. /**
  225. * 获取状态中文标签
  226. */
  227. protected function getStatusLabel(string $status): string
  228. {
  229. return match ($status) {
  230. Order::STATUS_PENDING => '待处理',
  231. Order::STATUS_PENDING_PAYMENT => '待付款',
  232. Order::STATUS_PROCESSING => '处理中',
  233. Order::STATUS_COMPLETED => '已完成',
  234. Order::STATUS_CANCELED => '已取消',
  235. Order::STATUS_CLOSED => '已关闭',
  236. Order::STATUS_FRAUD => '欺诈',
  237. default => $status,
  238. };
  239. }
  240. }