From 09ffc99c0f5b190f7ce2c3d679f84779dcdc3004 Mon Sep 17 00:00:00 2001 From: Sarobidy22 Date: Fri, 29 Aug 2025 22:48:32 +0300 Subject: [PATCH] modification 30082025 --- app/Config/Routes.php | 4 +- app/Controllers/AvanceController.php | 191 ++++++------ app/Controllers/AvanceControlleur.php | 108 +++++++ app/Controllers/Dashboard.php | 75 +++-- app/Controllers/RecouvrementController.php | 74 +++-- app/Models/Avance.php | 76 ++++- app/Models/Products.php | 31 +- app/Views/avances/avance.php | 331 ++++++++++++--------- app/Views/dashboard.php | 6 +- app/Views/mecanicien/index.php | 3 +- app/Views/recouvrement/index.php | 279 ++++++++--------- app/Views/templates/header.php | 2 + 12 files changed, 712 insertions(+), 468 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index de525130..d72f6c24 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -284,7 +284,9 @@ $routes->group('', ['filter' => 'auth'], function ($routes) { $routes->group('/avances', function ($routes) { $routes->get('/', [AvanceController::class, 'index']); $routes->get('fetchAvanceData', [AvanceController::class, 'fetchAvanceData']); - $routes->get('fetchAvanceBecameOrder', [AvanceController::class, 'fetchAvanceBecameOrder']); + $routes->get('fetchCompletedAvances', [AvanceController::class, 'fetchCompletedAvances']); + $routes->get('fetchIncompleteAvances', [AvanceController::class, 'fetchIncompleteAvances']); + $routes->get('fetchAvanceBecameOrder', 'AvanceController::fetchAvanceBecameOrder'); $routes->get('fetchExpiredAvance', [AvanceController::class, 'fetcheExpiredAvance']); $routes->get('fetchSingleAvance/(:num)', [AvanceController::class, 'fetchSingleAvance']); $routes->post('createAvance', [AvanceController::class, 'createAvance']); diff --git a/app/Controllers/AvanceController.php b/app/Controllers/AvanceController.php index d1e2d71c..1da9c316 100644 --- a/app/Controllers/AvanceController.php +++ b/app/Controllers/AvanceController.php @@ -128,13 +128,16 @@ class AvanceController extends AdminController public function fetchAvanceData() { - return $this->fetchAvanceDataGeneric('getAllAvanceData'); + // Avances incomplètes (reste à payer > 0) + return $this->fetchAvanceDataGeneric('getIncompleteAvances'); } - + public function fetchAvanceBecameOrder() { - return $this->fetchAvanceDataGeneric('getAllAvanceData1'); + // Avances complètes (reste à payer = 0) + return $this->fetchAvanceDataGeneric('getCompletedAvances'); } + public function fetcheExpiredAvance() { @@ -399,6 +402,7 @@ class AvanceController extends AdminController $data = [ 'type_avance' => $type_avance, + 'type_payment' => $this->request->getPost('type_payment'), 'customer_name' => $this->request->getPost('customer_name_avance'), 'customer_address' => $this->request->getPost('customer_address_avance'), 'customer_phone' => $this->request->getPost('customer_phone_avance'), @@ -444,30 +448,29 @@ class AvanceController extends AdminController ]); } } - - public function updateAvance(int $id) + public function updateAvance() { $this->verifyRole('updateAvance'); - + if ($this->request->getMethod() !== 'post') { return $this->response->setJSON([ 'success' => false, 'messages' => 'Méthode non autorisée' ]); } - + try { $session = session(); $users = $session->get('user'); $Avance = new Avance(); $Products = new Products(); - $Orders = new Orders(); - $Company = new Company(); $Notification = new NotificationController(); - + + // Validation des données (avec les vrais noms des champs du formulaire) $validation = \Config\Services::validation(); $validation->setRules([ + 'id' => 'required|numeric', 'customer_name_avance_edit' => 'required|min_length[2]', 'customer_phone_avance_edit' => 'required', 'customer_address_avance_edit' => 'required', @@ -476,121 +479,106 @@ class AvanceController extends AdminController 'avance_amount_edit' => 'required|numeric|greater_than[0]', 'type_avance_edit' => 'required|in_list[terre,mere]' ]); - + if (!$validation->withRequest($this->request)->run()) { return $this->response->setJSON([ 'success' => false, 'messages' => 'Données invalides: ' . implode(', ', $validation->getErrors()) ]); } - - // Récupérer la date de création actuelle de l'avance pour recalculer deadline - $currentAvance = $Avance->find($id); - if (!$currentAvance) { + + $avance_id = $this->request->getPost('id'); // Changement ici + + // Vérifier si l'avance existe + $existingAvance = $Avance->fetchSingleAvance($avance_id); + if (!$existingAvance) { return $this->response->setJSON([ 'success' => false, - 'messages' => 'Avance introuvable.' + 'messages' => 'Avance non trouvée' ]); } - $avance_date = $currentAvance['avance_date']; - - // Calcul automatique deadline selon le type d'avance - $type_avance = $this->request->getPost('type_avance_edit'); - if ($type_avance === 'terre') { - $deadline = date('Y-m-d', strtotime($avance_date . ' +15 days')); - } elseif ($type_avance === 'mere') { - $deadline = date('Y-m-d', strtotime($avance_date . ' +2 months')); - } else { - $deadline = null; + + // Vérifier les permissions (admin ou propriétaire) + $isAdmin = $this->isAdmin($users); + $isOwner = $users['id'] === $existingAvance['user_id']; + + if (!$isAdmin && !$isOwner) { + return $this->response->setJSON([ + 'success' => false, + 'messages' => 'Vous n\'avez pas les droits pour modifier cette avance' + ]); } - + + // Recalculer la deadline si le type d'avance a changé + $type_avance = $this->request->getPost('type_avance_edit'); // Changement ici + $current_deadline = $existingAvance['deadline']; + + // Si le type a changé, recalculer la deadline + if ($type_avance !== $existingAvance['type_avance']) { + if ($type_avance === 'terre') { + $current_deadline = date('Y-m-d', strtotime($existingAvance['avance_date'] . ' +15 days')); + } elseif ($type_avance === 'mere') { + $current_deadline = date('Y-m-d', strtotime($existingAvance['avance_date'] . ' +2 months')); + } + } + + $old_product_id = $existingAvance['product_id']; + $new_product_id = (int)$this->request->getPost('id_product_edit'); // Changement ici + $data = [ 'type_avance' => $type_avance, - 'customer_name' => $this->request->getPost('customer_name_avance_edit'), - 'customer_address' => $this->request->getPost('customer_address_avance_edit'), - 'customer_phone' => $this->request->getPost('customer_phone_avance_edit'), - 'customer_cin' => $this->request->getPost('customer_cin_avance_edit'), - 'gross_amount' => (float)$this->request->getPost('gross_amount_edit'), - 'avance_amount' => (float)$this->request->getPost('avance_amount_edit'), - 'amount_due' => (float)$this->request->getPost('amount_due_edit'), - 'product_id' => (int)$this->request->getPost('id_product_edit'), - 'deadline' => $deadline, + 'type_payment' => $this->request->getPost('type_payment_edit'), // Changement ici + 'customer_name' => $this->request->getPost('customer_name_avance_edit'), // Changement ici + 'customer_address' => $this->request->getPost('customer_address_avance_edit'), // Changement ici + 'customer_phone' => $this->request->getPost('customer_phone_avance_edit'), // Changement ici + 'customer_cin' => $this->request->getPost('customer_cin_avance_edit'), // Changement ici + 'deadline' => $current_deadline, + 'product_id' => $new_product_id, + 'gross_amount' => (float)$this->request->getPost('gross_amount_edit'), // Changement ici + 'avance_amount' => (float)$this->request->getPost('avance_amount_edit'), // Changement ici + 'amount_due' => (float)$this->request->getPost('amount_due_edit'), // Changement ici ]; - - $amount_due = $data['amount_due']; - - if ($amount_due <= 0) { - $bill_no = 'BILPR-' . strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 4)); - $company = $Company->getCompanyData(1); + + // Mettre à jour l'avance + if ($Avance->updateAvance($avance_id, $data)) { - $service_charge_rate = $company['service_charge_value'] ?? 0; - $vat_charge_rate = $company['vat_charge_value'] ?? 0; - $gross_amount = $data['gross_amount']; - $vat_charge = ($gross_amount / 100) * $vat_charge_rate; - - $order_data = [ - 'bill_no' => $bill_no, - 'customer_name' => $data['customer_name'], - 'customer_address' => $data['customer_address'], - 'customer_phone' => $data['customer_phone'], - 'customer_cin' => $data['customer_cin'], - 'gross_amount' => $gross_amount, - 'net_amount' => $gross_amount, - 'date_time' => date('Y-m-d H:i:s'), - 'service_charge_rate' => $service_charge_rate, - 'vat_charge_rate' => $vat_charge_rate, - 'vat_charge' => $vat_charge, - 'discount' => 0, - 'paid_status' => 1, - 'user_id' => $users['id'], - 'store_id' => $users['store_id'], - 'amount_value' => $gross_amount, - 'rate_value' => $gross_amount, - ]; - - $product_id = [$data['product_id']]; - - if ($Orders->create($order_data, $product_id)) { - $Avance->updateAvance($id, ['is_order' => 1]); - $Notification->createNotification( - 'Une avance a été convertie en commande', - "Conseil", - (int)$users['store_id'], - 'orders' - ); - - return $this->response->setJSON([ - 'success' => true, - 'messages' => 'Avance convertie en commande avec succès.' - ]); - } else { - return $this->response->setJSON([ - 'success' => false, - 'messages' => 'Erreur lors de la conversion de l\'avance en commande' - ]); + // Gérer le changement de produit si nécessaire + if ($old_product_id !== $new_product_id) { + // Libérer l'ancien produit + $Products->update($old_product_id, ['product_sold' => 0]); + // Marquer le nouveau produit comme vendu + $Products->update($new_product_id, ['product_sold' => 1]); } + + // Créer une notification + $Notification->createNotification( + 'Une avance a été modifiée', + "Conseil", + (int)$users['store_id'], + 'avances' + ); + + return $this->response->setJSON([ + 'success' => true, + 'messages' => 'Avance modifiée avec succès !' + ]); + } else { - if ($Avance->updateAvance($id, $data)) { - return $this->response->setJSON([ - 'success' => true, - 'messages' => 'Avance mise à jour avec succès.' - ]); - } else { - return $this->response->setJSON([ - 'success' => false, - 'messages' => 'Erreur lors de la mise à jour de l\'avance.' - ]); - } + return $this->response->setJSON([ + 'success' => false, + 'messages' => 'Erreur lors de la modification de l\'avance' + ]); } + } catch (\Exception $e) { - log_message('error', "Erreur mise à jour avance: " . $e->getMessage()); + log_message('error', "Erreur modification avance: " . $e->getMessage()); return $this->response->setJSON([ 'success' => false, 'messages' => 'Une erreur interne est survenue' ]); } } - + public function removeAvance() { $this->verifyRole('deleteAvance'); @@ -659,4 +647,7 @@ class AvanceController extends AdminController ]); } } + + + } \ No newline at end of file diff --git a/app/Controllers/AvanceControlleur.php b/app/Controllers/AvanceControlleur.php index ddfe8f6c..b13fe653 100644 --- a/app/Controllers/AvanceControlleur.php +++ b/app/Controllers/AvanceControlleur.php @@ -197,5 +197,113 @@ class AvanceController extends AdminController ->setJSON(['error' => 'Une erreur interne est survenue. Lors de la création d\'une avance']); } } +// Avances complètes (reste = 0) +public function fetchAvanceComplete() +{ + $this->verifyRole('viewAvance'); + $Avance = new Avance(); + $data = $Avance->getAvanceComplete(); // méthode à créer dans le modèle + + $result = ['data' => []]; + $session = session(); + $users = $session->get('user'); + $isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']); + $isCommerciale = in_array($users['group_name'], ['COMMERCIALE']); + $isCaissier = in_array($users['group_name'], ['Caissier']); + + foreach($data as $value) { + $buttons = ''; + $isOwner = $users['id'] === $value['user_id']; + + if (in_array('updateAvance', $this->permission) && ($isAdmin || $isOwner)) + $buttons .= ''; + if (in_array('deleteAvance', $this->permission) && ($isAdmin || $isOwner)) + $buttons .= ' '; + + $date_time = date('d-m-Y h:i a', strtotime($value['avance_date'])); + + if ($isAdmin) { + $row = [ + $value['customer_name'], + $value['customer_phone'], + $value['customer_address'], + number_format((int)$value['gross_amount'],0,',',' '), + number_format((int)$value['avance_amount'],0,',',' '), + number_format((int)$value['amount_due'],0,',',' '), + $date_time, + $buttons + ]; + $result['data'][] = $row; + } + + if ($isCommerciale || $isCaissier) { + $row = [ + $value['avance_id'], + number_format((int)$value['avance_amount'],0,',',' '), + number_format((int)$value['amount_due'],0,',',' '), + $date_time, + $buttons + ]; + $result['data'][] = $row; + } + } + + return $this->response->setJSON($result); +} + +// Avances expirées (deadline dépassée) +public function fetchExpiredAvance() +{ + $this->verifyRole('viewAvance'); + $Avance = new Avance(); + $data = $Avance->getAvanceExpired(); // méthode à créer dans le modèle + + $result = ['data' => []]; + $session = session(); + $users = $session->get('user'); + $isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']); + $isCommerciale = in_array($users['group_name'], ['COMMERCIALE']); + $isCaissier = in_array($users['group_name'], ['Caissier']); + + foreach($data as $value) { + $buttons = ''; + $isOwner = $users['id'] === $value['user_id']; + + if (in_array('updateAvance', $this->permission) && ($isAdmin || $isOwner)) + $buttons .= ''; + if (in_array('deleteAvance', $this->permission) && ($isAdmin || $isOwner)) + $buttons .= ' '; + + $date_time = date('d-m-Y h:i a', strtotime($value['avance_date'])); + + if ($isAdmin) { + $row = [ + $value['customer_name'], + $value['customer_phone'], + $value['customer_address'], + number_format((int)$value['gross_amount'],0,',',' '), + number_format((int)$value['avance_amount'],0,',',' '), + number_format((int)$value['amount_due'],0,',',' '), + $date_time, + $buttons + ]; + $result['data'][] = $row; + } + + if ($isCommerciale || $isCaissier) { + $row = [ + $value['avance_id'], + number_format((int)$value['avance_amount'],0,',',' '), + number_format((int)$value['amount_due'],0,',',' '), + $date_time, + $buttons + ]; + $result['data'][] = $row; + } + } + + return $this->response->setJSON($result); +} + } diff --git a/app/Controllers/Dashboard.php b/app/Controllers/Dashboard.php index d043fd20..3210d8fc 100644 --- a/app/Controllers/Dashboard.php +++ b/app/Controllers/Dashboard.php @@ -33,43 +33,68 @@ class Dashboard extends AdminController $sortieCaisse = new SortieCaisse(); $total_sortie_caisse = $sortieCaisse->getTotalSortieCaisse(); $total_sortie_caisse1= $total_sortie_caisse->mr; - // dd($totalRecouvrement); + + // Recouvrements $total_recouvrement_me = $totalRecouvrement->me; $total_recouvrement_bm = $totalRecouvrement->bm; $total_recouvrement_be = $totalRecouvrement->be; $total_recouvrement_mb = $totalRecouvrement->mb; - // total avance - $Avance = new Avance(); + + // Avances + $Avance = new Avance(); $totalAvance = $Avance->getTotalAvance(); - $total_avance = $totalAvance->ta; - // Initialisation des totaux avec 0 au cas où il n'y aurait pas de données - $total = isset($paymentData->total) ? $paymentData->total : 0; - $total_mvola1 = isset($paymentData->total_mvola1) ? $paymentData->total_mvola1 : 0; - $total_mvola2 = isset($paymentData->total_mvola2) ? $paymentData->total_mvola2 : 0; - $total_espece1 = isset($paymentData->total_espece1) ? $paymentData->total_espece1 : 0; - $total_espece2 = isset($paymentData->total_espece2) ? $paymentData->total_espece2 : 0; - $total_virement_bancaire1 = isset($paymentData->total_virement_bancaire1) ? $paymentData->total_virement_bancaire1 : 0; - $total_virement_bancaire2 = isset($paymentData->total_virement_bancaire2) ? $paymentData->total_virement_bancaire2 : 0; - - $total_mvola = $total_mvola1 + $total_mvola2; - $total_mvola1 = $total_mvola - $total_recouvrement_me - $total_recouvrement_mb + $total_recouvrement_bm; - - $total_espece = $total_espece1 + $total_espece2; - $total_espece1 = $total_espece + $total_recouvrement_me + $total_recouvrement_be - $total_sortie_caisse1 + $total_avance; + $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; - //die("Test Stephane " . $total_recouvrement_me); - $total_virement_bancaire = $total_virement_bancaire1 + $total_virement_bancaire2; - $total_virement_bancaire1 = $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_mvola1, - 'total_espece' => $total_espece1, - 'total_virement_bancaire' => $total_virement_bancaire1, + 'total_mvola' => $total_mvola_final, + 'total_espece' => $total_espece_final, + 'total_virement_bancaire' => $total_virement_bancaire_final, 'user_permission' => $this->permission, ]; diff --git a/app/Controllers/RecouvrementController.php b/app/Controllers/RecouvrementController.php index 009b02da..a3711476 100644 --- a/app/Controllers/RecouvrementController.php +++ b/app/Controllers/RecouvrementController.php @@ -41,54 +41,62 @@ class RecouvrementController extends AdminController return $this->response->setJSON($data); } - private function getTotalsArray(): array + private function getTotalsArray(): array { $orders = new Orders(); $recouvrement = new Recouvrement(); $sortieCaisse = new SortieCaisse(); $avance = new Avance(); - + // Récupère les données brutes - $paymentData = $orders->getPaymentModes(); - $total_sortie_caisse = $sortieCaisse->getTotalSortieCaisse()->mr ?? 0; - $totalRecouvrement = $recouvrement->getTotalRecouvrements(); - + $paymentDataOrders = $orders->getPaymentModes(); + $paymentDataAvance = $avance->getPaymentModesAvance(); + $total_sortie_caisse = $sortieCaisse->getTotalSortieCaisse()->mr ?? 0; + $totalRecouvrement = $recouvrement->getTotalRecouvrements(); + // Totaux recouvrement $me = $totalRecouvrement->me ?? 0; $bm = $totalRecouvrement->bm ?? 0; $be = $totalRecouvrement->be ?? 0; $mb = $totalRecouvrement->mb ?? 0; - $ta = $avance->getTotalAvance()->ta ?? 0; - - // Totaux paiements initiaux - $t = $paymentData->total ?? 0; - $mv1 = $paymentData->total_mvola1 ?? 0; - $mv2 = $paymentData->total_mvola2 ?? 0; - $es1 = $paymentData->total_espece1 ?? 0; - $es2 = $paymentData->total_espece2 ?? 0; - $vb1 = $paymentData->total_virement_bancaire1 ?? 0; - $vb2 = $paymentData->total_virement_bancaire2 ?? 0; - - // Calculs - $total_mvola = $mv1 + $mv2; - $mv_calc = $total_mvola - $me - $mb + $bm; - - $total_espece = $es1 + $es2; - $es_calc = $total_espece + $me + $be - $total_sortie_caisse + $ta; - - $total_vb = $vb1 + $vb2; - $vb_calc = $total_vb - $be - $bm + $mb; - - // Retourne le tableau à utiliser + + // Totaux paiements Orders + $total_orders = $paymentDataOrders->total ?? 0; + $mv1_orders = $paymentDataOrders->total_mvola1 ?? 0; + $mv2_orders = $paymentDataOrders->total_mvola2 ?? 0; + $es1_orders = $paymentDataOrders->total_espece1 ?? 0; + $es2_orders = $paymentDataOrders->total_espece2 ?? 0; + $vb1_orders = $paymentDataOrders->total_virement_bancaire1 ?? 0; + $vb2_orders = $paymentDataOrders->total_virement_bancaire2 ?? 0; + + // Totaux paiements Avances + $total_avances = $paymentDataAvance->total ?? 0; + $mv_avances = $paymentDataAvance->total_mvola ?? 0; + $es_avances = $paymentDataAvance->total_espece ?? 0; + $vb_avances = $paymentDataAvance->total_virement_bancaire ?? 0; + $ta = $avance->getTotalAvance()->ta ?? 0; + + // Combinaison Orders + Avances + $total_mvola = $mv1_orders + $mv2_orders + $mv_avances; + $total_espece = $es1_orders + $es2_orders + $es_avances; + $total_vb = $vb1_orders + $vb2_orders + $vb_avances; + $total = $total_orders + $total_avances; + + // Ajustements avec recouvrements et sorties caisse + $total_mvola_final = $total_mvola - $me - $mb + $bm; + $total_espece_final = $es1_orders + $es2_orders + $es_avances - $total_sortie_caisse; + $total_virement_bancaire_final = $total_vb - $be - $bm + $mb; + return [ - 'total' => $t, - 'total_mvola' => $mv_calc, - 'total_espece' => $es_calc, - 'total_virement_bancaire' => $vb_calc, + 'total' => $total, + 'total_mvola' => $total_mvola_final, + 'total_espece' => $total_espece_final, + 'total_virement_bancaire'=> $total_virement_bancaire_final, 'page_title' => $this->pageTitle, - 'permission' => $this->permission + 'permission' => $this->permission ]; } + public function fetchRecouvrementData() { diff --git a/app/Models/Avance.php b/app/Models/Avance.php index ddd51373..dad3586d 100644 --- a/app/Models/Avance.php +++ b/app/Models/Avance.php @@ -11,7 +11,7 @@ class Avance extends Model { 'avance_amount', 'avance_date','user_id', 'customer_name', 'customer_address', 'customer_phone', 'customer_cin', 'gross_amount','amount_due','product_id','is_order','active','store_id', - 'type_avance', 'deadline' // Ajout du champ type et deadline + 'type_avance','type_payment', 'deadline' // Ajout du champ type et deadline ]; public function createAvance(array $data) { @@ -149,6 +149,34 @@ class Avance extends Model { } } } + public function getPaymentModesAvance() + { + $session = session(); + $users = $session->get('user'); + $isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']); + + if ($isAdmin) { + return $this->db->table('avances') + ->select(' + SUM(avance_amount) AS total, + SUM(CASE WHEN LOWER(type_payment) = "mvola" THEN avance_amount ELSE 0 END) AS total_mvola, + SUM(CASE WHEN LOWER(type_payment) = "en espèce" THEN avance_amount ELSE 0 END) AS total_espece, + SUM(CASE WHEN LOWER(type_payment) = "virement bancaire" THEN avance_amount ELSE 0 END) AS total_virement_bancaire + ') + ->where('active', 1) + ->get() + ->getRowObject(); + } else { + return (object) [ + 'total' => 0, + 'total_mvola' => 0, + 'total_espece' => 0, + 'total_virement_bancaire' => 0 + ]; + } + } + + public function getAllAvanceData1(int $id=null) { $session = session(); @@ -288,5 +316,51 @@ public function getAvancesNearDeadline($days = 3) ->where('DATE(avances.deadline)', $alertDate) ->findAll(); } +// Avances incomplètes (reste à payer > 0 et non transformées en commande) +public function getIncompleteAvances(int $id = null) +{ + $session = session(); + $users = $session->get('user'); + $isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']); + + $builder = $this->where('is_order', 0) + ->where('active', 1) + ->where('amount_due >', 0); + + if (!$isAdmin) { + $builder->where('store_id', $users['store_id']); + } + + if ($id) { + $builder->where('user_id', $id); + } + + return $builder->orderBy('avance_date', 'DESC')->findAll(); +} + +// Avances complètes (reste à payer = 0 et non transformées en commande) +public function getCompletedAvances(int $id = null) +{ + $session = session(); + $users = $session->get('user'); + $isAdmin = in_array($users['group_name'], ['Conseil', 'Direction']); + + $builder = $this->where('is_order', 0) + ->where('active', 1) + ->where('amount_due', 0); + + if (!$isAdmin) { + $builder->where('store_id', $users['store_id']); + } + + if ($id) { + $builder->where('user_id', $id); + } + + return $builder->orderBy('avance_date', 'DESC')->findAll(); +} + + + } diff --git a/app/Models/Products.php b/app/Models/Products.php index 4a4b9e56..6851160d 100644 --- a/app/Models/Products.php +++ b/app/Models/Products.php @@ -62,16 +62,33 @@ class Products extends Model return $this->where('is_piece', 0)->where('product_sold', 0)->where('store_id', $id)->orderBy('id', 'DESC')->findAll(); } - public function getProductDataStore(int $id) + public function getProductDataStore(int $store_id, bool $excludeAvance = true, int $currentProductId = null) { - return $this->where('is_piece', 0) - ->where('product_sold', 0) - ->where('availability', 1) // ✅ filtre ajouté - ->where("store_id", $id) - ->orderBy('id', 'DESC') - ->findAll(); + $builder = $this->where('is_piece', 0) + ->where('product_sold', 0) + ->where('availability', 1) + ->where('store_id', $store_id); + + if ($excludeAvance) { + $db = \Config\Database::connect(); + $subQuery = $db->table('avances') + ->select('product_id') + ->where('active', 1) + ->where('is_order', 0) + ->getCompiledSelect(); + + $builder->where("id NOT IN ($subQuery)", null, false); + } + + // Si on modifie et qu'on veut inclure le produit actuel dans la liste + if ($currentProductId) { + $builder->orWhere('id', $currentProductId); + } + + return $builder->orderBy('id', 'DESC')->findAll(); } + /** * Get active products (availability = 1) diff --git a/app/Views/avances/avance.php b/app/Views/avances/avance.php index 0c1465eb..6a546495 100644 --- a/app/Views/avances/avance.php +++ b/app/Views/avances/avance.php @@ -119,6 +119,15 @@ +
+ + +
@@ -152,7 +161,7 @@ @@ -187,7 +196,7 @@
- +