1: <?php
2: namespace Opencart\Catalog\Controller\Account;
3: /**
4: * Class Subscription
5: *
6: * @package Opencart\Catalog\Controller\Account
7: */
8: class Subscription extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('account/subscription');
14:
15: if (isset($this->request->get['page'])) {
16: $page = (int)$this->request->get['page'];
17: } else {
18: $page = 1;
19: }
20:
21: if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
22: $this->session->data['redirect'] = $this->url->link('account/subscription', 'language=' . $this->config->get('config_language'));
23:
24: $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
25: }
26:
27: $this->document->setTitle($this->language->get('heading_title'));
28:
29: $url = '';
30:
31: if (isset($this->request->get['page'])) {
32: $url .= '&page=' . $this->request->get['page'];
33: }
34:
35: $data['breadcrumbs'] = [];
36:
37: $data['breadcrumbs'][] = [
38: 'text' => $this->language->get('text_home'),
39: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
40: ];
41:
42: $data['breadcrumbs'][] = [
43: 'text' => $this->language->get('text_account'),
44: 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
45: ];
46:
47: $data['breadcrumbs'][] = [
48: 'text' => $this->language->get('heading_title'),
49: 'href' => $this->url->link('account/subscription', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . $url)
50: ];
51:
52: $limit = 10;
53:
54: $data['subscriptions'] = [];
55:
56: $this->load->model('account/subscription');
57: $this->load->model('account/order');
58: $this->load->model('catalog/product');
59: $this->load->model('localisation/currency');
60: $this->load->model('localisation/subscription_status');
61:
62: $results = $this->model_account_subscription->getSubscriptions(($page - 1) * $limit, $limit);
63:
64: foreach ($results as $result) {
65: $product_info = $this->model_catalog_product->getProduct($result['product_id']);
66:
67: if ($product_info) {
68: $currency_info = $this->model_localisation_currency->getCurrency($result['currency_id']);
69:
70: if ($currency_info) {
71: $currency = $currency_info['code'];
72: } else {
73: $currency = $this->config->get('config_currency');
74: }
75:
76: $description = '';
77:
78: if ($result['trial_status']) {
79: $trial_price = $this->currency->format($this->tax->calculate($result['trial_price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $currency);
80: $trial_cycle = $result['trial_cycle'];
81: $trial_frequency = $this->language->get('text_' . $result['trial_frequency']);
82: $trial_duration = $result['trial_duration'];
83:
84: $description .= sprintf($this->language->get('text_subscription_trial'), $trial_price, $trial_cycle, $trial_frequency, $trial_duration);
85: }
86:
87: $price = $this->currency->format($this->tax->calculate($result['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $currency);
88: $cycle = $result['cycle'];
89: $frequency = $this->language->get('text_' . $result['frequency']);
90: $duration = $result['duration'];
91:
92: if ($duration) {
93: $description .= sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
94: } else {
95: $description .= sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
96: }
97:
98: $subscription_status_info = $this->model_localisation_subscription_status->getSubscriptionStatus($result['subscription_status_id']);
99:
100: if ($subscription_status_info) {
101: $subscription_status = $subscription_status_info['name'];
102: } else {
103: $subscription_status = '';
104: }
105:
106: $data['subscriptions'][] = [
107: 'subscription_id' => $result['subscription_id'],
108: 'product_id' => $result['product_id'],
109: 'product_name' => $product_info['name'],
110: 'description' => $description,
111: 'product' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $result['product_id']),
112: 'status' => $subscription_status,
113: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
114: 'view' => $this->url->link('account/subscription.info', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&subscription_id=' . $result['subscription_id'])
115: ];
116: }
117: }
118:
119: $subscription_total = $this->model_account_subscription->getTotalSubscriptions();
120:
121: $data['pagination'] = $this->load->controller('common/pagination', [
122: 'total' => $subscription_total,
123: 'page' => $page,
124: 'limit' => $limit,
125: 'url' => $this->url->link('account/subscription', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&page={page}')
126: ]);
127:
128: $data['results'] = sprintf($this->language->get('text_pagination'), ($subscription_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($subscription_total - $limit)) ? $subscription_total : ((($page - 1) * $limit) + $limit), $subscription_total, ceil($subscription_total / $limit));
129:
130: $data['continue'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
131:
132: $data['column_left'] = $this->load->controller('common/column_left');
133: $data['column_right'] = $this->load->controller('common/column_right');
134: $data['content_top'] = $this->load->controller('common/content_top');
135: $data['content_bottom'] = $this->load->controller('common/content_bottom');
136: $data['footer'] = $this->load->controller('common/footer');
137: $data['header'] = $this->load->controller('common/header');
138:
139: $this->response->setOutput($this->load->view('account/subscription_list', $data));
140: }
141:
142: /**
143: * Info
144: *
145: * @return \Opencart\System\Engine\Action|null
146: */
147: public function info(): ?\Opencart\System\Engine\Action {
148: $this->load->language('account/subscription');
149:
150: if (isset($this->request->get['subscription_id'])) {
151: $subscription_id = (int)$this->request->get['subscription_id'];
152: } else {
153: $subscription_id = 0;
154: }
155:
156: if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
157: $this->session->data['redirect'] = $this->url->link('account/subscription', 'language=' . $this->config->get('config_language'));
158:
159: $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
160: }
161:
162: $this->load->model('account/subscription');
163:
164: $subscription_info = $this->model_account_subscription->getSubscription($subscription_id);
165:
166: if ($subscription_info) {
167: $heading_title = sprintf($this->language->get('text_subscription'), $subscription_info['subscription_id']);
168:
169: $this->document->setTitle($heading_title);
170:
171: $data['heading_title'] = $heading_title;
172:
173: $url = '';
174:
175: if (isset($this->request->get['page'])) {
176: $url .= '&page=' . $this->request->get['page'];
177: }
178:
179: $data['breadcrumbs'] = [];
180:
181: $data['breadcrumbs'][] = [
182: 'text' => $this->language->get('text_home'),
183: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
184: ];
185:
186: $data['breadcrumbs'][] = [
187: 'text' => $this->language->get('text_account'),
188: 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
189: ];
190:
191: $data['breadcrumbs'][] = [
192: 'text' => $this->language->get('heading_title'),
193: 'href' => $this->url->link('account/subscription', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . $url)
194: ];
195:
196: $data['breadcrumbs'][] = [
197: 'text' => $heading_title,
198: 'href' => $this->url->link('account/subscription.info', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&subscription_id=' . $this->request->get['subscription_id'] . $url)
199: ];
200:
201: $data['subscription_id'] = $subscription_info['subscription_id'];
202: $data['order_id'] = $subscription_info['order_id'];
203:
204: $this->load->model('localisation/subscription_status');
205:
206: $subscription_status_info = $this->model_localisation_subscription_status->getSubscriptionStatus($subscription_info['subscription_status_id']);
207:
208: if ($subscription_status_info) {
209: $data['subscription_status'] = $subscription_status_info['name'];
210: } else {
211: $data['subscription_status'] = '';
212: }
213:
214: $data['date_added'] = date($this->language->get('date_format_short'), strtotime($subscription_info['date_added']));
215:
216: // Payment Address
217: if ($subscription_info['payment_address_id']) {
218: $payment_address_id = $subscription_info['payment_address_id'];
219: } else {
220: $payment_address_id = 0;
221: }
222:
223: $this->load->model('account/address');
224:
225: $address_info = $this->model_account_address->getAddress($this->customer->getId(), $payment_address_id);
226:
227: if ($address_info) {
228: if ($address_info['address_format']) {
229: $format = $address_info['address_format'];
230: } else {
231: $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
232: }
233:
234: $find = [
235: '{firstname}',
236: '{lastname}',
237: '{company}',
238: '{address_1}',
239: '{address_2}',
240: '{city}',
241: '{postcode}',
242: '{zone}',
243: '{zone_code}',
244: '{country}'
245: ];
246:
247: $replace = [
248: 'firstname' => $address_info['firstname'],
249: 'lastname' => $address_info['lastname'],
250: 'company' => $address_info['company'],
251: 'address_1' => $address_info['address_1'],
252: 'address_2' => $address_info['address_2'],
253: 'city' => $address_info['city'],
254: 'postcode' => $address_info['postcode'],
255: 'zone' => $address_info['zone'],
256: 'zone_code' => $address_info['zone_code'],
257: 'country' => $address_info['country']
258: ];
259:
260: $pattern_1 = [
261: "\r\n",
262: "\r",
263: "\n"
264: ];
265:
266: $pattern_2 = [
267: "/\\s\\s+/",
268: "/\r\r+/",
269: "/\n\n+/"
270: ];
271:
272: $data['payment_address'] = str_replace($pattern_1, '<br/>', preg_replace($pattern_2, '<br/>', trim(str_replace($find, $replace, $format))));
273: } else {
274: $data['payment_address'] = '';
275: }
276:
277: // Shipping Address
278: if ($subscription_info['shipping_address_id']) {
279: $shipping_address_id = $subscription_info['shipping_address_id'];
280: } else {
281: $shipping_address_id = 0;
282: }
283:
284: $this->load->model('account/address');
285:
286: $address_info = $this->model_account_address->getAddress($this->customer->getId(), $shipping_address_id);
287:
288: if ($address_info) {
289: if ($address_info['address_format']) {
290: $format = $address_info['address_format'];
291: } else {
292: $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
293: }
294:
295: $find = [
296: '{firstname}',
297: '{lastname}',
298: '{company}',
299: '{address_1}',
300: '{address_2}',
301: '{city}',
302: '{postcode}',
303: '{zone}',
304: '{zone_code}',
305: '{country}'
306: ];
307:
308: $replace = [
309: 'firstname' => $address_info['firstname'],
310: 'lastname' => $address_info['lastname'],
311: 'company' => $address_info['company'],
312: 'address_1' => $address_info['address_1'],
313: 'address_2' => $address_info['address_2'],
314: 'city' => $address_info['city'],
315: 'postcode' => $address_info['postcode'],
316: 'zone' => $address_info['zone'],
317: 'zone_code' => $address_info['zone_code'],
318: 'country' => $address_info['country']
319: ];
320:
321: $pattern_1 = [
322: "\r\n",
323: "\r",
324: "\n"
325: ];
326:
327: $pattern_2 = [
328: "/\\s\\s+/",
329: "/\r\r+/",
330: "/\n\n+/"
331: ];
332:
333: $data['shipping_address'] = str_replace($pattern_1, '<br/>', preg_replace($pattern_2, '<br/>', trim(str_replace($find, $replace, $format))));
334: } else {
335: $data['shipping_address'] = '';
336: }
337:
338: if ($subscription_info['shipping_method']) {
339: $data['shipping_method'] = $subscription_info['shipping_method']['name'];
340: } else {
341: $data['shipping_method'] = '';
342: }
343:
344: if ($subscription_info['payment_method']) {
345: $data['payment_method'] = $subscription_info['payment_method']['name'];
346: } else {
347: $data['payment_method'] = '';
348: }
349:
350: $this->load->model('catalog/product');
351:
352: $product_info = $this->model_catalog_product->getProduct($subscription_info['product_id']);
353:
354: if ($product_info) {
355: $data['name'] = $product_info['name'];
356: } else {
357: $data['name'] = '';
358: }
359:
360: $data['quantity'] = $subscription_info['quantity'];
361:
362: $currency_info = $this->model_localisation_currency->getCurrency($subscription_info['currency_id']);
363:
364: if ($currency_info) {
365: $currency = $currency_info['code'];
366: } else {
367: $currency = $this->config->get('config_currency');
368: }
369:
370: $this->load->model('localisation/subscription_status');
371:
372: $subscription_status_info = $this->model_localisation_subscription_status->getSubscriptionStatus($subscription_info['subscription_status_id']);
373:
374: if ($subscription_status_info) {
375: $data['subscription_status'] = $subscription_status_info['name'];
376: } else {
377: $data['subscription_status'] = '';
378: }
379:
380: $data['description'] = '';
381:
382: if ($subscription_info['trial_status']) {
383: $trial_price = $this->currency->format($this->tax->calculate($subscription_info['trial_price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $currency);
384: $trial_cycle = $subscription_info['trial_cycle'];
385: $trial_frequency = $this->language->get('text_' . $subscription_info['trial_frequency']);
386: $trial_duration = $subscription_info['trial_duration'];
387:
388: $data['description'] .= sprintf($this->language->get('text_subscription_trial'), $trial_price, $trial_cycle, $trial_frequency, $trial_duration);
389: }
390:
391: $price = $this->currency->format($this->tax->calculate($subscription_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $currency);
392: $cycle = $subscription_info['cycle'];
393: $frequency = $this->language->get('text_' . $subscription_info['frequency']);
394: $duration = $subscription_info['duration'];
395:
396: if ($duration) {
397: $data['description'] .= sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
398: } else {
399: $data['description'] .= sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
400: }
401:
402: // Orders
403: $data['history'] = $this->getHistory();
404: $data['order'] = $this->getOrder();
405:
406: //$data['order'] = $this->url->link('account/order.info', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&order_id=' . $subscription_info['order_id']);
407: $data['product'] = $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&product_id=' . $subscription_info['product_id']);
408:
409: $data['column_left'] = $this->load->controller('common/column_left');
410: $data['column_right'] = $this->load->controller('common/column_right');
411: $data['content_top'] = $this->load->controller('common/content_top');
412: $data['content_bottom'] = $this->load->controller('common/content_bottom');
413: $data['footer'] = $this->load->controller('common/footer');
414: $data['header'] = $this->load->controller('common/header');
415:
416: $this->response->setOutput($this->load->view('account/subscription_info', $data));
417: } else {
418: return new \Opencart\System\Engine\Action('error/not_found');
419: }
420:
421: return null;
422: }
423:
424: /**
425: * History
426: *
427: * @return void
428: */
429: public function history(): void {
430: $this->load->language('account/subscription');
431:
432: $this->response->setOutput($this->getHistory());
433: }
434:
435: /**
436: * Get History
437: *
438: * @return string
439: */
440: public function getHistory(): string {
441: if (isset($this->request->get['subscription_id'])) {
442: $subscription_id = (int)$this->request->get['subscription_id'];
443: } else {
444: $subscription_id = 0;
445: }
446:
447: if (isset($this->request->get['page']) && $this->request->get['route'] == 'account/subscription.history') {
448: $page = (int)$this->request->get['page'];
449: } else {
450: $page = 1;
451: }
452:
453: $limit = 10;
454:
455: $data['histories'] = [];
456:
457: $this->load->model('account/subscription');
458:
459: $results = $this->model_account_subscription->getHistories($subscription_id, ($page - 1) * $limit, $limit);
460:
461: foreach ($results as $result) {
462: $data['histories'][] = [
463: 'status' => $result['status'],
464: 'comment' => nl2br($result['comment']),
465: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
466: ];
467: }
468:
469: $subscription_total = $this->model_account_subscription->getTotalHistories($subscription_id);
470:
471: $data['pagination'] = $this->load->controller('common/pagination', [
472: 'total' => $subscription_total,
473: 'page' => $page,
474: 'limit' => $limit,
475: 'url' => $this->url->link('account/subscription.history', 'customer_token=' . $this->session->data['customer_token'] . '&subscription_id=' . $subscription_id . '&page={page}')
476: ]);
477:
478: $data['results'] = sprintf($this->language->get('text_pagination'), ($subscription_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($subscription_total - $limit)) ? $subscription_total : ((($page - 1) * $limit) + $limit), $subscription_total, ceil($subscription_total / $limit));
479:
480: return $this->load->view('account/subscription_history', $data);
481: }
482:
483: /**
484: * Order
485: *
486: * @return void
487: */
488: public function order(): void {
489: $this->load->language('account/subscription');
490:
491: $this->response->setOutput($this->getOrder());
492: }
493:
494: /**
495: * Get Order
496: *
497: * @return string
498: */
499: public function getOrder(): string {
500: if (isset($this->request->get['subscription_id'])) {
501: $subscription_id = (int)$this->request->get['subscription_id'];
502: } else {
503: $subscription_id = 0;
504: }
505:
506: if (isset($this->request->get['page']) && $this->request->get['route'] == 'account/subscription.order') {
507: $page = (int)$this->request->get['page'];
508: } else {
509: $page = 1;
510: }
511:
512: $limit = 10;
513:
514: $data['orders'] = [];
515:
516: $this->load->model('account/order');
517:
518: $results = $this->model_account_order->getOrdersBySubscriptionId($subscription_id, ($page - 1) * $limit, $limit);
519:
520: foreach ($results as $result) {
521: $data['orders'][] = [
522: 'order_id' => $result['order_id'],
523: 'status' => $result['status'],
524: 'total' => $this->currency->format($result['total'], $result['currency_code'], $result['currency_value']),
525: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
526: 'view' => $this->url->link('sale/subscription.order', 'customer_token=' . $this->session->data['customer_token'] . '&order_id=' . $result['order_id'] . '&page={page}')
527: ];
528: }
529:
530: $order_total = $this->model_account_order->getTotalOrdersBySubscriptionId($subscription_id);
531:
532: $data['pagination'] = $this->load->controller('common/pagination', [
533: 'total' => $order_total,
534: 'page' => $page,
535: 'limit' => $limit,
536: 'url' => $this->url->link('sale/subscription.order', 'customer_token=' . $this->session->data['customer_token'] . '&subscription_id=' . $subscription_id . '&page={page}')
537: ]);
538:
539: $data['results'] = sprintf($this->language->get('text_pagination'), ($order_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($order_total - $limit)) ? $order_total : ((($page - 1) * $limit) + $limit), $order_total, ceil($order_total / $limit));
540:
541: return $this->load->view('account/subscription_order', $data);
542: }
543: }
544: