187 lines
7.2 KiB
PHP
187 lines
7.2 KiB
PHP
<?php
|
|
|
|
class ContractTrigger_Finished {
|
|
private $log;
|
|
private $db;
|
|
private $me;
|
|
private $contract;
|
|
public $errors;
|
|
|
|
public function __construct($injection = []) {
|
|
foreach($injection as $name => $value) {
|
|
if(in_array($name, ["log", "db", "contract", "me"])) {
|
|
$this->$name = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks Contract if can/must run
|
|
* return true if it wants to run
|
|
* @return bool
|
|
*/
|
|
public function precheck() {
|
|
$contract = $this->contract;
|
|
if($contract->finish_date && !$contract->_old_data->finish_date) {
|
|
// contract was just finished, so we need to run
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public function run() {
|
|
$contract = $this->contract;
|
|
if($contract->_old_data->finish_date) {
|
|
var_dump($contract->finish_date, $contract->_old_data->finish_date);
|
|
return true;
|
|
}
|
|
// contract was just finished, so we need to run
|
|
|
|
/*
|
|
* Vorgänger Contracts kündigen
|
|
*/
|
|
foreach (ContractLinkModel::search(['contract_id' => $contract->id]) as $link) {
|
|
if (!in_array($link->type, ["upgrade", "downgrade", "relocation", "productchange", "ownerchange"])) {
|
|
continue;
|
|
}
|
|
|
|
$now = new DateTime("now");
|
|
$now->setTime(2,0,0);
|
|
|
|
$cancel_date = clone($now);
|
|
$cancel_date->modify("-1 day");
|
|
$cancel_date->setTime(23,59,59);
|
|
|
|
$origin = $link->origin;
|
|
$origin->cancel_date = $cancel_date->getTimestamp();
|
|
$origin->cancel_date_by = $this->me->id;
|
|
$origin->edit_by = $this->me->id;
|
|
if (!$origin->save()) {
|
|
throw new Exception("Fehler beim Speichern der Kündigung des Vorgängercontracts!");
|
|
}
|
|
// cancel journal
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $origin->id,
|
|
'type' => "canceled",
|
|
'value' => "",
|
|
'text' => ""
|
|
]);
|
|
$journal->save();
|
|
|
|
|
|
/*
|
|
* 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->origin->cancel_date) {
|
|
$old_link->origin->update([
|
|
'cancel_date' => $cancel_date->getTimestamp(),
|
|
'cancel_date_by' => $this->me->id,
|
|
'edit_by' => $this->me->id
|
|
]);
|
|
if(!$old_link->origin->save()) {
|
|
throw new Exception("Fehler beim Speichern der Kündigung des Vorgängercontracts!");
|
|
}
|
|
|
|
$old_link->change_action = null;
|
|
$old_link->save();
|
|
|
|
// cancel journal
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $old_link->origin->id,
|
|
'type' => "canceled",
|
|
'value' => "",
|
|
'text' => ""
|
|
]);
|
|
$journal->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' => $cancel_date->getTimestamp(),
|
|
'cancel_date_by' => $this->me->id,
|
|
'edit_by' => $this->me->id
|
|
]);
|
|
//$this->log->debug(print_r($old_link->contract, true));
|
|
$old_link->contract->save();
|
|
|
|
$old_link->change_action = null;
|
|
$old_link->save();
|
|
|
|
// cancel journal
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $old_link->contract->id,
|
|
'type' => "canceled",
|
|
'value' => "",
|
|
'text' => ""
|
|
]);
|
|
$journal->save();
|
|
}
|
|
}
|
|
|
|
foreach (ContractLinkModel::search(['type' => "credit", 'origin_contract_id' => $origin->id]) as $old_credit) {
|
|
if ($old_credit->change_action == "recreate" && !$old_credit->contract->cancel_date) {
|
|
// Alte Gutschrift kündigen und neue anlegen
|
|
//var_dump($old_credit->contract);
|
|
$old_credit->contract->update([
|
|
'cancel_date' => $cancel_date->getTimestamp(),
|
|
'cancel_date_by' => $this->me->id,
|
|
'edit_by' => $this->me->id
|
|
]);
|
|
$old_credit->contract->save();
|
|
//var_dump($old_credit->contract);
|
|
//exit;
|
|
|
|
$old_credit->change_action = null;
|
|
$old_credit->save();
|
|
|
|
$new_credit = ContractModel::createCreditForContract($contract);
|
|
if(!$new_credit->save()) {
|
|
$this->log->debug(print_r($new_credit, true));
|
|
throw new Exception("Fehler beim Speichern des neuen Gutschrift Contracts");
|
|
}
|
|
|
|
// create journal for credit
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $new_credit->id,
|
|
'type' => "created_from",
|
|
'value' => "productchange",
|
|
'text' => "Produkt-/Standortwechsel von Contract ID ".$old_credit->contract_id
|
|
]);
|
|
$journal->save();
|
|
|
|
|
|
$this->log->debug(print_r($new_credit, true));
|
|
|
|
// create link to new credit contract
|
|
$link = ContractLinkModel::create([
|
|
"contract_id" => $new_credit->id,
|
|
"origin_contract_id" => $contract->id,
|
|
"type" => "credit"
|
|
]);
|
|
$link->save();
|
|
|
|
// create link from old to new credit contract
|
|
$link = ContractLinkModel::create([
|
|
"contract_id" => $new_credit->id,
|
|
"origin_contract_id" => $old_credit->contract_id,
|
|
"type" => "upgrade"
|
|
]);
|
|
$link->save();
|
|
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $old_credit->contract_id,
|
|
'type' => "canceled",
|
|
'value' => "",
|
|
'text' => ""
|
|
]);
|
|
$journal->save();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
} |