From 83b10ba922d8229820e622205179b6dc84e4aa93 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 7 May 2024 22:53:16 +0200 Subject: [PATCH] WIP Contract 2024-05-07 - 2 --- .../Admin/functions/IvtContractImport.php | 152 ++++++++++++++---- 1 file changed, 122 insertions(+), 30 deletions(-) diff --git a/application/Admin/functions/IvtContractImport.php b/application/Admin/functions/IvtContractImport.php index e9d2b3c1e..891d38434 100644 --- a/application/Admin/functions/IvtContractImport.php +++ b/application/Admin/functions/IvtContractImport.php @@ -3,10 +3,37 @@ class Admin_IvtContractImport { private $request; private $log; + private $static_ivt_order_match = []; public function __construct($request) { $this->request = $request; $this->log = mfLoghandler::singleton(); + + /* + * ivt::customer_product::id => Order::id + * SELECT cp.id,cp.cid,cp.pid,products.name,cp.sid,cp.created,cp.lastdate,cp.comment,tp.name + * FROM `customer_product` as cp LEFT JOIN products ON (cp.pid = products.id) + * LEFT JOIN thetool_live4.IvtProductMatch ipm ON (ipm.ivt_product_id = cp.pid) + * LEFT JOIN thetool_live4.Product tp ON (ipm.product_id = tp.id) + * WHERE `cid` = 1840; + */ + $this->static_ivt_order_match = [ + 17245 => 3946, // lugitsch 1840 + 16704 => 3342, + 16703 => 3342, + 15712 => 3414, + 14798 => 2940, + 14322 => 2623, + 14321 => 2623, + 14222 => 2702, + 13951 => 2218, + 13322 => 2623, + 2034 => false, + 561 => false, + + 11409 => 1222, // x-tec 1842 + 14192 => 2596 + ]; } public function run($doit = false) { @@ -19,9 +46,8 @@ class Admin_IvtContractImport { $last_cid = 0; $i = 0; - foreach(IvtCustomerProductModel::search(["lasdt_date>", "2024-04-01"], "cid") as $ivt_contract) { - if($ivt_contract->cid == 1494); - if($i > 150 && $ivt_contract->cid != $last_cid) break; // only break after the last ivtcontract of this customer + foreach(IvtCustomerProductModel::search(["lasdt_date>", "2023-04-01"], "cid") as $ivt_contract) { + if($i > 1000 && $ivt_contract->cid != $last_cid) break; // only break after the last ivtcontract of this customer $last_cid = $ivt_contract->cid; $i++; @@ -32,20 +58,6 @@ class Admin_IvtContractImport { $ivt_customer = $ivt_contract->customer; $ivt_product = $ivt_contract->product; - /* - * check ivt customer and get Billingaddress - */ - $billingaddress_id = false; - - $customer_check_return = $this->checkIvtCustomer($ivt_customer); - - 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
\n"; - exit; - } - - $billingaddress_id = $customer_check_return["billingaddress_id"]; - /* * get thetool product from IvtProductMatch */ @@ -55,7 +67,6 @@ class Admin_IvtContractImport { exit; } - if(!$this->checkIvtProduct($productMatch->ivtproduct, $doit)) { exit; } @@ -63,6 +74,20 @@ class Admin_IvtContractImport { $ip = $productMatch->ivtproduct; $product = $productMatch->product; + /* + * check ivt customer and get Billingaddress + */ + $billingaddress_id = false; + + $customer_check_return = $this->checkIvtCustomer($ivt_contract, $ivt_customer, $product); + + 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
\n"; + exit; + } + + $billingaddress_id = $customer_check_return["billingaddress_id"]; + /* * get or create customer */ @@ -141,6 +166,8 @@ class Admin_IvtContractImport { $ignore = true; } + if($product->billing_period > 1 && $ivt_contract->last_date < "2023-04-08") + /* * Try to match ivt contract to thetool Order */ @@ -233,12 +260,16 @@ class Admin_IvtContractImport { if(count($contracts) < 2) continue; $prev_contracts = []; $primary_matchcode = false; + $primary_matchcode_source_product_group = ""; + $primary_matchcode_no_productgroups_pattern = '/(Pauschalen|TV|Managed Services|Vorbestellung|Mobilfunk|Standortvernetzung|Colo|Vorort|Webhosting|zusatz)/i'; + foreach($contracts as $contract) { - if($contract->matchcode && !$primary_matchcode && !preg_match('/(Pauschalen|TV|Managed Services|Vorbestellung|Mobilfunk|Standortvernetzung|Colo|Vorort|Webhosting|zusatz)/i',$contract->product->productgroup->name)) { + if($contract->matchcode && !$primary_matchcode && !preg_match($primary_matchcode_no_productgroups_pattern, $contract->product->productgroup->name)) { $primary_matchcode = $contract->matchcode; + $primary_matchcode_source_product_group = $contract->product->productgroup->name; } - if($primary_matchcode && !$contract->matchcode) { + if($primary_matchcode && !$contract->matchcode && !preg_match($primary_matchcode_no_productgroups_pattern, $primary_matchcode_source_product_group)) { $contract->matchcode = $primary_matchcode; $contract->save(); } @@ -248,10 +279,20 @@ class Admin_IvtContractImport { continue; } foreach($prev_contracts as $prev) { - if(!$prev->matchcode && $primary_matchcode) { + if(!$prev->matchcode && $primary_matchcode && !preg_match($primary_matchcode_no_productgroups_pattern, $primary_matchcode_source_product_group)) { $prev->matchcode = $primary_matchcode; $prev->save(); } + if(preg_match('/Service Pauschale/i',$prev->product->name)) { + foreach($contracts as $c) { + if($c->termination_id && $c->matchcode) { + $prev->matchcode = $c->matchcode; + $prev->save(); + break; + } + } + } + if (ContractLinkModel::getFirst(["contract_id" => $contract->id, "origin_contract_id" => $prev->id])) { continue; @@ -274,7 +315,7 @@ class Admin_IvtContractImport { return $data; } - private function checkIvtCustomer($ivt_customer, $doit = false) + private function checkIvtCustomer($ivt_contract, $ivt_customer, $product, $doit = false) { $return = []; // create customer if not exists @@ -288,14 +329,8 @@ class Admin_IvtContractImport { 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"); + $order = $this->findOrder($ivt_contract); + if(!$order) { return ["billingaddress_id" => $owner->id]; } @@ -511,6 +546,63 @@ class Admin_IvtContractImport { */ } + + private function findOrder($ivt_contract) { + // find order + $ivt_customer = $ivt_contract->customer; + $ivt_product = $ivt_contract->product; + + $productMatch = IvtProductMatchModel::getFirst(["ivt_product_id" => $ivt_contract->pid]); + if(!$productMatch) { + echo "Kein Match zu IVT Product " . $ivt_contract->pid . " gefunden.
\n"; + exit; + } + + $product = $productMatch->product; + + $owner = AddressModel::getFirst(["customer_number" => $ivt_customer->id]); + if (!$owner) { + //var_dump($ivt_customer, $ivt_contract); + die("Address ".$ivt_customer->id." not found!"); + } + + if(array_key_exists($ivt_contract->id, $this->static_ivt_order_match)) { + $order_id = $this->static_ivt_order_match[$ivt_contract->id]; + if($order_id === false) { + return false; + } + $order = new Order($order_id); + if(!$order->id) { + die("Manually specified order id ".$this->static_ivt_order_match[$ivt_contract->id]." not found for customer ".$ivt_customer->id); + } + return $order; + } else { + $order_count = OrderModel::count(["owner_id" => $owner->id, "finish_date" => true]); + $order_id = false; + + if ($order_count > 1) { + $found_product = 0; + foreach(OrderModel::search(["owner_id" => $owner->id, "finish_date" => true]) as $o) { + // find the one with the right product + foreach(OrderProductModel::search(["order_id" => $o->id, "product_id" => $product->id]) as $op) { + $order_id = $op->id; + $found_product++; + } + if($found_product > 1) { + die("Mehr als eine Bestellung für ivt_customer ".$ivt_customer->id." gefunden.\n" . print_r($owner, true)); + } + } + $order = new Order($order_id); + if(!$order) { + die("Order $order_id not found"); + } + return $order; + } + + } + return false; + } + private function getRadiusUser($ivt_customer, $ivt_product, $contract) { // find radius user (kundennummer in radius info feld $radius = new RadiusDB_Client();