Files
thetool/application/Admin/functions/MassProductchange.php
2025-06-03 12:33:00 +02:00

264 lines
9.8 KiB
PHP

<?php
class Admin_MassProductchange {
private $request;
private $db;
private $log;
private $flash = [];
public function __construct($request = false) {
$this->request = $request;
$this->db = FronkDB::singleton();
$this->log = mfLoghandler::singleton();
}
public function runRequest() {
$action = $this->request->do;
if(!$action) {
return $this->indexAction();
} else {
$method = $action."Action";
if(method_exists($this, $method)) {
return $this->$method();
} else {
throw new Exception("Method ".htmlentities($method)." not found", "404");
}
}
}
public function indexAction() {
return [
"template" => "Admin/MassProductchange/Index",
"redirect" => "",
"templateVars" => []
];
}
public function createAction() {
$this->log->debug("Create action called");
$this->log->debug(print_r($this->request->get(), true));
$return = [
"template" => "Admin/MassProductchange/Index",
"redirect" => "",
"templateVars" => []
];
$r = $this->request;
$do_commit = $r->commit;
$linked_contracts_action = $r->linked_contracts_action;
$link_action_cancel_type = $r->link_action_cancel_type;
$exclude_termination_contracts = $r->exclude_termination_contracts;
$no_provision_credits = $r->no_provision_credits;
$price_type = $r->price_type;
$change_date = $r->change_date;
$old_product_ids = $r->old_product_ids;
$new_product_id = $r->new_product_id;
$return["templateVars"]["linked_contracts_action"] = $linked_contracts_action;
$return["templateVars"]["link_action_cancel_type"] = $link_action_cancel_type;
$return["templateVars"]["exclude_termination_contracts"] = $exclude_termination_contracts;
$return["templateVars"]["no_provision_credits"] = $no_provision_credits;
$return["templateVars"]["price_type"] = $price_type;
$return["templateVars"]["change_date"] = $change_date;
$return["templateVars"]["old_product_ids"] = $old_product_ids;
$return["templateVars"]["new_product_id"] = $new_product_id;
if(!$r->change_date) {
$return["error"] = "Fertigstellungsdatum ist erforderlich.";
return $return;
}
try {
$finish_date = DateTime::createFromFormat("d.m.Y", $change_date, new DateTimeZone("Europe/Vienna"));
$finish_date->setTime(0,0,0);
} catch (Exception $e) {
$return["error"] = "Ungültiges Fertigstellungsdatum";
return $return;
}
if(!is_array($old_product_ids) || !count($old_product_ids) || !$new_product_id) {
$return["error"] = "Beide Produkte werden benötigt.";
return $return;
}
$old_products = [];
foreach($old_product_ids as $product_id) {
$old_product = ProductModel::getOne($product_id);
if($old_product) $old_products[$old_product->id] = $old_product;
}
$new_product = ProductModel::getOne($new_product_id);
$contract_search = [
"product_id" => $old_product_ids,
"billing_period" => ">0",
"price>=" => "0",
"cancel_date" => null,
];
if($exclude_termination_contracts) {
$contract_search["termination_id"] = null;
}
$contracts = ContractModel::searchActive($contract_search, [
"order_by" => "product_id, matchcode",
"limit" => 1000,
]);
if(!$contracts) {
$return["error"] = "Keine passenden Verträge gefunden.";
return $return;
}
$commit_setup_price = false;
if($r->commit_setup_price) {
$commit_setup_price = true;
}
$results = [];
foreach($contracts as $contract) {
$new_price = $new_price_setup = $new_price_nne = $new_price_nbe = null;
if($price_type == "keep") {
// Preise von bestehendem Contract übernehmen.
// Setup Preis wird nicht nochmal verrechnet.
$new_price = $contract->price;
$new_price_setup = 0;
$new_price_nne = $contract->price_nne;
$new_price_nbe = $contract->price_nbe;
} elseif($price_type == "from_new_product") {
// Preise vom neuen Produkt übernehmen.
// Setup Preis kann verrechnet werden, wenn angegeben.
$new_price = $new_product->price;
$new_price_setup = 0;
$new_price_nne = $new_product->price_nne;
$new_price_nbe = $new_product->price_nbe;
if($commit_setup_price) {
$new_price_setup = $new_product->price_setup;
}
} elseif($price_type == "fixed") {
$new_price = Layout::commaToDot(trim($r->fix_price));
$new_price_setup = Layout::commaToDot(trim($r->fix_price_setup));
}
if($old_products[$contract->product_id]->price > 0.0000 && $contract->price == 0.0000) {
// if old contract has a discount price of zero, keep it that way
$new_price = 0;
$new_price_setup = 0;
$new_price_nne = 0;
}
$contract_data = [];
$contract_data['product_id'] = $new_product->id;
$contract_data['product_name'] = trim($new_product->name);
$contract_data['product_info'] = trim($contract->product_info);
$contract_data['matchcode'] = trim($contract->matchcode);
$contract_data['termination_id'] = $contract->termination_id;
$contract_data['amount'] = $contract->amount;
$contract_data['price'] = $new_price;
$contract_data['price_setup'] = $new_price_setup;
$contract_data['price_nne'] = $new_price_nne;
$contract_data['price_nbe'] = $new_price_nbe;
$contract_data["finish_date"] = $finish_date->format("d.m.Y");
$contract_data['note'] = trim($r->note);
// get linked contracts
$link_data = [];
foreach(ContractLinkModel::includesContractId($contract->id, ["type" => ["link", "credit"]]) as $link) {
if($link->type == "credit") {
$link_data[$link->id] = [
"action" => $linked_contracts_action,
"cancel_date" => null,
];
continue;
}
$link_cancel_date = null;
if($linked_contracts_action == "cancel") {
// determine requested cancel date of linked contract
if($link_action_cancel_type == "finish_date") {
$lcd = clone $finish_date;
$lcd->modify("-1 day");
$link_cancel_date = $lcd->format("d.m.Y");
}
if($link_action_cancel_type == "link_contract_term") {
// Ende der Vertragslaufzeit
$link_cancel_date = $link->contract->getRegularCanceldate("d.m.Y");
}
if($link_action_cancel_type == "link_billing_period") {
// Ende der aktuellen Verrechnungsperiode
$link_cancel_date = $link->contract->getNextBillingPeriodEnd("d.m.Y");
}
}
$link_data[$link->id] = [
"action" => $linked_contracts_action,
"cancel_date" => $link_cancel_date,
];
}
//if(!count($link_data)) continue;
//var_dump($link_data);exit;
try {
$productchange_result = $contract->createProductchangeContract($contract_data, $link_data, [
"link_action" => $linked_contracts_action,
"link_action_cancel_type" => $link_action_cancel_type,
"no_provision_credits" => $no_provision_credits,
], [
"ignore_missing_term" => true,
]);
} catch (\Exception $e) {
$return["error"] = "Fehler beim Erstellen des Produktwechsels für Vertrag {$contract->id}: " . $e->getMessage();
return $return;
}
if(false && count($results) > 0) {
var_dump($productchange_result);
foreach($productchange_result["linked_contracts"] as $l) {
echo "Link FROM:";
var_dump($l["link_from"]);
echo "Link TO:";
var_dump($l["link_to"]);
}
exit;
}
$results[] = $productchange_result;
}
//var_dump($results);exit;
if($do_commit) {
// commit all productchanges
foreach($results as $result) {
$old_contract = $result["old_contract"];
$new_contract = $result["new_contract"];
$links = $result["linked_contracts"];
if(!$old_contract->commitProductchangeContract($new_contract, $links)) {
$return["error"] = "Fehler beim Commit des Produktwechsels für Vertrag {$old_contract->id}";
return $return;
}
}
$return["success"] = count($results)." Produktwechsel erfolgreich durchgeführt.";
return $return;
}
$return["templateVars"]["contracts"] = $contracts;
$return["templateVars"]["change_contracts"] = $results;
return $return;
}
}