diff --git a/application/controllers/api/frontend/v1/Ort.php b/application/controllers/api/frontend/v1/Ort.php index 0d8074fa9..570b8e785 100644 --- a/application/controllers/api/frontend/v1/Ort.php +++ b/application/controllers/api/frontend/v1/Ort.php @@ -36,7 +36,9 @@ class Ort extends FHCAPI_Controller 'ContentID' => self::PERM_LOGGED, 'getOrtKurzbzContent' => self::PERM_LOGGED, 'getRooms' => self::PERM_LOGGED, - 'getTypes' => self::PERM_LOGGED + 'getTypes' => self::PERM_LOGGED, + 'getRoomsWithEmployeesAssigned' => 'basis/ort:r', + 'getEmployeesWithRoomAssigned' => 'basis/ort:r' ]); $this->load->model('ressource/Ort_model', 'OrtModel'); @@ -174,5 +176,42 @@ class Ort extends FHCAPI_Controller $this->terminateWithSuccess($content); } + + public function getRoomsWithEmployeesAssigned($ort_kurzbz=null) + { + $res = $this->OrtModel->getRoomsWithEmployeesAssigned($ort_kurzbz); + + if (isError($res)) + { + $this->terminateWithError(getError($res)); + } + + $data = hasData($res) ? getData($res) : []; + $this->json_decode_db_res($data); + $this->terminateWithSuccess($data); + } + + public function getEmployeesWithRoomAssigned($mitarbeiter_uid=null) + { + $res = $this->OrtModel->getEmployeesWithRoomAssigned($mitarbeiter_uid); + + if (isError($res)) + { + $this->terminateWithError(getError($res)); + } + + $data = hasData($res) ? getData($res) : []; + $this->json_decode_db_res($data); + $this->terminateWithSuccess($data); + } + + protected function json_decode_db_res(&$data) + { + array_walk($data, function($item, $key) { + isset($item->employees) && $item->employees = json_decode($item->employees); + isset($item->employee) && $item->employee = json_decode($item->employee); + isset($item->room) && $item->room = json_decode($item->room); + }); + } } diff --git a/application/models/ressource/Ort_model.php b/application/models/ressource/Ort_model.php index de0c8e331..82802c84a 100644 --- a/application/models/ressource/Ort_model.php +++ b/application/models/ressource/Ort_model.php @@ -32,4 +32,124 @@ class Ort_model extends DB_Model } + public function getRoomsWithEmployeesAssigned($ort_kurzbz=null) + { + $ort_kurzbz_clause = is_null($ort_kurzbz) + ? '' + : 'and r.ort_kurzbz = ' . $this->escape($ort_kurzbz); + $sql = <<roomEmployeesCTEs()} + + select + r.rauminfo as room, + mir.ma_count as employee_count, + mir.mas_in_room as employees + from + roominfo r + join + mas_in_room mir on r.ort_kurzbz = mir.ort_kurzbz + where + 1=1 + {$ort_kurzbz_clause} + order by + mir.ma_count DESC +EOSQL; + + return $this->execReadOnlyQuery($sql); + } + + public function getEmployeesWithRoomAssigned($mitarbeiter_uid=null) + { + $mtarbeiter_uid_clause = is_null($mitarbeiter_uid) + ? '' + : 'and aer.mitarbeiter_uid = ' . $this->escape($mitarbeiter_uid); + $sql = <<roomEmployeesCTEs()} + + select + m.mainfo as employee, + r.rauminfo as room + from + active_employee_room aer + join + roominfo r on aer.ort_kurzbz = r.ort_kurzbz + join + mainfo m on aer.mitarbeiter_uid = m.mitarbeiter_uid + where + 1=1 + {$mtarbeiter_uid_clause} +EOSQL; + + return $this->execReadOnlyQuery($sql); + } + + protected function roomEmployeesCTEs() + { + return <<