WIP PreorderProduct 2025-02-17

This commit is contained in:
Frank Schubert
2025-02-18 13:52:35 +01:00
parent aa7a3eb1ed
commit fdd6ed7ebb
11 changed files with 893 additions and 219 deletions

View File

@@ -1,24 +1,125 @@
<?php
class PreorderProduct extends mfBaseModel {
private $preorder;
private $createor;
private $filter_netowner_id;
private $filter_netoperator_id;
private $prices;
private $first_price;
private $current_price;
private $current_regular_price;
private $creator;
private $editor;
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 getProperty($name) {
if($this->$name == null) {
if($name == "preorder") {
$preorder = PreorderModel::getFirst(["id" => $this->preorder_id]);
if(!$preorder) {
$this->log->debug(__METHOD__ . ": Preorder " . $this->preorder_id . " not found");
return 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;
}
$this->preorder = $preorder;
return $this->preorder;
return [];
}
if($name == "first_price") {
$price = PreorderProductPrice::getFirst([
"netowner_id" => $this->filter_netowner_id,
"netoperator_id" => $this->filter_netoperator_id,
"preorderproduct_id" => $this->id,
"start_date>=" => null,
"end_date>" => null,
], "`create` ASC");
if(!$price) {
$price = PreorderProductPrice::getFirst([
"netowner_id" => $this->filter_netowner_id,
"preorderproduct_id" => $this->id,
"start_date>=" => null,
"end_date>" => null,
], "`create` ASC");
}
if($price) {
$this->current_price = $price;
return $this->current_price;
}
return null;
}
if($name == "current_regular_price") {
$price = PreorderProductPrice::getFirst([
"netowner_id" => $this->filter_netowner_id,
"netoperator_id" => $this->filter_netoperator_id,
"preorderproduct_id" => $this->id,
"start_date<=" => date("Y-m-d"),
"end_date>" => null,
], "`create` ASC");
if($price) {
$this->current_price = $price;
return $this->current_price;
} else {
return $this->getProperty("first_price");
}
return null;
}
if($name == "current_price") {
$price = PreorderProductPrice::getFirst([
"netowner_id" => $this->filter_netowner_id,
"netoperator_id" => $this->filter_netoperator_id,
"preorderproduct_id" => $this->id,
"start_date<=" => date("Y-m-d"),
"end_date>" => date("Y-m-d"),
], "start_date");
if(!$price) {
$price = PreorderProductPrice::getFirst([
"netowner_id" => $this->filter_netowner_id,
"netoperator_id" => $this->filter_netoperator_id,
"preorderproduct_id" => $this->id,
"start_date<=" => date("Y-m-d"),
"end_date" => null,
], "start_date");
}
if(!$price) {
$price = PreorderProductPrice::getFirst([
"netowner_id" => $this->filter_netowner_id,
"netoperator_id" => $this->filter_netoperator_id,
"preorderproduct_id" => $this->id,
"start_date" => null,
"end_date" => null,
], "start_date");
}
if($price) {
$this->current_price = $price;
return $this->current_price;
}
return null;
}
if($name == "creator") {
$creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($creator) {
@@ -47,18 +148,24 @@ class PreorderProduct extends mfBaseModel {
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
return $this->editor;
}
if($name == "product") {
$product = ProductModel::getFirst(["id" => $this->product_id]);
if(!$product) {
$this->log->debug(__METHOD__ . ": Product " . $this->product_id . " not found");
return null;
}
$this->product = $product;
return $this->product;
$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 parent::getProperty($name);
return $this->$name;
}
/********************************
@@ -66,10 +173,10 @@ class PreorderProduct extends mfBaseModel {
*/
public static function create(Array $data) {
$model = new ConstructionConsent();
$model = new PreorderProduct();
$table_fields = [
"type", "name", "vatgroup_id", "price", "price_setup",
"type", "name", "vatgroup_id", "price_inet", "price_inet_tv", "price_catv", "price_passive",
"create_by","edit_by","create","edit"
];
@@ -92,15 +199,34 @@ class PreorderProduct extends mfBaseModel {
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() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ConstructionConsent", "*", "1 = 1 ORDER BY adb_hausnummer_id");
$res = $db->select("PreorderProduct", "*", "1 = 1 ORDER BY type");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsent($data);
$items[] = new PreorderProduct($data);
}
}
return $items;
@@ -111,14 +237,14 @@ class PreorderProduct extends mfBaseModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsent
$sql = "SELECT * FROM PreorderProduct
WHERE $where
ORDER BY adb_hausnummer_id LIMIT 1";
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 ConstructionConsent($data);
$item = new PreorderProduct($data);
if($item->id) {
return $item;
} else {
@@ -132,8 +258,7 @@ class PreorderProduct extends mfBaseModel {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM ConstructionConsent
LEFT JOIN ".ADDRESSDB_DBNAME.".view_hausnummer ON (ConstructionConsent.adb_hausnummer_id = view_hausnummer.hausnummer_id)
$sql = "SELECT COUNT(*) as cnt FROM PreorderProduct
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
@@ -151,14 +276,13 @@ class PreorderProduct extends mfBaseModel {
$items = [];
if(!$order) {
$order = "adb_hausnummer_id ASC";
$order = "type ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsent
LEFT JOIN ".ADDRESSDB_DBNAME.".view_hausnummer ON (ConstructionConsent.adb_hausnummer_id = view_hausnummer.hausnummer_id)
$sql = "SELECT * FROM PreorderProduct
WHERE $where
ORDER BY $order";
@@ -175,7 +299,7 @@ class PreorderProduct extends mfBaseModel {
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ConstructionConsent($data);
$items[$data->id] = new PreorderProduct($data);
}
}
@@ -188,28 +312,28 @@ class PreorderProduct extends mfBaseModel {
if(array_key_exists("project_id", $filter)) {
$project_id = $filter['project_id'];
if(is_numeric($project_id)) {
$where .= " AND ConstructionConsent.constructionconsentproject_id=$project_id";
$where .= " AND PreorderProduct.preorderProductproject_id=$project_id";
}
}
if(array_key_exists("constructionconsentproject_id", $filter)) {
$constructionconsentproject_id = $filter['constructionconsentproject_id'];
if(is_numeric($constructionconsentproject_id)) {
$where .= " AND ConstructionConsent.constructionconsentproject_id=$constructionconsentproject_id";
if(array_key_exists("preorderProductproject_id", $filter)) {
$preorderProductproject_id = $filter['preorderProductproject_id'];
if(is_numeric($preorderProductproject_id)) {
$where .= " AND PreorderProduct.preorderProductproject_id=$preorderProductproject_id";
}
}
if(array_key_exists("adb_wohneinheit_id", $filter)) {
$adb_wohneinheit_id = $filter['adb_wohneinheit_id'];
if(is_numeric($adb_wohneinheit_id)) {
$where .= " AND ConstructionConsent.adb_wohneinheit_id=$adb_wohneinheit_id";
$where .= " AND PreorderProduct.adb_wohneinheit_id=$adb_wohneinheit_id";
}
}
if(array_key_exists("termination_id", $filter)) {
$termination_id = $filter['termination_id'];
if(is_numeric($termination_id)) {
$where .= " AND ConstructionConsent.termination_id=$termination_id";
$where .= " AND PreorderProduct.termination_id=$termination_id";
}
}

View File

@@ -17,6 +17,98 @@ class PreorderProductController extends mfBaseController {
protected function indexAction() {
$this->layout()->setTemplate("PreorderProduct/Index");
//$product =
$netowner_id = 4807; // $this->me->address_id;
$netowner = new Address($netowner_id);
$products = PreorderProduct::getWithTypes($netowner_id);
$this->layout()->set("products", $products);
$netoperators = [];
foreach(PreordercampaignModel::search(["owner_id" => $netowner_id]) as $campaign) {
foreach($campaign->active_operators as $op) {
if(!array_key_exists($op->operator_id, $netoperators)) {
$nop = new Address($op->operator_id);
if($nop->id) {
$netoperators[$nop->id] = $nop;
}
}
}
foreach($campaign->passive_operators as $op) {
if(!array_key_exists($op->operator_id, $netoperators)) {
$nop = new Address($op->operator_id);
if($nop->id) {
$netoperators[$nop->id] = $nop;
}
}
}
}
$this->layout()->set("netoperators", $netoperators);
$this->layout()->set("netowner", $netowner);
}
protected function saveAction() {
$r = $this->request;
//var_dump($r->get());exit;
$netowner_id = 4807; // $this->me->address_id;
foreach($r->netoperators as $netoperator_id => $product_data) {
$netoperator = new Address($netoperator_id);
if(!$netoperator->id) {
$this->layout()->setFlash("Betzbetreiber nicht gefunden", "error");
$this->layout()->redirect("PreorderProduct");
}
foreach($product_data as $product_id => $price_data) {
var_dump($price_data);
$product = new PreorderProduct($product_id);
if(!$product->id) {
$this->layout()->setFlash("Produkt $product_id nicht gefunden", "error");
$this->layout()->redirect("PreorderProduct");
}
// create new PreorderProductPrice
$price = PreorderProductPrice::create([
"netowner_id" => $netowner_id,
"preorderproduct_id" => $product->id,
"netoperator_id" => $netoperator_id,
]);
if($price_data["price_setup"]) $price->price_setup = $price_data["price_setup"];
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->setTimezone(new DateTimeZone("Europe/Vienna"));
$price->start_date = $start_date->format("Y-m-d");
} catch(Exception $e) {
$this->layout()->setFlash("Fehler im Von-datum bei Produkt '".$product->name."' für '".$netoperator->getCompanyOrName()."'", "error");
$this->redirect("PreorderProduct");
}
}
if($price_data["end_date"]) {
try {
$end_date = new DateTime("@" . $this->dateToTimestamp($price_data["end_date"]));
$end_date->setTimezone(new DateTimeZone("Europe/Vienna"));
$price->end_date = $end_date->format("Y-m-d");
} catch(Exception $e) {
$this->layout()->setFlash("Fehler im Bis-Datum bei Produkt '".$product->name."' für '".$netoperator->getCompanyOrName()."'", "error");
$this->redirect("PreorderProduct");
}
}
//var_dump($product, $price);
$price->save();
}
}
$this->layout()->setFlash("Neue Preise erflgreich gespeichert", "success");
$this->redirect("PreorderProduct");
}
}