1662 lines
63 KiB
PHP
1662 lines
63 KiB
PHP
<?php
|
|
|
|
class Preorder extends mfBaseModel {
|
|
protected $forcestr = ['street', 'company', 'zip', 'phone', 'email', 'note', 'extref', 'ucode', 'ciftoken'];
|
|
private $in_after_save = 0;
|
|
|
|
private $status;
|
|
private $statusflags;
|
|
private $campaign;
|
|
private $partner;
|
|
private $discounts;
|
|
private $applied_discounts;
|
|
private $building;
|
|
private $adb_hausnummer;
|
|
private $adb_wohneinheit;
|
|
private $fcp;
|
|
private $services;
|
|
private $ordered_services;
|
|
private $creator;
|
|
private $editor;
|
|
private $attribute = [];
|
|
private $logistics;
|
|
private $history;
|
|
private $citycomoan; // ONT data
|
|
private $ctags; // network-keyed array of Ctags
|
|
private $statusjournals;
|
|
|
|
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 afterLoad() {
|
|
if($this->uid === "string") {
|
|
$this->uid = "";
|
|
}
|
|
}
|
|
|
|
public function beforeSave($_params = []) {
|
|
if(!isset($this->data->status_id)) {
|
|
$this->data->status_id = 1;
|
|
}
|
|
}
|
|
|
|
public function afterSave($_params = []) {
|
|
// reset auto magic properties
|
|
$this->status = null;
|
|
$this->campaign = null;
|
|
$this->partner = null;
|
|
$this->building = null;
|
|
$this->adb_hausnummer = null;
|
|
$this->adb_wohneinheit = null;
|
|
$this->services = null;
|
|
$this->ordered_services = null;
|
|
$this->creator = null;
|
|
$this->editor = null;
|
|
|
|
if(array_key_exists("no_aftersave", $_params) && $_params["no_aftersave"]) return true;
|
|
|
|
$this->log->debug("[".$this->_ruid."] "."-----------------------------------------");
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": preorder_id: ".$this->id);
|
|
|
|
// prevent potential infinite loop
|
|
$nesting_level = mfValuecache::singleton()->get("preorder-save-nesting-level-" . $this->id);
|
|
if(!$nesting_level) {
|
|
$nesting_level = 1;
|
|
} else {
|
|
$nesting_level++;
|
|
}
|
|
mfValuecache::singleton()->set("preorder-save-nesting-level-" . $this->id, $nesting_level);
|
|
|
|
if($nesting_level > 1) {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": (Preorder ".$this->id.") Nesting level limit reached ($nesting_level) -> aborting");
|
|
return true;
|
|
}
|
|
|
|
if(!array_key_exists("no_oaid_update", $_params) || !$_params['no_oaid_update']) {
|
|
// update preorder OAID if it's different from the unit OAID
|
|
// but only if the unit OAID is of the same origin as the campaign
|
|
$old_oaid = $this->oaid;
|
|
|
|
$this->setOrCreateOaid();
|
|
if($this->oaid != $old_oaid) {
|
|
$this->resetSaveNesting();
|
|
$this->save();
|
|
//return true;
|
|
}
|
|
}
|
|
|
|
|
|
//TODO: history start
|
|
//if($this->status_id != $this->_old_data->status_id) {
|
|
$this->createHistoryEntry();
|
|
//}
|
|
|
|
if(!array_key_exists("dont_cascade", $_params) || !$_params['dont_cascade']) {
|
|
$this->updateRimoWorkorderContact();
|
|
// run triggers based on new status
|
|
$this->runStatusTrigger();
|
|
// Cascade status changes down to adb_hausnummer and adb_wohneinheit
|
|
$this->cascadeStatus();
|
|
// Cascade status changes down all active preorders with the same hausnummer
|
|
$this->cascadeStatusToPreorders();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public function resetSaveNesting() {
|
|
mfValuecache::singleton()->delete("preorder-save-nesting-level-" . $this->id);
|
|
}
|
|
|
|
public function getStatuschangeTo($status_code) {
|
|
$status = PreorderstatusModel::getFirst(["code" => $status_code]);
|
|
if(!$status) {
|
|
return false;
|
|
}
|
|
|
|
$history = PreorderHistoryModel::getLastStatusChangeTo($this->id, $status_code);
|
|
if(!$history) {
|
|
return false;
|
|
}
|
|
|
|
return $history->changed;
|
|
}
|
|
|
|
public function updateRimoWorkorderContact() {
|
|
$contact_fields = [
|
|
"company",
|
|
"uid",
|
|
"firstname",
|
|
"lastname",
|
|
"phone",
|
|
"email"
|
|
];
|
|
$changed_fields = $this->getChangedFields();
|
|
|
|
$update = [];
|
|
foreach($changed_fields as $field) {
|
|
if(in_array($field, $contact_fields)) {
|
|
$update[$field] = $this->data->$field;
|
|
}
|
|
}
|
|
|
|
$campaign = $this->getProperty("campaign");
|
|
if($campaign->oaid_origin == "other") return true;
|
|
|
|
if($this->adb_wohneinheit_id && is_array($this->getProperty("adb_wohneinheit")->rimo_workorders)) {
|
|
foreach($this->getProperty("adb_wohneinheit")->rimo_workorders as $workorder) {
|
|
Rimoapi::updateWorkorder($workorder->rimo_id, $update);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function createHistoryEntry() {
|
|
if(!$this->id) return true;
|
|
|
|
$changed = $this->getChangedFields();
|
|
|
|
try {
|
|
foreach($changed as $field) {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": $field changed from '" . $this->_old_data->$field . "' to '" . $this->data->$field . "'");
|
|
$history = PreorderHistoryModel::create([
|
|
"preorder_id" => $this->id,
|
|
"key" => $field,
|
|
"old_value" => $this->_old_data->$field,
|
|
"new_value" => $this->data->$field,
|
|
"changed" => date("U")
|
|
]);
|
|
$history->save();
|
|
}
|
|
} catch(Exception $e) {
|
|
$this->log->debug("[".$this->_ruid."] ".$e->getTraceAsString());
|
|
}
|
|
}
|
|
|
|
public function runStatusTrigger() {
|
|
if(!$this->id) return true;
|
|
if(property_exists($this->_old_data, "status_id") && $this->status_id == $this->_old_data->status_id) return true;
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . " running");
|
|
|
|
$new_status = $this->getProperty("status");
|
|
if(!property_exists($this->_old_data, "status_id")) {
|
|
return true;
|
|
}
|
|
|
|
$old_status = new Preorderstatus($this->_old_data->status_id);
|
|
|
|
if(!$new_status->id || !$old_status->id) return true;
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . " status changed from '" . ($old_status ? $old_status->code : "") . "' to '" . $new_status->code . "'");
|
|
|
|
if($new_status->code <= $old_status->code) return true;
|
|
|
|
if(!defined("TT_PREORDER_STATUS_MATRIX") || !TT_PREORDER_STATUS_MATRIX) {
|
|
$this->log->error("[".$this->_ruid."] "."config TT_PREORDER_STATUS_MATRIX not defined!");
|
|
}
|
|
|
|
$actions = [];
|
|
|
|
// run every trigger bnetween old and new status code
|
|
foreach(TT_PREORDER_STATUS_MATRIX as $intermediate_code => $status) {
|
|
if($intermediate_code <= $old_status->code) continue;
|
|
if($intermediate_code > $new_status->code) continue;
|
|
|
|
$code = $intermediate_code;
|
|
// find trigger for new status code
|
|
$classname = "Preorder_Statustrigger_$code";
|
|
$filepath = __DIR__ . "/statustrigger/$code.php";
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": Looking for $classname in $filepath");
|
|
|
|
if(file_exists($filepath)) {
|
|
require_once $filepath;
|
|
}
|
|
|
|
if(class_exists($classname)) {
|
|
$trigger = new $classname($this, $new_status);
|
|
$trigger->run();
|
|
|
|
/*
|
|
// deprecated, replaced by PreordercampaignStatusnotifictionMailtemplate
|
|
//var_dump($status["action"]);
|
|
if(is_array($status) && array_key_exists("action", $status)) {
|
|
foreach ($status["action"] as $type => $action) {
|
|
if (!array_key_exists($type, $actions)) {
|
|
$actions[$type] = [];
|
|
}
|
|
$action["status_code"] = $intermediate_code;
|
|
$actions[$type][] = $action;
|
|
}
|
|
}*/
|
|
} else {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": $classname not found");
|
|
}
|
|
|
|
// TODO create email action if status templates configured in Campaign
|
|
$campaign = new Preordercampaign($this->preordercampaign_id);
|
|
if(!$campaign->id) continue;
|
|
|
|
$status_mail = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $this->preordercampaign_id, "status_code" => $intermediate_code]);
|
|
if($status_mail) {
|
|
$mailtemplate = $status_mail->mailtemplate;
|
|
if(!$mailtemplate) continue;
|
|
|
|
if(!array_key_exists("email", $actions)) {
|
|
$actions["email"] = [];
|
|
}
|
|
$actions["email"][] = [
|
|
"template" => $mailtemplate->code,
|
|
"from" => "campaign",
|
|
"data" => "preorder, adb_hausnummer, adb_wohneinheit",
|
|
"status_code" => $intermediate_code,
|
|
"allow_on_skip" => $status_mail->allow_on_skip
|
|
];
|
|
}
|
|
}
|
|
|
|
//var_dump($actions);exit;
|
|
|
|
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".print_r($actions, true));
|
|
//var_dump($actions);exit;
|
|
// run last email action
|
|
if(array_key_exists("email", $actions) && is_array($actions["email"]) && count($actions["email"])) {
|
|
// get last email action, ignore previous status'
|
|
// we only want to send emails for the highest new status
|
|
$email_action = array_pop($actions["email"]);
|
|
|
|
// check if this or a higher-status email was sent already
|
|
$send_email = true;
|
|
foreach(PreorderStatusnotificationLog::search(["preorder_id" => $this->id]) as $log) {
|
|
if($log->status_code >= $email_action["status_code"]) {
|
|
$send_email = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if($new_status->code != $email_action["status_code"]) {
|
|
// check if curent status allows sending previous status' notifications
|
|
$current_status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $this->preordercampaign_id, "status_code" => $new_status->code]);
|
|
if($current_status_mt && $current_status_mt->prevent_previous) {
|
|
$send_email = false;
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Sending disallowed by current status");
|
|
}
|
|
|
|
// check if email action allows sending if skipped over it
|
|
if(!$email_action["allow_on_skip"]) {
|
|
$send_email = false;
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Sending disallowed by previous status");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if($send_email) {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Sending Email Action for Status ".$email_action["status_code"]);
|
|
$email_to = $this->runTriggerEmailAction($email_action);
|
|
if($email_to === false) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": Could not send preorder action email (Preorder " . $this->id . ")");
|
|
} elseif(is_string($email_to)) {
|
|
// TODO Save history
|
|
$psn_log = PreorderStatusnotificationLog::create([
|
|
"preorder_id" => $this->id,
|
|
"status_code" => $email_action["status_code"],
|
|
"email" => $email_to
|
|
]);
|
|
$psn_log->save();
|
|
}
|
|
} else {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__.": Not sending status (".$email_action["status_code"].") email because disallowed by current status or same or higher status email was sent already (Preorder ".$this->id.")");
|
|
}
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public function runTriggerEmailAction($action, $custom_replacers = []) {
|
|
if(!array_key_exists("template", $action)) return false;
|
|
if(!array_key_exists("from", $action)) return false;
|
|
|
|
$from_name = $this->getProperty("campaign")->from_email_name;
|
|
$from_email = $this->getProperty("campaign")->from_email;
|
|
if(!$from_email || !$from_name) {
|
|
return true;
|
|
}
|
|
|
|
$to = $this->email;
|
|
|
|
if(array_key_exists("to", $action) && $action["to"]) {
|
|
$to = $action["to"];
|
|
}
|
|
|
|
if(!$to) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__.": Keine To Adresse (Preorder ".$this->id.")");
|
|
return false;
|
|
}
|
|
|
|
|
|
$data = [];
|
|
if(array_key_exists("data", $action)) {
|
|
foreach(preg_split('/ *, */', $action["data"]) as $data_type) {
|
|
if($data_type == "preorder") {
|
|
$data["preorder"] = $this->getPropertiesAsArray();
|
|
}
|
|
if($data_type == "adb_hausnummer") {
|
|
// TODO Array convertion in jeweilige Klasse auslagern
|
|
$hausnummer = $this->getProperty("adb_hausnummer");
|
|
if(!$hausnummer) continue;
|
|
$hausnummer_data = (array) $this->getProperty("adb_hausnummer")->data;
|
|
unset($hausnummer->data, $hausnummer->_old_data, $hausnummer->db, $hausnummer->log);
|
|
|
|
$data["adb_hausnummer"] = array_merge((array) $hausnummer, $hausnummer_data);
|
|
}
|
|
if($data_type == "adb_wohneinheit") {
|
|
$wohneinheit = $this->getProperty("adb_wohneinheit");
|
|
if(!$wohneinheit) continue;
|
|
$wohneinheit_data = (array) $this->getProperty("adb_wohneinheit")->data;
|
|
unset($wohneinheit->data, $wohneinheit->_old_data, $wohneinheit->db, $wohneinheit->log);
|
|
|
|
$data["adb_wohneinheit"] = array_merge((array) $wohneinheit, $wohneinheit_data);
|
|
}
|
|
}
|
|
}
|
|
|
|
$mailtemplate = MailtemplateModel::getFirst(["code" => $action["template"]]);
|
|
if(!$mailtemplate) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__.": Mailtemplate nicht gefunden: ".$action["template"]." (Preorder ".$this->id.")");
|
|
return false;
|
|
}
|
|
|
|
$replacers = [];
|
|
foreach($data as $type => $values) {
|
|
foreach($values as $key => $value) {
|
|
if(substr($key, 0, 1) == "\0") continue;
|
|
$replacers[$key] = $value;
|
|
}
|
|
}
|
|
|
|
foreach($custom_replacers as $key => $value) {
|
|
if(!$key) continue;
|
|
$replacers[$key] = $value;
|
|
}
|
|
|
|
$subject = $mailtemplate->getVariableReplacedSubject($replacers);
|
|
$body = '<html lang="de"><head><meta charset="utf-8" /></head><body>';
|
|
$body .= $mailtemplate->renderBody($replacers);
|
|
$body .= "</body></html>\n";
|
|
|
|
$body_type = $mailtemplate->body_html ? "html" : "text";
|
|
|
|
$email = new Emailnotification("Preorder", $this->id, json_encode(["template" => $action["template"]]));
|
|
$email->setSubject($subject);
|
|
if($body_type == "html") {
|
|
$email->setHtmlBody($body);
|
|
} else {
|
|
$email->setBody($body);
|
|
}
|
|
$email->setFrom($from_email, $from_name);
|
|
$email->setTo($to);
|
|
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $this->id);
|
|
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", $this->getProperty("status")->code);
|
|
// add attachments
|
|
try {
|
|
foreach ($mailtemplate->files as $file) {
|
|
if (!$file->filename || !$file->file_id || !$file->file->store_filename) continue;
|
|
$this->log->debug("[".$this->_ruid."] ".$file->file->getFullPath());
|
|
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
|
|
}
|
|
$email->send();
|
|
} catch (Exception $e) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__.": ".$e->getMessage());
|
|
}
|
|
|
|
$this->log->info("[".$this->_ruid."] ".__METHOD__.": Sending StatusTrigger Email to $to");
|
|
|
|
|
|
return $to;
|
|
}
|
|
|
|
public function getPropertiesAsArray() {
|
|
$preorder = clone $this;
|
|
unset($preorder->data, $preorder->_old_data, $preorder->db, $preorder->log);
|
|
$status = $this->getProperty("status");
|
|
$array = (array) $preorder;
|
|
|
|
$array = array_merge($array, (array) $this->data);
|
|
$return = [];
|
|
foreach($array as $key => $value) {
|
|
if(substr($key, 0, 1) == "\0") continue;
|
|
$return[$key] = $value;
|
|
}
|
|
|
|
$return["status_code"] = $status->code;
|
|
$return["status_name"] = $status->name;
|
|
$return["contact_fullname"] = $this->getCompanyOrName();
|
|
|
|
return $return;
|
|
}
|
|
|
|
/*
|
|
* Cascade status changes down to adb_hausnummer and adb_wohneinheit
|
|
*/
|
|
public function cascadeStatus() {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__. " entered");
|
|
/*
|
|
* defines status alignments of preorder status to hausnummer (h) and wohneinheit (w)
|
|
* if h or w are greater than defined here, don't change them
|
|
*
|
|
* $statusmatrix = [
|
|
* "preorder_status_code" => [
|
|
* "h" => adb_status_code,
|
|
* "w" => adb_status_code
|
|
* ]
|
|
* ]
|
|
*/
|
|
|
|
if(!$this->adb_hausnummer_id && !$this->adb_wohneinheit_id) {
|
|
return true;
|
|
}
|
|
|
|
if(!defined("TT_PREORDER_STATUS_MATRIX") || !is_array(TT_PREORDER_STATUS_MATRIX) || !count(TT_PREORDER_STATUS_MATRIX)) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": TT_PREORDER_STATUS_MATRIX undefined! Cannot cascade Preorder status to Hausnummer and Wohneinheit");
|
|
return true;
|
|
}
|
|
|
|
$hausnummer = new ADBHausnummer($this->adb_hausnummer_id);
|
|
if(!$hausnummer->id) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": hausnummer " . $this->adb_hausnummer_id . " not found!");
|
|
return true;
|
|
}
|
|
|
|
$new_status = new Preorderstatus($this->status_id);
|
|
if(!$new_status->id) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": Preorder has invalid status! ".print_r($this, true));
|
|
return true;
|
|
}
|
|
|
|
if(!array_key_exists($new_status->code, TT_PREORDER_STATUS_MATRIX)) {
|
|
return true;
|
|
}
|
|
|
|
$cascade = TT_PREORDER_STATUS_MATRIX[$new_status->code];
|
|
|
|
if($cascade["h"] && $this->adb_hausnummer_id) {
|
|
// set new hausnummer status
|
|
$new_hausnummer_status = ADBStatusModel::getFirst(["code" => $cascade["h"]]);
|
|
if(!$new_hausnummer_status) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": new ADBStatus code " . $cascade["h"] . " not found!");
|
|
return true;
|
|
}
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": new hausnummer status code " . $new_hausnummer_status->code);
|
|
|
|
// only update if current status is less than new status
|
|
if($hausnummer->status->code < $new_hausnummer_status->code) {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": Setting new hausnummer status code: " . $new_hausnummer_status->code);
|
|
$hausnummer->status_id = $new_hausnummer_status->id;
|
|
$hausnummer->save();
|
|
}
|
|
}
|
|
|
|
if($cascade["w"] && $this->adb_wohneinheit_id) {
|
|
// set new wohneinheit status
|
|
$new_wohneinheit_status = ADBStatusModel::getFirst(["code" => $cascade["w"]]);
|
|
if(!$new_wohneinheit_status) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": new ADBStatus code " . $cascade["w"] . " not found!");
|
|
return true;
|
|
}
|
|
|
|
$wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) return true;
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": new wohneinheit status code " . $new_wohneinheit_status->code);
|
|
// only update if current status is less than new status
|
|
if($wohneinheit->status->code < $new_wohneinheit_status->code) {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": Setting new wohneinheit status code: " . $new_wohneinheit_status->code);
|
|
$wohneinheit->status_id = $new_wohneinheit_status->id;
|
|
$wohneinheit->save();
|
|
}
|
|
}
|
|
|
|
$flags = $this->getProperty("statusflags");
|
|
|
|
if(is_array($flags) && count($flags)) {
|
|
foreach($flags as $flag) {
|
|
if(strlen($flag->value->value) && array_key_exists($flag->code, TT_PREORDER_STATUS_FLAG_MATRIX)) {
|
|
$flag_matrix_item = TT_PREORDER_STATUS_FLAG_MATRIX[$flag->code];
|
|
if(!$flag_matrix_item["flag"]) continue;
|
|
|
|
// set hausnummer flag
|
|
if($flag_matrix_item["h"] && $this->adb_hausnummer_id) {
|
|
$hflag = ADBStatusflagModel::getFirst(["code" => $flag_matrix_item["h"]]);
|
|
if(!$hflag) {
|
|
$this->log->warn("[".$this->_ruid."] "."Statusflag Code ".$flag->code." does not exist");
|
|
} else {
|
|
//var_dump($hflag);exit;
|
|
$hflag->hausnummer_id = $hausnummer->id;
|
|
$hflag->value->value = $flag->value->value;
|
|
$hflag->save();
|
|
$this->log->debug("[" . $this->_ruid . "] " . __METHOD__ . ": Hausnummer flag " . $hflag->code . " value '" . $hflag->value->value . "' gespeichert");
|
|
}
|
|
}
|
|
|
|
// set wohneiheit flag
|
|
if($flag_matrix_item["w"] && $this->adb_wohneinheit_id) {
|
|
$wflag = ADBStatusflagModel::getFirst(["code" => $flag_matrix_item["w"]]);
|
|
if(!$wflag) {
|
|
$this->log->warn("[".$this->_ruid."] "."Statusflag Code ".$flag->code." does not exist");
|
|
} else {
|
|
$wflag->wohneinheit_id = $wohneinheit->id;
|
|
$wflag->value->value = $flag->value->value;
|
|
$wflag->value->save();
|
|
$this->log->debug("[" . $this->_ruid . "] " . __METHOD__ . ": Wohneinheit flag " . $hflag->code . " value '" . $wflag->value->value . "' gespeichert");
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("ws", $flag_matrix_item) && $flag_matrix_item["ws"] && $this->adb_wohneinheit_id) {
|
|
//echo "Setting Wohneinheit status to ".$flag_matrix_item["ws"]." from ".$this->getProperty("adb_wohneinheit")->status->code."\n";
|
|
// set new wohneinheit status
|
|
$new_wohneinheit_status = ADBStatusModel::getFirst(["code" => $flag_matrix_item["ws"]]);
|
|
if(!$new_wohneinheit_status) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": new ADBStatus code " . $flag_matrix_item["w"] . " not found!");
|
|
return true;
|
|
}
|
|
|
|
$wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) return true;
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": new wohneinheit status code " . $new_wohneinheit_status->code);
|
|
// only update if current status is less than new status
|
|
if($wohneinheit->status->code < $new_wohneinheit_status->code) {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__ . ": Setting new wohneinheit status code: " . $new_wohneinheit_status->code);
|
|
$wohneinheit->status_id = $new_wohneinheit_status->id;
|
|
$wohneinheit->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public function cascadeStatusToPreorders() {
|
|
$status = new Preorderstatus($this->status_id);
|
|
if(!$status->id) {
|
|
return false;
|
|
}
|
|
|
|
if($this->adb_hausnummer_id) {
|
|
foreach(PreorderModel::searchActive(["adb_hausnummer_id" => $this->adb_hausnummer_id, "<status_code" => $status->code]) as $preorder) {
|
|
if($preorder->deleted) continue;
|
|
if($preorder->id == $this->id) continue;
|
|
|
|
// load status explicitly to work around
|
|
// visibility in different instances of same class
|
|
$tmp_s = $preorder->getProperty("status");
|
|
|
|
/*if(!$preorder->status->code || !$status->code) {
|
|
$this->log->debug("[".$this->_ruid."] "."Preorder->status->code". print_r($preorder->status, true));
|
|
$this->log->debug("[".$this->_ruid."] "."Status->code". print_r($status->code, true));
|
|
}*/
|
|
|
|
if($preorder->status->code < $status->code && $status->code <= 244) {
|
|
$preorder->status_id = $status->id;
|
|
$preorder->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function syncStatusFlagsFromAdb() {
|
|
if(!$this->adb_hausnummer_id) return true;
|
|
|
|
$hausnummer = $this->getProperty("adb_hausnummer");
|
|
|
|
foreach(ADBStatusflagModel::getAll() as $hflag) {
|
|
$hflag_value = ADBHausnummerStatusflagValueModel::search(["hausnummer_id" => $hausnummer->id, "flag_id" => $hflag->id]);
|
|
if(!$hflag_value) {
|
|
$hflag_value = ADBHausnummerStatusflagValueModel::create([
|
|
"hausnummer_id" => $hausnummer->id,
|
|
"flag_id" => $hflag->id
|
|
]);
|
|
}
|
|
|
|
$val = $hausnummer->statusflags[$hflag->id]->value->value;
|
|
//var_dump($hausnummer->statusflags[$hflag->id]->value->value);exit;
|
|
|
|
$pflag = PreorderStatusflagModel::getFirst(["preorder_id" => $this->id, "code" => $hflag->code]);
|
|
if(!$pflag) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__."PreorderStatusFlag with code ".$hflag->code." not found!");
|
|
continue;
|
|
}
|
|
$pflag_value = PreorderStatusflagValueModel::getFirst(["preorder_id" => $this->id, "flag_id" => $pflag->id]);
|
|
if(!$pflag_value) {
|
|
$pflag_value = PreorderStatusflagValueModel::create([
|
|
"preorder_id" => $this->id,
|
|
"flag_id" => $pflag->id
|
|
]);
|
|
}
|
|
$pflag_value->value = ($val) ? 1 : 0;
|
|
//var_dump($pflag_value);
|
|
$pflag_value->save();
|
|
//echo "saved value ".$pflag_value->value." for flag_id ".$pflag->id."\n";
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getCompanyOrName() {
|
|
$company = trim($this->company);
|
|
if($company) {
|
|
return $company;
|
|
}
|
|
return $this->getFullName();
|
|
}
|
|
|
|
public function getFullName() {
|
|
// Assumes "Firma1 Firma2" or "firstname lastname" as readable form
|
|
$name = "";
|
|
|
|
if($this->firstname && $this->lastname) {
|
|
$name = $this->firstname . " " . $this->lastname;
|
|
} elseif($this->lastname) {
|
|
$name = $this->lastname;
|
|
} elseif($this->firstname) {
|
|
$name = $this->firstname;
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
public function getFreeCtagsInSet($search_ctag) {
|
|
if(!$this->getProperty("adb_hausnummer")->vlan_stag) {
|
|
echo "no stag\n";
|
|
return false;
|
|
}
|
|
|
|
$stag = $this->adb_hausnummer->vlan_stag;
|
|
if(!$stag) {
|
|
return false;
|
|
}
|
|
|
|
$ctags_per_home = 1;
|
|
|
|
$network_name = "";
|
|
if($this->adb_hausnummer->netzgebiet->source == "citycom-oan-api") {
|
|
$network_name = "citycom-oan";
|
|
$ctags_per_home = count(CITYCOM_OAN_API_SERVICES_FOR_ORDER) + count(CITYCOM_OAN_API_SERVICES_FOR_RESERVATION); // Service VLANS + mgmt VLANS
|
|
}
|
|
|
|
if(!$network_name) {
|
|
echo "no network\n";
|
|
return false;
|
|
}
|
|
|
|
// get start of ctag range
|
|
$first_ctag = $search_ctag - ($search_ctag % $ctags_per_home);
|
|
|
|
$ctag_range = [];
|
|
for($i = $first_ctag; $i < $first_ctag + $ctags_per_home; $i++) {
|
|
if(!PreorderCtag::getFirstActive(["stag" => $stag, "ctag" => $i, "network" => $network_name])) {
|
|
$ctag_range[] = $i;
|
|
}
|
|
|
|
}
|
|
return $ctag_range;
|
|
}
|
|
|
|
public function getNextFreeCtags() {
|
|
if(!$this->getProperty("adb_hausnummer")->vlan_stag) {
|
|
echo "no stag\n";
|
|
return false;
|
|
}
|
|
|
|
$stag = $this->adb_hausnummer->vlan_stag;
|
|
if(!$stag) {
|
|
return false;
|
|
}
|
|
|
|
$new_ctag = false;
|
|
$ctags_per_home = 1;
|
|
|
|
$network_name = "";
|
|
if($this->adb_hausnummer->netzgebiet->source == "citycom-oan-api") {
|
|
$network_name = "citycom-oan";
|
|
$first_ctag = CITYCOM_OAN_FIRST_CTAG;
|
|
$ctags_per_home = count(CITYCOM_OAN_API_SERVICES_FOR_ORDER) + count(CITYCOM_OAN_API_SERVICES_FOR_RESERVATION); // Service VLANS + mgmt VLANS
|
|
}
|
|
|
|
if(!$network_name) {
|
|
echo "no network\n";
|
|
return false;
|
|
}
|
|
|
|
$last_ctag = PreorderCtag::getLastActive(["stag" => $stag, "network" => $network_name]);
|
|
if(!$last_ctag) {
|
|
$new_ctag = $first_ctag;
|
|
} else {
|
|
$last_ctag_num = $last_ctag->ctag;
|
|
|
|
$miss = $last_ctag_num % $ctags_per_home;
|
|
if($miss) {
|
|
$last_ctag_num = $last_ctag_num - $miss;
|
|
}
|
|
|
|
$new_ctag = $last_ctag_num + $ctags_per_home;
|
|
}
|
|
|
|
$new_ctags = [];
|
|
for($i = $new_ctag; $i < $new_ctag + $ctags_per_home; $i++) {
|
|
$new_ctags[] = $i;
|
|
}
|
|
|
|
return $new_ctags;
|
|
}
|
|
|
|
public function setOrCreateOaid($oaid_attributes = false) {
|
|
$campaign = new Preordercampaign($this->preordercampaign_id);
|
|
//var_dump($campaign);exit;
|
|
if(!$campaign->id) {
|
|
return false;
|
|
}
|
|
|
|
if(!$this->adb_wohneinheit_id) {
|
|
$this->oaid = null;
|
|
return false;
|
|
}
|
|
|
|
$wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
|
|
if(!$wohneinheit) {
|
|
return true;
|
|
}
|
|
|
|
/*if($campaign->oaid_origin == "other") {
|
|
// oaid in wohneinheit übernehmen, falls in Preorder schon vorhanden
|
|
if($this->oaid)
|
|
// oaid aus wohneinheit übernehmen, falls vorhanden
|
|
|
|
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Kampagne unterstützt keine OAIDs");
|
|
return true;
|
|
}*/
|
|
|
|
if($campaign->oaid_origin == "other") {
|
|
if($wohneinheit->oaid != $this->oaid) {
|
|
$this->oaid = $wohneinheit->oaid;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if($this->oaid) {
|
|
// If current OAID is from correct Origin then do nothing
|
|
if($campaign->oaid_origin == "thetool") {
|
|
$current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid);
|
|
if($current_oaid) {
|
|
$this->log->warning("[".$this->_ruid."] "."OAID of Preorder " . $this->id . " should be thetool, but is OFAA");
|
|
} else {
|
|
if($wohneinheit->oaid != $this->oaid) {
|
|
$this->oaid = $wohneinheit->oaid;
|
|
return true;
|
|
}
|
|
}
|
|
} elseif($campaign->oaid_origin == "ofaa") {
|
|
$current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid);
|
|
//var_dump($current_oaid);exit;
|
|
if(!$current_oaid) {
|
|
$this->log->error("[".$this->_ruid."] "."OAID of Preorder " . $this->id . " not found in OpenAccessIds");
|
|
// assume it's from a different origin
|
|
} else {
|
|
if($campaign->oaid_origin == $current_oaid->origin) {
|
|
// also check if wohneinheit has no oaid and set it
|
|
if(!$wohneinheit->oaid) {
|
|
$wohneinheit->oaid = $current_oaid->oaid;
|
|
$wohneinheit->save();
|
|
} elseif($wohneinheit->oaid != $this->oaid) {
|
|
// also check if wohneinheit has a different OAID and set it in the Preorder
|
|
// No need to update OAID object, it should be correct already, because Wohneinheit is the source of oaid data,
|
|
$this->oaid = $wohneinheit->oaid;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if($this->type == "interest") return true;
|
|
|
|
if(!$this->id) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__ . ": Tried to create OAID in unsaved Preorder");
|
|
throw new Exception(__METHOD__ . ": Tried to create OAID in unsaved Preorder");
|
|
}
|
|
|
|
|
|
// use Wohneinehit OAID if available and from same origin
|
|
$unit_oaid = $this->getOaidFromWohneinheitIfOriginMatch();
|
|
if($unit_oaid) {
|
|
$this->oaid = $unit_oaid;
|
|
$this->save();
|
|
return true;
|
|
}
|
|
|
|
|
|
// else create new OAID
|
|
|
|
if(!$campaign->network) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": Cannot create OAID: Invalid campaign Network");
|
|
return false;
|
|
}
|
|
$netowner = new Address($campaign->network->owner_id);
|
|
if(!$netowner->id) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__ . ": Cannot create OAID: Invalid campaign Network Owner");
|
|
return false;
|
|
}
|
|
|
|
switch($campaign->oaid_origin) {
|
|
case "ofaa":
|
|
if(!$oaid_attributes) {
|
|
$oaid_attributes = [
|
|
"origin" => $campaign->oaid_origin,
|
|
"owner_id" => $campaign->network->owner_id
|
|
];
|
|
}
|
|
|
|
// get new OFAA OAID
|
|
$oaid = new OpenAccessId();
|
|
$oaid->loadRandomUnassigned($oaid_attributes);
|
|
|
|
if(!$oaid->oaid) {
|
|
$this->log->error("[".$this->_ruid."] "."Keine weiteren OAIDs verfügbar");
|
|
throw new Exception("Keine weiteren OAIDs verfügbar in " . $netowner->getCompanyOrName());
|
|
} else {
|
|
// make sure this OAID is not in use on another wohneinheit by accident
|
|
$oaid_unit_count = ADBWohneinheitModel::count(['oaid' => $oaid->oaid]);
|
|
$oaid_unit_try = 0;
|
|
while($oaid_unit_count) {
|
|
if($oaid_unit_try > 5) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__ . ": Can't find random OAID which is not already used in Wohneinheit by accident after 5 tries. Giving up");
|
|
throw new Exception("Can't find random OAID which is not already used in Wohneinheit by accident after 5 tries. Giving up");
|
|
}
|
|
$oaid_unit_try++;
|
|
|
|
$oaid = new OpenAccessId();
|
|
$oaid->loadRandomUnassigned($oaid_attributes);
|
|
$oaid_unit_count = ADBWohneinheitModel::count(['oaid' => $oaid->oaid]);
|
|
}
|
|
}
|
|
|
|
if(!$oaid->oaid) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__ . ": Cannot generate OAID: OpenAccessId::loadRandomUnassigned() failed.");
|
|
return false;
|
|
}
|
|
|
|
if($wohneinheit->oaid != $oaid->oaid) {
|
|
$wohneinheit->oaid = $oaid->oaid;
|
|
$wohneinheit->save();
|
|
}
|
|
|
|
$this->oaid = $oaid->oaid;
|
|
|
|
$oaid->assigned = date('U');
|
|
$oaid->adb_wohneinheit_id = $wohneinheit->id;
|
|
$oaid->address = $wohneinheit->hausnummer->getAddress();
|
|
$oaid->unit_string = (string)$wohneinheit;
|
|
//var_dump($oaid);exit;
|
|
if(!$oaid->save()) {
|
|
$this->log->error("[".$this->_ruid."] "."Fehler beim Speichern der OAID für Preorder " . $this->id);
|
|
return false;
|
|
}
|
|
if(!$this->save()) {
|
|
$oaid->assigned = 0;
|
|
$oaid->adb_wohneinheit_id = null;
|
|
$oaid->address = null;
|
|
$oaid->unit_string = null;
|
|
$oaid->save();
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
case "thetool":
|
|
// create new thethool OAID
|
|
$wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
if(!$wohneinheit->oaid) {
|
|
$wohneinheit->oaid = $wohneinheit->getNewOAID();
|
|
$wohneinheit->save();
|
|
}
|
|
if($wohneinheit->oaid) {
|
|
$this->oaid = $wohneinheit->oaid;
|
|
}
|
|
$this->save();
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private function getOaidFromWohneinheitIfOriginMatch() {
|
|
if($this->adb_wohneinheit_id) {
|
|
$unit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
if($unit && $unit->oaid && $this->oaid != $unit->oaid) {
|
|
$campaign = new Preordercampaign($this->preordercampaign_id);
|
|
$unit_oaid = OpenAccessIdModel::getFirst(["oaid" => $unit->oaid]);
|
|
if($unit_oaid && $unit_oaid->origin == $campaign->oaid_origin) {
|
|
return $unit->oaid;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function createRimoWorkorder() {
|
|
if(!$this->adb_wohneinheit_id) {
|
|
return false;
|
|
}
|
|
$wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
if(!$wohneinheit->id) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__.": Wohneinheit nicht gefunden (Preorder ".$this->id." ".$this->ucode." ".$this->oaid.")");
|
|
return false;
|
|
}
|
|
|
|
if(!$wohneinheit->extref) {
|
|
return false;
|
|
}
|
|
|
|
$campaign = new Preordercampaign($this->preordercampaign_id);
|
|
|
|
if($campaign->oaid_origin == "other") {
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": Kampagne unterstützt keine OAIDs");
|
|
return false;
|
|
}
|
|
|
|
if(!$this->oaid) {
|
|
$this->setOrCreateOaid();
|
|
if(!$this->oaid) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$oaid = OpenAccessIdModel::getFirstOaid($wohneinheit->oaid);
|
|
if(!$oaid) {
|
|
$this->log->warning("[".$this->_ruid."] ".__METHOD__.": OAID '".$wohneinheit->oaid."' not found");
|
|
return false;
|
|
}
|
|
// create and assign OAID if not yet done
|
|
$oaid->exportToRimoAndAssignFtu();
|
|
|
|
// create Workorder
|
|
$data = [];
|
|
$data["firstname"] = trim($this->firstname);
|
|
$data["lastname"] = trim($this->lastname);
|
|
$data["company"] = trim($this->company);
|
|
$data["phone"] = trim($this->phone);
|
|
$data["email"] = trim($this->email);
|
|
$data["orderId"] = $this->ucode;
|
|
|
|
$response = Rimoapi::createWorkorder($wohneinheit->extref, $data);
|
|
//var_dump($response);exit;
|
|
if(!$response) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__."Cannot create RimoWorkorder! Invalid Response! (Preorder ucode: ".$this->ucode.")");
|
|
return false;
|
|
}
|
|
|
|
$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" => $this->adb_wohneinheit_id,
|
|
"create_data" => json_encode($response)
|
|
]);
|
|
//var_dump($wo);exit;
|
|
$wo->save();
|
|
|
|
return $wo;
|
|
}
|
|
|
|
public function setNewStatusCode($new_status_code) {
|
|
if(!$new_status_code) return false;
|
|
|
|
$new_status = PreorderstatusModel::getFirst(["code" => $new_status_code]);
|
|
if(!$new_status) return false;
|
|
|
|
$status = $this->getProperty("status");
|
|
if($status->code < $new_status->code) {
|
|
$this->status_id = $new_status->id;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function setStatusFlag($code, $value = 1) {
|
|
if(!$code) return false;
|
|
|
|
$sflag = PreorderstatusflagModel::getFirst(["code" => $code]);
|
|
if(!$sflag) return false;
|
|
|
|
$sflag->preorder_id = $this->id;
|
|
$sflag->value->value = $value;
|
|
$sflag->value->save();
|
|
return true;
|
|
}
|
|
|
|
public function getNetowner() {
|
|
if(!$this->id) return false;
|
|
if(!$this->adb_hausnummer_id) return false;
|
|
$network = $this->getProperty("campaign")->network;
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": campaign: ".$this->getProperty("campaign")->name);
|
|
$this->log->debug("[".$this->_ruid."] ".__METHOD__.": network: ".$network->name);
|
|
if(!$network) return false;
|
|
|
|
return $network->owner;
|
|
}
|
|
|
|
public function getNetownerRimoApiCredentials() {
|
|
$netowner = $this->getNetowner();
|
|
if(!$netowner) return false;
|
|
|
|
foreach(TT_RIMO_API_CREDS as $api_creds) {
|
|
if($api_creds["address_id"] == $netowner->id) {
|
|
return $api_creds;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function createUcode() {
|
|
$ucode = $this->generateNewUcode();
|
|
while(PreorderModel::search(['ucode' => $ucode])) {
|
|
$ucode = $this->generateNewUcode();
|
|
}
|
|
$this->ucode = $ucode;
|
|
return $this->ucode;
|
|
}
|
|
|
|
|
|
private function generateNewUcode() {
|
|
$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
|
|
$charsLength = strlen($chars);
|
|
$ucode = '';
|
|
for($i = 0; $i < 8; $i++) {
|
|
$ucode .= $chars[rand(0, $charsLength - 1)];
|
|
}
|
|
return $ucode;
|
|
}
|
|
|
|
public function createCiftoken() {
|
|
$ciftoken = $this->generateNewCiftoken();
|
|
while(PreorderModel::getFirst(['ciftoken' => $ciftoken])) {
|
|
$ciftoken = $this->generateNewCiftoken();
|
|
}
|
|
$this->ciftoken = $ciftoken;
|
|
return $this->ciftoken;
|
|
}
|
|
|
|
private function generateNewCiftoken() {
|
|
$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxzy';
|
|
$charsLength = strlen($chars);
|
|
$ciftoken = '';
|
|
for($i = 0; $i < 16; $i++) {
|
|
$ciftoken .= $chars[rand(0, $charsLength - 1)];
|
|
}
|
|
return $ciftoken;
|
|
}
|
|
|
|
public function generateCifUrl() {
|
|
$campaign = $this->getProperty("campaign");
|
|
|
|
$cifurl = $campaign->cifurl;
|
|
$cifurl = str_replace("{{CIFTOKEN}}", $this->ciftoken, $cifurl);
|
|
|
|
return $cifurl;
|
|
}
|
|
|
|
public function generateCifCableUrl() {
|
|
$campaign = $this->getProperty("campaign");
|
|
|
|
$cifcableurl = $campaign->cifcableurl;
|
|
str_replace("{{CIFTOKEN}}", $this->ciftoken, $cifcableurl);
|
|
|
|
return $cifcableurl;
|
|
}
|
|
|
|
public function getCifdataApiArray() {
|
|
if(!$this->id) {
|
|
return false;
|
|
}
|
|
|
|
$hausnummer = $this->getProperty("adb_hausnummer");
|
|
$wohneinheit = $this->getProperty("adb_wohneinheit");
|
|
$campaign = $this->getProperty("campaign");
|
|
|
|
$a = [];
|
|
$a["ciftoken"] = $this->ciftoken;
|
|
$a["code"] = $this->ucode;
|
|
$a['oaid'] = $this->oaid;
|
|
|
|
$a['company'] = ($this->company) ? $this->company : null;
|
|
$a['uid'] = ($this->uid) ? $this->uid : null;
|
|
$a['firstnam'] = ($this->firstname) ? $this->firstname : null;
|
|
$a['lastname'] = ($this->lastname) ? $this->lastname : null;
|
|
|
|
$a['street'] = $hausnummer->strasse->name;
|
|
$a['housenumber'] = $hausnummer->hausnummer;
|
|
$a['zip'] = $hausnummer->plz->plz;
|
|
$a['city'] = $hausnummer->strasse->gemeinde->name;
|
|
$a['municipality'] = "";
|
|
$a['district'] = $hausnummer->ortschaft->name;
|
|
$a['block'] = ($wohneinheit->block) ? $wohneinheit->block : null;
|
|
$a['stock'] = ($wohneinheit->stock) ? $wohneinheit->stock : null;
|
|
$a['stiege'] = ($wohneinheit->stiege) ? $wohneinheit->stiege : null;
|
|
$a['tuer'] = ($wohneinheit->tuer) ? $wohneinheit->tuer : null;
|
|
|
|
if($campaign->district_is_city) {
|
|
$a['city'] = $hausnummer->ortschaft->name;
|
|
$a['municipality'] = $hausnummer->strasse->gemeinde->name;
|
|
} else {
|
|
unset($a['municipality']);
|
|
}
|
|
|
|
return $a;
|
|
}
|
|
|
|
public function getBorderpointImageFile() {
|
|
if(!$this->adb_hausnummer_id) return false;
|
|
|
|
$border_lat = $this->getProperty("adb_hausnummer")->borderpoint_lat;
|
|
$border_long = $this->getProperty("adb_hausnummer")->borderpoint_long;
|
|
|
|
if(!$border_lat || !$border_long ) {
|
|
return false;
|
|
}
|
|
|
|
$filename = "borderpoint_map_h".$this->adb_hausnummer_id."_{$border_lat}_{$border_long}";
|
|
|
|
$bpi_file = PreorderFile::getFirst(["preorder_id" => $this->id, "filename" => $filename]);
|
|
if($bpi_file) {
|
|
return $bpi_file;
|
|
}
|
|
|
|
|
|
// get new Borderpoint Image from Mapbox API
|
|
$params = [
|
|
"gps_lat" => $border_lat,
|
|
"gps_long" => $border_long,
|
|
"zoom" => 19,
|
|
"size_x" => 640,
|
|
"size_y" => 640,
|
|
"style" => "satellite-streets-v12",
|
|
"pin" => [
|
|
"gps_lat" => $border_lat,
|
|
"gps_long" => $border_long,
|
|
"size" => "l",
|
|
"color" => "ee9900",
|
|
"icon" => "embassy"
|
|
],
|
|
"access_token" => TT_MAPBOX_TILE_API_TOKEN
|
|
];
|
|
|
|
$image_content = Mapbox_StaticImageApi::getImageFileContent($params);
|
|
if(!$image_content) {
|
|
return false;
|
|
}
|
|
|
|
$fs_filename = "$filename.jpg";
|
|
if(!file_put_contents(MFUPLOAD_FILE_SAVE_PATH."/preorder-borderpoint/$fs_filename", $image_content)) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__.": Error saving Borderpoint Static Map Image File");
|
|
return false;
|
|
}
|
|
|
|
|
|
$file = FileModel::create([
|
|
"name" => "borderpoint_map",
|
|
"description" => $this->adb_hausnummer_id,
|
|
"filename" => "$filename.jpg",
|
|
"orig_filename" => "$filename.jpg",
|
|
"store_filename" => $fs_filename,
|
|
"subfolder" => "preorder-borderpoint",
|
|
]);
|
|
if(!$file->save()) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__.": Error saving File Object");
|
|
return false;
|
|
}
|
|
|
|
$file->mimetype = $file->getMimetype();
|
|
$file->save();
|
|
|
|
$pf = PreorderFile::create([
|
|
"preorder_id" => $this->id,
|
|
"file_id" => $file->id,
|
|
"filename" => $filename
|
|
]);
|
|
|
|
if(!$pf->save()) {
|
|
$this->log->error("[".$this->_ruid."] ".__METHOD__.": Error saving PreorderFile Object");
|
|
return false;
|
|
}
|
|
|
|
return $pf;
|
|
|
|
|
|
}
|
|
|
|
public function getApiArray($options = []) {
|
|
if(!$this->id) {
|
|
return false;
|
|
}
|
|
|
|
$include_full_home = false;
|
|
$include_full_address = false;
|
|
|
|
if(in_array("full_home", $options)) {
|
|
$include_full_home = true;
|
|
}
|
|
if(in_array("full_address", $options)) {
|
|
$include_full_address = true;
|
|
}
|
|
|
|
$hausnummer = $this->getProperty("adb_hausnummer");
|
|
$wohneinheit = $this->getProperty("adb_wohneinheit");
|
|
$campaign = $this->getProperty("campaign");
|
|
$status = $this->getProperty("status")->getApiArray();
|
|
|
|
foreach($this->getProperty("statusflags") as $sflag) {
|
|
$status["flags"][] = [
|
|
"code" => (int)$sflag->code,
|
|
"text" => $sflag->name,
|
|
"value" => ($sflag->value->value == 1)
|
|
];
|
|
}
|
|
//var_dump($status);exit;
|
|
|
|
$a = [];
|
|
$a['code'] = strtoupper($this->ucode);
|
|
$a['oaid'] = $this->oaid;
|
|
$a['extref'] = $this->extref;
|
|
$a['orderDate'] = ($this->order_date) ? date("Y-m-d", $this->order_date) : null;
|
|
$a['status'] = $status;
|
|
$a['ciftoken'] = ($this->ciftoken) ? $this->ciftoken : null;
|
|
$a['cifurl'] = ($this->cifurl) ? $this->cifurl : null;
|
|
$a['cifcableurl'] = ($this->cifcableurl) ? $this->cifcableurl : null;
|
|
$a['installationDate'] = ($this->installation_date) ? date("c", $this->installation_date) : null;
|
|
$a['connectionType'] = $this->connection_type;
|
|
$a['connectionCount'] = ($this->connection_count) ? (int)$this->connection_count : 1;
|
|
$a['isAdditionalOrder'] = ($this->is_additional_order) ? true : false;
|
|
$a['technology'] = ($this->technology) ? $this->technology : null;
|
|
$a['equipment_name'] = ($wohneinheit->getPatchEqString()) ? $wohneinheit->getPatchEqString() : null;
|
|
$a['equipment_port'] = ($wohneinheit->patch_port) ? $wohneinheit->patch_port : null;
|
|
$a['preorderType'] = $this->type;
|
|
$a['acceptMarketing'] = ($this->accept_marketing) ? true : false;
|
|
$a['acceptAgb'] = ($this->accept_agb) ? true : false;
|
|
$a['acceptDsgvo'] = ($this->accept_dsgvo) ? true : false;
|
|
$a['acceptWithdrawal'] = ($this->accept_withdrawal) ? true : false;
|
|
$a['acceptDigging'] = ($this->accept_digging) ? true : false;
|
|
$a['address_info'] = ($this->address_info) ? $this->address_info : null;
|
|
|
|
$address = [];
|
|
$address['cluster_id'] = $hausnummer->netzgebiet->extref;
|
|
$address['street'] = $hausnummer->strasse->name;
|
|
$address['housenumber'] = $hausnummer->hausnummer;
|
|
$address['stiege'] = $hausnummer->stiege;
|
|
$address['zip'] = $hausnummer->plz->plz;
|
|
$address['city'] = $hausnummer->strasse->gemeinde->name;
|
|
$address['municipality'] = "";
|
|
$address['district'] = $hausnummer->ortschaft->name;
|
|
|
|
if($campaign->district_is_city) {
|
|
$address['city'] = $hausnummer->ortschaft->name;
|
|
$address['municipality'] = $hausnummer->strasse->gemeinde->name;
|
|
} else {
|
|
unset($address['municipality']);
|
|
}
|
|
|
|
if($include_full_address) {
|
|
$address['gps_lat'] = $hausnummer->gps_lat;
|
|
$address['gps_long'] = $hausnummer->gps_long;
|
|
$address["borderpoint_lat"] = $hausnummer->borderpoint_lat;
|
|
$address["borderpoint_long"] = $hausnummer->borderpoint_long;
|
|
|
|
$address["trenches"] = [];
|
|
$address["home_trench"] = [];
|
|
if($hausnummer->trenches) {
|
|
$address["trenches"] = json_decode($hausnummer->trenches);
|
|
}
|
|
if($hausnummer->home_trench) {
|
|
$address["home_trench"] = json_decode($hausnummer->home_trench);
|
|
}
|
|
}
|
|
|
|
if(!$include_full_home) {
|
|
$address['block'] = ($wohneinheit->block) ? $wohneinheit->block : null;
|
|
$address['stock'] = ($wohneinheit->stock) ? $wohneinheit->stock : null;
|
|
$address['tuer'] = ($wohneinheit->tuer) ? $wohneinheit->tuer : null;
|
|
$address['unit_string'] = ($wohneinheit->bezeichner) ? $wohneinheit->bezeichner : null;
|
|
} else {
|
|
$home = [];
|
|
|
|
$home_status = $wohneinheit->status->getApiArray();
|
|
foreach(ADBWohneinheitStatusflagValueModel::search(["hausnummer_id" => $hausnummer->id]) as $sflag) {
|
|
$home_status["flags"][] = [
|
|
"code" => (int)$sflag->flag->code,
|
|
"text" => $sflag->flag->name,
|
|
"value" => ($sflag->value == 1)
|
|
];
|
|
}
|
|
|
|
$home["id"] = (int)$wohneinheit->id;
|
|
$home["status"] = $home_status;
|
|
$home['block'] = ($wohneinheit->block) ? $wohneinheit->block : null;
|
|
$home['stock'] = ($wohneinheit->stock) ? $wohneinheit->stock : null;
|
|
$home['tuer'] = ($wohneinheit->tuer) ? $wohneinheit->tuer : null;
|
|
$home['unit_string'] = ($wohneinheit->bezeichner) ? $wohneinheit->bezeichner : null;
|
|
$home["rimo_external_id"] = $wohneinheit->extref;
|
|
$home["nutzung"] = $wohneinheit->nutzung;
|
|
$home["rimo_executionstate"] = $wohneinheit->rimo_ex_state;
|
|
$home["rimo_operationalstate"] = $wohneinheit->rimo_op_state;
|
|
|
|
$home['created'] = date("c", $wohneinheit->create);
|
|
$home['created_ts'] = (int)$wohneinheit->create;
|
|
$home['updated'] = date("c", $wohneinheit->edit);
|
|
$home['updated_ts'] = (int)$wohneinheit->edit;
|
|
|
|
$address['address_id'] = (int)$hausnummer->id;
|
|
}
|
|
|
|
$address['is_shipping'] = ($this->shipping_address == "address") ? true : false;
|
|
|
|
$customer = [];
|
|
$customer['company'] = ($this->company) ? $this->company : null;
|
|
$customer['uid'] = ($this->uid) ? $this->uid : null;
|
|
$customer['firstnam'] = ($this->firstname) ? $this->firstname : null;
|
|
$customer['lastname'] = ($this->lastname) ? $this->lastname : null;
|
|
$customer['street'] = ($this->street) ? $this->street : null;
|
|
$customer['stiege'] = ($this->stiege) ? $this->stiege : null;
|
|
$customer['housenumber'] = ($this->housenumber) ? $this->housenumber : null;
|
|
$customer['zip'] = ($this->zip) ? $this->zip : null;
|
|
$customer['city'] = ($this->city) ? $this->city : null;
|
|
$customer['block'] = ($this->block) ? $this->block : null;
|
|
$customer['stock'] = ($this->stock) ? $this->stock : null;
|
|
$customer['tuer'] = ($this->tuer) ? $this->tuer : null;
|
|
$customer['unit_string'] = ($this->unit_string) ? $this->unit_string : null;
|
|
$customer['phone'] = ($this->phone) ? $this->phone : null;
|
|
$customer['email'] = ($this->email) ? $this->email : null;
|
|
|
|
$a['address'] = $address;
|
|
if($include_full_home) {
|
|
$a["home"] = $home;
|
|
}
|
|
$a['customer'] = $customer;
|
|
|
|
$a['addonServices'] = null;
|
|
if($this->addon_services) {
|
|
$addon_services = json_decode($this->addon_services);
|
|
if(json_last_error() === JSON_ERROR_NONE) {
|
|
$a['addonServices'] = $addon_services;
|
|
}
|
|
}
|
|
|
|
$a['additionalData'] = null;
|
|
if($this->addon_data) {
|
|
$addon_data = json_decode($this->addon_data);
|
|
if(json_last_error() === JSON_ERROR_NONE) {
|
|
$a['additionalData'] = $addon_data;
|
|
}
|
|
}
|
|
|
|
$a['created'] = date("c", $this->create);
|
|
$a['created_ts'] = (int)$this->create;
|
|
$a['updated'] = date("c", $this->edit);
|
|
$a['updated_ts'] = (int)$this->edit;
|
|
|
|
return $a;
|
|
}
|
|
|
|
|
|
public function getProperty($name) {
|
|
if($this->$name == null) {
|
|
|
|
if($name == "campaign") {
|
|
$this->campaign = new Preordercampaign($this->preordercampaign_id);
|
|
return $this->campaign;
|
|
}
|
|
|
|
if($name == "status") {
|
|
$this->status = mfValuecache::singleton()->get("mfObjectmodel-Preorderstatus-" . $this->status_id);
|
|
if(!$this->status) {
|
|
$this->status = new Preorderstatus($this->status_id);
|
|
if($this->status->id) {
|
|
mfValuecache::singleton()->set("mfObjectmodel-Preorderstatus-" . $this->status_id, $this->status);
|
|
}
|
|
}
|
|
return $this->status;
|
|
}
|
|
|
|
if($name == "statusflags") {
|
|
$flags = [];
|
|
foreach(PreorderStatusflagModel::getAll() as $flag) {
|
|
$flag->preorder_id = $this->id;
|
|
$flags[$flag->id] = $flag;
|
|
}
|
|
if(count($flags)) {
|
|
$this->statusflags = $flags;
|
|
}
|
|
return $this->statusflags;
|
|
}
|
|
|
|
if($name == "partner") {
|
|
$this->partner = mfValuecache::singleton()->get("mfObjectmodel-Address-" . $this->partner_id);
|
|
if(!$this->partner) {
|
|
$this->partner = new Address($this->partner_id);
|
|
if($this->partner->id) {
|
|
mfValuecache::singleton()->set("mfObjectmodel-Address-" . $this->partner_id, $this->partner);
|
|
}
|
|
}
|
|
return $this->partner;
|
|
}
|
|
|
|
if($name == "discounts") {
|
|
$discounts = PreorderDiscountModel::search(["preorder_id" => $this->id]);
|
|
if($discounts) {
|
|
$this->discounts = $discounts;
|
|
}
|
|
return $this->discounts;
|
|
}
|
|
|
|
if($name == "applied_discounts") {
|
|
$discounts = PreorderDiscountModel::search(["preorder_id" => $this->id, "assigned" => true]);
|
|
if($discounts) {
|
|
$this->discounts = $discounts;
|
|
}
|
|
return $this->discounts;
|
|
}
|
|
|
|
if($name == "building") {
|
|
$this->building = mfValuecache::singleton()->get("mfObjectmodel-Building-" . $this->building_id);
|
|
if(!$this->building) {
|
|
$this->building = new Building($this->building_id);
|
|
if($this->building->id) {
|
|
mfValuecache::singleton()->set("mfObjectmodel-Building-" . $this->building_id, $this->building);
|
|
}
|
|
}
|
|
return $this->building;
|
|
}
|
|
|
|
if($name == "adb_hausnummer") {
|
|
$this->adb_hausnummer = new ADBHausnummer($this->adb_hausnummer_id);
|
|
return $this->adb_hausnummer;
|
|
}
|
|
|
|
if($name == "adb_wohneinheit") {
|
|
$this->adb_wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id);
|
|
return $this->adb_wohneinheit;
|
|
}
|
|
|
|
if($name == "services") {
|
|
if(!$this->addon_services) {
|
|
return null;
|
|
}
|
|
$addon_services = json_decode($this->addon_services);
|
|
if(json_last_error() === JSON_ERROR_NONE) {
|
|
$this->services = $addon_services;
|
|
return $this->services;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
if($name == "ordered_services") {
|
|
if(!$this->addon_services) {
|
|
return null;
|
|
}
|
|
$ordered_services = [];
|
|
$addon_services = json_decode($this->addon_services);
|
|
if(json_last_error() === JSON_ERROR_NONE) {
|
|
foreach($addon_services as $s) {
|
|
if($s->ordered) {
|
|
$ordered_services[] = $s;
|
|
}
|
|
}
|
|
|
|
$this->ordered_services = $ordered_services;
|
|
return $this->ordered_services;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
if($name == "logistics") {
|
|
$logistics = PreorderlogisticsModel::getFirst(["preorder_id" => $this->id]);
|
|
$this->logistics = $logistics;
|
|
return $this->logistics;
|
|
}
|
|
|
|
if($name == "history") {
|
|
$history = PreorderHistoryModel::search(["preorder_id" => $this->id]);
|
|
$this->history = $history;
|
|
|
|
return $this->history;
|
|
|
|
}
|
|
|
|
if($name == "attribute") {
|
|
if(!$this->attributes) {
|
|
return null;
|
|
}
|
|
$this->attribute = json_decode($this->attributes, true);
|
|
return $this->attribute;
|
|
}
|
|
|
|
if($name == "statusjournals") {
|
|
$journals = PreorderstatusJournal::search(["preorder_id" => $this->id]);
|
|
if($journals) {
|
|
$this->statusjournals = $journals;
|
|
return $this->statusjournals;
|
|
}
|
|
return [];
|
|
|
|
}
|
|
|
|
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 === 'fcp') {
|
|
if(!$this->adb_hausnummer || !$this->adb_hausnummer->fcp_id) return null;
|
|
return ADBRimoFcp::get($this->adb_hausnummer->fcp_id);
|
|
}
|
|
|
|
if($name == "citycomoan") {
|
|
$ccoan = PreorderCitycomOan::getFirst(["preorder_id" => $this->id]);
|
|
if(!$ccoan) return null;
|
|
$this->citycomoan = $ccoan;
|
|
return $this->citycomoan;
|
|
}
|
|
|
|
if($name == "ctags") {
|
|
$ctags = PreorderCtag::searchActive(["preorder_id" => $this->id]);
|
|
if(!$ctags) return null;
|
|
$this->ctags = $ctags;
|
|
return $this->ctags;
|
|
}
|
|
|
|
if($name == "editor") {
|
|
$this->editor = new User($this->edit_by);
|
|
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;
|
|
}
|
|
|
|
public function __clone() {
|
|
$me = new User;
|
|
$me->loadMe();
|
|
|
|
$old_id = $this->id;
|
|
//$old_ucode = $this->ucode;
|
|
$this->id = null;
|
|
$this->_ruid = bin2hex(random_bytes(16));
|
|
|
|
$this->data = clone($this->data);
|
|
$this->_old_data = new StdClass();
|
|
|
|
$this->status = null;
|
|
$this->campaign = null;
|
|
$this->partner = null;
|
|
$this->building = null;
|
|
$this->adb_hausnummer = null;
|
|
$this->adb_wohneinheit = null;
|
|
|
|
// cleanup Preorder data
|
|
$this->create_by = $me->id;
|
|
$this->edit_by = $me->id;
|
|
|
|
$this->create = null;
|
|
$this->edit = null;
|
|
$this->saved = 0;
|
|
$this->mode = "new";
|
|
|
|
//$this->save();
|
|
$this->log->debug("Cloned Preorder $old_id");
|
|
}
|
|
} |