337 lines
16 KiB
PHP
337 lines
16 KiB
PHP
<?php
|
|
|
|
require_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.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
|
|
{
|
|
|
|
protected $_html;
|
|
public $module_name;
|
|
public $config_tab;
|
|
public $controller;
|
|
public $helperForm;
|
|
public $_object;
|
|
|
|
public function __construct($config_tab = true)
|
|
{
|
|
parent::__construct();
|
|
$this->_object = false;
|
|
$this->controller = 'AdminModules';
|
|
$this->module_name = 'ant_wp';
|
|
$this->config_tab = (bool)$config_tab;
|
|
if ($config_tab) {
|
|
$this->controller = 'AdminAntWp';
|
|
}
|
|
$this->helperForm = new HelperFormBootstrap();
|
|
// $this->helperForm->_select2 = true;
|
|
// $this->helperForm->_inputSwitch = true;
|
|
}
|
|
|
|
public function display()
|
|
{
|
|
parent::displayForm();
|
|
|
|
$this->_html = '';
|
|
|
|
$this->_postProcess();
|
|
|
|
$this->_addCss();
|
|
$this->_html .= $this->helperForm->renderStyle();
|
|
|
|
$this->_displayForm();
|
|
//$this->_displayList();
|
|
|
|
$this->_html .='<div class="clearfix"></div>';
|
|
$this->_addJs();
|
|
$this->_html .= $this->helperForm->renderScript();
|
|
echo $this->_html;
|
|
}
|
|
|
|
|
|
protected function _addJs()
|
|
{
|
|
$this->helperForm->_js .= '<script type="text/javascript"></script>';
|
|
}
|
|
|
|
protected function _addCss()
|
|
{
|
|
$this->helperForm->_css .='
|
|
.table tr th {
|
|
background: #565485;
|
|
background: rgba(86,84,133,0.9);
|
|
color: #fff;
|
|
font-size: 12px;
|
|
}
|
|
.table tr:nth-child(even) {
|
|
background: #F1F1F1;
|
|
}
|
|
.table .input-group-btn .btn {
|
|
padding: 4px 5px;
|
|
color: #504d8b;
|
|
|
|
}
|
|
.table .input-group-btn .btn .anticon{
|
|
font-size: 12px;
|
|
}
|
|
.bg-grey{
|
|
background: #EFEFEF;
|
|
border-radius:4px;
|
|
}
|
|
.bg-grey .div-title {
|
|
border-bottom: 2px solid #504D8B;
|
|
}
|
|
.control-label.text-left{
|
|
text-align:left!important;
|
|
}
|
|
';
|
|
}
|
|
|
|
|
|
protected function _postProcess()
|
|
{
|
|
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((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])
|
|
);
|
|
$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]))
|
|
);
|
|
$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.`nc8`, cu.`id_country`
|
|
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).'")':'').'
|
|
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'];
|
|
}
|
|
}
|
|
|
|
foreach (Db::getInstance()->executeS('
|
|
SELECT p.`id_product`, p.`id_tax_rules_group`, p.`id_category_default`, at.`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`,
|
|
al.`name` as attribute_name
|
|
FROM
|
|
`'._DB_PREFIX_.'product` p
|
|
INNER JOIN (SELECT date_add, max(id_product) as maxid FROM `'._DB_PREFIX_.'product` GROUP BY `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`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute` at
|
|
ON (at.`id_product` = p.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` ac
|
|
ON (ac.`id_product_attribute` = at.`id_product_attribute`)
|
|
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al
|
|
ON (al.`id_attribute` = ac.`id_attribute` AND al.`id_lang` = 2)
|
|
WHERE at.`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++;
|
|
} 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'];
|
|
}
|
|
}
|
|
$new_products = count($products) - $product_found;
|
|
if(!empty($ids)){
|
|
foreach (Db::getInstance()->executeS('
|
|
SELECT p.`ean13`, p.`supplier_reference`, pl.*
|
|
FROM '._DB_PREFIX_.'product p
|
|
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.`id_product` = p.`id_product`)
|
|
WHERE p.`id_product` IN ('.implode(',',$ids).')
|
|
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'])){
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (Db::getInstance()->executeS('
|
|
SELECT at.`ean13`, p.`supplier_reference`, pl.*
|
|
FROM '._DB_PREFIX_.'product p
|
|
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.`id_product` = p.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute` at ON (at.`id_product` = p.`id_product`)
|
|
WHERE p.`id_product` IN ('.implode(',',$ids).')
|
|
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'])){
|
|
$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);
|
|
$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);
|
|
} else {
|
|
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Veuillez charger un fichier correct !'));
|
|
}
|
|
}
|
|
}
|
|
|
|
private function _displayForm()
|
|
{
|
|
global $cookie, $currentIndex;
|
|
if (Validate::isLoadedObject($this->_object)){
|
|
$trackingTag = $this->_object;
|
|
}
|
|
$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'),
|
|
'title' => '<span class="text-rose anticon anticon-table2"></span> '.$this->l('WP'),
|
|
'class' => 'form-horizontal',
|
|
'class_div' => 'col-md-12',
|
|
'sections' => array(
|
|
array(
|
|
'class' => 'col-md-6',
|
|
'inputs' => array(
|
|
array(
|
|
'type' => 'simpleText',
|
|
'name' => 'filename',
|
|
'label-class' => 'col-md-12 text-left',
|
|
'input-class' => 'col-md-6',
|
|
'text-class' => 'input-sm',
|
|
'label' => $this->l('Sale name :'),
|
|
),
|
|
array(
|
|
'type' => 'file',
|
|
'label' => $this->l('File :'),
|
|
'name' => 'csvfile',
|
|
'label-class' => 'col-md-12 text-left',
|
|
'input-class' => 'col-md-6',
|
|
),
|
|
),
|
|
'actions' => array(),
|
|
'actions-class' => 'text-right',
|
|
),
|
|
array(
|
|
'class' => 'col-md-6 bg-grey',
|
|
'title' => '<span class="anticon anticon-info"></span> Informations',
|
|
'inputs' => array(),
|
|
'info_html' => '
|
|
<p>Pour la génération du WP</p>
|
|
<p>Veuillez importer un fichier sous format csv<br>
|
|
Avec les colonnes suivantes :<br>
|
|
</p>
|
|
<ul style="list-style:none;">
|
|
<li style="font-size:13px;"><i>ean;ref_fournisseur;nom_produit_HA;quantite;PPC;prix_achat;taille_ha</i></li>
|
|
</ul>
|
|
<p>Les colonnes suivantes et la première ligne du fichier seront ignorées.</p>
|
|
<p></p>'
|
|
),
|
|
),
|
|
'actions' => array(),
|
|
'actions-class' => 'text-right',
|
|
)
|
|
);
|
|
|
|
$this->helperForm->_forms[0]['sections'][0]['title'] = "Générer un WP";
|
|
$this->helperForm->_forms[0]['sections'][0]['actions'] = array(
|
|
array(
|
|
'type' => 'submit',
|
|
'class' => 'btn-primary',
|
|
'name' => 'uploadWP',
|
|
'value' => $this->l('Générer')
|
|
),
|
|
);
|
|
|
|
$this->helperForm->renderForm();
|
|
}
|
|
}
|