name = 'ecstock'; $this->tab = 'administration'; $this->version = '1.1.0'; $this->author = 'Ether Creation'; $this->need_instance = 0; /** * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6) */ $this->bootstrap = true; parent::__construct(); $this->module_key = 'ee72c77ce80e9ee2dbfdafa0bfd0a5e2'; $this->displayName = $this->l('Ec Stock'); $this->description = $this->l('Export/import your stock automatically'); } /** * Don't forget to create update methods if needed: * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update */ public function install() { Configuration::updateValue('ECSTOCK_HOTE', 0); Configuration::updateValue('ECSTOCK_LOGIN', 0); Configuration::updateValue('ECSTOCK_PASSWORD', 0); Configuration::updateValue('ECSTOCK_CHEM', 0); Configuration::updateValue('ECSTOCK_TOKEN', md5(time() . _COOKIE_KEY_)); return parent::install() && $this->registerHook('header') && $this->registerHook('backOfficeHeader'); } public function uninstall() { return parent::uninstall(); } /** * Load the configuration form */ public function getContent() { /** * If values have been submitted in the form, process. */ if (((bool)Tools::isSubmit('submitEcstockModule')) == true) { Configuration::updateValue('ECSTOCK_HOTE', Tools::getValue('ECSTOCK_HOTE')); Configuration::updateValue('ECSTOCK_LOGIN', Tools::getValue('ECSTOCK_LOGIN')); Configuration::updateValue('ECSTOCK_PASSWORD', Tools::getValue('ECSTOCK_PASSWORD')); Configuration::updateValue('ECSTOCK_CHEM', Tools::getValue('ECSTOCK_CHEM')); Configuration::updateValue('ECSTOCK_FTP', Tools::getValue('ECSTOCK_FTP')); if (!Configuration::get('ECSTOCK_FTP')) { $ext= Tools::strtolower(Tools::substr(strrchr($_FILES['ec_stockfile']['name'], '.'), 1)); if ($ext != 'csv') { return $this->displayError($this->l('File extension must be CSV')).$this->showMenu('import').$this->renderForm().$this->exportForm(); } $tmp_name = $_FILES["ec_stockfile"]["tmp_name"]; $name = $_FILES["ec_stockfile"]["name"]=1; if (!move_uploaded_file($tmp_name, $this->local_path.'file/'.$name.'.'.$ext)) { return $this->displayError($this->l('Erreur')).$this->showMenu('import').$this->renderForm().$this->exportForm(); } else { return $this->displayConfirmation($this->l('File has been upload')).$this->showMenu('import').$this->renderForm().$this->exportForm(); } } return $this->displayConfirmation($this->l('Successful update')).$this->showMenu('import').$this->renderForm().$this->exportForm(); } if (((bool)Tools::isSubmit('submitExportStock')) == true) { if ((!is_numeric(Tools::getValue('ECSTOCK_STOCKMIN')) && Tools::getValue('ECSTOCK_STOCKMIN')!="") || (!is_numeric(Tools::getValue('ECSTOCK_STOCKMAX')) && Tools::getValue('ECSTOCK_STOCKMAX')!="")) { return $this->displayError($this->l('Minimum and Maximum stock must be a number')).$this->showMenu('export').$this->renderForm().$this->exportForm(); } if (Tools::getValue('ECSTOCK_EXPORTMAIL')) { $arraymails = explode(';', Tools::getValue('ECSTOCK_EXPORTMAIL')); foreach ($arraymails as $key => $value) { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { return $this->displayError($this->l('Wrong email(s)')).$this->showMenu('export').$this->renderForm().$this->exportForm(); } } } Configuration::updateValue('ECSTOCK_STOCKMIN', Tools::getValue('ECSTOCK_STOCKMIN')); Configuration::updateValue('ECSTOCK_STOCKMAX', Tools::getValue('ECSTOCK_STOCKMAX')); Configuration::updateValue('ECSTOCK_REFERENCE', Tools::getValue('ECSTOCK_REFERENCE')); Configuration::updateValue('ECSTOCK_PRODUCT', Tools::getValue('ECSTOCK_PRODUCT')); Configuration::updateValue('ECSTOCK_EXPORT_MAILOK', Tools::getValue('ECSTOCK_EXPORT_MAILOK')); Configuration::updateValue('ECSTOCK_EXPORTMAIL', Tools::getValue('ECSTOCK_EXPORTMAIL')); Configuration::updateValue('ECSTOCK_EXPORTFTP', Tools::getValue('ECSTOCK_EXPORTFTP')); Configuration::updateValue('ECSTOCK_EXPORTHOTE', Tools::getValue('ECSTOCK_EXPORTHOTE')); Configuration::updateValue('ECSTOCK_EXPORTLOGIN', Tools::getValue('ECSTOCK_EXPORTLOGIN')); Configuration::updateValue('ECSTOCK_EXPORTPASSWORD', Tools::getValue('ECSTOCK_EXPORTPASSWORD')); Configuration::updateValue('ECSTOCK_EXPORTCHEM', Tools::getValue('ECSTOCK_EXPORTCHEM')); Configuration::updateValue('ECSTOCK_SALEDATEMIN', Tools::getValue('ECSTOCK_SALEDATEMIN')); Configuration::updateValue('ECSTOCK_SALEDATEMAX', Tools::getValue('ECSTOCK_SALEDATEMAX')); return $this->displayConfirmation($this->l('Successful update')).$this->showMenu('export').$this->renderForm().$this->exportForm(); } $this->context->smarty->assign('module_dir', $this->_path); return $this->showMenu('import').$this->renderForm().$this->exportForm(); } /** * Create the form that will be displayed in the configuration of your module. */ protected function renderForm() { $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $helper->module = $this; $helper->default_form_language = $this->context->language->id; $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); $helper->identifier = $this->identifier; $helper->submit_action = 'submitEcstockModule'; $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->getConfigFormValues(), /* Add values for your inputs */ 'languages' => $this->context->controller->getLanguages(), 'id_language' => $this->context->language->id, ); return $helper->generateForm(array($this->getConfigForm())); } /** * Create the structure of your form. */ protected function getConfigForm() { return array( 'form' => array( 'legend' => array( 'title' => $this->l('Stock import'), 'icon' => 'icon-cogs', ), 'input' => array( array( 'type' => 'file', 'desc' => $this->l('Upload your csv file : reference;quantity;action. Action: update,replace.'), 'name' => 'ec_stockfile', 'label' => $this->l('File'), ), array( 'type' => 'switch', 'label' => $this->l('FTP'), 'name' => 'ECSTOCK_FTP', 'is_bool' => true, 'desc' => $this->l('Get csv file by FTP'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Yes') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Non') ) ), ), array( 'type' => 'text', 'name' => 'ECSTOCK_HOTE', 'label' => $this->l('Hote FTP'), ), array( 'type' => 'text', 'name' => 'ECSTOCK_LOGIN', 'label' => $this->l('Login FTP'), ), array( 'type' => 'text', 'name' => 'ECSTOCK_PASSWORD', 'label' => $this->l('Password FTP'), ), array( 'type' => 'text', 'name' => 'ECSTOCK_CHEM', 'label' => $this->l('FTP path'), 'desc' => $this->l('Exemple : "/www/export/filename.csv"'), ), ), 'submit' => array( 'title' => $this->l('Save'), ), ), ); } /** * Set values for the inputs. */ protected function getConfigFormValues() { return array( // 'ECSTOCK_LIVE_MODE' => Configuration::get('ECSTOCK_LIVE_MODE', true), 'ECSTOCK_FTP' => Configuration::get('ECSTOCK_FTP', 0), 'ECSTOCK_MIN' => Configuration::get('ECSTOCK_MIN', 0), 'ECSTOCK_HOTE' => Configuration::get('ECSTOCK_HOTE', 0), 'ECSTOCK_LOGIN' => Configuration::get('ECSTOCK_LOGIN', 0), 'ECSTOCK_PASSWORD' => Configuration::get('ECSTOCK_PASSWORD', 0), 'ECSTOCK_CHEM' => Configuration::get('ECSTOCK_CHEM', 0), // 'ECSTOCK_ACCOUNT_PASSWORD' => Configuration::get('ECSTOCK_ACCOUNT_PASSWORD', null), ); } public function showtraiter() { //$reports = Db::getInstance()->executes('SELECT * FROM '._DB_PREFIX_.'ecsecurity_reports order by id DESC'); //$linkajax = Tools::getHttpHost(true).__PS_BASE_URI__."modules/ecsecurity/ajax.php?cron_token=".Configuration::get('ECSECURITY_TOKEN'); /*$link = AdminController::$currentIndex.'&configure='.$this->name.'&tab_module=administration&token='.Tools::getAdminTokenLite('AdminModules').'&module_name=ecstock'; $this->smarty->assign(array( "link" => $link, )); return $this->display(__FILE__, '/views/templates/admin/traiter.tpl');*/ } protected function exportForm() { $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $helper->module = $this; $helper->default_form_language = $this->context->language->id; $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); $helper->identifier = $this->identifier; $helper->submit_action = 'submitExportStock'; $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->getExportConfigFormValues(), /* Add values for your inputs */ 'languages' => $this->context->controller->getLanguages(), 'id_language' => $this->context->language->id, ); return $helper->generateForm(array($this->getConfigExportForm())); } /** * Create the structure of your form. */ protected function getConfigExportForm() { $productactif = array( array('id' => 1, 'name' =>$this->l('All')), array('id' => 2, 'name' =>$this->l('Enable')), array('id' => 3, 'name' =>$this->l('Disable')) ); return array( 'form' => array( 'legend' => array( 'title' => $this->l('Stock export'), 'icon' => 'icon-cogs', ), 'input' => array( array( 'type' => 'text', 'name' => 'ECSTOCK_STOCKMIN', 'label' => $this->l('Minimum stock'), 'col' => 3, ), array( 'type' => 'text', 'name' => 'ECSTOCK_STOCKMAX', 'label' => $this->l('Maximum stock'), 'col' => 3, ), array( 'type' => 'text', 'name' => 'ECSTOCK_REFERENCE', 'label' => $this->l('Reference'), 'desc' => $this->l('Begin like prod1 (like %)'), 'col' => 3, ), array( 'type' => 'date', 'name' => 'ECSTOCK_SALEDATEMIN', 'label' => $this->l('Sales date minimum'), 'col' => 3, ), array( 'type' => 'date', 'name' => 'ECSTOCK_SALEDATEMAX', 'label' => $this->l('Sales date maximum'), 'col' => 3, ), array( 'type' => 'select', 'label' => $this->l('Product'), 'name' => 'ECSTOCK_PRODUCT', 'options' => array( 'query' => $productactif, 'id' => 'id', 'name' => 'name' ) ), array( 'type' => 'switch', 'label' => $this->l('Export by email ?'), 'name' => 'ECSTOCK_EXPORT_MAILOK', 'is_bool' => true, 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), array( 'col' => 3, 'type' => 'text', 'prefix' => '', 'name' => 'ECSTOCK_EXPORTMAIL', 'label' => $this->l('Emails'), 'desc' => $this->l('Enter emails who will receive the exports, separate them by ";". Exemple : test@test.com;test1@test.com;test2@test.com'), ), array( 'type' => 'switch', 'label' => $this->l('FTP'), 'name' => 'ECSTOCK_EXPORTFTP', 'is_bool' => true, 'desc' => $this->l('Export by FTP'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Yes') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Non') ) ), ), array( 'type' => 'text', 'name' => 'ECSTOCK_EXPORTHOTE', 'label' => $this->l('Hote FTP'), 'col' => 3, ), array( 'type' => 'text', 'name' => 'ECSTOCK_EXPORTLOGIN', 'label' => $this->l('Login FTP'), 'col' => 3, ), array( 'type' => 'text', 'name' => 'ECSTOCK_EXPORTPASSWORD', 'label' => $this->l('Password FTP'), 'col' => 3, ), array( 'type' => 'text', 'name' => 'ECSTOCK_EXPORTCHEM', 'label' => $this->l('FTP path'), 'desc' => $this->l('Exemple : "/www/export/filename.csv"'), 'col' => 3, ), ), 'submit' => array( 'title' => $this->l('Save'), ), ), ); } /** * Set values for the inputs. */ protected function getExportConfigFormValues() { return array( 'ECSTOCK_STOCKMIN' => Configuration::get('ECSTOCK_STOCKMIN', 0), 'ECSTOCK_STOCKMAX' => Configuration::get('ECSTOCK_STOCKMAX', 0), 'ECSTOCK_REFERENCE' => Configuration::get('ECSTOCK_REFERENCE', 0), 'ECSTOCK_EXPORT_MAILOK' => Configuration::get('ECSTOCK_EXPORT_MAILOK', 0), 'ECSTOCK_EXPORTMAIL' => Configuration::get('ECSTOCK_EXPORTMAIL', 0), 'ECSTOCK_PRODUCT' => Configuration::get('ECSTOCK_PRODUCT', 0), 'ECSTOCK_EXPORTFTP' => Configuration::get('ECSTOCK_EXPORTFTP', 0), 'ECSTOCK_EXPORTHOTE' => Configuration::get('ECSTOCK_EXPORTHOTE', 0), 'ECSTOCK_EXPORTLOGIN' => Configuration::get('ECSTOCK_EXPORTLOGIN', 0), 'ECSTOCK_EXPORTPASSWORD' => Configuration::get('ECSTOCK_EXPORTPASSWORD', 0), 'ECSTOCK_EXPORTCHEM' => Configuration::get('ECSTOCK_EXPORTCHEM', 0), 'ECSTOCK_SALEDATEMAX' => Configuration::get('ECSTOCK_SALEDATEMAX', 0), 'ECSTOCK_SALEDATEMIN' => Configuration::get('ECSTOCK_SALEDATEMIN', 0), ); } public function showMenu($active) { $linkcronimport = Tools::getHttpHost(true).__PS_BASE_URI__."modules/ecstock/ecupdatestock.php?token=".Configuration::get('ECSTOCK_TOKEN'); $linkcronexport = Tools::getHttpHost(true).__PS_BASE_URI__."modules/ecstock/ecexportstock.php?token=".Configuration::get('ECSTOCK_TOKEN')."&id_shop=".(int)$this->context->shop->id; $this->smarty->assign(array( 'token' => Configuration::get('ECSTOCK_TOKEN'), 'active' => $active, 'linkcronimport' => $linkcronimport, 'linkcronexport' => $linkcronexport, 'id_shop' => (int)$this->context->shop->id )); return $this->display(__FILE__, '/views/templates/admin/menu.tpl'); } /** * Save form data. */ protected function postProcess() { $form_values = $this->getConfigFormValues(); foreach (array_keys($form_values) as $key) { Configuration::updateValue($key, Tools::getValue($key)); } } public function hookDisplayOrderConfirmation($params) { $id_shop = (int)$this->context->shop->id; $ref = Db::getInstance()->executes('SELECT pa.reference, od.product_quantity FROM '._DB_PREFIX_.'order_detail od, '._DB_PREFIX_.'product_attribute pa WHERE od.id_order = '.(int)Tools::getValue('id_order').' AND od.product_attribute_id = pa.id_product_attribute AND od.product_id = pa.id_product'); //echo 'SELECT pa.reference, od.product_quantity FROM '._DB_PREFIX_.'order_detail od, '._DB_PREFIX_.'product_attribute pa // WHERE od.id_order = '.(int)Tools::getValue('id_order').' // AND od.product_attribute_id = pa.id_product_attribute // AND od.product_id = pa.id_product'; // var_export($ref); //exit(); foreach ($ref as $key => $value) { $res = explode('-', $value['reference']); if (count($res) > 1) { $ref = $res[0].'-'; $ids = self::getPIdsByReferenceLike($ref, $value['reference']); //var_export($ids); //exit(); foreach ($ids as $key => $val) { $stock = StockAvailable::getQuantityAvailableByProduct( (int) $val['id_product'], (int) $val['id_product_attribute'], ($id_shop ? (int) $id_shop : null) ); StockAvailable::setQuantity( (int) $val['id_product'], (int) $val['id_product_attribute'], (int) ($stock - $value['product_quantity']), ($id_shop ? (int) $id_shop : null) ); } } } } public function getPIdsByReferenceLike($reference, $basicref, $id_fournisseur = false) { $res = Db::getInstance()->ExecuteS(' SELECT pa.id_product, pa.id_product_attribute FROM ' . _DB_PREFIX_ . 'product_attribute pa ' . (!$id_fournisseur ? '' : 'LEFT JOIN ' . _DB_PREFIX_ . 'product p ON pa.id_product = p.id_product') . ' WHERE pa.reference like "' . pSQL($reference) . '%" AND pa.reference != "'.pSQL($basicref).'"'. (!$id_fournisseur ? '' : ' AND p.id_supplier = ' . (int) $id_fournisseur)); //if (isset($res['id_product_attribute'])) { return $res; //} //$id = Db::getInstance()->ExecuteS(' // SELECT `id_product` // FROM `' . _DB_PREFIX_ . 'product` // WHERE `reference` = "' . pSQL($reference) . '"' . // (!$id_fournisseur ? '' : ' AND `id_supplier` = ' . (int) $id_fournisseur)); // if ($id) { // return array('id_product' => $id, 'id_product_attribute' => 0); // } // // return false; } /** * Add the CSS & JavaScript files you want to be loaded in the BO. */ public function hookBackOfficeHeader() { if (Tools::getValue('module_name') == $this->name || Tools::getValue('configure') == $this->name) { //$this->context->controller->addJS($this->_path.'views/js/back.js'); $this->context->controller->addCSS($this->_path.'views/css/back.css'); $this->smarty->assign(array( "scriptjs" => $this->_path.'views/js/back.js', "token" => Configuration::get('ECSTOCK_TOKEN'), )); return $this->display(__FILE__, '/views/templates/admin/header.tpl'); } } public function getIdLang() { return $this->context->language->id; } public function getIdShop() { return $this->context->shop->id; } /** * Add the CSS & JavaScript files you want to be added on the FO. */ public function hookHeader() { $this->context->controller->addJS($this->_path.'/views/js/front.js'); $this->context->controller->addCSS($this->_path.'/views/css/front.css'); } }