in_after_save) return true; $this->in_after_save++; $this->generateFibuAccountNumber(); $this->syncToFibuMerge(); $this->in_after_save--; } private function generateFibuAccountNumber() { if($this->fibu_account_number || !$this->customer_number) { return true; } $fibu_account_number = Address::getNextFibuAccountNumber(); if(!$fibu_account_number) { return false; } $this->fibu_account_number = $fibu_account_number; $this->fibu_primary_account = 1; $this->save(); } public function getNewCustomerNumber() { $last_num = AddressModel::getLastCustomerNumber(); $this->log->debug("last_num: $last_num"); if($last_num) { $new_num = $last_num + 1; } else { $new_num = TT_FIRST_CUSTNUM; } if(!AddressModel::search(['customer_number' => $new_num])) { $this->customer_number = $new_num; return $this->customer_number; } return false; } private function syncToFibuMerge() { if(!$this->customer_number) return true; //var_dump($this);exit; $me = new User(); $me->loadMe(); if($this->fibu_account_number) { $old_custnum = $this->customer_number; if($old_custnum > 900000) { $old_custnum -= 900000; } $name_search = []; if($this->company) $name_search[] = $this->company; if($this->lastname) $name_search[] = $this->lastname; $fibumerge = XinonFibuMergeModel::getFirst(["old_custnum" => $old_custnum, "name" => $name_search]); if(!$fibumerge) { // create fibu merge $fibumerge = XinonFibuMergeModel::create([ "source" => "t", "old_custnum" => $old_custnum, "new_custnum" => $this->fibu_account_number, "name" => ($this->company) ? $this->company : $this->lastname, "vorname" => ($this->company) ? "" : $this->firstname, "strasse" => $this->street, "plz" => $this->zip, "ort" => $this->city, "create_by" => $me->id, "edit_by" => $me->id ]); $fibumerge->save(); return true; } if($fibumerge->new_custnum != $this->fibu_account_number) { $fibumerge->new_custnum = $this->fibu_account_number; $fibumerge->save(); } } elseif($this->_old_data->fibu_account_number) { // fibu account number was removed => remove fibumerge entry $fibumerge = XinonFibuMergeModel::getFirst(["old_custnum" => $this->customer_number, "new_custnum" => $this->_old_data->fibu_account_number]); //var_dump($fibumerge);exit; if(!$fibumerge) return true; $fibumerge->delete(); } return true; } 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 static function getNextFibuAccountNumber() { $db = FronkDB::singleton(); $res = $db->select("Address","fibu_account_number", "fibu_account_number > 0 ORDER BY fibu_account_number DESC LIMIT 1"); if(!$db->num_rows($res)) { return 230001; } $data = $db->fetch_object($res); $last_num = $data->fibu_account_number; if($last_num) { $new_num = $last_num + 1; } else { $new_num = 230001; } return $new_num; } public static function getNextSupplierNumber() { $db = FronkDB::singleton(); $res = $db->select("Address","fibu_supplier_number", "fibu_supplier_number > 0 ORDER BY fibu_supplier_number DESC LIMIT 1"); if(!$db->num_rows($res)) { return TT_FIRST_SUPPLIER_NUM; } $data = $db->fetch_object($res); $last_num = $data->fibu_supplier_number; if($last_num) { $new_num = $last_num + 1; } else { $new_num = TT_FIRST_SUPPLIER_NUM; } return $new_num; } 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; } }