Files
thetool/application/Preorder/PreorderController.php
2022-05-17 17:55:13 +02:00

291 lines
8.3 KiB
PHP

<?php
class PreorderController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->is(["Admin", "salespartner"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$this->layout()->setTemplate("Preorder/Index");
$rfilter = $this->request->filter;
iF(!is_array($rfilter)) {
$rfilter = [];
}
$this->layout->set("filter", $rfilter);
$filter = $this->getPreparedFilter($rfilter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 25;
$pagination['maxItems'] = 0;
if(is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
//var_dump($filter);exit;
//$pagination['maxItems'] = AddressModel::count($filter);
$this->layout()->set("pagination", $pagination);
$campaign_filter = [];
if($filter['campaign_id']) {
$campaign_id = $filter['campaign_id'];
if(is_numeric($campaign_id) && $campaign_id > 0) {
$campaign = new Preordercampaign($campaign_id);
$this->layout()->set("campaign", $campaign);
}
$campaign_filter["preordercampaign_id"] = $campaign_id;
}
//var_dump($campaign_filter);exit;
$preorders = PreorderModel::search($campaign_filter);
$this->layout()->set("preorders", $preorders);
}
private function getPreparedFilter($filter) {
$new_filter = [];
if(is_array($filter) && count($filter)) {
foreach($filter as $name => $value) {
$new_filter[$name] = $value;
}
}
return $new_filter;
}
protected function addAction() {
$this->layout()->setTemplate("Preorder/Form");
if($this->me->isAdmin()) {
$this->layout()->set("networks", NetworkModel::getAll());
} else {
$this->layout()->set("networks", $this->me->my_networks);
}
if(!$this->request->preordercampaign_id) {
$this->layout()->setFlash("Keine Kampagne ausgewählt!", "warn");
}
$campaign_id = $this->request->preordercampaign_id;
$campaign = new Preordercampaign($campaign_id);
$this->layout()->set("campaign", $campaign);
$products = [];
foreach(ProductNetworkModel::search(["network_id" => $campaign->network_id]) as $pn) {
if(!array_key_exists($pn->product_id, $products)) {
if(is_array($pn->product->attributes) && !array_key_exists("presales", $pn->product->attributes)) {
$products[$pn->product_id] = $pn->product;
}
}
}
//var_dump($products);exit;
$this->layout()->set("products", $products);
$partners = AddressModel::search(['addresstype' => ['salespartner']]);
$this->layout()->set("partners", $partners);
}
protected function editAction() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$preorder = new Preorder($id);
if(!$preorder->id) {
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
$this->request->set("preordercampaign_id", $preorder->preordercampaign_id); // needed in addAction()
$this->layout()->set("preorder", $preorder);
//var_dump($preorder->building->street);exit;
return $this->addAction();
}
protected function saveAction() {
$r = $this->request;
var_dump($r);
/*
* add or edit
*/
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$preorder = new Preorder($id);
if(!$preorder->id) {
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
$this->redirect("Preordercampaign");
}
} else {
$id = false;
$mode = "add";
}
/*
* data colletion
*/
$data = [];
$data['preordercampaign_id'] = $r->campaign_id;
$data['building_id'] = $r->building_id;
$data['termination_id'] = ($r->termination_id) ? $r->termination_id : null;
if($campaign->product_type != "setup_only") {
$data['product_id'] = $r->product_id;
}
if(!$r->product_id) {
$data['product_id'] = null;
}
if($campaign->product_type != "no_setup") {
$data['setup_product_id'] = $r->setup_product_id;
}
if(!$r->setup_product_id) {
$data['setup_product_id'] = null;
}
switch($r->type) {
case "interest":
$data['type'] = "interest";
break;
case "provision":
$data['type'] = "provision";
break;
case "order":
$data['type'] = "order";
break;
}
$data['price'] = ($r->price) ? $r->price : 0;
$data['price_setup'] = ($r->price_setup) ? $r->price_setup : 0;
$data['price_nne'] = ($r->price_nne) ? $r->price_nne : 0;
$data['price_nbe'] = ($r->price_nbe) ? $r->price_nbe : 0;
$data['billing_delay'] = ($r->billing_delay) ? $r->billing_delay : 0;
if($r->partner_id) {
$data['partner_id'] = $r->partner_id;
} else {
$data['partner_id'] = null;
}
$data['company'] = $r->company;
$data['uid'] = $r->uid;
$data['firstname'] = $r->firstname;
$data['lastname'] = $r->lastname;
$data['street'] = $r->street;
$data['zip'] = $r->zip;
$data['city'] = $r->city;
$data['phone'] = $r->phone;
$data['email'] = $r->email;
$data['note'] = $r->note;
$data['edit_by'] = $this->me->id;
if($mode == "add") {
$data['create_by'] = $this->me->id;
$preorder = PreorderModel::create($data);
} else {
$preorder->update($data);
}
/*
* validation
*/
$campaign = new Preordercampaign($preorder->preordercampaign_id);
if(!$campaign->id) {
$this->layout()->setFlash("Keine Kampagne ausgewählt!", "error");
$this->redirect("Preordercampaign");
}
if(!array_key_exists($data['type'], $campaign->types)) {
$this->layout()->setFlash("Bitte Vorbestelltyp auswählen!", "error");
$this->layout()->set("preorder", $preorder);
$this->layout()->set("campaign", $campaign);
return $this->addAction();
}
if($data['product_id']) {
$product = new Product($data['product_id']);
if(!$product->id) {
$this->layout()->setFlash("Bitte Produkt auswählen!", "error");
$this->layout()->set("preorder", $preorder);
$this->layout()->set("campaign", $campaign);
return $this->addAction();
}
$preorder->billing_period = $product->billing_period;
if(!$this->me->isAdmin()) {
$preorder->price_nne = $product->price_nne;
$preorder->price_nbe = $product->price_nbe;
}
if(!strlen($preorder->price)
|| !strlen($preorder->price_setup)
|| !strlen($preorder->price_nne)
|| !strlen($preorder->price_nbe)
|| !strlen($preorder->price_nne)) {
$this->layout()->setFlash("Bitte alle benötigten Produktdaten ausfüllen!", "error");
$this->layout()->set("preorder", $preorder);
$this->layout()->set("campaign", $campaign);
return $this->addAction();
}
if(!strlen($preorder->firstname)
|| !strlen($preorder->lastname)
|| !strlen($preorder->street)
|| !strlen($preorder->zip)
|| !strlen($preorder->city)) {
$this->layout()->setFlash("Bitte alle benötigten Kundendaten ausfüllen!", "error");
$this->layout()->set("preorder", $preorder);
$this->layout()->set("campaign", $campaign);
return $this->addAction();
}
} else {
$preorder->billing_period = 0;
}
//var_dump($preorder);exit;
/*
* save
*/
$new_id = $preorder->save();
if(!$new_id) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
$this->layout()->set("preorder", $preorder);
return $this->addAction();
}
$qs = ['filter' => ['campaign_id' => $preorder->preordercampaign_id]];
$qs = http_build_query($qs);
$this->layout()->setFlash("Vorbestellung erfolgreich gespeichert!", "success");
$this->redirect("Preorder", "Index", $qs);
}
}