1: | <?php
|
2: | namespace Opencart\Catalog\Model\Checkout;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Order extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: |
|
16: | public function addOrder(array $data): int {
|
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET `invoice_prefix` = '" . $this->db->escape($data['invoice_prefix']) . "', `store_id` = '" . (int)$data['store_id'] . "', `store_name` = '" . $this->db->escape($data['store_name']) . "', `store_url` = '" . $this->db->escape($data['store_url']) . "', `customer_id` = '" . (int)$data['customer_id'] . "', `customer_group_id` = '" . (int)$data['customer_group_id'] . "', `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `email` = '" . $this->db->escape($data['email']) . "', `telephone` = '" . $this->db->escape($data['telephone']) . "', `custom_field` = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', `payment_address_id` = '" . (int)$data['payment_address_id'] . "', `payment_firstname` = '" . $this->db->escape($data['payment_firstname']) . "', `payment_lastname` = '" . $this->db->escape($data['payment_lastname']) . "', `payment_company` = '" . $this->db->escape($data['payment_company']) . "', `payment_address_1` = '" . $this->db->escape($data['payment_address_1']) . "', `payment_address_2` = '" . $this->db->escape($data['payment_address_2']) . "', `payment_city` = '" . $this->db->escape($data['payment_city']) . "', `payment_postcode` = '" . $this->db->escape($data['payment_postcode']) . "', `payment_country` = '" . $this->db->escape($data['payment_country']) . "', `payment_country_id` = '" . (int)$data['payment_country_id'] . "', `payment_zone` = '" . $this->db->escape($data['payment_zone']) . "', `payment_zone_id` = '" . (int)$data['payment_zone_id'] . "', `payment_address_format` = '" . $this->db->escape($data['payment_address_format']) . "', `payment_custom_field` = '" . $this->db->escape(isset($data['payment_custom_field']) ? json_encode($data['payment_custom_field']) : '') . "', `payment_method` = '" . $this->db->escape($data['payment_method'] ? json_encode($data['payment_method']) : '') . "', `shipping_address_id` = '" . (int)$data['shipping_address_id'] . "', `shipping_firstname` = '" . $this->db->escape($data['shipping_firstname']) . "', `shipping_lastname` = '" . $this->db->escape($data['shipping_lastname']) . "', `shipping_company` = '" . $this->db->escape($data['shipping_company']) . "', `shipping_address_1` = '" . $this->db->escape($data['shipping_address_1']) . "', `shipping_address_2` = '" . $this->db->escape($data['shipping_address_2']) . "', `shipping_city` = '" . $this->db->escape($data['shipping_city']) . "', `shipping_postcode` = '" . $this->db->escape($data['shipping_postcode']) . "', `shipping_country` = '" . $this->db->escape($data['shipping_country']) . "', `shipping_country_id` = '" . (int)$data['shipping_country_id'] . "', `shipping_zone` = '" . $this->db->escape($data['shipping_zone']) . "', `shipping_zone_id` = '" . (int)$data['shipping_zone_id'] . "', `shipping_address_format` = '" . $this->db->escape($data['shipping_address_format']) . "', `shipping_custom_field` = '" . $this->db->escape(isset($data['shipping_custom_field']) ? json_encode($data['shipping_custom_field']) : '') . "', `shipping_method` = '" . $this->db->escape($data['shipping_method'] ? json_encode($data['shipping_method']) : '') . "', `comment` = '" . $this->db->escape($data['comment']) . "', `total` = '" . (float)$data['total'] . "', `affiliate_id` = '" . (int)$data['affiliate_id'] . "', `commission` = '" . (float)$data['commission'] . "', `marketing_id` = '" . (int)$data['marketing_id'] . "', `tracking` = '" . $this->db->escape($data['tracking']) . "', `language_id` = '" . (int)$data['language_id'] . "', `currency_id` = '" . (int)$data['currency_id'] . "', `currency_code` = '" . $this->db->escape($data['currency_code']) . "', `currency_value` = '" . (float)$data['currency_value'] . "', `ip` = '" . $this->db->escape((string)$data['ip']) . "', `forwarded_ip` = '" . $this->db->escape((string)$data['forwarded_ip']) . "', `user_agent` = '" . $this->db->escape((string)$data['user_agent']) . "', `accept_language` = '" . $this->db->escape((string)$data['accept_language']) . "', `date_added` = NOW(), `date_modified` = NOW()");
|
18: |
|
19: | $order_id = $this->db->getLastId();
|
20: |
|
21: |
|
22: | if (isset($data['products'])) {
|
23: | foreach ($data['products'] as $product) {
|
24: | $this->model_checkout_order->addProduct($order_id, $product);
|
25: | }
|
26: | }
|
27: |
|
28: |
|
29: | if (isset($data['vouchers'])) {
|
30: | foreach ($data['vouchers'] as $voucher) {
|
31: | $this->model_checkout_order->addVoucher($order_id, $voucher);
|
32: | }
|
33: | }
|
34: |
|
35: |
|
36: | if (isset($data['totals'])) {
|
37: | foreach ($data['totals'] as $total) {
|
38: | $this->model_checkout_order->addTotal($order_id, $total);
|
39: | }
|
40: | }
|
41: |
|
42: | return $order_id;
|
43: | }
|
44: |
|
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: |
|
53: | public function editOrder(int $order_id, array $data): void {
|
54: |
|
55: | $this->addHistory($order_id, 0);
|
56: |
|
57: | $order_info = $this->getOrder($order_id);
|
58: |
|
59: | if ($order_info) {
|
60: |
|
61: | foreach ($order_info as $key => $value) {
|
62: | if (!isset($data[$key])) {
|
63: | $data[$key] = $value;
|
64: | }
|
65: | }
|
66: |
|
67: | $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `invoice_prefix` = '" . $this->db->escape((string)$data['invoice_prefix']) . "', `store_id` = '" . (int)$data['store_id'] . "', `store_name` = '" . $this->db->escape((string)$data['store_name']) . "', `store_url` = '" . $this->db->escape((string)$data['store_url']) . "', `customer_id` = '" . (int)$data['customer_id'] . "', `customer_group_id` = '" . (int)$data['customer_group_id'] . "', `firstname` = '" . $this->db->escape((string)$data['firstname']) . "', `lastname` = '" . $this->db->escape((string)$data['lastname']) . "', `email` = '" . $this->db->escape((string)$data['email']) . "', `telephone` = '" . $this->db->escape((string)$data['telephone']) . "', `custom_field` = '" . $this->db->escape(json_encode($data['custom_field'])) . "', `payment_address_id` = '" . (int)$data['payment_address_id'] . "', `payment_firstname` = '" . $this->db->escape((string)$data['payment_firstname']) . "', `payment_lastname` = '" . $this->db->escape((string)$data['payment_lastname']) . "', `payment_company` = '" . $this->db->escape((string)$data['payment_company']) . "', `payment_address_1` = '" . $this->db->escape((string)$data['payment_address_1']) . "', `payment_address_2` = '" . $this->db->escape((string)$data['payment_address_2']) . "', `payment_city` = '" . $this->db->escape((string)$data['payment_city']) . "', `payment_postcode` = '" . $this->db->escape((string)$data['payment_postcode']) . "', `payment_country` = '" . $this->db->escape((string)$data['payment_country']) . "', `payment_country_id` = '" . (int)$data['payment_country_id'] . "', `payment_zone` = '" . $this->db->escape((string)$data['payment_zone']) . "', `payment_zone_id` = '" . (int)$data['payment_zone_id'] . "', `payment_address_format` = '" . $this->db->escape((string)$data['payment_address_format']) . "', `payment_custom_field` = '" . $this->db->escape(isset($data['payment_custom_field']) ? json_encode($data['payment_custom_field']) : '') . "', `payment_method` = '" . $this->db->escape($data['payment_method'] ? json_encode($data['payment_method']) : '') . "', `shipping_address_id` = '" . (int)$data['shipping_address_id'] . "', `shipping_firstname` = '" . $this->db->escape((string)$data['shipping_firstname']) . "', `shipping_lastname` = '" . $this->db->escape((string)$data['shipping_lastname']) . "', `shipping_company` = '" . $this->db->escape((string)$data['shipping_company']) . "', `shipping_address_1` = '" . $this->db->escape((string)$data['shipping_address_1']) . "', `shipping_address_2` = '" . $this->db->escape((string)$data['shipping_address_2']) . "', `shipping_city` = '" . $this->db->escape((string)$data['shipping_city']) . "', `shipping_postcode` = '" . $this->db->escape((string)$data['shipping_postcode']) . "', `shipping_country` = '" . $this->db->escape((string)$data['shipping_country']) . "', `shipping_country_id` = '" . (int)$data['shipping_country_id'] . "', `shipping_zone` = '" . $this->db->escape((string)$data['shipping_zone']) . "', `shipping_zone_id` = '" . (int)$data['shipping_zone_id'] . "', `shipping_address_format` = '" . $this->db->escape((string)$data['shipping_address_format']) . "', `shipping_custom_field` = '" . $this->db->escape(isset($data['shipping_custom_field']) ? json_encode($data['shipping_custom_field']) : '') . "', `shipping_method` = '" . $this->db->escape($data['shipping_method'] ? json_encode($data['shipping_method']) : '') . "', `comment` = '" . $this->db->escape((string)$data['comment']) . "', `total` = '" . (float)$data['total'] . "', `affiliate_id` = '" . (int)$data['affiliate_id'] . "', `commission` = '" . (float)$data['commission'] . "', `date_modified` = NOW() WHERE `order_id` = '" . (int)$order_id . "'");
|
68: |
|
69: |
|
70: | $this->model_checkout_order->deleteProducts($order_id);
|
71: |
|
72: | if (isset($data['products'])) {
|
73: | foreach ($data['products'] as $product) {
|
74: | $this->model_checkout_order->addProduct($order_id, $product);
|
75: | }
|
76: | }
|
77: |
|
78: |
|
79: | $this->model_checkout_order->deleteVouchers($order_id);
|
80: |
|
81: | if (isset($data['vouchers'])) {
|
82: | foreach ($data['vouchers'] as $voucher) {
|
83: | $this->model_checkout_order->addVoucher($order_id, $voucher);
|
84: | }
|
85: | }
|
86: |
|
87: |
|
88: | $this->model_checkout_order->deleteTotals($order_id);
|
89: |
|
90: | if (isset($data['totals'])) {
|
91: | foreach ($data['totals'] as $total) {
|
92: | $this->model_checkout_order->addTotal($order_id, $total);
|
93: | }
|
94: | }
|
95: | }
|
96: | }
|
97: |
|
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: |
|
106: | public function editTransactionId(int $order_id, string $transaction_id): void {
|
107: | $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `transaction_id` = '" . $this->db->escape($transaction_id) . "' WHERE `order_id` = '" . (int)$order_id . "'");
|
108: | }
|
109: |
|
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: |
|
118: | public function editOrderStatusId(int $order_id, int $order_status_id): void {
|
119: | $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `order_status_id` = '" . (int)$order_status_id . "' WHERE `order_id` = '" . (int)$order_id . "'");
|
120: | }
|
121: |
|
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: |
|
130: | public function editComment(int $order_id, string $comment): void {
|
131: | $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `comment` = '" . $this->db->escape($comment) . "' WHERE `order_id` = '" . (int)$order_id . "'");
|
132: | }
|
133: |
|
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: |
|
141: | public function deleteOrder(int $order_id): void {
|
142: |
|
143: | $this->model_checkout_order->addHistory($order_id, 0);
|
144: |
|
145: | $this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "'");
|
146: |
|
147: | $this->model_checkout_order->deleteProducts($order_id);
|
148: | $this->model_checkout_order->deleteVouchers($order_id);
|
149: | $this->model_checkout_order->deleteTotals($order_id);
|
150: | $this->model_checkout_order->deleteHistories($order_id);
|
151: |
|
152: | $this->load->model('account/transaction');
|
153: |
|
154: | $this->model_account_transaction->deleteTransactionsByOrderId($order_id);
|
155: |
|
156: | $this->load->model('account/reward');
|
157: |
|
158: | $this->model_account_reward->deleteRewardsByOrderId($order_id);
|
159: |
|
160: |
|
161: | $this->load->model('checkout/voucher');
|
162: |
|
163: | $this->model_checkout_voucher->deleteVouchersByOrderId($order_id);
|
164: | }
|
165: |
|
166: | |
167: | |
168: | |
169: | |
170: | |
171: | |
172: |
|
173: | public function getOrder(int $order_id): array {
|
174: | $order_query = $this->db->query("SELECT *, (SELECT `os`.`name` FROM `" . DB_PREFIX . "order_status` `os` WHERE `os`.`order_status_id` = `o`.`order_status_id` AND `os`.`language_id` = `o`.`language_id`) AS order_status FROM `" . DB_PREFIX . "order` `o` WHERE `o`.`order_id` = '" . (int)$order_id . "'");
|
175: |
|
176: | if ($order_query->num_rows) {
|
177: | $order_data = $order_query->row;
|
178: |
|
179: | $this->load->model('localisation/country');
|
180: | $this->load->model('localisation/zone');
|
181: |
|
182: | $order_data['custom_field'] = json_decode($order_query->row['custom_field'], true);
|
183: |
|
184: | foreach (['payment', 'shipping'] as $column) {
|
185: | $country_info = $this->model_localisation_country->getCountry($order_query->row[$column . '_country_id']);
|
186: |
|
187: | if ($country_info) {
|
188: | $order_data[$column . '_iso_code_2'] = $country_info['iso_code_2'];
|
189: | $order_data[$column . '_iso_code_3'] = $country_info['iso_code_3'];
|
190: | } else {
|
191: | $order_data[$column . '_iso_code_2'] = '';
|
192: | $order_data[$column . '_iso_code_3'] = '';
|
193: | }
|
194: |
|
195: | $zone_info = $this->model_localisation_zone->getZone($order_query->row[$column . '_zone_id']);
|
196: |
|
197: | if ($zone_info) {
|
198: | $order_data[$column . '_zone_code'] = $zone_info['code'];
|
199: | } else {
|
200: | $order_data[$column . '_zone_code'] = '';
|
201: | }
|
202: |
|
203: | $order_data[$column . '_custom_field'] = json_decode($order_query->row[$column . '_custom_field'], true);
|
204: |
|
205: | $order_data[$column . '_custom_field'] = json_decode($order_query->row[$column . '_custom_field'], true);
|
206: |
|
207: |
|
208: | $order_data[$column . '_method'] = json_decode($order_query->row[$column . '_method'], true);
|
209: | }
|
210: |
|
211: | return $order_data;
|
212: | }
|
213: |
|
214: | return [];
|
215: | }
|
216: |
|
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: |
|
225: | public function addProduct(int $order_id, array $data): int {
|
226: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order_product` SET `order_id` = '" . (int)$order_id . "', `product_id` = '" . (int)$data['product_id'] . "', `master_id` = '" . (int)$data['master_id'] . "', `name` = '" . $this->db->escape($data['name']) . "', `model` = '" . $this->db->escape($data['model']) . "', `quantity` = '" . (int)$data['quantity'] . "', `price` = '" . (float)$data['price'] . "', `total` = '" . (float)$data['total'] . "', `tax` = '" . (float)$data['tax'] . "', `reward` = '" . (int)$data['reward'] . "'");
|
227: |
|
228: | $order_product_id = $this->db->getLastId();
|
229: |
|
230: | foreach ($data['option'] as $option) {
|
231: | $this->model_checkout_order->addOption($order_id, $order_product_id, $option);
|
232: | }
|
233: |
|
234: |
|
235: | if ($data['subscription']) {
|
236: | $this->model_checkout_order->addSubscription($order_id, $order_product_id, $data['subscription']);
|
237: | }
|
238: |
|
239: | return $this->db->getLastId();
|
240: | }
|
241: |
|
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | |
249: |
|
250: | public function deleteProducts(int $order_id, int $order_product_id = 0): void {
|
251: | $sql = "DELETE FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'";
|
252: |
|
253: | if ($order_product_id) {
|
254: | $sql .= " AND `order_product_id` = '" . (int)$order_product_id . "'";
|
255: | }
|
256: |
|
257: | $this->db->query($sql);
|
258: |
|
259: | $this->deleteOptions($order_id, $order_product_id);
|
260: | $this->deleteSubscription($order_id, $order_product_id);
|
261: | }
|
262: |
|
263: | |
264: | |
265: | |
266: | |
267: | |
268: | |
269: | |
270: |
|
271: | public function getProduct(int $order_id, int $order_product_id): array {
|
272: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "'");
|
273: |
|
274: | return $query->rows;
|
275: | }
|
276: |
|
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: |
|
284: | public function getProducts(int $order_id): array {
|
285: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");
|
286: |
|
287: | return $query->rows;
|
288: | }
|
289: |
|
290: | |
291: | |
292: | |
293: | |
294: | |
295: | |
296: | |
297: | |
298: |
|
299: | public function addOption(int $order_id, int $order_product_id, array $data): void {
|
300: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order_option` SET `order_id` = '" . (int)$order_id . "', `order_product_id` = '" . (int)$order_product_id . "', `product_option_id` = '" . (int)$data['product_option_id'] . "', `product_option_value_id` = '" . (int)$data['product_option_value_id'] . "', `name` = '" . $this->db->escape($data['name']) . "', `value` = '" . $this->db->escape($data['value']) . "', `type` = '" . $this->db->escape($data['type']) . "'");
|
301: | }
|
302: |
|
303: | |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: |
|
311: | public function deleteOptions(int $order_id, int $order_product_id = 0): void {
|
312: | $sql = "DELETE FROM `" . DB_PREFIX . "order_option` WHERE `order_id` = '" . (int)$order_id . "'";
|
313: |
|
314: | if ($order_product_id) {
|
315: | $sql .= " AND `order_product_id` = '" . (int)$order_product_id . "'";
|
316: | }
|
317: |
|
318: | $this->db->query($sql);
|
319: | }
|
320: |
|
321: | |
322: | |
323: | |
324: | |
325: | |
326: | |
327: | |
328: |
|
329: | public function getOptions(int $order_id, int $order_product_id): array {
|
330: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_option` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "'");
|
331: |
|
332: | return $query->rows;
|
333: | }
|
334: |
|
335: | |
336: | |
337: | |
338: | |
339: | |
340: | |
341: | |
342: | |
343: |
|
344: | public function addSubscription(int $order_id, int $order_product_id, array $data): void {
|
345: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order_subscription` SET `order_id` = '" . (int)$order_id . "', `order_product_id` = '" . (int)$order_product_id . "', `subscription_plan_id` = '" . (int)$data['subscription_plan_id'] . "', `trial_price` = '" . (float)$data['trial_price'] . "', `trial_tax` = '" . (float)$data['trial_tax'] . "', `trial_frequency` = '" . $this->db->escape($data['trial_frequency']) . "', `trial_cycle` = '" . (int)$data['trial_cycle'] . "', `trial_duration` = '" . (int)$data['trial_duration'] . "', `trial_remaining` = '" . (int)$data['trial_remaining'] . "', `trial_status` = '" . (int)$data['trial_status'] . "', `price` = '" . (float)$data['price'] . "', `tax` = '" . (float)$data['tax'] . "', `frequency` = '" . $this->db->escape($data['frequency']) . "', `cycle` = '" . (int)$data['cycle'] . "', `duration` = '" . (int)$data['duration'] . "'");
|
346: | }
|
347: |
|
348: | |
349: | |
350: | |
351: | |
352: | |
353: | |
354: | |
355: |
|
356: | public function deleteSubscription(int $order_id, int $order_product_id = 0): void {
|
357: | $sql = "DELETE FROM `" . DB_PREFIX . "order_subscription` WHERE `order_id` = '" . (int)$order_id . "'";
|
358: |
|
359: | if ($order_product_id) {
|
360: | $sql .= " AND `order_product_id` = '" . (int)$order_product_id . "'";
|
361: | }
|
362: |
|
363: | $this->db->query($sql);
|
364: | }
|
365: |
|
366: | |
367: | |
368: | |
369: | |
370: | |
371: | |
372: | |
373: |
|
374: | public function getSubscription(int $order_id, int $order_product_id): array {
|
375: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_subscription` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "'");
|
376: |
|
377: | return $query->row;
|
378: | }
|
379: |
|
380: | |
381: | |
382: | |
383: | |
384: | |
385: | |
386: |
|
387: | public function getSubscriptions(array $data): array {
|
388: | $sql = "SELECT * FROM `" . DB_PREFIX . "subscription`";
|
389: |
|
390: | $implode = [];
|
391: |
|
392: | if (!empty($data['filter_date_next'])) {
|
393: | $implode[] = "DATE(`date_next`) <= DATE('" . $this->db->escape($data['filter_date_next']) . "')";
|
394: | }
|
395: |
|
396: | if (!empty($data['filter_subscription_status_id'])) {
|
397: | $implode[] = "`subscription_status_id` = '" . (int)$data['filter_subscription_status_id'] . "'";
|
398: | }
|
399: |
|
400: | if ($implode) {
|
401: | $sql .= " WHERE " . implode(" AND ", $implode);
|
402: | }
|
403: |
|
404: | $sort_data = [
|
405: | 'pd.name',
|
406: | 'p.model',
|
407: | 'p.price',
|
408: | 'p.quantity',
|
409: | 'p.status',
|
410: | 'p.sort_order'
|
411: | ];
|
412: |
|
413: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
414: | $sql .= " ORDER BY " . $data['sort'];
|
415: | } else {
|
416: | $sql .= " ORDER BY `o`.`order_id`";
|
417: | }
|
418: |
|
419: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
420: | $sql .= " DESC";
|
421: | } else {
|
422: | $sql .= " ASC";
|
423: | }
|
424: |
|
425: | if (isset($data['start']) || isset($data['limit'])) {
|
426: | if ($data['start'] < 0) {
|
427: | $data['start'] = 0;
|
428: | }
|
429: |
|
430: | if ($data['limit'] < 1) {
|
431: | $data['limit'] = 20;
|
432: | }
|
433: |
|
434: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
435: | }
|
436: |
|
437: | $query = $this->db->query($sql);
|
438: |
|
439: | return $query->rows;
|
440: | }
|
441: |
|
442: | |
443: | |
444: | |
445: | |
446: | |
447: | |
448: |
|
449: | public function getTotalOrdersBySubscriptionId(int $subscription_id): int {
|
450: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order` WHERE `subscription_id` = '" . (int)$subscription_id . "' AND `customer_id` = '" . (int)$this->customer->getId() . "'");
|
451: |
|
452: | return (int)$query->row['total'];
|
453: | }
|
454: |
|
455: | |
456: | |
457: | |
458: | |
459: | |
460: | |
461: | |
462: |
|
463: | public function addVoucher(int $order_id, array $data): int {
|
464: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order_voucher` SET `order_id` = '" . (int)$order_id . "', `description` = '" . $this->db->escape($data['description']) . "', `code` = '" . $this->db->escape($data['code']) . "', `from_name` = '" . $this->db->escape($data['from_name']) . "', `from_email` = '" . $this->db->escape($data['from_email']) . "', `to_name` = '" . $this->db->escape($data['to_name']) . "', `to_email` = '" . $this->db->escape($data['to_email']) . "', `voucher_theme_id` = '" . (int)$data['voucher_theme_id'] . "', `message` = '" . $this->db->escape($data['message']) . "', `amount` = '" . (float)$data['amount'] . "'");
|
465: |
|
466: | $order_voucher_id = $this->db->getLastId();
|
467: |
|
468: | $this->load->model('checkout/voucher');
|
469: |
|
470: | $voucher_id = $this->model_checkout_voucher->addVoucher($order_id, $data);
|
471: |
|
472: | $this->db->query("UPDATE `" . DB_PREFIX . "order_voucher` SET `voucher_id` = '" . (int)$voucher_id . "' WHERE `order_voucher_id` = '" . (int)$order_voucher_id . "'");
|
473: |
|
474: | return $order_voucher_id;
|
475: | }
|
476: |
|
477: | |
478: | |
479: | |
480: | |
481: | |
482: | |
483: | |
484: |
|
485: | public function deleteVouchers(int $order_id, int $order_voucher_id = 0): void {
|
486: | $sql = "DELETE FROM `" . DB_PREFIX . "order_voucher` WHERE `order_id` = '" . (int)$order_id . "'";
|
487: |
|
488: | if ($order_voucher_id) {
|
489: | $sql .= " AND `order_voucher_id` = '" . (int)$order_voucher_id . "'";
|
490: | }
|
491: |
|
492: | $this->db->query($sql);
|
493: | }
|
494: |
|
495: | |
496: | |
497: | |
498: | |
499: | |
500: | |
501: | |
502: |
|
503: | public function getVoucherByVoucherId(int $order_id, int $voucher_id): array {
|
504: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_voucher` WHERE `order_id` = '" . (int)$order_id . "' AND `voucher_id` = '" . (int)$voucher_id . "'");
|
505: |
|
506: | return $query->rows;
|
507: | }
|
508: |
|
509: | |
510: | |
511: | |
512: | |
513: | |
514: | |
515: |
|
516: | public function getVouchers(int $order_id): array {
|
517: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_voucher` WHERE `order_id` = '" . (int)$order_id . "'");
|
518: |
|
519: | return $query->rows;
|
520: | }
|
521: |
|
522: | |
523: | |
524: | |
525: | |
526: | |
527: | |
528: | |
529: |
|
530: | public function addTotal(int $order_id, array $data): void {
|
531: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order_total` SET `order_id` = '" . (int)$order_id . "', `extension` = '" . $this->db->escape($data['extension']) . "', `code` = '" . $this->db->escape($data['code']) . "', `title` = '" . $this->db->escape($data['title']) . "', `value` = '" . (float)$data['value'] . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
532: | }
|
533: |
|
534: | |
535: | |
536: | |
537: | |
538: |
|
539: | public function deleteTotals(int $order_id): void {
|
540: | $this->db->query("DELETE FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = '" . (int)$order_id . "'");
|
541: | }
|
542: |
|
543: | |
544: | |
545: | |
546: | |
547: | |
548: | |
549: |
|
550: | public function getTotals(int $order_id): array {
|
551: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `sort_order` ASC");
|
552: |
|
553: | return $query->rows;
|
554: | }
|
555: |
|
556: | |
557: | |
558: | |
559: | |
560: | |
561: | |
562: | |
563: | |
564: | |
565: | |
566: |
|
567: | public function addHistory(int $order_id, int $order_status_id, string $comment = '', bool $notify = false, bool $override = false): void {
|
568: | $order_info = $this->model_checkout_order->getOrder($order_id);
|
569: |
|
570: | if ($order_info) {
|
571: |
|
572: | $this->load->model('account/customer');
|
573: |
|
574: | $customer_info = $this->model_account_customer->getCustomer($order_info['customer_id']);
|
575: |
|
576: |
|
577: | if ($customer_info && $customer_info['safe']) {
|
578: | $safe = true;
|
579: | } else {
|
580: | $safe = false;
|
581: | }
|
582: |
|
583: |
|
584: | if (!$safe && !$override && in_array($order_status_id, (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status'))) {
|
585: |
|
586: | $this->load->model('setting/extension');
|
587: |
|
588: | $extensions = $this->model_setting_extension->getExtensionsByType('fraud');
|
589: |
|
590: | foreach ($extensions as $extension) {
|
591: | if ($this->config->get('fraud_' . $extension['code'] . '_status')) {
|
592: | $this->load->model('extension/' . $extension['extension'] . '/fraud/' . $extension['code']);
|
593: |
|
594: | $model_extension_fraud = ($this->{'model_extension_' . $extension['extension'] . '_fraud_' . $extension['code']}) ?? null;
|
595: |
|
596: | if ($model_extension_fraud && isset($model_extension_fraud->check)) {
|
597: | $fraud_status_id = $model_extension_fraud->check($order_info);
|
598: |
|
599: | if ($fraud_status_id) {
|
600: | $order_status_id = $fraud_status_id;
|
601: | }
|
602: | }
|
603: | }
|
604: | }
|
605: | }
|
606: |
|
607: |
|
608: | $order_products = $this->model_checkout_order->getProducts($order_id);
|
609: |
|
610: |
|
611: | $order_totals = $this->model_checkout_order->getTotals($order_id);
|
612: |
|
613: |
|
614: | if (!in_array($order_info['order_status_id'], (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status')) && in_array($order_status_id, (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status'))) {
|
615: |
|
616: | foreach ($order_totals as $order_total) {
|
617: | $this->load->model('extension/' . $order_total['extension'] . '/total/' . $order_total['code']);
|
618: |
|
619: | $model_extension_total = $this->{'model_extension_' . $order_total['extension'] . '_total_' . $order_total['code']} ?? null;
|
620: |
|
621: | if ($model_extension_total && isset($model_extension_total->confirm)) {
|
622: |
|
623: | $fraud_status_id = $model_extension_total->confirm($order_info, $order_total);
|
624: |
|
625: |
|
626: | if ($fraud_status_id) {
|
627: | $order_status_id = $fraud_status_id;
|
628: | }
|
629: | }
|
630: | }
|
631: |
|
632: | foreach ($order_products as $order_product) {
|
633: |
|
634: | $this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = (`quantity` - " . (int)$order_product['quantity'] . ") WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `subtract` = '1'");
|
635: |
|
636: |
|
637: | if ($order_product['master_id']) {
|
638: | $this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = (`quantity` - " . (int)$order_product['quantity'] . ") WHERE `product_id` = '" . (int)$order_product['master_id'] . "' AND `subtract` = '1'");
|
639: | }
|
640: |
|
641: | $order_options = $this->getOptions($order_id, $order_product['order_product_id']);
|
642: |
|
643: | foreach ($order_options as $order_option) {
|
644: | $this->db->query("UPDATE `" . DB_PREFIX . "product_option_value` SET `quantity` = (`quantity` - " . (int)$order_product['quantity'] . ") WHERE `product_option_value_id` = '" . (int)$order_option['product_option_value_id'] . "' AND `subtract` = '1'");
|
645: | }
|
646: | }
|
647: | }
|
648: |
|
649: |
|
650: | if (!in_array($order_info['order_status_id'], (array)$this->config->get('config_complete_status')) && in_array($order_status_id, (array)$this->config->get('config_complete_status'))) {
|
651: |
|
652: | if ($order_info['affiliate_id'] && $this->config->get('config_affiliate_auto')) {
|
653: |
|
654: | $this->load->model('account/customer');
|
655: |
|
656: | if (!$this->model_account_customer->getTotalTransactionsByOrderId($order_id)) {
|
657: | $this->model_account_customer->addTransaction($order_info['affiliate_id'], $this->language->get('text_order_id') . ' #' . $order_id, $order_info['commission'], $order_id);
|
658: | }
|
659: | }
|
660: |
|
661: |
|
662: | $this->load->model('checkout/subscription');
|
663: |
|
664: | foreach ($order_products as $order_product) {
|
665: |
|
666: | $order_subscription_info = $this->model_checkout_order->getSubscription($order_id, $order_product['order_product_id']);
|
667: |
|
668: | if ($order_subscription_info) {
|
669: |
|
670: | $option_data = [];
|
671: |
|
672: | $options = $this->getOptions($order_id, $order_product['order_product_id']);
|
673: |
|
674: | foreach ($options as $option) {
|
675: | if ($option['type'] == 'text' || $option['type'] == 'textarea' || $option['type'] == 'file' || $option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
|
676: | $option_data[$option['product_option_id']] = $option['value'];
|
677: | } elseif ($option['type'] == 'select' || $option['type'] == 'radio') {
|
678: | $option_data[$option['product_option_id']] = $option['product_option_value_id'];
|
679: | } elseif ($option['type'] == 'checkbox') {
|
680: | $option_data[$option['product_option_id']][] = $option['product_option_value_id'];
|
681: | }
|
682: | }
|
683: |
|
684: |
|
685: | $subscription_info = $this->model_checkout_subscription->getSubscriptionByOrderProductId($order_id, $order_product['order_product_id']);
|
686: |
|
687: | if ($subscription_info) {
|
688: | $subscription_id = $subscription_info['subscription_id'];
|
689: | } else {
|
690: | $subscription_id = $this->model_checkout_subscription->addSubscription($order_subscription_info + $order_product + $order_info + ['option' => $option_data]);
|
691: | }
|
692: |
|
693: |
|
694: | $this->model_checkout_subscription->addHistory($subscription_id, (int)$this->config->get('config_subscription_active_id'));
|
695: | }
|
696: | }
|
697: | }
|
698: |
|
699: |
|
700: | if (in_array($order_info['order_status_id'], (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status')) && !in_array($order_status_id, (array)$this->config->get('config_processing_status') + (array)$this->config->get('config_complete_status'))) {
|
701: |
|
702: | foreach ($order_products as $order_product) {
|
703: | $this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = (`quantity` + " . (int)$order_product['quantity'] . ") WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `subtract` = '1'");
|
704: |
|
705: |
|
706: | if ($order_product['master_id']) {
|
707: | $this->db->query("UPDATE `" . DB_PREFIX . "product` SET `quantity` = (`quantity` + " . (int)$order_product['quantity'] . ") WHERE `product_id` = '" . (int)$order_product['master_id'] . "' AND `subtract` = '1'");
|
708: | }
|
709: |
|
710: | $order_options = $this->model_checkout_order->getOptions($order_id, $order_product['order_product_id']);
|
711: |
|
712: | foreach ($order_options as $order_option) {
|
713: | $this->db->query("UPDATE `" . DB_PREFIX . "product_option_value` SET `quantity` = (`quantity` + " . (int)$order_product['quantity'] . ") WHERE `product_option_value_id` = '" . (int)$order_option['product_option_value_id'] . "' AND `subtract` = '1'");
|
714: | }
|
715: | }
|
716: |
|
717: |
|
718: | foreach ($order_totals as $order_total) {
|
719: | $this->load->model('extension/' . $order_total['extension'] . '/total/' . $order_total['code']);
|
720: |
|
721: | $model_extension_total = $this->{'model_extension_' . $order_total['extension'] . '_total_' . $order_total['code']} ?? null;
|
722: |
|
723: | if ($model_extension_total && isset($model_extension_total->unconfirm)) {
|
724: | $model_extension_total->unconfirm($order_info);
|
725: | }
|
726: | }
|
727: | }
|
728: |
|
729: |
|
730: | if (in_array($order_info['order_status_id'], (array)$this->config->get('config_complete_status')) && !in_array($order_status_id, (array)$this->config->get('config_complete_status'))) {
|
731: |
|
732: | $this->load->model('checkout/subscription');
|
733: |
|
734: | foreach ($order_products as $order_product) {
|
735: |
|
736: | $subscription_info = $this->model_checkout_subscription->getSubscriptionByOrderProductId($order_id, $order_product['order_product_id']);
|
737: |
|
738: | if ($subscription_info) {
|
739: |
|
740: | $this->model_checkout_subscription->addHistory($subscription_info['subscription_id'], (int)$this->config->get('config_subscription_suspended_status_id'));
|
741: | }
|
742: | }
|
743: |
|
744: |
|
745: | if ($order_info['affiliate_id']) {
|
746: | $this->load->model('account/transaction');
|
747: |
|
748: | $this->model_account_transaction->deleteTransaction($order_info['customer_id'], $order_id);
|
749: | }
|
750: | }
|
751: |
|
752: |
|
753: | $this->model_checkout_order->editOrderStatusId($order_id, $order_status_id);
|
754: |
|
755: | $this->db->query("INSERT INTO `" . DB_PREFIX . "order_history` SET `order_id` = '" . (int)$order_id . "', `order_status_id` = '" . (int)$order_status_id . "', `notify` = '" . (int)$notify . "', `comment` = '" . $this->db->escape($comment) . "', `date_added` = NOW()");
|
756: |
|
757: | $this->cache->delete('product');
|
758: | }
|
759: | }
|
760: |
|
761: | |
762: | |
763: | |
764: | |
765: | |
766: | |
767: |
|
768: | public function deleteHistory(int $order_id): void {
|
769: | $this->db->query("DELETE FROM `" . DB_PREFIX . "order_history` WHERE `order_id` = '" . (int)$order_id . "'");
|
770: | }
|
771: | }
|
772: | |