#!/usr/bin/php id); define("INTERNAL_USER_USERNAME", $me->username); $i = 0; //foreach(AddressModel::search(["customer_number" => true, "billing_type" => "sepa"]) as $address) { foreach(AddressModel::search(["billing_type" => "sepa"]) as $address) { //if($address->sepa_date && $address->last_invoice_date) continue; $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($bill) { $out_date = new DateTime($bill->date_outgoing); $sepa_date = $out_date; //echo "sepa date: $sepa_date - ".$bill->date_outgoing."\n"; } else { // find order $order = OrderModel::getFirst(["owner_id" => $address->id]); 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; } if(!$sepa_date) { // order create $sepa_date = new DateTime("@" . $order->create); } } } if(!$sepa_date) { echo "Kein sepa date für customer_number ".$address->customer_number."\n"; continue; } $last_bill = IvtBillModel::getLast(["cid" => $address->customer_number, "iban" => true, "bic" => true]); if($last_bill) { $last_date = new DateTime($last_bill->date_outgoing); if($last_date->format("Y-m") == "2024-07") { var_dump($last_date, $last_bill);exit; } $last_invoice_date = $last_date->getTimestamp(); //echo "last invoice date: $last_invoice_date ".$last_bill->date_outgoing."\n"; } else { $last_invoice_date = null; } if(!$address->sepa_date && $sepa_date) { //echo "updating sepa_date ".$address->customer_number."\n"; $address->sepa_date = $sepa_date->getTimestamp(); $address->save(); } if($last_invoice_date) { $address->last_invoice_date = $last_invoice_date; $address->save(); } if($last_invoice_date === null) { $address->last_invoice_date = null; $address->save(); } $i++; } echo "Updated $i addresses\n\n";