db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME); $this->addRoute("/preorder", "submitPreorder", "POST"); $this->allowMissingOrigin = true; } protected function authenticated() { $campaignApiusers = PreordercampaignApiuserModel::search(["worker_id" => $this->me->id]); foreach($campaignApiusers as $campaignApiuser) { $campaign = new Preordercampaign($campaignApiuser->preordercampaign_id); if($campaign) { foreach(PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $campain_scluster) { if(!in_array($campain_scluster->salescluster_id, $this->filter_salescluster_ids)) { $this->filter_salescluster_ids[] = $campain_scluster->salescluster_id; } $this->campaigns_by_scluster[$campain_scluster->salescluster_id] = $campaign->id; } $this->campaigns[$campaign->id] = $campaign; // get allowed preordertypes if(is_array($campaign->types) && count($campaign->types)) { foreach($campaign->types as $type) { $this->allowed_preordertypes[] = $type->type; } } if($campaign->district_is_city == 1) { $this->district_is_city = true; } if($campaign->hausnummer_add_zusatz == 1) { $this->hausnummer_add_zusatz = true; } if($campaign->exist_is_error == 1) { $this->exist_is_error = true; } } foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) { $this->addAllowedOrigin($origin->hostname); } } $this->allowed_preordertypes = array_unique($this->allowed_preordertypes); //var_dump($campaign, $this->allowed_origins);exit; } protected function submitPreorder() { //var_dump($this->post);exit; if(!$this->campaigns) { $this->log->debug("disallowed request because no campaign for apikey"); return mfResponse::Forbidden(); } $type = $this->post['preorderType']; if($type != "interest" && $type != "provision" && $type != "order" && $type != "reorder") { return mfResponse::BadRequest(["message" => "Invalid preorderType"]); } $connection_type = false; switch($this->post['connectionType']) { case "single-dwelling": $connection_type = "single-dwelling"; break; case "multi-dwelling": $connection_type = "multi-dwelling"; break; case "apartment-building": $connection_type = "apartment-building"; break; case "apartment": $connection_type = "apartment"; break; case "business": $connection_type = "business"; break; default: return mfResponse::BadRequest(["message" => "Invalid connectionType"]); } if(!array_key_exists("address", $this->post)) { return mfResponse::BadRequest(['message' => "address missing"]); } if(!array_key_exists("customer", $this->post)) { return mfResponse::BadRequest(['message' => "customer data missing"]); } /* * 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"]); } $address_search = []; $address_search_fields = [ 'street' => 'strasse', 'housenumber' => "hausnummer", 'zip' => "plz" ]; if($this->district_is_city) { $address_search_fields['city'] = "ortschaft"; } else { $address_search_fields['city'] = "gemeinde"; } foreach($address_search_fields as $key => $field_name) { if(property_exists($this->post['address'], $key)) { $address_search[$field_name] = $this->db()->escape(trim($this->post['address']->$key)); } } $zusatz = false; if($this->hausnummer_add_zusatz) { $m = []; if(preg_match('/^(.*) \((.+)\)$/', trim($address_search['hausnummer']), $m)) { $address_search['hausnummer'] = $this->db()->escape($m[1]); $zusatz = $m[2]; } } $unit_search = []; foreach(['block','stiege','stock','tuer'] as $key) { if(property_exists($this->post['address'], $key) && trim($this->post['address']->$key)) { $unit_search[$key] = trim($this->post['address']->$key); } } if($zusatz) { $unit_search['zusatz'] = $zusatz; } /* * check customer */ $customer = $this->post['customer']; 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"]); } /* * search address in AddressDB */ $where = "1=1 "; foreach($address_search as $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"]); } $address = $this->db()->fetch_object($res); //var_dump($address);exit; /* * search wohneinheit */ $where = "1=1 "; $unit = false; if(count($unit_search)) { //var_dump($unit_search);exit; foreach($unit_search as $field => $value) { if($field == "stock" || $field == "stiege") continue; // only check for block and tuer $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_wohneinheit WHERE $where AND hausnummer_id=".$address->hausnummer_id; $this->log->debug($sql); $res = $this->db()->query($sql); if($this->db()->num_rows($res)) { $unit = $this->db()->fetch_object($res); //return mfResponse::NotFound(['message' => "Wohneinheit nicht gefunden"]); } //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 $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)"; if($zusatz) { $where .= " AND zusatz='$zusatz'"; } else { $where .= " AND (zusatz = '' OR zusatz IS NULL)"; } // filter salesclusters if(count($this->filter_salescluster_ids)) { $where .= " AND netzgebiet_id IN (".implode(',', $this->filter_salescluster_ids).")"; } $sql = "SELECT * FROM view_wohneinheit WHERE $where"; $this->log->debug($sql); $res = $this->db()->query($sql); if($this->db()->num_rows($res)) { $unit = $this->db()->fetch_object($res); } } if($this->exist_is_error) { /* * check if there is an existing preorder for this unit already */ if($unit) { $existing_preorder = PreorderModel::getFirst(['adb_wohneinheit_id' => $unit->wohneinheit_id]); if($existing_preorder) { return mfResponse::Forbidden(['message' => "Für diese Wohneinheit liegt bereits eine Bestellung vor"]); } } } $address_info = $this->db()->escape(trim($this->post['address_info'])); // get correct campaign by salescluster if(!array_key_exists($address->netzgebiet_id, $this->campaigns_by_scluster)) { return mfResponse::NotFound(['message' => "Adresse nicht gefunden"]); } $campaign_id = $this->campaigns_by_scluster[$address->netzgebiet_id]; $this->campaign = new Preordercampaign($campaign_id); /* * build fields */ $preorder_data = []; $preorder_data['preordercampaign_id'] = $campaign_id; $preorder_data['type'] = $type; $preorder_data['connection_type'] = $connection_type; $preorder_data['connection_count'] = (intval($this->post['connectionCount'])) ? intval($this->post['connectionCount']) : 1; $preorder_data['submit_type'] = "api"; if($this->request_json) { $preorder_data['submit_request'] = $this->request_json; } $preorder_data['adb_hausnummer_id'] = $address->hausnummer_id; $preorder_data['partner_id'] = $this->me->address_id; if($unit) { $preorder_data['adb_wohneinheit_id'] = $unit->wohneinheit_id; } if($address_info) { $preorder_data['address_info'] = $address_info; } if(property_exists($this->post['address'],"district") && $this->post['address']->district) { $preorder_data['address_district'] = $this->post['address']->district; } if($this->post['acceptAgb'] === true) { $preorder_data['accept_agb'] = 1; } if($this->post['acceptDsgvo'] === true) { $preorder_data['accept_dsgvo'] = 1; } if($this->post['acceptMarketing'] === true) { $preorder_data['accept_marketing'] = 1; } if($this->post['acceptWithdrawal'] === true) { $preorder_data['accept_withdrawal'] = 1; } /* * setup price */ $product = false; if($type == "provision") { $product = $this->campaign->setup_products['provision'][0]; } if($type == "order") { $product = $this->campaign->setup_products['activation'][0]; } if($type == "reorder") { $product = $this->campaign->setup_products['reorder'][0]; } if($product) { $preorder_data['setup_product_id'] = $product->id; $preorder_data['price_setup'] = $product->price_setup; if($connection_type == "multi-dwelling") { if($preorder_data['connection_count'] == 2) { //$preorder_data['price_setup'] = round($product->price_setup * 2 - (($product->price_setup * 2) / 100) * TT_PREORDER_DISCOUNT_2); $preorder_data['price_setup'] = ($product->attributes["presales_setup_price2"]->value) ? $product->attributes["presales_setup_price2"]->value : $product->price_setup; } if($connection_type == "multi-dwelling" && $preorder_data['connection_count'] == 3) { //$preorder_data['price_setup'] = round($product->price_setup * 3 - (($product->price_setup * 3) / 100) * TT_PREORDER_DISCOUNT_3); $preorder_data['price_setup'] = ($product->attributes["presales_setup_price3"]->value) ? $product->attributes["presales_setup_price3"]->value : $product->price_setup; } } if($connection_type == "apartment" && $type == "order") { //$preorder_data['price_setup'] = round($product->price_setup - (($product->price_setup) / 100) * TT_PREORDER_DISCOUNT_APART); $preorder_data['price_setup'] = ($product->attributes["presales_setup_price4"]->value) ? $product->attributes["presales_setup_price4"]->value : $product->price_setup; } if($connection_type == "business") { //$preorder_data['price_setup'] = round($product->price_setup - (($product->price_setup) / 100) * TT_PREORDER_DISCOUNT_BUSINESS); $preorder_data['price_setup'] = ($product->attributes["presales_setup_price_business"]->value) ? $product->attributes["presales_setup_price_business"]->value : $product->price_setup; } } /* * get customer data */ foreach(['company','uid','firstname','lastname','street','zip','city','phone','email'] as $key) { if(property_exists($customer, $key)) { $preorder_data[$key] = $customer->$key; } } /* * check optional required fields */ if(is_array($this->campaign->required_fields) && count($this->campaign->required_fields)) { if(in_array("contact_type", $this->campaign->required_fields)) { if($customer->type == "tenant") { $preorder_data['contact_type'] = "tenant"; } elseif($customer->type == "owner") { $preorder_data['contact_type'] = "owner"; } else { return mfResponse::BadRequest(["message" => "customer type must be 'tenant' or 'owner'"]); } } } /* * check for addon services */ if(array_key_exists("addonServices", $this->post)) { $addon_services = $this->post['addonServices']; if($addon_services) { $preorder_data['addon_services'] = json_encode($addon_services); } } /* * check for addon data */ if(array_key_exists("additionalData", $this->post)) { $addon_data = $this->post['additionalData']; if($addon_data) { $preorder_data['addon_data'] = json_encode($addon_data); } } /* * create preorder record */ $preorder = PreorderModel::create($preorder_data); $preorder->createUcode(); //var_dump($preorder);exit; $preorder_id = $preorder->save(); if(!$preorder_id || !$preorder->ucode) { return mfResponse::InternalServerError(); } $return = ["code" => $preorder->ucode]; if($addon_data) { $return["additionalData"] = $addon_data; } return mfResponse::Ok($return); } }