Files
thetool/application/Admin/functions/IvtCreditImport.php
2024-05-23 18:16:41 +02:00

143 lines
5.2 KiB
PHP

<?php
class Admin_IvtCreditImport {
private $log;
private $request;
private $netownerid = [
3805 => 2840,
4941 => 2840,
4909 => 3199,
];
private $cust_cred_ignore = [
2267,
2934
];
public function __construct($request = false) {
$this->request = $request;
$this->log = mfLoghandler::singleton();
}
public function run($doit = false) {
$i = 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;*/
if(!$cust_cred->customer) continue;
$ivt_netowner = $cust_cred->netowner;
$ivt_customer = $cust_cred->customer;
$ivt_cred_product = $cust_cred->crediting_product;
$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) {
$this->log->debug("No Customer ".$ivt_customer->id." cust_cred ".$cust_cred->id);
exit;
}
if(!$netowner) {
$this->log->debug("No netowner ".$ivt_netowner->id);
exit;
}
$contracts = ContractModel::search(["owner_id" => $customer->id]);
// DEBUG: not all contracts imported yet
if(!count($contracts)) {
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) {
$this->log->debug("No primary contract cust ".$ivt_customer->id);
}
// create Credit Contract
if(!$primary_contract->product_id) {
var_dump($primary_contract);exit;
}
$data = [];
$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["finish_date"] = $primary_contract->finish_date;
$data["finish_date_by"] = $primary_contract->finish_date_by;
$data["note"] = $primary_contract->note;
$data["matchcode"] = $cust_cred->comment;
$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;
$cred_contract = ContractModel::create($data);
if(!$cred_contract->save()) {
$this->log->debug("error saving credit contract");
echo "error saving credit contract";
exit;
}
// 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' => 'link'
]);
$link->save();
}
}
}
}