Files
thetool/application/PreorderProductPrice/PreorderProductPrice.php
2025-02-18 13:52:35 +01:00

335 lines
11 KiB
PHP

<?php
class PreorderProductPrice extends mfBaseModel {
private $preorderprduct;
private $netowner;
private $preordercampaign;
private $campaign;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
if($name == "preorderproduct") {
$product = new PreorderProduct($this->preorderprduct_id);
if(!$product->id) {
return null;
}
$this->preorderprduct = $product;
return $this->preorderprduct;
}
if($name == "netowner") {
$netowner = new Address($this->netowner_id);
if(!$netowner->id) {
return null;
}
$this->netowner = $netowner;
return $this->netowner;
}
if($name == "preordercampaign" || $name == "campaign") {
$campaign = new PreorderCampaign($this->preordercampgin_id);
if(!$campaign->id) {
return null;
}
$this->preorderprduct = $campaign;
$this->campaign = $campaign;
return $this->preordercampaign;
}
if($name == "creator") {
$creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($creator) {
$this->creator = $creator;
return $this->creator;
}
$this->creator = new User($this->create_by);
if(!$this->creator->id) {
return null;
}
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
return $this->creator;
}
if($name == "editor") {
$editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
if($editor) {
$this->editor = $editor;
return $this->editor;
}
$this->editor = new User($this->edit_by);
if(!$this->editor->id) {
return null;
}
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name . "_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-" . $this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-" . $this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new PreorderProductPrice();
$table_fields = [
"preorderproduct_id", "netowner_id", "netoperator_id", "preordercampaign_id", "description",
"start_date", "end_date", "price_inet", "price_inet_tv", "price_catv", "price_passive",
"billing_delay", "billing_period", "contract_term", "note",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$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("PreorderProductPrice", "*", "1 = 1 ORDER BY start_date");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderProductPrice($data);
}
}
return $items;
}
public static function getFirst($filter = [], $order = false) {
$db = FronkDB::singleton();
if(!$order) {
$order = "start_date ASC";
}
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderProductPrice
WHERE $where
ORDER BY $order LIMIT 1";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderProductPrice($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 PreorderProductPrice
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 = "start_date ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderProductPrice
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 PreorderProductPrice($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("preorderproduct_id", $filter)) {
$preorderproduct_id = $filter['preorderproduct_id'];
if(is_numeric($preorderproduct_id)) {
$where .= " AND PreorderProductPrice.preorderproduct_id=$preorderproduct_id";
}
}
if(array_key_exists("netowner_id", $filter)) {
$netowner_id = $filter['netowner_id'];
if(is_numeric($netowner_id)) {
$where .= " AND PreorderProductPrice.netowner_id=$netowner_id";
}
}
if(array_key_exists("netoperator_id", $filter)) {
$netoperator_id = $filter['netoperator_id'];
if(is_numeric($netoperator_id)) {
$where .= " AND PreorderProductPrice.netoperator_id=$netoperator_id";
}
}
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {
$where .= " AND PreorderProductPrice.preordercampaign_id=$preordercampaign_id";
}
}
if(array_key_exists("start_date", $filter)) {
$start_date = $filter["start_date"];
if($start_date === null || $start_date === false) {
$where .= " AND PreorderProductPrice.start_date IS NULL";
} elseif($start_date) {
$start_date = FronkDB::singleton()->escape($filter["start_date"]);
$where .= " AND PreorderProductPrice.start_date='$start_date'";
}
}
if(array_key_exists("start_date>", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>']);
if($start_date) {
$where .= " AND PreorderProductPrice.start_date > '$start_date'";
}
}
if(array_key_exists("start_date<", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<']);
if($start_date) {
$where .= " AND PreorderProductPrice.start_date < '$start_date'";
}
}
if(array_key_exists("start_date>=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date>=']);
if($start_date) {
$where .= " AND PreorderProductPrice.start_date >= '$start_date'";
}
}
if(array_key_exists("start_date<=", $filter)) {
$start_date = FronkDB::singleton()->escape($filter['start_date<=']);
if($start_date) {
$where .= " AND PreorderProductPrice.start_date <= '$start_date'";
}
}
if(array_key_exists("end_date", $filter)) {
$end_date = $filter["end_date"];
if($end_date === null || $end_date === false) {
$where .= " AND PreorderProductPrice.end_date IS NULL";
} elseif($end_date) {
$end_date = FronkDB::singleton()->escape($filter["end_date"]);
$where .= " AND PreorderProductPrice.end_date='$end_date'";
}
}
if(array_key_exists("end_date>", $filter)) {
$end_date = FronkDB::singleton()->escape($filter['end_date>']);
if($end_date) {
$where .= " AND PreorderProductPrice.end_date > '$end_date'";
}
}
if(array_key_exists("end_date<", $filter)) {
$end_date = FronkDB::singleton()->escape($filter['end_date<']);
if($end_date) {
$where .= " AND PreorderProductPrice.end_date < '$end_date'";
}
}
if(array_key_exists("end_date>=", $filter)) {
$end_date = FronkDB::singleton()->escape($filter['end_date>=']);
if($end_date) {
$where .= " AND PreorderProductPrice.end_date >= '$end_date'";
}
}
if(array_key_exists("end_date<=", $filter)) {
$end_date = FronkDB::singleton()->escape($filter['end_date<=']);
if($end_date) {
$where .= " AND PreorderProductPrice.end_date <= '$end_date'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}