171 lines
5.3 KiB
PHP
171 lines
5.3 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() {
|
|
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", $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, $pagination) 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 importFinishedOrdersAction() {
|
|
$o = 0;
|
|
$max_orders = 1500;
|
|
foreach(OrderModel::search(["finish_date<" => date("U")]) 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;
|
|
}
|
|
|
|
$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 && $contract->matchcode) {
|
|
$primary_matchcode = $contract->matchcode;
|
|
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;
|
|
}
|
|
$contracts[] = $contract;
|
|
}
|
|
|
|
|
|
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++;
|
|
|
|
if($o >= $max_orders) {
|
|
$this->layout()->setFlash("$o Bestellungen importiert, es gibt noch mehr Bestellungen zum importieren.", "info");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
}
|
|
$this->layout()->setFlash("Alle Bestellungen importiert", "success");
|
|
$this->redirect("Contractqueue");
|
|
}
|
|
} |