bebeboutik/modules/philea_magistor/ajax.php
Michael RICOIS a762200fc2 Typo
2018-01-16 14:38:21 +01:00

228 lines
9.8 KiB
PHP

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
if(Tools::getValue('action') == 'getReport') {
header('Content-Type: application/json');
switch (Tools::getValue('type')) {
case 'ART':
die(getReport('ART'));
break;
case 'CDC':
die(getReport('CDC'));
break;
case 'CRP':
die(getReport('CRP'));
break;
default:
http_response_code(418);
die(json_encode('I\'m a teapot'));
break;
}
} elseif(Tools::getValue('action') == 'destock') {
getStepDestock();
} else {
http_response_code(418);
die('I\'m a teapot');
}
function getStepDestock(){
$step = $_REQUEST["step_number"];
$html = '';
if($step == 1) {
$html.='<h2 class="StepTitle">Choisir La nouvelle vente</h2>
<div class="form-group col-sm-4 col-sm-offset-4">
<select class="form-control" name="id_category" id="wizard_id_category" tabindex="-1">';
foreach(Db::getInstance()->ExecuteS('
SELECT p.`id_sale`, c.`name`, c.`id_category`
FROM `'._DB_PREFIX_.'philea_sync` p
Left JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = p.`id_sale`)
LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON (c.`id_category` = ps.`id_category`)
WHERE c.`id_lang` = 2
ORDER BY p.`id_sale` DESC
') as $row) {
$extrafields = Category::getSalesInfos(array((int) $row['id_category']));
$html .= '<option data-id_sale="'.(int) $row['id_sale'].'" id="id_category_'.(int)$row['id_category'].'" value="'.(int)$row['id_category'].'">'.(int) $row['id_sale'].' (#'.(int) $row['id_category'].') - '.$row['name'].(empty($extrafields[(int) $row['id_category']]['sales'][1])?'':' - '.$extrafields[(int) $row['id_category']]['sales'][1]).'</option>';
}
$html .='</select>
</div>
<div class="clearfix"></div>
<div class="form-group col-sm-4 col-sm-offset-4">
<p class="text-center">Les produits doivent déjà se trouver dans la nouvelle catégorie (vente).</p>
</div>
<script>
$(document).ready(function() {
$("#wizard_id_category").select2({
placeholder: "",
allowClear: true,
width: \'100%\'
});
});
</script>';
} elseif($step == 2) {
$html .= '<h2 class="StepTitle">Les produits de la catégorie</h2>';
if(Tools::getValue('id_category')) {
$id_category = Tools::getValue('id_category');
$id_sale = Tools::getValue('id_sale');
// Getting 3 level of category
$categories = Category::getChildren($id_category, 2);
$id_categories = array((int)$id_category);
foreach ($categories as $category) {
$id_categories[] = (int)$category['id_category'];
if($subcats = Category::getChildren((int)$category['id_category'], 2)) {
foreach ($subcats as $subcat) {
$id_categories[] = (int)$subcat['id_category'];
}
}
}
$product_ids = array();
$sale_ids = array();
$products = Db::getInstance()->ExecuteS('
SELECT p.`id_product`, ps.`id_sale`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_ps_cache` ps ON (p.`id_product` = ps.`id_product`)
WHERE p.`id_category_default` IN ('.implode(',', $id_categories).')
');
foreach($products as $row) {
$product_ids[(int)$row['id_product']] = (int)$row['id_product'];
$sale_ids[(int)$row['id_sale']] = (int)$row['id_sale'];
}
if(count($product_ids)>0) {
$html .= '<div>
<p class="text-center">Produits Concernés ('.count($product_ids).') :</p>
<div class="col-sm-4 col-sm-offset-4">
<table class="table">
<thead>
<tr>
<th align="center">ID Produit</th>
<th align="center">ID Vente (old)</th>
</tr>
</thead>
<tbody>';
foreach ($products as $product) {
$html .= '<tr>
<td align="center">'.$product['id_product'].'</td>
<td align="center">'.$product['id_sale'].'</td>
</tr>';
}
$html .= '</tbody>
</table>
<p class="text-center">Cliquer sur suivant pour réassocier les produits à la nouvelle vente</p>
</div>
</div>
<input type="hidden" id="wizard_product_ids" value="'.implode(',',$product_ids).'"/>
<input type="hidden" id="wizard_id_sale" value="'.$id_sale.'"/>
<input type="hidden" id="wizard_id_category" value="'.$id_category.'"/>';
} else {
$html .= '<div><p class="text-center">Pas de produits trouvés dans cette catégorie</p></div>';
}
} else {
$html .= '<div><p class="text-center">Vous devez choisir une catégorie</p></div>';
}
} elseif($step == 3) {
if(Tools::getValue('id_products')) {
$products_ids = Tools::getValue('id_products');
$id_category = Tools::getValue('id_category');
$id_sale = Tools::getValue('id_sale');
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_product` IN ('.$products_ids.')
');
Db::getInstance()->ExecuteS('
INSERT IGNORE INTO `'._DB_PREFIX_.'product_ps_cache` (
SELECT p.id_product, IFNULL(
(
SELECT s.id_sale
FROM `'._DB_PREFIX_.'privatesale_category` s
WHERE s.`id_category` = p.`id_category_default`
LIMIT 1)
, 0
)
FROM `ps_product` p
WHERE p.`id_category_default` = '.(int)$id_category.'
)
');
$html .= '<h2 class="StepTitle">Réassociation Réussie</h2>
<div class="col-sm-6 col-sm-offset-3">
<p class="text-center">Vous devez maintenant activer la vente pour que la synchronisation vers Philéa se fasse.</p>
<p class="text-center">(Pensez bien à vérifier vos stocks avec Philéa, la synchronisation ne s\'en charge pas)</p>
</div>
<input type="hidden" id="wizard_id_sale" value="'.$id_sale.'"/>';
} else {
$html .= '<div><p class="text-center">Pas de produits à associer</p></div>';
}
} elseif($step == 4) {
$id_sale = Tools::getValue('id_sale');
if($id_sale) {
$sql = '
UPDATE `'._DB_PREFIX_.'philea_sync` SET
`status` = 2,
`products` = 2
WHERE `id_sale` = '.(int)$id_sale;
if(Db::getInstance()->execute($sql)){
$sql = '
INSERT INTO `'._DB_PREFIX_.'philea_auto_sync`
VALUES (
'.(int) $id_sale.',
1,
"0000-00-00 00:00:00"
)
ON DUPLICATE KEY
UPDATE `active` = 1';
if(Db::getInstance()->execute($sql)){
$html .= '<h2 class="StepTitle">Activation de la synchronisation Philéa</h2>
<div class="col-sm-4 col-sm-offset-4 alert-custom alert-ok">
<i class="glyphicon glyphicon-ok"></i> <span class="text-alert">La vente est activée et automatiquement synchronisée</span>
</div>';
} else {
$html .= '<div><p class="text-center">La synchronisation auto n\'a pas pu se faire (faites appel à votre maintenance)</p></div>';
}
} else {
$html .= '<div><p class="text-center">L\'activation n\'a pas pu se faire (faites appel à votre maintenance)</p></div>';
}
} else {
$html .= '<div><p class="text-center">Une erreur est survenue</p></div>';
}
}
echo $html;
}
function getReport($type) {
//global $cookie;
$id_sale = (int)Tools::getValue('id_sale', false);
if (!$id_sale) {
http_response_code(500);
return json_encode(array(
'error'=> 'true',
'message'=> Tools::displayError('Vente non trouvée')
));
}
$report = Db::getInstance()->getRow('
SELECT `report_data`, `report_date`, `id_sale`
FROM `'._DB_PREFIX_.'philea_syncreport`
WHERE `id_sale` = '.$id_sale.'
AND `filename` LIKE "'.$type.'%"
');
if(empty($report['report_data'])){
$report['report_data'] = Tools::displayError('Pas encore de rapport '.$type.' disponible');
}
if(isset($report) && $report != false) {
return json_encode(array(
'error' => 'false',
'report' => $report
));
} else {
return json_encode(array(
'error'=> 'true',
'message' => Tools::displayError('Pas encore de rapport '.$type.' disponible')
));
}
}