Files
thetool/application/Billing/BillingController.php
2024-06-25 12:19:35 +02:00

377 lines
15 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 (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;
$now_year = date("Y");
$now_month = date("m");
$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_month = date("m", $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;
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 = new DateTime("@".$contract->finish_date);
$start_date->setTimezone(new DateTimeZone("Europe/Vienna"));
// ignore yearly contracts which are not billable this month
if($contract->billing_period == 12) {
if($start_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 "&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
// use cancel date as end_date
if ($cancel_date) {
$end_date = clone $cancel_date;
} else {
// 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) {
// aliquoter preis
$days = ($eday - $sday) + 1;
//echo "days: $days<br />";
$pc = $days / $eday * 100;
//echo "pc: $pc<br />";
$price = round($contract->price / 100 * $pc, 4);
} 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;
$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["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->mathcode;
$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["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");
}
}