1: <?php
2: namespace Opencart\Catalog\Model\Account;
3: /**
4: * Class Order
5: *
6: * @package Opencart\Catalog\Model\Account
7: */
8: class Order extends \Opencart\System\Engine\Model {
9: /**
10: * Get Order
11: *
12: * @param int $order_id
13: *
14: * @return array<string, mixed>
15: */
16: public function getOrder(int $order_id): array {
17: $order_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE `order_id` = '" . (int)$order_id . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `customer_id` != '0' AND `order_status_id` > '0'");
18:
19: if ($order_query->num_rows) {
20: $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `country_id` = '" . (int)$order_query->row['payment_country_id'] . "'");
21:
22: if ($country_query->num_rows) {
23: $payment_iso_code_2 = $country_query->row['iso_code_2'];
24: $payment_iso_code_3 = $country_query->row['iso_code_3'];
25: } else {
26: $payment_iso_code_2 = '';
27: $payment_iso_code_3 = '';
28: }
29:
30: $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE `zone_id` = '" . (int)$order_query->row['payment_zone_id'] . "'");
31:
32: if ($zone_query->num_rows) {
33: $payment_zone_code = $zone_query->row['code'];
34: } else {
35: $payment_zone_code = '';
36: }
37:
38: $country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'");
39:
40: if ($country_query->num_rows) {
41: $shipping_iso_code_2 = $country_query->row['iso_code_2'];
42: $shipping_iso_code_3 = $country_query->row['iso_code_3'];
43: } else {
44: $shipping_iso_code_2 = '';
45: $shipping_iso_code_3 = '';
46: }
47:
48: $zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE `zone_id` = '" . (int)$order_query->row['shipping_zone_id'] . "'");
49:
50: if ($zone_query->num_rows) {
51: $shipping_zone_code = $zone_query->row['code'];
52: } else {
53: $shipping_zone_code = '';
54: }
55:
56: return [
57: 'order_id' => $order_query->row['order_id'],
58: 'invoice_no' => $order_query->row['invoice_no'],
59: 'invoice_prefix' => $order_query->row['invoice_prefix'],
60: 'store_id' => $order_query->row['store_id'],
61: 'store_name' => $order_query->row['store_name'],
62: 'store_url' => $order_query->row['store_url'],
63: 'customer_id' => $order_query->row['customer_id'],
64: 'firstname' => $order_query->row['firstname'],
65: 'lastname' => $order_query->row['lastname'],
66: 'telephone' => $order_query->row['telephone'],
67: 'email' => $order_query->row['email'],
68: 'payment_firstname' => $order_query->row['payment_firstname'],
69: 'payment_lastname' => $order_query->row['payment_lastname'],
70: 'payment_company' => $order_query->row['payment_company'],
71: 'payment_address_1' => $order_query->row['payment_address_1'],
72: 'payment_address_2' => $order_query->row['payment_address_2'],
73: 'payment_postcode' => $order_query->row['payment_postcode'],
74: 'payment_city' => $order_query->row['payment_city'],
75: 'payment_zone_id' => $order_query->row['payment_zone_id'],
76: 'payment_zone' => $order_query->row['payment_zone'],
77: 'payment_zone_code' => $payment_zone_code,
78: 'payment_country_id' => $order_query->row['payment_country_id'],
79: 'payment_country' => $order_query->row['payment_country'],
80: 'payment_iso_code_2' => $payment_iso_code_2,
81: 'payment_iso_code_3' => $payment_iso_code_3,
82: 'payment_address_format' => $order_query->row['payment_address_format'],
83: 'payment_method' => $order_query->row['payment_method'] ? json_decode($order_query->row['payment_method'], true) : '',
84: 'shipping_firstname' => $order_query->row['shipping_firstname'],
85: 'shipping_lastname' => $order_query->row['shipping_lastname'],
86: 'shipping_company' => $order_query->row['shipping_company'],
87: 'shipping_address_1' => $order_query->row['shipping_address_1'],
88: 'shipping_address_2' => $order_query->row['shipping_address_2'],
89: 'shipping_postcode' => $order_query->row['shipping_postcode'],
90: 'shipping_city' => $order_query->row['shipping_city'],
91: 'shipping_zone_id' => $order_query->row['shipping_zone_id'],
92: 'shipping_zone' => $order_query->row['shipping_zone'],
93: 'shipping_zone_code' => $shipping_zone_code,
94: 'shipping_country_id' => $order_query->row['shipping_country_id'],
95: 'shipping_country' => $order_query->row['shipping_country'],
96: 'shipping_iso_code_2' => $shipping_iso_code_2,
97: 'shipping_iso_code_3' => $shipping_iso_code_3,
98: 'shipping_address_format' => $order_query->row['shipping_address_format'],
99: 'shipping_method' => $order_query->row['shipping_method'] ? json_decode($order_query->row['shipping_method'], true) : '',
100: 'comment' => $order_query->row['comment'],
101: 'total' => $order_query->row['total'],
102: 'order_status_id' => $order_query->row['order_status_id'],
103: 'language_id' => $order_query->row['language_id'],
104: 'currency_id' => $order_query->row['currency_id'],
105: 'currency_code' => $order_query->row['currency_code'],
106: 'currency_value' => $order_query->row['currency_value'],
107: 'date_modified' => $order_query->row['date_modified'],
108: 'date_added' => $order_query->row['date_added'],
109: 'ip' => $order_query->row['ip']
110: ];
111: } else {
112: return [];
113: }
114: }
115:
116: /**
117: * Get Orders
118: *
119: * @param int $start
120: * @param int $limit
121: *
122: * @return array<int, array<string, mixed>>
123: */
124: public function getOrders(int $start = 0, int $limit = 20): array {
125: if ($start < 0) {
126: $start = 0;
127: }
128:
129: if ($limit < 1) {
130: $limit = 1;
131: }
132:
133: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE `customer_id` = '" . (int)$this->customer->getId() . "' AND `order_status_id` > '0' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' ORDER BY `order_id` DESC LIMIT " . (int)$start . "," . (int)$limit);
134:
135: return $query->rows;
136: }
137:
138: /**
139: * Get Orders By Subscription ID
140: *
141: * @param int $subscription_id
142: * @param int $start
143: * @param int $limit
144: *
145: * @return array<int, array<string, mixed>>
146: */
147: public function getOrdersBySubscriptionId(int $subscription_id, int $start = 0, int $limit = 20): array {
148: if ($start < 0) {
149: $start = 0;
150: }
151:
152: if ($limit < 1) {
153: $limit = 1;
154: }
155:
156: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE `subscription_id` = '" . (int)$subscription_id . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `order_status_id` > '0' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' ORDER BY `order_id` DESC LIMIT " . (int)$start . "," . (int)$limit);
157:
158: return $query->rows;
159: }
160:
161: /**
162: * Get Product
163: *
164: * @param int $order_id
165: * @param int $order_product_id
166: *
167: * @return array<string, mixed>
168: */
169: public function getProduct(int $order_id, int $order_product_id): array {
170: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "'");
171:
172: return $query->row;
173: }
174:
175: /**
176: * Get Products
177: *
178: * @param int $order_id
179: *
180: * @return array<int, array<string, mixed>>
181: */
182: public function getProducts(int $order_id): array {
183: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");
184:
185: return $query->rows;
186: }
187:
188: /**
189: * Get Options
190: *
191: * @param int $order_id
192: * @param int $order_product_id
193: *
194: * @return array<int, array<string, mixed>>
195: */
196: public function getOptions(int $order_id, int $order_product_id): array {
197: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_option` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "'");
198:
199: return $query->rows;
200: }
201:
202: /**
203: * Get Subscription
204: *
205: * @param int $order_id
206: * @param int $order_product_id
207: *
208: * @return array<string, mixed>
209: */
210: public function getSubscription(int $order_id, int $order_product_id): array {
211: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_subscription` WHERE `order_id` = '" . (int)$order_id . "' AND `order_product_id` = '" . (int)$order_product_id . "'");
212:
213: return $query->row;
214: }
215:
216: /**
217: * Get Vouchers
218: *
219: * @param int $order_id
220: *
221: * @return array<int, array<string, mixed>>
222: */
223: public function getVouchers(int $order_id): array {
224: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_voucher` WHERE `order_id` = '" . (int)$order_id . "'");
225:
226: return $query->rows;
227: }
228:
229: /**
230: * Get Totals
231: *
232: * @param int $order_id
233: *
234: * @return array<int, array<string, mixed>>
235: */
236: public function getTotals(int $order_id): array {
237: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `sort_order`");
238:
239: return $query->rows;
240: }
241:
242: /**
243: * Get Histories
244: *
245: * @param int $order_id
246: *
247: * @return array<int, array<string, mixed>>
248: */
249: public function getHistories(int $order_id): array {
250: $query = $this->db->query("SELECT `date_added`, `os`.`name` AS `status`, `oh`.`comment`, `oh`.`notify` FROM `" . DB_PREFIX . "order_history` `oh` LEFT JOIN `" . DB_PREFIX . "order_status` `os` ON `oh`.`order_status_id` = `os`.`order_status_id` WHERE `oh`.`order_id` = '" . (int)$order_id . "' AND `os`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' ORDER BY `oh`.`date_added`");
251:
252: return $query->rows;
253: }
254:
255: /**
256: * Get Total Histories
257: *
258: * @param int $order_id
259: *
260: * @return int
261: */
262: public function getTotalHistories(int $order_id): int {
263: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order_history` WHERE `order_id` = '" . (int)$order_id . "'");
264:
265: if ($query->num_rows) {
266: return (int)$query->row['total'];
267: } else {
268: return 0;
269: }
270: }
271:
272: /**
273: * Get Total Orders
274: *
275: * @return int
276: */
277: public function getTotalOrders(): int {
278: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order` `o` WHERE `customer_id` = '" . (int)$this->customer->getId() . "' AND `o`.`order_status_id` > '0' AND `o`.`store_id` = '" . (int)$this->config->get('config_store_id') . "'");
279:
280: if ($query->num_rows) {
281: return (int)$query->row['total'];
282: } else {
283: return 0;
284: }
285: }
286:
287: /**
288: * Get Total Orders By Product ID
289: *
290: * @param int $product_id
291: *
292: * @return int
293: */
294: public function getTotalOrdersByProductId(int $product_id): int {
295: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order_product` `op` LEFT JOIN `" . DB_PREFIX . "order` `o` ON (`op`.`order_id` = `o`.`order_id`) WHERE `o`.`customer_id` = '" . (int)$this->customer->getId() . "' AND `op`.`product_id` = '" . (int)$product_id . "'");
296:
297: if ($query->num_rows) {
298: return (int)$query->row['total'];
299: } else {
300: return 0;
301: }
302: }
303:
304: /**
305: * Get Total Products By Order ID
306: *
307: * @param int $order_id
308: *
309: * @return int
310: */
311: public function getTotalProductsByOrderId(int $order_id): int {
312: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");
313:
314: if ($query->num_rows) {
315: return (int)$query->row['total'];
316: } else {
317: return 0;
318: }
319: }
320:
321: /**
322: * Get Total Vouchers By Order ID
323: *
324: * @param int $order_id
325: *
326: * @return int
327: */
328: public function getTotalVouchersByOrderId(int $order_id): int {
329: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order_voucher` WHERE `order_id` = '" . (int)$order_id . "'");
330:
331: if ($query->num_rows) {
332: return (int)$query->row['total'];
333: } else {
334: return 0;
335: }
336: }
337:
338: /**
339: * Get Total Orders By Subscription ID
340: *
341: * @param int $subscription_id
342: *
343: * @return int
344: */
345: public function getTotalOrdersBySubscriptionId(int $subscription_id): int {
346: $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() . "'");
347:
348: return (int)$query->row['total'];
349: }
350: }
351: