330 lines
9.3 KiB
PHP
330 lines
9.3 KiB
PHP
<?php
|
|
|
|
class ContractController 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->layout()->setTemplate("Contract/Index");
|
|
|
|
$rfilter = $this->request->filter;
|
|
iF(!is_array($rfilter)) {
|
|
$rfilter = [];
|
|
}
|
|
|
|
$this->layout->set("filter", $rfilter);
|
|
|
|
$filter = $this->getPreparedFilter($rfilter);
|
|
|
|
// pagination defaults
|
|
$pagination = [];
|
|
$pagination['start'] = 0;
|
|
$pagination['count'] = 50;
|
|
$pagination['maxItems'] = 0;
|
|
|
|
if(is_numeric($this->request->s)) {
|
|
$pagination['start'] = intval($this->request->s);
|
|
}
|
|
//var_dump($filter);exit;
|
|
$pagination['maxItems'] = ContractModel::count($filter);
|
|
$contracts = ContractModel::search($filter, $pagination);
|
|
|
|
$this->layout()->set("contracts", $contracts);
|
|
$this->layout()->set("pagination", $pagination);
|
|
}
|
|
|
|
private function getPreparedFilter($filter) {
|
|
$new_filter = [];
|
|
|
|
if(array_key_exists("show_canceled", $filter)) {
|
|
if($filter['show_canceled'] == 0) {
|
|
$new_filter['add-where'] = " AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())";
|
|
}
|
|
} else {
|
|
$new_filter['add-where'] = " AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())";
|
|
}
|
|
|
|
if(is_array($filter) && count($filter)) {
|
|
foreach($filter as $name => $value) {
|
|
$new_filter[$name] = $value;
|
|
}
|
|
}
|
|
|
|
return $new_filter;
|
|
}
|
|
|
|
protected function viewAction() {
|
|
$this->layout()->setTemplate("Contract/View");
|
|
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vertrag nicht gefunden", "error");
|
|
$this->redirect("Contract");
|
|
}
|
|
|
|
$contract = new Contract($id);
|
|
if(!$contract->id) {
|
|
$this->layout()->setFlash("Vertrag nicht gefunden", "error");
|
|
$this->redirect("Contract");
|
|
}
|
|
|
|
$this->layout()->set("contract", $contract);
|
|
|
|
if($this->request->filter) {
|
|
$this->layout()->set("filter", $this->request->filter);
|
|
}
|
|
if($this->request->s) {
|
|
$this->layout()->set("filter", $this->request->s);
|
|
}
|
|
|
|
}
|
|
|
|
protected function addAction() {
|
|
$this->layout()->setTemplate("Contract/Form");
|
|
}
|
|
|
|
protected function editAction() {
|
|
$id = $this->request->id;
|
|
if(!is_numeric($id) || !$id) {
|
|
$this->layout()->setFlash("Vertrag nicht gefunden", "error");
|
|
$this->redirect("Contract");
|
|
}
|
|
|
|
$contract = new Contract($id);
|
|
if(!$contract->id) {
|
|
$this->layout()->setFlash("Vertrag nicht gefunden", "error");
|
|
$this->redirect("Contract");
|
|
}
|
|
|
|
$this->layout()->set("contract", $contract);
|
|
|
|
if($this->request->f == "view") $this->layout()->set("f", "view");
|
|
if($this->request->f != "view") $this->layout()->set("f", "index");
|
|
|
|
if($this->request->filter) {
|
|
$this->layout()->set("filter", $this->request->filter);
|
|
}
|
|
if($this->request->s) {
|
|
$this->layout()->set("filter", $this->request->s);
|
|
}
|
|
|
|
return $this->addAction();
|
|
}
|
|
|
|
protected function saveAction() {
|
|
$r = $this->request;
|
|
//var_dump($r);
|
|
|
|
/*
|
|
* add or edit
|
|
*/
|
|
$id = $r->id;
|
|
if(is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$contract = new Contract($id);
|
|
if(!$contract->id) {
|
|
$this->layout()->setFlash("Vertrag nicht gefunden", "error");
|
|
$this->redirect("Contract");
|
|
}
|
|
} else {
|
|
$id = false;
|
|
$mode = "add";
|
|
}
|
|
|
|
//var_dump($r->get());exit;
|
|
$contract_data = [];
|
|
$contract_data["owner_id"] = (int)$r->owner_id;
|
|
$contract_data["billingaddress_id"] = ($r->billingaddress_id) ? (int)$r->billingaddress_id : null;
|
|
$contract_data["product_id"] = (int)$r->product_id;
|
|
$contract_data["matchcode"] = $r->matchcode;
|
|
$contract_data["product_name"] = $r->product_name;
|
|
$contract_data["product_info"] = $r->product_info;
|
|
$contract_data['amount'] = ($r->amount) ? (float)$r->amount : 1;
|
|
$contract_data['price'] = (float)$r->price;
|
|
$contract_data['price_setup'] = (float)$r->price_setup;
|
|
$contract_data['price_nne'] = (float)$r->price_nne;
|
|
$contract_data['price_nbe'] = (float)$r->price_nbe;
|
|
$contract_data['billing_period'] = (int)$r->billing_period;
|
|
$contract_data['billing_delay'] = (int)$r->billing_delay;
|
|
$contract_data['finish_date'] = ($r->finish_date) ? $this->dateToTimestamp($r->finish_date) : null;
|
|
$contract_data['cancel_date'] = ($r->cancel_date) ? $this->dateToTimestamp($r->cancel_date) : null;
|
|
$contract_data['note'] = $r->note;
|
|
|
|
|
|
//var_dump($contract_data);exit;
|
|
|
|
if($mode == "add") {
|
|
$contract = ContractModel::create($contract_data);
|
|
} else {
|
|
$contract->update($contract_data);
|
|
}
|
|
|
|
$this->layout()->set("contract", $contract);
|
|
|
|
if(!$contract_data["owner_id"]) {
|
|
$this->layout()->setFlash("Bitte Vertragsinhaber auswählen.", "error");
|
|
return $this->addAction();
|
|
}
|
|
if(!$contract_data["product_id"]) {
|
|
$this->layout()->setFlash("Bitte Produkt auswählen.", "error");
|
|
return $this->addAction();
|
|
}
|
|
if(!$contract_data['billing_period']) {
|
|
$this->layout()->setFlash("Bitte Rechnungsperiode auswählen.", "error");
|
|
return $this->addAction();
|
|
}
|
|
|
|
if(!$contract->product_name) {
|
|
$product = new Product($contract_data["product_id"]);
|
|
if(!$product->id) {
|
|
$this->layout()->setFlash("Ungültiges Produkt.", "error");
|
|
return $this->addAction();
|
|
}
|
|
$contract->product_name = $product->name;
|
|
}
|
|
|
|
//var_dump($contract);exit;
|
|
|
|
$contract_id = $contract->save();
|
|
if(!$contract_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern.", "error");
|
|
$this->layout()->set("contract", $contract);
|
|
return $this->addAction();
|
|
}
|
|
|
|
$this->layout()->setFlash("Vertrag erfolgreich gespeichert.", "success");
|
|
|
|
|
|
/* ContractLinks */
|
|
|
|
|
|
$query = [];
|
|
if($r->s) {
|
|
$query['s'] = $r->s;
|
|
}
|
|
if($r->filter) {
|
|
$query["filter"] = $r->filter;
|
|
}
|
|
if($r->return != "index") {
|
|
$query['id'] = $contract_id;
|
|
}
|
|
|
|
$qs = http_build_query($query);
|
|
|
|
if($mode == "add" || $r->f == "view") {
|
|
$this->redirect("Contract", "view", $qs, "contract=$contract_id");
|
|
} else {
|
|
$this->redirect("Contract", "Index", $qs);
|
|
}
|
|
|
|
}
|
|
|
|
protected function apiAction() {
|
|
if(!$this->me->is(["Admin"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
$do = $this->request->do;
|
|
$data = [];
|
|
|
|
switch($do) {
|
|
case "getContract":
|
|
$return = $this->getContractApi();
|
|
break;
|
|
case "findContract":
|
|
$return = $this->findContractApi();
|
|
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 getContractApi() {
|
|
$contract_id = $this->request->contract_id;
|
|
if(!is_numeric($contract_id) || $contract_id < 1) {
|
|
return false;
|
|
}
|
|
|
|
$form_id = false;
|
|
if($this->request->form_id) {
|
|
$form_id = $this->request->form_id;
|
|
}
|
|
|
|
$contract = new Contract($contract_id);
|
|
if(!$contract->id) {
|
|
return false;
|
|
}
|
|
|
|
$data = $contract->toArray();
|
|
|
|
return ["contract" => $data, "form_id" => $form_id];
|
|
}
|
|
|
|
private function findContractApi() {
|
|
$search = trim($this->request->q);
|
|
$autocomplete = $this->request->autocomplete;
|
|
|
|
$contracts = [];
|
|
|
|
if(is_numeric($search)) {
|
|
$c = new Contract($search);
|
|
if($c->id) {
|
|
if(!array_key_exists($c->id, $contracts)) {
|
|
$contracts[$c->id] = $c;
|
|
}
|
|
}
|
|
|
|
foreach(["id", "owner_id", "product_id"] as $search_key) {
|
|
foreach(ContractModel::search([$search_key => $search]) as $c) {
|
|
if(!array_key_exists($c->id, $contracts)) {
|
|
$contracts[$c->id] = $c;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach(["product_name", "matchcode", "owner"] as $search_key) {
|
|
foreach(ContractModel::search([$search_key => $search]) as $c) {
|
|
if(!array_key_exists($c->id, $contracts)) {
|
|
$contracts[$c->id] = $c;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!is_array($contracts) && !count($contracts)) {
|
|
return false;
|
|
}
|
|
|
|
$results = [];
|
|
|
|
// return bootstrap-autocomplete format
|
|
foreach($contracts as $contract) {
|
|
//$result = ['value' => $contract->id, 'text' => str_replace("'", "\\'", str_replace(["\n", "\r"], " ",$contract->name))];
|
|
$result = ['value' => $contract->id, 'text' => $contract->id.": ".$contract->product_name." [".$contract->matchcode."] (".$contract->owner->getCompanyOrName().", ".$contract->owner->street.", ".$contract->owner->zip." ".$contract->owner->city.")"];
|
|
|
|
$results[] = $result;
|
|
if(count($results) > 15) {
|
|
$results[] = ['value' => 0, 'text' => " --> Mehr Suchergebnisse vorhanden. Bitte Suchbegriff genauer definieren <--"];
|
|
break;
|
|
}
|
|
}
|
|
$this->returnJson($results);
|
|
}
|
|
} |