1: | <?php
|
2: | namespace Opencart\Admin\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 addCoupon(array $data): int {
|
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "coupon` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `discount` = '" . (float)$data['discount'] . "', `type` = '" . $this->db->escape((string)$data['type']) . "', `total` = '" . (float)$data['total'] . "', `logged` = '" . (isset($data['logged']) ? (bool)$data['logged'] : 0) . "', `shipping` = '" . (isset($data['shipping']) ? (bool)$data['shipping'] : 0) . "', `date_start` = '" . $this->db->escape((string)$data['date_start']) . "', `date_end` = '" . $this->db->escape((string)$data['date_end']) . "', `uses_total` = '" . (int)$data['uses_total'] . "', `uses_customer` = '" . (int)$data['uses_customer'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `date_added` = NOW()");
|
18: |
|
19: | $coupon_id = $this->db->getLastId();
|
20: |
|
21: | if (isset($data['coupon_product'])) {
|
22: | foreach ($data['coupon_product'] as $product_id) {
|
23: | $this->addProduct($coupon_id, $product_id);
|
24: | }
|
25: | }
|
26: |
|
27: | if (isset($data['coupon_category'])) {
|
28: | foreach ($data['coupon_category'] as $category_id) {
|
29: | $this->addCategory($coupon_id, $category_id);
|
30: | }
|
31: | }
|
32: |
|
33: | return $coupon_id;
|
34: | }
|
35: |
|
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: |
|
44: | public function editCoupon(int $coupon_id, array $data): void {
|
45: | $this->db->query("UPDATE `" . DB_PREFIX . "coupon` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `discount` = '" . (float)$data['discount'] . "', `type` = '" . $this->db->escape((string)$data['type']) . "', `total` = '" . (float)$data['total'] . "', `logged` = '" . (isset($data['logged']) ? (bool)$data['logged'] : 0) . "', `shipping` = '" . (isset($data['shipping']) ? (bool)$data['shipping'] : 0) . "', `date_start` = '" . $this->db->escape((string)$data['date_start']) . "', `date_end` = '" . $this->db->escape((string)$data['date_end']) . "', `uses_total` = '" . (int)$data['uses_total'] . "', `uses_customer` = '" . (int)$data['uses_customer'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "' WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
46: |
|
47: | $this->deleteProducts($coupon_id);
|
48: |
|
49: | if (isset($data['coupon_product'])) {
|
50: | foreach ($data['coupon_product'] as $product_id) {
|
51: | $this->addProduct($coupon_id, $product_id);
|
52: | }
|
53: | }
|
54: |
|
55: | $this->deleteCategories($coupon_id);
|
56: |
|
57: | if (isset($data['coupon_category'])) {
|
58: | foreach ($data['coupon_category'] as $category_id) {
|
59: | $this->addCategory($coupon_id, $category_id);
|
60: | }
|
61: | }
|
62: | }
|
63: |
|
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: |
|
71: | public function deleteCoupon(int $coupon_id): void {
|
72: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
73: |
|
74: | $this->deleteProducts($coupon_id);
|
75: | $this->deleteCategories($coupon_id);
|
76: | $this->deleteHistories($coupon_id);
|
77: | }
|
78: |
|
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: |
|
86: | public function getCoupon(int $coupon_id): array {
|
87: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "coupon` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
88: |
|
89: | return $query->row;
|
90: | }
|
91: |
|
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: |
|
99: | public function getCouponByCode(string $code): array {
|
100: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "coupon` WHERE `code` = '" . $this->db->escape($code) . "'");
|
101: |
|
102: | return $query->row;
|
103: | }
|
104: |
|
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: |
|
112: | public function getCoupons(array $data = []): array {
|
113: | $sql = "SELECT `coupon_id`, `name`, `code`, `discount`, `date_start`, `date_end`, `status` FROM `" . DB_PREFIX . "coupon`";
|
114: |
|
115: | $sort_data = [
|
116: | 'name',
|
117: | 'code',
|
118: | 'discount',
|
119: | 'date_start',
|
120: | 'date_end',
|
121: | 'status'
|
122: | ];
|
123: |
|
124: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
125: | $sql .= " ORDER BY " . $data['sort'];
|
126: | } else {
|
127: | $sql .= " ORDER BY `name`";
|
128: | }
|
129: |
|
130: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
131: | $sql .= " DESC";
|
132: | } else {
|
133: | $sql .= " ASC";
|
134: | }
|
135: |
|
136: | if (isset($data['start']) || isset($data['limit'])) {
|
137: | if ($data['start'] < 0) {
|
138: | $data['start'] = 0;
|
139: | }
|
140: |
|
141: | if ($data['limit'] < 1) {
|
142: | $data['limit'] = 20;
|
143: | }
|
144: |
|
145: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
146: | }
|
147: |
|
148: | $query = $this->db->query($sql);
|
149: |
|
150: | return $query->rows;
|
151: | }
|
152: |
|
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: |
|
161: | public function addProduct(int $coupon_id, int $product_id): void {
|
162: | $this->db->query("INSERT INTO `" . DB_PREFIX . "coupon_product` SET `coupon_id` = '" . (int)$coupon_id . "', `product_id` = '" . (int)$product_id . "'");
|
163: | }
|
164: |
|
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: |
|
172: | public function deleteProducts(int $coupon_id): void {
|
173: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_product` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
174: | }
|
175: |
|
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: |
|
183: | public function deleteProductsByProductId(int $product_id): void {
|
184: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_product` WHERE `product_id` = '" . (int)$product_id . "'");
|
185: | }
|
186: |
|
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: |
|
194: | public function getProducts(int $coupon_id): array {
|
195: | $coupon_product_data = [];
|
196: |
|
197: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_product` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
198: |
|
199: | foreach ($query->rows as $result) {
|
200: | $coupon_product_data[] = $result['product_id'];
|
201: | }
|
202: |
|
203: | return $coupon_product_data;
|
204: | }
|
205: |
|
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: |
|
214: | public function addCategory(int $coupon_id, int $category_id): void {
|
215: | $this->db->query("INSERT INTO `" . DB_PREFIX . "coupon_category` SET `coupon_id` = '" . (int)$coupon_id . "', `category_id` = '" . (int)$category_id . "'");
|
216: | }
|
217: |
|
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: |
|
225: | public function deleteCategories(int $coupon_id): void {
|
226: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_category` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
227: | }
|
228: |
|
229: | |
230: | |
231: | |
232: | |
233: | |
234: | |
235: |
|
236: | public function deleteCategoriesByCategoryId(int $category_id): void {
|
237: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_category` WHERE `category_id` = '" . (int)$category_id . "'");
|
238: | }
|
239: |
|
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: |
|
247: | public function getCategories(int $coupon_id): array {
|
248: | $coupon_category_data = [];
|
249: |
|
250: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon_category` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
251: |
|
252: | foreach ($query->rows as $result) {
|
253: | $coupon_category_data[] = $result['category_id'];
|
254: | }
|
255: |
|
256: | return $coupon_category_data;
|
257: | }
|
258: |
|
259: | |
260: | |
261: | |
262: | |
263: |
|
264: | public function getTotalCoupons(): int {
|
265: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "coupon`");
|
266: |
|
267: | return (int)$query->row['total'];
|
268: | }
|
269: |
|
270: | |
271: | |
272: | |
273: | |
274: | |
275: | |
276: | |
277: | |
278: |
|
279: | public function getHistories(int $coupon_id, int $start = 0, int $limit = 10): array {
|
280: | if ($start < 0) {
|
281: | $start = 0;
|
282: | }
|
283: |
|
284: | if ($limit < 1) {
|
285: | $limit = 10;
|
286: | }
|
287: |
|
288: | $query = $this->db->query("SELECT `ch`.`order_id`, CONCAT(`c`.`firstname`, ' ', `c`.`lastname`) AS `customer`, `ch`.`amount`, `ch`.`date_added` FROM `" . DB_PREFIX . "coupon_history` `ch` LEFT JOIN `" . DB_PREFIX . "customer` `c` ON (`ch`.`customer_id` = `c`.`customer_id`) WHERE `ch`.`coupon_id` = '" . (int)$coupon_id . "' ORDER BY `ch`.`date_added` ASC LIMIT " . (int)$start . "," . (int)$limit);
|
289: |
|
290: | return $query->rows;
|
291: | }
|
292: |
|
293: | |
294: | |
295: | |
296: | |
297: | |
298: | |
299: |
|
300: | public function deleteHistories(int $coupon_id): void {
|
301: | $this->db->query("DELETE FROM `" . DB_PREFIX . "coupon_history` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
302: | }
|
303: |
|
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: |
|
311: | public function getTotalHistories(int $coupon_id): int {
|
312: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "coupon_history` WHERE `coupon_id` = '" . (int)$coupon_id . "'");
|
313: |
|
314: | return (int)$query->row['total'];
|
315: | }
|
316: | }
|
317: | |