Preorder Billing Marketshare Price + RML layout tweaks

This commit is contained in:
Frank Schubert
2025-03-24 18:37:33 +01:00
parent 97c7a1dde5
commit 40f92c3b7c
11 changed files with 207 additions and 46 deletions
@@ -1,8 +1,8 @@
<?php
class PreorderBillingController extends mfBaseController {
private $billing_minimum_date = "2025-03-01";
private $marketshare = [];
protected function init() : void
{
@@ -133,6 +133,14 @@ class PreorderBillingController extends mfBaseController {
}
protected function importPreorders() {
$netowner_id = $this->me->address_id;
$netowner = new Address($netowner_id);
if(!defined("TT_PREORDER_BILLING") || !is_array(TT_PREORDER_BILLING)) {
die("Config Variable 'TT_PREORDER_BILLING' not found!");
}
$netowner_config = TT_PREORDER_BILLING[$netowner_id];
$earliest_bill_date = new DateTime(PreorderBilling::$earliest_bill_date);
$now_year = date("Y");
@@ -140,7 +148,7 @@ class PreorderBillingController extends mfBaseController {
$now_day = date("d");
$today = new DateTime("$now_year-$now_month-$now_day");
//$today = new DateTime("2025-02-13");
$today = new DateTime("2025-04-13");
$today->setTime(2,0,0);
$today->setTimezone(new DateTimeZone("Europe/Vienna"));
@@ -151,6 +159,18 @@ class PreorderBillingController extends mfBaseController {
$latest_bill_date = clone $bill_date;
$latest_bill_date->modify("last day of this month");
$this_quarter_start = clone $today;
$this_quarter_start->modify("first day of this month");
$q_subtractor = ($this_quarter_start->format("m") - 1) % 3;
$this_quarter_start->modify("- $q_subtractor months");
$this_quarter_end = clone $this_quarter_start;
$this_quarter_end->modify("+ 3 months");
$this_quarter_end->modify("last day of this month");
$latest_quarter_bill_date = clone $this_quarter_start;
$latest_quarter_bill_date->modify("-1 day");
$del = 0;
// first delete all non-invoiced billing records
foreach(PreorderBilling::search(["invoice_id" => null]) as $bill) {
@@ -212,14 +232,22 @@ class PreorderBillingController extends mfBaseController {
die("No netoperator found for preorder ".$preorder->id);
}
if(!is_array($netowner_config["netoperators"]) || !array_key_exists($netoperator->id, $netowner_config["netoperators"])) {
die("netoperator config missing for ".$netoperator->id);
}
$netoperator_config = $netowner_config["netoperators"][$netoperator->id];
$bill_params = [
"netowner" => new Address($this->me->address_id),
"netowner" => $netowner,
"netowner_config" => $netowner_config,
"netoperator" => $netoperator,
"netoperator_config" => $netoperator_config,
"order_date" => $order_date,
"today" => $today,
"bill_date" => $bill_date,
"earliest_bill_date" => $earliest_bill_date,
"latest_bill_date" => $latest_bill_date,
"latest_quarter_bill_date" => $latest_quarter_bill_date
];
if($preorder->status->code >= 241) {
@@ -232,6 +260,8 @@ class PreorderBillingController extends mfBaseController {
}
}
//echo "<pre>".print_r($this->marketshare, true)."</pre>";exit;
$this->Layout()->setFlash("Billing records erstellt", "success");
$this->redirect("PreorderBilling");
@@ -239,11 +269,14 @@ class PreorderBillingController extends mfBaseController {
private function billSetup($preorder, $type, $options) {
$netowner = $options['netowner'];
$netowner_config = $options['netowner_config'];
$netoperator = $options['netoperator'];
$netoperator_config = $options['netoperator_config'];
$order_date = $options['order_date'];
$today = $options['today'];
$bill_date = $options['bill_date'];
$earliest_bill_date = $options['earliest_bill_date'];
$latest_quarter_bill_date = $options["latest_quarter_bill_date"];
$this->log->debug(__METHOD__.": bill $type Preorder ".$preorder->id);
@@ -296,10 +329,6 @@ class PreorderBillingController extends mfBaseController {
$status_change_date = new DateTime("@".$preorder->create);
}
if($preorder->oaid == 'AT-8943-8392e815.001') {
//var_dump($status_change, $status_change_date);
}
$billing_data = [
"netowner_id" => $netowner->id,
"preorder_id" => $preorder->id,
@@ -373,8 +402,9 @@ class PreorderBillingController extends mfBaseController {
$billing_data["end_date"] = $status_change_date->format("Y-m-d");
}
if($preorder->oaid == 'AT-8943-8392e815.001') {
//var_dump($change_to_active, $status_change_date);exit;
if($netoperator_config["billing-period"] == "quarterly" && $status_change_date->format("Ymd") > $latest_quarter_bill_date->format("Ymd")) {
$this->log->debug(__METHOD__.": Skipping operator_setup for preorder ".$preorder->id." because Billing date ".$status_change_date->format("Y-m-d")." is after latest_quarter_bill_date ".$latest_quarter_bill_date->format("Y-m-d"));
return true;
}
// Netzbetreiber Setup Gebühr
@@ -407,12 +437,30 @@ class PreorderBillingController extends mfBaseController {
private function billOperatorPeriodic($preorder, $options) {
$netowner = $options['netowner'];
$netowner_config = $options['netowner_config'];
$netoperator = $options['netoperator'];
$netoperator_config = $options['netoperator_config'];
$order_date = $options['order_date'];
$today = $options['today'];
$bill_date = $options['bill_date'];
$earliest_bill_date = $options['earliest_bill_date'];
$latest_bill_date = $options['latest_bill_date'];
$latest_quarter_bill_date = $options["latest_quarter_bill_date"];
$campaign = new PreorderCampaign($preorder->preordercampaign_id);
if(!$campaign) {
die("Campaign ".$preorder->preordercampaign_id." not found!");
}
if(!array_key_exists($campaign->id, $this->marketshare)) {
$this->marketshare[$campaign->id] = [];
$this->marketshare[$campaign->id]["max"] = $campaign->getUnitCount();
$this->marketshare[$campaign->id]["netops"] = [];
}
if(!array_key_exists($netoperator->id, $this->marketshare[$campaign->id]["netops"])) {
$this->marketshare[$campaign->id]["netops"][$netoperator->id] = [];
$this->marketshare[$campaign->id]["netops"][$netoperator->id]["bracket_price"] = [];
}
if($preorder->status->code >= 899) {
$this->log->debug(__METHOD__.": Preorder ".$preorder->id." / ".$preorder->oaid." is cancelled");
@@ -490,6 +538,10 @@ class PreorderBillingController extends mfBaseController {
$existing_bill = PreorderBilling::getFirst(["product_id" => $product->id, "preorder_id" => $preorder->id, "start_date" => $create_date->format("Y-m-d")]);
//var_dump($existing_bill);
if(!$existing_bill) {
if($netoperator_config["billing-period"] == "quarterly" && $create_date->format("Ymd") > $latest_quarter_bill_date->format("Ymd")) {
$this->log->debug(__METHOD__.": Skipping operator_usage ".$create_date->format("m/Y")." for preorder ".$preorder->id." because Billing date ".$create_date->format("Y-m-d")." is after latest_quarter_bill_date ".$latest_quarter_bill_date->format("Y-m-d"));
return true;
}
$new_create_date = clone $create_date;
$to_bill_dates[] = $new_create_date;
$create_date->modify("-1 months");
@@ -511,9 +563,44 @@ class PreorderBillingController extends mfBaseController {
$sday = $start_date->format("d");
$eday = $end_date->format("d");
$bill_price = $price->price_inet;
$price_id = $price->id;
$base_price = $price->price_inet;
if ($price->price_inet && ($sday > 1)) {
if($netoperator_config["marketshare-discount"]) {
// get current percentage of billed homes
$ms_bill_month = $start_date->format("Ym");
if(!array_key_exists("billed-$ms_bill_month", $this->marketshare[$campaign->id]["netops"][$netoperator->id])) {
$this->marketshare[$campaign->id]["netops"][$netoperator->id]["billed-$ms_bill_month"] = 0;
}
$max_homes = $this->marketshare[$campaign->id]["max"];
$billed_homes = $this->marketshare[$campaign->id]["netops"][$netoperator->id]["billed-$ms_bill_month"];
if($billed_homes) {
$billed_pct = (100 * $billed_homes) / $max_homes;
foreach([15,20,25,30,35,40,45,50] as $bracket) {
if ($billed_pct >= $bracket) {
if(array_key_exists("$bracket-$price_id", $this->marketshare[$campaign->id]["netops"][$netoperator->id]["bracket_price"])) {
$base_price = $this->marketshare[$campaign->id]["netops"][$netoperator->id]["bracket_price"]["$bracket-$price_id"]->price_inet;
} else {
$bracket_price = $price->getMarketshareBracket($bracket);
if ($bracket_price) {
$this->marketshare[$campaign->id]["netops"][$netoperator->id]["bracket_price"]["$bracket-$price_id"] = $bracket_price;
$base_price = $bracket_price->price_inet;
}
}
}
}
}
$this->log->debug(__METHOD__.": ".$netoperator->getCompanyOrName()." (month: $ms_bill_month; campaign: ".$campaign->name.") Marketshare Price: $base_price (max: $max_homes | billed: $billed_homes | billed% $billed_pct ");
}
$bill_price = $base_price;
if ($base_price && ($sday > 1)) {
// Aliquoten Preis errechnen
$first_of_period = clone $start_date;
$first_of_period->modify("first day of this month");
@@ -527,7 +614,7 @@ class PreorderBillingController extends mfBaseController {
if ($period_days < 0) return true; // don't bill for negative time range
$pc = $period_days / $total_days * 100;
$bill_price = round($price->price_inet / 100 * $pc, 2);
$bill_price = round($base_price / 100 * $pc, 2);
}
$article_number = $product->article_number;
@@ -573,8 +660,16 @@ class PreorderBillingController extends mfBaseController {
if (!$billing->save()) {
die("Billing record could not be saved!");
}
$ms_bill_month = $start_date->format("Ym");
$this->marketshare[$campaign->id]["netops"][$netoperator->id]["billed-$ms_bill_month"]++;
//$this->marketshare[$campaign->id]["netops"][$netoperator->id]["billed-$ms_bill_month"]
}
//var_dump($this->marketshare);exit;
//var_dump($billing);
}
}