WIP Contract/Billing 2024-07-04

This commit is contained in:
Frank Schubert
2024-07-05 10:45:22 +02:00
parent 0640babfa3
commit 92edb9c812
27 changed files with 8043 additions and 116 deletions

View File

@@ -592,7 +592,7 @@ class Address extends mfBaseModel {
}*/
if($name == "links") {
$links = AddressLinkModel::search(['origin_address_id' => $this->id]);
$links = AddressLinkModel::search(['address_or_origin_id' => $this->id]);
//var_dump($links);exit;
foreach($links as $link) {
if(!array_key_exists($link->type, $this->links)) {

View File

@@ -91,7 +91,7 @@ class AddressLinkModel {
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
@@ -126,6 +126,13 @@ class AddressLinkModel {
}
}
if(array_key_exists("address_or_origin_id", $filter)) {
$address_or_origin_id = $filter["address_or_origin_id"];
if(is_numeric($address_or_origin_id)) {
$where .= " AND (address_id=$address_or_origin_id OR origin_address_id=$address_or_origin_id)";
}
}
if(array_key_exists("type", $filter)) {
switch($filter['type']) {
case "employee":

View File

@@ -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) {
if ($order && $order->billingaddress_id) {
$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;
}
$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;
}
}
$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;
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);
}
}
//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;
@@ -868,6 +1024,7 @@ class Admin_IvtContractImport {
}
// link to all $contracts
if(!$nolink) {
foreach($contracts as $contract) {
if(ContractLinkModel::getFirst(["contract_id" => $voice_contract->id, "origin_contract_id" => $contract->id])) {
continue;
@@ -879,6 +1036,7 @@ class Admin_IvtContractImport {
]);
$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'] = "";

View File

@@ -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;

View File

@@ -3,6 +3,7 @@
class Billing extends mfBaseModel {
protected $forcestr = ["product_name","product_info","matchcode"];
private $contract;
private $vatgroup;

View File

@@ -109,7 +109,7 @@ class BillingController extends mfBaseController {
$now_day = date("d");
//$now_year = 2024;
//$now_month = 6;
$now_day = 3; // XXX for debugging only, must be removed
//$now_day = 3; // XXX for debugging only, must be removed
// XXX only for 1st Billing after IVT Import
//$yearly_not_before = new DateTime("$now_year-$now_month-01");
@@ -354,13 +354,13 @@ class BillingController extends mfBaseController {
$data["owner_id"] = $contract->owner_id;
$data["billingaddress_id"] = ($contract->billingaddress_id) ? $contract->billingaddress_id : $contract->owner_id;
$data["customer_number"] = $contract->owner->customer_number;
$data["fibu_account_number"] = $contract->owner->fibu_account_number;
$data["company"] = $billingaddress->company;
$data["firstname"] = $billingaddress->firstname;
$data["lastname"] = $billingaddress->lastname;
$data["street"] = $billingaddress->street;
$data["zip"] = $billingaddress->zip;
$data["city"] = $billingaddress->city;
$data["country"] = $billingaddress->country->name;
$data["email"] = $billingaddress->email;
$data["uid"] = $billingaddress->uid;
$data["billing_type"] = $billing_type;
@@ -376,9 +376,28 @@ class BillingController extends mfBaseController {
$data["amount"] = $contract->amount;
$data["price"] = $price;
$data["price_setup"] = $price_setup;
$data["vatrate"] = $contract->vatrate;
$data["billing_period"] = $contract->billing_period;
if(!$contract->billingaddress->country_id) {
$billcountry = CountryModel::getFirst(["isocode" => TT_HOMECOUNTRY_ISOCODE]);
} else {
$billcountry = $contract->billingaddress->country;
}
$vatgroup = $contract->vatgroup;
$vatarea = "domestic";
if($billcountry->isocode != TT_HOMECOUNTRY_ISOCODE && $billcountry->is_eu) {
$vatarea = "eu";
} if($billcountry->isocode != TT_HOMECOUNTRY_ISOCODE && !$billcountry->is_eu) {
$vatarea = "other";
}
$data["country"] = $billcountry->name;
$data["vatrate"] = $vatgroup->rates[$vatarea]->rate;
$data["vatgroup_id"] = $contract->vatgroup_id;
$data["vatarea"] = $vatarea;
$billing = BillingModel::create($data);
if (!$billing->save()) {
var_dump($billing);

View File

@@ -8,6 +8,7 @@ class BillingModel {
public $owner_id;
public $billingaddress_id;
public $customer_number;
public $fibu_account_number;
public $company;
public $firstname;
public $lastname;
@@ -30,7 +31,9 @@ class BillingModel {
public $amount;
public $price;
public $price_setup;
public $vatarea;
public $vatrate;
public $vatgroup_id;
public $billing_period;
public $create_by;

View File

@@ -0,0 +1,5 @@
<?php
class BillingVoicenumber extends mfBaseModel {
}

View File

@@ -0,0 +1,19 @@
<?php
class BillingVoicenumberController extends mfBaseController {
protected function init()
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
}

View File

@@ -0,0 +1,413 @@
<?php
class BillingVoicenumberModel {
public $invoice_id;
public $contract_id;
public $start_date;
public $end_date;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new BillingVoicenumber();
foreach($data as $field => $value) {
if(property_exists(get_called_class(), $field)) {
$model ->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("BillingVoicenumber", "*", "1 = 1 ORDER BY BillingVoicenumberaddress_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new BillingVoicenumber($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM BillingVoicenumber
WHERE $where
ORDER BY BillingVoicenumberaddress_id LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new BillingVoicenumber($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getLast($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM BillingVoicenumber
WHERE $where
ORDER BY `create` DESC LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new BillingVoicenumber($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getInvoiceBaseData($filter) {
$db = FronkDB::singleton();
$items = [];
$where = self::getSqlFilter($filter);
$sql = "SELECT owner_id, BillingVoicenumberaddress_id, BillingVoicenumber_type, BillingVoicenumber_delivery FROM BillingVoicenumber
WHERE $where
GROUP BY owner_id, BillingVoicenumberaddress_id, BillingVoicenumber_type, BillingVoicenumber_delivery
ORDER BY owner_id, BillingVoicenumberaddress_id, BillingVoicenumber_type, BillingVoicenumber_delivery";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = [
"owner_id" => $data->owner_id,
"BillingVoicenumberaddress_id" => $data->BillingVoicenumberaddress_id,
"BillingVoicenumber_type" => $data->BillingVoicenumber_type,
"BillingVoicenumber_delivery" => $data->BillingVoicenumber_delivery
];
}
}
return $items;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM BillingVoicenumber
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "id ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM BillingVoicenumber
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new BillingVoicenumber($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("id", $filter)) {
$id = $filter['id'];
if(is_numeric($id)) {
$where .= " AND BillingVoicenumber.id like '%$id%'";
}
}
if(array_key_exists("approved", $filter)) {
$approved = $filter['approved'];
if($approved) {
$where .= " AND BillingVoicenumber.approved = 1";
} else {
$where .= " AND BillingVoicenumber.approved = 0";
}
}
if(array_key_exists("invoice_id", $filter)) {
$invoice_id = $filter['invoice_id'];
if(is_numeric($invoice_id)) {
$where .= " AND BillingVoicenumber.invoice_id=$invoice_id";
} elseif($invoice_id === null || $invoice_id === false) {
$where .= " AND (BillingVoicenumber.invoice_id IS NULL OR BillingVoicenumber.invoice_id=0)";
}
}
if(array_key_exists("contract_id", $filter)) {
$contract_id = $filter['contract_id'];
if(is_numeric($contract_id)) {
$where .= " AND BillingVoicenumber.contract_id=$contract_id";
}
}
if(array_key_exists("start_date", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date='$start_date'";
}
}
if(array_key_exists("start_date>", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date > '$start_date'";
}
}
if(array_key_exists("start_date<", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date < '$start_date'";
}
}
if(array_key_exists("start_date>=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>=']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date >= '$start_date'";
}
}
if(array_key_exists("start_date<=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<=']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date <= '$start_date'";
}
}
if(array_key_exists("owner_id", $filter)) {
$owner_id = $filter['owner_id'];
if(is_numeric($owner_id)) {
$where .= " AND BillingVoicenumber.owner_id=$owner_id";
}
}
if(array_key_exists("BillingVoicenumberaddress_id", $filter)) {
$BillingVoicenumberaddress_id = $filter['BillingVoicenumberaddress_id'];
if(is_numeric($BillingVoicenumberaddress_id)) {
$where .= " AND BillingVoicenumber.BillingVoicenumberaddress_id=$BillingVoicenumberaddress_id";
}
}
if(array_key_exists("customer_number", $filter)) {
$customer_number = $filter['customer_number'];
if(is_numeric($customer_number)) {
$where .= " AND BillingVoicenumber.customer_number LIKE $customer_number";
}
}
if (array_key_exists("company", $filter)) {
$company = FronkDB::singleton()->escape($filter["company"]);
if ($company) {
$where .= " AND company like '%$company%'";
}
}
if (array_key_exists("firstname", $filter)) {
$firstname = FronkDB::singleton()->escape($filter["firstname"]);
if ($firstname) {
$where .= " AND firstname like '%$firstname%'";
}
}
if (array_key_exists("lastname", $filter)) {
$lastname = FronkDB::singleton()->escape($filter["lastname"]);
if ($lastname) {
$where .= " AND lastname like '%$lastname%'";
}
}
if (array_key_exists("mergedName", $filter)) {
$name = FronkDB::singleton()->escape($filter["mergedName"]);
if ($name) {
$where .= " AND (CONCAT(firstname, ' ', lastname) like '%$name%' OR CONCAT(lastname, ' ', firstname) like '%$name%' )";
}
}
if (array_key_exists("street", $filter)) {
$street = FronkDB::singleton()->escape($filter["street"]);
if ($street) {
$where .= " AND street like '%$street%'";
}
}
if (array_key_exists("zip", $filter)) {
$zip = FronkDB::singleton()->escape($filter["zip"]);
if ($zip) {
$where .= " AND zip like '%$zip%'";
}
}
if (array_key_exists("city", $filter)) {
$city = FronkDB::singleton()->escape($filter["city"]);
if ($city) {
$where .= " AND city like '%$city%'";
}
}
if (array_key_exists("country", $filter)) {
$country = FronkDB::singleton()->escape($filter["country"]);
if ($country) {
$where .= " AND country like '%$country%'";
}
}
if (array_key_exists("email", $filter)) {
$email = FronkDB::singleton()->escape($filter["email"]);
if ($email) {
$where .= " AND email like '%$email%'";
}
}
if(array_key_exists("matchcode", $filter)) {
$matchcode = FronkDB::singleton()->escape($filter["matchcode"]);
if($matchcode) {
$where .= " AND matchcode like '%$matchcode%'";
}
}
if(array_key_exists("product_id", $filter)) {
$product_id = $filter['product_id'];
if(is_numeric($product_id)) {
$where .= " AND BillingVoicenumber.product_id=$product_id";
}
}
if(array_key_exists("product_name", $filter)) {
$product_name = $db->escape($filter['product_name']);
if($product_name) {
$where .= " AND product_name like '%$product_name%')";
}
}
if(array_key_exists("matchcode", $filter)) {
$matchcode = $db->escape($filter['matchcode']);
if($matchcode) {
$where .= " AND BillingVoicenumber.`matchcode` like '%$matchcode%'";
}
}
if(array_key_exists("price<", $filter)) {
$price = $filter['price<'];
if(is_numeric($price)) {
$where .= " AND BillingVoicenumber.price < $price";
}
}
if(array_key_exists("price<=", $filter)) {
$price = $filter['price<='];
if(is_numeric($price)) {
$where .= " AND BillingVoicenumber.price <= $price";
}
}
if(array_key_exists("price>", $filter)) {
$price = $filter['price>'];
if(is_numeric($price)) {
$where .= " AND BillingVoicenumber.price > $price";
}
}
if(array_key_exists("price>=", $filter)) {
$price = $filter['price>='];
if(is_numeric($price)) {
$where .= " AND BillingVoicenumber.price >= $price";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
if(array_key_exists("BillingVoicenumber_type", $filter)) {
$BillingVoicenumber_type = $filter['BillingVoicenumber_type'];
if(is_numeric($BillingVoicenumber_type)) {
$where .= " AND BillingVoicenumber.BillingVoicenumber_type = $BillingVoicenumber_type";
}
}
if(array_key_exists("BillingVoicenumber_type", $filter)) {
$BillingVoicenumber_type = $db->escape($filter['BillingVoicenumber_type']);
if($BillingVoicenumber_type) {
$where .= " AND BillingVoicenumber.BillingVoicenumber_type = '$BillingVoicenumber_type'";
}
}
if(array_key_exists("BillingVoicenumber_delivery", $filter)) {
$BillingVoicenumber_delivery = $db->escape($filter['BillingVoicenumber_delivery']);
if($BillingVoicenumber_delivery) {
$where .= " AND BillingVoicenumber.BillingVoicenumber_delivery = '$BillingVoicenumber_delivery'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -27,6 +27,8 @@ class Contract extends mfBaseModel {
private $productchangeTo;
private $relocationFrom;
private $relocationTo;
private $vatgroup;
private $vatrate;
private $finisher;
private $canceler;
private $creator;
@@ -193,6 +195,15 @@ class Contract extends mfBaseModel {
}
public function getConfigValue($itemname) {
$configvalues = $this->getProperty("configvalues");
if(!$configvalues) return null;
if(!array_key_exists($itemname, $configvalues)) return null;
$confitem = $configvalues[$itemname];
return $confitem->value;
}
public function getProperty($name) {
if($this->$name == null) {
@@ -293,6 +304,19 @@ class Contract extends mfBaseModel {
return $this->contractConfigItems;
}*/
if($name == "vatrate") {
$vatgroup = $this->getProperty("vatgroup");
$country = $this->getProperty("country");
$vatrate = $vatgroup->rates["domestic"]->rate;
if($this->country_id && $country->is_eu) {
$vatrate = $vatgroup->rates["eu"]->rate;
} if($this->country_id && !$country->is_eu) {
$vatrate = $vatgroup->rates["other"]->rate;
}
$this->vatrate = $vatrate;
return $this->vatrate;
}
if($name == "journals") {
$this->journals = array_reverse(ContractjournalModel::search(["contract_id" => $this->id]));
return $this->journals;

View File

@@ -99,7 +99,7 @@ class CountryModel {
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
@@ -131,7 +131,7 @@ class CountryModel {
if(array_key_exists("isocode", $filter)) {
$isocode = $db->escape($filter['isocode']);
if($isocode) {
$where .= " AND Country.`isocode` like '%$isocode%'";
$where .= " AND Country.`isocode` like '$isocode'";
}
}

View File

@@ -65,7 +65,7 @@ class InvoiceController extends mfBaseController {
}
if(array_key_exists("address", $filter)) {
if(array_key_exists("address", $filter) && $filter["address"]) {
if($filter["address"]) {
$search = $this->db()->escape($filter['address']);
if(!array_key_exists("add-where", $new_filter)) $new_filter["add-where"] = "";
$new_filter['add-where'] .= " AND (street like '%$search%' OR zip like '%$search%' OR city like '%$search%' OR country like '%$search%')";
@@ -102,7 +102,7 @@ class InvoiceController extends mfBaseController {
if(!array_key_exists($p->vatrate, $vat)) {
$vat[$p->vatrate] = 0;
}
$vat[$p->vatrate] += $p->price_gross - $p->price;
$vat[$p->vatrate] += $p->price_gross - ($p->price * $p->amount);
}
$pdf_vars = [
@@ -153,9 +153,13 @@ class InvoiceController extends mfBaseController {
//var_dump($owner_id, $billingaddress_id, $bills);exit;
$invoice_data = [];
$invoice_vatrate = 20;
foreach($billing_rows as $bill) {
$vatrate = $bill->vatrate;
$vatarea = $bill->vatarea;
$invoice_vatrate = $bill->vatgroup->rates[$vatarea]->rate;
$vatrate = $invoice_vatrate;
$price = $bill->price;
$price_total = $bill->price * $bill->amount;
$price_gross = ($vatrate) ? $price_total + ($price_total / 100) * $vatrate : $price_total;
@@ -177,6 +181,9 @@ class InvoiceController extends mfBaseController {
$position_data["amount"] = $bill->amount;
$position_data["billing_period"] = $bill->billing_period;
$position_data["fibu_cost_account"] = $bill->vatgroup->rates[$vatarea]->account;
$position_data["fibu_cost_account_legacy"] = $bill->vatgroup->rates[$vatarea]->legacy_account;
$position_data["fibu_taxcode"] = $bill->vatgroup->rates[$vatarea]->taxcode;
if($is_setup_only) {
$this->log->debug("Contract ID ". $bill->contract_id." is setup only");
@@ -219,6 +226,8 @@ class InvoiceController extends mfBaseController {
$invoice_data["owner_id"] = $owner_id;
$invoice_data["billingaddress_id"] = $billingaddress_id;
$invoice_data["customer_number"] = $bill->customer_number;
$invoice_data["fibu_account_number"] = $bill->fibu_account_number;
$invoice_data["company"] = $bill->company;
$invoice_data["firstname"] = $bill->firstname;
$invoice_data["lastname"] = $bill->lastname;
@@ -239,6 +248,12 @@ class InvoiceController extends mfBaseController {
$invoice_data["total_vat"] = 0;
$invoice_data["fibu_cost_area"] = $vatarea;
$invoice_data["fibu_cost_account"] = $bill->vatgroup->rates[$vatarea]->account;
$invoice_data["fibu_cost_account_legacy"] = $bill->vatgroup->rates[$vatarea]->legacy_account;
$invoice_data["fibu_taxcode"] = $bill->vatgroup->rates[$vatarea]->taxcode;
$invoice_data["tax_text"] = $bill->vatgroup->rates[$vatarea]->invoice_text;
}
@@ -265,9 +280,9 @@ class InvoiceController extends mfBaseController {
// add Invoice::id to Invoiceposition
$position->invoice_id = $invoice->id;
if(!$position->vatrate) {
if(!$invoice_vatrate || !$position->vatrate) {
$total_net += $position->price_total;
$total_gross += $position->price_total;
} else {
$total_vat += ($position->price_total / 100) * $position->vatrate;
$total_net += $position->price_total;

View File

@@ -3,8 +3,15 @@
class InvoiceModel {
public $invoice_number;
public $invoice_date;
public $owner_id;
public $billingaddress_id;
public $customer_number;
public $fibu_account_number;
public $fibu_cost_area;
public $fibu_cost_account;
public $fibu_cost_account_legacy;
public $fibu_taxcode;
public $tax_text;
public $company;
public $firstname;
public $lastname;

View File

@@ -15,6 +15,9 @@ class InvoicepositionModel {
public $price_total;
public $price_gross;
public $vatrate;
public $fibu_cost_account;
public $fibu_cost_account_legacy;
public $fibu_taxcode;
public $billing_period;
public $create_by;

View File

@@ -12,7 +12,7 @@ class IvtCustomerProduct extends mfBaseModel {
public function __construct($_=NULL) {
$this->log = mfLoghandler::singleton();
$this->data = new stdClass();
$this->table = "products";
$this->table = "customer_product";
$this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);

View File

@@ -106,4 +106,69 @@ class VoiceCallHistoryController extends mfBaseController {
]
];
}
public function addContractIds() {
$unknown_numbers = [];
// get calls without contract id
foreach(VoiceCallHistoryModel::getVoiceCallHistoryAsEntity(["contract_id" => null, "billable" => "1", "duration" => ["from" => 1], "end" => "2024-06-01"]) as $call) {
//var_dump($call);exit;
//echo "\n";
$number = $call->voice_account;
if(!$number) continue;
if(in_array($number, $unknown_numbers)) continue;
$voicenumber = VoicenumberModel::getFirst(["number" => $number]);
// TODO: Mail an office
if(!$voicenumber) {
$this->log->debug(__METHOD__.": Voicenumber $number not found.");
$unknown_numbers[] = $number;
continue;
}
if(!$voicenumber->contract_id) {
$this->log->debug(__METHOD__.": Missing Contract_ID in Voicenumber ".$voicenumber->number);
continue;
}
$calldate = new DateTime($call->start);
$calldate->setTimezone(new DateTimeZone("Europe/Vienna"));
$contract = $voicenumber->contract;
// calls from before first IVT Import must be right, we don't have historic contract data
if($calldate->format("Y-m") < "2024-08") {
$call->contract_id = $contract->id;
//echo "save 1\n";
if(!$call->save()) {
die("Cannot save call ".$call->id);
}
continue;
}
// check if contract was active at this time ...
if($calldate->getTimestamp() <= $contract->finish_date && (!$contract->cancel_date || $calldate->getTimestamp() <= $contract->cancel_date)) {
// 2023-08 is the oldest we can know because of contract import in July 2024
$call->contract_id = $contract->id;
echo "save 2\n";
$call->save();
}
// ... else look for contract with this number active at that time ...
// ... else use current active contract
if(!$contract) {
// find contract by ContractConfigValue voicenumberblock_voicenumber
}
$call->contract_id = $voicenumber->contract_id;
echo "save 3\n";
$call->save();
}
}
}

View File

@@ -2,6 +2,7 @@
class VoiceCallHistoryModel {
public $uid;
public $voice_account;
public $contract_id;
public $start;
public $source;
public $destination;
@@ -109,6 +110,7 @@ class VoiceCallHistoryModel {
$sql .= isset($filters['destination']) ? Helper::generateFilterCondition($filters['destination'], "destination") : "";
$sql .= isset($filters['billable']) ? Helper::generateFilterCondition($filters['billable'], "billable") : "";
$sql .= isset($filters['duration']) ? Helper::generateFilterCondition($filters['duration'], "duration") : "";
$sql .= array_key_exists("contract_id", $filters) ? Helper::generateFilterCondition($filters['contract_id'], "contract_id") : "";
return $sql;
}
@@ -119,6 +121,7 @@ class VoiceCallHistoryModel {
$sql .= $order === null || $order['key'] === null ? " ORDER BY `start` DESC" : " ORDER BY `" . $order['key'] . "` " . $order['order'];
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
//mfLoghandler::singleton()->debug($sql);exit;
// die($sql);
$result = $db->query($sql);
$rows = [];
@@ -129,6 +132,23 @@ class VoiceCallHistoryModel {
return $rows;
}
public static function getVoiceCallHistoryAsEntity($filters, $limit = null, $offset = 0, $order = null) {
$db = FronkDB::singleton();
$sql = "SELECT * FROM `VoiceCallHistory` WHERE 1 " . self::getSqlFilter($filters);
$sql .= $order === null || $order['key'] === null ? " ORDER BY `start` DESC" : " ORDER BY `" . $order['key'] . "` " . $order['order'];
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
//mfLoghandler::singleton()->debug($sql);exit;
// die($sql);
$result = $db->query($sql);
$rows = [];
while ($row = $db->fetch_object($result)) {
$rows[] = new VoiceCallHistory($row);
}
return $rows;
}
public static function countVoiceCallHistory($filters) {
$db = FronkDB::singleton();
$sql = "SELECT COUNT(*) as `total_rows` FROM `VoiceCallHistory` WHERE 1 " . self::getSqlFilter($filters);

View File

@@ -67,7 +67,7 @@ class VoicenumberModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
//mfLoghandler::singleton()->debug($where);
$res = $db->select("Voicenumber", "*", "$where ORDER BY voicenumberblock_id, number");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);

View File

@@ -9,7 +9,7 @@ final class ContractAddVat extends AbstractMigration
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("Contract");
$table->addColumn("vatrate", "decimal", ["null" => false, "default" => 0, "precision" => 6, "scale" => 2, "after" => "price_setup"]);
$table->addColumn("vatgroup_id", "integer", ["null" => false, "after" => "price_setup"]);
$table->update();
}
@@ -22,7 +22,7 @@ final class ContractAddVat extends AbstractMigration
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("Contract");
$table->removeColumn("vatrate");
$table->removeColumn("vatgroup_id");
$table->update();
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class BillingAddFibuData extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$billing = $this->table("Billing");
$billing->addColumn("fibu_account_number", "integer", ["null" => true, "default" => null, "after" => "customer_number"]);
$billing->addColumn("vatgroup_id", "string", ["null" => true, "default" => null, "after" => "vatrate"]);
$billing->addColumn("vatarea", "string", ["null" => true, "default" => null, "after" => "vatgroup_id"]);
$billing->save();
$invoice = $this->table("Invoice");
$invoice->addColumn("fibu_account_number", "integer", ["null" => true, "default" => null, "after" => "customer_number"]);
$invoice->addColumn("fibu_cost_area", "string", ["null" => true, "default" => null, "after" => "fibu_account_number"]);
$invoice->addColumn("fibu_cost_account", "integer", ["null" => true, "default" => null, "after" => "fibu_cost_area"]);
$invoice->addColumn("fibu_cost_account_legacy", "integer", ["null" => true, "default" => null, "after" => "fibu_cost_account"]);
$invoice->addColumn("fibu_taxcode", "integer", ["null" => true, "default" => null, "after" => "fibu_cost_account_legacy"]);
$invoice->addColumn("tax_text", "string", ["null" => true, "default" => null, "length" => 255, "after" => "fibu_taxcode"]);
$invoice->save();
$ip = $this->table("Invoiceposition");
$ip->addColumn("fibu_cost_account", "integer", ["null" => true, "default" => null, "after" => "vatrate"]);
$ip->addColumn("fibu_cost_account_legacy", "integer", ["null" => true, "default" => null, "after" => "fibu_cost_account"]);
$ip->addColumn("fibu_taxcode", "integer", ["null" => true, "default" => null, "after" => "fibu_cost_account_legacy"]);
$ip->save();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("Invoiceposition")
->removeColumn("fibu_taxcode")
->removeColumn("fibu_cost_account_legacy")
->removeColumn("fibu_cost_account")
->update();
$this->table("Invoice")
->removeColumn("fibu_invoice_text")
->removeColumn("fibu_taxcode")
->removeColumn("fibu_cost_account_legacy")
->removeColumn("fibu_cost_account")
->removeColumn("fibu_cost_area")
->removeColumn("fibu_account_number")
->update();
$this->table("Billing")
->removeColumn("vatarea")
->removeColumn("vatgroup_id")
->removeColumn("fibu_account_number")
->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class CreateBillingVoicenumber extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("BillingVoicenumber");
$table->addColumn("invoice_id", "integer", ["null" => true, "default" => null]);
$table->addColumn("contract_id", "integer", ["null" => false]);
$table->addColumn("owner_id", "integer", ["null" => false]);
$table->addColumn("billingaddress_id", "integer", ["null" => false]);
$table->addColumn("voicenumber", "string", ["null" => false, "limit" => 64]);
$table->addColumn("start_date", "date", ["null" => false]);
$table->addColumn("end_date", "date", ["null" => false]);
$table->addColumn("zone", "string", ["null" => false, "limit" => 64]);
$table->addColumn("duration", "integer", ["null" => false]);
$table->addColumn("price", "decimal", ["null" => false, "precision" => 14, "scale" => 4]);
$table->addColumn("total_vat", "decimal", ["null" => false, "precision" => 14, "scale" => 4]);
$table->addColumn("create_by", "integer", ["null" => false]);
$table->addColumn("edit_by", "integer", ["null" => false]);
$table->addColumn("create", "integer", ["null" => false]);
$table->addColumn("edit", "integer", ["null" => false]);
$table->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("BillingVoicenumber")->drop()->save();
}
if($this->getEnvironment() == "addressdb") {
}
}
}

View File

@@ -22,6 +22,8 @@ class Helper {
}
} else if ($filterValue === "0" || $filterValue === "1") {
$sql .= " AND `$columnName` = " . $filterValue;
} else if ($filterValue === null) {
$sql .= " AND `$columnName` IS NULL";
} else if (!empty($filterValue)) {
if ($exactMatch) {
$sql .= " AND `$columnName` = '" . $filterValue . "'";

View File

@@ -1,16 +1,23 @@
@page {
size: A4;
margin: 0mm;
}
* {
font-family: "Open Sans";
}
html {
margin: 24pt;
/*margin: 24pt;*/
margin: 0;
height:100%;
}
body {
font-size:9pt;
height:100%;
margin-top: 50pt;
margin: 0;
margin-top: 20pt;
}
h2 {

View File

@@ -15,50 +15,279 @@ $me = new User(1);
define("INTERNAL_USER_ID", $me->id);
define("INTERNAL_USER_USERNAME", $me->username);
$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$charsLength = strlen($chars);
require_once(APPDIR . "Admin/functions/IvtContractImport.php");
$c = 0;
$ownerIdToBillingAddress = [];
$db = FronkDB::singleton();
$res = $db->query("SELECT id FROM Preorder WHERE oaid IS NOT NULL ORDER BY RAND()");
/*while($data = $db->fetch_object($res)) {
if($c > 1000) break;
$ici = new Admin_IvtContractImport();
$preorder = new Preorder($data->id);
if(!$preorder->oaid) {
echo "no oaid ".$preorder->id."\n";
}
$oaid = $preorder->oaid;
$ucode = '';
for($i = 0; $i < 20; $i++) {
$ucode .= $chars[rand(0, $charsLength - 1)];
$orderproduct = false;
$orderproduct_id = false;
$ivt_contract = new IvtCustomerProduct(5983);
$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;
}
echo "$oaid;$ucode\n";
$c++;
$ip = $productMatch->ivtproduct;
$product = $productMatch->product;
/*
* check ivt customer and get Billingaddress
*/
$billingaddress_id = false;
//var_dump($ivt_contract);exit;
$customer_check_return = checkIvtCustomer($ivt_contract, $ivt_customer, $product);
private function checkIvtCustomer($ivt_contract, $ivt_customer, $product, $doit = false)
{
global $ownerIdToBillingAddress;
$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] = $billingaddress->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)) {
$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
$l = AddressLinkModel::create([
'origin_address_id' => $owner->id,
'type' => "billing",
'address_id' => $billingaddress->id
]);
$l->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 = 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;
}*/
$stati = PreorderstatusModel::getAll();
while($data = $db->fetch_object($res)) {
if($c > 1000) break;
$preorder = new Preorder($data->id);
if(!$preorder->oaid) {
echo "no oaid ".$preorder->id."\n";
}
$oaid = $preorder->oaid;
$r = rand(0, count($stati)-1);
$new_status = $stati[$r];
if(!$new_status->code) {
fwrite(STDERR, "status not found\n");
continue;
if($create) {
return $new_billing;
}
echo "$oaid;".$new_status->code."\n";
return true;
$c++;
}

View File

@@ -0,0 +1,21 @@
#!/usr/bin/php
<?php
//require 'vendor/autoload.php';
require("../../config/config.php");
define('FRONKDB_SQLDEBUG',false);
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
$me = new User(1);
define("INTERNAL_USER_ID", $me->id);
define("INTERNAL_USER_USERNAME", $me->username);
$vchc = new VoiceCallHistoryController(false);
$vchc->addContractIds();

File diff suppressed because it is too large Load Diff