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

114 lines
4.5 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->is(["Admin", "netowner", "salespartner"]) && !$me->can("Preorder")) {
$this->redirect("Dashboard");
}
}
protected function indexAction() {
$this->layout()->setTemplate("PreorderProduct/Index");
$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");
}
}