1: <?php
2: namespace Opencart\Admin\Controller\Mail;
3: /**
4: * Class Subscription
5: *
6: * @package Opencart\Admin\Controller\Mail
7: */
8: class Subscription extends \Opencart\System\Engine\Controller {
9: // admin/controller/sale/subscription/addHistory/after
10: /**
11: * History
12: *
13: * @param string $route
14: * @param array<int, mixed> $args
15: * @param mixed $output
16: *
17: * @throws \Exception
18: *
19: * @return void
20: */
21: public function history(string &$route, array &$args, &$output): void {
22: if (isset($args[0])) {
23: $subscription_id = $args[0];
24: } else {
25: $subscription_id = 0;
26: }
27:
28: if (isset($args[1])) {
29: $subscription_status_id = $args[1];
30: } else {
31: $subscription_status_id = 0;
32: }
33:
34: if (isset($args[2])) {
35: $comment = $args[2];
36: } else {
37: $comment = '';
38: }
39:
40: if (isset($args[3])) {
41: $notify = $args[3];
42: } else {
43: $notify = '';
44: }
45:
46: // Subscription
47: $this->load->model('sale/subscription');
48:
49: $filter_data = [
50: 'filter_subscription_id' => $subscription_id,
51: 'filter_subscription_status_id' => $subscription_status_id,
52: 'filter_date_next' => date('Y-m-d H:i:s')
53: ];
54:
55: $subscriptions = $this->model_checkout_subscription->getSubscriptions($filter_data);
56:
57: if ($subscriptions) {
58: foreach ($subscriptions as $subscription) {
59: // Subscription histories
60: $history_total = $this->model_sale_subscription->getTotalHistoriesBySubscriptionStatusId($subscription_status_id);
61:
62: // The charge() method handles the subscription statuses in the cron/subscription
63: // controller from the catalog whereas an extension needs to return the active subscription status
64: if ($history_total && $subscription['subscription_status_id'] == $subscription_status_id) {
65: // Subscription Statuses
66: $this->load->model('localisation/subscription_status');
67:
68: $subscription_status_info = $this->model_localisation_subscription_status->getSubscriptionStatus($subscription_status_id);
69:
70: if ($subscription_status_info) {
71: // Customer payment
72: $customer_payment_info = $this->model_sale_subscription->getSubscriptions(['filter_customer_id' => $subscription['customer_id'], 'filter_customer_payment_id' => $subscription['customer_payment_id']]);
73:
74: if ($customer_payment_info) {
75: // Customers
76: $this->load->model('customer/customer');
77:
78: // Since the customer payment is integrated into the customer/customer page,
79: // we need to gather the customer's information rather than the order
80: $customer_info = $this->model_customer_customer->getCustomer($subscription['customer_id']);
81:
82: if ($customer_info) {
83: // Settings
84: $this->load->model('setting/setting');
85:
86: // Store
87: $store_info = $this->model_setting_setting->getSetting('config', $customer_info['store_id']);
88:
89: if ($store_info) {
90: $from = $store_info['config_email'];
91: $store_name = $store_info['config_name'];
92: $store_url = $store_info['config_url'];
93: $alert_email = $store_info['config_mail_alert_email'];
94: } else {
95: $from = $this->config->get('config_email');
96: $store_name = $this->config->get('config_name');
97: $store_url = HTTP_CATALOG;
98: $alert_email = $this->config->get('config_mail_alert_email');
99: }
100:
101: // Languages
102: $this->load->model('localisation/language');
103:
104: $language_info = $this->model_localisation_language->getLanguage($customer_info['language_id']);
105:
106: if ($language_info) {
107: if ($comment && $notify) {
108: $data['comment'] = nl2br($comment);
109: } else {
110: $data['comment'] = '';
111: }
112:
113: $data['subscription_status'] = $subscription_status_info['name'];
114:
115: // Languages
116: $this->load->model('localisation/language');
117:
118: $language_info = $this->model_localisation_language->getLanguage($customer_info['language_id']);
119:
120: if ($language_info) {
121: $language_code = $language_info['code'];
122: } else {
123: $language_code = $this->config->get('config_language');
124: }
125:
126: // Load the language for any mails using a different country code and prefixing it so it does not pollute the main data pool.
127: $this->load->language('default', 'mail', $language_code);
128: $this->load->language('mail/subscription', 'mail', $language_code);
129:
130: $data['date_added'] = date($this->language->get('mail_date_format_short'), $subscription['date_added']);
131:
132: // Text
133: $data['text_comment'] = $this->language->get('mail_text_comment');
134: $data['text_date_added'] = $this->language->get('mail_text_date_added');
135: $data['text_footer'] = $this->language->get('mail_text_footer');
136: $data['text_subscription_status'] = $this->language->get('mail_text_subscription_status');
137:
138: if ($this->config->get('config_mail_engine')) {
139: $mail_option = [
140: 'parameter' => $this->config->get('config_mail_parameter'),
141: 'smtp_hostname' => $this->config->get('config_mail_smtp_hostname'),
142: 'smtp_username' => $this->config->get('config_mail_smtp_username'),
143: 'smtp_password' => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
144: 'smtp_port' => $this->config->get('config_mail_smtp_port'),
145: 'smtp_timeout' => $this->config->get('config_mail_smtp_timeout')
146: ];
147: $mail = new \Opencart\System\Library\Mail($this->config->get('config_mail_engine'), $mail_option);
148:
149: $mail->setTo($customer_info['email']);
150: $mail->setFrom($from);
151: $mail->setSender(html_entity_decode($store_name, ENT_QUOTES, 'UTF-8'));
152: $mail->setSubject(html_entity_decode(sprintf($this->language->get('mail_text_subject'), $store_name), ENT_QUOTES, 'UTF-8'));
153: $mail->setText($this->load->view('mail/subscription_history', $data));
154: $mail->send();
155: }
156: }
157: }
158: }
159: }
160: }
161: }
162: }
163: }
164:
165: // admin/controller/sale/subscription/addTransaction/after
166:
167: /**
168: * Transaction
169: *
170: * @param string $route
171: * @param array<int, mixed> $args
172: * @param mixed $output
173: *
174: * @throws \Exception
175: *
176: * @return void
177: */
178: public function transaction(string &$route, array &$args, &$output): void {
179: if (isset($args[0])) {
180: $subscription_id = $args[0];
181: } else {
182: $subscription_id = 0;
183: }
184:
185: if (isset($args[1])) {
186: $order_id = $args[1];
187: } else {
188: $order_id = 0;
189: }
190:
191: if (isset($args[2])) {
192: $comment = $args[2];
193: } else {
194: $comment = '';
195: }
196:
197: if (isset($args[3])) {
198: $amount = $args[3];
199: } else {
200: $amount = '';
201: }
202:
203: if (isset($args[4])) {
204: $type = $args[4];
205: } else {
206: $type = '';
207: }
208:
209: if (isset($args[5])) {
210: $payment_method = $args[5];
211: } else {
212: $payment_method = '';
213: }
214:
215: if (isset($args[6])) {
216: $payment_code = $args[6];
217: } else {
218: $payment_code = '';
219: }
220:
221: // Subscription
222: $this->load->model('sale/subscription');
223:
224: $filter_data = [
225: 'filter_subscription_id' => $subscription_id,
226: 'filter_subscription_status_id' => $this->config->get('config_subscription_canceled_status_id'),
227: 'filter_date_next' => date('Y-m-d H:i:s')
228: ];
229:
230: $subscriptions = $this->model_checkout_subscription->getSubscriptions($filter_data);
231:
232: if ($subscriptions) {
233: $this->load->model('customer/customer');
234:
235: foreach ($subscriptions as $subscription) {
236: $transaction_total = $this->model_customer_customer->getTotalTransactionsByOrderId($subscription['order_id']);
237:
238: if ($transaction_total) {
239: // Orders
240: $this->load->model('sale/order');
241:
242: $order_info = $this->model_sale_order->getOrder($order_id);
243:
244: // In this case, since we're canceling a subscription,
245: // the order ID needs to be identical
246: if ($order_info && $subscription['order_id'] == $order_info['order_id']) {
247: // Same for the payment method
248: if ($order_info['payment_method'] == $subscription['payment_method'] && $subscription['payment_method'] == $payment_method) {
249: // Same for the payment code
250: if ($order_info['payment_code'] == $subscription['payment_code'] && $subscription['payment_code'] == $payment_code) {
251: $this->load->language('mail/subscription');
252:
253: // Store
254: $from = $this->config->get('config_email');
255: $store_name = $this->config->get('config_name');
256: $store_url = HTTP_CATALOG;
257: $alert_email = $this->config->get('config_mail_alert_email');
258:
259: if ($comment) {
260: $data['comment'] = nl2br($comment);
261: } else {
262: $data['comment'] = '';
263: }
264:
265: $data['subscription_id'] = $subscription_id;
266: $data['payment_method'] = $payment_method;
267: $data['payment_code'] = $payment_code;
268:
269: $data['date_added'] = date($this->language->get('date_format_short'), $subscription['date_added']);
270:
271: if ($this->config->get('config_mail_engine')) {
272: $mail_option = [
273: 'parameter' => $this->config->get('config_mail_parameter'),
274: 'smtp_hostname' => $this->config->get('config_mail_smtp_hostname'),
275: 'smtp_username' => $this->config->get('config_mail_smtp_username'),
276: 'smtp_password' => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
277: 'smtp_port' => $this->config->get('config_mail_smtp_port'),
278: 'smtp_timeout' => $this->config->get('config_mail_smtp_timeout')
279: ];
280: $mail = new \Opencart\System\Library\Mail($this->config->get('config_mail_engine'), $mail_option);
281:
282: $mail->setTo($from);
283: $mail->setFrom($from);
284: $mail->setSender(html_entity_decode($store_name, ENT_QUOTES, 'UTF-8'));
285: $mail->setSubject(html_entity_decode(sprintf($this->language->get('text_subject'), $store_name), ENT_QUOTES, 'UTF-8'));
286: $mail->setText($this->load->view('mail/subscription_canceled', $data));
287: $mail->send();
288: }
289: }
290: }
291: }
292: }
293: }
294: }
295: }
296: }
297: