600 lines
19 KiB
PHP
600 lines
19 KiB
PHP
<?php
|
|
|
|
class InvoiceModel {
|
|
public $invoice_number;
|
|
public $invoice_date;
|
|
public $owner_id;
|
|
public $billingaddress_id;
|
|
public $customer_number;
|
|
public $fibu_account_number;
|
|
public $sepa_date;
|
|
public $sepa_id;
|
|
public $sepa_last_date;
|
|
public $fibu_cost_area;
|
|
public $fibu_cost_account;
|
|
public $fibu_cost_account_legacy;
|
|
public $fibu_taxcode;
|
|
public $tax_text;
|
|
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 $total;
|
|
public $total_gross;
|
|
public $vatgroup_id;
|
|
public $bmd_export_date;
|
|
public $date_delivered;
|
|
public $total_vat;
|
|
public $create_by;
|
|
public $edit_by;
|
|
public $create;
|
|
public $edit;
|
|
|
|
|
|
public static function create(Array $data) {
|
|
$model = new Invoice();
|
|
|
|
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 getNextInvoiceNUmber() {
|
|
$last_invoice_num = self::getLastInvoiceNumber();
|
|
|
|
if(!$last_invoice_num) {
|
|
return TT_FIRST_INVOICE_NUM;
|
|
}
|
|
|
|
$year_part = 0;
|
|
$num_part = 0;
|
|
|
|
$m = [];
|
|
if(preg_match('/^RN(\d+)-X(\d+)$/', $last_invoice_num, $m)) {
|
|
if(array_key_exists(1, $m)) {
|
|
$year_part = $m[1];
|
|
if(array_key_exists(2, $m)) {
|
|
$num_part = $m[2];
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!$year_part || !$num_part) {
|
|
die("NO LAST INVOICE NUMBER!!!");
|
|
}
|
|
|
|
//var_dump($year_part, $num_part);exit;
|
|
if(date("Y") == $year_part) {
|
|
$new_year_part = $year_part;
|
|
$new_num_part = $num_part + 1;
|
|
} else {
|
|
$new_year_part = date("Y");
|
|
$new_num_part = 1;
|
|
}
|
|
|
|
$new_invoice_num = "RN$new_year_part-X".str_pad($new_num_part,"6", "0", STR_PAD_LEFT);
|
|
return $new_invoice_num;
|
|
}
|
|
|
|
public static function getLastInvoiceNumber() {
|
|
$last_invoice = self::getLast(["invoice_number" => true]);
|
|
if(!$last_invoice || !$last_invoice->invoice_number) {
|
|
return false;
|
|
}
|
|
return $last_invoice->invoice_number;
|
|
}
|
|
|
|
public static function getSumPrice($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
if(array_key_exists("price>=", $filter)) unset($filter["price>="]);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT SUM(total) as p FROM Invoice WHERE total > 0 AND $where";
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
return $data->p;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static function getSumGrossPrice($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
if(array_key_exists("price>=", $filter)) unset($filter["price>="]);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT SUM(total_gross) as p FROM Invoice WHERE total_gross > 0 AND $where";
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
return $data->p;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static function getSumCreditPrice($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
if(array_key_exists("price>=", $filter)) unset($filter["price>="]);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT SUM(total) as p FROM Invoice WHERE total < 0 AND $where";
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
return $data->p;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static function getSumCreditGrossPrice($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
if(array_key_exists("price>=", $filter)) unset($filter["price>="]);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT SUM(total_gross) as p FROM Invoice WHERE total_gross < 0 AND $where";
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
return $data->p;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static function getSumVoicecallsPrice($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
if(array_key_exists("price>=", $filter)) unset($filter["price>="]);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT SUM(Invoiceposition.price) as p FROM Invoiceposition
|
|
LEFT JOIN Invoice ON (Invoice.id = Invoiceposition.invoice_id)
|
|
WHERE Invoiceposition.product_name LIKE 'Gesprächsgebühren %'
|
|
AND $where
|
|
";
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
return $data->p;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static function getSumVoicecallsGrossPrice($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
if(array_key_exists("price>=", $filter)) unset($filter["price>="]);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT SUM(Invoiceposition.price_gross) as p FROM Invoiceposition
|
|
LEFT JOIN Invoice ON (Invoice.id = Invoiceposition.invoice_id)
|
|
WHERE Invoiceposition.product_name LIKE 'Gesprächsgebühren %'
|
|
AND $where
|
|
";
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
return $data->p;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
public static function getAll() {
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$sql = "SELECT Invoice.* FROM Invoice
|
|
LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id)
|
|
ORDER BY invoice_number";
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new Invoice($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getFirst($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT Invoice.* FROM Invoice
|
|
LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id)
|
|
WHERE $where
|
|
ORDER BY invoice_number LIMIT 1";
|
|
//var_dump($sql);exit;
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new Invoice($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 Invoice.* FROM Invoice
|
|
LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id)
|
|
WHERE $where
|
|
ORDER BY invoice_number DESC LIMIT 1";
|
|
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new Invoice($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 Invoice
|
|
LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id)
|
|
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, $order = false) {
|
|
//var_dump($filter);exit;
|
|
$items = [];
|
|
|
|
if(!$order) {
|
|
$order = "invoice_number ASC";
|
|
}
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT Invoice.* FROM Invoice
|
|
LEFT JOIN InvoiceFile ON (InvoiceFile.invoice_id = Invoice.id)
|
|
WHERE $where
|
|
ORDER BY $order";
|
|
|
|
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 Invoice($data);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
//var_dump($filter);exit;
|
|
$db = FronkDB::singleton();
|
|
|
|
//var_dump($filter);exit;
|
|
|
|
if(array_key_exists("id", $filter)) {
|
|
$id = $filter['id'];
|
|
if(is_numeric($id)) {
|
|
$where .= " AND Invoice.id like '%$id%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("invoice_file", $filter)) {
|
|
$invoice_file = $filter['invoice_file'];
|
|
if($invoice_file === true) {
|
|
$where .= " AND InvoiceFile.id > 0'";
|
|
} elseif($invoice_file === false || $invoice_file === null) {
|
|
$where .= " AND InvoiceFile.id IS NULL";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("invoice_number", $filter)) {
|
|
$invoice_number = $filter['invoice_number'];
|
|
if($invoice_number === true) {
|
|
$where .= " AND Invoice.invoice_number IS NOT NULL AND Invoice.invoice_number <> ''";
|
|
} elseif($invoice_number) {
|
|
$invoice_number = $db->escape($invoice_number);
|
|
$where .= " AND Invoice.invoice_number='$invoice_number'";
|
|
} elseif($invoice_number === null || $invoice_number === false) {
|
|
$where .= " AND Invoice.invoice_number IS NULL";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("invoice_number%", $filter)) {
|
|
$invoice_number = $filter['invoice_number%'];
|
|
if($invoice_number) {
|
|
$where .= " AND Invoice.invoice_number LIKE '%$invoice_number%'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("invoice_date", $filter)) {
|
|
$invoice_date = $filter['invoice_date'];
|
|
if($invoice_date) {
|
|
$where .= " AND Invoice.invoice_date='$invoice_date'";
|
|
} elseif($invoice_date === null || $invoice_date === false) {
|
|
$where .= " AND Invoice.invoice_date IS NULL";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("invoice_date>", $filter)) {
|
|
$invoice_date = $db->escape($filter['invoice_date>']);
|
|
if($invoice_date) {
|
|
$where .= " AND Invoice.invoice_date > '$invoice_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("invoice_date>=", $filter)) {
|
|
$invoice_date = $db->escape($filter['invoice_date>=']);
|
|
if($invoice_date) {
|
|
$where .= " AND Invoice.invoice_date >= '$invoice_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("invoice_date<", $filter)) {
|
|
$invoice_date = $db->escape($filter['invoice_date<']);
|
|
if($invoice_date) {
|
|
$where .= " AND Invoice.invoice_date < '$invoice_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("invoice_date<=", $filter)) {
|
|
$invoice_date = $db->escape($filter['invoice_date<=']);
|
|
if($invoice_date) {
|
|
$where .= " AND Invoice.invoice_date <= '$invoice_date'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("bmd_export_date", $filter)) {
|
|
$bmd_export_date = $filter['bmd_export_date'];
|
|
if(is_numeric($bmd_export_date)) {
|
|
$where .= " AND Invoice.bmd_export_date=$bmd_export_date";
|
|
} elseif($bmd_export_date === null || $bmd_export_date === false) {
|
|
$where .= " AND Invoice.bmd_export_date IS NULL";
|
|
} elseif($bmd_export_date === true) {
|
|
$where .= " AND Invoice.bmd_export_date > 0";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("date_delivered", $filter)) {
|
|
$date_delivered = $filter['date_delivered'];
|
|
if(is_numeric($date_delivered)) {
|
|
$where .= " AND Invoice.date_delivered=$date_delivered";
|
|
} elseif($date_delivered === null || $date_delivered === false) {
|
|
$where .= " AND Invoice.date_delivered IS NULL";
|
|
} elseif($date_delivered === true) {
|
|
$where .= " AND Invoice.date_delivered > 0";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("owner_id", $filter)) {
|
|
$owner_id = $filter['owner_id'];
|
|
if(is_numeric($owner_id)) {
|
|
$where .= " AND Invoice.owner_id=$owner_id";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("billingaddress_id", $filter)) {
|
|
$billingaddress_id = $filter['billingaddress_id'];
|
|
if(is_numeric($billingaddress_id)) {
|
|
$where .= " AND Invoice.billingaddress_id=$billingaddress_id";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("owner_or_billingaddress_id", $filter)) {
|
|
$address_id = $filter['owner_or_billingaddress_id'];
|
|
if(is_numeric($address_id)) {
|
|
$where .= " AND (Invoice.owner_id=$address_id OR Invoice.billingaddress_id=$address_id)";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("customer_number", $filter)) {
|
|
$customer_number = $filter['customer_number'];
|
|
if(is_numeric($customer_number)) {
|
|
$where .= " AND Invoice.customer_number LIKE $customer_number";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("billing_type", $filter)) {
|
|
$billing_type = $db->escape($filter['billing_type']);
|
|
if($billing_type) {
|
|
$where .= " AND Invoice.billing_type LIKE '$billing_type'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("billing_delivery", $filter)) {
|
|
$billing_delivery = $db->escape($filter['billing_delivery']);
|
|
if($billing_delivery) {
|
|
$where .= " AND Invoice.billing_delivery LIKE '$billing_delivery'";
|
|
}
|
|
}
|
|
|
|
|
|
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("vatgroup_id", $filter)) {
|
|
$vatgroup_id = $filter['vatgroup_id'];
|
|
if(is_numeric($vatgroup_id)) {
|
|
$where .= " AND Invoice.vatgroup_id = '$vatgroup_id'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("!vatgroup_id", $filter)) {
|
|
$vatgroup_id = $filter['!vatgroup_id'];
|
|
if(is_numeric($vatgroup_id)) {
|
|
$where .= " AND Invoice.vatgroup_id <> '$vatgroup_id'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("sepa_id", $filter)) {
|
|
$sepa_id = $filter['sepa_id'];
|
|
if($sepa_id) {
|
|
$where .= " AND Invoice.sepa_id = '$sepa_id'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("fibu_account_number", $filter)) {
|
|
$fibu_account_number = $filter['fibu_account_number'];
|
|
if($fibu_account_number) {
|
|
$where .= " AND Invoice.fibu_account_number = '$fibu_account_number'";
|
|
}
|
|
}
|
|
|
|
|
|
if(array_key_exists("add-where", $filter)) {
|
|
$where .= " ".$filter['add-where'];
|
|
}
|
|
|
|
|
|
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
}
|