Overhauled Address linking

This commit is contained in:
Frank Schubert
2022-06-29 17:16:14 +02:00
parent bbfc6c6eb5
commit 2d09924372
12 changed files with 637 additions and 74 deletions
+15 -2
View File
@@ -5,6 +5,7 @@ class Address extends mfBaseModel {
private $parent;
private $childaddresses;
private $links;
private $linked_as;
private $types;
private $attributes;
private $permissions;
@@ -129,7 +130,7 @@ class Address extends mfBaseModel {
return $spin;
}
public function deleteLinks() {
/*public function deleteLinks() {
$links = $this->getProperty("links");
//var_dump($links);exit;
if(is_array($links) && count($links)) {
@@ -141,7 +142,7 @@ class Address extends mfBaseModel {
}
}
}
}
}*/
public function getProperty($name) {
if($this->$name == null) {
@@ -205,6 +206,18 @@ class Address extends mfBaseModel {
return $this->links;
}
if($name == "linked_as") {
$linked_as = AddressLinkModel::search(['address_id' => $this->id]);
foreach($linked_as as $link) {
if(!array_key_exists($link->type, $this->linked_as)) {
$this->linked_as[$link->type] = [];
}
$this->linked_as[$link->type][] = $link;
//var_dump($this->links);exit;
}
return $this->linked_as;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = new $classname($this->$idfield);
+77 -2
View File
@@ -84,13 +84,36 @@ class AddressController extends mfBaseController {
protected function addAction() {
$this->layout()->setTemplate("Address/Form");
$this->layout->set("filter", $this->request->filter);
$parents = AddressModel::search(['parent_id' => null]);
$this->layout()->set("parents", $parents);
}
protected function viewAction() {
$this->layout()->setTemplate("Address/View");
$this->layout->set("filter", $this->request->filter);
$this->layout->set("f", $this->request->f);
$this->layout->set("s", $this->request->s);
$address = new Address($this->request->id);
$this->layout()->set("address", $address);
if(!$address->id) {
$this->layout()->setFlash("Addresse nicht gefunden", "error");
$this->redirect("Address");
}
}
protected function editAction() {
$address = new Address($this->request->id);
$this->layout->set("filter", $this->request->filter);
$this->layout->set("f", $this->request->f);
$this->layout->set("s", $this->request->s);
$this->layout()->set("address", $address);
if(!$address->id) {
@@ -101,6 +124,27 @@ class AddressController extends mfBaseController {
return $this->addAction();
}
protected function deleteLink() {
$id = $this->request->id;
if(!is_numeric($id) || !$id) {
$this->layout()->setFlash("Addresse nicht gefunden", "error");
$this->redirect("Address");
}
$link = new AddressLink($id);
if(!$link->id) {
$this->layout()->setFlash("Addresse nicht gefunden", "error");
$this->redirect("Address");
}
$address_id = $link->origin_address_id;
$link->delete();
$this->layout()->setFlash("Verknüpfung erfolgreich entfernt", "success");
$this->redirect("Address", "edit", ['id' => $address_id]);
}
protected function saveAction() {
$r = $this->request;
$id = $r->id;
@@ -229,7 +273,13 @@ class AddressController extends mfBaseController {
}
$address->deleteLinks();
//$address->deleteLinks();
$existing_links = [];
foreach(AddressLinkModel::search(['origin_address_id', $new_id]) as $elink) {
$existing_links[$elink->address_id] = $elink;
}
//var_dump($r->links);exit;
if(is_array($r->links) && count($r->links)) {
//var_dump($r->links);exit;
@@ -237,17 +287,42 @@ class AddressController extends mfBaseController {
if(!$link['type'] || !$link['address_id']) {
continue;
}
if(array_key_exists($link['address_id'], $existing_links)) {
continue;
}
$l = AddressLinkModel::create([
'origin_address_id' => $new_id,
'type' => $link['type'],
'address_id' => $link['address_id']
]);
$l->save();
}
}
$sq = "";
$query = [];
if($r->s) {
$query['s'] = $r->s;
}
if($r->filter) {
$query["filter"] = $r->filter;
}
if($r->return != "index") {
$query['id'] = $new_id;
}
$qs = http_build_query($query);
$this->layout()->setFlash("Adresse erfolgreich gespeichert.", "success");
$this->redirect("Address", "Edit", ['id' => $new_id]);
if($r->return == "index") {
$this->redirect("Address", "Index", $qs);
}
if($r->f == "view") {
$this->redirect("Address", "View", $qs);
}
$this->redirect("Address", "Edit", $qs);
}
+2 -2
View File
@@ -346,13 +346,13 @@ class AddressModel {
$where .= " AND Address.create_by IN (". implode(",",$create_by).")";
}
}
/*
if(array_key_exists("parents_only", $filter)) {
$po = $filter['parents_only'];
if($po) {
$where .= " AND parent_id IS NULL";
}
}
}*/
//var_dump($filter, $where);exit;
return $where;
}
+19 -1
View File
@@ -36,7 +36,8 @@ class AddressLinkModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("AddressLink", "*", "$where ORDER BY address_id,`create`,origin_address_idLIMIT 1");
$res = $db->select("AddressLink", "*", "$where ORDER BY address_id,`create`,origin_address_id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new AddressLink($data);
@@ -125,6 +126,23 @@ class AddressLinkModel {
}
}
if(array_key_exists("type", $filter)) {
switch($filter['type']) {
case "employee":
$where .= " AND type='employee'";
break;
case "billing":
$where .= " AND type='billing'";
break;
case "contact":
$where .= " AND type='contact'";
break;
case "techcontact":
$where .= " AND type='techcontact'";
break;
}
}
return $where;
}
+12
View File
@@ -3,6 +3,7 @@
class Order extends mfBaseModel {
private $owner;
private $billingaddress;
private $techcontact;
private $products;
private $customer_type;
private $contracts;
@@ -251,6 +252,17 @@ class Order extends mfBaseModel {
return $this->billingaddress;
}
if($name == "techcontact") {
$this->techcontact = mfValuecache::singleton()->get("mfObjectmodel-Address-".$this->techcontact_id);
if($this->techcontact === null) {
$this->techcontact = new Address($this->techcontact_id);
if($this->techcontact->id) {
mfValuecache::singleton()->set("mfObjectmodel-Address-".$this->techcontact_id, $this->techcontact);
}
}
return $this->techcontact;
}
if($name == "products") {
$this->products = OrderProductModel::search(["order_id" => $this->id]);
//var_dump($this->products);exit;
+82 -2
View File
@@ -546,6 +546,44 @@ class OrderController extends mfBaseController {
}
}
// validate techcontact
$techcontact = false;
if($r->techcontact_id) {
// techcontact can be empty
if(is_numeric($r->techcontact_id)) {
$techcontact = new Address($r->techcontact_id);
if(!$techcontact->id) {
$this->layout()->setFlash("Ungültiger technischer Kontakt.", "error");
$this->layout()->set("order", $r);
return $this->add();
}
}
}
// if any required part of new techcontact is set
$techcontact_new = false;
if(!$r->techcontact_id
&& ($r->techcontact_company || $r->techcontact_firstname || $r->techcontact_lastname
|| $r->techcontact_street || $r->techcontact_zip || $r->techcontact_city || $r->techcontact_phone || $r->techcontact_email)
) {
$techcontact_new = true;
if(!$r->techcontact_company && !$r->techcontact_firstname && !$r->techcontact_lastname) {
$this->layout()->setFlash("Fehler in technischem Kontakt: Firmenname oder Name benötigt.");
$this->layout()->set("order", $r);
return $this->add();
}
if(!$r->techcontact_phone && !$r->techcontact_mobile && !$r->techcontact_email) {
$this->layout()->setFlash("Fehler in technischem Kontakt: Kontaktmöglichkeit benötigt.");
$this->layout()->set("order", $r);
return $this->add();
}
}
// validate sepa
if(!$r->billing_type) {
$this->layout()->setFlash("Ungültige Verrechnungsart.");
@@ -566,6 +604,7 @@ class OrderController extends mfBaseController {
// create objects for saving (if new) but don't save yet
$owner_data = [];
$billing_data = [];
$techcontact_data = [];
$request = $r->get();
foreach($request as $field => $value) {
@@ -577,6 +616,9 @@ class OrderController extends mfBaseController {
if($m[1] == "billing" && !$billingaddress) {
$billing_data[$m[2]] = $value;
}
if($m[1] == "techcontact" && !$techcontact) {
$techcontact_data[$m[2]] = $value;
}
}
}
@@ -622,6 +664,9 @@ class OrderController extends mfBaseController {
if(!$billingaddress) {
$billingaddress = AddressModel::create($billing_data);
}
if(!$techcontact) {
$techcontact = AddressModel::create($techcontact_data);
}
// create or save Order object
@@ -632,6 +677,9 @@ class OrderController extends mfBaseController {
if(is_numeric($r->billingaddress_id)) {
$order_data['billingaddress_id'] = $r->billingaddress_id;
}
if(is_numeric($r->techcontact_id)) {
$order_data['techcontact_id'] = $r->techcontact_id;
}
$order_data['partner_number'] = $r->partner_number;
$order_data['allow_contact'] = ($r->allow_contact) ? 1 : 0;
@@ -685,7 +733,7 @@ class OrderController extends mfBaseController {
exit;*/
if(!$owner || !$billingaddress) {
if(!$owner) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
$this->layout()->set("order", $order);
return $this->add();
@@ -720,6 +768,38 @@ class OrderController extends mfBaseController {
$order->billingaddress_id = $billingaddress_id;
$order->save();
// add billingaddress as AddressLink to owner
$linkdata = [];
$linkdata['address_id'] = $billingaddress->id;
$linkdata['origin_address_id'] = $order->owner_id;
$linkdata['type'] = "billing";
$link = AddressLinkModel::create($linkdata);
$link->save();
}
if($techcontact_new) {
$techcontact_id = $techcontact->save();
if(!$techcontact_id) {
$this->layout()->setFlash("Fehler beim Speichern des technischen Kontakts", "error");
$this->redirect("Order", "edit", ['id' => $new_id]);
}
// create addresstype (techcontact)
$at = AddresstypeModel::create(['address_id' => $techcontact_id, 'type' => "techcontact"]);
$at->save();
$order->techcontact_id = $techcontact_id;
$order->save();
}
// add techcontact as AddressLink to owner if not exists
$link = AddressLinkModel::getFirst(['address_id' => $techcontact->id, 'origin_address_id' => $order->owner_id, 'type' => "techcontact"]);
if(!$link) {
$linkdata = [];
$linkdata['address_id'] = $techcontact->id;
$linkdata['origin_address_id'] = $order->owner_id;
$linkdata['type'] = "techcontact";
$link = AddressLinkModel::create($linkdata);
$link->save();
}
@@ -986,7 +1066,7 @@ class OrderController extends mfBaseController {
if($r->return == "index") {
$this->redirect("Order", "Index", $qs, "order=$new_id-details");
} else {
$this->redirect("Order", "edit", $qs, "order=$new_id-details");
$this->redirect("Order", "edit", $qs);
}
}
+1
View File
@@ -3,6 +3,7 @@
class OrderModel {
public $owner_id;
public $billingaddress_id;
public $techcontact_id;
public $upgrade;
public $partner_number;
public $order_date;