added POST /preorder Api

This commit is contained in:
Frank Schubert
2022-09-15 14:15:00 +02:00
parent 2cbdbf3813
commit 824f3a6ef2
6 changed files with 86 additions and 17 deletions

View File

@@ -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 @@
}
});
<?php if($preorder->adb_hausnummer_id): ?>
console.log("Hausnummer");
$('#adb_hausnummer_id').trigger("change");
// afterHausnummerChange() will be called by change event
<?php endif; ?>
});
function afterHausnummerChange() {
<?php if($preorder->adb_wohneinheit_id): ?>
console.log("selecting wohneinheit");
$('#adb_wohneinheit_id option[value=<?=$preorder->adb_wohneinheit_id?>]').prop("selected", true);
$('#adb_wohneinheit_id').trigger("change");
<?php endif; ?>
}
function updateSetupProduct(type) {
if(type == "provision") {
$('#setup_product_id').val($("#setup-provision select").val());

View File

@@ -55,7 +55,7 @@
<td>
<?=$preorder->adb_hausnummer->strasse->name?>
<?=$preorder->adb_hausnummer->hausnummer?><br />
<?=($preorder->adb_wohneinheit_id) ? $preorder->adb_wohneinheit."<br />" : ""?>
<?=($preorder->adb_wohneinheit_id) ? ((string)$preorder->adb_wohneinheit ? $preorder->adb_wohneinheit."<br />" : "") : "<i class='text-pink'>&lt;keine Wohneinheit&gt;</i><br />"?>
<?=$preorder->adb_hausnummer->plz->plz?>
<?=$preorder->adb_hausnummer->strasse->ortschaft->name?>
</td>

View File

@@ -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;

View File

@@ -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) {

View File

@@ -1,6 +1,7 @@
<?php
class PreorderModel {
public $ucode;
public $preordercampaign_id;
public $adb_hausnummer_id;
public $adb_wohneinheit_id;
@@ -26,6 +27,8 @@ class PreorderModel {
public $city;
public $phone;
public $email;
public $submit_type;
public $submit_request;
public $note;
@@ -155,6 +158,13 @@ class PreorderModel {
}
}
if(array_key_exists("ucode", $filter)) {
$ucode = FronkDB::singleton()->escape($filter['ucode']);
if($ucode) {
$where .= " AND ucode like '%$ucode%'";
}
}
//var_dump($filter, $where);exit;
return $where;

View File

@@ -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;
}