diff --git a/Layout/default/Preorder/Form.php b/Layout/default/Preorder/Form.php index d71fb9e33..bd426ceee 100644 --- a/Layout/default/Preorder/Form.php +++ b/Layout/default/Preorder/Form.php @@ -301,7 +301,6 @@ } - $("#product_id").select2({ allowClear: true, placeholder: "" @@ -392,6 +391,7 @@ .data("tuer", unit.tuer) .data("zusatz", unit.zusatz); + afterHausnummerChange(); }); @@ -453,10 +453,24 @@ } }); + + adb_hausnummer_id): ?> + console.log("Hausnummer"); + $('#adb_hausnummer_id').trigger("change"); + // afterHausnummerChange() will be called by change event + }); + function afterHausnummerChange() { + adb_wohneinheit_id): ?> + console.log("selecting wohneinheit"); + $('#adb_wohneinheit_id option[value=adb_wohneinheit_id?>]').prop("selected", true); + $('#adb_wohneinheit_id').trigger("change"); + + } + function updateSetupProduct(type) { if(type == "provision") { $('#setup_product_id').val($("#setup-provision select").val()); diff --git a/Layout/default/Preorder/Index.php b/Layout/default/Preorder/Index.php index 206de80c5..b289d5f61 100644 --- a/Layout/default/Preorder/Index.php +++ b/Layout/default/Preorder/Index.php @@ -55,7 +55,7 @@ adb_hausnummer->strasse->name?> adb_hausnummer->hausnummer?>
- adb_wohneinheit_id) ? $preorder->adb_wohneinheit."
" : ""?> + adb_wohneinheit_id) ? ((string)$preorder->adb_wohneinheit ? $preorder->adb_wohneinheit."
" : "") : "<keine Wohneinheit>
"?> adb_hausnummer->plz->plz?> adb_hausnummer->strasse->ortschaft->name?> diff --git a/application/Api/v1/PreorderApicontroller.php b/application/Api/v1/PreorderApicontroller.php index 4c522fc31..f05cbe86b 100644 --- a/application/Api/v1/PreorderApicontroller.php +++ b/application/Api/v1/PreorderApicontroller.php @@ -37,7 +37,7 @@ class PreorderApicontroller extends mfBaseApicontroller { } $type = $this->post['type']; - if($type != "provision" && $type != "order") { + if($type != "interest" && $type != "provision") { return mfResponse::BadRequest(["message" => "Unknown type"]); } @@ -50,10 +50,10 @@ class PreorderApicontroller extends mfBaseApicontroller { } // check address - if(!property_exists($this->post['address'],"street") || - !property_exists($this->post['address'],"housenumber") || - !property_exists($this->post['address'],"zip") || - !property_exists($this->post['address'],"city") + 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"]); } @@ -67,20 +67,19 @@ class PreorderApicontroller extends mfBaseApicontroller { $unit_search = []; foreach(['block','stiege','stock','tuer'] as $key) { - if(property_exists($this->post['address'], $key) && trim($this->post['address']->key)) { + if(property_exists($this->post['address'], $key) && trim($this->post['address']->$key)) { $unit_search[$key] = trim($this->post['address']->$key); } } - + // check customer - $customer = $this->post['customer']; - if(!property_exists($customer,"firstname") || - !property_exists($customer,"lastname") || - !property_exists($customer,"street") || - !property_exists($customer,"zip") || - !property_exists($customer,"city") + if(!property_exists($customer,"firstname") || !$customer->firstname || + !property_exists($customer,"lastname") || !$customer->lastname || + !property_exists($customer,"street") || !$customer->street || + !property_exists($customer,"zip") || !$customer->zip || + !property_exists($customer,"city") || !$customer->city ) { return mfResponse::BadRequest(['message' => "Mandatory customer fields missing"]); } @@ -120,12 +119,24 @@ class PreorderApicontroller extends mfBaseApicontroller { $unit = $this->db()->fetch_object($res); //var_dump($this->db()->num_rows($res), $this->db()->fetch_object($res)); - + } else { + // if all unit values are empty try to find the unit with all empty values + // failure is not an error, but must be checked by a human at some point + $sql = "SELECT * FROM view_wohneinheit WHERE hausnummer_id=".$address->hausnummer_id." AND (block = '' OR block IS NULL) AND (stiege = '' OR stiege IS NULL) AND (stock = '' OR stock IS NULL) AND (tuer = '' OR tuer IS NULL)"; + $res = $this->db()->query($sql); + if($this->db()->num_rows($res)) { + $unit = $this->db()->fetch_object($res); + } } + //var_dump($unit);exit; $preorder_data = []; $preorder_data['preordercampaign_id'] = $this->campaign->id; $preorder_data['type'] = $type; + $preorder_data['submit_type'] = "api"; + if($this->request_json) { + $preorder_data['submit_request'] = $this->request_json; + } $preorder_data['adb_hausnummer_id'] = $address->hausnummer_id; if($unit) { @@ -148,7 +159,19 @@ class PreorderApicontroller extends mfBaseApicontroller { } } - var_dump($preorder_data);exit; + + + $preorder = PreorderModel::create($preorder_data); + $preorder->createUcode(); + $preorder_id = $preorder->save(); + + if(!$preorder_id || !$preorder->ucode) { + return mfResponse::InternalServerError(); + } + + return mfResponse::Ok(["code" => $preorder->ucode]); + + var_dump($preorder);exit; var_dump($this->post);exit; diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 61e5a5255..03a07369c 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -8,6 +8,26 @@ class Preorder extends mfBaseModel { private $adb_hausnummer; private $adb_wohneinheit; + + public function createUcode() { + $ucode = $this->generateNewUcode(); + while(PreorderModel::search(['ucode' => $ucode])) { + $ucode = $this->generateNewUcode(); + } + $this->ucode = $ucode; + return $this->ucode; + } + + private function generateNewUcode() { + $chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; + $charsLength = strlen($chars); + $ucode = ''; + for ($i = 0; $i < 8; $i++) { + $ucode .= $chars[rand(0, $charsLength - 1)]; + } + return $ucode; + } + public function getProperty($name) { if($this->$name == null) { diff --git a/application/Preorder/PreorderModel.php b/application/Preorder/PreorderModel.php index c28dfda14..d4841d5ce 100644 --- a/application/Preorder/PreorderModel.php +++ b/application/Preorder/PreorderModel.php @@ -1,6 +1,7 @@ escape($filter['ucode']); + if($ucode) { + $where .= " AND ucode like '%$ucode%'"; + } + } + //var_dump($filter, $where);exit; return $where; diff --git a/lib/mvcfronk/mfBase/mfBaseApicontroller.php b/lib/mvcfronk/mfBase/mfBaseApicontroller.php index 7030566e6..464252e2e 100644 --- a/lib/mvcfronk/mfBase/mfBaseApicontroller.php +++ b/lib/mvcfronk/mfBase/mfBaseApicontroller.php @@ -18,6 +18,7 @@ class mfBaseApicontroller { protected $apiversion; protected $headers = []; protected $route; + protected $request_json; protected $get = []; protected $post = []; protected $format = "default"; @@ -143,6 +144,7 @@ class mfBaseApicontroller { $json_request = json_decode($body); if(json_last_error() === JSON_ERROR_NONE) { //var_dump((array)$json_request);exit; + $this->request_json = $body; return (array)$json_request; }