$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("Contract", "*", "1 = 1 ORDER BY owner_id,product_id,`create`"); if($db->num_rows($res)) { while($data = $db->fetch_object($res)) { $items[] = new Contract($data); } } return $items; } public static function getFirst($filter) { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $sql = "SELECT Contract.* FROM Contract LEFT JOIN Address ON (Contract.owner_id = Address.id) LEFT JOIN OrderProduct ON (Contract.orderproduct_id = OrderProduct.id) LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id) LEFT JOIN Product ON (Contract.product_id = Product.id) WHERE $where GROUP BY Contract.id ORDER BY Contract.owner_id,Contract.product_id,Contract.`create` LIMIT 1"; //var_dump($sql);exit; $res = $db->query($sql); if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Contract($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 Contract LEFT JOIN Address ON (Contract.owner_id = Address.id) LEFT JOIN OrderProduct ON (Contract.orderproduct_id = OrderProduct.id) LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id) LEFT JOIN Product ON (Contract.product_id = Product.id) WHERE $where GROUP BY Contract.id"; $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 Contract.* FROM Contract LEFT JOIN Address ON (Contract.owner_id = Address.id) LEFT JOIN OrderProduct ON (Contract.orderproduct_id = OrderProduct.id) LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id) LEFT JOIN Product ON (Contract.product_id = Product.id) WHERE $where GROUP BY Contract.id ORDER BY Contract.owner_id,Contract.product_id,Contract.`create`"; 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)) { $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 Contract($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 Contract.id like '%$id%'"; } } if(array_key_exists("owner_id", $filter)) { $owner_id = $filter['owner_id']; if(is_numeric($owner_id)) { $where .= " AND Contract.owner_id=$owner_id"; } } if(array_key_exists("product_id", $filter)) { $product_id = $filter['product_id']; if(is_numeric($product_id)) { $where .= " AND Contract.product_id=$product_id"; } } if(array_key_exists("orderproduct_id", $filter)) { $orderproduct_id = $filter['orderproduct_id']; if(is_numeric($orderproduct_id)) { $where .= " AND Contract.orderproduct_id=$orderproduct_id"; } } if(array_key_exists("product_name", $filter)) { $product_name = $db->escape($filter['product_name']); if($product_name) { $where .= " AND (Contract.`product_name` like '%$product_name%' OR Product.name like '%$product_name%')"; } } if(array_key_exists("matchcode", $filter)) { $matchcode = $db->escape($filter['matchcode']); if($matchcode) { $where .= " AND Contract.`matchcode` like '%$matchcode%'"; } } if(array_key_exists("customer_number", $filter)) { $customer_number = $filter['customer_number']; if(is_numeric($customer_number)) { $where .= " AND Address.customer_number=$customer_number"; } } if(array_key_exists("owner", $filter)) { $owner = FronkDB::singleton()->escape($filter["owner"]); if($owner) { $where .= " AND (Address.company like '%$owner%' OR (CONCAT(Address.firstname, ' ', Address.lastname) like '%$owner%' OR CONCAT(Address.lastname, ' ', Address.firstname) like '%$owner%' ))"; } } if(array_key_exists("add-where", $filter)) { $where .= " ".$filter['add-where']; } //var_dump($filter, $where);exit; return $where; } }