PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
1.6.5 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 All 48 releases
fluent-cart / api / Resource / OrderItemResource.php

OrderItemResource.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.27, at api/Resource/OrderItemResource.php

337 lines 16.0 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\Resource;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\OrderItem;
7 use FluentCart\Framework\Database\Orm\Builder;
8 use FluentCart\Framework\Support\Arr;
9 use FluentCart\Framework\Support\Collection;
10 use FluentCart\Framework\Support\DateTime;
11
12 class OrderItemResource extends BaseResourceApi
13 {
14
15 public static function getQuery(): Builder
16 {
17 return OrderItem::query();
18 }
19
20 public static function get(array $params = [])
21 {
22
23 }
24
25 /**
26 * Find and retrieve order items based on the order ID.
27 *
28 * @param int $id Required. The ID of the order to find and retrieve.
29 * @param array $params Optional. Additional parameters for finding order items.
30 * [
31 * // Include optional parameters, if any.
32 * ]
33 *
34 */
35 public static function find($id, $params = [])
36 {
37 return static::getQuery()->where('order_id', $id)->get()->toArray();
38 }
39
40 /**
41 * Create order items with the provided data.
42 *
43 * This function creates order items based on the provided data.
44 * It also dispatches events related to order creation and stock changes.
45 *
46 * @param array $data Required. Array containing the necessary parameters
47 * $data => (array) Required. Array of order item details.
48 * [
49 * 'order_id' => (int) The ID of the order to which the item belongs.
50 * 'post_id' => (int) The product ID associated with the order item.
51 * 'object_id' => (int) The variation ID of the order item.
52 * 'thumbnail' => (string) The URL of the thumbnail of order item.
53 * 'price' => (float) The price of the item.
54 * 'title' => (string) The name of the item.
55 * 'quantity' => (int) The quantity of the item.
56 * 'fulfillment_type' => (string) Type (e.g., 'physical', 'digital').
57 * 'stockStatus' => (string) (e.g., 'in-stock'|'out-of-stock').
58 * 'stock' => (int) The current stock quantity.
59 * 'tax_amount' => (float) The tax amount for the item.
60 * 'discount_total' => (float) The total discount amount for the item.
61 * 'total' => (float) The total amount for the item.
62 * 'line_total' => (float) The total amount for the line
63 * ]
64 * @param array $params Optional. Additional parameters for order item creation.
65 * [
66 * // Include optional parameters, if any.
67 * ]
68 *
69 */
70 public static function create($data, $params = [])
71 {
72 $isCreated = static::getQuery()->insert($data);
73
74 if ($isCreated) {
75 return static::makeSuccessResponse(
76 $isCreated,
77 __('Order items created.', 'fluent-cart')
78 );
79 }
80
81 return static::makeErrorResponse([
82 ['code' => 400, 'message' => __('Order items failed to create!', 'fluent-cart')]
83 ]);
84 }
85
86 /**
87 * Update order items with the provided data.
88 *
89 * This function creates order items based on the provided data.
90 * It also dispatches events related to order creation and stock changes.
91 *
92 * @param array $data Required. Array containing the necessary parameters
93 * $data => (array) Required. Array of order item details.
94 * [
95 * 'id' => (int) The id for the order item.
96 * 'order_id' => (int) The ID of the order to which the item belongs.
97 * 'post_id' => (int) The product ID associated with the order item.
98 * 'object_id' => (int) The variation ID of the order item.
99 * 'thumbnail' => (string) The URL of the thumbnail of order item.
100 * 'price' => (float) The price of the item.
101 * 'title' => (string) The name of the item.
102 * 'quantity' => (int) The quantity of the item.
103 * 'fulfillment_type' => (string) Type (e.g., 'physical', 'digital').
104 * 'stockStatus' => (string) (e.g., 'in-stock'|'out-of-stock').
105 * 'stock' => (int) The current stock quantity.
106 * 'tax_amount' => (float) The tax amount for the item.
107 * 'discount_total' => (float) The total discount amount for the item.
108 * 'total' => (float) The total amount for the item.
109 * 'line_total' => (float) The total amount for the line
110 * ]
111 * @param int $id Required. The id of the order item to update.
112 * @param array $params Optional. Additional parameters for order item creation.
113 * [
114 * // Include optional parameters, if any.
115 * ]
116 *
117 */
118 public static function update($data, $id, $params = [])
119 {
120 $isUpdate = static::getQuery()->batchUpdate($data);
121
122 if ($isUpdate) {
123 return static::makeSuccessResponse(
124 $isUpdate,
125 __('Order items updated.', 'fluent-cart')
126 );
127 }
128
129 return static::makeErrorResponse([
130 ['code' => 400, 'message' => __('Order items failed to update!', 'fluent-cart')]
131 ]);
132 }
133
134 public static function delete($id, $params = [])
135 {
136
137 }
138
139 /**
140 * Create or Update order items with the provided data.
141 *
142 * This function creates or updates order items based on the provided data.
143 *
144 * @param array $data Required. Array containing the necessary parameters
145 * $order => (array) Required. Array of order details.
146 * [
147 * 'id' => (int) The id for the order.
148 * 'status' => (string) The current status of the order
149 * 'parent_id' => (int) The parent order ID, if applicable.
150 * 'invoice_no' => (string) The order number assigned to the order.
151 * 'receipt_number' => (int) The sequential order number for using in invoice
152 * 'fulfillment_type' => (string) (e.g., 'virtual', 'physical', etc.).
153 * 'type' => (string) Type (e.g., 'sale', 'refund', etc.).
154 * 'customer_id' => (int) The ID of the customer associated with the order.
155 * 'payment_method' => (string) The payment method used for the order.
156 * 'payment_method_title' => (string) The title of the payment method.
157 * 'currency' => (string) The currency used for the order (e.g., 'BDT').
158 * 'subtotal' => (float) The subtotal amount of the order.
159 * 'discount_tax' => (float) The tax amount on discounts.
160 * 'discount_total' => (float) The total discount amount for the order.
161 * 'shipping_tax' => (float) The tax amount on shipping.
162 * 'shipping_total' => (float) The total shipping amount for the order.
163 * 'tax_total' => (float) The total tax amount for the order.
164 * 'total_amount' => (float) The total amount for the order.
165 * 'total_paid' => (float) The total amount paid for the order.
166 * 'rate' => (float) The exchange rate used for currency conversion.
167 * 'note' => (string) Additional notes or comments for the order.
168 * 'ip_address' => (string) The IP address associated with the order.
169 * 'completed_at' => (string|null) date-time order completed|null
170 * 'refunded_at' => (string|null) date-time the order was refunded|null
171 * 'uuid' => (string) The id for the order.
172 * 'created_at' => (string) The date and time the order was created.
173 * 'updated_at' => (string) The date and time the order was last updated.
174 * ]
175 * @param int $id Required. The id of the order to create or update.
176 * @param array $params Required. Additional parameters for order item creation|update.
177 * $params => (array) Required. Array of order item details.
178 * [
179 * 'id' => (int) The id for the order item|null if not.
180 * 'order_id' => (int) The ID of the order to which the item belongs.
181 * 'post_id' => (int) The product ID associated with the order item.
182 * 'object_id' => (int) The variation ID of the order item.
183 * 'thumbnail' => (string) The URL of the thumbnail of order item.
184 * 'unit_price' => (float) The price of the item.
185 * 'title' => (string) The name of the item.
186 * 'quantity' => (int) The quantity of the item.
187 * 'fulfillment_type' => (string) Type (e.g., 'physical', 'digital').
188 * 'stockStatus' => (string) (e.g., 'in-stock'|'out-of-stock').
189 * 'stock' => (int) The current stock quantity.
190 * 'tax_amount' => (float) The tax amount for the item.
191 * 'discount_total' => (float) The total discount amount for the item.
192 * 'total' => (float) The total amount for the item.
193 * 'line_total' => (float) The total amount for the line
194 * ]
195 *
196 */
197 public static function updateOrInsertOrderItems($order, $id, $params = [], $couponCalculation = [])
198 {
199 if (empty($params) || !is_array($params)) {
200 return static::makeErrorResponse([
201 ['code' => 403, 'message' => __('No items given.', 'fluent-cart')]
202 ]);
203 }
204
205 $newItems = [];
206 $existingItems = [];
207
208 $variationIds = (new Collection($params))->pluck('object_id')->toArray();
209
210 $orderItems = static::getQuery()
211 ->where('order_id', $id)
212 ->whereIn('object_id', $variationIds)
213 ->get();
214
215 foreach ($params as $idx => &$item) {
216 $item['order_id'] = $id;
217 $productId = Arr::get($item, 'post_id');
218 $variationId = Arr::get($item, 'object_id', null);
219 $isCustom = Arr::get($item, 'other_info.is_custom', false);
220
221 if (isset($productId) && isset($variationId) && !$isCustom) {
222
223 $orderItem = $orderItems->where('post_id', $productId)
224 ->where('object_id', $variationId)->first();
225
226 $isExist = !empty($orderItem);
227
228 $itemData = Arr::only($item, ['id', 'title', 'post_id', 'object_id', 'post_title', 'quantity', 'unit_price', 'cost', 'tax_amount', 'discount_total', 'line_total']);
229 $itemData['subtotal'] = Arr::get($item, 'quantity', 0) * Arr::get($item, 'unit_price', 0);
230 $itemData['line_total'] = $itemData['subtotal'] - Arr::get($item, 'discount_total', 0) + Arr::get($item, 'tax_amount', 0);
231 $fulfillment_type = Arr::get($item, 'fulfillment_type', 'physical');
232
233
234 if (empty($isExist)) {
235 unset($itemData['id']);
236 $item = wp_parse_args([
237 'order_id' => $id,
238 'cart_index' => $idx + 1,
239 'fulfillment_type' => $fulfillment_type,
240 'other_info' => json_encode(Arr::get($item, 'other_info', [])),
241 'payment_type' => Arr::get($item, 'payment_type', Arr::get($item, 'other_info.payment_type', '')),
242 'shipping_charge' => Arr::get($item, 'shipping_charge', 0),
243 'created_at' => DateTime::now(),
244 'updated_at' => DateTime::now(),
245 'fulfilled_quantity' => $fulfillment_type === 'physical' ? 0 : Arr::get($item, 'quantity', 0)
246 ], $itemData);
247 $newItems[] = $item;
248 } else {
249 $existingData = Arr::only($itemData, ['id', 'quantity', 'unit_price', 'cost', 'subtotal', 'tax_amount', 'discount_total', 'line_total', 'shipping_charge']) + ['updated_at' => DateTime::now()];
250 $existingData['fulfilled_quantity'] = $fulfillment_type === 'physical' ? Arr::get($item, 'fulfilled_quantity', 0) : Arr::get($item, 'quantity', 0);
251 $existingItems[] = $existingData;
252 }
253 }
254 else if(isset($productId) && isset($variationId) && $isCustom){
255 $oldItem = $orderItems->firstWhere('object_id', $variationId);
256 $filteredItem = (array) apply_filters('fluent_cart/order/custom_item_changed',
257 $oldItem,
258 $item
259 );
260
261 $filteredData = Arr::only($filteredItem, ['id', 'quantity', 'unit_price', 'cost', 'subtotal', 'tax_amount', 'discount_total', 'line_total', 'shipping_charge']
262 );
263
264 $filteredData['fulfilled_quantity'] = $fulfillment_type === 'physical'
265 ? Arr::get($item, 'fulfilled_quantity', 0)
266 : Arr::get($item, 'quantity', 0);
267
268 $existingItems[] = $filteredData;
269 }
270 }
271
272 // if any item is adjustment, then only add the adjustment item
273 $adjustmentItem = array_filter($newItems, function ($item) {
274 return Arr::get($item, 'payment_type') === 'adjustment';
275 });
276
277 if (count($adjustmentItem) > 0) {
278 $newItems = $adjustmentItem[0];
279 }
280
281 if (count($newItems) > 0) {
282 OrderItemResource::create($newItems, $order);
283 }
284
285 if (count($existingItems) > 0) {
286 OrderItemResource::update($existingItems, $order);
287 }
288
289 return static::makeSuccessResponse(
290 '',
291 __('Order items updated.', 'fluent-cart')
292 );
293 }
294
295 /**
296 * Retrieve the top-selling products based on the total quantity sold.
297 *
298 * @param array $params Optional. Additional parameters for retrieving top-selling products.
299 * [
300 * 'created_at' => (array) Optional. Filter results by creation date.
301 * [ "created_at" => [
302 * "column" => "created_at",
303 * "operator" => "between",
304 * "value" => "Date range e.g. start_date,end_date ]
305 * ],
306 * 'operator' => (string) Optional. Operator for the 'total_sold' condition.
307 * (e.g., '>', '=', '<', etc.)
308 * 'total_sold' => (int) Optional. Total quantity sold condition.
309 * ]
310 *
311 */
312 public static function topProductsSold($params = [])
313 {
314 return OrderItem::search(Arr::only($params, ['created_at']))
315 ->select('post_id')
316 ->selectRaw('SUM(quantity) as total_sold')
317 ->groupBy('post_id')
318 ->orderBy(sanitize_sql_orderby('total_sold'), sanitize_sql_orderby('DESC'))
319 ->having('total_sold', Arr::get($params, 'operator'), Arr::get($params, 'total_sold'))
320 // ->with([
321 // 'product' => function(Builder $query){
322 // return $query->select('ID','post_title');
323 // }
324 // ])
325 ->withWhereHas('product', function ($query) {
326 $query->select('ID', 'post_title');
327 })
328 ->lazy()->forPage(1, 5);
329 }
330
331 public static function bulkDeleteByOrderIds($ids, $params = [])
332 {
333 return static::getQuery()->whereIn('order_id', $ids)->delete();
334 }
335
336 }
337