1: | <?php
|
2: | namespace Opencart\Catalog\Model\Marketing;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Coupon extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: |
|
16: | public function getCoupon(string $code): array {
|
17: | $status = true;
|
18: |
|
19: | $coupon_info = $this->model_marketing_coupon->getCouponByCode($code);
|
20: |
|
21: | if ($coupon_info && ($coupon_info['date_start'] == '0000-00-00' || strtotime($coupon_info['date_start']) < strtotime(date('Y-m-d H:i:s'))) && ($coupon_info['date_end'] == '0000-00-00' || strtotime($coupon_info['date_end']) > strtotime(date('Y-m-d H:i:s')))) {
|
22: | if ($coupon_info['total'] > $this->cart->getSubTotal()) {
|
23: | $status = false;
|
24: | }
|
25: |
|
26: | $coupon_total = $this->model_marketing_coupon->getTotalHistories($coupon_info['coupon_id']);
|
27: |
|
28: | if ($coupon_info['uses_total'] > 0 && ($coupon_total >= $coupon_info['uses_total'])) {
|
29: | $status = false;
|
30: | }
|
31: |
|
32: | if ($coupon_info['logged'] && !$this->customer->getId()) {
|
33: | $status = false;
|
34: | }
|
35: |
|
36: | if ($this->customer->getId()) {
|
37: | $customer_total = $this->model_marketing_coupon->getTotalHistoriesByCustomerId($coupon_info['coupon_id'], $this->customer->getId());
|
38: |
|
39: | if ($coupon_info['uses_customer'] > 0 && ($customer_total >= $coupon_info['uses_customer'])) {
|
40: | $status = false;
|
41: | }
|
42: | }
|
43: |
|
44: |
|
45: | $coupon_product_data = $this->getProducts($coupon_info['coupon_id']);
|
46: |
|
47: |
|
48: | $coupon_category_data = $this->getCategories($coupon_info['coupon_id']);
|
49: |
|
50: | $product_data = [];
|
51: |
|
52: | if ($coupon_product_data || $coupon_category_data) {
|
53: | $this->load->model('catalog/product');
|
54: |
|
55: | foreach ($this->cart->getProducts() as $product) {
|
56: | if (in_array($product['product_id'], $coupon_product_data)) {
|
57: | $product_data[] = $product['product_id'];
|
58: |
|
59: | continue;
|
60: | }
|
61: |
|
62: | foreach ($coupon_category_data as $category_id) {
|
63: | $product_total = $this->model_catalog_product->getTotalCategoriesByCategoryId($product['product_id'], $category_id);
|
64: |
|
65: | if ($product_total) {
|
66: | $product_data[] = $product['product_id'];
|
67: |
|
68: | continue;
|
69: | }
|
70: | }
|
71: | }
|
72: |
|
73: | if (!$product_data) {
|
74: | $status = false;
|
75: | }
|
76: | }
|
77: | } else {
|
78: | $status = false;
|
79: | }
|
80: |
|
81: | if ($status) {
|
82: | return [
|
83: | 'coupon_id' => $coupon_info['coupon_id'],
|
84: | 'code' => $coupon_info['code'],
|
85: | 'name' => $coupon_info['name'],
|
86: | 'type' => $coupon_info['type'],
|
87: | 'discount' => $coupon_info['discount'],
|
88: | 'shipping' => $coupon_info['shipping'],
|
89: | 'total' => $coupon_info['total'],
|
90: | 'product' => $product_data,
|
91: | 'date_start' => $coupon_info['date_start'],
|
92: | 'date_end' => $coupon_info['date_end'],
|
93: | 'uses_total' => $coupon_info['uses_total'],
|
94: | 'uses_customer' => $coupon_info['uses_customer'],
|
95: | 'status' => $coupon_info['status'],
|
96: | 'date_added' => $coupon_info['date_added']
|
97: | ];
|
98: | } else {
|
99: | return [];
|
100: | }
|
101: | }
|
102: |
|
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: |
|
110: | public function getCouponByCode(string $code): array {
|
111: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon` WHERE `code` = '" . $this->db->escape($code) . "' AND `status` = '1'");
|
112: |
|
113: | return $query->row;
|
114: | }
|
115: |
|
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: |
|
123: | public function getProducts(int $coupon_id): array {
|
124: | $product_data = [];
|
125: |
|
126: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_product` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
127: |
|
128: | foreach ($query->rows as $product) {
|
129: | $product_data[] = $product['product_id'];
|
130: | }
|
131: |
|
132: | return $product_data;
|
133: | }
|
134: |
|
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: |
|
142: | public function getCategories(int $coupon_id): array {
|
143: | $category_data = [];
|
144: |
|
145: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_category` `cc` LEFT JOIN `" . DB_PREFIX . "category_path` `cp` ON (`cc`.`category_id` = `cp`.`path_id`) WHERE `cc`.`coupon_id` = '" . (int)$coupon_id . "'");
|
146: |
|
147: | foreach ($query->rows as $category) {
|
148: | $category_data[] = $category['category_id'];
|
149: | }
|
150: |
|
151: | return $category_data;
|
152: | }
|
153: |
|
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: |
|
164: | public function addHistory(int $coupon_id, int $order_id, int $customer_id, float $amount = 0.00): void {
|
165: | $this->db->query("INSERT INTO `" . DB_PREFIX . "coupon_history` SET `coupon_id` = '" . (int)$coupon_id . "', `order_id` = '" . (int)$order_id . "', `customer_id` = '" . (int)$customer_id . "', `amount` = '" . (float)$amount . "', `date_added` = NOW()");
|
166: | }
|
167: |
|
168: | |
169: | |
170: | |
171: | |
172: | |
173: | |
174: |
|
175: | public function deleteHistoriesByOrderId(int $order_id): void {
|
176: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_history` WHERE `order_id` = '" . (int)$order_id . "'");
|
177: | }
|
178: |
|
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: |
|
186: | public function getTotalHistories(string $coupon_id): int {
|
187: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "coupon_history` `ch` LEFT JOIN `" . DB_PREFIX . "coupon` `c` ON (`ch`.`coupon_id` = `c`.`coupon_id`) WHERE `c`.`coupon_id` = '" . $this->db->escape($coupon_id) . "'");
|
188: |
|
189: | return (int)$query->row['total'];
|
190: | }
|
191: |
|
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: |
|
200: | public function getTotalHistoriesByCustomerId(int $coupon_id, int $customer_id): int {
|
201: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "coupon_history` `ch` LEFT JOIN `" . DB_PREFIX . "coupon` `c` ON (`ch`.`coupon_id` = `c`.`coupon_id`) WHERE `c`.`coupon_id` = '" . (int)$coupon_id . "' AND `ch`.`customer_id` = '" . (int)$customer_id . "'");
|
202: |
|
203: | return (int)$query->row['total'];
|
204: | }
|
205: | }
|
206: | |