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

View File

@@ -46,7 +46,7 @@ $pagination_entity_name = "Rechnungen";
</select>
</div>
<div class="col-1">
<label class="form-label" for="filter_fibu_account_number">Fibu Kontonummer</label>
<label class="form-label" for="filter_fibu_account_number">Kundennummer</label>
<input type="text" class="form-control" name="filter[fibu_account_number]" id="filter_fibu_account_number" value="<?=(array_key_exists("fibu_account_number", $filter)) ? $filter['fibu_account_number'] : ""?>"/>
</div>
@@ -109,6 +109,7 @@ $pagination_entity_name = "Rechnungen";
<th>Rechnungsdatum</th>
<th>Kundennummer</th>
<th>Rechnungsadresse</th>
<th>Text</th>
<th>Netto</th>
<th>Ust.</th>
<th>Brutto</th>
@@ -136,6 +137,7 @@ $pagination_entity_name = "Rechnungen";
<?=$invoice->zip?> <?=$invoice->city?>
<?=($invoice->country != "Österreich") ? "<br >".$invoice->country : ""?>
</td>
<td><?=$invoice->head_text?></td>
<td>€ <?=number_format($invoice->total, 2, ",", ".")?></td>
<td>€ <?=number_format($invoice->total_gross - $invoice->total, 2, ",", ".")?></td>
<td>€ <?=number_format($invoice->total_gross, 2, ",", ".")?></td>

View File

@@ -6,7 +6,7 @@
<style>
body {
position: absolute;
top: 360px;
top: 290px;
width: 100%;
border: 0;
margin: 0;
@@ -102,22 +102,6 @@
<td>{{ customerNumber }}</td>
</tr>
{{ vatHtml }}
<tr>
<th style="padding-top: 8px;">Sachbearbeiter:</th>
<td style="padding-top: 8px;">{{ caseWorker }}</td>
</tr>
<tr>
<th>Telefon:</th>
<td>{{ caseWorkerPhone }}</td>
</tr>
<tr>
<th>Email:</th>
<td>{{ caseWorkerEmail }}</td>
</tr>
<tr>
<th>Unsere Auftragsnummer:</th>
<td>{{ orderNumber }}</td>
</tr>
</table>
</td>
</tr>

View File

@@ -86,12 +86,15 @@ $this->setReturnValue(['filename' => $invoice->invoice_number . ".pdf"]);
<h2><?=($is_credit) ? "Gutschrift" : "Rechnung"?> <?=$invoice->invoice_number?></h2>
<?php if($invoice->head_text):?>
<p><?=$invoice->head_text?></p>
<?php endif; ?>
<table style="border-collapse: collapse; width: 100%;" id="invoiceTable">
<tr style="font-weight: bold; border-bottom: 1px solid black;" class="uneven">
<th style="text-align: left">Pos</th>
<th style="text-align: left">Artikel</th>
<th style="text-align: left">Leistung / Produkt</th>
<th style="text-align: right">Preis</th>
<th style="text-align: right">Menge</th>
<th style="text-align: left">Einheit</th>
<th style="text-align: right">Preis €</th>
@@ -112,7 +115,6 @@ $this->setReturnValue(['filename' => $invoice->invoice_number . ".pdf"]);
<td style="text-align: left"><?=$i+1?></td>
<td style="text-align: left"><?=$p->article_number?></td>
<td style="text-align: left"><?=$p->article_name?></td>
<td style="text-align: right"><?=$price?> €</td>
<td style="text-align: right"><?=$amount?></td>
<td style="text-align: left"><?=$p->unit?></td>
<td style="text-align: right"><?=$price?> €</td>
@@ -146,13 +148,14 @@ $this->setReturnValue(['filename' => $invoice->invoice_number . ".pdf"]);
</tr>
</table>
<div style="margin-top: 20pt;">
<?php if($invoice->tax_text): ?>
<p style="font-weight: bold;"><?=$invoice->tax_text?></p>
<?php endif; ?>
</div>
Zahlungsziel 14 Tage.
</div>
</body>
</html>

View File

@@ -108,11 +108,13 @@ class PreorderBilling extends mfBaseModel {
$items = [];
$where = self::getSqlFilter($filter);
$sql = "SELECT preorderbillingcustomer_id, owner_id, billingaddress_id, billing_delivery FROM PreorderBilling
$sql = "SELECT preorderbillingcustomer_id, owner_id, billingaddress_id, Preorder.preordercampaign_id as campaign_id, billing_delivery FROM PreorderBilling
LEFT JOIN Preorder ON (PreorderBilling.preorder_id = Preorder.id)
WHERE $where
GROUP BY preorderbillingcustomer_id, owner_id, billingaddress_id, billing_delivery
ORDER BY preorderbillingcustomer_id, owner_id, billingaddress_id, billing_delivery";
GROUP BY preorderbillingcustomer_id, owner_id, billingaddress_id, campaign_id, billing_delivery
ORDER BY preorderbillingcustomer_id, owner_id, billingaddress_id, campaign_id, billing_delivery";
//var_dump($sql);exit;
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
@@ -120,6 +122,7 @@ class PreorderBilling extends mfBaseModel {
"preorderbillingcustomer_id" => $data->preorderbillingcustomer_id,
"owner_id" => $data->owner_id,
"billingaddress_id" => $data->billingaddress_id,
"campaign_id" => $data->campaign_id,
"billing_delivery" => $data->billing_delivery,
];
}
@@ -448,6 +451,13 @@ class PreorderBilling extends mfBaseModel {
}
}
if(array_key_exists("campaign_id", $filter)) {
$campaign_id = $filter['campaign_id'];
if(is_numeric($campaign_id)) {
$where .= " AND Preorder.campaign_id = $campaign_id";
}
}
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {

View File

@@ -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);
}
}

View File

@@ -84,7 +84,7 @@ class PreorderBillingInvoice extends mfBaseModel {
$pdf = new PdfForm("PreorderBillingInvoice/PDF_MAIN", $pdf_vars);
$wkhtmltopdfArgs = "--margin-top 10mm --header-spacing 96 --header-html '$headerFile' --footer-html '$footerFile'";
$wkhtmltopdfArgs = "--margin-top 10mm --header-spacing 90 --header-html '$headerFile' --footer-html '$footerFile'";
$filename = $pdf->render($wkhtmltopdfArgs);
@@ -182,8 +182,8 @@ class PreorderBillingInvoice extends mfBaseModel {
$table_fields = [
"netowner_id","invoice_number","invoice_date","preorderbillingcustomer_id","owner_id","billingaddress_id","fibu_account_number","fibu_cost_area",
"fibu_cost_account", "fibu_revenue_account","fibu_taxcode","tax_text","company","firstname","lastname","street","zip","city","country","email","uid",
"billing_type", "billing_delivery","bank_account_bank","bank_account_owner","bank_account_iban","bank_account_bic","total","total_gross","vatgroup_id",
"fibu_cost_account", "fibu_revenue_account","fibu_taxcode","tax_text","head_text","company","firstname","lastname","street","zip","city","country","email",
"uid", "billing_type", "billing_delivery","bank_account_bank","bank_account_owner","bank_account_iban","bank_account_bic","total","total_gross","vatgroup_id",
"vatrate", "bmd_export_date","date_delivered",
"create_by","edit_by","create","edit"
];

View File

@@ -183,6 +183,14 @@ class PreorderBillingInvoiceController extends mfBaseController {
$owner_id = $base["owner_id"];
$billingaddress_id = $base["billingaddress_id"];
$billing_delivery = $base["billing_delivery"];
$campaign_id = $base["campaign_id"];
$campaign = new Preordercampaign($campaign_id);
if(!$campaign->id) {
die("Kampagne $campaign_id nicht gefunden");
}
$campaign_name = $campaign->name;
$campaign_name = preg_replace('/^RML Liezen\s+-\s+/', '', $campaign_name);
$bill_positions = [];
$credit_positions = [];
@@ -193,12 +201,11 @@ class PreorderBillingInvoiceController extends mfBaseController {
"preorderbillingcustomer_id" => $preorderbillingcustomer_id,
"owner_id" => $owner_id,
"billingaddress_id" => $billingaddress_id,
"preordercampaign_id" => $campaign_id,
"billing_delivery" => $billing_delivery,
"invoice_id" => null,
];
$billing_rows = PreorderBilling::search($bill_filter);
if(!count($billing_rows)) {
var_dump($bill_filter);
@@ -210,7 +217,7 @@ class PreorderBillingInvoiceController extends mfBaseController {
$sorted_positions = [];
$invoice_positions = [];
$invoice_vatrate = 20;
$price_total_sum = 0;
//$price_total_sum = 0;
foreach($billing_rows as $bill) {
$billing_row_count++;
@@ -241,6 +248,7 @@ class PreorderBillingInvoiceController extends mfBaseController {
$detail_data = [
"owner_id" => $bill->owner_id,
"company" => ($bill->company) ?: $bill->firstname . " " . $bill->lastname,
"network_name" => $campaign_name,
"oaid" => $bill->oaid,
"order_date" => $bill->order_date,
"start_date" => $start_date->format("Y-m-d"),
@@ -349,6 +357,7 @@ class PreorderBillingInvoiceController extends mfBaseController {
$invoice_data["fibu_revenue_account"] = TT_PREORDER_BILLING[$netowner_id]["fibu-revenue-code"];
$invoice_data["fibu_tax_code"] = 1;
$invoice_data["tax_text"] = null;
$invoice_data["head_text"] = null;
$invoice_data["uid"] = $bill->uid;
$invoice_data["company"] = $bill->company;
$invoice_data["firstname"] = $bill->firstname;
@@ -385,6 +394,11 @@ class PreorderBillingInvoiceController extends mfBaseController {
unset($position->preorder_billings);
unset($position->count);
if($position->product_id != 2) {
$invoice->head_text = "Entgelte für Netzgebiet $campaign_name";
}
$total_net += $position->price_total;
$total_gross += $position->price_gross;
@@ -446,14 +460,15 @@ class PreorderBillingInvoiceController extends mfBaseController {
if(count($invoice_detail_data)) {
// create CSV file
//var_dump($invoice_detail_data);exit;
$csv = "\u{FEFF}Netzbetreiber;Rechungsnummer;Rechnungsdateum;OAID;Bestelldatum;Periode von;Periode bis;Artikelnummer;Produkt;Anzahl;Preis Netto\n";
$csv = "\u{FEFF}Netzbetreiber;Rechungsnummer;Rechnungsdateum;Netzgebiet;OAID;Bestelldatum;Periode von;Periode bis;Artikelnummer;Produkt;Anzahl;Preis Netto\n";
foreach ($invoice_detail_data as $detail) {
//var_dump($detail);
//exit;
$csv .= '"'.str_replace(["\r","\n"], " ", $detail["company"]).'";';
$csv .= '"'.str_replace(["\r","\n", '"'], [" "," ",'""'], $detail["company"]).'";';
$csv .= '"'.$invoice->invoice_number.'";';
$csv .= '"'.$invoice->invoice_date.'";';
$csv .= '"'.str_replace('"','""',$detail["network_name"]).'";';
$csv .= '"'.$detail["oaid"].'";';
$csv .= '"'.$detail["order_date"].'";';
$csv .= '"'.$detail["start_date"].'";';

View File

@@ -110,6 +110,7 @@ class PreorderProductMarketshareDiscount extends mfBaseModel {
WHERE $where
ORDER BY preorderproductprice_id,bracket LIMIT 1";
//var_dump($sql);exit;
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);

View File

@@ -10,6 +10,15 @@ class PreorderProductPrice extends mfBaseModel {
private $creator;
private $editor;
public function getMarketshareBracket($bracket) {
$bracket = PreorderProductMarketshareDiscount::getFirst(["preorderproductprice_id" => $this->id, "bracket" => $bracket]);
if(!$bracket) {
return false;
}
return $bracket;
}
public function getProperty($name) {
if($this->$name == null) {

View File

@@ -109,6 +109,17 @@ class Preordercampaign extends mfBaseModel {
return ['activation' => $activation_products, 'provision' => $provision_products, 'reorder' => $reorder_products];
}
public function getUnitCount() {
$count = 0;
$netzgebiete = $this->getProperty("salesclusters");
foreach($netzgebiete as $netzgebiet) {
$count += $netzgebiet->unit_count;
}
return $count;
}
public function getProperty($name) {
if($this->$name == null) {

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class PreorderBillingInvoiceAddHeadText extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("PreorderBillingInvoice");
$table->addColumn("head_text", "text", ["null" => true, "default" => null, "after" => "tax_text"]);
$table->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("PreorderBillingInvoice")->removeColumn("head_text")->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
}