From ce3e8f07e63a2e6774d359b81801167537d10610 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 4 Jun 2024 18:09:01 +0200 Subject: [PATCH] Added Preorder discount code import and statusupdate import --- .../default/Preorder/Statusupdateimport.php | 64 ++++++++ .../Preorder/include/preorder-detail.php | 23 ++- Layout/default/PreorderDiscount/Import.php | 64 ++++++++ Layout/default/Preordercampaign/Index.php | 13 +- application/Preorder/Preorder.php | 20 ++- application/Preorder/PreorderController.php | 78 ++++++++- .../PreorderDiscount/PreorderDiscount.php | 45 ++++++ .../PreorderDiscountController.php | 93 +++++++++++ .../PreorderDiscountModel.php | 153 ++++++++++++++++++ ...0240604132904_create_preorder_discount.php | 39 +++++ lib/mvcfronk/mfBase/mfBaseController.php | 7 + 11 files changed, 593 insertions(+), 6 deletions(-) create mode 100644 Layout/default/Preorder/Statusupdateimport.php create mode 100644 Layout/default/PreorderDiscount/Import.php create mode 100644 application/PreorderDiscount/PreorderDiscount.php create mode 100644 application/PreorderDiscount/PreorderDiscountController.php create mode 100644 application/PreorderDiscount/PreorderDiscountModel.php create mode 100644 db/migrations/20240604132904_create_preorder_discount.php diff --git a/Layout/default/Preorder/Statusupdateimport.php b/Layout/default/Preorder/Statusupdateimport.php new file mode 100644 index 000000000..062a391ad --- /dev/null +++ b/Layout/default/Preorder/Statusupdateimport.php @@ -0,0 +1,64 @@ + + + +
+
+
+
+ +
+

Statusupdates importieren

+
+
+
+ + +
+
+ +
+
+

Statusupdates importieren

+ +
" enctype="multipart/form-data"> +
+
+ +
+ +
+ + Format: OAID;Neuer Status Code +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+
+
+
+
+
+ + + diff --git a/Layout/default/Preorder/include/preorder-detail.php b/Layout/default/Preorder/include/preorder-detail.php index 96b36a7f2..618ae201f 100644 --- a/Layout/default/Preorder/include/preorder-detail.php +++ b/Layout/default/Preorder/include/preorder-detail.php @@ -143,6 +143,25 @@ address_info == "address") ? "Anschlussadresse" : "Kontaktadresse"?> + + discounts): ?> +

Gutscheincodes

+ + + + + + + discounts as $discount): ?> + + + + + + + +
CodeZugewiesenInfo
code?>assigned)?>info)?>
+
@@ -398,12 +417,12 @@ data-default="adb_wohneinheit->patch_cluster) ? $preorder->adb_wohneinheit->patch_cluster : $preorder->adb_hausnummer->netzgebiet->extref?>" /> - Shelf: + Shelf: - Module: + Module: diff --git a/Layout/default/PreorderDiscount/Import.php b/Layout/default/PreorderDiscount/Import.php new file mode 100644 index 000000000..dbf624213 --- /dev/null +++ b/Layout/default/PreorderDiscount/Import.php @@ -0,0 +1,64 @@ + + + +
+
+
+
+ +
+

Gutscheincodes importieren

+
+
+
+ + +
+
+ +
+
+

Gutscheincodes importieren

+ +
" enctype="multipart/form-data"> +
+
+ +
+ +
+ + Format: OAID;Gutscheincode +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+
+
+
+
+
+ + + diff --git a/Layout/default/Preordercampaign/Index.php b/Layout/default/Preordercampaign/Index.php index cfbb12237..49db23276 100644 --- a/Layout/default/Preordercampaign/Index.php +++ b/Layout/default/Preordercampaign/Index.php @@ -74,8 +74,17 @@
- - + + + address_id == 1 || $me->address_id == 4807): // xinon, rml ?> +
+ +
+ +
diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 989950242..824625966 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -7,6 +7,8 @@ class Preorder extends mfBaseModel { private $status; private $campaign; private $partner; + private $discounts; + private $applied_discounts; private $building; private $adb_hausnummer; private $adb_wohneinheit; @@ -623,7 +625,23 @@ class Preorder extends mfBaseModel { } return $this->partner; } - + + if($name == "discounts") { + $discounts = PreorderDiscountModel::search(["preorder_id" => $this->id]); + if($discounts) { + $this->discounts = $discounts; + } + return $this->discounts; + } + + if($name == "applied_discounts") { + $discounts = PreorderDiscountModel::search(["preorder_id" => $this->id, "assigned" => true]); + if($discounts) { + $this->discounts = $discounts; + } + return $this->discounts; + } + if($name == "building") { $this->building = mfValuecache::singleton()->get("mfObjectmodel-Building-".$this->building_id); if(!$this->building) { diff --git a/application/Preorder/PreorderController.php b/application/Preorder/PreorderController.php index 461c8f0e7..955260fd2 100644 --- a/application/Preorder/PreorderController.php +++ b/application/Preorder/PreorderController.php @@ -756,7 +756,83 @@ class PreorderController extends mfBaseController { } - + + protected function statusupdateimport() { + $this->layout()->setTemplate("Preorder/Statusupdateimport"); + } + + protected function saveStatusupdateimport() { + $headline_included = $this->request->headline; + + if(is_array($_FILES) && array_key_exists("statusupdate_csv", $_FILES) && !$_FILES['statusupdate_csv']['error']) { + // look for uploaded import file + try { + // returns File object or throws Exception on error + $file = mfUpload::handleFormUpload("statusupdate_csv"); + } catch(Exception $ex) { + $this->layout()->setFlash("Fehler beim Dateiupload: " . $ex->getMessage(), "error"); + $this->redirect("Preorder", "statusupdateimport"); + } + + $i = 0; + $notfound = 0; + $invalidcode = 0; + $nochange = 0; + $saved = 0; + + $filename = $file->getFullPath(); + $input = fopen($filename, "r"); + while($csv = fgetcsv($input, 0, ";")) { + $i++; + if($i == 1 && $headline_included) continue; + + if(!trim($csv[0])) { + continue; + } + + $oaid = trim($csv[0]); + $new_status_code = trim($csv[1]); + + $preorder = PreorderModel::getFirstActive(["oaid" => $oaid]); + if(!$preorder) { + $notfound++; + continue; + } + + if($preorder->status->code != $new_status_code) { + $new_status = PreorderstatusModel::getFirst(["code" => $new_status_code]); + if(!$new_status_code) { + $invalidcode++; + continue; + } + $preorder->status_id = $new_status->id; + $preorder->save(); + $saved++; + } else { + $nochange++; + } + + } + + $message = "Import erfolgreich. $saved Statusupdates importiert"; + if($notfound) { + $message .= "
$notfound Bestellungen nicht gefunden"; + } + if($nochange) { + $message .= "
$nochange Bestelllungen haben bereits den neuen Status"; + } + if($invalidcode) { + $message .= "
$invalidcode ungültige Statuscodes"; + } + $this->layout()->setFlash($message); + $this->redirect("Preorder", "statusupdateimport"); + + } else { + $this->layout()->setFlash("Nichts importiert!", "info"); + $this->redirect("Preorder", "statusupdateimport"); + } + } + protected function exportAction() { $rfilter = $this->request->filter; if(!is_array($rfilter)) { diff --git a/application/PreorderDiscount/PreorderDiscount.php b/application/PreorderDiscount/PreorderDiscount.php new file mode 100644 index 000000000..0b0ed3d81 --- /dev/null +++ b/application/PreorderDiscount/PreorderDiscount.php @@ -0,0 +1,45 @@ +$name == null) { + + 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/PreorderDiscount/PreorderDiscountController.php b/application/PreorderDiscount/PreorderDiscountController.php new file mode 100644 index 000000000..9ceb292ec --- /dev/null +++ b/application/PreorderDiscount/PreorderDiscountController.php @@ -0,0 +1,93 @@ +needlogin=true; + $me = new User(); + $me->loadMe(); + $this->me = $me; + $this->layout()->set("me",$me); + + if(!$me->is(["Admin", "netowner", "salespartner", "preorderfront"])) { + $this->redirect("Dashboard"); + } + } + + protected function importAction() { + $this->layout()->setTemplate("PreorderDiscount/Import"); + } + + protected function saveImport() { + $headline_included = $this->request->headline; + + if(is_array($_FILES) && array_key_exists("discount_csv", $_FILES) && !$_FILES['discount_csv']['error']) { + // look for uploaded import file + try { + // returns File object or throws Exception on error + $file = mfUpload::handleFormUpload("discount_csv"); + } catch(Exception $ex) { + $this->layout()->setFlash("Fehler beim Dateiupload: " . $ex->getMessage(), "error"); + $this->redirect("PreorderDiscount", "import"); + } + + $i = 0; + $notfound = 0; + $exists = 0; + $saved = 0; + + $filename = $file->getFullPath(); + $input = fopen($filename, "r"); + while($csv = fgetcsv($input, 0, ";")) { + $i++; + if($i == 1 && $headline_included) continue; + + if(!trim($csv[0])) { + continue; + } + + $oaid = trim($csv[0]); + $code = trim($csv[1]); + + $preorder = PreorderModel::getFirstActive(["oaid" => $oaid]); + if(!$preorder) { + $notfound++; + continue; + } + + $discount_code = PreorderDiscountModel::getFirst(["code" => $code, "preorer_id" => $preorder->id]); + if($discount_code) { + $exists++; + continue; + } + + $discount_code = PreorderDiscountModel::create([ + "code" => $code, + "preorder_id" => $preorder->id, + "assigned" => date('U') + ]); + + if(!$discount_code->save()) { + $this->layout()->setFlash("Fehler beim Speichern!", "error"); + $this->redirect("PreorderDiscount", "import"); + } + + $saved++; + } + + $message = "Import erfolgreich. $saved Gutscheincodes importiert"; + if($notfound) { + $message .= "
$notfound Bestellungen nicht gefunden"; + } + if($exists) { + $message .= "
$exists Gutscheincodes schon verknüpft"; + } + $this->layout()->setFlash($message); + $this->redirect("PreorderDiscount", "import"); + + } else { + $this->layout()->setFlash("Nichts importiert!", "info"); + $this->redirect("PreorderDiscount", "import"); + } + } +} \ No newline at end of file diff --git a/application/PreorderDiscount/PreorderDiscountModel.php b/application/PreorderDiscount/PreorderDiscountModel.php new file mode 100644 index 000000000..fcfdd2c01 --- /dev/null +++ b/application/PreorderDiscount/PreorderDiscountModel.php @@ -0,0 +1,153 @@ + $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 getAll() { + $items = []; + + $db = FronkDB::singleton(); + + $res = $db->select("PreorderDiscount", "*", "1 = 1 ORDER BY code"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new PreorderDiscount($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT PreorderDiscount.* FROM PreorderDiscount + WHERE $where + LIMIT 1"; + //var_dump($sql);exit; + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new PreorderDiscount($data); + if($item->id) { + return $item; + } else { + return null; + } + } + return null; + } + + public static function count($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT COUT(*) as cnt FROM PreorderDiscount + WHERE $where + "; + + mfLoghandler::singleton()->debug($sql); + + $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) { + //var_dump($filter);exit; + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT PreorderDiscount.* FROM PreorderDiscount + WHERE $where + ORDER BY code"; + + 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']; + } + } + + mfLoghandler::singleton()->debug($sql); + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[$data->id] = new PreorderDiscount($data); + } + } + + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + $db = FronkDB::singleton(); + + if(array_key_exists("preorder_id", $filter)) { + $preorder_id = $filter['preorder_id']; + if(is_numeric($preorder_id)) { + $where .= " AND PreorderDiscount.`preorder_id` = $preorder_id"; + } + } + + if(array_key_exists("code", $filter)) { + $code = $db->escape($filter['code']); + if($code) { + $where .= " AND PreorderDiscount.`code` = '$code'"; + } + } + + if(array_key_exists("assigned", $filter)) { + $assigned = $filter['assigned']; + if($assigned === true) { + $where .= " AND PreorderDiscount.`assigned` > 0"; + } + if($assigned === false || $assigned === null) { + $where .= " AND (PreorderDiscount.`assigned` IS NULL OR PreorderDiscount.`assigned` = 0)"; + } + } + + //var_dump($filter, $where);exit; + return $where; + } + +} diff --git a/db/migrations/20240604132904_create_preorder_discount.php b/db/migrations/20240604132904_create_preorder_discount.php new file mode 100644 index 000000000..916a99cb1 --- /dev/null +++ b/db/migrations/20240604132904_create_preorder_discount.php @@ -0,0 +1,39 @@ +getEnvironment() == "thetool") { + $table = $this->table("PreorderDiscount"); + $table->addColumn("preorder_id", "integer", ["null" => true, "default" => null]); + $table->addColumn("code", "string", ["null" => false, "limit" => 64]); + $table->addColumn("info", "string", ["null" => true, "default" => null, "limit" => 1024]); + $table->addColumn("assigned", "integer", ["null" => true, "default" => null]); + $table->addColumn("create_by", "integer", ["null" => false]); + $table->addColumn("edit_by", "integer", ["null" => false]); + $table->addColumn("create", "integer", ["null" => false]); + $table->addColumn("edit", "integer", ["null" => false]); + $table->create(); + + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $this->table("PreorderDiscount")->drop()->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/lib/mvcfronk/mfBase/mfBaseController.php b/lib/mvcfronk/mfBase/mfBaseController.php index dd90074a3..ff8e3cad1 100644 --- a/lib/mvcfronk/mfBase/mfBaseController.php +++ b/lib/mvcfronk/mfBase/mfBaseController.php @@ -202,6 +202,13 @@ class mfBaseController return FronkDB::singleton($dbhost, $dbuser, $dbpass, $dbname); } + /** + * @param $mod + * @param $action + * @param $params + * @param $anker + * @return never-returns + */ public static function redirect($mod = false, $action = false, $params = false, $anker = false) { //var_dump($mod);