180 lines
6.2 KiB
PHP
180 lines
6.2 KiB
PHP
<?php
|
|
|
|
class PreorderApicontroller extends mfBaseApicontroller {
|
|
private $filter_gemeinde_ids = [];
|
|
private $campaign;
|
|
|
|
|
|
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() {
|
|
$campaignApiuser = PreordercampaignApiuserModel::getFirst(["worker_id" => $this->me->id]);
|
|
$campaign = new Preordercampaign($campaignApiuser->preordercampaign_id);
|
|
if($campaign) {
|
|
$this->campaign = $campaign;
|
|
foreach(PreordercampaignGemeindeModel::search(['preordercampaign_id' => $campaign->id]) as $gemeinde) {
|
|
$this->filter_gemeinde_ids[] = $gemeinde->id;
|
|
}
|
|
}
|
|
|
|
foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) {
|
|
$this->addAllowedOrigin($origin->hostname);
|
|
}
|
|
|
|
//var_dump($campaign, $this->allowed_origins);exit;
|
|
}
|
|
|
|
protected function submitPreorder() {
|
|
if(!$this->campaign) {
|
|
$this->log->debug("disallowed request because no campaign for apikey");
|
|
return mfResponse::Forbidden();
|
|
}
|
|
|
|
$type = $this->post['type'];
|
|
if($type != "interest" && $type != "provision") {
|
|
return mfResponse::BadRequest(["message" => "Unknown type"]);
|
|
}
|
|
|
|
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' => "ortschaft"] as $key => $field_name) {
|
|
if(property_exists($this->post['address'], $key)) {
|
|
$address_search[$field_name] = 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
|
|
|
|
foreach($address_search as $field => $value) {
|
|
$where .= " AND `$field` = '$value'";
|
|
}
|
|
|
|
$sql = "SELECT * FROM view_hausnummer WHERE 1=1 $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'";
|
|
}
|
|
|
|
$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)) {
|
|
return mfResponse::NotFound(['message' => "Wohneinheit nicht gefunden"]);
|
|
}
|
|
|
|
$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) {
|
|
$preorder_data['adb_wohneinheit_id'] = $unit->wohneinheit_id;
|
|
}
|
|
|
|
if($type == "provision") {
|
|
$product = $this->campaign->setup_products['provision'][0];
|
|
if($product) {
|
|
$preorder_data['setup_product_id'] = $product->id;
|
|
$preorder_data['price_setup'] = $product->price_setup;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
foreach(['company','uid','firstname','lastname','street','zip','city','phone','email'] as $key) {
|
|
if(property_exists($customer, $key)) {
|
|
$preorder_data[$key] = $customer->$key;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
}
|
|
|
|
} |