Files
thetool/application/BillingVoicenumber/BillingVoicenumberModel.php
2024-07-07 12:04:51 +02:00

272 lines
7.9 KiB
PHP

<?php
class BillingVoicenumberModel {
public $billing_id;
public $contract_id;
public $voicenumber;
public $start_date;
public $end_date;
public $voiceplan;
public $zone;
public $call_count;
public $duration;
public $price;
public $price_total;
public $increment;
public $increment_first;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new BillingVoicenumber();
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("BillingVoicenumber", "*", "1 = 1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new BillingVoicenumber($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM BillingVoicenumber
WHERE $where
ORDER BY id LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new BillingVoicenumber($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 BillingVoicenumber
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 BillingVoicenumber($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 BillingVoicenumber
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 = "id ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM BillingVoicenumber
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 BillingVoicenumber($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 BillingVoicenumber.id like '%$id%'";
}
}
if(array_key_exists("billing_id", $filter)) {
$billing_id = $filter['billing_id'];
if(is_numeric($billing_id)) {
$where .= " AND BillingVoicenumber.billing_id=$billing_id";
} elseif($billing_id === null || $billing_id === false) {
$where .= " AND (BillingVoicenumber.billing_id IS NULL OR BillingVoicenumber.billing_id=0)";
}
}
if(array_key_exists("contract_id", $filter)) {
$contract_id = $filter['contract_id'];
if(is_numeric($contract_id)) {
$where .= " AND BillingVoicenumber.contract_id=$contract_id";
}
}
if(array_key_exists("voicenumber", $filter)) {
$voicenumber = FronkDB::singleton()->escape($filter['voicenumber']);
if($voicenumber) {
$where .= " AND BillingVoicenumber.voicenumber='$voicenumber'";
}
}
if(array_key_exists("start_date", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date='$start_date'";
}
}
if(array_key_exists("start_date>", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date > '$start_date'";
}
}
if(array_key_exists("start_date<", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date < '$start_date'";
}
}
if(array_key_exists("start_date>=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>=']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date >= '$start_date'";
}
}
if(array_key_exists("start_date<=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<=']);
if($start_date) {
$where .= " AND BillingVoicenumber.start_date <= '$start_date'";
}
}
if(array_key_exists("owner_id", $filter)) {
$owner_id = $filter['owner_id'];
if(is_numeric($owner_id)) {
$where .= " AND BillingVoicenumber.owner_id=$owner_id";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
if(array_key_exists("BillingVoicenumber_type", $filter)) {
$BillingVoicenumber_type = $filter['BillingVoicenumber_type'];
if(is_numeric($BillingVoicenumber_type)) {
$where .= " AND BillingVoicenumber.BillingVoicenumber_type = $BillingVoicenumber_type";
}
}
if(array_key_exists("BillingVoicenumber_type", $filter)) {
$BillingVoicenumber_type = $db->escape($filter['BillingVoicenumber_type']);
if($BillingVoicenumber_type) {
$where .= " AND BillingVoicenumber.BillingVoicenumber_type = '$BillingVoicenumber_type'";
}
}
if(array_key_exists("BillingVoicenumber_delivery", $filter)) {
$BillingVoicenumber_delivery = $db->escape($filter['BillingVoicenumber_delivery']);
if($BillingVoicenumber_delivery) {
$where .= " AND BillingVoicenumber.BillingVoicenumber_delivery = '$BillingVoicenumber_delivery'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}