From fa795feee4dcb442f3afc04ab6ac3a6d6664ec5a Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 12 Apr 2018 16:03:28 +0200 Subject: [PATCH] Kostenstellen can be retrieved by geschaeftsjahr (valid Kostenstellen), last Geschaeftsjahr can be retrieved --- .../models/accounting/Kostenstelle_model.php | 48 +++++++++++++++++++ .../organisation/Geschaeftsjahr_model.php | 16 +++++++ 2 files changed, 64 insertions(+) diff --git a/application/models/accounting/Kostenstelle_model.php b/application/models/accounting/Kostenstelle_model.php index 489286306..fd064125c 100644 --- a/application/models/accounting/Kostenstelle_model.php +++ b/application/models/accounting/Kostenstelle_model.php @@ -11,4 +11,52 @@ class Kostenstelle_model extends DB_Model $this->dbTable = 'wawi.tbl_kostenstelle'; $this->pk = 'kostenstelle_id'; } + + /** + * Gets all active Kostenstellen for a geschaeftsjahr, as determined by the geschaeftsjahrvon and bis fields + * @param $geschaeftsjahr + * @return array|null + */ + public function getActiveKostenstellenForGeschaeftsjahr($geschaeftsjahr = null) + { + $this->load->model('organisation/geschaeftsjahr_model', 'GeschaeftsjahrModel'); + + if ($geschaeftsjahr === null) + { + $lgj = $this->GeschaeftsjahrModel->getLastGeschaeftsjahr(); + + if ($lgj->error) + return error($lgj->retval); + + if (count($lgj->retval) < 1) + return success(array()); + + $geschaeftsjahr = $lgj->retval[0]->geschaeftsjahr_kurzbz; + } + + $this->GeschaeftsjahrModel->addSelect('start, ende'); + $gj = $this->GeschaeftsjahrModel->load($geschaeftsjahr); + + if ($gj->error) + return error($gj->retval); + + if (count($gj->retval) < 1) + return success(array()); + + $gjstart = $gj->retval[0]->start; + + $query = 'SELECT kostenstelle_id, kostenstelle_nr, kurzbz, wawi.tbl_kostenstelle.bezeichnung, kgjvon.start, kgjbis.ende + FROM wawi.tbl_kostenstelle + LEFT JOIN public.tbl_geschaeftsjahr kgjvon on wawi.tbl_kostenstelle.geschaeftsjahrvon = kgjvon.geschaeftsjahr_kurzbz + LEFT JOIN public.tbl_geschaeftsjahr kgjbis on wawi.tbl_kostenstelle.geschaeftsjahrbis = kgjbis.geschaeftsjahr_kurzbz + WHERE + (wawi.tbl_kostenstelle.geschaeftsjahrbis IS NULL AND wawi.tbl_kostenstelle.geschaeftsjahrvon IS NULL) + OR + (DATE ? >= kgjvon.start AND (DATE ? < kgjbis.ende OR wawi.tbl_kostenstelle.geschaeftsjahrbis IS NULL)) + OR + (DATE ? < kgjbis.ende AND (DATE ? >= kgjvon.start OR wawi.tbl_kostenstelle.geschaeftsjahrvon IS NULL)) + ORDER BY wawi.tbl_kostenstelle.bezeichnung'; + + return $this->execQuery($query, array($gjstart, $gjstart, $gjstart, $gjstart)); + } } diff --git a/application/models/organisation/Geschaeftsjahr_model.php b/application/models/organisation/Geschaeftsjahr_model.php index e3c0e9fb2..d475b8142 100644 --- a/application/models/organisation/Geschaeftsjahr_model.php +++ b/application/models/organisation/Geschaeftsjahr_model.php @@ -11,4 +11,20 @@ class Geschaeftsjahr_model extends DB_Model $this->dbTable = 'public.tbl_geschaeftsjahr'; $this->pk = 'geschaeftsjahr_kurzbz'; } + + /** + * Gets latest Geschaeftsjahr, as determined by its start date + * @return array|null + */ + public function getLastGeschaeftsjahr() + { + $query = 'SELECT * + FROM public.tbl_geschaeftsjahr + WHERE start = ( + SELECT max(start) + FROM public.tbl_geschaeftsjahr + )'; + + return $this->execQuery($query); + } }