Files
thetool/application/IvtBill/IvtBillModel.php
2024-07-09 18:50:08 +02:00

160 lines
4.6 KiB
PHP

<?php
class IvtBillModel {
public static function create(Array $data) {
throw new Exception("Please use IvtBillController to create IvtBills");
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$res = $db->select("bills", "*", "1=1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtBill($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
$res = $db->select("bills", "*", "$where ORDER BY id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtBill($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getLast($filter = []) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
mfLoghandler::singleton()->debug($where);
$res = $db->select("bills", "*", "$where ORDER BY id DESC LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtBill($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM bills WHERE $where ORDER by id";
mfLoghandler::singleton()->debug($sql);
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'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtBill($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("cid", $filter)) {
$cid = $filter['cid'];
if(is_numeric($cid)) {
$where .= " AND bills.cid=$cid";
}
}
if(array_key_exists("date_outgoing", $filter)) {
$date_outgoing = $filter['date_outgoing'];
if($date_outgoing) {
$where .= " AND bills.date_outgoing='$date_outgoing'";
}
}
if(array_key_exists("date_outgoing>", $filter)) {
$date_outgoing = $filter['date_outgoing>'];
if($date_outgoing) {
$where .= " AND bills.date_outgoing >= '$date_outgoing'";
}
}
if(array_key_exists("date_outgoing<", $filter)) {
$date_outgoing = $filter['date_outgoing<'];
if($date_outgoing) {
$where .= " AND bills.date_outgoing <= '$date_outgoing'";
}
}
if(array_key_exists("iban", $filter)) {
$iban = $filter['iban'];
if($iban === true) {
$where .= " AND bills.iban IS NOT NULL AND bills.iban <> ''";
} elseif ($iban === null || $iban === false) {
$where .= " AND (bills.iban IS NULL OR bills.iban = '')";
} else {
$iban = FronkDB::singleton()->escape($filter["iban"]);
$where .= " AND bills.iba='$iban' ";
}
}
if(array_key_exists("bic", $filter)) {
$bic = $filter['bic'];
if($bic === true) {
$where .= " AND bills.bic IS NOT NULL AND bills.bic <> ''";
} elseif ($bic === null || $bic === false) {
$where .= " AND (bills.bic IS NULL OR bills.bic = '')";
} else {
$bic = FronkDB::singleton()->escape($filter["bic"]);
$where .= " AND bills.iba='$bic' ";
}
}
/*
if(array_key_exists("status_id", $filter)) {
$status_id = $filter['status_id'];
if(is_numeric($status_id)) {
$where .= " AND IvtBill.status_id=$status_id";
}
}
if(array_key_exists("street", $filter)) {
$street = FronkDB::singleton()->escape($filter["street"]);
if($street) {
$where .= " AND street like '%$street%'";
}
}
*/
//var_dump($filter, $where);exit;
return $where;
}
}