254 lines
7.1 KiB
PHP
254 lines
7.1 KiB
PHP
<?php
|
|
|
|
class InvoiceJobModel {
|
|
public $task;
|
|
public $from_date;
|
|
public $to_date;
|
|
public $started;
|
|
public $finished;
|
|
public $result;
|
|
public $create_by;
|
|
public $edit_by;
|
|
public $create;
|
|
public $edit;
|
|
|
|
|
|
public static function create(Array $data) {
|
|
$model = new InvoiceJob();
|
|
|
|
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("InvoiceJob", "*", "1 = 1 ORDER BY from_date");
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new InvoiceJob($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getFirst($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT * FROM InvoiceJob
|
|
WHERE $where
|
|
ORDER BY from_date LIMIT 1";
|
|
//var_dump($sql);exit;
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new InvoiceJob($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 InvoiceJob
|
|
WHERE $where
|
|
ORDER BY from_date DESC LIMIT 1";
|
|
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new InvoiceJob($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 InvoiceJob
|
|
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, $new_db_connection = false) {
|
|
//var_dump($filter);exit;
|
|
$items = [];
|
|
|
|
if(!$order) {
|
|
$order = "from_date ASC";
|
|
}
|
|
|
|
$db = FronkDB::singleton();
|
|
if($new_db_connection) {
|
|
$db->reconnect();
|
|
}
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT * FROM InvoiceJob
|
|
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 InvoiceJob($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("started", $filter)) {
|
|
$started = $filter['started'];
|
|
if($started === null || $started === false) {
|
|
$where .= " AND InvoiceJob.started IS NULL";
|
|
} elseif($started === true) {
|
|
$where .= " AND InvoiceJob.started > 0";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("finished", $filter)) {
|
|
$finished = $filter['finished'];
|
|
if($finished === null || $finished === false) {
|
|
$where .= " AND InvoiceJob.finished IS NULL";
|
|
} elseif($finished === true) {
|
|
$where .= " AND InvoiceJob.finished > 0";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("from_date>", $filter)) {
|
|
$from_date = $db->escape($filter['from_date>']);
|
|
if($from_date) {
|
|
$where .= " AND InvoiceJob.from_date > '$from_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("from_date>=", $filter)) {
|
|
$from_date = $db->escape($filter['from_date>=']);
|
|
if($from_date) {
|
|
$where .= " AND InvoiceJob.from_date >= '$from_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("from_date<", $filter)) {
|
|
$from_date = $db->escape($filter['from_date<']);
|
|
if($from_date) {
|
|
$where .= " AND InvoiceJob.from_date < '$from_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("from_date<=", $filter)) {
|
|
$from_date = $db->escape($filter['from_date<=']);
|
|
if($from_date) {
|
|
$where .= " AND InvoiceJob.from_date <= '$from_date'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("to_date>", $filter)) {
|
|
$to_date = $db->escape($filter['to_date>']);
|
|
if($to_date) {
|
|
$where .= " AND InvoiceJob.to_date > '$to_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("to_date>=", $filter)) {
|
|
$to_date = $db->escape($filter['to_date>=']);
|
|
if($to_date) {
|
|
$where .= " AND InvoiceJob.to_date >= '$to_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("to_date<", $filter)) {
|
|
$to_date = $db->escape($filter['to_date<']);
|
|
if($to_date) {
|
|
$where .= " AND InvoiceJob.to_date < '$to_date'";
|
|
}
|
|
}
|
|
if(array_key_exists("to_date<=", $filter)) {
|
|
$to_date = $db->escape($filter['to_date<=']);
|
|
if($to_date) {
|
|
$where .= " AND InvoiceJob.to_date <= '$to_date'";
|
|
}
|
|
}
|
|
|
|
if (array_key_exists("task", $filter)) {
|
|
$task = FronkDB::singleton()->escape($filter["task"]);
|
|
if ($task) {
|
|
$where .= " AND task='$task'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("vatgroup_id", $filter)) {
|
|
$vatgroup_id = $filter['vatgroup_id'];
|
|
if(is_numeric($vatgroup_id)) {
|
|
$where .= " AND InvoiceJob.vatgroup_id = '$vatgroup_id'";
|
|
}
|
|
}
|
|
|
|
|
|
if(array_key_exists("add-where", $filter)) {
|
|
$where .= " ".$filter['add-where'];
|
|
}
|
|
|
|
|
|
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
}
|