1: <?php
2: namespace Opencart\Admin\Controller\Marketing;
3: /**
4: * Class Affiliate
5: *
6: * @package Opencart\Admin\Controller\Marketing
7: */
8: class Affiliate extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('marketing/affiliate');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: if (isset($this->request->get['filter_customer'])) {
20: $filter_customer = $this->request->get['filter_customer'];
21: } else {
22: $filter_customer = '';
23: }
24:
25: if (isset($this->request->get['filter_tracking'])) {
26: $filter_tracking = $this->request->get['filter_tracking'];
27: } else {
28: $filter_tracking = '';
29: }
30:
31: if (isset($this->request->get['filter_payment_method'])) {
32: $filter_payment_method = (string)$this->request->get['filter_payment_method'];
33: } else {
34: $filter_payment_method = '';
35: }
36:
37: if (isset($this->request->get['filter_commission'])) {
38: $filter_commission = $this->request->get['filter_commission'];
39: } else {
40: $filter_commission = '';
41: }
42:
43: if (isset($this->request->get['filter_date_from'])) {
44: $filter_date_from = $this->request->get['filter_date_from'];
45: } else {
46: $filter_date_from = '';
47: }
48:
49: if (isset($this->request->get['filter_date_to'])) {
50: $filter_date_to = $this->request->get['filter_date_to'];
51: } else {
52: $filter_date_to = '';
53: }
54:
55: if (isset($this->request->get['filter_status'])) {
56: $filter_status = $this->request->get['filter_status'];
57: } else {
58: $filter_status = '';
59: }
60:
61: if (isset($this->request->get['limit'])) {
62: $limit = (int)$this->request->get['limit'];
63: } else {
64: $limit = $this->config->get('config_pagination');
65: }
66:
67: $url = '';
68:
69: if (isset($this->request->get['filter_customer'])) {
70: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
71: }
72:
73: if (isset($this->request->get['filter_tracking'])) {
74: $url .= '&filter_tracking=' . $this->request->get['filter_tracking'];
75: }
76:
77: if (isset($this->request->get['filter_payment_method'])) {
78: $url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
79: }
80:
81: if (isset($this->request->get['filter_commission'])) {
82: $url .= '&filter_commission=' . $this->request->get['filter_commission'];
83: }
84:
85: if (isset($this->request->get['filter_date_from'])) {
86: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
87: }
88:
89: if (isset($this->request->get['filter_date_to'])) {
90: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
91: }
92:
93: if (isset($this->request->get['filter_status'])) {
94: $url .= '&filter_status=' . $this->request->get['filter_status'];
95: }
96:
97: if (isset($this->request->get['sort'])) {
98: $url .= '&sort=' . $this->request->get['sort'];
99: }
100:
101: if (isset($this->request->get['order'])) {
102: $url .= '&order=' . $this->request->get['order'];
103: }
104:
105: if (isset($this->request->get['page'])) {
106: $url .= '&page=' . $this->request->get['page'];
107: }
108:
109: if (isset($this->request->get['limit'])) {
110: $url .= '&limit=' . $this->request->get['limit'];
111: }
112:
113: $data['breadcrumbs'] = [];
114:
115: $data['breadcrumbs'][] = [
116: 'text' => $this->language->get('text_home'),
117: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
118: ];
119:
120: $data['breadcrumbs'][] = [
121: 'text' => $this->language->get('heading_title'),
122: 'href' => $this->url->link('marketing/affiliate', 'user_token=' . $this->session->data['user_token'] . $url)
123: ];
124:
125: $data['csv'] = $this->url->link('marketing/affiliate.csv', 'user_token=' . $this->session->data['user_token']);
126: $data['complete'] = $this->url->link('marketing/affiliate.complete', 'user_token=' . $this->session->data['user_token']);
127: $data['add'] = $this->url->link('marketing/affiliate.form', 'user_token=' . $this->session->data['user_token'] . $url);
128: $data['delete'] = $this->url->link('marketing/affiliate.delete', 'user_token=' . $this->session->data['user_token']);
129:
130: $data['list'] = $this->getList();
131:
132: $data['payment_methods'] = [];
133:
134: $data['payment_methods'][] = [
135: 'text' => $this->language->get('text_cheque'),
136: 'value' => 'cheque'
137: ];
138:
139: $data['payment_methods'][] = [
140: 'text' => $this->language->get('text_paypal'),
141: 'value' => 'paypal'
142: ];
143:
144: $data['payment_methods'][] = [
145: 'text' => $this->language->get('text_bank'),
146: 'value' => 'bank'
147: ];
148:
149: $data['limits'] = [];
150:
151: $data['limits'][] = [
152: 'text' => $this->config->get('config_pagination'),
153: 'value' => $this->config->get('config_pagination')
154: ];
155:
156: $data['limits'][] = [
157: 'text' => 100,
158: 'value' => 100
159: ];
160:
161: $data['limits'][] = [
162: 'text' => 200,
163: 'value' => 200
164: ];
165:
166: $data['limits'][] = [
167: 'text' => 400,
168: 'value' => 400
169: ];
170:
171: $data['limits'][] = [
172: 'text' => 800,
173: 'value' => 800
174: ];
175:
176: $data['filter_customer'] = $filter_customer;
177: $data['filter_tracking'] = $filter_tracking;
178: $data['filter_payment_method'] = $filter_payment_method;
179: $data['filter_commission'] = $filter_commission;
180: $data['filter_date_from'] = $filter_date_from;
181: $data['filter_date_to'] = $filter_date_to;
182: $data['filter_status'] = $filter_status;
183:
184: $data['limit'] = $limit;
185:
186: $data['user_token'] = $this->session->data['user_token'];
187:
188: $data['header'] = $this->load->controller('common/header');
189: $data['column_left'] = $this->load->controller('common/column_left');
190: $data['footer'] = $this->load->controller('common/footer');
191:
192: $this->response->setOutput($this->load->view('marketing/affiliate', $data));
193: }
194:
195: /**
196: * List
197: *
198: * @return void
199: */
200: public function list(): void {
201: $this->load->language('marketing/affiliate');
202:
203: $this->response->setOutput($this->getList());
204: }
205:
206: /**
207: * Get List
208: *
209: * @return string
210: */
211: protected function getList(): string {
212: if (isset($this->request->get['filter_customer'])) {
213: $filter_customer = (string)$this->request->get['filter_customer'];
214: } else {
215: $filter_customer = '';
216: }
217:
218: if (isset($this->request->get['filter_tracking'])) {
219: $filter_tracking = (string)$this->request->get['filter_tracking'];
220: } else {
221: $filter_tracking = '';
222: }
223:
224: if (isset($this->request->get['filter_payment_method'])) {
225: $filter_payment_method = (string)$this->request->get['filter_payment_method'];
226: } else {
227: $filter_payment_method = '';
228: }
229:
230: if (isset($this->request->get['filter_commission'])) {
231: $filter_commission = $this->request->get['filter_commission'];
232: } else {
233: $filter_commission = '';
234: }
235:
236: if (isset($this->request->get['filter_date_from'])) {
237: $filter_date_from = (string)$this->request->get['filter_date_from'];
238: } else {
239: $filter_date_from = '';
240: }
241:
242: if (isset($this->request->get['filter_date_to'])) {
243: $filter_date_to = (string)$this->request->get['filter_date_to'];
244: } else {
245: $filter_date_to = '';
246: }
247:
248: if (isset($this->request->get['filter_status'])) {
249: $filter_status = $this->request->get['filter_status'];
250: } else {
251: $filter_status = '';
252: }
253:
254: if (isset($this->request->get['sort'])) {
255: $sort = (string)$this->request->get['sort'];
256: } else {
257: $sort = 'name';
258: }
259:
260: if (isset($this->request->get['order'])) {
261: $order = (string)$this->request->get['order'];
262: } else {
263: $order = 'ASC';
264: }
265:
266: if (isset($this->request->get['page'])) {
267: $page = (int)$this->request->get['page'];
268: } else {
269: $page = 1;
270: }
271:
272: if (isset($this->request->get['limit'])) {
273: $limit = (int)$this->request->get['limit'];
274: } else {
275: $limit = $this->config->get('config_pagination');
276: }
277:
278: $url = '';
279:
280: if (isset($this->request->get['filter_customer'])) {
281: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
282: }
283:
284: if (isset($this->request->get['filter_tracking'])) {
285: $url .= '&filter_tracking=' . $this->request->get['filter_tracking'];
286: }
287:
288: if (isset($this->request->get['filter_payment_method'])) {
289: $url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
290: }
291:
292: if (isset($this->request->get['filter_commission'])) {
293: $url .= '&filter_commission=' . $this->request->get['filter_commission'];
294: }
295:
296: if (isset($this->request->get['filter_date_from'])) {
297: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
298: }
299:
300: if (isset($this->request->get['filter_date_to'])) {
301: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
302: }
303:
304: if (isset($this->request->get['filter_status'])) {
305: $url .= '&filter_status=' . $this->request->get['filter_status'];
306: }
307:
308: if (isset($this->request->get['sort'])) {
309: $url .= '&sort=' . $this->request->get['sort'];
310: }
311:
312: if (isset($this->request->get['order'])) {
313: $url .= '&order=' . $this->request->get['order'];
314: }
315:
316: if (isset($this->request->get['page'])) {
317: $url .= '&page=' . $this->request->get['page'];
318: }
319:
320: if (isset($this->request->get['limit'])) {
321: $url .= '&limit=' . $this->request->get['limit'];
322: }
323:
324: $data['action'] = $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . $url);
325:
326: $data['affiliates'] = [];
327:
328: $filter_data = [
329: 'filter_name' => $filter_customer,
330: 'filter_tracking' => $filter_tracking,
331: 'filter_payment_method' => $filter_payment_method,
332: 'filter_commission' => $filter_commission,
333: 'filter_date_from' => $filter_date_from,
334: 'filter_date_to' => $filter_date_to,
335: 'filter_status' => $filter_status,
336: 'sort' => $sort,
337: 'order' => $order,
338: 'start' => ($page - 1) * $limit,
339: 'limit' => $limit
340: ];
341:
342: $this->load->model('marketing/affiliate');
343:
344: $results = $this->model_marketing_affiliate->getAffiliates($filter_data);
345:
346: foreach ($results as $result) {
347: $data['affiliates'][] = [
348: 'customer_id' => $result['customer_id'],
349: 'name' => $result['name'],
350: 'tracking' => $result['tracking'],
351: 'commission' => $result['commission'],
352: 'balance' => $this->currency->format((int)$result['balance'], $this->config->get('config_currency')),
353: 'status' => $result['status'],
354: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
355: 'customer' => $this->url->link('customer/customer.form', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $result['customer_id']),
356: 'edit' => $this->url->link('marketing/affiliate.form', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $result['customer_id'] . $url)
357: ];
358: }
359:
360: $url = '';
361:
362: if (isset($this->request->get['filter_customer'])) {
363: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
364: }
365:
366: if (isset($this->request->get['filter_tracking'])) {
367: $url .= '&filter_tracking=' . $this->request->get['filter_tracking'];
368: }
369:
370: if (isset($this->request->get['filter_payment_method'])) {
371: $url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
372: }
373:
374: if (isset($this->request->get['filter_commission'])) {
375: $url .= '&filter_commission=' . $this->request->get['filter_commission'];
376: }
377:
378: if (isset($this->request->get['filter_date_from'])) {
379: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
380: }
381:
382: if (isset($this->request->get['filter_date_to'])) {
383: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
384: }
385:
386: if (isset($this->request->get['filter_status'])) {
387: $url .= '&filter_status=' . $this->request->get['filter_status'];
388: }
389:
390: if ($order == 'ASC') {
391: $url .= '&order=DESC';
392: } else {
393: $url .= '&order=ASC';
394: }
395:
396: if (isset($this->request->get['limit'])) {
397: $url .= '&limit=' . $this->request->get['limit'];
398: }
399:
400: $data['sort_name'] = $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
401: $data['sort_tracking'] = $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=ca.tracking' . $url);
402: $data['sort_commission'] = $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=ca.commission' . $url);
403: $data['sort_balance'] = $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=ca.balance' . $url);
404: $data['sort_date_added'] = $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . '&sort=ca.date_added' . $url);
405:
406: $url = '';
407:
408: if (isset($this->request->get['filter_customer'])) {
409: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
410: }
411:
412: if (isset($this->request->get['filter_tracking'])) {
413: $url .= '&filter_tracking=' . $this->request->get['filter_tracking'];
414: }
415:
416: if (isset($this->request->get['filter_payment_method'])) {
417: $url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
418: }
419:
420: if (isset($this->request->get['filter_commission'])) {
421: $url .= '&filter_commission=' . $this->request->get['filter_commission'];
422: }
423:
424: if (isset($this->request->get['filter_date_from'])) {
425: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
426: }
427:
428: if (isset($this->request->get['filter_date_to'])) {
429: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
430: }
431:
432: if (isset($this->request->get['filter_status'])) {
433: $url .= '&filter_status=' . $this->request->get['filter_status'];
434: }
435:
436: if (isset($this->request->get['sort'])) {
437: $url .= '&sort=' . $this->request->get['sort'];
438: }
439:
440: if (isset($this->request->get['order'])) {
441: $url .= '&order=' . $this->request->get['order'];
442: }
443:
444: if (isset($this->request->get['limit'])) {
445: $url .= '&limit=' . $this->request->get['limit'];
446: }
447:
448: $affiliate_total = $this->model_marketing_affiliate->getTotalAffiliates($filter_data);
449:
450: $data['pagination'] = $this->load->controller('common/pagination', [
451: 'total' => $affiliate_total,
452: 'page' => $page,
453: 'limit' => $this->config->get('config_pagination_admin'),
454: 'url' => $this->url->link('marketing/affiliate.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
455: ]);
456:
457: $data['results'] = sprintf($this->language->get('text_pagination'), ($affiliate_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($affiliate_total - $limit)) ? $affiliate_total : ((($page - 1) * $limit) + $limit), $affiliate_total, ceil($affiliate_total / $limit));
458:
459: $data['sort'] = $sort;
460: $data['order'] = $order;
461: $data['limit'] = $limit;
462:
463: return $this->load->view('marketing/affiliate_list', $data);
464: }
465:
466: /**
467: * Form
468: *
469: * @return void
470: */
471: public function form(): void {
472: $this->load->language('marketing/affiliate');
473:
474: $this->document->setTitle($this->language->get('heading_title'));
475:
476: $data['text_form'] = !isset($this->request->get['customer_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
477:
478: $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
479:
480: $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
481:
482: $url = '';
483:
484: if (isset($this->request->get['filter_customer'])) {
485: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
486: }
487:
488: if (isset($this->request->get['filter_tracking'])) {
489: $url .= '&filter_tracking=' . $this->request->get['filter_tracking'];
490: }
491:
492: if (isset($this->request->get['filter_payment_method'])) {
493: $url .= '&filter_payment_method=' . $this->request->get['filter_payment_method'];
494: }
495:
496: if (isset($this->request->get['filter_commission'])) {
497: $url .= '&filter_commission=' . $this->request->get['filter_commission'];
498: }
499:
500: if (isset($this->request->get['filter_date_from'])) {
501: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
502: }
503:
504: if (isset($this->request->get['filter_date_to'])) {
505: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
506: }
507:
508: if (isset($this->request->get['filter_status'])) {
509: $url .= '&filter_status=' . $this->request->get['filter_status'];
510: }
511:
512: if (isset($this->request->get['sort'])) {
513: $url .= '&sort=' . $this->request->get['sort'];
514: }
515:
516: if (isset($this->request->get['order'])) {
517: $url .= '&order=' . $this->request->get['order'];
518: }
519:
520: if (isset($this->request->get['page'])) {
521: $url .= '&page=' . $this->request->get['page'];
522: }
523:
524: if (isset($this->request->get['limit'])) {
525: $url .= '&limit=' . $this->request->get['limit'];
526: }
527:
528: $data['breadcrumbs'] = [];
529:
530: $data['breadcrumbs'][] = [
531: 'text' => $this->language->get('text_home'),
532: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
533: ];
534:
535: $data['breadcrumbs'][] = [
536: 'text' => $this->language->get('heading_title'),
537: 'href' => $this->url->link('marketing/affiliate', 'user_token=' . $this->session->data['user_token'] . $url)
538: ];
539:
540: $data['save'] = $this->url->link('marketing/affiliate.save', 'user_token=' . $this->session->data['user_token']);
541: $data['back'] = $this->url->link('marketing/affiliate', 'user_token=' . $this->session->data['user_token'] . $url);
542: $data['upload'] = $this->url->link('tool/upload.upload', 'user_token=' . $this->session->data['user_token']);
543:
544: // Affiliate
545: if (isset($this->request->get['customer_id'])) {
546: $this->load->model('marketing/affiliate');
547:
548: $affiliate_info = $this->model_marketing_affiliate->getAffiliate($this->request->get['customer_id']);
549: }
550:
551: if (isset($this->request->get['customer_id'])) {
552: $data['customer_id'] = (int)$this->request->get['customer_id'];
553: } else {
554: $data['customer_id'] = 0;
555: }
556:
557: if (!empty($affiliate_info)) {
558: $data['customer'] = $affiliate_info['customer'];
559: } else {
560: $data['customer'] = '';
561: }
562:
563: if (!empty($affiliate_info)) {
564: $data['customer_group_id'] = $affiliate_info['customer_group_id'];
565: } else {
566: $data['customer_group_id'] = '';
567: }
568:
569: if (!empty($affiliate_info)) {
570: $data['company'] = $affiliate_info['company'];
571: } else {
572: $data['company'] = '';
573: }
574:
575: if (!empty($affiliate_info)) {
576: $data['website'] = $affiliate_info['website'];
577: } else {
578: $data['website'] = '';
579: }
580:
581: if (!empty($affiliate_info)) {
582: $data['tracking'] = $affiliate_info['tracking'];
583: } else {
584: $data['tracking'] = oc_token(10);
585: }
586:
587: if (!empty($affiliate_info)) {
588: $data['commission'] = $affiliate_info['commission'];
589: } else {
590: $data['commission'] = $this->config->get('config_affiliate_commission');
591: }
592:
593: if (!empty($affiliate_info)) {
594: $data['status'] = $affiliate_info['status'];
595: } else {
596: $data['status'] = '';
597: }
598:
599: if (!empty($affiliate_info)) {
600: $data['tax'] = $affiliate_info['tax'];
601: } else {
602: $data['tax'] = '';
603: }
604:
605: if (!empty($affiliate_info)) {
606: $data['payment_method'] = $affiliate_info['payment_method'];
607: } else {
608: $data['payment_method'] = 'cheque';
609: }
610:
611: if (!empty($affiliate_info)) {
612: $data['cheque'] = $affiliate_info['cheque'];
613: } else {
614: $data['cheque'] = '';
615: }
616:
617: if (!empty($affiliate_info)) {
618: $data['paypal'] = $affiliate_info['paypal'];
619: } else {
620: $data['paypal'] = '';
621: }
622:
623: if (!empty($affiliate_info)) {
624: $data['bank_name'] = $affiliate_info['bank_name'];
625: } else {
626: $data['bank_name'] = '';
627: }
628:
629: if (!empty($affiliate_info)) {
630: $data['bank_branch_number'] = $affiliate_info['bank_branch_number'];
631: } else {
632: $data['bank_branch_number'] = '';
633: }
634:
635: if (!empty($affiliate_info)) {
636: $data['bank_swift_code'] = $affiliate_info['bank_swift_code'];
637: } else {
638: $data['bank_swift_code'] = '';
639: }
640:
641: if (!empty($affiliate_info)) {
642: $data['bank_account_name'] = $affiliate_info['bank_account_name'];
643: } else {
644: $data['bank_account_name'] = '';
645: }
646:
647: if (!empty($affiliate_info)) {
648: $data['bank_account_number'] = $affiliate_info['bank_account_number'];
649: } else {
650: $data['bank_account_number'] = '';
651: }
652:
653: $data['custom_fields'] = [];
654:
655: $filter_data = [
656: 'filter_location' => 'affiliate',
657: 'sort' => 'cf.sort_order',
658: 'order' => 'ASC'
659: ];
660:
661: // Custom Fields
662: $this->load->model('customer/custom_field');
663:
664: $custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);
665:
666: foreach ($custom_fields as $custom_field) {
667: if ($custom_field['status']) {
668: $data['custom_fields'][] = [
669: 'custom_field_id' => $custom_field['custom_field_id'],
670: 'custom_field_value' => $this->model_customer_custom_field->getValues($custom_field['custom_field_id']),
671: 'name' => $custom_field['name'],
672: 'value' => $custom_field['value'],
673: 'type' => $custom_field['type'],
674: 'location' => $custom_field['location'],
675: 'sort_order' => $custom_field['sort_order']
676: ];
677: }
678: }
679:
680: if (!empty($affiliate_info)) {
681: $data['affiliate_custom_field'] = $affiliate_info['custom_field'];
682: } else {
683: $data['affiliate_custom_field'] = [];
684: }
685:
686: $data['history'] = $this->load->controller('customer/customer.getHistory');
687: $data['transaction'] = $this->load->controller('customer/customer.getTransaction');
688: $data['report'] = $this->getReport();
689:
690: $data['user_token'] = $this->session->data['user_token'];
691:
692: $data['header'] = $this->load->controller('common/header');
693: $data['column_left'] = $this->load->controller('common/column_left');
694: $data['footer'] = $this->load->controller('common/footer');
695:
696: $this->response->setOutput($this->load->view('marketing/affiliate_form', $data));
697: }
698:
699: /**
700: * Save
701: *
702: * @return void
703: */
704: public function save(): void {
705: $this->load->language('marketing/affiliate');
706:
707: $json = [];
708:
709: if (!$this->user->hasPermission('modify', 'marketing/affiliate')) {
710: $json['error']['warning'] = $this->language->get('error_permission');
711: }
712:
713: $this->load->model('customer/customer');
714:
715: $customer_info = $this->model_customer_customer->getCustomer((int)$this->request->post['customer_id']);
716:
717: if (!$customer_info) {
718: $json['error']['customer'] = $this->language->get('error_customer');
719: }
720:
721: // Check to see if customer is already a affiliate
722: $this->load->model('marketing/affiliate');
723:
724: $affiliate_info = $this->model_marketing_affiliate->getAffiliate((int)$this->request->post['customer_id']);
725:
726: if ($affiliate_info && (!isset($this->request->post['customer_id']) || ($this->request->post['customer_id'] != $affiliate_info['customer_id']))) {
727: $json['error']['warning'] = $this->language->get('error_already');
728: }
729:
730: if (!$this->request->post['tracking']) {
731: $json['error']['tracking'] = $this->language->get('error_tracking');
732: }
733:
734: $affiliate_info = $this->model_marketing_affiliate->getAffiliateByTracking($this->request->post['tracking']);
735:
736: if ($affiliate_info && (!isset($this->request->post['customer_id']) || ($this->request->post['customer_id'] != $affiliate_info['customer_id']))) {
737: $json['error']['tracking'] = $this->language->get('error_exists');
738: }
739:
740: // Payment validation
741: if (empty($this->request->post['payment_method'])) {
742: $json['error']['payment_method'] = $this->language->get('error_payment_method');
743: }
744:
745: if ($this->request->post['payment_method'] == 'cheque' && $this->request->post['cheque'] == '') {
746: $json['error']['cheque'] = $this->language->get('error_cheque');
747: } elseif ($this->request->post['payment_method'] == 'paypal' && ((oc_strlen($this->request->post['paypal']) > 96) || !filter_var($this->request->post['paypal'], FILTER_VALIDATE_EMAIL))) {
748: $json['error']['paypal'] = $this->language->get('error_paypal');
749: } elseif ($this->request->post['payment_method'] == 'bank') {
750: if ($this->request->post['bank_account_name'] == '') {
751: $json['error']['bank_account_name'] = $this->language->get('error_bank_account_name');
752: }
753:
754: if ($this->request->post['bank_account_number'] == '') {
755: $json['error']['bank_account_number'] = $this->language->get('error_bank_account_number');
756: }
757: }
758:
759: // Custom field validation
760: if ($customer_info) {
761: $this->load->model('customer/custom_field');
762:
763: $filter_data = [
764: 'filter_location' => 'account',
765: 'filter_customer_group_id' => $this->request->post['customer_group_id'],
766: 'filter_status' => 1
767: ];
768:
769: $custom_fields = $this->model_customer_custom_field->getCustomFields(['filter_customer_group_id' => $customer_info['customer_group_id']]);
770:
771: foreach ($custom_fields as $custom_field) {
772: if ($custom_field['status']) {
773: if (($custom_field['location'] == 'affiliate') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
774: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
775: } elseif (($custom_field['location'] == 'affiliate') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
776: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
777: }
778: }
779: }
780: }
781:
782: if (isset($json['error']) && !isset($json['error']['warning'])) {
783: $json['error']['warning'] = $this->language->get('error_warning');
784: }
785:
786: if (!$json) {
787: // Use affiliate
788: if (!$affiliate_info) {
789: $this->model_marketing_affiliate->addAffiliate($this->request->post);
790: } else {
791: $this->model_marketing_affiliate->editAffiliate($this->request->post['customer_id'], $this->request->post);
792: }
793:
794: $json['success'] = $this->language->get('text_success');
795: }
796:
797: $this->response->addHeader('Content-Type: application/json');
798: $this->response->setOutput(json_encode($json));
799: }
800:
801: /**
802: * Delete
803: *
804: * @return void
805: */
806: public function delete(): void {
807: $this->load->language('marketing/affiliate');
808:
809: $json = [];
810:
811: if (isset($this->request->post['selected'])) {
812: $selected = $this->request->post['selected'];
813: } else {
814: $selected = [];
815: }
816:
817: if (!$this->user->hasPermission('modify', 'marketing/affiliate')) {
818: $json['error'] = $this->language->get('error_permission');
819: }
820:
821: if (!$json) {
822: $this->load->model('marketing/affiliate');
823:
824: foreach ($selected as $affiliate_id) {
825: $this->model_marketing_affiliate->deleteAffiliate($affiliate_id);
826: }
827:
828: $json['success'] = $this->language->get('text_success');
829: }
830:
831: $this->response->addHeader('Content-Type: application/json');
832: $this->response->setOutput(json_encode($json));
833: }
834:
835: /**
836: * Calculate
837: *
838: * @return void
839: */
840: public function calculate(): void {
841: $this->load->language('marketing/affiliate');
842:
843: $json = [];
844:
845: if (!$this->user->hasPermission('modify', 'marketing/affiliate')) {
846: $json['error'] = $this->language->get('error_permission');
847: }
848:
849: if (!$json) {
850: $this->load->model('marketing/affiliate');
851: $this->load->model('customer/customer');
852:
853: $results = $this->model_marketing_affiliate->getAffiliates(['filter_status' => 1]);
854:
855: foreach ($results as $result) {
856: $this->model_marketing_affiliate->editBalance($result['customer_id'], $this->model_customer_customer->getTransactionTotal($result['customer_id']));
857: }
858:
859: $json['success'] = $this->language->get('text_success');
860: }
861:
862: $this->response->addHeader('Content-Type: application/json');
863: $this->response->setOutput(json_encode($json));
864: }
865:
866: /**
867: * Csv
868: *
869: * @return \Opencart\System\Engine\Action|void
870: */
871: public function csv() {
872: $this->load->language('marketing/affiliate');
873:
874: if (isset($this->request->post['selected'])) {
875: $selected = $this->request->post['selected'];
876: } else {
877: $selected = [];
878: }
879:
880: if ($this->user->hasPermission('modify', 'marketing/affiliate')) {
881: $this->load->model('marketing/affiliate');
882:
883: $csv = '';
884:
885: foreach ($selected as $customer_id) {
886: $affiliate_info = $this->model_marketing_affiliate->getAffiliate($customer_id);
887:
888: if ($affiliate_info && $affiliate_info['status'] && (float)$affiliate_info['balance'] > 0) {
889: $balance = $this->currency->format($affiliate_info['balance'], $this->config->get('config_currency'), 1.00000000, false);
890:
891: if ($affiliate_info['payment_method'] == 'cheque') {
892: $csv .= $affiliate_info['cheque'] . ',' . $balance . ',' . $this->config->get('config_currency') . ',' . $affiliate_info['customer'] . "\n";
893: }
894:
895: if ($affiliate_info['payment_method'] == 'paypal') {
896: $csv .= $affiliate_info['paypal'] . ',' . $balance . ',' . $this->config->get('config_currency') . ',' . $affiliate_info['customer'] . ',Thanks for your business!' . "\n";
897: }
898:
899: if ($affiliate_info['payment_method'] == 'bank') {
900: $csv .= $affiliate_info['bank_name'] . ',' . $affiliate_info['bank_branch_number'] . ',' . $affiliate_info['bank_swift_code'] . ',' . $affiliate_info['bank_account_name'] . ',' . $affiliate_info['bank_account_number'] . ',' . $balance . ',' . $this->config->get('config_currency') . ',' . $affiliate_info['customer'] . "\n";
901: }
902: }
903: }
904:
905: if (!headers_sent()) {
906: header('Pragma: public');
907: header('Expires: 0');
908: header('Content-Description: File Transfer');
909: header('Content-Type: application/octet-stream');
910: header('Content-Transfer-Encoding: binary');
911: header('Content-Disposition: attachment; filename=payout-' . date('d-m-Y') . '.csv"');
912: header('Content-Length: ' . strlen($csv));
913:
914: echo $csv;
915: } else {
916: exit('Error: Headers already sent out!');
917: }
918: } else {
919: return new \Opencart\System\Engine\Action('error/permission');
920: }
921: }
922:
923: /**
924: * Complete
925: *
926: * @return void
927: */
928: public function complete(): void {
929: $this->load->language('marketing/affiliate');
930:
931: $json = [];
932:
933: if (isset($this->request->post['selected'])) {
934: $selected = $this->request->post['selected'];
935: } else {
936: $selected = [];
937: }
938:
939: if (!$this->user->hasPermission('modify', 'marketing/affiliate')) {
940: $json['error'] = $this->language->get('error_permission');
941: }
942:
943: if (!$json) {
944: $this->load->model('marketing/affiliate');
945: $this->load->model('customer/customer');
946:
947: foreach ($selected as $customer_id) {
948: $affiliate_info = $this->model_marketing_affiliate->getAffiliate($customer_id);
949:
950: if ($affiliate_info && $affiliate_info['status'] && (float)$affiliate_info['balance'] > 0) {
951: $this->model_customer_customer->addTransaction($affiliate_info['customer_id'], $this->language->get('text_payment_' . $affiliate_info['payment_method']), -$affiliate_info['balance']);
952:
953: $this->model_marketing_affiliate->editBalance($affiliate_info['customer_id'], 0);
954: }
955: }
956:
957: $json['success'] = $this->language->get('text_success');
958: }
959:
960: $this->response->addHeader('Content-Type: application/json');
961: $this->response->setOutput(json_encode($json));
962: }
963:
964: /**
965: * Report
966: *
967: * @return void
968: */
969: public function report(): void {
970: $this->load->language('marketing/affiliate');
971:
972: $this->response->setOutput($this->getReport());
973: }
974:
975: /**
976: * Get Report
977: *
978: * @return string
979: */
980: private function getReport(): string {
981: if (isset($this->request->get['customer_id'])) {
982: $customer_id = (int)$this->request->get['customer_id'];
983: } else {
984: $customer_id = 0;
985: }
986:
987: if (isset($this->request->get['page']) && $this->request->get['route'] == 'marketing/affiliate.report') {
988: $page = (int)$this->request->get['page'];
989: } else {
990: $page = 1;
991: }
992:
993: $limit = 10;
994:
995: $data['reports'] = [];
996:
997: $this->load->model('marketing/affiliate');
998: $this->load->model('customer/customer');
999: $this->load->model('setting/store');
1000:
1001: $results = $this->model_marketing_affiliate->getReports($customer_id, ($page - 1) * $limit, $limit);
1002:
1003: foreach ($results as $result) {
1004: $store_info = $this->model_setting_store->getStore($result['store_id']);
1005:
1006: if ($store_info) {
1007: $store = $store_info['name'];
1008: } elseif (!$result['store_id']) {
1009: $store = $this->config->get('config_name');
1010: } else {
1011: $store = '';
1012: }
1013:
1014: $data['reports'][] = [
1015: 'ip' => $result['ip'],
1016: 'account' => $this->model_customer_customer->getTotalCustomersByIp($result['ip']),
1017: 'store' => $store,
1018: 'country' => $result['country'],
1019: 'date_added' => date($this->language->get('datetime_format'), strtotime($result['date_added'])),
1020: 'filter_ip' => $this->url->link('customer/customer', 'user_token=' . $this->session->data['user_token'] . '&filter_ip=' . $result['ip'])
1021: ];
1022: }
1023:
1024: $report_total = $this->model_marketing_affiliate->getTotalReports($customer_id);
1025:
1026: $data['pagination'] = $this->load->controller('common/pagination', [
1027: 'total' => $report_total,
1028: 'page' => $page,
1029: 'limit' => $limit,
1030: 'url' => $this->url->link('marketing/affiliate.report', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $customer_id . '&page={page}')
1031: ]);
1032:
1033: $data['results'] = sprintf($this->language->get('text_pagination'), ($report_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($report_total - $limit)) ? $report_total : ((($page - 1) * $limit) + $limit), $report_total, ceil($report_total / $limit));
1034:
1035: return $this->load->view('marketing/affiliate_report', $data);
1036: }
1037:
1038: /**
1039: * Autocomplete
1040: *
1041: * @return void
1042: */
1043: public function autocomplete(): void {
1044: $json = [];
1045:
1046: if (isset($this->request->get['filter_name'])) {
1047: $filter_name = $this->request->get['filter_name'];
1048: } else {
1049: $filter_name = '';
1050: }
1051:
1052: if (isset($this->request->get['filter_email'])) {
1053: $filter_email = $this->request->get['filter_email'];
1054: } else {
1055: $filter_email = '';
1056: }
1057:
1058: $filter_data = [
1059: 'filter_name' => $filter_name,
1060: 'filter_email' => $filter_email,
1061: 'start' => 0,
1062: 'limit' => 5
1063: ];
1064:
1065: $this->load->model('marketing/affiliate');
1066:
1067: $results = $this->model_marketing_affiliate->getAffiliates($filter_data);
1068:
1069: foreach ($results as $result) {
1070: $json[] = [
1071: 'customer_id' => $result['customer_id'],
1072: 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
1073: 'email' => $result['email']
1074: ];
1075: }
1076:
1077: $sort_order = [];
1078:
1079: foreach ($json as $key => $value) {
1080: $sort_order[$key] = $value['name'];
1081: }
1082:
1083: array_multisort($sort_order, SORT_ASC, $json);
1084:
1085: $this->response->addHeader('Content-Type: application/json');
1086: $this->response->setOutput(json_encode($json));
1087: }
1088: }
1089: