Files
thetool/application/Billing/BillingController.php
2024-07-07 12:04:51 +02:00

583 lines
26 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);
}
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("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;
$today = new DateTime("now");
$today->setTime(0,0,0);
//$tomorrow = new DateTime("tomorrow");
//$tomorrow->setTime(0,0,0);
$i = 0;
$v = 0;
//$yearly_not_before = new DateTime("2023-06-01");
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
//$now_year = 2024;
//$now_month = 6;
//$now_day = 3; // XXX for debugging only, must be removed
// XXX only for 1st Billing after IVT Import
//$yearly_not_before = new DateTime("$now_year-$now_month-01");
$yearly_not_before = new DateTime("$now_year-06-01");
$del = 0;
// first delete all non-invoiced billing records
/*foreach(BillingModel::search(["invoice_id" => null]) as $bill) {
$bill->delete();
$del++;
}*/
$this->log->notice(__METHOD__.": $del Billing records deleted");
//$stop = false;
foreach(ContractModel::search(["finish_date<" => mktime(0,1,0,$now_month, $now_day, $now_year), "cancel_date" => null]) as $contract) {
//while(!$stop) {
//$stop = true;
//$contract = new Contract(1475);
//var_dump($contract);exit;
$bill_month = $now_month;
$bill_year = $now_year;
//$bill_day = $now_day;
$bill_date = new DateTime("$bill_year-$bill_month-01");
//echo $bill_date->format("Y-m-d H:i:s")."<br>";
$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->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);
while($create_date->format("Y-m-d") >= $earliest_bill_date->format("Y-m-d")) {
if($last_create_date) {
// just for safety / shouldn't happen
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);
}
$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 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)) {
// aliquoter preis
$days = ($eday - $sday) + 1;
$first_of_period = clone $start_date;
$first_of_period->modify("first day of this month");
$total_days = $end_date->diff($first_of_period)->format("%a") + 1;
$period_days = ($end_date->diff($start_date)->format("%a")) + 1;
$pc = $period_days / $total_days * 100;
$price = round($contract->price / 100 * $pc, 4);
/*if($contract->id == 8766) {
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;
}
$owner = $contract->owner;
$billingaddress = $contract->billingaddress;
$billing_type = "invoice";
$billing_delivery = "paper";
if ($owner->billing_type) {
$billing_type = $owner->billing_type;
}
if ($owner->billing_delivery) {
$billing_delivery = $owner->billing_delivery;
}
if ($billingaddress->billing_type) {
$billing_type = $billingaddress->billing_type;
}
if ($billingaddress->billing_delivery) {
$billing_delivery = $billingaddress->billing_delivery;
}
$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"] = $contract->owner->fibu_account_number;
$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"] = $billingaddress->bank_account_bank;
$data["bank_account_owner"] = $billingaddress->bank_account_owner;
$data["bank_account_iban"] = $billingaddress->bank_account_iban;
$data["bank_account_bic"] = $billingaddress->bank_account_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;
$matchcode = $contract->matchcode;
// if voice product and matchcode consists oh 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;
if(!$contract->billingaddress->country_id) {
$billcountry = CountryModel::getFirst(["isocode" => TT_HOMECOUNTRY_ISOCODE]);
} else {
$billcountry = $contract->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";
}
$data["country"] = $billcountry->name;
$data["vatrate"] = $vatgroup->rates[$vatarea]->rate;
$data["vatgroup_id"] = $contract->vatgroup_id;
$data["vatarea"] = $vatarea;
$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);
$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
}
$calls = VoiceCallHistoryModel::getVoiceCallHistoryAsEntity(["contract_id" => $contract->id, "start" => ["from" => $voice_start_date->getTimestamp(), "to" => $voice_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;
//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($zone->id, $voicebills[$number])) {
$voicebills[$number][$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
];
}
$voicebills[$number][$zone->id]["count"]++;
$voicebills[$number][$zone->id]["zone_total"] += $call_price;
$voicebills[$number][$zone->id]["duration"] += $call->duration;
}
}
// save to BillingVoicenumber
foreach($voicebills as $vbnumber => $zones) {
foreach($zones as $zone_id => $vb) {
$vbdata = [];
$vbdata["billing_id"] = $billing->id;
$vbdata["contract_id"] = $contract->id;
$vbdata["voicenumber"] = $vbnumber;
$vbdata["start_date"] = $voice_start_date->format("Y-m-d");
$vbdata["end_date"] = $voice_end_date->format("Y-m-d");
$vbdata["voiceplan"] = $vb["voiceplan"];
$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);
if(!$bill_voice->save()) {
var_dump($vbdata);
die("Error saving Billing Voicenumber!");
}
}
}
$v++;
//var_dump($voicebills);exit;
}
/*foreach(VoiceCallHistoryModel::getVoiceCallHistoryAsEntity(["contract_id" => $contract->id, "start" => ["from" => $start_date->getTimestamp()]]) as $call) {
// find BillingVoicenumber record for this call
$vbill = BillingVoicenumberModel::getFirst(["contract_id" => $contract->id, "voicenumber" => $call->voice_account, "start_date" => ]);
if($vbill) {
}
}*/
}
}
$this->layout()->setFlash("$i Contract Billing records generiert. $v Voicenumber Billing records generiert");
$this->redirect("Billing");
}
}