* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7456 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) exit; class Tm4b extends Module { private $_html = ''; private $_postErrors = array(); private $_postSucess; private $_data; private $_password; private $_user; private $_originator; private $_route; private $_simulation; private $_new_order_numbers; private $_alert_new_order_active; private $_alert_update_quantity_active; private $_daily_report_active; public $token; const __TM4B_LOWBALANCE__ = '20'; const __TM4B_NUMBER_DELIMITOR__ = ','; static private $_tpl_sms_files = array( 'name' => array( 'new_orders' => 'sms_new_order', 'out_of_stock' => 'sms_out_of_stock' ), 'ext' => array( 'new_orders' => '.txt', 'out_of_stock' => '.txt' ) ); public function __construct() { $this->name = 'tm4b'; $this->displayName = 'SMS Tm4b'; $this->description = $this->l('Sends an SMS for each new order'); $this->tab = 'administration'; $this->version = 1.1; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); if ($this->id) { $this->_data = array('shopname' => Configuration::get('PS_SHOP_NAME')); /* Get config vars */ $this->_password = Configuration::get('TM4B_PASSWORD'); $this->_user = Configuration::get('TM4B_USER'); $this->_originator = Configuration::get('TM4B_ORIGINATOR'); $this->_route = Configuration::get('TM4B_ROUTE'); $this->_simulation = Configuration::get('TM4B_SIM'); $this->_new_order_numbers = Configuration::get('TM4B_NEW_ORDER_NUMBERS'); $this->_alert_new_order_active = Configuration::get('TM4B_ALERT_NO_ACTIVE'); $this->_alert_update_quantity_active = Configuration::get('TM4B_ALERT_UQ_ACTIVE'); $this->_daily_report_active = Configuration::get('TM4B_DAILY_REPORT_ACTIVE'); } $this->displayName = 'SMS Tm4b'; $this->description = $this->l('Sends an SMS for each new order'); $this->confirmUninstall = $this->l('Are you sure you want to delete your info?'); $this->token = md5('tm4b'._COOKIE_KEY_); } public function install() { if (!parent::install() OR !$this->registerHook('newOrder') OR !$this->registerHook('updateQuantity')) return false; Configuration::updateValue('TM4B_SIM', 1); Configuration::updateValue('TM4B_ALERT_NO_ACTIVE', 1); Configuration::updateValue('TM4B_ALERT_UQ_ACTIVE', 1); Configuration::updateValue('TM4B_DAILY_REPORT_ACTIVE', 0); return true; } public function uninstall() { Configuration::deleteByName('TM4B_PASSWORD'); Configuration::deleteByName('TM4B_USER'); Configuration::deleteByName('TM4B_ORIGINATOR'); Configuration::deleteByName('TM4B_ROUTE'); Configuration::deleteByName('TM4B_SIM'); Configuration::deleteByName('TM4B_NEW_ORDER_NUMBERS'); Configuration::deleteByName('TM4B_ALERT_NO_ACTIVE'); Configuration::deleteByName('TM4B_ALERT_UQ_ACTIVE'); Configuration::deleteByName('TM4B_DAILY_REPORT_ACTIVE'); Configuration::deleteByName('TM4B_LAST_REPORT'); return parent::uninstall(); } private function _getTplBody($tpl_file, $vars = array()) { $iso = Language::getIsoById((int)(Configuration::get('PS_LANG_DEFAULT'))); $file = dirname(__FILE__).'/mails/'.$iso.'/'.$tpl_file; if (!file_exists($file)) die($file); $tpl = file($file); $template = str_replace(array_keys($vars), array_values($vars), $tpl); return (implode("\n", $template)); } public function hookNewOrder($params) { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); if ( !(int)($this->_alert_new_order_active) OR empty($this->_user) OR empty($this->_password) OR empty($this->_new_order_numbers)) return ; $order = $params['order']; $customer = $params['customer']; $currency = $params['currency']; $templateVars = array( '{firstname}' => utf8_decode($customer->firstname), '{lastname}' => utf8_decode($customer->lastname), '{order_name}' => sprintf("%06d", $order->id), '{shop_name}' => Configuration::get('PS_SHOP_NAME'), '{payment}' => $order->payment, '{total_paid}' => $order->total_paid, '{currency}' => $currency->sign); $body = $this->_getTplBody(self::$_tpl_sms_files['name']['new_orders'].self::$_tpl_sms_files['ext']['new_orders'], $templateVars); $sms = new Tm4bSms($this->_user, $this->_password, $this->_route, $this->_originator); $sms->msg = $body; $numbers = explode(self::__TM4B_NUMBER_DELIMITOR__, $this->_new_order_numbers); foreach ($numbers as $number) if ($number != '') $sms->addRecipient($number); $sms->Send($this->_simulation); } public function hookUpdateQuantity($params) { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); if (!(int)($this->_alert_update_quantity_active) OR empty($this->_new_order_numbers)) return ; if (is_object($params['product'])) $params['product'] = get_object_vars($params['product']); $product = $params['product']; $qty = (int)($params['product']['quantity_attribute'] ? $params['product']['quantity_attribute'] : $params['product']['stock_quantity']) - (int)($params['product']['quantity']); if ($qty <= (int)(Configuration::get('PS_LAST_QTIES'))) { $templateVars = array( '{last_qty}' => (int)(Configuration::get('PS_LAST_QTIES')), '{qty}' => $qty, '{product}' => strval($params['product']['name'])); $body = $this->_getTplBody(self::$_tpl_sms_files['name']['out_of_stock'].self::$_tpl_sms_files['ext']['out_of_stock'], $templateVars); } if (!empty($body)) { $sms = new Tm4bSms($this->_user, $this->_password, $this->_route, $this->_originator); $sms->msg = $body; $numbers = explode(self::__TM4B_NUMBER_DELIMITOR__, $this->_new_order_numbers); foreach ($numbers as $number) if ($number != '') $sms->addRecipient($number); $sms->Send($this->_simulation); } } public function getContent() { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); $this->_html = '

'.$this->displayName.'

'; if (!empty($_POST)) { if (isset($_POST['btnTestSms'])) { if (!empty($this->_user) AND !empty($this->_password) AND !empty($_POST['test_number']) AND is_numeric($_POST['test_number'])) { $sms = new Tm4bSms($this->_user, $this->_password, $this->_route, $this->_originator); $sms->msg = 'Test SMS for your PrestaShop website'; $sms->addRecipient($_POST['test_number']); $ret = $sms->Send($this->_simulation); if ($sms->isSent()) $this->_html .= $this->displayConfirmation($this->l('Message sent')); else $this->_html .= $this->displayError($this->l('Error while sending message')); } else $this->_html .= $this->displayError($this->l('Login and phone number')); } else { $this->_postValidation(); if (!sizeof($this->_postErrors)) $this->_postProcess(); else foreach ($this->_postErrors AS $err) $this->_html .= $this->displayError($err); } } $this->_displayTm4b(); $this->_displayForm(); return $this->_html; } private function _displayTm4b() { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); $testsms_txt = 'Send'; $this->_html .= '
'.$this->l('Information').'

'.$this->l('ex: 33642424242').'
'; if (!empty($this->_user) AND !empty($this->_password)) { $sms = new Tm4bSms($this->_user, $this->_password, $this->_route, $this->_originator); $credits = $sms->CheckCredits(); $color = ($credits < self::__TM4B_LOWBALANCE__ ? '#900' : '#080'); $this->_html .= '
'.$this->l('You have').' '.$credits.' '.$this->l('credits').'
'; } $this->_html .= '

'.$this->l('Information about CRON tab').' http://'.Tools::getShopDomain().__PS_BASE_URI__.'modules/'.$this->name.'/cron.php?token='.$this->token.'

'; } private function _displayForm() { if (Tools::isSubmit('btnSubmit')) { if ($this->_user) { $_POST['user'] = $this->_user; $_POST['password'] = $this->_password; $_POST['route'] = $this->_route; $_POST['originator'] = $this->_originator; $_POST['simulation'] = $this->_simulation; $_POST['new_order_numbers'] = str_replace(self::__TM4B_NUMBER_DELIMITOR__, "\n", $this->_new_order_numbers); $_POST['alert_new_order'] = $this->_alert_new_order_active; $_POST['alert_update_quantity'] = $this->_alert_update_quantity_active; $_POST['daily_report'] = $this->_daily_report_active; } } $this->_html .= '
'.$this->l('Settings').'

'.$this->l('ex: 33642424242').'
'.$this->l('Simulation').'   '.$this->l('Production').'

 '.$this->l('Yes').'
'.$this->l('Send SMS if a new order is made').'
 '.$this->l('Yes').'
'.$this->l('Send SMS if the stock of product is updated').'
 '.$this->l('Yes').'
'.$this->l('Send a daily stats report - You must set a CRON to').' /modules/tm4b/cron.php


'.$this->l('ex: 33642424242').'

'; } private function _postProcess() { Configuration::updateValue('TM4B_PASSWORD', Tools::getValue('password')); Configuration::updateValue('TM4B_USER', Tools::getValue('user')); Configuration::updateValue('TM4B_ORIGINATOR', Tools::getValue('originator')); Configuration::updateValue('TM4B_ROUTE', Tools::getValue('route')); Configuration::updateValue('TM4B_SIM', Tools::getValue('simulation')); Configuration::updateValue('TM4B_ALERT_NO_ACTIVE', (int)Tools::isSubmit('alert_new_order')); Configuration::updateValue('TM4B_ALERT_UQ_ACTIVE', (int)Tools::isSubmit('alert_update_quantity')); Configuration::updateValue('TM4B_DAILY_REPORT_ACTIVE', (int)Tools::isSubmit('daily_report')); $numbers = explode("\n", Tools::getValue('new_order_numbers')); $this->_new_order_numbers = ''; foreach ($numbers as $number) { if (preg_match("/([0-9]+)/", $number, $regs)) $this->_new_order_numbers .= $regs[1].self::__TM4B_NUMBER_DELIMITOR__; } Configuration::updateValue('TM4B_NEW_ORDER_NUMBERS', $this->_new_order_numbers); $this->_html .= $this->displayConfirmation($this->l('Settings updated')); } private function _postValidation() { if (!Tools::getValue('user') || Validate::isGenericName(Tools::getValue('user')) != true) $this->_postErrors[] = $this->l('Username is mandatory'); elseif (!Tools::getValue('password') || Validate::isPasswd(Tools::getValue('password')) != true) $this->_postErrors[] = $this->l('Password is mandatory'); elseif (!Tools::getValue('route') OR (Tools::getValue('route') != 'GD01' AND Tools::getValue('route') != 'GD02' AND Tools::getValue('route') != 'USS1')) $this->_postErrors[] = $this->l('Route is mandatory'); elseif (!Tools::getValue('originator')) $this->_postErrors[] = $this->l('Origin is mandatory'); elseif (!Tools::isSubmit('simulation') OR (Tools::getValue('simulation') != 0 AND Tools::getValue('simulation') != 1)) $this->_postErrors[] = $this->l('Mode is mandatory'); elseif (!Tools::getValue('new_order_numbers')) $this->_postErrors[] = $this->l('Please enter a phone number'); elseif (preg_match('/([^0-9[:space:],])/', $_POST['new_order_numbers'], $regs)) $this->_postErrors[] = $this->l('Phone number invalid'); } public function getStatsBody() { $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT')); $currency->sign = $currency->iso_code; $query = ' SELECT SUM(o.`total_paid_real`) as total_sales, COUNT(o.`total_paid_real`) as total_orders FROM `'._DB_PREFIX_.'orders` o WHERE ( SELECT IF(os.`id_order_state` = 8, 0, 1) FROM `'._DB_PREFIX_.'orders` oo LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON oh.`id_order` = oo.`id_order` LEFT JOIN `'._DB_PREFIX_.'order_state` os ON os.`id_order_state` = oh.`id_order_state` WHERE oo.`id_order` = o.`id_order` ORDER BY oh.`date_add` DESC, oh.`id_order_history` DESC LIMIT 1 ) = 1 '; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query.' AND o.`date_add` >= DATE_SUB(\''.date('Y-m-d').' 20:00:00\', INTERVAL 1 DAY) AND o.`date_add` < \''.date('Y-m-d').' 20:00:00\''); $result2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query.'AND o.`date_add` LIKE \''.date('Y-m').'-%\''); return date('Y-m-d')."\n". $this->l('Orders:').' '.(int)$result['total_orders']."\n". $this->l('Sales:').' '.Tools::displayPrice($result['total_sales'], $currency, true)."\n". '('.$this->l('Month:').' '.Tools::displayPrice($result2['total_sales'], $currency, true).')'; } }