Added Terminations to buildings

This commit is contained in:
Frank Schubert
2021-07-22 22:56:36 +02:00
parent 7f6ed3e1eb
commit 4ff09b319b
25 changed files with 655 additions and 137 deletions

View File

@@ -8,38 +8,7 @@ class Building extends mfBaseModel {
private $type;
private $status;
private $pipeworker;
public function getProperty($name) {
if($this->$name == null) {
if($name == "type") {
$this->type = new Buildingtype($this->type_id);
return $this->type;
}
if($name == "status") {
$this->status = new Buildingstatus($this->status_id);
return $this->status;
}
if($name == "pipeworker") {
$this->pipeworker = new Address($this->pipeworker_id);
return $this->pipeworker;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
private $terminations;
public function getNewObjectCode() {
if(!$this->zip) {
@@ -116,4 +85,41 @@ class Building extends mfBaseModel {
return $Gridcell;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "type") {
$this->type = new Buildingtype($this->type_id);
return $this->type;
}
if($name == "status") {
$this->status = new Buildingstatus($this->status_id);
return $this->status;
}
if($name == "pipeworker") {
$this->pipeworker = new Address($this->pipeworker_id);
return $this->pipeworker;
}
if($name == "terminations") {
$this->terminations = TerminationModel::search(['building_id' => $this->id]);
return $this->terminations;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}

View File

@@ -140,6 +140,31 @@ class BuildingController extends mfBaseController {
$building->save();
}
// Anschlüsse anlegen
if($building->units > 0) {
for($i = 1; $i <= $building->units; $i++) {
$data = [];
$data['building_id'] = $building->id;
$data['code'] = $building->code . "." . sprintf("%03d", $i);
if($building->units == 1) {
$data['contact'] = $building->contact;
$data['phone'] = $building->phone;
$data['email'] = $building->email;
}
if($building->lineworker_id) {
$data['lineworker_id'] = $building->lineworker_id;
}
if($building->oan_id) {
$data['oan_id'] = $building->oan_id. "." . sprintf("%03d", $i);
}
$term = TerminationModel::create($data);
$term->save();
}
}
$this->layout()->setFlash("Objekt erfolgreich gespeichert.", "success");
$this->redirect("Building", "Edit", ['id' => $new_id]);

View File

@@ -0,0 +1,29 @@
<?php
class Termination extends mfBaseModel {
protected $forcestr = ['phone','email','note'];
private $building;
private $status;
private $lineworker;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}

View File

@@ -0,0 +1,69 @@
<?php
class TerminationController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->isAdmin() && !$me->is("netowner") && !$me->is("lineplanner") && !$me->is("lineworker")) {
$this->redirect("Dashboard");
}
}
protected function apiAction() {
$do = $this->request->do;
$data = [];
switch($do) {
case "setValue":
$return = $this->setValueApi();
break;
default:
$return = false;
}
if(!is_array($return) || !count($return)) {
$data = ["status" => "error"];
$this->returnJson($data);
}
$data['status'] = "OK";
$data['result'] = $return;
$this->returnJson($data);
}
private function setValueApi() {
$term_id = $this->request->id;
$field = $this->request->type;
$value = $this->db->escape($this->request->value);
if(!is_numeric($term_id) || $term_id < 1) {
$this->log->debug(__FILE__.": id not a number");
return false;
}
if(!in_array($field, ["name","contact","phone","email"])) {
$this->log->debug(__FILE__.": invalid field");
return false;
}
$term = new Termination($term_id);
if(!$term->id) {
$this->log->debug(__FILE__.": no termination");
return false;
}
$term->$field = $value;
if(!$term->save()) {
$this->log->debug(__FILE__.": error saving");
return false;
}
return ["msg" => "Saved successfully"];
}
}

View File

@@ -0,0 +1,139 @@
<?php
class TerminationModel {
public $building_id = null;
public $status_id = null;
public $lineworker_id = null;
public $name = null;
public $code = null;
public $oan_id = null;
public $rimo_id = null;
public $contact = null;
public $phone = null;
public $email = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new Termination();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
if(!$model->status_id) {
$model->status_id = 1;
}
$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 getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("Termination", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Termination($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Termination", "*", "1=1 ORDER BY code");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Termination($data);
}
}
return $items;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Termination.* FROM Termination
LEFT JOIN Building ON (Building.id = Termination.building_id)
LEFT JOIN Terminationstatus ON (Terminationstatus.id = Termination.status_id)
WHERE $where
ORDER BY code
";
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Termination($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("building_id", $filter)) {
$building_id = $filter['building_id'];
if(is_numeric($building_id) && $building_id > 0) {
$where .= " AND building_id = $building_id";
}
}
if(array_key_exists("status", $filter)) {
if(in_array(substr($filter['status'], 1, 2), ["<=", ">="])) {
$op = substr($filter['status'], 1, 2);
$status = substr($filter['status'], 3);
} elseif(in_array(substr($filter['status'], 1, 1), ["<", ">"])) {
$op = substr($filter['status'], 1, 1);
$status = substr($filter['status'], 2);
} else {
$op = "=";
$status = $filter['status'];
}
if(is_numeric($status)) {
$where .= " AND Terminationstatus.code $op $status";
} else {
// get code of statusname
$code = Terminationstatus::getOne(["name" => $status]);
if($code) {
$status = FronkDB::singleton()->escape($status);
$where .= " AND Terminationstatus.code $op '$status'";
}
}
}
return $where;
}
}

View File

@@ -0,0 +1,5 @@
<?php
class Terminationstatus extends mfBaseModel {
}

View File

@@ -0,0 +1,95 @@
<?php
class TerminationstatusModel {
public $code = null;
public $name = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new Terminationstatus();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$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 getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("Terminationstatus", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Terminationstatus($data);
}
return $item;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Terminationstatus", "*", "1=1 ORDER BY code,name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Terminationstatus($data);
}
}
return $items;
}
public static function search($filter) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Terminationstatus", "*", $where);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Terminationstatus($data);
}
}
return $items;
}
private function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("code", $filter)) {
$code = FronkDB::singleton()->escape($filter['code']);
$where .= " AND code='$code'";
}
if(array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter['name']);
$where .= " AND name='$name'";
}
//var_dump($filter, $where);exit;
return $where;
}
}