- OAIDs are now auto assigned to preorders/wohneinheiten on save - OAIDs can be exported to rimo via Preorder Admin functions - Preorder admin function createWorkorder automatically creates, exports and assigns OAIDs
428 lines
14 KiB
PHP
428 lines
14 KiB
PHP
<?php
|
|
|
|
class Preorder extends mfBaseModel {
|
|
protected $forcestr = ['street','company','zip','phone','email','note'];
|
|
private $status;
|
|
private $campaign;
|
|
private $partner;
|
|
private $building;
|
|
private $adb_hausnummer;
|
|
private $adb_wohneinheit;
|
|
private $attribute = [];
|
|
|
|
|
|
public function afterLoad() {
|
|
if($this->uid === "string") {
|
|
$this->uid = "";
|
|
}
|
|
}
|
|
|
|
public function afterSave() {
|
|
// 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->getOaidFromWohneinheitIfOriginMatch();
|
|
$this->setOrCreateOaid();
|
|
if($this->oaid != $old_oaid) {
|
|
$this->save();
|
|
}
|
|
}
|
|
|
|
public function setOrCreateOaid($oaid_attributes = false) {
|
|
$campaign = new Preordercampaign($this->preordercampaign_id);
|
|
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
|
|
$current_oaid = OpenAccessIdModel::getFirstOaid($this->oaid);
|
|
if(!$current_oaid) {
|
|
return true; // this is a thetool oaid, so do nothing
|
|
}
|
|
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->layout->setFlash("Keine weiteren OAIDs verfügbar", "error");
|
|
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->layout()->setFlash("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 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['status'] = $this->getProperty("status")->getApiArray();
|
|
$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['patchposition'] = ($this->patchposition) ? $this->patchposition : 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 == "addon_services") {
|
|
|
|
}
|
|
|
|
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");
|
|
}
|
|
} |