Files
thetool/application/Preordercampaign/Preordercampaign.php
2022-10-12 17:41:03 +02:00

177 lines
5.4 KiB
PHP

<?php
class Preordercampaign extends mfBaseModel {
private $network;
private $preorders;
private $active_preorders;
private $types;
private $setup_products;
private $salesclusters;
private $apiusers;
private $corsorigins;
private $total_homes;
public function addTypes(Array $types) {
$allowd_types = ["interest","provision","order","reorder"];
$new_types = [];
foreach($types as $type) {
if(!in_array($type, $allowd_types)) {
$this->log->debug("$type not in allowed_types");
continue;
}
$new_types[] = $type;
}
//var_dump($new_types);
foreach($allowd_types as $atype) {
$existing = PreordercampaignTypeModel::getFirst(['preordercampaign_id' => $this->id, "type" => $atype]);
//var_dump($existing);exit;
if($existing) {
if(!in_array($atype,$new_types)) {
$existing->delete();
}
} else {
if(in_array($atype, $new_types)) {
$data = [];
$data['preordercampaign_id'] = $this->id;
$data['type'] = $atype;
$nt = PreordercampaignTypeModel::create($data);
$nt->save();
}
}
}
return true;
}
public function getSetupProducts() {
$provision_products = [];
$activation_products = [];
$provisions = ProductModel::search(["attributename" => "presales", "attributevalue" => "provision"]);
$activations = ProductModel::search(["attributename" => "presales", "attributevalue" => "activation"]);
foreach($provisions as $p) {
if(ProductNetworkModel::search(['product_id' => $p->id, "network_id" => $this->network_id])) {
$provision_products[] = $p;
}
}
foreach($activations as $p) {
if(ProductNetworkModel::search(['product_id' => $p->id, "network_id" => $this->network_id])) {
$activation_products[] = $p;
}
}
return ['activation' => $activation_products, 'provision' => $provision_products];
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "total_homes") {
$total = 0;
foreach($this->getProperty("salesclusters") as $scluster) {
$netzgebiet_id = $scluster->salescluster_id;
$total += ADBWohneinheitModel::count(['netzgebiet_id' => $netzgebiet_id]);
}
$this->total_homes = $total;
return $total;
}
if($name == "preorders") {
$this->preorders = PreorderModel::search(['preordercampaign_id' => $this->id]);
return $this->preorders;
}
if($name == "active_preorders") {
$this->active_preorders = PreorderModel::search(['preordercampaign_id' => $this->id, 'deleted' => 0]);
return $this->active_preorders;
}
if($name == "types") {
$types = PreordercampaignTypeModel::search(['preordercampaign_id' => $this->id]);
foreach($types as $type) {
$this->types[$type->type] = $type;
}
return $this->types;
}
if($name == "setup_products") {
$this->setup_products = $this->getSetupProducts();
//var_dump($this->setup_products);exit;
return $this->setup_products;
}
if($name == "salesclusters") {
$items = PreordercampaignSalesclusterModel::search(["preordercampaign_id" => $this->id]);
foreach($items as $pog) {
$this->salesclusters[$pog->salescluster_id] = $pog;
}
return $this->salesclusters;
}
if($name == "apiusers") {
$items = PreordercampaignApiuserModel::search(["preordercampaign_id" => $this->id]);
foreach($items as $poa) {
$this->apiusers[$poa->worker_id] = $poa;
}
return $this->apiusers;
}
if($name == "corsorigins") {
$items = PreordercampaignOriginhostnameModel::search(["preordercampaign_id" => $this->id]);
foreach($items as $origin) {
$this->corsorigins[] = htmlentities($origin->hostname);
}
return $this->corsorigins;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
if($user) {
$this->editor = $user;
return $this->editor;
}
$this->editor = new User($this->edit_by);
if($this->editor->id) {
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
}
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}