1192 lines
45 KiB
PHP
1192 lines
45 KiB
PHP
<?php
|
|
|
|
class Admin_IvtContractImport {
|
|
private $request;
|
|
private $log;
|
|
private $static_ivt_order_match = [];
|
|
private $no_matchcode = [];
|
|
private $ownerIdToBillingAddress = [];
|
|
private $ivt_to_voiceplan = [
|
|
1 => 1, // Ivt Standard
|
|
2 => 4, // Ivt Privat Plus
|
|
3 => 3, // Ivt Business Easy
|
|
4 => 1 // ESIT Partner Tarif
|
|
];
|
|
|
|
public function __construct($request = false) {
|
|
$this->request = $request;
|
|
$this->log = mfLoghandler::singleton();
|
|
|
|
/*
|
|
* ivt::customer_product::id => Order::id
|
|
* SELECT cp.id,cp.cid,cp.pid,products.name,cp.sid,cp.created,cp.lastdate,cp.comment,tp.name
|
|
* FROM `customer_product` as cp LEFT JOIN products ON (cp.pid = products.id)
|
|
* LEFT JOIN thetool_live4.IvtProductMatch ipm ON (ipm.ivt_product_id = cp.pid)
|
|
* LEFT JOIN thetool_live4.Product tp ON (ipm.product_id = tp.id)
|
|
* WHERE `cid` = 1840;
|
|
*/
|
|
$this->static_ivt_order_match = [
|
|
17245 => 3946, // lugitsch 1840
|
|
16704 => 3342,
|
|
16703 => 3342,
|
|
15712 => 3414,
|
|
14798 => 2940,
|
|
14322 => 2623,
|
|
14321 => 2623,
|
|
14222 => 2702,
|
|
13951 => 2218,
|
|
13322 => 2623,
|
|
2034 => false,
|
|
561 => false,
|
|
11409 => 1222, // x-tec 1842
|
|
14192 => 2596,
|
|
//tourismusverband thermen- & vulkanland
|
|
11221 => 1120,
|
|
11222 => 1044,
|
|
11223 => 1003,
|
|
11224 => 1004,
|
|
11225 => 1002,
|
|
11226 => 999,
|
|
11227 => 1000,
|
|
2987 => 4126, // Atos
|
|
14890 => 3612,
|
|
14891 => 3612,
|
|
13551 => 2776, // Wolfgang Lipp
|
|
14908 => 2776,
|
|
13552 => 2055,
|
|
10550 => 230, // Anneliese Wabnegg
|
|
10551 => 230,
|
|
10995 => 1190,
|
|
12040 => 1687, // peter junior koch
|
|
12179 => 1687,
|
|
12982 => 1863, // reinhard strauss
|
|
12983 => 1863,
|
|
13188 => false, // magna
|
|
10909 => 2820, // bernd zöhrer
|
|
12101 => false,
|
|
14679 => 2820,
|
|
10041 => 3351, // walter würfel
|
|
15759 => 3351,
|
|
10483 => 1042, // joachim novak
|
|
10850 => 1042,
|
|
13216 => 1310, // theo werner koblischek
|
|
13217 => 1310,
|
|
13456 => 2060,
|
|
13746 => 3614, // manfred hartmann
|
|
13747 => 3614,
|
|
13436 => 2076, // hernuß josef gmbh
|
|
13437 => 2076,
|
|
13440 => 2077,
|
|
13441 => 2077,
|
|
14295 => 3172, // Christa Eisenberger-Strobl
|
|
15545 => 3172,
|
|
14189 => 3322, // andeßner-angleitner
|
|
16722 => 3322,
|
|
16267 => 3079, // schmuck marianne
|
|
16786 => 3079
|
|
];
|
|
$this->no_matchcode = [
|
|
9,
|
|
2847,
|
|
3049,
|
|
5719
|
|
|
|
];
|
|
}
|
|
|
|
/* TODO: Credit */
|
|
/* TODO: Credit */
|
|
public function run($doit = false) {
|
|
//echo "running...<br />\n";
|
|
$data = [];
|
|
$data['ignore'] = 0;
|
|
$data["contracts"] = [];
|
|
|
|
$new_contracts = [];
|
|
$last_cid = 0;
|
|
|
|
$i = 0;
|
|
$maxIvtContracts = IvtCustomerProductModel::count([]);
|
|
echo "Importing from $maxIvtContracts Ivt customer_products\n";
|
|
|
|
foreach(IvtCustomerProductModel::getAll() as $ivt_contract) {
|
|
//if($ivt_contract->cid < 104200) continue;
|
|
//if($i > 1000 && $ivt_contract->cid != $last_cid) break; // only break after the last ivtcontract of this customer
|
|
$last_cid = $ivt_contract->cid;
|
|
|
|
$i++;
|
|
$neu = [];
|
|
$orderproduct = false;
|
|
$orderproduct_id = false;
|
|
|
|
$ivt_customer = $ivt_contract->customer;
|
|
$ivt_product = $ivt_contract->product;
|
|
|
|
/*
|
|
* get thetool product from IvtProductMatch
|
|
*/
|
|
$productMatch = IvtProductMatchModel::getFirst(["ivt_product_id" => $ivt_contract->pid]);
|
|
if(!$productMatch) {
|
|
echo "Kein Match zu IVT Product " . $ivt_contract->pid . " gefunden.<br />\n";
|
|
exit;
|
|
}
|
|
|
|
if(!$this->checkIvtProduct($productMatch->ivtproduct, $doit)) {
|
|
exit;
|
|
}
|
|
|
|
$ip = $productMatch->ivtproduct;
|
|
$product = $productMatch->product;
|
|
|
|
/*
|
|
* check ivt customer and get Billingaddress
|
|
*/
|
|
$billingaddress_id = false;
|
|
|
|
$customer_check_return = $this->checkIvtCustomer($ivt_contract, $ivt_customer, $product);
|
|
|
|
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;
|
|
}
|
|
|
|
$billingaddress_id = $customer_check_return["billingaddress_id"];
|
|
|
|
/*
|
|
* get or create customer
|
|
*/
|
|
$customer = AddressModel::getFirst(["customer_number" => $ivt_contract->cid]);
|
|
/*if($ivt_contract->id == 3035) {
|
|
var_dump($ivt_contract->cid, $customer);
|
|
}*/
|
|
|
|
if(!$customer) {
|
|
$this->log->debug("Kein thetool Kunde zu IVT customer " . $ivt_contract->cid . " gefunden -> wird angelegt");
|
|
$customer = $this->createAddressFromIvtCustomer($ivt_contract->customer);
|
|
if(!$customer) {
|
|
echo "Fehler beim anlegen der Adresse<br />\n";
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
// check if contract was imported already
|
|
$existing_contract = ContractModel::getFirst(["imported_from" => "ivt", "imported_data" => $ivt_contract->id]);
|
|
if($existing_contract) {
|
|
//echo "Contract gibts schon ".$existing_contract->id."<br />\n";
|
|
continue;
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$contract_data = [];
|
|
$contract_data['owner_id'] = $customer->id;
|
|
$contract_data['billingaddress_id'] = $billingaddress_id;
|
|
$contract_data['product_id'] = $product->id;
|
|
$contract_data['product_name'] = $ip->name;
|
|
$contract_data['vatgroup_id'] = $product->vatgroup_id;
|
|
$contract_data['amount'] = 1;
|
|
$contract_data['price'] = $ivt_product->price;
|
|
$contract_data['price_setup'] = 0;
|
|
$contract_data['price_nne'] = 0;
|
|
$contract_data['price_nbe'] = 0;
|
|
$contract_data['sla_id'] = $this->getNewSlaId($ivt_contract->sid);
|
|
$contract_data['imported_from'] = "ivt";
|
|
$contract_data['imported_data'] = $ivt_contract->id;
|
|
$contract_data['create_by'] = 1;
|
|
$contract_data['edit_by'] = 1;
|
|
|
|
$finish_date = new DateTime($ivt_contract->lastdate);
|
|
|
|
switch($ip->interval) {
|
|
case 0:
|
|
$contract_data['billing_period'] = 1;
|
|
//$finish_date->modify("+1 month");
|
|
break;
|
|
case 1:
|
|
$contract_data['billing_period'] = 12;
|
|
//$finish_date->modify("+1 year");
|
|
break;
|
|
}
|
|
|
|
$order_date = new DateTime($ivt_contract->created);
|
|
$order_date->setTime(2,0,0);
|
|
|
|
$finish_date->modify("first day of this month");
|
|
$finish_date->setTime(2,0,0);
|
|
$finish_date->modify("+1 month"); // XXX for debugging only; must be removed
|
|
//$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1);
|
|
//$finish_date->modify("+1 hours");
|
|
|
|
$contract_data['order_date'] = $order_date->getTimestamp();
|
|
$contract_data['finish_date'] = $finish_date->getTimestamp();
|
|
$contract_data['finish_date_by'] = 1;
|
|
|
|
$neu["customer"] = $customer;
|
|
$neu["ivtcustomer"] = $ivt_customer;
|
|
$neu["ivtproduct"] = $ip;
|
|
$neu["product"] = $product;
|
|
$neu["orderproduct"] = false;
|
|
$neu['action'] = "import";
|
|
$ignore = false;
|
|
|
|
if($product->price <= 0 && $ivt_contract->cid != 1799) {
|
|
$contract_data['price'] = 0;
|
|
$contract_data['price_setup'] = $ip->price;
|
|
$contract_data['billing_period'] = 0;
|
|
//$neu['action'] = "ignore";
|
|
|
|
//$ignore = true;
|
|
}
|
|
|
|
// filter cancelled products
|
|
/* // disabled weil gibt keine
|
|
if($product->billing_period == 1 && $ivt_contract->lastdate < "2024-04-09") {
|
|
continue;
|
|
}
|
|
if($product->billing_period >= 12 && $ivt_contract->lastdate < "2023-06-06") {
|
|
continue;
|
|
}*/
|
|
|
|
/*
|
|
* Try to match ivt contract to thetool Order
|
|
*/
|
|
$order = OrderModel::getFirst(["owner_id" => $customer->id]);
|
|
|
|
if($order) {
|
|
foreach($order->products as $orderproduct) {
|
|
if($orderproduct->product_id == $product->id) {
|
|
$orderproduct_id = $orderproduct->id;
|
|
if($orderproduct->termination_id) {
|
|
$contract_data['termination_id'] = $orderproduct->termination_id;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if($orderproduct_id) {
|
|
$contract_data["orderproduct_id"] = $orderproduct->id;
|
|
$neu["orderproduct"] = $orderproduct;
|
|
} else {
|
|
$this->log->debug("Kein Orderproduct gefunden in Order ".$order->id." für Ivt customer_product ".$ivt_contract->id);
|
|
}
|
|
} else {
|
|
//$this->log->debug("Keine thetool order für ivt customer_product ".$ivt_contract->id);
|
|
}
|
|
|
|
/*
|
|
* Create Contract (don't save yet)
|
|
*/
|
|
$contract = ContractModel::create($contract_data);
|
|
if(trim($ivt_contract->comment)) {
|
|
$contract->matchcode = trim($ivt_contract->comment);
|
|
} elseif(!in_array($ivt_contract->id, $this->no_matchcode)) {
|
|
$contract->matchcode = $contract->generateMatchcode();
|
|
}
|
|
/*if($ivt_contract->id == 3035) {
|
|
var_dump($contract);
|
|
}*/
|
|
$neu["contract"] = $contract;
|
|
|
|
if($doit && !$ignore) {
|
|
$contract_id = $contract->save();
|
|
if(!$contract_id) {
|
|
echo "Fehler beim Contract speichern!<br />\n";
|
|
exit;
|
|
}
|
|
|
|
if(!array_key_exists($ivt_customer->id, $new_contracts)) {
|
|
$new_contracts[$ivt_customer->id] = [];
|
|
}
|
|
$new_contracts[$ivt_customer->id][] = $contract;
|
|
|
|
|
|
// create journal entry
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $contract_id,
|
|
'type' => "created_from",
|
|
'value' => "import",
|
|
'text' => "IVT customer_product ".$ivt_contract->id."; cid: ".$ivt_contract->cid."; pid: ".$ivt_contract->pid
|
|
]);
|
|
$journal_id = $journal->save();
|
|
if(!$journal_id) {
|
|
echo "Fehler beim Journal erstellen.<br />\n";
|
|
exit;
|
|
}
|
|
|
|
// import Contractconfig
|
|
|
|
}
|
|
|
|
if($doit && !$ignore) {
|
|
$contractconfig = $this->importContractconfig($ivt_customer, $ivt_product, $contract);
|
|
}
|
|
|
|
//var_dump($contractconfig);exit;
|
|
|
|
$data["contracts"][] = $neu;
|
|
if($neu['action'] == "ignore") {
|
|
$data['ignore']++;
|
|
}
|
|
|
|
/*if($ivt_contract->id == 3035) {
|
|
var_dump($neu);exit;
|
|
}*/
|
|
}
|
|
|
|
if($doit) {
|
|
$this->log->debug("Linking new contracts, creating missing matchcodes and adding voip data");
|
|
//var_dump($new_contracts[1789]);exit;
|
|
foreach($new_contracts as $ivt_customer_id => $contracts) {
|
|
$this->addVoipData($ivt_customer_id, $contracts);
|
|
|
|
if(count($contracts) < 2) continue;
|
|
$prev_contracts = [];
|
|
$primary_matchcode = false;
|
|
$primary_matchcode_source_product_group = "";
|
|
$primary_matchcode_no_productgroups_pattern = '/(Pauschalen|TV|Managed Services|Vorbestellung|Mobilfunk|Standortvernetzung|Colo|Vorort|Webhosting|zusatz)/i';
|
|
|
|
|
|
foreach($contracts as $contract) {
|
|
if($contract->matchcode && !$primary_matchcode && !preg_match($primary_matchcode_no_productgroups_pattern, $contract->product->productgroup->name)) {
|
|
if(in_array($ivt_contract->id, $this->no_matchcode)) continue;
|
|
$primary_matchcode = $contract->matchcode;
|
|
$primary_matchcode_source_product_group = $contract->product->productgroup->name;
|
|
}
|
|
if($primary_matchcode && !$contract->matchcode && !preg_match($primary_matchcode_no_productgroups_pattern, $primary_matchcode_source_product_group)) {
|
|
if(in_array($ivt_contract->id, $this->no_matchcode)) continue;
|
|
$contract->matchcode = $primary_matchcode;
|
|
$contract->save();
|
|
}
|
|
|
|
if(!count($prev_contracts)) {
|
|
$prev_contracts[] = $contract;
|
|
continue;
|
|
}
|
|
foreach($prev_contracts as $prev) {
|
|
if(!$prev->matchcode && $primary_matchcode && !preg_match($primary_matchcode_no_productgroups_pattern, $primary_matchcode_source_product_group)) {
|
|
$prev->matchcode = $primary_matchcode;
|
|
$prev->save();
|
|
}
|
|
if(preg_match('/Service Pauschale/i',$prev->product->name)) {
|
|
foreach($contracts as $c) {
|
|
if($c->termination_id && $c->matchcode) {
|
|
$prev->matchcode = $c->matchcode;
|
|
$prev->save();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (ContractLinkModel::getFirst(["contract_id" => $contract->id, "origin_contract_id" => $prev->id])) {
|
|
continue;
|
|
}
|
|
$link = ContractLinkModel::create([
|
|
'contract_id' => $contract->id,
|
|
'origin_contract_id' => $prev->id,
|
|
'type' => 'link'
|
|
]);
|
|
$link->save();
|
|
}
|
|
$prev_contracts[] = $contract;
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function checkIvtCustomer($ivt_contract, $ivt_customer, $product, $doit = false)
|
|
{
|
|
$return = [];
|
|
// create customer if not exists
|
|
$ivt_custnum = $ivt_customer->id;
|
|
if (!$ivt_custnum) return false;
|
|
|
|
// sync billing address
|
|
$owner = AddressModel::getFirst(["customer_number" => $ivt_custnum]);
|
|
if (!$owner) {
|
|
//var_dump($ivt_customer, $ivt_contract);
|
|
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;
|
|
$compare_address = $owner;
|
|
$compare_type = "owner";
|
|
}
|
|
|
|
// look for billingaddress_id in order and use it
|
|
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 (strtolower(trim($ivt_customer->company)) != strtolower(trim($owner->company))) $new_billing["company"] = $ivt_customer->company;
|
|
} else {
|
|
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 (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;
|
|
}
|
|
}
|
|
|
|
$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(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(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;
|
|
}
|
|
|
|
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", "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" && $new_billing["email"] == "dummy@xinon.at") continue;
|
|
$create = true;
|
|
}
|
|
}
|
|
|
|
/*
|
|
if($create) {
|
|
$billingaddress = AddressModel::create($new_billing);
|
|
//var_dump($billingaddress, $missing_fields);exit;
|
|
if(!$billingaddress->save()) {
|
|
die("Error createing billingaddress\n");
|
|
}
|
|
$return["billingaddress_id"] = $billingaddress->id;
|
|
}*/
|
|
|
|
if($create) {
|
|
return $new_billing;
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private function checkIvtProduct($product) {
|
|
if(!$product->id) {
|
|
echo __METHOD__ . ": Keine Produkt id<br />\n";
|
|
return false;
|
|
}
|
|
if(!$product->name) {
|
|
echo __METHOD__ . ": Keine Produkt id<br />\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private function importContractconfig($ivt_customer, $ivt_product, &$contract, $doit = false) {
|
|
//return true;
|
|
//var_dump($contract, $contract->product, $contract->configgroups);
|
|
$this->log->debug("Getting Contractconfig for ivt_customer ".$ivt_customer->id);
|
|
|
|
if(!is_array($contract->configgroups) || !count($contract->configgroups)) {
|
|
//echo "no contract config";
|
|
return true;
|
|
}
|
|
|
|
$product = $contract->product;
|
|
if(!$product->id) {
|
|
echo "Produkt nicht gefunden für Contract " . $contract->id . "<br />\n";
|
|
exit;
|
|
}
|
|
|
|
// lookup radius data
|
|
$ruser = $this->getRadiusUser($ivt_customer, $ivt_product, $contract);
|
|
// lookup voip data
|
|
|
|
foreach($contract->configgroups as $cgroup) {
|
|
|
|
$items = $cgroup->items;
|
|
/*var_dump($cgroup);
|
|
var_dump($items);
|
|
exit;*/
|
|
|
|
foreach($items as $i) {
|
|
/** @var ContractconfigItem $i */
|
|
if(!$i->contract_id) {
|
|
$i->setContractId($contract->id);
|
|
}
|
|
|
|
switch($i->name) {
|
|
case "bandwidth_down":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("Rate-Limit-Down"));
|
|
$i->value->save();
|
|
}
|
|
break;
|
|
case "bandwidth_up":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("Rate-Limit-Up"));
|
|
$i->value->save();
|
|
}
|
|
break;
|
|
case "radiususer_username":
|
|
if($ruser) {
|
|
$i->value->set($ruser->username);
|
|
$i->value->save();
|
|
}
|
|
break;
|
|
case "radiususer_password":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getPassword());
|
|
$i->value->save();
|
|
}
|
|
break;
|
|
case "radiususer_ipaddress":
|
|
case "ipv4_address":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("Framed-IP-Address"));
|
|
$i->value->save();
|
|
|
|
}
|
|
break;
|
|
case "radiususer_netmask":
|
|
case "ipv4_netmask":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("Framed-IP-Netmask"));
|
|
$i->value->save();
|
|
|
|
}
|
|
break;
|
|
case "radiususer_iproute":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("Framed-Route"));
|
|
$i->value->save();
|
|
|
|
}
|
|
break;
|
|
case "radiususer_dns1":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("MS-Primary-DNS-Server"));
|
|
$i->value->save();
|
|
|
|
}
|
|
break;
|
|
case "radiususer_dns2":
|
|
if($ruser) {
|
|
$i->value->set($ruser->getAttribute("MS-Secondary-DNS-Server"));
|
|
$i->value->save();
|
|
|
|
}
|
|
break;
|
|
default:
|
|
$this->log->debug(__METHOD__.": ".$ivt_customer->id.": Kein Wert für Configgroupitem ".$i->name." gefunden");
|
|
}
|
|
//var_dump($i, $i->value);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//var_dump($contract->configgroups);
|
|
//exit;
|
|
|
|
/*
|
|
* backbone (vlan)
|
|
* Radiususer
|
|
* Bandbreite
|
|
* IPv4
|
|
* SIP daten
|
|
* Voip
|
|
* Rufnummer
|
|
* Webhosting
|
|
*
|
|
*/
|
|
return true;
|
|
}
|
|
|
|
|
|
public function findOrder($ivt_contract) {
|
|
// find order
|
|
$ivt_customer = $ivt_contract->customer;
|
|
$ivt_product = $ivt_contract->product;
|
|
|
|
$productMatch = IvtProductMatchModel::getFirst(["ivt_product_id" => $ivt_contract->pid]);
|
|
if(!$productMatch) {
|
|
echo "Kein Match zu IVT Product " . $ivt_contract->pid . " gefunden.<br />\n";
|
|
exit;
|
|
}
|
|
|
|
$product = $productMatch->product;
|
|
|
|
$owner = AddressModel::getFirst(["customer_number" => $ivt_customer->id]);
|
|
if (!$owner) {
|
|
//var_dump($ivt_customer, $ivt_contract);
|
|
die("Address ".$ivt_customer->id." not found!");
|
|
}
|
|
|
|
if(array_key_exists($ivt_contract->id, $this->static_ivt_order_match)) {
|
|
$order_id = $this->static_ivt_order_match[$ivt_contract->id];
|
|
if($order_id === false) {
|
|
return false;
|
|
}
|
|
$order = new Order($order_id);
|
|
if(!$order->id) {
|
|
die("Manually specified order id ".$this->static_ivt_order_match[$ivt_contract->id]." not found for customer ".$ivt_customer->id);
|
|
}
|
|
return $order;
|
|
} else {
|
|
$order_count = OrderModel::count(["owner_id" => $owner->id, "finish_date" => true]);
|
|
$order_id = false;
|
|
|
|
if ($order_count > 1) {
|
|
$found_product = 0;
|
|
foreach(OrderModel::search(["owner_id" => $owner->id, "finish_date" => true]) as $o) {
|
|
// find the one with the right product
|
|
foreach(OrderProductModel::search(["order_id" => $o->id, "product_id" => $product->id]) as $op) {
|
|
$order_id = $op->id;
|
|
$found_product++;
|
|
}
|
|
if($found_product > 1) {
|
|
die("Mehr als eine Bestellung für ivt_customer ".$ivt_customer->id." (ivt contract ".$ivt_contract->id.") gefunden.\n" . print_r($owner, true));
|
|
}
|
|
}
|
|
$order = new Order($order_id);
|
|
if(!$order) {
|
|
die("Order $order_id not found");
|
|
}
|
|
return $order;
|
|
}
|
|
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function getRadiusUser($ivt_customer, $ivt_product, $contract) {
|
|
// find radius user (kundennummer in radius info feld
|
|
$radius = new RadiusDB_Client();
|
|
|
|
$user_count = 0;
|
|
$user = $radius->getUserByCustnum($ivt_customer->id);
|
|
if(!$user) {
|
|
$this->log->debug("User in Radius nicht gefunden Kundennummer ".$ivt_customer->id);
|
|
return false;
|
|
}
|
|
|
|
return $user;
|
|
|
|
// return data
|
|
}
|
|
|
|
private function addVoipData($ivt_customer_id, $contracts) {
|
|
//$this->log->debug("in addVoipData(): cid ".$ivt_customer_id);
|
|
|
|
/*if($ivt_customer_id == 1376) {
|
|
$this->log->debug("Not importing reseller voicenumbers for PROMETHEUS - Markus Paar [1376]");
|
|
return true;
|
|
}*/
|
|
|
|
$ported_in = [];
|
|
$voicenumbers = [];
|
|
// find voice numbers in ivt
|
|
$ivt_num_count = IvtCustomerTelephoneNrModel::count(["cid" => $ivt_customer_id]);
|
|
if(!$ivt_num_count) {
|
|
$this->log->debug("No voicenumber in ivt for cid ".$ivt_customer_id);
|
|
return false;
|
|
}
|
|
$this->log->debug("$ivt_num_count voicenumbers in ivt for cid ".$ivt_customer_id);
|
|
$ivtnumbers = IvtCustomerTelephoneNrModel::search(["cid" => $ivt_customer_id]);
|
|
|
|
if($ivt_customer_id == 1840) {
|
|
$injecting_ivtnumber = new IvtCustomerTelephoneNr();
|
|
$injecting_ivtnumber->number = "433152255410";
|
|
$ivtnumbers[] = $injecting_ivtnumber;
|
|
|
|
$injecting_ivtnumber = new IvtCustomerTelephoneNr();
|
|
$injecting_ivtnumber->number = "433159258910";
|
|
$ivtnumbers[] = $injecting_ivtnumber;
|
|
}
|
|
|
|
foreach($ivtnumbers as $ivtnum) {
|
|
$number = preg_replace('/^0043/', '43', $ivtnum->number);
|
|
if(!$number) continue;
|
|
|
|
|
|
|
|
// find number in block
|
|
$voicenumberblock = Voicenumberblock::findBlock($number);
|
|
if(!$voicenumberblock) {
|
|
$ported_in[] = $number;
|
|
continue;
|
|
}
|
|
|
|
if(!$voicenumberblock->isNumberInBlock($number)) {
|
|
echo "Block für Nummer $number enthält nummer nicht.\n";
|
|
exit;
|
|
}
|
|
|
|
$voicenumber = $voicenumberblock->getVoicenumber($number);
|
|
if(!$voicenumber) {
|
|
echo "Nummer $number gehört nicht in Block ".$voicenumberblock->id."\n";
|
|
exit;
|
|
}
|
|
$voicenumbers[] = $voicenumber;
|
|
}
|
|
|
|
$voice_contract = false;
|
|
|
|
// always create new voice contract for special customers
|
|
$new_nolink_contract = false;
|
|
if($ivt_customer_id == 1376) {
|
|
$new_nolink_contract = true;
|
|
/*echo "is PROMETHEUS number\n";
|
|
//if(in_array($number, [43313228451, 43313228406, 43720666572, 43313228400, 4331324890, 43720103806])) {
|
|
$prometheus_primary_contract = ContractModel::getFirst(["customer_number" => 1376]);
|
|
$voice_contract = $this->createVoiceContract($contracts, true);*/
|
|
} elseif($ivt_customer_id == 1840) {
|
|
$new_nolink_contract = true;
|
|
/*//if(in_array($number, [433152255410, 433159258910])) {
|
|
$lugitsch_primary_contract = ContractModel::getFirst(["customer_number" => 1840]);
|
|
$voice_contract = $this->createVoiceContract($contracts, true);*/
|
|
}
|
|
|
|
if(!$new_nolink_contract) {
|
|
// try finding voice contract, otherwise create it
|
|
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, $new_nolink_contract);
|
|
}
|
|
|
|
|
|
|
|
|
|
//var_dump($voice_contract);exit;
|
|
|
|
// put all numbers in $all_numbers array
|
|
$all_numbers = [];
|
|
foreach($voicenumbers as $voicenumber) {
|
|
$all_numbers[] = $voicenumber;
|
|
}
|
|
|
|
foreach($ported_in as $voicenumber) {
|
|
// create single number if it does not exist
|
|
$ported_number = VoicenumberModel::getFirst(["number" => $voicenumber]);
|
|
if(!$ported_number) {
|
|
$num_data = [];
|
|
if($voice_contract->orderproduct_id) {
|
|
$num_data["orderproduct_id"] = $voice_contract->orderproduct_id;
|
|
}
|
|
$ported_number = VoicenumberModel::create([
|
|
"voicenumberblock_id" => null,
|
|
"number" => $voicenumber,
|
|
"port_in_date" => date("U"),
|
|
"ported_in" => 1,
|
|
"ported_from" => "A1 Telekom Austria",
|
|
]);
|
|
} else {
|
|
die("Importierte Nummer gibts bereits $voicenumber");
|
|
}
|
|
$all_numbers[] = $ported_number;
|
|
}
|
|
|
|
$contract_new_numbers = [];
|
|
$contract_matchcode_numbers = [];
|
|
// update all numbers
|
|
foreach($all_numbers as $voicenumber) {
|
|
$voicenumber->contract_id = $voice_contract->id;
|
|
$voicenumber->active = 1;
|
|
$voicenumber->activated_date = date("U");
|
|
$voicenumber->routing = "kolmisoft";
|
|
|
|
$voicenumber->comment = "IVT Import ".date("Y-m-d");
|
|
|
|
if(!$voicenumber->save()) {
|
|
die("Fehler beim Speichern der neuen Nummer ".$voicenumber->number);
|
|
}
|
|
$contract_new_numbers[] = $voicenumber->number;
|
|
$contract_matchcode_numbers[] = preg_replace('/^\+?43 */', '0', $voicenumber->number);
|
|
}
|
|
|
|
|
|
if(array_key_exists("voicenumberblock_voicenumber", $voice_contract->configvalues)) {
|
|
$confitem = $voice_contract->configvalues["voicenumberblock_voicenumber"];
|
|
} else {
|
|
$confitem = ContractconfigItemModel::getFirst(["name" => "voicenumberblock_voicenumber"]);
|
|
$confitem->setContractId($voice_contract->id);
|
|
}
|
|
|
|
$confitem->value->set($contract_new_numbers);
|
|
$confitem->save();
|
|
|
|
// import Voiceplan
|
|
$this->getVoiceplan($voice_contract);
|
|
|
|
//$voice_contract->matchcode = implode(", ", $contract_matchcode_numbers);
|
|
$voice_contract->matchcode = "";
|
|
$voice_contract->save();
|
|
|
|
|
|
|
|
// return voicenumber object
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @var Contract $contract
|
|
*/
|
|
private function getVoiceplan($contract) {
|
|
$cid = $contract->owner->customer_number;
|
|
$ivt_customer = new IvtCustomer($cid);
|
|
if(!$ivt_customer) {
|
|
die(__METHOD__.": Ivt Customer nicht gefunden\n");
|
|
}
|
|
|
|
$ivt_plan = $ivt_customer->telephony_pricelist;
|
|
if(!array_key_exists($ivt_plan, $this->ivt_to_voiceplan)) {
|
|
die("Invalid Ivt Voiceplan: ".$ivt_plan."\n");
|
|
}
|
|
|
|
$new_plan = $this->ivt_to_voiceplan[$ivt_plan];
|
|
$contract->setConfigValue("voicenumberblock_voiceplan_id", $new_plan);
|
|
/*$voiceplan_item = $contract->getConfigValue("voicenumberblock_voiceplan_id");
|
|
$voiceplan_item->set($new_plan);
|
|
$voiceplan_item->save();
|
|
*/
|
|
return true;
|
|
|
|
}
|
|
|
|
private function createVoiceContract($contracts, $nolink = false) {
|
|
// find rufnummer only product (residential or business)
|
|
$product = new Product(101); // Telefonie (nur Rufnummer - Privat)
|
|
$sla_id = 4;
|
|
foreach($contracts as $contract) {
|
|
if($contract->sla_id < 4) {
|
|
$product = new Product(102); // Telefonie (nur Rufnummer - Business)
|
|
$sla_id = 3;
|
|
}
|
|
}
|
|
|
|
// first contract
|
|
$fc = $contracts[0];
|
|
|
|
$data = [];
|
|
$data["orderproduct_id"] = null;
|
|
$data["owner_id"] = $fc->owner_id;
|
|
$data["billingaddress_id"] = $fc->billingaddress_id;
|
|
$data["termination_id"] = null;
|
|
$data["product_id"] = $product->id;
|
|
$data["product_name"] = "Telefonie (nur Rufnummer)";
|
|
$data["product_info"] = null;
|
|
$data["amount"] = 1;
|
|
$data["sla_id"] = $sla_id;
|
|
$data["product_external"] = 0;
|
|
$data["product_external_id"] = null;
|
|
$data["price"] = $product->price;
|
|
$data["price_setup"] = $product->price_setup;
|
|
$data["price_nne"] = $product->price_nne;
|
|
$data["price_nbe"] = $product->price_nbe;
|
|
$data["vatgroup_id"] = $product->vatgroup_id;
|
|
$data["billing_delay"] = $product->billing_delay;
|
|
$data["billing_period"] = $product->billing_period;
|
|
$data["order_date"] = $fc->order_date;
|
|
$data["finish_date"] = $fc->finish_date;
|
|
$data["finish_date_by"] = 1;
|
|
$data["note"] = null;
|
|
|
|
$voice_contract = ContractModel::create($data);
|
|
if(!$voice_contract->save()) {
|
|
$this->log->error("Unable to create voice contract for cust " . $fc->customer_number);
|
|
exit;
|
|
}
|
|
|
|
// link to all $contracts
|
|
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();
|
|
}
|
|
}
|
|
|
|
return $voice_contract;
|
|
|
|
}
|
|
|
|
private function createAddressFromIvtCustomer(IvtCustomer $cust) {
|
|
$address_data['customer_number'] = $cust->id;
|
|
$address_data['spin'] = $cust->MandatID;
|
|
$address_data['company'] = $cust->company;
|
|
$address_data['firstname'] = $cust->firstname;
|
|
$address_data['lastname'] = $cust->surname;
|
|
|
|
if($cust->company && !$cust->firstname && $cust->company == $cust->surname) {
|
|
$address_data["firstname"] = "";
|
|
$address_data["lastname"] = "";
|
|
}
|
|
|
|
$address_data['street'] = $cust->street;
|
|
if(strlen($cust->housenumber)) {
|
|
$address_data['street'] .= " " . $cust->housenumber;
|
|
}
|
|
$address_data['zip'] = $cust->zip;
|
|
$address_data['city'] = $cust->location;
|
|
$address_data['country_id'] = 163; // 163 - Austria
|
|
$address_data['phone'] = $cust->phone;
|
|
$address_data['fax'] = "";
|
|
$address_data['mobile'] = "";
|
|
$address_data['email'] = $cust->email;
|
|
$address_data['note'] = $cust->extrainfo;
|
|
|
|
$address_data['uid'] = ($cust->UID) ? $cust->UID : null;
|
|
$address_data["bank_account_bank"] = "";
|
|
$address_data["bank_account_owner"] = ($cust->company) ? $cust->company : $cust->firstname . " " . $cust->surname;
|
|
$address_data["bank_account_iban"] = $cust->IBAN;
|
|
$address_data["bank_account_bic"] = $cust->BIC;
|
|
|
|
if($cust->payment == 0) { // 0 = sepa / 1 = rechnung
|
|
$address_data['billing_type'] = "sepa";
|
|
} else {
|
|
$address_data['billing_type'] = "invoice";
|
|
}
|
|
|
|
if($cust->paper_invoice == 1) {
|
|
$address_data["billing_delivery"] = "paper";
|
|
} else {
|
|
$address_data["billing_delivery"] = "email";
|
|
}
|
|
|
|
if($cust->accept_adver == 2) {
|
|
$address_data["allow_contact"] = 1;
|
|
} else {
|
|
$address_data["allow_contact"] = 0;
|
|
}
|
|
|
|
if($cust->accept_info == 2) {
|
|
$address_data["allow_spin"] = 1;
|
|
} else {
|
|
$address_data["allow_spin"] = 0;
|
|
}
|
|
|
|
$address_data['create_by'] = 1;
|
|
$address_data['edit_by'] = 1;
|
|
|
|
$address = AddressModel::create($address_data);
|
|
|
|
if(!$address->save()) {
|
|
var_dump($address);
|
|
return false;
|
|
}
|
|
return $address;
|
|
}
|
|
|
|
private function getNewSlaId($ivt_sla_id) {
|
|
$sla_trans = [
|
|
0 => 4, // ivt undefined => thetool residential
|
|
1 => 4, // ivt undefined => thetool residential
|
|
2 => 4, // ivt residential => thetool residential
|
|
3 => 3, // ivt 8h/5T - Next Business Day => thetool 8h/5T - Next Business Day
|
|
4 => 1, // ivt 12h/6T - Working Hours => thetool 12h/6T - Working Hours
|
|
5 => 2 // ivt 24h/7T - 2h/6h Emergency => thetool 24h/7T - 2h/6h Emergency
|
|
];
|
|
|
|
if(array_key_exists($ivt_sla_id, $sla_trans)) {
|
|
return $sla_trans[$ivt_sla_id];
|
|
}
|
|
|
|
return 4; // if ivt sla is unknown -> return residential
|
|
}
|
|
}
|
|
|