633 lines
19 KiB
PHP
633 lines
19 KiB
PHP
<?php
|
|
|
|
class Address extends mfBaseModel {
|
|
protected $forcestr = ['street','company','zip','phone','fax','mobile','note'];
|
|
private $in_after_save = 0;
|
|
|
|
private $country;
|
|
private $parent;
|
|
private $childaddresses;
|
|
private $links = [];
|
|
private $linked_as = [];
|
|
private $types;
|
|
private $attributes;
|
|
private $permissions;
|
|
private $contracts;
|
|
private $active_contracts;
|
|
|
|
private $phoneparts;
|
|
|
|
protected function afterSave() {
|
|
// prevent potential infinite loop
|
|
if($this->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;
|
|
|
|
$country = new Country($this->country_id);
|
|
if(!$country) {
|
|
return false;
|
|
}
|
|
|
|
$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,
|
|
"land" => $country->isocode,
|
|
"create_by" => 1,
|
|
"edit_by" => 1
|
|
]);
|
|
$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 static function runBmdExport($request_type) {
|
|
$last_export = 0;
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$export_ts = new mfConfig("bmd.export.ts");
|
|
if($export_ts->value()) {
|
|
$last_export = $export_ts->value();
|
|
}
|
|
|
|
if(!file_exists(TT_ADDRESS_BMD_EXPORT_PATH)) {
|
|
return 10;
|
|
}
|
|
$filepath = TT_ADDRESS_BMD_EXPORT_PATH."/".TT_ADDRESS_BMD_EXPORT_FILENAME;
|
|
|
|
$type = "inc";
|
|
if($request_type == "full") {
|
|
$last_export = 0;
|
|
$type = "full";
|
|
}
|
|
|
|
// delete export file if export if full
|
|
if($last_export == 0 && file_exists($filepath)) {
|
|
unlink($filepath);
|
|
}
|
|
|
|
$export_ts->value(date('U'));
|
|
|
|
$search = ["edit>" => $last_export, "customer_or_fibu_numbers" => true];
|
|
|
|
if(!AddressModel::count($search)) {
|
|
return 11;
|
|
}
|
|
|
|
$addresses = [];
|
|
|
|
foreach(AddressModel::search($search) as $address) {
|
|
// if supplier -> add it as extra line
|
|
if($address->fibu_supplier_number) {
|
|
$addresses[$address->fibu_supplier_number] = $address;
|
|
if(!$address->fibu_account_number) continue;
|
|
}
|
|
|
|
// if is primary -> use it
|
|
if($address->fibu_account_number && $address->fibu_primary_account) {
|
|
$addresses[$address->fibu_account_number] = $address;
|
|
continue;
|
|
}
|
|
|
|
// if only address with fibu_account_num -> make primary
|
|
if($address->fibu_account_number && !$address->fibu_primary_account) {
|
|
// look up other addresses with same account num
|
|
$address_count = AddressModel::count(["fibu_account_number" => $address->fibu_account_number]);
|
|
if($address_count === 1) {
|
|
$address->fibu_primary_account = 1;
|
|
$address->save();
|
|
$addresses[$address->fibu_account_number] = $address;
|
|
continue;
|
|
}
|
|
|
|
// if more addresses with fibu_account_num -> find primary
|
|
if($address_count > 1) {
|
|
// find primary
|
|
$primary = AddressModel::getFirst(["fibu_account_number" => $address->fibu_account_number, "fibu_primary_account" => true]);
|
|
if($primary) {
|
|
// use single primary
|
|
$addresses[$primary->fibu_account_number] = $primary;
|
|
continue;
|
|
} else {
|
|
// if no primary -> make last one primary
|
|
$new_primary = false;
|
|
foreach(AddressModel::search(["fibu_account_number" => $address->fibu_account_number]) as $primary) {
|
|
$new_primary = $primary;
|
|
}
|
|
if(!$new_primary) {
|
|
var_dump($address);exit;
|
|
}
|
|
$new_primary->fibu_primary_account = 1;
|
|
$new_primary->save();
|
|
$addresses[$new_primary->fibu_account_number] = $new_primary;
|
|
continue;
|
|
}
|
|
}
|
|
var_dump($address);exit;
|
|
}
|
|
|
|
// if no fibu account number but customer number -> create fibu account number
|
|
if($address->customer_number && !$address->fibu_account_number) {
|
|
// Address::afterSave() generates new fibu account number
|
|
$address->save();
|
|
if(!$address->fibu_account_number) {
|
|
var_dump($address);exit;
|
|
}
|
|
$addresses[$address->fibu_account_number] = $address;
|
|
continue;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//$country_code_errors = [];
|
|
$export_addresses = [];
|
|
|
|
foreach($addresses as $fibu_num => $address) {
|
|
//var_dump($address->country_id, $address->country);exit;
|
|
$a = [];
|
|
$a["id"] = $address->id;
|
|
//$a["is_supplier"] = (array_key_exists("supplier", $address->types) && $address->types['supplier']) ? "1" : "0";
|
|
//$a["is_customer"] = "1";
|
|
//$a["customer_number"] = $address->customer_number;
|
|
$a["fibu_account_number"] = $fibu_num;
|
|
//$a["fibu_supplier_number"] = $address->fibu_supplier_number;
|
|
if($fibu_num >= 300000 && $fibu_num < 400000) {
|
|
$a["fibu_supplier_due"] = (is_numeric($address->fibu_supplier_due)) ? $address->fibu_supplier_due : TT_ADDRESS_DEFAULT_SUPPLIER_DUE;
|
|
$a["fibu_supplier_skonto"] = $address->fibu_supplier_skonto;
|
|
$a["fibu_supplier_skonto_rate"] = $address->fibu_supplier_skonto_rate;
|
|
$a["fibu_supplier_paymentblock"] = $address->fibu_supplier_paymentblock;
|
|
} else {
|
|
$a["fibu_supplier_due"] = "";
|
|
$a["fibu_supplier_skonto"] = "";
|
|
$a["fibu_supplier_skonto_rate"] = "";
|
|
$a["fibu_supplier_paymentblock"] = "";
|
|
}
|
|
$a["street"] = $address->street;
|
|
$a["zip"] = $address->zip;
|
|
$a["city"] = $address->city;
|
|
//var_dump($address->getProperty("country"), $address->country);exit;
|
|
$a["countrycode"] = $address->getProperty("country")->isocode;
|
|
$a["phone"] = "";
|
|
if($address->mobile) $a["phone"] = $address->mobile;
|
|
if($address->phone) $a["phone"] = $address->phone;
|
|
$a["uid"] = $address->uid;
|
|
$a["billing_type"] = ($address->billing_type == "sepa") ? "1" : "0";
|
|
//$a["billing_delivery"] = $address->billing_delivery;
|
|
|
|
if($address->company) {
|
|
$a["firstname"] = "";
|
|
$a["lastname"] = $address->company;
|
|
} else {
|
|
$a["firstname"] = $address->firstname;
|
|
$a["lastname"] = $address->lastname;
|
|
}
|
|
|
|
if(array_key_exists("billing", $address->links) && $address->links["billing"][0] && $address->links["billing"][0]->address_id) {
|
|
//var_dump($address->links);exit;
|
|
$bill = $address->links["billing"][0]->address;
|
|
$a["bank"] = $bill->bank_account_bank;
|
|
$a["bank_owner"] = $bill->bank_account_owner;
|
|
$a["iban"] = $bill->bank_account_iban;
|
|
$a["bic"] = $bill->bank_account_bic;
|
|
$a["email"] = $bill->email;
|
|
} else {
|
|
$a["bank"] = $address->bank_account_bank;
|
|
$a["bank_owner"] = $address->bank_account_owner;
|
|
$a["iban"] = $address->bank_account_iban;
|
|
$a["bic"] = $address->bank_account_bic;
|
|
$a["email"] = $address->email;
|
|
}
|
|
|
|
$a["customer_numbers"] = [];
|
|
$a["customer_numbers"][] = $address->customer_number;
|
|
|
|
// add all customer numbers to customer_numbers
|
|
if($address->fibu_account_number) {
|
|
foreach(AddressModel::search(["fibu_account_number" => $address->fibu_account_number]) as $secondary) {
|
|
if($secondary->customer_number && !in_array($secondary->customer_number, $a["customer_numbers"])) {
|
|
$a["customer_numbers"][] = $secondary->customer_number;
|
|
}
|
|
}
|
|
}
|
|
|
|
$export_addresses[] = $a;
|
|
}
|
|
|
|
/*
|
|
* Irrelevant since Country table
|
|
if(count($country_code_errors)) {
|
|
$msg = "Hallo,\r\n\r\ndas in thetool eingetragene Land von folgenden Kunden/Lieferanten konnte nicht in einen ISO-Code übersetzt werden:\r\n\r\n";
|
|
foreach($country_code_errors as $fibu_num => $address) {
|
|
$msg .= "$fibu_num (".$address->getCompanyOrName()."): ".$address->country."\r\n";
|
|
}
|
|
|
|
$email = new Emailnotification();
|
|
$email->setFrom(TT_OUTGOING_EMAIL, "thetool");
|
|
$email->setTo(TT_FIBU_EMAIL_TO);
|
|
$email->setSubject("[BMD-Export] Fehlerhaftes Land");
|
|
$email->setBody($msg);
|
|
$email->send();
|
|
}
|
|
*/
|
|
$tpl = new Layout();
|
|
$tpl->setTemplate("Address/bmd_export.csv");
|
|
$tpl->set("addresses", $export_addresses);
|
|
$csv_content = $tpl->render();
|
|
$return_values = $tpl->getReturnedValue();
|
|
$csv_header = $return_values['header'];
|
|
|
|
if(file_exists($filepath)) {
|
|
// if file exists, append new content
|
|
$result = file_put_contents($filepath, $csv_content, FILE_APPEND);
|
|
} else {
|
|
// else, add content with csv header
|
|
$result = file_put_contents($filepath, $csv_header.$csv_content);
|
|
}
|
|
|
|
if($result === false) {
|
|
return 19;
|
|
} else {
|
|
$export_ts->save();
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
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 $this->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") {
|
|
$contracts = [];
|
|
foreach(ContractModel::search(['owner_id' => $this->id]) as $contract) {
|
|
if(!array_key_exists($contract->id, $contracts)) {
|
|
$contracts[$contract->id] = $contract;
|
|
}
|
|
}
|
|
foreach(ContractModel::search(['billingaddress_id' => $this->id]) as $contract) {
|
|
if(!array_key_exists($contract->id, $contracts)) {
|
|
$contracts[$contract->id] = $contract;
|
|
}
|
|
}
|
|
|
|
$this->contracts = $contracts;
|
|
return $this->contracts;
|
|
}
|
|
|
|
if($name == "active_contracts") {
|
|
$contracts = [];
|
|
foreach(ContractModel::searchActive(['owner_id' => $this->id]) as $contract) {
|
|
if(!array_key_exists($contract->id, $contracts)) {
|
|
$contracts[$contract->id] = $contract;
|
|
}
|
|
}
|
|
foreach(ContractModel::searchActive(['billingaddress_id' => $this->id]) as $contract) {
|
|
if(!array_key_exists($contract->id, $contracts)) {
|
|
$contracts[$contract->id] = $contract;
|
|
}
|
|
}
|
|
|
|
$this->contracts = $contracts;
|
|
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(['address_or_origin_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;
|
|
}
|
|
} |