Feature/preorder add history

This commit is contained in:
Luca Haid
2024-07-17 13:34:22 +00:00
committed by Frank Schubert
parent 076b513039
commit 04153eb8d3
6 changed files with 332 additions and 2 deletions
+26 -2
View File
@@ -18,6 +18,7 @@ class Preorder extends mfBaseModel {
private $editor;
private $attribute = [];
private $logistics;
private $history;
public function afterLoad() {
if($this->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;
@@ -0,0 +1,87 @@
<?php
class PreorderHistory extends mfBaseModel {
private $preorder;
private $creator;
private $editor;
private $old;
private $new;
public function getValue($type = "new", $raw = false) {
if($type != "old" && $type != "new") return null;
if($type == "new") {
$value = $this->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;
}
}
@@ -0,0 +1,144 @@
<?php
class PreorderHistoryModel {
public $preorder_id;
public $key;
public $old_value;
public $new_value;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new PreorderHistory();
foreach ($data as $field => $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;
}
}
@@ -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'];