needlogin = true; $me = new User(); $me->loadMe(); $this->me = $me; $this->layout()->set("me", $me); if (!$me->is(["Admin", "salespartner", "netowner"])) { $this->redirect("Dashboard"); } } protected function indexAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $this->layout()->setTemplate("Contract/Index"); if ($this->request->resetFilter) { unset($_SESSION[MFAPPNAME . '-Contract-filter']); } $filter = []; if (is_array($this->request->filter)) { $filter = $this->request->filter; $_SESSION[MFAPPNAME . '-Contract-filter'] = $filter; } else { if (array_key_exists(MFAPPNAME . '-Contract-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-Contract-filter'])) { $filter = $_SESSION[MFAPPNAME . '-Contract-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); } //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 (array_key_exists("show_credit", $filter)) { if ($filter["show_credit"] == 0) { $new_filter["price>="] = 0; } unset($filter["show_credit"]); } else { $new_filter["price>="] = 0; } if (is_array($filter) && count($filter)) { foreach ($filter as $name => $value) { $new_filter[$name] = $value; } } return $new_filter; } protected function viewAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $this->layout()->setTemplate("Contract/View"); $id = $this->request->contract_id; if (!$id) { $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 cancelAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $this->layout()->setTemplate("Contract/CancelForm"); $id = $this->request->contract_id; if (!$id) $id = $this->request->id; if(!$id) { $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"); } if(!$contract->billing_period) { $this->layout()->setFlash("Kündigung nicht möglich, Produkt ist Einmalprodukt!", "error"); $this->redirect("Contract", "view", ["contract_id" => $contract->id]); } $today = new DateTime(); $tomorrow = clone($today); $tomorrow->modify("+1 day"); $this->layout()->set("tomorrow", $tomorrow); $this->layout()->set("term_end_date", $contract->getRegularCanceldate()); $this->layout()->set("period_end_date", $contract->getNextBillingPeriodEnd()); $this->layout()->set("contract", $contract); } protected function saveCancel() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $r = $this->request; $id = $r->contract_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"); } try { $cancel_date = DateTime::createFromFormat("d.m.Y", trim($r->cancel_date), new DateTimeZone("Europe/Vienna")); $cancel_date->setTime(23,59,59); } catch(Exception $e) { $this->layout()->setFlash("Ungültiges Datumsformat"); $this->redirect("Contract", "cancel", ["contract_id" => $contract->id]); } $contract->cancel_date = $cancel_date->getTimestamp(); if(!$contract->save()) { $this->layout()->setFlash("Verlinkten Vertrag $link_id nicht gefunden", "error"); $this->redirect("Contract", "cancel", ["contract_id" => $contract->id]); } $linked_contracts = []; if(is_array($r->links)) { foreach($r->links as $link_id => $action) { if($action == "cancel") { $link_contract = new Contract($link_id); if (!$link_contract->id) { $this->layout()->setFlash("Verlinkten Vertrag $link_id nicht gefunden", "warning"); continue; } $link_contract->cancel_date = $cancel_date->getTimestamp(); if(!$link_contract->save()) { $this->layout()->setFlash("Fehler beim Speichern von verlinktem Vertrag", "warning"); } if($link_contract->owner_id != $contract->owner_id) continue; $linked_contracts[] = $link_contract; } } } $contract->sendCancelNotification($linked_contracts); $this->layout()->setFlash("Kündigung gespeichert", "success"); $this->redirect("Contract", "view", ["contract_id" => $contract->id]); } protected function sendCancelNotification() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $contract_id = $this->request->contract_id; $contract = new Contract($contract_id); $linked_contracts = []; foreach($contract->links as $link) { if($link->origin_contract_id == $contract_id) { $link_contract = $link->contract; } else { $link_contract = $link->origin; } if($link_contract->owner_id != $contract->owner_id) continue; $linked_contracts[] = $link_contract; } $contract->sendCancelNotification($linked_contracts); $this->layout()->setFlash("gesendet", "success"); $this->redirect("Contract", "view", ["contract_id" => $contract_id]); } protected function productchangeAction() { $this->layout()->setTemplate("Contract/ProductchangeForm"); $f = $this->request->f; if(!$f) { $f = "c"; // from Contract } $this->layout()->set("f", $f); $id = $this->request->contract_id; if (!$id) { $id = $this->request->id; } if (!is_numeric($id) || !$id) { $this->layout()->setFlash("Vertrag nicht gefunden", "error"); if($f == "o") { $this->redirect("Order", "addUpgrade"); } else { $this->redirect("Contract"); } } $contract = new Contract($id); if (!$contract->id) { $this->layout()->setFlash("Vertrag nicht gefunden", "error"); if($f == "o") { $this->redirect("Order", "addUpgrade"); } else { $this->redirect("Contract"); } } if($this->me->isAdmin()) { $this->layout()->set("terminations", TerminationModel::getAll()); } else { // check permissions // check if correct network $my_network_ids = []; foreach($this->me->my_networks as $network) { $my_network_ids[] = $network->id; } if($contract->termination_id) { if(!in_array($contract->termination->network_id, $my_network_ids)) { if($f == "o") { // from Order, redirect back to Order $this->layout()->setFlash("Keine Berechtigung", "error"); $this->redirect("Order", "addUpgrade", ["owner_id" => $contract->owner_id]); } } } $terms = TerminationModel::search(["network_id" => $my_network_ids]); $this->layout()->set("terminations", $terms); } $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 saveProductchangeAction() { if(!$this->me->is(["Admin", "salespartner", "netowner"])) { $this->redirect("Dashboard"); } $r = $this->request; //var_dump($r->links);exit; $f = $r->f; if(!$f) { $f = "c"; // from Contract } $id = $r->contract_id; if (!is_numeric($id) || !$id) { $this->layout()->setFlash("Vertrag nicht gefunden", "error"); if($f == "o") { $this->redirect("Order", "addUpgrade"); } else { $this->redirect("Contract"); } } $contract = new Contract($id); if (!$contract->id) { $this->layout()->setFlash("Vertrag nicht gefunden", "error"); if($f == "o") { $this->redirect("Order", "addUpgrade"); } else { $this->redirect("Contract"); } } $new_contract = clone($contract); $contract_data = []; $contract_data['product_id'] = $r->product_id; $contract_data['product_name'] = trim($r->product_name); $contract_data['product_info'] = trim($r->product_info); $contract_data['matchcode'] = trim($r->matchcode); $contract_data['termination_id'] = $r->termination_id; $contract_data['amount'] = 1; $contract_data['price'] = $r->price; $contract_data['price_setup'] = $r->price_setup; $contract_data['price_nne'] = $r->price_nne; $contract_data['price_nbe'] = $r->price_nbe; $contract_data['note'] = trim($r->note); /* * termination check */ $product = new Product($r->product_id); if (!$product->id) { $this->layout()->setFlash("Produkt nicht gefunden", "error"); if($f == "o") { $this->redirect("Order", "productchange", ["contract_id" => $id]); } else { $this->redirect("Contract", "productchange", ["contract_id" => $id]); } } $contract_data['product_external'] = $product->external; $contract_data['product_external_id'] = $product->external_id; $contract_data['sla_id'] = $product->sla_id; if($r->finish_date) { try { $finish_date = DateTime::createFromFormat("d.m.Y", $r->finish_date, new DateTimeZone("Europe/Vienna")); } catch (Exception $e) { $this->layout()->setFlash("Ungültiges Kündigungsdateum", "error"); if($f == "o") { $this->redirect("Order", "productchange", ["contract_id" => $id]); } else { $this->redirect("Contract", "productchange", ["contract_id" => $id]); } } $finish_date->setTime(0,0,0); $contract_data["finish_date"] = $finish_date->getTimestamp(); $contract_cancel_date = clone($finish_date); $contract_cancel_date->modify("-1 day"); $contract_cancel_date->setTime(23,59,59); } $require_term = false; if (array_key_exists(TT_ATTRIB_TERMINATION_REQUIRED_NAME, $product->attributes) && $product->attributes[TT_ATTRIB_TERMINATION_REQUIRED_NAME]->value == 1) { //var_dump($prod->attributes); $require_term = true; $termination = new Termination($contract_data['termination_id']); if (!$contract_data['termination_id'] || !$termination->id) { $this->layout()->setFlash("Produkt erfordert Anschluss.", "error"); if($f == "o") { $this->redirect("Order", "productchange", ["contract_id" => $id]); } else { $this->redirect("Contract", "productchange", ["contract_id" => $id]); } } } else { $contract_data['termination_id'] = null; } //var_dump($r->links); // lookup credit contract and if it's missing in $r->links if(!$this->me->is("Admin")) { $credit_link = ContractLinkModel::includesContractId($contract->id, ["type" => "credit"]); if($credit_link) { if(is_array($r->links) && !array_key_exists($credit_link->id, $r->links)) { $r->links[$credit_link->id] = []; } $r->links[$credit_link->id]["action"] = "keep"; } } //var_dump($r->links);exit; $new_contract->update($contract_data); $new_contract_id = $new_contract->save(); if (!$new_contract_id) { $this->layout()->setFlash("Neuer Contract konnte nicht gespeichert werden", "error"); if($f == "o") { $this->redirect("Order", "productchange", ["contract_id" => $id]); } else { $this->redirect("Contract", "productchange", ["contract_id" => $id]); } } if($contract_cancel_date) { $contract->cancel_date = $contract_cancel_date->getTimestamp(); $contract->save(); } $journal = ContractjournalModel::create([ 'contract_id' => $new_contract->id, 'type' => "created_from", 'value' => "productchange", 'text' => "Produkt-/Standortwechsel von Contract ID ".$contract->id ]); $journal->save(); if (is_array($r->links) && count($r->links)) { foreach ($r->links as $link_id => $link_data) { $action = $link_data["action"]; $cancel_date = false; if($link_data["cancel_date"]) { try { $cancel_date = DateTime::createFromFormat("d.m.Y", $link_data["cancel_date"], new DateTimeZone("Europe/Vienna")); } catch (Exception $e) { $this->layout()->setFlash("Ungültiges Kündigungsdatum", "error"); if($f == "o") { $this->redirect("Order", "productchange", ["contract_id" => $id]); } else { $this->redirect("Contract", "productchange", ["contract_id" => $id]); } } } $old_link = new ContractLink($link_id); if (!$old_link->id) continue; // check if link contains this contract if ($old_link->contract_id == $contract->id) { $origin_id = $old_link->origin_contract_id; $link_contract_id = $old_link->contract_id; $new_link_contract_id = $new_contract->id; $new_link_origin_id = $old_link->origin_contract_id; } elseif ($old_link->origin_contract_id == $contract->id) { $origin_id = $old_link->contract_id; $link_contract_id = $old_link->origin_contract_id; $new_link_contract_id = $old_link->contract_id; $new_link_origin_id = $new_contract->id; } else { continue; } if($action != "cancel" && $old_link->type != "credit") { $new_link = ContractLinkModel::create([ 'contract_id' => $new_link_contract_id, 'origin_contract_id' => $new_link_origin_id, 'type' => $old_link->type, ]); if (!$new_link->save()) { $this->layout()->setFlash("Konnte neuen Link nicht speichern", "warn"); } } if ($action == "cancel") { if($cancel_date && $contract_cancel_date) { // insert cancel_date in old contract $lc = new Contract($origin_id); $lc->cancel_date = $cancel_date->getTimestamp(); $lc->save(); } else { // leave cancellation for later (when finishing upgrade) $old_link->change_action = "cancel"; if (!$old_link->save()) { $this->layout()->setFlash("Konnte alten Link nicht speichern", "warn"); } } } if ($old_link->type == "credit" && $action == "keep") { // XXX - if we have finish date then recreate credit contract right now if($contract_cancel_date) { $new_credit = ContractModel::createCreditForContract($new_contract); $new_credit->save(); // create journal for credit $journal = ContractjournalModel::create([ 'contract_id' => $new_credit->id, 'type' => "created_from", 'value' => "productchange", 'text' => "Produkt-/Standortwechsel von Contract ID ".$new_link_origin_id ]); $journal->save(); $this->log->debug(print_r($new_credit, true)); // set cancel date for old credit $old_credit = new Contract($origin_id); $old_credit->cancel_date = $contract_cancel_date->getTimestamp(); $old_credit->save(); // create link to new credit contract $link = ContractLinkModel::create([ "contract_id" => $new_credit->id, "origin_contract_id" => $new_contract->id, "type" => "credit" ]); $link->save(); // create upgrade link from old to new credit contract $link = ContractLinkModel::create([ "contract_id" => $new_credit->id, "origin_contract_id" => $origin_id, "type" => "upgrade" ]); $link->save(); } else { $old_link->change_action = "recreate"; $old_link->save(); } } //var_dump($new_link);exit; } } /* * Upgrade Link erstellen */ $change_type = "upgrade"; /*if($contract->product_id != $new_contract->product_id) { $change_type = "upgrade"; } elseif($contract->matchcode != $new_contract->matchcode) { $change_type = "relocation"; } else { $change_type = "productchange"; }*/ $link = ContractLinkModel::create([ 'contract_id' => $new_contract_id, 'origin_contract_id' => $id, 'type' => $change_type ]); $link_id = $link->save(); if (!$link_id) { $this->layout()->setFlash("Konnte Verknüpfung nicht speichern", "warn"); } $new_contract->sendProductchangeNotification($contract); $this->layout()->setFlash("Produktwechsel erfolgreich erstellt", "success"); if($f == "o") { $this->redirect("Order","Upgrades"); } else { $this->redirect("Contract", "view", ["contract_id" => $new_contract_id]); } } protected function finishContractAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $r = $this->request; $id = $r->contract_id; if (!is_numeric($id) || !$id) { $this->layout()->setFlash("Contract nicht gefunden", "error"); $this->redirect("Contract"); } $contract = new Contract($id); if (!$contract->id) { $this->layout()->setFlash("Contract nicht gefunden", "error"); $this->redirect("Contract"); } $now = new DateTime("now"); $now->setTime(0,0,0); $contract->finish_date = $now->getTimestamp(); $contract->finish_date_by = $this->me->id; try { $saved = $contract->save(); } catch(Exception $e) { $saved = false; } if(!$saved) { $this->layout()->setFlash("Contract konnte nicht gespeichert werden", "error"); $this->redirect("Contract", "view", ['contract_id' => $id]); } // create Journal $journal = ContractjournalModel::create([ 'contract_id' => $contract->id, 'type' => "contract_finished" ]); $journal_id = $journal->save(); $this->layout()->setFlash("Contract erfolgreich fertiggestellt", "success"); $this->redirect("Contract", "view", ['contract_id' => $id]); } protected function addAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $this->layout()->setTemplate("Contract/Form"); $this->layout()->set("terminations", TerminationModel::getAll()); if ($this->request->origin_contract_id) { $origin = new Contract($this->request->origin_contract_id); if ($origin->id) { $contract = new Contract(); $contract->owner_id = $origin->owner_id; $contract->billingaddress_id = $origin->billingaddress_id; $contract->matchcode = $origin->matchcode; //var_dump($contract);exit; $this->layout()->set("contract", $contract); $this->layout()->set("origin_contract_id", $origin->id); } } } protected function editAction() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $id = $this->request->contract_id; if (!$id) { $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); //var_dump($contract->owner);exit; 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() { if(!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $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['vatgroup_id'] = $r->vatgroup_id; $contract_data['sla_id'] = (float)$r->sla_id; $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['note'] = $r->note; if($r->termination_id) { $contract_data["termination_id"] = $r->termination_id; } if($r->order_date) { $order_date = new DateTime("@" . $this->dateToTimestamp($r->order_date)); $order_date->setTimezone(new DateTimeZone("Europe/Vienna")); $order_date->setTime(0, 0, 0); $contract_data['order_date'] = $order_date->getTimestamp(); } else { $contract_data['order_date'] = null; } if($r->finish_date) { $finish_date = new DateTime("@" . $this->dateToTimestamp($r->finish_date)); $finish_date->setTimezone(new DateTimeZone("Europe/Vienna")); $finish_date->setTime(0, 0, 0); $contract_data['finish_date'] = $finish_date->getTimestamp(); } else { $contract_data['finish_date'] = null; } if($r->cancel_date) { $cancel_date = new DateTime("@" . $this->dateToTimestamp($r->cancel_date)); $cancel_date->setTimezone(new DateTimeZone("Europe/Vienna")); $cancel_date->setTime(0, 0, 0); $contract_data['cancel_date'] = $cancel_date->getTimestamp(); } else { $contract_data['cancel_date'] = null; } //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 (!in_array($contract_data['billing_period'], [0,1,12])) { $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(); } // create journal if ($mode == "add") { $journal = ContractjournalModel::create([ 'contract_id' => $contract_id, 'type' => "created_from", 'value' => "manual" ]); $journal->save(); } $this->layout()->setFlash("Vertrag erfolgreich gespeichert.", "success"); /* * Create link to origin contract if set */ if ($mode == "add" && $r->origin_contract_id) { $origin = new Contract($r->origin_contract_id); if ($origin->id) { $link = ContractLinkModel::create([ 'contract_id' => $contract_id, 'origin_contract_id' => $origin->id, 'type' => 'link' ]); $link_id = $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(); } } } /* 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", "salespartner", "netowner"])) { $this->redirect("Dashboard"); } $do = $this->request->do; $data = []; switch ($do) { case "getContract": $return = $this->getContractApi(); break; case "findContracts": $return = $this->getContractsApi(); break; case "findContract": $return = $this->findContractApi(); break; case "": $return = ""; 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 getContractApi() { if (!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $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 getContractsApi() { $owner_id = $this->request->owner_id; if(!$owner_id) return false; $return = []; $contracts = ContractModel::search(["owner_id" => $owner_id]); if(!$contracts) { header("Content-type: application/json"); echo json_encode([]); exit; } $is_valid_owner = false; if(!$this->me->is("Admin")) { foreach($contracts as $contract) { foreach(ContractLinkModel::includesContractId($contract->id) as $link) { if($link->type != "credit") continue; $link_contract = $link->contract; if($link->contract_id == $contract->id) $link_contract = $link->origin; if($link_contract->owner_id == $this->me->address_id) { $is_valid_owner = true; break; } } } if(!$is_valid_owner) { header("Content-type: application/json"); echo json_encode([]); exit; } } foreach($contracts as $contract) { $c = get_object_vars($contract->data); $c["id"] = $contract->id; $return[] = $c; } header("Content-type: application/json"); echo json_encode($return); exit; } private function findContractApi() { if (!$this->me->is(["Admin"])) { $this->redirect("Dashboard"); } $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); } }