WIP Contract/Billing 2024-06-24

This commit is contained in:
Frank Schubert
2024-06-25 12:19:35 +02:00
parent da52a802ef
commit 4589e61ab8
9 changed files with 515 additions and 113 deletions

View File

@@ -13,10 +13,10 @@ $pagination_entity_name = "Billingrecords";
<ol class="breadcrumb m-0"> <ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="<?=self::getUrl("Dashboard")?>"><?=MFAPPNAME_SLUG?></a> <li class="breadcrumb-item"><a href="<?=self::getUrl("Dashboard")?>"><?=MFAPPNAME_SLUG?></a>
</li> </li>
<li class="breadcrumb-item active">Fertiggestellte Bestellungen</li> <li class="breadcrumb-item active">Rechnungsdatensätze</li>
</ol> </ol>
</div> </div>
<h4 class="page-title">Contractfreigabe</h4> <h4 class="page-title">Rechnungsdatensätze</h4>
</div> </div>
</div> </div>
</div> </div>
@@ -36,7 +36,21 @@ $pagination_entity_name = "Billingrecords";
<label class="form-label" for="filter_owner">Kundennummer</label> <label class="form-label" for="filter_owner">Kundennummer</label>
<input type="text" class="form-control" name="filter[customer_number]" id="filter_street" value="<?=(array_key_exists("customer_number", $filter)) ? $filter['customer_number'] : ""?>"/> <input type="text" class="form-control" name="filter[customer_number]" id="filter_street" value="<?=(array_key_exists("customer_number", $filter)) ? $filter['customer_number'] : ""?>"/>
</div> </div>
<div class="col-2">
<label class="form-label" for="filter_show_credit">Gutschriften</label>
<select class="form-control" name="filter[show_credit]" id="filter_show_credit">
<option value="0" <?=(array_key_exists("show_credit", $filter) && $filter['show_credit'] == 0) ? "selected='selected'" : ""?>>Ausblenden</option>
<option value="1" <?=(array_key_exists("show_credit", $filter) && $filter['show_credit'] == 1 ) ? "selected='selected'" : ""?>>Anzeigen</option>
</select>
</div>
<div class="col-2">
<label class="form-label" for="filter_show_credit">Rechnungsperiode</label>
<select class="form-control" name="filter[billing_period]" id="filter_billing_period">
<option></option>
<option value="1" <?=(array_key_exists("billing_period", $filter) && $filter['billing_period'] == 1) ? "selected='selected'" : ""?>>Monatlich</option>
<option value="12" <?=(array_key_exists("billing_period", $filter) &&$filter['billing_period'] == 12 ) ? "selected='selected'" : ""?>>Jährlich</option>
</select>
</div>
</div> </div>
<div class="row mt-2"> <div class="row mt-2">
<div class="col"> <div class="col">
@@ -73,8 +87,62 @@ $pagination_entity_name = "Billingrecords";
<table class="table table-sm table-striped table-hover"> <table class="table table-sm table-striped table-hover">
<tr> <tr>
<th>Contract ID</th>
<th>Periode</th>
<th>Vertragsinhaber</th>
<th>Rechnungsadresse</th>
<th>Kundennummer</th>
<th>Einzug</th>
<th>Zustellung</th>
<th>Bankdaten</th>
<th>Produkt</th>
<th>Anzahl</th>
<th>Preis</th>
<th>Preis Setup</th>
<th></th> <th></th>
</tr> </tr>
<?php foreach($billings as $billing): ?>
<tr>
<td><a href="<?=self::getUrl("Contract", "view", ["contract_id" => $billing->contract_id])?>" target="_blank"><?=$billing->contract_id?></a></td>
<td><?=$billing->start_date?> - <?=$billing->end_date?></td>
<td>
<?=$billing->contract->owner->getCompanyOrName()?><br />
<?=$billing->contract->owner->street?><br />
<?=$billing->contract->owner->zip?> <?=$billing->contract->owner->city?>
<?=$billing->contract->owner->country->name?>
</td>
<td>
<?=($billing->company) ? $billing->company."<br />" : ""?>
<?=($billing->firstname || $billing->lastname) ? $billing->firstname." ".$billing->lastname."<br />" : ""?>
<?=$billing->street?><br />
<?=$billing->zip?> <?=$billing->city?><br />
<?=$billing->country?>
</td>
<td><?=$billing->customer_number?></td>
<td><?=($billing->billing_type == "sepa") ? "SEPA" : ""?></td>
<td><?=($billing->billing_delivery == "email") ? "Email" : "Papier"?></td>
<td>
<?php if($billing->billing_type == "sepa"): ?>
Kontoinhaber: <?=$billing->bank_account_owner?><br />
Bank: <?=$billing->bank_account_bank?><br />
IBAN: <?=$billing->bank_account_iban?><br />
BIC: <?=$billing->bank_account_bic?><br />
<?php endif; ?>
</td>
<td>
<?=$billing->product_name?><?=($billing->matchcode) ? " (".$billing->matchcode.")" : ""?>
<?php if($billing->product_info): ?>
<div class="pl-2">
<?=$billing->product_info?>
</div>
<?php endif; ?>
</td>
<td><?=number_format($billing->amount,3,",",".")?></td>
<td>€ <?=number_format($billing->price,4,",",".")?></td>
<td>€ <?=number_format($billing->price_setup,4,",",".")?></td>
<td></td>
</tr>
<?php endforeach; ?>
</table> </table>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?> <?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?>

View File

@@ -128,8 +128,8 @@
</a> </a>
<?php endif; ?> <?php endif; ?>
</td>--> </td>-->
<td class="<?=($contract->isCancelled()) ? "canceled" : "" ?> <?=(!$contract->isFinished()) ? "not-finished" : "" ?> <?=($contract->price < 0) ? "text-danger" : ""?>"><?=$contract->price?></td> <td class="<?=($contract->isCancelled()) ? "canceled" : "" ?> <?=(!$contract->isFinished()) ? "not-finished" : "" ?> <?=($contract->price < 0) ? "text-danger" : ""?>"><?=number_format($contract->price,4,",",".")?></td>
<td class="<?=($contract->isCancelled()) ? "canceled" : "" ?> <?=(!$contract->isFinished()) ? "not-finished" : "" ?> <?=($contract->price_setup < 0) ? "text-danger" : ""?>"><?=$contract->price_setup?></td> <td class="<?=($contract->isCancelled()) ? "canceled" : "" ?> <?=(!$contract->isFinished()) ? "not-finished" : "" ?> <?=($contract->price_setup < 0) ? "text-danger" : ""?>"><?=number_format($contract->price_setup,4,",",".")?></td>
<td class="<?=($contract->isCancelled()) ? "canceled" : "" ?> <?=(!$contract->isFinished()) ? "not-finished" : "" ?>"> <td class="<?=($contract->isCancelled()) ? "canceled" : "" ?> <?=(!$contract->isFinished()) ? "not-finished" : "" ?>">
<?php if($contract->billing_period == 1): ?> <?php if($contract->billing_period == 1): ?>
monatlich monatlich

View File

@@ -4,6 +4,7 @@ class Admin_IvtContractImport {
private $request; private $request;
private $log; private $log;
private $static_ivt_order_match = []; private $static_ivt_order_match = [];
private $no_matchcode = [];
public function __construct($request = false) { public function __construct($request = false) {
$this->request = $request; $this->request = $request;
@@ -77,6 +78,13 @@ class Admin_IvtContractImport {
16267 => 3079, // schmuck marianne 16267 => 3079, // schmuck marianne
16786 => 3079 16786 => 3079
]; ];
$this->no_matchcode = [
9,
2847,
3049,
5719
];
} }
/* TODO: Credit */ /* TODO: Credit */
@@ -166,14 +174,9 @@ class Admin_IvtContractImport {
$order_date = new DateTime($ivt_contract->created);
$order_date->setTime(0,0,0);
$finish_date = new DateTime($ivt_contract->lastdate);
$finish_date->modify("first day of this month");
$finish_date->setTime(0,0,0);
//$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1);
//$finish_date->modify("+1 hours");
$contract_data = []; $contract_data = [];
$contract_data['owner_id'] = $customer->id; $contract_data['owner_id'] = $customer->id;
@@ -186,23 +189,35 @@ class Admin_IvtContractImport {
$contract_data['price_nne'] = 0; $contract_data['price_nne'] = 0;
$contract_data['price_nbe'] = 0; $contract_data['price_nbe'] = 0;
$contract_data['sla_id'] = $this->getNewSlaId($ivt_contract->sid); $contract_data['sla_id'] = $this->getNewSlaId($ivt_contract->sid);
$contract_data['order_date'] = $order_date->getTimestamp();
$contract_data['finish_date'] = $finish_date->getTimestamp();
$contract_data['finish_date_by'] = 1;
$contract_data['imported_from'] = "ivt"; $contract_data['imported_from'] = "ivt";
$contract_data['imported_data'] = $ivt_contract->id; $contract_data['imported_data'] = $ivt_contract->id;
$contract_data['create_by'] = 1; $contract_data['create_by'] = 1;
$contract_data['edit_by'] = 1; $contract_data['edit_by'] = 1;
$finish_date = new DateTime($ivt_contract->lastdate);
switch($ip->interval) { switch($ip->interval) {
case 0: case 0:
$contract_data['billing_period'] = 1; $contract_data['billing_period'] = 1;
$finish_date->modify("+1 month");
break; break;
case 1: case 1:
$contract_data['billing_period'] = 12; $contract_data['billing_period'] = 12;
$finish_date->modify("+1 year");
break; break;
} }
$order_date = new DateTime($ivt_contract->created);
$order_date->setTime(2,0,0);
$finish_date->modify("first day of this month");
$finish_date->setTime(2,0,0);
//$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1);
//$finish_date->modify("+1 hours");
$contract_data['order_date'] = $order_date->getTimestamp();
$contract_data['finish_date'] = $finish_date->getTimestamp();
$contract_data['finish_date_by'] = 1;
$neu["customer"] = $customer; $neu["customer"] = $customer;
$neu["ivtcustomer"] = $ivt_customer; $neu["ivtcustomer"] = $ivt_customer;
@@ -321,6 +336,8 @@ class Admin_IvtContractImport {
foreach($new_contracts as $ivt_customer_id => $contracts) { foreach($new_contracts as $ivt_customer_id => $contracts) {
$this->addVoipData($ivt_customer_id, $contracts); $this->addVoipData($ivt_customer_id, $contracts);
if(in_array($ivt_contract->id, $this->no_matchcode)) break;
if(count($contracts) < 2) continue; if(count($contracts) < 2) continue;
$prev_contracts = []; $prev_contracts = [];
$primary_matchcode = false; $primary_matchcode = false;

View File

@@ -163,7 +163,9 @@ class Admin_IvtCreditImport {
$create_date->setTime(2,0); $create_date->setTime(2,0);
$finish_date = new DateTime($cust_cred->lastdate); $finish_date = new DateTime($cust_cred->lastdate);
$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1); $finish_date->modify("first day of this month");
$finish_date->setTime(2,0,0);
//$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1);
$data["orderproduct_id"] = null; $data["orderproduct_id"] = null;
$data["product_id"] = 336; // Mainproduct dummy for crediting import [IVT] $data["product_id"] = 336; // Mainproduct dummy for crediting import [IVT]
@@ -227,8 +229,7 @@ class Admin_IvtCreditImport {
$journal = ContractjournalModel::create([ $journal = ContractjournalModel::create([
'contract_id' => $primary_contract->id, 'contract_id' => $primary_contract->id,
'type' => "credit_created", 'type' => "credit_created",
'value' => $primary_contract->id, 'value' => $cred_contract->id,
//'text' => "IVT customer_credit ".$cust_cred->id."; cid: ".$cust_cred->cust_id."; pid: ".$ivt_cred_product->id." (".$ivt_cred_product->name.")"
]); ]);
$journal_id = $journal->save(); $journal_id = $journal->save();
if(!$journal_id) { if(!$journal_id) {

View File

@@ -2,6 +2,7 @@
class Billing extends mfBaseModel { class Billing extends mfBaseModel {
protected $forcestr = ["product_name","product_info","matchcode"]; protected $forcestr = ["product_name","product_info","matchcode"];
private $contract;

View File

@@ -57,6 +57,15 @@ class BillingController extends mfBaseController {
{ {
$new_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 (is_array($filter) && count($filter)) { if (is_array($filter) && count($filter)) {
foreach ($filter as $name => $value) { foreach ($filter as $name => $value) {
$new_filter[$name] = $value; $new_filter[$name] = $value;
@@ -77,17 +86,49 @@ class BillingController extends mfBaseController {
$i = 0; $i = 0;
foreach(ContractModel::searchActive(["finish_date<" => $today->getTimestamp()]) as $contract) {
//var_dump($contract);exit;
$now_year = date("Y"); $now_year = date("Y");
$now_month = date("m"); $now_month = date("m");
$now_day = date("d"); $now_day = date("d");
$now_year = 2024;
$now_month = 7;
foreach(ContractModel::search(["finish_date<" => mktime(0,0,0,$now_month, $now_day, $now_year), "cancel_date" => null]) as $contract) {
//var_dump($contract);exit;
//$contract = new Contract(1);
$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);
$finish_year = date("Y", $contract->finish_date); $finish_year = date("Y", $contract->finish_date);
$finish_month = date("m", $contract->finish_date); $finish_month = date("m", $contract->finish_date);
$finish_day = date("d", $contract->finish_date); $finish_day = date("d", $contract->finish_date);
//echo "$bill_month $bill_year\n";exit;
//echo $monthly_bill_period_to->format("Y-m-d H:i:s");exit;
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;
}
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; $cancel_date = false;
if($contract->cancel_date) { if($contract->cancel_date) {
$cancel_date = new DateTime("@".$contract->cancel_date); $cancel_date = new DateTime("@".$contract->cancel_date);
@@ -97,57 +138,198 @@ class BillingController extends mfBaseController {
} }
} }
// find last Billing row
$last_billing = BillingModel::getLast(["contract_id" => $contract->id]);
if(!$last_billing) {
// First billing
// check finish_date
// create Billing with start_date=finish_date and end_date = end of billing_period (month or year)
$start_date = new DateTime("@".$contract->finish_date); $start_date = new DateTime("@".$contract->finish_date);
$start_date->setTime(2,0,0); $start_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$price_setup = $contract->price_setup; // ignore yearly contracts which are not billable this month
} else { if($contract->billing_period == 12) {
// Concurrent Billing if($start_date->format("m") != $bill_month) {
// start_date next date after previous end_date continue;
}
$start_date = new DateTime($last_billing->end_date);
$start_date->modify("+1 month");
// set Setup price to 0, because it was billed already
$price_setup = 0;
} }
$create_bills = [];
// Concurrent Billing
// find not yet billed periods
$create_dates = [];
$create_date = clone $start_date;
//$create_date->modify("+".$contract->billing_period." month");
$create_date->modify("first day of this month");
$last_create_date = false;
//echo "first create_date: ".$create_date->format("Y-m-d H:i:s")."<br />\n";
//echo "while ".$create_date->getTimestamp()." (".$create_date->format("Y-m-d H:i:s").") >= ".$contract->finish_date." (".date("Y-m-d H:i:s", $contract->finish_date).")<br>\n";
while($create_date->getTimestamp() >= $contract->finish_date) {
//echo "in need date while (".$create_date->format("Y-m-d H:i:s").")<br>";
if($last_create_date) {
// must for safety / shouldn't happen
die("need-date ran out of dates");
}
//echo "&nbsp;&nbsp;&nbsp;&nbsp;";
//echo $create_date->format("Y")." == ".$finish_year." && ".$create_date->format("m")." == ".$finish_month."<br>";
if($create_date->format("Y") == $finish_year && $create_date->format("m") == $finish_month) {
$create_date->setDate($finish_year, $finish_month, $finish_day);
//echo "set last_create_date true<br>";
$last_create_date = true;
}
$existing_bill = BillingModel::getFirst(["contract_id" => $contract->id, "start_date" => $create_date->format("Y-m-d")]);
//var_dump($need_bill);exit;
if(!$existing_bill) {
//echo "adding date to create_dates[]<br>";
$new_create_date = clone $create_date;
$create_dates[] = $new_create_date;
$create_date->modify("-".$contract->billing_period." months");
continue;
}
break;
}
//var_dump($create_dates);
// 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
];
}
/*$last_billings = BillingModel::count(["contract_id" => $contract->id]);
if(!$last_billings) {
// First billing
$start_date->setTime(0,0,0);
//echo "start_date: ".$start_date->format("Y-m-d H:i:s")."<br />";
$create_bills[] = [
"start_date" => $start_date,
"price_setup" => $contract->price_setup
];
} else {
// Concurrent Billing
// find not yet billed periods
$create_dates = [];
$create_date = clone $start_date;
$create_date->modify("+1 month");
$create_date->modify("first day of this month");
$last_create_date = false;
//echo "first create_date: ".$create_date->format("Y-m-d H:i:s")."<br />\n";
//echo "while ".$create_date->getTimestamp()." (".$create_date->format("Y-m-d H:i:s").") >= ".$contract->finish_date." (".date("Y-m-d H:i:s", $contract->finish_date).")<br>\n";
while($create_date->getTimestamp() >= $contract->finish_date) {
//echo "in need date while (".$create_date->format("Y-m-d H:i:s").")<br>";
if($last_create_date) {
// must for safety / shouldn't happen
die("need-date ran out of dates");
}
//echo "&nbsp;&nbsp;&nbsp;&nbsp;";
//echo $create_date->format("Y")." == ".$finish_year." && ".$create_date->format("m")." == ".$finish_month."<br>";
if($create_date->format("Y") == $finish_year && $create_date->format("m") == $finish_month) {
$create_date->setDate($finish_year, $finish_month, $finish_day);
//echo "set last_create_date true<br>";
$last_create_date = true;
}
$existing_bill = BillingModel::getFirst(["contract_id" => $contract->id, "start_date" => $create_date->format("Y-m-d")]);
//var_dump($need_bill);exit;
if(!$existing_bill) {
//echo "adding date to create_dates[]<br>";
$new_create_date = clone $create_date;
$create_dates[] = $new_create_date;
$create_date->modify("-".$contract->billing_period." months");
continue;
}
break;
}
//var_dump($create_dates);
// find missing billings
foreach($create_dates as $start_date) {
// ignore if last billing row is from this month
/*if($start_date->getTimestamp() < $earliest_next_billing_date->getTimestamp()) {
$this->log->debug(__METHOD__.": last billing row is current billing row. Skip creating new billing row");
continue;
}*//*
$create_bills[] = [
"start_date" => $start_date,
"price_setup" => 0 // 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"];
$price_setup = $bill_data["price_setup"];
// if contract has cancel date this month // if contract has cancel date this month
// use cancel date as end_date // use cancel date as end_date
if($cancel_date) { if ($cancel_date) {
$end_date = clone($cancel_date); $end_date = clone $cancel_date;
} else { } else {
// else calculate last of month // else calculate last of month
$end_date = clone($start_date); $end_date = clone $start_date;
$end_date->modify("+".$contract->billing_period." months");
$end_date->modify("first day of this month"); $end_date->modify("first day of this month");
$end_date->modify("+" . $contract->billing_period . " months");
//$end_date->modify("first day of this month");
$end_date->modify("-1 day"); $end_date->modify("-1 day");
} }
$sday = $start_date->format("d"); $sday = $start_date->format("d");
$eday = $end_date->format("d"); $eday = $end_date->format("d");
if($sday > 1 || $cancel_date) { if ($sday > 1 || $cancel_date) {
// aliquoter preis // aliquoter preis
$days = ($eday - $sday) + 1; $days = ($eday - $sday) + 1;
//echo "days: $days<br />";
$pc = $days / $eday * 100; $pc = $days / $eday * 100;
//echo "pc: $pc<br />";
$price = round($contract->price / 100 * $pc, 4); $price = round($contract->price / 100 * $pc, 4);
} else { } else {
$price = $contract->price; $price = $contract->price;
} }
/*
echo "contact ID: ".$contract->id."<br />";
echo "contract price: ". $contract->price."<br />";
echo "price: ". $price."<br />";
echo "start_date: ".$start_date->format("Y-m-d H:i:s")."<br />";
echo "sday: $sday<br />";
exit;
*/
$owner = $contract->owner; $owner = $contract->owner;
$billingaddress = $contract->billingaddress; $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 = [];
$data["contract_id"] = $contract->id; $data["contract_id"] = $contract->id;
$data["start_date"] = $start_date->format("Y-m-d"); $data["start_date"] = $start_date->format("Y-m-d");
@@ -163,8 +345,8 @@ class BillingController extends mfBaseController {
$data["country"] = $billingaddress->country->name; $data["country"] = $billingaddress->country->name;
$data["email"] = $billingaddress->email; $data["email"] = $billingaddress->email;
$data["uid"] = $billingaddress->uid; $data["uid"] = $billingaddress->uid;
$data["billing_type"] = $billingaddress->billing_type; $data["billing_type"] = $billing_type;
$data["billing_delivery"] = $billingaddress->billing_delivery; $data["billing_delivery"] = $billing_delivery;
$data["bank_account_bank"] = $billingaddress->bank_account_bank; $data["bank_account_bank"] = $billingaddress->bank_account_bank;
$data["bank_account_owner"] = $billingaddress->bank_account_owner; $data["bank_account_owner"] = $billingaddress->bank_account_owner;
$data["bank_account_iban"] = $billingaddress->bank_account_iban; $data["bank_account_iban"] = $billingaddress->bank_account_iban;
@@ -179,13 +361,17 @@ class BillingController extends mfBaseController {
$data["billing_period"] = $contract->billing_period; $data["billing_period"] = $contract->billing_period;
$billing = BillingModel::create($data); $billing = BillingModel::create($data);
if(!$billing->save()) { if (!$billing->save()) {
var_dump($billing);exit; var_dump($billing);
exit;
} }
$i++; $i++;
}
} }
$this->layout()->setFlash("$i Billing records generiert"); $this->layout()->setFlash("$i Billing records generiert");
$this->redirect("Billing");
} }
} }

View File

@@ -124,7 +124,7 @@ class BillingModel {
$sql = "SELECT COUNT(*) as cnt FROM Billing $sql = "SELECT COUNT(*) as cnt FROM Billing
WHERE $where"; WHERE $where";
mfLoghandler::singleton()->debug($sql); //mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql); $res = $db->query($sql);
if($db->num_rows($res)) { if($db->num_rows($res)) {
@@ -134,15 +134,20 @@ class BillingModel {
return 0; return 0;
} }
public static function search($filter, $limit = false) { public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit; //var_dump($filter);exit;
$items = []; $items = [];
if(!$order) {
$order = "id ASC";
}
$db = FronkDB::singleton(); $db = FronkDB::singleton();
$where = self::getSqlFilter($filter); $where = self::getSqlFilter($filter);
$sql = "SELECT * FROM Billing $sql = "SELECT * FROM Billing
WHERE $where WHERE $where
ORDER BY billingaddress_id"; ORDER BY $order";
if(is_array($limit) && count($limit)) { if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) { if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
@@ -208,6 +213,41 @@ class BillingModel {
} }
} }
if(array_key_exists("start_date", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date']);
if($start_date) {
$where .= " AND Billing.start_date='$start_date'";
}
}
if(array_key_exists("start_date>", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>']);
if($start_date) {
$where .= " AND Billing.start_date > '$start_date'";
}
}
if(array_key_exists("start_date<", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<']);
if($start_date) {
$where .= " AND Billing.start_date < '$start_date'";
}
}
if(array_key_exists("start_date>=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>=']);
if($start_date) {
$where .= " AND Billing.start_date >='$start_date'";
}
}
if(array_key_exists("start_date<=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<=']);
if($start_date) {
$where .= " AND Billing.start_date <= '$start_date'";
}
}
if(array_key_exists("billingaddress_id", $filter)) { if(array_key_exists("billingaddress_id", $filter)) {
$billingaddress_id = $filter['billingaddress_id']; $billingaddress_id = $filter['billingaddress_id'];
if(is_numeric($billingaddress_id)) { if(is_numeric($billingaddress_id)) {
@@ -313,11 +353,44 @@ class BillingModel {
} }
} }
if(array_key_exists("price<", $filter)) {
$price = $filter['price<'];
if(is_numeric($price)) {
$where .= " AND Billing.price < $price";
}
}
if(array_key_exists("price<=", $filter)) {
$price = $filter['price<='];
if(is_numeric($price)) {
$where .= " AND Billing.price <= $price";
}
}
if(array_key_exists("price>", $filter)) {
$price = $filter['price>'];
if(is_numeric($price)) {
$where .= " AND Billing.price > $price";
}
}
if(array_key_exists("price>=", $filter)) {
$price = $filter['price>='];
if(is_numeric($price)) {
$where .= " AND Billing.price >= $price";
}
}
if(array_key_exists("add-where", $filter)) { if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where']; $where .= " ".$filter['add-where'];
} }
if(array_key_exists("billing_period", $filter)) {
$billing_period = $filter['billing_period'];
if(is_numeric($billing_period)) {
$where .= " AND Billing.billing_period = $billing_period";
}
}
//var_dump($filter, $where);exit; //var_dump($filter, $where);exit;
return $where; return $where;
} }

View File

@@ -61,7 +61,7 @@ class PreorderDiscountController extends mfBaseController {
continue; continue;
} }
$discount_code = PreorderDiscountModel::getFirst(["code" => $code, "preorer_id" => $preorder->id]); $discount_code = PreorderDiscountModel::getFirst(["code" => $code, "preorder_id" => $preorder->id]);
if($discount_code) { if($discount_code) {
$exists++; $exists++;
continue; continue;

View File

@@ -14,8 +14,8 @@ $me = new User(1);
define("INTERNAL_USER_ID", $me->id); define("INTERNAL_USER_ID", $me->id);
$finish_date_from = new DateTime("2024-05-31 22:00:00"); $finish_date_from = new DateTime("2024-05-31 22:00:00");
$finish_date_to = new DateTime("2024-06-01 02:00:00"); $finish_date_to = new DateTime("2024-06-01 03:00:00");
$finish_date = new DateTime("2024-06-01 00:00:00"); $finish_date = new DateTime("2024-06-01 02:00:00");
$cms = 0; $cms = 0;
$cmss = 0; $cmss = 0;
@@ -33,9 +33,9 @@ foreach(ContractModel::search(["finish_date" => $finish_date->getTimestamp()]) a
$First = new DateTime("2023-05-31 22:00:00"); $First = new DateTime("2023-05-31 22:00:00");
//$First = new DateTime("2024-06-09 00:00:00"); //$First = new DateTime("2024-06-09 00:00:00");
$Last = new DateTime("2024-06-01 00:00:00"); $Last = new DateTime("2024-06-01 03:00:00");
foreach(ContractModel::search(["billing_period" => 12]) as $contract) { foreach(ContractModel::search(["billing_period" => 12, "finish_date" => $finish_date->getTimestamp()]) as $contract) {
$fdate = new DateTime("@".$contract->finish_date); $fdate = new DateTime("@".$contract->finish_date);
$y = $fdate->format("Y"); $y = $fdate->format("Y");
$m = $fdate->format("m"); $m = $fdate->format("m");
@@ -43,9 +43,9 @@ foreach(ContractModel::search(["billing_period" => 12]) as $contract) {
//if(($m == 5 && $d >= 11) || ($m == 6 && $d <= 10)) { //if(($m == 5 && $d >= 11) || ($m == 6 && $d <= 10)) {
//if($contract->finish_date >= $First->getTimestamp() && $contract->finish_date <= $Last->getTimestamp()) { //if($contract->finish_date >= $First->getTimestamp() && $contract->finish_date <= $Last->getTimestamp()) {
if($contract->finish_date == $Last->getTimestamp()) { //if($contract->finish_date == $Last->getTimestamp()) {
$cys += $contract->price; $cys += $contract->price;
} //}
} }
$cts = $cms + $cys + $cmss; $cts = $cms + $cys + $cmss;
@@ -94,8 +94,64 @@ echo "======================================\n";
$cid = $ibs - $cts; $cid = $ibs - $cts;
echo "Diff thetool <-> ivt Bills: ".round($cid, 4)."\n"; echo "Diff thetool <-> ivt Bills: ".round($cid, 4)."\n";
echo "======================================\n";
$tbms = 0;
$tbys = 0;
$tbs = 0;
foreach(BillingModel::search(["price>=" => 0]) as $bill) {
if($bill->billing_period == 1) {
$tbms += $bill->price;
}
if($bill->billing_period == 12) {
$tbys += $bill->price;
}
if($bill->price_setup) {
$tbs += $bill->price_setup;
}
}
$tbts = $tbms + $tbys + $tbs;
echo "\n";
echo "Tool Billing Monthly Sum: $tbms\n";
echo "Tool Billing Setup Sum: $tbs\n";
echo "Tool Billing Yearly Sum: $tbys\n";
echo "--------------------------------------\n";
echo "Tool Billing Total Sum: $tbts\n";
echo "======================================\n";
$tbcms = 0;
$tbcys = 0;
$tbcs = 0;
foreach(BillingModel::search(["price<" => 0]) as $bill) {
if($bill->billing_period == 1) {
$tbcms += $bill->price;
}
if($bill->billing_period == 12) {
$tbcys += $bill->price;
}
if($bill->price_setup) {
$tbcs += $bill->price_setup;
}
}
$tbcts = $tbcms + $tbcys + $tbcs;
echo "\n";
echo "Tool Billing Crediting Monthly Sum: $tbcms\n";
echo "Tool Billing Crediting Setup Sum: $tbcs\n";
echo "Tool Billing Crediting Yearly Sum: $tbcys\n";
echo "--------------------------------------\n";
echo "Tool Billing Crediting Total Sum: $tbcts\n";
echo "======================================\n";
$gsm = 0; $gsm = 0;
$gsy = 0; $gsy = 0;