WIP Contract/Billing/Invoice 2024-06-28
This commit is contained in:
@@ -66,6 +66,22 @@ class BillingController extends mfBaseController {
|
||||
$new_filter["price>="] = 0;
|
||||
}
|
||||
|
||||
if(array_key_exists("customer", $filter)) {
|
||||
if(array_key_exists("customer", $filter) && $filter["customer"]) {
|
||||
$kunde = $this->db()->escape($filter['customer']);
|
||||
if(!array_key_exists("add-where", $new_filter)) $new_filter["add-where"] = "";
|
||||
$new_filter['add-where'] .= " AND (company like '%$kunde%' OR firstname like '%$kunde%' OR lastname like '%$kunde%' OR concat(firstname, ' ', lastname) like '%$kunde%' OR concat(lastname, ' ', firstname) like '%$kunde%')";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("address", $filter)) {
|
||||
if(array_key_exists("address", $filter) && $filter["address"]) {
|
||||
$search = $this->db()->escape($filter['address']);
|
||||
if(!array_key_exists("add-where", $new_filter)) $new_filter["add-where"] = "";
|
||||
$new_filter['add-where'] .= " AND (street like '%$search%' OR zip like '%$search%' OR city like '%$search%' OR country like '%$search%')";
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($filter) && count($filter)) {
|
||||
foreach ($filter as $name => $value) {
|
||||
$new_filter[$name] = $value;
|
||||
@@ -86,19 +102,33 @@ class BillingController extends mfBaseController {
|
||||
|
||||
$i = 0;
|
||||
|
||||
//$yearly_not_before = new DateTime("2023-06-01");
|
||||
|
||||
$now_year = date("Y");
|
||||
$now_month = date("m");
|
||||
$now_day = date("d");
|
||||
$now_year = 2024;
|
||||
$now_month = 7;
|
||||
|
||||
// XXX only for 1st Billing after IVT Import
|
||||
//$yearly_not_before = new DateTime("$now_year-$now_month-01");
|
||||
$yearly_not_before = new DateTime("$now_year-06-01");
|
||||
|
||||
$del = 0;
|
||||
// first delete all non-invoiced billing records
|
||||
/*foreach(BillingModel::search(["invoice_id" => null]) as $bill) {
|
||||
$bill->delete();
|
||||
$del++;
|
||||
}*/
|
||||
|
||||
$this->log->notice(__METHOD__.": $del Billing records deleted");
|
||||
//$stop = false;
|
||||
foreach(ContractModel::search(["finish_date<" => mktime(0,0,0,$now_month, $now_day, $now_year), "cancel_date" => null]) as $contract) {
|
||||
//while(!$stop) {
|
||||
//$stop = true;
|
||||
//$contract = new Contract(1475);
|
||||
|
||||
//var_dump($contract);exit;
|
||||
//$contract = new Contract(1);
|
||||
|
||||
|
||||
|
||||
|
||||
$bill_month = $now_month;
|
||||
$bill_year = $now_year;
|
||||
//$bill_day = $now_day;
|
||||
@@ -108,22 +138,27 @@ class BillingController extends mfBaseController {
|
||||
$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_month = date("m", $contract->finish_date);
|
||||
$finish_day = date("d", $contract->finish_date);
|
||||
$contract_finish_date->setTimezone(new DateTimeZone("Europe/Vienna"));
|
||||
$contract_finish_date->setTime(0,0,0);
|
||||
if($contract->billing_delay) {
|
||||
// add billing delay to finish_date so the first x months won't be billed
|
||||
$this->log->debug(__METHOD__.": Adding ".$contract->billing_delay." billing_delay months to finish_date");
|
||||
$contract_finish_date->modify("+".$contract->billing_delay." months");
|
||||
}
|
||||
|
||||
//echo "$bill_month $bill_year\n";exit;
|
||||
$finish_year = $contract_finish_date->format("Y");
|
||||
$finish_month = $contract_finish_date->format("m");
|
||||
$finish_day = $contract_finish_date->format("d");
|
||||
|
||||
//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) {
|
||||
/*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;
|
||||
@@ -138,177 +173,156 @@ class BillingController extends mfBaseController {
|
||||
}
|
||||
}
|
||||
|
||||
$start_date = new DateTime("@".$contract->finish_date);
|
||||
$start_date->setTimezone(new DateTimeZone("Europe/Vienna"));
|
||||
//$start_date = clone $contract_finish_date;
|
||||
|
||||
// ignore yearly contracts which are not billable this month
|
||||
if($contract->billing_period == 12) {
|
||||
if($start_date->format("m") != $bill_month) {
|
||||
/*if($contract->billing_period == 12) {
|
||||
if($contract_finish_date->format("m") != $bill_month) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
$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 " ";
|
||||
//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");
|
||||
if(!$contract->billing_period) {
|
||||
// setup only
|
||||
if(BillingModel::getFirst(["contract_id" => $contract->id])) {
|
||||
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,
|
||||
"start_date" => $contract_finish_date,
|
||||
"end_date" => $contract_finish_date,
|
||||
"price_setup" => $contract->price_setup
|
||||
];
|
||||
} else {
|
||||
// Concurrent Billing
|
||||
// find not yet billed periods
|
||||
|
||||
// contracts with billing period
|
||||
$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";
|
||||
//echo "initial bill_date: ".$bill_date->format("Y-m-d")."<br>";
|
||||
|
||||
while($create_date->getTimestamp() >= $contract->finish_date) {
|
||||
//echo "in need date while (".$create_date->format("Y-m-d H:i:s").")<br>";
|
||||
// if more than 1 month period, adjust initial billing_date to contracts finish_date in current period
|
||||
// otherwise i.e. yearly contracts older than one year would never be billed ever, or in the best case
|
||||
// they would be billed a few months too late
|
||||
if($contract->billing_period > 1 && $bill_date > $contract_finish_date) {
|
||||
$new_bill_date = clone $contract_finish_date;
|
||||
$new_bill_date->modify("+".$contract->billing_period." months");
|
||||
|
||||
while($new_bill_date->format("Ymd") < $bill_date->format("Ymd")) {
|
||||
$new_bill_date->modify("+".$contract->billing_period." months");
|
||||
}
|
||||
|
||||
if($new_bill_date->format("Ymd") > $bill_date->format("Ymd")) {
|
||||
$new_bill_date->modify("-".$contract->billing_period." months");
|
||||
}
|
||||
|
||||
$bill_date = $new_bill_date;
|
||||
//echo "new bill_date: ".$bill_date->format("Y-m-d");
|
||||
}
|
||||
$create_date = clone $bill_date;
|
||||
$create_date->modify("first day of this month");
|
||||
$create_date->setTime(0,0,0);
|
||||
$last_create_date = false;
|
||||
|
||||
$earliest_bill_date = clone $contract_finish_date;
|
||||
$earliest_bill_date->modify("first day of this month");
|
||||
$earliest_bill_date->setTime(0,0,0);
|
||||
|
||||
while($create_date->format("Y-m-d") >= $earliest_bill_date->format("Y-m-d")) {
|
||||
if($last_create_date) {
|
||||
// must for safety / shouldn't happen
|
||||
// just for safety / shouldn't happen
|
||||
die("need-date ran out of dates");
|
||||
}
|
||||
|
||||
//echo " ";
|
||||
//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) {
|
||||
// this is the finish month, so set day back to day of finish_date
|
||||
$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");
|
||||
$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;
|
||||
}*//*
|
||||
$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" => 0 // set Setup price to 0, because it was billed already
|
||||
"price_setup" => $price_setup // 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"];
|
||||
$end_date = (array_key_exists("end_date", $bill_data)) ? $bill_data["end_date"] : null;
|
||||
$price_setup = $bill_data["price_setup"];
|
||||
|
||||
if($contract->billing_period > 1 && $start_date < $yearly_not_before) {
|
||||
// XXX only for 1st Billing after IVT Import
|
||||
$this->log->debug(__METHOD__.": Ignoring Contract ".$contract->id." with start_date ".$start_date->format("Y-m-d")." because is yearly and before ".$yearly_not_before->format("Y-m-d"));
|
||||
continue;
|
||||
}
|
||||
|
||||
// if contract has cancel date this month
|
||||
// use cancel date as end_date
|
||||
if ($cancel_date) {
|
||||
$end_date = clone $cancel_date;
|
||||
} else {
|
||||
} elseif(!$end_date) {
|
||||
// else calculate last of month
|
||||
$end_date = clone $start_date;
|
||||
$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");
|
||||
}
|
||||
|
||||
|
||||
|
||||
$sday = $start_date->format("d");
|
||||
$eday = $end_date->format("d");
|
||||
|
||||
if ($sday > 1 || $cancel_date) {
|
||||
if ($contract->price && ($sday > 1 || $cancel_date)) {
|
||||
// aliquoter preis
|
||||
$days = ($eday - $sday) + 1;
|
||||
//echo "days: $days<br />";
|
||||
$pc = $days / $eday * 100;
|
||||
//echo "pc: $pc<br />";
|
||||
|
||||
$first_of_period = clone $start_date;
|
||||
$first_of_period->modify("first day of this month");
|
||||
|
||||
$total_days = $end_date->diff($first_of_period)->format("%a") + 1;
|
||||
$period_days = ($end_date->diff($start_date)->format("%a")) + 1;
|
||||
|
||||
$pc = $period_days / $total_days * 100;
|
||||
$price = round($contract->price / 100 * $pc, 4);
|
||||
|
||||
/*if($contract->id == 8766) {
|
||||
echo "start date: ".$start_date->format("Y-m-d H:i:s") . "<br>";
|
||||
echo "end date: ".$end_date->format("Y-m-d H:i:s") . "<br><br>";
|
||||
echo "first_of_period: " . $first_of_period->format("Y-m-d H:i:s") . "<br>";
|
||||
echo "total days: $total_days<br>";
|
||||
echo "period days: $period_days<br>";
|
||||
echo "price: $price<br><br>";
|
||||
echo "classic days: $days<br>";
|
||||
exit;
|
||||
}*/
|
||||
} else {
|
||||
$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;
|
||||
$billingaddress = $contract->billingaddress;
|
||||
@@ -334,6 +348,7 @@ class BillingController extends mfBaseController {
|
||||
$data["contract_id"] = $contract->id;
|
||||
$data["start_date"] = $start_date->format("Y-m-d");
|
||||
$data["end_date"] = $end_date->format("Y-m-d");
|
||||
$data["owner_id"] = $contract->owner_id;
|
||||
$data["billingaddress_id"] = ($contract->billingaddress_id) ? $contract->billingaddress_id : $contract->owner_id;
|
||||
$data["customer_number"] = $contract->owner->customer_number;
|
||||
$data["company"] = $billingaddress->company;
|
||||
@@ -351,13 +366,14 @@ 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->mathcode;
|
||||
$data["matchcode"] = $contract->matchcode;
|
||||
$data["product_id"] = $contract->product_id;
|
||||
$data["product_name"] = $contract->product_name;
|
||||
$data["product_info"] = $contract->product_info;
|
||||
$data["amount"] = $contract->amount;
|
||||
$data["price"] = $price;
|
||||
$data["price_setup"] = $price_setup;
|
||||
$data["vatrate"] = $contract->vatrate;
|
||||
$data["billing_period"] = $contract->billing_period;
|
||||
|
||||
$billing = BillingModel::create($data);
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
class BillingModel {
|
||||
public $invoice_id;
|
||||
public $invoice_date;
|
||||
public $contract_id;
|
||||
public $start_date;
|
||||
public $end_date;
|
||||
public $owner_id;
|
||||
public $billingaddress_id;
|
||||
public $customer_number;
|
||||
public $company;
|
||||
@@ -30,8 +30,8 @@ class BillingModel {
|
||||
public $amount;
|
||||
public $price;
|
||||
public $price_setup;
|
||||
public $billing_period;
|
||||
|
||||
public $vatrate;
|
||||
|
||||
public $create_by;
|
||||
public $edit_by;
|
||||
public $create;
|
||||
@@ -117,6 +117,31 @@ class BillingModel {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getInvoiceBaseData($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$items = [];
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT owner_id, billingaddress_id, billing_type, billing_delivery FROM Billing
|
||||
WHERE $where
|
||||
GROUP BY owner_id, billingaddress_id, billing_type, billing_delivery
|
||||
ORDER BY owner_id, billingaddress_id, billing_type, billing_delivery";
|
||||
//var_dump($sql);exit;
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = [
|
||||
"owner_id" => $data->owner_id,
|
||||
"billingaddress_id" => $data->billingaddress_id,
|
||||
"billing_type" => $data->billing_type,
|
||||
"billing_delivery" => $data->billing_delivery
|
||||
];
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function count($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
@@ -196,16 +221,11 @@ class BillingModel {
|
||||
$invoice_id = $filter['invoice_id'];
|
||||
if(is_numeric($invoice_id)) {
|
||||
$where .= " AND Billing.invoice_id=$invoice_id";
|
||||
} elseif($invoice_id === null || $invoice_id === false) {
|
||||
$where .= " AND (Billing.invoice_id IS NULL OR Billing.invoice_id=0)";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("invoice_date", $filter)) {
|
||||
$invoice_date = $filter['invoice_date'];
|
||||
if($invoice_date) {
|
||||
$where .= " AND Billing.invoice_date='$invoice_date'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("contract_id", $filter)) {
|
||||
$contract_id = $filter['contract_id'];
|
||||
if(is_numeric($contract_id)) {
|
||||
@@ -237,7 +257,7 @@ 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'";
|
||||
$where .= " AND Billing.start_date >= '$start_date'";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,6 +268,13 @@ class BillingModel {
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("owner_id", $filter)) {
|
||||
$owner_id = $filter['owner_id'];
|
||||
if(is_numeric($owner_id)) {
|
||||
$where .= " AND Billing.owner_id=$owner_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("billingaddress_id", $filter)) {
|
||||
$billingaddress_id = $filter['billingaddress_id'];
|
||||
if(is_numeric($billingaddress_id)) {
|
||||
@@ -384,10 +411,24 @@ class BillingModel {
|
||||
}
|
||||
|
||||
|
||||
if(array_key_exists("billing_period", $filter)) {
|
||||
$billing_period = $filter['billing_period'];
|
||||
if(is_numeric($billing_period)) {
|
||||
$where .= " AND Billing.billing_period = $billing_period";
|
||||
if(array_key_exists("billing_type", $filter)) {
|
||||
$billing_type = $filter['billing_type'];
|
||||
if(is_numeric($billing_type)) {
|
||||
$where .= " AND Billing.billing_type = $billing_type";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("billing_type", $filter)) {
|
||||
$billing_type = $db->escape($filter['billing_type']);
|
||||
if($billing_type) {
|
||||
$where .= " AND Billing.billing_type = '$billing_type'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("billing_delivery", $filter)) {
|
||||
$billing_delivery = $db->escape($filter['billing_delivery']);
|
||||
if($billing_delivery) {
|
||||
$where .= " AND Billing.billing_delivery = '$billing_delivery'";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user