id_lang; return $db->ExecuteS($sql); } 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'); } $db = Db::getInstance(); $sql = 'SELECT `'._DB_PREFIX_.'product_lang`.`id_product`, `'._DB_PREFIX_.'product_lang`.`name` FROM `'._DB_PREFIX_.'product` LEFT JOIN `'._DB_PREFIX_.'product_lang` ON `'._DB_PREFIX_.'product`.id_product = `'._DB_PREFIX_.'product_lang`.id_product WHERE `ean13` = '.$ean.' AND `id_lang` = '.(int)$cookie->id_lang.' ORDER BY `date_add` DESC LIMIT 1'; $result = $db->ExecuteS($sql); if (count($result) > 0) { return $result[0]; } else { http_response_code(500); return Tools::displayError('Aucun produit trouvé'); } } function addToSellout() { $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'); } $db = Db::getInstance(); // vérifie si le produit n'es pas déjçà dans la vente $sql = 'SELECT * FROM `'._DB_PREFIX_.'category_product` WHERE `id_product` = '.pSQL($product_id).' AND `id_category` = '.pSQL($category).' LIMIT 1'; //var_dump($sql); $product = $db->ExecuteS($sql); if (!empty($product)) { http_response_code(500); return Tools::displayError('Le produit existe déjà dans la vente'); } // duplication produit $sql = 'SELECT * FROM `'._DB_PREFIX_.'product` WHERE `'._DB_PREFIX_.'product`.`id_product` = '.pSQL($product_id).' ORDER BY `date_add` DESC LIMIT 1'; $product = $db->ExecuteS($sql)[0]; unset($product['id_product']); $product['quantity'] = $quantity; $product['active'] = 1; $product['reference'] = $storage.'_'.$product['reference']; $r = $db->autoExecute('ps_product', pSQLArray($product), INSERT); if (!$r) { http_response_code(500); return Tools::displayError('Une erreur s\'est produite'); } $last_product_id = $db->Insert_ID(); // duplication categorie $r = $db->autoExecute(_DB_PREFIX_.'category_product', [ 'id_category' => pSQL($category), 'id_product' => pSQL($last_product_id), 'position' => 0 ], INSERT); if (!$r) { http_response_code(500); return Tools::displayError('Une erreur s\'est produite'); } // duplication lang $sql = 'SELECT * FROM `'._DB_PREFIX_.'product_lang` WHERE `'._DB_PREFIX_.'product_lang`.`id_product` = '.$product_id; $products_lang = $db->ExecuteS($sql); foreach ($products_lang as $key => $p) { $p['id_product'] = $last_product_id; $r = $db->autoExecute(_DB_PREFIX_.'product_lang', pSQLArray($p), INSERT); if (!$r) { http_response_code(500); return Tools::displayError('Une erreur s\'est produite'); } } Product::duplicateAttributes($product_id, $last_product_id); recurse_copy(_PS_IMG_DIR_.$product_id, _PS_IMG_DIR_.$last_product_id); return Tools::displayError('Le produit à été mis dans 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); } } } } }