316 lines
13 KiB
PHP
316 lines
13 KiB
PHP
<?php
|
|
|
|
require_once(dirname(__FILE__).'../../../config/config.inc.php');
|
|
require_once(dirname(__FILE__).'../../../init.php');
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
switch (Tools::getValue('action')) {
|
|
case 'getCategories':
|
|
die(json_encode(getCategories()));
|
|
break;
|
|
|
|
case 'getProductId':
|
|
die(json_encode(getProductId()));
|
|
break;
|
|
|
|
case 'addToSellout':
|
|
die(json_encode(addToSellout()));
|
|
break;
|
|
|
|
default:
|
|
http_response_code(418);
|
|
die('I\'m a teapot');
|
|
break;
|
|
}
|
|
|
|
function getCategories()
|
|
{
|
|
global $cookie;
|
|
$id_sale = (int)Tools::getValue('sale', false);
|
|
|
|
if (!$id_sale) {
|
|
http_response_code(500);
|
|
return Tools::displayError('Catégorie invalide');
|
|
}
|
|
|
|
return Db::getInstance()->ExecuteS('
|
|
SELECT *
|
|
FROM `'._DB_PREFIX_.'category` c
|
|
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`
|
|
WHERE c.`id_parent` = '.$id_sale.'
|
|
AND cl.`id_lang` = '.(int)$cookie->id_lang
|
|
);
|
|
}
|
|
|
|
function getProductId()
|
|
{
|
|
global $cookie;
|
|
$ean = (float)Tools::getValue('ean', false);
|
|
|
|
if (!$ean) {
|
|
http_response_code(500);
|
|
return Tools::displayError('Code EAN invalide');
|
|
}
|
|
|
|
if (!is_float($ean)) {
|
|
http_response_code(500);
|
|
return Tools::displayError('Code EAN invalide');
|
|
}
|
|
|
|
$result = Db::getInstance()->getRow('
|
|
SELECT p.`id_product`, p.`reference`, pl.`name`, pl.`description_comment` as comments, i.`id_image`
|
|
FROM `'._DB_PREFIX_.'product` p
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
|
|
WHERE p.`ean13` = '.pSQL($ean).'
|
|
AND pl.`id_lang` = '.(int)$cookie->id_lang.'
|
|
ORDER BY `date_add` DESC
|
|
-- ORDER BY `date_add` ASC
|
|
');
|
|
|
|
if (empty($result)) {
|
|
$result = Db::getInstance()->getRow('
|
|
SELECT pa.*, pl.`name`, pl.`description_comment` as comments, p.`reference` as product_reference, al.`name` as attribute_name, ia.`id_image` as id_image_attribute, i.`id_image`
|
|
FROM `'._DB_PREFIX_.'product_attribute` pa
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = pa.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`)
|
|
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (al.`id_attribute` = pac.`id_attribute`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute_image` ia ON (ia.`id_product_attribute` = pa.`id_product_attribute`)
|
|
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
|
|
WHERE pa.`ean13` = '.pSQL($ean).'
|
|
AND pl.`id_lang` = '.(int)$cookie->id_lang.'
|
|
ORDER BY pa.`id_product_attribute` DESC
|
|
-- ORDER BY pa.`id_product_attribute` ASC'
|
|
);
|
|
$result['reference'] = (!empty($result['reference'])) ? $result['reference'] : $result['product_reference'];
|
|
$result['id_image'] = (!empty($result['id_image_attribute'])) ? $result['id_image_attribute'] : $result['id_image'];
|
|
}
|
|
|
|
/*$category_name = Db::getInstance()->getRow('
|
|
SELECT cl.`name`
|
|
FROM `'._DB_PREFIX_.'category_lang` cl
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_category` = cl.`id_category`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` pps ON (pps.`id_sale` = ps.`id_sale`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = pps.`id_product`)
|
|
WHERE p.`ean13` = '.pSQL($ean).'
|
|
AND cl.`id_lang` = '.(int)$cookie->id_lang.'
|
|
AND ps.`braderie`= 0
|
|
ORDER BY p.`date_add` ASC'
|
|
);
|
|
if(!isset($category_name) || empty($category_name)) {
|
|
$category_name = Db::getInstance()->getRow('
|
|
SELECT cl.`name`
|
|
FROM `'._DB_PREFIX_.'category_lang` cl
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_category` = cl.`id_category`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` pps ON (pps.`id_sale` = ps.`id_sale`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product` = pps.`id_product`)
|
|
WHERE pa.`ean13` = '.pSQL($ean).' AND cl.`id_lang` = '.(int)$cookie->id_lang.'
|
|
AND ps.`braderie`= 0
|
|
ORDER BY pa.`id_product_attribute` ASC'
|
|
);
|
|
}
|
|
$result['category_name'] = $category_name['name'];
|
|
|
|
$product_name = Db::getInstance()->getRow('
|
|
SELECT pl.`name`
|
|
FROM `'._DB_PREFIX_.'product` p
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
|
|
WHERE p.`ean13` = '.pSQL($ean).'
|
|
AND pl.`id_lang` = '.(int)$cookie->id_lang.'
|
|
ORDER BY p.`date_add` ASC
|
|
');
|
|
|
|
if(!isset($product_name) || empty($product_name)) {
|
|
$product_name = Db::getInstance()->getRow('
|
|
SELECT pl.`name`
|
|
FROM `'._DB_PREFIX_.'product_attribute` pa
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = pa.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`)
|
|
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (al.`id_attribute` = pac.`id_attribute`)
|
|
WHERE pa.`ean13` = '.pSQL($ean).'
|
|
AND pl.`id_lang` = '.(int)$cookie->id_lang.'
|
|
ORDER BY p.`date_add` ASC
|
|
');
|
|
}
|
|
$result['product_name'] = $product_name['name'];*/
|
|
|
|
if (!empty($result)) {
|
|
if(isset($result['id_image'])){
|
|
$image_obj = new Image((int)$result['id_image']);
|
|
$result['img_path'] = (isset($image_obj)?$image_obj->getExistingImgPath():'');
|
|
} else {
|
|
$result['img_path'] = null;
|
|
}
|
|
return $result;
|
|
} else {
|
|
http_response_code(500);
|
|
return Tools::displayError('Aucun produit trouvé');
|
|
}
|
|
}
|
|
|
|
function addToSellout()
|
|
{
|
|
$parent_category = (int)Tools::getValue('parent_category', false);
|
|
if (!$parent_category || !is_int($parent_category)) {
|
|
http_response_code(500);
|
|
return Toold::displayError('La catégorie principale n\'est pas valide');
|
|
}
|
|
|
|
$category = (int)Tools::getValue('category', false);
|
|
if (!$category || !is_int($category)) {
|
|
http_response_code(500);
|
|
return Toold::displayError('La catégorie n\'est pas valide');
|
|
}
|
|
|
|
$product_id = (int)Tools::getValue('product', false);
|
|
if (!$product_id || !is_int($product_id)) {
|
|
http_response_code(500);
|
|
return Tools::displayError('Le produit n\'est pas valide');
|
|
}
|
|
|
|
$quantity = (int)Tools::getValue('quantity', false);
|
|
if (!$quantity || !is_int($quantity)) {
|
|
http_response_code(500);
|
|
return Tools::displayError('La quantitée n\'est pas valide');
|
|
}
|
|
|
|
$storage = Tools::getValue('storage');
|
|
if (empty($storage)) {
|
|
http_response_code(500);
|
|
return Tools::displayError('L\'emplacement n\'est pas valide');
|
|
}
|
|
|
|
$comment = Tools::getValue('comment', false);
|
|
|
|
//$category_name = Tools::getValue('category_name', false);
|
|
//$product_name = Tools::getValue('product_name', false);
|
|
$ean13 = Tools::getValue('ean', false);
|
|
$id_attribute = (int)Tools::getValue('attribute', false);
|
|
|
|
// duplication produit classique
|
|
$product = new Product($product_id);
|
|
$id_product_old = $product->id;
|
|
unset($product->id);
|
|
unset($product->id_product);
|
|
$product->active = 1;
|
|
$product->quantity = $quantity;
|
|
$product->reference = $storage.'-'.$product->supplier_reference;
|
|
|
|
$languages = Language::getLanguages(false);
|
|
foreach ($languages as $key => $language) {
|
|
|
|
// récupération de la catégorie originale (nom de la première catégorie)
|
|
$category_name = Db::getInstance()->getRow('
|
|
SELECT cl.`name`
|
|
FROM `'._DB_PREFIX_.'category_lang` cl
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_category` = cl.`id_category`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` pps ON (pps.`id_sale` = ps.`id_sale`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = pps.`id_product`)
|
|
WHERE p.`ean13` = '.pSQL($ean13).'
|
|
AND cl.`id_lang` = '.(int)$language['id_lang'].'
|
|
AND ps.`braderie`= 0
|
|
ORDER BY p.`date_add` ASC'
|
|
);
|
|
if(!isset($category_name) || empty($category_name)) {
|
|
$category_name = Db::getInstance()->getRow('
|
|
SELECT cl.`name`
|
|
FROM `'._DB_PREFIX_.'category_lang` cl
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_category` = cl.`id_category`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` pps ON (pps.`id_sale` = ps.`id_sale`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product` = pps.`id_product`)
|
|
WHERE pa.`ean13` = '.pSQL($ean13).' AND cl.`id_lang` = '.(int)$language['id_lang'].'
|
|
AND ps.`braderie`= 0
|
|
ORDER BY pa.`id_product_attribute` ASC'
|
|
);
|
|
}
|
|
|
|
if ($ean13) {
|
|
// récupération du nom original (nom du premier produit ajouté)
|
|
$product_name = Db::getInstance()->getRow('
|
|
SELECT pl.`name`
|
|
FROM `'._DB_PREFIX_.'product` p
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
|
|
WHERE p.`ean13` = '.pSQL($ean13).'
|
|
AND pl.`id_lang` = '.(int)$language['id_lang'].'
|
|
ORDER BY p.`date_add` ASC
|
|
');
|
|
|
|
if(!isset($product_name) || empty($product_name)) {
|
|
$product_name = Db::getInstance()->getRow('
|
|
SELECT pl.`name`
|
|
FROM `'._DB_PREFIX_.'product_attribute` pa
|
|
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = pa.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`)
|
|
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`)
|
|
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (al.`id_attribute` = pac.`id_attribute`)
|
|
WHERE pa.`ean13` = '.pSQL($ean13).'
|
|
AND pl.`id_lang` = '.(int)$language['id_lang'].'
|
|
ORDER BY p.`date_add` ASC
|
|
');
|
|
}
|
|
}
|
|
|
|
$name = (isset($product_name) && !empty($product_name))? $product_name['name'] : $product->name[(int)$language['id_lang']];
|
|
$product->name[(int)$language['id_lang']] = $name.' - '.strtoupper($category_name['name']);
|
|
}
|
|
|
|
$product->id_category_default = (int)$category;
|
|
if ($comment) {
|
|
$product->description_comment[2] = $comment;
|
|
}
|
|
|
|
if ($product->add()) {
|
|
if (!$id_attribute) {
|
|
$combinationImages = Product::duplicateAttributes($id_product_old, $product->id, $quantity);
|
|
} else {
|
|
// duplication spécific attribute
|
|
$combinationImages = Product::duplicateOneAttribute($id_product_old, $product->id, (int) $id_attribute, $quantity);
|
|
}
|
|
|
|
Product::duplicateSpecificPrices($id_product_old, $product->id);
|
|
Product::duplicateFeatures($id_product_old, $product->id);
|
|
Product::duplicateAccessories($id_product_old, $product->id);
|
|
GroupReduction::duplicateReduction($id_product_old, $product->id);
|
|
|
|
$product->UpdateCategories(array((int)$category,(int)$parent_category));
|
|
|
|
if ($product->hasAttributes())
|
|
Product::updateDefaultAttribute($product->id);
|
|
|
|
Image::duplicateProductImages($id_product_old, $product->id, $combinationImages);
|
|
}
|
|
return Tools::displayError('Le produit a été ajouté à la braderie');
|
|
}
|
|
|
|
function pSQLArray($data)
|
|
{
|
|
foreach ($data as $key => $value) {
|
|
if (is_array($value)) {
|
|
$this->pSQLArray($value);
|
|
} else {
|
|
$data[$key] = pSQL($value);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
function recurse_copy($src, $dst) {
|
|
if (is_dir($src) && is_dir($dst)) {
|
|
$dir = opendir($src);
|
|
mkdir($dst);
|
|
while(false !== ( $file = readdir($dir)) ) {
|
|
if (( $file != '.' ) && ( $file != '..' )) {
|
|
if ( is_dir($src . '/' . $file) ) {
|
|
recurse_copy($src . '/' . $file, $dst . '/' . $file);
|
|
}
|
|
else {
|
|
copy($src . '/' . $file, $dst . '/' . $file);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|