WIP Voicenumber Billing 2024-07-05
This commit is contained in:
@@ -101,6 +101,7 @@ class BillingController extends mfBaseController {
|
||||
//$tomorrow->setTime(0,0,0);
|
||||
|
||||
$i = 0;
|
||||
$v = 0;
|
||||
|
||||
//$yearly_not_before = new DateTime("2023-06-01");
|
||||
|
||||
@@ -160,10 +161,6 @@ class BillingController extends mfBaseController {
|
||||
$this->log->debug(__METHOD__.": Ignoring Contract ".$contract->id." because billing_period == 0");
|
||||
continue;
|
||||
}*/
|
||||
if($contract->price == 0 && $contract->price_setup == 0) {
|
||||
$this->log->debug(__METHOD__.": Ignoring Contract ".$contract->id." because price and price_setup == 0");
|
||||
continue;
|
||||
}
|
||||
|
||||
$cancel_date = false;
|
||||
if($contract->cancel_date) {
|
||||
@@ -295,6 +292,10 @@ class BillingController extends mfBaseController {
|
||||
}
|
||||
|
||||
|
||||
/*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");
|
||||
@@ -369,7 +370,6 @@ class BillingController extends mfBaseController {
|
||||
$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["matchcode"] = $contract->matchcode;
|
||||
$data["product_id"] = $contract->product_id;
|
||||
$data["product_name"] = $contract->product_name;
|
||||
$data["product_info"] = $contract->product_info;
|
||||
@@ -378,6 +378,13 @@ class BillingController extends mfBaseController {
|
||||
$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 {
|
||||
@@ -405,10 +412,170 @@ class BillingController extends mfBaseController {
|
||||
}
|
||||
|
||||
$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 (array_key_exists($dest_nummer, $destinations_cache)) {
|
||||
$destination = $destinations_cache[$dest_nummer];
|
||||
} else {
|
||||
$destination = $voiceplan->getDestinationByNumber($dest_nummer);
|
||||
$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;
|
||||
}
|
||||
if(!count($voicebills)) {
|
||||
continue;
|
||||
}
|
||||
//var_dump($voicebills);exit;
|
||||
|
||||
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++;
|
||||
|
||||
// save to BillingVoicenumber
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*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 Billing records generiert");
|
||||
$this->layout()->setFlash("$i Contract Billing records generiert. $v Voicenumber Billing records generiert");
|
||||
$this->redirect("Billing");
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user