Files
thetool/application/Preordercampaign/Preordercampaign.php
2025-12-15 16:03:59 +01:00

328 lines
10 KiB
PHP

<?php
class Preordercampaign extends mfBaseModel {
private $network;
private $adb_netzgebiet;
private $preorders;
private $active_preorders;
private $active_preorder_count;
private $active_preorder_count_sd;
private $active_preorder_count_md;
private $types;
private $setup_products = [];
private $salesclusters;
private $apiusers;
private $corsorigins;
private $workorder_count;
private $required_fields = [];
private $banned_fcps;
private $all_fcp_names;
private $active_operators;
private $passive_operators;
private $statusnotifcation_mailtemplates;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function getAllFcpNames() {
if(!$this->id) return [];
$network = $this->getProperty("network");
$netzgebiet_id = $network->adb_netzgebiet_id;
if(!$netzgebiet_id) return [];
$addressdb = new AddressDB();
$fcps = $addressdb->getAllFcpNames($netzgebiet_id);
return $fcps;
}
public function addTypes(Array $types) {
$allowd_types = ["interest","provision","order","reorder","legacytransfer"];
$new_types = [];
foreach($types as $type) {
if(!in_array($type, $allowd_types)) {
$this->log->debug(__METHOD__.": $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 = [];
$reorder_products = [];
$provisions = ProductModel::search(["active" => 1, "attributename" => "presales", "attributevalue" => "provision"]);
$activations = ProductModel::search(["active" => 1, "attributename" => "presales", "attributevalue" => "activation"]);
$reorders = ProductModel::search(["active" => 1, "attributename" => "presales", "attributevalue" => "reorder"]);
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;
}
}
foreach($reorders as $p) {
if(ProductNetworkModel::search(['product_id' => $p->id, "network_id" => $this->network_id])) {
$reorder_products[] = $p;
}
}
return ['activation' => $activation_products, 'provision' => $provision_products, 'reorder' => $reorder_products];
}
public function getUnitCount() {
$count = 0;
$netzgebiete = $this->getProperty("salesclusters");
foreach($netzgebiete as $netzgebiet) {
$count += $netzgebiet->unit_count;
}
return $count;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "workorder_count") {
$wo_count = PreorderModel::countActive(["rimo_workorder" => true, "preordercampaign_id" => $this->id]);
if($wo_count) {
$this->workorder_count = $wo_count;
}
return $wo_count;
}
if($name == "required_fields") {
if(!$this->required_preorder_fields) return [];
$this->required_fields = json_decode($this->required_preorder_fields);
return $this->required_fields;
}
if($name == "preorders") {
$this->preorders = PreorderModel::search(['preordercampaign_id' => $this->id]);
return $this->preorders;
}
if($name == "active_preorders") {
$this->active_preorders = PreorderModel::searchActive(['preordercampaign_id' => $this->id]);
return $this->active_preorders;
}
if($name == "active_preorder_count_sd") {
$this->getProperty("active_preorder_count");
return $this->active_preorder_count_sd;
}
if($name == "active_preorder_count_md") {
$this->getProperty("active_preorder_count");
return $this->active_preorder_count_md;
}
if($name == "active_preorder_count") {
$count = 0;
$count_sd = 0;
$count_md = 0;
foreach($this->getProperty("active_preorders") as $preorder) {
$bt = $preorder->adb_hausnummer->getBuildingType();
if($bt == "sd") {
$count_sd += $preorder->connection_count ? (int)$preorder->connection_count : 1;
} elseif ($bt == "md") {
$count_md += $preorder->connection_count ? (int)$preorder->connection_count : 1;
}
$count += (int)$preorder->connection_count;
}
$this->active_preorder_count = $count;
$this->active_preorder_count_sd = $count_sd;
$this->active_preorder_count_md = $count_md;
return $this->active_preorder_count;
}
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) {
$sc = new ADBNetzgebiet($pog->salescluster_id);
if(!$sc->id) continue;
$this->salesclusters[$pog->salescluster_id] = $sc;
}
return $this->salesclusters;
}
if($name == "all_fcp_names") {
$fcps = $this->getAllFcpNames();
if($fcps) {
$this->all_fcp_names = $fcps;
} else {
$this->all_fcp_names = [];
}
return $this->all_fcp_names;
}
if($name == "banned_fcps") {
$bfcp_json = $this->banned_rimo_fcp;
$banned_fcp_array = json_decode($bfcp_json);
if(!is_array($banned_fcp_array) || !$banned_fcp_array) {
return [];
}
$this->banned_fcps = $banned_fcp_array;
return $this->banned_fcps;
}
if($name == "passive_operators") {
$pops = PreordercampaignOperatorModel::search(["preordercampaign_id" => $this->id, "type" => "passive"]);
if(!$pops) {
return [];
}
$this->passive_operators = [];
foreach($pops as $pop) {
$this->passive_operators[$pop->operator_id] = $pop;
}
return $this->passive_operators;
}
if($name == "active_operators") {
$aops = PreordercampaignOperatorModel::search(["preordercampaign_id" => $this->id, "type" => "active"]);
if(!$aops) {
return [];
}
$this->active_operators = [];
foreach($aops as $aop) {
$this->active_operators[$aop->operator_id] = $aop;
}
return $this->active_operators;
}
if($name == "statusnotifcation_mailtemplates") {
$snmts = PreordercampaignStatusnotificationMailtemplate::search(["preordercampaign_id" => $this->id]);
if(!count($snmts)) {
return [];
}
foreach($snmts as $snmt) {
if ($snmt->status_code !== null) {
$this->statusnotifcation_mailtemplates[$snmt->status_code] = $snmt;
} else {
$logical_config = json_decode($snmt->logical_config, true);
$this->statusnotifcation_mailtemplates[$logical_config['type']] = $snmt;
}
}
return $this->statusnotifcation_mailtemplates;
}
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 == "adb_netzgebiet") {
if(!$this->network_id) return null;
return $this->getProperty("network")->adb_netzgebiet;
}
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;
}
}