WIP PreorderPrice 2025-02-21

This commit is contained in:
Frank Schubert
2025-02-24 12:37:33 +01:00
parent fdd6ed7ebb
commit 99bdbd3b39
12 changed files with 1329 additions and 135 deletions

View File

@@ -3,12 +3,17 @@
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"];
@@ -31,6 +36,230 @@ class PreorderProduct extends mfBaseModel {
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 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) {
@@ -46,9 +275,30 @@ class PreorderProduct extends mfBaseModel {
}
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,
@@ -56,66 +306,112 @@ class PreorderProduct extends mfBaseModel {
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->current_price = $price;
return $this->current_price;
//$this->first_campaign_price = $price;
return $price;
//return $this->first_campaign_price;
}
return null;
}
if($name == "current_regular_price") {
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("Y-m-d"),
"start_date<=" => date($this->today_date),
"end_date>" => null,
], "`create` ASC");
if($price) {
$this->current_price = $price;
return $this->current_price;
//$this->current_campaign_regular_price = $price;
return $price;
//return $this->current_campaign_regular_price;
} else {
return $this->getProperty("first_price");
return $this->getProperty("first_campaign_price");
}
return null;
}
if($name == "current_price") {
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("Y-m-d"),
"end_date>" => date("Y-m-d"),
], "start_date");
"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("Y-m-d"),
"start_date<=" => date($this->today_date),
"end_date" => null,
], "start_date");
], "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,
], "start_date");
], "`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) {
$this->current_price = $price;
return $this->current_price;
return $price;
} else {
return $this->getProperty("current_campaign_regular_price");
}
return null;
}
@@ -168,6 +464,24 @@ class PreorderProduct extends mfBaseModel {
}
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
*/

View File

@@ -44,6 +44,16 @@ class PreorderProductController extends mfBaseController {
}
}
$campaigns = PreordercampaignModel::search(["owner_id" => $netowner_id]);
$today_date = date("Y-m-d");
if($this->request->view_date) {
$today_date = $this->request->view_date;
}
$this->layout()->set("today_date", $today_date);
$this->layout()->set("campaigns", $campaigns);
$this->layout()->set("netoperators", $netoperators);
$this->layout()->set("netowner", $netowner);
}
@@ -61,7 +71,7 @@ class PreorderProductController extends mfBaseController {
$this->layout()->redirect("PreorderProduct");
}
foreach($product_data as $product_id => $price_data) {
var_dump($price_data);
//var_dump($price_data);exit;
$product = new PreorderProduct($product_id);
if(!$product->id) {
$this->layout()->setFlash("Produkt $product_id nicht gefunden", "error");
@@ -74,14 +84,23 @@ class PreorderProductController extends mfBaseController {
"preorderproduct_id" => $product->id,
"netoperator_id" => $netoperator_id,
]);
if($price_data["price_setup"]) $price->price_setup = $price_data["price_setup"];
if($price_data["description"]) $price->description = trim($price_data["description"]);
if($product->type == "operator_setup" || $product->type == "enduser_setup") {
if($price_data["price_setup"]) $price->price_setup = Layout::commaToDot(trim($price_data["price_setup"]));
} elseif($product->type == "operator_usage") {
if($price_data["price_inet"]) $price->price_inet = Layout::commaToDot(trim($price_data["price_inet"]));
if($price_data["price_inet_tv"]) $price->price_inet_tv = Layout::commaToDot(trim($price_data["price_inet_tv"]));
if($price_data["price_catv"]) $price->price_catv = Layout::commaToDot(trim($price_data["price_catv"]));
if($price_data["price_passive"]) $price->price_passive = Layout::commaToDot(trim($price_data["price_passive"]));
}
if(!$price_data["start_date"]) {
$this->layout()->setFlash("Von-datum fehlt bei Produkt '".$product->name."' für '".$netoperator->getCompanyOrName()."'", "error");
$this->redirect("PreorderProduct");
} else {
try {
$start_date = new DateTime("@" . $this->dateToTimestamp($price_data["start_date"]));
$start_date = new DateTime("@" . $this->dateToTimestamp(trim($price_data["start_date"])));
$start_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$price->start_date = $start_date->format("Y-m-d");
} catch(Exception $e) {
@@ -89,9 +108,9 @@ class PreorderProductController extends mfBaseController {
$this->redirect("PreorderProduct");
}
}
if($price_data["end_date"]) {
if(trim($price_data["end_date"])) {
try {
$end_date = new DateTime("@" . $this->dateToTimestamp($price_data["end_date"]));
$end_date = new DateTime("@" . $this->dateToTimestamp(trim($price_data["end_date"])));
$end_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$price->end_date = $end_date->format("Y-m-d");
} catch(Exception $e) {
@@ -100,11 +119,25 @@ class PreorderProductController extends mfBaseController {
}
}
//var_dump($product, $price);
$price->save();
// create PreorderProductPriceCampaign for all submitted campaign_ids
foreach($price_data["campaigns"] as $campaign_id) {
$campaign = new Preordercampaign($campaign_id);
if(!$campaign->id) {
$this->layout()->setFlash("Ungültige Kampagne bei Produkt '".$product->name."' für '".$netoperator->getCompanyOrName()."'", "error");
$this->layout()->redirect("PreorderProduct");
}
$price_campaign = PreorderProductPriceCampaign::create([
"preorderproductprice_id" => $price->id,
"preordercampaign_id" => $campaign->id,
]);
$price_campaign->save();
}
}
}
$this->layout()->setFlash("Neue Preise erflgreich gespeichert", "success");

View File

@@ -4,6 +4,7 @@ class PreorderProductPrice extends mfBaseModel {
private $preorderprduct;
private $netowner;
private $preordercampaign;
private $campaigns;
private $campaign;
private $creator;
private $editor;
@@ -39,6 +40,19 @@ class PreorderProductPrice extends mfBaseModel {
return $this->preordercampaign;
}
if($name == "campaigns") {
$ccount = PreorderProductPriceCampaign::count(["preorderproductprice_id" => $this->id]);
$this->log->debug("Counted $ccount Campaigns for Price ".$this->id);
if(!$ccount) return [];
$this->campaigns = [];
foreach(PreorderProductPriceCampaign::search(["preorderproductprice_id" => $this->id]) as $pppc) {
$campaign = $pppc->campaign;
if(!$campaign || !$campaign->id) continue;
$this->campaigns[$campaign->id] = new PreorderCampaign($campaign->id);
}
return $this->campaigns;
}
if($name == "creator") {
$creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
@@ -97,7 +111,7 @@ class PreorderProductPrice extends mfBaseModel {
$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",
"billing_delay", "billing_period", "contract_term", "description", "note",
"create_by","edit_by","create","edit"
];
@@ -125,7 +139,12 @@ class PreorderProductPrice extends mfBaseModel {
$db = FronkDB::singleton();
$res = $db->select("PreorderProductPrice", "*", "1 = 1 ORDER BY start_date");
$sql = "SELECT PreorderProductPrice.* FROM PreorderProductPrice
LEFT OUTER JOIN (SELECT COUNT(*) as cnt, preorderproductprice_id, preordercampaign_id as preordercampaign_id FROM PreorderProductPriceCampaign GROUP BY PreorderProductPriceCampaign.preorderproductprice_id, PreorderProductPriceCampaign.preordercampaign_id) pppc ON PreorderProductPrice.id = pppc.preorderproductprice_id
GROUP BY PreorderProductPrice.id
ORDER BY start_date";
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderProductPrice($data);
@@ -143,8 +162,10 @@ class PreorderProductPrice extends mfBaseModel {
}
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderProductPrice
WHERE $where
$sql = "SELECT PreorderProductPrice.* FROM PreorderProductPrice
LEFT OUTER JOIN (SELECT COUNT(*) as cnt, preorderproductprice_id, preordercampaign_id as preordercampaign_id FROM PreorderProductPriceCampaign GROUP BY PreorderProductPriceCampaign.preorderproductprice_id, PreorderProductPriceCampaign.preordercampaign_id) pppc ON PreorderProductPrice.id = pppc.preorderproductprice_id
WHERE $where
GROUP BY PreorderProductPrice.id
ORDER BY $order LIMIT 1";
mfLoghandler::singleton()->debug($sql);
@@ -167,7 +188,9 @@ class PreorderProductPrice extends mfBaseModel {
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM PreorderProductPrice
WHERE $where";
LEFT OUTER JOIN (SELECT COUNT(*) as cnt, preorderproductprice_id, preordercampaign_id as preordercampaign_id FROM PreorderProductPriceCampaign GROUP BY PreorderProductPriceCampaign.preorderproductprice_id, PreorderProductPriceCampaign.preordercampaign_id) pppc ON PreorderProductPrice.id = pppc.preorderproductprice_id
WHERE $where
GROUP BY PreorderProductPrice.id";
//mfLoghandler::singleton()->debug($sql);
@@ -190,8 +213,10 @@ class PreorderProductPrice extends mfBaseModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderProductPrice
$sql = "SELECT PreorderProductPrice.* FROM PreorderProductPrice
LEFT OUTER JOIN (SELECT COUNT(*) as cnt, preorderproductprice_id, preordercampaign_id as preordercampaign_id FROM PreorderProductPriceCampaign GROUP BY PreorderProductPriceCampaign.preorderproductprice_id, PreorderProductPriceCampaign.preordercampaign_id) pppc ON PreorderProductPrice.id = pppc.preorderproductprice_id
WHERE $where
GROUP BY PreorderProductPrice.id
ORDER BY $order";
if(is_array($limit) && count($limit)) {
@@ -233,15 +258,28 @@ class PreorderProductPrice extends mfBaseModel {
if(array_key_exists("netoperator_id", $filter)) {
$netoperator_id = $filter['netoperator_id'];
if(is_numeric($netoperator_id)) {
if($netoperator_id === null || $netoperator_id === false) {
$where .= " AND PreorderProductPrice.netoperator_id IS NULL";
} elseif(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($preordercampaign_id === null || $preordercampaign_id === false) {
$where .= " AND (pppc.cnt IS NULL OR pppc.cnt = 0)";
} elseif(is_numeric($preordercampaign_id)) {
$where .= " AND pppc.preordercampaign_id=$preordercampaign_id";
}
}
if(array_key_exists("campaign_id", $filter)) {
$preordercampaign_id = $filter['campaign_id'];
if($preordercampaign_id === null || $preordercampaign_id === false) {
$where .= " AND (pppc.cnt IS NULL OR pppc.cnt = 0)";
} elseif(is_numeric($preordercampaign_id)) {
$where .= " AND pppc.preordercampaign_id=$preordercampaign_id";
}
}

View File

@@ -0,0 +1,253 @@
<?php
class PreorderProductPriceCampaign extends mfBaseModel {
private $price;
private $preordercampaign;
private $campaign;
private $preorderproduct;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
if($name == "preorderproduct") {
$price = $this->getProperty("price");
$product = $price->preorderproduct;
if(!$product || !$product->id) {
return null;
}
$this->preorderproduct = $product;
return $this->preorderproduct;
}
if($name == "price") {
$price = new PreorderProductPrice($this->preorderproductprice_id);
if(!$price->id) {
return null;
}
$this->price = $price;
return $this->price;
}
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->preordercampaign_id);
if(!$campaign->id) {
return null;
}
$this->preordercampaign = $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 PreorderProductPriceCampaign();
$table_fields = [
"preorderproductprice_id", "preordercampaign_id",
"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("PreorderProductPriceCampaign", "*", "1 = 1 ORDER BY preorderproductprice_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderProductPriceCampaign($data);
}
}
return $items;
}
public static function getFirst($filter = [], $order = false) {
$db = FronkDB::singleton();
if(!$order) {
$order = "preorderproductprice_id ASC";
}
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderProductPriceCampaign
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 PreorderProductPriceCampaign($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 PreorderProductPriceCampaign
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 = "preorderproductprice_id ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderProductPriceCampaign
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 PreorderProductPriceCampaign($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("preordercampaign_id", $filter)) {
$preordercampaign_id = $filter['preordercampaign_id'];
if(is_numeric($preordercampaign_id)) {
$where .= " AND PreorderProductPriceCampaign.preordercampaign_id=$preordercampaign_id";
}
}
if(array_key_exists("preorderproductprice_id", $filter)) {
$preorderproductprice_id = $filter['preorderproductprice_id'];
if(is_numeric($preorderproductprice_id)) {
$where .= " AND PreorderProductPriceCampaign.preorderproductprice_id=$preorderproductprice_id";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}