From 4abaa50f561bf2b7bd7c38b799bdb5c73b635514 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Mon, 29 Jul 2024 14:44:08 +0200 Subject: [PATCH] Added PreorderStatusflag to Preorder/Index --- Layout/default/Preorder/Index.php | 45 ++++++++++++++++ .../Preorder/include/preorder-detail.php | 51 ++++++++++++++++++- application/Preorder/Preorder.php | 13 +++++ application/Preorder/PreorderController.php | 36 ++++++++++++- .../PreorderStatusflag/PreorderStatusflag.php | 47 +++++++++++++++++ .../PreorderStatusflagValueModel.php | 10 ++-- ...40717144237_create_preorderstatus_flag.php | 3 ++ 7 files changed, 196 insertions(+), 9 deletions(-) diff --git a/Layout/default/Preorder/Index.php b/Layout/default/Preorder/Index.php index 19650db08..3af5dd407 100644 --- a/Layout/default/Preorder/Index.php +++ b/Layout/default/Preorder/Index.php @@ -630,6 +630,51 @@ return false; } } + + $("input.preorder-statusflag").change((e) => { + var elem = e.target; + var preorder_id = $(elem).data("preorder_id"); + var flag_id = $(elem).data("flag_id"); + var value = $(elem).prop("checked") ? 1 : 0; + + $.ajax({ + url: "", + type: "POST", + data: { + do: "setStatusFlag", + preorder_id: preorder_id, + flag_id: flag_id, + value: value + }, + dataType: "json", + context: { + elem: elem, + preorder_id: preorder_id, + flag_id: flag_id, + }, + success: function (success) { + var textelem = $("#preorder-" + this.preorder_id + "-statusflag-" + this.flag_id + "-text"); + if(success.status != "OK") { + notify("error","Fehler beim Speichern des Statusflags"); + $(this.elem).prop("checked", !$(this.elem).prop("checked")); + } else { + textelem.removeClass("text-danger").addClass("text-success"); + setTimeout(function() { + textelem.removeClass("text-success"); + textelem.removeClass("text-success") + }, 2000, textelem); + } + + + }, + error: function () { + notify("error","Fehler beim Speichern des Statusflags"); + $(this.elem).prop("checked", !$(this.elem).prop("checked")); + } + + }); + + }); // navigation var preorder_id; diff --git a/Layout/default/Preorder/include/preorder-detail.php b/Layout/default/Preorder/include/preorder-detail.php index 1d271623c..dffcfebae 100644 --- a/Layout/default/Preorder/include/preorder-detail.php +++ b/Layout/default/Preorder/include/preorder-detail.php @@ -5,13 +5,15 @@
@@ -466,6 +468,51 @@
+
+
+
+
+
+
Status
+ +
+
+

Bestellstatus

+ + + + + + + + +
Status Codestatus->code?>
Status Textstatus->name?>
+
+
+

Statusflags

+ + + + + + + +
+ id, $preorder->statusflags) && $preorder->statusflags[$flag->id]->value && $preorder->statusflags[$flag->id]->value->value) ? "checked='checked'" : ""?> + /> + statusflags[$flag->id]->code?> - statusflags[$flag->id]->name?>
+
+
+
+
+
+
+
+
diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 33dd5df87..5c0a30544 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -5,6 +5,7 @@ class Preorder extends mfBaseModel { private $in_after_save = 0; private $status; + private $statusflags; private $campaign; private $partner; private $discounts; @@ -658,6 +659,18 @@ class Preorder extends mfBaseModel { } return $this->status; } + + if($name == "statusflags") { + $flags = []; + foreach(PreorderStatusflagModel::getAll() as $flag) { + $flag->preorder_id = $this->id; + $flags[$flag->id] = $flag; + } + if(count($flags)) { + $this->statusflags = $flags; + } + return $this->statusflags; + } if($name == "partner") { $this->partner = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->partner_id); diff --git a/application/Preorder/PreorderController.php b/application/Preorder/PreorderController.php index fb03d24e2..6cdcab285 100644 --- a/application/Preorder/PreorderController.php +++ b/application/Preorder/PreorderController.php @@ -914,6 +914,9 @@ class PreorderController extends mfBaseController { case "savePatchposition": $return = $this->savePatchpositionApi(); break; + case "setStatusFlag": + $return = $this->setStatusFlagAction(); + break; default: $return = false; } @@ -926,7 +929,38 @@ class PreorderController extends mfBaseController { $data['result'] = $return; $this->returnJson($data); } - + + private function setStatusFlagAction() { + $preorder_id = $this->request->preorder_id; + $flag_id = $this->request->flag_id; + $value = $this->request->value; + + if(!$preorder_id || !$flag_id) { + return false; + } + + $flag = new PreorderStatusflag($flag_id); + if(!$flag->id) { + return false; + } + + $flagvalue = PreorderStatusflagValueModel::getFirst(["preorder_id" => $preorder_id, "flag_id" => $flag_id]); + if(!$flagvalue) { + $flagvalue = PreorderStatusflagValueModel::create([ + "preorder_id" => $preorder_id, + "flag_id" => $flag_id + ]); + } + + $flagvalue->value = ($value) ? 1 : 0; + + if(!$flagvalue->save()) { + return false; + } + + return ["message" => "Statusflag saved successfully"]; + } + private function getFilteredPreordersApi() { $preorders = []; $filter = []; diff --git a/application/PreorderStatusflag/PreorderStatusflag.php b/application/PreorderStatusflag/PreorderStatusflag.php index a57a2cfaa..b847aedd6 100644 --- a/application/PreorderStatusflag/PreorderStatusflag.php +++ b/application/PreorderStatusflag/PreorderStatusflag.php @@ -1,5 +1,52 @@ $name == null) { + + if($name == "value") { + if(!$this->preorder_id) return null; + $value = PreorderStatusflagValueModel::getFirst(["preorder_id" => $this->preorder_id, "flag_id" => $this->id]); + $this->value = $value; + return $this->value; + } + + if($name == "creator") { + $user = mfValuecache::singleton()->get("Worker-id-".$this->create_by); + if($user) { + $this->creator = $user; + return $this->creator; + } + $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 = new User($this->edit_by); + 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; + } } \ No newline at end of file diff --git a/application/PreorderStatusflagValue/PreorderStatusflagValueModel.php b/application/PreorderStatusflagValue/PreorderStatusflagValueModel.php index 74e960a85..2864edba6 100644 --- a/application/PreorderStatusflagValue/PreorderStatusflagValueModel.php +++ b/application/PreorderStatusflagValue/PreorderStatusflagValueModel.php @@ -5,8 +5,6 @@ class PreorderStatusflagValueModel { public $preorder_id; public $flag_id; public $value; - public $old_value; - public $new_value; public $create_by; public $edit_by; public $create; @@ -42,7 +40,8 @@ class PreorderStatusflagValueModel { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); - $res = $db->select("PreorderStatusflagValue", "*", "$where ORDER BY statusflag_id LIMIT 1"); + + $res = $db->select("PreorderStatusflagValue", "*", "$where ORDER BY flag_id LIMIT 1"); if ($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new PreorderStatusflagValue($data); @@ -60,7 +59,7 @@ class PreorderStatusflagValueModel { $db = FronkDB::singleton(); - $res = $db->select("PreorderStatusflagValue", "*", "1=1 ORDER BY statusflag_id"); + $res = $db->select("PreorderStatusflagValue", "*", "1=1 ORDER BY flag_id"); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = new PreorderStatusflagValue($data); @@ -92,7 +91,7 @@ class PreorderStatusflagValueModel { $where = self::getSqlFilter($filter); $sql = "SELECT * FROM PreorderStatusflagValue WHERE $where - ORDER BY statusflag_id"; + ORDER BY flag_id"; mfLoghandler::singleton()->debug($sql); if (is_array($limit) && count($limit)) { @@ -116,7 +115,6 @@ class PreorderStatusflagValueModel { $where = "1=1 "; - if (array_key_exists("preorder_id", $filter)) { $preorder_id = $filter['preorder_id']; if (is_numeric($preorder_id)) { diff --git a/db/migrations/20240717144237_create_preorderstatus_flag.php b/db/migrations/20240717144237_create_preorderstatus_flag.php index d77ab98d3..296755643 100644 --- a/db/migrations/20240717144237_create_preorderstatus_flag.php +++ b/db/migrations/20240717144237_create_preorderstatus_flag.php @@ -58,12 +58,15 @@ final class CreatePreorderstatusFlag extends AbstractMigration ])->save(); $psfv = $this->table("PreorderStatusflagValue"); + $psfv->addColumn("preorder_id", "integer", ["null" => false]); $psfv->addColumn("flag_id", "integer", ["null" => false]); $psfv->addColumn("value", "integer", ["null" => false]); $psfv->addColumn("create_by", "integer", ["null" => false]); $psfv->addColumn("edit_by", "integer", ["null" => false]); $psfv->addColumn("create", "integer", ["null" => false]); $psfv->addColumn("edit", "integer", ["null" => false]); + $psfv->addIndex("preorder_id"); + $psfv->addIndex(["preorder_id", "flag_id"]); $psfv->save(); }