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);