From 40b1ea860530a20df8324fd59ebb462097966f19 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Mon, 8 Jul 2024 20:09:47 +0200 Subject: [PATCH] Added Sepa Mandats stuff --- application/Address/AddressModel.php | 3 ++ application/Billing/BillingController.php | 34 ++++++++++---- application/Billing/BillingModel.php | 3 ++ application/Invoice/InvoiceController.php | 3 ++ application/Invoice/InvoiceModel.php | 3 ++ ...08170212_address_add_last_invoice_date.php | 31 +++++++++++++ .../20240708175301_billing_add_sepa.php | 43 ++++++++++++++++++ scripts/contract/generate-sepa-data.php | 45 +++++++++++++++++++ scripts/import-sepa-date-from-ivt.php | 28 ++++++++---- 9 files changed, 176 insertions(+), 17 deletions(-) create mode 100644 db/migrations/20240708170212_address_add_last_invoice_date.php create mode 100644 db/migrations/20240708175301_billing_add_sepa.php create mode 100644 scripts/contract/generate-sepa-data.php diff --git a/application/Address/AddressModel.php b/application/Address/AddressModel.php index 73189b104..cd8b2053c 100644 --- a/application/Address/AddressModel.php +++ b/application/Address/AddressModel.php @@ -30,6 +30,7 @@ class AddressModel { public $bank_account_iban; public $bank_account_bic; public $sepa_date; + public $last_invoice_date; public $allow_contact; public $allow_spin; @@ -270,6 +271,8 @@ class AddressModel { if ($fan) { if (is_numeric($fan)) { $where .= " AND fibu_account_number=$fan"; + } elseif($fan === true) { + $where .= " AND (fibu_account_number IS NOT NULL AND fibu_account_number > 0)"; } else { $fan = FronkDB::singleton()->escape($fan); $where .= " AND fibu_account_number LIKE '$fan'"; diff --git a/application/Billing/BillingController.php b/application/Billing/BillingController.php index 1275ae703..a83028959 100644 --- a/application/Billing/BillingController.php +++ b/application/Billing/BillingController.php @@ -95,7 +95,7 @@ class BillingController extends mfBaseController { $r = $this->request; $last_run_ts = new mfConfig("voicecallhistory.contact-job.ts"); - if($last_run_ts->value() < date("U") - 43200) { + if($last_run_ts->value() < date("U") - 86400) { $this->layout()->setFlash("Voicecall History Contract Job ist heute noch nicht gelaufen", "error"); $this->redirect("Billing"); } @@ -370,6 +370,11 @@ class BillingController extends mfBaseController { $billing_delivery = $billingaddress->billing_delivery; } + $fibu_account_num = $contract->billingaddress->fibu_account_number; + if(!$fibu_account_num) { + die("Keine Fibu Account Nummer in Rechnungskontakt in Contract ID ".$contract->id); + } + $data = []; $data["contract_id"] = $contract->id; $data["start_date"] = $start_date->format("Y-m-d"); @@ -377,7 +382,7 @@ class BillingController extends mfBaseController { $data["owner_id"] = $contract->owner_id; $data["billingaddress_id"] = ($contract->billingaddress_id) ? $contract->billingaddress_id : $contract->owner_id; $data["customer_number"] = $contract->owner->customer_number; - $data["fibu_account_number"] = $contract->owner->fibu_account_number; // TODO: fibu_account_number von billingaddress + $data["fibu_account_number"] = $fibu_account_num; $data["company"] = $billingaddress->company; $data["firstname"] = $billingaddress->firstname; $data["lastname"] = $billingaddress->lastname; @@ -400,12 +405,7 @@ class BillingController extends mfBaseController { $data["price_setup"] = $price_setup; $data["billing_period"] = $contract->billing_period; - $matchcode = $contract->matchcode; - // if voice product and matchcode consists of phonenumbers only, remove matchcode - if(array_key_exists("needs_number", $contract->product->attributes) && $contract->product->attributes["needs_number"] == 1 && preg_match('/^[0-9, ]+$]/', $matchcode)) { - $matchcode = ""; - } - $data["matchcode"] = $matchcode; + $data["matchcode"] = $contract->matchcode; if(!$contract->billingaddress->country_id) { $billcountry = CountryModel::getFirst(["isocode" => TT_HOMECOUNTRY_ISOCODE]); @@ -426,6 +426,24 @@ class BillingController extends mfBaseController { $data["vatgroup_id"] = $contract->vatgroup_id; $data["vatarea"] = $vatarea; + if($billing_type == "sepa") { + if($contract->billingaddress->sepa_date) { + $sepa_date = new DateTime("@".$contract->billingaddress->sepa_date); + $sepa_date->setTimezone(new DateTimeZone("Europe/Vienna")); + $data["sepa_date"] = $sepa_date->format("Y-m-d"); + } + + if($contract->billingaddress->last_invoice_date) { + $sepa_last_date = new DateTime("@".$contract->billingaddress->last_invoice_date); + $sepa_last_date->setTimezone(new DateTimeZone("Europe/Vienna")); + $data["sepa_last_date"] = $sepa_last_date->format("Y-m-d"); + } + + $data["sepa_id"] = "R".$fibu_account_num; + + } + + $billing = BillingModel::create($data); if (!$billing->save()) { diff --git a/application/Billing/BillingModel.php b/application/Billing/BillingModel.php index dc3d1745c..f1d164438 100644 --- a/application/Billing/BillingModel.php +++ b/application/Billing/BillingModel.php @@ -9,6 +9,9 @@ class BillingModel { public $billingaddress_id; public $customer_number; public $fibu_account_number; + public $sepa_date; + public $sepa_id; + public $sepa_last_date; public $company; public $firstname; public $lastname; diff --git a/application/Invoice/InvoiceController.php b/application/Invoice/InvoiceController.php index 9b3081657..5cb1b79bf 100644 --- a/application/Invoice/InvoiceController.php +++ b/application/Invoice/InvoiceController.php @@ -382,6 +382,9 @@ XINON GmbH"; $invoice_data["billingaddress_id"] = $billingaddress_id; $invoice_data["customer_number"] = $bill->customer_number; $invoice_data["fibu_account_number"] = $bill->fibu_account_number; + $invoice_data["sepa_date"] = $bill->sepa_date; + $invoice_data["sepa_id"] = $bill->sepa_id; + $invoice_data["sepa_last_date"] = $bill->sepa_last_date; $invoice_data["company"] = $bill->company; $invoice_data["firstname"] = $bill->firstname; diff --git a/application/Invoice/InvoiceModel.php b/application/Invoice/InvoiceModel.php index c68f6becd..2753f7c1c 100644 --- a/application/Invoice/InvoiceModel.php +++ b/application/Invoice/InvoiceModel.php @@ -7,6 +7,9 @@ class InvoiceModel { public $billingaddress_id; public $customer_number; public $fibu_account_number; + public $sepa_date; + public $sepa_id; + public $sepa_last_date; public $fibu_cost_area; public $fibu_cost_account; public $fibu_cost_account_legacy; diff --git a/db/migrations/20240708170212_address_add_last_invoice_date.php b/db/migrations/20240708170212_address_add_last_invoice_date.php new file mode 100644 index 000000000..91a43cb8b --- /dev/null +++ b/db/migrations/20240708170212_address_add_last_invoice_date.php @@ -0,0 +1,31 @@ +getEnvironment() == "thetool") { + $table = $this->table('Address'); + $table->addColumn("last_invoice_date", "integer", ["null" => true, "default" => null, "after" => "sepa_date"]); + $table->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $this->table('Address')->removeColumn("last_invoice_date")->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/db/migrations/20240708175301_billing_add_sepa.php b/db/migrations/20240708175301_billing_add_sepa.php new file mode 100644 index 000000000..0b73f0939 --- /dev/null +++ b/db/migrations/20240708175301_billing_add_sepa.php @@ -0,0 +1,43 @@ +getEnvironment() == "thetool") { + $table = $this->table("Billing"); + $table->addColumn("sepa_date", "date", ["null" => true, "default" => null, "after" => "fibu_account_number"]); + $table->addColumn("sepa_id", "string", ["null" => true, "default" => null, "after" => "sepa_date"]); + $table->addColumn("sepa_last_date", "date", ["null" => true, "default" => null, "after" => "sepa_id"]); + $table->update(); + + $invoice = $this->table("Invoice"); + $invoice->addColumn("sepa_date", "date", ["null" => true, "default" => null, "after" => "fibu_account_number"]); + $invoice->addColumn("sepa_id", "string", ["null" => true, "default" => null, "after" => "sepa_date"]); + $invoice->addColumn("sepa_last_date", "date", ["null" => true, "default" => null, "after" => "sepa_id"]); + $invoice->update(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $table = $this->table("Billing"); + $table->removeColumn("sepa_date"); + $table->removeColumn("sepa_id"); + $table->removeColumn("sepa_last_date"); + $table->update(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/scripts/contract/generate-sepa-data.php b/scripts/contract/generate-sepa-data.php new file mode 100644 index 000000000..a282340e8 --- /dev/null +++ b/scripts/contract/generate-sepa-data.php @@ -0,0 +1,45 @@ +#!/usr/bin/php +owner; + $billingaddress = $contract->billingaddress; + + if($billingaddress->last_invcice_date) continue; + if($billingaddress->billing_type != "sepa") continue; + if(!$billingaddress->fibu_account_number) continue; + + if(!$billingaddress->sepa_date) { + $sepa_date = $contract->owner->sepa_date; + if($sepa_date) { + echo $billingaddress->id." sepa date $sepa_date\n"; + $billingaddress->sepa_date = $sepa_date; + $billingaddress->save(); + } + } + + if(!$billingaddress->last_invoice_date) { + $last_bill = IvtBillModel::getLast(["cid" => $owner->customer_number]); + if(!$last_bill) continue; + + $last_date = new DateTime($last_bill->date_outgoing); + $last_invoice_date = $last_date->getTimestamp(); + + echo $billingaddress->id." last_invoice: $last_invoice_date\n"; + $billingaddress->last_invoice_date = $last_invoice_date; + $billingaddress->save(); + } + +} \ No newline at end of file diff --git a/scripts/import-sepa-date-from-ivt.php b/scripts/import-sepa-date-from-ivt.php index e27056c6e..7b0649f4a 100755 --- a/scripts/import-sepa-date-from-ivt.php +++ b/scripts/import-sepa-date-from-ivt.php @@ -16,28 +16,38 @@ define("INTERNAL_USER_ID", $me->id); define("INTERNAL_USER_USERNAME", $me->username); $i = 0; -foreach(AddressModel::search(["customer_number" => true, "billing_type" => "sepa"]) as $address) { - if($address->sepa_date) continue; - +//foreach(AddressModel::search(["customer_number" => true, "billing_type" => "sepa"]) as $address) { +foreach(AddressModel::search(["fibu_account_number" => true, "billing_type" => "sepa"]) as $address) { + //if($address->sepa_date) continue; $bill = IvtBillModel::getFirst(["cid" => $address->customer_number]); if(!$bill) continue; - + if(!trim($bill->IBAN) || !trim($bill->BIC)) continue; - + if(!$bill->date_outgoing) { echo "date outgoing fehlt im ivt: ".$address->customer_number."\n"; continue; } - + $out_date = new DateTime($bill->date_outgoing); $sepa_date = $out_date->getTimestamp(); - echo "$sepa_date - ".$bill->date_outgoing."\n"; + echo "sepa date: $sepa_date - ".$bill->date_outgoing."\n"; if(!$sepa_date){ echo "Konnte Datum nicht umwandeln\n"; } - - $address->sepa_date = $sepa_date; + + $last_bill = IvtBillModel::getLast(["cid" => $address->customer_number]); + if(!$last_bill) continue; + $last_date = new DateTime($last_bill->date_outgoing); + $last_invoice_date = $last_date->getTimestamp(); + echo "last invoice date: $last_invoice_date ".$last_bill->date_outgoing."\n"; + + if(!$address->sepa_date) { + $address->sepa_date = $sepa_date; + } + + $address->last_invoice_date = $last_invoice_date; $address->save(); $i++; }