* @copyright 2012-2019 Patryk Marek - PrestaDev.pl * @license License is for use in domain / or one multistore enviroment (do not modify or reuse this code or part of it) if you want any changes please contact with me at info@prestadev.pl * @link http://prestadev.pl * @package PD CSV Price update on products - PrestaShop 1.5.x and 1.6.x Module and 1.7.x Module * @version 1.0.3 * @date 20-02-2017 */ if (!defined('_PS_VERSION_')) { exit; } include_once(dirname(__FILE__) . '/models/HistoryCsvModel.php'); class PdCsvPriceUpdate extends Module { private $html = ''; private $postErrors = array(); private $file = ''; public $csv_file; public function __construct() { @set_time_limit(0); $this->need_instance = 0; $this->name = 'pdcsvpriceupdate'; $this->tab = 'administration'; $this->version = '1.1.0'; $this->author = 'PrestaDev.pl'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Aktualizacja cen z pliku CSV'); $this->description = $this->l('Aktualizacja cen produktów z pliku CSV'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall my module?'); $this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_); if (file_exists(_PS_MODULE_DIR_ . $this->name . '/files/' . Configuration::get('PD_CSVPU_CSV_FILE'))) { $this->csv_file = _PS_MODULE_DIR_ . $this->name . '/files/' . Configuration::get('PD_CSVPU_CSV_FILE'); } $this->file = 'raport_z_aktualizacji_nie_znaleziono.csv'; $this->file_export = 'eksport_produktow.csv'; $this->update_type = Configuration::get('PD_CSVPU_UPDATE_TYPE'); $this->products_identifier = Configuration::get('PD_CSVPU_IDENTIFIER'); $this->csv_file_name = Configuration::get('PD_CSVPU_CSV_FILE'); $this->same_csv_allow = Configuration::get('PD_CSVPU_SAME_CSV'); $this->csv_header_ignore = Configuration::get('PD_CSVPU_HEADER_IGN'); $this->csv_delimeter = Configuration::get('PD_CSVPU_CSV_DELIM'); $this->csv_shop_update = Configuration::get('PD_CSVPU_UPDATE_SHOP'); $this->prices_with_tax = Configuration::get('PD_CSVPU_PRICES_WITH_TAX'); $this->products_identifier_export = Configuration::get('PD_CSVPU_IDENTIFIER_EXPORT'); $this->csv_delimeter_export = Configuration::get('PD_CSVPU_CSV_DELIM_EXPORT'); $this->csv_shop_update_export = Configuration::get('PD_CSVPU_UPDATE_SHOP_EXPORT'); $this->zero_base_price = Configuration::get('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE'); $this->update_multiple = Configuration::get('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS'); } public function install() { if ( !parent::install() || !HistoryCsvPriceUpdateModel::createTables() || !Configuration::updateValue('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE', 0) || !Configuration::updateValue('PD_CSVPU_SAME_CSV', 1) || !Configuration::updateValue('PD_CSVPU_UPDATE_SHOP', 1) || !Configuration::updateValue('PD_CSVPU_IDENTIFIER', 1) || !Configuration::updateValue('PD_CSVPU_PRICES_WITH_TAX', 0) || !Configuration::updateValue('PD_CSVPU_CSV_FILE', '') || !Configuration::updateValue('PD_CSVPU_CSV_DELIM', ';') || !Configuration::updateValue('PD_CSVPU_UPDATE_TYPE', 1) || !Configuration::updateValue('PD_CSVPU_IDENTIFIER_EXPORT', 1) || !Configuration::updateValue('PD_CSVPU_UPDATE_SHOP_EXPORT', 1) || !Configuration::updateValue('PD_CSVPU_CSV_DELIM_EXPORT', ';') || !Configuration::updateValue('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS', 0) ) { return false; } return true; } public function uninstall() { if ( !Configuration::deleteByName('PD_CSVPU_CSV_FILE') || !Configuration::updateValue('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE', 0) || !Configuration::deleteByName('PD_CSVPU_UPDATE_SHOP') || !Configuration::deleteByName('PD_CSVPU_IDENTIFIER') || !Configuration::deleteByName('PD_CSVPU_PRICES_WITH_TAX') || !Configuration::deleteByName('PD_CSVPU_CSV_FILE') || !Configuration::deleteByName('PD_CSVPU_CSV_DELIM') || !Configuration::deleteByName('PD_CSVPU_UPDATE_TYPE') || !Configuration::deleteByName('PD_CSVPU_SAME_CSV') || !Configuration::deleteByName('PD_CSVPU_IDENTIFIER_EXPORT') || !Configuration::deleteByName('PD_CSVPU_UPDATE_SHOP_EXPORT') || !Configuration::deleteByName('PD_CSVPU_CSV_DELIM_EXPORT') || !Configuration::deleteByName('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS') || !HistoryCsvPriceUpdateModel::dropTables() || !parent::uninstall() ) { return false; } return true; } public function getContent() { if (Tools::isSubmit('btnSubmitFile') || Tools::isSubmit('btnSubmitConfig') || Tools::isSubmit('btnDownloadUpdate') || Tools::isSubmit('btnDownloadCSVUpdate')) { $this->_postProcess(); } $this->html .= $this->renderForm(); $this->html .= $this->renderList(); return $this->html; } public function _postProcess() { if (Tools::isSubmit('btnDownloadUpdate')) { if ($this->same_csv_allow == false) { if ($this->checkIfCsvNameWasUsed($this->csv_file_name)) { $this->html .= $this->displayError($this->l('Plik o tej samej nazwie był już raz wgrany prosimy wgrać inny lub włączyć opcje "Ten sam plik" w konfiguracji modułu')); return false; } } $this->parseAndUpdateProducts(); } if (Tools::isSubmit('btnDownloadCSVUpdate')) { $this->exportCSVProducts(); } if (Tools::isSubmit('btnSubmitConfig')) { Configuration::updateValue('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE', Tools::getValue('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE')); Configuration::updateValue('PD_CSVPU_SAME_CSV', Tools::getValue('PD_CSVPU_SAME_CSV')); Configuration::updateValue('PD_CSVPU_CSV_DELIM', Tools::getValue('PD_CSVPU_CSV_DELIM')); Configuration::updateValue('PD_CSVPU_IDENTIFIER', Tools::getValue('PD_CSVPU_IDENTIFIER')); Configuration::updateValue('PD_CSVPU_HEADER_IGN', Tools::getValue('PD_CSVPU_HEADER_IGN')); Configuration::updateValue('PD_CSVPU_UPDATE_TYPE', Tools::getValue('PD_CSVPU_UPDATE_TYPE')); Configuration::updateValue('PD_CSVPU_UPDATE_SHOP', Tools::getValue('PD_CSVPU_UPDATE_SHOP')); Configuration::updateValue('PD_CSVPU_PRICES_WITH_TAX', Tools::getValue('PD_CSVPU_PRICES_WITH_TAX')); Configuration::updateValue('PD_CSVPU_CSV_DELIM_EXPORT', Tools::getValue('PD_CSVPU_CSV_DELIM_EXPORT')); Configuration::updateValue('PD_CSVPU_IDENTIFIER_EXPORT', Tools::getValue('PD_CSVPU_IDENTIFIER_EXPORT')); Configuration::updateValue('PD_CSVPU_UPDATE_SHOP_EXPORT', Tools::getValue('PD_CSVPU_UPDATE_SHOP_EXPORT')); Configuration::updateValue('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS', Tools::getValue('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS')); $this->update_type = Configuration::get('PD_CSVPU_UPDATE_TYPE'); $this->products_identifier = Configuration::get('PD_CSVPU_IDENTIFIER'); $this->csv_file_name = Configuration::get('PD_CSVPU_CSV_FILE'); $this->same_csv_allow = Configuration::get('PD_CSVPU_SAME_CSV'); $this->csv_header_ignore = Configuration::get('PD_CSVPU_HEADER_IGN'); $this->csv_delimeter = Configuration::get('PD_CSVPU_CSV_DELIM'); $this->csv_shop_update = Configuration::get('PD_CSVPU_UPDATE_SHOP'); $this->prices_with_tax = Configuration::get('PD_CSVPU_PRICES_WITH_TAX'); $this->products_identifier_export = Configuration::get('PD_CSVPU_IDENTIFIER_EXPORT'); $this->csv_delimeter_export = Configuration::get('PD_CSVPU_CSV_DELIM_EXPORT'); $this->csv_shop_update_export = Configuration::get('PD_CSVPU_UPDATE_SHOP_EXPORT'); $this->zero_base_price = Configuration::get('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE'); $this->update_multiple = Configuration::get('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS'); } if (Tools::isSubmit('btnSubmitFile')) { if (isset($_FILES['PD_CSVPU_CSV_FILE']) && isset($_FILES['PD_CSVPU_CSV_FILE']['tmp_name']) && !empty($_FILES['PD_CSVPU_CSV_FILE']['tmp_name'])) { if ($this->same_csv_allow == false) { if ($this->checkIfCsvNameWasUsed($_FILES['PD_CSVQU_CSV_FILE']['name'])) { $this->html .= $this->displayError($this->l('Plik o tej samej nazwie był już raz wgrany prosimy wgrać inny lub włączyć opcje "Ten sam plik" w konfiguracji modułu')); return false; } } // Delete previous file if (file_exists(_PS_MODULE_DIR_ . $this->name . '/files/' . Configuration::get('PD_CSVPU_CSV_FILE'))) { @unlink(_PS_MODULE_DIR_ . $this->name . '/files/' . Configuration::get('PD_CSVPU_CSV_FILE')); Configuration::updateValue('PD_CSVPU_CSV_FILE', ''); } // Store new file name in config Configuration::updateValue('PD_CSVPU_CSV_FILE', $_FILES['PD_CSVPU_CSV_FILE']['name']); // Copy the pdf file in the module directory/files with its own name if (!move_uploaded_file($_FILES['PD_CSVPU_CSV_FILE']['tmp_name'], _PS_MODULE_DIR_ . $this->name . '/files/' . $_FILES['PD_CSVPU_CSV_FILE']['name'])) { $this->html .= $this->displayError($this->l('Błąd wgrywania pliku CSV')); } else { $this->html .= $this->displayConfirmation($this->l('Plik CSV został wgrany')); } } else { $this->html .= $this->displayError($this->l('Prosimy wybrać plik CSV przed wgraniem go')); } } } public function checkIfCsvNameWasUsed($file_name) { $sql = 'SELECT COUNT(*) FROM `' . _DB_PREFIX_ . 'pdcsvpriceupdate_history` WHERE `file_name` = \'' . pSQL($file_name) . '\''; return Db::getInstance()->getValue($sql); } public function renderForm() { $switch = version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'; $fields_form_0 = array( 'form' => array( 'legend' => array( 'title' => $this->l('Wgraj plik csv'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'file', 'label' => $this->l('Plik CSV'), 'desc' => $this->l('Wgraj plik CSV (2 kolumny identyfikator, cena)'), 'name' => 'PD_CSVPU_CSV_FILE', 'required' => true, 'file' => Configuration::get('PD_CSVPU_CSV_FILE') ? $this->context->link->protocol_content . Tools::getMediaServer($this->name) . _MODULE_DIR_ . $this->name . '/files/' . Configuration::get('PD_CSVPU_CSV_FILE') : null, ), ), 'submit' => array( 'name' => 'btnSubmitFile', 'title' => $this->l('Wgraj plik'), ) ), ); $fields_form_1 = array( 'form' => array( 'legend' => array( 'title' => $this->l('Konfiguracja modułu'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Separator kolumn'), 'desc' => $this->l('Separator kolumn w CSV (domyślinie ";")'), 'lang' => false, 'name' => 'PD_CSVPU_CSV_DELIM', 'required' => true ), array( 'type' => $switch, 'label' => $this->l('Ignoruj nagłówek'), 'name' => 'PD_CSVPU_HEADER_IGN', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Jeśli włączone moduł zingonuje pierwszy wiersz w pliku csv (jeśli masz tytuły kolumn w plku CSV to prosimy włączyć)'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), array( 'type' => 'select', 'label' => $this->l('Identyfikator produktów'), 'name' => 'PD_CSVPU_IDENTIFIER', 'width' => 300, 'class' => 'fixed-width-ld', 'desc' => $this->l('Proszę wybrać identyfikator produktów / kombinacji produktów znajdujacy się w pierwszej kolumnie pliku CSV (musi być taki sam jak na produktach / kombinacjach w sklepie by możliwa była aktualziacja)'), 'options' => array( 'query' => array( array( 'id' => '1', 'name' => $this->l('Index (referencja)') ), array( 'id' => '2', 'name' => $this->l('Ean13') ), array( 'id' => '3', 'name' => $this->l('UPC') ), array( 'id' => '4', 'name' => $this->l('Id produktu / Id kombinacji') ), ), 'id' => 'id', 'name' => 'name', ) ), array( 'type' => 'select', 'label' => $this->l('Sklep'), 'name' => 'PD_CSVPU_UPDATE_SHOP', 'required' => true, 'desc' => $this->l('Wybierz sklep którego aktualizacja ma się dotyczyć'), 'options' => array( 'query' => Shop::getShops(true), 'id' => 'id_shop', 'name' => 'name' ) ), array( 'type' => 'select', 'label' => $this->l('Typ aktualizacji'), 'name' => 'PD_CSVPU_UPDATE_TYPE', 'width' => 300, 'class' => 'fixed-width-ld', 'desc' => $this->l('Proszę wybrać czy aktualizujemy produkty bazowe czy kombinacje produktów'), 'options' => array( 'query' => array( array( 'id' => '1', 'name' => $this->l('Produkt bazowy') ), array( 'id' => '2', 'name' => $this->l('Kombinacje') ), ), 'id' => 'id', 'name' => 'name', ) ), array( 'type' => $switch, 'label' => $this->l('Ten sam plik'), 'name' => 'PD_CSVPU_SAME_CSV', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Jeśli opcja jest wyłączona moduł nie pozwoli na wykonanie aktualizacji gdy nazwa wgrywanego pliku była już użyta'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), array( 'type' => $switch, 'label' => $this->l('Ceny z podatkiem 23%'), 'name' => 'PD_CSVPU_PRICES_WITH_TAX', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Jeśli opcja jest wyłączona moduł odejmie stawke 23% vat od cen produktów z CSV przed zapisem do bazy danych Prestahop'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), array( 'type' => $switch, 'label' => $this->l('Wyzeruj cenę bazową'), 'name' => 'PD_CSVPU_ZERO_BASE_PRODUCT_PRICE', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Jeśli opcja jest wyłączona moduł podczas aktualizacji ceny kombinacji atrybutu produktu wyzeruje cenę bazową tego produktu'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), array( 'type' => $switch, 'label' => $this->l('Aktualizuj wszystkie wystąpienia'), 'name' => 'PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Jeśli opcja jest wyłączona moduł podczas aktualizacji ceny zaktualizuje wszystkie znalezione wystąpienia danego identyfikatora produktów a nie tylko pierwsze napotkane zakładająć iż identyfikatory sa unikatowe'), 'values' => array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => false, 'label' => $this->l('Disabled') ) ), ), ), 'submit' => array( 'name' => 'btnSubmitConfig', 'title' => $this->l('Zapisz ustawienia'), ) ), ); $fields_form_2 = array( 'form' => array( 'legend' => array( 'title' => $this->l('Aktualizacja'), 'icon' => 'icon-cogs' ), 'submit' => array( 'name' => 'btnDownloadUpdate', 'title' => $this->l('Aktualizuj produkty z CSV'), ) ), ); $fields_form_3 = array( 'form' => array( 'legend' => array( 'title' => $this->l('Konfiguracja exportu CSV'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Separator kolumn'), 'desc' => $this->l('Separator kolumn w CSV (domyślinie ";")'), 'lang' => false, 'name' => 'PD_CSVPU_CSV_DELIM_EXPORT', 'required' => true ), array( 'type' => 'select', 'label' => $this->l('Identyfikator produktów'), 'name' => 'PD_CSVPU_IDENTIFIER_EXPORT', 'width' => 300, 'class' => 'fixed-width-ld', 'desc' => $this->l('Proszę wybrać identyfikator produktów / kombinacji produktów znajdujacy się w pierwszej kolumnie pliku CSV (musi być taki sam jak na produktach / kombinacjach w sklepie by możliwa była aktualizacja)'), 'options' => array( 'query' => array( array( 'id' => '1', 'name' => $this->l('Index (referencja)') ), array( 'id' => '2', 'name' => $this->l('Ean13') ), array( 'id' => '3', 'name' => $this->l('UPC') ), array( 'id' => '4', 'name' => $this->l('Id produktu') ), ), 'id' => 'id', 'name' => 'name', ) ), array( 'type' => 'select', 'label' => $this->l('Sklep'), 'name' => 'PD_CSVPU_UPDATE_SHOP_EXPORT', 'required' => true, 'desc' => $this->l('Wybierz sklep którego aktualizacja ma się dotyczyć'), 'options' => array( 'query' => Shop::getShops(true), 'id' => 'id_shop', 'name' => 'name' ) ), ), 'submit' => array( 'name' => 'btnSubmitConfig', 'title' => $this->l('Zapisz ustawienia'), ) ), ); $fields_form_4 = array( 'form' => array( 'legend' => array( 'title' => $this->l('Export do CSV'), 'icon' => 'icon-cogs' ), 'submit' => array( 'name' => 'btnDownloadCSVUpdate', 'title' => $this->l('Exportuj produkty z CSV'), ) ), ); $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->module = $this; $helper->identifier = $this->identifier; $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_0, $fields_form_1, $fields_form_2, $fields_form_3, $fields_form_4)); } public function getConfigFieldsValues() { return array( 'PD_CSVPU_ZERO_BASE_PRODUCT_PRICE' => Tools::getValue('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE', Configuration::get('PD_CSVPU_ZERO_BASE_PRODUCT_PRICE')), 'PD_CSVPU_CSV_DELIM_EXPORT' => Tools::getValue('PD_CSVPU_CSV_DELIM_EXPORT', Configuration::get('PD_CSVPU_CSV_DELIM_EXPORT')), 'PD_CSVPU_IDENTIFIER_EXPORT' => Tools::getValue('PD_CSVPU_IDENTIFIER_EXPORT', Configuration::get('PD_CSVPU_IDENTIFIER_EXPORT')), 'PD_CSVPU_UPDATE_SHOP_EXPORT' => Tools::getValue('PD_CSVPU_UPDATE_SHOP_EXPORT', Configuration::get('PD_CSVPU_UPDATE_SHOP_EXPORT')), 'PD_CSVPU_PRICES_WITH_TAX' => Tools::getValue('PD_CSVPU_PRICES_WITH_TAX', Configuration::get('PD_CSVPU_PRICES_WITH_TAX')), 'PD_CSVPU_SAME_CSV' => Tools::getValue('PD_CSVPU_SAME_CSV', Configuration::get('PD_CSVPU_SAME_CSV')), 'PD_CSVPU_UPDATE_SHOP' => Tools::getValue('PD_CSVPU_UPDATE_SHOP', Configuration::get('PD_CSVPU_UPDATE_SHOP')), 'PD_CSVPU_UPDATE_TYPE' => Tools::getValue('PD_CSVPU_UPDATE_TYPE', Configuration::get('PD_CSVPU_UPDATE_TYPE')), 'PD_CSVPU_HEADER_IGN' => Tools::getValue('PD_CSVPU_HEADER_IGN', Configuration::get('PD_CSVPU_HEADER_IGN')), 'PD_CSVPU_IDENTIFIER' => Tools::getValue('PD_CSVPU_IDENTIFIER', Configuration::get('PD_CSVPU_IDENTIFIER')), 'PD_CSVPU_CSV_FILE' => Tools::getValue('PD_CSVPU_CSV_FILE', Configuration::get('PD_CSVPU_CSV_FILE')), 'PD_CSVPU_CSV_DELIM' => Tools::getValue('PD_CSVPU_CSV_DELIM', Configuration::get('PD_CSVPU_CSV_DELIM')), 'PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS' => Tools::getValue('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS', Configuration::get('PD_CSVPU_UPDATE_MULTIPLE_IDENTIFIERS')) ); } public function renderList() { $fields_list = array( 'id_history_csv' => array( 'title' => $this->l('Id'), 'type' => 'text', 'orderby' => true, 'search' => false, ), 'file_name' => array( 'title' => $this->l('Nazwa pliku'), 'width' => 140, 'type' => 'text', 'search' => false, ), 'products_csv' => array( 'title' => $this->l('Produktów w CSV'), 'type' => 'text', 'search' => false, ), 'products_updated' => array( 'title' => $this->l('Zaktualizowanych prouktów'), 'type' => 'text', 'search' => false, ), 'products_attributes_updated' => array( 'title' => $this->l('Zaktualizowanych kombinacji'), 'type' => 'text', 'search' => false, ), 'products_not_found' => array( 'title' => $this->l('Nie znalezionych'), 'type' => 'text', 'search' => false, ), 'csv_delimeter' => array( 'title' => $this->l('Separator kolumn'), 'width' => 140, 'type' => 'text', 'search' => false, ), 'same_csv_allow' => array( 'title' => $this->l('Ten sam plik'), 'type' => 'bool', 'active' => 'same_csv_allow', 'search' => false, ), 'prices_with_tax' => array( 'title' => $this->l('Ceny z vat 23%'), 'type' => 'bool', 'active' => 'prices_with_tax', 'search' => false, ), 'csv_header_ignore' => array( 'title' => $this->l('Pomin nagłowek'), 'type' => 'bool', 'active' => 'csv_header_ignore', 'search' => false, ), 'update_type' => array( 'title' => $this->l('Typ aktualizacji'), 'type' => 'text', 'search' => false, ), 'products_identifier' => array( 'title' => $this->l('Identyfikator produktów'), 'type' => 'text', 'search' => false, ), 'date_add' => array( 'title' => $this->l('Data aktualizacji'), 'type' => 'text', 'search' => false, ), ); $helper_list = new HelperList(); $helper_list->module = $this; $helper_list->title = $this->l('Historia i parametry ostatnich aktualizacji'); $helper_list->shopLinkType = ''; $helper_list->no_link = true; $helper_list->show_toolbar = true; $helper_list->simple_header = false; $helper_list->identifier = 'id_history_csv'; $helper_list->table = 'merged'; $helper_list->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name; $helper_list->token = Tools::getAdminTokenLite('AdminModules'); /* Retrieve list data */ $content_list = $this->getListContent(); $helper_list->listTotal = count($content_list); /* Paginate the result */ $page = ($page = Tools::getValue('submitFilter' . $helper_list->table)) ? $page : 1; $pagination = ($pagination = Tools::getValue($helper_list->table . '_pagination')) ? $pagination : 50; $content_list = $this->paginateEntries($content_list, $page, $pagination); return $helper_list->generateList($content_list, $fields_list); } public function paginateEntries($content_list, $page = 1, $pagination = 50) { if (count($content_list) > $pagination) { $content_list = array_slice($content_list, $pagination * ($page - 1), $pagination); } return $content_list; } protected function getListContent() { $update_type = array( 1 => $this->l('Produkt bazowy'), 2 => $this->l('Kombinacje') ); $products_identifier = array( 1 => $this->l('Index (referencja) produktu lub kombinacji'), 2 => $this->l('Ean13 produktu lub kombinacji'), 3 => $this->l('UPC produktu lub kombinacji'), 4 => $this->l('Id produktu / id kombinacji produktu') ); $content = Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'pdcsvpriceupdate_history` ORDER BY `id_history_csv` DESC'); foreach ($content as $k => &$c) { $c['update_type'] = $update_type[$c['update_type']]; $c['products_identifier'] = $products_identifier[$c['products_identifier']]; } return $content; } public function getSqlWherePart($product_identifier) { switch ($this->products_identifier) { case '1': // index / reference return 'WHERE `reference` = \'' . $product_identifier . '\''; break; case '2': // ean13 return 'WHERE `ean13` = \'' . $product_identifier . '\''; break; case '3': // upc return 'WHERE `upc` = \'' . $product_identifier . '\''; break; case '4': // id_product return 'WHERE `id_product` = \'' . $product_identifier . '\''; break; default: break; } } private static function csvToArray($filename = '', $delimiter = ';', $header_ignore) { if (!file_exists($filename) || !is_readable($filename)) { return false; } $data = array(); if (($handle = fopen($filename, 'r')) !== false) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) { $data[] = array(self::autoUTF($row[0]) => self::autoUTF($row[1])); } fclose($handle); } if ($header_ignore) { unset($data[0]); } return $data; } private static function autoUTF($s) { // detect UTF-8 if (preg_match('#[\x80-\x{1FF}\x{2000}-\x{3FFF}]#u', $s)) return $s; // detect WINDOWS-1250 if (preg_match('#[\x7F-\x9F\xBC]#', $s)) return iconv('WINDOWS-1250', 'UTF-8', $s); // assume ISO-8859-2 return iconv('ISO-8859-2', 'UTF-8', $s); } public function parseAndUpdateProducts() { $id_shop = $this->csv_shop_update; $not_found = array(); $prod_csv_count = 0; $prod_upd_count = 0; $prod_attributes_upd_count = 0; $csv = self::csvToArray($this->csv_file, $this->csv_delimeter, $this->csv_header_ignore); foreach ($csv as $item) { foreach ($item as $product_identifier => $price) { $prod_csv_count++; $product_identifier = trim($product_identifier); $price = trim($price); $price = str_replace(',', '.', $price); if ($this->prices_with_tax) { $price = (float)$price / 1.23; $price = Tools::ps_round($price, 6); } if (isset($product_identifier) && $product_identifier != '') { if ($this->update_type == 1) { $sql = 'SELECT id_product FROM `' . _DB_PREFIX_ . 'product`'; $sql .= $this->getSqlWherePart(str_replace( "ďťż", "", $product_identifier)); $result = Db::getInstance()->executeS($sql); if ($result && sizeof($result) && !$this->update_multiple) { $id_product = (int)$result[0]['id_product']; $res = Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product` SET price = ' . (float)$price . ' WHERE id_product = ' . (int)$id_product); $res &= Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product_shop` SET price = ' . (float)$price . ' WHERE id_product = ' . (int)$id_product . ' AND id_shop = ' . (int)$id_shop); if ($res) { $prod_upd_count++; } } else { $not_found[] = $product_identifier; } if ($result && sizeof($result) >= 1 && $this->update_multiple) { foreach ($result as $r) { $id_product = (int)$r['id_product']; $res = Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product` SET price = ' . (float)$price . ' WHERE id_product = ' . (int)$id_product); $res &= Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product_shop` SET price = ' . (float)$price . ' WHERE id_product = ' . (int)$id_product . ' AND id_shop = ' . (int)$id_shop); if ($res) { $prod_upd_count++; } } } else { $not_found[] = $product_identifier; } } elseif ($this->update_type == 2) { $sql2 = 'SELECT id_product, id_product_attribute FROM `' . _DB_PREFIX_ . 'product_attribute`'; $sql2 .= $this->getSqlWherePart($product_identifier); $result2 = Db::getInstance()->executeS($sql2); if ($result2 && sizeof($result2) && !$this->update_multiple) { $id_product = (int)$result2[0]['id_product']; $id_product_attribute = (int)$result2[0]['id_product_attribute']; $res2 = Db::getInstance()->execute( 'UPDATE `' . _DB_PREFIX_ . 'product_attribute_shop` SET price = ' . (float)$price . ' WHERE id_product_attribute = ' . (int)$id_product_attribute . ' AND id_product = ' . (int)$id_product . ' AND id_shop = ' . (int)$id_shop ); if ($res2) { $prod_attributes_upd_count++; if ($this->zero_base_price) { Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product` SET price = 0 WHERE id_product = ' . (int)$id_product); Db::getInstance()->execute( 'UPDATE `' . _DB_PREFIX_ . 'product_shop` SET price = 0 WHERE id_product = ' . (int)$id_product . ' AND id_shop = ' . (int)$id_shop ); } } } else { $not_found[] = $product_identifier; } if ($result2 && sizeof($result2) > 1 && $this->update_multiple) { foreach ($result2 as $r) { $id_product = (int)$r['id_product']; $id_product_attribute = (int)$r['id_product_attribute']; $res2 = Db::getInstance()->execute( 'UPDATE `' . _DB_PREFIX_ . 'product_attribute_shop` SET price = ' . (float)$price . ' WHERE id_product_attribute = ' . (int)$id_product_attribute . ' AND id_product = ' . (int)$id_product . ' AND id_shop = ' . (int)$id_shop ); if ($res2) { $prod_attributes_upd_count++; if ($this->zero_base_price) { Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product` SET price = 0 WHERE id_product = ' . (int)$id_product); Db::getInstance()->execute( 'UPDATE `' . _DB_PREFIX_ . 'product_shop` SET price = 0 WHERE id_product = ' . (int)$id_product . ' AND id_shop = ' . (int)$id_shop ); } } } } else { $not_found[] = $product_identifier; } } } } } // create history entry $ho = new HistoryCsvPriceUpdateModel(); $ho->file_name = $this->csv_file_name; $ho->products_csv = $prod_csv_count; $ho->products_updated = $prod_upd_count; $ho->products_attributes_updated = $prod_attributes_upd_count; $ho->products_not_found = count($not_found); $ho->update_type = $this->update_type; $ho->products_identifier = $this->products_identifier; $ho->same_csv_allow = $this->same_csv_allow; $ho->csv_header_ignore = $this->csv_header_ignore; $ho->csv_delimeter = $this->csv_delimeter; $ho->date_add = date('Y-m-d H:i:s'); $ho->add(); // Save report from update not found references $this->generateAndSaveReport($not_found); } public function getProductsDBLight() { $out = array(); $id_shop = $this->csv_shop_update_export; $sql = 'SELECT DISTINCT p.`id_product`, p.`reference`, p.`ean13`, p.`upc`, product_shop.`price` FROM `' . _DB_PREFIX_ . 'product` p INNER JOIN `' . _DB_PREFIX_ . 'product_shop` product_shop ON (product_shop.id_product = p.id_product) WHERE product_shop.`id_shop` = ' . (int)$id_shop . ($only_active ? ' AND product_shop.`active` = 1' : '') . ' ORDER BY p.`id_product` '; $products = Db::getInstance()->executeS($sql); //d($products); foreach ($products as $product) { $out[] = array( 'identifier' => $this->getidentifierExport($product), 'price' => $product['price'] ); } return $out; } public function getidentifierExport($product) { switch ($this->products_identifier_export) { case '1': // index / reference return $product['reference']; break; case '2': // ean13 return $product['ean13']; break; case '3': // upc return $product['upc']; break; case '4': // id_product return $product['id_product']; break; default: break; } } private function exportCSVProducts() { $products = $this->getProductsDBLight(); //d($products); $date = date('m-d-Y_h-i'); $out = array(); // Clean duplicate references of products in array $nb = count($result); if ($nb == 1) { $this->html .= $this->displayConfirmation($this->l('Wszystkie produkty zostały zaktualizowane')); } elseif ($fd = @fopen(dirname(__FILE__) . '/' . $date . '_' . $this->file_export, 'w')) { $header = array('identifier', 'price'); $array_to_export = array_merge(array($header), $products); foreach ($array_to_export as $tab) { $this->_my_fputcsvexport($fd, $tab); } fclose($fd); $this->html .= $this->displayConfirmation(sprintf($this->l('Plik CSV został utworzony zapisano w nim %d rekordów produktów których znaleziono w sklepie'), $nb) . '
' . $this->l('Pobierz plik') . ' ' . $date . '_' . $this->file_export . ''); } else { $this->html .= $this->displayError($this->l('Bład: nie mogę zapisac pliku') . ' ' . dirname(__FILE__) . '/' . $date . '_' . $this->file_export . ' !'); } } private function generateAndSaveReport($not_found) { $date = date('m-d-Y_h-i'); $out = array(); // Clean duplicate references of products in array $not_found = array_unique($not_found); foreach ($not_found as $item) { $out[] = array('identifier' => $item); } $header = array('identifier'); $result = (is_array($out) ? array_merge(array($header), $out) : $header); $nb = count($result); if ($nb == 1) { $this->html .= $this->displayConfirmation($this->l('Wszystkie produkty zostały zaktualizowane')); } elseif ($fd = @fopen(dirname(__FILE__) . '/' . $date . '_' . $this->file, 'w')) { foreach ($result as $tab) { $this->_my_fputcsv($fd, $tab); } fclose($fd); $this->html .= $this->displayConfirmation(sprintf($this->l('Plik raportu z aktualizacji został utworzony zapisano w nim %d rekordów identifikatorów produktów lub kombinacji produktów których nie znaleziono w sklepie'), $nb) . '
' . $this->l('Pobierz plik') . ' ' . $date . '_' . $this->file . ''); } else { $this->html .= $this->displayError($this->l('Bład: nie mogę zapisac pliku') . ' ' . dirname(__FILE__) . '/' . $date . '_' . $this->file . ' !'); } } private function _my_fputcsvexport($fd, $array) { $line = implode($this->csv_delimeter_export, $array); $line .= "\n"; if (!fwrite($fd, $line, 4096)) { $this->postErrors[] = $this->l('Bład: nie mogę zapisać') . ' ' . dirname(__FILE__) . '/' . $this->file . ' !'; } } private function _my_fputcsv($fd, $array) { $line = implode(';', $array); $line .= "\n"; if (!fwrite($fd, $line, 4096)) { $this->postErrors[] = $this->l('Bład: nie mogę zapisać') . ' ' . dirname(__FILE__) . '/' . $this->file . ' !'; } } }