102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
|
|
|
class PreorderDiscountController 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", "preorderfront"])) {
|
|
$this->redirect("Dashboard");
|
|
}
|
|
}
|
|
|
|
protected function importAction() {
|
|
$this->layout()->setTemplate("PreorderDiscount/Import");
|
|
}
|
|
|
|
protected function saveImport() {
|
|
$headline_included = $this->request->headline;
|
|
|
|
if(is_array($_FILES) && array_key_exists("discount_csv", $_FILES) && !$_FILES['discount_csv']['error']) {
|
|
// look for uploaded import file
|
|
try {
|
|
// returns File object or throws Exception on error
|
|
$file = mfUpload::handleFormUpload("discount_csv");
|
|
} catch(Exception $ex) {
|
|
$this->layout()->setFlash("Fehler beim Dateiupload: " . $ex->getMessage(), "error");
|
|
$this->redirect("PreorderDiscount", "import");
|
|
}
|
|
|
|
$i = 0;
|
|
$forbidden = 0;
|
|
$notfound = 0;
|
|
$exists = 0;
|
|
$saved = 0;
|
|
|
|
$filename = $file->getFullPath();
|
|
$input = fopen($filename, "r");
|
|
while($csv = fgetcsv($input, 0, ";")) {
|
|
$i++;
|
|
if($i == 1 && $headline_included) continue;
|
|
|
|
if(!trim($csv[0])) {
|
|
continue;
|
|
}
|
|
|
|
$oaid = trim($csv[0]);
|
|
$code = trim($csv[1]);
|
|
|
|
$preorder = PreorderModel::getFirstActive(["oaid" => $oaid]);
|
|
if(!$preorder) {
|
|
$notfound++;
|
|
continue;
|
|
}
|
|
|
|
if($preorder->campaign->network->owner_id != $this->me->address_id) {
|
|
$forbidden++;
|
|
continue;
|
|
}
|
|
|
|
$discount_code = PreorderDiscountModel::getFirst(["code" => $code, "preorer_id" => $preorder->id]);
|
|
if($discount_code) {
|
|
$exists++;
|
|
continue;
|
|
}
|
|
|
|
$discount_code = PreorderDiscountModel::create([
|
|
"code" => $code,
|
|
"preorder_id" => $preorder->id,
|
|
"assigned" => date('U')
|
|
]);
|
|
|
|
if(!$discount_code->save()) {
|
|
$this->layout()->setFlash("Fehler beim Speichern!", "error");
|
|
$this->redirect("PreorderDiscount", "import");
|
|
}
|
|
|
|
$saved++;
|
|
}
|
|
|
|
$message = "Import erfolgreich. $saved Gutscheincodes importiert";
|
|
if($notfound) {
|
|
$message .= "<br />$notfound Bestellungen nicht gefunden";
|
|
}
|
|
if($forbidden) {
|
|
$message .= "<br />$forbidden Bestellungen in falschem Netzgebiet";
|
|
}
|
|
if($exists) {
|
|
$message .= "<br />$exists Gutscheincodes schon verknüpft";
|
|
}
|
|
$this->layout()->setFlash($message);
|
|
$this->redirect("PreorderDiscount", "import");
|
|
|
|
} else {
|
|
$this->layout()->setFlash("Nichts importiert!", "info");
|
|
$this->redirect("PreorderDiscount", "import");
|
|
}
|
|
}
|
|
} |