More changes to Billing and Invoice

This commit is contained in:
Frank Schubert
2024-08-07 16:32:18 +02:00
parent 4bc7ef960e
commit 9a0d0791e7
6 changed files with 119 additions and 44 deletions

View File

@@ -54,11 +54,15 @@ class BillingController extends mfBaseController {
// 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);
@@ -155,9 +159,10 @@ class BillingController extends mfBaseController {
$now_month = date("m");
$now_day = date("d");
$now_year = 2024;
$now_month = 7;
$now_day = 10; // XXX for debugging only, must be removed
// for debugging to bill a specific month
//$now_year = 2024;
//$now_month = 7;
//$now_day = 10;
// XXX only for 1st Billing after IVT Import
// Locking to July 2024 and keeping it for now
@@ -488,6 +493,16 @@ class BillingController extends mfBaseController {
$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) {
@@ -553,11 +568,11 @@ class BillingController extends mfBaseController {
}
foreach ($voicenumbers as $voicenumber) {
$vbill = BillingVoicenumberModel::getFirst(["contract_id" => $contract->id, "voicenumber" => $voicenumber->number, "start_date" => $voice_start_date->format("Y-m-d")]);
/*$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 = [];
@@ -567,7 +582,7 @@ class BillingController extends mfBaseController {
do {
$earlier_voice_start_date->modify("-1 month");
// not before first billing in thetool
if($earlier_voice_start_date->format("Y-m-d") < "2024-07-01") break;
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");
@@ -591,11 +606,15 @@ class BillingController extends mfBaseController {
"end_date" => clone $voice_end_date,
];
if(count($call_dates) > 1) {
var_dump($call_dates);exit;
}
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);
@@ -651,8 +670,12 @@ class BillingController extends mfBaseController {
if (!array_key_exists($number, $voicebills)) {
$voicebills[$number] = [];
}
if (!array_key_exists($zone->id, $voicebills[$number])) {
$voicebills[$number][$zone->id] = [
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_name" => $zone->name,
"voiceplan" => $voiceplan->name,
"duration" => 0,
@@ -666,9 +689,9 @@ class BillingController extends mfBaseController {
];
}
$voicebills[$number][$zone->id]["count"]++;
$voicebills[$number][$zone->id]["zone_total"] += $call_price;
$voicebills[$number][$zone->id]["duration"] += $call->duration;
$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;
}
}
}
@@ -676,26 +699,28 @@ class BillingController extends mfBaseController {
// 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"] = $vb["start_date"];
$vbdata["end_date"] = $vb["end_date"];
$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"];
foreach($zones as $zone_id => $zone) {
foreach($zone as $zone_start_date => $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"] = $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!");
$bill_voice = BillingVoicenumberModel::create($vbdata);
if(!$bill_voice->save()) {
var_dump($vbdata);
die("Error saving Billing Voicenumber!");
}
}
}
}