loadMe(); $data["edit_by"] = $me->id; } return $data; } protected function beforeDelete() { if(!$this->id) return true; // delete owners foreach($this->getProperty("owners") as $owner) { $owner->delete(); } // delete contacts foreach($this->getProperty("contacts") as $contact) { $contact->delete(); } // delete journals foreach($this->getProperty("journal") as $journal) { $journal->delete(); } // delete history foreach($this->getProperty("history") as $history) { $history->delete(); } // delete files if($this->getProperty("file")) { $this->getProperty("file")->delete(); } return true; } protected function afterSave() { $this->createHistory(); } private function createHistory() { if(!$this->id) return true; $changed = $this->getChangedFields(); try { foreach($changed as $field) { $this->log->debug(__METHOD__ . ": $field changed from '" . $this->_old_data->$field . "' to '" . $this->data->$field . "'"); $history = ConstructionConsentHistory::create([ "constructionconsent_id" => $this->id, "key" => $field, "old_value" => $this->_old_data->$field, "new_value" => $this->data->$field ]); $history->save(); } } catch(Exception $e) { $this->log->debug($e->getTraceAsString()); } return true; } public function createConsentFormPdf(array $owners): ?string { if (empty($owners)) { return null; } $pdf = new PdfForm("ConstructionConsent/Consentform.pdf", [ 'consent' => $this, 'owners' => $owners ]); $footerFile = BASEDIR . "/public/assets/pdf/ConstructionConsent/Consentform.footer.html"; $wkOpts = sprintf( "--footer-html '%s' --margin-bottom 15mm --margin-top 10mm --margin-bottom 15mm --margin-top 10mm", $footerFile ); $filename = $pdf->render($wkOpts); return @is_file($filename) ? $filename : null; } public function getProperty($name) { if($this->$name == null && $name !== 'netzgebiet_id') { if($name == "journal") { $journal = ConstructionConsentJournal::search(["constructionconsent_id" => $this->id], false, "id DESC"); if($journal) { $this->journal = $journal; } return $journal; } if($name == "history") { $history = ConstructionConsentHistory::search(["constructionconsent_id" => $this->id], false, "id DESC"); if($history) { $this->history = $history; } return $history; } if($name == "project") { $project = new ConstructionConsentProject($this->constructionconsentproject_id); if($project->id) { $this->project = $project; } return $this->project; } if($name == 'netzgebiet') { if(!$this->data->netzgebiet_id) return null; $netzgebiet = new ADBNetzgebiet($this->data->netzgebiet_id); if($netzgebiet->id) { $this->netzgebiet = $netzgebiet; } return $this->netzgebiet; } if($name == "adb_hausnummer") { if(!$this->adb_hausnummer_id) return null; $hausnummer = new ADBHausnummer($this->adb_hausnummer_id); if($hausnummer->id) { $this->adb_hausnummer = $hausnummer; } return $this->adb_hausnummer; } if($name == "adb_strasse") { if(!$this->adb_strasse_id) return null; $strasse = new ADBStrasse($this->adb_strasse_id); if($strasse->id) { $this->adb_strasse = $strasse; } return $this->adb_strasse; } if($name == "preorder_count") { if(!$this->adb_hausnummer_id) { return 0; } $preorder_count = 0; foreach(PreorderModel::searchActive(["adb_hausnummer_id" => $this->adb_hausnummer_id]) as $preorder) { if($preorder->connection_count) { $preorder_count += $preorder->connection_count; } else { $preorder_count++; } } if($preorder_count) { $this->preorder_count = $preorder_count; } return $this->preorder_count; } if($name == "owners") { if(!$this->id) return []; $owners = ConstructionConsentOwner::search(["constructionconsent_id" => $this->id]); if(count($owners)) { $this->owners = $owners; } return $this->owners; } if($name == "contacts") { if(!$this->id) return []; $contacts = []; foreach(ConstructionConsentContact::search(["constructionconsent_id" => $this->id]) as $contact) { if(!array_key_exists($contact->type, $contacts)) { $contacts[$contact->type] = []; } $contacts[$contact->type][] = $contact; } if(count($contacts)) { $this->contacts = $contacts; } return $contacts; } if($name == "file") { if(!$this->id) return null; $file = ConstructionConsentFile::getFirst(["constructionconsent_id" => $this->id]); if($file) { $this->file = $file; } return $this->file; } if($name == "inspection_protocol_planner") { if(!$this->id) return []; $file = ConstructionConsentFile::getFirst(["constructionconsent_id" => $this->id, "filename" => "inspection_protocol_planner"]); if($file) { $this->inspection_protocol_planner = $file; } return $this->inspection_protocol_planner; } if($name == "inspection_protocol_electrician") { if(!$this->id) return []; $file = ConstructionConsentFile::getFirst(["constructionconsent_id" => $this->id, "filename" => "inspection_protocol_electrician"]); if($file) { $this->inspection_protocol_electrician = $file; } return $this->inspection_protocol_electrician; } if($name == "owner_result_counts") { $counts = []; $owners = $this->getProperty("owners"); if (!isset($owners) || !is_array($owners)) { return []; } foreach($owners as $owner) { if($owner->result == "open" && $owner->status == "new") { $owner->result = "new"; } if(!array_key_exists($owner->result, $counts)) { $counts[$owner->result] = 0; } $counts[$owner->result]++; } $this->owner_result_counts = $counts; return $this->owner_result_counts; } if($name == "owner_status_counts") { $counts = []; $owners = $this->getProperty("owners"); foreach($owners as $owner) { if(!array_key_exists($owner->status, $counts)) { $counts[$owner->status] = 0; } $counts[$owner->status]++; } $this->owner_status_counts = $counts; return $this->owner_status_counts; } if($name == "creator") { $this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by); if($this->creator === null) { $this->creator = new User($this->create_by); if($this->creator->id) { mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator); } } return $this->creator; } if($name == "editor") { $this->editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by); if($this->editor === null) { $this->editor = new User($this->edit_by); if($this->editor->id) { mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor); } } return $this->editor; } $classname = ucfirst($name); $idfield = $name."_id"; $this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield); if(!$this->$name) { $this->$name = new $classname($this->$idfield); } if($this->$name->id) { mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name); return $this->$name; } else { return null; } } return $this->$name; } /******************************** * Begin static Model functions */ public static function create(Array $data) { $model = new ConstructionConsent(); $table_fields = [ "constructionconsentproject_id", "termination_id","adb_hausnummer_id", "adb_strasse_id", "object_type", "name", "ez", "kg", "gst", "gstnr", "rimo_gn", "usage_length", "usage_pipe_on_plot", "usage_pipe_in_building", "usage_manhole", "usage_owner", "status", "result", "result_text", "note", "create_by","edit_by","create","edit" ]; foreach($data as $field => $value) { if(in_array($field, $table_fields)) { $model->$field = $value; } } $me = new User(); $me->loadMe(); if($model->create_by === null) { $model->create_by = $me->id; } if($model->edit_by === null) { $model->edit_by = $me->id; } return $model; } public static function getAll() { $items = []; $db = FronkDB::singleton(); $res = $db->select("ConstructionConsent", "*", "1 = 1 ORDER BY adb_hausnummer_id"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new ConstructionConsent($data); } } return $items; } public static function getFirst($filter) { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT * FROM ConstructionConsent WHERE $where ORDER BY adb_hausnummer_id LIMIT 1"; //var_dump($sql);exit; $res = $db->query($sql); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new ConstructionConsent($data); if($item->id) { return $item; } else { return null; } } return null; } public static function count($filter) { return self::search($filter, false, false, true); } public static function search($filter, $limit = false, $order = false, $returnCount = false) { $items = []; if(!$order) { $order = "vh.strasse, CAST(vh.hausnummer AS UNSIGNED) ASC"; } $having = ''; if(array_key_exists("status_light", $filter)) { if (is_array($filter['status_light']) && count($filter['status_light']) > 0) { $statusLights = array_map(function($item) { return "'" . addslashes(trim($item)) . "'"; }, $filter['status_light']); $having = " HAVING status_light IN (" . implode(",", $statusLights) . ")"; } elseif (strlen(trim($filter['status_light'])) > 2) { $having = " HAVING status_light = '" . addslashes(trim($filter['status_light'])) . "'"; } } $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT ConstructionConsent.*, COALESCE(SUM(CASE WHEN ConstructionConsent.approve_override = 1 THEN 0 ELSE (cwo.result = 'denied') END), 0) AS denied_count, COALESCE(SUM(CASE WHEN ConstructionConsent.approve_override = 1 THEN 0 ELSE (cwo.result = 'unresolvable') END), 0) AS unresolvable_count, COALESCE(SUM(CASE WHEN ConstructionConsent.approve_override = 1 THEN 0 ELSE (cwo.result = 'moved') END), 0) AS moved_count, COALESCE(SUM(CASE WHEN ConstructionConsent.approve_override = 1 THEN 1 ELSE (cwo.result = 'accepted') END), 0) AS accepted_count, COUNT(cwo.id) AS total_owners, CASE WHEN ConstructionConsent.approve_override = 1 THEN 'green' WHEN COALESCE(SUM(CASE WHEN approve_override = 1 THEN 0 ELSE (cwo.result = 'open' AND cwo.status = 'new') END), 0) > 0 THEN 'blue' WHEN COALESCE(SUM(CASE WHEN approve_override = 1 THEN 0 ELSE (cwo.result = 'denied') END), 0) > 0 THEN 'red' WHEN COALESCE(SUM(CASE WHEN approve_override = 1 THEN 0 ELSE (cwo.result = 'unresolvable') END), 0) > 0 OR COALESCE(SUM(CASE WHEN approve_override = 1 THEN 0 ELSE (cwo.result = 'open' AND cwo.status != 'new') END), 0) > 0 OR COALESCE(SUM(CASE WHEN approve_override = 1 THEN 0 ELSE (cwo.result = 'moved') END), 0) > 0 THEN 'yellow' WHEN COALESCE(SUM(CASE WHEN approve_override = 1 THEN 1 ELSE (cwo.result = 'accepted') END), 0) = COUNT(cwo.id) AND COUNT(cwo.id) > 0 THEN 'green' ELSE 'blue' END AS status_light FROM ConstructionConsent LEFT JOIN ConstructionConsentOwner cwo ON ConstructionConsent.id = cwo.constructionconsent_id LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer vh ON ConstructionConsent.adb_hausnummer_id = vh.hausnummer_id LEFT JOIN `".ADDRESSDB_DBNAME."`.Strasse vs ON ConstructionConsent.adb_strasse_id = vs.id WHERE $where GROUP BY ConstructionConsent.id $having ORDER BY $order"; // die($sql); if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; } elseif(is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['count']; } } $res = $db->query($sql); if($db->num_rows($res)) { if ($returnCount) return $db->num_rows($res); while($data = $db->fetch_object($res)) { $items[$data->id] = new ConstructionConsent($data); } } return $returnCount ? 0 : $items; } private static function getSqlFilter($filter) { $where = "1=1 "; if(array_key_exists("project_id", $filter)) { $project_id = $filter['project_id']; if(is_numeric($project_id)) { $where .= " AND ConstructionConsent.constructionconsentproject_id=$project_id"; } else if (is_array($project_id)) { $where .= " AND ConstructionConsent.constructionconsentproject_id IN (".implode(",", $project_id).")"; } } if(array_key_exists("constructionconsentproject_id", $filter)) { $constructionconsentproject_id = $filter['constructionconsentproject_id']; if(is_numeric($constructionconsentproject_id)) { $where .= " AND ConstructionConsent.constructionconsentproject_id=$constructionconsentproject_id"; } } if(array_key_exists("adb_wohneinheit_id", $filter)) { $adb_wohneinheit_id = $filter['adb_wohneinheit_id']; if(is_numeric($adb_wohneinheit_id)) { $where .= " AND ConstructionConsent.adb_wohneinheit_id=$adb_wohneinheit_id"; } } if(array_key_exists("termination_id", $filter)) { $termination_id = $filter['termination_id']; if(is_numeric($termination_id)) { $where .= " AND ConstructionConsent.termination_id=$termination_id"; } } if(array_key_exists("object_type", $filter)) { $object_type = FronkDB::singleton()->escape($filter["object_type"]); if($object_type) { $where .= " AND object_type='$object_type'"; } } if(array_key_exists("ez", $filter)) { $ez = FronkDB::singleton()->escape($filter["ez"]); if($ez) { $where .= " AND ez='$ez'"; } } if(array_key_exists("kg", $filter)) { $ez = FronkDB::singleton()->escape($filter["kg"]); if($ez) { $where .= " AND kg='$ez'"; } } if(array_key_exists("gst", $filter)) { $gst = FronkDB::singleton()->escape($filter["gst"]); if($gst) { $where .= " AND gst LIKE '%$gst%'"; } } if(array_key_exists("rimo_gn", $filter)) { $rimo_gn = FronkDB::singleton()->escape($filter["rimo_gn"]); if($rimo_gn) { $where .= " AND rimo_gn LIKE '%$rimo_gn%'"; } } if(array_key_exists("address", $filter)) { $address = FronkDB::singleton()->escape($filter["address"]); if ($address) { $address = trim($address); $searchTerms = explode(' ', $address); $conditions = []; foreach ($searchTerms as $term) { $term = '%' . $term . '%'; $conditions[] = "(vs.name LIKE '$term' OR vh.strasse LIKE '$term' OR vh.hausnummer LIKE '$term' OR vh.plz LIKE '$term' OR vh.ortschaft LIKE '$term' OR vh.gemeinde LIKE '$term')"; } if (!empty($conditions)) { $where .= " AND (" . implode(' AND ', $conditions) . ")"; } } } if(array_key_exists("status", $filter)) { $status = FronkDB::singleton()->escape($filter["status"]); if($status) { $where .= " AND status='$status'"; } } if(array_key_exists("result", $filter)) { $result = FronkDB::singleton()->escape($filter["result"]); if($result) { $where .= " AND result='$result'"; } } if(array_key_exists("owner_name", $filter)) { $owner_name = FronkDB::singleton()->escape($filter["owner_name"]); if($owner_name) { $where .= " AND owner_name like '%$owner_name%'"; } } if(array_key_exists("owner_street", $filter)) { $owner_street = FronkDB::singleton()->escape($filter["owner_street"]); if($owner_street) { $where .= " AND owner_street like '%$owner_street%'"; } } if(array_key_exists("owner_zip", $filter)) { $owner_zip = FronkDB::singleton()->escape($filter["owner_zip"]); if($owner_zip) { $where .= " AND owner_zip like '%$owner_zip%'"; } } if(array_key_exists("owner_city", $filter)) { $owner_city = FronkDB::singleton()->escape($filter["owner_city"]); if($owner_city) { $where .= " AND owner_city like '%$owner_city%'"; } } if(array_key_exists("owner_country", $filter)) { $owner_country = FronkDB::singleton()->escape($filter["owner_country"]); if($owner_country) { $where .= " AND owner_country like '%$owner_country%'"; } } if(array_key_exists("network", $filter)) { $network = FronkDB::singleton()->escape($filter["network"]); if($network) { $where .= " AND (vh.netzgebiet_id=$network OR ConstructionConsent.netzgebiet_id=$network)"; } } if(array_key_exists("inspection_planner", $filter)) { $inspection_planner = $filter["inspection_planner"]; if($inspection_planner == "!NULL") { $where .= " AND inspection_planner IS NOT NULL"; } elseif($inspection_planner == "NULL") { $where .= " AND inspection_planner IS NULL"; } } if(array_key_exists("conduit_installed_building", $filter)) { $conduit_installed_building = $filter["conduit_installed_building"]; if($conduit_installed_building == "!NULL") { $where .= " AND (conduit_installed_building IS NOT NULL AND conduit_installed_building != 0)"; } elseif($conduit_installed_building == "NULL") { $where .= " AND (conduit_installed_building IS NULL OR conduit_installed_building = 0)"; } } if(array_key_exists("prioritize", $filter)) { $approve_override = $filter["prioritize"]; if($approve_override == "!NULL") { $where .= " AND (prioritize IS NOT NULL AND prioritize != 0)"; } elseif($approve_override == "NULL") { $where .= " AND (prioritize IS NULL OR prioritize = 0)"; } } if(array_key_exists("deferred", $filter)) { $approve_override = $filter["deferred"]; if($approve_override == "!NULL") { $where .= " AND (deferred IS NOT NULL AND deferred != 0)"; } elseif($approve_override == "NULL") { $where .= " AND (deferred IS NULL OR deferred = 0)"; } } if(array_key_exists("conduit_installed_ftu", $filter)) { $conduit_installed_ftu = $filter["conduit_installed_ftu"]; if($conduit_installed_ftu == "!NULL") { $where .= " AND (conduit_installed_ftu IS NOT NULL AND conduit_installed_ftu != 0)"; } elseif($conduit_installed_ftu == "NULL") { $where .= " AND (conduit_installed_ftu IS NULL OR conduit_installed_ftu = 0)"; } } if(array_key_exists("inhouse_cabling", $filter)) { $inhouse_cabling = $filter["inhouse_cabling"]; if($inhouse_cabling == "!NULL") { $where .= " AND (inhouse_cabling IS NOT NULL AND inhouse_cabling != 0)"; } elseif($inhouse_cabling == "NULL") { $where .= " AND (inhouse_cabling IS NULL OR inhouse_cabling = 0)"; } } if(array_key_exists("electric_approval", $filter)) { $inhouse_cabling = $filter["electric_approval"]; if($inhouse_cabling == "!NULL") { $where .= " AND (inspection_electrician IS NOT NULL AND inspection_electrician != 0)"; } elseif($inhouse_cabling == "NULL") { $where .= " AND (inspection_electrician IS NULL OR inspection_electrician = 0)"; } } if (array_key_exists("cwo", $filter) && !empty($filter['cwo'])) { $where .= " AND EXISTS ( SELECT 1 FROM ConstructionConsentOwner co WHERE co.constructionconsent_id = ConstructionConsent.id AND (LOWER(CONCAT(co.firstname, ' ', co.lastname)) LIKE LOWER('%".$filter['cwo']."%') OR LOWER(co.company) LIKE LOWER('%".$filter['cwo']."%')) ) "; } if(array_key_exists("add-where", $filter)) { $where .= " ".$filter['add-where']; } //var_dump($filter, $where);exit; return $where; } public static function searchConstructionConsentBasedOnName($name, $filter = []) { $items = []; $db = FronkDB::singleton(); $searchName = trim($name); if (empty($searchName)) { // return $items; } $searchTerms = explode(' ', $searchName); $searchConditions = []; foreach ($searchTerms as $term) { $escapedTerm = $db->escape($term); $searchConditions[] = "(cco.firstname LIKE '%$escapedTerm%' OR cco.lastname LIKE '%$escapedTerm%' OR cc.name LIKE '%$escapedTerm%' OR ccp.name LIKE '%$escapedTerm%')"; } if (!empty($searchConditions)) { $intelligentSearchWhere = "(" . implode(' AND ', $searchConditions) . ")"; if (isset($filter['add-where'])) { $filter['add-where'] .= " AND $intelligentSearchWhere"; } else { $filter['add-where'] = "AND $intelligentSearchWhere"; } } if (isset($filter['add-where'])) { $filter['add-where'] .= " AND cco.firstname IS NOT NULL"; } else { $filter['add-where'] = "AND cco.firstname IS NOT NULL"; } $where = self::getSqlFilter($filter); $sql = "SELECT cc.id AS consent_id, cc.name AS consent_name, cc.status AS consent_status, cc.result AS consent_result, cc.ez, cc.kg, cc.gst, cc.gstnr, ccp.id AS project_id, ccp.name AS project_name, cco.id AS owner_id, cco.firstname, cco.lastname, cco.street, cco.zip, cco.city, cco.email, cco.phone, cco.status AS owner_status, cco.result AS owner_result, vh.strasse AS address_street, vh.hausnummer AS address_number, vh.plz AS address_zip, vh.ortschaft AS address_city FROM ConstructionConsent cc LEFT JOIN ConstructionConsentProject ccp ON cc.constructionconsentproject_id = ccp.id LEFT JOIN ConstructionConsentOwner cco ON cc.id = cco.constructionconsent_id LEFT JOIN `".ADDRESSDB_DBNAME."`.view_hausnummer vh ON cc.adb_hausnummer_id = vh.hausnummer_id WHERE $where ORDER BY cc.edit DESC, cco.lastname ASC, cco.firstname ASC"; $res = $db->query($sql); while ($data = $res->fetch_assoc()) { $items[] = [ 'consent_id' => $data['consent_id'], 'consent_name' => $data['consent_name'], 'consent_status' => $data['consent_status'], 'consent_result' => $data['consent_result'], 'project_id' => $data['project_id'], 'project_name' => $data['project_name'], 'owner_id' => $data['owner_id'], 'owner_name' => trim($data['firstname'] . ' ' . $data['lastname']), 'owner_address' => $data['street'] . ', ' . $data['zip'] . ' ' . $data['city'], 'owner_contact' => $data['email'] . ($data['phone'] ? ' / ' . $data['phone'] : ''), 'owner_status' => $data['owner_status'], 'owner_result' => $data['owner_result'], 'property_info' => 'EZ:' . $data['ez'] . ' KG:' . $data['kg'] . ' GST:' . $data['gst'] . ($data['gstnr'] ? '-' . $data['gstnr'] : ''), 'address' => $data['address_street'] . ' ' . $data['address_number'] . ', ' . $data['address_zip'] . ' ' . $data['address_city'], ]; } return $items; } }