Files
thetool/application/Admin/functions/IvtContractImport.php
2024-05-06 13:24:25 +02:00

486 lines
18 KiB
PHP

<?php
class Admin_IvtContractImport {
private $request;
private $log;
public function __construct($request) {
$this->request = $request;
$this->log = mfLoghandler::singleton();
}
public function run($doit = false) {
echo "running...<br />\n";
$data = [];
$data['ignore'] = 0;
$data["contracts"] = [];
$i = 0;
foreach(IvtCustomerTelephoneNrModel::getAll("cid") as $ivt_contract) {
if($i > 100) break;
$i++;
$neu = [];
$orderproduct = false;
$orderproduct_id = false;
$ivt_customer = $ivt_contract->customer;
$ivt_product = $ivt_contract->product;
if(!$this->checkIvtCustomer($ivt_customer)) {
echo "IVT Customer " . $ivt_contract->cid . " nicht im tool<br />\n";
exit;
}
$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;
}
$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;
}
}
$ip = $productMatch->ivtproduct;
$product = $productMatch->product;
/*
// 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;
}
*/
$finish_date = new DateTime($ivt_contract->created);
$finish_date->modify("+2 hours");
$contract_data = [];
$contract_data['owner_id'] = $customer->id;
$contract_data['billingaddress_id'] = $customer->id;
$contract_data['product_id'] = $product->id;
$contract_data['product_name'] = $ip->name;
$contract_data['amount'] = 1;
$contract_data['price'] = $ip->price;
$contract_data['price_setup'] = 0;
$contract_data['price_nne'] = 0;
$contract_data['price_nbe'] = 0;
$contract_data['finish_date'] = $finish_date->getTimestamp();
$contract_data['finish_date_by'] = 1;
$contract_data['imported_from'] = "ivt";
$contract_data['imported_data'] = $ivt_contract->id;
switch($ip->interval) {
case 0:
$contract_data['billing_period'] = 1;
break;
case 1:
$contract_data['billing_period'] = 12;
break;
}
$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) {
$contract_data['price'] = 0;
$contract_data['price_setup'] = $ip->price;
$contract_data['billing_period'] = 0;
$neu['action'] = "ignore";
$ignore = true;
}
// find 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) {
if($orderproduct->product_id == $product->id) {
$orderproduct_id = $orderproduct->id;
}
}
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);
}
/*if($ivt_contract->id == 3035) {
var_dump($order, $orderproduct);
}*/
$contract = ContractModel::create($contract_data);
$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;
}
// create journal entry
$journal = ContractjournalModel::create([
'contract_id' => $contract_id,
'type' => "created_from",
'value' => "import",
'text' => "IVT"
]);
$journal_id = $journal->save();
if(!$journal_id) {
echo "Fehler beim Journal erstellen.<br />\n";
exit;
}
// import Contractconfig
}
$contractconfig = $this->importContractconfig($ivt_customer, $ivt_product, $contract, ($doit && !$ignore));
var_dump($contractconfig);exit;
$data["contracts"][] = $neu;
if($neu['action'] == "ignore") {
$data['ignore']++;
}
/*if($ivt_contract->id == 3035) {
var_dump($neu);exit;
}*/
}
return $data;
}
private function checkIvtCustomer($ivt_customer, $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) {
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));
}
$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;
}
// 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 no billingaddress_id in order, compare address of Address and ivt_customer and
// 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;
} 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->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->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;
$billingaddress = AddressModel::create($new_billing);
// TODO: sync bank data
return $return;
}
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);
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
$voip_data = $this->getVoipData($ivt_customer, $ivt_product, $contract);
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);
}
//var_dump($i, $i->value);exit;
switch($i->name) {
case "bandwidth_down":
$i->value->set($product->attributes["bw_down"]->value);
$i->value->save();
break;
case "bandwidth_up":
$i->value->set($product->attributes["bw_up"]->value);
$i->value->save();
break;
case "radiususer_username":
$i->value->set($ruser->username);
$i->value->save();
break;
case "radiususer_password":
$i->value->set($ruser->getPassword());
$i->value->save();
break;
case "radiususer_ipaddress":
$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;
case "radiususer_iproute":
$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;
case "radiususer_dns2":
$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
*
*/
}
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 getVoipData($ivt_customer, $ivt_product, $contract) {
// find voice number in ivt
$ivtnum = IvtCustomerTelephoneNrModel::getFirst(["cid" => $ivt_customer->id]);
if(!$ivtnum) return true;
$number = preg_replace('/^0043/', '43', $ivtnum->number);
if(!$number) return true;
// find number in block
$voicenumberblock = Voicenumberblock::findBlock($number);
if(!$voicenumberblock) return false;
if(!$voicenumberblock->isNumberInBlock($number)) {
die("Block für Nummer $number enthält nummer nicht.");
}
$voicenumber = $voicenumberblock->getVoicenumber($number);
if(!$voicenumber) {
die("Nummer $number gehört nicht in Block ".$voicenumberblock->id);
}
// return voicenumber object
return $voicenumber;
}
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'] = "";
$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;
}
}