WIP Contract/Billing 2024-07-04
This commit is contained in:
@@ -5,6 +5,7 @@ class Admin_IvtContractImport {
|
||||
private $log;
|
||||
private $static_ivt_order_match = [];
|
||||
private $no_matchcode = [];
|
||||
private $ownerIdToBillingAddress = [];
|
||||
|
||||
public function __construct($request = false) {
|
||||
$this->request = $request;
|
||||
@@ -140,6 +141,7 @@ class Admin_IvtContractImport {
|
||||
|
||||
if(!$customer_check_return || !array_key_exists("billingaddress_id", $customer_check_return) || !$customer_check_return["billingaddress_id"]) {
|
||||
echo "IVT Customer " . $ivt_contract->cid . " nicht im tool<br />\n";
|
||||
var_dump($customer_check_return);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -413,77 +415,217 @@ class Admin_IvtContractImport {
|
||||
die("Address $ivt_custnum not found!");
|
||||
}
|
||||
|
||||
if(array_key_exists($owner->id, $this->ownerIdToBillingAddress)) {
|
||||
return ["billingaddress_id" => $this->ownerIdToBillingAddress[$owner->id]];
|
||||
}
|
||||
|
||||
$compare_address = $owner;
|
||||
$compare_type = "owner";
|
||||
$return["billingaddress_id"] = $owner->id;
|
||||
|
||||
$order = $this->findOrder($ivt_contract);
|
||||
if(!$order) {
|
||||
return ["billingaddress_id" => $owner->id];
|
||||
$return["billingaddress_id"] = $owner->id;
|
||||
$compare_address = $owner;
|
||||
$compare_type = "owner";
|
||||
}
|
||||
|
||||
// look for billingaddress_id in order and use it
|
||||
if ($order->billingaddress_id) {
|
||||
if ($order->billingaddress_id == $ivt_custnum) {
|
||||
$return["billingaddress_id"] = $order->billingaddress_id;
|
||||
} else {
|
||||
$billingaddress = new Address($order->billingaddress_id);
|
||||
if (!$billingaddress->id) {
|
||||
die("Billingaddress " . $order->billingaddress_id . " does not exist (order " . $order->id . "; ivt customer $ivt_custnum)");
|
||||
}
|
||||
$return["billingaddress_id"] = $billingaddress->id;
|
||||
if ($order && $order->billingaddress_id) {
|
||||
$return["billingaddress_id"] = $order->billingaddress_id;
|
||||
|
||||
$billingaddress = new Address($order->billingaddress_id);
|
||||
if (!$billingaddress->id) {
|
||||
die("Billingaddress " . $order->billingaddress_id . " does not exist (order " . $order->id . "; ivt customer $ivt_custnum)");
|
||||
}
|
||||
|
||||
$compare_address = $billingaddress;
|
||||
$compare_type = "billingaddress";
|
||||
}
|
||||
|
||||
|
||||
|
||||
// if no billingaddress_id in order, compare address of Address and ivt_customer and
|
||||
// create new billingaddress if nessecary
|
||||
|
||||
if(!$compare_address) {
|
||||
die("No Compare address");
|
||||
}
|
||||
|
||||
$address_update = $this->compareBillingAddresses($ivt_customer, $compare_address);
|
||||
if($address_update === true) {
|
||||
$return["billingaddress_id"] = $compare_address->id;
|
||||
} elseif(is_array($address_update) && count($address_update)) {
|
||||
if($compare_type == "billingaddress") {
|
||||
// update billingaddress
|
||||
$compare_address->update($address_update);
|
||||
if(!$compare_address->save()) {
|
||||
die("error updateing Billingaddress");
|
||||
}
|
||||
$this->ownerIdToBillingAddress[$owner->id] = $compare_address->id;
|
||||
$return["billingaddress_id"] = $compare_address->id;
|
||||
} else {
|
||||
// create billingaddress
|
||||
$billingaddress = AddressModel::create($address_update);
|
||||
try {
|
||||
if(!$billingaddress->save()) {
|
||||
var_dump($address_update);
|
||||
die("error creating Billingaddress");
|
||||
}
|
||||
$return["billingaddress_id"] = $billingaddress->id;
|
||||
if(!array_key_exists($owner->id, $this->ownerIdToBillingAddress)) {
|
||||
$this->ownerIdToBillingAddress[$owner->id] = $billingaddress->id;
|
||||
$this->log->debug(__METHOD__.": Creating billing link for owner ".$owner->id." ".$owner->getCompanyOrName()." with billing address ".$billingaddress->id." ".$billingaddress->getCompanyOrName());
|
||||
$this->log->debug("OWNER:");
|
||||
$this->log->debug(print_r($owner->data, true));
|
||||
$this->log->debug("ADDRESS UPDATE:");
|
||||
$this->log->debug(print_r($address_update, true));
|
||||
// create addresslink
|
||||
|
||||
$link = AddressLinkModel::create([
|
||||
'origin_address_id' => $owner->id,
|
||||
'type' => "billing",
|
||||
'address_id' => $billingaddress->id
|
||||
]);
|
||||
$link->save();
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "Exception: ".$e->getMessage()."\n";
|
||||
var_dump($address_update);
|
||||
exit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
private function compareBillingAddresses($ivt_customer, $tool_customer) {
|
||||
$owner = $tool_customer;
|
||||
|
||||
$new_billing = [];
|
||||
|
||||
|
||||
if ($ivt_customer->company) {
|
||||
if ($ivt_customer->company != $owner->company) $new_billing["company"] = $ivt_customer->company;
|
||||
if (strtolower(trim($ivt_customer->company)) != strtolower(trim($owner->company))) $new_billing["company"] = $ivt_customer->company;
|
||||
} else {
|
||||
if ($ivt_customer->firstname != $owner->firstname) $new_billing["firstname"] = $ivt_customer->firstname;
|
||||
if ($ivt_customer->surname != $owner->lastname) $new_billing["lastname"] = $ivt_customer->surname;
|
||||
if (strtolower(trim($ivt_customer->firstname)) != strtolower(trim($owner->firstname))) $new_billing["firstname"] = $ivt_customer->firstname;
|
||||
if (strtolower(trim($ivt_customer->surname)) != strtolower(trim($owner->lastname))) $new_billing["lastname"] = $ivt_customer->surname;
|
||||
}
|
||||
|
||||
//if ($ivt_customer->UID != $owner->uid) $new_billing["uid"] = $ivt_customer->UID;
|
||||
if ($ivt_customer->zip != $owner->zip) $new_billing["zip"] = $ivt_customer->zip;
|
||||
if ($ivt_customer->location != $owner->city) $new_billing["city"] = $ivt_customer->location;
|
||||
if ($ivt_customer->street . " " . $ivt_customer->housenumber != $owner->street) $new_billing["street"] = $ivt_customer->street . " " . $ivt_customer->housenumber;
|
||||
if ($ivt_customer->phone != $owner->phone) $new_billing["phone"] = $ivt_customer->phone;
|
||||
if ($ivt_customer->email != $owner->email) $new_billing["email"] = $ivt_customer->email;
|
||||
if (strtolower(trim($ivt_customer->zip)) != strtolower(trim($owner->zip))) $new_billing["zip"] = $ivt_customer->zip;
|
||||
if (strtolower(trim($ivt_customer->location)) != strtolower(trim($owner->city))) $new_billing["city"] = $ivt_customer->location;
|
||||
if (strtolower(trim($ivt_customer->street . " " . $ivt_customer->housenumber)) != strtolower(trim($owner->street))) $new_billing["street"] = $ivt_customer->street . " " . $ivt_customer->housenumber;
|
||||
//if (strtolower(trim($ivt_customer->phone)) != strtolower(trim($owner->phone))) $new_billing["phone"] = $ivt_customer->phone;
|
||||
if (strtolower(trim($ivt_customer->email)) != strtolower(trim($owner->email))) {
|
||||
if($ivt_customer->email && (!$owner->email || $owner->email = "dummy@xinon.at")) {
|
||||
$owner->email = strtolower(trim($ivt_customer->email));
|
||||
$owner->save();
|
||||
} else {
|
||||
$new_billing["email"] = $ivt_customer->email;
|
||||
}
|
||||
}
|
||||
|
||||
if ($ivt_customer->payment == 1 && $owner->billing_type != "invoice") {
|
||||
$new_billing["billing_type"] = "invoice";
|
||||
} elseif ($ivt_customer->payment == 0 && $owner->billing_type != "sepa") {
|
||||
$new_billing["billing_type"] = "sepa";
|
||||
} elseif ($ivt_customer->paper_invoice == 1 && $owner->billing_delivery != "paper") {
|
||||
$new_billing["billing_delivery"] = "paper";
|
||||
} elseif ($ivt_customer->paper_invoice == 0 && $owner->billing_delivery != "email") {
|
||||
$new_billing["billing_delivery"] = "email";
|
||||
$billing_type = "";
|
||||
$billing_delivery = "";
|
||||
if($ivt_customer->payment == 1 && $owner->billing_type != "invoice") {
|
||||
$billing_type = "invoice";
|
||||
if($owner->billing_type) {
|
||||
$new_billing["billing_type"] = "invoice";
|
||||
} else {
|
||||
$owner->billing_type = "invoice";
|
||||
$owner->save();
|
||||
}
|
||||
} elseif($ivt_customer->payment == 0 && $owner->billing_type != "sepa") {
|
||||
$billing_type = "sepa";
|
||||
if($owner->billing_type) {
|
||||
$new_billing["billing_type"] = "sepa";
|
||||
} else {
|
||||
$owner->billing_type = "sepa";
|
||||
$owner->save();
|
||||
}
|
||||
} elseif($ivt_customer->paper_invoice == 1 && $owner->billing_delivery != "paper") {
|
||||
$billing_delivery = "paper";
|
||||
if($owner->billing_delivery) {
|
||||
$new_billing["billing_delivery"] = "paper";
|
||||
} else {
|
||||
$owner->billing_delivery = "paper";
|
||||
$owner->save();
|
||||
}
|
||||
} elseif($ivt_customer->paper_invoice == 0 && $owner->billing_delivery != "email") {
|
||||
$billing_delivery = "email";
|
||||
if($owner->billing_delivery) {
|
||||
$new_billing["billing_delivery"] = "email";
|
||||
} else {
|
||||
$owner->billing_delivery = "email";
|
||||
$owner->save();
|
||||
}
|
||||
}
|
||||
|
||||
//if($ivt_customer->bank_account_bank != $owner->bank_account_bank) $new_billing["bank_account_bank"] = $ivt_customer->bank_account_bank;
|
||||
//if($ivt_customer->bank_account_owner != $owner->bank_account_owner) $new_billing["bank_account_owner"] = $ivt_customer->bank_account_owner;
|
||||
$iban = strtoupper(trim($ivt_customer->IBAN));
|
||||
$bic = strtoupper(trim($ivt_customer->BIC));
|
||||
$iban = strtoupper(trim(str_replace(" ","", $ivt_customer->IBAN)));
|
||||
$bic = strtoupper(trim(str_replace(" ","", $ivt_customer->BIC)));
|
||||
if ((array_key_exists("billing_type", $new_billing) && $new_billing["billing_type"] == "sepa") || $ivt_customer->payment == 0 && ($iban || $bic)) {
|
||||
if ($iban != strtoupper(trim($owner->bank_account_iban))) $new_billing["bank_account_iban"] = $iban;
|
||||
if ($bic != strtoupper(trim($owner->bank_account_bic))) $new_billing["bank_account_bic"] = $bic;
|
||||
if ($iban != strtoupper(trim(str_replace(" ","", $owner->bank_account_iban)))) $new_billing["bank_account_iban"] = $iban;
|
||||
if ($bic != strtoupper(trim(str_replace(" ","", $owner->bank_account_bic)))) $new_billing["bank_account_bic"] = $bic;
|
||||
}
|
||||
|
||||
$missing_fields = [];
|
||||
if(array_key_exists("bank_account_bic", $new_billing) && !array_key_exists("bank_account_iban", $new_billing)) {
|
||||
$owner->bank_account_bic = $new_billing["bank_account_bic"];
|
||||
$owner->save();
|
||||
unset($new_billing["bank_account_bic"]);
|
||||
}
|
||||
|
||||
if(count($new_billing) == 2 && array_key_exists("billing_type", $new_billing) && array_key_exists("billing_delivery", $new_billing)) {
|
||||
$owner->billing_type = $billing_type;
|
||||
$owner->billing_delivery = $billing_delivery;
|
||||
$owner->save();
|
||||
return true;
|
||||
}
|
||||
if(count($new_billing) == 1) {
|
||||
if(array_key_exists("billing_type", $new_billing)) {
|
||||
$owner->billing_type = $billing_type;
|
||||
$owner->save();
|
||||
return true;
|
||||
}
|
||||
if(array_key_exists("billing_delivery", $new_billing)) {
|
||||
$owner->billing_delivery = $billing_delivery;
|
||||
$owner->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//$missing_fields = [];
|
||||
$create = false;
|
||||
foreach(["company", "firstname", "lastname", "street", "zip", "city", "phone", "email", "uid", "billing_type", "billing_delivery",
|
||||
foreach(["company", "firstname", "lastname", "street", "zip", "city", "email", "uid", "billing_type", "billing_delivery",
|
||||
"bank_account_iban", "bank_account_bic"] as $field) {
|
||||
if(!array_key_exists($field, $new_billing)) {
|
||||
/*if(!$owner->$field) {
|
||||
if($field == "billing_delivery") {
|
||||
$new_billing[$field] = "paper";
|
||||
} else {
|
||||
$new_billing[$field] = "";
|
||||
}
|
||||
|
||||
} else {
|
||||
$new_billing[$field] = $owner->$field;
|
||||
}*/
|
||||
$new_billing[$field] = $owner->$field;
|
||||
} else {
|
||||
if($field == "email" && $field = "dummy@xinon.at") continue;
|
||||
$missing_fields[] = $field;
|
||||
if($field == "email" && $new_billing["email"] == "dummy@xinon.at") continue;
|
||||
$create = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if($create) {
|
||||
$billingaddress = AddressModel::create($new_billing);
|
||||
//var_dump($billingaddress, $missing_fields);exit;
|
||||
@@ -491,9 +633,14 @@ class Admin_IvtContractImport {
|
||||
die("Error createing billingaddress\n");
|
||||
}
|
||||
$return["billingaddress_id"] = $billingaddress->id;
|
||||
}*/
|
||||
|
||||
if($create) {
|
||||
return $new_billing;
|
||||
}
|
||||
|
||||
return ["billingaddress_id" => $owner->id];
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private function checkIvtProduct($product) {
|
||||
@@ -631,7 +778,7 @@ class Admin_IvtContractImport {
|
||||
}
|
||||
|
||||
|
||||
private function findOrder($ivt_contract) {
|
||||
public function findOrder($ivt_contract) {
|
||||
// find order
|
||||
$ivt_customer = $ivt_contract->customer;
|
||||
$ivt_product = $ivt_contract->product;
|
||||
@@ -724,6 +871,8 @@ class Admin_IvtContractImport {
|
||||
$number = preg_replace('/^0043/', '43', $ivtnum->number);
|
||||
if(!$number) continue;
|
||||
|
||||
|
||||
|
||||
// find number in block
|
||||
$voicenumberblock = Voicenumberblock::findBlock($number);
|
||||
if(!$voicenumberblock) {
|
||||
@@ -746,16 +895,23 @@ class Admin_IvtContractImport {
|
||||
|
||||
$voice_contract = false;
|
||||
|
||||
foreach($contracts as $contract) {
|
||||
if(array_key_exists("needs_number", $contract->product->attributes) && $contract->product->attributes["needs_number"] == 1) {
|
||||
$voice_contract = $contract;
|
||||
if($ivt_customer_id == 1376) {
|
||||
//if(in_array($number, [43313228451, 43313228406, 43720666572, 43313228400, 4331324890, 43720103806])) {
|
||||
$prometheus_primary_contract = ContractModel::getFirst(["customer_number" => 1376]);
|
||||
$voice_contract = $this->createVoiceContract($prometheus_primary_contract, true);
|
||||
} else {
|
||||
foreach($contracts as $contract) {
|
||||
if(array_key_exists("needs_number", $contract->product->attributes) && $contract->product->attributes["needs_number"] == 1) {
|
||||
$voice_contract = $contract;
|
||||
}
|
||||
}
|
||||
|
||||
if(!$voice_contract) {
|
||||
$voice_contract = $this->createVoiceContract($contracts);
|
||||
}
|
||||
}
|
||||
|
||||
if(!$voice_contract) {
|
||||
$voice_contract = $this->createVoiceContract($contracts);
|
||||
}
|
||||
|
||||
|
||||
//var_dump($voice_contract);exit;
|
||||
|
||||
@@ -824,7 +980,7 @@ class Admin_IvtContractImport {
|
||||
return true;
|
||||
}
|
||||
|
||||
private function createVoiceContract($contracts) {
|
||||
private function createVoiceContract($contracts, $nolink = false) {
|
||||
// find rufnummer only product (residential or business)
|
||||
$product = new Product(101); // Telefonie (nur Rufnummer - Privat)
|
||||
$sla_id = 4;
|
||||
@@ -863,21 +1019,23 @@ class Admin_IvtContractImport {
|
||||
|
||||
$voice_contract = ContractModel::create($data);
|
||||
if(!$voice_contract->save()) {
|
||||
$this->log->error("Unable to create voice contract for cust ".$fc->customer_number);
|
||||
$this->log->error("Unable to create voice contract for cust " . $fc->customer_number);
|
||||
exit;
|
||||
}
|
||||
|
||||
// link to all $contracts
|
||||
foreach($contracts as $contract) {
|
||||
if (ContractLinkModel::getFirst(["contract_id" => $voice_contract->id, "origin_contract_id" => $contract->id])) {
|
||||
continue;
|
||||
if(!$nolink) {
|
||||
foreach($contracts as $contract) {
|
||||
if(ContractLinkModel::getFirst(["contract_id" => $voice_contract->id, "origin_contract_id" => $contract->id])) {
|
||||
continue;
|
||||
}
|
||||
$link = ContractLinkModel::create([
|
||||
'contract_id' => $voice_contract->id,
|
||||
'origin_contract_id' => $contract->id,
|
||||
'type' => 'link'
|
||||
]);
|
||||
$link->save();
|
||||
}
|
||||
$link = ContractLinkModel::create([
|
||||
'contract_id' => $voice_contract->id,
|
||||
'origin_contract_id' => $contract->id,
|
||||
'type' => 'link'
|
||||
]);
|
||||
$link->save();
|
||||
}
|
||||
|
||||
return $voice_contract;
|
||||
@@ -902,7 +1060,7 @@ class Admin_IvtContractImport {
|
||||
}
|
||||
$address_data['zip'] = $cust->zip;
|
||||
$address_data['city'] = $cust->location;
|
||||
$address_data['country'] = "";
|
||||
$address_data['country_id'] = 163; // 163 - Austria
|
||||
$address_data['phone'] = $cust->phone;
|
||||
$address_data['fax'] = "";
|
||||
$address_data['mobile'] = "";
|
||||
|
||||
@@ -158,6 +158,20 @@ class Admin_IvtCreditImport {
|
||||
$data["finish_date"] = $primary_contract->finish_date;
|
||||
$data["finish_date_by"] = $primary_contract->finish_date_by;
|
||||
$data["note"] = $primary_contract->note;
|
||||
|
||||
$matchcode = "";
|
||||
if($primary_contract->termination_id) {
|
||||
$building = $primary_contract->termination->building;
|
||||
if($building->networksection_id) {
|
||||
$matchcode .= $building->networksection->name;
|
||||
}
|
||||
$matchcode .= $customer->getCompanyOrName();
|
||||
$matchcode .= "; ".$building->street.", ".$building->zip." ".$building->city;
|
||||
} else {
|
||||
$matchcode .= "; ".$customer->street.", ".$customer->zip." ".$customer->city;
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
$create_date = new DateTime($cust_cred->created);
|
||||
$create_date->setTime(2,0);
|
||||
@@ -183,9 +197,17 @@ class Admin_IvtCreditImport {
|
||||
$data["finish_date"] = $finish_date->getTimestamp();
|
||||
$data["finish_date_by"] = 1;
|
||||
$data["note"] = null;
|
||||
|
||||
$matchcode = $cust_cred->comment;
|
||||
}
|
||||
|
||||
$data["matchcode"] = $cust_cred->comment;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$data["matchcode"] = $matchcode;
|
||||
$data["owner_id"] = $netowner->id;
|
||||
$data["billingaddress_id"] = $netowner->id;
|
||||
$data["termination_id"] = null;
|
||||
|
||||
Reference in New Issue
Block a user