* @copyright Magavenue * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0 * @category modules * * @note If you want to customize the module, contact us at contact@magavenue.com */ if (!defined('_PS_VERSION_')) { exit; } class InstallmentPayment extends Module { public function __construct() { $this->name = 'installmentpayment'; $this->tab = 'payments_gateways'; $this->version = '1.0.24'; $this->author = 'MagAvenue'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.6'); $this->bootstrap = true; $this->module_key = 'dee2a6fce312c621d8a08ba9a24b6bbe'; parent::__construct(); $this->displayName = $this->l('Installment payment'); $this->description = $this->l('Allows you to set a percentage payment to ask to your customers for the payment of their order.'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); } public function install() { $this->copyOverrideFolder(); if (_PS_VERSION_ >= '1.7') { if (parent::install() && $this->registerHook('displayAdminOrder') && $this->registerHook('displayHeader') && $this->registerHook('displayPDFInvoice') && $this->registerHook('displayPaymentTop') && $this->registerHook('actionPaymentConfirmation') && $this->registerHook('displayOrderDetail') && $this->createTables() ) { return true; } } else { if (parent::install() && $this->registerHook('displayAdminOrder') && $this->registerHook('displayHeader') && $this->registerHook('displayPDFInvoice') && $this->registerHook('displayPayment') && $this->registerHook('actionPaymentConfirmation') && $this->registerHook('displayOrderDetail') && $this->createTables() ) { return true; } } return false; } public function copyOverrideFolder() { $version_override_folder = _PS_MODULE_DIR_ . $this->name . '/override_' . Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2); $override_folder = _PS_MODULE_DIR_ . $this->name . '/override'; if (file_exists($override_folder) && is_dir($override_folder)) { $this->recursiveRmdir($override_folder); } if (is_dir($version_override_folder)) { $this->copyDir($version_override_folder, $override_folder); } return true; } protected function copyDir($src, $dst) { if (is_dir($src)) { $dir = opendir($src); @mkdir($dst); while (false !== ($file = readdir($dir))) { if (($file != '.') && ($file != '..')) { if (is_dir($src . '/' . $file)) { $this->copyDir($src . '/' . $file, $dst . '/' . $file); } else { copy($src . '/' . $file, $dst . '/' . $file); } } } closedir($dir); } } protected function recursiveRmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir . "/" . $object) == "dir") { $this->recursiveRmdir($dir . "/" . $object); } else { unlink($dir . "/" . $object); } } } reset($objects); rmdir($dir); } } public function reset() { if (!$this->uninstall(false)) { return false; } if (!$this->install(false)) { return false; } return true; } protected function createTables() { Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'installmentpayment` ( `id_installmentpayment` int(11) NOT NULL AUTO_INCREMENT, `id_cart` int(11) NOT NULL, `total` varchar(255) NOT NULL, `payer` varchar(255) NOT NULL, `rest` varchar(255) NOT NULL, `state` int(11) NOT NULL, PRIMARY KEY (`id_installmentpayment`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ; '); Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'installmentpayment_group` ( `id_installmentpayment_group` int(11) NOT NULL AUTO_INCREMENT, `id_group` int(11) NOT NULL, `price` varchar(255) NOT NULL, PRIMARY KEY (`id_installmentpayment_group`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ; '); return true; } public function uninstall() { if (!parent::uninstall()) { return false; } return true; } public function getContent() { $output = ''; if (Tools::isSubmit('submitEdit')) { $ACOMPTE_PERCENTAGE = Tools::getValue('ACOMPTE_PERCENTAGE'); $ACOMPTE_TYPE = Tools::getValue('ACOMPTE_TYPE'); Configuration::updateValue('ACOMPTE_TYPE', $ACOMPTE_TYPE); Configuration::updateValue('ACOMPTE_PERCENTAGE', $ACOMPTE_PERCENTAGE); $ACOMPTE_CATS = serialize(Tools::getValue('ACOMPTE_CATS')); Configuration::updateValue('ACOMPTE_CATS', $ACOMPTE_CATS); $ACOMPTE_TOTALTYPE = Tools::getValue('ACOMPTE_TOTALTYPE'); Configuration::updateValue('ACOMPTE_TOTALTYPE', $ACOMPTE_TOTALTYPE); $ACOMPTE_SHIPPING = Tools::getValue('ACOMPTE_SHIPPING'); Configuration::updateValue('ACOMPTE_SHIPPING', $ACOMPTE_SHIPPING); $ACOMPTE_GROUP = serialize(Tools::getValue('ACOMPTE_GROUP')); Configuration::updateValue('ACOMPTE_GROUP', $ACOMPTE_GROUP); $ACOMPTE_CHOICE = Tools::getValue('ACOMPTE_CHOICE'); Configuration::updateValue('ACOMPTE_CHOICE', $ACOMPTE_CHOICE); $ACOMPTE_MIN_AMOUNT = Tools::getValue('ACOMPTE_MIN_AMOUNT'); Configuration::updateValue('ACOMPTE_MIN_AMOUNT', (float) $ACOMPTE_MIN_AMOUNT); $ACOMPTE_PAYMENT = serialize(Tools::getValue('ACOMPTE_PAYMENT')); Configuration::updateValue('ACOMPTE_PAYMENT', $ACOMPTE_PAYMENT); $ACOMPTE_STATUSES = Tools::getValue('ACOMPTE_STATUSES'); Configuration::updateValue('ACOMPTE_STATUSES', $ACOMPTE_STATUSES); $ACOMPTE_DAYS = Tools::getValue('ACOMPTE_DAYS'); Configuration::updateValue('ACOMPTE_DAYS', $ACOMPTE_DAYS); $sql = 'Delete FROM `'._DB_PREFIX_.'installmentpayment_group`'; $deleteall = Db::getInstance()->execute($sql); if(!empty($deleteall)) { foreach ($_POST as $key => $value) { if(substr($key, 0, 5) === 'group') { $sql = 'Insert into `'._DB_PREFIX_.'installmentpayment_group` set id_group = '.(int) str_replace('group_', '', $key). ', price = "'.$value.'"'; $insertgroupprice = Db::getInstance()->execute($sql); } } } $output .= $this->displayConfirmation($this->l('Your settings have been updated.')); } return $output . $this->renderForm(); } public function renderForm() { $categories = Category::getSimpleCategories($this->context->language->id); $groups = Group::getGroups((int) $this->context->language->id); $payment = PaymentModule::getInstalledPaymentModules(); $statuses = OrderState::getOrderStates((int) $this->context->language->id); array_unshift($statuses, array('id_order_state' => 0, 'name' => $this->l('Default state of module'))); $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->l('Settings'), 'icon' => 'icon-cogs' ), 'description' => $this->l('If you want to view the manual of the module,') . '  ' . $this->l('click here.') . '', 'input' => array( array( 'type' => 'text', 'label' => $this->l('Percentage or amount of advance payment'), 'desc' => $this->l('Indicate here, the amount or percentage for installment.'), 'name' => 'ACOMPTE_PERCENTAGE', 'size' => 15, 'required' => true ), array( 'type' => 'radio', 'label' => $this->l('Advance payment type'), 'name' => 'ACOMPTE_TYPE', 'values' => array( array( 'id' => 'strip_tags_from_desc_on', 'value' => 0, 'label' => $this->l('Percentage'), ), array( 'id' => 'strip_tags_from_desc_off', 'value' => 1, 'label' => $this->l('Amount'), ), ), ), array( 'type' => 'select', 'label' => $this->l('Category'), 'name' => 'ACOMPTE_CATS[]', 'width' => 350, 'desc' => $this->l('Do you want limit installment at few categories.'), 'multiple' => true, 'options' => array( 'query' => $categories, 'id' => 'id_category', 'name' => 'name' ) ), array( 'type' => 'select', 'label' => $this->l('Group'), 'name' => 'ACOMPTE_GROUP[]', 'desc' => $this->l('Do you want limit installment at few groups.'), 'multiple' => true, 'options' => array( 'query' => $groups, 'id' => 'id_group', 'name' => 'name' ) ), array( 'type' => 'select', 'label' => $this->l('STATUSES'), 'name' => 'ACOMPTE_STATUSES', 'desc' => $this->l('Default order status at final payment.'), 'multiple' => false, 'options' => array( 'query' => $statuses, 'id' => 'id_order_state', 'name' => 'name' ) ), array( 'type' => 'radio', 'label' => $this->l('The down payment on the total'), 'name' => 'ACOMPTE_TOTALTYPE', 'desc' => $this->l('Do you want used amount taxe excluded or included in installment.'), 'required' => true, 'class' => 't', 'is_bool' => true, 'values' => array( array( 'id' => 'totaltype_ttc', 'value' => 0, 'label' => $this->l('TTC') ), array( 'id' => 'totaltype_ht', 'value' => 1, 'label' => $this->l('HT') ) ), ), array( 'type' => 'radio', 'label' => $this->l('Shipping costs'), 'name' => 'ACOMPTE_SHIPPING', 'desc' => $this->l('Do you want add shipping cost in installment.'), 'required' => true, 'class' => 't', 'is_bool' => true, 'values' => array( array( 'id' => 'hipping_with', 'value' => 0, 'label' => $this->l('With') ), array( 'id' => 'hipping_without', 'value' => 1, 'label' => $this->l('Without') ) ), ), array( 'type' => 'radio', 'label' => $this->l('Choice of payment type'), 'name' => 'ACOMPTE_CHOICE', 'desc' => $this->l('Indicate here, if you want that the customer can choose to pay the total order or the installment, or only the installment.'), 'required' => true, 'class' => 't', 'is_bool' => true, 'values' => array( array( 'id' => 'choice_ttc', 'value' => 0, 'label' => $this->l('Yes') ), array( 'id' => 'choice_ht', 'value' => 1, 'label' => $this->l('No') ) ), ), array( 'type' => 'text', 'label' => $this->l('Minimum amount of the order'), 'name' => 'ACOMPTE_MIN_AMOUNT', 'size' => 15, 'required' => true, 'desc' => $this->l('Indicate here, at what amount of the down payment order will be proposed. 0 to ignore') ), array( 'type' => 'text', 'label' => $this->l('Days for balance payment email'), 'name' => 'ACOMPTE_DAYS', 'size' => 15, 'required' => true, 'desc' => $this->l('Indicate here, Days for balance payment email') ), array( 'type' => 'select', 'label' => $this->l('Payment'), 'name' => 'ACOMPTE_PAYMENT[]', 'desc' => $this->l('Do you want limit installment at few modules payment.'), 'multiple' => true, 'options' => array( 'query' => $payment, 'id' => 'id_module', 'name' => 'name' ) ), ), 'submit' => array( 'title' => $this->l('Save'), 'name' => 'submitEdit', ) ) ); foreach ($groups as $key => $value) { $groupsform = array(); $groupsform['type'] = 'text'; $groupsform['label'] = $value['name']; $groupsform['name'] = 'group_'.$value["id_group"]; $groupsform['desc'] = $this->l('Indicate here, percentage of installment amount for this group ').$value['name']; array_push($fields_form['form']['input'],$groupsform); } $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT')); $helper->default_form_language = $lang->id; $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0; $this->fields_form = array(); $helper->identifier = $this->identifier; $helper->submit_action = 'submit'; $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->tpl_vars = array( 'fields_value' => $this->getConfigFieldsValues(), 'languages' => $this->context->controller->getLanguages(), 'id_language' => $this->context->language->id ); return $helper->generateForm(array($fields_form)); } public function getConfigFieldsValues() { $categoryIdsTakenFromDb = Configuration::get('ACOMPTE_CATS'); $selectedcategories = @unserialize($categoryIdsTakenFromDb); if ($selectedcategories === false && $selectedcategories !== 'b:0;') { $selectedcategories = array(); } $groupIdsTakenFromDb = Configuration::get('ACOMPTE_GROUP'); $selectedgroups = @unserialize($groupIdsTakenFromDb); if ($selectedgroups === false && $selectedgroups !== 'b:0;') { $selectedgroups = array(); } $paymentIdsTakenFromDb = Configuration::get('ACOMPTE_PAYMENT'); $selectedpayments = @unserialize($paymentIdsTakenFromDb); if ($selectedpayments === false && $selectedpayments !== 'b:0;') { $selectedpayments = array(); } $groups = Group::getGroups((int) $this->context->language->id); $groupvalue = array(); foreach ($groups as $key => $value) { $data = array(); $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $value['id_group']; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $groupvalue['group_'.$value['id_group']] = $data['price']; } else { $groupvalue['group_'.$value['id_group']] = ''; } } $data = array( 'ACOMPTE_PERCENTAGE' => Tools::getValue('ACOMPTE_PERCENTAGE', Configuration::get('ACOMPTE_PERCENTAGE')), 'ACOMPTE_TYPE' => Tools::getValue('ACOMPTE_TYPE', Configuration::get('ACOMPTE_TYPE')), 'ACOMPTE_CATS[]' => $selectedcategories, 'ACOMPTE_TOTALTYPE' => Tools::getValue('ACOMPTE_TOTALTYPE', Configuration::get('ACOMPTE_TOTALTYPE')), 'ACOMPTE_SHIPPING' => Tools::getValue('ACOMPTE_SHIPPING', Configuration::get('ACOMPTE_SHIPPING')), 'ACOMPTE_GROUP[]' => $selectedgroups, 'ACOMPTE_CHOICE' => Tools::getValue('ACOMPTE_CHOICE', Configuration::get('ACOMPTE_CHOICE')), 'ACOMPTE_MIN_AMOUNT' => Tools::getValue('ACOMPTE_MIN_AMOUNT', Configuration::get('ACOMPTE_MIN_AMOUNT')), 'ACOMPTE_PAYMENT[]' => $selectedpayments, 'ACOMPTE_STATUSES' => Tools::getValue('ACOMPTE_STATUSES', Configuration::get('ACOMPTE_STATUSES')), 'ACOMPTE_DAYS' => Tools::getValue('ACOMPTE_DAYS', Configuration::get('ACOMPTE_DAYS')), ); if(!empty($groupvalue)){ return $data = array_merge($data, $groupvalue); } else { return $data ; } } public function getinstallmentInfos($id_cart) { $sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'installmentpayment` WHERE id_cart=' . (int) $id_cart; return Db::getInstance()->getRow($sql); } public function hookDisplayOrderDetail($params) { $installment = $this->getinstallmentInfos($params['order']->id_cart); if ((float) $installment['rest'] <= 0) { return; } $this->smarty->assign(array('installment' => $installment)); return $this->display(__FILE__, 'order_detail.tpl'); } public function hookDisplayPayment($params) { if (Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2) == 16 || Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2) == 16) { $page_name = Dispatcher::getInstance()->getController(); if ($page_name == 'installmentpayment') { return; } //die('Context::getContext()->cart->getOrderTotalGross(true)'.Context::getContext()->cart->getOrderTotalGross(true)); if (Context::getContext()->cart->getOrderTotalGross(true) < (float) Configuration::get('ACOMPTE_MIN_AMOUNT')) { return; } //test group user $groups = FrontController::getCurrentCustomerGroups(); $groupIdsTakenFromDb = Configuration::get('ACOMPTE_GROUP'); $selectedgroups = @unserialize($groupIdsTakenFromDb); if ($selectedgroups === false && $selectedgroups !== 'b:0;') { $selectedgroups = array(); } $group_accepted = false; foreach ($groups as $group) { if (in_array($group, $selectedgroups)) { $group_accepted = true; break; } } if (!$group_accepted) { return; } //test by categorie $categoryIdsTakenFromDb = Configuration::get('ACOMPTE_CATS'); $selectedcategories = @unserialize($categoryIdsTakenFromDb); if ($selectedcategories === false && $selectedcategories !== 'b:0;') { $selectedcategories = array(); } if (!is_array($selectedcategories) || empty($selectedcategories)) { return; } $pre_commande = true; if (is_array($this->context->cart->getProducts()) && count($this->context->cart->getProducts()) > 0) { foreach ($this->context->cart->getProducts() as $product) { if (!in_array((int) $product['id_category_default'], $selectedcategories)) { $pre_commande = false; return; } } } if (isset(Context::getContext()->cookie->installmentpayment_type) && (int) Context::getContext()->cookie->installmentpayment_type > 0 || Configuration::get('ACOMPTE_CHOICE') == 1) { $id_cart = $this->context->cart->id; $total = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $id_defaultgroup = Db::getInstance()->executeS('SELECT id_default_group FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . (int) Context::getContext()->customer->id); if(is_array($id_defaultgroup)) { $id_defaultgroup = $id_defaultgroup[0]['id_default_group']; } $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $id_defaultgroup; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $percent = $data['price']; } else { $percent = Configuration::get('ACOMPTE_PERCENTAGE'); } $acompte = (float) $total - ($total * (100 - $percent) / 100); $rest = $total - $acompte; $data_insert = array( 'id_cart' => (int) $id_cart, 'total' => $total, 'payer' => $acompte, 'rest' => (float) $rest, 'state' => 0, ); $installement = $this->getinstallmentInfos($id_cart); if (isset($installement['state'])) { Db::getInstance()->update('installmentpayment', $data_insert, 'id_cart=' . (int) $id_cart); } else { Db::getInstance()->insert('installmentpayment', $data_insert); } $installment = $this->getinstallmentInfos((int) $id_cart); } $total1 = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $id_defaultgroup = Db::getInstance()->executeS('SELECT id_default_group FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . (int) Context::getContext()->customer->id); if(is_array($id_defaultgroup)) { $id_defaultgroup = $id_defaultgroup[0]['id_default_group']; } $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $id_defaultgroup; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $percent = $data['price']; } else { $percent = Configuration::get('ACOMPTE_PERCENTAGE'); } $acompte1 = (float) $total1 - ($total1 * (100 - $percent) / 100); $this->smarty->assign(array( '_path' => $this->_path, "installmentpayment_type" => (int) Context::getContext()->cookie->installmentpayment_type, 'percent' => $percent, 'acompte_chose' => Configuration::get('ACOMPTE_CHOICE'), 'depositPrice' => (float) round($acompte1, 2) )); return $this->display(__FILE__, 'installment_chose.tpl'); } elseif (Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2) == 17) { $page_name = Dispatcher::getInstance()->getController(); if ($page_name == 'installmentpayment') { return; } if (Context::getContext()->cart->getOrderTotalGross(true) < (float) Configuration::get('ACOMPTE_MIN_AMOUNT')) { return; } //test group user $groups = FrontController::getCurrentCustomerGroups(); $groupIdsTakenFromDb = Configuration::get('ACOMPTE_GROUP'); $selectedgroups = @unserialize($groupIdsTakenFromDb); if ($selectedgroups === false && $selectedgroups !== 'b:0;') { $selectedgroups = array(); } $group_accepted = false; foreach ($groups as $group) { if (in_array($group, $selectedgroups)) { $group_accepted = true; break; } } if (!$group_accepted) { return; } //test by categorie $categoryIdsTakenFromDb = Configuration::get('ACOMPTE_CATS'); $selectedcategories = @unserialize($categoryIdsTakenFromDb); if ($selectedcategories === false && $selectedcategories !== 'b:0;') { $selectedcategories = array(); } if (!is_array($selectedcategories) || empty($selectedcategories)) { return; } $pre_commande = true; if (is_array($this->context->cart->getProducts()) && count($this->context->cart->getProducts()) > 0) { foreach ($this->context->cart->getProducts() as $product) { if (!in_array((int) $product['id_category_default'], $selectedcategories)) { $pre_commande = false; return; } } } if (isset(Context::getContext()->cookie->installmentpayment_type) && (int) Context::getContext()->cookie->installmentpayment_type > 0) { $id_cart = $this->context->cart->id; $total = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $acompte = (float) $this->context->cart->getOrderTotalSmall(true, Cart::BOTH); $rest = $total - $acompte; $data_insert = array( 'id_cart' => (int) $id_cart, 'total' => $total, 'payer' => $acompte, 'rest' => (float) $rest, 'state' => 0, ); $installement = $this->getinstallmentInfos($id_cart); if (isset($installement['state'])) { Db::getInstance()->update('installmentpayment', $data_insert, 'id_cart=' . (int) $id_cart); } else { Db::getInstance()->insert('installmentpayment', $data_insert); } $installment = $this->getinstallmentInfos((int) $id_cart); } $cartdata = new Cart(Context::getContext()->cart->id); $total = (float) $cartdata->getOrderTotalGross(true, Cart::BOTH); $id_defaultgroup = Db::getInstance()->executeS('SELECT id_default_group FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . (int) Context::getContext()->customer->id); if(is_array($id_defaultgroup)) { $id_defaultgroup = $id_defaultgroup[0]['id_default_group']; } $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $id_defaultgroup; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $percent = $data['price']; } else { $percent = Configuration::get('ACOMPTE_PERCENTAGE'); } $acompte = (float) $total - ($total * (100 - $percent) / 100); $this->smarty->assign(array( '_path' => $this->_path, "installmentpayment_type" => (int) Context::getContext()->cookie->installmentpayment_type, 'percent' => $percent, 'acompte_chose' => Configuration::get('ACOMPTE_CHOICE'), 'TotalPrice' => (float) round($total, 2), 'depositPrice' => (float) round($acompte, 2) )); return $this->display(__FILE__, 'installment_chose1_7.tpl'); } } public function hookDisplayPaymentTop($params) { if (Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2) == 16 || Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2) == 16) { $page_name = Dispatcher::getInstance()->getController(); if ($page_name == 'installmentpayment') { return; } if (Context::getContext()->cart->getOrderTotalGross(true) < (float) Configuration::get('ACOMPTE_MIN_AMOUNT')) { return; } //test group user $groups = FrontController::getCurrentCustomerGroups(); $groupIdsTakenFromDb = Configuration::get('ACOMPTE_GROUP'); $selectedgroups = @unserialize($groupIdsTakenFromDb); if ($selectedgroups === false && $selectedgroups !== 'b:0;') { $selectedgroups = array(); } $group_accepted = false; foreach ($groups as $group) { if (in_array($group, $selectedgroups)) { $group_accepted = true; break; } } if (!$group_accepted) { return; } //test by categorie $categoryIdsTakenFromDb = Configuration::get('ACOMPTE_CATS'); $selectedcategories = @unserialize($categoryIdsTakenFromDb); if ($selectedcategories === false && $selectedcategories !== 'b:0;') { $selectedcategories = array(); } if (!is_array($selectedcategories) || empty($selectedcategories)) { return; } $pre_commande = true; if (is_array($this->context->cart->getProducts()) && count($this->context->cart->getProducts()) > 0) { foreach ($this->context->cart->getProducts() as $product) { if (!in_array((int) $product['id_category_default'], $selectedcategories)) { $pre_commande = false; return; } } } if (isset(Context::getContext()->cookie->installmentpayment_type) && (int) Context::getContext()->cookie->installmentpayment_type > 0) { $id_cart = $this->context->cart->id; $total = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $id_defaultgroup = Db::getInstance()->executeS('SELECT id_default_group FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . (int) Context::getContext()->customer->id); if(is_array($id_defaultgroup)) { $id_defaultgroup = $id_defaultgroup[0]['id_default_group']; } $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $id_defaultgroup; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $percent = $data['price']; } else { $percent = Configuration::get('ACOMPTE_PERCENTAGE'); } $acompte = (float) $total - ($total * (100 - $percent) / 100); $rest = $total - $acompte; $data_insert = array( 'id_cart' => (int) $id_cart, 'total' => $total, 'payer' => $acompte, 'rest' => (float) $rest, 'state' => 0, ); $installement = $this->getinstallmentInfos($id_cart); if (isset($installement['state'])) { Db::getInstance()->update('installmentpayment', $data_insert, 'id_cart=' . (int) $id_cart); } else { Db::getInstance()->insert('installmentpayment', $data_insert); } $installment = $this->getinstallmentInfos((int) $id_cart); } $total1 = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $id_defaultgroup = Db::getInstance()->executeS('SELECT id_default_group FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . (int) Context::getContext()->customer->id); if(is_array($id_defaultgroup)) { $id_defaultgroup = $id_defaultgroup[0]['id_default_group']; } $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $id_defaultgroup; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $percent = $data['price']; } else { $percent = Configuration::get('ACOMPTE_PERCENTAGE'); } $acompte1 = (float) $total1 - ($total1 * (100 - $percent) / 100); $this->smarty->assign(array( '_path' => $this->_path, "installmentpayment_type" => (int) Context::getContext()->cookie->installmentpayment_type, 'percent' => $percent, 'acompte_chose' => Configuration::get('ACOMPTE_CHOICE'), 'depositPrice' => (float) round($acompte1, 2) )); return $this->display(__FILE__, 'installment_chose.tpl'); } elseif (Tools::substr(str_replace('.', '', _PS_VERSION_), 0, 2) == 17) { $page_name = Dispatcher::getInstance()->getController(); if ($page_name == 'installmentpayment') { return; } if (Context::getContext()->cart->getOrderTotalGross(true) < (float) Configuration::get('ACOMPTE_MIN_AMOUNT')) { return; } //test group user $groups = FrontController::getCurrentCustomerGroups(); $groupIdsTakenFromDb = Configuration::get('ACOMPTE_GROUP'); $selectedgroups = @unserialize($groupIdsTakenFromDb); if ($selectedgroups === false && $selectedgroups !== 'b:0;') { $selectedgroups = array(); } $group_accepted = false; foreach ($groups as $group) { if (in_array($group, $selectedgroups)) { $group_accepted = true; break; } } if (!$group_accepted) { return; } //test by categorie $categoryIdsTakenFromDb = Configuration::get('ACOMPTE_CATS'); $selectedcategories = @unserialize($categoryIdsTakenFromDb); if ($selectedcategories === false && $selectedcategories !== 'b:0;') { $selectedcategories = array(); } if (!is_array($selectedcategories) || empty($selectedcategories)) { return; } $pre_commande = true; if (is_array($this->context->cart->getProducts()) && count($this->context->cart->getProducts()) > 0) { foreach ($this->context->cart->getProducts() as $product) { if (!in_array((int) $product['id_category_default'], $selectedcategories)) { $pre_commande = false; return; } } } if (isset(Context::getContext()->cookie->installmentpayment_type) && (int) Context::getContext()->cookie->installmentpayment_type > 0) { $id_cart = $this->context->cart->id; $total = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $acompte = (float) $this->context->cart->getOrderTotalSmall(true, Cart::BOTH); $rest = $total - $acompte; $data_insert = array( 'id_cart' => (int) $id_cart, 'total' => $total, 'payer' => $acompte, 'rest' => (float) $rest, 'state' => 0, ); $installement = $this->getinstallmentInfos($id_cart); if (isset($installement['state'])) { Db::getInstance()->update('installmentpayment', $data_insert, 'id_cart=' . (int) $id_cart); } else { Db::getInstance()->insert('installmentpayment', $data_insert); } $installment = $this->getinstallmentInfos((int) $id_cart); } $cartdata = new Cart(Context::getContext()->cart->id); $total = (float) $cartdata->getOrderTotalGross(true, Cart::BOTH); $id_defaultgroup = Db::getInstance()->executeS('SELECT id_default_group FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . (int) Context::getContext()->customer->id); if(is_array($id_defaultgroup)) { $id_defaultgroup = $id_defaultgroup[0]['id_default_group']; } $sql = 'SELECT price FROM `' . _DB_PREFIX_ . 'installmentpayment_group` WHERE id_group=' . (int) $id_defaultgroup; $data = Db::getInstance()->getRow($sql); if(!empty($data)){ $percent = $data['price']; } else { $percent = Configuration::get('ACOMPTE_PERCENTAGE'); } $acompte = (float) $total - ($total * (100 - $percent) / 100); $this->smarty->assign(array( '_path' => $this->_path, "installmentpayment_type" => (int) Context::getContext()->cookie->installmentpayment_type, 'percent' => $percent, 'acompte_chose' => Configuration::get('ACOMPTE_CHOICE'), 'TotalPrice' => (float) round($total, 2), 'depositPrice' => (float) round($acompte, 2) )); return $this->display(__FILE__, 'installment_chose1_7.tpl'); } } public function hookDisplayHeader($params) { if ((int) Configuration::get('ACOMPTE_CHOICE') == 1) { Context::getContext()->cookie->__set('installmentpayment_type', 1); } if (Context::getContext()->cart->getOrderTotal(true) < (float) Configuration::get('ACOMPTE_MIN_AMOUNT') || !isset(Context::getContext()->cookie->installmentpayment_type) || (int) Context::getContext()->cookie->installmentpayment_type == 0) { $id_cart = (int) $this->context->cart->id; Db::getInstance()->delete('installmentpayment', 'id_cart=' . (int) $id_cart); return; } //test group user $groups = FrontController::getCurrentCustomerGroups(); $groupIdsTakenFromDb = Configuration::get('ACOMPTE_GROUP'); $selectedgroups = @unserialize($groupIdsTakenFromDb); if ($selectedgroups === false && $selectedgroups !== 'b:0;') { $selectedgroups = array(); } $group_accepted = false; foreach ($groups as $group) { if (in_array($group, $selectedgroups)) { $group_accepted = true; break; } } if (!$group_accepted) { return; } //test by categorie $categoryIdsTakenFromDb = Configuration::get('ACOMPTE_CATS'); $selectedcategories = @unserialize($categoryIdsTakenFromDb); if ($selectedcategories === false && $selectedcategories !== 'b:0;') { $selectedcategories = array(); } if (!is_array($selectedcategories) || empty($selectedcategories)) { return; } if (!isset(Context::getContext()->cookie->installmentpayment_type) || (int) Context::getContext()->cookie->installmentpayment_type == 0) { return; } $pre_commande = true; if (is_array($this->context->cart->getProducts()) && count($this->context->cart->getProducts()) > 0) { foreach ($this->context->cart->getProducts() as $product) { /**/ $sql = 'SELECT id_product FROM `' . _DB_PREFIX_ . 'category_product` WHERE `id_product` = ' . (int) $product['id_product'] . ' AND `id_category` IN ('; foreach ($selectedcategories as $category) { $sql .= (int) $category . ','; } $sql = rtrim($sql, ',') . ')'; /**/ if (!Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql)) { $pre_commande = false; return; } } } $pages_name = array(); $page_name = Dispatcher::getInstance()->getController(); $controllers = Dispatcher::getControllers(_PS_FRONT_CONTROLLER_DIR_); foreach ($controllers as $key => $value) { if ($key != 'orderconfirmation' && $key != 'pagenotfound') { $pages_name[] = $key; } } if (in_array($page_name, $pages_name)) { Context::getContext()->cookie->__set('installmentpayment_id_order', ''); } if ($page_name == 'order' || $page_name == 'orderopc') { $id_cart = $this->context->cart->id; $total = (float) $this->context->cart->getOrderTotalGross(true, Cart::BOTH); $acompte = (float) $this->context->cart->getOrderTotalSmall(true, Cart::BOTH); $rest = $total - $acompte; $data_insert = array( 'id_cart' => (int) $id_cart, 'total' => $total, 'payer' => $acompte, 'rest' => (float) $rest, 'state' => 0, ); $installement = $this->getinstallmentInfos($id_cart); if (isset($installement['state'])) { Db::getInstance()->update('installmentpayment', $data_insert, 'id_cart=' . (int) $id_cart); } else { Db::getInstance()->insert('installmentpayment', $data_insert); } $this->smarty->assign(array( 'percent' => Configuration::get('ACOMPTE_PERCENTAGE'))); return $this->display(__FILE__, 'installment.tpl'); } } public function hookdisplayAdminOrder($params) { // paiement manuel $order = new Order((int) $params['id_order']); if (Tools::isSubmit('submitAddPayment') || Tools::getIsset('payment_amount')) { $installment = $this->getinstallmentInfos((int) $order->id_cart); $amount = str_replace(',', '.', Tools::getValue('payment_amount')); if ((float) $installment['rest'] <= 0) { return; } $rest = (float) $installment['rest'] - (float) $amount; $acompte = (float) $installment['payer'] + (float) $amount; $state = 0; if ($rest <= 0) { $rest = 0; $state = 1; } $data_insert = array( 'payer' => $acompte, 'rest' => (float) $rest, 'state' => (int) $state, ); if (!Db::getInstance()->update('installmentpayment', $data_insert, 'id_cart=' . (int) $order->id_cart)) { die('problem'); } } // end paiement manuel $message = false; if (Tools::getIsset('installmentclaim')) { $this->sendMailClaim($params['id_order']); $message = true; } $sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'installmentpayment` '; $return = Db::getInstance()->executeS($sql); $order = new Order((int) $params['id_order']); $order->total_paid_tax_incl = (float) $order->total_paid; $order->save(); $installment = $this->getinstallmentInfos((int) $order->id_cart); if (isset($installment['state']) && $installment['state'] == 1) { $this->generateInvoice((int) $params['id_order']); } $this->smarty->assign(array( 'message' => $message, 'installment' => $installment, 'id_order' => (int) $params['id_order'])); return $this->display(__FILE__, 'order_installment.tpl'); } private function sendMailClaim($id_order) { if ((float) Tools::getValue('installmentpayment') <= 0) { return; } $iso = Language::getIsoById((int) $this->context->language->id); $order = new Order((int) $id_order); $customer = new Customer((int) $order->id_customer); $installement = $this->getinstallmentInfos($order->id_cart); $link = new LinkCore(); if (isset($installement['state'])) { $templateVars = array( '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{email}' => $customer->email, '{reference}' => $order->reference, '{pay}' => Tools::displayPrice($installement['payer'], new Currency($order->id_currency)), '{rest}' => Tools::displayPrice($installement['rest'], new Currency($order->id_currency)), '{total}' => Tools::displayPrice($installement['total'], new Currency($order->id_currency)), '{be_paid}' => Tools::displayPrice((float) Tools::getValue('installmentpayment'), new Currency($order->id_currency)), '{installement_url}' => $link->getModuleLink('installmentpayment', 'installmentpayment', array('select' => $id_order)) ); $template = 'claim'; //$customer->email = 'magavenueanuj@gmail.com'; $subject = $this->l('It remains to pay for the order #') . ' ' . $order->reference; if (file_exists(_PS_MODULE_DIR_ . $this->name . '/mails/' . $iso . '/' . $template . '.txt') and file_exists(_PS_MODULE_DIR_ . $this->name . '/mails/' . $iso . '/' . $template . '.html')) { Mail::Send((int) $this->context->language->id, $template, $subject, $templateVars, $customer->email, null, Configuration::get('PS_SHOP_EMAIL'), Configuration::get('PS_SHOP_NAME'), null, null, _PS_MODULE_DIR_ . $this->name . '/mails/'); } } } private function generateInvoice($id_order) { $order = new Order((int) $id_order); if ((int) $order->invoice_number == 0) { $order->setInvoice(); } return $order->invoice_number; } }