From d6e2f71c9fa5822b404eb365f7972c6f6b7dbde2 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Thu, 11 May 2023 13:09:35 +0200 Subject: [PATCH] Added temporary script to create Contracts from Orders --- Layout/default/Contract/Index.php | 2 +- application/Contract/Contract.php | 31 ++++++++++--- application/Contract/ContractModel.php | 52 ++++++++++++++++++++++ scripts/contract/order2contract.php | 60 ++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 scripts/contract/order2contract.php diff --git a/Layout/default/Contract/Index.php b/Layout/default/Contract/Index.php index 9e190f367..f7140565e 100644 --- a/Layout/default/Contract/Index.php +++ b/Layout/default/Contract/Index.php @@ -141,7 +141,7 @@ $contract->id, "s" => $s, "filter" => $filter])?>"> $contract->id, "s" => $s, "filter" => $filter])?>"> - orderproduct_id): ?> $contract->orderproduct->order_id])?>"> + orderproduct_id): ?> $contract->orderproduct->order_id])?>" target="_blank"> diff --git a/application/Contract/Contract.php b/application/Contract/Contract.php index 7cdda9229..0d4be125c 100644 --- a/application/Contract/Contract.php +++ b/application/Contract/Contract.php @@ -111,6 +111,22 @@ class Contract extends mfBaseModel { return false; } + public function generateMatchcode() { + $owner_address = $this->getProperty("owner")->street.", ".$this->getProperty("owner")->zip." ".$this->getProperty("owner")->city; + + if($this->termination_id) { + $termination = new Termination($this->termination_id); + $termination_address = $termination->building->street.", ".$termination->building->zip." ".$termination->building->city; + return $termination_address; + } + + if(stripos($this->getProperty("product")->name, "dsl") != false) { + return $owner_address; + } + + return false; + } + public function getProperty($name) { if($this->$name == null) { @@ -136,12 +152,6 @@ class Contract extends mfBaseModel { return $this->owner; } - - if(!$this->id) { - return null; - } - - if($name == "product") { $this->product = mfValuecache::singleton()->get("mfObjectmodel-Product-".$this->product_id); if($this->product === null) { @@ -164,6 +174,14 @@ class Contract extends mfBaseModel { return $this->orderproduct; } + + if(!$this->id) { + return null; + } + + + + /*if($name == "contractConfigGroups") { $product = $this->getProperty("product"); $this->contractConfigGroups = ContractconfigGroupModel::search(['producttech_id' => $product->producttech_id]); @@ -265,7 +283,6 @@ class Contract extends mfBaseModel { return $this->editor; } - $classname = ucfirst($name); $idfield = $name."_id"; $this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield); diff --git a/application/Contract/ContractModel.php b/application/Contract/ContractModel.php index c05182549..a37b3a085 100644 --- a/application/Contract/ContractModel.php +++ b/application/Contract/ContractModel.php @@ -54,6 +54,58 @@ class ContractModel { return $model; } + public static function createFromOrderproduct($op) { + $log = mfLoghandler::singleton(); + + if(!$op->id) { + $log->warning(__METHOD__."(): Invalid OrderProduct object"); + return false; + } + + $order = $op->order; + $product = $op->product; + + if(!$order->id || !$product->id) { + $log->warning(__METHOD__."(): Invalid Order or Product"); + return false; + } + + if(!$order->finish_date || $order->finish_date > date("U")) { + $log->warning(__METHOD__."(): Order not finished yet"); + return false; + } + + $data = []; + $data["orderproduct_id"] = $op->id; + $data["owner_id"] = $order->owner_id; + $data["billingaddress_id"] = $order->billingaddress_id; + $data["termination_id"] = ($op->termination_id) ? $op->termination_id : null; + $data["product_id"] = $op->product_id; + $data["product_name"] = $product->name; + $data["product_info"] = $product->description; + $data["amount"] = $op->amount; + $data["sla_id"] = $product->sla_id; + $data["produt_external"] = ($product->external) ? $product->external : 0; + $data["product_external_id"] = ($product->external) ? $product->external_id : null; + $data["price"] = $op->price; + $data["price_setup"] = $op->price_setup; + $data["price_nne"] = $op->price_nne; + $data["price_nbe"] = $op->price_nbe; + $data["billing_delay"] = $op->billing_delay; + $data["billing_period"] = $op->billing_period; + $data["order_date"] = $order->order_date; + + $data["finish_date"] = $order->finish_date; + $data["finish_date_by"] = 1; + $data["note"] = $order->note; + + $contract = ContractModel::create($data); + $contract->matchcode = $contract->generateMatchcode(); + //var_dump($contract);exit; + + return $contract; + } + public static function getAll() { $items = []; diff --git a/scripts/contract/order2contract.php b/scripts/contract/order2contract.php new file mode 100644 index 000000000..8e9ba6a22 --- /dev/null +++ b/scripts/contract/order2contract.php @@ -0,0 +1,60 @@ +#!/usr/bin/php + true]) as $order) { + if(!is_array($order->products) || !count($order->products)) { + echo "keine Produkte in Order ".$order->id."\n"; + continue; + } + + $contracts = []; + $primary_matchcode = false; + + // make contracts but don't save them yet + foreach($order->products as $op) { + $contract = ContractModel::getFirst(["product_id" => $op->product_id, "orderproduct_id" => $op->id]); + if($contract) { + if($contract->matchcode) { + $primary_matchcode = $contract->matchcode; + } + echo "Contract für Order ".$order->id." Produkt ".$op->product->name." (".$op->product_id.") schon vorhanden (contract_id ".$contract->id.")\n"; + continue; + } + + $contract = ContractModel::createFromOrderproduct($op); + $contract->create_by = $me->id; + $contract->edit_by = $me->id; + + if($contract->matchcode) { + $primary_matchcode = $contract->matchcode; + } + $contracts[] = $contract; + } + + if(!$primary_matchcode) { + $primary_matchcode = $order->owner->street.", ".$order->owner->zip." ".$order->owner->city; + } + + // set matchcode and save + foreach($contracts as $contract) { + if(!$contract->matchcode) { + $contract->matchcode = $primary_matchcode; + + } + //var_dump($contract);exit; + $contract->save(); + + } + exit; +} \ No newline at end of file