= 10 ){ echo '

'.$this->l('Association automatique en cours, réessayez plus tard').'


'; } else { $min_id_product = Db::getInstance()->getValue(' SELECT MIN(`id_product`) FROM `'._DB_PREFIX_.'product` WHERE `date_add` > DATE_SUB(NOW(), INTERVAL 10 DAY) '); if ($min_id_product) { Db::getInstance()->ExecuteS(' DELETE FROM `'._DB_PREFIX_.'product_ps_cache` WHERE `id_product` >= '.$min_id_product.' '); Db::getInstance()->ExecuteS(' INSERT IGNORE INTO `'._DB_PREFIX_.'product_ps_cache` ( SELECT p.id_product, IFNULL( ( SELECT s.id_sale FROM `'._DB_PREFIX_.'privatesale_category` s WHERE s.`id_category` = p.`id_category_default` LIMIT 1) , 0 ) FROM `'._DB_PREFIX_.'product` p WHERE p.`id_product` >= '.$min_id_product.' ) '); echo '

'.$this->l('Produits associés aux ventes depuis le produit #').$min_id_product.'


'; } else { echo '

'.$this->l('Pas de produit ajouté depuis 10 jours').'


'; } } } elseif(Tools::isSubmit('submitPhilea')) { $id_sale = (int) Tools::getValue('id_sale'); if($id_sale) { if(!Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'philea_sync` WHERE `id_sale` = '.(int) $id_sale.' AND `status` = 0 ')) { Db::getInstance()->execute(' INSERT INTO `'._DB_PREFIX_.'philea_sync` VALUES ( DEFAULT, '.(int) $id_sale.', 0, NOW() ) '); // no REC on BBB // if (Tools::getValue('force_rec_file')){ // // get last sync order form for this sale // $last_order_sync = Db::getInstance()->getRow( // 'SELECT * FROM `' . _DB_PREFIX_ . 'philea_supplier_order_sync` // WHERE `id_sale` = ' . (int) $id_sale . ' // ORDER BY `date_add` DESC'); // if ($last_order_sync && isset($last_order_sync['id_order_form'])){ // Db::getInstance()->execute(' // DELETE FROM `' . _DB_PREFIX_ . 'philea_supplier_order_sync` // WHERE `id_sale` = ' . (int) $id_sale . ' // AND `id_order_form` = ' . (int) $last_order_sync['id_order_form'] . ' // ORDER BY `date_add` DESC // LIMIT 1'); // } // } echo '

'.$this->l('Sale added to queue successfully').'


'; } else { echo '

'.$this->l('Sync already in progress for this sale').'


'; } } else { echo '

'.$this->l('Invalid sale id').'


'; } } elseif (Tools::isSubmit('auto_sync') && Tools::isSubmit('active')){ $id_sale = (int) Tools::getValue('id_sale'); if($id_sale) { $active = (int) Tools::getValue('active'); $sql = ' INSERT INTO `'._DB_PREFIX_.'philea_auto_sync` VALUES ( '.(int) $id_sale.', '.(int) $active.', "0000-00-00 00:00:00" ) ON DUPLICATE KEY UPDATE `active` = '.(int) $active; if(Db::getInstance()->execute($sql)){ echo '

'.$this->l('Export auto activé').'


'; } else { echo '

'.$this->l('Export auto désactivé').'


'; } } else { echo '

'.$this->l('Invalid sale id').'


'; } } elseif (Tools::isSubmit('addCDC') && Tools::getValue('id_sale')){ $id_sale = (int) Tools::getValue('id_sale'); if($id_sale) { if(Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'philea_sync` WHERE `id_sale` = '.(int) $id_sale.' AND (`status` = 2 OR `status` = 4) ')) { Db::getInstance()->ExecuteS(' UPDATE `'._DB_PREFIX_.'philea_sync` SET `status` = 3 WHERE id_sale = '.(int) $id_sale.' LIMIT 1 '); echo '

'.$this->l('CDC for this Sale added to queue successfully').'


'; } else { echo '

'.$this->l('CDC Sync already in progress for this sale').'


'; } } else { echo '

'.$this->l('Invalid sale id').'


'; } } elseif(Tools::isSubmit('getCsv') && Tools::getValue('id_sale') && Tools::getValue('filename')){ $id_sale = (int) Tools::getValue('id_sale'); $filename = Tools::getValue('filename'); $this->exportCsv((int) $id_sale, $filename); } elseif(Tools::isSubmit('philea_archive_submit')){ $id_sales = Tools::getValue('id_sales'); if (count($id_sales)){ $sql_values = array(); $sql_insert = 'INSERT INTO `'._DB_PREFIX_.'philea_archive` (`id_sale`, `date_add`) VALUES'; foreach ($id_sales as $id_sale) $sql_values[] = '(' . (int) $id_sale . ', NOW())'; $sql_insert .= implode(', ', $sql_values) . ' ON DUPLICATE KEY UPDATE `date_add` = NOW()'; if (Db::getInstance()->execute($sql_insert)){ echo '

'.$this->l('Sales successfully archived').'


'; } else{ echo '

'.$this->l('Sales cannot be archived').'


'; } } else{ echo '

'.$this->l('No sale selected.').'


'; } } elseif(Tools::isSubmit('unarchiveSale') && Tools::getValue('id_sale_unarchive')){ if (Db::getInstance()->executeS(' DELETE FROM `' . _DB_PREFIX_ . 'philea_archive` WHERE `id_sale` = ' . (int) Tools::getValue('id_sale_unarchive') . ' LIMIT 1 ')){ echo '

'.$this->l('Vente désarchivée').'


'; } else{ echo '

'.$this->l('La vente n\'a pas pu être désarchivée.').'


'; } } } public function exportCsv($id_sale, $filename){ $sql = ' SELECT * FROM `'._DB_PREFIX_.'philea_reception` WHERE `id_sale` = ' . (int) $id_sale . ' AND `filename` LIKE "' . pSQL($filename) . '"'; $recept = Db::getInstance()->getRow($sql); if (!$recept) return; $Y = date('Y', strtotime($recept['date_add'])); $m = date('m', strtotime($recept['date_add'])); $filepath = dirname(__FILE__).'/script/archives/IN/RECEPTION/'; $filepath .= $Y . '/' . $m . '/' . $recept['filename']; $content = file_get_contents( $filepath ); $lines = preg_split( '@\n@', $content ); header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Description: File Transfer'); header('Content-Type: application/csv; charset=utf-8'); header('Content-Disposition: attachment; filename='.$filename.'_'.(int)$id_sale.'.csv;'); header('Content-Transfer-Encoding: binary'); $format = array( 'OP_CODE' => array(1,10), 'CODE_SOC' => array(11,20), 'N_CDE' => array(31,20), 'DATE_RECEP' => array(51,8), 'CODE_ART' => array(59,50), 'QTE' => array(109,10), 'OTHER' => array(119, 28), ); ob_clean(); //open file pointer to standard output $fp = fopen('php://output', 'w'); $delim = ';'; // first row $data=array(); foreach ($format as $col_name => $array_def) $data[]=Tools::htmlentitiesDecodeUTF8($col_name); fputcsv ($fp,$data,$delim); foreach( $lines as $line ) { $data = array(); foreach($format as $field => $value) { $data[] = substr($line, ($value[0]-1), $value[1]); } fputcsv ($fp,array_map('utf8_decode',array_values($data)),$delim); } fclose($fp); exit; } public function display() { global $cookie, $currentIndex; $form = ''; $form .= '

'.$this->l('Philea').'

'; $base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminPhileaMagistor'); $id_sale_options = array(); foreach(Db::getInstance()->ExecuteS(' SELECT p.`id_sale`, c.`name`, c.`id_category` FROM `'._DB_PREFIX_.'privatesale` p LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON (c.`id_category` = p.`id_category`) WHERE c.`id_lang` = '.$cookie->id_lang.' AND p.`date_start` > "2015-01-01 00:00:00" ORDER BY p.`id_sale` DESC ') as $row) { $extrafields = Category::getSalesInfos(array((int) $row['id_category'])); $id_sale_options[] = array( 'label' => (int) $row['id_sale'].' - '.$row['name'].(empty($extrafields[(int) $row['id_category']]['sales'][1])?'':' - '.$extrafields[(int) $row['id_category']]['sales'][1]) , 'value' => (int) $row['id_sale'] ); } $helperForm = new HelperForm(); $helperForm->_forms = array( array( 'action' => $base_link, 'legend' => $this->l('Association Vente - Produits'), 'actions' => array( array( 'type' => 'submit', 'name' => 'submitProductSaleCache', 'label' => $this->l('Associer les produits aux ventes') ) ) ) ); $form .= $helperForm->renderForm(false, NULL, NULL, true); $helperForm = new HelperForm(); $helperForm->_forms = array( array( 'action' => $base_link, 'legend' => $this->l('Philea'), 'inputs' => array( array( 'type' => 'select-styled', 'class-select' => 'bg-purple-light big-select', 'name' => 'id_sale', 'label' => $this->l('Select a sale'), 'options' => $id_sale_options, 'filter' => true, 'filter_text' => $this->l('Filter par vente') ), // array( // 'type' => 'checkbox', // 'name' => 'force_rec_file', // 'label' => $this->l('Forcer l\'envoi'), // 'text' => $this->l('Force un nouvel envoi du fichier REC.') // ) ), 'actions' => array( array( 'type' => 'submit', 'name' => 'submitPhilea', 'label' => $this->l('Envoyer à Philea') ) ) ) ); $form .= $helperForm->renderForm(false, NULL, NULL, true); $CRR_list = $this->getCRRList(); $form .= '
 
'.$this->l('Orders CRR').'

'.$this->l('Historique des ventes envoyées à Philea'). (self::TIME_DISPLAY > 0 ? ' (' . self::TIME_DISPLAY . ' ' . $this->l('derniers jours') . ')' : '').'

'; $dowload_link = _MODULE_DIR_.'philea_magistor/script/archives/IN/RECEPTION/'; $upload_link = $base_link; $now = date('Y-m-d H:i:s', mktime(date('H') -1)); foreach ($CRR_list as $CRR) { $_recep = FALSE; if (isset($CRR['recep_date']) && $CRR['recep_date'] && isset($CRR['filename']) && $CRR['filename']){ $_recep = TRUE; $Y = date('Y', strtotime($CRR['recep_date'])); $m = date('m', strtotime($CRR['recep_date'])); $dl_link = $dowload_link . $Y . '/' . $m . '/' . $CRR['filename']; $dl_csv = $base_link . '&getCsv&id_sale='.(int) $CRR['id_sale'].'&filename='.$CRR['filename']; $dl_link = ''.$this->l('.DAT').'   '; //$dl_link .= ''.$this->l('.CSV').''; } else{ $CRR['recep_date'] = '--'; $dl_link = ''; } $auto_sync = (isset($CRR['auto_sync_active']) && $CRR['auto_sync_active']) ? true : false; $title = ($auto_sync ? $this->l('Désactiver l\'envoi automatique pour la vente') : $this->l('Activer l\'envoi automatique pour la vente') ); if (isset($CRR['date_end']) && $CRR['date_end'] >= $now) $auto_sync_btn = ''; else $auto_sync_btn = ''; $submit_link = $upload_link;// . '&id_sale=' . (int) $CRR['id_sale'] . '&addCDC=1'; $art_reports = Db::getInstance()->executeS(' SELECT `report_data`, `report_date`, `id_sale` FROM `'._DB_PREFIX_.'philea_syncreport` WHERE `id_sale` = '.(int) $CRR['id_sale'] .' AND `filename` LIKE "ART%" AND `report_data` IS NOT NULL '); $cdc_reports = Db::getInstance()->executeS(' SELECT `report_data`, `report_date`, `id_sale` FROM `'._DB_PREFIX_.'philea_syncreport` WHERE `id_sale` = '.(int) $CRR['id_sale'] .' AND `filename` LIKE "CDC%" AND `report_data` IS NOT NULL '); $crp_reports = Db::getInstance()->executeS(' SELECT `report_data`, `report_date`, `id_sale` FROM `'._DB_PREFIX_.'philea_syncreport` WHERE `id_sale` = '.(int) $CRR['id_sale'] .' AND `filename` LIKE "CRP%" AND `report_data` IS NOT NULL '); $form .= ''; } $form .= '
' . $this->l('ID sale') . ' ' . $this->l('Name') . ' ' . $this->l('Date envoi (ART)') . ' ' . $this->l('Envoyer la commande') . ' ' . $this->l('Date envoi (CMD)') . ' ' . $this->l('Préparation (CRP)') . ' ' . $this->l('Envoi auto') . '

'.(!empty($CRR['sync_date'])?' '.date('d/m/Y H:i',strtotime($CRR['sync_date'])):'--').'

'; if(!empty($art_reports)){ foreach($art_reports as $key => $report) { $form .= '
'.date('d/m/Y H:i',strtotime($report['report_date'])).'
'; } } $form .= '
'.$this->l('Envoyer à Philea').'

'.(!empty($CRR['sent_date'])?' '.date('d/m/Y H:i',strtotime($CRR['sent_date'])):'--').'

'; if(!empty($cdc_reports)){ $form .= ''; } $form .= '
'; if(!empty($crp_reports)){ $form .= '

'.date('d/m/Y H:i',strtotime($crp_reports[0]['report_date'])).'

'; } $form .= '
'.$auto_sync_btn.'

'; $form .= $this->displayArchives(); echo $form; } /** * Get CRR files for not sent sales */ public function getCRRList(){ if (isset(self::$_crrlist_cache) && self::$_crrlist_cache) return self::$_crrlist_cache; // Get CRR if CDC has not been sent OR sync_date < TIME_DISPLAY DAYs $id_sales = array(); $pm_sync = array(); $sql = ' SELECT pm_sync.`id_sale`, MAX(pm_sync.`date_add`) as `sync_date` FROM `'._DB_PREFIX_.'philea_sync` pm_sync LEFT JOIN `'._DB_PREFIX_.'philea_archive` pm_arch ON pm_arch.`id_sale` = pm_sync.`id_sale` WHERE 1 AND `pm_arch`.`id_sale` IS NULL GROUP BY pm_sync.`id_sale` ' . ((int) self::TIME_DISPLAY > 0 ? 'HAVING `sync_date` > DATE_SUB(NOW(), INTERVAL ' . self::TIME_DISPLAY . ' DAY)' : '') . ' ORDER BY `sync_date` DESC'; foreach (Db::getInstance()->executeS($sql) as $row) { $id_sales[] = (int) $row['id_sale']; $pm_sync[(int) $row['id_sale']] = $row['sync_date']; } $receptions = array(); $sql = ' SELECT pm_r.`id_sale`, pm_r.`filename`, MAX(pm_r.`date_add`) as `recep_date` FROM `'._DB_PREFIX_.'philea_reception` pm_r WHERE `id_sale` IN (' . implode(', ', $id_sales) . ') GROUP BY `id_sale`'; foreach (Db::getInstance()->executeS($sql) as $row) $receptions[$row['id_sale']] = $row; $sales = array(); $sql = ' SELECT p.`id_sale`, c.`name`, c.`id_category`, p.`date_start`, p.`date_end` FROM `'._DB_PREFIX_.'privatesale` p LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON c.`id_category` = p.`id_category` AND c.`id_lang` = 2 WHERE p.`id_sale` IN (' .implode(', ', $id_sales) . ')'; foreach (Db::getInstance()->executeS($sql) as $row) $sales[(int) $row['id_sale']] = $row; $pm_autosales = array(); $sql = ' SELECT pm_as.`id_sale`, pm_as.`active` as `auto_sync_active` FROM `'._DB_PREFIX_.'philea_auto_sync` pm_as WHERE `id_sale` IN (' . implode(', ', $id_sales) . ') AND pm_as.`active` = 1'; foreach (Db::getInstance()->executeS($sql) as $row) $pm_active[(int) $row['id_sale']] = (int) $row['auto_sync_active']; $pm_sent = array(); $sql = ' SELECT pm_s.`id_sale`, MAX(pm_s.`date_add`) as `sent_date` FROM `'._DB_PREFIX_.'philea_sent` pm_s WHERE pm_s.`id_sale` IN (' . implode(', ', $id_sales) . ') GROUP BY pm_s.`id_sale`'; foreach (Db::getInstance()->executeS($sql) as $row) $pm_sent[(int) $row['id_sale']] = $row['sent_date']; $CRR_list = array(); foreach ($id_sales as $id_sale) { $sync_date = (isset($pm_sync[(int) $id_sale]) ? $pm_sync[(int) $id_sale] : ''); $recep = (isset($receptions[(int) $id_sale]) ? $receptions[(int) $id_sale] : array('filename' => '', 'recep_date' => '')); $sale = (isset($sales[(int) $id_sale]) ? $sales[(int) $id_sale] : array('name' => '', 'date_start' => '', 'date_end' => '')); $sent_date = (isset($pm_sent[(int) $id_sale]) ? $pm_sent[(int) $id_sale] : ''); $as_active = (isset($pm_active[(int) $id_sale])) ? 1 : 0; $extrafields = Category::getSalesInfos(array((int) $sales[(int) $id_sale]['id_category'])); $CRR_list[$id_sale] = array( 'id_sale' => $id_sale, 'sync_date' => $sync_date, 'filename' => $recep['filename'], 'recep_date' => $recep['recep_date'], 'name' => $sale['name'], 'sent_date' => $sent_date, 'auto_sync_active' => $as_active, 'date_start' => $sale['date_start'], 'date_end' => $sale['date_end'], 'subtitle' => $extrafields[(int) $sales[(int) $id_sale]['id_category']]['sales'][1] ); } self::$_crrlist_cache = $CRR_list; return self::$_crrlist_cache; // $CRR_files = Db::getInstance()->executeS(' // SELECT // pmsync.id_sale, // MAX(pmsync.`date_add`) as `sync_date`, // pmr.`filename`, // MAX(pmr.`date_add`) as `recep_date`, // c.`name`, // MAX(pms.date_add) as `sent_date`, // pmas.`active` as `auto_sync_active`, // p.date_start, // p.date_end // FROM `'._DB_PREFIX_.'philea_sync` pmsync // LEFT JOIN `'._DB_PREFIX_.'philea_sent` pms // ON pms.`id_sale` = pmsync.`id_sale` // LEFT JOIN `'._DB_PREFIX_.'privatesale` p // ON p.`id_sale` = pmsync.`id_sale` // LEFT JOIN `'._DB_PREFIX_.'category_lang` c // ON c.`id_category` = p.`id_category` // LEFT JOIN `'._DB_PREFIX_.'philea_reception` pmr // ON pmsync.`id_sale` = pmr.`id_sale` // LEFT JOIN `'._DB_PREFIX_.'philea_auto_sync` pmas // ON pmas.`id_sale` = pmsync.`id_sale` // LEFT JOIN `'._DB_PREFIX_.'philea_archive` pm_arch // ON pm_arch.`id_sale` = pmsync.`id_sale` // WHERE 1 // AND c.id_lang = 2 // AND `pm_arch`.id_sale IS NULL // ' . ((int) self::TIME_DISPLAY > 0 ? 'AND pmsync.`date_add` > DATE_SUB(NOW(), INTERVAL ' . self::TIME_DISPLAY . ' DAY)' : '') . ' // GROUP BY pmsync.`id_sale` // ' . ((int) self::TIME_DISPLAY > 0 ? ' // HAVING ( // MAX(pms.date_add) IS NULL // OR MAX(pmsync.`date_add`) > DATE_SUB(NOW(), INTERVAL ' . self::TIME_DISPLAY . ' DAY) // OR pmsync.id_sale = 6304 // )' : '') . ' // ORDER BY `sync_date` DESC, `recep_date` DESC'); // self::$_crrlist_cache = $CRR_files; // return self::$_crrlist_cache; } public function displayArchives(){ global $cookie, $currentIndex; $base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminPhileaMagistor'); $archives_options = array(); foreach(Db::getInstance()->executeS(' SELECT a.`id_sale`, cl.`name` FROM `' . _DB_PREFIX_ . 'philea_archive` a LEFT JOIN `' . _DB_PREFIX_ . 'privatesale` p ON p.`id_sale` = a.`id_sale` LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON cl.`id_category` = p.`id_category` AND cl.`id_lang` = ' . Configuration::get('PS_LANG_DEFAULT') . ' ') as $row){ $archives_options[] = array( 'label' => (int) $row['id_sale'].' - '.$row['name'], 'value' => (int) $row['id_sale'] ); } $helperForm = new HelperForm(); $helperForm->_forms = array( array( 'action' => $base_link, 'legend' => $this->l('Archives'), 'inputs' => array( array( 'type' => 'select-styled', 'class-select' => 'bg-purple-light big-select', 'name' => 'id_sale_unarchive', 'label' => $this->l('Désarchiver une vente'), 'options' => $archives_options, 'filter' => true, 'filter_text' => $this->l('Filter par vente') ) ), 'actions' => array( array( 'type' => 'submit', 'name' => 'unarchiveSale', 'label' => $this->l('Désarchiver la vente') ) ) ) ); return $helperForm->renderForm(false, NULL, NULL, true); } }