646 lines
22 KiB
PHP
646 lines
22 KiB
PHP
<?php
|
|
|
|
class ContractqueueController 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");
|
|
}
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$this->importOrders();
|
|
|
|
if($this->request->resetFilter) {
|
|
unset($_SESSION[MFAPPNAME.'-Contractqueue-filter']);
|
|
}
|
|
|
|
$filter = [];
|
|
if(is_array($this->request->filter)) {
|
|
$filter = $this->request->filter;
|
|
$_SESSION[MFAPPNAME.'-Contractqueue-filter'] = $filter;
|
|
} else {
|
|
if(array_key_exists(MFAPPNAME.'-Contractqueue-filter', $_SESSION) && count($_SESSION[MFAPPNAME.'-Contractqueue-filter'])) {
|
|
$filter = $_SESSION[MFAPPNAME.'-Contractqueue-filter'];
|
|
}
|
|
}
|
|
|
|
$this->layout->set("filter", array_map(fn($a) => htmlentities($a), $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);
|
|
}
|
|
|
|
$filter["contract_id"] = null;
|
|
$pagination['maxItems'] = ContractqueueModel::count($filter);
|
|
|
|
$last_order_id = false;
|
|
$orders = [];
|
|
foreach(ContractqueueModel::search($filter, $pagination) as $contractq) {
|
|
$last_order_id = $contractq->order_id;
|
|
$orders[$contractq->order_id][] = $contractq;
|
|
}
|
|
|
|
if($last_order_id) {
|
|
$filter["order_id"] = $last_order_id;
|
|
$orders[$contractq->order_id] = [];
|
|
foreach(ContractqueueModel::search($filter) as $contractq) {
|
|
$orders[$contractq->order_id][] = $contractq;
|
|
}
|
|
}
|
|
|
|
$this->layout()->set("orders", $orders);
|
|
$this->layout()->set("pagination", $pagination);
|
|
|
|
|
|
}
|
|
|
|
protected function getPreparedFilter($filter) {
|
|
return $filter;
|
|
}
|
|
|
|
protected function deletePosition() {
|
|
$op_id = $this->request->op_id;
|
|
|
|
$cq = ContractqueueModel::getFirst(["orderproduct_id" => $op_id]);
|
|
if(!$cq) {
|
|
$this->layout()->setFlash("Bestellung in Contractqueue nicht gefunden", "error");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
|
|
$op = new OrderProduct($op_id);
|
|
if(!$op->id) {
|
|
$this->layout()->setFlash("Bestellung nicht gefunden", "error");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
|
|
$order = $op->order;
|
|
if(!$order) {
|
|
$this->layout()->setFlash("Bestellung nicht gefunden", "error");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
|
|
if(!is_array($order->products) || $order->products < 2 ) {
|
|
$this->layout()->setFlash("Nur ein Produkt in Bestellung gefunden. Kann nicht gelöscht werden", "warning");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
|
|
$this->log->debug(__METHOD__.": Deleting OrderProduct $op_id from Order ".$order->id." (product_id: ".$op->product_id." product_name: ".$op->product->name." price: ".$op->price." price_setup: ".$op->price_setup." termination_id: ".$op->termination_id." voicenumber: ".$op->voicenumber.")");
|
|
|
|
if(!$op->delete()) {
|
|
$this->layout()->setFlash("Fehler beim Löschen der Bestellposition", "error");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
|
|
if(!$cq->delete()) {
|
|
$this->layout()->setFlash("Fehler beim Löschen der Contractqueue Position", "error");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
|
|
$this->layout()->setFlash("Position erfolgreich gelöscht", "success");
|
|
$this->redirect("Contractqueue");
|
|
|
|
|
|
}
|
|
|
|
private function importOrders() {
|
|
$o = 0;
|
|
//$max_orders = 1500;
|
|
|
|
$not_before = new DateTime("2024-07-01");
|
|
$not_before->setTimezone(new DateTimeZone("Europe/Vienna"));
|
|
|
|
foreach(OrderModel::search(["finish_date" => true, "finish_date<=" => date("U"), "finish_date>=" => $not_before->getTimestamp()]) as $order) {
|
|
if(!is_array($order->products) || !count($order->products)) {
|
|
//echo "keine Produkte in Order ".$order->id."\n";
|
|
continue;
|
|
}
|
|
|
|
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) {
|
|
if($contract->matchcode) {
|
|
$primary_matchcode = $contract->matchcode;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if($op->product->external) {
|
|
$skip_order = true;
|
|
continue;
|
|
}
|
|
|
|
// if contract does not exist yet, create Contractqueue
|
|
$contract = ContractqueueModel::createFromOrderproduct($op);
|
|
|
|
// find reseller / receiver of credit
|
|
// from product
|
|
$product_attribs = $contract->product->attributes;
|
|
/*if($op->id == "8666") {
|
|
var_dump($product_attribs);exit;
|
|
}*/
|
|
if(is_array($product_attribs) && array_key_exists("crediting_partner", $product_attribs) && $product_attribs["crediting_partner"] && is_object($product_attribs["crediting_partner"])) {
|
|
if($product_attribs["crediting_partner"]->value) {
|
|
$contract->crediting_partner_id = $product_attribs["crediting_partner"]->value;
|
|
}
|
|
if($product_attribs["crediting_rate"]->value) {
|
|
$contract->crediting_partner_rate = str_replace(",", ".",$product_attribs["crediting_rate"]->value);
|
|
}
|
|
}
|
|
// or from netowner if anschluss product
|
|
if(!$contract->crediting_partner_id && $contract->termination_id) {
|
|
$contract->crediting_partner_id = $contract->termination->building->network->owner_id;
|
|
}
|
|
|
|
if($contract->matchcode) {
|
|
$primary_matchcode = $contract->matchcode;
|
|
}
|
|
$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;
|
|
}
|
|
|
|
$links = [];
|
|
// set matchcode and save
|
|
foreach($contracts as $contract) {
|
|
if(!$contract->matchcode) {
|
|
$contract->matchcode = $primary_matchcode;
|
|
|
|
}
|
|
//var_dump($contract);exit;
|
|
$contract->save();
|
|
|
|
/*if(!count($links)) {
|
|
$links[$contract->id] = $contract;
|
|
} elseif(!array_key_exists($contract->id, $links)) {
|
|
$first_link = reset($links);
|
|
|
|
$new_link = ContractLinkModel::create([
|
|
"contract_id" => $first_link->id,
|
|
"origin_contract_id" => $contract->id,
|
|
"type" => "link"
|
|
]);
|
|
$new_link->save();
|
|
$links[$contract->id] = $contract;
|
|
}*/
|
|
|
|
}
|
|
|
|
$o++;
|
|
}
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
protected function commitOrderAction() {
|
|
$r = $this->request;
|
|
//var_dump($r->get());exit;
|
|
|
|
$c = 0;
|
|
$new_contracts = [];
|
|
$order_id = $r->order_id;
|
|
|
|
foreach(ContractqueueModel::search(["order_id" => $order_id, "contract_id" => null]) as $cq) {
|
|
//var_dump($cq);exit;
|
|
$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
|
|
|
|
/*
|
|
* Create Contract
|
|
*/
|
|
$contract = ContractModel::createFromContractqueue($cq);
|
|
if(!$contract->save()) {
|
|
$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();
|
|
|
|
// create Contractjournal
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $contract->id,
|
|
'type' => "created_from",
|
|
'value' => "order"
|
|
]);
|
|
$journal->save();
|
|
|
|
$contract->addFilesFromOrder();
|
|
$contract->addJournalFromOrder();
|
|
|
|
/*
|
|
* Create Crediting Contract
|
|
* if required
|
|
*/
|
|
|
|
if($cq->approved_credit) {
|
|
$credit = ContractModel::createFromContractQueue($cq, "credit");
|
|
if(!$credit->save()) {
|
|
$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([
|
|
'contract_id' => $credit->id,
|
|
'type' => "created_from",
|
|
'value' => "order"
|
|
]);
|
|
$journal->save();
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $contract->id,
|
|
'type' => "credit_created",
|
|
'value' => $credit->id
|
|
]);
|
|
$journal->save();
|
|
$link = ContractLinkModel::create([
|
|
'contract_id' => $credit->id,
|
|
'origin_contract_id' => $contract->id,
|
|
'type' => 'credit'
|
|
]);
|
|
$link->save();
|
|
// XXX: retour link erstellen?
|
|
}
|
|
|
|
/*
|
|
* Create ContractLinks
|
|
*/
|
|
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,
|
|
'origin_contract_id' => $origin->id,
|
|
'type' => 'link'
|
|
]);
|
|
$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();
|
|
}
|
|
}
|
|
} else {
|
|
$new_contracts[$order_id] = [];
|
|
}
|
|
|
|
$new_contracts[$order_id][] = $contract;
|
|
|
|
}
|
|
|
|
$this->layout()->setFlash("$c Contracts erstellt", "success");
|
|
$this->redirect("Contractqueue");
|
|
|
|
|
|
}
|
|
|
|
/*protected function commitAction() {
|
|
$r = $this->request;
|
|
//var_dump($r->get());exit;
|
|
|
|
$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 = 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->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();
|
|
|
|
// create Contractjournal
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $contract->id,
|
|
'type' => "created_from",
|
|
'value' => "order"
|
|
]);
|
|
$journal->save();
|
|
|
|
$contract->addFilesFromOrder();
|
|
$contract->addJournalFromOrder();
|
|
|
|
/*
|
|
* Create Crediting Contract
|
|
* if required
|
|
*//*
|
|
|
|
if($cq->approved_credit) {
|
|
$credit = ContractModel::createFromContractQueue($cq, "credit");
|
|
if(!$credit->save()) {
|
|
$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([
|
|
'contract_id' => $credit->id,
|
|
'type' => "created_from",
|
|
'value' => "order"
|
|
]);
|
|
$journal->save();
|
|
$journal = ContractjournalModel::create([
|
|
'contract_id' => $contract->id,
|
|
'type' => "credit_created",
|
|
'value' => $credit->id
|
|
]);
|
|
$journal->save();
|
|
$link = ContractLinkModel::create([
|
|
'contract_id' => $credit->id,
|
|
'origin_contract_id' => $contract->id,
|
|
'type' => 'credit'
|
|
]);
|
|
$link->save();
|
|
// XXX: retour link erstellen?
|
|
}
|
|
|
|
/*
|
|
* Create ContractLinks
|
|
*//*
|
|
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,
|
|
'origin_contract_id' => $origin->id,
|
|
'type' => 'link'
|
|
]);
|
|
$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();
|
|
}
|
|
}
|
|
} else {
|
|
$new_contracts[$order_id] = [];
|
|
}
|
|
|
|
$new_contracts[$order_id][] = $contract;
|
|
|
|
}
|
|
|
|
$this->layout()->setFlash("$c Contracts erstellt", "success");
|
|
$this->redirect("Contractqueue");
|
|
|
|
|
|
}*/
|
|
|
|
protected function apiAction() {
|
|
if(!$this->me->is(["Admin"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
$do = $this->request->do;
|
|
$data = [];
|
|
|
|
switch($do) {
|
|
case "saveContract":
|
|
$return = $this->saveContractApi();
|
|
break;
|
|
case "setApproval":
|
|
$return = $this->setApprovalApi();
|
|
break;
|
|
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 saveContractApi() {
|
|
$r = $this->request;
|
|
|
|
$valid_fields = [
|
|
"product" => "product_id",
|
|
"matchcode" => "matchcode",
|
|
"price" => "price",
|
|
"price_setup" => "price_setup",
|
|
"billing_period" => "billing_period",
|
|
"billing_delay" => "billing_delay"
|
|
];
|
|
|
|
$data = [];
|
|
foreach($r->get() as $f => $v) {
|
|
if(array_key_exists($f, $valid_fields)) {
|
|
$data[$valid_fields[$f]] = $v;
|
|
}
|
|
}
|
|
|
|
if(!count($data)) {
|
|
return false;
|
|
}
|
|
|
|
$cq = new Contractqueue($r->id);
|
|
if(!$cq->id) {
|
|
return false;
|
|
}
|
|
|
|
if(array_key_exists("product_id", $data)) {
|
|
$product = new Product($data["product_id"]);
|
|
if(!$product->id) {
|
|
$this->log->error(__METHOD__.": neue product_id nicht gefunden");
|
|
return false;
|
|
}
|
|
$data["product_name"] = $product->name;
|
|
}
|
|
|
|
$cq->update($data);
|
|
if(!$cq->save()) {
|
|
return false;
|
|
}
|
|
//var_dump($cq->product, $cq->product->attributes);exit;
|
|
// find reseller / receiver of credit
|
|
// from product
|
|
$product_attribs = $cq->product->attributes;
|
|
/*if($op->id == "8666") {
|
|
var_dump($product_attribs);exit;
|
|
}*/
|
|
|
|
$cq->crediting_partner_id = null;
|
|
$cq->crediting_partner_rate = null;
|
|
|
|
if(is_array($product_attribs) && array_key_exists("crediting_partner", $product_attribs) && $product_attribs["crediting_partner"] && is_object($product_attribs["crediting_partner"])) {
|
|
if($product_attribs["crediting_partner"]->value && $product_attribs["crediting_partner"]->value != $cq->crediting_partner_id) {
|
|
$cq->crediting_partner_id = $product_attribs["crediting_partner"]->value;
|
|
}
|
|
if($product_attribs["crediting_rate"]->value) {
|
|
$crediting_rate_price = round($cq->price / 100 * $product_attribs["crediting_rate"]->value, 4);
|
|
if($crediting_rate_price != $cq->crediting_partner_rate) {
|
|
$cq->crediting_partner_rate = $crediting_rate_price;
|
|
}
|
|
|
|
}
|
|
}
|
|
// or from netowner if anschluss product
|
|
if(!$cq->crediting_partner_id && $cq->termination_id && $cq->product->price_nne > 0) {
|
|
$cq->crediting_partner_id = $cq->termination->building->network->owner_id;
|
|
}
|
|
|
|
if(!$cq->save()) {
|
|
return false;
|
|
}
|
|
|
|
$return = [];
|
|
$return["product"] = $cq->product_id;
|
|
$return["product_name"] = $cq->product->name;
|
|
$return["termination"] = ($cq->termination_id) ? $cq->termination->code." ".str_replace("\n", " - ", $cq->termination->getAddress()) : null;
|
|
$return["matchcode"] = $cq->matchcode;
|
|
$return["price"] = $cq->price;
|
|
$return["price_setup"] = $cq->price_setup;
|
|
$return["billing_period"] = $cq->billing_period;
|
|
$return["billing_delay"] = $cq->billing_delay;
|
|
|
|
$credit = [];
|
|
$credit["crediting_rate"] = null;
|
|
$credit["crediting_partner_id"] = $cq->crediting_partner_id;
|
|
$credit["crediting_partner_name"] = "";
|
|
$credit["crediting_rate"] = 0;
|
|
$credit["crediting_matchcode"] = "";
|
|
|
|
|
|
if($cq->crediting_partner_id) {
|
|
$credit["crediting_partner_name"] = $cq->crediting_partner->getCompanyOrName();
|
|
|
|
if($cq->crediting_partner_rate) {
|
|
$credit["crediting_rate"] = $cq->crediting_partner_rate;
|
|
$credit["crediting_partner_name"] = "€ ".number_format($cq->crediting_partner_rate, 4, ",",".")." (Prozentrate)";
|
|
} else {
|
|
$credit["crediting_rate"] = $cq->price_nne;
|
|
$credit["crediting_partner_name"] = "€ ".number_format($cq->crediting_partner_rate, 4, ",",".")." (NNE)";
|
|
}
|
|
|
|
$credit["crediting_matchcode"] = $cq->crediting_matchcode;
|
|
}
|
|
|
|
|
|
|
|
|
|
return ["contract" => $return, "credit" => $credit];
|
|
|
|
}
|
|
|
|
private function setApprovalApi() {
|
|
$r = $this->request;
|
|
|
|
//var_dump($r->get());exit;
|
|
|
|
$id = $r->cq_id;
|
|
if(!is_numeric($id) || $id < 1) {
|
|
return false;
|
|
}
|
|
|
|
$cq = new Contractqueue($id);
|
|
if(!$cq->id) {
|
|
return false;
|
|
}
|
|
|
|
if($r->type == "order") {
|
|
$cq->approved = $r->value;
|
|
}
|
|
if($r->type == "credit") {
|
|
$cq->approved_credit = $r->value;
|
|
}
|
|
|
|
$cq->save();
|
|
|
|
return ["message" => "contract saved"];
|
|
}
|
|
} |