clear('sale*');
die();
}
if (isset($_GET['getSales'])){
global $cookie;
$type = $_GET['type'];
if($type == 'finished'){
$sales = Sale::getSalesBack(NULL, NULL, NULL, FALSE , FALSE, FALSE, '`position` DESC', NULL, NULL, FALSE, FALSE);
}else{
$sales = Sale::getSalesBack(NULL, NULL, NULL, $type , FALSE, FALSE, '`position` DESC', NULL, NULL, FALSE, FALSE);
}
$ids = array();
foreach ($sales as $key => $sale) {
$ids[] = $sale->id;
}
$extrafields = PrivateSales_ExtraFields::getFieldsForSale($ids);
$employees = array();
foreach(Db::getInstance()->ExecuteS('
SELECT `id_employee`, `firstname`, `lastname`
FROM `'._DB_PREFIX_.'employee`
') as $row) {
$employees[(int) $row['id_employee']] = $row['firstname'].' '.$row['lastname'];
}
$sale_shipping = array();
foreach(Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'privatesale_shipping_sale`
WHERE 1
') as $ss) {
$sale_shipping[$ss['id_sale']] = $ss['id_shipping'];
}
echo "
ID |
Cat. |
Titre |
Début |
Fin |
Activée |
Nouveau |
Commercial |
FR |
ES |
Delay |
Expe |
Position |
Actions |
";
$export = true;
foreach($sales as $key => $sale) {
$delay = SaleDelay::getDelaySmallName($sale->delivery_delay,2);
echo '
'.$sale->id.' |
'.$sale->id_category.' |
'.$sale->title[intval($cookie->id_lang)].' '.$extrafields[$sale->id][1].' |
'.$sale->date_start.' |
'.$sale->date_end.' |
'.($sale->enabled? '':'').' | ';
if($sale->new == 0){
echo "Non | ";
}
else if($sale->new == 1){
echo "Oui | ";
}
else if($sale->new == 3){
echo "Non défini | ";
}
echo ''.((int) $sale->id_employee != 0? $employees[(int) $sale->id_employee]: '--').' |
FR |
ES |
'.$delay.' |
'.(isset($sale_shipping[(int) $sale->id])? ($sale_shipping[(int) $sale->id] == 1? 'Philéa': ($sale_shipping[(int) $sale->id] == 2? 'Drop' : '')): '').' |
';
if($sale->lock_position == 1){
echo '';
} else {
if($key != 0){
echo '';
}
if($key + 1 != count($sales)){
echo ' ';
}
}
$category_link = Link::getCategoryLink($sale->id_category);
$category_link_bo = '?tab=AdminCatalog&viewcategory&id_category='.$sale->id_category.'&token='.Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)($cookie->id_employee));
echo ' |
|
';
// '.($export? '': '').'
//
//
//
//
//
}
echo "";
die();
}
function getSaleType($sale_type){
$type = "";
if(in_array(1, $sale_type)){
$type.= "Bébé ";
}
if(in_array(2, $sale_type)){
$type.= "Enfant ";
}
if(in_array(3, $sale_type)){
$type.= "Maman ";
}
if(empty($type)){
return "undefined";
}
return $type;
}
if (isset($_GET['updatePosition'])){
$position = $_GET['position'];
$way = $_GET['way'];
if($way == 0){
$new_position = $position + 1;
}elseif($way == 1){
$new_position = $position - 1;
}
updateSalePosition($position, $new_position);
die();
}
function updateSalePosition($position, $new_position){
$sale = Sale::getByPosition($position);
$last_sale = Sale::getByPosition($new_position);
if($sale->lock_position || $last_sale->lock_position){
return true;
}
// // update lactuelle new position
Db::getInstance()->autoExecute( _DB_PREFIX_.'privatesale', array(
'position' => $position),
'UPDATE',
'id_sale = ' . $last_sale->id);
// // update pour la nouvelle position
Db::getInstance()->autoExecute( _DB_PREFIX_.'privatesale', array(
'position' => $new_position),
'UPDATE',
'id_sale = ' . $sale->id);
return true;
}
if (isset($_GET['updatePositionDrag'])){
$id_sale = str_replace("item_", "", $_GET['id_sale']);
$position = $_GET['position'];
$sql = 'SELECT position, lock_position FROM '._DB_PREFIX_.'privatesale WHERE id_sale =' . $id_sale;
$query = Db::getInstance()->getRow($sql);
if((int)$query['lock_position']==1){
die();
}
$position_initial = (int)$query['position'];
$new_position = $position_initial + $position;
$sql = 'SELECT lock_position FROM '._DB_PREFIX_.'privatesale WHERE position =' . $new_position;
$is_lock = Db::getInstance()->getValue($sql);
if($is_lock==1){
die();
}
$lock_positions = array();
foreach(Db::getInstance()->executeS('SELECT position FROM '._DB_PREFIX_.'privatesale WHERE lock_position = 1') as $lock){
$lock_positions[] = (int)$lock['position'];
}
$_positions = array();
if($position > 0){
$sales_sql = "SELECT id_sale, position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". ($position_initial +1) .' AND ' .$new_position.' AND lock_position = 0 ORDER BY position DESC';
$sales = Db::getInstance()->ExecuteS($sales_sql);
foreach ($sales as $key => $sale){
$pos = (int)$sale['position']-1;
while(in_array($pos, $lock_positions)){
$pos = $pos - 1;
}
$_positions[(int)$sale['id_sale']] = array(
'initial' => (int)$sale['position'],
'new' => $pos,
);
//Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '.$pos.' WHERE id_sale = '. $sale['id_sale']);
}
} else {
$sales_sql = "SELECT id_sale, position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". $new_position . ' AND ' . ($position_initial - 1).' AND lock_position = 0 ORDER BY position DESC';
$sales = Db::getInstance()->ExecuteS($sales_sql);
foreach ($sales as $key => $sale){
$pos = (int)$sale['position']+1;
while(in_array($pos, $lock_positions)){
$pos = $pos + 1;
}
$_positions[(int)$sale['id_sale']] = array(
'initial' => (int)$sale['position'],
'new' => $pos,
);
//Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '.$position.' WHERE id_sale = '. $sale['id_sale']);
}
}
foreach ($_positions as $id => $value) {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '. (int)$value['new'].' WHERE id_sale = '. $id);
}
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '. (int)$new_position.' WHERE id_sale = '. $id_sale);
die();
}
if (isset($_GET['updatePositionImg'])){
$id_image = str_replace("image_", "", $_GET['id_image']);
$position = $_GET['position'];
$sql = '
SELECT
`position`,
`id_product`
FROM '._DB_PREFIX_.'image
WHERE
`id_image` =' . (int)$id_image;
$position_initial = Db::getInstance()->getRow($sql);
$id_product = $position_initial['id_product'];
$new_position = $position_initial['position'] + $position;
// fix bug duplicate entry
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'image`
SET `position` = 0
WHERE `id_image` = '. (int)$id_image
);
if ($position > 0) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'image`
SET `position` = (`position` -1)
WHERE `id_product` = '. (int)$id_product.'
AND `position` BETWEEN '.(int)$position_initial['position'].' AND '.(int)$new_position
);
} else {
// fix bug duplicate entry
foreach (Db::getInstance()->ExecuteS('
SELECT
`id_image`
FROM `'._DB_PREFIX_.'image`
WHERE `id_product` = '. (int)$id_product .'
AND `position` BETWEEN '.(int)$new_position.' AND '.(int)$position_initial['position'].'
ORDER BY position DESC') as $key => $value) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'image`
SET `position` = (`position` + 1)
WHERE `id_image` = '. (int)$value['id_image']
);
}
}
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'image`
SET `position` = '.(int)$new_position.'
WHERE `id_image` = '. (int)$id_image
);
die();
}
if (isset($_GET['getStats'])) {
$id_sale = Tools::getValue('id_sale');
if(Validate::isLoadedObject($sale = new Sale($id_sale))) {
$exports = array();
$sold_products = array();
foreach(Db::getInstance()->ExecuteS('
SELECT DISTINCT `product_id`, `product_attribute_id`
FROM `'._DB_PREFIX_.'order_detail`d
INNER JOIN `'._DB_PREFIX_.'orders` o ON d.id_order = o.id_order
WHERE `product_id` IN (
SELECT `id_product`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_sale` = '.(int) $sale->id.'
)
AND o.valid = 1
') as $row) {
$sold_products[] = (int) $row['product_id'].'-'.(int) $row['product_attribute_id'];
}
$detail_sales = array();
foreach(Db::getInstance()->ExecuteS('
SELECT `product_id`, `product_attribute_id`, SUM(`product_quantity`) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail` d
INNER JOIN `'._DB_PREFIX_.'orders` o ON o.id_order = d.id_order
WHERE `product_id` IN (
SELECT `id_product`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_sale` = '.(int) $sale->id.'
)
AND o.valid = 1
GROUP BY `product_id`, `product_attribute_id`
') as $row) {
$detail_sales[(int) $row['product_id'].'-'.(int) $row['product_attribute_id']] = (int) $row['quantity'];
}
// get les produits avec attributs
$query = Db::getInstance()->ExecuteS('
SELECT a.`id_product_attribute`, a.`id_product`, a.`quantity`, p.`reference`, pl.`name`
FROM `'._DB_PREFIX_.'product_attribute` a
LEFT JOIN `'._DB_PREFIX_.'product` p
ON p.`id_product` = a.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON p.`id_product` = pl.`id_product` AND id_lang = '.(int)$cookie->id_lang.'
WHERE a.`id_product` IN (
SELECT `id_product`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_sale` = '.(int) $sale->id.'
)
AND p.`active` = 1
GROUP BY a.`id_product_attribute`
');
$id_attributes = array();
foreach($query as $row) {
if(in_array((int) $row['id_product'].'-'.(int) $row['id_product_attribute'], $sold_products)) {
$stock_actual = $row['quantity'];
$stock_sales = $detail_sales[(int) $row['id_product'].'-'.(int) $row['id_product_attribute']];
$stock_initial = (int)$stock_actual + (int)$stock_sales;
$percent_sale = ((int)$stock_sales * 100) / (int)$stock_initial;
if ($stock_actual == 0
|| $percent_sale > 85) {
$exports[] = [
'id_product' => $row['id_product'] . '-'.(int) $row['id_product_attribute'],
'name' => $row['name'],
'reference' => $row['reference'],
'quantity' => $stock_actual,
'quantity_sale' => $stock_sales,
'percent_sale' => round((100 - $percent_sale), 2),
];
}
}
$id_attributes[] = (int) $row['id_product'];
}
// get les produits sans attributs
$query_whitout_attributes = Db::getInstance()->ExecuteS('
SELECT p.`quantity`, p.`id_product`, pl.`name`, p.`reference`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON p.`id_product` = pl.`id_product` AND id_lang = '.(int)$cookie->id_lang.'
WHERE p.`id_product` IN (
SELECT `id_product`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_sale` = '.(int) $sale->id.'
)
'.((count($id_attributes) > 0)? ' AND p.`id_product` NOT IN ('.implode(', ', $id_attributes).')': '').'
GROUP BY p.`id_product`
');
foreach($query_whitout_attributes as $row) {
if(in_array((int) $row['id_product'].'-0', $sold_products)) {
$stock_actual = $row['quantity'];
$stock_sales = $detail_sales[(int) $row['id_product'].'-0'];
$stock_initial = (int)$stock_actual + (int)$stock_sales;
$percent_sale = ((int)$stock_sales * 100) / (int)$stock_initial;
if ($stock_actual == 0
|| $percent_sale > 85) {
$exports[] = [
'id_product' => $row['id_product'],
'name' => $row['name'],
'reference' => $row['reference'],
'quantity' => $stock_actual,
'quantity_sale' => $stock_sales,
'percent_sale' => round((100 - $percent_sale), 2),
];
}
}
}
$result = '';
if (!empty($exports)) {
$result .= '
Image |
Référence |
Nom du produit |
Quantité vendue |
Quantité restante |
% vendu |
';
foreach ($exports as $key => $product) {
$id_image = Product::getCover($product['id_product']);
$link = new Link();
if ($id_image) {
$href = $link->getImageLink('image', $id_image['id_image'], 'small');
}
$result.= '
|
'.$product['reference'].' |
'.$product['name'].' |
'.$product['quantity_sale'].' |
'.$product['quantity'].' |
'.(($product['percent_sale'] == 0) ? '100' : (100 - $product['percent_sale'])).'% |
';
}
$result.= '
|
';
}
echo $result;
die;
}
}