396 lines
16 KiB
PHP
396 lines
16 KiB
PHP
<?php
|
|
|
|
class BillingController extends mfBaseController {
|
|
|
|
protected function init()
|
|
{
|
|
$this->needlogin = true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
$this->layout()->set("me", $me);
|
|
|
|
if (!$me->is(["Admin"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
}
|
|
|
|
protected function indexAction() {
|
|
$this->layout()->setTemplate("Billing/Index");
|
|
|
|
if ($this->request->resetFilter) {
|
|
unset($_SESSION[MFAPPNAME . '-Billing-filter']);
|
|
}
|
|
|
|
$filter = [];
|
|
if (is_array($this->request->filter)) {
|
|
$filter = $this->request->filter;
|
|
$_SESSION[MFAPPNAME . '-Billing-filter'] = $filter;
|
|
} else {
|
|
if (array_key_exists(MFAPPNAME . '-Billing-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-Billing-filter'])) {
|
|
$filter = $_SESSION[MFAPPNAME . '-Billing-filter'];
|
|
}
|
|
}
|
|
|
|
$this->layout->set("filter", $filter);
|
|
$filter = $this->getPreparedFilter($filter);
|
|
|
|
// pagination defaults
|
|
$pagination = [];
|
|
$pagination['start'] = 0;
|
|
$pagination['count'] = 50;
|
|
$pagination['maxItems'] = 0;
|
|
|
|
if (is_numeric($this->request->s)) {
|
|
$pagination['start'] = intval($this->request->s);
|
|
}
|
|
//var_dump($filter);exit;
|
|
$pagination['maxItems'] = BillingModel::count($filter);
|
|
$billings = BillingModel::search($filter, $pagination);
|
|
|
|
$this->layout()->set("billings", $billings);
|
|
$this->layout()->set("pagination", $pagination);
|
|
|
|
}
|
|
|
|
private function getPreparedFilter($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(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;
|
|
}
|
|
}
|
|
|
|
return $new_filter;
|
|
}
|
|
|
|
protected function importContractsAction() {
|
|
$r = $this->request;
|
|
|
|
$today = new DateTime("now");
|
|
$today->setTime(0,0,0);
|
|
|
|
//$tomorrow = new DateTime("tomorrow");
|
|
//$tomorrow->setTime(0,0,0);
|
|
|
|
$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 = 6;
|
|
$now_day = 3; // XXX for debugging only, must be removed
|
|
|
|
// 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,1,0,$now_month, $now_day, $now_year), "cancel_date" => null]) as $contract) {
|
|
//while(!$stop) {
|
|
//$stop = true;
|
|
//$contract = new Contract(1475);
|
|
|
|
//var_dump($contract);exit;
|
|
$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);
|
|
$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");
|
|
}
|
|
|
|
$finish_year = $contract_finish_date->format("Y");
|
|
$finish_month = $contract_finish_date->format("m");
|
|
$finish_day = $contract_finish_date->format("d");
|
|
|
|
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;
|
|
if($contract->cancel_date) {
|
|
$cancel_date = new DateTime("@".$contract->cancel_date);
|
|
$cancel_date->setTime(0,0,0);
|
|
if($cancel_date->format("Y") != $now_year || $cancel_date->format("m") != $now_month) {
|
|
$cancel_date = false;
|
|
}
|
|
}
|
|
|
|
//$start_date = clone $contract_finish_date;
|
|
|
|
// ignore yearly contracts which are not billable this month
|
|
/*if($contract->billing_period == 12) {
|
|
if($contract_finish_date->format("m") != $bill_month) {
|
|
continue;
|
|
}
|
|
}*/
|
|
|
|
$create_bills = [];
|
|
|
|
// Concurrent Billing
|
|
// find not yet billed periods
|
|
|
|
if(!$contract->billing_period) {
|
|
// setup only
|
|
if(BillingModel::getFirst(["contract_id" => $contract->id])) {
|
|
continue;
|
|
}
|
|
|
|
$create_bills[] = [
|
|
"start_date" => $contract_finish_date,
|
|
"end_date" => $contract_finish_date,
|
|
"price_setup" => $contract->price_setup
|
|
];
|
|
} else {
|
|
// contracts with billing period
|
|
$create_dates = [];
|
|
|
|
//echo "initial bill_date: ".$bill_date->format("Y-m-d")."<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) {
|
|
// just for safety / shouldn't happen
|
|
die("need-date ran out of dates");
|
|
}
|
|
|
|
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, unless billing_period is more than 1 month
|
|
if($contract->billing_period == 1) {
|
|
$create_date->setDate($finish_year, $finish_month, $finish_day);
|
|
}
|
|
$last_create_date = true;
|
|
}
|
|
|
|
$existing_bill = BillingModel::getFirst(["contract_id" => $contract->id, "start_date" => $create_date->format("Y-m-d")]);
|
|
if(!$existing_bill) {
|
|
$new_create_date = clone $create_date;
|
|
$create_dates[] = $new_create_date;
|
|
$create_date->modify("-" . $contract->billing_period . " months");
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
// 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
|
|
];
|
|
}
|
|
}
|
|
|
|
$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;
|
|
} 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("-1 day");
|
|
}
|
|
|
|
|
|
|
|
$sday = $start_date->format("d");
|
|
$eday = $end_date->format("d");
|
|
|
|
if ($contract->price && ($sday > 1 || $cancel_date)) {
|
|
// aliquoter preis
|
|
$days = ($eday - $sday) + 1;
|
|
|
|
$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;
|
|
}
|
|
|
|
|
|
$owner = $contract->owner;
|
|
$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["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;
|
|
$data["firstname"] = $billingaddress->firstname;
|
|
$data["lastname"] = $billingaddress->lastname;
|
|
$data["street"] = $billingaddress->street;
|
|
$data["zip"] = $billingaddress->zip;
|
|
$data["city"] = $billingaddress->city;
|
|
$data["country"] = $billingaddress->country->name;
|
|
$data["email"] = $billingaddress->email;
|
|
$data["uid"] = $billingaddress->uid;
|
|
$data["billing_type"] = $billing_type;
|
|
$data["billing_delivery"] = $billing_delivery;
|
|
$data["bank_account_bank"] = $billingaddress->bank_account_bank;
|
|
$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;
|
|
$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);
|
|
if (!$billing->save()) {
|
|
var_dump($billing);
|
|
exit;
|
|
}
|
|
|
|
$i++;
|
|
}
|
|
|
|
}
|
|
$this->layout()->setFlash("$i Billing records generiert");
|
|
$this->redirect("Billing");
|
|
|
|
}
|
|
} |