Files
thetool/application/Preorder/Preorder.php
2023-11-09 17:04:07 +01:00

631 lines
21 KiB
PHP

<?php
class Preorder extends mfBaseModel {
protected $forcestr = ['street','company','zip','phone','email','note'];
private $in_after_save = false;
private $status;
private $campaign;
private $partner;
private $building;
private $adb_hausnummer;
private $adb_wohneinheit;
private $services;
private $ordered_services;
private $creator;
private $editor;
private $attribute = [];
public function afterLoad() {
if($this->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 = true;
// 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();
}
// 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();
}
/*
* 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::search(["deleted" => 0, "adb_hausnummer_id" => $this->adb_hausnummer_id, "<status_code" => $status->code]) as $preorder) {
if($preorder->id == $this->id) continue;
/*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 ".$preorder->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->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 createCifCode() {
$cifcode = $this->generateNewCifcode();
while(PreorderModel::search(['cifcode' => $cifcode])) {
$cifcode = $this->generateNewCifcode();
}
$this->cifcode = $cifcode;
return $this->cifcode;
}
private function generateNewCifcode() {
$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxzy';
$charsLength = strlen($chars);
$cifcode = '';
for ($i = 0; $i < 16; $i++) {
$cifcode .= $chars[rand(0, $charsLength - 1)];
}
return $cifcode;
}
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['cifcode'] = $this->cifcode;
$a['cifurl'] = $this->cifurl;
$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['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'] = ($this->equipment_name) ? $this->equipment_name: null;
$a['equipment_port'] = ($this->equipment_port) ? $this->equipment_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 == "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");
}
}