WIP Snopp Order

This commit is contained in:
Frank Schubert
2026-01-22 17:07:51 +01:00
parent 225e14c74d
commit 976844d623
7 changed files with 334 additions and 47 deletions

View File

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

View File

@@ -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,142 @@ 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]);
}
$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");