WIP Productchange 2024-07-16
This commit is contained in:
@@ -140,34 +140,13 @@ class ContractController extends mfBaseController
|
||||
$this->redirect("Contract", "view", ["contract_id" => $contract->id]);
|
||||
}
|
||||
|
||||
if($contract->finish_date) {
|
||||
$today = new DateTime();
|
||||
$tomorrow = clone($today);
|
||||
$tomorrow->modify("+1 day");
|
||||
$today = new DateTime();
|
||||
$tomorrow = clone($today);
|
||||
$tomorrow->modify("+1 day");
|
||||
|
||||
$finish_date = new DateTime("@".$contract->finish_date);
|
||||
$finish_date->setTimezone(new DateTimeZone("Europe/Vienna"));
|
||||
|
||||
$period_end_date = clone($finish_date);
|
||||
$period_end_date->modify("+".$contract->contract_term." months");
|
||||
|
||||
while($period_end_date->format("Y-m-d") <= $today->format("Y-m-d")) {
|
||||
$period_end_date = $finish_date->modify("+".$contract->billing_period." months");
|
||||
|
||||
}
|
||||
$period_end_date->modify("-1 day");
|
||||
|
||||
$next_billing_period = clone($finish_date);
|
||||
$next_billing_period->modify("+".$contract->billing_period." months");
|
||||
while($next_billing_period->format("Y-m-d") <= $today->format("Y-m-d")) {
|
||||
$next_billing_period->modify("+".$contract->billing_period." months");
|
||||
}
|
||||
$next_billing_period->modify("-1 day");
|
||||
|
||||
$this->layout()->set("tomorrow", $tomorrow);
|
||||
$this->layout()->set("term_end_date", $period_end_date);
|
||||
$this->layout()->set("period_end_date", $next_billing_period);
|
||||
}
|
||||
$this->layout()->set("tomorrow", $tomorrow);
|
||||
$this->layout()->set("term_end_date", $contract->getRegularCanceldate());
|
||||
$this->layout()->set("period_end_date", $contract->getNextBillingPeriodEnd());
|
||||
|
||||
|
||||
$this->layout()->set("contract", $contract);
|
||||
@@ -191,7 +170,7 @@ class ContractController extends mfBaseController
|
||||
|
||||
try {
|
||||
$cancel_date = DateTime::createFromFormat("d.m.Y", trim($r->cancel_date), new DateTimeZone("Europe/Vienna"));
|
||||
$cancel_date->setTime(2,0,0);
|
||||
$cancel_date->setTime(23,59,59);
|
||||
} catch(Exception $e) {
|
||||
$this->layout()->setFlash("Ungültiges Datumsformat");
|
||||
$this->redirect("Contract", "cancel", ["contract_id" => $contract->id]);
|
||||
@@ -288,6 +267,7 @@ class ContractController extends mfBaseController
|
||||
protected function saveProductchangeAction()
|
||||
{
|
||||
$r = $this->request;
|
||||
//var_dump($r->links);exit;
|
||||
|
||||
$id = $r->contract_id;
|
||||
if (!is_numeric($id) || !$id) {
|
||||
@@ -330,6 +310,22 @@ class ContractController extends mfBaseController
|
||||
$contract_data['product_external_id'] = $product->external_id;
|
||||
$contract_data['sla_id'] = $product->sla_id;
|
||||
|
||||
if($r->finish_date) {
|
||||
try {
|
||||
$finish_date = DateTime::createFromFormat("d.m.Y", $r->finish_date, new DateTimeZone("Europe/Vienna"));
|
||||
} catch (Exception $e) {
|
||||
$this->layout()->setFlash("Ungültiges Kündigungsdateum", "error");
|
||||
$this->redirect("Contract", "productchange", ["contract_id" => $id]);
|
||||
}
|
||||
|
||||
$finish_date->setTime(0,0,0);
|
||||
$contract_data["finish_date"] = $finish_date->getTimestamp();
|
||||
|
||||
$contract_cancel_date = clone($finish_date);
|
||||
$contract_cancel_date->modify("-1 day");
|
||||
$contract_cancel_date->setTime(23,59,59);
|
||||
}
|
||||
|
||||
$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);
|
||||
@@ -353,41 +349,124 @@ class ContractController extends mfBaseController
|
||||
$this->redirect("Contract", "productchange", ["contract_id" => $id]);
|
||||
}
|
||||
|
||||
if($contract_cancel_date) {
|
||||
$contract->cancel_date = $contract_cancel_date->getTimestamp();
|
||||
$contract->save();
|
||||
}
|
||||
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $new_contract->id,
|
||||
'type' => "created_from",
|
||||
'value' => "productchange",
|
||||
'text' => "Produkt-/Standortwechsel von Contract ID ".$contract->id
|
||||
]);
|
||||
$journal->save();
|
||||
|
||||
if (is_array($r->links) && count($r->links)) {
|
||||
foreach ($r->links as $link_id => $action) {
|
||||
foreach ($r->links as $link_id => $link_data) {
|
||||
$action = $link_data["action"];
|
||||
$cancel_date = false;
|
||||
if($link_data["cancel_date"]) {
|
||||
try {
|
||||
$cancel_date = DateTime::createFromFormat("d.m.Y", $link_data["cancel_date"], new DateTimeZone("Europe/Vienna"));
|
||||
} catch (Exception $e) {
|
||||
$this->layout()->setFlash("Ungültiges Kündigungsdateum", "error");
|
||||
$this->redirect("Contract", "productchange", ["contract_id" => $id]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$old_link = new ContractLink($link_id);
|
||||
if (!$old_link->id) continue;
|
||||
|
||||
// check if link contains this contract
|
||||
if ($old_link->contract_id == $contract->id) {
|
||||
$origin_id = $old_link->origin_contract_id;
|
||||
$link_contract_id = $old_link->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) {
|
||||
$origin_id = $old_link->contract_id;
|
||||
$link_contract_id = $old_link->origin_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($action != "cancel" && $old_link->type != "credit") {
|
||||
$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") {
|
||||
if($cancel_date) {
|
||||
// insert cancel_date in old contract
|
||||
$lc = new Contract($origin_id);
|
||||
$lc->cancel_date = $cancel_date->getTimestamp();
|
||||
$lc->save();
|
||||
} else {
|
||||
// leave cancellation for later (when finishing upgrade)
|
||||
$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";
|
||||
// XXX - if we have finish date then recreate credit contract right now
|
||||
if($contract_cancel_date) {
|
||||
$new_credit = ContractModel::createCreditForContract($new_contract);
|
||||
$new_credit->save();
|
||||
|
||||
// create journal for credit
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $new_credit->id,
|
||||
'type' => "created_from",
|
||||
'value' => "productchange",
|
||||
'text' => "Produkt-/Standortwechsel von Contract ID ".$new_link_origin_id
|
||||
]);
|
||||
$journal->save();
|
||||
|
||||
|
||||
$this->log->debug(print_r($new_credit, true));
|
||||
|
||||
// set cancel date for old credit
|
||||
$old_credit = new Contract($origin_id);
|
||||
$old_credit->cancel_date = $contract_cancel_date->getTimestamp();
|
||||
$old_credit->save();
|
||||
|
||||
// create link to new credit contract
|
||||
$link = ContractLinkModel::create([
|
||||
"contract_id" => $new_credit->id,
|
||||
"origin_contract_id" => $new_contract->id,
|
||||
"type" => "credit"
|
||||
]);
|
||||
$link->save();
|
||||
|
||||
// create upgrade link from old to new credit contract
|
||||
$link = ContractLinkModel::create([
|
||||
"contract_id" => $new_credit->id,
|
||||
"origin_contract_id" => $origin_id,
|
||||
"type" => "upgrade"
|
||||
]);
|
||||
$link->save();
|
||||
|
||||
} else {
|
||||
$old_link->change_action = "recreate";
|
||||
$old_link->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//var_dump($new_link);exit;
|
||||
@@ -439,82 +518,23 @@ class ContractController extends mfBaseController
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
$now = new DateTime("now");
|
||||
$now->setTime(0,0,0);
|
||||
|
||||
$contract->finish_date = $now->getTimestamp();
|
||||
$contract->finish_date_by = $this->me->id;
|
||||
try {
|
||||
$saved = $contract->save();
|
||||
} catch(Exception $e) {
|
||||
$saved = false;
|
||||
}
|
||||
|
||||
|
||||
$contract->finish_date = $now;
|
||||
$contract->finish_date_by = $this->me->id;
|
||||
if (!$contract->save()) {
|
||||
if(!$saved) {
|
||||
$this->layout()->setFlash("Contract konnte nicht gespeichert werden", "error");
|
||||
$this->redirect("Contract", "view", ['contract_id' => $id]);
|
||||
}
|
||||
|
||||
|
||||
// create Journal
|
||||
$journal = ContractjournalModel::create([
|
||||
'contract_id' => $contract->id,
|
||||
@@ -620,6 +640,10 @@ class ContractController extends mfBaseController
|
||||
$contract_data['billing_delay'] = (int)$r->billing_delay;
|
||||
$contract_data['note'] = $r->note;
|
||||
|
||||
if($r->termination_id) {
|
||||
$contract_data["termination_id"] = $r->termination_id;
|
||||
}
|
||||
|
||||
if($r->order_date) {
|
||||
$order_date = new DateTime("@" . $this->dateToTimestamp($r->order_date));
|
||||
$order_date->setTimezone(new DateTimeZone("Europe/Vienna"));
|
||||
|
||||
Reference in New Issue
Block a user