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

View File

@@ -8,6 +8,7 @@
<li class="nav-item"><a class="nav-link active" href="#preorder-detail-<?=$preorder->id?>-detail" data-toggle="tab" aria-expanded="false">Details</a></li>
<li class="nav-item"><a class="nav-link" href="#preorder-detail-<?=$preorder->id?>-address" data-toggle="tab" aria-expanded="false">Adressdetails</a></li>
<li class="nav-item"><a class="nav-link" href="#preorder-detail-<?=$preorder->id?>-rimo" data-toggle="tab" aria-expanded="false">RIMO</a></li>
<li class="nav-item"><a class="nav-link" href="#preorder-detail-<?=$preorder->id?>-history" data-toggle="tab" aria-expanded="false">Status-Historie</a></li>
<?php if($me->is("Admin") && $preorder->adb_hausnummer->borderpoint_lat && $preorder->adb_hausnummer->borderpoint_long): ?>
<li class="nav-item"><a class="nav-link" href="#preorder-detail-<?=$preorder->id?>-map" data-toggle="tab" aria-expanded="false" onclick="loadBorderpointMap(<?=$preorder->id?>)">Übergabepunkt</a></li>
<?php endif; ?>
@@ -465,6 +466,41 @@
</div>
</div>
<div id="preorder-detail-<?=$preorder->id?>-history" class="tab-pane">
<div class="row justify-content-center">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="card-header bg-info text-white pl-2 pr-2 pt-1 pb-1">Addressdetails</div>
<div class="row">
<div class="col-12">
<h3>Status-Historie</h3>
<table class="table table-sm table-striped">
<tr>
<th>Zeitpunkt</th>
<th>Benutzer</th>
<th>Alter Status</th>
<th>Neuer Status</th>
</tr>
<?php foreach($preorder->history as $history): ?>
<?php if($history->key != "preorderstatus_id") continue; ?>
<tr>
<td><?=date("d.m.Y H:i:s", $history->create)?></td>
<td><?=$history->creator->name?></td>
<td><?=$history->old->code?> - <?=$history->old->name?></td>
<td><?=$history->new->code?> - <?=$history->new->name?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php if($me->is("Admin")): ?>
<div
id="preorder-detail-<?=$preorder->id?>-map"

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;

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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'];

View File

@@ -0,0 +1,32 @@
<?php /** @noinspection ALL */
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddPreorderHistory extends AbstractMigration {
public function up(): void {
if ($this->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();
}
}
}