Files
thetool/application/Api/v1/PreorderApicontroller.php
2022-10-13 13:00:38 +02:00

306 lines
11 KiB
PHP

<?php
class PreorderApicontroller extends mfBaseApicontroller {
//private $filter_gemeinde_ids = [];
//private $campaign;
private $campaign;
private $campaigns = [];
private $filter_salescluster_ids = [];
private $campaigns_by_scluster = [];
private $allowed_preordertypes = [];
protected function init() {
$db = $this->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;
}
}
}
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() {
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 = [];
foreach(['street' => 'strasse','housenumber' => "hausnummer",'zip' => "plz",'city' => "gemeinde"] as $key => $field_name) {
if(property_exists($this->post['address'], $key)) {
$address_search[$field_name] = $this->db()->escape(trim($this->post['address']->$key));
}
}
$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);
}
}
/*
* 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";
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
//var_dump($this->db()->num_rows($res), $this->db()->fetch_object($res));
return mfResponse::NotFound(['message' => "Adresse nicht gefunden"]);
}
$address = $this->db()->fetch_object($res);
/*
* search wohneinheit
*/
$unit = false;
if(count($unit_search)) {
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 1=1 $where AND hausnummer_id=".$address->hausnummer_id;
$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)";
// 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";
$res = $this->db()->query($sql);
if($this->db()->num_rows($res)) {
$unit = $this->db()->fetch_object($res);
}
}
$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;
if($unit) {
$preorder_data['adb_wohneinheit_id'] = $unit->wohneinheit_id;
}
if($address_info) {
$preorder_data['address_info'] = $address_info;
}
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($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);
}
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);
}
}
if($connection_type == "apartment" && $type == "order") {
$preorder_data['price_setup'] = round($product->price_setup - (($product->price_setup) / 100) * TT_PREORDER_DISCOUNT_APART);
}
if($connection_type == "business") {
$preorder_data['price_setup'] = round($product->price_setup - (($product->price_setup) / 100) * TT_PREORDER_DISCOUNT_BUSINESS);
}
}
/*
* 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;
}
}
/*
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'"]);
}*/
/*
* 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 mfResponse::Ok(["code" => $preorder->ucode]);
}
}