From 04153eb8d3408ba4bc14af9aabf444d2f93ec0e5 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Wed, 17 Jul 2024 13:34:22 +0000 Subject: [PATCH] Feature/preorder add history --- .../Preorder/include/preorder-detail.php | 36 +++++ application/Preorder/Preorder.php | 28 +++- .../PreorderHistory/PreorderHistory.php | 87 +++++++++++ .../PreorderHistory/PreorderHistoryModel.php | 144 ++++++++++++++++++ .../Preorderstatus/PreorderstatusModel.php | 7 + .../20240610121400_add_preorder_history.php | 32 ++++ 6 files changed, 332 insertions(+), 2 deletions(-) create mode 100644 application/PreorderHistory/PreorderHistory.php create mode 100644 application/PreorderHistory/PreorderHistoryModel.php create mode 100644 db/migrations/20240610121400_add_preorder_history.php diff --git a/Layout/default/Preorder/include/preorder-detail.php b/Layout/default/Preorder/include/preorder-detail.php index 1982a68c8..a83498d5f 100644 --- a/Layout/default/Preorder/include/preorder-detail.php +++ b/Layout/default/Preorder/include/preorder-detail.php @@ -8,6 +8,7 @@ + is("Admin") && $preorder->adb_hausnummer->borderpoint_lat && $preorder->adb_hausnummer->borderpoint_long): ?> @@ -465,6 +466,41 @@ +
+
+
+
+
+
Addressdetails
+ +
+
+

Status-Historie

+ + + + + + + + history as $history): ?> + key != "preorderstatus_id") continue; ?> + + + + + + + +
ZeitpunktBenutzerAlter StatusNeuer Status
create)?>creator->name?>old->code?> - old->name?>new->code?> - new->name?>
+
+
+
+
+
+
+
+ is("Admin")): ?>
uid === "string") { @@ -49,7 +50,12 @@ class Preorder extends mfBaseModel { if($this->oaid != $old_oaid) { $this->save(); } - + + //TODO: history start + if($this->status_id != $this->_old_data->status_id) { + $this->createHistoryEntry(); + } + // run triggers based on new status $this->runStatusTrigger(); // Cascade status changes down to adb_hausnummer and adb_wohneinheit @@ -59,6 +65,16 @@ class Preorder extends mfBaseModel { $this->in_after_save--; } + + public function createHistoryEntry() { + $history = PreorderHistoryModel::create([ + "preorder_id" => $this->id, + "key" => 'preorderstatus_id', + "old_value" => $this->_old_data->status_id, + "new_value" => $this->status_id + ]); + $history->save(); + } public function runStatusTrigger() { if(!$this->id) return true; @@ -699,7 +715,15 @@ class Preorder extends mfBaseModel { $this->logistics = $logistics; return $this->logistics; } - + + if($name == "history") { + $history = PreorderHistoryModel::search(["preorder_id" => $this->id]); + $this->history = $history; + + return $this->history; + + } + if($name == "attribute") { if(!$this->attributes) { return null; diff --git a/application/PreorderHistory/PreorderHistory.php b/application/PreorderHistory/PreorderHistory.php new file mode 100644 index 000000000..fb39c4711 --- /dev/null +++ b/application/PreorderHistory/PreorderHistory.php @@ -0,0 +1,87 @@ +new_value; + } else { + $value = $this->old_value; + } + + if($raw) { + return $value; + } + + $m = []; + if(preg_match('/(.+)_id/', $this->key, $m)) { + if(array_key_exists(1, $m)) { + $object = ucfirst($m[1]); + + $value = new $object($value); + if(!$value->id) return null; + } + } + + return $value; + + } + + public function getProperty($name) { + if($this->$name == null) { + + if($name == "old" || $name == "new") { + return $this->getValue($name, false); + } + + if($name == "old_raw") { + return $this->getValue("old", true); + } + if($name == "new_raw") { + return $this->getValue("new", true); + } + + 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 = new $classname($this->$idfield); + + if($this->$name->id) { + return $this->$name; + } else { + return null; + } + } + + return $this->$name; + } +} \ No newline at end of file diff --git a/application/PreorderHistory/PreorderHistoryModel.php b/application/PreorderHistory/PreorderHistoryModel.php new file mode 100644 index 000000000..e216d6908 --- /dev/null +++ b/application/PreorderHistory/PreorderHistoryModel.php @@ -0,0 +1,144 @@ + $value) { + if (property_exists(get_called_class(), $field)) { + $model->$field = $value; + } + } + + $me = mfValuecache::singleton()->get("me"); + if (!$me) { + $me = new User(); + $me->loadMe(); + mfValuecache::singleton()->set("me", $me); + } + + 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 getFirst($filter = []) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $res = $db->select("PreorderHistory", "*", "$where ORDER BY preorder_id,`key`,`create` LIMIT 1"); + if ($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new PreorderHistory($data); + if ($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function getAll() { + $items = []; + + $db = FronkDB::singleton(); + + $res = $db->select("PreorderHistory", "*", "1=1 ORDER BY preorder_id,`key`,`create`"); + if ($db->num_rows($res)) { + while ($data = $db->fetch_object($res)) { + $items[] = new PreorderHistory($data); + } + } + return $items; + } + + public static function count($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT COUNT(*) as cnt FROM PreorderHistory + WHERE $where + "; + + $res = $db->query($sql); + if ($db->num_rows($res)) { + $data = $db->fetch_object($res); + return $data->cnt; + } + return 0; + } + + public static function search($filter = [], $limit = false) { + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM PreorderHistory + WHERE $where + ORDER BY preorder_id,`key`,`create`"; + + mfLoghandler::singleton()->debug($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)) { + while ($data = $db->fetch_object($res)) { + $items[] = new PreorderHistory($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + + + if (array_key_exists("preorder_id", $filter)) { + $preorder_id = $filter['preorder_id']; + if (is_numeric($preorder_id)) { + $where .= " AND PreorderHistory.preorder_id=$preorder_id"; + } + } + + if (array_key_exists("key", $filter)) { + $key = FronkDB::singleton()->escape($filter["key"]); + if($key) { + $where .= " AND `key` like '$key'"; + } + } + + + if (array_key_exists("create_by", $filter)) { + $create_by = $filter['create_by']; + if (is_numeric($create_by)) { + $where .= " AND PreorderHistory.create_by=$create_by"; + } + } + + return $where; + } + +} diff --git a/application/Preorderstatus/PreorderstatusModel.php b/application/Preorderstatus/PreorderstatusModel.php index 93400601e..d1e4da2be 100644 --- a/application/Preorderstatus/PreorderstatusModel.php +++ b/application/Preorderstatus/PreorderstatusModel.php @@ -125,6 +125,13 @@ class PreorderstatusModel { $where = "1=1 "; //var_dump($filter);exit; + + if(array_key_exists("id", $filter)) { + $id = $filter['id']; + if(is_numeric($id)) { + $where .= " AND id=$id"; + } + } if(array_key_exists("code", $filter)) { $code = $filter['code']; diff --git a/db/migrations/20240610121400_add_preorder_history.php b/db/migrations/20240610121400_add_preorder_history.php new file mode 100644 index 000000000..1a834b60a --- /dev/null +++ b/db/migrations/20240610121400_add_preorder_history.php @@ -0,0 +1,32 @@ +getEnvironment() == "thetool") { + + $preorderHistoryTable = $this->table("PreorderHistory", ["signed" => true]); + $preorderHistoryTable + ->addColumn("preorder_id", "integer", ["null" => false, "signed" => true]) + ->addColumn("key", "string", ["null" => false, "limit" => 100]) + ->addColumn("old_value", "text", ["null" => true]) + ->addColumn("new_value", "text", ["null" => true]) + ->addColumn("create_by", "integer", ["null" => false]) + ->addColumn("edit_by", "integer", ["null" => false]) + ->addColumn("create", "integer", ["null" => false]) + ->addColumn("edit", "integer", ["null" => false]); + + $preorderHistoryTable->addForeignKey("preorder_id", "Preorder", "id", ["delete" => "CASCADE", "update" => "NO_ACTION"]); + $preorderHistoryTable->create(); + } + } + + public function down(): void { + if ($this->getEnvironment() == "thetool") { + $this->table("PreorderHistory")->drop()->save(); + } + } +}