WIP Contract linking / productchange

This commit is contained in:
Frank Schubert
2023-01-11 15:53:13 +01:00
parent b47bf6ca0f
commit 25236493ee
15 changed files with 672 additions and 127 deletions
+288 -2
View File
@@ -67,7 +67,10 @@ class ContractController extends mfBaseController {
protected function viewAction() {
$this->layout()->setTemplate("Contract/View");
$id = $this->request->id;
$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");
@@ -90,12 +93,280 @@ class ContractController extends mfBaseController {
}
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;
$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;
} 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");
}
}
//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 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->id;
$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");
@@ -205,6 +476,21 @@ class ContractController extends mfBaseController {
$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->save();
}
}
/* ContractLinks */