Button to create Order from Preorder
This commit is contained in:
@@ -1048,6 +1048,157 @@ class PreorderController extends mfBaseController {
|
||||
$this->layout()->set("no_filename", false);
|
||||
}
|
||||
|
||||
protected function createOrderFromPreorderAction() {
|
||||
$preorder_id = $this->request->preorder_id;
|
||||
|
||||
if(!is_numeric($preorder_id) || $preorder_id < 1) {
|
||||
$this->layout()->setFlash("Vorbestellung nicht gefunden!", "error");
|
||||
$this->redirect("Preorder", "Index");
|
||||
}
|
||||
|
||||
$preorder = new Preorder($preorder_id);
|
||||
if(!$preorder->id) {
|
||||
$this->layout()->setFlash("Vorbestellung nicht gefunden!", "error");
|
||||
$this->redirect("Preorder", "Index");
|
||||
}
|
||||
|
||||
$order_data = [];
|
||||
$order_data["preorder_id"] = $preorder->id;
|
||||
|
||||
$owner_data = [];
|
||||
foreach(["company","uid","firstname","lastname","street","zip","city","phone","email"] as $field) {
|
||||
if(!trim($preorder->$field)) {
|
||||
$owner_data[$field] = "";
|
||||
}
|
||||
$owner_data[$field] = trim($preorder->$field);
|
||||
}
|
||||
|
||||
// search owner in Address and add owner_id ...
|
||||
$owner = false;
|
||||
$owners = AddressModel::search($owner_data);
|
||||
foreach($owners as $o) {
|
||||
if(!$this->me->is("employee")) {
|
||||
// external salespartners must not use addresses with customer_number
|
||||
if($o->customer_number) continue;
|
||||
// otherwise use with address
|
||||
$owner = $o;
|
||||
} else {
|
||||
// every address can be used as fallback
|
||||
$owner = $o;
|
||||
|
||||
// if we are employees, customers with customer_number and fibu_primary_account have precedence
|
||||
// but still use addresses with only customer_number as fallback
|
||||
if($o->customer_number) {
|
||||
$owner = $o;
|
||||
if($o->fibu_primary_account) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($owner && $owner->id) {
|
||||
$order_data["owner_id"] = $owner->id;
|
||||
$order_data["owner"] = $owner;
|
||||
} else {
|
||||
// ... otherwise add owner data to order
|
||||
foreach($owner_data as $field => $value) {
|
||||
if(!$preorder->$field) continue;
|
||||
$order_data["owner_".$field] = $value;
|
||||
}
|
||||
$order_data["new_owner"] = 1;
|
||||
}
|
||||
|
||||
if($preorder->order_date) {
|
||||
$order_data["order_date"] = $preorder->order_date;
|
||||
} else {
|
||||
$order_data["order_date"] = $preorder->create;
|
||||
}
|
||||
|
||||
$operator = false;
|
||||
$campaign = $preorder->campaign;
|
||||
if(is_array($campaign->active_operators) && count($campaign->active_operators)) {
|
||||
$campaign_operator = reset($campaign->active_operators);
|
||||
$operator = $campaign_operator->operator;
|
||||
}
|
||||
|
||||
if(!$operator) {
|
||||
$this->layout()->setFlash("Kampagne hat keinen Netzbetreiber!", "error");
|
||||
$this->redirect("Preorder", "Index", ["filter" => ["preordercampaign_id" => $campaign->id]], "preorder=$preorder_id");
|
||||
}
|
||||
|
||||
// try product with correct network id
|
||||
$product = ProductModel::getFirst(["external_id" => $operator->id, "network_id" => $campaign->network_id]);
|
||||
if(!$product) {
|
||||
// else use any product from operator
|
||||
$product = ProductModel::getFirst(["external_id" => $operator->id, "productgroup_id" => TT_PRODUCTGROUP_ID_INTERNET_ACCESS_RESI, "active" => true]);
|
||||
}
|
||||
if($operator->id == 1) {
|
||||
if(!$product) {
|
||||
$product = ProductModel::getFirst([
|
||||
"external" => 0,
|
||||
"productgroup_id" => TT_PRODUCTGROUP_ID_INTERNET_ACCESS_RESI,
|
||||
"network_id" => $campaign->network_id,
|
||||
"attributename" => "termination_required",
|
||||
"attributevalue" => 0,
|
||||
"active" => true
|
||||
]);
|
||||
}
|
||||
if(!$product) {
|
||||
$product = ProductModel::getFirst([
|
||||
"external" => 0,
|
||||
"productgroup_id" => TT_PRODUCTGROUP_ID_INTERNET_ACCESS_RESI,
|
||||
"name" => "%OAN%",
|
||||
"attributename" => "termination_required",
|
||||
"attributevalue" => 0,
|
||||
"active" => true
|
||||
]);
|
||||
}
|
||||
}
|
||||
//var_dump($product);exit;
|
||||
if(!$product) {
|
||||
$this->layout()->setFlash("Keine Produkte für Netzbetreiber gefunden!", "error");
|
||||
$this->redirect("Preorder", "Index", ["filter" => ["preordercampaign_id" => $campaign->id]], "preorder=$preorder_id");
|
||||
}
|
||||
|
||||
$product_data = [];
|
||||
$product_data["preorder_id"] = $preorder->id;
|
||||
$product_data["oaid"] = $preorder->oaid;
|
||||
$product_data["product_id"] = $product->id;
|
||||
$product_data['amount'] = 1;
|
||||
$product_data["pos"] = 1;
|
||||
$product_data["description"] = "";
|
||||
$product_data["price"] = trim($product->price) ? Layout::commaToDot(trim($product->price)) : 0;
|
||||
$product_data["price_setup"] = trim($product->price_setup) ? Layout::commaToDot(trim($product->price_setup)) : 0;
|
||||
|
||||
$product_data["billing_delay"] = ($product->billing_delay) ? $product->billing_delay : 0;
|
||||
if($product_data["billing_delay"] > 6) {
|
||||
$product_data["billing_delay"] = 6;
|
||||
}
|
||||
$product_data["billing_period"] = $product->billing_period;
|
||||
$product_data["contract_term"] = $product->contract_term;
|
||||
|
||||
if($this->me->is("Admin")) {
|
||||
$product_data["price_nne"] = $product->price_nne;
|
||||
$product_data["price_nbe"] = $product->price_nbe;
|
||||
}
|
||||
|
||||
$order_data["products"] = [1 => OrderProductModel::create($product_data)];
|
||||
|
||||
//var_dump($order_data["products"]);exit;
|
||||
$order = new Order();
|
||||
$order->update($order_data);
|
||||
|
||||
//var_dump($owner_data);exit;
|
||||
|
||||
$oc = new OrderController();
|
||||
|
||||
$this->layout()->set("order", $order);
|
||||
return $oc->addAction();
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function apiAction() {
|
||||
$do = $this->request->do;
|
||||
$data = [];
|
||||
|
||||
Reference in New Issue
Block a user