WIP Contract/Billing 2024-06-20
This commit is contained in:
@@ -219,7 +219,7 @@ class AddressModel {
|
||||
if (is_array($limit) && count($limit)) {
|
||||
if (is_numeric($limit['start']) && is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT " . $limit['start'] . ", " . $limit['count'];
|
||||
} elseif (is_numeric($count)) {
|
||||
} elseif (is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT " . $limit['count'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,28 +34,14 @@ class AddresstypeModel {
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function getOne($id) {
|
||||
if(!is_numeric($id) || !$id) {
|
||||
throw new Exception("Invalid number", 400);
|
||||
}
|
||||
$item = [];
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select($this->table, "*", "id=$id LIMIT 1");
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new Addresstype($data);
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
|
||||
public static function getAll() {
|
||||
$items = [];
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select($this->table, "*");
|
||||
$res = $db->select("Addresstype", "*");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new Addresstype($data);
|
||||
|
||||
@@ -167,10 +167,12 @@ class Admin_IvtContractImport {
|
||||
|
||||
|
||||
$order_date = new DateTime($ivt_contract->created);
|
||||
$order_date->modify("+2 hours");
|
||||
$order_date->setTime(0,0,0);
|
||||
|
||||
$finish_date = new DateTime($ivt_contract->lastdate);
|
||||
$finish_date->setDate($finish_date->format("Y"), $finish_date->format("m"), 1);
|
||||
$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 = [];
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
class Billing extends mfBaseModel {
|
||||
protected $forcestr = ["product_name","product_info","matchcode"];
|
||||
|
||||
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
|
||||
|
||||
$classname = ucfirst($name);
|
||||
$idfield = $name."_id";
|
||||
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
|
||||
if(!$this->$name) {
|
||||
$this->$name = new $classname($this->$idfield);
|
||||
}
|
||||
|
||||
if($this->$name->id) {
|
||||
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
|
||||
return $this->$name;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $this->$name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?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 (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;
|
||||
|
||||
foreach(ContractModel::searchActive(["finish_date<" => $today->getTimestamp()]) as $contract) {
|
||||
//var_dump($contract);exit;
|
||||
|
||||
$now_year = date("Y");
|
||||
$now_month = date("m");
|
||||
$now_day = date("d");
|
||||
|
||||
$finish_year = date("Y", $contract->finish_date);
|
||||
$finish_month = date("m", $contract->finish_date);
|
||||
$finish_day = date("d", $contract->finish_date);
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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->setTime(2,0,0);
|
||||
|
||||
$price_setup = $contract->price_setup;
|
||||
} else {
|
||||
// Concurrent Billing
|
||||
// start_date next date after previous end_date
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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("+".$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;
|
||||
$pc = $days / $eday * 100;
|
||||
$price = round($contract->price / 100 * $pc, 4);
|
||||
} else {
|
||||
$price = $contract->price;
|
||||
}
|
||||
|
||||
$owner = $contract->owner;
|
||||
$billingaddress = $contract->billingaddress;
|
||||
|
||||
$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"] = $billingaddress->billing_type;
|
||||
$data["billing_delivery"] = $billingaddress->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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
<?php
|
||||
|
||||
class BillingModel {
|
||||
public $invoice_id;
|
||||
public $invoice_date;
|
||||
public $contract_id;
|
||||
public $start_date;
|
||||
public $end_date;
|
||||
public $billingaddress_id;
|
||||
public $customer_number;
|
||||
public $company;
|
||||
public $firstname;
|
||||
public $lastname;
|
||||
public $street;
|
||||
public $zip;
|
||||
public $city;
|
||||
public $country;
|
||||
public $email;
|
||||
public $uid;
|
||||
public $billing_type;
|
||||
public $billing_delivery;
|
||||
public $bank_account_bank;
|
||||
public $bank_account_owner;
|
||||
public $bank_account_iban;
|
||||
public $bank_account_bic;
|
||||
public $matchcode;
|
||||
public $product_id;
|
||||
public $product_name;
|
||||
public $product_info;
|
||||
public $amount;
|
||||
public $price;
|
||||
public $price_setup;
|
||||
public $billing_period;
|
||||
|
||||
public $create_by;
|
||||
public $edit_by;
|
||||
public $create;
|
||||
public $edit;
|
||||
|
||||
|
||||
public static function create(Array $data) {
|
||||
$model = new Billing();
|
||||
|
||||
foreach($data as $field => $value) {
|
||||
if(property_exists(get_called_class(), $field)) {
|
||||
$model ->$field = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
|
||||
if($model->create_by === null) {
|
||||
$model->create_by = $me->id;
|
||||
}
|
||||
if($model->edit_by === null) {
|
||||
$model->edit_by = $me->id;
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function getAll() {
|
||||
$items = [];
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("Billing", "*", "1 = 1 ORDER BY billingaddress_id");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new Billing($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function getFirst($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT * FROM Billing
|
||||
WHERE $where
|
||||
ORDER BY billingaddress_id LIMIT 1";
|
||||
//var_dump($sql);exit;
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new Billing($data);
|
||||
if($item->id) {
|
||||
return $item;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getLast($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT * FROM Billing
|
||||
WHERE $where
|
||||
ORDER BY `create` DESC LIMIT 1";
|
||||
//var_dump($sql);exit;
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new Billing($data);
|
||||
if($item->id) {
|
||||
return $item;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function count($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT COUNT(*) as cnt FROM Billing
|
||||
WHERE $where";
|
||||
|
||||
mfLoghandler::singleton()->debug($sql);
|
||||
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
return $data->cnt;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function search($filter, $limit = false) {
|
||||
//var_dump($filter);exit;
|
||||
$items = [];
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT * FROM Billing
|
||||
WHERE $where
|
||||
ORDER BY billingaddress_id";
|
||||
|
||||
if(is_array($limit) && count($limit)) {
|
||||
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
|
||||
} elseif(is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT ".$limit['count'];
|
||||
}
|
||||
}
|
||||
|
||||
mfLoghandler::singleton()->debug($sql);
|
||||
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[$data->id] = new Billing($data);
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
//var_dump($filter);exit;
|
||||
|
||||
if(array_key_exists("id", $filter)) {
|
||||
$id = $filter['id'];
|
||||
if(is_numeric($id)) {
|
||||
$where .= " AND Billing.id like '%$id%'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("approved", $filter)) {
|
||||
$approved = $filter['approved'];
|
||||
if($approved) {
|
||||
$where .= " AND Billing.approved = 1";
|
||||
} else {
|
||||
$where .= " AND Billing.approved = 0";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("invoice_id", $filter)) {
|
||||
$invoice_id = $filter['invoice_id'];
|
||||
if(is_numeric($invoice_id)) {
|
||||
$where .= " AND Billing.invoice_id=$invoice_id";
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
$where .= " AND Billing.contract_id=$contract_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("billingaddress_id", $filter)) {
|
||||
$billingaddress_id = $filter['billingaddress_id'];
|
||||
if(is_numeric($billingaddress_id)) {
|
||||
$where .= " AND Billing.billingaddress_id=$billingaddress_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("customer_number", $filter)) {
|
||||
$customer_number = $filter['customer_number'];
|
||||
if(is_numeric($customer_number)) {
|
||||
$where .= " AND Billing.customer_number LIKE $customer_number";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("company", $filter)) {
|
||||
$company = FronkDB::singleton()->escape($filter["company"]);
|
||||
if ($company) {
|
||||
$where .= " AND company like '%$company%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("firstname", $filter)) {
|
||||
$firstname = FronkDB::singleton()->escape($filter["firstname"]);
|
||||
if ($firstname) {
|
||||
$where .= " AND firstname like '%$firstname%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("lastname", $filter)) {
|
||||
$lastname = FronkDB::singleton()->escape($filter["lastname"]);
|
||||
if ($lastname) {
|
||||
$where .= " AND lastname like '%$lastname%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("mergedName", $filter)) {
|
||||
$name = FronkDB::singleton()->escape($filter["mergedName"]);
|
||||
if ($name) {
|
||||
$where .= " AND (CONCAT(firstname, ' ', lastname) like '%$name%' OR CONCAT(lastname, ' ', firstname) like '%$name%' )";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("street", $filter)) {
|
||||
$street = FronkDB::singleton()->escape($filter["street"]);
|
||||
if ($street) {
|
||||
$where .= " AND street like '%$street%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("zip", $filter)) {
|
||||
$zip = FronkDB::singleton()->escape($filter["zip"]);
|
||||
if ($zip) {
|
||||
$where .= " AND zip like '%$zip%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("city", $filter)) {
|
||||
$city = FronkDB::singleton()->escape($filter["city"]);
|
||||
if ($city) {
|
||||
$where .= " AND city like '%$city%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("country", $filter)) {
|
||||
$country = FronkDB::singleton()->escape($filter["country"]);
|
||||
if ($country) {
|
||||
$where .= " AND country like '%$country%'";
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists("email", $filter)) {
|
||||
$email = FronkDB::singleton()->escape($filter["email"]);
|
||||
if ($email) {
|
||||
$where .= " AND email like '%$email%'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("matchcode", $filter)) {
|
||||
$matchcode = FronkDB::singleton()->escape($filter["matchcode"]);
|
||||
if($matchcode) {
|
||||
$where .= " AND matchcode like '%$matchcode%'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("product_id", $filter)) {
|
||||
$product_id = $filter['product_id'];
|
||||
if(is_numeric($product_id)) {
|
||||
$where .= " AND Billing.product_id=$product_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("product_name", $filter)) {
|
||||
$product_name = $db->escape($filter['product_name']);
|
||||
if($product_name) {
|
||||
$where .= " AND product_name like '%$product_name%')";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("matchcode", $filter)) {
|
||||
$matchcode = $db->escape($filter['matchcode']);
|
||||
if($matchcode) {
|
||||
$where .= " AND Billing.`matchcode` like '%$matchcode%'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("add-where", $filter)) {
|
||||
$where .= " ".$filter['add-where'];
|
||||
}
|
||||
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -184,10 +184,6 @@ class ContractModel {
|
||||
return $contract;
|
||||
}
|
||||
|
||||
public function savePrecontract($contract) {
|
||||
|
||||
}
|
||||
|
||||
public static function getAll() {
|
||||
$items = [];
|
||||
|
||||
@@ -475,6 +471,13 @@ class ContractModel {
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("finish_date", $filter)) {
|
||||
$finish_date = $filter['finish_date'];
|
||||
if(is_numeric($finish_date)) {
|
||||
$where .= " AND Contract.finish_date = $finish_date";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("finish_date>", $filter)) {
|
||||
$finish_date = $filter['finish_date>'];
|
||||
if(is_numeric($finish_date)) {
|
||||
@@ -488,6 +491,20 @@ class ContractModel {
|
||||
$where .= " AND Contract.finish_date <= $finish_date";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("cancel_date>", $filter)) {
|
||||
$cancel_date = $filter['cancel_date>'];
|
||||
if(is_numeric($cancel_date)) {
|
||||
$where .= " AND Contract.cancel_date >= $cancel_date";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("cancel_date<", $filter)) {
|
||||
$cancel_date = $filter['cancel_date<'];
|
||||
if(is_numeric($cancel_date)) {
|
||||
$where .= " AND Contract.cancel_date <= $cancel_date";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("add-where", $filter)) {
|
||||
$where .= " ".$filter['add-where'];
|
||||
|
||||
Reference in New Issue
Block a user