497 lines
18 KiB
PHP
497 lines
18 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_delay/saledelay.php');
|
|
require_once('../modules/privatesales_extrafields/privatesales_extrafields.php');
|
|
|
|
if (isset($_GET['flush_sale'])){
|
|
$replies = CacheRedis::getInstance()->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 "<thead><tr>
|
|
<th>ID</th>
|
|
<th>Cat.</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 align='center'>Delay</th>
|
|
<th>Expe</th>
|
|
<th>Position</th>
|
|
<th>Actions</th>
|
|
</tr></thead>
|
|
<tbody id='saleActive' class='sortable'>";
|
|
|
|
$export = true;
|
|
foreach($sales as $key => $sale) {
|
|
$delay = SaleDelay::getDelaySmallName($sale->delivery_delay,2);
|
|
echo '<tr id="item_'.$sale->id. '" class="">
|
|
<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; font-size:11px;">'.$extrafields[$sale->id][1].'</span></td>
|
|
<td style="font-size:11px;">'.$sale->date_start.'</td>
|
|
<td style="font-size:11px;">'.$sale->date_end.'</td>
|
|
<td align="center">'.($sale->enabled? '<span class="anticon anticon-checkmark text-green-light"></span>':'<span class="anticon anticon-cross text-rose"></span>').'</td>';
|
|
|
|
if($sale->new == 0){
|
|
echo "<td align='center'>Non</td>";
|
|
}
|
|
else if($sale->new == 1){
|
|
echo "<td align='center'>Oui</td>";
|
|
}
|
|
else if($sale->new == 3){
|
|
echo "<td align='center'>Non défini</td>";
|
|
}
|
|
|
|
echo '<td>'.((int) $sale->id_employee != 0? $employees[(int) $sale->id_employee]: '--').'</td>
|
|
<td class="text-center '.(in_array('fr', $sale->versions) ? 'green' : 'rose').'">FR</td>
|
|
<td class="text-center '.(in_array('es', $sale->versions) ? 'green' : 'rose').'">ES</td>
|
|
<td align="center">'.$delay.'</td>
|
|
<td align="center">'.(isset($sale_shipping[(int) $sale->id])? ($sale_shipping[(int) $sale->id] == 1? 'Philéa': ($sale_shipping[(int) $sale->id] == 2? 'Drop' : '')): '').'</td>
|
|
<td align="center" class="position">';
|
|
if($sale->lock_position == 1){
|
|
echo '<span class="anticon anticon-lock"></span>';
|
|
} else {
|
|
if($key != 0){
|
|
echo '<a href="#" onclick="updatePositionVP(0,'. $sale->position .'); return false;" 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" onclick="updatePositionVP(1,'. $sale->position .'); return false;" 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);
|
|
$category_link_bo = '?tab=AdminCatalog&viewcategory&id_category='.$sale->id_category.'&token='.Tools::getAdminToken('AdminCatalog'.(int)(Tab::getIdFromClassName('AdminCatalog')).(int)($cookie->id_employee));
|
|
echo '</td>
|
|
<td>
|
|
<div class="input-group-btn" role="group">
|
|
<button class="btn btn-default" onclick="itemEdition('.$sale->id.')" title="Editer la vente">
|
|
<span style="font-size:12px;" class="anticon anticon-pencil2"></span>
|
|
</button>
|
|
<a href="'.$category_link.'" target="_blank" class="btn btn-default" title="Voir la vente sur le site">
|
|
<span style="font-size:12px;" class="anticon anticon-eye"></span>
|
|
</a>
|
|
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
|
<span style="font-size:12px;" class="anticon anticon-menu"></span>
|
|
</button>
|
|
|
|
<ul class="dropdown-menu dropdown-menu-right">
|
|
'.($export? '
|
|
<li>
|
|
<a onclick="window.open(this.href); return false;" href="'.__PS_BASE_URI__.'modules/exports/exports/privatesales.php?id_sale='.$sale->id.'&adtoken='.Tools::encrypt('PrivateSalesDirectExtract'.$sale->id).'">
|
|
<i class="glyphicon glyphicon-new-window"></i> Exporter
|
|
</a>
|
|
</li>':'').'
|
|
<li>
|
|
<a onclick="window.open(this.href); return false;" href="'.$category_link_bo.'">
|
|
<i class="glyphicon glyphicon-eye-open"></i> Voir Catégorie BO
|
|
</a>
|
|
</li>
|
|
<li>
|
|
<a style="cursor: pointer;" onclick="'.($sale->lock_position?'itemUnLockPosition('.$sale->id.')':'itemLockPosition('.$sale->id.')').'">
|
|
<span class="anticon anticon-lock"></span> '.($sale->lock_position?'Défixer':'Fixer').' position
|
|
</a>
|
|
</li>
|
|
<li role="separator" class="divider"></li>
|
|
<li>
|
|
<a style="cursor: pointer;" onclick="itemDeletion('.$sale->id.')">
|
|
<span class="anticon anticon-bin"></span> Supprimer
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
';
|
|
// '.($export? '<a onclick="window.open(this.href); return false;" href="'.__PS_BASE_URI__.'modules/exports/exports/privatesales.php?id_sale='.$sale->id.'&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>
|
|
}
|
|
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);
|
|
|
|
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 .= '
|
|
<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;
|
|
}
|
|
|
|
}
|