From e388c6e066394b1c89b13da10554f8f802fc74a2 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Thu, 18 Apr 2024 22:58:37 +0200 Subject: [PATCH] Contractqueue: commit to Contract finished --- Layout/default/Contract/Form.php | 2 +- Layout/default/Contract/Index.php | 21 +- Layout/default/Contract/ProductchangeForm.php | 38 +- Layout/default/Contract/View.php | 23 +- application/Address/Address.php | 34 +- application/Contract/Contract.php | 27 +- application/Contract/ContractController.php | 1341 +++++++++-------- application/Contract/ContractModel.php | 36 +- .../ContractLink/ContractLinkModel.php | 15 +- .../Contractqueue/ContractqueueController.php | 106 +- .../Contractqueue/ContractqueueModel.php | 6 +- public/assets/css/thetool.css | 4 + 12 files changed, 932 insertions(+), 721 deletions(-) diff --git a/Layout/default/Contract/Form.php b/Layout/default/Contract/Form.php index fc02e4c32..8ce9dc516 100644 --- a/Layout/default/Contract/Form.php +++ b/Layout/default/Contract/Form.php @@ -377,7 +377,7 @@ */ product_id && is_object(($contract->product))): ?> $('#product_id').autoComplete('set', { value: product_id?>, text: 'product_id) ? str_replace("'", "\\'", str_replace(["\n", "\r"], " ", $contract->product->name))." [".$contract->product_id."]" : ""?>'}); - product) && $contract->product->attributes["termination_required"] != 1): ?> + product->attributes) && $contract->product->attributes["termination_required"] != 1): ?> $("#termination_row").hide(); diff --git a/Layout/default/Contract/Index.php b/Layout/default/Contract/Index.php index 70f7aeba2..6db6634a5 100644 --- a/Layout/default/Contract/Index.php +++ b/Layout/default/Contract/Index.php @@ -63,7 +63,14 @@ - + +
+ + +
@@ -110,8 +117,8 @@ $contract->id])?>">id?> $contract->owner_id])?>" target="_blank"> owner->getCompanyOrName()?> (owner->customer_number?>) - $contract->id])?>">product_name?> - $contract->id])?>">matchcode?> + isFinished()) ? "not-finished" : "" ?>"> $contract->id])?>">product_name?> + isFinished()) ? "not-finished" : "" ?>"> $contract->id])?>">matchcode?> - price?> - price_setup?> - + isFinished()) ? "not-finished" : "" ?> price < 0) ? "text-danger" : ""?>">price?> + isFinished()) ? "not-finished" : "" ?> price_setup < 0) ? "text-danger" : ""?>">price_setup?> + isFinished()) ? "not-finished" : "" ?>"> billing_period == 1): ?> monatlich billing_period == 24): ?> @@ -134,7 +141,7 @@ billing_period)?>x Jährlich - finish_date) ? date('d.m.Y', $contract->finish_date) : ""?> + ">finish_date) ? date('d.m.Y', $contract->finish_date) : ""?> cancel_date) ? date('d.m.Y', $contract->cancel_date) : ""?> create)?>
creator->name?> edit)?>
editor->name?> diff --git a/Layout/default/Contract/ProductchangeForm.php b/Layout/default/Contract/ProductchangeForm.php index ea0e81b81..4ec20f25f 100644 --- a/Layout/default/Contract/ProductchangeForm.php +++ b/Layout/default/Contract/ProductchangeForm.php @@ -165,7 +165,7 @@ linkFrom) && count($contract->linkFrom)) || (is_array($contract->linkTo) && count($contract->linkTo))): ?>

Verknüpfte Verträge

- +
@@ -177,7 +177,7 @@ - links as $link): ?> + linksWithCredit as $link): ?> contract_id == $contract->id) { @@ -188,9 +188,14 @@ } ?> - - - + + + @@ -307,6 +312,29 @@ //$('#termination_id-' + id + '-line').hide(); } + // set credit-link to cancel if new product does not need credit + const attrib = p.attributes; + + let crediting_partner = false; + let crediting_rate = 0; + + if(crediting_partner in p.attributes) { + crediting_partner = parseInt(p.attributes.crediting_partner); + } + if(crediting_rate in p.attributes) { + crediting_rate = parseFloat(p.attributes.crediting_rate); + } + + let price_nne = parseFloat(p.price_nne); + //console.log(p.price_nne, crediting_partner, crediting_rate); + + if(price_nne || (crediting_partner && crediting_rate)) { + $("#link-table tr[data-link-type='credit'] input.link-keep").prop("checked", "checked"); + } + if(!price_nne && (!crediting_partner || !crediting_rate)) { + $("#link-table tr[data-link-type='credit'] input.link-cancel").prop("checked", "checked"); + } + }, 'json'); diff --git a/Layout/default/Contract/View.php b/Layout/default/Contract/View.php index 46823344d..53af585f4 100644 --- a/Layout/default/Contract/View.php +++ b/Layout/default/Contract/View.php @@ -29,12 +29,17 @@
- finish_date || $contract->finish_date > date('U')): ?> + isFinished()): ?>

In Herstellung

isCancelled()): ?>

GEKÜNDIGT

+ sla->name), "residential")): ?> +

Privatprodukt

+ +

Businessprodukt

+

">product_name?> [id?>]

@@ -232,7 +237,13 @@ type == "text" || $j->type == "phone"):?> - + text) > 120): ?> type == "file"): ?> - + type == "credit_created"): ?> @@ -344,7 +355,7 @@ $direction = "von"; $linkcontract = $link->origin; if($link->type == "credit") { - $direction = ""; + $direction = "zu"; //continue; } } else { @@ -356,7 +367,7 @@ $direction = "nach"; } if($link->type == "credit") { - $direction = "zu"; + $direction = ""; //continue; } diff --git a/application/Address/Address.php b/application/Address/Address.php index 7dbbc1ea7..ea834c6ae 100644 --- a/application/Address/Address.php +++ b/application/Address/Address.php @@ -526,7 +526,7 @@ class Address extends mfBaseModel { if($name == "permissions") { $this->permissions = NetworkAddressModel::search(['address_id' => $this->id]); - return $permissions; + return $this->permissions; } if($name == "parent") { @@ -540,16 +540,36 @@ class Address extends mfBaseModel { } if($name == "contracts") { - $owning = ContractModel::search(['owner_id' => $this->id]); - $billing = ContractModel::search(['billingaddress_id' => $this->id]); - $this->contracts = array_merge($owning, $billing); + $contracts = []; + foreach(ContractModel::search(['owner_id' => $this->id]) as $contract) { + if(!array_key_exists($contract->id, $contracts)) { + $contracts[$contract->id] = $contract; + } + } + foreach(ContractModel::search(['billingaddress_id' => $this->id]) as $contract) { + if(!array_key_exists($contract->id, $contracts)) { + $contracts[$contract->id] = $contract; + } + } + + $this->contracts = $contracts; return $this->contracts; } if($name == "active_contracts") { - $owning = ContractModel::searchActive(['owner_id' => $this->id]); - $billing = ContractModel::searchActive(['billingaddress_id' => $this->id]); - $this->contracts = array_merge($owning, $billing); + $contracts = []; + foreach(ContractModel::searchActive(['owner_id' => $this->id]) as $contract) { + if(!array_key_exists($contract->id, $contracts)) { + $contracts[$contract->id] = $contract; + } + } + foreach(ContractModel::searchActive(['billingaddress_id' => $this->id]) as $contract) { + if(!array_key_exists($contract->id, $contracts)) { + $contracts[$contract->id] = $contract; + } + } + + $this->contracts = $contracts; return $this->contracts; } diff --git a/application/Contract/Contract.php b/application/Contract/Contract.php index cfc32d4af..527395e4c 100644 --- a/application/Contract/Contract.php +++ b/application/Contract/Contract.php @@ -16,6 +16,7 @@ class Contract extends mfBaseModel { private $isCancelled; private $journals; private $links; + private $linksWithCredit; private $linkFrom; private $linkTo; private $upgradeFrom; @@ -116,6 +117,24 @@ class Contract extends mfBaseModel { return false; } + + public function isFinished() { + if(!$this->id) { + return false; + } + + $now = date('U'); + + if($this->finish_date && $this->finish_date <= $now) { + return true; + } + + if($this->cancel_date && $this->cancel_date <= $now) { + return true; + } + + return false; + } public function generateMatchcode() { $owner_address = $this->getProperty("owner")->street.", ".$this->getProperty("owner")->zip." ".$this->getProperty("owner")->city; @@ -270,7 +289,13 @@ class Contract extends mfBaseModel { //var_dump($this->links);exit; return $this->links; } - + + if($name == "linksWithCredit") { + $this->linksWithCredit = ContractLinkModel::includesContractId($this->id, ["type" => ["link", "credit"]]); + //var_dump($this->links);exit; + return $this->linksWithCredit; + } + if(in_array($name, ['linkFrom','linkTo','upgradeFrom','upgradeTo','downgradeFrom','downgradeTo','productchangeFrom','productchangeTo','relocationFrom','relocationTo'])) { if($this->$name === null) { $this->getLinks(); diff --git a/application/Contract/ContractController.php b/application/Contract/ContractController.php index 31b6d89a2..d77ec2a23 100644 --- a/application/Contract/ContractController.php +++ b/application/Contract/ContractController.php @@ -1,663 +1,706 @@ needlogin=true; - $me = new User(); - $me->loadMe(); - $this->me = $me; - $this->layout()->set("me",$me); - - if(!$me->is(["Admin"])) { - $this->redirect("Dashboard"); - } - } - - - protected function indexAction() { - $this->layout()->setTemplate("Contract/Index"); - - if($this->request->resetFilter) { - unset($_SESSION[MFAPPNAME.'-Contract-filter']); - } - - $filter = []; - if(is_array($this->request->filter)) { - $filter = $this->request->filter; - $_SESSION[MFAPPNAME.'-Contract-filter'] = $filter; - } else { - if(array_key_exists(MFAPPNAME.'-Contract-filter', $_SESSION) && count($_SESSION[MFAPPNAME.'-Contract-filter'])) { - $filter = $_SESSION[MFAPPNAME.'-Contract-filter']; - } - } - - $this->layout->set("filter", $filter); - $filter = $this->getPreparedFilter($filter); - - // pagination defaults - $pagination = []; - $pagination['start'] = 0; - $pagination['count'] = 50; - $pagination['maxItems'] = 0; - - if(is_numeric($this->request->s)) { - $pagination['start'] = intval($this->request->s); - } - //var_dump($filter);exit; - $pagination['maxItems'] = ContractModel::count($filter); - $contracts = ContractModel::search($filter, $pagination); - - $this->layout()->set("contracts", $contracts); - $this->layout()->set("pagination", $pagination); - } - - private function getPreparedFilter($filter) { - $new_filter = []; - - if(array_key_exists("show_canceled", $filter)) { - if($filter['show_canceled'] == 0) { - $new_filter['add-where'] = " AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())"; - } - } else { - $new_filter['add-where'] = " AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())"; - } - - if(is_array($filter) && count($filter)) { - foreach($filter as $name => $value) { - $new_filter[$name] = $value; - } - } - - return $new_filter; - } - - protected function viewAction() { - $this->layout()->setTemplate("Contract/View"); - - $id = $this->request->contract_id; - if(!$id) { - $id = $this->request->id; - } - if(!is_numeric($id) || !$id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $contract = new Contract($id); - if(!$contract->id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $this->layout()->set("contract", $contract); - - if($this->request->filter) { - $this->layout()->set("filter", $this->request->filter); - } - if($this->request->s) { - $this->layout()->set("filter", $this->request->s); - } - - } - - protected function productchangeAction() { - $this->layout()->setTemplate("Contract/ProductchangeForm"); - - $id = $this->request->contract_id; - if(!$id) { - $id = $this->request->id; - } - if(!is_numeric($id) || !$id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $contract = new Contract($id); - if(!$contract->id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $this->layout()->set("contract", $contract); - $this->layout()->set("terminations", TerminationModel::getAll()); - - if($this->request->filter) { - $this->layout()->set("filter", $this->request->filter); - } - if($this->request->s) { - $this->layout()->set("filter", $this->request->s); - } - } - - protected function saveProductchangeAction() { - $r = $this->request; - - $id = $r->contract_id; - if(!is_numeric($id) || !$id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $contract = new Contract($id); - if(!$contract->id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); +class ContractController extends mfBaseController +{ + + protected function init() + { + $this->needlogin = true; + $me = new User(); + $me->loadMe(); + $this->me = $me; + $this->layout()->set("me", $me); + + if (!$me->is(["Admin"])) { + $this->redirect("Dashboard"); + } } - $new_contract = clone($contract); - - - $contract_data = []; - $contract_data['product_id'] = $r->product_id; - $contract_data['product_name'] = trim($r->product_name); - $contract_data['product_info'] = trim($r->product_info); - $contract_data['matchcode'] = trim($r->matchcode); - $contract_data['termination_id'] = $r->termination_id; - $contract_data['amount'] = 1; - $contract_data['price'] = $r->price; - $contract_data['price_setup'] = $r->price_setup; - $contract_data['price_nne'] = $r->price_nne; - $contract_data['price_nbe'] = $r->price_nbe; - $contract_data['note'] = trim($r->note); - - /* - * termination check - */ - $product = new Product($r->product_id); - if(!$product->id) { - $this->layout()->setFlash("Produkt nicht gefunden", "error"); - $this->redirect("Contract", "productchange", ["contract_id" => $id]); - } - - $contract_data['product_external'] = $product->external; - $contract_data['product_external_id'] = $product->external_id; - $contract_data['sla_id'] = $product->sla_id; - - $require_term = false; - if(array_key_exists(TT_ATTRIB_TERMINATION_REQUIRED_NAME, $product->attributes) && $product->attributes[TT_ATTRIB_TERMINATION_REQUIRED_NAME]->value == 1) { - //var_dump($prod->attributes); - $require_term = true; - $termination = new Termination($contract_data['termination_id']); - - if(!$contract_data['termination_id'] || !$termination->id) { - $this->layout()->setFlash("Produkt erfordert Anschluss.", "error"); - $this->redirect("Contract", "productchange", ["contract_id" => $id]); - } - } else { - $contract_data['termination_id'] = null; - } - - - $new_contract->update($contract_data); - $new_contract_id = $new_contract->save(); - - if(!$new_contract_id) { - $this->layout()->setFlash("Neuer Contract konnte nicht gespeichert werden", "error"); - $this->redirect("Contract", "productchange", ["contract_id" => $id]); - } - - - - if(is_array($r->links) && count($r->links)) { - foreach($r->links as $link_id => $action) { - $old_link = new ContractLink($link_id); - if(!$old_link->id) continue; - $new_link_origin_id = 0; - $new_link_origin_id = 0; - // check if link contains this contract - if($old_link->contract_id == $contract->id) { - $new_link_contract_id = $new_contract->id; - $new_link_origin_id = $old_link->origin_contract_id; - } elseif($old_link->origin_contract_id == $contract->id) { - $new_link_contract_id = $old_link->contract_id; - $new_link_origin_id = $new_contract->id; + + protected function indexAction() + { + $this->layout()->setTemplate("Contract/Index"); + + if ($this->request->resetFilter) { + unset($_SESSION[MFAPPNAME . '-Contract-filter']); + } + + $filter = []; + if (is_array($this->request->filter)) { + $filter = $this->request->filter; + $_SESSION[MFAPPNAME . '-Contract-filter'] = $filter; } else { - continue; + if (array_key_exists(MFAPPNAME . '-Contract-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-Contract-filter'])) { + $filter = $_SESSION[MFAPPNAME . '-Contract-filter']; + } } - - $new_link = ContractLinkModel::create([ - 'contract_id' => $new_link_contract_id, - 'origin_contract_id' => $new_link_origin_id, - 'type' => $old_link->type, - ]); - if(!$new_link->save()) { - $this->layout()->setFlash("Konnte neuen Link nicht speichern", "warn"); + + $this->layout->set("filter", $filter); + $filter = $this->getPreparedFilter($filter); + + // pagination defaults + $pagination = []; + $pagination['start'] = 0; + $pagination['count'] = 50; + $pagination['maxItems'] = 0; + + if (is_numeric($this->request->s)) { + $pagination['start'] = intval($this->request->s); } - - if($action == "cancel") { - $old_link->change_action = "cancel"; - if(!$old_link->save()) { - $this->layout()->setFlash("Konnte alten Link nicht speichern", "warn"); - } - } - - //var_dump($new_link);exit; - } - } - - /* - * Upgrade Link erstellen - */ - $change_type = "upgrade"; - /*if($contract->product_id != $new_contract->product_id) { - $change_type = "upgrade"; - } elseif($contract->matchcode != $new_contract->matchcode) { - $change_type = "relocation"; - } else { - $change_type = "productchange"; - }*/ - - $link = ContractLinkModel::create([ - 'contract_id' => $new_contract_id, - 'origin_contract_id' => $id, - 'type' => $change_type - ]); - - $link_id = $link->save(); - if(!$link_id) { - $this->layout()->setFlash("Konnte Verknüpfung nicht speichern", "warn"); - } - - $this->layout()->setFlash("Neuer Contract erfolgreich erstellt", "success"); - $this->redirect("Contract", "view", ["contract_id" => $new_contract_id]); - - - - } - - protected function finishContractAction() { - $r = $this->request; - - $id = $r->contract_id; - if(!is_numeric($id) || !$id) { - $this->layout()->setFlash("Contract nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $contract = new Contract($id); - if(!$contract->id) { - $this->layout()->setFlash("Contract nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $now = date('U'); - - /* - * Vorgänger Contracts kündigen - */ - foreach(ContractLinkModel::search(['contract_id' => $id]) as $link) { - if(!in_array($link->type, ["upgrade","downgrade","relocation","productchange"])) { - continue; - } - - $origin = $link->origin; - $origin->cancel_date = $now; - $origin->cancel_date_by = $this->me->id; - $origin->edit_by = $this->me->id; - if(!$origin->save()) { - $this->layout()->setFlash("Achtung: Konnte nicht alle Vorgängercontracts kündigen!", "warn"); - } - - /* - * alte Links übernehmen / kündigen - */ - foreach(ContractLinkModel::search(['type' => "link", 'contract_id' => $origin->id] ) as $old_link) { - // verlinkten Contract kündigen (wenn nicht schon gekündigt) - if($old_link->change_action == "cancel" && !$old_link->contract->cancel_date) { - $old_link->origin->update([ - 'cancel_date' => $now, - 'cancel_date_by' => $this->me->id, - 'edit_by' => $this->me->id - ]); - $old_link->origin->save(); - - $old_link->change_action = null; - $old_link->save(); - } - } - - foreach(ContractLinkModel::search(['type' => "link", 'origin_contract_id' => $origin->id] ) as $old_link) { - // verlinkten Contract kündigen (wenn nicht schon gekündigt) - if($old_link->change_action == "cancel" && !$old_link->contract->cancel_date) { - $old_link->contract->update([ - 'cancel_date' => $now, - 'cancel_date_by' => $this->me->id, - 'edit_by' => $this->me->id - ]); - $old_link->contract->save(); - - $old_link->change_action = null; - $old_link->save(); - } - } - - } - - - - $contract->finish_date = $now; - $contract->finish_date_by = $this->me->id; - if(!$contract->save()) { - $this->layout()->setFlash("Contract konnte nicht gespeichert werden", "error"); - $this->redirect("Contract", "view", ['contract_id' => $id]); - } - - /* - * bestehende Links übernehmen oder kündigen - */ - - $this->layout()->setFlash("Contract erfolgreich fertiggestellt", "success"); - $this->redirect("Contract", "view", ['contract_id' => $id]); - - } - - protected function cancelAction() { - $id = $this->request->contract_id; - if(!$id) $id = $this->request->id; - - $this->layout()->setFlash("Not implemented", "error"); - if($id) { - $this->redirect("Contract","view",["id" => $id]); - } else { - $this->redirect("Contract"); - } - } - - protected function addAction() { - $this->layout()->setTemplate("Contract/Form"); - $this->layout()->set("terminations", TerminationModel::getAll()); - - if($this->request->origin_contract_id) { - $origin = new Contract($this->request->origin_contract_id); - if($origin->id) { - $contract = new Contract(); - $contract->owner_id = $origin->owner_id; - $contract->billingaddress_id = $origin->billingaddress_id; - $contract->matchcode = $origin->matchcode; - //var_dump($contract);exit; - $this->layout()->set("contract", $contract); - $this->layout()->set("origin_contract_id", $origin->id); - } - } - } - - protected function editAction() { - $id = $this->request->contract_id; - if(!$id) { - $id = $this->request->id; - } - if(!is_numeric($id) || !$id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $contract = new Contract($id); - if(!$contract->id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - - $this->layout()->set("contract", $contract); - //var_dump($contract->owner);exit; - - if($this->request->f == "view") $this->layout()->set("f", "view"); - if($this->request->f != "view") $this->layout()->set("f", "index"); - - if($this->request->filter) { - $this->layout()->set("filter", $this->request->filter); - } - if($this->request->s) { - $this->layout()->set("filter", $this->request->s); - } - - return $this->addAction(); - } - - protected function saveAction() { - $r = $this->request; - //var_dump($r); - - /* - * add or edit - */ - $id = $r->id; - if(is_numeric($id) && $id > 0) { - $mode = "edit"; - $contract = new Contract($id); - if(!$contract->id) { - $this->layout()->setFlash("Vertrag nicht gefunden", "error"); - $this->redirect("Contract"); - } - } else { - $id = false; - $mode = "add"; - } - - //var_dump($r->get());exit; - $contract_data = []; - $contract_data["owner_id"] = (int)$r->owner_id; - $contract_data["billingaddress_id"] = ($r->billingaddress_id) ? (int)$r->billingaddress_id : null; - $contract_data["product_id"] = (int)$r->product_id; - $contract_data["matchcode"] = $r->matchcode; - $contract_data["product_name"] = $r->product_name; - $contract_data["product_info"] = $r->product_info; - $contract_data['amount'] = ($r->amount) ? (float)$r->amount : 1; - $contract_data['price'] = (float)$r->price; - $contract_data['price_setup'] = (float)$r->price_setup; - $contract_data['price_nne'] = (float)$r->price_nne; - $contract_data['price_nbe'] = (float)$r->price_nbe; - $contract_data['billing_period'] = (int)$r->billing_period; - $contract_data['billing_delay'] = (int)$r->billing_delay; - $contract_data['order_date'] = ($r->order_date) ? $this->dateToTimestamp($r->order_date) : null; - $contract_data['finish_date'] = ($r->finish_date) ? $this->dateToTimestamp($r->finish_date) : null; - $contract_data['cancel_date'] = ($r->cancel_date) ? $this->dateToTimestamp($r->cancel_date) : null; - $contract_data['note'] = $r->note; - - - //var_dump($contract_data);exit; - - if($mode == "add") { - $contract = ContractModel::create($contract_data); - } else { - $contract->update($contract_data); - } - - $this->layout()->set("contract", $contract); - - if(!$contract_data["owner_id"]) { - $this->layout()->setFlash("Bitte Vertragsinhaber auswählen.", "error"); - return $this->addAction(); - } - if(!$contract_data["product_id"]) { - $this->layout()->setFlash("Bitte Produkt auswählen.", "error"); - return $this->addAction(); - } - if(!$contract_data['billing_period']) { - $this->layout()->setFlash("Bitte Rechnungsperiode auswählen.", "error"); - return $this->addAction(); - } - - if(!$contract->product_name) { - $product = new Product($contract_data["product_id"]); - if(!$product->id) { - $this->layout()->setFlash("Ungültiges Produkt.", "error"); - return $this->addAction(); - } - $contract->product_name = $product->name; - } - - //var_dump($contract);exit; - - $contract_id = $contract->save(); - if(!$contract_id) { - $this->layout()->setFlash("Fehler beim Speichern.", "error"); - $this->layout()->set("contract", $contract); - return $this->addAction(); - } - - // create journal - if($mode == "add") { - $journal = ContractjournalModel::create([ - 'contract_id' => $contract_id, - 'type' => "created_from", - 'value' => "manual" - ]); - $journal->save(); - } - - $this->layout()->setFlash("Vertrag erfolgreich gespeichert.", "success"); - - /* - * Create link to origin contract if set - */ - if($mode == "add" && $r->origin_contract_id) { - $origin = new Contract($r->origin_contract_id); - if($origin->id) { - $link = ContractLinkModel::create([ - 'contract_id' => $contract_id, - 'origin_contract_id' => $origin->id, - 'type' => 'link' - ]); - $link_id = $link->save(); - if($link_id) { - $journal = ContractjournalModel::create([ - 'contract_id' => $contract_id, - 'type' => "link", - 'value' => $origin->id - ]); - $journal->save(); - - $ojournal = ContractjournalModel::create([ - 'contract_id' => $origin->id, - 'type' => "link", - 'value' => $contract_id - ]); - $ojournal->save(); - } - } - } - - - /* ContractLinks */ - - - $query = []; - if($r->s) { - $query['s'] = $r->s; - } - if($r->filter) { - $query["filter"] = $r->filter; - } - if($r->return != "index") { - $query['id'] = $contract_id; - } - - $qs = http_build_query($query); - - if($mode == "add" || $r->f == "view") { - $this->redirect("Contract", "view", $qs, "contract=$contract_id"); - } else { - $this->redirect("Contract", "Index", $qs); - } - - } - - protected function apiAction() { - if(!$this->me->is(["Admin"])) { - $this->redirect("Dashboard"); - } - $do = $this->request->do; - $data = []; - - switch($do) { - case "getContract": - $return = $this->getContractApi(); - break; - case "findContract": - $return = $this->findContractApi(); - default: - $return = false; - } - - if(!is_array($return) || !count($return)) { - $data = ["status" => "error"]; - $this->returnJson($data); - } - $data['status'] = "OK"; - $data['result'] = $return; - $this->returnJson($data); - } - - private function getContractApi() { - $contract_id = $this->request->contract_id; - if(!is_numeric($contract_id) || $contract_id < 1) { - return false; - } - - $form_id = false; - if($this->request->form_id) { - $form_id = $this->request->form_id; - } - - $contract = new Contract($contract_id); - if(!$contract->id) { - return false; - } - - $data = $contract->toArray(); - - return ["contract" => $data, "form_id" => $form_id]; - } - - private function findContractApi() { - $search = trim($this->request->q); - $autocomplete = $this->request->autocomplete; - - $contracts = []; - - if(is_numeric($search)) { - $c = new Contract($search); - if($c->id) { - if(!array_key_exists($c->id, $contracts)) { - $contracts[$c->id] = $c; - } - } - - foreach(["id", "owner_id", "product_id"] as $search_key) { - foreach(ContractModel::search([$search_key => $search]) as $c) { - if(!array_key_exists($c->id, $contracts)) { - $contracts[$c->id] = $c; - } - } - } + //var_dump($filter);exit; + $pagination['maxItems'] = ContractModel::count($filter); + $contracts = ContractModel::search($filter, $pagination); + + $this->layout()->set("contracts", $contracts); + $this->layout()->set("pagination", $pagination); } - foreach(["product_name", "matchcode", "owner"] as $search_key) { - foreach(ContractModel::search([$search_key => $search]) as $c) { - if(!array_key_exists($c->id, $contracts)) { - $contracts[$c->id] = $c; + private function getPreparedFilter($filter) + { + $new_filter = []; + + if (array_key_exists("show_canceled", $filter)) { + if ($filter['show_canceled'] == 0) { + $new_filter['add-where'] = " AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())"; + } + } else { + $new_filter['add-where'] = " AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())"; } - } + + if (array_key_exists("show_credit", $filter)) { + if ($filter["show_credit"] == 0) { + $new_filter["price>="] = 0; + } + unset($filter["show_credit"]); + } else { + $new_filter["price>="] = 0; + } + + if (is_array($filter) && count($filter)) { + foreach ($filter as $name => $value) { + $new_filter[$name] = $value; + } + } + + return $new_filter; } - - if(!is_array($contracts) && !count($contracts)) { - return false; + + protected function viewAction() + { + $this->layout()->setTemplate("Contract/View"); + + $id = $this->request->contract_id; + if (!$id) { + $id = $this->request->id; + } + if (!is_numeric($id) || !$id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $contract = new Contract($id); + if (!$contract->id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $this->layout()->set("contract", $contract); + + if ($this->request->filter) { + $this->layout()->set("filter", $this->request->filter); + } + if ($this->request->s) { + $this->layout()->set("filter", $this->request->s); + } + } - - $results = []; - - // return bootstrap-autocomplete format - foreach($contracts as $contract) { - //$result = ['value' => $contract->id, 'text' => str_replace("'", "\\'", str_replace(["\n", "\r"], " ",$contract->name))]; - $result = ['value' => $contract->id, 'text' => $contract->id.": ".$contract->product_name." [".$contract->matchcode."] (".$contract->owner->getCompanyOrName().", ".$contract->owner->street.", ".$contract->owner->zip." ".$contract->owner->city.")"]; - - $results[] = $result; - if(count($results) > 15) { - $results[] = ['value' => 0, 'text' => "  -->   Mehr Suchergebnisse vorhanden. Bitte Suchbegriff genauer definieren   <--"]; - break; - } + + protected function productchangeAction() + { + $this->layout()->setTemplate("Contract/ProductchangeForm"); + + $id = $this->request->contract_id; + if (!$id) { + $id = $this->request->id; + } + if (!is_numeric($id) || !$id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $contract = new Contract($id); + if (!$contract->id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $this->layout()->set("contract", $contract); + $this->layout()->set("terminations", TerminationModel::getAll()); + + if ($this->request->filter) { + $this->layout()->set("filter", $this->request->filter); + } + if ($this->request->s) { + $this->layout()->set("filter", $this->request->s); + } + } + + protected function saveProductchangeAction() + { + $r = $this->request; + + $id = $r->contract_id; + if (!is_numeric($id) || !$id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $contract = new Contract($id); + if (!$contract->id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $new_contract = clone($contract); + + + $contract_data = []; + $contract_data['product_id'] = $r->product_id; + $contract_data['product_name'] = trim($r->product_name); + $contract_data['product_info'] = trim($r->product_info); + $contract_data['matchcode'] = trim($r->matchcode); + $contract_data['termination_id'] = $r->termination_id; + $contract_data['amount'] = 1; + $contract_data['price'] = $r->price; + $contract_data['price_setup'] = $r->price_setup; + $contract_data['price_nne'] = $r->price_nne; + $contract_data['price_nbe'] = $r->price_nbe; + $contract_data['note'] = trim($r->note); + + /* + * termination check + */ + $product = new Product($r->product_id); + if (!$product->id) { + $this->layout()->setFlash("Produkt nicht gefunden", "error"); + $this->redirect("Contract", "productchange", ["contract_id" => $id]); + } + + $contract_data['product_external'] = $product->external; + $contract_data['product_external_id'] = $product->external_id; + $contract_data['sla_id'] = $product->sla_id; + + $require_term = false; + if (array_key_exists(TT_ATTRIB_TERMINATION_REQUIRED_NAME, $product->attributes) && $product->attributes[TT_ATTRIB_TERMINATION_REQUIRED_NAME]->value == 1) { + //var_dump($prod->attributes); + $require_term = true; + $termination = new Termination($contract_data['termination_id']); + + if (!$contract_data['termination_id'] || !$termination->id) { + $this->layout()->setFlash("Produkt erfordert Anschluss.", "error"); + $this->redirect("Contract", "productchange", ["contract_id" => $id]); + } + } else { + $contract_data['termination_id'] = null; + } + + + $new_contract->update($contract_data); + $new_contract_id = $new_contract->save(); + + if (!$new_contract_id) { + $this->layout()->setFlash("Neuer Contract konnte nicht gespeichert werden", "error"); + $this->redirect("Contract", "productchange", ["contract_id" => $id]); + } + + + if (is_array($r->links) && count($r->links)) { + foreach ($r->links as $link_id => $action) { + $old_link = new ContractLink($link_id); + if (!$old_link->id) continue; + + // check if link contains this contract + if ($old_link->contract_id == $contract->id) { + $new_link_contract_id = $new_contract->id; + $new_link_origin_id = $old_link->origin_contract_id; + } elseif ($old_link->origin_contract_id == $contract->id) { + $new_link_contract_id = $old_link->contract_id; + $new_link_origin_id = $new_contract->id; + } else { + continue; + } + + $new_link = ContractLinkModel::create([ + 'contract_id' => $new_link_contract_id, + 'origin_contract_id' => $new_link_origin_id, + 'type' => $old_link->type, + ]); + if (!$new_link->save()) { + $this->layout()->setFlash("Konnte neuen Link nicht speichern", "warn"); + } + + if ($action == "cancel") { + $old_link->change_action = "cancel"; + if (!$old_link->save()) { + $this->layout()->setFlash("Konnte alten Link nicht speichern", "warn"); + } + } + + if ($old_link->type == "credit" && $action == "keep") { + $old_link->change_action = "recreate"; + } + + //var_dump($new_link);exit; + } + } + + /* + * Upgrade Link erstellen + */ + $change_type = "upgrade"; + /*if($contract->product_id != $new_contract->product_id) { + $change_type = "upgrade"; + } elseif($contract->matchcode != $new_contract->matchcode) { + $change_type = "relocation"; + } else { + $change_type = "productchange"; + }*/ + + $link = ContractLinkModel::create([ + 'contract_id' => $new_contract_id, + 'origin_contract_id' => $id, + 'type' => $change_type + ]); + + $link_id = $link->save(); + if (!$link_id) { + $this->layout()->setFlash("Konnte Verknüpfung nicht speichern", "warn"); + } + + $this->layout()->setFlash("Neuer Contract erfolgreich erstellt", "success"); + $this->redirect("Contract", "view", ["contract_id" => $new_contract_id]); + + + } + + protected function finishContractAction() + { + $r = $this->request; + + $id = $r->contract_id; + if (!is_numeric($id) || !$id) { + $this->layout()->setFlash("Contract nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $contract = new Contract($id); + if (!$contract->id) { + $this->layout()->setFlash("Contract nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $now = date('U'); + + /* + * Vorgänger Contracts kündigen + */ + foreach (ContractLinkModel::search(['contract_id' => $id]) as $link) { + if (!in_array($link->type, ["upgrade", "downgrade", "relocation", "productchange"])) { + continue; + } + + $origin = $link->origin; + $origin->cancel_date = $now; + $origin->cancel_date_by = $this->me->id; + $origin->edit_by = $this->me->id; + if (!$origin->save()) { + $this->layout()->setFlash("Achtung: Konnte nicht alle Vorgängercontracts kündigen!", "warn"); + } + + /* + * alte Links übernehmen / kündigen + */ + foreach (ContractLinkModel::search(['type' => "link", 'contract_id' => $origin->id]) as $old_link) { + // verlinkten Contract kündigen (wenn nicht schon gekündigt) + if ($old_link->change_action == "cancel" && !$old_link->contract->cancel_date) { + $old_link->origin->update([ + 'cancel_date' => $now, + 'cancel_date_by' => $this->me->id, + 'edit_by' => $this->me->id + ]); + $old_link->origin->save(); + + $old_link->change_action = null; + $old_link->save(); + } + } + + foreach (ContractLinkModel::search(['type' => "link", 'origin_contract_id' => $origin->id]) as $old_link) { + // verlinkten Contract kündigen (wenn nicht schon gekündigt) + if ($old_link->change_action == "cancel" && !$old_link->contract->cancel_date) { + $old_link->contract->update([ + 'cancel_date' => $now, + 'cancel_date_by' => $this->me->id, + 'edit_by' => $this->me->id + ]); + $old_link->contract->save(); + + $old_link->change_action = null; + $old_link->save(); + } + } + + /* + * TODO: alten Credit Vertag kündigen und neuen anlegen + */ + foreach (ContractLinkModel::search(['type' => "credit", 'contract_id' => $origin->id]) as $old_credit) { + // verlinkten Contract kündigen (wenn nicht schon gekündigt) + if ($old_credit->change_action == "recreate" && !$old_credit->contract->cancel_date) { + $old_credit->origin->update([ + 'cancel_date' => $now, + 'cancel_date_by' => $this->me->id, + 'edit_by' => $this->me->id + ]); + $old_credit->origin->save(); + + $old_credit->change_action = null; + $old_credit->save(); + } + } + + + } + + + $contract->finish_date = $now; + $contract->finish_date_by = $this->me->id; + if (!$contract->save()) { + $this->layout()->setFlash("Contract konnte nicht gespeichert werden", "error"); + $this->redirect("Contract", "view", ['contract_id' => $id]); + } + + /* + * bestehende Links übernehmen oder kündigen + */ + + $this->layout()->setFlash("Contract erfolgreich fertiggestellt", "success"); + $this->redirect("Contract", "view", ['contract_id' => $id]); + + } + + protected function cancelAction() + { + $id = $this->request->contract_id; + if (!$id) $id = $this->request->id; + + $this->layout()->setFlash("Not implemented", "error"); + if ($id) { + $this->redirect("Contract", "view", ["id" => $id]); + } else { + $this->redirect("Contract"); + } + } + + protected function addAction() + { + $this->layout()->setTemplate("Contract/Form"); + $this->layout()->set("terminations", TerminationModel::getAll()); + + if ($this->request->origin_contract_id) { + $origin = new Contract($this->request->origin_contract_id); + if ($origin->id) { + $contract = new Contract(); + $contract->owner_id = $origin->owner_id; + $contract->billingaddress_id = $origin->billingaddress_id; + $contract->matchcode = $origin->matchcode; + //var_dump($contract);exit; + $this->layout()->set("contract", $contract); + $this->layout()->set("origin_contract_id", $origin->id); + } + } + } + + protected function editAction() + { + $id = $this->request->contract_id; + if (!$id) { + $id = $this->request->id; + } + if (!is_numeric($id) || !$id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $contract = new Contract($id); + if (!$contract->id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + + $this->layout()->set("contract", $contract); + //var_dump($contract->owner);exit; + + if ($this->request->f == "view") $this->layout()->set("f", "view"); + if ($this->request->f != "view") $this->layout()->set("f", "index"); + + if ($this->request->filter) { + $this->layout()->set("filter", $this->request->filter); + } + if ($this->request->s) { + $this->layout()->set("filter", $this->request->s); + } + + return $this->addAction(); + } + + protected function saveAction() + { + $r = $this->request; + //var_dump($r); + + /* + * add or edit + */ + $id = $r->id; + if (is_numeric($id) && $id > 0) { + $mode = "edit"; + $contract = new Contract($id); + if (!$contract->id) { + $this->layout()->setFlash("Vertrag nicht gefunden", "error"); + $this->redirect("Contract"); + } + } else { + $id = false; + $mode = "add"; + } + + //var_dump($r->get());exit; + $contract_data = []; + $contract_data["owner_id"] = (int)$r->owner_id; + $contract_data["billingaddress_id"] = ($r->billingaddress_id) ? (int)$r->billingaddress_id : null; + $contract_data["product_id"] = (int)$r->product_id; + $contract_data["matchcode"] = $r->matchcode; + $contract_data["product_name"] = $r->product_name; + $contract_data["product_info"] = $r->product_info; + $contract_data['amount'] = ($r->amount) ? (float)$r->amount : 1; + $contract_data['price'] = (float)$r->price; + $contract_data['price_setup'] = (float)$r->price_setup; + $contract_data['price_nne'] = (float)$r->price_nne; + $contract_data['price_nbe'] = (float)$r->price_nbe; + $contract_data['billing_period'] = (int)$r->billing_period; + $contract_data['billing_delay'] = (int)$r->billing_delay; + $contract_data['order_date'] = ($r->order_date) ? $this->dateToTimestamp($r->order_date) : null; + $contract_data['finish_date'] = ($r->finish_date) ? $this->dateToTimestamp($r->finish_date) : null; + $contract_data['cancel_date'] = ($r->cancel_date) ? $this->dateToTimestamp($r->cancel_date) : null; + $contract_data['note'] = $r->note; + + + //var_dump($contract_data);exit; + + if ($mode == "add") { + $contract = ContractModel::create($contract_data); + } else { + $contract->update($contract_data); + } + + $this->layout()->set("contract", $contract); + + if (!$contract_data["owner_id"]) { + $this->layout()->setFlash("Bitte Vertragsinhaber auswählen.", "error"); + return $this->addAction(); + } + if (!$contract_data["product_id"]) { + $this->layout()->setFlash("Bitte Produkt auswählen.", "error"); + return $this->addAction(); + } + if (!$contract_data['billing_period']) { + $this->layout()->setFlash("Bitte Rechnungsperiode auswählen.", "error"); + return $this->addAction(); + } + + if (!$contract->product_name) { + $product = new Product($contract_data["product_id"]); + if (!$product->id) { + $this->layout()->setFlash("Ungültiges Produkt.", "error"); + return $this->addAction(); + } + $contract->product_name = $product->name; + } + + //var_dump($contract);exit; + + $contract_id = $contract->save(); + if (!$contract_id) { + $this->layout()->setFlash("Fehler beim Speichern.", "error"); + $this->layout()->set("contract", $contract); + return $this->addAction(); + } + + // create journal + if ($mode == "add") { + $journal = ContractjournalModel::create([ + 'contract_id' => $contract_id, + 'type' => "created_from", + 'value' => "manual" + ]); + $journal->save(); + } + + $this->layout()->setFlash("Vertrag erfolgreich gespeichert.", "success"); + + /* + * Create link to origin contract if set + */ + if ($mode == "add" && $r->origin_contract_id) { + $origin = new Contract($r->origin_contract_id); + if ($origin->id) { + $link = ContractLinkModel::create([ + 'contract_id' => $contract_id, + 'origin_contract_id' => $origin->id, + 'type' => 'link' + ]); + $link_id = $link->save(); + if ($link_id) { + $journal = ContractjournalModel::create([ + 'contract_id' => $contract_id, + 'type' => "link", + 'value' => $origin->id + ]); + $journal->save(); + + $ojournal = ContractjournalModel::create([ + 'contract_id' => $origin->id, + 'type' => "link", + 'value' => $contract_id + ]); + $ojournal->save(); + } + } + } + + + /* ContractLinks */ + + + $query = []; + if ($r->s) { + $query['s'] = $r->s; + } + if ($r->filter) { + $query["filter"] = $r->filter; + } + if ($r->return != "index") { + $query['id'] = $contract_id; + } + + $qs = http_build_query($query); + + if ($mode == "add" || $r->f == "view") { + $this->redirect("Contract", "view", $qs, "contract=$contract_id"); + } else { + $this->redirect("Contract", "Index", $qs); + } + + } + + protected function apiAction() + { + if (!$this->me->is(["Admin"])) { + $this->redirect("Dashboard"); + } + $do = $this->request->do; + $data = []; + + switch ($do) { + case "getContract": + $return = $this->getContractApi(); + break; + case "findContract": + $return = $this->findContractApi(); + default: + $return = false; + } + + if (!is_array($return) || !count($return)) { + $data = ["status" => "error"]; + $this->returnJson($data); + } + $data['status'] = "OK"; + $data['result'] = $return; + $this->returnJson($data); + } + + private function getContractApi() + { + $contract_id = $this->request->contract_id; + if (!is_numeric($contract_id) || $contract_id < 1) { + return false; + } + + $form_id = false; + if ($this->request->form_id) { + $form_id = $this->request->form_id; + } + + $contract = new Contract($contract_id); + if (!$contract->id) { + return false; + } + + $data = $contract->toArray(); + + return ["contract" => $data, "form_id" => $form_id]; + } + + private function findContractApi() + { + $search = trim($this->request->q); + $autocomplete = $this->request->autocomplete; + + $contracts = []; + + if (is_numeric($search)) { + $c = new Contract($search); + if ($c->id) { + if (!array_key_exists($c->id, $contracts)) { + $contracts[$c->id] = $c; + } + } + + foreach (["id", "owner_id", "product_id"] as $search_key) { + foreach (ContractModel::search([$search_key => $search]) as $c) { + if (!array_key_exists($c->id, $contracts)) { + $contracts[$c->id] = $c; + } + } + } + } + + foreach (["product_name", "matchcode", "owner"] as $search_key) { + foreach (ContractModel::search([$search_key => $search]) as $c) { + if (!array_key_exists($c->id, $contracts)) { + $contracts[$c->id] = $c; + } + } + } + + if (!is_array($contracts) && !count($contracts)) { + return false; + } + + $results = []; + + // return bootstrap-autocomplete format + foreach ($contracts as $contract) { + //$result = ['value' => $contract->id, 'text' => str_replace("'", "\\'", str_replace(["\n", "\r"], " ",$contract->name))]; + $result = ['value' => $contract->id, 'text' => $contract->id . ": " . $contract->product_name . " [" . $contract->matchcode . "] (" . $contract->owner->getCompanyOrName() . ", " . $contract->owner->street . ", " . $contract->owner->zip . " " . $contract->owner->city . ")"]; + + $results[] = $result; + if (count($results) > 15) { + $results[] = ['value' => 0, 'text' => "  -->   Mehr Suchergebnisse vorhanden. Bitte Suchbegriff genauer definieren   <--"]; + break; + } + } + $this->returnJson($results); } - $this->returnJson($results); - } } \ No newline at end of file diff --git a/application/Contract/ContractModel.php b/application/Contract/ContractModel.php index 276a60f0a..036a2c765 100644 --- a/application/Contract/ContractModel.php +++ b/application/Contract/ContractModel.php @@ -214,7 +214,7 @@ class ContractModel { LEFT JOIN Product ON (Contract.product_id = Product.id) WHERE $where GROUP BY Contract.id - ORDER BY Contract.owner_id,Contract.product_id,Contract.`create` + ORDER BY Contract.`create`,Contract.id LIMIT 1"; //var_dump($sql);exit; $res = $db->query($sql); @@ -269,12 +269,12 @@ class ContractModel { WHERE $where AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP()) GROUP BY Contract.id - ORDER BY Contract.owner_id,Contract.`create`"; + ORDER BY Contract.`create`,Contract.id"; if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; - } elseif(is_numeric($count)) { + } elseif(is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['count']; } } @@ -328,12 +328,12 @@ class ContractModel { LEFT JOIN Product ON (Contract.product_id = Product.id) WHERE $where GROUP BY Contract.id - ORDER BY Contract.owner_id,Contract.`create`"; + ORDER BY Contract.`create`,Contract.id"; if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; - } elseif(is_numeric($count)) { + } elseif(is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['count']; } } @@ -441,6 +441,32 @@ class ContractModel { $where .= " AND Contract.imported_data like '$imported_data'"; } } + + if(array_key_exists("price<", $filter)) { + $price = $filter['price<']; + if(is_numeric($price)) { + $where .= " AND Contract.price < $price"; + } + } + if(array_key_exists("price<=", $filter)) { + $price = $filter['price<=']; + if(is_numeric($price)) { + $where .= " AND Contract.price <= $price"; + } + } + + if(array_key_exists("price>", $filter)) { + $price = $filter['price>']; + if(is_numeric($price)) { + $where .= " AND Contract.price > $price"; + } + } + if(array_key_exists("price>=", $filter)) { + $price = $filter['price>=']; + if(is_numeric($price)) { + $where .= " AND Contract.price >= $price"; + } + } if(array_key_exists("add-where", $filter)) { $where .= " ".$filter['add-where']; diff --git a/application/ContractLink/ContractLinkModel.php b/application/ContractLink/ContractLinkModel.php index a3a7cfc30..b3c2f4170 100644 --- a/application/ContractLink/ContractLinkModel.php +++ b/application/ContractLink/ContractLinkModel.php @@ -160,8 +160,19 @@ class ContractLinkModel { $where .= " AND ContractLink.origin_contract_id = '$origin_contract_id'"; } } - - + + if(array_key_exists("type", $filter)) { + $type = $filter["type"]; + if(is_array($type) && count($type)) { + $where .= " AND ContractLink.type IN ('".join("','", $type)."')"; + } else { + $type = $db->escape($filter['type']); + if($type) { + $where .= " AND ContractLink.type = '$type'"; + } + } + + } //var_dump($filter, $where);exit; diff --git a/application/Contractqueue/ContractqueueController.php b/application/Contractqueue/ContractqueueController.php index 0eba9d837..03946b702 100644 --- a/application/Contractqueue/ContractqueueController.php +++ b/application/Contractqueue/ContractqueueController.php @@ -83,19 +83,27 @@ class ContractqueueController extends mfBaseController { if(ContractqueueModel::getFirst(["order_id" => $order->id])) { continue; } - + + $skip_order = false; $contracts = []; $primary_matchcode = false; - + foreach($order->products as $op) { $contract = ContractModel::getFirst(["orderproduct_id" => $op->id]); if(!$contract) { $contract = ContractqueueModel::getFirst(["orderproduct_id" => $op->id]); } - if($contract && $contract->matchcode) { - $primary_matchcode = $contract->matchcode; - continue; + if($contract) { + if($contract->matchcode) { + $primary_matchcode = $contract->matchcode; + } + continue; + } + + if($op->product->external) { + $skip_order = true; + continue; } // if contract does not exist yet, create Contractqueue @@ -126,7 +134,11 @@ class ContractqueueController extends mfBaseController { $contract->crediting_matchcode = $order->owner->getCompanyOrName().", ".$order->owner->street.", ".$order->owner->zip." ".$order->owner->city; $contracts[] = $contract; } - + + if($skip_order) { + $contracts = []; + continue; + } if(!$primary_matchcode) { $primary_matchcode = $order->owner->street.", ".$order->owner->zip." ".$order->owner->city; @@ -176,22 +188,41 @@ class ContractqueueController extends mfBaseController { $new_contracts = []; $c = 0; $o = 0; + $last_order_id = 0; + foreach(ContractqueueModel::search(["approved" => true, "contract_id" => null]) as $cq) { //var_dump($cq);exit; - $contract = new Contract($cq->orderproduct_id); - if($contract->id) continue; // contract should not yet exist + $contract = ContractModel::getFirst(["orderproduct_id" => $cq->orderproduct_id]); + if($contract) { + $this->log->debug("Contract von orderproduct ".$cq->orderproduct_id." existiert schon: ".$contract->id); + continue; + } // contract should not yet exist + + $order_id = $cq->orderproduct->order_id; + + if($c >= 2000) { + if($order_id != $last_order_id) { + $this->layout()->setFlash("$c Contracts (exkl. Gutschriften) erstellt", "info"); + $this->redirect("Contractqueue"); + } + } + + + $last_order_id = $order_id; /* * Create Contract */ $contract = ContractModel::createFromContractqueue($cq); if(!$contract->save()) { - $this->layout()->setFlash("Eine Position in Bestellung ".$cq->order_id." konnte nicht übernommen werden: Fehler beim Speichern"); + $this->log->debug("Eine Position in Bestellung ".$cq->order_id." konnte nicht übernommen werden: Fehler beim Speichern"); + $this->layout()->setFlash("Eine Position in Bestellung ".$cq->order_id." konnte nicht übernommen werden: Fehler beim Speichern", "warning"); continue; } $c++; + $cq->contract_id = $contract->id; $cq->save(); @@ -213,7 +244,8 @@ class ContractqueueController extends mfBaseController { if($cq->approved_credit) { $credit = ContractModel::createFromContractQueue($cq, "credit"); if(!$credit->save()) { - $this->layout()->setFlash("Zu einer Position in Bestellung ".$cq->order_id." konnte keine Gutschrift erstellt werden: Fehler beim Speichern"); + $this->log->log("Zu einer Position in Bestellung ".$cq->order_id." konnte keine Gutschrift erstellt werden: Fehler beim Speichern"); + $this->layout()->setFlash("Zu einer Position in Bestellung ".$cq->order_id." konnte keine Gutschrift erstellt werden: Fehler beim Speichern", "warning"); continue; } $journal = ContractjournalModel::create([ @@ -229,8 +261,8 @@ class ContractqueueController extends mfBaseController { ]); $journal->save(); $link = ContractLinkModel::create([ - 'contract_id' => $contract->id, - 'origin_contract_id' => $credit->id, + 'contract_id' => $credit->id, + 'origin_contract_id' => $contract->id, 'type' => 'credit' ]); $link->save(); @@ -240,39 +272,43 @@ class ContractqueueController extends mfBaseController { /* * Create ContractLinks */ - foreach($new_contracts as $origin) { - if(ContractLinkModel::getFirst(["contract_id" => $contract->id, "origin_contract_id" => $origin->id])) { - continue; - } - $link = ContractLinkModel::create([ - 'contract_id' => $contract->id, - 'origin_contract_id' => $origin->id, - 'type' => 'link' - ]); - $link->save(); - if($link->id) { - $journal = ContractjournalModel::create([ + if(array_key_exists($order_id, $new_contracts)) { + foreach ($new_contracts[$order_id] as $origin) { + if (ContractLinkModel::getFirst(["contract_id" => $contract->id, "origin_contract_id" => $origin->id])) { + continue; + } + $link = ContractLinkModel::create([ 'contract_id' => $contract->id, - 'type' => "link", - 'value' => $origin->id + 'origin_contract_id' => $origin->id, + 'type' => 'link' ]); - $journal->save(); + $link->save(); + if ($link->id) { + $journal = ContractjournalModel::create([ + 'contract_id' => $contract->id, + 'type' => "link", + 'value' => $origin->id + ]); + $journal->save(); - $ojournal = ContractjournalModel::create([ - 'contract_id' => $origin->id, - 'type' => "link", - 'value' => $contract->id - ]); - $ojournal->save(); + $ojournal = ContractjournalModel::create([ + 'contract_id' => $origin->id, + 'type' => "link", + 'value' => $contract->id + ]); + $ojournal->save(); + } } + } else { + $new_contracts[$order_id] = []; } - $new_contracts[] = $contract; + $new_contracts[$order_id][] = $contract; } $this->layout()->setFlash("$c Contracts erstellt", "success"); - $this->redirect("Contract"); + $this->redirect("Contractqueue"); } diff --git a/application/Contractqueue/ContractqueueModel.php b/application/Contractqueue/ContractqueueModel.php index b6d5d1aaf..1d8f85f67 100644 --- a/application/Contractqueue/ContractqueueModel.php +++ b/application/Contractqueue/ContractqueueModel.php @@ -141,7 +141,7 @@ class ContractqueueModel { LEFT JOIN Product ON (Contractqueue.product_id = Product.id) WHERE $where GROUP BY Contractqueue.id - ORDER BY Contractqueue.owner_id,Contractqueue.product_id,Contractqueue.`create` + ORDER BY Contractqueue.order_id,Contractqueue.orderproduct_id,Contractqueue.owner_id,Contractqueue.product_id,Contractqueue.`create` LIMIT 1"; //var_dump($sql);exit; $res = $db->query($sql); @@ -196,7 +196,7 @@ class ContractqueueModel { WHERE $where AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP()) GROUP BY Contractqueue.id - ORDER BY Contractqueue.owner_id,Contractqueue.`create`"; + ORDER BY Contractqueue.order_id,Contractqueue.orderproduct_id,Contractqueue.owner_id,Contractqueue.`create`"; if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { @@ -231,7 +231,7 @@ class ContractqueueModel { LEFT JOIN Product ON (Contractqueue.product_id = Product.id) WHERE $where GROUP BY Contractqueue.id - ORDER BY Contractqueue.owner_id,Contractqueue.`create`"; + ORDER BY Contractqueue.order_id,Contractqueue.orderproduct_id,Contractqueue.owner_id,Contractqueue.`create`"; if(is_array($limit) && count($limit)) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) { diff --git a/public/assets/css/thetool.css b/public/assets/css/thetool.css index f801ca836..861beff97 100644 --- a/public/assets/css/thetool.css +++ b/public/assets/css/thetool.css @@ -145,6 +145,10 @@ h1, h2, h3, h4, h5, h6 { text-decoration: line-through; } +.not-finished { + opacity: 0.7; +} + /* tinymce */ .tox-editor-header, .tox-menubar,
create)?> (creator?>) text-white p-1"> + type == "text"): ?> + + + + + text), 120)?> @@ -246,7 +257,7 @@ text): ?> text), 128)?>
@@ -262,7 +273,7 @@ value == "import"): ?> Vertrag importiert: text))?> value == "order"): ?> - Vertrag aus Bestellung # $contract->orderproduct->order_id])?>">orderproduct->order_id?> erstellt + Vertrag aus Bestellung $contract->orderproduct->order_id])?>">#orderproduct->order_id?> erstellt