Added doubled ean recovery options

This commit is contained in:
David 2018-02-02 21:59:26 +01:00
parent cbf9b268ce
commit 7c218242be
2 changed files with 160 additions and 71 deletions

View File

@ -817,13 +817,14 @@ class HelperFormBootstrap{
public function inputCheckbox($p = array()){
$checked = ((isset($p['checked']) && $p['checked']) ? $p['checked'] : false);
if ($this->_object)
if ($this->_object) {
$checked = $this->_object->{$p['name']};
$this->_html .= '<label' . (isset($p['class']) && $p['class'] ? ' class="' . $p['class'] . '"' : '') . '>'.$p['label'].'</label>
<div class="margin-form checkbox' . (isset($p['class']) && $p['class'] ? ' ' . $p['class'] : '') . '">
<input type="checkbox" id="'.$p['name'].'" name="'.$p['name'].'" value="1" id="'.$p['name'].'"'.($checked ? ' checked="checked"' : '').'/>
' . (isset($p['text']) && $p['text'] ? '<label class="checkbox_label" for="'.$p['name'].'">'.$p['text'].'</label>' : '') . '
<div class="clear"></div>
}
$this->_html .='
<div class="checkbox '.(isset($p['class-group'])?$p['class-group']:'').'">
' . (isset($p['text']) && $p['text'] ? ' <label class="control-label '.(isset($p['label-class']) ? $p['label-class'] : '').'" for="'.(isset($p['id']) ? $p['id'] : $p['name']).'">': '') .
'<input type="checkbox" id="'.$p['name'].'" name="'.$p['name'].'" value="1" id="'.$p['name'].'"'.($checked ? ' checked="checked"' : '').'/>' .
(isset($p['text']) && $p['text'] ? $p['text'].'</label>' : '') . '
' . ((isset($p['hint']) && $p['hint']) ? '<p class="small">'.$p['hint'].'</p>' : '') . '
' . ((isset($p['html']) && $p['html']) ? $p['html'] : '') . '
<div class="clear"></div>

View File

@ -104,7 +104,7 @@ class AdminAntWp extends AdminTab
$filename = Tools::getValue('filename')?'WP-Vente '.Tools::getValue('filename'):'WP-Vente';
$marque = Tools::getValue('filename')?Tools::getValue('filename'):'Marque';
$params = array('marque' => $marque, 'filename' => $filename, 'lines' => array());
$debug = (Tools::getValue('debug') !== false);
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
@ -113,6 +113,7 @@ class AdminAntWp extends AdminTab
$callStartTime = microtime(true);
$refs = array();
$eans = array();
$refs_ean = array();
while($line = fgetcsv($f, 0, ';')) {
if($line[0] == '' && $line[1] == '') {
continue;
@ -122,48 +123,60 @@ class AdminAntWp extends AdminTab
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])),
'supplier_ref' => trim(utf8_encode($line[1])),
'name_ha' => trim(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])
);
$eans[] = $ean;
}else{
//as the product is using a duplicated EAN we add it by ref
$ref = trim((String)utf8_encode($line[1]));
if(!isset($products[$ref])) {
$products[$ref] = array(
//as the product is using a duplicated EAN we add it by ean¤ref
$ean = trim($line[0]);
$ref = trim(utf8_encode($line[1]));
$key_ean_ref = $ean . '¤' . $ref;
//we need to clean the old ean entry to add the ref
if(isset($products[$ean]) && $products[$ean]['supplier_ref'] != ''){
$products[$ean . '¤' . $products[$ean]['supplier_ref']] = $products[$ean];
$refs_ean[] = $ean . '¤' . $products[$ean]['supplier_ref'];
unset($products[$ean]);
$ean_array_key = array_search($ean,$eans,true);
if($ean_array_key !== false){
unset($eans[$ean_array_key]);
}
}
if(!isset($products[$key_ean_ref])) {
$products[$key_ean_ref] = array(
'ean' => trim($line[0]),
'supplier_ref' => $ref,
'name_ha' => trim((String)utf8_encode($line[2])),
'name_ha' => trim(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]))
'name_attribute_ha' => trim(utf8_encode($line[6]))
);
$refs_ean[] = $key_ean_ref;
}else{
$this->report[self::DUPLICATED_REFS_AND_EAN][] = $ean . "-" . $ref;
$this->report[self::DUPLICATED_REFS_AND_EAN][] = $key_ean_ref;
}
$refs[] = $ref;
}
$eans[] = $ean;
} else {
$ref = trim((String)utf8_encode($line[1]));
$ref = trim(utf8_encode($line[1]));
if(!isset($products[$ref])) {
$products[$ref] = array(
'ean' => trim($line[0]),
'supplier_ref' => $ref,
'name_ha' => trim((String)utf8_encode($line[2])),
'name_ha' => trim(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]))
'name_attribute_ha' => trim(utf8_encode($line[6]))
);
$refs[] = $ref;
}else{
$this->report[self::DUPLICATED_REFS][] = $ref;
}
$refs[] = $ref;
}
}
$product_info = array();
@ -171,6 +184,7 @@ class AdminAntWp extends AdminTab
$ids = array();
$exclude_ids = $this->getBraderieProducts();
$timer_table[] = 'Excluded products gathered at '.(microtime(true)-$callStartTime);
$timer_table[] = 'Found by ean '.count($eans).', by ref'.count($refs).', duplicates ean '. count($refs_ean);
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`,
@ -195,19 +209,11 @@ class AdminAntWp extends AdminTab
$result = Db::getInstance()->executeS($sql);
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);
@ -232,11 +238,6 @@ class AdminAntWp extends AdminTab
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++;
@ -245,6 +246,42 @@ class AdminAntWp extends AdminTab
}
}
$timer_table[] = 'Ref product gathered at '.(microtime(true)-$callStartTime);
if(count($refs_ean) > 0) {
foreach($refs_ean as $key) {
$result[] = $key;
$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, supplier_reference
) 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` = "' . explode('¤', $key)[0] . '" AND p.`supplier_reference` = "' . explode('¤', $key)[1] . '"
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
';
$result = Db::getInstance()->executeS($sql);
foreach ($result as $row) {
//$key = $row['ean13'] . '-' . $row['supplier_reference'];
if ($row['ean13'] != '' && $row['supplier_reference'] != '' && isset($products[$key])) {
$products[$key] = array_merge($products[$key], $row);
$product_found++;
$ids[] = (int)$row['id_product'];
}
}
}
}
$timer_table[] = 'Ean-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`,
@ -278,19 +315,6 @@ class AdminAntWp extends AdminTab
$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'];
// }
// }
}
}
$timer_table[] = 'Ean product attr gathered at '.(microtime(true)-$callStartTime);
@ -320,13 +344,6 @@ class AdminAntWp extends AdminTab
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++;
@ -343,6 +360,47 @@ class AdminAntWp extends AdminTab
}
}
$timer_table[] = 'Ref product attr gathered at '.(microtime(true)-$callStartTime);
if(count($refs_ean) > 0) {
foreach($refs_ean as $key){
$sql = '
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
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) . ')
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`)
LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` ac
ON (ac.`id_product_attribute` = b.`id_product_attribute`)
LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al
ON (al.`id_attribute` = ac.`id_attribute` AND al.`id_lang` = 2)
WHERE b.`ean13` = "'.explode('¤',$key)[0].'" AND b.`supplier_reference` = "' . explode('¤',$key)[1] . '"
AND p.`id_product` IS NOT NULL
ORDER BY c.`position`
';
$result = Db::getInstance()->executeS($sql);
foreach ($result as $row) {
if ($row[$key] != '' && isset($products[$key])) {
$products[$key] = array_merge($products[$key], $row);
$product_found++;
if (!in_array((int)$row['id_product'], $ids)) {
$ids[] = (int)$row['id_product'];
}
}
}
}
}
$timer_table[] = 'Ean - Ref product attr gathered at '.(microtime(true)-$callStartTime);
$new_products = count($products) - $product_found;
if(!empty($ids)){
foreach (Db::getInstance()->executeS('
@ -353,20 +411,28 @@ class AdminAntWp extends AdminTab
AND (pl.id_lang = 2 OR pl.id_lang = 3)
ORDER BY p.`id_product`
') as $row) {
if($row['ean13']!='' && isset($products[(int)$row['ean13']])){
if(!isset($products[$row['ean13']]['lang'])){
if ($row['ean13'] != '' && isset($products[(int)$row['ean13']])) {
if (!isset($products[$row['ean13']]['lang'])) {
$products[$row['ean13']]['lang'] = array();
}
if(!isset($products[$row['ean13']]['lang'][(int)$row['id_lang']])){
if (!isset($products[$row['ean13']]['lang'][(int)$row['id_lang']])) {
$products[$row['ean13']]['lang'][(int)$row['id_lang']] = $row;
}
} elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
if(!isset($products[$row['supplier_reference']]['lang'])){
} elseif ($row['supplier_reference'] != '' && isset($products[$row['supplier_reference']])) {
if (!isset($products[$row['supplier_reference']]['lang'])) {
$products[$row['supplier_reference']]['lang'] = array();
}
if(!isset($products[$row['supplier_reference']]['lang'][(int)$row['id_lang']])){
if (!isset($products[$row['supplier_reference']]['lang'][(int)$row['id_lang']])) {
$products[$row['supplier_reference']]['lang'][(int)$row['id_lang']] = $row;
}
} elseif ($row['supplier_reference'] != '' && $row['ean13'] != '' && isset($products[$row['ean13'] . '¤' . $row['supplier_reference']])) {
$key = $row['ean13'] . '¤' . $row['supplier_reference'];
if (!isset($products[$key]['lang'])) {
$products[$key]['lang'] = array();
}
if (!isset($products[$key]['lang'][(int)$row['id_lang']])) {
$products[$key]['lang'][(int)$row['id_lang']] = $row;
}
}
}
$timer_table[] = 'Additional info gathered at '.(microtime(true)-$callStartTime);
@ -379,31 +445,42 @@ class AdminAntWp extends AdminTab
AND (pl.id_lang = 2 OR pl.id_lang = 3)
ORDER BY p.`id_product`
') as $row) {
if($row['ean13']!='' && isset($products[(int)$row['ean13']])){
if(!isset($products[$row['ean13']]['lang'])){
if ($row['ean13'] != '' && isset($products[(int)$row['ean13']])) {
if (!isset($products[$row['ean13']]['lang'])) {
$products[$row['ean13']]['lang'] = array();
}
if(!isset($products[$row['ean13']]['lang'][(int)$row['id_lang']])){
if (!isset($products[$row['ean13']]['lang'][(int)$row['id_lang']])) {
$products[$row['ean13']]['lang'][(int)$row['id_lang']] = $row;
}
} elseif($row['supplier_reference']!='' && isset($products[$row['supplier_reference']])){
if(!isset($products[$row['supplier_reference']]['lang'])){
} elseif ($row['supplier_reference'] != '' && isset($products[$row['supplier_reference']])) {
if (!isset($products[$row['supplier_reference']]['lang'])) {
$products[$row['supplier_reference']]['lang'] = array();
}
if(!isset($products[$row['supplier_reference']]['lang'][(int)$row['id_lang']])){
if (!isset($products[$row['supplier_reference']]['lang'][(int)$row['id_lang']])) {
$products[$row['supplier_reference']]['lang'][(int)$row['id_lang']] = $row;
}
} elseif($row['at_ref']!='' && isset($products[$row['at_ref']])){
if(!isset($products[$row['at_ref']]['lang'])){
} elseif ($row['at_ref'] != '' && isset($products[$row['at_ref']])) {
if (!isset($products[$row['at_ref']]['lang'])) {
$products[$row['at_ref']]['lang'] = array();
}
if(!isset($products[$row['at_ref']]['lang'][(int)$row['id_lang']])){
if (!isset($products[$row['at_ref']]['lang'][(int)$row['id_lang']])) {
$products[$row['at_ref']]['lang'][(int)$row['id_lang']] = $row;
}
} elseif ($row['supplier_reference'] != '' && $row['ean13'] != '' && isset($products[$row['ean13'] . '¤' . $row['supplier_reference']])) {
$key = $row['ean13'] . '¤' . $row['supplier_reference'];
if (!isset($products[$key]['lang'])) {
$products[$key]['lang'] = array();
}
if (!isset($products[$key]['lang'][(int)$row['id_lang']])) {
$products[$key]['lang'][(int)$row['id_lang']] = $row;
}
}
}
$timer_table[] = 'Additional info (attr) gathered at '.(microtime(true)-$callStartTime);
}
foreach($products as $product_key => $product_values){
$timer_table[] = $product_key . ' - ' . $product_values['id_product'];
}
$params['lines'] = $products;
if(!empty($params['lines'])){
$wp = new GenerateWp();
@ -418,8 +495,12 @@ class AdminAntWp extends AdminTab
if (!empty($this->report[self::DUPLICATED_REFS_AND_EAN])) {
$duplicated_formatted .= '<hr>Duplications d\'EANs<br><p style="color:darkred">' . implode('<br>', $this->report[self::DUPLICATED_REFS_AND_EAN]) . '</p>';
}
$debug_output ='';
if($debug){
$debug_output = $duplicated_formatted.'<p style="font-size: 6px"><br>Timers : <br>'.$timer_table_formatted.'</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>');
'<br>Produits trouvés : '.$product_found.'<br>Produits nouveaux : '.$new_products. $debug_output);
} else {
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Veuillez charger un fichier correct !'));
}
@ -459,6 +540,13 @@ class AdminAntWp extends AdminTab
'label-class' => 'col-md-12 text-left',
'input-class' => 'col-md-6',
),
array(
'type' => 'checkbox',
'label' => $this->l('Debug'),
'name' => 'debug',
'text' => $this->l('Debug'),
'class-group' => 'col-md-12 text-left',
),
),
'actions' => array(),
'actions-class' => 'text-right',