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.

134 lines
4.1 KiB

5 months ago
<?php
7 months ago
namespace App\Models;
use CodeIgniter\Model;
use App\Models\Orders;
use DateTime;
class Remise extends Model
{
protected $table = 'demande_remise';
5 months ago
protected $primaryKey = 'id_demande';
7 months ago
protected $allowedFields = ['id_order', 'montant_demande', 'date_demande','product','id_store','demande_status','total_price'];
5 months ago
7 months ago
public function getAllDemandeRemiseToday()
{
try {
5 months ago
$session = session();
$users = $session->get('user');
5 months ago
// ✅ SUPERADMIN VOIT TOUTES LES DEMANDES "EN ATTENTE" (POUR VALIDATION)
if ($users["group_name"] === "SuperAdmin") {
5 months ago
return $this->where('demande_status', 'En attente')
->orderBy('date_demande', 'DESC')
->findAll();
}
5 months ago
// ✅ DIRECTION ET DAF VOIENT TOUTES LES DEMANDES (TOUS STATUTS) POUR CONSULTATION
if ($users["group_name"] === "Direction" || $users["group_name"] === "DAF") {
5 months ago
return $this->orderBy('date_demande', 'DESC')
->findAll();
}
5 months ago
// ✅ AUTRES RÔLES : VOIR UNIQUEMENT CELLES DE LEUR MAGASIN
5 months ago
return $this->where('id_store', $users['store_id'])
->orderBy('date_demande', 'DESC')
->findAll();
7 months ago
} catch (\Exception $e) {
5 months ago
log_message('error', 'Erreur lors de la récupération des demandes : ' . $e->getMessage());
7 months ago
return [];
}
}
5 months ago
public function addDemande(array $data)
{
7 months ago
try {
5 months ago
return $this->insert($data);
7 months ago
} catch (\Exception $e) {
log_message('error', 'Erreur lors de l\'ajout de la demande de remise : ' . $e->getMessage());
return false;
}
}
5 months ago
public function getRemiseData1(int $remise_id)
{
return $this->select('montant_demande, id_order')
->where('id_demande', $remise_id)
->first();
7 months ago
}
5 months ago
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)
{
7 months ago
if ($id <= 0) {
5 months ago
log_message('error', 'ID invalide pour la mise à jour de la demande : ' . $id);
7 months ago
return false;
}
5 months ago
7 months ago
try {
5 months ago
$updateResult = $this->update($id, $data);
return $updateResult;
7 months ago
} catch (\Exception $e) {
5 months ago
log_message('error', 'Erreur lors de la mise à jour de la demande : ' . $e->getMessage());
return false;
7 months ago
}
}
5 months ago
7 months ago
public function getProductByDemandeId(int $id_demande): ?string
{
5 months ago
$row = $this->select('product')
->where('id_demande', $id_demande)
->first();
7 months ago
return $row['product'] ?? null;
}
5 months ago
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.";
}
7 months ago
}
4 months ago
/**
* 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;
}
5 months ago
}