From 563d5bf2de1f4245d370afdd5dc196db73ebcdfc Mon Sep 17 00:00:00 2001 From: Tiavina Date: Fri, 1 Aug 2025 13:54:18 +0300 Subject: [PATCH 1/2] fix trajet hfc --- gestion/lib/Controller/PageController.php | 56 ++++++++++++++ gestion/lib/Db/Bdd.php | 92 ++++++++++++++++++++++- gestion/lib/Service/GestionService.php | 19 ++++- 3 files changed, 162 insertions(+), 5 deletions(-) diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index b997df2..cb4b48d 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -5,6 +5,7 @@ use \FPDF; use \Datetime; use Exception; use OCA\Gestion\Constants\DevisExportTypeConstant; +use OCA\Gestion\Helpers\DateHelpers; use OCP\IConfig; use OCP\IRequest; defined("TAB1") or define("TAB1", "\t"); @@ -3164,4 +3165,59 @@ class PageController extends Controller { 'logo' => $this->getLogo() )); } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @param int $defuntId + */ + + public function updateDevisLigneTrajet($idNextcloud ,$mois , $annee){ + //get all devis id from month and year + if(!$idNextcloud || !$mois || !$mois){ + return null; + } + + $idsNextcloud = explode(',', $idNextcloud); + foreach($idsNextcloud as $id){ + $this->updateLigneTrajet($id, $mois, $annee); + } + return "update successfully"; + + } + public function updateLigneTrajet ($idNextcloud ,$mois , $annee){ + $all_devis = $this->myDb->getAllDevisByMonthAndYear($idNextcloud,$mois,$annee); + + foreach($all_devis as $devis){ + + $ligne_trajet = json_decode($this->myDb->getOneTrajetdetails_byIdDevis($devis['id'], $idNextcloud))[0]; + + + if($ligne_trajet != NULL) { + $this->myDb->gestion_delete('ligne_trajet', $ligne_trajet->id, $idNextcloud); + $ligne_trajet = NULL; + } + $devisObject = json_decode($this->myDb->getOneDevis($devis['id'], $idNextcloud))[0]; + + $this->myDb->generate_ligneTrajet($devisObject, $idNextcloud); + $ligne_trajet = json_decode( json: $this->myDb->getOneTrajetdetails_byIdDevis($devisObject->devisid, $idNextcloud))[0]; + $this->myDb->range_ligneTrajet($ligne_trajet->id_trajet, $idNextcloud); + $this->myDb->calculer_distance_trajet(numtrajet: $ligne_trajet->id_trajet, idNextcloud: $idNextcloud); + } + + $this->deleteUnusedLigneTrajet($idNextcloud ,$mois , $annee); + return true; + } + + public function deleteUnusedLigneTrajet($idNextcloud ,$mois , $annee){ + $dateOfMonths = DateHelpers::getDatesOfMonth($annee,$mois); + + foreach($dateOfMonths as $currentDate){ + $currentDateFormatted = $currentDate->format('Y-m-d'); + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->myDb->thereIsThanatoDevisRattachedToLigneTrajetForADate($currentDateFormatted, $idNextcloud ); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + $this->myDb->deleteThanatoLigneTrajetForADateWithoutDevis($currentDateFormatted, $idNextcloud ); + } + } + } } diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index d0c347c..b48eb18 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -1773,6 +1773,18 @@ class Bdd { if($ligne_trajet != NULL) { $this->gestion_delete('ligne_trajet', $ligne_trajet->id, $idNextcloud); + + $oldThanatoId = $devis->id_thanato; + $oldThanato = $this->getThanatoByThanatoId($oldThanatoId); + // Supprimer les lignes trajet qui n' ont pas devis sur cette date + if($oldThanato != NULL) { + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->thereIsThanatoDevisRattachedToLigneTrajetForADate($updated_devis->date, $oldThanato['fk_user_uuid'] ); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + //delete all ligne trajet for this date + $this->deleteThanatoLigneTrajetForADate($devis->date, $oldThanato['fk_user_uuid'] ); + } + } + } $this->generate_ligneTrajet($updated_devis, $idNextcloud); $ligne_trajet = json_decode($this->getOneTrajetdetails_byIdDevis($updated_devis->devisid, $idNextcloud))[0]; @@ -1994,7 +2006,13 @@ class Bdd { $this->range_ligneTrajet($ligne_trajet->id_trajet, $idNextcloud); $this->calculer_distance_trajet($ligne_trajet->id_trajet, $idNextcloud); - } else { + }else if(strcmp($table, "trajet")==0) { + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE `id_trajet` = ?"; + $this->execSQLNoData($sql, array($id)); + $sql = "DELETE FROM ".$this->tableprefix.$table." WHERE `id` = ?"; + $this->execSQLNoData($sql, array($id)); + + }else { $sql = "DELETE FROM ".$this->tableprefix.$table." WHERE `id` = ?"; $this->execSQLNoData($sql, array($id)); } @@ -5193,4 +5211,76 @@ COMMENTAIRES: ".$comment; } return null; } + + public function getThanatoByThanatoId($thanatoId){ + $sql = "SELECT * + FROM ".$this->tableprefix."thanato as thanato + WHERE thanato.id = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($thanatoId)); + if($res){ + return $res[0]; + } + return null; + + } + + public function deleteLigneTrajetByDevisId($devisId){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE id_devis = ?;"; + $this->execSQLNoData($sql, array($devisId)); + } + + public function thereIsThanatoDevisRattachedToLigneTrajetForADate($date,$idNextcloud){ + $sql = "SELECT * FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ? AND id_devis != 0 AND id_devis != NULL AND id_devis != ?;"; + $ligneTrajet = $this->execSQLNoJsonReturn($sql, array($date,$idNextcloud,"-")); + if(!empty($ligneTrajet)){ + return true; + } + return false; + } + + public function deleteThanatoLigneTrajetForADate($date,$thanatoUserUuid){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ?;"; + $this->execSQLNoData($sql, array($date,$thanatoUserUuid)); + } + + public function getThanatoByUserUuid($userUuid){ + $sql = "SELECT * + FROM ".$this->tableprefix."thanato as thanato + LEFT JOIN ".$this->tableprefix."thanato_type as thanato_type + ON thanato.fk_thanato_type_key = thanato_type.thanato_type_key + WHERE thanato.fk_user_uuid = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($userUuid)); + if($res){ + return $res[0]; + } + return null; + } + + public function reArrangeLigneTrajetyByDevisId($devisId,$idNextcloud){ + $ligne_trajet = json_decode($this->getOneTrajetdetails_byIdDevis($devisId, $idNextcloud))[0]; + if($ligne_trajet != NULL) { + $this->gestion_delete('ligne_trajet', $ligne_trajet->id, $idNextcloud); + $ligne_trajet = NULL; + } + $devisObject = json_decode($this->getOneDevis($devisId, $idNextcloud))[0]; + + $this->generate_ligneTrajet($devisObject, $idNextcloud); + $ligne_trajet = json_decode( json: $this->getOneTrajetdetails_byIdDevis($devisObject->devisid, $idNextcloud))[0]; + $this->range_ligneTrajet($ligne_trajet->id_trajet, $idNextcloud); + $this->calculer_distance_trajet(numtrajet: $ligne_trajet->id_trajet, idNextcloud: $idNextcloud); + } + + public function getAllDevisByMonthAndYear($idNextcloud,$mois, $annee ){ + $thanato = $this->getThanatoByUserUuid($idNextcloud); + if($thanato == null){ + return array(); + } + $sql = "SELECT * FROM {$this->tableprefix}devis WHERE MONTH(date) = ? AND YEAR(date) = ? AND id_thanato = ?;"; + return $this->execSQLNoJsonReturn($sql,array( $mois , $annee, $thanato['id'] )); + } + + public function deleteThanatoLigneTrajetForADateWithoutDevis($date,$thanatoUserUuid){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ? AND id_devis = NULL;"; + $this->execSQLNoData($sql, array($date,$thanatoUserUuid)); + } } diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php index 7bdfd1a..59e7b5a 100644 --- a/gestion/lib/Service/GestionService.php +++ b/gestion/lib/Service/GestionService.php @@ -280,11 +280,22 @@ class GestionService if ($devis != null) { $userPrincipalName = $this->gestionBdd->getCalendarPrincipalNameByCalendarId($targetCalendarId); if ($userPrincipalName != null) { - $thanatoId = $this->gestionBdd->getThanatoIdByUserUuid($userPrincipalName); - if ($thanatoId != null) { - $thanatoHasBeenChanged = $thanatoId != $devis["id_thanato"]; + $thanato = $this->gestionBdd->getThanatoByUserUuid($userPrincipalName); + if ($thanato != null) { + $thanatoHasBeenChanged = $thanato['id'] != $devis["id_thanato"]; if ($thanatoHasBeenChanged) { - $this->gestionBdd->updateDevisThanato($devis['id'], $thanatoId); + $oldThanato = $this->gestionBdd->getThanatoByThanatoId($devis['id_thanato']); + $oldThanatoUserUuid = $oldThanato['fk_user_uuid']; + //delete ligne trajet from old devis + $this->gestionBdd->deleteLigneTrajetByDevisId($devis['id']); + //delete from home and to home trajet if there is no devis at all for this date + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'], $oldThanatoUserUuid ); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + //delete all ligne trajet for this date + $this->gestionBdd->deleteThanatoLigneTrajetForADate($devis['date'], $oldThanatoUserUuid ); + } + $this->gestionBdd->updateDevisThanato($devis['id'], $thanato['id']); + $this->gestionBdd->reArrangeLigneTrajetyByDevisId($devis['id'], $userPrincipalName); } } } From d8ff187d087e0f67b483164d28cae22a00020794 Mon Sep 17 00:00:00 2001 From: narindraezway Date: Fri, 1 Aug 2025 16:37:10 +0300 Subject: [PATCH 2/2] HFC : fix par trajet rearrange ligne trajet --- gestion/appinfo/routes.php | 3 ++ gestion/lib/Controller/PageController.php | 23 ++++++-- gestion/lib/Db/Bdd.php | 66 ++++++++++++++++++----- gestion/lib/Service/GestionService.php | 16 ++++-- 4 files changed, 87 insertions(+), 21 deletions(-) diff --git a/gestion/appinfo/routes.php b/gestion/appinfo/routes.php index a84cfea..7095824 100644 --- a/gestion/appinfo/routes.php +++ b/gestion/appinfo/routes.php @@ -181,5 +181,8 @@ return [ //invoice controller ['name' => 'invoice#sendInvoicePdfViaMail', 'url' => '/invoice/{factureId}/sendInvoicePdfViaMail', 'verb' => 'POST'], ['name' => 'invoice#getInvoicePdfContent', 'url' => '/invoice/{factureId}/getInvoicePdfContent', 'verb' => 'GET'], + + + ['name' => 'page#updateDevisLigneTrajet', 'url' => '/updateDevisLigneTrajet', 'verb' => 'POST'], ] ]; diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index cb4b48d..184c9b7 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -3211,13 +3211,30 @@ class PageController extends Controller { public function deleteUnusedLigneTrajet($idNextcloud ,$mois , $annee){ $dateOfMonths = DateHelpers::getDatesOfMonth($annee,$mois); - + $thanato = $this->myDb->getThanatoByUserUuid($idNextcloud); + $trajet = $this->myDb->getTrajetByThanatoAndMonthYear($thanato['id'], $mois, $annee); + if($trajet == null){ + return; + } foreach($dateOfMonths as $currentDate){ $currentDateFormatted = $currentDate->format('Y-m-d'); - $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->myDb->thereIsThanatoDevisRattachedToLigneTrajetForADate($currentDateFormatted, $idNextcloud ); + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->myDb->thereIsThanatoDevisRattachedToLigneTrajetForADate($currentDateFormatted, $trajet['id']); if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ - $this->myDb->deleteThanatoLigneTrajetForADateWithoutDevis($currentDateFormatted, $idNextcloud ); + $this->myDb->deleteThanatoLigneTrajetForADateWithoutDevisByTrajetId($currentDateFormatted, $trajet['id'] ); + }else{ + $devisRelatedToLigneTrajetButNotRelatedToCurrentThanato = $this->myDb->getLigneTrajetsWithDevisNotRelatedToThanato($currentDateFormatted,$thanato['id'],$trajet['id']); + if($devisRelatedToLigneTrajetButNotRelatedToCurrentThanato != null && count($devisRelatedToLigneTrajetButNotRelatedToCurrentThanato) > 0){ + foreach($devisRelatedToLigneTrajetButNotRelatedToCurrentThanato as $currentLigneTrajet){ + $this->myDb->gestion_delete('ligne_trajet', $currentLigneTrajet['id'], $idNextcloud); + } + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->myDb->thereIsThanatoDevisRattachedToLigneTrajetForADate($currentDateFormatted, $trajet['id']); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + $this->myDb->deleteThanatoLigneTrajetForADateWithoutDevisByTrajetId($currentDateFormatted, $trajet['id'] ); + } + } + } } + } } diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index b48eb18..af6501a 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -1778,10 +1778,15 @@ class Bdd { $oldThanato = $this->getThanatoByThanatoId($oldThanatoId); // Supprimer les lignes trajet qui n' ont pas devis sur cette date if($oldThanato != NULL) { - $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->thereIsThanatoDevisRattachedToLigneTrajetForADate($updated_devis->date, $oldThanato['fk_user_uuid'] ); - if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ - //delete all ligne trajet for this date - $this->deleteThanatoLigneTrajetForADate($devis->date, $oldThanato['fk_user_uuid'] ); + $month = explode('-', $updated_devis->date)[1]; + $year = explode('-', $updated_devis->date)[0]; + $trajet = $this->getTrajetByThanatoAndMonthYear($oldThanatoId,$month,$year); + if($trajet != NULL) { + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->thereIsThanatoDevisRattachedToLigneTrajetForADate($updated_devis->date, $trajet['id'] ); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + //delete all ligne trajet for this date + $this->deleteThanatoLigneTrajetForADateByIdTrajet($devis->date, $trajet['id'] ); + } } } @@ -5229,26 +5234,45 @@ COMMENTAIRES: ".$comment; $this->execSQLNoData($sql, array($devisId)); } - public function thereIsThanatoDevisRattachedToLigneTrajetForADate($date,$idNextcloud){ - $sql = "SELECT * FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ? AND id_devis != 0 AND id_devis != NULL AND id_devis != ?;"; - $ligneTrajet = $this->execSQLNoJsonReturn($sql, array($date,$idNextcloud,"-")); + public function thereIsThanatoDevisRattachedToLigneTrajetForADate($date,$trajetId){ + $sql = " + SELECT ligne_trajet.* + FROM ".$this->tableprefix."ligne_trajet as ligne_trajet + WHERE ligne_trajet.date = ? AND ligne_trajet.id_devis != 0 AND ligne_trajet.id_devis IS NOT NULL AND ligne_trajet.id_devis != ? + AND ligne_trajet.id_trajet = ?;"; + $ligneTrajet = $this->execSQLNoJsonReturn($sql, array($date,"-",$trajetId)); if(!empty($ligneTrajet)){ return true; } return false; } + public function getLigneTrajetsWithDevisNotRelatedToThanato($date,$idThanato,$trajetId){ + $sql = " + SELECT ligne_trajet.id as id FROM ".$this->tableprefix."ligne_trajet as ligne_trajet + LEFT JOIN ".$this->tableprefix."devis as devis ON devis.id = ligne_trajet.id_devis + WHERE ligne_trajet.date = ? AND ligne_trajet.id_devis != 0 AND ligne_trajet.id_devis IS NOT NULL AND ligne_trajet.id_devis != ? + AND ligne_trajet.id_trajet = ? + AND devis.id_thanato != ?; + "; + $ligneTrajets = $this->execSQLNoJsonReturn($sql, array($date,"-",$trajetId,$idThanato)); + if(!empty($ligneTrajets)){ + return $ligneTrajets; + } + return []; + } public function deleteThanatoLigneTrajetForADate($date,$thanatoUserUuid){ $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ?;"; $this->execSQLNoData($sql, array($date,$thanatoUserUuid)); } + public function deleteThanatoLigneTrajetForADateByIdTrajet($date,$idTrajet){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_trajet = ?;"; + $this->execSQLNoData($sql, array($date,$idTrajet)); + } + public function getThanatoByUserUuid($userUuid){ - $sql = "SELECT * - FROM ".$this->tableprefix."thanato as thanato - LEFT JOIN ".$this->tableprefix."thanato_type as thanato_type - ON thanato.fk_thanato_type_key = thanato_type.thanato_type_key - WHERE thanato.fk_user_uuid = ?"; + $sql = "SELECT * FROM ".$this->tableprefix."thanato as thanato WHERE thanato.fk_user_uuid = ?"; $res = $this->execSQLNoJsonReturn($sql,array($userUuid)); if($res){ return $res[0]; @@ -5280,7 +5304,21 @@ COMMENTAIRES: ".$comment; } public function deleteThanatoLigneTrajetForADateWithoutDevis($date,$thanatoUserUuid){ - $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ? AND id_devis = NULL;"; - $this->execSQLNoData($sql, array($date,$thanatoUserUuid)); + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_nextcloud = ? AND (id_devis = NULL or id_devis = 0 or id_devis = ?) ;"; + $this->execSQLNoData($sql, array($date,$thanatoUserUuid,"-")); + } + + public function deleteThanatoLigneTrajetForADateWithoutDevisByTrajetId($date,$trajetId){ + $sql = "DELETE FROM ".$this->tableprefix."ligne_trajet WHERE date = ? AND id_trajet = ? AND (id_devis = NULL or id_devis = 0 or id_devis = ?) ;"; + $this->execSQLNoData($sql, array($date,$trajetId,"-")); + } + + public function getTrajetByThanatoAndMonthYear($thanatoId, $month, $year){ + $sql = "SELECT * FROM ".$this->tableprefix."trajet WHERE mois = ? AND annee = ? AND id_thanato = ?;"; + $result = $this->execSQLNoJsonReturn($sql, array($month, $year, $thanatoId)); + if(!empty($result)){ + return $result[0]; + } + return null; } } diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php index 59e7b5a..79c9521 100644 --- a/gestion/lib/Service/GestionService.php +++ b/gestion/lib/Service/GestionService.php @@ -289,10 +289,18 @@ class GestionService //delete ligne trajet from old devis $this->gestionBdd->deleteLigneTrajetByDevisId($devis['id']); //delete from home and to home trajet if there is no devis at all for this date - $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'], $oldThanatoUserUuid ); - if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ - //delete all ligne trajet for this date - $this->gestionBdd->deleteThanatoLigneTrajetForADate($devis['date'], $oldThanatoUserUuid ); + $month = explode('-', $devis['date'])[1]; + $year = explode('-', $devis['date'])[0]; + $oldThanatoTrajet = $this->gestionBdd->getTrajetByThanatoAndMonthYear( + $devis['id_thanato'], + $month, + $year + ); + if($oldThanatoTrajet != null){ + $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'], $oldThanatoTrajet['id'] ); + if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate){ + $this->gestionBdd->deleteThanatoLigneTrajetForADateByIdTrajet($devis['date'], $oldThanatoTrajet['id'] ); + } } $this->gestionBdd->updateDevisThanato($devis['id'], $thanato['id']); $this->gestionBdd->reArrangeLigneTrajetyByDevisId($devis['id'], $userPrincipalName);