WIP Contract 2024-05-06

This commit is contained in:
Frank Schubert
2024-05-07 16:31:01 +02:00
parent 6002876343
commit 4b52599209
4 changed files with 211 additions and 81 deletions

View File

@@ -77,7 +77,7 @@
</table>
<div>
<a href="<?=self::getUrl("Admin", "ivtAdminImport", ["doit" => 1])?>">
<a href="<?=self::getUrl("Admin", "ivtContractImport", ["doit" => 1])?>">
<button class="btn btn-primary" id="doitbutton"><i
class="far fa-fw fa-exclamation-circle"></i> Jetzt importieren
</button>

View File

@@ -52,6 +52,11 @@ class AddressModel {
$me = new User();
$me->loadMe();
if($model->phone == null) $model->phone = "";
if($model->fax == null) $model->fax = "";
if($model->mobile == null) $model->mobile = "";
if($model->email == null) $model->email = "";
if (!is_numeric($model->create_by) && !$model->create_by) {
$model->create_by = $me->id;
}

View File

@@ -14,9 +14,15 @@ class Admin_IvtContractImport {
$data = [];
$data['ignore'] = 0;
$data["contracts"] = [];
$new_contracts = [];
$last_cid = 0;
$i = 0;
foreach(IvtCustomerTelephoneNrModel::getAll("cid") as $ivt_contract) {
if($i > 100) break;
foreach(IvtCustomerProductModel::search(["lasdt_date>", "2024-04-01"], "cid") as $ivt_contract) {
if($i > 150 && $ivt_contract->cid != $last_cid) break; // only break after the last ivtcontract of this customer
$last_cid = $ivt_contract->cid;
$i++;
$neu = [];
$orderproduct = false;
@@ -25,11 +31,23 @@ class Admin_IvtContractImport {
$ivt_customer = $ivt_contract->customer;
$ivt_product = $ivt_contract->product;
if(!$this->checkIvtCustomer($ivt_customer)) {
/*
* check ivt customer and get Billingaddress
*/
$billingaddress_id = false;
$customer_check_return = $this->checkIvtCustomer($ivt_customer);
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";
exit;
}
$billingaddress_id = $customer_check_return["billingaddress_id"];
/*
* 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";
@@ -41,6 +59,12 @@ class Admin_IvtContractImport {
exit;
}
$ip = $productMatch->ivtproduct;
$product = $productMatch->product;
/*
* get or create customer
*/
$customer = AddressModel::getFirst(["customer_number" => $ivt_contract->cid]);
/*if($ivt_contract->id == 3035) {
var_dump($ivt_contract->cid, $customer);
@@ -55,8 +79,7 @@ class Admin_IvtContractImport {
}
}
$ip = $productMatch->ivtproduct;
$product = $productMatch->product;
/*
// check if contract was imported already
$existing_contract = ContractModel::getFirst(["imported_from" => "ivt", "imported_data" => $ivt_contract->id]);
@@ -74,7 +97,7 @@ class Admin_IvtContractImport {
$contract_data = [];
$contract_data['owner_id'] = $customer->id;
$contract_data['billingaddress_id'] = $customer->id;
$contract_data['billingaddress_id'] = $billingaddress_id;
$contract_data['product_id'] = $product->id;
$contract_data['product_name'] = $ip->name;
$contract_data['amount'] = 1;
@@ -82,6 +105,8 @@ class Admin_IvtContractImport {
$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['order_date'] = $finish_date->getTimestamp();
$contract_data['finish_date'] = $finish_date->getTimestamp();
$contract_data['finish_date_by'] = 1;
$contract_data['imported_from'] = "ivt";
@@ -113,15 +138,10 @@ class Admin_IvtContractImport {
$ignore = true;
}
// find thetool Order
/*
* Try to match ivt contract to thetool Order
*/
$order = OrderModel::getFirst(["owner_id" => $customer->id]);
/*if($ivt_contract->id == 3035) {
var_dump($order);
}*/
/*if($ivt_contract->id == 3035) {
var_dump($order);exit;
}*/
if($order) {
foreach($order->products as $orderproduct) {
@@ -138,11 +158,16 @@ class Admin_IvtContractImport {
} else {
//$this->log->debug("Keine thetool order für ivt customer_product ".$ivt_contract->id);
}
/*if($ivt_contract->id == 3035) {
var_dump($order, $orderproduct);
}*/
/*
* Create Conttract (dont save yet)
*/
$contract = ContractModel::create($contract_data);
$contract->matchcode = $contract->generateMatchcode();
if(trim($ivt_contract->comment)) {
$contract->matchcode = trim($ivt_contract->comment);
} else {
$contract->matchcode = $contract->generateMatchcode();
}
/*if($ivt_contract->id == 3035) {
var_dump($contract);
}*/
@@ -155,6 +180,11 @@ class Admin_IvtContractImport {
exit;
}
if(!array_key_exists($ivt_customer->id, $new_contracts)) {
$new_contracts[$ivt_customer->id] = [];
}
$new_contracts[$ivt_customer->id][] = $contract_id;
// create journal entry
$journal = ContractjournalModel::create([
@@ -173,7 +203,7 @@ class Admin_IvtContractImport {
}
$contractconfig = $this->importContractconfig($ivt_customer, $ivt_product, $contract, ($doit && !$ignore));
var_dump($contractconfig);exit;
//var_dump($contractconfig);exit;
$data["contracts"][] = $neu;
if($neu['action'] == "ignore") {
@@ -184,39 +214,72 @@ class Admin_IvtContractImport {
var_dump($neu);exit;
}*/
}
if($doit) {
//var_dump($new_contracts[1789]);exit;
foreach($new_contracts as $ivt_customer_id => $contracts) {
if(count($contracts) < 2) continue;
$prev_contracts = [];
foreach($contracts as $contract_id) {
if(!$prev_contracts) {
$prev_contracts[] = $contract_id;
continue;
}
foreach($prev_contracts as $prev) {
if (ContractLinkModel::getFirst(["contract_id" => $contract_id, "origin_contract_id" => $prev])) {
continue;
}
$link = ContractLinkModel::create([
'contract_id' => $contract_id,
'origin_contract_id' => $prev,
'type' => 'link'
]);
$link->save();
}
$prev_contracts[] = $contract_id;
}
}
}
return $data;
}
private function checkIvtCustomer($ivt_customer, $doit = false) {
private function checkIvtCustomer($ivt_customer, $doit = false)
{
$return = [];
// create customer if not exists
$ivt_custnum = $ivt_customer->id;
if(!$ivt_custnum) return false;
if (!$ivt_custnum) return false;
// sync billing address
$owner = AddressModel::getFirst(["customer_number" => $ivt_custnum]);
if(!$owner) {
if (!$owner) {
//var_dump($ivt_customer, $ivt_contract);
die("Address $ivt_custnum not found!");
}
// find order
$order_count = OrderModel::count(["owner_id" => $owner->id, "finish_date" => true]);
if($order_count > 1) {
die("Mehr als eine Bestellung für ivt_customer $ivt_custnum gefunden.\n".print_r($owner, true));
if ($order_count > 1) {
die("Mehr als eine Bestellung für ivt_customer $ivt_custnum gefunden.\n" . print_r($owner, true));
}
$order = OrderModel::getFirst(["owner_id" => $owner->id, "finish_date" => true]);
if(!$order) {
$this->log->debug(__METHOD__.": No order for ivt customer $ivt_custnum");
return true;
if (!$order) {
$this->log->debug(__METHOD__ . ": No order for ivt customer $ivt_custnum");
return ["billingaddress_id" => $owner->id];
}
// look for billingaddress_id in order and use it
if($order->billingaddress_id) {
if($order->billingaddress_id == $ivt_custnum) {
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) {
if (!$billingaddress->id) {
die("Billingaddress " . $order->billingaddress_id . " does not exist (order " . $order->id . "; ivt customer $ivt_custnum)");
}
$return["billingaddress_id"] = $billingaddress->id;
@@ -227,41 +290,65 @@ class Admin_IvtContractImport {
// create new billingaddress if nessecary
$new_billing = [];
foreach(["company", "firstname", "lastname", "street", "zip", "city", "phone", "email", "uid", "billing_type", "billing_delivery",
"bank_account_bank", "bank_account_owner", "bank_account_iban", "bank_account_bic"] as $field) {
$new_billing[$field] = $owner->$field;
}
if($ivt_customer->company) {
if($ivt_customer->company != $owner->company) $new_billing["company"] = $ivt_customer->company;
if ($ivt_customer->company) {
if ($ivt_customer->company != $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 ($ivt_customer->firstname != $owner->firstname) $new_billing["firstname"] = $ivt_customer->firstname;
if ($ivt_customer->surname != $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 ($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;
$new_billing["billing_type"] = ($ivt_customer->payment == 1) ? "invoice" : "sepa";
$new_billing["billing_delivery"] = ($ivt_customer->paper_invoice == 1) ? "paper" : "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";
}
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;
if($ivt_customer->bank_account_iban != $owner->bank_account_iban) $new_billing["bank_account_iban"] = $ivt_customer->bank_account_iban;
if($ivt_customer->bank_account_bic != $owner->bank_account_bic) $new_billing["bank_account_bic"] = $ivt_customer->bank_account_bic;
//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));
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;
}
$billingaddress = AddressModel::create($new_billing);
$missing_fields = [];
$create = false;
foreach(["company", "firstname", "lastname", "street", "zip", "city", "phone", "email", "uid", "billing_type", "billing_delivery",
"bank_account_iban", "bank_account_bic"] as $field) {
if(!array_key_exists($field, $new_billing)) {
$new_billing[$field] = $owner->$field;
} else {
if($field == "email" && $field = "dummy@xinon.at") continue;
$missing_fields[] = $field;
$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;
}
// TODO: sync bank data
return $return;
return ["billingaddress_id" => $owner->id];
}
private function checkIvtProduct($product) {
@@ -282,7 +369,7 @@ class Admin_IvtContractImport {
if(!is_array($contract->configgroups) || !count($contract->configgroups)) {
echo "no contract config";
//echo "no contract config";
return true;
}
@@ -295,7 +382,7 @@ class Admin_IvtContractImport {
// lookup radius data
$ruser = $this->getRadiusUser($ivt_customer, $ivt_product, $contract);
// lookup voip data
$voip_data = $this->getVoipData($ivt_customer, $ivt_product, $contract);
//$voip_data = $this->getVoipData($ivt_customer, $ivt_product, $contract);
foreach($contract->configgroups as $cgroup) {
@@ -321,33 +408,47 @@ class Admin_IvtContractImport {
break;
case "radiususer_username":
$i->value->set($ruser->username);
$i->value->save();
if($ruser) {
$i->value->set($ruser->username);
$i->value->save();
}
break;
case "radiususer_password":
$i->value->set($ruser->getPassword());
$i->value->save();
break;
if($ruser) {
$i->value->set($ruser->getPassword());
$i->value->save();
break;
}
case "radiususer_ipaddress":
$i->value->set($ruser->getAttribute("Framed-IP-Address"));
$i->value->save();
break;
if($ruser) {
$i->value->set($ruser->getAttribute("Framed-IP-Address"));
$i->value->save();
break;
}
case "radiususer_netmask":
$i->value->set($ruser->getAttribute("Framed-IP-Netmask"));
$i->value->save();
break;
if($ruser) {
$i->value->set($ruser->getAttribute("Framed-IP-Netmask"));
$i->value->save();
break;
}
case "radiususer_iproute":
$i->value->set($ruser->getAttribute("Framed-Route"));
$i->value->save();
break;
if($ruser) {
$i->value->set($ruser->getAttribute("Framed-Route"));
$i->value->save();
break;
}
case "radiususer_dns1":
$i->value->set($ruser->getAttribute("MS-Primary-DNS-Server"));
$i->value->save();
break;
if($ruser) {
$i->value->set($ruser->getAttribute("MS-Primary-DNS-Server"));
$i->value->save();
break;
}
case "radiususer_dns2":
$i->value->set($ruser->getAttribute("MS-Secondary-DNS-Server"));
$i->value->save();
break;
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");
}
@@ -358,8 +459,8 @@ class Admin_IvtContractImport {
}
var_dump($contract->configgroups);
exit;
//var_dump($contract->configgroups);
//exit;
/*
* backbone (vlan)
@@ -481,5 +582,22 @@ class Admin_IvtContractImport {
}
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
}
}

View File

@@ -117,6 +117,13 @@ class IvtCustomerProductModel {
$where .= " AND sid=$sid";
}
}
if(array_key_exists("last_date>", $filter)) {
$last_date = $filter['last_date>'];
if(is_numeric($last_date)) {
$where .= " AND last_date >=$last_date";
}
}
//var_dump($filter, $where);exit;
return $where;