bebeboutik/modules/bulkupdate/AdminBulkUpdate.php
2017-09-07 11:36:48 +02:00

2672 lines
138 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
if(!defined('_PS_VERSION_')) {
exit;
}
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
include_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php');
class AdminBulkUpdate extends AdminTab {
public $_html = '';
public function postProcess() {
global $cookie;
set_time_limit(300);
if (Tools::isSubmit('submitUploadProductCustoms')) {
$process = (int) Tools::getValue('customs_process');
if(isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
while($line = fgetcsv($f, 0, ';')) {
if($line[0] == '' || $line[1] == '' || $line[2] == '') {
continue;
}
$products[(int)$line[0]] = $line;
}
if($process == 1) {
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'product_customs`
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
');
}
foreach($products as $line) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'product_customs` VALUES (
'.(int) $line[0].',
"'.pSQL($line[1]).'",
"'.pSQL($line[2]).'",
0
)
');
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Product customs updated').'</p>';
}
}elseif (Tools::isSubmit('submitUploadEans')) {
if(isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$i = 1;
while($line = fgetcsv($f, 0, ';')) {
$i++;
$product = new Product((int) $line[0]);
if(Validate::isLoadedObject($product)) {
if(isset($line[2]) && $line[2] != '' && !Validate::isEan13($line[2])) {
$output .= '<p class="error">'.$this->l('Invalid ean for this product ID on line').' '.$i.'</p>';
continue;
}
if((int) $line[1] != 0) {
if(!Db::getInstance()->getRow('
SELECT `id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` = '.(int) $line[0].'
AND `id_product_attribute` = '.(int) $line[1].'
')) {
$output .= '<p class="error">'.$this->l('Invalid attribute ID for this product ID on line').' '.$i.'</p>';
continue;
}
}
if((int) $line[1] != 0) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `ean13` = "'.pSQL($line[2]).'"
WHERE `id_product` = '.(int) $line[0].'
AND `id_product_attribute` = '.(int) $line[1].'
LIMIT 1
');
} else {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `ean13` = "'.pSQL($line[2]).'"
WHERE `id_product` = '.(int) $line[0].'
LIMIT 1
');
}
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = "'.pSQL($line[2]).'"
WHERE `product_id` = '.(int) $line[0].'
AND `product_attribute_id` = '.(int) $line[1].'
');
} else {
$output .= '<p class="warn">'.$this->l('Product #').(int) $line[0].' '.$this->l('not found').'</p>';
}
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} else {
$output .= '<p class="conf">'.$this->l('Pas de fichier csv !').'</p>';
}
} elseif (Tools::isSubmit('submitUploadQuantities')) {
$process = (int) Tools::getValue('qty_process');
if(isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$ean_ok = TRUE;
$i = 2;
while($line = fgetcsv($f, 0, ';')) {
if(isset($line[4]) && $line[4] != '' && !Validate::isEan13($line[4])) {
$ean_ok = FALSE;
break;
}
$i++;
}
fclose($f);
if(!$ean_ok) {
$output .= '<p class="error">'.$this->l('Invalid EAN13 on line').' '.$i.'</p>';
return $output.$this->displayForm();
}
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$i = 1;
while($line = fgetcsv($f, 0, ';')) {
$i++;
$product = new Product((int) $line[0]);
if(Validate::isLoadedObject($product)) {
if((int) $line[1] != 0) {
if(!Db::getInstance()->getRow('
SELECT `id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` = '.(int) $line[0].'
AND `id_product_attribute` = '.(int) $line[1].'
')) {
$output .= '<p class="error">'.$this->l('Invalid attribute ID for this product ID on line').' '.$i.'</p>';
continue;
}
}
if($line[2] != '') {
$qty = (int) $line[2];
$old_qty = Product::getQuantity((int) $line[0], (int) $line[1] == 0? NULL: (int) $line[1]);
if($process == 0) {
$product->addStockMvt($qty, 5, (int) $line[1]);
} else {
if($qty > $old_qty) {
$product->addStockMvt($qty - $old_qty, 5, (int) $line[1]);
} else {
$product->addStockMvt(-($old_qty - $qty), 5, (int) $line[1]);
}
}
}
if(isset($line[4]) && $line[4] != '' && Validate::isEan13($line[4])) {
if((int) $line[1] != 0) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `ean13` = "'.pSQL($line[4]).'"
WHERE `id_product` = '.(int) $line[0].'
AND `id_product_attribute` = '.(int) $line[1].'
LIMIT 1
');
} else {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `ean13` = "'.pSQL($line[4]).'"
WHERE `id_product` = '.(int) $line[0].'
LIMIT 1
');
}
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = "'.pSQL($line[4]).'"
WHERE `product_id` = '.(int) $line[0].'
AND `product_attribute_id` = '.(int) $line[1].'
');
}
if(isset($line[5]) && $line[5] != '' && Validate::isReference($line[5])) {
if((int) $line[1] != 0) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `location` = "'.pSQL($line[5]).'"
WHERE `id_product` = '.(int) $line[0].'
AND `id_product_attribute` = '.(int) $line[1].'
LIMIT 1
');
} else {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `location` = "'.pSQL($line[5]).'"
WHERE `id_product` = '.(int) $line[0].'
LIMIT 1
');
}
}
} else {
$output .= '<p class="warn">'.$this->l('Product #').(int) $line[0].' '.$this->l('not found').'</p>';
}
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
}
} elseif(Tools::isSubmit('submitUploadCategories')) {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
$defaults = array();
$positions = array();
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0])) {
continue;
}
if(!isset($line[1])) {
continue;
}
if(isset($line[2]) && !empty($line[2]) && (int) $line[2] != 0) {
$defaults[(int) $line[0]] = (int) $line[2];
}
$categories = array_map('intval', explode(',', $line[1]));
$products[(int) $line[0]] = $categories;
foreach($categories as $id_category) {
$positions[(int) $id_category] = 1;
}
}
fclose($f);
if(Tools::getValue('category_process') == 1) {
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'category_product`
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
');
}
foreach(Db::getInstance()->ExecuteS('
SELECT `id_category`, MAX(`position`) AS `position`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` IN ('.implode(', ', array_keys($positions)).')
GROUP BY `id_category`
') as $row) {
$positions[(int) $row['id_category']] = (int) $row['position'];
}
foreach($products as $id_product => $id_categories) {
foreach($id_categories as $id_category) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
'.(int) $id_category.',
'.(int) $id_product.',
'.(int) ++$positions[(int) $id_category].'
)
');
}
}
foreach($defaults as $id_product => $id_category_default) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `id_category_default` = '.(int) $id_category_default.'
WHERE `id_product` = '.(int) $id_product.'
LIMIT 1
');
if(!$row = Db::getInstance()->getRow('
SELECT `id_product`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_product` = '.(int) $id_product.'
AND `id_category` = '.(int) $id_category_default.'
')) {
$pos = (int) Db::getInstance()->getValue('
SELECT MAX(`position`)
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int) $id_category_default.'
') + 1;
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
'.(int) $id_category_default.',
'.(int) $id_product.',
'.(int) $pos.'
)
');
}
}
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} elseif(Tools::isSubmit('submitUploadAccessories')) {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0]) || empty($line[1])) {
continue;
}
if(!isset($products[(int) $line[0]])) {
$products[(int) $line[0]] = array();
}
$products[(int) $line[0]][] = (int) $line[1];
}
fclose($f);
if(Tools::getValue('access_process') == 1) {
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'accessory`
WHERE `id_product_1` IN ('.implode(', ', array_keys($products)).')
');
}
foreach($products as $id_product_1 => $id_products_2) {
foreach($id_products_2 as $id_product_2) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'accessory` VALUES (
'.(int) $id_product_1.',
'.(int) $id_product_2.'
)
');
}
}
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
}elseif(Tools::isSubmit('submitUploadImages')) {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
$products_a = array();
$products_a_ids = array();
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0])) {
continue;
}
if(!isset($line[1])
|| empty($line[1])) {
continue;
}
if(isset($line[2]) && !empty($line[2])) {
if(!isset($products_a[(int) $line[0]])) {
$products_a[(int) $line[0]] = array(array(), array());
}
$products_a[(int) $line[0]][0] = array_merge($products_a[(int) $line[0]][0], array_map('trim', explode(',', $line[1])));
$products_a[(int) $line[0]][1][(int) $line[2]] = array_map('trim', explode(',', $line[1]));
$products_a_ids[] = (int) $line[2];
} else {
$products[(int) $line[0]] = array_map('trim', explode(',', $line[1]));
}
}
fclose($f);
if(Tools::getValue('image_process') == 1) {
if(count($products) > 0) {
foreach(Db::getInstance()->ExecuteS('
SELECT `id_image`
FROM `'._DB_PREFIX_.'image`
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
') as $row) {
$i = new Image((int) $row['id_image']);
$i->delete();
}
}
if(count($products_a_ids) > 0) {
foreach(Db::getInstance()->ExecuteS('
SELECT DISTINCT i.`id_image`
FROM `'._DB_PREFIX_.'image` i
LEFT JOIN `'._DB_PREFIX_.'product_attribute` a
ON i.`id_product` = a.`id_product`
WHERE a.`id_product_attribute` IN ('.implode(', ', $products_a_ids).')
') as $row) {
$i = new Image((int) $row['id_image']);
$i->delete();
}
}
}
foreach($products as $id_product => $urls) {
foreach($urls as $key => $url) {
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
if ($key == 0 && Tools::getValue('image_process') == 1) {
$image->cover = true;
} else {
$image->cover = FALSE;
}
$image->legend = self::createMultiLangField(Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'product_lang`
WHERE `id_product` = '.(int) $id_product.'
AND `id_lang` = 2
'));
$image->add();
self::copyImg($id_product, $image->id, $url, 'products');
}
}
$_id_products = array();
foreach($products_a as $id_product => $data) {
$_id_products[] = $id_product;
foreach(array_unique($data[0]) as $url) {
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = FALSE;
$image->legend = self::createMultiLangField(Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'product_lang`
WHERE `id_product` = '.(int) $id_product.'
AND `id_lang` = 2
'));
$image->add();
self::copyImg($id_product, $image->id, $url, 'products');
foreach($data[1] as $id_product_attribute => $urls) {
if(in_array($url, $urls)) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'product_attribute_image`
VALUES (
'.(int) $id_product_attribute.',
'.(int) $image->id.'
)
');
}
}
}
}
$covers = array();
foreach(Db::getInstance()->ExecuteS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'image`
WHERE `id_product` IN ('.implode(', ', array_merge($_id_products, array_keys($products))).')
AND `cover` = 1
') as $row) {
$covers[] = (int) $row['id_product'];
}
$to_cover = array_diff(array_merge($_id_products, array_keys($products)), $covers);
foreach($to_cover as $id_product) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'image`
SET `cover` = 1
WHERE `id_product` = '.(int) $id_product.'
LIMIT 1
');
}
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} elseif(Tools::isSubmit('submitUploadStatus')) {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products_0 = array();
$products_1 = array();
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0]) || !isset($line[1])) {
continue;
}
if((int) $line[1] == 0) {
$products_0[] = (int) $line[0];
} else {
$products_1[] = (int) $line[0];
}
}
fclose($f);
if(count($products_0) > 0) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `active` = 0
WHERE `id_product` IN ('.implode(', ', $products_0).')
');
}
if(count($products_1) > 0) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `active` = 1
WHERE `id_product` IN ('.implode(', ', $products_1).')
');
}
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} elseif(Tools::isSubmit('submitUploadPrices')) {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
$prices = array();
if(Tools::getValue('price_process') == 2) {
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0])) {
continue;
}
$products[] = (int) $line[0];
}
} elseif(Tools::getValue('price_process') == 3
|| Tools::getValue('price_process') == 4){
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0])) {
continue;
}
$prices[] = array(
(int) $line[0],
$line[1]
);
}
}else {
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0])) {
continue;
}
if(!isset($line[1])) {
continue;
}
$products[] = (int) $line[0];
$prices[] = array(
(int) $line[0],
(float) $line[1] > 1? (float) $line[1] / 100: (float) $line[1]
);
}
}
fclose($f);
if(Tools::getValue('price_process') == 1
|| Tools::getValue('price_process') == 2) {
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'specific_price`
WHERE `id_product` IN ('.implode(', ', $products).')
');
}
if(Tools::getValue('price_process') == 1) {
foreach($prices as $price) {
if((int) $price[0] != 0) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'specific_price` VALUES (
DEFAULT,
'.(int) $price[0].',
1,
0,
0,
0,
0.0,
1,
'.(float) $price[1].',
"percentage",
"0000-00-00 00:00:00",
"0000-00-00 00:00:00"
)
');
} else {
$output .= '<p class="error">ID produit à 0, ligne non traitée : '.serialize($price).'</p>';
}
}
}
if(Tools::getValue('price_process') == 3) {
foreach($prices as $price) {
if((int) $price[0] != 0){
$price[1] = str_replace(',', '.', $price[1]);
$price_ht = floatval($price[1]);
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET wholesale_price = '. (float)$price_ht .'
WHERE id_product ='. (int)$price[0]);
}else{
$output .= '<p class="error">ID produit à 0, ligne non traitée : '.serialize($price).'</p>';
}
}
}
if(Tools::getValue('price_process') == 4) {
foreach($prices as $price) {
if((int) $price[0] != 0){
$price[1] = str_replace(',', '.', $price[1]);
$price_ht = floatval($price[1] / 1.20);
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET price = '. (float)$price_ht .'
WHERE id_product ='. (int)$price[0]);
}else{
$output .= '<p class="error">ID produit à 0, ligne non traitée : '.serialize($price).'</p>';
}
}
}
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} elseif(Tools::isSubmit('submitUploadProductDescriptions')) {
$id_lang = Tools::getValue('description_process');
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
while($line = fgetcsv($f, 0, ';')) {
if($line[0] == '' || $line[1] == '') {
continue;
}
$products[] = $line;
}
foreach($products as $line) {
$result = Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product_lang`
SET `description` = "'. pSQL($line[1],true) .'"
WHERE `id_product` ='. (int) $line[0].'
AND `id_lang` = '.(int) $id_lang.'
');
/*if (!$result) {
$output .= '<p class="error">ligne non traitée, id_product: '.$line[0].'</p>';
}*/
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} elseif(Tools::isSubmit('submitUploadCombinations')) {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
while($line = fgetcsv($f, 0, ';')) {
if(empty($line[0])) {
continue;
}
$products[] = (int) $line[0];
}
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'product_attribute_combination`
WHERE `id_product_attribute` IN (
SELECT `id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` IN ('.implode(', ', $products).')
)
');
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` IN ('.implode(', ', $products).')
');
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'product`
SET `quantity` = 0
WHERE `id_product` IN ('.implode(', ', $products).')
');
fclose($f);
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
} elseif(Tools::isSubmit('submitExportDeb')){
set_time_limit(300);
$id_lang = Tools::getValue('id_lang', $cookie->id_lang);
$deb = Tools::getValue('deb', 0);
if (!$deb) {
$output .= '<p class="error">Erreur produit: Vous devez choisir une date</p>';
}
if (!Tools::getValue('date_start') || !Tools::getValue('date_end')) {
$output .= '<p class="error">Erreur produit: Vous devez choisir une date</p>';
}
$date_start = Tools::getValue('date_start');
$date_end = Tools::getValue('date_end');
switch ($deb) {
case '1':
// DEB INTRODUCTION
$ids = Db::getInstance()->ExecuteS('
SELECT c.id_category
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'privatesale_category` c ON c.id_sale = p.id_sale
WHERE date_start BETWEEN "'. $date_start .' 00:00:00" AND "'. $date_end .' 00:00:00"
ORDER BY date_start ASC
');
if (!$ids) {
$output .= '<p class="error">Erreur produit: Pas de ventes trouvées</p>';
break;
}
setlocale(LC_TIME, "fr_FR");
$products = array();
$category_properties = array();
foreach($ids as $id) {
$id_category = $id['id_category'];
foreach(Category::getProductsWsSold((int)$id_category) as $p) {
$products[] = $p['id'];
if (!is_array($category_properties[(int)$p['id']])) {
$privatesale = Sale::getSaleFromCategory((int) $id_category);
$country_ps = '';
if ($privatesale->id_country > 0) {
$country_ps = Country::getNameById((int)$id_lang,(int)$privatesale->id_country);
}
$category_properties[(int)$p['id']] = array(
'name' => $privatesale->title[(int)$id_lang],
'country' => $country_ps,
'month' => strftime("%B", strtotime($privatesale->date_start)),
'id_country' => (int)$privatesale->id_country
);
}
}
}
$products = array_unique($products);
if(count($products) > 20000){
$output .= '<p class="conf">Choisir une plage moins grande. <br /> Export > 20000 produits</p>';
break;
};
$fname = 'DEB-int-'.date( "Ymd", strtotime($date_start)).'-'.date( "Ymd", strtotime($date_end)).'.csv';
foreach(glob(dirname(__FILE__).'/*.csv') as $filename) {
if ($fname == $filename) {
unlink($filename);
}
}
$f = fopen(dirname(__FILE__).'/'.$fname, 'w');
// uft8 sans bom pour accent
$BOM = "\xEF\xBB\xBF"; // UTF-8 BOM
fwrite($f, $BOM); // NEW LINE
fputcsv($f, array(
'Ordre',
'Pays de provenance',
'Marque',
'Mois',
'Référence fournisseur',
'Produit',
'Nomenclature',
'NGP9',
'Pays dorigine',
'Valeur',
'Régime',
'Masse',
'Unité supplémentaire',
'Nature de transaction',
'Mode de transport',
'Département '
), ';', '"');
$pos = 0;
foreach($products as $product) {
$p = new Product((int) $product, $id_lang);
if(!Validate::isLoadedObject($p)) {
$output .= '<p class="error">Erreur produit: '.serialize($product).' ce produit ne figure pas dans l\'export</p>';
} else {
$customs = Db::getInstance()->getRow('
SELECT `nc8`, `id_country`
FROM `'._DB_PREFIX_.'product_customs`
WHERE `id_product` = '.(int) $p->id
);
if (strtolower($customs['id_country']) == 'france') {
continue;
}
$ue_countries = array(1,2,3,6,7,9,10,12,13,14,15,16,17,18,19,20,26,36,37);
if (!in_array($category_properties[$p->id]['id_country'], $ue_countries)) {
continue;
}
/*$country_product = Db::getInstance()->getRow('
SELECT `id_country`
FROM `'._DB_PREFIX_.'country_lang`
WHERE `name` LIKE \''.pSQL(ucfirst(strtolower($customs['id_country']))).'\'
AND `id_lang` = '.(int)($id_lang)
);
if (!in_array((int)$country_product['id_country'], $ue_countries)) {
continue;
}*/
$position = Db::getInstance()->getValue('
SELECT `position`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int) $p->id_category_default.'
AND id_product = '. (int) $p->id . '
');
$p->position = $position;
$combinations = array();
foreach($p->getAttributeCombinaisons($id_lang) as $combi) {
if(!isset($combinations[$combi['id_product_attribute']])) {
$combinations[$combi['id_product_attribute']] = array(
'qty' => $combi['quantity'],
'name' => array($combi['attribute_name']),
'ean13' => $combi['ean13'],
'location' => empty($combi['location'])? $p->location: $combi['location'],
'brand' => $p->manufacturer_name,
'supplier_reference' => empty($combi['supplier_reference'])? $p->supplier_reference: $combi['supplier_reference'],
);
} else {
$combinations[$combi['id_product_attribute']]['name'][] = $combi['attribute_name'];
}
}
$quantity_sold = 0;
if(count($combinations) > 0) {
foreach($combinations as $k => $v) {
$quantity_sold += (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order`
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`product_attribute_id` = '.(int) $k.'
AND (
SELECT `id_order_state`
FROM `'._DB_PREFIX_.'order_history`
WHERE `id_order`=d.`id_order`
ORDER BY `date_add` DESC LIMIT 1
) NOT IN (1,6,8,9,10,11,14,15,18)
');
/*if ($quantity_sold == 0) {
continue;
}
$names = array_unique($v['name']);
sort($names, SORT_STRING);
fputcsv($f, array(
$p->position,
$category_properties[$p->id]['country'],
$category_properties[$p->id]['name'],
$category_properties[$p->id]['month'],
$v['supplier_reference'],
$p->name[$id_lang].' - '.implode(' ', $names),
$customs['nc8'],
'',
$customs['id_country'],
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
11,
str_replace('.', ',', Tools::ps_round(($p->weight * $quantity_sold),2)),
$quantity_sold,
11,
3,
31
), ';', '"');*/
}
} else {
$quantity_sold += (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order`
WHERE d.`product_id` = '.(int) $p->id.'
AND (
SELECT `id_order_state`
FROM `'._DB_PREFIX_.'order_history`
WHERE `id_order`=d.`id_order`
ORDER BY `date_add` DESC LIMIT 1
) NOT IN (1,6,8,9,10,11,14,15,18)
');
/*if ($quantity_sold == 0) {
continue;
}
fputcsv($f, array(
//$p->position,
$pos,
$category_properties[$p->id]['country'],
$category_properties[$p->id]['name'],
$category_properties[$p->id]['month'],
$p->supplier_reference,
$p->name[$id_lang],
$customs['nc8'],
'',
$customs['id_country'],
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
11,
str_replace('.', ',', Tools::ps_round(($p->weight * $quantity_sold),2)),
$quantity_sold,
11,
3,
31
), ';', '"');*/
}
if ($quantity_sold == 0) {
continue;
}
$pos++;
fputcsv($f, array(
//$p->position,
$pos,
$category_properties[$p->id]['country'],
$category_properties[$p->id]['name'],
$category_properties[$p->id]['month'],
$p->supplier_reference,
$p->name[$id_lang],
$customs['nc8'],
'',
$customs['id_country'],
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
11,
str_replace('.', ',', Tools::ps_round(($p->weight * $quantity_sold),2)),
$quantity_sold,
11,
3,
31
), ';', '"');
}
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Export complete.').' <a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/'.$fname.'">'.$this->l('Click here to download the file').'</a></p>';
break;
case '2':
// DEB EXPEDITION
// Seulement les expeditions vers Espagne et Belgique
$orders = Db::getInstance()->ExecuteS('
SELECT DISTINCT od.`id_order_detail`, od.`product_id`, psc.`id_sale`, a.`id_country` as country_exp
FROM `'._DB_PREFIX_.'order_detail` od
LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_order` = od.`id_order`)
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` psc ON (psc.`id_product` = od.`product_id`)
LEFT JOIN `'._DB_PREFIX_.'address` a ON (a.`id_address` = o.`id_address_delivery`)
WHERE
o.date_add >= "'.$date_start.' 00:00:00"
AND o.date_add <= "'.$date_end.' 23:59:59"
AND a.`id_country` IN (3,6)
AND
(o.valid = 1
OR (
o.valid = 0
AND (
(SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 6
OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 7
OR (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) = 11
)
))
AND (SELECT h.id_order_state FROM `'._DB_PREFIX_.'order_history` h WHERE h.id_order = o.id_order ORDER BY h.date_add DESC LIMIT 1) != 14
-- o.`date_add` BETWEEN "'. $date_start .' 00:00:00" AND "'. $date_end .' 00:00:00
-- AND (
-- SELECT `id_order_state`
-- FROM `'._DB_PREFIX_.'order_history`
-- WHERE `id_order`=od.`id_order`
-- ORDER BY `date_add` DESC LIMIT 1
-- ) NOT IN (1,6,8,9,10,11,14,15,18)
ORDER BY od.`product_id` ASC
');
if (!$orders) {
$output .= '<p class="error">Erreur produit: Pas de commandes trouvées pour ces dates</p>';
break;
}
setlocale(LC_TIME, "fr_FR");
$products = array();
$orders_detail = array();
$row_properties = array();
$by_sale = array();
foreach($orders as $order) {
$products[] = $order['product_id'];
$orders_detail[] = $order['id_order_detail'];
if (!isset($row_properties[(int)$order['product_id']])) {
$by_sale[(int)$order['id_sale']] = array();
$privatesale = new Sale((int) $order['id_sale']);
$country_ps = '';
if ($privatesale->id_country > 0) {
$country_ps = Country::getNameById((int)$id_lang,(int)$privatesale->id_country);
}
$country_exp = Country::getNameById((int)$id_lang,(int)$order['country_exp']);
$row_properties[(int)$order['product_id']] = array(
'name' => $privatesale->title[(int)$id_lang],
'country' => $country_ps,
'month' => strftime("%B", strtotime($privatesale->date_start)),
'date' => strtotime($privatesale->date_start),
'id_sale' => (int) $order['id_sale'],
'country_exp' => $country_exp
);
}
}
$products = array_unique($products);
if(count($products) > 20000){
$output .= '<p class="conf">Choisir une plage moins grande. <br /> Export > 20000 produits</p>';
break;
};
$fname = 'DEB-exp-'.date( "Ymd", strtotime($date_start)).'-'.date( "Ymd", strtotime($date_end)).'.csv';
foreach(glob(dirname(__FILE__).'/*.csv') as $filename) {
if ($fname == $filename) {
unlink($filename);
}
}
$f = fopen(dirname(__FILE__).'/'.$fname, 'w');
// uft8 sans bom pour accent
$BOM = "\xEF\xBB\xBF"; // UTF-8 BOM
fwrite($f, $BOM); // NEW LINE
fputcsv($f, array(
'Ordre',
'Pays de provenance',
'Marque',
'Mois',
'Référence fournisseur',
'Produit',
'Nomenclature',
'NGP9',
'Pays de destination',
'Pays d\'origine',
'Valeur',
'Régime',
'Masse',
'Unité supplémentaire',
'Nature de transaction',
'Mode de transport',
'Département '
), ';', '"');
$products_detail = array();
foreach($products as $product) {
$p = new Product((int) $product, $id_lang);
if(!Validate::isLoadedObject($p)) {
$output .= '<p class="error">Erreur produit: '.serialize($product).' ce produit ne figure pas dans l\'export</p>';
} else {
$customs = Db::getInstance()->getRow('
SELECT `nc8`, `id_country`
FROM `'._DB_PREFIX_.'product_customs`
WHERE `id_product` = '.(int) $p->id
);
$position = Db::getInstance()->getValue('
SELECT `position`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int) $p->id_category_default.'
AND id_product = '. (int) $p->id . '
');
$p->position = $position;
$combinations = array();
foreach($p->getAttributeCombinaisons($id_lang) as $combi) {
if(!isset($combinations[$combi['id_product_attribute']])) {
$combinations[$combi['id_product_attribute']] = array(
'qty' => $combi['quantity'],
'name' => array($combi['attribute_name']),
'ean13' => $combi['ean13'],
'location' => empty($combi['location'])? $p->location: $combi['location'],
'brand' => $p->manufacturer_name,
'supplier_reference' => empty($combi['supplier_reference'])? $p->supplier_reference: $combi['supplier_reference'],
);
} else {
$combinations[$combi['id_product_attribute']]['name'][] = $combi['attribute_name'];
}
}
$quantity_sold = 0;
if(count($combinations) > 0) {
foreach($combinations as $k => $v) {
$quantity_sold += (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity`)
FROM `'._DB_PREFIX_.'order_detail` d
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`product_attribute_id` = '.(int) $k.'
AND d.`id_order_detail` IN ('.(count($orders_detail)>1?implode(',',$orders_detail):$orders_detail[0]).')
');
/*if ($quantity_sold == 0) {
continue;
}
$names = array_unique($v['name']);
sort($names, SORT_STRING);
$products_detail[] = array(
'position' => $p->position,
'sale_country' => $row_properties[$p->id]['country'],
'sale_name' => $row_properties[$p->id]['name'],
'sale_month' => $row_properties[$p->id]['month'],
'supplier_reference' => $v['supplier_reference'],
'product_name' => $p->name[$id_lang].' - '.implode(' ', $names),
'nc8' => $customs['nc8'],
'NGP9' => '',
'id_country' => $customs['id_country'],
'value' => str_replace('.', ',', ($quantity_sold * $p->wholesale_price)),
'regime' => 21,
'weight' => str_replace('.', ',', Tools::ps_round(($p->weight * $quantity_sold),2)),
'quantity_sold' => $quantity_sold,
'transaction_type' => 11,
'transport' => 3,
'department' => 31,
'sale_date' => $row_properties[$p->id]['date'],
'id_sale' => $row_properties[$p->id]['id_sale'],
);*/
}
} else {
$quantity_sold += (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity`)
FROM `'._DB_PREFIX_.'order_detail` d
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`id_order_detail` IN ('.(count($orders_detail)>1?implode(',',$orders_detail):$orders_detail[0]).')
');
/* if ($quantity_sold == 0) {
continue;
}
$products_detail[] = array(
'position' => $p->position,
'sale_country' => $row_properties[$p->id]['country'],
'sale_name' => $row_properties[$p->id]['name'],
'sale_month' => $row_properties[$p->id]['month'],
'supplier_reference' => $p->supplier_reference,
'product_name' => $p->name[$id_lang],
'nc8' => $customs['nc8'],
'NGP9' => '',
'id_country' => $customs['id_country'],
'value' => str_replace('.', ',', ($quantity_sold * $p->wholesale_price)),
'regime' => 21,
'weight' => str_replace('.', ',', Tools::ps_round(($p->weight * $quantity_sold),2)),
'quantity_sold' => $quantity_sold,
'transaction_type' => 11,
'transport' => 3,
'department' => 31,
'sale_date' => $row_properties[$p->id]['date'],
'id_sale' => $row_properties[$p->id]['id_sale'],
);*/
}
if ($quantity_sold == 0) {
continue;
}
$products_detail[] = array(
'position' => $p->position,
'sale_country' => $row_properties[$p->id]['country'],
'sale_name' => $row_properties[$p->id]['name'],
'sale_month' => $row_properties[$p->id]['month'],
'supplier_reference' => $p->supplier_reference,
'product_name' => $p->name[$id_lang],
'nc8' => $customs['nc8'],
'NGP9' => '',
'country_exp' => $row_properties[$p->id]['country_exp'],
'product_country' => $customs['id_country'],
'value' => str_replace('.', ',', ($quantity_sold * Product::getPriceStatic($p->id, false, NULL, 2))),
'regime' => 21,
'weight' => str_replace('.', ',', Tools::ps_round(($p->weight * $quantity_sold),2)),
'quantity_sold' => $quantity_sold,
'transaction_type' => 11,
'transport' => 3,
'department' => 31,
'sale_date' => $row_properties[$p->id]['date'],
'id_sale' => $row_properties[$p->id]['id_sale'],
);
}
}
foreach ($products_detail as $key => $row) {
$id_sale[$key] = (int)$row['id_sale'];
$sale_date[$key] = (int)$row['sale_date'];
}
array_multisort($sale_date, SORT_ASC, $id_sale, SORT_DESC, $products_detail);
$pos = 0;
foreach ($products_detail as $key => $product_d) {
$pos++;
fputcsv($f, array(
//$product_d['position'],
$pos,
$product_d['sale_country'],
$product_d['sale_name'],
$product_d['sale_month'],
$product_d['supplier_reference'],
$product_d['product_name'],
$product_d['nc8'],
$product_d['NGP9'],
$product_d['country_exp'],
$product_d['product_country'],
$product_d['value'],
$product_d['regime'],
$product_d['weight'],
$product_d['quantity_sold'],
$product_d['transaction_type'],
$product_d['transport'],
$product_d['department']
), ';', '"');
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Export complete.').' <a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/'.$fname.'">'.$this->l('Click here to download the file').'</a></p>';
break;
default:
$output .= '<p class="error">Veuillez choisir un type d\'export</p>';
break;
}
} elseif(Tools::isSubmit('submitExport')) {
set_time_limit(300);
$id_lang = Tools::getValue('id_lang', $cookie->id_lang);
$order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES'));
if ((int)Tools::getValue('category')!=0 && $id_category = (int) Tools::getValue('category')) {
$c = new Category($id_category, $cookie->id_lang);
$children = $c->recurseLiteCategTree(5, 0, $id_lang);
$ids = $this->_recurse_array(array($children));
$products = array();
foreach($c->getProductsWs() as $p) {
$products[] = $p['id'];
}
foreach($ids as $id) {
$sc = new Category($id, $id_lang);
foreach($sc->getProductsWs() as $p) {
$products[] = $p['id'];
}
}
$products = array_unique($products);
$privatesale = Sale::getSaleFromCategory((int) $id_category);
$fname = date( "Ymd", strtotime($privatesale->date_start)).'-'.$privatesale->title[(int)$id_lang].'-'.$id_category.'.csv';
foreach(glob(dirname(__FILE__).'/*.csv') as $filename) {
if ($fname == $filename) {
unlink($filename);
}
}
$f = fopen(dirname(__FILE__).'/'.$fname, 'w');
// uft8 sans bom pour accent
$BOM = "\xEF\xBB\xBF"; // UTF-8 BOM
fwrite($f, $BOM); // NEW LINE
fputcsv($f, array(
'supplier_reference',
'ean13',
'id_product',
'id_product_attribute',
'position',
'poids',
'stock_initial',
'quantity_sold',
'quantity',
'product_name',
'combination',
'NC8',
'Pays de fabrication',
// 'location',
// 'brand',
'public_price_wt',
'price_wt',
'wholesale_price',
'BDC HT',
'id_TVA',
// 'active',
'description_short',
'bon_a_savoir',
'plus',
'videos',
'livraison',
'images',
'nb_images',
'categories',
'sous-categories',
'sous-sous-categories',
// 'categories_title',
), ';', '"');
foreach($products as $product) {
$p = new Product((int) $product, $id_lang);
if(!Validate::isLoadedObject($p)) {
$output .= '<p class="error">Erreur produit: '.serialize($product).' ce produit ne figure pas dans l\'export</p>';
} else {
$position = Db::getInstance()->getValue('
SELECT `position`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int) $id_category.'
AND id_product = '. (int) $p->id . '
');
$p->position = $position;
$categories = array();
foreach(Db::getInstance()->ExecuteS('
SELECT `id_category`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_product` = '.(int) $p->id.'
') as $cat) {
$categories[] = (int) $cat['id_category'];
}
sort($categories);
$categories_title = array();
foreach ($categories as $category) {
$title = Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'category_lang`
WHERE `id_category` = '.(int) $category.'
AND id_lang = '. $id_lang . '
');
$categories_title[(int) $category] = $title;
}
$categorie_0 = '';
$categorie_1 = '';
$categorie_2 = '';
foreach ($categories as $key => $cat) {
if ($key == 0) {
$categorie_0 = $categories_title[$cat];
} elseif ($key == 1) {
$categorie_1 = $categories_title[$cat];
} elseif ($key == 2) {
$categorie_2 = $categories_title[$cat];
}
}
$combinations = array();
foreach($p->getAttributeCombinaisons($id_lang) as $combi) {
if(!isset($combinations[$combi['id_product_attribute']])) {
$combinations[$combi['id_product_attribute']] = array(
'qty' => $combi['quantity'],
'name' => array($combi['attribute_name']),
'ean13' => $combi['ean13'],
'location' => empty($combi['location'])? $p->location: $combi['location'],
'brand' => $p->manufacturer_name,
'supplier_reference' => empty($combi['supplier_reference'])? $p->supplier_reference: $combi['supplier_reference'],
);
} else {
$combinations[$combi['id_product_attribute']]['name'][] = $combi['attribute_name'];
}
}
$images = array();
foreach(Db::getInstance()->ExecuteS('
SELECT i.`id_image`, l.`legend`
FROM `'._DB_PREFIX_.'image` i
LEFT JOIN `'._DB_PREFIX_.'image_lang` l
ON l.`id_image` = i.`id_image`
WHERE l.`id_lang` = '.(int) $id_lang.'
AND i.`id_product` = '.(int) $p->id.'
ORDER BY i.`position`
') as $img) {
$link_image = str_split($img['id_image']);
$images[] = 'https://static.bebeboutik.com/img/p/'.implode('/', $link_image) .'/'. $img['id_image'].'.jpg';
}
$customs = Db::getInstance()->getRow('
SELECT `nc8`, `id_country`
FROM `'._DB_PREFIX_.'product_customs`
WHERE `id_product` = '.(int) $p->id
);
if(count($combinations) > 0) {
foreach($combinations as $k => $v) {
$c_images = array();
foreach(Db::getInstance()->ExecuteS('
SELECT i.`id_image`, l.`legend`
FROM `'._DB_PREFIX_.'image` i
LEFT JOIN `'._DB_PREFIX_.'image_lang` l
ON l.`id_image` = i.`id_image`
LEFT JOIN `'._DB_PREFIX_.'product_attribute_image` a
ON a.`id_image` = i.`id_image`
WHERE l.`id_lang` = '.(int) $id_lang.'
AND a.`id_product_attribute` = '.(int) $k.'
ORDER BY i.`position`
') as $img) {
$link_image = str_split($img['id_image']);
$c_images[] = 'https://static.bebeboutik.com/img/p/'.implode('/', $link_image) .'/'. $img['id_image'].'.jpg';
// $c_images[] = 'http://static.bebeboutik.com/'.(int) $p->id.'-'.$img['id_image'].'.jpg';
}
$quantity_sold = (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity` - d.`product_quantity_reinjected`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order`
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`product_attribute_id` = '.(int) $k.'
AND (
SELECT `id_order_state`
FROM `'._DB_PREFIX_.'order_history` oh
WHERE o.`id_order` = oh.`id_order`
ORDER BY `id_order_history` DESC
LIMIT 1
) IN ("'.implode('","', $order_states).'")
');
// $quantity_sold = (int) Db::getInstance()->getValue('
// SELECT SUM(d.`product_quantity`)
// FROM `'._DB_PREFIX_.'order_detail` d
// LEFT JOIN `'._DB_PREFIX_.'orders` o
// ON o.`id_order` = d.`id_order`
// LEFT JOIN `'._DB_PREFIX_.'order_history` h
// ON h.`id_order` = d.`id_order`
// WHERE d.`product_id` = '.(int) $p->id.'
// AND d.`product_attribute_id` = '.(int) $k.'
// AND h.`id_order_state` = 2
// ');
$names = array_unique($v['name']);
sort($names, SORT_STRING);
fputcsv($f, array(
$v['supplier_reference'],
$v['ean13'],
$p->id,
$k,
$p->position,
str_replace('.', ',', $p->weight),
($v['qty']+$quantity_sold),
$quantity_sold,
$v['qty'],
$p->name[$id_lang],
implode(' - ', $names),
$customs['nc8'],
$customs['id_country'],
// $v['location'],
// $v['brand'],
str_replace('.', ',', $p->getPrice(TRUE, (int) $k, 2, NULL, FALSE, FALSE)),
str_replace('.', ',', $p->getPrice(TRUE, (int) $k, 2)),
str_replace('.', ',', Tools::ps_round($p->wholesale_price, 2)),
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
$p->id_tax_rules_group,
// $p->active,
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description_short[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description_more[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->videos[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description_delivery[$id_lang]),
count($c_images) > 0? implode(', ', $c_images): implode(', ', $images),
count($c_images) > 0? count($c_images): count($images),
$categorie_0,
$categorie_1,
$categorie_2,
//implode(', ', $categories),
//implode(', ', $categories_title),
), ';', '"');
}
} else {
$quantity_sold = (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity` - d.`product_quantity_reinjected`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order`
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`product_attribute_id` = 0
AND (
SELECT `id_order_state`
FROM `'._DB_PREFIX_.'order_history` oh
WHERE o.`id_order` = oh.`id_order`
ORDER BY `id_order_history` DESC
LIMIT 1
) IN ("'.implode('","', $order_states).'")
');
// $quantity_sold = (int) Db::getInstance()->getValue('
// SELECT SUM(d.`product_quantity`)
// FROM `'._DB_PREFIX_.'order_detail` d
// LEFT JOIN `'._DB_PREFIX_.'orders` o
// ON o.`id_order` = d.`id_order`
// LEFT JOIN `'._DB_PREFIX_.'order_history` h
// ON h.`id_order` = d.`id_order`
// WHERE d.`product_id` = '.(int) $p->id.'
// AND h.`id_order_state` = 2
// ');
fputcsv($f, array(
$p->supplier_reference,
$p->ean13,
$p->id,
0,
$p->position,
str_replace('.', ',', $p->weight),
($p->quantity+$quantity_sold),
$quantity_sold,
$p->quantity,
$p->name[$id_lang],
'',
$customs['nc8'],
$customs['id_country'],
// $p->location,
// $p->manufacturer_name,
str_replace('.', ',', $p->getPrice(TRUE, NULL, 2, NULL, FALSE, FALSE)),
str_replace('.', ',', $p->getPrice(TRUE, NULL, 2)),
str_replace('.', ',', Tools::ps_round($p->wholesale_price, 2)),
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
$p->id_tax_rules_group,
// $p->active,
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description_short[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description_more[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->videos[$id_lang]),
str_replace(array('&euro;','&amp;'),array('€','&'),$p->description_delivery[$id_lang]),
implode(', ', $images),
count($images),
$categorie_0,
$categorie_1,
$categorie_2,
// implode(', ', $categories),
// implode(', ', $categories_title),
), ';', '"');
}
}
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Export complete.').' <a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/'.$fname.'">'.$this->l('Click here to download the file').'</a></p>';
} elseif (Tools::getValue('date_start') && Tools::getValue('date_end')) {
$date_start = Tools::getValue('date_start');
$date_end = Tools::getValue('date_end');
$order_states = explode(',', Configuration::get('PS_IT_OF_ORDER_STATES'));
$ids = Db::getInstance()->ExecuteS('
SELECT c.id_category
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'privatesale_category` c ON c.id_sale = p.id_sale
WHERE date_start BETWEEN "'. $date_start .' 00:00:00" AND "'. $date_end .' 00:00:00"
ORDER BY c.id_sale DESC
');
$products = array();
foreach($ids as $id) {
$id_category = $id['id_category'];
$sc = new Category($id_category, $id_lang);
foreach($sc->getProductsWs() as $p) {
$products[] = $p['id'];
}
}
$products = array_unique($products);
if(count($products) > 20000){
echo $output .= '<p class="conf">Choisir une plage moins grande. <br /> Export > 20000 produits</p>';
continue;
};
$privatesale = Sale::getSaleFromCategory((int) $id_category);
$fname = date( "Ymd", strtotime($date_start)).'-'.date( "Ymd", strtotime($date_end)).'.csv';
foreach(glob(dirname(__FILE__).'/*.csv') as $filename) {
if ($fname == $filename) {
unlink($filename);
}
}
$f = fopen(dirname(__FILE__).'/'.$fname, 'w');
// uft8 sans bom pour accent
$BOM = "\xEF\xBB\xBF"; // UTF-8 BOM
fwrite($f, $BOM); // NEW LINE
fputcsv($f, array(
'supplier_reference',
'ean13',
'id_product',
'id_product_attribute',
'position',
'poids',
'stock_initial',
'quantity_sold',
'quantity',
'product_name',
'combination',
'NC8',
'Pays de fabrication',
// 'location',
// 'brand',
'public_price_wt',
'price_wt',
'wholesale_price',
'BDC HT',
// 'active',
'description_short',
'bon_a_savoir',
'plus',
'videos',
'livraison',
'images',
'categories',
'sous-categories',
'sous-sous-categories',
// 'categories_title',
), ';', '"');
foreach($products as $product) {
$p = new Product((int) $product, $id_lang);
if(!Validate::isLoadedObject($p)) {
$output .= '<p class="error">Erreur produit: '.serialize($product).' ce produit ne figure pas dans l\'export</p>';
} else {
$position = Db::getInstance()->getValue('
SELECT `position`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_category` = '.(int) $p->id_category_default.'
AND id_product = '. (int) $p->id . '
');
$p->position = $position;
$categories = array();
foreach(Db::getInstance()->ExecuteS('
SELECT `id_category`
FROM `'._DB_PREFIX_.'category_product`
WHERE `id_product` = '.(int) $p->id.'
') as $cat) {
$categories[] = (int) $cat['id_category'];
}
sort($categories);
$categories_title = array();
foreach ($categories as $category) {
$title = Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'category_lang`
WHERE `id_category` = '.(int) $category.'
AND id_lang = '. $id_lang . '
');
$categories_title[] = $title;
}
$categorie_0 = '';
$categorie_1 = '';
$categorie_2 = '';
foreach ($categories as $key => $cat) {
if ($key == 0) {
$categorie_0 = $categories_title[$cat];
} elseif ($key == 1) {
$categorie_1 = $categories_title[$cat];
} elseif ($key == 2) {
$categorie_2 = $categories_title[$cat];
}
}
$combinations = array();
foreach($p->getAttributeCombinaisons($id_lang) as $combi) {
if(!isset($combinations[$combi['id_product_attribute']])) {
$combinations[$combi['id_product_attribute']] = array(
'qty' => $combi['quantity'],
'name' => array($combi['attribute_name']),
'ean13' => $combi['ean13'],
'location' => empty($combi['location'])? $p->location: $combi['location'],
'brand' => $p->manufacturer_name,
'supplier_reference' => empty($combi['supplier_reference'])? $p->supplier_reference: $combi['supplier_reference'],
);
} else {
$combinations[$combi['id_product_attribute']]['name'][] = $combi['attribute_name'];
}
}
$images = array();
foreach(Db::getInstance()->ExecuteS('
SELECT i.`id_image`, l.`legend`
FROM `'._DB_PREFIX_.'image` i
LEFT JOIN `'._DB_PREFIX_.'image_lang` l
ON l.`id_image` = i.`id_image`
WHERE l.`id_lang` = '.(int) $id_lang.'
AND i.`id_product` = '.(int) $p->id.'
ORDER BY i.`position`
') as $img) {
$link_image = str_split($img['id_image']);
$images[] = 'https://static.bebeboutik.com/img/p/'.implode('/', $link_image) .'/'. $img['id_image'].'.jpg';
}
if(count($combinations) > 0) {
foreach($combinations as $k => $v) {
$c_images = array();
foreach(Db::getInstance()->ExecuteS('
SELECT i.`id_image`, l.`legend`
FROM `'._DB_PREFIX_.'image` i
LEFT JOIN `'._DB_PREFIX_.'image_lang` l
ON l.`id_image` = i.`id_image`
LEFT JOIN `'._DB_PREFIX_.'product_attribute_image` a
ON a.`id_image` = i.`id_image`
WHERE l.`id_lang` = '.(int) $id_lang.'
AND a.`id_product_attribute` = '.(int) $k.'
ORDER BY i.`position`
') as $img) {
$link_image = str_split($img['id_image']);
$c_images[] = 'https://static.bebeboutik.com/img/p/'.implode('/', $link_image) .'/'. $img['id_image'].'.jpg';
// $c_images[] = 'http://static.bebeboutik.com/'.(int) $p->id.'-'.$img['id_image'].'.jpg';
}
$names = array_unique($v['name']);
sort($names, SORT_STRING);
$quantity_sold = (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity` - d.`product_quantity_reinjected`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order`
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`product_attribute_id` = '.(int) $k.'
AND (
SELECT `id_order_state`
FROM `'._DB_PREFIX_.'order_history` oh
WHERE o.`id_order` = oh.`id_order`
ORDER BY `id_order_history` DESC
LIMIT 1
) IN ("'.implode('","', $order_states).'")
');
// $quantity_sold = (int) Db::getInstance()->getValue('
// SELECT SUM(d.`product_quantity`)
// FROM `'._DB_PREFIX_.'order_detail` d
// LEFT JOIN `'._DB_PREFIX_.'orders` o
// ON o.`id_order` = d.`id_order`
// LEFT JOIN `'._DB_PREFIX_.'order_history` h
// ON h.`id_order` = d.`id_order`
// WHERE d.`product_id` = '.(int) $p->id.'
// AND d.`product_attribute_id` = '.(int) $k.'
// AND h.`id_order_state` = 2
// ');
$customs = Db::getInstance()->getRow('
SELECT `nc8`, `id_country`
FROM `'._DB_PREFIX_.'product_customs`
WHERE `id_product` = '.(int) $p->id
);
fputcsv($f, array(
$v['supplier_reference'],
$v['ean13'],
$p->id,
$k,
$p->position,
str_replace('.', ',', $p->weight),
($v['qty']+$quantity_sold),
$quantity_sold,
$v['qty'],
$p->name[$id_lang],
implode(' - ', $names),
$customs['nc8'],
$customs['id_country'],
// $v['location'],
// $v['brand'],
str_replace('.', ',', $p->getPrice(TRUE, (int) $k, 2, NULL, FALSE, FALSE)),
str_replace('.', ',', $p->getPrice(TRUE, (int) $k, 2)),
str_replace('.', ',', Tools::ps_round($p->wholesale_price, 2)),
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
// $p->active,
$p->description_short[$id_lang],
$p->description[$id_lang],
$p->description_more[$id_lang],
$p->videos[$id_lang],
$p->description_delivery[$id_lang],
count($c_images) > 0? implode(', ', $c_images): implode(', ', $images),
$categorie_0,
$categorie_1,
$categorie_2,
// implode(', ', $categories),
// implode(', ', $categories_title),
), ';', '"');
}
} else {
$quantity_sold = (int) Db::getInstance()->getValue('
SELECT SUM(d.`product_quantity` - d.`product_quantity_reinjected`)
FROM `'._DB_PREFIX_.'order_detail` d
LEFT JOIN `'._DB_PREFIX_.'orders` o ON o.`id_order` = d.`id_order`
WHERE d.`product_id` = '.(int) $p->id.'
AND d.`product_attribute_id` = 0
AND (
SELECT `id_order_state`
FROM `'._DB_PREFIX_.'order_history` oh
WHERE o.`id_order` = oh.`id_order`
ORDER BY `id_order_history` DESC
LIMIT 1
) IN ("'.implode('","', $order_states).'")
');
// SELECT SUM(d.`product_quantity`)
// FROM `'._DB_PREFIX_.'order_detail` d
// LEFT JOIN `'._DB_PREFIX_.'orders` o
// ON o.`id_order` = d.`id_order`
// LEFT JOIN `'._DB_PREFIX_.'order_history` h
// ON h.`id_order` = d.`id_order`
// WHERE d.`product_id` = '.(int) $p->id.'
// AND h.`id_order_state` = 2
$customs = Db::getInstance()->getRow('
SELECT `nc8`, `id_country`
FROM `'._DB_PREFIX_.'product_customs`
WHERE `id_product` = '.(int) $p->id
);
fputcsv($f, array(
$p->supplier_reference,
$p->ean13,
$p->id,
0,
$p->position,
str_replace('.', ',', $p->weight),
($p->quantity+$quantity_sold),
$quantity_sold,
$p->quantity,
$p->name[$id_lang],
'',
$customs['nc8'],
$customs['id_country'],
// $p->location,
// $p->manufacturer_name,
str_replace('.', ',', $p->getPrice(TRUE, NULL, 2, NULL, FALSE, FALSE)),
str_replace('.', ',', $p->getPrice(TRUE, NULL, 2)),
str_replace('.', ',', Tools::ps_round($p->wholesale_price, 2)),
str_replace('.', ',', Tools::ps_round(($quantity_sold * $p->wholesale_price),2)),
// $p->active,
$p->description_short[$id_lang],
$p->description[$id_lang],
$p->description_more[$id_lang],
$p->videos[$id_lang],
$p->description_delivery[$id_lang],
implode(', ', $images),
$categorie_0,
$categorie_1,
$categorie_2,
// implode(', ', $categories),
// implode(', ', $categories_title),
), ';', '"');
}
}
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Export complete.').' <a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/'.$fname.'">'.$this->l('Click here to download the file').'</a></p>';
}
} elseif(Tools::isSubmit('submitExportProducts')) {
HelperFormBootstrap::displaySuccess('<p>'.$this->l('Export complete.').' <a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/base/base_produits.csv.zip">'.$this->l('Click here to download the file').'</a></p>');
} elseif(Tools::isSubmit('submitExportPositions')) {
set_time_limit(300);
if($id_category = (int) Tools::getValue('category')) {
foreach(glob(dirname(__FILE__).'/*.csv') as $filename) {
unlink($filename);
}
$fname = Tools::passwdGen(10).'.csv';
$f = fopen(dirname(__FILE__).'/'.$fname, 'w');
fputcsv($f, array(
'id_category',
'id_product',
'position',
'name',
), ';', '"');
foreach(Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product_lang` l
ON cp.`id_product` = l.`id_product`
WHERE cp.`id_category` = '.(int) $id_category.'
AND l.`id_lang` = 2
') as $row) {
fputcsv($f, array(
(int) $row['id_category'],
(int) $row['id_product'],
(int) $row['position'],
$row['name'],
), ';', '"');
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Export complete.').' <a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/'.$fname.'">'.$this->l('Click here to download the file').'</a></p>';
}
}
elseif(Tools::isSubmit('submitUploadPositions')) {
set_time_limit(300);
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$products = array();
while($line = fgetcsv($f, 0, ';')) {
if($line[0] == '' || $line[1] == '' || $line[2] == '') {
continue;
}
$products[] = $line;
}
foreach($products as $line) {
Db::getInstance()->ExecuteS('
UPDATE `'._DB_PREFIX_.'category_product`
SET `position` = '.(int) $line[2].'
WHERE `id_product` = '.(int) $line[1].'
AND `id_category` = '.(int) $line[0].'
');
}
fclose($f);
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
}
echo $output;
}
public function display(){
global $cookie;
$option_langs = array();
$option_select_langs = array();
foreach (Language::getLanguages(false) as $key => $value) {
$option_langs[$key] = array(
'value' =>$value['id_lang'],
'label' => $value['iso_code']
);
$option_select_langs[$key] = array(
'value' =>$value['id_lang'],
'label' => $value['name']
);
if($value['id_lang'] == 2){
$option_langs[$key]['checked'] = 1;
$option_select_langs[$key]['selected'] = 1;
}
}
$option_sales = array();
$option_sales[] = array(
'value' => 0,
'label' => 'Selectionner une vente'
);
foreach(Db::getInstance()->ExecuteS('
SELECT c.`id_category`, l.`name`
FROM `'._DB_PREFIX_.'category_lang` l
LEFT JOIN `'._DB_PREFIX_.'category` c
ON c.`id_category` = l.`id_category`
WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
AND c.`id_parent` = 1
ORDER BY id_category DESC
'.((Tools::getIsset('nolimit') && Tools::getValue('nolimit')==1)?'':'LIMIT 1200').'
') as $row) {
$extrafields = Category::getSalesInfosWithDate(array((int) $row['id_category']));
$option_sales[] = array(
'value' => $row['id_category'],
'label' => '#'.$row['id_category'].' ('.date("d/m/Y",strtotime($extrafields[(int) $row['id_category']]['date_start'])).') - '.$row['name'].' - '.$extrafields[(int) $row['id_category']]['sales'][1]
);
}
$helperForm = new HelperFormBootstrap();
$helperForm->_select2 = true;
$helperForm->_inputSwitch = true;
$helperForm->_inputMask = true;
$output = $helperForm->renderStyle();
// Categories
$output .= '<div class="row">
<div class="col-md-12">
<div class="panel">
<h2 class="heading-title"><span class="anticon anticon-cog"></span> '.$this->l('Mises à jour groupées').'</h2>
<ul class="nav nav-tabs nav-justified">
<li role="presentation" class="active">
<a href="#categories" aria-controls="categories" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-tree"></span> '.$this->l('Categories').'</a>
</li>
<li role="presentation">
<a href="#prices" aria-controls="prices" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-coin-euro"></span> '.$this->l('Prices').'</a>
</li>
<li role="presentation">
<a href="#images" aria-controls="images" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-images"></span> '.$this->l('Images').'</a>
</li>
<li role="presentation">
<a href="#quantities" aria-controls="quantities" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-calculator"></span> '.$this->l('Quantities').'</a>
</li>
<li role="presentation">
<a href="#descriptions" aria-controls="descriptions" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-newspaper"></span> '.$this->l('Descriptions').'</a>
</li>
<li role="presentation">
<a href="#combinations" aria-controls="combinations" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-cross"></span> '.$this->l('Combinations').'</a>
</li>
<li role="presentation">
<a href="#eans" aria-controls="eans" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-barcode"></span> '.$this->l('EAN').'</a>
</li>
<li role="presentation">
<a href="#customs" aria-controls="customs" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-earth"></span> '.$this->l('Customs').'</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="categories">
<div class="panel-content">
<h3 class="">'.$this->l('Update categories').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$input = array(
'type' => 'radio2',
'label' => $this->l('Action'),
'label-class' => 'text-left',
'name' => 'category_process',
'options' => array(
array(
'value' => 0,
'label' => $this->l('Update')
),
array(
'value' => 1,
'label' => $this->l('Replace')
)
),
);
$output .= $helperForm->generateInput($input);
$help = '<br>
<p class="help-block">'.$this->l('Format : id_produit;id_catégorie,id_catégorie,...;id_catégorie_défaut').'</p>
<p class="help-block">'.$this->l('The subsequent columns and the first line are ignored').'</p>
';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input).'</div>';
$output .= '
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadCategories">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="prices">
<div class="panel-content">
<h3 class="">'.$this->l('Update prices').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$input = array(
'type' => 'radio2',
'label' => $this->l('Action'),
'label-class' => 'text-left',
'name' => 'price_process',
'options' => array(
array(
'value' => 1,
'label' => $this->l('Réduction')
),
array(
'value' => 3,
'label' => $this->l('Prix d\'achat HT')
),
array(
'value' => 4,
'label' => $this->l('Prix public TTC')
)
),
);
$output .= $helperForm->generateInput($input);
$help = '<br><p class="help-block">'.$this->l('Format : id_product;montant').'</p>
<p class="help-block">'.$this->l('Montant : Pourcentage de remise, Prix HT, Prix TTC en fonction du choix').'</p>';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input).'</div>';
$output .= '<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadPrices">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="images">
<div class="panel-content">
<h3 class="">'.$this->l('Update images').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$input = array(
'type' => 'radio2',
'label' => $this->l('Action'),
'label-class' => 'text-left',
'select-class' => 'text-left',
'name' => 'image_process',
'options' => array(
array(
'value' => 0,
'label' => $this->l('Update')
),
array(
'value' => 1,
'label' => $this->l('Replace')
)
),
);
$help = '<br>
<p class="help-block">'.$this->l('Format: id_product;image_url,image_url,...').'</p>
<p class="help-block">'.$this->l('the subsequent columns and the first line are ignored').'</p>';
$output .= $helperForm->generateInput($input);
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input);
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadImages">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="quantities">
<div class="panel-content">
<h3 class="">'.$this->l('Update quantities').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$input = array(
'type' => 'radio2',
'label' => $this->l('Action'),
'label-class' => 'text-left',
'name' => 'qty_process',
'options' => array(
array(
'value' => 0,
'label' => $this->l('Update')
),
array(
'value' => 1,
'label' => $this->l('Replace')
)
),
);
$output .= $helperForm->generateInput($input);
$help = '<br>
<p class="help-block">'.$this->l('Format: id_product;id_product_attribute;quantity').'</p>
<p class="help-block">'.$this->l('The subsequent columns and the first line are ignored.').'</p>
';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input);
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadQuantities">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>';
// Product Description
$output .= '
<div role="tabpanel" class="tab-pane" id="descriptions">
<div class="panel-content">
<h3 class="">'.$this->l('Update product descriptions').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$input = array(
'type' => 'radio2',
'label' => $this->l('Action'),
'label-class' => 'text-left',
'name' => 'description_process',
'options' => $option_langs
);
$output .= $helperForm->generateInput($input);
$help = '<br>
<p class="help-block">'.$this->l('Format : id_product;description').'</p>
<p class="help-block">'.$this->l('The subsequent columns and the first line are ignored.').'</p>
';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input);
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadProductDescriptions">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="combinations">
<div class="panel-content">
<h3 class="">'.$this->l('Delete combinations').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$help = '<br>
<p class="help-block">'.$this->l('Format: id_product').'</p>
<p class="help-block">'.$this->l('The subsequent columns and the first line are ignored.').'</p>
';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input);
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadCombinations">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="eans">
<div class="panel-content">
<h3 class="">'.$this->l('Update EAN').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$help = '<br>
<p class="help-block">'.$this->l('Format : id_produit;id_product_attribute;ean').'</p>
<p class="help-block">'.$this->l('The subsequent columns and the first line are ignored.').'</p>
';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input);
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadEans">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="customs">
<div class="panel-content">
<h3 class="">'.$this->l('Update product customs').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-6 col-md-offset-4">';
$input = array(
'type' => 'radio2',
'label' => $this->l('Action'),
'label-class' => 'text-left',
'name' => 'customs_process',
'options' => array(
array(
'value' => 0,
'label' => $this->l('Update')
),
array(
'value' => 1,
'label' => $this->l('Replace')
)
),
);
$output .= $helperForm->generateInput($input);
$help = '<br>
<p class="help-block">'.$this->l('Format : id_product;nc8;country').'</p>
<p class="help-block">'.$this->l('The subsequent columns and the first line are ignored.').'</p>
';
$input = array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'csvfile',
'html' => $help
);
$output .= $helperForm->generateInput($input);
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitUploadProductCustoms">'.$this->l('Upload').'</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>';
// Sale Export
$output .= '
<div class="col-md-12">
<div class="panel">
<h2 class="heading-title"><span class="anticon anticon-upload2"></span> '.$this->l('Exports').'</h2>
<ul class="nav nav-tabs nav-justified">
<li role="presentation" class="active">
<a href="#exportSales" aria-controls="exportSales" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-upload"></span> '.$this->l('Export Ventes').'</a>
</li>
<li role="presentation">
<a href="#exportDEB" aria-controls="exportDEB" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-upload"></span> '.$this->l('Export DEB').'</a>
</li>
<li role="presentation">
<a href="#exportProducts" aria-controls="exportProducts" role="tab" data-toggle="tab"><span class="text-rose anticon anticon-upload"></span> '.$this->l('Export Produits').'</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="exportSales">
<div class="panel-content">
<h3 class="">'.$this->l('Export Ventes').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<br>
<div class="col-md-5">';
$input = array(
'type' => 'select2',
'name' => 'category',
'id' => 'category',
'label' => $this->l('Select a category to export:'),
'label-class' => 'col-md-12',
'input-class' => 'col-md-12',
'select-class' => 'col-md-12',
'options' => $option_sales
);
$output .= $helperForm->generateInput($input).
'</div>';
$output .='<div class="col-md-2 text-center"><span class="lateral-delimiter"></span></div>
<div class="col-md-5">';
$input = array(
'type' => 'simpleDate',
'period' => true,
'class-from' => 'col-md-6',
'class-to' => 'col-md-6',
'label' => 'Du :',
'label-to' => 'Au :',
'id' => 'date_start',
'id-to' => 'date_end',
'name' => 'date_start',
'name-to' => 'date_end',
'before' => '<span class="glyphicon glyphicon-calendar"></span>',
'before-to' => '<span class="glyphicon glyphicon-calendar"></span>',
);
$output .= $helperForm->generateInput($input).
'<p class="text-center help-block">Format : AAAA-MM-JJ</p>
</div>
<div class="clearfix"></div>
<br>
<div class="col-md-4 col-md-offset-4 text-center">';
$input = array(
'type' => 'select',
'name' => 'id_lang',
'id' => 'id_lang',
'label' => $this->l('Select a language:'),
'label-class' => 'col-md-12 text-center',
'input-class' => 'col-md-12',
'options' => $option_select_langs
);
$output .= $helperForm->generateInput($input).
'</div>';
$output .= '
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<a class="btn btn-default" href="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'&nolimit=1">'.$this->l('Charger toutes les catégories').'</a>
<button type="submit" class="btn btn-primary" name="submitExport">'.$this->l('Exporter').'</button>
</div>
</form>
</div>
</div>';
// Export Sale DEB
$output .= '
<div role="tabpanel" class="tab-pane" id="exportDEB">
<div class="panel-content">
<h3 class="">'.$this->l('Export DEB').'</h3>
<br>
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<div class="col-md-5 col-md-offset-4">';
$input = array(
'type' => 'select',
'name' => 'deb',
'id' => 'deb',
'label' => $this->l('Select a DEB:'),
'label-class' => 'col-md-12',
'input-class' => 'col-md-12',
'options' => array(
array(
'value' => 1,
'label' => 'DEB introduction'
),
array(
'value' => 2,
'label' => 'DEB expedition'
)
)
);
$output .= $helperForm->generateInput($input).
'<div class="clearfix"></div><br>
<div class="col-md-12">
<p class="text-left help-block">Introduction : Selon date des ventes</p>
<p class="text-left help-block">Expedition : Selon date des commandes</p>
</div>
<div class="clearfix"></div><br>';
$input = array(
'type' => 'simpleDate',
'period' => true,
'class-from' => 'col-md-6',
'class-to' => 'col-md-6',
'label' => 'Du :',
'label-to' => 'Au :',
'id' => 'date_start_deb',
'id-to' => 'date_end_deb',
'name' => 'date_start',
'name-to' => 'date_end',
'before' => '<span class="glyphicon glyphicon-calendar"></span>',
'before-to' => '<span class="glyphicon glyphicon-calendar"></span>',
);
$output .= $helperForm->generateInput($input).
'<p class="text-center help-block">Format : AAAA-MM-JJ</p>';
$output .= '</div>
<div class="clearfix"></div>
<div class="ln_solid"></div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary" name="submitExportDeb">'.$this->l('Exporter').'</button>
</div>
</form>
</div>
</div>';
// Export Products
$isFileProductsExist = false;
if(is_file(dirname(__FILE__).'/base/base_produits.csv.zip')) {
$isFileProductsExist = true;
}
$output .= '
<div role="tabpanel" class="tab-pane" id="exportProducts">
<div class="panel-content">
<h3 class="">'.$this->l('Export Base Produit').'</h3>
<br>
<div class="col-md-5 col-md-offset-4">
<p class="text-center">'.$this->l('Export de la base produit totale généré toutes les semaines.').'</p>'
.($isFileProductsExist?
'<p class="text-center">
<a onclick="window.open(this.href); return false;" href="http://'.$_SERVER['HTTP_HOST'].__PS_BASE_URI__.'modules/bulkupdate/base/base_produits.csv.zip">'.$this->l('Click here to download the file').'</a>
</p>'
:'<p class="text-center">'.$this->l('Pas de fichier trouvé !').'</p>'
).'
</div>
</div>
</div>
</div>
</div>
</div>';
// $output .= '
// <p><br /></p>
// <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
// <fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Update accessories').'</legend>
// <label>'.$this->l('Accessory process:').'</label>
// <div class="margin-form">
// <input type="radio" id="access_process_0" name="access_process" value="0" /> <label style="float: none;" for="access_process_0">'.$this->l('Update').'</label>
// <input type="radio" id="access_process_1" name="access_process" value="1" /> <label style="float: none;" for="access_process_1">'.$this->l('Replace').'</label>
// </div>
// <p> </p>
// <label>'.$this->l('File:').'</label>
// <div class="margin-form">
// <input type="file" name="csvfile" />
// <p>'.$this->l('Format: id_product_1;id_product_2 -- the subsequent columns and the first line are ignored').'</p>
// </div>
// <p> </p>
// <center><input type="submit" name="submitUploadAccessories" value="'.$this->l('Upload').'" class="button" /></center>
// </fieldset>
// </form>';
// $output .= '
// <p><br /></p>
// <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
// <fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Update products positions').'</legend>
// <div class="margin-form">
// <br />';
// if(is_file(dirname(__FILE__).'/ctree.html') && (time() - filectime(dirname(__FILE__).'/ctree.html') < 86400)) {
// $output .= file_get_contents(dirname(__FILE__).'/ctree.html');
// } else {
// $ctree = '
// <select id="category_positions" name="category">';
// foreach(Db::getInstance()->ExecuteS('
// SELECT c.`id_category`, l.`name`
// FROM `'._DB_PREFIX_.'category_lang` l
// LEFT JOIN `'._DB_PREFIX_.'category` c
// ON c.`id_category` = l.`id_category`
// WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
// AND c.`id_parent` = 1
// ') as $row) {
// $ctree .= '<option label="'.$row['name'].'">'.$row['id_category'].' - '.$row['name'].'</option>';
// foreach(Db::getInstance()->ExecuteS('
// SELECT c.`id_category`, l.`name`
// FROM `'._DB_PREFIX_.'category_lang` l
// LEFT JOIN `'._DB_PREFIX_.'category` c
// ON c.`id_category` = l.`id_category`
// WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
// AND c.`id_parent` = '.$row['id_category'].'
// ') as $srow) {
// $ctree .= '<option style="padding-left: 30px;" value="'.$srow['id_category'].'">'.$srow['id_category'].' - '.$srow['name'].'</option>';
// foreach(Db::getInstance()->ExecuteS('
// SELECT c.`id_category`, l.`name`
// FROM `'._DB_PREFIX_.'category_lang` l
// LEFT JOIN `'._DB_PREFIX_.'category` c
// ON c.`id_category` = l.`id_category`
// WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
// AND c.`id_parent` = '.$srow['id_category'].'
// ') as $ssrow) {
// $ctree .= '<option style="padding-left: 60px;" value="'.$ssrow['id_category'].'">'.$ssrow['id_category'].' - '.$ssrow['name'].'</option>';
// foreach(Db::getInstance()->ExecuteS('
// SELECT c.`id_category`, l.`name`
// FROM `'._DB_PREFIX_.'category_lang` l
// LEFT JOIN `'._DB_PREFIX_.'category` c
// ON c.`id_category` = l.`id_category`
// WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
// AND c.`id_parent` = '.$ssrow['id_category'].'
// ') as $sssrow) {
// $ctree .= '<option style="padding-left: 90px;" value="'.$sssrow['id_category'].'">'.$sssrow['id_category'].' - '.$sssrow['name'].'</option>';
// foreach(Db::getInstance()->ExecuteS('
// SELECT c.`id_category`, l.`name`
// FROM `'._DB_PREFIX_.'category_lang` l
// LEFT JOIN `'._DB_PREFIX_.'category` c
// ON c.`id_category` = l.`id_category`
// WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
// AND c.`id_parent` = '.$sssrow['id_category'].'
// ') as $ssssrow) {
// $ctree .= '<option style="padding-left: 120px;" value="'.$ssssrow['id_category'].'">'.$ssssrow['id_category'].' - '.$ssssrow['name'].'</option>';
// }
// }
// }
// }
// }
// $ctree .= '</select>';
// file_put_contents(dirname(__FILE__).'/ctree.html', $ctree);
// }
// $output .= '
// <p class="text">
// <input type="text" style="width: 300px;" id="filter_categories" value="" autocomplete="off" placeholder="'.$this->l('Filter by category').'" />
// </p>
// <script type="text/javascript">
// <!--
// var filter_timeout = null;
// $("#filter_categories").keyup(function() {
// clearTimeout(filter_timeout);
// filter_timeout = setTimeout(function() {
// $("#category_positions option").css("display", "none");
// var filter_str = $("#filter_categories").val().toLowerCase();
// $("#category_positions option").filter(function(index) {
// return $(this).text().toLowerCase().indexOf(filter_str) >= 0;
// }).css("display", "block");
// }, 300);
// });
// -->
// </script>
// </div>
// <p> </p>
// <center><input type="submit" name="submitExportPositions" value="'.$this->l('Export').'" class="button" /></center>
// <p> </p>
// <hr />
// <p> </p>
// <label>'.$this->l('File:').'</label>
// <div class="margin-form">
// <input type="file" name="csvfile" />
// <p>'.$this->l('Format: id_category;id_product;position -- the subsequent columns and the first line are ignored').'</p>
// </div>
// <p> </p>
// <center><input type="submit" name="submitUploadPositions" value="'.$this->l('Upload').'" class="button" /></center>
// </fieldset>
// </form>';
$output .='</div>';
$helperForm->_js .= '<script type="text/javascript">
$(document).ready(function() {
$("#date_start").inputmask("9999-99-99");
$("#date_end").inputmask("9999-99-99");
$("#date_start_deb").inputmask("9999-99-99");
$("#date_end_deb").inputmask("9999-99-99");
});
</script>';
$output .= $helperForm->renderScript();
echo $output;
}
private static function copyImg($id_entity, $id_image=NULL, $url, $entity='products') {
$tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
switch($entity) {
default:
case 'products':
$imageObj = new Image($id_image);
$path = $imageObj->getPathForCreation();
break;
case 'categories':
$path = _PS_CAT_IMG_DIR_.(int)($id_entity);
break;
}
if(copy(str_replace(' ', '%20', trim($url)), $tmpfile)) {
imageResize($tmpfile, $path.'.jpg');
$imagesTypes = ImageType::getImagesTypes($entity);
foreach($imagesTypes AS $k => $imageType) {
imageResize($tmpfile, $path.'-'.stripslashes($imageType['name']).'.jpg', $imageType['width'], $imageType['height']);
}
if(in_array($imageType['id_image_type'], $watermark_types)) {
Module::hookExec('watermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
} else {
unlink($tmpfile);
return false;
}
unlink($tmpfile);
return true;
}
private static function createMultiLangField($field) {
$languages = Language::getLanguages(FALSE);
$res = array();
foreach($languages AS $lang) {
$res[$lang['id_lang']] = $field;
}
return $res;
}
private function _recurse_array($array) {
$result = array();
foreach($array as $i) {
$result[] = $i['id'];
if(count($i['children']) > 0) {
$result = array_merge($result, $this->_recurse_array($i['children']));
}
}
return $result;
}
}