POST /preorder now finds the right salescluster from address

This commit is contained in:
Frank Schubert
2022-10-04 22:23:11 +02:00
parent 9b3335c820
commit 5c0d8df911

View File

@@ -1,8 +1,15 @@
<?php
class PreorderApicontroller extends mfBaseApicontroller {
private $filter_gemeinde_ids = [];
//private $filter_gemeinde_ids = [];
//private $campaign;
private $campaign;
private $campaigns = [];
private $filter_salescluster_ids = [];
private $campaigns_by_scluster = [];
private $allowed_preordertypes = [];
protected function init() {
@@ -14,24 +21,41 @@ class PreorderApicontroller extends mfBaseApicontroller {
}
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;
$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);
}
}
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->campaign) {
if(!$this->campaigns) {
$this->log->debug("disallowed request because no campaign for apikey");
return mfResponse::Forbidden();
}
@@ -117,6 +141,11 @@ class PreorderApicontroller extends mfBaseApicontroller {
$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 1=1 $where";
$res = $this->db()->query($sql);
if(!$this->db()->num_rows($res)) {
@@ -126,7 +155,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
$address = $this->db()->fetch_object($res);
/*
* search wohneinheit
*/
@@ -137,6 +166,11 @@ class PreorderApicontroller extends mfBaseApicontroller {
$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)) {
@@ -148,18 +182,33 @@ class PreorderApicontroller extends mfBaseApicontroller {
} 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)";
$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);
}
}
// 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'] = $this->campaign->id;
$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;
@@ -196,6 +245,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
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;