From 33ff1273ed25e64eb5f4187bf50bc7b3b24c994c Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 6 Dec 2022 12:43:42 +0100 Subject: [PATCH] Added GET /preorder api endpoint --- application/Api/v1/PreorderApicontroller.php | 26 ++++ application/Preorder/Preorder.php | 83 +++++++++++ application/Preorderstatus/Preorderstatus.php | 16 ++ .../Preorderstatus/PreorderstatusModel.php | 140 ++++++++++++++++++ 4 files changed, 265 insertions(+) create mode 100644 application/Preorderstatus/Preorderstatus.php create mode 100644 application/Preorderstatus/PreorderstatusModel.php diff --git a/application/Api/v1/PreorderApicontroller.php b/application/Api/v1/PreorderApicontroller.php index 63a252640..b291013ce 100644 --- a/application/Api/v1/PreorderApicontroller.php +++ b/application/Api/v1/PreorderApicontroller.php @@ -18,6 +18,7 @@ class PreorderApicontroller extends mfBaseApicontroller { $db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); $this->addRoute("/preorder", "submitPreorder", "POST"); + $this->addRoute("/preorder/:code", "getPreorder", "GET"); $this->allowMissingOrigin = true; } @@ -69,6 +70,31 @@ class PreorderApicontroller extends mfBaseApicontroller { //var_dump($campaign, $this->allowed_origins);exit; } + protected function getPreorder($code) { + $code = trim(strtoupper($code)); + if(!$code) { + return mfResponse::NotFound(["message" => "Preorder not found"]); + } + + $preorder = PreorderModel::getFirst(['ucode' => $code]); + if(!$preorder) { + // try oan id + $preorder = PreorderModel::getFirst(['oaid' => $code]); + if(!$preorder) { + return mfResponse::NotFound(["message" => "Preorder not found"]); + } + } + + $return = $preorder->getApiArray(); + if(!$return) { + return mfResponse::NotFound(["message" => "Preorder not found"]); + } + + + return mfResponse::Ok($return); + + } + protected function submitPreorder() { //var_dump($this->post);exit; if(!$this->campaigns) { diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 73f4e89ae..d56d3ece9 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -2,6 +2,7 @@ class Preorder extends mfBaseModel { protected $forcestr = ['street','company','zip','phone','email','note']; + private $status; private $campaign; private $partner; private $building; @@ -33,7 +34,78 @@ class Preorder extends mfBaseModel { } return $ucode; } + + public function getApiArray() { + if(!$this->id) { + return false; + } + + $hausnummer = $this->getProperty("adb_hausnummer"); + $wohneinheit = $this->getProperty("adb_wohneinheit"); + + $a = []; + $a['ucode'] = strtoupper($this->ucode); + $a['oaid'] = $this->oaid; + $a['status'] = $this->getProperty("status")->getApiArray(); + $a['connectionType'] = $this->connection_type; + $a['connectionCount'] = ($this->connection_count) ? (int)$this->connection_count : 1; + $a['preorderType'] = $this->type; + $a['acceptMarketing'] = ($this->accept_marketing) ? true : false; + $a['acceptAgb'] = ($this->accept_agb) ? true : false; + $a['acceptDsgvo'] = ($this->accept_dsgvo) ? true : false; + $a['acceptWithdrawal'] = ($this->accept_withdrawal) ? true : false; + $a['address_info'] = ($this->address_info) ? $this->address_info : null; + + $address = []; + $address['street'] = $hausnummer->strasse->name; + $address['housenumber'] = $hausnummer->hausnummer; + $address['zip'] = $hausnummer->plz->plz; + $address['city'] = $hausnummer->strasse->gemeinde->name; + $address['district'] = $hausnummer->ortschaft->name; + $address['bock'] = ($wohneinheit->block) ? $wohneinheit->block : null; + $address['stock'] = ($wohneinheit->stock) ? $wohneinheit->stock : null; + $address['stiege'] = ($wohneinheit->stiege) ? $wohneinheit->stiege : null; + $address['tuer'] = ($wohneinheit->tuer) ? $wohneinheit->tuer : null; + + $customer = []; + $customer['company'] = ($this->company) ? $this->company : null; + $customer['uid'] = ($this->uid) ? $this->uid : null; + $customer['firstnam'] = ($this->firstname) ? $this->firstname : null; + $customer['lastname'] = ($this->lastname) ? $this->lastname : null; + $customer['street'] = ($this->street) ? $this->street : null; + $customer['housenumber'] = ($this->hausnummer) ? $this->hausnummer : null; + $customer['zip'] = ($this->plz) ? $this->plz : null; + $customer['city'] = ($this->city) ? $this->city : null; + $customer['bock'] = ($this->block) ? $this->block : null; + $customer['stock'] = ($this->stock) ? $this->stock : null; + $customer['stiege'] = ($this->stiege) ? $this->stiege : null; + $customer['tuer'] = ($this->tuer) ? $this->tuer : null; + $customer['phone'] = ($this->phone) ? $this->phone : null; + $customer['email'] = ($this->email) ? $this->email : null; + + $a['address'] = $address; + $a['customer'] = $customer; + + $a['addonServices'] = null; + if($this->addon_services) { + $addon_services = json_decode($this->addon_services); + if(json_last_error() === JSON_ERROR_NONE) { + $a['addonServices'] = $addon_services; + } + } + + $a['additionalData'] = null; + if($this->addon_data) { + $addon_data = json_decode($this->addon_data); + if(json_last_error() === JSON_ERROR_NONE) { + $a['additionalData'] = $addon_data; + } + } + + return $a; + } + public function getProperty($name) { if($this->$name == null) { @@ -42,6 +114,17 @@ class Preorder extends mfBaseModel { return $this->campaign; } + if($name == "status") { + $this->status = mfValuecache::singleton()->get("mfObjectmodel-Preorderstatus-".$this->status_id); + if(!$this->status) { + $this->status = new Preorderstatus($this->status_id); + if($this->status->id) { + mfValuecache::singleton()->set("mfObjectmodel-Preorderstatus-".$this->status_id, $this->status); + } + } + return $this->status; + } + if($name == "partner") { $this->partner = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->partner_id); if(!$this->partner) { diff --git a/application/Preorderstatus/Preorderstatus.php b/application/Preorderstatus/Preorderstatus.php new file mode 100644 index 000000000..391d61669 --- /dev/null +++ b/application/Preorderstatus/Preorderstatus.php @@ -0,0 +1,16 @@ +id) { + return false; + } + + $a = []; + $a['code'] = (int)$this->code; + $a['text'] = $this->name; + + return $a; + } +} \ No newline at end of file diff --git a/application/Preorderstatus/PreorderstatusModel.php b/application/Preorderstatus/PreorderstatusModel.php new file mode 100644 index 000000000..40f2e81ed --- /dev/null +++ b/application/Preorderstatus/PreorderstatusModel.php @@ -0,0 +1,140 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + if(substr($field, 0, 5) == "vlan_" && !$value) { + $model->$field = null; + continue; + } + $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 getAll() { + $items = []; + + $db = FronkDB::singleton(); + + $res = $db->select("Preorderstatus", "*"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new Preorderstatus($data); + } + } + return $items; + + } + + public static function getFirst($filter = false) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $res = $db->select("Preorderstatus", "*", "$where ORDER BY code, name"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new Preorderstatus($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 COUNT(*) as cnt FROM Preorderstatus + 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) { + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + + $sql = "SELECT * FROM Preorderstatus + WHERE $where + ORDER BY code, name + "; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($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[] = new Preorderstatus($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + //var_dump($filter);exit; + + if(array_key_exists("code", $filter)) { + $code = $filter['code']; + if(is_numeric($code)) { + $where .= " AND code=$code"; + } + } + + + //var_dump($filter, $where);exit; + return $where; + } + +}