diff --git a/Layout/default/Order/Form.php b/Layout/default/Order/Form.php index 356b9747c..5ff2e19c1 100644 --- a/Layout/default/Order/Form.php +++ b/Layout/default/Order/Form.php @@ -587,9 +587,6 @@ preorder_id): ?> - oaid): ?> - -
+
+
+ =$product->product->name?> =$product->oaid ? "{$product->oaid}" : ""?>
product->attributes) && count($product->product->attributes))
diff --git a/application/Order/Order.php b/application/Order/Order.php
index 1774f35fb..00960d6a5 100644
--- a/application/Order/Order.php
+++ b/application/Order/Order.php
@@ -67,6 +67,27 @@ class Order extends mfBaseModel {
//var_dump($this->terminations);exit;
return $terminations;
}
+
+ public function getSnoppProduct() {
+ foreach($this->getProperty("products") as $product) {
+ if($product->snopp_order_id) return $product;
+ }
+ return null;
+ }
+
+ public function getOaidProduct() {
+ foreach($this->getProperty("products") as $product) {
+ if($product->oaid) return $product;
+ }
+ return null;
+ }
+
+ public function getPreorderProduct() {
+ foreach($this->getProperty("products") as $product) {
+ if($product->preorder_id) return $product;
+ }
+ return null;
+ }
public function getShippingdate() {
if(!$this->id) {
diff --git a/application/Order/OrderController.php b/application/Order/OrderController.php
index 993cb2274..09c88cd3c 100644
--- a/application/Order/OrderController.php
+++ b/application/Order/OrderController.php
@@ -1344,7 +1344,7 @@ class OrderController extends mfBaseController {
$this->layout()->setFlash("Keine Berechtigung", "error");
$this->redirect("Order");
}
-
+
$r = $this->request;
$order_id = $r->id;
@@ -1372,7 +1372,146 @@ class OrderController extends mfBaseController {
$this->returnJson(["status" => "OK", "order" => ['id' => $order_id]]);
}
-
+
+ protected function createSnoppOrderAction() {
+ $order_id = $this->request->id;
+
+ if(!$order_id || $order_id < 1) {
+ $this->layout()->setFlash("Bestellung nicht gefunden.", "error");
+ $this->redirect("Order");
+ }
+ $order = new Order($order_id);
+ if(!$order->id) {
+ $this->layout()->setFlash("Bestellung nicht gefunden.", "error");
+ $this->redirect("Order");
+ }
+
+ $order_product = false;
+
+ $product_snopp_id = false;
+ $products_noterm = false;
+
+ // find product
+ foreach($order->products as $op) {
+ // check for valid internet access product
+ if(!in_array($op->product->producttech_id, TT_PRODUCTTECH_IDS_INTERNET_ACCESSS)) {
+ continue;
+ }
+
+ if($op->oaid) {
+ $order_product = $op;
+ break;
+ }
+ if($op->preorder_id) {
+ $order_product = $op;
+ break;
+ }
+ // if product has a snopp product_id, then this must be it
+ if($op->product->getAttributeValue("oan_pid_snopp")) {
+ $order_product = $op;
+ break;
+ }
+ }
+
+ if(!$order_product) {
+ $this->layout()->setFlash("Kein für SNOPP-Bestellungen geeignetes Produkt in dieser Bestellung gefunden.", "error");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+ }
+
+ // order in snopp
+ $snopp_prod_id = $op->product->getAttributeValue("oan_pid_snopp");
+ if(!$snopp_prod_id) {
+ $this->layout()->setFlash("SNOPP Product ID fehlt im Produkt (".$order_product->product->name.").", "error");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+ }
+
+ // find snopp api credentials
+ $api_creds = $order_product->product->getOwnerSnoppApiCredentials();
+ $this->log->debug(__METHOD__.": Snopp Api Creds: ".print_r($api_creds, true));
+ if(!$api_creds) {
+ $this->layout()->setFlash("Produktbesitzer hat keinen SNOPP Api Key", "error");
+ $this->redirect("Order");
+ }
+ $baseurl = $api_creds["prod"]["url"];
+ $apikey = $api_creds["prod"]["key"];
+ $snopp = new Snoppapi($baseurl, $apikey);
+
+ $ext_id = false;
+
+ if($order_product->oaid) {
+ $ext_id = $order_product->oaid;
+ } elseif($order_product->preorder_id) {
+ $preorder = new Preorder($order_product->preorder_id);
+ if($preorder->id && $preorder->campaign->fulfillment == "citycom_oan") {
+ $ext_id = "SDIHome_xtc{$preorder->adb_wohneinheit_id}_1700000000";
+ } elseif($preorder->id) {
+ if($preorder->adb_wohneinheit->oaid) {
+ $ext_id = $preorder->adb_wohneinheit->oaid;
+ } elseif($preorder->adb_wohneinheit->extref) {
+ $ext_id = $preorder->adb_wohneinheit->extref;
+ }
+ }
+ } else {
+ // search for address in snopp
+ $search_data = [
+ "street" => $order->owner->street,
+ "zip" => $order->owner->zip,
+ "city" => $order->owner->city,
+ ];
+ $homes = $snopp->searchAddress($search_data);
+ if(!$homes) {
+ $this->layout()->setFlash("Home in Snopp nicht gefunden", "error");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+ }
+
+ $home = reset($homes);
+ $ext_id = $home->oan_id;
+ }
+
+ if(!$ext_id) {
+ $this->layout()->setFlash("Konnte keine OAID oder External ID zur Adresse finden.", "error");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+ }
+
+ $data = [
+ "oan_id" => ($order_product->oaid) ?: $ext_id,
+ "product_id" => $snopp_prod_id,
+ "extref" => ($op->order->partner_number) ?: $op->order->owner->customer_number,
+ "execution_date" => (new DateTime("now"))->setTimezone(new DateTimeZone("Europe/Vienna"))->format("c"),
+ "name" => $order->owner->getCompanyOrName(),
+ "street" => $order->owner->street,
+ "zip" => $order->owner->zip,
+ "city" => $order->owner->city,
+ "phone" => $order->owner->phone,
+ "mobile" => $order->owner->mobile,
+ "email" => $order->owner->email,
+ ];
+
+
+ $resp = $snopp->submitOrder($data);
+
+ if(!$resp) {
+ $this->layout()->setFlash("Fehler beim Bestellen im Snopp.", "error");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+ }
+
+ if($resp->status != "Created") {
+ $this->layout()->setFlash("Konnte nicht bestellt werden: '{$resp->result->message}'", "error");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+ }
+
+ if($resp->result->order_id) {
+ $order_product->snopp_order_id = $resp->result->order_id;
+ } else {
+ $order_product->snopp_order_id = 1;
+ }
+ $order_product->save();
+
+ $this->layout()->setFlash("Bestellung erfolgreich in SNOPP erstellt.", "success");
+ $this->redirect("Order", "Index", ["id" => $order->id]);
+
+ }
+
protected function deleteAction() {
if(!$this->me->is(["Admin","salespartner"])) {
$this->layout()->setFlash("Keine Berechtigung", "error");
diff --git a/application/Product/Product.php b/application/Product/Product.php
index 9bbcd6d9d..eff806c3a 100644
--- a/application/Product/Product.php
+++ b/application/Product/Product.php
@@ -54,6 +54,28 @@ class Product extends mfBaseModel {
}
+ public function getAttributeValue($name) {
+ $attributes = $this->getProperty("attributes");
+ if(!array_key_exists($name, $attributes) || !$attributes[$name]->value) {
+ return null;
+ }
+ return $attributes[$name]->value;
+ }
+
+ public function getOwnerSnoppApiCredentials() {
+ $owner = $this->getProperty("owner");
+ if(!$owner) return false;
+
+ foreach(TT_SNOPP_API_CREDS as $api_creds) {
+ if($api_creds["address_id"] == $owner->id) {
+ return $api_creds;
+ }
+ }
+
+ return null;
+ }
+
+
public function getProperty($name) {
if($this->$name == null) {
diff --git a/lib/SNOPP/SNOPP.php b/lib/SNOPP/SNOPP.php
deleted file mode 100644
index d995ea3e5..000000000
--- a/lib/SNOPP/SNOPP.php
+++ /dev/null
@@ -1,36 +0,0 @@
- [
- 'method' => 'GET',
- 'header' => "X-Api-Key: $SNOPP_API_KEY\r\n",
- ]
- ];
-
- $snopp_output = file_get_contents("$SNOPP_API_URL/ticket/find?provider_id=all&status=open", false, stream_context_create($ctx_opts));
- $ticket_obj = json_decode($snopp_output);
- $tickets = $ticket_obj->result->tickets;
-
- return $tickets;
- }
-
-}
-?>
diff --git a/lib/Snoppapi/Snoppapi.php b/lib/Snoppapi/Snoppapi.php
new file mode 100644
index 000000000..2b7aedaca
--- /dev/null
+++ b/lib/Snoppapi/Snoppapi.php
@@ -0,0 +1,111 @@
+baseurl = $baseurl;
+ $this->apikey = $apikey;
+
+ $this->log = mfLoghandler::singleton();
+ }
+
+
+ public function searchAddress(Array $address_data) {
+ $street = trim($address_data['street']);
+ $zip = trim($address_data['zip']);
+ $city = trim($address_data['city']);
+
+ $ctx_opts = [
+ 'http' => [
+ 'method' => 'GET',
+ 'ignore_errors' => true,
+ 'header' => [
+ "Accept: application/json",
+ "X-Api-Key: {$this->apikey}",
+ ]
+ ]
+ ];
+
+ $getHomesEp = $this->baseurl.SNOPP_API_EP_GET_HOMES;
+ //$url = $getHomesEp."?".$qs;
+ $url = $getHomesEp;
+
+ $ctx = stream_context_create($ctx_opts);
+ $this->log->debug(__METHOD__.": Getting SNOPP homes: $url");
+ $response = file_get_contents($url, false, $ctx);
+ //var_dump($response);exit;
+ if($response === false) {
+ $this->log->error("Fehler beim Homes abfragen in SNOPP");
+ return false;
+ }
+
+ $resp_data = json_decode($response);
+ $homes = $resp_data->result->homes;
+
+ $results = [];
+
+ foreach($homes as $home) {
+ if(trim($home->street) == $street
+ && trim($home->zip) == $zip
+ && trim($home->city) == $city
+ ) {
+ $results[] = $home;
+ }
+ }
+
+ return $results;
+ }
+
+ public function submitOrder(Array $order_data) {
+ $data = [];
+ foreach(["oan_id", "execution_date", "product_id", "name", "street", "zip", "city"] as $field) {
+ if(!array_key_exists($field, $order_data) || !$order_data[$field]) {
+ $this->log->error(__METHOD__.": Mandatory field '$field' missing");
+ return false;
+ }
+ $data[$field] = $order_data[$field];
+ }
+
+ foreach(["phone", "mobile", "email"] as $field) {
+ if(array_key_exists($field, $order_data) && $order_data[$field]) {
+ $data[$field] = $order_data[$field];
+ }
+ }
+
+ $ctx_opts = [
+ 'http' => [
+ 'method' => 'POST',
+ 'content' => json_encode($data),
+ 'ignore_errors' => true,
+ 'header' => [
+ "Accept: application/json",
+ "Content-type: application/json",
+ "X-Api-Key: {$this->apikey}",
+ ]
+ ]
+ ];
+
+ $url = $this->baseurl.SNOPP_API_EP_SUBMIT_ORDER;
+
+ $ctx = stream_context_create($ctx_opts);
+ $this->log->debug(__METHOD__.": Ordering Snopp product: $url\n".print_r($data, true));
+ $response = file_get_contents($url, false, $ctx);
+
+ $this->log->debug(__METHOD__.": ".print_r($response, true));
+
+ if($response === false) {
+ $this->log->error("Fehler beim Bestellen in SNOPP ".print_r($response, true));
+ return false;
+ }
+
+ $resp_data = json_decode($response);
+ return $resp_data;
+ }
+
+}