@ -110,8 +110,8 @@ class Orders extends Model
'orders.customer_phone',
'orders.customer_cin',
'orders.customer_address',
'orders.customer_type', // ✅ DÉJÀ PRÉSENT
'orders.source', // ✅ DÉJÀ PRÉSENT
'orders.customer_type',
'orders.source',
'orders.discount',
'orders.date_time',
'orders.gross_amount',
@ -367,12 +367,12 @@ class Orders extends Model
$isAdmin = in_array($users['group_name'], ['SuperAdmin', 'Direction', 'DAF']);
if($isAdmin) {
return $this->whereIn('paid_status', [1, 3]) // ← Ajoutez 3 ici
return $this->whereIn('paid_status', [1, 3]) // ✅ DÉJÀ BON !
->orderBy('id', 'DESC')
->findAll();
}
else {
return $this->whereIn('paid_status', [1, 3]) // ← Ajoutez 3 ici
return $this->whereIn('paid_status', [1, 3]) // ✅ DÉJÀ BON !
->where('store_id', $users['store_id'])
->orderBy('id', 'DESC')
->findAll();
@ -598,19 +598,34 @@ class Orders extends Model
return $order;
}
public function getPerformanceByOrders()
public function getPerformanceByOrders($startDate = null, $endDate = null, $pvente = null )
{
$Performance = $this->db->table('orders')
$builder = $this->db->table('orders')
->select('orders.id as orderid, orders.net_amount, orders.date_time as datevente, orders.discount, products.price, products.sku, products.prix_vente, products.name as motoname, stores.id as store_id, users.firstname, users.lastname, users.email')
->join('stores', 'orders.store_id = stores.id')
->join('orders_item', 'orders.id = orders_item.order_id')
->join('products', 'products.id = orders_item.product_id')
->join('users', 'users.id = orders.user_id')
->whereIn('orders.paid_status', [1, 3])
->get()
->getResultArray();
->whereIn('orders.paid_status', [1, 3]);
return $Performance;
// ✅ AJOUT : FILTRES PAR DATE
if (!empty($startDate) & & !empty($endDate)) {
$builder->where('DATE(orders.date_time) >=', $startDate);
$builder->where('DATE(orders.date_time) < =', $endDate);
} elseif (!empty($startDate)) {
$builder->where('DATE(orders.date_time) >=', $startDate);
} elseif (!empty($endDate)) {
$builder->where('DATE(orders.date_time) < =', $endDate);
}
// ✅ AJOUT : FILTRE PAR POINT DE VENTE
if (!empty($pvente) & & $pvente !== 'TOUS') {
$builder->where('stores.name', $pvente);
}
$builder->orderBy('orders.date_time', 'DESC');
return $builder->get()->getResultArray();
}
// for Adminan cheffe d'agence
@ -692,33 +707,46 @@ class Orders extends Model
public function getUserPerformanceToday(string $date)
{
$session = session();
$currentUser = $session->get('user');
$isAdmin = in_array($currentUser['group_name'], ['SuperAdmin', 'Direction', 'DAF']);
$users = $this->getUserList();
$results = [];
foreach ($users as $user) {
$summary = $this->db->table('orders')
$builder = $this->db->table('orders')
->select('COUNT(id) AS total_user_order, SUM(net_amount) AS total_prix_vente')
->where('user_id', $user->user_id)
->where('DATE(date_time)', $date)
->whereIn('paid_status', [1, 3])
->get()
->getRow();
->whereIn('paid_status', [1, 3]);
// ✅ Filtre par magasin si pas admin
if (!$isAdmin & & !empty($currentUser['store_id']) & & $currentUser['store_id'] != 0) {
$builder->where('store_id', $currentUser['store_id']);
}
$ids = $this->db->table('orders')
$summary = $builder->get()->getRow();
$idsBuilder = $this->db->table('orders')
->select('id')
->where('user_id', $user->user_id)
->where('DATE(date_time)', $date)
->whereIn('paid_status', [1, 3])
->get()
->getResult();
->whereIn('paid_status', [1, 3]);
// ✅ Filtre par magasin si pas admin
if (!$isAdmin & & !empty($currentUser['store_id']) & & $currentUser['store_id'] != 0) {
$idsBuilder->where('store_id', $currentUser['store_id']);
}
$ids = $idsBuilder->get()->getResult();
$orderIds = array_map(fn($o) => $o->id, $ids);
$results[] = [
'user_id' => $user->user_id,
'full_name' => $user->full_name,
'total_user_order' => $summary->total_user_order,
'total_prix_vente' => $summary->total_prix_vente,
'total_user_order' => $summary->total_user_order ?? 0 ,
'total_prix_vente' => $summary->total_prix_vente ?? 0 ,
'order_ids' => $orderIds,
];
}
@ -728,6 +756,10 @@ class Orders extends Model
public function getUserPerformanceByWeek(string $date = null)
{
$session = session();
$currentUser = $session->get('user');
$isAdmin = in_array($currentUser['group_name'], ['SuperAdmin', 'Direction', 'DAF']);
$users = $this->getUserList();
$results = [];
@ -740,24 +772,33 @@ class Orders extends Model
$endOfWeek = date('Y-m-d', strtotime('sunday this week', $timestamp));
foreach ($users as $user) {
$summary = $this->db->table('orders')
$builder = $this->db->table('orders')
->select('COUNT(id) AS total_user_order, SUM(net_amount) AS total_prix_vente')
->where('user_id', $user->user_id)
->where('DATE(date_time) >=', $startOfWeek)
->where('DATE(date_time) < =', $endOfWeek)
->whereIn('paid_status', [1, 3])
->get()
->getRow();
->whereIn('paid_status', [1, 3]);
$ids = $this->db->table('orders')
// ✅ Filtre par magasin si pas admin
if (!$isAdmin & & !empty($currentUser['store_id']) & & $currentUser['store_id'] != 0) {
$builder->where('store_id', $currentUser['store_id']);
}
$summary = $builder->get()->getRow();
$idsBuilder = $this->db->table('orders')
->select('id')
->where('user_id', $user->user_id)
->where('DATE(date_time) >=', $startOfWeek)
->where('DATE(date_time) < =', $endOfWeek)
->whereIn('paid_status', [1, 3])
->get()
->getResult();
->whereIn('paid_status', [1, 3]);
// ✅ Filtre par magasin si pas admin
if (!$isAdmin & & !empty($currentUser['store_id']) & & $currentUser['store_id'] != 0) {
$idsBuilder->where('store_id', $currentUser['store_id']);
}
$ids = $idsBuilder->get()->getResult();
$orderIds = array_map(fn($o) => $o->id, $ids);
$results[] = [
@ -771,7 +812,6 @@ class Orders extends Model
return $results;
}
public function checkMinimalPrice($product_id, $discount)
{
$fourchetteModel = new FourchettePrix(); // plus court car on a "use"
@ -787,6 +827,10 @@ public function checkMinimalPrice($product_id, $discount)
public function getUserPerformanceByMonth(string $month)
{
$session = session();
$currentUser = $session->get('user');
$isAdmin = in_array($currentUser['group_name'], ['SuperAdmin', 'Direction', 'DAF']);
$startOfMonth = date('Y-m-01', strtotime($month . '-01'));
$endOfMonth = date('Y-m-t', strtotime($month . '-01'));
@ -794,24 +838,33 @@ public function getUserPerformanceByMonth(string $month)
$results = [];
foreach ($users as $user) {
$orderData = $this->db->table('orders')
$builder = $this->db->table('orders')
->select('COUNT(id) as total_user_order, SUM(net_amount) as total_prix_vente')
->where('user_id', $user->user_id)
->where('DATE(date_time) >=', $startOfMonth)
->where('DATE(date_time) < =', $endOfMonth)
->whereIn('paid_status', [1, 3])
->get()
->getRow();
->whereIn('paid_status', [1, 3]);
// ✅ Filtre par magasin si pas admin
if (!$isAdmin & & !empty($currentUser['store_id']) & & $currentUser['store_id'] != 0) {
$builder->where('store_id', $currentUser['store_id']);
}
$orderData = $builder->get()->getRow();
$ids = $this->db->table('orders')
$idsBuilder = $this->db->table('orders')
->select('id')
->where('user_id', $user->user_id)
->where('DATE(date_time) >=', $startOfMonth)
->where('DATE(date_time) < =', $endOfMonth)
->whereIn('paid_status', [1, 3])
->get()
->getResult();
->whereIn('paid_status', [1, 3]);
// ✅ Filtre par magasin si pas admin
if (!$isAdmin & & !empty($currentUser['store_id']) & & $currentUser['store_id'] != 0) {
$idsBuilder->where('store_id', $currentUser['store_id']);
}
$ids = $idsBuilder->get()->getResult();
$orderIds = array_map(fn($o) => $o->id, $ids);
$results[] = [
@ -866,4 +919,6 @@ public function getPerformanceByCaissier(int $caissierId, $startDate = null, $en
->getResultArray();
}
}