Files
thetool/application/Admin/functions/IvtCreditImport.php
2024-07-10 21:13:21 +02:00

285 lines
11 KiB
PHP

<?php
class Admin_IvtCreditImport {
private $log;
private $request;
private $netownerid = [
3805 => 2840,
4941 => 2840,
4909 => 3199
];
private $cust_cred_ignore = [
2267,
2934
];
private $netowner_ignore = [
3716
];
private $netowner_rules = [
3715 => ["action" => "double_price"],
];
private $cust_cred_id_to_customer_id = [
2891 => 104004,
];
public function __construct($request = false) {
$this->request = $request;
$this->log = mfLoghandler::singleton();
}
public function run($doit = false) {
$i = 0;
$c = 0;
foreach(IvtCustomerCreditingModel::getAll() as $cust_cred) {
/*if($i > 200) {
continue;
}*/
$i++;
if(in_array($cust_cred->id, $this->cust_cred_ignore)) continue;
/*var_dump($cust_cred);
var_dump($cust_cred->netowner);
var_dump($cust_cred->customer);
var_dump($cust_cred->crediting_product);
exit;*/
$ivt_netowner = $cust_cred->netowner;
$ivt_cred_product = $cust_cred->crediting_product;
if(array_key_exists($cust_cred->id, $this->cust_cred_id_to_customer_id)) {
$ivt_customer = new IvtCustomer($this->cust_cred_id_to_customer_id[$cust_cred->id]);
} else {
$ivt_customer = $cust_cred->customer;
}
if(in_array($ivt_netowner->id, $this->netowner_ignore)) continue;
if(!$ivt_customer) {
echo __METHOD__.": No customer in cust_cred ".$cust_cred->id."\n";
continue;
}
$customer = AddressModel::getFirst(["customer_number" => $ivt_customer->id]);
$netowner = AddressModel::getFirst(["customer_number" => $ivt_netowner->id]);
if(array_key_exists($ivt_netowner->id, $this->netownerid)) {
$netowner = AddressModel::getFirst(["customer_number" => $this->netownerid[$ivt_netowner->id]]);
}
if(!$customer) {
echo __METHOD__ . ": No Customer ".$ivt_customer->id." cust_cred ".$cust_cred->id."\n";
$this->log->debug("No Customer ".$ivt_customer->id." cust_cred ".$cust_cred->id);
exit;
}
if(!$netowner) {
echo __METHOD__ . ": No Netowner ".$ivt_netowner->id."\n";
//$this->log->debug("No netowner ".$ivt_netowner->id);
exit;
}
$contracts = ContractModel::search(["owner_id" => $customer->id]);
$data = [];
/*
* 1962 - internal xinon account for credits missing contract
*
*/
if($ivt_customer->id != 1962) {
// if not xinon internal, look up primary contract for linking
if(!count($contracts)) {
$ivt_contracts = IvtCustomerProductModel::search(["cid" => $ivt_customer->id]);
//echo __METHOD__ . ": Contract not found for credit ".$cust_cred->id." Kunde ".$customer->getCompanyOrName()." (".$customer->customer_number."; ".count($ivt_contracts)." IVT Contracts), GS an ".$netowner->getCompanyOrName()." (".$netowner->customer_number.")\n";
echo "Netzinhaber: " . $netowner->getCompanyOrName() . " (" . $netowner->customer_number . ")\n";
echo "Kunde: " . $customer->getCompanyOrName() . " (" . $customer->customer_number . ")\n";
echo "Gutschft: " . $cust_cred->crediting_product->name . " | " . $cust_cred->crediting_product->price_excl . "\n";
echo "GS Comment: " . $cust_cred->comment . "\n";
if($ivt_contracts) {
echo "Kunde Produkte:\n";
foreach($ivt_contracts as $ivtc) {
echo "\t" . $ivtc->pid . ": " . $ivtc->product->name . " | " . $ivtc->product->price . "\n";
}
} else {
echo "Keine aktiven Produkte\n";
}
echo "=========================\n\n";
continue;
}
// use first contract as primary contract
$primary_contract = reset($contracts);
// find more suitable primary contract
foreach($contracts as $contract) {
// only monthly products
if($contract->biling_period > 1) continue;
// no one-off products
if(!$contract->price && $contract->price_setup) continue;
if($contract->termination_id) {
$primary_contract = $contract;
break;
}
if(preg_match('/(dsl|funkinternet|standortgeber|glasfaser|brettljausn)/i', $contract->product->name)) {
$primary_contract = $contract;
break;
}
}
if(!$primary_contract) {
//echo __METHOD__ . ": Contract not found for credit ".$cust_cred->id."\n";
$this->log->debug("No primary contract cust " . $ivt_customer->id);
}
// create Credit Contract
if(!$primary_contract->product_id) {
var_dump($primary_contract);
exit;
}
$data["orderproduct_id"] = $primary_contract->orderproduct_id;
$data["product_id"] = $primary_contract->product_id;
$data["product_name"] = $primary_contract->product_name;
$data["product_info"] = $primary_contract->product_info;
$data["amount"] = $primary_contract->amount;
$data["sla_id"] = $primary_contract->sla_id;
$data["product_external"] = $primary_contract->product_external;
$data["product_external_id"] = $primary_contract->product_external_id;
$data["billing_delay"] = $primary_contract->billing_delay;
$data["billing_period"] = $primary_contract->billing_period;
$data["contract_term"] = $primary_contract->contract_term;
$data["order_date"] = $primary_contract->order_date;
$data["vatgroup_id"] = $primary_contract->product->vatgroup_id;
$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->getCompanyOrName();
$matchcode .= ", ".$customer->street.", ".$customer->zip." ".$customer->city;
}
} else {
$create_date = new DateTime($cust_cred->created);
$create_date->setTime(2,0);
$finish_date = new DateTime($cust_cred->lastdate);
$finish_date->modify("first day of this month");
$finish_date->setTime(2,0,0);
$finish_date->modify("+ 1 month"); // XXX For import, because july was not invoiced in IVT
//$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1);
$data["orderproduct_id"] = null;
$data["product_id"] = 336; // Mainproduct dummy for crediting import [IVT]
$data["product_name"] = $cust_cred->crediting_product->name;
$data["product_info"] = null;
$data["amount"] = 1;
$data["sla_id"] = 4;
$data["product_external"] = 0;
$data["product_external_id"] = null;
$data["vatgroup_id"] = 1;
$data["billing_delay"] = 0;
$data["billing_period"] = 1;
$data["contract_term"] = 12;
$data["order_date"] = $create_date->getTimestamp();
$data["finish_date"] = $finish_date->getTimestamp();
$data["finish_date_by"] = 1;
$data["note"] = null;
$matchcode = $cust_cred->comment;
}
$data["matchcode"] = $matchcode;
$data["owner_id"] = $netowner->id;
$data["billingaddress_id"] = $netowner->id;
$data["termination_id"] = null;
$data["price"] = $ivt_cred_product->price_excl * -1;
$data["price_setup"] = 0;
$data["price_nne"] = 0;
$data["price_nbe"] = 0;
$data['imported_from'] = "ivt-credit";
$data['imported_data'] = $cust_cred->id;
if(array_key_exists($ivt_netowner->id, $this->netowner_rules)) {
if($this->netowner_rules[$ivt_netowner->id]["action"] == "double_price") {
$data['price'] *= 2;
}
}
$cred_contract = ContractModel::create($data);
if(!$cred_contract->save()) {
$this->log->debug("error saving credit contract");
echo __METHOD__.": error saving credit contract";
exit;
}
$c++;
// create Journals
// creation journal
$journal = ContractjournalModel::create([
'contract_id' => $cred_contract->id,
'type' => "created_from",
'value' => "import",
'text' => "IVT customer_credit ".$cust_cred->id."; cid: ".$cust_cred->cust_id."; pid: ".$ivt_cred_product->id." (".$ivt_cred_product->name.")"
]);
$journal_id = $journal->save();
if(!$journal_id) {
echo "Fehler beim Journal erstellen.<br />\n";
exit;
}
// credit linking journal
$journal = ContractjournalModel::create([
'contract_id' => $primary_contract->id,
'type' => "credit_created",
'value' => $cred_contract->id,
]);
$journal_id = $journal->save();
if(!$journal_id) {
echo "Fehler beim Journal erstellen.<br />\n";
exit;
}
if(count($contracts)) {
// Link to all Contracts of Customer
foreach($contracts as $contract) {
if(ContractLinkModel::getFirst(["contract_id" => $cred_contract->id, "origin_contract_id" => $contract->id])) {
continue;
}
$link = ContractLinkModel::create([
'contract_id' => $cred_contract->id,
'origin_contract_id' => $contract->id,
'type' => 'credit'
]);
$link->save();
}
}
}
echo "Imported $i credits; $c Credit Contracts erstellt\n";
}
}