From 93ecc89ebef28a314a7563e9ecf3085e514a9e74 Mon Sep 17 00:00:00 2001 From: Cris Date: Thu, 6 Aug 2020 10:32:45 +0200 Subject: [PATCH] Added method getPhones_byPerson to Kontakt model. Gets all latest phones of person where zustellung is true. Ordered by telefon > mobil > firmenhandy > else. Signed-off-by: Cris --- application/models/person/Kontakt_model.php | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/application/models/person/Kontakt_model.php b/application/models/person/Kontakt_model.php index b8ad67509..e007b81fb 100644 --- a/application/models/person/Kontakt_model.php +++ b/application/models/person/Kontakt_model.php @@ -77,4 +77,36 @@ class Kontakt_model extends DB_Model 'person_id' => $person_id )); } + + /** + * Get all latest phones of person where zustellung is true. Ordered by + * telefon > mobil > firmenhandy > else. + * @param string person_id + */ + public function getPhones_byPerson($person_id) + { + $qry = ' + WITH latest_phones AS( + SELECT DISTINCT ON (kontakttyp) kontakttyp, kontakt + FROM public.tbl_kontakt kontakt + LEFT JOIN public.tbl_standort USING (standort_id) + LEFT JOIN public.tbl_firma USING (firma_id) + WHERE person_id = ? + AND zustellung + AND kontakttyp IN (\'telefon\', \'mobil\', \'firmenhandy\') + ORDER BY kontakttyp, kontakt, kontakt.updateamum + ) + + SELECT * FROM latest_phones + ORDER BY + CASE + WHEN kontakttyp = \'telefon\' THEN 0 + WHEN kontakttyp = \'mobil\' THEN 1 + WHEN kontakttyp = \'firmenhandy\' THEN 2 + ELSE 3 + END + '; + + return $this->execQuery($qry, array($person_id)); + } }