321 lines
9.1 KiB
PHP
321 lines
9.1 KiB
PHP
<?php
|
|
|
|
class AddressController extends mfBaseController {
|
|
private $filter;
|
|
|
|
protected function init() {
|
|
$this->needlogin=true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
$this->layout()->set("me",$me);
|
|
|
|
if(!$me->isAdmin()) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$rfilter = $this->request->filter;
|
|
iF(!is_array($rfilter)) {
|
|
$rfilter = [];
|
|
}
|
|
if(!array_key_exists("addresstype", $rfilter)) {
|
|
$rfilter["addresstype"] = [];
|
|
}
|
|
|
|
$this->layout->set("filter", $rfilter);
|
|
|
|
$filter = $this->getPreparedFilter($rfilter);
|
|
|
|
// pagination defaults
|
|
$pagination = [];
|
|
$pagination['start'] = 0;
|
|
$pagination['count'] = 25;
|
|
$pagination['maxItems'] = 0;
|
|
|
|
if(is_numeric($this->request->s)) {
|
|
$pagination['start'] = intval($this->request->s);
|
|
}
|
|
//var_dump($filter);exit;
|
|
$pagination['maxItems'] = AddressModel::count($filter);
|
|
$addresses = AddressModel::search($filter, $pagination);
|
|
|
|
$this->layout()->set("addresses", $addresses);
|
|
$this->layout()->set("request", $this->request);
|
|
$this->layout()->set("pagination", $pagination);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
//var_dump($this->request->filter);
|
|
$default_filter = ['parents_only' => 1];
|
|
if(is_array($this->request->filter) && count($this->request->filter)) {
|
|
$filter = array_merge($default_filter, $this->request->filter);
|
|
} else {
|
|
$filter = $default_filter;
|
|
}
|
|
//var_dump($filter);exit;
|
|
$addresses = AddressModel::search($filter);
|
|
|
|
$this->layout()->set("addresses", $addresses);
|
|
$this->layout()->set("filter", $filter);
|
|
$this->layout()->set("request", $this->request);
|
|
}
|
|
|
|
|
|
private function getPreparedFilter($filter) {
|
|
$new_filter = [];
|
|
|
|
if(is_array($filter) && count($filter)) {
|
|
if(!array_key_exists("parents_only", $filter)) {
|
|
$new_filter["parents_only"] = 1;
|
|
}
|
|
|
|
foreach($filter as $name => $value) {
|
|
$new_filter[$name] = $value;
|
|
}
|
|
}
|
|
|
|
return $new_filter;
|
|
}
|
|
|
|
protected function addAction() {
|
|
$this->layout()->setTemplate("Address/Form");
|
|
|
|
$parents = AddressModel::search(['parent_id' => null]);
|
|
$this->layout()->set("parents", $parents);
|
|
}
|
|
|
|
protected function editAction() {
|
|
$address = new Address($this->request->id);
|
|
|
|
$this->layout()->set("address", $address);
|
|
|
|
if(!$address->id) {
|
|
$this->layout()->setFlash("Addresse nicht gefunden", "error");
|
|
return $this->addAction();
|
|
}
|
|
|
|
return $this->addAction();
|
|
}
|
|
|
|
protected function saveAction() {
|
|
$r = $this->request;
|
|
$id = $r->id;
|
|
//var_dump($r);exit;
|
|
if(is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$address = new Address($id);
|
|
if(!$address->id) {
|
|
$this->layout()->setFlash("Addresse nicht gefunden", "error");
|
|
$this->redirect("Address");
|
|
}
|
|
} else {
|
|
$mode = "add";
|
|
}
|
|
|
|
//var_dump($r->addresstypes);exit;
|
|
|
|
$data = [];
|
|
$data['parent_id'] = (!$r->parent_id) ? null : $r->parent_id;
|
|
$data['company'] = $r->company;
|
|
$data['firstname'] = $r->firstname;
|
|
$data['lastname'] = $r->lastname;
|
|
$data['street'] = $r->street;
|
|
$data['zip'] = $r->zip;
|
|
$data['city'] = $r->city;
|
|
$data['country'] = $r->country;
|
|
$data['phone'] = $r->phone;
|
|
$data['fax'] = $r->fax;
|
|
$data['mobile'] = $r->mobile;
|
|
$data['email'] = $r->email;
|
|
$data['note'] = $r->note;
|
|
$data['uid'] = $r->uid;
|
|
|
|
|
|
// billing data
|
|
// validate sepa
|
|
if(!$r->billing_type) {
|
|
$this->layout()->setFlash("Ungültige Verrechnungsart.");
|
|
$this->layout()->set("order", $r);
|
|
return $this->add();
|
|
}
|
|
|
|
if($r->billing_type == "sepa") {
|
|
foreach(['owner', 'iban', 'bic'] as $required) {
|
|
if(!$r->{"bank_account_$required"}) {
|
|
$this->layout()->setFlash("Bitte Bankdaten für SEPA ausfüllen.", "warn");
|
|
$this->layout()->set("address", $r);
|
|
return $this->add();
|
|
}
|
|
}
|
|
}
|
|
|
|
if($r->billing_type == "sepa") {
|
|
$data['billing_type'] = "sepa";
|
|
} else {
|
|
$data['billing_type'] = "invoice";
|
|
}
|
|
|
|
if($r->billing_delivery == "paper") {
|
|
$data['billing_delivery'] = "paper";
|
|
} else {
|
|
$data['billing_delivery'] = "email";
|
|
}
|
|
|
|
|
|
$data['bank_account_bank'] = $r->bank_account_bank;
|
|
$data['bank_account_owner'] = $r->bank_account_owner;
|
|
$data['bank_account_iban'] = $r->bank_account_iban;
|
|
$data['bank_account_bic'] = $r->bank_account_bic;
|
|
$data['allow_contact'] = ($r->allow_contact) ? 1 : 0;
|
|
$data['allow_spin'] = ($r->allow_spin) ? 1 : 0;
|
|
|
|
|
|
$data['edit_by'] = 1;
|
|
|
|
if($mode == "add") {
|
|
$data['create_by'] = 1;
|
|
$address = AddressModel::create($data);
|
|
} else {
|
|
$address->update($data);
|
|
}
|
|
|
|
//var_dump($address);exit;
|
|
|
|
$new_id = $address->save();
|
|
if(!$new_id) {
|
|
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
|
$this->layout()->set("address", $address);
|
|
return $this->add();
|
|
}
|
|
|
|
$new_types = $r->addresstypes;
|
|
if(is_array($new_types)) {
|
|
foreach($address->types as $existing_type) {
|
|
//var_dump($existing_type);
|
|
//var_dump($new_types);
|
|
//echo $existing_type->type;
|
|
if(!in_array($existing_type->type, $new_types)) {
|
|
$existing_type->delete();
|
|
} else {
|
|
// remove existing type from new_types array (dont need to create again)
|
|
$new_types = array_diff($new_types, [$existing_type->type]);
|
|
//unset($new_types[$existing_type]);
|
|
}
|
|
}
|
|
//exit;
|
|
foreach($new_types as $type) {
|
|
$type_object = AddresstypeModel::create(['address_id' => $address->id, 'type' => $type]);
|
|
$type_object->save();
|
|
$address->types[$type] = $type_object;
|
|
}
|
|
}
|
|
|
|
$attribs = $r->attributes;
|
|
//var_dump($attribs);exit;
|
|
if(is_array($attribs) && count($attribs)) {
|
|
foreach($attribs as $attrib => $value) {
|
|
$aa = AddressattributeModel::getFirst(["address_id" => $new_id, "name" => $attrib]);
|
|
|
|
if(!$aa) {
|
|
$aa = AddressattributeModel::create(["address_id" => $new_id, "name" => $attrib]);
|
|
}
|
|
$aa->value = $value;
|
|
$aa->save();
|
|
}
|
|
}
|
|
|
|
$this->layout()->setFlash("Adresse erfolgreich gespeichert.", "success");
|
|
$this->redirect("Address", "Edit", ['id' => $new_id]);
|
|
}
|
|
|
|
|
|
protected function apiAction() {
|
|
if(!$this->me->is(["Admin","salespartner"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
$do = $this->request->do;
|
|
$data = [];
|
|
|
|
switch($do) {
|
|
case "findAddress":
|
|
$return = $this->findAddressApi();
|
|
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 findAddressApi() {
|
|
$search = $this->request->q;
|
|
$autocomplete = $this->request->autocomplete;
|
|
|
|
$po = 1;
|
|
$role = false;
|
|
// if searching for billingaddress, set role and parents_only to 0
|
|
if($this->request->role == "billingaddress") {
|
|
$role = "billing";
|
|
$po = 0;
|
|
}
|
|
|
|
$addresses = [];
|
|
$addresses = array_merge($addresses, AddressModel::search(["parents_only" => $po, "addresstype" => [$role], "company" => $search]));
|
|
$addresses = array_merge($addresses, AddressModel::search(["parents_only" => $po, "addresstype" => [$role], "firstname" => $search]));
|
|
$addresses = array_merge($addresses, AddressModel::search(["parents_only" => $po, "addresstype" => [$role], "lastname" => $search]));
|
|
|
|
if(!is_array($addresses) && !count($addresses)) {
|
|
return false;
|
|
}
|
|
|
|
$all_addresses = [];
|
|
|
|
// remove duplicates
|
|
foreach($addresses as $address) {
|
|
if(!array_key_exists($address->id, $all_addresses)) {
|
|
$all_addresses[$address->id] = $address;
|
|
}
|
|
}
|
|
|
|
$results = [];
|
|
|
|
if(!$autocomplete) {
|
|
foreach($all_addresses as $id => $address) {
|
|
$results[$id] = $address->getCompanyOrName()." (".$address->zip." ".$address->city.", ".$address->street.")".(($address->customer_number) ? " [".$address->customer_number."]" : "");
|
|
if(count($results) > 5) {
|
|
$results["more"] = "...";
|
|
break;
|
|
}
|
|
}
|
|
|
|
return ["addresses" => $results];
|
|
}
|
|
|
|
// return bootstrap-autocomplete format
|
|
foreach($all_addresses as $id => $address) {
|
|
$result = ['value' => $id, 'text' => $address->getCompanyOrName()." (".$address->zip." ".$address->city.", ".$address->street.")".(($address->customer_number) ? " [".$address->customer_number."]" : "")];
|
|
$results[] = $result;
|
|
if(count($results) > 5) {
|
|
$results[] = ['value' => 0, 'text' => " --> Mehr Suchergebnisse vorhanden. Bitte Suchbegriff genauer definieren <--"];
|
|
break;
|
|
}
|
|
}
|
|
|
|
$this->returnJson($results);
|
|
}
|
|
|
|
private function searchAddress() {
|
|
|
|
}
|
|
}
|