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.
246 lines
9.4 KiB
246 lines
9.4 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()
|
|
{
|
|
$this->verifyRole('viewRemise');
|
|
|
|
$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 = '';
|
|
|
|
// ✅ SEUL LE SUPERADMIN PEUT VALIDER/REFUSER
|
|
if ($role === 'SuperAdmin' && $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>';
|
|
}
|
|
|
|
// ✅ DIRECTION ET DAF VOIENT SEULEMENT (pas de boutons d'action)
|
|
if (in_array($role, ['Direction', 'DAF'])) {
|
|
$buttons = '<span class="label label-info">Consultation uniquement</span>';
|
|
}
|
|
|
|
$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)
|
|
{
|
|
// ✅ VÉRIFIER QUE SEUL LE SUPERADMIN PEUT VALIDER
|
|
$session = session();
|
|
$user = $session->get('user');
|
|
$role = $user['group_name'] ?? '';
|
|
|
|
if ($role !== 'SuperAdmin') {
|
|
return $this->response->setJSON([
|
|
'success' => false,
|
|
'messages' => 'Seul le SuperAdmin peut valider ou refuser les demandes de remise.'
|
|
]);
|
|
}
|
|
|
|
$this->verifyRole('validateRemise');
|
|
|
|
$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 ($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)) {
|
|
$remise_product = $Remise->getProductByDemandeId($id_demande);
|
|
$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 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 : SuperAdmin";
|
|
|
|
// Notifier le commercial du store concerné
|
|
$Notification->createNotification(
|
|
$messageRefus . "<br><em>Veuillez modifier la commande.</em>",
|
|
"COMMERCIALE",
|
|
(int)$store_id,
|
|
"orders"
|
|
);
|
|
|
|
// ✅ NOTIFIER DIRECTION ET DAF DE TOUS LES STORES (POUR INFORMATION)
|
|
if (is_array($allStores) && count($allStores) > 0) {
|
|
foreach ($allStores as $store) {
|
|
$Notification->createNotification(
|
|
$messageRefus . "<br><em>Pour information</em>",
|
|
"Direction",
|
|
(int)$store['id'],
|
|
'remise/'
|
|
);
|
|
|
|
$Notification->createNotification(
|
|
$messageRefus . "<br><em>Pour information</em>",
|
|
"DAF",
|
|
(int)$store['id'],
|
|
'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 : SuperAdmin";
|
|
|
|
// Notifier la Caissière du store concerné
|
|
$Notification->createNotification(
|
|
$messageAcceptation . "<br><em>Commande prête à être traitée</em>",
|
|
"Caissière",
|
|
(int)$store_id,
|
|
"orders"
|
|
);
|
|
|
|
// Notifier le commercial du store concerné
|
|
$Notification->createNotification(
|
|
$messageAcceptation,
|
|
"COMMERCIALE",
|
|
(int)$store_id,
|
|
"orders"
|
|
);
|
|
|
|
// ✅ NOTIFIER DIRECTION ET DAF DE TOUS LES STORES (POUR INFORMATION)
|
|
if (is_array($allStores) && count($allStores) > 0) {
|
|
foreach ($allStores as $store) {
|
|
$Notification->createNotification(
|
|
$messageAcceptation . "<br><em>Pour information</em>",
|
|
"Direction",
|
|
(int)$store['id'],
|
|
'remise/'
|
|
);
|
|
|
|
$Notification->createNotification(
|
|
$messageAcceptation . "<br><em>Pour information</em>",
|
|
"DAF",
|
|
(int)$store['id'],
|
|
'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.'
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|