diff --git a/app/Controllers/OrderController.php b/app/Controllers/OrderController.php index 9d33be04..2ce17ab0 100644 --- a/app/Controllers/OrderController.php +++ b/app/Controllers/OrderController.php @@ -915,14 +915,28 @@ public function update(int $id) if ($role === 'COMMERCIALE') { $paid_status = 2; - } else { + } elseif ($role === 'Caissière') { $paid_status = $this->request->getPost('paid_status'); + } else { + // SuperAdmin, Direction, DAF ne peuvent pas valider (paid_status=1) directement + // Leur rôle est de valider les remises via la page remise/, pas les commandes + $requested_status = (int)$this->request->getPost('paid_status'); + $paid_status = ($requested_status == 1) ? $old_paid_status : $requested_status; } $validated_by = $current_order['validated_by'] ?? null; $validated_at = $current_order['validated_at'] ?? null; - + if ($old_paid_status != 1 && $paid_status == 1 && $role === 'Caissière') { + // ✅ Bloquer si la commande a une remise non encore approuvée par SuperAdmin + $currentDiscount = (float)($current_order['discount'] ?? 0); + if ($currentDiscount > 0) { + $RemiseCheck = new Remise(); + if (!$RemiseCheck->hasRemiseValidatedForOrder($id)) { + session()->setFlashData('errors', 'Cette commande a une demande de remise en attente de validation par le SuperAdmin.'); + return redirect()->to('orders/'); + } + } $validated_by = $user['id']; $validated_at = date('Y-m-d H:i:s'); } @@ -1030,7 +1044,7 @@ public function update(int $id) $customer_name = $this->request->getPost('customer_name'); $bill_no = $current_order['bill_no']; - // ✅ Notification SECURITE du store concerné + // ✅ Notification SECURITE du store concerné (toujours) $Notification->createNotification( "Commande validée: {$bill_no} - Client: {$customer_name}", "SECURITE", @@ -1038,38 +1052,39 @@ public function update(int $id) 'orders' ); - // ✅ RÉCUPÉRER TOUS LES STORES - $Stores = new Stores(); - $allStores = $Stores->getActiveStore(); - - $messageGlobal = "✅ Commande validée : {$bill_no}
" . - "Store : " . $this->returnStore($user['store_id']) . "
" . - "Client : {$customer_name}
" . - "Validée par : {$user['firstname']} {$user['lastname']}"; - - // ✅ NOTIFIER DIRECTION, DAF, SUPERADMIN DE TOUS LES STORES - if (is_array($allStores) && count($allStores) > 0) { - foreach ($allStores as $store) { - $Notification->createNotification( - $messageGlobal, - "Direction", - (int)$store['id'], - 'orders' - ); - - $Notification->createNotification( - $messageGlobal, - "DAF", - (int)$store['id'], - 'orders' - ); - - $Notification->createNotification( - $messageGlobal, - "SuperAdmin", - (int)$store['id'], - 'orders' - ); + // ✅ Notifier Direction, DAF, SuperAdmin UNIQUEMENT pour les commandes avec remise + if ((float)$discount > 0) { + $Stores = new Stores(); + $allStores = $Stores->getActiveStore(); + + $messageGlobal = "✅ Commande validée : {$bill_no}
" . + "Store : " . $this->returnStore($user['store_id']) . "
" . + "Client : {$customer_name}
" . + "Validée par : {$user['firstname']} {$user['lastname']}"; + + if (is_array($allStores) && count($allStores) > 0) { + foreach ($allStores as $store) { + $Notification->createNotification( + $messageGlobal, + "Direction", + (int)$store['id'], + 'orders' + ); + + $Notification->createNotification( + $messageGlobal, + "DAF", + (int)$store['id'], + 'orders' + ); + + $Notification->createNotification( + $messageGlobal, + "SuperAdmin", + (int)$store['id'], + 'orders' + ); + } } } } diff --git a/app/Views/orders/edit.php b/app/Views/orders/edit.php index a2110fe1..181200a3 100644 --- a/app/Views/orders/edit.php +++ b/app/Views/orders/edit.php @@ -342,9 +342,9 @@ - get('user'); - if ($users && $users['group_name'] !== 'COMMERCIALE'): + get('user'); + if ($users && $users['group_name'] === 'Caissière'): ?>
diff --git a/app/Views/templates/footer.php b/app/Views/templates/footer.php index d00e1b5a..51dc8f33 100644 --- a/app/Views/templates/footer.php +++ b/app/Views/templates/footer.php @@ -3,7 +3,7 @@
Copyright ©. All rights reserved.
-