uid === "string") { $this->uid = ""; } } public function afterSave() { // 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; // prevent potential infinite loop if($this->in_after_save) return true; $this->in_after_save++; // 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->save(); } // 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(); $this->in_after_save--; } public function runStatusTrigger() { if(!$this->id) return true; if($this->status_id == $this->_old_data->status_id) return true; $this->log->debug(__METHOD__." running"); $new_status = $this->getProperty("status"); $old_status = new Preorderstatus($this->_old_data->status_id); if(!$new_status->id || !$old_status->id) return true; $this->log->debug(__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("config TT_PREORDER_STATUS_MATRIX not defined!"); } // 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(__METHOD__.": Looking for $classname in $filepath"); if(!file_exists($filepath)) { $this->log->debug(__METHOD__.": $filepath not found"); continue; } require_once $filepath; if(!class_exists($classname)) { $this->log->debug(__METHOD__.": $classname not found"); continue; } $trigger = new $classname($this, $new_status); $trigger->run(); } return true; } /* * Cascade status changes down to adb_hausnummer and adb_wohneinheit */ public function cascadeStatus() { /* * 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(__METHOD__.": TT_PREORDER_STATUS_MATRIX undefined! Cannot cascade Preorder status to Hausnummer and Wohneinheit"); return true; } $new_status = new Preorderstatus($this->status_id); if(!$new_status->id) { $this->log->warning(__METHOD__.": Preorder has invalid status!"); 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(__METHOD__.": new ADBStatus code ".$cascade["h"]." not found!"); return true; } $hausnummer = new ADBHausnummer($this->adb_hausnummer_id); if(!$hausnummer->id) { $this->log->warning(__METHOD__.": hausnummer ".$this->adb_hausnummer_id." not found!"); return true; } $this->log->debug(__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(__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(__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(__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(__METHOD__.": Setting new wohneinheit status code: ".$new_wohneinheit_status->code); $wohneinheit->status_id = $new_wohneinheit_status->id; $wohneinheit->save(); } } return true; } private 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]) 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("Preorder->status->code". print_r($preorder->status, true)); $this->log->debug("Status->code". print_r($status->code, true)); }*/ if($preorder->status->code < $status->code && $status->code <= 244) { $preorder->status_id = $this->status_id; $preorder->save(); } } } return true; } 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) { return false; } $wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); 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("OAID of Preorder ".$this->id." should be thetool, but is OFAA"); } } elseif($campaign->oaid_origin == "ofaa") { $current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid); //var_dump($current_oaid);exit; if(!$current_oaid) { $this->log->error("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(); } return true; } } } } if($this->type == "interest") return true; if(!$this->id) { $this->log->error(__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(__METHOD__.": Cannot create OAID: Invalid campaign Network"); return false; } $netowner = new Address($campaign->network->owner_id); if(!$netowner->id) { $this->log->warning(__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("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(__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(__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("Fehler beim Speichern der OAID für Preorder ".$this->id); $this->redirect("Preordercampaign", "Admin"); } 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 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; str_replace("{{CIFTOKEN}}", $this->ciftoken, $cifurl); return $cifurl; } public function generateCifCableUrl() { $campaign = $this->getProperty("campaign"); $cifcableurl = $campaign->cifcableurl; str_replace("{{}}", $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 getApiArray() { if(!$this->id) { return false; } $hausnummer = $this->getProperty("adb_hausnummer"); $wohneinheit = $this->getProperty("adb_wohneinheit"); $campaign = $this->getProperty("campaign"); $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'] = $this->getProperty("status")->getApiArray(); $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['zip'] = $hausnummer->plz->plz; $address['city'] = $hausnummer->strasse->gemeinde->name; $address['municipality'] = ""; $address['district'] = $hausnummer->ortschaft->name; $address['block'] = ($wohneinheit->block) ? $wohneinheit->block : null; $address['stock'] = ($wohneinheit->stock) ? $wohneinheit->stock : null; $address['stiege'] = ($wohneinheit->stiege) ? $wohneinheit->stiege : null; $address['tuer'] = ($wohneinheit->tuer) ? $wohneinheit->tuer : null; $address['unit_string'] = ($wohneinheit->bezeichner) ? $wohneinheit->bezeichner : null; $address['is_shipping'] = ($this->shipping_address == "address") ? true : false; if($campaign->district_is_city) { $address['city'] = $hausnummer->ortschaft->name; $address['municipality'] = $hausnummer->strasse->gemeinde->name; } else { unset($address['municipality']); } $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['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['stiege'] = ($this->stiege) ? $this->stiege : 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; $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 == "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 == "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 == "attribute") { if(!$this->attributes) { return null; } $this->attribute = json_decode($this->attributes, true); return $this->attribute; } if($name == "creator") { $user = mfValuecache::singleton()->get("Worker-id-".$this->create_by); if($user) { $this->creator = $user; return $this->creator; } $this->creator = new User($this->create_by); if($this->creator->id) { mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator); } return $this->creator; } if($name == "editor") { $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->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; // 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"); } }