Merge branch 'fronkdev' into 'master'
Added Preorder discount code import and statusupdate import See merge request fronk/thetool!392
This commit is contained in:
@@ -7,6 +7,8 @@ class Preorder extends mfBaseModel {
|
||||
private $status;
|
||||
private $campaign;
|
||||
private $partner;
|
||||
private $discounts;
|
||||
private $applied_discounts;
|
||||
private $building;
|
||||
private $adb_hausnummer;
|
||||
private $adb_wohneinheit;
|
||||
@@ -623,7 +625,23 @@ class Preorder extends mfBaseModel {
|
||||
}
|
||||
return $this->partner;
|
||||
}
|
||||
|
||||
|
||||
if($name == "discounts") {
|
||||
$discounts = PreorderDiscountModel::search(["preorder_id" => $this->id]);
|
||||
if($discounts) {
|
||||
$this->discounts = $discounts;
|
||||
}
|
||||
return $this->discounts;
|
||||
}
|
||||
|
||||
if($name == "applied_discounts") {
|
||||
$discounts = PreorderDiscountModel::search(["preorder_id" => $this->id, "assigned" => true]);
|
||||
if($discounts) {
|
||||
$this->discounts = $discounts;
|
||||
}
|
||||
return $this->discounts;
|
||||
}
|
||||
|
||||
if($name == "building") {
|
||||
$this->building = mfValuecache::singleton()->get("mfObjectmodel-Building-".$this->building_id);
|
||||
if(!$this->building) {
|
||||
|
||||
@@ -756,7 +756,83 @@ class PreorderController extends mfBaseController {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function statusupdateimport() {
|
||||
$this->layout()->setTemplate("Preorder/Statusupdateimport");
|
||||
}
|
||||
|
||||
protected function saveStatusupdateimport() {
|
||||
$headline_included = $this->request->headline;
|
||||
|
||||
if(is_array($_FILES) && array_key_exists("statusupdate_csv", $_FILES) && !$_FILES['statusupdate_csv']['error']) {
|
||||
// look for uploaded import file
|
||||
try {
|
||||
// returns File object or throws Exception on error
|
||||
$file = mfUpload::handleFormUpload("statusupdate_csv");
|
||||
} catch(Exception $ex) {
|
||||
$this->layout()->setFlash("Fehler beim Dateiupload: " . $ex->getMessage(), "error");
|
||||
$this->redirect("Preorder", "statusupdateimport");
|
||||
}
|
||||
|
||||
$i = 0;
|
||||
$notfound = 0;
|
||||
$invalidcode = 0;
|
||||
$nochange = 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]);
|
||||
$new_status_code = trim($csv[1]);
|
||||
|
||||
$preorder = PreorderModel::getFirstActive(["oaid" => $oaid]);
|
||||
if(!$preorder) {
|
||||
$notfound++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if($preorder->status->code != $new_status_code) {
|
||||
$new_status = PreorderstatusModel::getFirst(["code" => $new_status_code]);
|
||||
if(!$new_status_code) {
|
||||
$invalidcode++;
|
||||
continue;
|
||||
}
|
||||
$preorder->status_id = $new_status->id;
|
||||
$preorder->save();
|
||||
$saved++;
|
||||
} else {
|
||||
$nochange++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$message = "Import erfolgreich. $saved Statusupdates importiert";
|
||||
if($notfound) {
|
||||
$message .= "<br />$notfound Bestellungen nicht gefunden";
|
||||
}
|
||||
if($nochange) {
|
||||
$message .= "<br />$nochange Bestelllungen haben bereits den neuen Status";
|
||||
}
|
||||
if($invalidcode) {
|
||||
$message .= "<br />$invalidcode ungültige Statuscodes";
|
||||
}
|
||||
$this->layout()->setFlash($message);
|
||||
$this->redirect("Preorder", "statusupdateimport");
|
||||
|
||||
} else {
|
||||
$this->layout()->setFlash("Nichts importiert!", "info");
|
||||
$this->redirect("Preorder", "statusupdateimport");
|
||||
}
|
||||
}
|
||||
|
||||
protected function exportAction() {
|
||||
$rfilter = $this->request->filter;
|
||||
if(!is_array($rfilter)) {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
class PreorderDiscount extends mfBaseModel {
|
||||
protected $forcestr = ['code'];
|
||||
private $preorder;
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
if($name == "creator") {
|
||||
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
|
||||
if($user) {
|
||||
$this->creator = $user;
|
||||
return $this->creator;
|
||||
}
|
||||
$this->creator = new User($this->create_by);
|
||||
if($this->creator->id) {
|
||||
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
|
||||
}
|
||||
return $this->creator;
|
||||
}
|
||||
|
||||
if($name == "editor") {
|
||||
$this->editor = new User($this->edit_by);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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;
|
||||
$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;
|
||||
}
|
||||
|
||||
$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($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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
class PreorderDiscountModel {
|
||||
public $preorder_id;
|
||||
public $code;
|
||||
public $info;
|
||||
public $assigned;
|
||||
|
||||
public $create_by = null;
|
||||
public $edit_by = null;
|
||||
public $create = null;
|
||||
public $edit = null;
|
||||
|
||||
public static function create(Array $data) {
|
||||
$model = new PreorderDiscount();
|
||||
|
||||
foreach($data as $field => $value) {
|
||||
if(property_exists(get_called_class(), $field)) {
|
||||
$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("PreorderDiscount", "*", "1 = 1 ORDER BY code");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new PreorderDiscount($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function getFirst($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT PreorderDiscount.* FROM PreorderDiscount
|
||||
WHERE $where
|
||||
LIMIT 1";
|
||||
//var_dump($sql);exit;
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new PreorderDiscount($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 COUT(*) as cnt FROM PreorderDiscount
|
||||
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) {
|
||||
//var_dump($filter);exit;
|
||||
$items = [];
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT PreorderDiscount.* FROM PreorderDiscount
|
||||
WHERE $where
|
||||
ORDER BY code";
|
||||
|
||||
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 PreorderDiscount($data);
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
if(array_key_exists("preorder_id", $filter)) {
|
||||
$preorder_id = $filter['preorder_id'];
|
||||
if(is_numeric($preorder_id)) {
|
||||
$where .= " AND PreorderDiscount.`preorder_id` = $preorder_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("code", $filter)) {
|
||||
$code = $db->escape($filter['code']);
|
||||
if($code) {
|
||||
$where .= " AND PreorderDiscount.`code` = '$code'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("assigned", $filter)) {
|
||||
$assigned = $filter['assigned'];
|
||||
if($assigned === true) {
|
||||
$where .= " AND PreorderDiscount.`assigned` > 0";
|
||||
}
|
||||
if($assigned === false || $assigned === null) {
|
||||
$where .= " AND (PreorderDiscount.`assigned` IS NULL OR PreorderDiscount.`assigned` = 0)";
|
||||
}
|
||||
}
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user