Files
thetool/application/PreorderProduct/PreorderProductController.php
2025-03-17 12:22:03 +01:00

169 lines
8.1 KiB
PHP

<?php
class PreorderProductController extends mfBaseController {
protected function init() {
$this->needlogin=true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me",$me);
if(!$me->can(["preorderpricing", "preorderpricingReadonly", "preorderbilling", "preorderbillingReadonly"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$this->layout()->setTemplate("PreorderProduct/Index");
$netowner_id = $this->me->address_id; // 4807 == rml
$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;
}
}
}
}
$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);
}
protected function saveAction() {
$r = $this->request;
//var_dump($r->get());exit;
$netowner_id = $this->me->address_id; // 4807 == rml
foreach($r->netoperators as $netoperator_id => $product_data) {
$netoperator = new Address($netoperator_id);
if(!$netoperator->id) {
$this->layout()->setFlash("Netzbetreiber nicht gefunden", "error");
$this->layout()->redirect("PreorderProduct");
}
foreach($product_data as $product_id => $price_data) {
//var_dump($price_data);exit;
$product = new PreorderProduct($product_id);
if(!$product->id) {
$this->layout()->setFlash("Produkt $product_id nicht gefunden", "error");
$this->layout()->redirect("PreorderProduct","",[], "product-".$netoperator_id);
}
// create new PreorderProductPrice
$price = PreorderProductPrice::create([
"netowner_id" => $netowner_id,
"preorderproduct_id" => $product->id,
"netoperator_id" => $netoperator_id,
"article_number" => $price_data["article_number"],
]);
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","",[], "product-".$netoperator_id."-".$product_id);
} else {
try {
$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) {
$this->layout()->setFlash("Fehler im Von-datum bei Produkt '".$product->name."' für '".$netoperator->getCompanyOrName()."'", "error");
$this->redirect("PreorderProduct","",[], "product-".$netoperator_id."-".$product_id);
}
}
if(trim($price_data["end_date"])) {
try {
$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) {
$this->layout()->setFlash("Fehler im Bis-Datum bei Produkt '".$product->name."' für '".$netoperator->getCompanyOrName()."'", "error");
$this->redirect("PreorderProduct","",[], "product-".$netoperator_id."-".$product_id);
}
}
//var_dump($product, $price);
$price->save();
// marketshare prices
if(array_key_exists("marketshareprice", $price_data) && is_array($price_data["marketshareprice"])) {
foreach($price_data["marketshareprice"] as $msbracket => $msprice) {
if(!is_numeric($msbracket)) continue;
if(!$msbracket) continue; // dont save bracket 0 - 15%
if(empty(trim($msprice["inet"])) && empty(trim($msprice["inet_tv"])) && empty(trim($msprice["catv"])) && empty(trim($msprice["passive"]))) continue;
$msp = PreorderProductMarketshareDiscount::create([
"preorderproductprice_id" => $price->id,
"bracket" => $msbracket,
"price_inet" => Layout::commaToDot(trim($msprice["inet"])),
"price_inet_tv" => Layout::commaToDot(trim($msprice["inet_tv"])),
"price_catv" => Layout::commaToDot(trim($msprice["catv"])),
"price_passive" => Layout::commaToDot(trim($msprice["passive"])),
]);
$msp->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","",[], "product-".$netoperator_id."-".$product_id);
}
$price_campaign = PreorderProductPriceCampaign::create([
"preorderproductprice_id" => $price->id,
"preordercampaign_id" => $campaign->id,
]);
$price_campaign->save();
}
}
}
$this->layout()->setFlash("Neue Preise erflgreich gespeichert", "success");
$this->redirect("PreorderProduct","",[], "product-".$netoperator_id."-".$product_id);
}
}