diff --git a/application/Invoice/InvoiceController.php b/application/Invoice/InvoiceController.php index c62d93191..792b28a31 100644 --- a/application/Invoice/InvoiceController.php +++ b/application/Invoice/InvoiceController.php @@ -606,6 +606,7 @@ XINON GmbH"; $sepa_id = ""; $sepa_date = false; $last_invoice_date = false; + $mandatskz = ""; if($is_sepa) { $iban = $invoice->bank_account_iban; @@ -622,6 +623,8 @@ XINON GmbH"; } } + $mandatskz = ($last_invoice_date ? "1" : "0"); + } @@ -642,7 +645,7 @@ XINON GmbH"; $csv_out .= $bic.";"; $csv_out .= $sepa_id.";"; $csv_out .= ($sepa_date ? $sepa_date->format("d.m.Y") : "").";"; - $csv_out .= ($last_invoice_date ? "1" : "0").";"; + $csv_out .= $mandatskz.";"; $csv_out .= ($last_invoice_date ? $last_invoice_date->format("d.m.Y") : "").";"; $csv_out .= ($is_sepa ? 0 : 10).";"; $csv_out .= ($is_sepa ? 1 : 0); diff --git a/scripts/import-sepa-date-from-ivt.php b/scripts/import-sepa-date-from-ivt.php index 100ddbc0b..ad2ced473 100755 --- a/scripts/import-sepa-date-from-ivt.php +++ b/scripts/import-sepa-date-from-ivt.php @@ -22,9 +22,12 @@ foreach(AddressModel::search(["billing_type" => "sepa"]) as $address) { $sepa_date = false; $last_invoice_date = false; + $bill = false; + + if($address->customer_number) { + $bill = IvtBillModel::getFirst(["cid" => $address->customer_number, "iban" => true, "bic" => true]); + } - if(!$address->customer_number) continue; - $bill = IvtBillModel::getFirst(["cid" => $address->customer_number, "iban" => true, "bic" => true]); if($bill) { $out_date = new DateTime($bill->date_outgoing); $sepa_date = $out_date; @@ -32,7 +35,34 @@ foreach(AddressModel::search(["billing_type" => "sepa"]) as $address) { } else { // find order $order = OrderModel::getFirst(["owner_id" => $address->id]); - if($order) { + if(!$order) { + // try finding order via contract + $contract = ContractModel::getFirst(["billingaddress_id" => $address->id]); + if(!$contract) { + echo "Kunde address_id " . $address->id . " nirgendwo gefunden (kein Kundennummer, keine Order, kein Contract\n"; + continue; + } + + $order = OrderModel::getFirst(["owner_id" => $contract->owner_id]); + if(!$order) { + $order = OrderModel::getFirst(["owner_id" => $contract->billingaddress_id]); + } + + if(!$order) { + $bill = IvtBillModel::getFirst(["cid" => $contract->owner->customer_number, "iban" => true, "bic" => true]); + if($bill) { + $out_date = new DateTime($bill->date_outgoing); + $sepa_date = $out_date; + } + } + } + + if(!$sepa_date) { + if(!$order) { + echo "Kunde address_id " . $address->id . " nirgendwo gefunden (kein Kundennummer, keine Order, kein Contract\n"; + continue; + } + if($order->order_date) { $sepa_date = new DateTime("@" . $order->order_date); if($sepa_date->format("Y") < 2020) $sepa_date = false; @@ -42,6 +72,8 @@ foreach(AddressModel::search(["billing_type" => "sepa"]) as $address) { $sepa_date = new DateTime("@" . $order->create); } } + + } if(!$sepa_date) {