IVT Contract Import WIP

This commit is contained in:
Frank Schubert
2023-11-02 16:05:40 +01:00
parent 90f65abd6f
commit e5f7b59ab4
14 changed files with 610 additions and 7 deletions

View File

@@ -312,7 +312,7 @@ class AddressModel {
/*
* Address Type
*/
if(is_array($filter['addresstype']) && count($filter['addresstype'])) {
if(array_key_exists("addresstype", $filter) && is_array($filter['addresstype']) && count($filter['addresstype'])) {
$at = $filter['addresstype'];
$in = [];
foreach(TT_ROLES as $role) {

View File

@@ -150,4 +150,27 @@ class AdminController extends mfBaseController {
$this->layout()->set("products", $products);
}
protected function ivtAdminImportAction() {
exit;
$doit = false;
if($this->request->doit == 1) {
$doit = true;
}
require_once(realpath(dirname(__FILE__)."/functions")."/IvtAdminImport.php");
if(class_exists("Admin_IvtAdminImport")) {
$import = new Admin_IvtAdminImport($this->request);
$data = $import->run($doit);
$this->layout()->setTemplate("Admin/IvtAdminImport");
$this->layout()->set("data", $data);
if($doit) {
$this->layout()->setFlash((count($data['contracts']) - $data['ignore'])." Contracts aus IVT importiert!", "success");
}
}
}
}

View File

@@ -0,0 +1,305 @@
<?php
class Admin_IvtAdminImport {
private $request;
public function __construct($request) {
$this->request = $request;
}
public function run($doit = false) {
echo "running...<br />\n";
$data = [];
$data['ignore'] = 0;
$data["contracts"] = [];
foreach(IvtCustomerProductModel::search(["pid" => 784]) as $ivt_contract) {
$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)) {
exit;
}
$customer = AddressModel::getFirst(["customer_number" => $ivt_contract->cid]);
$ip = $productMatch->ivtproduct;
$product = $productMatch->product;
if(!$customer->id) {
echo "Kein thetool Kunde zu IVT customer ".$ivt_contract->cid." gefunden -> wird angelegt<br />\n";
$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'] = $customer->id;
$contract_data['product_id'] = $product->id;
$contract_data['product_name'] = $ip->name;
$contract_data['matchcode'] = "";
$contract_data['amount'] = 1;
$contract_data['price'] = $ip->price;
$contract_data['price_setup'] = 0;
$contract_data['finish_date'] = mktime(2,0,0,date("m"),1,date("Y"));
$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["ivtproduct"] = $ip;
$neu["product"] = $product;
$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;
}
$contract = ContractModel::create($contract_data);
$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
$this->importContractconfig($ivt_customer, $ivt_product, $contract);
}
$data["contracts"][] = $neu;
if($neu['action'] == "ignore") {
$data['ignore']++;
}
}
return $data;
}
private function checkIvtCustomer($customer) {
// create customer if not exists
$old_custnum = $customer->id;
if(!$old_custnum) return false;
// TODO: sync bank data
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) {
var_dump($contract, $contract->product, $contract->configgroups);
if(!is_array($contract->configgroups) || !count($contract->configgroups)) {
return true;
}
$product = $contract->product;
if(!$product->id) {
echo "Produkt nicht gefunden für Contract ".$contract->id."<br />\n";
exit;
}
// lookup radius data
$radius_data = $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);
foreach($items as $i) {
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($radius_data["username"]);
$i->value->sav();
break;
case "radiususer_password":
$i->value->set($radius_data["username"]);
$i->value->sav();
break;
case "radiususer_ipaddress":
$i->value->set($radius_data["ipaddress"]);
$i->value->sav();
break;
case "radiususer_ipprefix":
$i->value->set($radius_data["subnetmask"]);
$i->value->sav();
break;
}
}
}
exit;
/*
* backbone (vlan)
* Radiususer
* Bandbreite
* IPv4
* SIP daten
* Voip
* Rufnummer
* Webhosting
*
*/
}
private function getRadiusUser($ivt_customer, $ivt_product, $contract) {
// find radius user
// return data
}
private function getVoipData($ivt_customer, $ivt_product, $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'] = "";
$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;
}
}

View File

@@ -24,6 +24,8 @@ class ContractModel {
public $finish_date_by;
public $cancel_date;
public $cancel_date_by;
public $imported_from;
public $imported_data;
public $note = null;
public $create_by = null;
@@ -152,13 +154,18 @@ class ContractModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM Contract
$sql = "SELECT COUNT(*) as cnt FROM (
SELECT Contract.* FROM Contract
LEFT JOIN Address ON (Contract.owner_id = Address.id)
LEFT JOIN OrderProduct ON (Contract.orderproduct_id = OrderProduct.id)
LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id)
LEFT JOIN Product ON (Contract.product_id = Product.id)
WHERE $where
GROUP BY Contract.id";
AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())
GROUP BY Contract.id
) contract";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
@@ -317,6 +324,20 @@ class ContractModel {
}
}
if(array_key_exists("imported_from", $filter)) {
$imported_from = FronkDB::singleton()->escape($filter["imported_from"]);
if($imported_from) {
$where .= " AND Contract.imported_from like '$imported_from'";
}
}
if(array_key_exists("imported_data", $filter)) {
$imported_data = FronkDB::singleton()->escape($filter["imported_data"]);
if($imported_data) {
$where .= " AND Contract.imported_data like '$imported_data'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}

View File

@@ -58,6 +58,7 @@ class ContractconfigItem extends mfBaseModel {
$value->item_id = $this->id;
$value->contract_id = $this->contract_id;
$value->create_by = $me->id;
$value->edit_by = $me->id;
}
//var_dump($value);exit;
$this->value = $value;

View File

@@ -3,6 +3,7 @@
class IvtCustomerProduct extends mfBaseModel {
protected $forcestr = [];
private $customer;
private $product;
/**
* Takes ID or DB row as arguments

View File

@@ -22,6 +22,7 @@ class IvtProduct extends mfBaseModel {
} elseif(is_object($_)) {
$this->load($_);
}
}
public function save() {