getUserData(); $orderPaid = $orderModel->getPaidOrderData(); $paymentData = $orderModel->getPaymentModes(); $Recouvrement = new Recouvrement(); $totalRecouvrement = $Recouvrement->getTotalRecouvrements(); $sortieCaisse = new SortieCaisse(); $total_sortie_caisse = $sortieCaisse->getTotalSortieCaisse(); $total_sortie_caisse1= $total_sortie_caisse->mr; // Recouvrements $total_recouvrement_me = $totalRecouvrement->me; $total_recouvrement_bm = $totalRecouvrement->bm; $total_recouvrement_be = $totalRecouvrement->be; $total_recouvrement_mb = $totalRecouvrement->mb; // Avances $Avance = new Avance(); $totalAvance = $Avance->getTotalAvance(); $paymentDataAvance = $Avance->getPaymentModesAvance(); // Initialisation des totaux Orders (en utilisant les bonnes propriétés) $total_orders = isset($paymentData->total) ? $paymentData->total : 0; $total_mvola1_orders = isset($paymentData->total_mvola1) ? $paymentData->total_mvola1 : 0; $total_mvola2_orders = isset($paymentData->total_mvola2) ? $paymentData->total_mvola2 : 0; $total_espece1_orders = isset($paymentData->total_espece1) ? $paymentData->total_espece1 : 0; $total_espece2_orders = isset($paymentData->total_espece2) ? $paymentData->total_espece2 : 0; $total_virement_bancaire1_orders = isset($paymentData->total_virement_bancaire1) ? $paymentData->total_virement_bancaire1 : 0; $total_virement_bancaire2_orders = isset($paymentData->total_virement_bancaire2) ? $paymentData->total_virement_bancaire2 : 0; // Initialisation des totaux Avances (utiliser les bonnes propriétés de getPaymentModesAvance) $total_avances = isset($paymentDataAvance->total) ? $paymentDataAvance->total : 0; $total_mvola_avances = isset($paymentDataAvance->total_mvola) ? $paymentDataAvance->total_mvola : 0; $total_espece_avances = isset($paymentDataAvance->total_espece) ? $paymentDataAvance->total_espece : 0; $total_virement_bancaire_avances = isset($paymentDataAvance->total_virement_bancaire) ? $paymentDataAvance->total_virement_bancaire : 0; // Addition Orders + Avances $total = $total_orders + $total_avances; // Combiner les totaux par type de paiement $total_mvola1 = $total_mvola1_orders; $total_mvola2 = $total_mvola2_orders; $total_espece1 = $total_espece1_orders; $total_espece2 = $total_espece2_orders; $total_virement_bancaire1 = $total_virement_bancaire1_orders; $total_virement_bancaire2 = $total_virement_bancaire2_orders; // Totaux combinés Orders $total_mvola_orders_combined = $total_mvola1 + $total_mvola2; $total_espece_orders_combined = $total_espece1 + $total_espece2; $total_virement_bancaire_orders_combined = $total_virement_bancaire1 + $total_virement_bancaire2; // Totaux finaux (Orders + Avances) $total_mvola = $total_mvola_orders_combined + $total_mvola_avances; $total_espece = $total_espece_orders_combined + $total_espece_avances; $total_virement_bancaire = $total_virement_bancaire_orders_combined + $total_virement_bancaire_avances; // Ajustements avec les recouvrements et sorties caisse $total_mvola_final = $total_mvola - $total_recouvrement_me - $total_recouvrement_mb + $total_recouvrement_bm; $total_espece_final = $total_espece + $total_recouvrement_me + $total_recouvrement_be - $total_sortie_caisse1; $total_virement_bancaire_final = $total_virement_bancaire - $total_recouvrement_be - $total_recouvrement_bm + $total_recouvrement_mb; // check avance expired $avance = new Avance(); $avance->checkExpiredAvance(); $data = [ 'total' => $total, 'total_mvola' => $total_mvola_final, 'total_espece' => $total_espece_final, 'total_virement_bancaire' => $total_virement_bancaire_final, 'user_permission' => $this->permission, ]; $data['total_products'] = $productModel->countTotalProducts(); $data['total_paid_orders'] = $orderModel->countTotalPaidOrders(); $data['total_users'] = $userModel->countTotalUsers(); $data['total_stores'] = $storeModel->countTotalStores(); $newData = []; for ($i=0; $i < count($orderPaid); $i++) { $transitionData = []; for ($k=0; $k < count($allUsers); $k++) { if ($allUsers[$k]['id'] == $orderPaid[$i]['user_id']) { $transitionData2 = [ 'userId' => $allUsers[$k]['id'], 'userName' => $allUsers[$k]['username'], 'billId' => $orderPaid[$i]['id'], 'billNo' => $orderPaid[$i]['bill_no'], 'dateVente' => $orderPaid[$i]['date_time'], 'montantNet' => $orderPaid[$i]['net_amount'], ]; $transitionData[] = $transitionData2; } } $newData[] = $transitionData; } //data for the camember in dashboard $totalStoreOrder = $orderModel->getTotalOrderPerStore(); $totalOrders = $orderModel->getTotalOrders(); $totalOrdersCount = (int) $totalOrders->total_orders; if($totalOrdersCount>0){ foreach ($totalStoreOrder as $totalOrdersInStore) { $storeList = $storeModel->getStoreById($totalOrdersInStore->store_id); $labelStore[] = $storeList->name ?? 'Inconnu'; $totalPerStore[] = ((int)$totalOrdersInStore->total / $totalOrdersCount) * 100; } } else{ $labelStore[] = "zéro vente"; $totalPerStore[] = 0; } $data['labelStore'] = json_encode($labelStore); $data['totalPerStore'] = json_encode($totalPerStore); // filter to keep non empty array $filteredArray = array_filter($newData, function($item) { return !empty($item); // Keep only non-empty arrays }); // Re-index the array (optional, if you want sequential keys) $userWhoSoldProducts = array_values($filteredArray); // Count occurrences of each userId $userIdCounts = array_reduce($userWhoSoldProducts, function($carry, $item) { $userId = $item[0]['userId']; if (!isset($carry[$userId])) { $carry[$userId] = 0; } $carry[$userId]++; return $carry; }, []); $countId = []; foreach ($userIdCounts as $key => $value) { $countId[] = $key; } $data['count_id'] = $countId; // Check if the user is an Conseil $session = session(); $user_id = $session->get('user'); $data['is_admin'] = false; $data['isCommercial'] = false; $data['isChef'] = false; $data['isCaissier'] = false; $data['isMecanicien'] = false; if ($user_id['group_name'] == "Direction" || $user_id['group_name'] == "Conseil") { $data['is_admin'] = true; } $data['user_order'] = []; if ($user_id['group_name'] == \strtoupper("commerciale")) { $data['isCommercial'] = true; $data['user_order'] = json_encode($orderModel->getSingleStat($user_id['id']), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); } if ($user_id['group_name'] == "Cheffe d'Agence") { $data['isChef'] = true; } if ($user_id['group_name'] == "Caissière") { $data['isCaissier'] = true; } if ($user_id['group_name'] == "MECANICIEN") { $data['isMecanicien'] = true; } $data['page_title'] = 'Dashboard'; $data['marques_total'] = json_encode($orderModel->getTotalProductvente()); $data['marques'] = json_encode($Brancds->getName()); $Orders = new Orders(); $Products = new Products(); $Stores = new Stores(); $productVente = $Orders->getTotalProductvente(); $produitStock = $Products->getProductData(); $stor = $Stores->getActiveStore(); $data['ventes'] = \json_encode($productVente); $data['stock'] = \json_encode($produitStock); $data['stores'] = $stor; // Load the dashboard view with the data return $this->render_template('dashboard', $data); } }