Files
thetool/application/Billing/BillingController.php

985 lines
45 KiB
PHP

<?php
class BillingController extends mfBaseController {
protected function init()
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$this->layout()->setTemplate("Billing/Index");
if ($this->request->resetFilter) {
unset($_SESSION[MFAPPNAME . '-Billing-filter']);
}
$filter = [];
if (is_array($this->request->filter)) {
$filter = $this->request->filter;
$_SESSION[MFAPPNAME . '-Billing-filter'] = $filter;
} else {
if (array_key_exists(MFAPPNAME . '-Billing-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-Billing-filter'])) {
$filter = $_SESSION[MFAPPNAME . '-Billing-filter'];
}
}
$this->layout->set("filter", $filter);
$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 50;
$pagination['maxItems'] = 0;
if (is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
//var_dump($filter);exit;
$pagination['maxItems'] = BillingModel::count($filter);
$billings = BillingModel::search($filter, $pagination);
$this->layout()->set("billings", $billings);
$this->layout()->set("pagination", $pagination);
// summen berechnen
$sum_price = BillingModel::getSumPrice($filter);
$sum_price_setup = BillingModel::getSumPriceSetup($filter);
$sum_price_sepa = BillingModel::getSumPrice(array_merge($filter, ["billing_type" => "sepa"])) + BillingModel::getSumPriceSetup(array_merge($filter, ["billing_type" => "sepa"]));
$sum_credit_price = BillingModel::getSumCreditPrice($filter);
$sum_credit_price_setup = BillingModel::getSumCreditPriceSetup($filter);
$this->layout()->set("sum_price", $sum_price);
$this->layout()->set("sum_price_setup", $sum_price_setup);
$this->layout()->set("sum_price_sepa", $sum_price_sepa);
$this->layout()->set("sum_credit_price", $sum_credit_price);
$this->layout()->set("sum_credit_price_setup", $sum_credit_price_setup);
}
private function getPreparedFilter($filter)
{
$new_filter = [];
if (array_key_exists("show_credit", $filter)) {
if ($filter["show_credit"] == 0) {
$new_filter["price>="] = 0;
}
unset($filter["show_credit"]);
} else {
$new_filter["price>="] = 0;
}
if(array_key_exists("status", $filter)) {
if($filter["status"] == "billed") {
$new_filter["invoice_id"] = true;
} else {
$new_filter["invoice_id"] = null;
}
} else {
$new_filter["invoice_id"] = null;
}
if(array_key_exists("start_date_from", $filter)) {
if($filter["start_date_from"]) {
try {
$from = DateTime::createFromFormat("d.m.Y", $filter["start_date_from"]);
} catch (Exception $e) {}
$new_filter["start_date>="] = $from->format("Y-m-d");
}
unset($filter["start_date_from"]);
}
if(array_key_exists("start_date_to", $filter)) {
if($filter["start_date_to"]) {
try {
$to = DateTime::createFromFormat("d.m.Y", $filter["start_date_to"]);
} catch (Exception $e) {}
$new_filter["start_date<="] = $to->format("Y-m-d");
}
unset($filter["start_date_to"]);
}
if(array_key_exists("customer", $filter)) {
if(array_key_exists("customer", $filter) && $filter["customer"]) {
$kunde = $this->db()->escape($filter['customer']);
if(!array_key_exists("add-where", $new_filter)) $new_filter["add-where"] = "";
$new_filter['add-where'] .= " AND (company like '%$kunde%' OR firstname like '%$kunde%' OR lastname like '%$kunde%' OR concat(firstname, ' ', lastname) like '%$kunde%' OR concat(lastname, ' ', firstname) like '%$kunde%')";
}
}
if(array_key_exists("address", $filter)) {
if(array_key_exists("address", $filter) && $filter["address"]) {
$search = $this->db()->escape($filter['address']);
if(!array_key_exists("add-where", $new_filter)) $new_filter["add-where"] = "";
$new_filter['add-where'] .= " AND (street like '%$search%' OR zip like '%$search%' OR city like '%$search%' OR country like '%$search%')";
}
}
if (is_array($filter) && count($filter)) {
foreach ($filter as $name => $value) {
$new_filter[$name] = $value;
}
}
return $new_filter;
}
protected function importContractsAction() {
$r = $this->request;
/*$last_run_ts = new mfConfig("voicecallhistory.contact-job.ts");
if($last_run_ts->value() < date("U") - 86400) {
$this->layout()->setFlash("Voicecall History Contract Job ist heute noch nicht gelaufen", "error");
$this->redirect("Billing");
}*/
$i = 0;
$v = 0;
$today = new DateTime("now");
$today->setTime(0,0,0);
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
// for debugging to bill a specific month
//$now_year = 2024;
//$now_month = 11;
//$now_day = 3;
$locked = new mfConfig("billing.running");
if($locked->value()) {
$this->layout()->setFlash("Läuft schon seit ".date("d.m.Y H:i", $locked->edit));
$this->redirect("Billing");
}
$locked->value(1);
$locked->save();
// XXX only for 1st Billing after IVT Import
// Locking to July 2024 and keeping it for now
$yearly_not_before = new DateTime("2024-07-01");
$del = 0;
// first delete all non-invoiced billing records
foreach(BillingModel::search(["invoice_id" => null]) as $bill) {
foreach(BillingVoicenumberModel::search(["billing_id" => $bill->id]) as $billingVoicenumber) {
$billingVoicenumber->delete();
}
$bill->delete();
$del++;
}
$this->log->notice(__METHOD__.": $del Billing records deleted");
$contract_search = [
"finish_date<" => mktime(2,0,0,$now_month, $now_day, $now_year),
"cancel_date_null_or_gte" => mktime(0,0,0,$now_month, 1, $now_year),
//"owner_id" => 7719
];
foreach(ContractModel::search($contract_search) as $contract) {
$bill_month = $now_month;
$bill_year = $now_year;
//$bill_day = $now_day;
$bill_date = new DateTime("$bill_year-$bill_month-01");
$monthly_bill_period_to = clone($bill_date);
$monthly_bill_period_to->modify("last day of this month");
$contract_finish_date = new DateTime("@".$contract->finish_date);
$contract_finish_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$contract_finish_date->setTime(0,0,0);
if($contract->billing_delay) {
// add billing delay to finish_date so the first x months won't be billed
$this->log->debug(__METHOD__.": Adding ".$contract->billing_delay." billing_delay months to finish_date");
$contract_finish_date->modify("+".$contract->billing_delay." months");
}
$finish_year = $contract_finish_date->format("Y");
$finish_month = $contract_finish_date->format("m");
$finish_day = $contract_finish_date->format("d");
if($contract_finish_date > $monthly_bill_period_to) {
$this->log->debug(__METHOD__.": Ignoring Contract ".$contract->id." because finish_date is in $finish_month $finish_year");
continue;
}
/*if($contract->billing_period < 1) {
$this->log->debug(__METHOD__.": Ignoring Contract ".$contract->id." because billing_period == 0");
continue;
}*/
$cancel_date = false;
if($contract->cancel_date) {
$cancel_date = new DateTime("@".$contract->cancel_date);
$cancel_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$cancel_date->setTime(0,0,0);
if($cancel_date->format("Y") != $now_year || $cancel_date->format("m") != $now_month) {
$cancel_date = false;
}
}
//$start_date = clone $contract_finish_date;
// ignore yearly contracts which are not billable this month
/*if($contract->billing_period == 12) {
if($contract_finish_date->format("m") != $bill_month) {
continue;
}
}*/
$create_bills = [];
// Concurrent Billing
// find not yet billed periods
if(!$contract->billing_period) {
// setup only
if(BillingModel::getFirst(["contract_id" => $contract->id])) {
continue;
}
$create_bills[] = [
"start_date" => $contract_finish_date,
"end_date" => $contract_finish_date,
"price_setup" => $contract->price_setup
];
} else {
// contracts with billing period
$create_dates = [];
//echo "initial bill_date: ".$bill_date->format("Y-m-d")."<br>";
// if more than 1 month period, adjust initial billing_date to contracts finish_date in current period
// otherwise i.e. yearly contracts older than one year would never be billed ever, or in the best case
// they would be billed a few months too late
if($contract->billing_period > 1 && $bill_date > $contract_finish_date) {
$new_bill_date = clone $contract_finish_date;
$new_bill_date->modify("+".$contract->billing_period." months");
while($new_bill_date->format("Ymd") < $bill_date->format("Ymd")) {
$new_bill_date->modify("+".$contract->billing_period." months");
}
if($new_bill_date->format("Ymd") > $bill_date->format("Ymd")) {
$new_bill_date->modify("-".$contract->billing_period." months");
}
$bill_date = $new_bill_date;
//echo "new bill_date: ".$bill_date->format("Y-m-d");
}
$create_date = clone $bill_date;
$create_date->modify("first day of this month");
$create_date->setTime(0,0,0);
$last_create_date = false;
$earliest_bill_date = clone $contract_finish_date;
$earliest_bill_date->modify("first day of this month");
$earliest_bill_date->setTime(0,0,0);
//var_dump($create_date, $earliest_bill_date, $finish_year, $finish_month);exit;
while($create_date->format("Y-m-d") >= $earliest_bill_date->format("Y-m-d")) {
if($last_create_date) {
//var_dump($create_dates);
// just for safety / shouldn't happen
break;
die("need-date ran out of dates");
}
if($create_date->format("Y") == $finish_year && $create_date->format("m") == $finish_month) {
// this is the finish month, so set day back to day of finish_date, unless billing_period is more than 1 month
if($contract->billing_period == 1) {
$create_date->setDate($finish_year, $finish_month, $finish_day);
//var_dump($create_date);
}
$last_create_date = true;
}
$existing_bill = BillingModel::getFirst(["contract_id" => $contract->id, "start_date" => $create_date->format("Y-m-d")]);
if(!$existing_bill) {
$new_create_date = clone $create_date;
$create_dates[] = $new_create_date;
$create_date->modify("-" . $contract->billing_period . " months");
continue;
}
break;
}
// find missing billings
foreach($create_dates as $start_date) {
$price_setup = 0;
if($start_date->format("Y") == $finish_year && $start_date->format("m") == $finish_month) {
$price_setup = $contract->price_setup;
}
$create_bills[] = [
"start_date" => $start_date,
"price_setup" => $price_setup // set Setup price to 0, because it was billed already
];
}
}
$create_bills = array_reverse($create_bills);
//var_dump($create_bills);exit;
foreach($create_bills as $bill_data) {
$start_date = $bill_data["start_date"];
$end_date = (array_key_exists("end_date", $bill_data)) ? $bill_data["end_date"] : null;
$price_setup = $bill_data["price_setup"];
if ($contract->billing_period > 1 && $start_date < $yearly_not_before) {
// XXX only for 1st Billing after IVT Import
$this->log->debug(__METHOD__ . ": Ignoring Contract " . $contract->id . " with start_date " . $start_date->format("Y-m-d") . " because is yearly and before " . $yearly_not_before->format("Y-m-d"));
continue;
}
if ($contract->billing_period == 1 && ($start_date->format("Y") < 2024 || ($start_date->format("Y") == 2024 && $start_date->format("m") < 7))) {
// XXX only for 1st Billing after IVT Import
$this->log->debug(__METHOD__ . ": Ignoring Contract " . $contract->id . " with start_date " . $start_date->format("Y-m-d") . " because is before this month");
continue;
}
// if contract has cancel date this month
// use cancel date as end_date
if ($cancel_date) {
$end_date = clone $cancel_date;
} elseif (!$end_date) {
// else calculate last of month
$end_date = clone $start_date;
$end_date->modify("first day of this month");
$end_date->modify("+" . $contract->billing_period . " months");
$end_date->modify("-1 day");
}
/*if($contract->price != 0 || $contract->price_setup != 0) {
$this->log->debug(__METHOD__.": Ignoring Contract ".$contract->id." because price and price_setup == 0");
continue;
}*/
$sday = $start_date->format("d");
$eday = $end_date->format("d");
if ($contract->price && ($sday > 1 || $cancel_date)) {
// Aliquoten Preis errechnen
$first_of_period = clone $start_date;
$first_of_period->modify("first day of this month");
$last_of_period = clone $start_date;
$last_of_period->modify("last day of this month");
$total_days = $last_of_period->diff($first_of_period)->format("%a") + 1;
$period_days = ($end_date->diff($start_date)->format("%a")) + 1;
if ($period_days < 0) continue; // don't bill for negative time range
$pc = $period_days / $total_days * 100;
$price = round($contract->price / 100 * $pc, 4);
/*
if($contract->id == 12500) {
echo "\n". "<br>";
echo "first day: ".$first_of_period->format("Y-m-d")."\n". "<br>";
echo "total_days: $total_days\n". "<br>";
echo "period_days: $period_days\n". "<br>";
echo "pc: $pc\n". "<br>";
echo "contract price: ".$contract->price."\n". "<br>";
echo "price: $price\n". "<br>";
//exit;
echo "\n". "<br>";
echo "start date: ".$start_date->format("Y-m-d H:i:s") . "<br>";
echo "end date: ".$end_date->format("Y-m-d H:i:s") . "<br><br>";
echo "first_of_period: " . $first_of_period->format("Y-m-d H:i:s") . "<br>";
echo "total days: $total_days<br>";
echo "period days: $period_days<br>";
echo "price: $price<br><br>";
//echo "classic days: $days<br>";
exit;
}*/
} else {
$price = $contract->price;
}
$this->createProductchangeCredit($contract, $start_date);
$owner = $contract->owner;
if ($contract->billingaddress_id) {
$billingaddress = $contract->billingaddress;
} else {
$billingaddress = $owner;
}
$billing_type = "invoice";
$billing_delivery = "paper";
$billing_bank = $billing_owner = $billing_iban = $billing_bic = null;
if ($owner->billing_type) {
$billing_type = $owner->billing_type;
if($billing_type == "sepa") {
$billing_bank = $owner->bank_account_bank;
$billing_owner = $owner->bank_account_owner;
$billing_iban = str_replace(" ", "", $owner->bank_account_iban);
$billing_bic = str_replace(" ", "", $owner->bank_account_bic);
}
}
if ($owner->billing_delivery) {
$billing_delivery = $owner->billing_delivery;
}
if ($billingaddress->billing_type) {
$billing_type = $billingaddress->billing_type;
if($billing_type == "sepa") {
$billing_bank = $billingaddress->bank_account_bank;
$billing_owner = $billingaddress->bank_account_owner;
$billing_iban = str_replace(" ", "", $billingaddress->bank_account_iban);
$billing_bic = str_replace(" ", "", $billingaddress->bank_account_bic);
}
}
if ($billingaddress->billing_delivery) {
$billing_delivery = $billingaddress->billing_delivery;
}
if($contract->vatgroup_id == TT_VATGROUP_CREDIT) {
$fibu_account_num = $billingaddress->fibu_supplier_number;
if(!$fibu_account_num) {
die("Partner " . $billingaddress->customer_number . " hat keine Lieferantennummer (" . $billingaddress->getCompanyOrName().")");
}
} else {
$fibu_account_num = $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");
$data["end_date"] = $end_date->format("Y-m-d");
$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"] = $fibu_account_num;
// TODO: Zahlungsziel / Skonto muss in Zukuft vom Contract übernommen werden
$data["fibu_payment_due"] = $owner->fibu_payment_due ?: TT_ADDRESS_DEFAULT_PAYMENT_DUE;
$data["fibu_payment_skonto"] = $owner->fibu_payment_skonto ?: 0;
$data["fibu_payment_skonto_rate"] = $owner->fibu_payment_skonto_rate ?: 0;
$data["company"] = $billingaddress->company;
$data["firstname"] = $billingaddress->firstname;
$data["lastname"] = $billingaddress->lastname;
$data["street"] = $billingaddress->street;
$data["zip"] = $billingaddress->zip;
$data["city"] = $billingaddress->city;
$data["email"] = $billingaddress->email;
$data["uid"] = $billingaddress->uid;
$data["billing_type"] = $billing_type;
$data["billing_delivery"] = $billing_delivery;
$data["bank_account_bank"] = $billing_bank;
$data["bank_account_owner"] = $billing_owner;
$data["bank_account_iban"] = $billing_iban;
$data["bank_account_bic"] = $billing_bic;
$data["product_id"] = $contract->product_id;
$data["product_name"] = $contract->product_name;
$data["product_info"] = $contract->product_info;
$data["amount"] = $contract->amount;
$data["price"] = $price;
$data["price_setup"] = $price_setup;
$data["billing_period"] = $contract->billing_period;
$data["matchcode"] = $contract->matchcode;
if(!$billingaddress->country_id) {
$billcountry = CountryModel::getFirst(["isocode" => TT_HOMECOUNTRY_ISOCODE]);
} else {
$billcountry = $billingaddress->country;
}
$vatgroup = $contract->vatgroup;
$vatarea = "domestic";
if($billcountry->isocode != TT_HOMECOUNTRY_ISOCODE && $billcountry->is_eu) {
$vatarea = "eu";
} if($billcountry->isocode != TT_HOMECOUNTRY_ISOCODE && !$billcountry->is_eu) {
$vatarea = "other";
}
if(substr(strtolower(preg_replace('/[^a-z0-9]/i', "", $billingaddress->uid)), 0, 3) == "atu") {
$vatarea = "domestic";
}
$data["country"] = $billcountry->name;
$data["vatrate"] = $vatgroup->rates[$vatarea]->rate;
$data["vatgroup_id"] = $contract->vatgroup_id;
$data["vatarea"] = $vatarea;
if($billing_type == "sepa") {
if($billingaddress->sepa_date) {
$sepa_date = new DateTime("@".$billingaddress->sepa_date);
$sepa_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$data["sepa_date"] = $sepa_date->format("Y-m-d");
} else {
if($contract->order_date) {
$order_date = new DateTime("@".$contract->order_date);
} else {
$order_date = new DateTime("now");
}
$data["sepa_date"] = $order_date->format("Y-m-d");
$billingaddress->sepa_date = $order_date->getTimestamp();
$billingaddress->save();
}
if($billingaddress->last_invoice_date) {
$sepa_last_date = new DateTime("@".$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()) {
var_dump($billing);
exit;
}
$i++;
/*
* Create Voice Billing, if contract has voicenumbers
*/
$voicenumbers = VoicenumberModel::search(["contract_id" => $contract->id]);
if(count($voicenumbers)) {
//var_dump($voicenumbers);exit;
$voice_start_date = clone $start_date;
$voice_start_date->modify("-1 month");
$voice_start_date->setTime(0,0,0);
if($voice_start_date->format("Y-m-d") < "2024-06-01") {
continue;
}
$voice_end_date = clone $voice_start_date;
$voice_end_date->modify("first day of this month");
$voice_end_date->modify("+1 months");
$voice_end_date->modify("-1 day");
$voice_end_date->setTime(23,59,59);
$this->log->debug("Voice End Date: ".$voice_end_date->format("Y-m-d H:i:s"));
$earliest_start_date = $start_date;
$voicebills = [];
$zones = [];
$destinations_cache = [];
$voiceplan_id = $contract->getConfigValue("voicenumberblock_voiceplan_id")->int;
if (!$voiceplan_id) {
$this->log->debug(__METHOD__ . ": No voiceplan_id in Contract " . $contract->id. ". Numbers: ".count($voicenumbers));
continue;
}
$voiceplan = new Voiceplan($voiceplan_id);
// always look for whole month
// numbers usually don't change owner without at least a few months being stale
if($voice_start_date->format("d") > 1) {
$voice_start_date->modify("first day of this month");
}
foreach ($voicenumbers as $voicenumber) {
/*$vbill = BillingVoicenumberModel::getFirst(["contract_id" => $contract->id, "voicenumber" => $voicenumber->number, "start_date" => $voice_start_date->format("Y-m-d")]);
if ($vbill) {
//var_dump($vbill);exit;
continue; // number was already billed in this period
}*/
// check for unbilled call records in earlier months
$call_dates = [];
$earlier_voice_start_date = clone $voice_start_date;
//$historic_call_count = 0;
do {
$earlier_voice_start_date->modify("-1 month");
// not before first billing in thetool
if($earlier_voice_start_date->format("Y-m-d") < "2024-06-01") break;
$earlier_voice_end_date = clone $earlier_voice_start_date;
$earlier_voice_end_date->modify("last day of this month");
$earlier_voice_end_date->setTime(23, 59, 59);
$historic_call_count = VoiceCallHistoryModel::countVoiceCallHistory(["contract_id" => $contract->id, "voice_account" => $voicenumber->number, "start" => ["from" => $earlier_voice_start_date->getTimestamp(), "to" => $earlier_voice_end_date->getTimestamp()]]);
//var_dump($historic_call_count);exit;
if ($historic_call_count) {
$call_dates[] = [
"start_date" => clone $earlier_voice_start_date,
"end_date" => clone $earlier_voice_end_date,
];
}
//var_dump($historic_call_count > 0);exit;
} while ($historic_call_count > 0);
$call_dates = array_reverse($call_dates);
$call_dates[] = [
"start_date" => clone $voice_start_date,
"end_date" => clone $voice_end_date,
];
foreach ($call_dates as $call_date) {
$call_date_start = $call_date["start_date"]->format("Y-m-d");
$call_date_end = $call_date["end_date"]->format("Y-m-d");
$test_bill = BillingVoicenumberModel::getFirst(["contract_id" => $contract->id, "voicenumber" => $voicenumber->number, "start_date" => $call_date_start]);
if ($test_bill) {
continue; // number was already billed in this period
}
$calls = VoiceCallHistoryModel::getVoiceCallHistoryAsEntity(["contract_id" => $contract->id, "voice_account" => $voicenumber->number, "start" => ["from" => $call_date["start_date"]->getTimestamp(), "to" => $call_date["end_date"]->getTimestamp()]]);
foreach ($calls as $call) {
//var_dump($call);
$number = $call->voice_account;
$dest_nummer = $call->destination;
if (substr($dest_nummer, 0, 2) == "00") {
$dest_nummer = substr($dest_nummer, 2);
}
if (substr($dest_nummer, 0, 1) == "+") {
$dest_nummer = substr($dest_nummer, 1);
}
if (array_key_exists($dest_nummer, $destinations_cache)) {
$destination = $destinations_cache[$dest_nummer];
} else {
$destination = $voiceplan->getDestinationByNumber($dest_nummer);
if (!$destination) {
die("Destination für Zielrufnummer " . $call->destination . " nicht gefunden");
}
$destinations_cache[$dest_nummer] = $destination;
}
//var_dump($destination);
$zone = $destination->voiceplanzone;
if (!$zone) {
die("Keine Zone für Destination " . $dest_nummer . " gefunden");
}
//var_dump($zone);
// inc_first - first minimumm duration to bill
// inc - subsequent minimum duration to bill
$inc_first = $zone->increment_first;
$inc = $zone->increment;
$billable_duration = $call->duration;
if ($billable_duration <= 0) continue;
// calculate price of first duration unit
// then subtract first minimum duration from duration
$sec_price = $zone->price / 60;
$call_price = $inc_first * $sec_price;
$billable_duration -= $inc_first;
// calculate price of remaining duration and make sure to bill in full duration units
if ($billable_duration > 0) {
$multi = ceil($billable_duration / $inc);
$call_price += ($multi * $inc) * $sec_price;
}
if (!array_key_exists($number, $voicebills)) {
$voicebills[$number] = [];
}
if (!array_key_exists($call_date_start, $voicebills[$number])) {
$voicebills[$number][$call_date_start] = [];
}
if (!array_key_exists($zone->id, $voicebills[$number][$call_date_start])) {
$voicebills[$number][$call_date_start][$zone->id] = [
//"zone_id" => $zone->id,
"zone_name" => $zone->name,
"voiceplan" => $voiceplan->name,
"duration" => 0,
"price" => $sec_price,
"zone_total" => 0,
"increment_first" => $zone->increment_first,
"increment" => $zone->increment,
"count" => 0,
"start_date" => $call_date["start_date"]->format("Y-m-d"),
"end_date" => $call_date["end_date"]->format("Y-m-d"),
];
}
$voicebills[$number][$call_date_start][$zone->id]["count"]++;
$voicebills[$number][$call_date_start][$zone->id]["zone_total"] += $call_price;
$voicebills[$number][$call_date_start][$zone->id]["duration"] += $call->duration;
}
}
}
// save to BillingVoicenumber
foreach($voicebills as $vbnumber => $zones) {
foreach($zones as $zone_start_date => $zone) {
foreach($zone as $zone_id => $vb) {
$vbdata = [];
$vbdata["billing_id"] = $billing->id;
$vbdata["contract_id"] = $contract->id;
$vbdata["voicenumber"] = $vbnumber;
$vbdata["start_date"] = $vb["start_date"];
$vbdata["end_date"] = $vb["end_date"];
$vbdata["voiceplan"] = $vb["voiceplan"];
$vbdata["zone_id"] = $zone_id;
$vbdata["zone"] = $vb["zone_name"];
$vbdata["call_count"] = $vb["count"];
$vbdata["duration"] = $vb["duration"];
$vbdata["price"] = $vb["price"];
$vbdata["price_total"] = $vb["zone_total"];
$vbdata["increment"] = $vb["increment"];
$vbdata["increment_first"] = $vb["increment_first"];
$bill_voice = BillingVoicenumberModel::create($vbdata);
$this->log->debug(__METHOD__.": billingvoicenumber record created: ".print_r($vbdata, true));
if(!$bill_voice->save()) {
var_dump($vbdata);
die("Error saving Billing Voicenumber!");
}
}
}
}
$v++;
//var_dump($voicebills);exit;
}
}
}
$locked->value(0);
$locked->save();
$this->layout()->setFlash("$i Contract Billing records generiert. $v Voicenumber Billing records generiert");
$this->redirect("Billing");
}
private function createProductchangeCredit(Contract $contract, $start_date) {
$today = new DateTime("now");
// find previous contract if upgrade for crediting
$links = ContractLinkModel::search(["contract_id" => $contract->id, "type" => ["upgrade","downgrade","relocation","productchange"]]);
//var_dump($links);
if(!is_array($links) || count($links) !== 1) {
return false;
}
$link = array_shift($links);
//var_dump($link);
if($link->origin_contract_id == $contract->id) {
return false;
}
$origin_contract = $link->origin;
//var_dump($origin_contract);
if(!$origin_contract->cancel_date) {
return false;
}
$origin_cancel_date = new DateTime("@".$origin_contract->cancel_date);
$origin_cancel_date->setTimezone(new DateTimeZone("Europe/Vienna"));
//var_dump($link_cancel_date);
//var_dump($)
// link_cancel_date gleicher monat wie herstellungsdatum
// und
// link_cancel_date kleiner als herstellungsdatum
/*if($contract->id == 12527) {
echo "origin cancel date: ".$origin_cancel_date->format("m.Y")."\n<br/>";
echo "new cancel date: ".$start_date->format("m.Y")."\n<br/>";
exit;
}*/
if($origin_cancel_date->format("m") == $today->format("m") ||
($origin_cancel_date->format("m.Y") != $start_date->format("m.Y") ||
$origin_cancel_date->format("d") > $start_date->format("d"))
) {
return false;
}
$end_of_month = clone($start_date);
$end_of_month->modify("last day of this month");
$first_of_month = clone($start_date);
$first_of_month->modify("first day of this month");
$prev_bill = BillingModel::getFirst(["contract_id" => $origin_contract->id, "start_date>=" => $first_of_month->format("Y-m-d"), "end_date<=" => $end_of_month->format("Y-m-d")]);
//var_dump($prev_bill);
if(!$prev_bill) {
return false;
}
$prev_bill_end_date = new DateTime($prev_bill->end_date);
$prev_end_of_month = clone($prev_bill_end_date);
$prev_end_of_month->modify("Last day of this month");
$prev_cancel_date = new DateTime("@".$origin_contract->cancel_date);
$prev_cancel_date->modify("+1 days");
$prev_cancel_date->setTimezone(new DateTimeZone("Europe/Vienna"));
//var_dump($prev_cancel_date);
$credit_days = ($prev_end_of_month->diff($prev_cancel_date)->format("%a"))+2;
if($credit_days < 1) return false;
$credit_from_date = clone($end_of_month);
$credit_from_date->modify("-$credit_days days");
$credit_total_days = $prev_end_of_month->diff($first_of_month)->format("%a") + 1;
$credit_pc = $credit_days / $credit_total_days * 100;
$credit_price = round($origin_contract->price / 100 * $credit_pc, 4);
$credit_price *= -1;
/*
echo "prev end of month: ".$prev_end_of_month->format("Y-m-d H:i:s")."\n<br />";
echo "credit days: $credit_days\n<br />";
echo "credit pc: $credit_pc\n<br />";
exit;
*/
/*
$period_days = ($end_date->diff($start_date)->format("%a")) + 1;
if ($period_days < 0) continue; // don't bill for negative time range
$pc = $period_days / $total_days * 100;
*/
/*
echo $prev_cancel_date->format("d.m.Y") . " -> " . $prev_bill_end_date->format("d.m.Y") . "<br />\n";
echo "start date: " . $start_date->format("d.m.Y") . "<br />\n";
echo "credit days: " . $credit_days . "<br />\n";
exit;
*/
// create credit bill
$credit_owner = $contract->owner;
if ($contract->billingaddress_id) {
$credit_billingaddress = $origin_contract->billingaddress;
} else {
$credit_billingaddress = $credit_owner;
}
$credit_billing_type = "invoice";
$credit_billing_delivery = "paper";
if ($credit_owner->billing_type) {
$credit_billing_type = $credit_owner->billing_type;
}
if ($credit_owner->billing_delivery) {
$credit_billing_delivery = $credit_owner->billing_delivery;
}
if ($credit_billingaddress->billing_type) {
$credit_billing_type = $credit_billingaddress->billing_type;
}
if ($credit_billingaddress->billing_delivery) {
$credit_billing_delivery = $credit_billingaddress->billing_delivery;
}
if($origin_contract->vatgroup_id == TT_VATGROUP_CREDIT) {
$credit_fibu_account_num = $credit_billingaddress->fibu_supplier_number;
if(!$credit_fibu_account_num) {
die("Partner " . $credit_billingaddress->customer_number . " hat keine Lieferantennummer (" . $credit_billingaddress->getCompanyOrName().")");
}
} else {
$credit_fibu_account_num = $credit_billingaddress->fibu_account_number;
if(!$credit_fibu_account_num) {
die("Keine Fibu Account Nummer in Rechnungskontakt in Contract ID ".$origin_contract->id);
}
}
$product_name = "Gutschrift zu ".$origin_contract->product_name;
$credit_data = [];
$credit_data["contract_id"] = $origin_contract->id;
$credit_data["start_date"] = $prev_cancel_date->format("Y-m-d");
$credit_data["end_date"] = $prev_bill_end_date->format("Y-m-d");
$credit_data["owner_id"] = $origin_contract->owner_id;
$credit_data["billingaddress_id"] = ($origin_contract->billingaddress_id) ? $origin_contract->billingaddress_id : $origin_contract->owner_id;
$credit_data["customer_number"] = $origin_contract->owner->customer_number;
$credit_data["fibu_account_number"] = $credit_fibu_account_num;
$credit_data["company"] = $credit_billingaddress->company;
$credit_data["firstname"] = $credit_billingaddress->firstname;
$credit_data["lastname"] = $credit_billingaddress->lastname;
$credit_data["street"] = $credit_billingaddress->street;
$credit_data["zip"] = $credit_billingaddress->zip;
$credit_data["city"] = $credit_billingaddress->city;
$credit_data["email"] = $credit_billingaddress->email;
$credit_data["uid"] = $credit_billingaddress->uid;
$credit_data["billing_type"] = $credit_billing_type;
$credit_data["billing_delivery"] = $credit_billing_delivery;
$credit_data["bank_account_bank"] = $credit_billingaddress->bank_account_bank;
$credit_data["bank_account_owner"] = $credit_billingaddress->bank_account_owner;
$credit_data["bank_account_iban"] = str_replace(" ", "", $credit_billingaddress->bank_account_iban);
$credit_data["bank_account_bic"] = $credit_billingaddress->bank_account_bic;
$credit_data["product_id"] = $origin_contract->product_id;
$credit_data["product_name"] = $product_name;
$credit_data["product_info"] = $origin_contract->product_info;
$credit_data["amount"] = $origin_contract->amount;
$credit_data["price"] = $credit_price;
$credit_data["price_setup"] = 0;
$credit_data["billing_period"] = $origin_contract->billing_period;
$credit_data["matchcode"] = $origin_contract->matchcode;
if(!$credit_billingaddress->country_id) {
$credit_billcountry = CountryModel::getFirst(["isocode" => TT_HOMECOUNTRY_ISOCODE]);
} else {
$credit_billcountry = $credit_billingaddress->country;
}
$credit_vatgroup = $origin_contract->vatgroup;
$credit_vatarea = "domestic";
if($credit_billcountry->isocode != TT_HOMECOUNTRY_ISOCODE && $credit_billcountry->is_eu) {
$credit_vatarea = "eu";
} if($credit_billcountry->isocode != TT_HOMECOUNTRY_ISOCODE && !$credit_billcountry->is_eu) {
$credit_vatarea = "other";
}
$credit_data["country"] = $credit_billcountry->name;
$credit_data["vatrate"] = $credit_vatgroup->rates[$credit_vatarea]->rate;
$credit_data["vatgroup_id"] = $origin_contract->vatgroup_id;
$credit_data["vatarea"] = $credit_vatarea;
//var_dump($credit_data);exit;
$credit = BillingModel::create($credit_data);
if(!$credit->save()) {
die("Error saving Billing record for Credit of Contract ".$origin_contract->id. " (Created from Upgrade to Contract ".$contract->id.")");
}
$this->log->info("Created Credit for Contract ".$origin_contract->id." (Created from Upgrade to Contract ".$contract->id.")");
return $credit;
}
}