Files
thetool/application/Address/Address.php
Frank Schubert aa373b5f4e Contract WIP
2023-02-23 13:06:04 +01:00

251 lines
6.6 KiB
PHP

<?php
class Address extends mfBaseModel {
protected $forcestr = ['street','company','zip','phone','fax','mobile','note'];
private $parent;
private $childaddresses;
private $links = [];
private $linked_as = [];
private $types;
private $attributes;
private $permissions;
private $contracts;
private $active_contracts;
private $phoneparts;
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 splitPhoneNumber() {
if(!$this->phone) {
return false;
}
if($this->phoneparts) {
return $this->phoneparts;
}
$phone = preg_replace('/[^0-9]\+/', '', $this->phone);
$cc = "";
$num = "";
if(substr($phone, 0, 1) === '+') {
$cc = substr($phone, 1, 2);
$num = substr($phone, 3);
} elseif(substr($phone, 0, 2) === '00') {
$cc = substr($phone, 2, 2);
$num = substr($phone, 4);
} elseif(substr($phone, 0, 1) === '0') {
$cc = 43;
$num = substr($phone, 1);
} else {
$cc = 43;
$num = $phone;
}
$this->phoneparts = [$cc,$num];
return $this->phoneparts;
}
public function getCompanyOrName($returnParent = false) {
if($returnParent && $this->parent_id) {
return $this->getProperty("parent")->getCompanyOrName(true);
}
if($this->company) {
return $this->company;
}
return $this->getFullName();
}
public function getUserIds($childs = true) {
$userIds = [];
foreach(UserModel::search(['address_id' => $this->id]) as $user) {
$userIds[] = $user->id;
}
if($childs) {
foreach(AddressModel::search(['parent_id' => $this->id]) as $child) {
foreach(UserModel::search(['address_id' => $child->id]) as $user) {
$userIds[] = $user->id;
}
}
}
$userIds = array_unique($userIds);
return $userIds;
}
public function loadAddresstypes() {
if(!$this->id) {
return false;
}
$all_types = AddresstypeModel::search(['address_id' => $this->id], true);
if($this->parent_id) {
// get types from parent
$parent_types = $this->getProperty("parent")->getProperty("types");
if($parent_types) {
$all_types = array_merge($all_types, $parent_types);
}
}
$this->types = $all_types;
return true;
}
public function getChildrenByType($type) {
if(!$this->id || !$type) {
return [];
}
$children = AddressModel::search(['addresstype' => [$type], "parent_id" => $this->id]);
return $children;
}
public function generateServicePin() {
if(!$this->customer_number) {
return false;
}
$num1 = rand(65, 90);
$num2 = rand(65, 90);
$c1 = chr($num1);
$c2 = chr($num2);
$spin = $c1.$c2.$this->customer_number;
return $spin;
}
/*public function deleteLinks() {
$links = $this->getProperty("links");
//var_dump($links);exit;
if(is_array($links) && count($links)) {
foreach($links as $type => $linktypes) {
//var_dump($type, $linktypes);exit;
foreach($linktypes as $link) {
//var_dump($link);exit;
$link->delete();
}
}
}
}*/
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "types") {
$this->loadAddresstypes();
return $this->types;
}
if($name == "attributes") {
$attribs = AddressattributeModel::search(['address_id' => $this->id]);
if(count($attribs)) {
$this->attributes = [];
foreach($attribs as $a) {
$this->attributes[$a->name] = $a;
}
}
return $this->attributes;
}
if($name == "permissions") {
$this->permissions = NetworkAddressModel::search(['address_id' => $this->id]);
return $permissions;
}
if($name == "parent") {
$this->parent = new Address($this->parent_id);
return $this->parent;
}
if($name == "childaddresses") {
$this->childaddresses = AddressModel::search(['parent' => $this->id]);
return $this->childaddresses;
}
if($name == "contracts") {
$owning = ContractModel::search(['owner_id' => $this->id]);
$billing = ContractModel::search(['billingaddress_id' => $this->id]);
$this->contracts = array_merge($owning, $billing);
return $this->contracts;
}
if($name == "active_contracts") {
$owning = ContractModel::searchActive(['owner_id' => $this->id]);
$billing = ContractModel::searchActive(['billingaddress_id' => $this->id]);
$this->contracts = array_merge($owning, $billing);
return $this->contracts;
}
/*
if($name == "links_to") {
$links = AddressLinkModel::search(['address_id' => $this->id]);
foreach($links as $link) {
if(!array_key_exists($link->type, $this->links_to)) {
$this->links_to[$link->type] = [];
}
$this->links_to[$link->type][] = $link->linked_to;
}
return $this->links_to;
}*/
if($name == "links") {
$links = AddressLinkModel::search(['origin_address_id' => $this->id]);
//var_dump($links);exit;
foreach($links as $link) {
if(!array_key_exists($link->type, $this->links)) {
$this->links[$link->type] = [];
}
$this->links[$link->type][] = $link;
//var_dump($this->links);exit;
}
return $this->links;
}
if($name == "linked_as") {
$linked_as = AddressLinkModel::search(['address_id' => $this->id]);
foreach($linked_as as $link) {
if(!array_key_exists($link->type, $this->linked_as)) {
$this->linked_as[$link->type] = [];
}
$this->linked_as[$link->type][] = $link;
//var_dump($this->links);exit;
}
return $this->linked_as;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
if($this->$name->id) {
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}