get('user'); // ✅ SUPERADMIN VOIT TOUTES LES DEMANDES "EN ATTENTE" (POUR VALIDATION) if ($users["group_name"] === "SuperAdmin") { return $this->where('demande_status', 'En attente') ->orderBy('date_demande', 'DESC') ->findAll(); } // ✅ DIRECTION ET DAF VOIENT TOUTES LES DEMANDES (TOUS STATUTS) POUR CONSULTATION if ($users["group_name"] === "Direction" || $users["group_name"] === "DAF") { return $this->orderBy('date_demande', 'DESC') ->findAll(); } // ✅ AUTRES RÔLES : VOIR UNIQUEMENT CELLES DE LEUR MAGASIN return $this->where('id_store', $users['store_id']) ->orderBy('date_demande', 'DESC') ->findAll(); } catch (\Exception $e) { log_message('error', 'Erreur lors de la récupération des demandes : ' . $e->getMessage()); return []; } } public function addDemande(array $data) { try { return $this->insert($data); } catch (\Exception $e) { log_message('error', 'Erreur lors de l\'ajout de la demande de remise : ' . $e->getMessage()); return false; } } public function getRemiseData1(int $remise_id) { return $this->select('montant_demande, id_order') ->where('id_demande', $remise_id) ->first(); } public function getOrderIdByDemandeId(int $id_demande): ?int { $result = $this->select('id_order') ->where('id_demande', $id_demande) ->first(); return $result['id_order'] ?? null; } public function updateRemise($id, $data) { if ($id <= 0) { log_message('error', 'ID invalide pour la mise à jour de la demande : ' . $id); return false; } try { $updateResult = $this->update($id, $data); return $updateResult; } catch (\Exception $e) { log_message('error', 'Erreur lors de la mise à jour de la demande : ' . $e->getMessage()); return false; } } public function getProductByDemandeId(int $id_demande): ?string { $row = $this->select('product') ->where('id_demande', $id_demande) ->first(); return $row['product'] ?? null; } public function updateRemise1(int $id, $data) { $existing = $this->where('id_order', $id)->first(); if ($existing) { $this->update($existing['id_demande'], $data); return "Remise mise à jour avec succès."; } else { $this->insert($data); return "Nouvelle remise créée avec succès."; } } /** * Vérifier si une commande a une demande de remise en attente * @param int $order_id * @return bool true si remise en attente, false sinon */ public function hasRemisePendingForOrder(int $order_id): bool { $result = $this->where('id_order', $order_id) ->where('demande_status', 'En attente') ->first(); return $result !== null; } /** * Vérifier si une commande a une remise validée * @param int $order_id * @return bool */ public function hasRemiseValidatedForOrder(int $order_id): bool { $result = $this->where('id_order', $order_id) ->whereIn('demande_status', ['Accepté', 'Validé']) ->first(); return $result !== null; } }