411 lines
13 KiB
PHP
411 lines
13 KiB
PHP
<?php
|
|
|
|
class ContractLinkModel {
|
|
public $contract_id;
|
|
public $origin_contract_id;
|
|
public $type;
|
|
public $change_action;
|
|
|
|
public $create_by = null;
|
|
public $edit_by = null;
|
|
public $create = null;
|
|
public $edit = null;
|
|
|
|
|
|
public static function create(Array $data) {
|
|
$model = new ContractLink();
|
|
|
|
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("ContractLink", "*", "1 = 1 ORDER BY contract_id,origin_contract_id");
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new ContractLink($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getFirst($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT * FROM ContractLink
|
|
WHERE $where
|
|
ORDER BY contract_id,origin_contract_id
|
|
LIMIT 1";
|
|
//var_dump($sql);exit;
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new ContractLink($data);
|
|
if($item->id) {
|
|
return $item;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function includesContractId($contract_id, $filter=[]) {
|
|
$items = [];
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT * FROM ContractLink
|
|
WHERE (contract_id=$contract_id OR origin_contract_id=$contract_id)
|
|
AND $where
|
|
ORDER BY contract_id,origin_contract_id";
|
|
|
|
|
|
mfLoghandler::singleton()->debug($sql);
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[$data->id] = new ContractLink($data);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public static function countWithContracts($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT COUNT(*) as cnt FROM ContractLink
|
|
LEFT JOIN Contract ON (ContractLink.contract_id = Contract.id)
|
|
LEFT JOIN Address ON (Contract.owner_id = Address.id)
|
|
LEFT JOIN Worker ON (ContractLink.create_by = Worker.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
|
|
";
|
|
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
return $data->cnt;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public static function searchWithContracts($filter, $limit = false) {
|
|
$items = [];
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT ContractLink.* FROM ContractLink
|
|
LEFT JOIN Contract ON (ContractLink.contract_id = Contract.id)
|
|
LEFT JOIN Address ON (Contract.owner_id = Address.id)
|
|
LEFT JOIN Worker ON (ContractLink.create_by = Worker.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
|
|
ORDER BY Contract.`create`,Contract.id";
|
|
|
|
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 ContractLink($data);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public static function count($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT COUNT(*) FROM ContractLink
|
|
WHERE $where
|
|
";
|
|
|
|
$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 * FROM ContractLink
|
|
WHERE $where
|
|
ORDER BY contract_id,origin_contract_id";
|
|
|
|
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 ContractLink($data);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
//var_dump($filter);exit;
|
|
|
|
if(array_key_exists("contract_id", $filter)) {
|
|
$contract_id = $filter['contract_id'];
|
|
if(is_numeric($contract_id)) {
|
|
$where .= " AND ContractLink.contract_id = '$contract_id'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("origin_contract_id", $filter)) {
|
|
$origin_contract_id = $filter['origin_contract_id'];
|
|
if(is_numeric($origin_contract_id)) {
|
|
$where .= " AND ContractLink.origin_contract_id = '$origin_contract_id'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("type", $filter)) {
|
|
$type = $filter["type"];
|
|
if(is_array($type) && count($type)) {
|
|
$where .= " AND ContractLink.type IN ('".join("','", $type)."')";
|
|
} else {
|
|
$type = $db->escape($filter['type']);
|
|
if($type) {
|
|
$where .= " AND ContractLink.type = '$type'";
|
|
}
|
|
}
|
|
}
|
|
|
|
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("billingaddress_id", $filter)) {
|
|
$billingaddress_id = $filter['billingaddress_id'];
|
|
if(is_numeric($billingaddress_id)) {
|
|
$where .= " AND Contract.billingaddress_id=$billingaddress_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("rtr_code", $filter)) {
|
|
$rtr_code = $filter['rtr_code'];
|
|
if(is_numeric($rtr_code)) {
|
|
$where .= " AND Contract.rtr_code=$rtr_code";
|
|
}
|
|
}
|
|
|
|
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.customer_number like '$owner' OR Address.company like '%$owner%' OR (CONCAT(Address.firstname, ' ', Address.lastname) like '%$owner%' OR CONCAT(Address.lastname, ' ', Address.firstname) like '%$owner%' ))";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("imported_from", $filter)) {
|
|
$imported_from = FronkDB::singleton()->escape($filter["imported_from"]);
|
|
if($imported_from) {
|
|
$where .= " AND Contract.imported_from like '$imported_from'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("imported_data", $filter)) {
|
|
$imported_data = FronkDB::singleton()->escape($filter["imported_data"]);
|
|
if($imported_data) {
|
|
$where .= " AND Contract.imported_data like '$imported_data'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("price<", $filter)) {
|
|
$price = $filter['price<'];
|
|
if(is_numeric($price)) {
|
|
$where .= " AND Contract.price < $price";
|
|
}
|
|
}
|
|
if(array_key_exists("price<=", $filter)) {
|
|
$price = $filter['price<='];
|
|
if(is_numeric($price)) {
|
|
$where .= " AND Contract.price <= $price";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("price>", $filter)) {
|
|
$price = $filter['price>'];
|
|
if(is_numeric($price)) {
|
|
$where .= " AND Contract.price > $price";
|
|
}
|
|
}
|
|
if(array_key_exists("price>=", $filter)) {
|
|
$price = $filter['price>='];
|
|
if(is_numeric($price)) {
|
|
$where .= " AND Contract.price >= $price";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("billing_period", $filter)) {
|
|
$billing_period = $filter['billing_period'];
|
|
if(is_numeric($billing_period)) {
|
|
$where .= " AND Contract.billing_period=$billing_period";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("finish_date", $filter)) {
|
|
$finish_date = $filter['finish_date'];
|
|
if(is_numeric($finish_date)) {
|
|
$where .= " AND Contract.finish_date = $finish_date";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("finish_date>", $filter)) {
|
|
$finish_date = $filter['finish_date>'];
|
|
if(is_numeric($finish_date)) {
|
|
$where .= " AND Contract.finish_date >= $finish_date";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("finish_date<", $filter)) {
|
|
$finish_date = $filter['finish_date<'];
|
|
if(is_numeric($finish_date)) {
|
|
$where .= " AND Contract.finish_date <= $finish_date";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("finish_date_null_or_gte", $filter)) {
|
|
$cancel_date = $filter['finish_date_null_or_gte'];
|
|
if(is_numeric($cancel_date)) {
|
|
$where .= " AND (Contract.finish_date IS NULL OR Contract.finish_date >= $cancel_date)";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("finish_date_null_or_lte", $filter)) {
|
|
$cancel_date = $filter['finish_date_null_or_lte'];
|
|
if(is_numeric($cancel_date)) {
|
|
$where .= " AND (Contract.finish_date IS NULL OR Contract.finish_date <= $cancel_date)";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("cancel_date>", $filter)) {
|
|
$cancel_date = $filter['cancel_date>'];
|
|
if(is_numeric($cancel_date)) {
|
|
$where .= " AND Contract.cancel_date >= $cancel_date";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("cancel_date_null_or_gte", $filter)) {
|
|
$cancel_date = $filter['cancel_date_null_or_gte'];
|
|
if(is_numeric($cancel_date)) {
|
|
$where .= " AND (Contract.cancel_date IS NULL OR Contract.cancel_date >= $cancel_date)";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("cancel_date<", $filter)) {
|
|
$cancel_date = $filter['cancel_date<'];
|
|
if(is_numeric($cancel_date)) {
|
|
$where .= " AND Contract.cancel_date <= $cancel_date";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("cancel_date_null_or_lte", $filter)) {
|
|
$cancel_date = $filter['cancel_date_null_or_lte'];
|
|
if(is_numeric($cancel_date)) {
|
|
$where .= " AND (Contract.cancel_date IS NULL OR Contract.cancel_date <= $cancel_date)";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
} |