From f8e30e30d7f0fd673f334ef4a4d05fcb224099bd Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Mon, 19 Jun 2023 11:34:12 +0200 Subject: [PATCH] POST Preorder with OAID instead of address --- application/Api/v1/PreorderApicontroller.php | 117 ++++++++++++------- 1 file changed, 76 insertions(+), 41 deletions(-) diff --git a/application/Api/v1/PreorderApicontroller.php b/application/Api/v1/PreorderApicontroller.php index 3d57f7049..cb59c0c8e 100644 --- a/application/Api/v1/PreorderApicontroller.php +++ b/application/Api/v1/PreorderApicontroller.php @@ -255,7 +255,6 @@ class PreorderApicontroller extends mfBaseApicontroller { } } - if(!array_key_exists("address", $this->post)) { return mfResponse::BadRequest(['message' => "address missing"]); } @@ -264,6 +263,24 @@ class PreorderApicontroller extends mfBaseApicontroller { return mfResponse::BadRequest(['message' => "customer data missing"]); } + $request_oaid = false; + $request_oaid_hausnummer = false; + $request_oaid_unit = false; + if(property_exists($this->post['address'],"oaid")) { + $request_oaid = trim($this->post['address']->oaid); + + $m = []; + if($request_oaid && preg_match('/^([a-z]{2}-\d+-[0-9a-f]+)\.(\d+)/i', $request_oaid, $m)) { + if(array_key_exists(1, $m)) { + $request_oaid_hausnummer = $m[1]; + } + + if(array_key_exists(2, $m)) { + $request_oaid_unit = "$request_oaid_hausnummer.".$m[2]; + } + } + } + $is_additional_order = false; if(array_key_exists("isAdditionalOrder", $this->post) && $this->post['isAdditionalOrder']) { $is_additional_order = true; @@ -272,12 +289,15 @@ class PreorderApicontroller extends mfBaseApicontroller { /************************************************************ * check address */ - if(!property_exists($this->post['address'],"street") || !$this->post['address']->street || - !property_exists($this->post['address'],"housenumber") || !$this->post['address']->housenumber || - !property_exists($this->post['address'],"zip") || !$this->post['address']->zip || - !property_exists($this->post['address'],"city") || !$this->post['address']->city - ) { - return mfResponse::BadRequest(['message' => "Mandatory address fields missing"]); + //var_dump($request_oaid_unit);exit; + if(!$request_oaid_unit) { + if(!property_exists($this->post['address'],"street") || !$this->post['address']->street || + !property_exists($this->post['address'],"housenumber") || !$this->post['address']->housenumber || + !property_exists($this->post['address'],"zip") || !$this->post['address']->zip || + !property_exists($this->post['address'],"city") || !$this->post['address']->city + ) { + return mfResponse::BadRequest(['message' => "Mandatory address fields missing"]); + } } $shipping_address = "customer"; @@ -346,53 +366,61 @@ class PreorderApicontroller extends mfBaseApicontroller { /************************************************************** * search address in AddressDB */ - $where = "1=1 "; - foreach($address_search as $field => $value) { - if($field == "ortschaft" || $field == "gemeinde") continue; - $where .= " AND `$field` = '$value'"; - } - - // filter salesclusters - if(count($this->filter_salescluster_ids)) { - $where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")"; - } - - $sql = "SELECT * FROM view_hausnummer WHERE $where"; - $this->log->debug($sql); - $res = $this->db()->query($sql); - if(!$this->db()->num_rows($res)) { - // try with Ortschaft in front of strasse (liezen gwr issue) + if($request_oaid_hausnummer) { + $res = $this->db()->select("view_hausnummer", "*", "oaid='$request_oaid_hausnummer'"); + if(!$this->db()->num_rows($res)) { + return mfResponse::NotFound(["message" => "OAID nicht gefunden"]); + } + $address = $this->db()->fetch_object($res); + } else { $where = "1=1 "; foreach($address_search as $field => $value) { if($field == "ortschaft" || $field == "gemeinde") continue; - - if($field == "strasse") { - $prefix = ($address_search['ortschaft']) ? $address_search['ortschaft'] : $address_search['gemeinde']; - if(strpos($value, "$prefix ") === 0) { - $str = substr($value, strlen($prefix)+1); - } else { - $str = $value; - } - $where .= " AND `strasse` = '$str'"; - } else { - $where .= " AND `$field` = '$value'"; - } + $where .= " AND `$field` = '$value'"; } // filter salesclusters if(count($this->filter_salescluster_ids)) { $where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")"; } - + $sql = "SELECT * FROM view_hausnummer WHERE $where"; $this->log->debug($sql); $res = $this->db()->query($sql); if(!$this->db()->num_rows($res)) { - return mfResponse::NotFound(['message' => "Adresse nicht gefunden"]); + // try with Ortschaft in front of strasse (liezen gwr issue) + $where = "1=1 "; + foreach($address_search as $field => $value) { + if($field == "ortschaft" || $field == "gemeinde") continue; + + if($field == "strasse") { + $prefix = ($address_search['ortschaft']) ? $address_search['ortschaft'] : $address_search['gemeinde']; + if(strpos($value, "$prefix ") === 0) { + $str = substr($value, strlen($prefix)+1); + } else { + $str = $value; + } + $where .= " AND `strasse` = '$str'"; + } else { + $where .= " AND `$field` = '$value'"; + } + } + + // filter salesclusters + if(count($this->filter_salescluster_ids)) { + $where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")"; + } + + $sql = "SELECT * FROM view_hausnummer WHERE $where"; + $this->log->debug($sql); + $res = $this->db()->query($sql); + if(!$this->db()->num_rows($res)) { + return mfResponse::NotFound(['message' => "Adresse nicht gefunden"]); + } } + + $address = $this->db()->fetch_object($res); } - - $address = $this->db()->fetch_object($res); //var_dump($address);exit; /* ************************************************************** @@ -402,7 +430,14 @@ class PreorderApicontroller extends mfBaseApicontroller { $assign_unit = false; $where = "1=1 "; $unit = false; - if(count($unit_search)) { + + if($request_oaid_unit) { + $unit = ADBWohneinheitModel::getFirst(["oaid" => $request_oaid_unit]); + if(!$unit) { + return mfResponse::NotFound(["message" => "Wohneinheit nicht gefunden"]); + } + + } elseif(count($unit_search)) { //var_dump($unit_search);exit; if(array_key_exists("unit_string", $unit_search)) { $where .= " AND bezeichner LIKE '".$unit_search['unit_string']."'"; @@ -638,7 +673,7 @@ class PreorderApicontroller extends mfBaseApicontroller { } } } - + // get units excluding unavailable units $where = "hausnummer_id=".$address->hausnummer_id; $where .= " AND ( block = '' OR block IS NULL)";