correction of formulae

This commit is contained in:
Marion Muszynski 2017-10-31 12:32:41 +01:00
parent a6ebb26af0
commit caf88c2c7e
2 changed files with 164 additions and 156 deletions

View File

@ -1,7 +1,9 @@
<?php
require_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php');
//include_once(_PS_ROOT_DIR_.'/modules/ant_wp/GenerateWp.php');
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';
class AdminAntWp extends AdminTab
{
@ -90,14 +92,109 @@ class AdminAntWp extends AdminTab
protected function _postProcess()
{
if (Tools::getValue('found') && Tools::getValue('new')) {
$this->_html .= HelperFormBootstrap::displaySuccess($this->l('Génération complète <br>Produit trouvés : '.Tools::getValue('found').'<br>Produit nouveau : '.Tools::getValue('new')));
// if ($res) {
// } else {
// $this->_html .= HelperFormBootstrap::displayErrors($this->l('Des erreurs sont survenues : '));
// }
} elseif(Tools::getValue('error_file')) {
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Veuillez charger un fichier correct !'));
if(isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') {
$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());
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
$product_info = array();
$refs = array();
$eans = array();
while($line = fgetcsv($f, 0, ';')) {
if($line[0] == '' && $line[1] == '') {
continue;
}
if($line[0] != '') {
$ean = trim($line[0]);
$products[$ean] = array(
'ean' => $ean,
'supplier_ref' => trim($line[1]),
'name_ha' => trim($line[2]),
'quantity' => trim($line[3]),
'ppc' => trim($line[4]),
'prix_ha' => trim($line[5])
);
$eans[] = $ean;
} else {
$ref = trim($line[1]);
$products[$ref] = array(
'ean' => trim($line[0]),
'supplier_ref' => $ref,
'name_ha' => trim($line[2]),
'quantity' => trim($line[3]),
'ppc' => trim($line[4]),
'prix_ha' => trim($line[5])
);
$refs[] = $ref;
}
}
$product_info = array();
$product_found = 0;
$ids = array();
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.*
FROM
`'._DB_PREFIX_.'product` p
INNER JOIN (SELECT date_add, max(id_product) as maxid FROM `'._DB_PREFIX_.'product` 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).'")
'.(!empty($refs)?' OR p.`supplier_reference` IN ("'.implode('","',$refs).'")':'').'
ORDER BY c.`position`
') as $row) {
if($row['ean13']!='' && isset($products[$row['ean13']])){
$products[$row['ean13']] = array_merge($products[$row['ean13']], $row);
$product_found++;
} 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'];
}
$new_products = count($ids) - $product_found;
if(!empty($ids)){
foreach (Db::getInstance()->executeS('
SELECT p.`ean13`, p.`supplier_reference`, pl.*
FROM ps_product_lang pl
LEFT JOIN ps_product p ON (pl.`id_product` = p.`id_product`)
WHERE pl.`id_product` IN ('.implode(',',$ids).')
AND (pl.id_lang = 2 OR pl.id_lang = 3)
ORDER BY pl.`id_product`
') as $row) {
if($row['ean13']!='' && isset($products[$row['ean13']])){
if(!isset($products[$row['ean13']]['lang'])){
$products[$row['ean13']]['lang'] = array();
}
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'])){
$products[$row['supplier_reference']]['lang'] = array();
}
if(!isset($products[$row['supplier_reference']]['lang'][(int)$row['id_lang']])){
$products[$row['supplier_reference']]['lang'][(int)$row['id_lang']] = $row;
}
}
}
}
$params['lines'] = $products;
if(!empty($params['lines'])){
$wp = new GenerateWp();
$wp->generate($params);
$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);
} else {
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Veuillez charger un fichier correct !'));
}
}
}
@ -110,8 +207,8 @@ class AdminAntWp extends AdminTab
$ps_url = ((Configuration::get('PS_SSL_ENABLED') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PORT'] == '443')) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].__PS_BASE_URI__;
$this->helperForm->_forms = array(
array(
// 'action' => $currentIndex . '&token=' . Tools::getAdminTokenLite('AdminAntWp'),
'action' => $ps_url.'modules/ant_wp/GenerateWp.php?url='.$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminAntWp'),
'action' => $currentIndex . '&token=' . Tools::getAdminTokenLite('AdminAntWp'),
// 'action' => $ps_url.'modules/ant_wp/GenerateWp.php?url='.$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminAntWp'),
'title' => '<span class="text-rose anticon anticon-table2"></span> '.$this->l('WP'),
'class' => 'form-horizontal',
'class_div' => 'col-md-12',

File diff suppressed because one or more lines are too long