1330 lines
46 KiB
PHP
1330 lines
46 KiB
PHP
<?php
|
|
|
|
class PreordercampaignController extends mfBaseController {
|
|
|
|
protected function init() {
|
|
$this->needlogin = true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
$this->layout()->set("me", $this->me);
|
|
|
|
if (!$this->me->is(["Admin", "netowner", "salespartner"]) && !$this->me->can("Preorder")) $this->redirect("Dashboard");
|
|
if ($this->me->is("Preorderlogistics")) $this->redirect("Preorderlogistics");
|
|
if ($this->me->is("Preorderfront")) $this->redirect("Preorder");
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$isAdmin = $this->me->is("Admin");
|
|
|
|
$filter = [];
|
|
if ($isAdmin) $my_networks = NetworkModel::getAll();
|
|
else $filter['network_id'] = array_unique(array_merge(
|
|
array_column($this->me->myNetworks(["netowner", "salespartner"]), 'id'),
|
|
json_decode($this->me->getFlag("preorder_networks")->value() ?: '[]')
|
|
));
|
|
|
|
|
|
|
|
$campaigns = array_map(function ($c) {
|
|
// Helper function for field caching
|
|
$getCachedField = function ($campaignId, $cacheType, $callback) {
|
|
$cacheKey = md5($campaignId . $cacheType);
|
|
$cacheDir = TEMP_DIR . "/Preordercampaign/";
|
|
$cacheFile = $cacheDir . $cacheKey . ".json";
|
|
|
|
// Create directory if missing
|
|
if (!is_dir($cacheDir)) {
|
|
mkdir($cacheDir, 0755, true);
|
|
}
|
|
|
|
// Return cached value if fresh
|
|
if (file_exists($cacheFile) && time() - filemtime($cacheFile) < 3600) {
|
|
return json_decode(file_get_contents($cacheFile), true);
|
|
}
|
|
|
|
// Generate and cache new value
|
|
$value = $callback();
|
|
file_put_contents($cacheFile, json_encode($value));
|
|
return $value;
|
|
};
|
|
|
|
return [
|
|
'id' => $c->id,
|
|
'network_id' => $c->network_id,
|
|
'network_name' => $c->network->name,
|
|
'network_owner_name' => $c->network->owner ? $c->network->owner->getCompanyOrName() : '',
|
|
'name' => $c->name,
|
|
'area' => $c->area,
|
|
'from' => $c->from,
|
|
'to' => $c->to,
|
|
'rimo_workoders' => $getCachedField($c->id, 'workorders', function() use ($c) {
|
|
return $c->workorder_count;
|
|
}),
|
|
'active_preorder_count' => $getCachedField($c->id, 'active_count', function() use ($c) {
|
|
return PreorderModel::countActivePreorder($c->id);
|
|
}),
|
|
'count_total_units' => $getCachedField($c->id, 'total_units', function() use ($c) {
|
|
return PreorderModel::countTotalUnits($c->id);
|
|
}),
|
|
];
|
|
}, PreordercampaignModel::search($filter));
|
|
|
|
$net_owners = array_map(function ($n) {
|
|
return [
|
|
'value' => $n->getCompanyOrName(),
|
|
'text' => $n->getCompanyOrName()
|
|
];
|
|
}, $isAdmin ? $this->getNetworkOwners(array_column($my_networks, 'id')) : []);
|
|
|
|
Helper::renderVue($this, "Preordercampaign", "Vorbestellkampagnen",
|
|
[
|
|
"NETWORK_OWNER_OPTIONS" => $net_owners,
|
|
"SHOW_MISC_BUTTONS" => in_array($this->me->address_id, ["1", "4807"], true),
|
|
"IS_ADMIN" => $isAdmin,
|
|
"CAMPAIGNS" => $campaigns,
|
|
"VIEW_URL" => self::getUrl("Preorder", "Index"),
|
|
"PREORDER_DISCOUNT_IMPORT_URL" => self::getUrl("PreorderDiscount", "import"),
|
|
"PREORDER_STATUS_UPDATE_IMPORT_URL" => self::getUrl("Preorder", "statusupdateimport"),
|
|
"ADD_PREORDER_URL" => self::getUrl("Preorder", "add"),
|
|
"DOWNLOAD_ADDON_SERVICES_URL" => self::getUrl("Preordercampaign", "downloadAddonServices"),
|
|
"PREORDER_NOTIFICATION_URL" => self::getUrl("Preordernotification", "Index"),
|
|
"ADD_URL" => self::getUrl("Preordercampaign", "add"),
|
|
"EDIT_URL" => self::getUrl("Preordercampaign", "edit"),
|
|
"ADMIN_URL" => self::getUrl("Preordercampaign", "admin"),
|
|
]);
|
|
}
|
|
private function getPreparedFilter($filter) {
|
|
$new_filter = [];
|
|
|
|
if(array_key_exists("netowner", $filter) && $filter['netowner']) {
|
|
if($this->me->is("Admin")) {
|
|
$owner_id = $filter['netowner'];
|
|
foreach(NetworkModel::search(['owner_id' => $owner_id]) as $network) {
|
|
$new_filter['network_id'][] = $network->id;
|
|
}
|
|
unset($filter['network_id']);
|
|
} else {
|
|
unset($filter['netowner']);
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("name", $filter) && $filter['name']) {
|
|
$new_filter['name%'] = "%".$filter['name'];
|
|
unset($filter['name']);
|
|
}
|
|
if(array_key_exists("area", $filter) && $filter['area']) {
|
|
$new_filter['area%'] = "%".$filter['area'];
|
|
unset($filter['area']);
|
|
}
|
|
|
|
foreach($filter as $name => $value) {
|
|
$new_filter[$name] = $value;
|
|
}
|
|
|
|
return $new_filter;
|
|
}
|
|
|
|
private function getNetworkOwners() {
|
|
$owners = [];
|
|
|
|
$sql = "SELECT Address.* FROM Preordercampaign
|
|
LEFT JOIN Network ON (Preordercampaign.network_id = Network.id)
|
|
LEFT JOIN Address ON (Network.owner_id = Address.id)
|
|
GROUP BY Address.id
|
|
ORDER BY Address.company, Address.lastname, Address.firstname
|
|
";
|
|
$res = $this->db()->query($sql);
|
|
if($this->db()->num_rows($res)) {
|
|
while($data = $this->db()->fetch_object($res)) {
|
|
$owners[] = new Address($data->id);
|
|
}
|
|
}
|
|
|
|
return $owners;
|
|
}
|
|
|
|
protected function addAction() {
|
|
if(!$this->me->is("Admin")) {
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$this->layout()->setTemplate("Preordercampaign/Form");
|
|
|
|
if($this->me->isAdmin()) {
|
|
$this->layout()->set("networks", NetworkModel::getAll());
|
|
} else {
|
|
$this->layout()->set("networks", $this->me->my_networks);
|
|
}
|
|
|
|
$this->layout()->set("types", BuildingtypeModel::getAll());
|
|
$this->layout()->set("statuses", BuildingstatusModel::getAll());
|
|
|
|
$this->layout()->set("networksections", NetworksectionModel::getAll());
|
|
|
|
$this->layout()->set("adb_netzgebiete", ADBNetzgebietModel::getAll());
|
|
|
|
|
|
}
|
|
|
|
protected function editAction() {
|
|
if(!$this->me->is("Admin")) {
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$this->layout()->set("campaign", $campaign);
|
|
|
|
return $this->addAction();
|
|
}
|
|
|
|
protected function saveAction() {
|
|
if(!$this->me->is("Admin")) {
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$r = $this->request;
|
|
//var_dump($r->get());exit;
|
|
$id = $r->id;
|
|
if(is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
} else {
|
|
$id = false;
|
|
$mode = "add";
|
|
}
|
|
|
|
$data = [];
|
|
$data['network_id'] = $r->network_id;
|
|
$data['name'] = trim($r->name);
|
|
$data['description'] = trim($r->description);
|
|
$data['area'] = trim($r->area);
|
|
$data['note'] = trim($r->note);
|
|
$data['homes_total'] = (int)$r->homes_total;
|
|
$data["cifurl"] = trim($r->cifurl);
|
|
$data["cifcableurl"] = trim($r->cifcableurl);
|
|
$data["from_email_name"] = trim($r->from_email_name);
|
|
$data["from_email"] = trim($r->from_email);
|
|
$data["netowner_fibu_cost_code"] = trim($r->netowner_fibu_cost_code);
|
|
$data["iframe_origins"] = trim($r->iframe_origins);
|
|
$data["iframe_consents"] = trim($r->iframe_consents);
|
|
|
|
if($r->from) {
|
|
$data['from'] = self::dateToTimestamp($r->from);
|
|
}
|
|
|
|
if($r->to) {
|
|
$data['to'] = self::dateToTimestamp($r->to);
|
|
}
|
|
|
|
if($r->fulfillment == "thirdparty") {
|
|
$data['fulfillment'] = "thirdparty";
|
|
} elseif($r->fulfillment == "rimo") {
|
|
$data['fulfillment'] = "rimo";
|
|
} elseif($r->fulfillment == "citycom_oan") {
|
|
$data['fulfillment'] = "citycom_oan";
|
|
} else {
|
|
$data['fulfillment'] = "thetool";
|
|
}
|
|
|
|
if($r->oaid_origin == "ofaa") {
|
|
$data['oaid_origin'] = "ofaa";
|
|
} elseif($r->oaid_origin == "thetool") {
|
|
$data['oaid_origin'] = "thetool";
|
|
} else {
|
|
$data['oaid_origin'] = "other";
|
|
}
|
|
|
|
if($r->product_type == "setup_only") {
|
|
$data['product_type'] = "setup_only";
|
|
} elseif($r->product_type == "no_setup") {
|
|
$data['product_type'] = "no_setup";
|
|
} else {
|
|
$data['product_type'] = "all";
|
|
}
|
|
|
|
if(is_array($r->required_fields) && count($r->required_fields)) {
|
|
$rfields = [];
|
|
foreach(['contact_type'] as $afield) {
|
|
if(in_array($afield, $r->required_fields)) {
|
|
$rfields[] = "contact_type";
|
|
}
|
|
}
|
|
$data['required_preorder_fields'] = json_encode($rfields);
|
|
} else {
|
|
$data['required_preorder_fields'] = null;
|
|
}
|
|
|
|
if($r->district_is_city == 1) {
|
|
$data['district_is_city'] = 1;
|
|
} else {
|
|
$data['district_is_city'] = 0;
|
|
}
|
|
|
|
if($r->hausnummer_add_zusatz == 1) {
|
|
$data['hausnummer_add_zusatz'] = 1;
|
|
} else {
|
|
$data['hausnummer_add_zusatz'] = 0;
|
|
}
|
|
|
|
if($r->exist_is_error == 1) {
|
|
$data['exist_is_error'] = 1;
|
|
} else {
|
|
$data['exist_is_error'] = 0;
|
|
}
|
|
|
|
//var_dump($r->banned_rimo_fcp);exit;
|
|
if($r->banned_rimo_fcp && is_array($r->banned_rimo_fcp) && count($r->banned_rimo_fcp)) {
|
|
$banned_fcp_json = json_encode($r->banned_rimo_fcp);
|
|
if($banned_fcp_json) {
|
|
$data["banned_rimo_fcp"] = $banned_fcp_json;
|
|
} else {
|
|
$data["banned_rimo_fcp"] = null;
|
|
}
|
|
} else {
|
|
$data["banned_rimo_fcp"] = null;
|
|
}
|
|
|
|
$data['edit_by'] = $this->me->id;
|
|
|
|
if($mode == "add") {
|
|
$data['create_by'] = $this->me->id;
|
|
$campaign = PreordercampaignModel::create($data);
|
|
} else {
|
|
$campaign->update($data);
|
|
}
|
|
|
|
$new_id = $campaign->save();
|
|
if(!$new_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
|
$this->layout()->set("campaign", $campaign);
|
|
return $this->add();
|
|
}
|
|
|
|
if(is_array($r->types) && count($r->types)) {
|
|
$campaign->addTypes($r->types);
|
|
}
|
|
|
|
//var_dump($r->adb_netzgebiet_ids);exit;
|
|
foreach(PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $pcg) {
|
|
$pcg->delete();
|
|
}
|
|
//var_dump($r->adb_netzgebiet_ids);exit;
|
|
if(is_array($r->adb_netzgebiet_ids) && count($r->adb_netzgebiet_ids)) {
|
|
foreach($r->adb_netzgebiet_ids as $netzgebiet_id) {
|
|
$pcg = PreordercampaignSalesclusterModel::getFirst(['preordercampaign_id' => $campaign->id, 'salescluster_id' => $netzgebiet_id]);
|
|
if($pcg) continue;
|
|
|
|
$pcg = PreordercampaignSalesclusterModel::create(['preordercampaign_id' => $campaign->id, 'salescluster_id' => $netzgebiet_id]);
|
|
$pcg->save();
|
|
}
|
|
}
|
|
|
|
foreach(PreordercampaignApiuserModel::search(['preordercampaign_id' => $campaign->id]) as $pca) {
|
|
$pca->delete();
|
|
}
|
|
if(is_array($r->apiusers) && count($r->apiusers)) {
|
|
foreach($r->apiusers as $user_id) {
|
|
$pca = PreordercampaignApiuserModel::getFirst(['preordercampaign_id' => $campaign->id, 'worker_id' => $user_id]);
|
|
if($pca) continue;
|
|
|
|
$pca = PreordercampaignApiuserModel::create(['preordercampaign_id' => $campaign->id, 'worker_id' => $user_id]);
|
|
$pca->save();
|
|
}
|
|
}
|
|
|
|
foreach(PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) {
|
|
$origin->delete();
|
|
}
|
|
// var_dump($r->corsorigins);exit;
|
|
if($r->corsorigins) {
|
|
$origins = str_replace("\r", "\n", $r->corsorigins);
|
|
$origins = preg_replace("/\n+/", "\n", $origins);
|
|
$origins = explode("\n", $origins);
|
|
if(is_array($origins) && count($origins)) {
|
|
foreach($origins as $origin) {
|
|
$poh = PreordercampaignOriginhostnameModel::getFirst(['preordercampaign_id' => $campaign->id, 'hostname' => $origin]);
|
|
if($poh) continue;
|
|
|
|
$poh = PreordercampaignOriginhostnameModel::create(['preordercampaign_id' => $campaign->id, 'hostname' => $origin]);
|
|
$poh->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//var_dump($r->get());exit;
|
|
// sync active operators and ISPs
|
|
if(is_array($r->active_operators) && count($r->active_operators)) {
|
|
// delete removed operators
|
|
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "active"]) as $op) {
|
|
if(!in_array($op->operator_id, $r->active_operators)) {
|
|
$this->log->debug("deleting campaignoperator ".$op->id);
|
|
$op->delete(); // automatically deletes isps too
|
|
}
|
|
}
|
|
//var_dump($r->active_operators);exit;
|
|
// add missing operators
|
|
foreach($r->active_operators as $op_key => $op_id) {
|
|
if(!$op_id) continue;
|
|
$op = PreordercampaignOperatorModel::getFirst(["preordercampaign_id" => $campaign->id, "type" => "active", "operator_id" => $op_id]);
|
|
if(!$op) {
|
|
$op = PreordercampaignOperatorModel::create([
|
|
"preordercampaign_id" => $campaign->id,
|
|
"operator_id" => $op_id,
|
|
"type" => "active"
|
|
]);
|
|
$op->save();
|
|
}
|
|
|
|
//var_dump($r->active_isps, $op_id);exit;
|
|
// sync operators ISPs
|
|
if(is_array($r->active_isps) && array_key_exists($op_id, $r->active_isps) && count($r->active_isps[$op_id])) {
|
|
// delete removed ISPs
|
|
foreach(PreordercampaignOperatorIspModel::search(["campaignoperator_id" => $op_key]) as $isp) {
|
|
if(!array_key_exists($op_id, $r->active_isps) || !in_array($isp->isp_id, $r->active_isps[$op_id])) {
|
|
$this->log->debug("Deleting isp ".$isp->id." from operator $op_id");
|
|
$isp->delete();
|
|
}
|
|
}
|
|
// create new ISPs if not existing
|
|
//var_dump($op_key, $r->active_isps[$op_id]);exit;
|
|
foreach ($r->active_isps[$op_id] as $isp_id) {
|
|
if ($op_key == "new" || !PreordercampaignOperatorIspModel::getFirst(["campaignoperator_id" => $op_key, "isp_id" => $isp_id])) {
|
|
$new_isp = PreordercampaignOperatorIspModel::create([
|
|
"campaignoperator_id" => $op_key,
|
|
"isp_id" => $isp_id
|
|
]);
|
|
//var_dump($new_isp);exit;
|
|
if (!$new_isp->save()) {
|
|
$this->log->debug("Error saving isp " . print_r($new_isp, true));
|
|
}
|
|
$this->log->debug("created operatorIsp $op_id $isp_id");
|
|
}
|
|
}
|
|
} else {
|
|
// got empty ISP list, so delete all ISPs
|
|
if($op_id && $op_key != "new") {
|
|
foreach(PreordercampaignOperatorIspModel::search(["campaignoperator_id" => $op_key]) as $isp) {
|
|
$this->log->debug("Deleting all ISPs for $op_id");
|
|
$isp->delete();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
} else {
|
|
// got empty operator list, delete all
|
|
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "active"]) as $op) {
|
|
$op->delete(); // automatically deletes isps too
|
|
}
|
|
}
|
|
|
|
|
|
if(is_array($r->passive_operators)) {
|
|
if(!count($r->passive_operators)) {
|
|
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "passive"]) as $op) {
|
|
$op->delete();
|
|
}
|
|
} else {
|
|
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "passive"]) as $op) {
|
|
if(!in_array($op->id, $r->passive_operators)) {
|
|
$op->delete();
|
|
}
|
|
}
|
|
foreach($r->passive_operators as $op_id) {
|
|
if(!$op_id) continue;
|
|
if(!PreordercampaignOperatorModel::getFirst(["preordercampaign_id" => $campaign->id, "type" => "passive", "operator_id" => $op_id])) {
|
|
$new_op = PreordercampaignOperatorModel::create([
|
|
"preordercampaign_id" => $campaign->id,
|
|
"operator_id" => $op_id,
|
|
"type" => "passive"
|
|
]);
|
|
$new_op->save();
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
foreach(PreordercampaignOperatorModel::search(["preordercampaign_id" => $campaign->id, "type" => "passive"]) as $op) {
|
|
$op->delete();
|
|
}
|
|
}
|
|
|
|
// Delete Status Email Templates from Campaign
|
|
/*foreach(PreordercampaignStatusnotificationMailtemplate::search(["preordercampaign_id" => $campaign->id]) as $status_mailtemplate) {
|
|
$status_mailtemplate->delete();
|
|
}*/
|
|
|
|
//var_dump($r->get());exit;
|
|
$mailtemplates_delete = [];
|
|
// Save Status Email Templates
|
|
foreach($r->mailtemplates as $status_code => $status_data) {
|
|
$mailtemplate_id = $status_data["mailtemplate_id"];
|
|
$allow_on_skip = $status_data["allow_on_skip"] ?? false;
|
|
$prevent_previous = $status_data["prevent_previous"] ?? false;
|
|
|
|
if(!$mailtemplate_id && !$allow_on_skip && !$prevent_previous) {
|
|
$mailtemplates_delete[] = $status_code;
|
|
continue;
|
|
}
|
|
|
|
if($mailtemplate_id) {
|
|
// check if Mailtemplate exists
|
|
$mailtemplate = new Mailtemplate($mailtemplate_id);
|
|
if(!$mailtemplate || !$mailtemplate->id) continue;
|
|
}
|
|
|
|
// if status_code is only numbers then log it and die for debugging
|
|
if(preg_match("/^\d+$/", $status_code)) {
|
|
$mt_data = [
|
|
"preordercampaign_id" => $campaign->id,
|
|
"status_code" => $status_code,
|
|
"mailtemplate_id" => ($mailtemplate_id) ? $mailtemplate->id : null,
|
|
"allow_on_skip" => ($allow_on_skip) ? 1 : 0,
|
|
"prevent_previous" => ($prevent_previous) ? 1 : 0,
|
|
];
|
|
|
|
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $status_code]);
|
|
if($statusmailtemplate) {
|
|
$statusmailtemplate->update($mt_data);
|
|
} else {
|
|
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::create($mt_data);
|
|
}
|
|
} else {
|
|
$mt_data = [
|
|
"preordercampaign_id" => $campaign->id,
|
|
"notification_type" => "logical",
|
|
"logical_config" => json_encode(["type" => $status_code]),
|
|
"mailtemplate_id" => ($mailtemplate_id) ? $mailtemplate->id : null,
|
|
"allow_on_skip" => ($allow_on_skip) ? 1 : 0,
|
|
"prevent_previous" => ($prevent_previous) ? 1 : 0,
|
|
];
|
|
|
|
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "logical_config" => $status_code]);
|
|
if($statusmailtemplate) {
|
|
$statusmailtemplate->update($mt_data);
|
|
} else {
|
|
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::create($mt_data);
|
|
}
|
|
}
|
|
|
|
if(!$statusmailtemplate->save()) {
|
|
$this->layout()->setFlash("Fehler beim Speichern der Status Emailtemplates", "warn");
|
|
}
|
|
}
|
|
|
|
foreach($mailtemplates_delete as $mt_code) {
|
|
$status_mt = null;
|
|
if(preg_match("/^\d+$/", $mt_code)) $status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $mt_code]);
|
|
else $status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "logical_config" => $mt_code]);
|
|
|
|
if($status_mt) $status_mt->delete();
|
|
}
|
|
|
|
$this->layout()->setFlash("Vorbestellkampagne erfolgreich gespeichert.", "success");
|
|
|
|
if($r->return == "index") {
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
$this->redirect("Preordercampaign", "Edit", ['id' => $new_id]);
|
|
|
|
|
|
}
|
|
|
|
protected function billingAction() {
|
|
if(!$this->me->is("Admin")) {
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
// Get mysqli resource from Model, so layout can output data as it's retrieved
|
|
// Works around lousy performance and horrendous memory usage
|
|
$res = PreorderModel::searchActive(["preordercampaign_id" => $id], [], true);
|
|
|
|
$this->layout()->setTemplate("Preorder/billing-export.csv");
|
|
$this->layout()->set("res", $res);
|
|
$this->layout()->set("campaign", $campaign);
|
|
}
|
|
|
|
protected function downloadAddonServices() {
|
|
$id = $this->request->preordercampaign_id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$this->layout()->set("campaign", $campaign);
|
|
$preorders = PreorderModel::searchActive(["preordercampaign_id" => $id, "add-where" => "AND JSON_LENGTH(addon_services) > 1"]);
|
|
|
|
if(!count($preorders)) {
|
|
$this->layout->setFlash("Keine Vorbestellungen mit bestellten Dienstleistungen gefunden.");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$csv = [];
|
|
foreach($preorders as $preorder) {
|
|
$sjson = json_decode($preorder->addon_services);
|
|
if(!$sjson) {
|
|
continue;
|
|
}
|
|
|
|
// address data and stuff
|
|
$general = [
|
|
"ucode" => $preorder->ucode,
|
|
"gemeinde" => $preorder->adb_hausnummer->ortschaft->gemeinde->name,
|
|
"plz" => $preorder->adb_hausnummer->plz->plzstring,
|
|
"ortschaft" => $preorder->adb_hausnummer->ortschaft->name,
|
|
"strasse" => $preorder->adb_hausnummer->strasse->name,
|
|
"hausnummer" => $preorder->adb_hausnummer->hausnummer,
|
|
"block" => "",
|
|
"stiege" => "",
|
|
"stock" => "",
|
|
"tuer" => "",
|
|
"zusatz" => "",
|
|
"address_info" => $preorder->address_info,
|
|
"contact_firma" => $preorder->company,
|
|
"contact_uid" => $preorder->uid,
|
|
"contact_firstname" => $preorder->firstname,
|
|
"contact_lastname" => $preorder->lastname,
|
|
"contact_street" => $preorder->street,
|
|
"contact_plz" => $preorder->zip,
|
|
"contact_ort" => $preorder->city,
|
|
"phone" => $preorder->phone,
|
|
"email" => $preorder->email,
|
|
];
|
|
|
|
if($preorder->adb_wohneinheit_id) {
|
|
$general["block"] = $preorder->adb_wohneinheit->block;
|
|
$general["stiege"] = $preorder->adb_wohneinheit->stiege;
|
|
$general["stock"] = $preorder->adb_wohneinheit->stock;
|
|
$general["tuer"] = $preorder->adb_wohneinheit->tuer;
|
|
$general["zusatz"] = $preorder->adb_wohneinheit->zusatz;
|
|
}
|
|
|
|
foreach($sjson as $service) {
|
|
if(!$service->ordered) continue;
|
|
|
|
$s = $general;
|
|
$s["service"] = $service->service;
|
|
$data = [];
|
|
foreach($service->data as $d) {
|
|
$data[] = (string)$d->name.": ".(is_bool($d->value) ? (int)$d->value : (string)$d->value);
|
|
}
|
|
$s["service_data"] = implode("; ", $data);
|
|
$csv[] = $s;
|
|
}
|
|
//var_dump($csv);exit;
|
|
$this->layout()->setTemplate("Preordercampaign/services.csv");
|
|
$this->layout()->set("csv", $csv);
|
|
}
|
|
|
|
}
|
|
|
|
protected function apiAction() {
|
|
if(!$this->me->is(["Admin","netowner"]) && !$this->me->can("Preorder")) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
$do = $this->request->do;
|
|
$data = [];
|
|
|
|
switch($do) {
|
|
case "sendStatusEmail":
|
|
$return = $this->sendStatusEmailApi();
|
|
break;
|
|
default:
|
|
$return = false;
|
|
}
|
|
|
|
if(!is_array($return) || !count($return)) {
|
|
$data = ["status" => "error"];
|
|
$this->returnJson($data);
|
|
}
|
|
$data['status'] = "OK";
|
|
$data['result'] = $return;
|
|
$this->returnJson($data);
|
|
}
|
|
|
|
private function sendStatusEmailApi() {
|
|
$campaign_id = $this->request->campaign_id;
|
|
$template_id = $this->request->template_id;
|
|
$to_email = $this->request->to_email;
|
|
|
|
if(!$to_email) {
|
|
return false;
|
|
}
|
|
|
|
if(!is_numeric($campaign_id) || $campaign_id < 1) {
|
|
return false;
|
|
}
|
|
|
|
$campaign = new Preordercampaign($campaign_id);
|
|
if(!$campaign->id) {
|
|
return false;
|
|
}
|
|
|
|
$preorder = PreorderModel::getFirstActive(["preordercampaign_id" => $campaign_id]);
|
|
if(!$preorder) {
|
|
return false;
|
|
}
|
|
|
|
$template = new Mailtemplate($template_id);
|
|
if(!$template->id || !$template->code) {
|
|
return false;
|
|
}
|
|
|
|
$email_data = [
|
|
"template" => $template->code,
|
|
"from" => "campaign",
|
|
"to" => $to_email,
|
|
"data" => "preorder, adb_hausnummer, adb_wohneinheit"
|
|
];
|
|
|
|
$preorder->runTriggerEmailAction($email_data, [
|
|
"RML_NEW_ORDER_PRICE_OPTION_EMBED" => "{{EMBED:tpl-rml-neue-bestellung-preistext-option2-preis}}",
|
|
"price_setup" => number_format(1500, 2, ",", "."),
|
|
]);
|
|
|
|
return ["message" => "Testemail sent"];
|
|
}
|
|
|
|
/****************************
|
|
* begin Campaign Admin actions
|
|
*/
|
|
|
|
protected function adminAction() {
|
|
$this->layout()->setTemplate("Preordercampaign/Admin");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$this->layout()->set("campaign", $campaign);
|
|
}
|
|
|
|
protected function updateUnitOAIDsAction() {
|
|
$this->layout()->setTemplate("Preordercampaign/Admin");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$updated_units = 0;
|
|
|
|
foreach(PreorderModel::searchActive(["preordercampaign_id" => $id, 'deleted' => 0]) as $preorder) {
|
|
if(!$preorder->adb_wohneinheit_id) continue;
|
|
if(!$preorder->oaid) continue;
|
|
$unit = $preorder->adb_wohneinheit;
|
|
|
|
if(!$preorder->oaid || $unit->oaid != $preorder->oaid) {
|
|
$preorder->save();
|
|
$updated_units++;
|
|
}
|
|
}
|
|
|
|
$this->layout()->setFlash("$updated_units Wohneinheiten aktualisiert", "success");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
|
|
}
|
|
|
|
protected function assignOpenAccessIdsToPreordersAction() {
|
|
$this->layout()->setTemplate("Preordercampaign/Admin");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
if($campaign->oaid_origin == "other") {
|
|
$this->layout()->setFlash("Kampagne unterstützt keine OAIDs", "warning");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
}
|
|
|
|
$assigned_oaids = 0;
|
|
$multiple_unit = 0;
|
|
|
|
foreach(PreorderModel::searchActive(["preordercampaign_id" => $id, "oaid" => null, "connection_count" => 1]) as $preorder) {
|
|
if($preorder->type == "interest") {
|
|
continue;
|
|
}
|
|
|
|
if($preorder->oaid) {
|
|
$this->log->warning(__METHOD__.": OAID is set already");
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->adb_wohneinheit_id) {
|
|
//echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n";
|
|
continue;
|
|
}
|
|
$wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) {
|
|
$this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
|
|
continue;
|
|
}
|
|
|
|
/*if(!$wohneinheit->extref) {
|
|
continue;
|
|
}*/
|
|
|
|
$wohneinheit_count = PreorderModel::countActive(["adb_wohneinheit_id" => $wohneinheit->id]);
|
|
if($wohneinheit_count > 1) {
|
|
$this->log->error(__METHOD__.": Wohneinheit ".$wohneinheit->id." is assigned to multiple preorders!");
|
|
$multiple_unit++;
|
|
continue;
|
|
}
|
|
|
|
$netowner = new Address($campaign->network->owner_id);
|
|
//var_dump($netowner);exit;
|
|
// get random active OAID from network owner pool
|
|
|
|
$oaid_attributes = [
|
|
"owner_id" => $netowner->id,
|
|
"origin" => $campaign->oaid_origin
|
|
];
|
|
|
|
if($this->request->origin_id) {
|
|
$oaid_attributes["origin_id"] = $this->request->origin_id;
|
|
}
|
|
|
|
$preorder->setOrCreateOaid($oaid_attributes);
|
|
|
|
/*if($wohneinheit->oaid != $oaid->oaid) {
|
|
$wohneinheit->oaid = $oaid->oaid;
|
|
$wohneinheit->save();
|
|
}*/
|
|
|
|
$assigned_oaids++;
|
|
|
|
}
|
|
|
|
$this->layout()->setFlash("$assigned_oaids OAIDs wurden Vorbestellungen zugewiesen", "success");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
}
|
|
|
|
protected function createRimoWorkordersAction() {
|
|
$this->layout()->setTemplate("Preordercampaign/Admin");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
if($campaign->oaid_origin == "other") {
|
|
$this->layout()->setFlash("Kampagne unterstützt keine OAIDs, somit können keine Workorders erstellt werden", "warning");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
}
|
|
|
|
$missing_units = [];
|
|
$missing_extrefs = [];
|
|
$workorders_created = 0;
|
|
$workorders_failed = 0;
|
|
|
|
foreach(PreorderModel::searchActive(["preordercampaign_id" => $id, "rimo_workorder" => false, "connection_count" => 1]) as $preorder) {
|
|
if($preorder->workorder_export_date) {
|
|
$this->log->warning(__METHOD__.": workorder_export_date not null");
|
|
}
|
|
|
|
if($preorder->type == "interest" || $preorder->type == "legacytransfer") {
|
|
continue;
|
|
}
|
|
|
|
if($preorder->status->code == 20) {
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->adb_wohneinheit_id) {
|
|
//echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n";
|
|
$missing_units[] = $preorder;
|
|
continue;
|
|
}
|
|
$wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) {
|
|
$this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
|
|
continue;
|
|
}
|
|
|
|
if(!$wohneinheit->extref) {
|
|
$missing_extrefs[] = $preorder;
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->oaid) {
|
|
$preorder->setOrCreateOaid();
|
|
if(!$preorder->oaid) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$oaid = OpenAccessIdModel::getFirstOaid($wohneinheit->oaid);
|
|
if(!$oaid) {
|
|
$this->log->warning(__METHOD__.": OAID '".$wohneinheit->oaid."' not found");
|
|
continue;
|
|
}
|
|
// create and assign OAID if not yet done
|
|
$oaid->exportToRimoAndAssignFtu();
|
|
|
|
// create Workorder
|
|
$data = [];
|
|
$data["firstname"] = trim($preorder->firstname);
|
|
$data["lastname"] = trim($preorder->lastname);
|
|
$data["company"] = trim($preorder->company);
|
|
$data["phone"] = trim($preorder->phone);
|
|
$data["email"] = trim($preorder->email);
|
|
$data["orderId"] = $preorder->ucode;
|
|
|
|
$response = Rimoapi::createWorkorder($wohneinheit->extref, $data);
|
|
//var_dump($response);exit;
|
|
if(!$response) {
|
|
$this->log->error("Cannot create RimoWorkorder! Invalid Response! (Preorder ucode: ".$preorder->ucode.")");
|
|
throw new Exception("Cannot create RimoWorkorder! Invalid Response! (Preorder ucode: ".$preorder->ucode.")");
|
|
}
|
|
|
|
$rimo_status = $response->state->userLabel;
|
|
if(!$rimo_status) {
|
|
$rimo_status = "";
|
|
}
|
|
|
|
$wo = RimoWorkorderModel::create([
|
|
"rimo_id" => $response->id,
|
|
"rimo_name" => $response->name,
|
|
"rimo_status" => $rimo_status,
|
|
"adb_wohneinheit_id" => $preorder->adb_wohneinheit_id,
|
|
"create_data" => json_encode($response)
|
|
]);
|
|
//var_dump($wo);exit;
|
|
$wo->save();
|
|
$workorders_created++;
|
|
}
|
|
|
|
$errors = [];
|
|
$warnings = [];
|
|
|
|
if(count($missing_units)) {
|
|
$warnings[] = count($missing_units)." Vobestellungen ohne Wohneinheit";
|
|
}
|
|
if(count($missing_extrefs)) {
|
|
$warnings[] = count($missing_extrefs)." Wohneinheiten ohne extref (SDIHome_)";
|
|
}
|
|
|
|
|
|
if($workorders_failed) {
|
|
$errors[] = "$workorders_failed Workorders konnten nicht erstellt werden.";
|
|
}
|
|
|
|
|
|
if(count($errors)) {
|
|
$this->layout()->setFlash(implode("<br />\n", $errors), "error");
|
|
}
|
|
if(count($warnings)) {
|
|
$this->layout()->setFlash(implode("<br />\n", $warnings), "warning");
|
|
}
|
|
|
|
if($workorders_created) {
|
|
$this->layout()->setFlash("$workorders_created Workorders erfolgreich erstellt", "success");
|
|
} else {
|
|
$this->layout()->setFlash("Es konnten keine Workorders erstellt werden", "info");
|
|
}
|
|
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
|
|
}
|
|
|
|
protected function exportOaidsToRimoAction() {
|
|
|
|
$this->layout()->setTemplate("Preordercampaign/Admin");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
if($campaign->oaid_origin == "other") {
|
|
$this->layout()->setFlash("Kampagne unterstützt keine OAIDs", "warning");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
}
|
|
|
|
$oaid_assigned = 0;
|
|
$missing_units = 0;
|
|
$missing_extrefs = 0;
|
|
$missing_oaid = 0;
|
|
|
|
foreach(PreorderModel::searchActive(["preordercampaign_id" => $id, "connection_count" => 1]) as $preorder) {
|
|
|
|
if($preorder->type == "interest") {
|
|
$this->log->debug(__METHOD__.": Ignoring Preorder ".$preorder->id.": is interest");
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->adb_wohneinheit_id) {
|
|
//echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n";
|
|
//$this->log->debug(__METHOD__.": Ignoring Preorder ".$preorder->id.": missing unit");
|
|
$missing_units++;
|
|
continue;
|
|
}
|
|
$wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) {
|
|
$this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
|
|
continue;
|
|
}
|
|
|
|
if(!$wohneinheit->extref) {
|
|
$missing_extrefs++;
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->oaid) {
|
|
$missing_oaid++;
|
|
continue;
|
|
}
|
|
|
|
// TODO; OpenaccessID::exportToRimoFtu
|
|
$oaid = OpenAccessIdModel::getFirstOaid($wohneinheit->oaid);
|
|
if(!$oaid) {
|
|
$this->log->warning("Cannot export OAID to rimo because not found: ".$wohneinheit->oaid." (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
|
|
continue;
|
|
}
|
|
//var_dump($oaid);exit;
|
|
if($oaid->exportToRimoAndAssignFtu()) {
|
|
$oaid_assigned++;
|
|
}
|
|
}
|
|
|
|
$this->layout()->setFlash("$oaid_assigned OAIDs erfolgreich erstellt und/oder FTUs zugeordnet.<br />$missing_units missing units<br />$missing_extrefs missing extref<br />$missing_oaid missing oaid");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
|
|
}
|
|
|
|
protected function updateOaidFromUnitAction() {
|
|
$this->layout()->setTemplate("Preordercampaign/Admin");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
if($campaign->oaid_origin == "other") {
|
|
$this->layout()->setFlash("Kampagne unterstützt keine OAIDs", "warning");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
}
|
|
|
|
$oaid_updated = 0;
|
|
|
|
foreach(PreorderModel::searchActive(["preordercampaign_id" => $id, "connection_count" => 1]) as $preorder) {
|
|
if($preorder->type == "interest") {
|
|
//$this->log->debug(__METHOD__.": Ignoring Preorder ".$preorder->id.": is interest");
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->adb_wohneinheit_id) {
|
|
//echo "Keine Wohneinheit in Preorder (".$preorder->id.") ".$preorder->ucode." ".$preorder->oaid."\n";
|
|
$this->log->debug(__METHOD__.": Ignoring Preorder ".$preorder->id.": missing unit");
|
|
$missing_units++;
|
|
continue;
|
|
}
|
|
$wohneinheit = new ADBWohneinheit($preorder->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) {
|
|
$this->log->error(__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
|
|
continue;
|
|
}
|
|
|
|
if(!$wohneinheit->extref) {
|
|
$missing_extrefs++;
|
|
continue;
|
|
}
|
|
|
|
if(!$preorder->oaid) {
|
|
continue;
|
|
}
|
|
|
|
$oaid = OpenAccessIdModel::getFirstOaid($wohneinheit->oaid);
|
|
if(!$oaid) {
|
|
$this->log->warning("Cannot export OAID to rimo because not found: ".$wohneinheit->oaid." (Preorder ".$preorder->id." ".$preorder->ucode." ".$preorder->oaid.")");
|
|
continue;
|
|
}
|
|
//var_dump($oaid);exit;
|
|
|
|
|
|
|
|
if($oaid->adb_wohneinheit_id != $wohneinheit->id) {
|
|
//$rimo = new OpenAccessId_Helper_Rimo($oaid->oaid);
|
|
|
|
$unit_ftu_data = $wohneinheit->ftu_data;
|
|
//var_dump($unit_ftu_data);
|
|
if(!$unit_ftu_data) {
|
|
continue;
|
|
}
|
|
|
|
$oaid_ftu = $oaid->getExportData("rimo");
|
|
//var_dump($oaid_ftu);
|
|
if($oaid_ftu->ftu_id) {
|
|
Rimoapi::unassignOaid($oaid->oaid, $oaid_ftu->ftu_id);
|
|
Rimoapi::assignOaid($oaid->oaid, $unit_ftu_data["id"]);
|
|
|
|
$oaid->updateExportData("rimo", "ftu_id", $unit_ftu_data["id"]);
|
|
$oaid->updateExportData("rimo", "ftu_name", $unit_ftu_data["name"]);
|
|
$oaid->updateExportData("rimo", "ftu_assigned_date", date("U"));
|
|
}
|
|
$oaid->address = $wohneinheit->hausnummer->getAddress();
|
|
$oaid->unit_string = (string)$wohneinheit;
|
|
|
|
$oaid->adb_wohneinheit_id = $wohneinheit->id;
|
|
$oaid->assigned = date("U");
|
|
$oaid->exported_to = "rimo";
|
|
$oaid->exported = date('U');
|
|
|
|
//var_dump($oaid);exit;
|
|
$oaid->save();
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
$this->layout()->setFlash("Done.", "success");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
|
|
}
|
|
|
|
protected function splitMultipleConnectionsAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$orders_split = 0;
|
|
$orders_new = 0;
|
|
$missing_units = 0;
|
|
|
|
foreach(PreorderModel::searchActive(["preordercampaign_id" => $campaign->id, "connection_count" => 2]) as $preorder) {
|
|
// check if we have enough units with extref
|
|
$wohneinheiten = ADBWohneinheitModel::search(["hausnummer_id" => $preorder->adb_hausnummer_id]);
|
|
$unit_count = count($wohneinheiten);
|
|
if($unit_count < $preorder->connection_count) {
|
|
$missing_units++;
|
|
continue;
|
|
}
|
|
|
|
$available_units = [];
|
|
$additional_units = []; // to sort non-specific units last
|
|
foreach($wohneinheiten as $unit) {
|
|
if($preorder->adb_wohneinheit_id == $unit->id || !PreorderModel::getFirstActive(["adb_wohneinheit_id" => $unit->id])) {
|
|
if($unit->tuer || preg_match('/^(Top|Tuer|Tür)\s+\d+$/i', $unit->zusatz)) {
|
|
$available_units[] = $unit;
|
|
} else {
|
|
$additional_units[] = $unit;
|
|
}
|
|
} else {
|
|
//echo $preorder->id." balh\n";
|
|
}
|
|
}
|
|
|
|
if(count($additional_units)) $available_units = array_merge($available_units, $additional_units);
|
|
|
|
if($preorder->connection_count > count($available_units)) {
|
|
$missing_units++;
|
|
continue;
|
|
}
|
|
|
|
// create new Preorders with available units
|
|
// then set original Preorder deleted
|
|
|
|
for($c = 0; $c < $preorder->connection_count; $c++) {
|
|
if(!$available_units[$c]->oaid) {
|
|
$available_units[$c]->oaid = $available_units[$c]->getNewOAID();
|
|
$available_units[$c]->save();
|
|
}
|
|
|
|
$new_preorder = clone($preorder);
|
|
$new_preorder->ucode = $preorder->ucode.".".($c+1);
|
|
$new_preorder->connection_count = 1;
|
|
$new_preorder->adb_wohneinheit_id = $available_units[$c]->id;
|
|
$new_preorder->oaid = $available_units[$c]->oaid;
|
|
|
|
$new_preorder->save();
|
|
}
|
|
//exit;
|
|
$preorder->deleted = date("U");
|
|
$preorder->delete_reason = "connection_split";
|
|
$preorder->save();
|
|
}
|
|
|
|
if($missing_units) {
|
|
$this->layout()->setFlash("$missing_units Bestellungen konnten nicht geteilt werden, da nicht genug Wohneinheiten verfügbar sind.", "warning");
|
|
}
|
|
|
|
$this->layout()->setFlash("$orders_split Bestellungen in $orders_new Bestellungen aufgeteilt.", "success");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
}
|
|
|
|
protected function addUnitsAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$campaign = new Preordercampaign($id);
|
|
if(!$campaign->id) {
|
|
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
|
|
$this->redirect("Preordercampaign");
|
|
}
|
|
|
|
$units_added = 0;
|
|
$missing_units = 0;
|
|
|
|
foreach(PreorderModel::searchActive(['preordercampaign_id' => $campaign->id, 'adb_wohneinheit_id' => null, 'connection_count' => 1, "connection_type" => "single-dwelling"]) as $preorder) {
|
|
if($preorder->adb_wohneinheit_id) {
|
|
continue;
|
|
}
|
|
if(!$preorder->adb_hausnummer_id) {
|
|
echo "missing hausnummer Preorder ".$preorder->id."\n";
|
|
continue;
|
|
}
|
|
|
|
$wohneinheiten = ADBWohneinheitModel::search(['hausnummer_id' => $preorder->adb_hausnummer_id]);
|
|
if(!count($wohneinheiten)) {
|
|
$missing_units++;
|
|
continue;
|
|
}
|
|
$unit_count = count($wohneinheiten);
|
|
//echo "$unit_count\n";
|
|
if($unit_count === 1) {
|
|
$same_unit_count++;
|
|
$unit = $wohneinheiten[0];
|
|
$preorder->adb_wohneinheit_id = $unit->id;
|
|
$preorder->save();
|
|
|
|
$units_added++;
|
|
continue;
|
|
}
|
|
|
|
|
|
if($unit_count > 1) {
|
|
// legacy comment: assume its single-dwelling with erroneously high door count
|
|
// current comment: Just use any unit
|
|
$unit_candidates = [];
|
|
|
|
foreach($wohneinheiten as $unit) {
|
|
if(!$unit->tuer) continue;
|
|
if($unit->tuer > 1) {
|
|
$unit_candidates[$unit->tuer] = $unit;
|
|
}
|
|
}
|
|
|
|
// no candidates with door number -> use any
|
|
if(!count($unit_candidates)) {
|
|
foreach($wohneinheiten as $unit) {
|
|
$unit_candidates[] = $unit;
|
|
}
|
|
}
|
|
|
|
if(count($unit_candidates)) {
|
|
ksort($unit_candidates, SORT_NUMERIC);
|
|
$new_unit = array_shift($unit_candidates);
|
|
$preorder->adb_wohneinheit_id = $new_unit->id;
|
|
$preorder->save();
|
|
|
|
$units_added++;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$i++;
|
|
}
|
|
|
|
if($missing_units) {
|
|
$this->layout()->setFlash("Für $missing_units Bestellungen wurde keine Wohneinheit gefunden.", "warning");
|
|
}
|
|
|
|
$this->layout()->setFlash("$units_added Wohneinheiten eingetragen.", "success");
|
|
$this->redirect("Preordercampaign", "Admin", ["id" => $id]);
|
|
|
|
}
|
|
|
|
} |