You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
8.1 KiB
201 lines
8.1 KiB
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\AdminController;
|
|
use App\Models\Notification;
|
|
use App\Models\Orders;
|
|
use App\Models\Remise;
|
|
use App\Models\Stores;
|
|
|
|
class RemiseController extends AdminController
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
private $pageTitle = 'Remise';
|
|
|
|
public function index()
|
|
{
|
|
if (!in_array('viewRemise', $this->permission) && !in_array('validateRemise', $this->permission)) {
|
|
redirect()->to('/')->send();
|
|
exit();
|
|
}
|
|
|
|
$data = json_decode($this->fetchTotal(),true);
|
|
|
|
$this->render_template('demande/index', $data);
|
|
}
|
|
|
|
public function fetchTotal(){
|
|
$data = [
|
|
'user_permission' => $this->permission,
|
|
'page_title' => $this->pageTitle
|
|
];
|
|
return json_encode($data);
|
|
}
|
|
|
|
public function fetchRemiseData()
|
|
{
|
|
helper(['url', 'form']);
|
|
$Remise = new Remise();
|
|
|
|
$draw = intval($this->request->getVar('draw'));
|
|
$data = $Remise->getAllDemandeRemiseToday();
|
|
$totalRecords = count($data);
|
|
|
|
$result = [
|
|
"draw" => $draw,
|
|
"recordsTotal" => $totalRecords,
|
|
"recordsFiltered" => $totalRecords,
|
|
"data" => []
|
|
];
|
|
|
|
// ✅ RÉCUPÉRER LE RÔLE DE L'UTILISATEUR
|
|
$session = session();
|
|
$user = $session->get('user');
|
|
$role = $user['group_name'] ?? '';
|
|
|
|
foreach ($data as $key => $value) {
|
|
$buttons = '';
|
|
|
|
// ✅ TOUS LES GROUPES AYANT validateRemise PEUVENT VALIDER/REFUSER
|
|
if (in_array('validateRemise', $this->permission) && $value['demande_status'] == 'En attente') {
|
|
$buttons .= '<button type="submit" class="btn btn-success" onclick="valideFunc(' . $value['id_demande'] . ')">';
|
|
$buttons .= '<i class="fa fa-check-circle"></i>';
|
|
$buttons .= '</button>';
|
|
|
|
$buttons .= ' <button type="button" class="btn btn-danger" onclick="refuseFunc(' . $value['id_demande'] . ')">';
|
|
$buttons .= '<i class="fa fa-times-circle"></i>';
|
|
$buttons .= '</button>';
|
|
}
|
|
|
|
$result['data'][$key] = [
|
|
$value['id_demande'],
|
|
$value['product'],
|
|
number_format($value['total_price'], 0, '.', ' '),
|
|
number_format($value['montant_demande'], 0, '.', ' '),
|
|
$value['demande_status'],
|
|
$buttons
|
|
];
|
|
}
|
|
|
|
return $this->response->setJSON($result);
|
|
}
|
|
|
|
/**
|
|
* ✅ AMÉLIORATION : Notifications centralisées lors de la validation/refus des remises
|
|
*/
|
|
public function updateRemise($id_demande)
|
|
{
|
|
$this->verifyRole('validateRemise');
|
|
|
|
$session = session();
|
|
$user = $session->get('user');
|
|
|
|
$validation = \Config\Services::validation();
|
|
$data['page_title'] = $this->pageTitle;
|
|
|
|
$validation->setRules([
|
|
'demande_status' => 'required'
|
|
]);
|
|
|
|
$validationData = [
|
|
'demande_status' => $this->request->getPost('demande_status')
|
|
];
|
|
|
|
$Remise = new Remise();
|
|
|
|
if (strtolower($this->request->getMethod()) === 'post') {
|
|
$today = date('Y-m-d');
|
|
$demande_status = $this->request->getPost('demande_status');
|
|
|
|
$data = [
|
|
'demande_status' => $demande_status,
|
|
'date_demande' => $today,
|
|
];
|
|
|
|
if ($Remise->updateRemise($id_demande, $data)) {
|
|
$Notification = new NotificationController();
|
|
$ordersModel = new Orders();
|
|
$order_id = $Remise->getOrderIdByDemandeId($id_demande);
|
|
|
|
// Récupérer les infos de la commande
|
|
$order_info = $ordersModel->getOrdersData($order_id);
|
|
$bill_no = $order_info['bill_no'] ?? '';
|
|
$store_id = $order_info['store_id'] ?? 0;
|
|
|
|
// Récupérer les infos complètes des motos
|
|
$products_info = $Remise->getFullProductInfoByDemandeId($id_demande);
|
|
$remise_product = '';
|
|
foreach ($products_info as $p) {
|
|
$remise_product .= "<br>• Modèle : " . ($p['name'] ?? '-');
|
|
$remise_product .= " | Marque : " . ($p['marque_name'] ?? '-');
|
|
$remise_product .= " | N° Série : " . ($p['sku'] ?? '-');
|
|
$remise_product .= " | N° Moteur : " . ($p['numero_de_moteur'] ?? '-');
|
|
$remise_product .= " | Châssis : " . ($p['chasis'] ?? '-');
|
|
if (!empty($p['puissance'])) {
|
|
$remise_product .= " | Puissance : " . $p['puissance'];
|
|
}
|
|
}
|
|
|
|
// ✅ RÉCUPÉRER TOUS LES STORES
|
|
$Stores = new Stores();
|
|
$allStores = $Stores->getActiveStore();
|
|
|
|
// ✅ SI REFUSÉ PAR LE SUPERADMIN
|
|
if ($demande_status == 'Refusé') {
|
|
if ($order_id) {
|
|
$ordersModel->update($order_id, ['paid_status' => 0]);
|
|
}
|
|
|
|
// Message de refus
|
|
$messageRefus = "❌ Demande de remise refusée : {$bill_no}<br>" .
|
|
"Produit : {$remise_product}<br>" .
|
|
"Décision : {$user['firstname']} {$user['lastname']} ({$user['group_name']})";
|
|
|
|
// Notifier les commerciaux du store concerné
|
|
$Notification->notifyGroupsByPermission('notifRemise', $messageRefus . "<br><em>Veuillez modifier la commande.</em>", (int)$store_id, "orders");
|
|
|
|
// ✅ NOTIFIER TOUS LES GROUPES AYANT validateRemise (POUR INFORMATION)
|
|
$Notification->notifyGroupsByPermissionAllStores('notifRemise', $messageRefus . "<br><em>Pour information</em>", 'remise/');
|
|
|
|
$message = 'La demande de remise a été refusée. Le commercial et les responsables ont été notifiés.';
|
|
|
|
} elseif ($demande_status == 'Accepté' || $demande_status == 'Validé') {
|
|
// ✅ SI ACCEPTÉ PAR LE SUPERADMIN
|
|
if ($order_id) {
|
|
$ordersModel->update($order_id, ['paid_status' => 2]);
|
|
}
|
|
|
|
$messageAcceptation = "✅ Demande de remise acceptée : {$bill_no}<br>" .
|
|
"Produit : {$remise_product}<br>" .
|
|
"Décision : {$user['firstname']} {$user['lastname']} ({$user['group_name']})";
|
|
|
|
// Notifier les caissières du store concerné
|
|
$Notification->notifyGroupsByPermission('notifSortieCaisse', $messageAcceptation . "<br><em>Commande prête à être traitée</em>", (int)$store_id, "orders");
|
|
|
|
// Notifier les commerciaux du store concerné
|
|
$Notification->notifyGroupsByPermission('notifRemise', $messageAcceptation, (int)$store_id, "orders");
|
|
|
|
// ✅ NOTIFIER TOUS LES GROUPES AYANT validateRemise (POUR INFORMATION)
|
|
$Notification->notifyGroupsByPermissionAllStores('notifRemise', $messageAcceptation . "<br><em>Pour information</em>", 'remise/');
|
|
|
|
$message = 'La demande de remise a été acceptée. La caissière, le commercial et les responsables ont été notifiés.';
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'success' => true,
|
|
'messages' => $message
|
|
]);
|
|
} else {
|
|
return $this->response->setJSON([
|
|
'success' => false,
|
|
'messages' => 'Erreur lors de la modification de la remise.'
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|