Files
thetool/application/Contractqueue/ContractqueueModel.php
2024-04-18 22:58:37 +02:00

385 lines
12 KiB
PHP

<?php
class ContractqueueModel {
public $approved;
public $contract_id;
public $order_id;
public $orderproduct_id;
public $owner_id;
public $crediting_partner_id;
public $crediting_partner_rate;
public $billingaddress_id;
public $termination_id;
public $product_id;
public $product_name;
public $product_info;
public $matchcode;
public $crediting_matchcode;
public $amount;
public $sla_id = null;
public $product_external;
public $product_external_id;
public $price = null;
public $price_setup = null;
public $price_nne = null;
public $price_nbe = null;
public $billing_delay = null;
public $billing_period = null;
public $order_date;
public $finish_date;
public $finish_date_by;
public $cancel_date;
public $cancel_date_by;
public $imported_from;
public $imported_data;
public $note = null;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new Contractqueue();
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 createFromOrderproduct($op) {
$log = mfLoghandler::singleton();
if(!$op->id) {
$log->warning(__METHOD__."(): Invalid OrderProduct object");
return false;
}
$order = $op->order;
$product = $op->product;
if(!$order->id || !$product->id) {
$log->warning(__METHOD__."(): Invalid Order or Product");
return false;
}
if(!$order->finish_date || $order->finish_date > date("U")) {
$log->warning(__METHOD__."(): Order not finished yet");
return false;
}
$data = [];
$data["order_id"] = $order->id;
$data["orderproduct_id"] = $op->id;
$data["owner_id"] = $order->owner_id;
$data["billingaddress_id"] = $order->billingaddress_id;
$data["termination_id"] = ($op->termination_id) ? $op->termination_id : null;
$data["product_id"] = $op->product_id;
$data["product_name"] = $product->name;
$data["product_info"] = $product->description;
$data["amount"] = $op->amount;
$data["sla_id"] = $product->sla_id;
$data["product_external"] = ($product->external) ? $product->external : 0;
$data["product_external_id"] = ($product->external) ? $product->external_id : null;
$data["price"] = $op->price;
$data["price_setup"] = $op->price_setup;
$data["price_nne"] = $op->price_nne;
$data["price_nbe"] = $op->price_nbe;
$data["billing_delay"] = $op->billing_delay;
$data["billing_period"] = $op->billing_period;
$data["order_date"] = $order->order_date;
$data["finish_date"] = $order->finish_date;
$data["finish_date_by"] = 1;
$data["note"] = $order->note;
$contractq = ContractqueueModel::create($data);
$contractq->matchcode = $contractq->generateMatchcode();
//var_dump($contract);exit;
return $contractq;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Contractqueue", "*", "1 = 1 ORDER BY owner_id,`create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Contractqueue($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Contractqueue.* FROM Contractqueue
LEFT JOIN Address ON (Contractqueue.owner_id = Address.id)
LEFT JOIN OrderProduct ON (Contractqueue.orderproduct_id = OrderProduct.id)
LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id)
LEFT JOIN Product ON (Contractqueue.product_id = Product.id)
WHERE $where
GROUP BY Contractqueue.id
ORDER BY Contractqueue.order_id,Contractqueue.orderproduct_id,Contractqueue.owner_id,Contractqueue.product_id,Contractqueue.`create`
LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Contractqueue($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 (
SELECT Contractqueue.* FROM Contractqueue
LEFT JOIN Address ON (Contractqueue.owner_id = Address.id)
LEFT JOIN OrderProduct ON (Contractqueue.orderproduct_id = OrderProduct.id)
LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id)
LEFT JOIN Product ON (Contractqueue.product_id = Product.id)
WHERE $where
AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())
GROUP BY Contractqueue.id
) contract";
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 searchActive($filter, $limit = false) {
//var_dump($filter);exit;
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Contractqueue.* FROM Contractqueue
LEFT JOIN Address ON (Contractqueue.owner_id = Address.id)
LEFT JOIN OrderProduct ON (Contractqueue.orderproduct_id = OrderProduct.id)
LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id)
LEFT JOIN Product ON (Contractqueue.product_id = Product.id)
WHERE $where
AND (cancel_date IS NULL OR cancel_date > UNIX_TIMESTAMP())
GROUP BY Contractqueue.id
ORDER BY Contractqueue.order_id,Contractqueue.orderproduct_id,Contractqueue.owner_id,Contractqueue.`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 Contractqueue($data);
}
}
return $items;
}
public static function search($filter, $limit = false) {
//var_dump($filter);exit;
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Contractqueue.* FROM Contractqueue
LEFT JOIN Address ON (Contractqueue.owner_id = Address.id)
LEFT JOIN OrderProduct ON (Contractqueue.orderproduct_id = OrderProduct.id)
LEFT JOIN `Order` ON (OrderProduct.order_id = `Order`.id)
LEFT JOIN Product ON (Contractqueue.product_id = Product.id)
WHERE $where
GROUP BY Contractqueue.id
ORDER BY Contractqueue.order_id,Contractqueue.orderproduct_id,Contractqueue.owner_id,Contractqueue.`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($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 Contractqueue($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 Contractqueue.id like '%$id%'";
}
}
if(array_key_exists("approved", $filter)) {
$approved = $filter['approved'];
if($approved) {
$where .= " AND Contractqueue.approved = 1";
} else {
$where .= " AND Contractqueue.approved = 0";
}
}
if(array_key_exists("contract_id", $filter)) {
$contract_id = $filter['contract_id'];
if(is_numeric($contract_id)) {
$where .= " AND Contractqueue.contract_id=$contract_id";
} elseif($contract_id === null || $contract_id === false) {
$where .= " AND (Contractqueue.contract_id IS NULL OR Contractqueue.contract_id = 0)";
} elseif($contract_id === true) {
$where .= " AND (Contractqueue.contract_id IS NOT NULL AND Contractqueue.contract_id > 0)";
}
}
if(array_key_exists("owner_id", $filter)) {
$owner_id = $filter['owner_id'];
if(is_numeric($owner_id)) {
$where .= " AND Contractqueue.owner_id=$owner_id";
}
}
if(array_key_exists("billingaddress_id", $filter)) {
$billingaddress_id = $filter['billingaddress_id'];
if(is_numeric($billingaddress_id)) {
$where .= " AND Contractqueue.billingaddress_id=$billingaddress_id";
}
}
if(array_key_exists("product_id", $filter)) {
$product_id = $filter['product_id'];
if(is_numeric($product_id)) {
$where .= " AND Contractqueue.product_id=$product_id";
}
}
if(array_key_exists("order_id", $filter)) {
$order_id = $filter['order_id'];
if(is_numeric($order_id)) {
$where .= " AND `Order`.id=$order_id";
}
}
if(array_key_exists("orderproduct_id", $filter)) {
$orderproduct_id = $filter['orderproduct_id'];
if(is_numeric($orderproduct_id)) {
$where .= " AND Contractqueue.orderproduct_id=$orderproduct_id";
}
}
if(array_key_exists("product_name", $filter)) {
$product_name = $db->escape($filter['product_name']);
if($product_name) {
$where .= " AND (Contractqueue.`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 Contractqueue.`matchcode` like '%$matchcode%'";
}
}
if(array_key_exists("rtr_code", $filter)) {
$rtr_code = $filter['rtr_code'];
if(is_numeric($rtr_code)) {
$where .= " AND Contractqueue.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.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 Contractqueue.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 Contractqueue.imported_data like '$imported_data'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}