PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / api / Orders.php

Orders.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at api/Orders.php

157 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\Order;
7 use FluentCart\App\Models\OrderItem;
8 use FluentCart\App\Helpers\Status;
9 use FluentCart\Framework\Database\Orm\Builder;
10 use FluentCart\Framework\Database\Orm\Relations\HasOne;
11 use FLuentCart\Framework\Support\Arr;
12 use FluentCart\Api\Resource\OrderMetaResource;
13
14 class Orders
15 {
16
17 /**
18 * @return array $orders
19 *
20 * It will return all order lists by query passed
21 *
22 */
23 public function get($params)
24 {
25 $orders = Order::with(['customer', 'order_items'])
26 ->whereHas('customer', function ($query) use ($params) {
27 $query->when(Arr::get($params["params"], 'search'), function ($query) use ($params) {
28 return $query->search(Arr::get($params["params"], 'search', ''));
29 });
30 })
31 ->applyCustomFilters(Arr::get($params["params"], 'filters', []))
32 ->orderBy(
33 sanitize_sql_orderby(Arr::get($params["params"], 'order_by', 'id')),
34 sanitize_sql_orderby(Arr::get($params["params"], 'order_type', 'DESC')))
35 ->paginate(Arr::get($params["params"], 'per_page'), ['*'], 'page', Arr::get($params["params"], 'page'));
36
37 return [
38 'orders' => $orders
39 ];
40 }
41
42 public function getByHash($hash)
43 {
44 $order = Order::with(
45 [
46 'customer' => function ($query) {
47 $query->with(['billing_address' => function ($query) {
48 $query->where('is_primary', '1');
49 }]);
50 $query->with(['shipping_address' => function ($query) {
51 $query->where('is_primary', '1');
52 }]);
53 },
54 'transactions'
55 ])
56 ->where('uuid', $hash)
57 ->first();
58
59 return $order;
60 }
61
62
63 /**
64 * @param array $data array of order details
65 * @return string ID of order created
66 *
67 * Will create order with blank order items
68 */
69 public function create($data)
70 {
71
72 $data['currency'] = (new StoreSettings())->getCurrency();
73
74 return (new Order())->store($data);
75 }
76
77 /**
78 *
79 * @param Order $order
80 * @param $orderData - will be the order updated data array
81 * @param $deleteItems
82 * @return array
83 * @throws \Exception
84 */
85 public function update($order, $orderData, $deleteItems = [], $discount = '', $shipping = '')
86 {
87
88 if (empty($order) || $order->status === Status::ORDER_COMPLETED) {
89
90 throw new \Exception(esc_html__('Order information does not match.', 'fluent-cart'));
91 }
92 $orderId = $order->id;
93
94 /**
95 * First delete the deleted items
96 */
97 if (!empty($deleteItems)) {
98
99 OrderItem::destroy($deleteItems);
100 }
101
102 if (!empty($discount)) {
103
104 OrderMetaApi::updateDiscountMeta($orderId, $discount);
105 }
106
107 if (!empty($shipping)) {
108 OrderMetaApi::updateShippingMeta($orderId, $shipping);
109 }
110
111 $items = Arr::get($orderData, 'order_items');
112
113 try {
114
115 (new OrderItems)->updateOrInsertOrderItems($orderId, $items);
116
117 } catch (\Exception $ex) {
118
119 throw $ex;
120 }
121
122 unset($orderData['order_items']);
123 unset($orderData['customer']);
124
125 $orderData['currency'] = (new StoreSettings())->get('checkout_currency', 'BDT');
126
127 $order->update($orderData);
128
129 return [
130 'message' => __('Order updated.', 'fluent-cart')
131 ];
132 }
133
134 public function getBy(string $column, $value)
135 {
136 return Order::query()->where($column, $value)
137 ->with(['customer' => function ($query) {
138 $query->with('billing_address')
139 ->with('shipping_address');
140 }])
141 ->with(['shipping_address' => function (HasOne $query) {
142 return $query->addAppends(['first_name', 'last_name']);
143 }])
144 ->with(['billing_address' => function (HasOne $query) {
145 return $query->addAppends(['first_name', 'last_name']);
146 }])
147 ->with('order_items')
148 ->first();
149 }
150
151 public function getById($id)
152 {
153 return $this->getBy('id', $id);
154 }
155
156 }
157