1: | <?php
2: | namespace Opencart\Admin\Controller\Marketing;
3: | /**
4: | * Class Coupon
5: | *
6: | * @package Opencart\Admin\Controller\Marketing
7: | */
8: | class Coupon extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('marketing/coupon');
16: |
17: | $this->document->setTitle($this->language->get('heading_title'));
18: |
19: | $url = '';
20: |
21: | if (isset($this->request->get['sort'])) {
22: | $url .= '&sort=' . $this->request->get['sort'];
23: | }
24: |
25: | if (isset($this->request->get['order'])) {
26: | $url .= '&order=' . $this->request->get['order'];
27: | }
28: |
29: | if (isset($this->request->get['page'])) {
30: | $url .= '&page=' . $this->request->get['page'];
31: | }
32: |
33: | $data['breadcrumbs'] = [];
34: |
35: | $data['breadcrumbs'][] = [
36: | 'text' => $this->language->get('text_home'),
37: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
38: | ];
39: |
40: | $data['breadcrumbs'][] = [
41: | 'text' => $this->language->get('heading_title'),
42: | 'href' => $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url)
43: | ];
44: |
45: | $data['add'] = $this->url->link('marketing/coupon.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: | $data['delete'] = $this->url->link('marketing/coupon.delete', 'user_token=' . $this->session->data['user_token']);
47: |
48: | $data['list'] = $this->getList();
49: |
50: | $data['user_token'] = $this->session->data['user_token'];
51: |
52: | $data['header'] = $this->load->controller('common/header');
53: | $data['column_left'] = $this->load->controller('common/column_left');
54: | $data['footer'] = $this->load->controller('common/footer');
55: |
56: | $this->response->setOutput($this->load->view('marketing/coupon', $data));
57: | }
58: |
59: | /**
60: | * List
61: | *
62: | * @return void
63: | */
64: | public function list(): void {
65: | $this->load->language('marketing/coupon');
66: |
67: | $this->response->setOutput($this->getList());
68: | }
69: |
70: | /**
71: | * Get List
72: | *
73: | * @return string
74: | */
75: | protected function getList(): string {
76: | if (isset($this->request->get['sort'])) {
77: | $sort = (string)$this->request->get['sort'];
78: | } else {
79: | $sort = 'name';
80: | }
81: |
82: | if (isset($this->request->get['order'])) {
83: | $order = (string)$this->request->get['order'];
84: | } else {
85: | $order = 'ASC';
86: | }
87: |
88: | if (isset($this->request->get['page'])) {
89: | $page = (int)$this->request->get['page'];
90: | } else {
91: | $page = 1;
92: | }
93: |
94: | $url = '';
95: |
96: | if (isset($this->request->get['sort'])) {
97: | $url .= '&sort=' . $this->request->get['sort'];
98: | }
99: |
100: | if (isset($this->request->get['order'])) {
101: | $url .= '&order=' . $this->request->get['order'];
102: | }
103: |
104: | if (isset($this->request->get['page'])) {
105: | $url .= '&page=' . $this->request->get['page'];
106: | }
107: |
108: | $data['action'] = $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . $url);
109: |
110: | $data['coupons'] = [];
111: |
112: | $filter_data = [
113: | 'sort' => $sort,
114: | 'order' => $order,
115: | 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
116: | 'limit' => $this->config->get('config_pagination_admin')
117: | ];
118: |
119: | $this->load->model('marketing/coupon');
120: |
121: | $results = $this->model_marketing_coupon->getCoupons($filter_data);
122: |
123: | foreach ($results as $result) {
124: | $data['coupons'][] = [
125: | 'coupon_id' => $result['coupon_id'],
126: | 'name' => $result['name'],
127: | 'code' => $result['code'],
128: | 'discount' => $result['discount'],
129: | 'date_start' => date($this->language->get('date_format_short'), strtotime($result['date_start'])),
130: | 'date_end' => date($this->language->get('date_format_short'), strtotime($result['date_end'])),
131: | 'status' => $result['status'],
132: | 'edit' => $this->url->link('marketing/coupon.form', 'user_token=' . $this->session->data['user_token'] . '&coupon_id=' . $result['coupon_id'] . $url)
133: | ];
134: | }
135: |
136: | $url = '';
137: |
138: | if ($order == 'ASC') {
139: | $url .= '&order=DESC';
140: | } else {
141: | $url .= '&order=ASC';
142: | }
143: |
144: | $data['sort_name'] = $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
145: | $data['sort_code'] = $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . '&sort=code' . $url);
146: | $data['sort_discount'] = $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . '&sort=discount' . $url);
147: | $data['sort_date_start'] = $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_start' . $url);
148: | $data['sort_date_end'] = $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_end' . $url);
149: |
150: | $url = '';
151: |
152: | if (isset($this->request->get['sort'])) {
153: | $url .= '&sort=' . $this->request->get['sort'];
154: | }
155: |
156: | if (isset($this->request->get['order'])) {
157: | $url .= '&order=' . $this->request->get['order'];
158: | }
159: |
160: | $coupon_total = $this->model_marketing_coupon->getTotalCoupons();
161: |
162: | $data['pagination'] = $this->load->controller('common/pagination', [
163: | 'total' => $coupon_total,
164: | 'page' => $page,
165: | 'limit' => $this->config->get('config_pagination_admin'),
166: | 'url' => $this->url->link('marketing/coupon.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
167: | ]);
168: |
169: | $data['results'] = sprintf($this->language->get('text_pagination'), ($coupon_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($coupon_total - $this->config->get('config_pagination_admin'))) ? $coupon_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $coupon_total, ceil($coupon_total / $this->config->get('config_pagination_admin')));
170: |
171: | $data['sort'] = $sort;
172: | $data['order'] = $order;
173: |
174: | return $this->load->view('marketing/coupon_list', $data);
175: | }
176: |
177: | /**
178: | * Form
179: | *
180: | * @return void
181: | */
182: | public function form(): void {
183: | $this->load->language('marketing/coupon');
184: |
185: | $this->document->setTitle($this->language->get('heading_title'));
186: |
187: | $data['text_form'] = !isset($this->request->get['coupon_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
188: |
189: | $url = '';
190: |
191: | if (isset($this->request->get['page'])) {
192: | $url .= '&page=' . $this->request->get['page'];
193: | }
194: |
195: | if (isset($this->request->get['sort'])) {
196: | $url .= '&sort=' . $this->request->get['sort'];
197: | }
198: |
199: | if (isset($this->request->get['order'])) {
200: | $url .= '&order=' . $this->request->get['order'];
201: | }
202: |
203: | $data['breadcrumbs'] = [];
204: |
205: | $data['breadcrumbs'][] = [
206: | 'text' => $this->language->get('text_home'),
207: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
208: | ];
209: |
210: | $data['breadcrumbs'][] = [
211: | 'text' => $this->language->get('heading_title'),
212: | 'href' => $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url)
213: | ];
214: |
215: | $data['save'] = $this->url->link('marketing/coupon.save', 'user_token=' . $this->session->data['user_token']);
216: | $data['back'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url);
217: |
218: | if (isset($this->request->get['coupon_id'])) {
219: | $this->load->model('marketing/coupon');
220: |
221: | $coupon_info = $this->model_marketing_coupon->getCoupon($this->request->get['coupon_id']);
222: | }
223: |
224: | if (isset($this->request->get['coupon_id'])) {
225: | $data['coupon_id'] = (int)$this->request->get['coupon_id'];
226: | } else {
227: | $data['coupon_id'] = 0;
228: | }
229: |
230: | if (!empty($coupon_info)) {
231: | $data['name'] = $coupon_info['name'];
232: | } else {
233: | $data['name'] = '';
234: | }
235: |
236: | if (!empty($coupon_info)) {
237: | $data['code'] = $coupon_info['code'];
238: | } else {
239: | $data['code'] = '';
240: | }
241: |
242: | if (!empty($coupon_info)) {
243: | $data['type'] = $coupon_info['type'];
244: | } else {
245: | $data['type'] = '';
246: | }
247: |
248: | if (!empty($coupon_info)) {
249: | $data['discount'] = $coupon_info['discount'];
250: | } else {
251: | $data['discount'] = '';
252: | }
253: |
254: | if (!empty($coupon_info)) {
255: | $data['logged'] = $coupon_info['logged'];
256: | } else {
257: | $data['logged'] = '';
258: | }
259: |
260: | if (!empty($coupon_info)) {
261: | $data['shipping'] = $coupon_info['shipping'];
262: | } else {
263: | $data['shipping'] = '';
264: | }
265: |
266: | if (!empty($coupon_info)) {
267: | $data['total'] = $coupon_info['total'];
268: | } else {
269: | $data['total'] = '';
270: | }
271: |
272: | if (!empty($coupon_info)) {
273: | $products = $this->model_marketing_coupon->getProducts($this->request->get['coupon_id']);
274: | } else {
275: | $products = [];
276: | }
277: |
278: | $this->load->model('catalog/product');
279: |
280: | $data['coupon_products'] = [];
281: |
282: | foreach ($products as $product_id) {
283: | $product_info = $this->model_catalog_product->getProduct($product_id);
284: |
285: | if ($product_info) {
286: | $data['coupon_products'][] = [
287: | 'product_id' => $product_info['product_id'],
288: | 'name' => $product_info['name']
289: | ];
290: | }
291: | }
292: |
293: | if (!empty($coupon_info)) {
294: | $categories = $this->model_marketing_coupon->getCategories($this->request->get['coupon_id']);
295: | } else {
296: | $categories = [];
297: | }
298: |
299: | $this->load->model('catalog/category');
300: |
301: | $data['coupon_categories'] = [];
302: |
303: | foreach ($categories as $category_id) {
304: | $category_info = $this->model_catalog_category->getCategory($category_id);
305: |
306: | if ($category_info) {
307: | $data['coupon_categories'][] = [
308: | 'category_id' => $category_info['category_id'],
309: | 'name' => ($category_info['path'] ? $category_info['path'] . ' &gt; ' : '') . $category_info['name']
310: | ];
311: | }
312: | }
313: |
314: | if (!empty($coupon_info)) {
315: | $data['date_start'] = ($coupon_info['date_start'] != '0000-00-00' ? $coupon_info['date_start'] : '');
316: | } else {
317: | $data['date_start'] = date('Y-m-d', time());
318: | }
319: |
320: | if (!empty($coupon_info)) {
321: | $data['date_end'] = ($coupon_info['date_end'] != '0000-00-00' ? $coupon_info['date_end'] : '');
322: | } else {
323: | $data['date_end'] = date('Y-m-d', strtotime('+1 month'));
324: | }
325: |
326: | if (!empty($coupon_info)) {
327: | $data['uses_total'] = $coupon_info['uses_total'];
328: | } else {
329: | $data['uses_total'] = 1;
330: | }
331: |
332: | if (!empty($coupon_info)) {
333: | $data['uses_customer'] = $coupon_info['uses_customer'];
334: | } else {
335: | $data['uses_customer'] = 1;
336: | }
337: |
338: | if (!empty($coupon_info)) {
339: | $data['status'] = $coupon_info['status'];
340: | } else {
341: | $data['status'] = true;
342: | }
343: |
344: | $data['history'] = $this->getHistory();
345: |
346: | $data['user_token'] = $this->session->data['user_token'];
347: |
348: | $data['header'] = $this->load->controller('common/header');
349: | $data['column_left'] = $this->load->controller('common/column_left');
350: | $data['footer'] = $this->load->controller('common/footer');
351: |
352: | $this->response->setOutput($this->load->view('marketing/coupon_form', $data));
353: | }
354: |
355: | /**
356: | * Save
357: | *
358: | * @return void
359: | */
360: | public function save(): void {
361: | $this->load->language('marketing/coupon');
362: |
363: | $json = [];
364: |
365: | if (!$this->user->hasPermission('modify', 'marketing/coupon')) {
366: | $json['error']['warning'] = $this->language->get('error_permission');
367: | }
368: |
369: | if ((oc_strlen($this->request->post['name']) < 3) || (oc_strlen($this->request->post['name']) > 128)) {
370: | $json['error']['name'] = $this->language->get('error_name');
371: | }
372: |
373: | if ((oc_strlen($this->request->post['code']) < 3) || (oc_strlen($this->request->post['code']) > 20)) {
374: | $json['error']['code'] = $this->language->get('error_code');
375: | }
376: |
377: | $this->load->model('marketing/coupon');
378: |
379: | $coupon_info = $this->model_marketing_coupon->getCouponByCode($this->request->post['code']);
380: |
381: | if ($coupon_info) {
382: | if (!isset($this->request->post['coupon_id'])) {
383: | $json['error']['warning'] = $this->language->get('error_exists');
384: | } elseif ($coupon_info['coupon_id'] != (int)$this->request->post['coupon_id']) {
385: | $json['error']['warning'] = $this->language->get('error_exists');
386: | }
387: | }
388: |
389: | if (!$json) {
390: | if (!$this->request->post['coupon_id']) {
391: | $json['coupon_id'] = $this->model_marketing_coupon->addCoupon($this->request->post);
392: | } else {
393: | $this->model_marketing_coupon->editCoupon($this->request->post['coupon_id'], $this->request->post);
394: | }
395: |
396: | $json['success'] = $this->language->get('text_success');
397: | }
398: |
399: | $this->response->addHeader('Content-Type: application/json');
400: | $this->response->setOutput(json_encode($json));
401: | }
402: |
403: | /**
404: | * Delete
405: | *
406: | * @return void
407: | */
408: | public function delete(): void {
409: | $this->load->language('marketing/coupon');
410: |
411: | $json = [];
412: |
413: | if (isset($this->request->post['selected'])) {
414: | $selected = $this->request->post['selected'];
415: | } else {
416: | $selected = [];
417: | }
418: |
419: | if (!$this->user->hasPermission('modify', 'marketing/coupon')) {
420: | $json['error'] = $this->language->get('error_permission');
421: | }
422: |
423: | if (!$json) {
424: | $this->load->model('marketing/coupon');
425: |
426: | foreach ($selected as $coupon_id) {
427: | $this->model_marketing_coupon->deleteCoupon($coupon_id);
428: | }
429: |
430: | $json['success'] = $this->language->get('text_success');
431: | }
432: |
433: | $this->response->addHeader('Content-Type: application/json');
434: | $this->response->setOutput(json_encode($json));
435: | }
436: |
437: | /**
438: | * History
439: | *
440: | * @return void
441: | */
442: | public function history(): void {
443: | $this->load->language('marketing/coupon');
444: |
445: | $this->response->setOutput($this->getHistory());
446: | }
447: |
448: | /**
449: | * Get History
450: | *
451: | * @return string
452: | */
453: | public function getHistory(): string {
454: | if (isset($this->request->get['coupon_id'])) {
455: | $coupon_id = (int)$this->request->get['coupon_id'];
456: | } else {
457: | $coupon_id = 0;
458: | }
459: |
460: | if (isset($this->request->get['page']) && $this->request->get['route'] == 'marketing/coupon.history') {
461: | $page = (int)$this->request->get['page'];
462: | } else {
463: | $page = 1;
464: | }
465: |
466: | $limit = 10;
467: |
468: | $this->load->model('marketing/coupon');
469: |
470: | $data['histories'] = [];
471: |
472: | $results = $this->model_marketing_coupon->getHistories($coupon_id, ($page - 1) * $limit, $limit);
473: |
474: | foreach ($results as $result) {
475: | $data['histories'][] = [
476: | 'order_id' => $result['order_id'],
477: | 'customer' => $result['customer'],
478: | 'amount' => $result['amount'],
479: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
480: | ];
481: | }
482: |
483: | $history_total = $this->model_marketing_coupon->getTotalHistories($coupon_id);
484: |
485: | $data['pagination'] = $this->load->controller('common/pagination', [
486: | 'total' => $history_total,
487: | 'page' => $page,
488: | 'limit' => $limit,
489: | 'url' => $this->url->link('marketing/coupon.history', 'user_token=' . $this->session->data['user_token'] . '&coupon_id=' . $coupon_id . '&page={page}')
490: | ]);
491: |
492: | $data['results'] = sprintf($this->language->get('text_pagination'), ($history_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($history_total - $limit)) ? $history_total : ((($page - 1) * $limit) + $limit), $history_total, ceil($history_total / $limit));
493: |
494: | return $this->load->view('marketing/coupon_history', $data);
495: | }
496: | }
497: |

OpenCart API API documentation generated by ApiGen dev-master