From 7646345779bde3edfb14a3eaef0ee9751f322030 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Fri, 5 Jan 2024 13:56:02 +0100 Subject: [PATCH 1/3] WIP Address BMD Export --- Layout/default/Address/Form.php | 8 ++ Layout/default/Address/Index.php | 11 ++- Layout/default/Address/View.php | 3 + Layout/default/Address/bmd_export.csv.php | 9 ++ application/Address/AddressController.php | 97 +++++++++++++++++++ application/Address/AddressModel.php | 29 ++++++ application/System/SystemController.php | 67 +++++++++++++ ...04200158_address_add_fibu_supplier_due.php | 33 +++++++ .../20240104201005_create_system_table.php | 38 ++++++++ lib/Layout/Layout.php | 4 + lib/mvcfronk/mfConfig/mfConfig.php | 18 +++- 11 files changed, 308 insertions(+), 9 deletions(-) create mode 100644 Layout/default/Address/bmd_export.csv.php create mode 100644 application/System/SystemController.php create mode 100644 db/migrations/20240104200158_address_add_fibu_supplier_due.php create mode 100644 db/migrations/20240104201005_create_system_table.php diff --git a/Layout/default/Address/Form.php b/Layout/default/Address/Form.php index 12b5dc107..bff629cb1 100644 --- a/Layout/default/Address/Form.php +++ b/Layout/default/Address/Form.php @@ -256,6 +256,14 @@ + +
+ +
+ + Standard: 30 Tage +
+
diff --git a/Layout/default/Address/Index.php b/Layout/default/Address/Index.php index 170f0336b..d37b6fd14 100644 --- a/Layout/default/Address/Index.php +++ b/Layout/default/Address/Index.php @@ -38,7 +38,10 @@
@@ -47,7 +50,7 @@
-

Filter

+

Filter

">
@@ -157,8 +160,8 @@
- - ">Filter zurücksetzen + + "> Filter zurücksetzen
diff --git a/Layout/default/Address/View.php b/Layout/default/Address/View.php index f3832d092..f62321c14 100644 --- a/Layout/default/Address/View.php +++ b/Layout/default/Address/View.php @@ -128,6 +128,9 @@ FIBU Lieferanten Konto fibu_supplier_number?> + + FIBU Lieferanten Zahlungsziel + fibu_supplier_due?>

Zusatzdaten

diff --git a/Layout/default/Address/bmd_export.csv.php b/Layout/default/Address/bmd_export.csv.php new file mode 100644 index 000000000..b8e3cb6dc --- /dev/null +++ b/Layout/default/Address/bmd_export.csv.php @@ -0,0 +1,9 @@ +Lieferant;Kunde;Kundenummer;FIBU-Verrechnungsnummer;FIBU Lieferantennummer;Zahlungsziel Lieferant;Firma;Vorname;Nachname;Straße Hausnummer;PLZ;Ort;Land;Telefon;Mobil;Mail;UID;Verrechnungsart;Rechnungsversand;Kreditinstitut;Kontoinhaber;IBAN;BIC + +;;;;;;nl2ws($a["company"])?>;nl2ws($a["firstname"])?>;nl2ws($a["lastname"])?>;nl2ws($a["street"])?>;nl2ws($a["$address->zip"])?>;nl2ws($a["city"])?>;nl2ws($a["country"])?>;nl2ws($a["phone"])?>;nl2ws($a["mobile"])?>;nl2ws($a["email"])?>;nl2ws($a["uid"])?>;;;;;; + +type("int"); + $last_export->value(date("U")); + $last_export->save(); + */ + $last_export = 0; + + $export_ts = new mfConfig("bmd.export.ts"); + if($export_ts->value()) { + $last_export = $export_ts->value(); + } + + $type = "inc"; + + if($this->request->type == "full") { + $last_export = 0; + $type = "full"; + } + + if(!file_exists(TT_ADDRESS_BMD_EXPORT_PATH)) { + $this->layout()->setFlash("Export Pfad (".TT_ADDRESS_BMD_EXPORT_PATH.") nicht gefunden!", "error"); + } + + $export_ts->value(date('U')); + + $search = ["edit>" => $last_export, "customer_or_fibu_numbers" => true]; + + if(!AddressModel::count($search)) { + $this->layout()->setFlash("Keine geänderten Adressdatensätze gefunden. Export abgebrochen.", "warn"); + $this->redirect("Address"); + } + + $addresses = []; + + foreach(AddressModel::search($search) as $address) { + $a = []; + $a["is_supplier"] = (array_key_exists("supplier", $address->types) && $address->types['supplier']) ? "1" : "0"; + $a["is_customer"] = "1"; + $a["customer_number"] = $address->customer_number; + $a["fibu_account_number"] = $address->fibu_account_number; + $a["fibu_supplier_number"] = $address->fibu_supplier_number; + $a["fibu_supplier_due"] = $address->fibu_supplier_due; + $a["company"] = $address->company; + $a["firstname"] = $address->firstname; + $a["lastname"] = $address->lastname; + $a["street"] = $address->street; + $a["zip"] = $address->zip; + $a["city"] = $address->city; + $a["country"] = $address->country; + $a["phone"] = $address->phone; + $a["mobile"] = $address->mobile; + $a["email"] = $address->email; + $a["uid"] = $address->uid; + $a["billing_type"] = $address->billing_type; + $a["billing_delivery"] = $address->billing_delivery; + if(array_key_exists("billing", $address->links) && $address->links["billing"]) { + $a["bank"] = $address->links["billing"]->bank_account_bank; + $a["bank_owner"] = $address->links["billing"]->bank_account_owner; + $a["iban"] = $address->links["billing"]->bank_account_iban; + $a["bic"] = $address->links["billing"]->bank_account_bic; + } else { + $a["bank"] = $address->bank_account_bank; + $a["bank_owner"] = $address->bank_account_owner; + $a["iban"] = $address->bank_account_iban; + $a["bic"] = $address->bank_account_bic; + } + + $addresses[] = $a; + } + + + + + $tpl = new Layout(); + $tpl->setTemplate("Address/bmd_export.csv"); + $tpl->set("addresses", $addresses); + $csv_content = $tpl->render(); + + $filename = "thetool_address_export_".$type."_".date("Y-m-d-H-i-s").".csv"; + + // save to TT_ADDRESS_BMD_EXPORT_PATH + $filepath = TT_ADDRESS_BMD_EXPORT_PATH."/".$filename; + + if(!file_put_contents($filepath, $csv_content)) { + $this->layout()->setFlash("Datei $filepath konnte nicht gespeichert werden!", "error"); + } else { + $export_ts->save(); + $this->layout()->setFlash("Adressen erfolgreich exportiert", "success"); + } + + $this->redirect("Address"); + + + } + protected function saveAction() { $r = $this->request; $id = $r->id; @@ -204,6 +300,7 @@ class AddressController extends mfBaseController { } else { $data['fibu_primary_account'] = 0; } + $data['fibu_supplier_due'] = ($r->fibu_supplier_due) ? $r->fibu_supplier_due : null; // billing data diff --git a/application/Address/AddressModel.php b/application/Address/AddressModel.php index 3d2759ebe..e6ca33522 100644 --- a/application/Address/AddressModel.php +++ b/application/Address/AddressModel.php @@ -397,6 +397,35 @@ class AddressModel { $where .= " AND parent_id IS NULL"; } }*/ + + if(array_key_exists("create>", $filter)) { + $create = $filter['create>']; + if(is_numeric($create)) { + $where .= " AND Address.create > $create"; + } + } + + if(array_key_exists("create<", $filter)) { + $create = $filter['create<']; + if(is_numeric($create)) { + $where .= " AND Address.create < $create"; + } + } + + if(array_key_exists("edit>", $filter)) { + $edit = $filter['edit>']; + if(is_numeric($edit)) { + $where .= " AND Address.edit > $edit"; + } + } + + if(array_key_exists("edit<", $filter)) { + $edit = $filter['edit<']; + if(is_numeric($edit)) { + $where .= " AND Address.edit < $edit"; + } + } + //var_dump($filter, $where);exit; return $where; } diff --git a/application/System/SystemController.php b/application/System/SystemController.php new file mode 100644 index 000000000..708421775 --- /dev/null +++ b/application/System/SystemController.php @@ -0,0 +1,67 @@ +loadMe(); + $this->layout()->set("me",$me); + $this->me = $me; + + if(!$me->isAdmin()) { + // all users can call non-action methods + if($this->action != "" || $request != null) { + $this->redirect("Dashboard"); + } + } + } + + protected function indexAction() { + if(!$this->me->isAdmin()) { + $this->redirect("Dashboard"); + } + $this->layout()->setTemplate("System/System"); + } + + protected function saveAction($request) { + if(!$this->me->isAdmin()) { + $this->redirect("Dashboard"); + } + foreach($request as $name => $value) { + $m = []; + if(!preg_match('/^system_(.+)$/',$name,$m)) { + continue; + } + $name = str_replace("_", ".", $m[1]); + $config = new mfConfig($name); + $config->value($value); + $config->save(); + } + + $this->redirect("System"); + } + + public function getLastCommit() { + $git_path = BASEDIR."/.git"; + $output = []; + + if(defined("GIT_BIN_PATH") && GIT_BIN_PATH) { + $git = GIT_BIN_PATH; + } else { + $git = "/usr/bin/git"; + } + + $cmd = "$git --git-dir $git_path rev-parse HEAD"; + if(!exec($cmd, $output)) { + $log = mfLoghandler::singleton(); + $log->warn("Cannot read git ref!"); + } + + $ref = $output[0]; + if(strlen($output[0]) > 8) { + $ref = substr($output[0], 0, 8); + } + + return $ref; + } +} \ No newline at end of file diff --git a/db/migrations/20240104200158_address_add_fibu_supplier_due.php b/db/migrations/20240104200158_address_add_fibu_supplier_due.php new file mode 100644 index 000000000..1ac6fa875 --- /dev/null +++ b/db/migrations/20240104200158_address_add_fibu_supplier_due.php @@ -0,0 +1,33 @@ +getEnvironment() == "thetool") { + $table = $this->table("Address"); + $table->addColumn("fibu_supplier_due", "integer", ["null" => true, "default" => null, "after" => "fibu_supplier_number"]); + $table->update(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $table = $this->table("Address"); + $table->removeColumn("fibu_supplier_due"); + $table->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/db/migrations/20240104201005_create_system_table.php b/db/migrations/20240104201005_create_system_table.php new file mode 100644 index 000000000..66b166495 --- /dev/null +++ b/db/migrations/20240104201005_create_system_table.php @@ -0,0 +1,38 @@ +getEnvironment() == "thetool") { + $table = $this->table("System"); + $table->addColumn("active", "integer", ["null" => false, "default" => 1, "limit" => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY]); + $table->addColumn("name", "string", ["null" => false, "limit" => 255]); + $table->addColumn("value", "string", ["null" => false, "limit" => 1024]); + $table->addColumn("type", "enum", ["null" => false, "values" => 'int,float,string,json']); + $table->addColumn("create_by", "integer", ["null" => false]); + $table->addColumn("edit_by", "integer", ["null" => false]); + $table->addColumn("create", "integer", ["null" => false]); + $table->addColumn("edit", "integer", ["null" => false]); + $table->create(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $this->table("System")->drop(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/lib/Layout/Layout.php b/lib/Layout/Layout.php index 69d4d2d7f..5d0122c12 100644 --- a/lib/Layout/Layout.php +++ b/lib/Layout/Layout.php @@ -43,6 +43,10 @@ class Layout extends mfLayout { return date("d.m.Y", $int); } + public function nl2ws($string) { + return str_replace(["\n","\r","\t","\v"], " ", $string); + } + /* * Gets mfBaseModel object from Cache or gets in from DB and saves it to Cache */ diff --git a/lib/mvcfronk/mfConfig/mfConfig.php b/lib/mvcfronk/mfConfig/mfConfig.php index 4049879d0..fcbc8c7d2 100644 --- a/lib/mvcfronk/mfConfig/mfConfig.php +++ b/lib/mvcfronk/mfConfig/mfConfig.php @@ -7,6 +7,10 @@ class mfConfig { private $value; private $active; private $type; + public $create_by; + public $edit_by; + public $create; + public $edit; private $me; public function __construct($name) { @@ -14,22 +18,27 @@ class mfConfig { $name = $this->db->escape($name); $this->name = $name; - - $me = new User(); + + if(defined("INTERNAL_USER_ID")) { - $me->id = INTERNAL_USER_ID; + $me = new User(INTERNAL_USER_ID); } else { + $me = new User(); $me->loadMe(); } $this->me = $me; - $res = $this->db->select("System", "*", "name='$name'"); + $res = $this->db->select("System", "*", "name='$name' LIMIT 1"); if($this->db->num_rows($res)) { $data = $this->db->fetch_object($res); $this->id = $data->id; $this->value = $data->value; $this->active = $data->active; $this->type = $data->type; + $this->create_by = $data->create_by; + $this->edit_by = $data->edit_by; + $this->create = $data->create; + $this->edit = $data->edit; } else { $this->active = 1; $this->type = "string"; @@ -54,7 +63,6 @@ class mfConfig { return $this->active; } - public function save() { $id = $this->id; From 3591da6729cc4b32532d16f4a9130d890e1998f3 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Fri, 5 Jan 2024 17:45:50 +0100 Subject: [PATCH 2/3] Moved Building unit count to Building Model --- application/Building/Building.php | 18 ++++++++++++++++++ application/Termination/Termination.php | 12 +++++++++++- .../Termination/TerminationController.php | 9 +++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/application/Building/Building.php b/application/Building/Building.php index 5eac00956..19b14d447 100644 --- a/application/Building/Building.php +++ b/application/Building/Building.php @@ -3,6 +3,7 @@ class Building extends mfBaseModel { protected $forcestr = ['street','zip','phone','email','note']; + private $in_after_save; private $network; private $networksection; private $pop; @@ -29,7 +30,12 @@ class Building extends mfBaseModel { } protected function afterSave() { + if($this->in_after_save) return true; + $this->in_after_save++; + $this->resetProperties(); + + $this->in_after_save--; } public function resetProperties() { @@ -43,6 +49,18 @@ class Building extends mfBaseModel { $this->files = null; } + public function updateUnitCount() { + if(!$this->id) return true; + + $unit_count = TerminationModel::count(["building_id" => $this->id]); + if($this->units != $unit_count) { + $this->units = $unit_count; + $this->save(); + } + + return true; + } + public function getNewObjectCode() { if(!$this->zip) { return false; diff --git a/application/Termination/Termination.php b/application/Termination/Termination.php index 2e5f7d178..8f19c38e6 100644 --- a/application/Termination/Termination.php +++ b/application/Termination/Termination.php @@ -3,6 +3,7 @@ class Termination extends mfBaseModel { protected $forcestr = ['phone','email','note']; + private $in_after_save = 0; private $building; private $status; private $lineworker; @@ -14,7 +15,16 @@ class Termination extends mfBaseModel { private $creator; private $editor; - + protected function afterSave() { + if($this->in_after_save) return true; + $this->in_after_save++; + + if($this->building_id) { + $this->getProperty("building")->updateUnitCount(); + } + + $this->in_after_save--; + } public function getAddress($singelLine = false) { if(!$this->id) { diff --git a/application/Termination/TerminationController.php b/application/Termination/TerminationController.php index 7b21cf476..c164d3f5d 100644 --- a/application/Termination/TerminationController.php +++ b/application/Termination/TerminationController.php @@ -69,10 +69,10 @@ class TerminationController extends mfBaseController { } // increment Building::units - if(is_numeric($building->units)) { + /*if(is_numeric($building->units)) { ++$building->units; $building->save(); - } + }*/ $this->layout()->setFlash("Anschluss gespeichert.", "success"); $this->redirect("Building", "Index", [], "building=".$term->building_id); @@ -132,10 +132,11 @@ class TerminationController extends mfBaseController { $term->delete(); - if($building->units) { + $building->updateUnitCount(); + /*if($building->units) { --$building->units; $building->save(); - } + }*/ $this->layout()->setFlash("Anschluss gelöscht", "success"); From f81fd2be06675f6aae98a6831b1318014312015f Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Fri, 5 Jan 2024 18:22:17 +0100 Subject: [PATCH 3/3] Finished Address BMD Export --- Layout/default/Address/Form.php | 2 +- Layout/default/Address/Index.php | 1 + Layout/default/Address/bmd_export.csv.php | 2 +- application/Address/AddressController.php | 79 ++++++++++++++++++++++- application/Address/AddressModel.php | 2 +- lib/mvcfronk/mfConfig/mfConfig.php | 11 +++- 6 files changed, 90 insertions(+), 7 deletions(-) diff --git a/Layout/default/Address/Form.php b/Layout/default/Address/Form.php index bff629cb1..73e7e9da6 100644 --- a/Layout/default/Address/Form.php +++ b/Layout/default/Address/Form.php @@ -261,7 +261,7 @@
- Standard: 30 Tage + Standard: Tage
diff --git a/Layout/default/Address/Index.php b/Layout/default/Address/Index.php index d37b6fd14..c2d1376bb 100644 --- a/Layout/default/Address/Index.php +++ b/Layout/default/Address/Index.php @@ -40,6 +40,7 @@ diff --git a/Layout/default/Address/bmd_export.csv.php b/Layout/default/Address/bmd_export.csv.php index b8e3cb6dc..ee1f1ac88 100644 --- a/Layout/default/Address/bmd_export.csv.php +++ b/Layout/default/Address/bmd_export.csv.php @@ -3,7 +3,7 @@ Lieferant;Kunde;Kundenummer;FIBU-Verrechnungsnummer;FIBU Lieferantennummer;Zahlu foreach($addresses as $a): ?> -;;;;;;nl2ws($a["company"])?>;nl2ws($a["firstname"])?>;nl2ws($a["lastname"])?>;nl2ws($a["street"])?>;nl2ws($a["$address->zip"])?>;nl2ws($a["city"])?>;nl2ws($a["country"])?>;nl2ws($a["phone"])?>;nl2ws($a["mobile"])?>;nl2ws($a["email"])?>;nl2ws($a["uid"])?>;;;;;; +;;;;;;nl2ws($a["company"])?>;nl2ws($a["firstname"])?>;nl2ws($a["lastname"])?>;nl2ws($a["street"])?>;nl2ws($a["$address->zip"])?>;nl2ws($a["city"])?>;nl2ws($a["country"])?>;nl2ws($a["phone"])?>;nl2ws($a["mobile"])?>;nl2ws($a["email"])?>;nl2ws($a["uid"])?>;;;;;; layout()->set("request", $this->request); $this->layout()->set("pagination", $pagination); + + $last_export = false; + $bmd_export_ts = new mfConfig("bmd.export.ts"); + if($bmd_export_ts->value()) { + $last_export = $bmd_export_ts->value(); + } + $this->layout()->set("last_bmd_export", $last_export); + return true; @@ -201,13 +209,78 @@ class AddressController extends mfBaseController { $addresses = []; foreach(AddressModel::search($search) as $address) { + // if supplier -> use it + if($address->fibu_supplier_number) { + $addresses[$address->fibu_supplier_number] = $address; + continue; + } + + // if is primary -> use it + if($address->fibu_account_number && $address->fibu_primary_account) { + $addresses[$address->fibu_account_number] = $address; + continue; + } + + // if only address with fibu_account_num -> make primary + if($address->fibu_account_number && !$address->fibu_primary_account) { + // look up other addresses with same account num + $address_count = AddressModel::count(["fibu_account_number" => $address->fibu_account_number]); + if($address_count === 1) { + $address->fibu_primary_account = 1; + $address->save(); + $addresses[$address->fibu_account_number] = $address; + continue; + } + + // if more addresses with fibu_account_num -> find primary + if($address_count > 1) { + // find primary + $primary = AddressModel::getFirst(["fibu_account_number" => $address->fibu_account_number, "fibu_primary_account" => true]); + if($primary) { + // use single primary + $addresses[$primary->fibu_account_number] = $primary; + continue; + } else { + // if no primary -> make last one primary + $new_primary = false; + foreach(AddressModel::search(["fibu_account_number" => $address->fibu_account_number]) as $primary) { + $new_primary = $primary; + } + if(!$new_primary) { + var_dump($address);exit; + } + $new_primary->fibu_primary_account = 1; + $new_primary->save(); + $addresses[$new_primary->fibu_account_number] = $new_primary; + continue; + } + } + var_dump($address);exit; + } + + // if no fibu account number but customer number -> create fibu account number + if($address->customer_number && !$address->fibu_account_number) { + // Address::afterSave() generates new fibu account number + $address->save(); + if(!$address->fibu_account_number) { + var_dump($address);exit; + } + $addresses[$address->fibu_account_number] = $address; + continue; + } + + } + + $export_addresses = []; + + foreach($addresses as $address) { $a = []; $a["is_supplier"] = (array_key_exists("supplier", $address->types) && $address->types['supplier']) ? "1" : "0"; $a["is_customer"] = "1"; $a["customer_number"] = $address->customer_number; $a["fibu_account_number"] = $address->fibu_account_number; $a["fibu_supplier_number"] = $address->fibu_supplier_number; - $a["fibu_supplier_due"] = $address->fibu_supplier_due; + $a["fibu_supplier_due"] = (is_numeric($address->fibu_supplier_due)) ? $address->fibu_supplier_due : TT_ADDRESS_DEFAULT_SUPPLIER_DUE; $a["company"] = $address->company; $a["firstname"] = $address->firstname; $a["lastname"] = $address->lastname; @@ -233,7 +306,7 @@ class AddressController extends mfBaseController { $a["bic"] = $address->bank_account_bic; } - $addresses[] = $a; + $export_addresses[] = $a; } @@ -241,7 +314,7 @@ class AddressController extends mfBaseController { $tpl = new Layout(); $tpl->setTemplate("Address/bmd_export.csv"); - $tpl->set("addresses", $addresses); + $tpl->set("addresses", $export_addresses); $csv_content = $tpl->render(); $filename = "thetool_address_export_".$type."_".date("Y-m-d-H-i-s").".csv"; diff --git a/application/Address/AddressModel.php b/application/Address/AddressModel.php index e6ca33522..4bc868620 100644 --- a/application/Address/AddressModel.php +++ b/application/Address/AddressModel.php @@ -166,7 +166,7 @@ class AddressModel { $res = $db->query($sql); if($db->num_rows($res)) { $data = $db->fetch_object($res); - return $data->cnt; + return (int)$data->cnt; } return 0; } diff --git a/lib/mvcfronk/mfConfig/mfConfig.php b/lib/mvcfronk/mfConfig/mfConfig.php index fcbc8c7d2..d0bc2250a 100644 --- a/lib/mvcfronk/mfConfig/mfConfig.php +++ b/lib/mvcfronk/mfConfig/mfConfig.php @@ -49,7 +49,16 @@ class mfConfig { if($value !== null) { $this->value = $value; } - return $this->value; + $return_value = $this->value; + + if($this->type == "int") { + $return_value = (int)$return_value; + } + if($this->type == "float") { + $return_value = (float)$return_value; + } + + return $return_value; } public function type($type = null) {