#!/usr/bin/php id); define("INTERNAL_USER_USERNAME", $me->username); require_once(APPDIR . "Admin/functions/IvtContractImport.php"); $ownerIdToBillingAddress = []; $ici = new Admin_IvtContractImport(); $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.
\n"; exit; } $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; }*/ if($create) { return $new_billing; } return true; }