671 lines
23 KiB
PHP
671 lines
23 KiB
PHP
<?php
|
|
|
|
class PreorderProduct extends mfBaseModel {
|
|
private $filter_netowner_id;
|
|
private $filter_netoperator_id;
|
|
private $filter_campaign_id;
|
|
private $prices;
|
|
private $first_price;
|
|
private $current_price;
|
|
private $current_regular_price;
|
|
private $first_campaign_price;
|
|
private $current_campaign_price;
|
|
private $current_campaign_regular_price;
|
|
private $creator;
|
|
private $editor;
|
|
private $today_date;
|
|
|
|
protected static $types = ["operator_setup", "enduser_setup", "operator_usage"];
|
|
|
|
|
|
public function setNetownerId($netowner_id) {
|
|
if(!$netowner_id) {
|
|
$this->filter_netowner_id = null;
|
|
return true;
|
|
}
|
|
$this->filter_netowner_id = $netowner_id;
|
|
return true;
|
|
}
|
|
|
|
public function setNetoperatorId($netoperator_id) {
|
|
if(!$netoperator_id) {
|
|
$this->filter_netoperator_id = null;
|
|
return true;
|
|
}
|
|
$this->filter_netoperator_id = $netoperator_id;
|
|
return true;
|
|
}
|
|
|
|
public function setCampaignId($campaign_id) {
|
|
if(!$campaign_id) {
|
|
$this->filter_campaign_id = null;
|
|
return true;
|
|
}
|
|
$this->filter_campaign_id = $campaign_id;
|
|
return true;
|
|
}
|
|
|
|
public function setTodayDate($date) {
|
|
$this->today_date = date($date);
|
|
}
|
|
|
|
public function getTodayDate() {
|
|
return $this->today_date;
|
|
}
|
|
|
|
public function getDefaultArticlenumber() {
|
|
$prices = PreorderProductPrice::search([
|
|
"article_number" => true,
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"preorderproduct_id" => $this->id
|
|
]);
|
|
foreach($prices as $price) {
|
|
if($price->article_number) return $price->article_number;
|
|
}
|
|
|
|
$prices = PreorderProductPrice::search([
|
|
"article_number" => true,
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"preorderproduct_id" => $this->id
|
|
]);
|
|
foreach($prices as $price) {
|
|
if($price->article_number) return $price->article_number;
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public function getCampaignFirstPrice($campaign_id, $date = null) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` DESC");
|
|
if($price) {
|
|
//$this->first_campaign_price = $price;
|
|
return $price;
|
|
//return $this->first_campaign_price;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function getCampaignCurrentRegularPrice($campaign_id, $date) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => $date,
|
|
"end_date" => null,
|
|
], "start_date DESC, `create` DESC");
|
|
|
|
if($price) {
|
|
//$this->current_campaign_regular_price = $price;
|
|
return $price;
|
|
//return $this->current_campaign_regular_price;
|
|
} else {
|
|
//$this->log->debug(__METHOD__.": TRY FIRST PRICE");
|
|
return $this->getCampaignFirstPrice($campaign_id, $date);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function getCampaignCurrentPrice($campaign_id, $date = null) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
// search for discounted price (with startdate and enddate
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => $date,
|
|
"end_date>=" => $date,
|
|
], "start_date DESC, end_date ASC, `create` DESC");
|
|
|
|
if(!$price) {
|
|
// search for current regular price (with startdate and no enddate)
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => $date,
|
|
"end_date" => null,
|
|
], "start_date DESC, `create` DESC");
|
|
}
|
|
|
|
return $price;
|
|
}
|
|
|
|
|
|
|
|
public function getCampaignPrice($campaign_id, $date = null) {
|
|
$this->log->debug("=== in ".__METHOD__);
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
//$this->log->debug(__METHOD__.": TRY CURRENT PRICE");
|
|
$price = $this->getCampaignCurrentPrice($campaign_id, $date);
|
|
if(!$price) {
|
|
//$this->log->debug(__METHOD__.": TRY CURRENT REGULAR PRICE");
|
|
$price = $this->getCampaignCurrentRegularPrice($campaign_id, $date);
|
|
}
|
|
if(!$price) {
|
|
//$this->log->debug(__METHOD__.": TRY FIRST PRICE");
|
|
$price = $this->getCampaignFirstPrice($campaign_id, $date);
|
|
}
|
|
if(!$price) {
|
|
//$this->log->debug(__METHOD__.": No Campaign price found. GET NETOP CURRENT PRICE");
|
|
$price = $this->getPrice();
|
|
}
|
|
|
|
if($price) {
|
|
//$this->log->debug("=== done in ".__METHOD__);
|
|
return $price;
|
|
}
|
|
return null;
|
|
|
|
}
|
|
|
|
public function getFirstPrice($date = null) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` DESC");
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => null,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` DESC");
|
|
}
|
|
|
|
return $price;
|
|
}
|
|
|
|
public function getCurrentRegularPrice($date = null, $allowCascading = false) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date" => null,
|
|
], "start_date DESC, `create` DESC");
|
|
|
|
if(!$price && $allowCascading) {
|
|
//$this->log->debug(__METHOD__.": Cascading to getFirstPrice()");
|
|
return $this->getFirstPrice($date, true);
|
|
}
|
|
return $price;
|
|
}
|
|
|
|
public function getCurrentPrice($date = null, $allowCascading = false) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date>=" => date($this->today_date),
|
|
], "start_date DESC, end_date ASC, `create` DESC");
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date" => null,
|
|
], "start_date DESC, `create` DESC");
|
|
}
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` DESC");
|
|
}
|
|
|
|
if(!$price && $allowCascading) {
|
|
//$this->log->debug(__METHOD__.": Cascading to getCurrentRegularPrice()");
|
|
return $this->getCurrentRegularPrice($date, true);
|
|
}
|
|
return $price;
|
|
}
|
|
|
|
public function getPrice($date = null) {
|
|
if(!$date) $date = date("Y-m-d");
|
|
|
|
//$this->log->debug("=== in ".__METHOD__);
|
|
//$this->log->debug(__METHOD__.": TRY CURRENT PRICE");
|
|
$price = $this->getCurrentPrice($date);
|
|
if(!$price) {
|
|
//$this->log->debug(__METHOD__.": TRY CURRENT REGULAR PRICE");
|
|
$price = $this->getCurrentRegularPrice($date);
|
|
}
|
|
if(!$price) {
|
|
//$this->log->debug(__METHOD__.": TRY FIRST PRICE");
|
|
$price = $this->getFirstPrice($date);
|
|
}
|
|
|
|
if(!$price) {
|
|
//$this->log->error(__METHOD__.": Unable to find price! netowner_id: ".$this->filter_netowner_id, " netoperator_id: ".$this->filter_netoperator_id." date: $date");
|
|
return null;
|
|
}
|
|
//$this->log->debug("=== done in ".__METHOD__);
|
|
return $price;
|
|
}
|
|
|
|
|
|
public function getProperty($name) {
|
|
if($this->$name == null) {
|
|
|
|
if($name == "prices") {
|
|
$prices = PreorderProductPrice::search(["netowner_id" => $this->filter_netowner_id, "preorderproduct_id" => $this->id]);
|
|
if(count($prices)) {
|
|
foreach($prices as $price) {
|
|
$this->prices[$price->netoperator_id] = $price;
|
|
}
|
|
return $this->prices;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
if($name == "first_price") {
|
|
if(!$this->today_date) $this->today_date = date("Y-m-d");
|
|
|
|
|
|
}
|
|
|
|
if($name == "current_regular_price") {
|
|
if(!$this->today_date) $this->today_date = date("Y-m-d");
|
|
|
|
|
|
}
|
|
|
|
if($name == "current_price") {
|
|
if(!$this->today_date) $this->today_date = date("Y-m-d");
|
|
|
|
|
|
}
|
|
|
|
if($name == "first_campaign_price") {
|
|
if(!$this->today_date) $this->today_date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $this->filter_campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date>=" => null,
|
|
"end_date>=" => null,
|
|
], "`create` ASC");
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` ASC");
|
|
}
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => null,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` ASC");
|
|
}
|
|
if($price) {
|
|
//$this->first_campaign_price = $price;
|
|
return $price;
|
|
//return $this->first_campaign_price;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
if($name == "current_campaign_regular_price") {
|
|
if(!$this->today_date) $this->today_date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $this->filter_campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date" => null,
|
|
], "`create` ASC");
|
|
|
|
if($price) {
|
|
//$this->current_campaign_regular_price = $price;
|
|
return $price;
|
|
//return $this->current_campaign_regular_price;
|
|
} else {
|
|
return $this->getProperty("first_campaign_price");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
if($name == "current_campaign_price") {
|
|
if(!$this->today_date) $this->today_date = date("Y-m-d");
|
|
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $this->filter_campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date>=" => date($this->today_date),
|
|
], "start_date DESC, end_date ASC, `create` DESC");
|
|
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $this->filter_campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date" => null,
|
|
], "start_date DESC, `create` DESC");
|
|
}
|
|
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => $this->filter_campaign_id,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date" => null,
|
|
"end_date" => null,
|
|
], "`create` DESC");
|
|
}
|
|
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date>=" => date($this->today_date),
|
|
], "start_date DESC, end_date ASC, `create` DESC");
|
|
}
|
|
|
|
if(!$price) {
|
|
$price = PreorderProductPrice::getFirst([
|
|
"netowner_id" => $this->filter_netowner_id,
|
|
"netoperator_id" => $this->filter_netoperator_id,
|
|
"campaign_id" => null,
|
|
"preorderproduct_id" => $this->id,
|
|
"start_date<=" => date($this->today_date),
|
|
"end_date" => null,
|
|
], "start_date DESC, `create` DESC");
|
|
}
|
|
if($price) {
|
|
return $price;
|
|
} else {
|
|
return $this->getProperty("current_campaign_regular_price");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
public function __clone() {
|
|
$this->prices = null;
|
|
|
|
$this->first_price = null;
|
|
$this->current_price = null;
|
|
$this->current_regular_price = null;
|
|
|
|
$this->first_campaign_price = null;
|
|
$this->current_campaign_price = null;
|
|
$this->current_campaign_regular_price = null;
|
|
|
|
$this->creator = null;
|
|
$this->editor = null;
|
|
//$this->filter_netoperator_id = null;
|
|
//$this->filter_netowner_id = null;
|
|
//$this->filter_campaign_id = null;
|
|
}
|
|
|
|
/********************************
|
|
* Begin static Model functions
|
|
*/
|
|
|
|
public static function create(Array $data) {
|
|
$model = new PreorderProduct();
|
|
|
|
$table_fields = [
|
|
"type", "name", "vatgroup_id", "price_inet", "price_inet_tv", "price_catv", "price_passive",
|
|
"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 getWithTypes($netowner_id = false) {
|
|
$types = [];
|
|
|
|
foreach(self::$types as $type) {
|
|
$types[$type] = null;
|
|
}
|
|
|
|
foreach(self::getAll() as $product) {
|
|
if(array_key_exists($product->type, $types)) {
|
|
if($netowner_id) {
|
|
$product->setNetownerId($netowner_id);
|
|
}
|
|
$types[$product->type] = $product;
|
|
}
|
|
}
|
|
|
|
return $types;
|
|
}
|
|
|
|
public static function getAll($withKeyIsId = false) {
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$res = $db->select("PreorderProduct", "*", "1 = 1 ORDER BY type");
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
if($withKeyIsId) {
|
|
$items[$data->id] = new PreorderProduct($data);
|
|
} else {
|
|
$items[] = new PreorderProduct($data);
|
|
}
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getFirst($filter) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT * FROM PreorderProduct
|
|
WHERE $where
|
|
ORDER BY type LIMIT 1";
|
|
//var_dump($sql);exit;
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new PreorderProduct($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 PreorderProduct
|
|
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 = "type ASC";
|
|
}
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$sql = "SELECT * FROM PreorderProduct
|
|
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 PreorderProduct($data);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
|
|
|
|
if(array_key_exists("type", $filter)) {
|
|
$type = FronkDB::singleton()->escape($filter["type"]);
|
|
if($type) {
|
|
$where .= " AND type='$type'";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if(array_key_exists("add-where", $filter)) {
|
|
$where .= " ".$filter['add-where'];
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
} |