Merge branch 'ticket-14436-excel-format' into develop

This commit is contained in:
David 2018-01-24 16:52:07 +01:00
commit bbe237f64e
2 changed files with 542 additions and 332 deletions

View File

@ -5,15 +5,20 @@ include_once(_PS_ROOT_DIR_.'/modules/ant_wp/GenerateWp.php');
require_once _PS_PHPEXCEL_DIR_.'Classes/PHPExcel.php';
require_once _PS_PHPEXCEL_DIR_.'Classes/PHPExcel/IOFactory.php';
ini_set('memory_limit', '8G');
ini_set('max_execution_time', 300);
class AdminAntWp extends AdminTab
{
const DUPLICATED_EANS = 'duplicated_ean';
const DUPLICATED_REFS = 'duplicated_refs';
protected $_html;
public $module_name;
public $config_tab;
public $controller;
public $helperForm;
public $_object;
public $report;
public function __construct($config_tab = true)
{
@ -26,6 +31,9 @@ class AdminAntWp extends AdminTab
$this->controller = 'AdminAntWp';
}
$this->helperForm = new HelperFormBootstrap();
$this->report = array();
$this->report[self::DUPLICATED_EANS] = array();
$this->report[self::DUPLICATED_REFS] = array();
// $this->helperForm->_select2 = true;
// $this->helperForm->_inputSwitch = true;
}
@ -101,6 +109,8 @@ class AdminAntWp extends AdminTab
fgetcsv($f, 0, ';');
$products = array();
$product_info = array();
$timer_table = array();
$callStartTime = microtime(true);
$refs = array();
$eans = array();
while($line = fgetcsv($f, 0, ';')) {
@ -109,191 +119,215 @@ class AdminAntWp extends AdminTab
}
if($line[0] != '') {
$ean = trim($line[0]);
$products[$ean] = array(
'ean' => $ean,
'supplier_ref' => trim((String)utf8_encode($line[1])),
'name_ha' => trim((String)utf8_encode($line[2])),
'quantity' => trim($line[3]),
'ppc' => trim((float)$line[4]),
'prix_ha' => trim((float)$line[5]),
'name_attribute_ha' => trim((String)$line[6])
);
if(!isset($products[$ean])){
$products[$ean] = array(
'ean' => $ean,
'supplier_ref' => trim((String)utf8_encode($line[1])),
'name_ha' => trim((String)utf8_encode($line[2])),
'quantity' => trim($line[3]),
'ppc' => trim((float)$line[4]),
'prix_ha' => trim((float)$line[5]),
'name_attribute_ha' => trim((String)$line[6])
);
}else{
$this->report[self::DUPLICATED_EANS][] = $ean;
}
$eans[] = $ean;
} else {
$ref = trim((String)utf8_encode($line[1]));
$products[$ref] = array(
'ean' => trim($line[0]),
'supplier_ref' => $ref,
'name_ha' => trim((String)utf8_encode($line[2])),
'quantity' => trim($line[3]),
'ppc' => trim((float)$line[4]),
'prix_ha' => trim((float)$line[5]),
'name_attribute_ha' => trim((String)utf8_encode($line[6]))
);
if(!isset($products[$ref])) {
$products[$ref] = array(
'ean' => trim($line[0]),
'supplier_ref' => $ref,
'name_ha' => trim((String)utf8_encode($line[2])),
'quantity' => trim($line[3]),
'ppc' => trim((float)$line[4]),
'prix_ha' => trim((float)$line[5]),
'name_attribute_ha' => trim((String)utf8_encode($line[6]))
);
}else{
$this->report[self::DUPLICATED_REFS][] = $ref;
}
$refs[] = $ref;
}
}
$product_info = array();
$product_found = 0;
$ids = array();
$exclude_ids = $this->getBraderieProducts();
foreach (Db::getInstance()->executeS('
SELECT p.`id_product`, p.`id_tax_rules_group`, p.`id_category_default`, p.`ean13`, p.`price`, p.`wholesale_price`,
p.`reference`, p.`supplier_reference`, p.`weight`, p.`height`, p.`width`, p.`depth`, c.`position`, cu.`nc8`, cu.`id_country`
FROM
`'._DB_PREFIX_.'product` p
INNER JOIN (
SELECT date_add, max(id_product) as maxid
FROM `'._DB_PREFIX_.'product`
WHERE id_product NOT IN ('.implode(',',$exclude_ids).')
GROUP BY ean13
) as b
ON p.`id_product` = b.maxid
LEFT JOIN `'._DB_PREFIX_.'category_product` c
ON (b.maxid = c.`id_product` AND p.`id_category_default` = c.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'product_customs` cu
ON (cu.`id_product` = p.`id_product`)
WHERE p.`ean13` IN ("'.implode('","',$eans).'")
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
') as $row) {
if($row['ean13']!='' && isset($products[$row['ean13']])){
$products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
$product_found++;
$ids[] = (int)$row['id_product'];
}
// elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
// $products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
// $product_found++;
// $ids[] = (int)$row['id_product'];
// }
}
$timer_table[] = 'Excluded products gathered at '.(microtime(true)-$callStartTime);
if(count($eans) > 0) {
$sql = '
SELECT p.`id_product`, p.`id_tax_rules_group`, p.`id_category_default`, p.`ean13`, p.`price`, p.`wholesale_price`,
p.`reference`, p.`supplier_reference`, p.`weight`, p.`height`, p.`width`, p.`depth`, c.`position`, cu.`nc8`, cu.`id_country`
FROM
`' . _DB_PREFIX_ . 'product` p
INNER JOIN (
SELECT date_add, max(id_product) as maxid
FROM `' . _DB_PREFIX_ . 'product`
WHERE id_product NOT IN (' . implode(',', $exclude_ids) . ')
GROUP BY ean13
) as b
ON p.`id_product` = b.maxid
LEFT JOIN `' . _DB_PREFIX_ . 'category_product` c
ON (b.maxid = c.`id_product` AND p.`id_category_default` = c.`id_category`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_customs` cu
ON (cu.`id_product` = p.`id_product`)
WHERE p.`ean13` IN ("' . implode('","', $eans) . '")
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
';
$result = Db::getInstance()->executeS($sql);
foreach (Db::getInstance()->executeS('
foreach ($result as $row) {
if($row['ean13'] == '8427077902761'){
$timer_table[] = json_encode($row);
}
if ($row['ean13'] != '' && isset($products[$row['ean13']])) {
$products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
$product_found++;
$ids[] = (int)$row['id_product'];
}
// elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
// $products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
// $product_found++;
// $ids[] = (int)$row['id_product'];
// }
}
}
$timer_table[] = 'Ean product gathered at '.(microtime(true)-$callStartTime);
if(count($refs)> 0) {
foreach (Db::getInstance()->executeS('
SELECT p.`id_product`, p.`id_tax_rules_group`, p.`id_category_default`, p.`ean13`, p.`price`, p.`wholesale_price`,
p.`reference`, p.`supplier_reference`, p.`weight`, p.`height`, p.`width`, p.`depth`, c.`position`, cu.`nc8`, cu.`id_country`
FROM
`'._DB_PREFIX_.'product` p
`' . _DB_PREFIX_ . 'product` p
INNER JOIN (
SELECT date_add, max(id_product) as maxid
FROM `'._DB_PREFIX_.'product`
WHERE id_product NOT IN ('.implode(',',$exclude_ids).')
FROM `' . _DB_PREFIX_ . 'product`
WHERE id_product NOT IN (' . implode(',', $exclude_ids) . ')
GROUP BY supplier_reference
) as b
ON p.`id_product` = b.maxid
LEFT JOIN `'._DB_PREFIX_.'category_product` c
LEFT JOIN `' . _DB_PREFIX_ . 'category_product` c
ON (b.maxid = c.`id_product` AND p.`id_category_default` = c.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'product_customs` cu
LEFT JOIN `' . _DB_PREFIX_ . 'product_customs` cu
ON (cu.`id_product` = p.`id_product`)
WHERE p.`supplier_reference` IN ("'.implode('","',$refs).'")
WHERE p.`supplier_reference` IN ("' . implode('","', $refs) . '")
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
') as $row) {
// if($row['ean13']!='' && isset($products[$row['ean13']])){
// $products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
// $product_found++;
// $ids[] = (int)$row['id_product'];
// } else
if($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
$products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
$product_found++;
$ids[] = (int)$row['id_product'];
// if($row['ean13']!='' && isset($products[$row['ean13']])){
// $products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
// $product_found++;
// $ids[] = (int)$row['id_product'];
// } else
if ($row['supplier_reference'] != '' && isset($products[$row['supplier_reference']])) {
$products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
$product_found++;
$ids[] = (int)$row['id_product'];
}
}
}
foreach (Db::getInstance()->executeS('
$timer_table[] = 'Ref product gathered at '.(microtime(true)-$callStartTime);
if(count($eans) > 0) {
foreach (Db::getInstance()->executeS('
SELECT p.`id_product`, p.`id_tax_rules_group`, p.`id_category_default`, b.`id_product_attribute`, b.`ean13`, b.`supplier_reference` as at_ref, p.`price`, p.`wholesale_price`,
p.`reference`, p.`supplier_reference`, p.`weight`, p.`height`, p.`width`, p.`depth`, c.`position`, cu.`nc8`, cu.`id_country`,
al.`name` as attribute_name
FROM
`'._DB_PREFIX_.'product` p
`' . _DB_PREFIX_ . 'product` p
INNER JOIN (
SELECT max(id_product) as maxid, ean13, `supplier_reference`, `id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute`
WHERE id_product NOT IN ('.implode(',',$exclude_ids).')
FROM `' . _DB_PREFIX_ . 'product_attribute`
WHERE id_product NOT IN (' . implode(',', $exclude_ids) . ')
GROUP BY ean13
) as b
ON p.`id_product`= b.maxid
LEFT JOIN `'._DB_PREFIX_.'category_product` c
LEFT JOIN `' . _DB_PREFIX_ . 'category_product` c
ON (b.maxid = c.`id_product` AND p.`id_category_default` = c.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'product_customs` cu
LEFT JOIN `' . _DB_PREFIX_ . 'product_customs` cu
ON (cu.`id_product` = p.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` ac
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` ac
ON (ac.`id_product_attribute` = b.`id_product_attribute`)
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al
ON (al.`id_attribute` = ac.`id_attribute` AND al.`id_lang` = 2)
WHERE b.`ean13` IN ("'.implode('","',$eans).'")
WHERE b.`ean13` IN ("' . implode('","', $eans) . '")
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
') as $row) {
if($row['ean13']!='' && isset($products[$row['ean13']])){
$products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
$product_found++;
if(!in_array((int)$row['id_product'],$ids)){
$ids[] = (int)$row['id_product'];
if ($row['ean13'] != '' && isset($products[$row['ean13']])) {
$products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
$product_found++;
if (!in_array((int)$row['id_product'], $ids)) {
$ids[] = (int)$row['id_product'];
}
}
// elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
// $products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
// $product_found++;
// if(!in_array((int)$row['id_product'],$ids)){
// $ids[] = (int)$row['id_product'];
// }
// } elseif($row['at_ref']!='' && isset($products[$row['at_ref']])){
// $products[$row['at_ref']] = array_merge($products[$row['at_ref']], $row);
// $product_found++;
// if(!in_array((int)$row['id_product'],$ids)){
// $ids[] = (int)$row['id_product'];
// }
// }
}
// elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
// $products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
// $product_found++;
// if(!in_array((int)$row['id_product'],$ids)){
// $ids[] = (int)$row['id_product'];
// }
// } elseif($row['at_ref']!='' && isset($products[$row['at_ref']])){
// $products[$row['at_ref']] = array_merge($products[$row['at_ref']], $row);
// $product_found++;
// if(!in_array((int)$row['id_product'],$ids)){
// $ids[] = (int)$row['id_product'];
// }
// }
}
foreach (Db::getInstance()->executeS('
$timer_table[] = 'Ean product attr gathered at '.(microtime(true)-$callStartTime);
if(count($refs) > 0) {
foreach (Db::getInstance()->executeS('
SELECT p.`id_product`, p.`id_tax_rules_group`, p.`id_category_default`,b.`id_product_attribute`, b.`ean13`, b.`supplier_reference` as at_ref, p.`price`, p.`wholesale_price`,
p.`reference`, p.`supplier_reference`, p.`weight`, p.`height`, p.`width`, p.`depth`, c.`position`, cu.`nc8`, cu.`id_country`,
al.`name` as attribute_name
FROM
`'._DB_PREFIX_.'product` p
`' . _DB_PREFIX_ . 'product` p
INNER JOIN (
SELECT max(id_product) as maxid, ean13, `supplier_reference`, `id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute`
WHERE id_product NOT IN ('.implode(',',$exclude_ids).')
FROM `' . _DB_PREFIX_ . 'product_attribute`
WHERE id_product NOT IN (' . implode(',', $exclude_ids) . ')
GROUP BY supplier_reference
) as b
ON p.`id_product`= b.maxid
LEFT JOIN `'._DB_PREFIX_.'category_product` c
LEFT JOIN `' . _DB_PREFIX_ . 'category_product` c
ON (b.maxid = c.`id_product` AND p.`id_category_default` = c.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'product_customs` cu
LEFT JOIN `' . _DB_PREFIX_ . 'product_customs` cu
ON (cu.`id_product` = p.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` ac
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` ac
ON (ac.`id_product_attribute` = b.`id_product_attribute`)
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al
ON (al.`id_attribute` = ac.`id_attribute` AND al.`id_lang` = 2)
WHERE b.`supplier_reference` IN ("'.implode('","',$refs).'")
WHERE b.`supplier_reference` IN ("' . implode('","', $refs) . '")
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
') as $row) {
// if($row['ean13']!='' && isset($products[$row['ean13']])){
// $products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
// $product_found++;
// if(!in_array((int)$row['id_product'],$ids)){
// $ids[] = (int)$row['id_product'];
// }
// }
if($row['at_ref']!='' && isset($products[$row['at_ref']])){
$products[$row['at_ref']] = array_merge($products[$row['at_ref']], $row);
$product_found++;
if(!in_array((int)$row['id_product'],$ids)){
$ids[] = (int)$row['id_product'];
}
} elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
$products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
$product_found++;
if(!in_array((int)$row['id_product'],$ids)){
$ids[] = (int)$row['id_product'];
// if($row['ean13']!='' && isset($products[$row['ean13']])){
// $products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
// $product_found++;
// if(!in_array((int)$row['id_product'],$ids)){
// $ids[] = (int)$row['id_product'];
// }
// }
if ($row['at_ref'] != '' && isset($products[$row['at_ref']])) {
$products[$row['at_ref']] = array_merge($products[$row['at_ref']], $row);
$product_found++;
if (!in_array((int)$row['id_product'], $ids)) {
$ids[] = (int)$row['id_product'];
}
} elseif ($row['supplier_reference'] != '' && isset($products[$row['supplier_reference']])) {
$products[$row['supplier_reference']] = array_merge($products[$row['supplier_reference']], $row);
$product_found++;
if (!in_array((int)$row['id_product'], $ids)) {
$ids[] = (int)$row['id_product'];
}
}
}
}
$timer_table[] = 'Ref product attr gathered at '.(microtime(true)-$callStartTime);
$new_products = count($products) - $product_found;
if(!empty($ids)){
foreach (Db::getInstance()->executeS('
@ -320,7 +354,7 @@ class AdminAntWp extends AdminTab
}
}
}
$timer_table[] = 'Additional info gathered at '.(microtime(true)-$callStartTime);
foreach (Db::getInstance()->executeS('
SELECT at.`ean13`, at.`supplier_reference` as at_ref, p.`supplier_reference`, pl.*
FROM '._DB_PREFIX_.'product p
@ -353,14 +387,24 @@ class AdminAntWp extends AdminTab
}
}
}
$timer_table[] = 'Additional info (attr) gathered at '.(microtime(true)-$callStartTime);
}
$params['lines'] = $products;
if(!empty($params['lines'])){
$wp = new GenerateWp();
$wp->generate($params);
$wp->generate($params, $timer_table, $callStartTime);
$filename = str_replace(' ', '_', $filename);
$wp->objWriter->save(dirname(__FILE__).'/'.$filename.'.xlsx');
$this->_html .= HelperFormBootstrap::displaySuccess('Génération complète <br><a class="btn btn-primary btn-xs" href="../modules/ant_wp/'.$filename.'.xlsx" download="'.$filename.'.xlsx">Télécharger le XLS</a><br>Produits trouvés : '.$product_found.'<br>Produits nouveaux : '.$new_products);
$wp->objWriter->save(__DIR__ .'/'.$filename.'.xlsx');
$timer_table_formatted = implode('<br>', $timer_table);
$duplicated_formatted = '';
if (!empty($this->report[self::DUPLICATED_REFS])) {
$duplicated_formatted = '<hr>Duplications de Reférences<br><p style="color:darkred">' . implode('<br>', $this->report[self::DUPLICATED_REFS]). '</p>';
}
if (!empty($this->report[self::DUPLICATED_EANS])) {
$duplicated_formatted .= '<hr>Duplications d\'EANs<br><p style="color:darkred">' . implode('<br>', $this->report[self::DUPLICATED_EANS]) . '</p>';
}
$this->_html .= HelperFormBootstrap::displaySuccess('Génération complète <br><a class="btn btn-primary btn-xs" href="../modules/ant_wp/'.$filename.'.xlsx" download="'.$filename.'.xlsx">Télécharger le XLS</a>'.
'<br>Produits trouvés : '.$product_found.'<br>Produits nouveaux : '.$new_products.$duplicated_formatted.'<p style="font-size: 6px"><br>Timers : <br>'.$timer_table_formatted.'</p>');
} else {
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Veuillez charger un fichier correct !'));
}

File diff suppressed because one or more lines are too long