bebeboutik/adm/ajax_vp.php
2016-10-14 13:10:56 +02:00

396 lines
12 KiB
PHP

<?php
define('PS_ADMIN_DIR', getcwd());
include(PS_ADMIN_DIR.'/../config/config.inc.php');
/* Getting cookie or logout */
require_once(dirname(__FILE__).'/init.php');
require_once('../modules/privatesales/Sale.php');
require_once('../modules/privatesales_extrafields/privatesales_extrafields.php');
if (isset($_GET['getSales'])){
$type = $_GET['type'];
if($type == 'finished'){
$sales = Sale::getSales(NULL, NULL, NULL, FALSE , FALSE, FALSE, '`position` DESC', NULL, NULL, FALSE, FALSE);
}else{
$sales = Sale::getSales(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'];
}
echo "<thead><tr>
<th>ID</th>
<th>Categorie</th>
<th>Titre</th>
<th>Début</th>
<th>Fin</th>
<th>Activée</th>
<th>Nouveau</th>
<th>Commercial</th>
<th>FR</th>
<th>ES</th>
<th>Position</th>
<th style='width: 68px;'>Actions</th>
</tr></thead>
<tbody id='saleActive' class='sortable'>";
$export = true;
foreach($sales as $key => $sale) {
echo '<tr id="item_'.$sale->id. '">
<td>'.$sale->id.'</td>
<td>'.$sale->id_category.'</td>
<td><strong>'.$sale->title[intval($cookie->id_lang)].'</strong><br/><span style="color:#666; font-style:italic">'.$extrafields[$sale->id][1].'</span></td>
<td>'.$sale->date_start.'</td>
<td>'.$sale->date_end.'</td>
<td>'.($sale->enabled? '<img src="../img/admin/module_install.png"/>':'<img src="../img/admin/module_notinstall.png"/>').'</td>';
if($sale->new == 0){
echo "<td>Non</td>";
}
else if($sale->new == 1){
echo "<td>Oui</td>";
}
else if($sale->new == 3){
echo "<td>Non défini</td>";
}
echo '<td>'.((int) $sale->id_employee != 0? $employees[(int) $sale->id_employee]: '--').'</td>
<td '.(in_array('fr', $sale->versions) ? 'class="green"' : 'class="red"').'>FR</td>
<td '.(in_array('es', $sale->versions) ? 'class="green"' : 'class="red"').'>ES</td>
<td class="position">';
if($key != 0){
echo '<a href="" class="updatePosition" data-way="0" data-position='. $sale->position .'><img title="Haut" alt="Haut" src="../img/admin/up.gif"></a>';
}
if($key + 1 != count($sales)){
echo '<a href="" class="updatePosition" data-way="1" data-position='. $sale->position .'><img title="Bas" alt="Bas" src="../img/admin/down.gif"></a>';
}
$category_link = Link::getCategoryLink($sale->id_category);
echo '</td>
<td>
'.($export? '<a onclick="window.open(this.href); return false;" href="'.__PS_BASE_URI__.'modules/exports/exports/privatesales.php?id_sale='.$sale->id.'&amp;adtoken='.Tools::encrypt('PrivateSalesDirectExtract'.$sale->id).'"><img style="cursor: pointer;" title="Exporter cette vente" alt="" src="../img/admin/export.gif"></a>': '').'
<img style="cursor: pointer;" onclick="itemEdition('.$sale->id.')" title="Éditer cette vente" alt="" src="../img/admin/edit.gif">
<img style="cursor: pointer;" onclick="itemDeletion('.$sale->id.')" title="Supprimer cette vente" alt="" src="../img/admin/delete.gif">
<a href="'.$category_link.'" target="_blank">
<img style="cursor: pointer;" title="Voir cette vente" alt="" src="../img/admin/details.gif">
</a>
</td>
</tr>
';
}
echo "</tbody>";
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);
// // 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 FROM '._DB_PREFIX_.'privatesale WHERE id_sale =' . $id_sale;
$position_initial = Db::getInstance()->getValue($sql);
$new_position = $position_initial + $position;
if($position > 0){
$sales_sql = "SELECT id_sale, position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". ($position_initial +1) .' AND ' .$new_position;
$sales = Db::getInstance()->ExecuteS($sales_sql);
foreach ($sales as $key => $sale){
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = (position -1) 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);
$sales = Db::getInstance()->ExecuteS($sales_sql);
foreach ($sales as $key => $sale){
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = (position + 1) WHERE id_sale = '. $sale['id_sale']);
}
}
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 .= '
<tr class="details_sale_'.(int)$sale->id.'">
<td colspan="15">
<table style="width:100%; font-size: 13px;">
<tr>
<th>Image</th>
<th>Référence</th>
<th>Nom du produit</th>
<th>Quantité vendue</th>
<th>Quantité restante</th>
<th>% vendu</th>
</tr>';
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.= '
<tr>
<td><img src="'.$href.'" /></td>
<td>'.$product['reference'].'</td>
<td>'.$product['name'].'</td>
<td>'.$product['quantity_sale'].'</td>
<td>'.$product['quantity'].'</td>
<td>'.(($product['percent_sale'] == 0) ? '100' : (100 - $product['percent_sale'])).'%</td>
</tr>
';
}
$result.= '</table>
</td>
</tr>
';
}
echo $result;
die;
}
}