· 6 years ago · Dec 10, 2019, 08:54 AM
1<?php
2/**
3 * 2007-2015 PrestaShop.
4 *
5 * NOTICE OF LICENSE
6 *
7 * This source file is subject to the Academic Free License (AFL 3.0)
8 * that is bundled with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://opensource.org/licenses/afl-3.0.php
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@prestashop.com so we can send you a copy immediately.
14 *
15 * DISCLAIMER
16 *
17 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18 * versions in the future. If you wish to customize PrestaShop for your
19 * needs please refer to http://www.prestashop.com for more information.
20 *
21 * @author PrestaShop SA <contact@prestashop.com>
22 * @copyright 2007-2015 PrestaShop SA
23 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
24 * International Registered Trademark & Property of PrestaShop SA
25 */
26
27if (!defined('_CAN_LOAD_FILES_')) {
28 exit;
29}
30
31include_once dirname(__FILE__).'/MailAlert.php';
32
33class Ps_EmailAlerts extends Module
34{
35 protected $html = '';
36
37 protected $merchant_mails;
38 protected $merchant_order;
39 protected $merchant_oos;
40 protected $customer_qty;
41 protected $merchant_coverage;
42 protected $product_coverage;
43 protected $order_edited;
44 protected $return_slip;
45
46 const __MA_MAIL_DELIMITOR__ = "\n";
47
48 public function __construct()
49 {
50 $this->name = 'ps_emailalerts';
51 $this->tab = 'administration';
52 $this->version = '2.1.1';
53 $this->author = 'PrestaShop';
54 $this->need_instance = 0;
55
56 $this->controllers = array('account');
57
58 $this->bootstrap = true;
59 parent::__construct();
60
61 if ($this->id) {
62 $this->init();
63 }
64
65 $this->displayName = $this->trans('Mail alerts', array(), 'Modules.Mailalerts.Admin');
66 $this->description = $this->trans('Sends e-mail notifications to customers and merchants regarding stock and order modifications.', array(), 'Modules.Mailalerts.Admin');
67 $this->ps_versions_compliancy = [
68 'min' => '1.7.1.0',
69 'max' => _PS_VERSION_,
70 ];
71 }
72
73 protected function init()
74 {
75 $this->merchant_mails = str_replace(',', self::__MA_MAIL_DELIMITOR__, (string) Configuration::get('MA_MERCHANT_MAILS'));
76 $this->merchant_order = (int) Configuration::get('MA_MERCHANT_ORDER');
77 $this->merchant_oos = (int) Configuration::get('MA_MERCHANT_OOS');
78 $this->customer_qty = (int) Configuration::get('MA_CUSTOMER_QTY');
79 $this->merchant_coverage = (int) Configuration::getGlobalValue('MA_MERCHANT_COVERAGE');
80 $this->product_coverage = (int) Configuration::getGlobalValue('MA_PRODUCT_COVERAGE');
81 $this->order_edited = (int) Configuration::getGlobalValue('MA_ORDER_EDIT');
82 $this->return_slip = (int) Configuration::getGlobalValue('MA_RETURN_SLIP');
83 }
84
85 public function install($delete_params = true)
86 {
87 if (!parent::install() ||
88 !$this->registerHook('actionValidateOrder') ||
89 !$this->registerHook('actionUpdateQuantity') ||
90 !$this->registerHook('displayProductButtons') ||
91 !$this->registerHook('displayCustomerAccount') ||
92 !$this->registerHook('displayMyAccountBlock') ||
93 !$this->registerHook('actionProductDelete') ||
94 !$this->registerHook('actionProductAttributeDelete') ||
95 !$this->registerHook('actionProductAttributeUpdate') ||
96 !$this->registerHook('actionProductCoverage') ||
97 !$this->registerHook('actionProductOutOfStock') ||
98 !$this->registerHook('actionOrderReturn') ||
99 !$this->registerHook('actionOrderEdited') ||
100 !$this->registerHook('registerGDPRConsent') ||
101 !$this->registerHook('actionDeleteGDPRCustomer') ||
102 !$this->registerHook('actionExportGDPRData') ||
103 !$this->registerHook('displayProductAdditionalInfo') ||
104 !$this->registerHook('displayHeader')) {
105 return false;
106 }
107
108 if ($delete_params) {
109 Configuration::updateValue('MA_MERCHANT_ORDER', 1);
110 Configuration::updateValue('MA_MERCHANT_OOS', 1);
111 Configuration::updateValue('MA_CUSTOMER_QTY', 1);
112 Configuration::updateValue('MA_ORDER_EDIT', 1);
113 Configuration::updateValue('MA_RETURN_SLIP', 1);
114 Configuration::updateValue('MA_MERCHANT_MAILS', Configuration::get('PS_SHOP_EMAIL'));
115 Configuration::updateValue('MA_LAST_QTIES', (int) Configuration::get('PS_LAST_QTIES'));
116 Configuration::updateGlobalValue('MA_MERCHANT_COVERAGE', 0);
117 Configuration::updateGlobalValue('MA_PRODUCT_COVERAGE', 0);
118
119 $sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.MailAlert::$definition['table'].'`
120 (
121 `id_customer` int(10) unsigned NOT NULL,
122 `customer_email` varchar(128) NOT NULL,
123 `id_product` int(10) unsigned NOT NULL,
124 `id_product_attribute` int(10) unsigned NOT NULL,
125 `id_shop` int(10) unsigned NOT NULL,
126 `id_lang` int(10) unsigned NOT NULL,
127 PRIMARY KEY (`id_customer`,`customer_email`,`id_product`,`id_product_attribute`,`id_shop`)
128 ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
129
130 if (!Db::getInstance()->execute($sql)) {
131 return false;
132 }
133 }
134
135 return true;
136 }
137
138 public function uninstall($delete_params = true)
139 {
140 if ($delete_params) {
141 Configuration::deleteByName('MA_MERCHANT_ORDER');
142 Configuration::deleteByName('MA_MERCHANT_OOS');
143 Configuration::deleteByName('MA_CUSTOMER_QTY');
144 Configuration::deleteByName('MA_MERCHANT_MAILS');
145 Configuration::deleteByName('MA_LAST_QTIES');
146 Configuration::deleteByName('MA_MERCHANT_COVERAGE');
147 Configuration::deleteByName('MA_PRODUCT_COVERAGE');
148 Configuration::deleteByName('MA_ORDER_EDIT');
149 Configuration::deleteByName('MA_RETURN_SLIP');
150
151 if (!Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.MailAlert::$definition['table'])) {
152 return false;
153 }
154 }
155
156 return parent::uninstall();
157 }
158
159 public function reset()
160 {
161 if (!$this->uninstall(false)) {
162 return false;
163 }
164 if (!$this->install(false)) {
165 return false;
166 }
167
168 return true;
169 }
170
171 public function getContent()
172 {
173 $this->html = '';
174
175 $this->postProcess();
176
177 $this->html .= $this->renderForm();
178
179 return $this->html;
180 }
181
182 protected function postProcess()
183 {
184 $errors = array();
185
186 if (Tools::isSubmit('submitMailAlert')) {
187 if (!Configuration::updateValue('MA_CUSTOMER_QTY', (int) Tools::getValue('MA_CUSTOMER_QTY'))) {
188 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
189 } elseif (!Configuration::updateGlobalValue('MA_ORDER_EDIT', (int) Tools::getValue('MA_ORDER_EDIT'))) {
190 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
191 }
192 } elseif (Tools::isSubmit('submitMAMerchant')) {
193 $emails = (string) Tools::getValue('MA_MERCHANT_MAILS');
194
195 if (!$emails || empty($emails)) {
196 $errors[] = $this->trans('Please type one (or more) e-mail address', array(), 'Modules.Mailalerts.Admin');
197 } else {
198 $emails = str_replace(',', self::__MA_MAIL_DELIMITOR__, $emails);
199 $emails = explode(self::__MA_MAIL_DELIMITOR__, $emails);
200 foreach ($emails as $k => $email) {
201 $email = trim($email);
202 if (!empty($email) && !Validate::isEmail($email)) {
203 $errors[] = $this->trans('Invalid e-mail:', array(), 'Modules.Mailalerts.Admin').' '.Tools::safeOutput($email);
204 break;
205 } elseif (!empty($email) && count($email) > 0) {
206 $emails[$k] = $email;
207 } else {
208 unset($emails[$k]);
209 }
210 }
211
212 $emails = implode(self::__MA_MAIL_DELIMITOR__, $emails);
213
214 if (!Configuration::updateValue('MA_MERCHANT_MAILS', (string) $emails)) {
215 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
216 } elseif (!Configuration::updateValue('MA_MERCHANT_ORDER', (int) Tools::getValue('MA_MERCHANT_ORDER'))) {
217 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
218 } elseif (!Configuration::updateValue('MA_MERCHANT_OOS', (int) Tools::getValue('MA_MERCHANT_OOS'))) {
219 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
220 } elseif (!Configuration::updateValue('MA_LAST_QTIES', (int) Tools::getValue('MA_LAST_QTIES'))) {
221 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
222 } elseif (!Configuration::updateGlobalValue('MA_MERCHANT_COVERAGE', (int) Tools::getValue('MA_MERCHANT_COVERAGE'))) {
223 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
224 } elseif (!Configuration::updateGlobalValue('MA_PRODUCT_COVERAGE', (int) Tools::getValue('MA_PRODUCT_COVERAGE'))) {
225 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
226 } elseif (!Configuration::updateGlobalValue('MA_RETURN_SLIP', (int) Tools::getValue('MA_RETURN_SLIP'))) {
227 $errors[] = $this->trans('Cannot update settings', array(), 'Modules.Mailalerts.Admin');
228 }
229 }
230 }
231
232 if (count($errors) > 0) {
233 $this->html .= $this->displayError(implode('<br />', $errors));
234 } else if (Tools::isSubmit('submitMailAlert') || Tools::isSubmit('submitMAMerchant')) {
235 $this->html .= $this->displayConfirmation($this->trans('Settings updated successfully', array(), 'Modules.Mailalerts.Admin'));
236 }
237
238 $this->init();
239 }
240
241 public function getAllMessages($id)
242 {
243 $messages = Db::getInstance()->executeS('
244 SELECT `message`
245 FROM `'._DB_PREFIX_.'message`
246 WHERE `id_order` = '.(int) $id.'
247 ORDER BY `id_message` ASC');
248 $result = array();
249 foreach ($messages as $message) {
250 $result[] = $message['message'];
251 }
252
253 return implode('<br/>', $result);
254 }
255
256 public function hookActionValidateOrder($params)
257 {
258 if (!$this->merchant_order || empty($this->merchant_mails)) {
259 return;
260 }
261
262 // Getting differents vars
263 $context = Context::getContext();
264 $id_lang = (int) $context->language->id;
265 $id_shop = (int) $context->shop->id;
266 $currency = $params['currency'];
267 $order = $params['order'];
268 $customer = $params['customer'];
269 $configuration = Configuration::getMultiple(
270 array(
271 'PS_SHOP_EMAIL',
272 'PS_MAIL_METHOD',
273 'PS_MAIL_SERVER',
274 'PS_MAIL_USER',
275 'PS_MAIL_PASSWD',
276 'PS_SHOP_NAME',
277 'PS_MAIL_COLOR',
278 ), $id_lang, null, $id_shop
279 );
280 $delivery = new Address((int) $order->id_address_delivery);
281 $invoice = new Address((int) $order->id_address_invoice);
282 $contatto = new Address((int) $order->id_address_contatto);
283 $order_date_text = Tools::displayDate($order->date_add);
284 $carrier = new Carrier((int) $order->id_carrier);
285 $message = $this->getAllMessages($order->id);
286
287 if (!$message || empty($message)) {
288 $message = $this->trans('No message', array(), 'Modules.Mailalerts.Admin');
289 }
290
291 $items_table = '';
292
293 $products = $params['order']->getProducts();
294 $customized_datas = Product::getAllCustomizedDatas((int) $params['cart']->id);
295 Product::addCustomizationPrice($products, $customized_datas);
296 foreach ($products as $key => $product) {
297 $unit_price = Product::getTaxCalculationMethod($customer->id) == PS_TAX_EXC ? $product['product_price'] : $product['product_price_wt'];
298
299 $customization_text = '';
300 if (isset($customized_datas[$product['product_id']][$product['product_attribute_id']])) {
301 foreach ($customized_datas[$product['product_id']][$product['product_attribute_id']][$order->id_address_delivery] as $customization) {
302 if (isset($customization['datas'][Product::CUSTOMIZE_TEXTFIELD])) {
303 foreach ($customization['datas'][Product::CUSTOMIZE_TEXTFIELD] as $text) {
304 $customization_text .= $text['name'].': '.$text['value'].'<br />';
305 }
306 }
307
308 if (isset($customization['datas'][Product::CUSTOMIZE_FILE])) {
309 $customization_text .= count($customization['datas'][Product::CUSTOMIZE_FILE]).' '.$this->trans('image(s)', array(), 'Modules.Mailalerts.Admin').'<br />';
310 }
311
312 $customization_text .= '---<br />';
313 }
314 if (method_exists('Tools', 'rtrimString')) {
315 $customization_text = Tools::rtrimString($customization_text, '---<br />');
316 } else {
317 $customization_text = preg_replace('/---<br \/>$/', '', $customization_text);
318 }
319 }
320
321 $url = $context->link->getProductLink($product['product_id']);
322 $items_table .=
323 '<tr style="background-color:'.($key % 2 ? '#DDE2E6' : '#EBECEE').';">
324 <td style="padding:0.6em 0.4em;">'.$product['product_reference'].'</td>
325 <td style="padding:0.6em 0.4em;">
326 <strong><a href="'.$url.'">'.$product['product_name'].'</a>'
327 .(isset($product['attributes_small']) ? ' '.$product['attributes_small'] : '')
328 .(!empty($customization_text) ? '<br />'.$customization_text : '')
329 .'</strong>
330 </td>
331 <td style="padding:0.6em 0.4em; text-align:right;">'.Tools::displayPrice($unit_price, $currency, false).'</td>
332 <td style="padding:0.6em 0.4em; text-align:center;">'.(int) $product['product_quantity'].'</td>
333 <td style="padding:0.6em 0.4em; text-align:right;">'
334 .Tools::displayPrice(($unit_price * $product['product_quantity']), $currency, false)
335 .'</td>
336 </tr>';
337 }
338 foreach ($params['order']->getCartRules() as $discount) {
339 $items_table .=
340 '<tr style="background-color:#EBECEE;">
341 <td colspan="4" style="padding:0.6em 0.4em; text-align:right;">'.$this->trans('Voucher code:', array(), 'Modules.Mailalerts.Admin').' '.$discount['name'].'</td>
342 <td style="padding:0.6em 0.4em; text-align:right;">-'.Tools::displayPrice($discount['value'], $currency, false).'</td>
343 </tr>';
344 }
345 if ($delivery->id_state) {
346 $delivery_state = new State((int) $delivery->id_state);
347 }
348 if ($invoice->id_state) {
349 $invoice_state = new State((int) $invoice->id_state);
350 }
351 if ($contatto->id_state) {
352 $contatto_state = new State((int) $contatto->id_state);
353 }
354
355 if (Product::getTaxCalculationMethod($customer->id) == PS_TAX_EXC) {
356 $total_products = $order->getTotalProductsWithoutTaxes();
357 } else {
358 $total_products = $order->getTotalProductsWithTaxes();
359 }
360
361 $order_state = $params['orderStatus'];
362
363 $id_cart = $params['cart']->id;
364
365 // Query id_address_contatto
366 $rescontatto = Db::getInstance()->executeS('
367 SELECT `id_address_contatto` FROM `'._DB_PREFIX_.'cart`
368 WHERE `id_cart` = \''.pSQL($id_cart).'\'
369 ');
370
371 if($rescontatto){
372 $id_address_contatto = $rescontatto[0]['id_address_contatto'];
373 }else{
374 $id_address_contatto = 1;
375 }
376
377$smarty = new Smarty();
378$dati = array('id_address_contatto'=>$id_address_contatto);
379
380$smarty->assign('dati', $dati);
381
382 // Filling-in vars for email
383 $template_vars = array(
384 '{firstname}' => $customer->firstname,
385 '{lastname}' => $customer->lastname,
386 '{email}' => $customer->email,
387 '{company}' => $customer->company,
388 '{delivery_block_txt}' => MailAlert::getFormatedAddress($delivery, "\n"),
389 '{invoice_block_txt}' => MailAlert::getFormatedAddress($invoice, "\n"),
390 '{delivery_block_html}' => MailAlert::getFormatedAddress(
391 $delivery, '<br />', array(
392 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
393 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
394 )
395 ),
396 '{invoice_block_html}' => MailAlert::getFormatedAddress(
397 $invoice, '<br />', array(
398 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
399 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
400 )
401 ),
402 '{contatto_block_html}' => MailAlert::getFormatedAddress(
403 $contatto, '<br />', array(
404 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
405 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
406 )
407 ),
408 '{delivery_company}' => $delivery->company,
409 '{delivery_firstname}' => $delivery->firstname,
410 '{delivery_lastname}' => $delivery->lastname,
411 '{delivery_address1}' => $delivery->address1,
412 '{delivery_address2}' => $delivery->address2,
413 '{delivery_city}' => $delivery->city,
414 '{delivery_postal_code}' => $delivery->postcode,
415 '{delivery_country}' => $delivery->country,
416 '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
417 '{delivery_phone}' => $delivery->phone ? $delivery->phone : $delivery->phone_mobile,
418 '{delivery_other}' => $delivery->other,
419 '{invoice_company}' => $invoice->company,
420 '{invoice_firstname}' => $invoice->firstname,
421 '{invoice_lastname}' => $invoice->lastname,
422 '{invoice_address2}' => $invoice->address2,
423 '{invoice_address1}' => $invoice->address1,
424 '{invoice_city}' => $invoice->city,
425 '{invoice_postal_code}' => $invoice->postcode,
426 '{invoice_country}' => $invoice->country,
427 '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
428 '{invoice_phone}' => $invoice->phone ? $invoice->phone : $invoice->phone_mobile,
429 '{invoice_other}' => $invoice->other,
430 '{contatto_company}' => $contatto->company,
431 '{contatto_vat_number}' => $contatto->vat_number,
432 '{contatto_firstname}' => 'coooontttaaa',
433 '{contatto_lastname}' => $contatto->lastname,
434 '{contatto_address2}' => $contatto->address2,
435 '{contatto_address1}' => $contatto->address1,
436 '{contatto_city}' => $contatto->city,
437 '{contatto_postal_code}' => $contatto->postcode,
438 '{contatto_country}' => $contatto->country,
439 '{contatto_state}' => $contatto->id_state ? $contatto_state->name : '',
440 '{contatto_phone}' => ($contatto->phone) ? $contatto->phone : $contatto->phone_mobile,
441 '{contatto_other}' => $contatto->other,
442 '{order_name}' => $order->reference,
443 '{order_status}' => $order_state->name,
444 '{shop_name}' => $configuration['PS_SHOP_NAME'],
445 '{date}' => $order_date_text,
446 '{carrier}' => (($carrier->name == '0') ? $configuration['PS_SHOP_NAME'] : $carrier->name),
447 '{payment}' => Tools::substr($order->payment, 0, 32),
448 '{items}' => $items_table,
449 '{total_paid}' => Tools::displayPrice($order->total_paid, $currency),
450 '{total_products}' => Tools::displayPrice($total_products, $currency),
451 '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
452 '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
453 '{total_tax_paid}' => Tools::displayPrice(
454 ($order->total_products_wt - $order->total_products) + ($order->total_shipping_tax_incl - $order->total_shipping_tax_excl),
455 $currency,
456 false
457 ),
458 '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
459 '{currency}' => $currency->sign,
460 '{gift}' => (bool) $order->gift,
461 '{gift_message}' => $order->gift_message,
462 '{message}' => $message,
463 );
464
465 // Shop iso
466 $iso = Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT'));
467
468 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
469 $merchant_mails = explode(self::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
470 foreach ($merchant_mails as $merchant_mail) {
471 // Default language
472 $mail_id_lang = $id_lang;
473 $mail_iso = $iso;
474
475 // Use the merchant lang if he exists as an employee
476 $results = Db::getInstance()->executeS('
477 SELECT `id_lang` FROM `'._DB_PREFIX_.'employee`
478 WHERE `email` = \''.pSQL($merchant_mail).'\'
479 ');
480 if ($results) {
481 $user_iso = Language::getIsoById((int) $results[0]['id_lang']);
482 if ($user_iso) {
483 $mail_id_lang = (int) $results[0]['id_lang'];
484 $mail_iso = $user_iso;
485 }
486 }
487
488 $dir_mail = false;
489 if (file_exists(dirname(__FILE__).'/mails/'.$mail_iso.'/new_order.txt') &&
490 file_exists(dirname(__FILE__).'/mails/'.$mail_iso.'/new_order.html')) {
491 $dir_mail = dirname(__FILE__).'/mails/';
492 }
493
494 if (file_exists(_PS_MAIL_DIR_.$mail_iso.'/new_order.txt') &&
495 file_exists(_PS_MAIL_DIR_.$mail_iso.'/new_order.html')) {
496 $dir_mail = _PS_MAIL_DIR_;
497 }
498
499 $id_customer = $customer->id;
500 // Query per ricavare email agente
501 $querymail = Db::getInstance()->executeS('
502 SELECT `email` FROM `'._DB_PREFIX_.'employee`
503 JOIN `'._DB_PREFIX_.'portfolio_customer_employee` ON
504 '._DB_PREFIX_.'employee.id_employee = '._DB_PREFIX_.'portfolio_customer_employee.id_employee
505 WHERE '._DB_PREFIX_.'portfolio_customer_employee.id_customer = \''.pSQL($customer->id).'\'
506 ');
507
508
509
510 if($querymail){
511 $emailagente = $querymail[0]['email'];
512 }else{
513 $emailagente = 'SELECT `email` FROM `'._DB_PREFIX_.'employee`
514 JOIN `'._DB_PREFIX_.'portfolio_customer_employee` ON
515 '._DB_PREFIX_.'employee.id_employee = '._DB_PREFIX_.'portfolio_customer_employee.id_employee
516 WHERE '._DB_PREFIX_.'portfolio_customer_employee.id_customer = \''.pSQL($customer->id).'\'';
517 }
518
519
520
521
522 // Query update
523 $resupdate = Db::getInstance()->executeS('
524 UPDATE `'._DB_PREFIX_.'orders` SET `id_address_contatto` = '.$id_address_contatto.'
525 WHERE `id_order` = \''.$order->id.'\'
526 ');
527
528 if($resupdate){
529 $notupdate = 1;
530 }else{
531 $notupdate = 2;
532 }
533
534 //$template_vars['{id_address_contatto}'] = $id_address_contatto;
535
536 $emailmia = 'info@visioweb.it';
537
538
539 if ($dir_mail) {
540
541 Mail::Send(
542 $mail_id_lang,
543 'new_order',
544 sprintf(Mail::l('New order'.$template_vars['{contatto_firstname}'].' : #%d - %s', $mail_id_lang), $order->id, $order->reference),
545 $template_vars,
546 $emailmia,
547 null,
548 $configuration['PS_SHOP_EMAIL'],
549 $configuration['PS_SHOP_NAME'],
550 null,
551 null,
552 $dir_mail,
553 null,
554 $id_shop
555 );
556 Mail::Send(
557 $mail_id_lang,
558 'new_order',
559 sprintf(Mail::l('New order : #%d - %s', $mail_id_lang), $order->id, $order->reference),
560 $template_vars,
561 $merchant_mail,
562 null,
563 $configuration['PS_SHOP_EMAIL'],
564 $configuration['PS_SHOP_NAME'],
565 null,
566 null,
567 $dir_mail,
568 null,
569 $id_shop
570 );
571
572 Mail::Send(
573 $mail_id_lang,
574 'new_order',
575 sprintf(Mail::l('New order : #%d - %s', $mail_id_lang), $order->id, $order->reference),
576 $template_vars,
577 $emailagente,
578 null,
579 $configuration['PS_SHOP_EMAIL'],
580 $configuration['PS_SHOP_NAME'],
581 null,
582 null,
583 $dir_mail,
584 null,
585 $id_shop
586 );
587 }
588 }
589 }
590
591 public function hookDisplayProductAdditionalInfo($params)
592 {
593 if (0 < $params['product']['quantity'] ||
594 !$this->customer_qty ||
595 !Configuration::get('PS_STOCK_MANAGEMENT') ||
596 Product::isAvailableWhenOutOfStock($params['product']['out_of_stock']))
597 return;
598 $context = Context::getContext();
599 $id_product = (int)$params['product']['id'];
600 $id_product_attribute = $params['product']['id_product_attribute'];
601 $id_customer = (int)$context->customer->id;
602 if ((int)$context->customer->id <= 0)
603 $this->context->smarty->assign('email', 1);
604 elseif (MailAlert::customerHasNotification($id_customer, $id_product, $id_product_attribute, (int)$context->shop->id))
605 return;
606 $this->context->smarty->assign(
607 array(
608 'id_product' => $id_product,
609 'id_product_attribute' => $id_product_attribute,
610 'id_module' => $this->id
611 )
612 );
613 return $this->display(__FILE__, 'product.tpl');
614 }
615
616 public function hookActionUpdateQuantity($params)
617 {
618 $id_product = (int) $params['id_product'];
619 $id_product_attribute = (int) $params['id_product_attribute'];
620
621 $quantity = (int) $params['quantity'];
622 $context = Context::getContext();
623 $id_shop = (int) $context->shop->id;
624 $id_lang = (int) $context->language->id;
625 $product = new Product($id_product, false, $id_lang, $id_shop, $context);
626 $product_has_attributes = $product->hasAttributes();
627 $configuration = Configuration::getMultiple(
628 array(
629 'MA_LAST_QTIES',
630 'PS_STOCK_MANAGEMENT',
631 'PS_SHOP_EMAIL',
632 'PS_SHOP_NAME',
633 ), null, null, $id_shop
634 );
635 $ma_last_qties = (int) $configuration['MA_LAST_QTIES'];
636
637 $check_oos = ($product_has_attributes && $id_product_attribute) || (!$product_has_attributes && !$id_product_attribute);
638
639 if ($check_oos &&
640 $product->active == 1 &&
641 (int) $quantity <= $ma_last_qties &&
642 !(!$this->merchant_oos || empty($this->merchant_mails)) &&
643 $configuration['PS_STOCK_MANAGEMENT']) {
644 $iso = Language::getIsoById($id_lang);
645 $product_name = Product::getProductName($id_product, $id_product_attribute, $id_lang);
646 $template_vars = array(
647 '{qty}' => $quantity,
648 '{last_qty}' => $ma_last_qties,
649 '{product}' => $product_name,
650 );
651
652 // Do not send mail if multiples product are created / imported.
653 if (!defined('PS_MASS_PRODUCT_CREATION') &&
654 file_exists(dirname(__FILE__).'/mails/'.$iso.'/productoutofstock.txt') &&
655 file_exists(dirname(__FILE__).'/mails/'.$iso.'/productoutofstock.html')) {
656 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
657 $merchant_mails = explode(self::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
658 foreach ($merchant_mails as $merchant_mail) {
659 Mail::Send(
660 $id_lang,
661 'productoutofstock',
662 Mail::l('Product out of stock', $id_lang),
663 $template_vars,
664 $merchant_mail,
665 null,
666 (string) $configuration['PS_SHOP_EMAIL'],
667 (string) $configuration['PS_SHOP_NAME'],
668 null,
669 null,
670 dirname(__FILE__).'/mails/',
671 false,
672 $id_shop
673 );
674 }
675 }
676 }
677
678 if ($this->customer_qty && $quantity > 0) {
679 MailAlert::sendCustomerAlert((int) $product->id, (int) $params['id_product_attribute']);
680 }
681 }
682
683 public function hookActionProductAttributeUpdate($params)
684 {
685 $sql = '
686 SELECT `id_product`, `quantity`
687 FROM `'._DB_PREFIX_.'stock_available`
688 WHERE `id_product_attribute` = '.(int) $params['id_product_attribute'];
689
690 $result = Db::getInstance()->getRow($sql);
691
692 if ($this->customer_qty && $result['quantity'] > 0) {
693 MailAlert::sendCustomerAlert((int) $result['id_product'], (int) $params['id_product_attribute']);
694 }
695 }
696
697 public function hookDisplayCustomerAccount($params)
698 {
699 return $this->customer_qty ? $this->display(__FILE__, 'my-account.tpl') : null;
700 }
701
702 public function hookDisplayMyAccountBlock($params)
703 {
704 return $this->customer_qty ? $this->display(__FILE__, 'my-account-footer.tpl') : null;
705 }
706
707 public function hookActionProductDelete($params)
708 {
709 $sql = '
710 DELETE FROM `'._DB_PREFIX_.MailAlert::$definition['table'].'`
711 WHERE `id_product` = '.(int) $params['product']->id;
712
713 Db::getInstance()->execute($sql);
714 }
715
716 public function hookActionAttributeDelete($params)
717 {
718 if ($params['deleteAllAttributes']) {
719 $sql = '
720 DELETE FROM `'._DB_PREFIX_.MailAlert::$definition['table'].'`
721 WHERE `id_product` = '.(int) $params['id_product'];
722 } else {
723 $sql = '
724 DELETE FROM `'._DB_PREFIX_.MailAlert::$definition['table'].'`
725 WHERE `id_product_attribute` = '.(int) $params['id_product_attribute'].'
726 AND `id_product` = '.(int) $params['id_product'];
727 }
728
729 Db::getInstance()->execute($sql);
730 }
731
732 public function hookActionProductCoverage($params)
733 {
734 // if not advanced stock management, nothing to do
735 if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
736 return;
737 }
738
739 // retrieves informations
740 $id_product = (int) $params['id_product'];
741 $id_product_attribute = (int) $params['id_product_attribute'];
742 $warehouse = $params['warehouse'];
743 $product = new Product($id_product);
744
745 if (!Validate::isLoadedObject($product)) {
746 return;
747 }
748
749 if (!$product->advanced_stock_management) {
750 return;
751 }
752
753 // sets warehouse id to get the coverage
754 if (!Validate::isLoadedObject($warehouse)) {
755 $id_warehouse = 0;
756 } else {
757 $id_warehouse = (int) $warehouse->id;
758 }
759
760 // coverage of the product
761 $warning_coverage = (int) Configuration::getGlobalValue('MA_PRODUCT_COVERAGE');
762
763 $coverage = StockManagerFactory::getManager()->getProductCoverage($id_product, $id_product_attribute, $warning_coverage, $id_warehouse);
764
765 // if we need to send a notification
766 if ($product->active == 1 &&
767 ($coverage < $warning_coverage) && !empty($this->merchant_mails) &&
768 Configuration::getGlobalValue('MA_MERCHANT_COVERAGE')) {
769 $context = Context::getContext();
770 $id_lang = (int) $context->language->id;
771 $id_shop = (int) $context->shop->id;
772 $iso = Language::getIsoById($id_lang);
773 $product_name = Product::getProductName($id_product, $id_product_attribute, $id_lang);
774 $template_vars = array(
775 '{current_coverage}' => $coverage,
776 '{warning_coverage}' => $warning_coverage,
777 '{product}' => pSQL($product_name),
778 );
779
780 if (file_exists(dirname(__FILE__).'/mails/'.$iso.'/productcoverage.txt') &&
781 file_exists(dirname(__FILE__).'/mails/'.$iso.'/productcoverage.html')) {
782 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
783 $merchant_mails = explode(self::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
784 foreach ($merchant_mails as $merchant_mail) {
785 Mail::Send(
786 $id_lang,
787 'productcoverage',
788 Mail::l('Stock coverage', $id_lang),
789 $template_vars,
790 $merchant_mail,
791 null,
792 (string) Configuration::get('PS_SHOP_EMAIL'),
793 (string) Configuration::get('PS_SHOP_NAME'),
794 null,
795 null,
796 dirname(__FILE__).'/mails/',
797 null,
798 $id_shop
799 );
800 }
801 }
802 }
803 }
804
805 public function hookDisplayHeader()
806 {
807 $this->page_name = Dispatcher::getInstance()->getController();
808 if (in_array($this->page_name, array('product', 'account'))) {
809 $this->context->controller->addJS($this->_path.'js/mailalerts.js');
810 $this->context->controller->addCSS($this->_path.'css/mailalerts.css', 'all');
811 }
812 }
813
814 /**
815 * Send a mail when a customer return an order.
816 *
817 * @param array $params Hook params
818 */
819 public function hookActionOrderReturn($params)
820 {
821 if (!$this->return_slip || empty($this->return_slip)) {
822 return;
823 }
824
825 $context = Context::getContext();
826 $id_lang = (int) $context->language->id;
827 $id_shop = (int) $context->shop->id;
828 $configuration = Configuration::getMultiple(
829 array(
830 'PS_SHOP_EMAIL',
831 'PS_MAIL_METHOD',
832 'PS_MAIL_SERVER',
833 'PS_MAIL_USER',
834 'PS_MAIL_PASSWD',
835 'PS_SHOP_NAME',
836 'PS_MAIL_COLOR',
837 ), $id_lang, null, $id_shop
838 );
839
840 // Shop iso
841 $iso = Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT'));
842
843 $order = new Order((int) $params['orderReturn']->id_order);
844 $customer = new Customer((int) $params['orderReturn']->id_customer);
845 $delivery = new Address((int) $order->id_address_delivery);
846 $invoice = new Address((int) $order->id_address_invoice);
847 $contatto = new Address((int) $order->id_address_contatto);
848 $order_date_text = Tools::displayDate($order->date_add);
849 if ($delivery->id_state) {
850 $delivery_state = new State((int) $delivery->id_state);
851 }
852 if ($invoice->id_state) {
853 $invoice_state = new State((int) $invoice->id_state);
854 }
855 if ($contatto->id_state) {
856 $contatto_state = new State((int) $contatto->id_state);
857 }
858
859 $order_return_products = OrderReturn::getOrdersReturnProducts($params['orderReturn']->id, $order);
860
861 $items_table = '';
862 foreach ($order_return_products as $key => $product) {
863 $url = $context->link->getProductLink($product['product_id']);
864 $items_table .=
865 '<tr style="background-color:'.($key % 2 ? '#DDE2E6' : '#EBECEE').';">
866 <td style="padding:0.6em 0.4em;">'.$product['product_reference'].'</td>
867 <td style="padding:0.6em 0.4em;">
868 <strong><a href="'.$url.'">'.$product['product_name'].'</a>
869 </strong>
870 </td>
871 <td style="padding:0.6em 0.4em; text-align:center;">'.(int) $product['product_quantity'].'</td>
872 </tr>';
873 }
874
875
876
877
878 $template_vars = array(
879 '{firstname}' => $customer->firstname,
880 '{lastname}' => $customer->lastname,
881 '{email}' => $customer->email,
882 '{delivery_block_txt}' => MailAlert::getFormatedAddress($delivery, "\n"),
883 '{invoice_block_txt}' => MailAlert::getFormatedAddress($invoice, "\n"),
884 '{delivery_block_html}' => MailAlert::getFormatedAddress(
885 $delivery, '<br />ciao', array(
886 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
887 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
888 )
889 ),
890 '{invoice_block_html}' => MailAlert::getFormatedAddress(
891 $invoice, '<br />', array(
892 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
893 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
894 )
895 ),
896 '{contatto_block_html}' => MailAlert::getFormatedAddress(
897 $contatto, '<br />', array(
898 'firstname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
899 'lastname' => '<span style="color:'.$configuration['PS_MAIL_COLOR'].'; font-weight:bold;">%s</span>',
900 )
901 ),
902 '{delivery_company}' => $delivery->company,
903 '{delivery_firstname}' => $delivery->firstname,
904 '{delivery_lastname}' => $delivery->lastname,
905 '{delivery_address1}' => $delivery->address1,
906 '{delivery_address2}' => $delivery->address2,
907 '{delivery_city}' => $delivery->city,
908 '{delivery_postal_code}' => $delivery->postcode,
909 '{delivery_country}' => $delivery->country,
910 '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
911 '{delivery_phone}' => $delivery->phone ? $delivery->phone : $delivery->phone_mobile,
912 '{delivery_other}' => $delivery->other,
913 '{invoice_company}' => $invoice->company,
914 '{invoice_firstname}' => $invoice->firstname,
915 '{invoice_lastname}' => $invoice->lastname,
916 '{invoice_address2}' => $invoice->address2,
917 '{invoice_address1}' => $invoice->address1,
918 '{invoice_city}' => $invoice->city,
919 '{invoice_postal_code}' => $invoice->postcode,
920 '{invoice_country}' => $invoice->country,
921 '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
922 '{invoice_phone}' => $invoice->phone ? $invoice->phone : $invoice->phone_mobile,
923 '{invoice_other}' => $invoice->other,
924 '{contatto_company}' => $contatto->company,
925 '{contatto_firstname}' => 'ciaaaaooo',
926 '{contatto_lastname}' => $contatto->lastname,
927 '{contatto_address2}' => $contatto->address2,
928 '{contatto_address1}' => $contatto->address1,
929 '{contatto_city}' => $contatto->city,
930 '{contatto_postal_code}' => $contatto->postcode,
931 '{contatto_country}' => $contatto->country,
932 '{contatto_state}' => $contatto->id_state ? $contatto_state->name : '',
933 '{contatto_phone}' => $contatto->phone ? $contatto->phone : $contatto->phone_mobile,
934 '{contatto_other}' => $contatto->other,
935 '{order_name}' => $order->reference,
936 '{shop_name}' => $configuration['PS_SHOP_NAME'],
937 '{date}' => $order_date_text,
938 '{items}' => $items_table,
939 '{message}' => Tools::purifyHTML($params['orderReturn']->question),
940 );
941
942 // Send 1 email by merchant mail, because Mail::Send doesn't work with an array of recipients
943 $merchant_mails = explode(self::__MA_MAIL_DELIMITOR__, $this->merchant_mails);
944 foreach ($merchant_mails as $merchant_mail) {
945 // Default language
946 $mail_id_lang = $id_lang;
947 $mail_iso = $iso;
948
949 // Use the merchant lang if he exists as an employee
950 $results = Db::getInstance()->executeS('
951 SELECT `id_lang` FROM `'._DB_PREFIX_.'employee`
952 WHERE `email` = \''.pSQL($merchant_mail).'\'
953 ');
954 if ($results) {
955 $user_iso = Language::getIsoById((int) $results[0]['id_lang']);
956 if ($user_iso) {
957 $mail_id_lang = (int) $results[0]['id_lang'];
958 $mail_iso = $user_iso;
959 }
960 }
961
962 $dir_mail = false;
963 if (file_exists(dirname(__FILE__).'/mails/'.$mail_iso.'/return_slip.txt') &&
964 file_exists(dirname(__FILE__).'/mails/'.$mail_iso.'/return_slip.html')) {
965 $dir_mail = dirname(__FILE__).'/mails/';
966 }
967
968 if (file_exists(_PS_MAIL_DIR_.$mail_iso.'/return_slip.txt') &&
969 file_exists(_PS_MAIL_DIR_.$mail_iso.'/return_slip.html')) {
970 $dir_mail = _PS_MAIL_DIR_;
971 }
972
973
974 if ($dir_mail) {
975 Mail::Send(
976 $mail_id_lang,
977 'return_slip',
978 sprintf(Mail::l('New return from order #%d - %s', $mail_id_lang), $order->id, $order->reference),
979 $template_vars,
980 $merchant_mail,
981 null,
982 $configuration['PS_SHOP_EMAIL'],
983 $configuration['PS_SHOP_NAME'],
984 null,
985 null,
986 $dir_mail,
987 null,
988 $id_shop
989 );
990 }
991 }
992 }
993
994 /**
995 * Send a mail when an order is modified.
996 *
997 * @param array $params Hook params
998 */
999 public function hookActionOrderEdited($params)
1000 {
1001 if (!$this->order_edited || empty($this->order_edited)) {
1002 return;
1003 }
1004
1005 $order = $params['order'];
1006
1007 $data = array(
1008 '{lastname}' => $order->getCustomer()->lastname,
1009 '{firstname}' => $order->getCustomer()->firstname,
1010 '{id_order}' => (int) $order->id,
1011 '{order_name}' => $order->getUniqReference(),
1012 );
1013
1014 Mail::Send(
1015 (int) $order->id_lang,
1016 'order_changed',
1017 Mail::l('Your order has been changed', (int) $order->id_lang),
1018 $data,
1019 $order->getCustomer()->email,
1020 $order->getCustomer()->firstname.' '.$order->getCustomer()->lastname,
1021 null, null, null, null, _PS_MAIL_DIR_, true, (int) $order->id_shop);
1022 }
1023
1024 public function renderForm()
1025 {
1026 $fields_form_1 = array(
1027 'form' => array(
1028 'legend' => array(
1029 'title' => $this->trans('Customer notifications', array(), 'Modules.Mailalerts.Admin'),
1030 'icon' => 'icon-cogs',
1031 ),
1032 'input' => array(
1033 array(
1034 'type' => 'switch',
1035 'is_bool' => true, //retro compat 1.5
1036 'label' => $this->trans('Product availability', array(), 'Modules.Mailalerts.Admin'),
1037 'name' => 'MA_CUSTOMER_QTY',
1038 'desc' => $this->trans('Gives the customer the option of receiving a notification when an out-of-stock product is available again.', array(), 'Modules.Mailalerts.Admin'),
1039 'values' => array(
1040 array(
1041 'id' => 'active_on',
1042 'value' => 1,
1043 'label' => $this->trans('Enabled', array(), 'Admin.Global'),
1044 ),
1045 array(
1046 'id' => 'active_off',
1047 'value' => 0,
1048 'label' => $this->trans('Disabled', array(), 'Admin.Global'),
1049 ),
1050 ),
1051 ),
1052 array(
1053 'type' => 'switch',
1054 'is_bool' => true, //retro compat 1.5
1055 'label' => $this->trans('Order edit', array(), 'Modules.Mailalerts.Admin'),
1056 'name' => 'MA_ORDER_EDIT',
1057 'desc' => $this->trans('Send a notification to the customer when an order is edited.', array(), 'Modules.Mailalerts.Admin'),
1058 'values' => array(
1059 array(
1060 'id' => 'active_on',
1061 'value' => 1,
1062 'label' => $this->trans('Enabled', array(), 'Admin.Global'),
1063 ),
1064 array(
1065 'id' => 'active_off',
1066 'value' => 0,
1067 'label' => $this->trans('Disabled', array(), 'Admin.Global'),
1068 ),
1069 ),
1070 ),
1071 ),
1072 'submit' => array(
1073 'title' => $this->trans('Save', array(), 'Admin.Actions'),
1074 'class' => 'btn btn-default pull-right',
1075 'name' => 'submitMailAlert',
1076 ),
1077 ),
1078 );
1079
1080 $inputs = array(
1081 array(
1082 'type' => 'switch',
1083 'is_bool' => true, //retro compat 1.5
1084 'label' => $this->trans('New order', array(), 'Modules.Mailalerts.Admin'),
1085 'name' => 'MA_MERCHANT_ORDER',
1086 'desc' => $this->trans('Receive a notification when an order is placed.', array(), 'Modules.Mailalerts.Admin'),
1087 'values' => array(
1088 array(
1089 'id' => 'active_on',
1090 'value' => 1,
1091 'label' => $this->trans('Enabled', array(), 'Admin.Global'),
1092 ),
1093 array(
1094 'id' => 'active_off',
1095 'value' => 0,
1096 'label' => $this->trans('Disabled', array(), 'Admin.Global'),
1097 ),
1098 ),
1099 ),
1100 array(
1101 'type' => 'switch',
1102 'is_bool' => true, //retro compat 1.5
1103 'label' => $this->trans('Out of stock', array(), 'Modules.Mailalerts.Admin'),
1104 'name' => 'MA_MERCHANT_OOS',
1105 'desc' => $this->trans('Receive a notification if the available quantity of a product is below the following threshold.', array(), 'Modules.Mailalerts.Admin'),
1106 'values' => array(
1107 array(
1108 'id' => 'active_on',
1109 'value' => 1,
1110 'label' => $this->trans('Enabled', array(), 'Admin.Global'),
1111 ),
1112 array(
1113 'id' => 'active_off',
1114 'value' => 0,
1115 'label' => $this->trans('Disabled', array(), 'Admin.Global'),
1116 ),
1117 ),
1118 ),
1119 array(
1120 'type' => 'text',
1121 'label' => $this->trans('Threshold', array(), 'Modules.Mailalerts.Admin'),
1122 'name' => 'MA_LAST_QTIES',
1123 'class' => 'fixed-width-xs',
1124 'desc' => $this->trans('Quantity for which a product is considered out of stock.', array(), 'Modules.Mailalerts.Admin'),
1125 ),
1126 );
1127
1128 if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
1129 $inputs[] = array(
1130 'type' => 'switch',
1131 'is_bool' => true, //retro compat 1.5
1132 'label' => $this->trans('Coverage warning', array(), 'Modules.Mailalerts.Admin'),
1133 'name' => 'MA_MERCHANT_COVERAGE',
1134 'desc' => $this->trans('Receive a notification when a product has insufficient coverage.', array(), 'Modules.Mailalerts.Admin'),
1135 'values' => array(
1136 array(
1137 'id' => 'active_on',
1138 'value' => 1,
1139 'label' => $this->trans('Enabled', array(), 'Admin.Global'),
1140 ),
1141 array(
1142 'id' => 'active_off',
1143 'value' => 0,
1144 'label' => $this->trans('Disabled', array(), 'Admin.Global'),
1145 ),
1146 ),
1147 );
1148 $inputs[] = array(
1149 'type' => 'text',
1150 'label' => $this->trans('Coverage', array(), 'Modules.Mailalerts.Admin'),
1151 'name' => 'MA_PRODUCT_COVERAGE',
1152 'class' => 'fixed-width-xs',
1153 'desc' => $this->trans('Stock coverage, in days. Also, the stock coverage of a given product will be calculated based on this number.', array(), 'Modules.Mailalerts.Admin'),
1154 );
1155 }
1156
1157 $inputs[] = array(
1158 'type' => 'switch',
1159 'is_bool' => true, //retro compat 1.5
1160 'label' => $this->trans('Returns', array(), 'Modules.Mailalerts.Admin'),
1161 'name' => 'MA_RETURN_SLIP',
1162 'desc' => $this->trans('Receive a notification when a customer requests a merchandise return.', array(), 'Modules.Mailalerts.Admin'),
1163 'values' => array(
1164 array(
1165 'id' => 'active_on',
1166 'value' => 1,
1167 'label' => $this->trans('Enabled', array(), 'Admin.Global'),
1168 ),
1169 array(
1170 'id' => 'active_off',
1171 'value' => 0,
1172 'label' => $this->trans('Disabled', array(), 'Admin.Global'),
1173 ),
1174 ),
1175 );
1176 $inputs[] = array(
1177 'type' => 'textarea',
1178 'cols' => 36,
1179 'rows' => 4,
1180 'label' => $this->trans('E-mail addresses', array(), 'Modules.Mailalerts.Admin'),
1181 'name' => 'MA_MERCHANT_MAILS',
1182 'desc' => $this->trans('One e-mail address per line (e.g. bob@example.com).', array(), 'Modules.Mailalerts.Admin'),
1183 );
1184
1185 $fields_form_2 = array(
1186 'form' => array(
1187 'legend' => array(
1188 'title' => $this->trans('Merchant notifications', array(), 'Modules.Mailalerts.Admin'),
1189 'icon' => 'icon-cogs',
1190 ),
1191 'input' => $inputs,
1192 'submit' => array(
1193 'title' => $this->trans('Save', array(), 'Admin.Actions'),
1194 'class' => 'btn btn-default pull-right',
1195 'name' => 'submitMAMerchant',
1196 ),
1197 ),
1198 );
1199
1200 $helper = new HelperForm();
1201 $helper->show_toolbar = false;
1202 $helper->table = $this->table;
1203 $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
1204 $helper->default_form_language = $lang->id;
1205 $helper->module = $this;
1206 $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
1207 $helper->identifier = $this->identifier;
1208 $helper->submit_action = 'submitMailAlertConfiguration';
1209 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
1210 .'&configure='.$this->name
1211 .'&tab_module='.$this->tab
1212 .'&module_name='.$this->name;
1213 $helper->token = Tools::getAdminTokenLite('AdminModules');
1214 $helper->tpl_vars = array(
1215 'fields_value' => $this->getConfigFieldsValues(),
1216 'languages' => $this->context->controller->getLanguages(),
1217 'id_language' => $this->context->language->id,
1218 );
1219
1220 return $helper->generateForm(array($fields_form_1, $fields_form_2));
1221 }
1222
1223 public function hookActionDeleteGDPRCustomer($customer)
1224 {
1225 if (!empty($customer['email']) && Validate::isEmail($customer['email'])) {
1226 $sql = "DELETE FROM "._DB_PREFIX_."mailalert_customer_oos WHERE customer_email = '".pSQL($customer['email'])."'";
1227 if (Db::getInstance()->execute($sql)) {
1228 return json_encode(true);
1229 }
1230 return json_encode($this->l('Mail alert: Unable to delete customer using email.'));
1231 }
1232 }
1233
1234 public function hookActionExportGDPRData($customer)
1235 {
1236 if (!Tools::isEmpty($customer['email']) && Validate::isEmail($customer['email'])) {
1237 $sql = "SELECT * FROM "._DB_PREFIX_."mailalert_customer_oos WHERE customer_email = '".pSQL($customer['email'])."'";
1238 if ($res = Db::getInstance()->ExecuteS($sql)) {
1239 return json_encode($res);
1240 }
1241 return json_encode($this->l('Mail alert: Unable to export customer using email.'));
1242 }
1243 }
1244
1245 public function getConfigFieldsValues()
1246 {
1247 return array(
1248 'MA_CUSTOMER_QTY' => Tools::getValue('MA_CUSTOMER_QTY', Configuration::get('MA_CUSTOMER_QTY')),
1249 'MA_MERCHANT_ORDER' => Tools::getValue('MA_MERCHANT_ORDER', Configuration::get('MA_MERCHANT_ORDER')),
1250 'MA_MERCHANT_OOS' => Tools::getValue('MA_MERCHANT_OOS', Configuration::get('MA_MERCHANT_OOS')),
1251 'MA_LAST_QTIES' => Tools::getValue('MA_LAST_QTIES', Configuration::get('MA_LAST_QTIES')),
1252 'MA_MERCHANT_COVERAGE' => Tools::getValue('MA_MERCHANT_COVERAGE', Configuration::get('MA_MERCHANT_COVERAGE')),
1253 'MA_PRODUCT_COVERAGE' => Tools::getValue('MA_PRODUCT_COVERAGE', Configuration::get('MA_PRODUCT_COVERAGE')),
1254 'MA_MERCHANT_MAILS' => Tools::getValue('MA_MERCHANT_MAILS', Configuration::get('MA_MERCHANT_MAILS')),
1255 'MA_ORDER_EDIT' => Tools::getValue('MA_ORDER_EDIT', Configuration::get('MA_ORDER_EDIT')),
1256 'MA_RETURN_SLIP' => Tools::getValue('MA_RETURN_SLIP', Configuration::get('MA_RETURN_SLIP')),
1257 );
1258 }
1259}