Added Vatgroup & Vatrate
This commit is contained in:
37
application/Vatgroup/Vatgroup.php
Normal file
37
application/Vatgroup/Vatgroup.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
class Vatgroup extends mfBaseModel {
|
||||
private $rates;
|
||||
|
||||
private function loadRates() {
|
||||
foreach(VatrateModel::search(["vatgroup_id" => $this->id]) as $rate) {
|
||||
$this->rates[$rate->area] = $rate;
|
||||
}
|
||||
return $this->rates;
|
||||
}
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
if(!$this->id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if($name == "rates") {
|
||||
return $this->loadRates();
|
||||
}
|
||||
|
||||
$classname = ucfirst($name);
|
||||
$idfield = $name."_id";
|
||||
$this->$name = new $classname($this->$idfield);
|
||||
|
||||
if($this->$name->id) {
|
||||
return $this->$name;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->$name;
|
||||
}
|
||||
}
|
||||
103
application/Vatgroup/VatgroupController.php
Normal file
103
application/Vatgroup/VatgroupController.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
class VatgroupController 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"])) {
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function indexAction() {
|
||||
$vatgroups = VatgroupModel::getAll();
|
||||
$this->layout()->set("vatgroups", $vatgroups);
|
||||
}
|
||||
|
||||
protected function addGroupAction() {
|
||||
$name = $this->request->group_name;
|
||||
if(!$name) {
|
||||
$this->layout()->setFlash("Bitte einen Namen angeben!", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
if(VatgroupModel::getFirst(["name" => $name])) {
|
||||
$this->layout()->setFlash("Eine Steuersatzgruppe mit diesem Namen ist bereits vorhanden!", "warning");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
$group = VatgroupModel::create([
|
||||
"name" => $name
|
||||
]);
|
||||
|
||||
if(!$group->save()) {
|
||||
$this->layout()->setFlash("Fehler beim Anlegen der Steuersatzgruppe!", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("Steuersatzgruppe erfolgreic erstellt!", "success");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
protected function saveAction() {
|
||||
$id = $this->request->id;
|
||||
if(!is_numeric($id) || $id < 1) {
|
||||
$this->layout()->setFlash("Ungültige Steuersatzgruppe", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
$group = new Vatgroup($id);
|
||||
if(!$group->id) {
|
||||
$this->layout()->setFlash("Ungültige Steuersatzgruppe", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
foreach($this->request->rates as $area => $rate) {
|
||||
if(!$rate['account'] && !$rate['rate']) continue;
|
||||
|
||||
if(!$area || !array_key_exists($area, TT_VATRATE_AREAS)) {
|
||||
$this->layout()->setFlash("Ungültiges Zielland", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data["vatgroup_id"] = $group->id;
|
||||
$data["area"] = $area;
|
||||
$data["account"] = trim($rate["account"]);
|
||||
$data["rate"] = str_replace(",",".", trim($rate["rate"]));
|
||||
$data["invoice_text"] = (trim($rate["invoice_text"])) ? trim($rate["invoice_text"]) : null;
|
||||
|
||||
if(!$data["account"] || !is_numeric($data["account"])) {
|
||||
$this->layout()->setFlash("Ungültige Erlöskontonummer", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
if(!is_numeric($data["rate"])) {
|
||||
$this->layout()->setFlash("Ungültiger Steuersatz", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
|
||||
$vatrate = VatrateModel::getFirst(["vatgroup_id" => $group->id, "area" => $area]);
|
||||
if($vatrate) {
|
||||
$vatrate->update($data);
|
||||
} else {
|
||||
$vatrate = VatrateModel::create($data);
|
||||
}
|
||||
|
||||
if(!$vatrate->save()) {
|
||||
$this->layout()->setFlash("Fehler beim Speichern eines Steuersatzes", "error");
|
||||
$this->redirect("Vatgroup");
|
||||
}
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("Steuersätze erfolgreich gespeichert", "success");
|
||||
$this->redirect("Vatgroup");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
133
application/Vatgroup/VatgroupModel.php
Normal file
133
application/Vatgroup/VatgroupModel.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
class VatgroupModel {
|
||||
public $name;
|
||||
|
||||
public $create_by = null;
|
||||
public $edit_by = null;
|
||||
public $create = null;
|
||||
public $edit = null;
|
||||
|
||||
public static function create(Array $data) {
|
||||
$model = new Vatgroup();
|
||||
|
||||
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("Vatgroup", "*", "1 = 1 ORDER BY name");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new Vatgroup($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function getFirst($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT Vatgroup.* FROM Vatgroup
|
||||
WHERE $where
|
||||
LIMIT 1";
|
||||
//var_dump($sql);exit;
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new Vatgroup($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 Vatgroup
|
||||
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 Vatgroup.* FROM Vatgroup
|
||||
WHERE $where
|
||||
ORDER BY Vatgroup.name";
|
||||
|
||||
if(is_array($limit) && count($limit)) {
|
||||
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
|
||||
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
|
||||
} elseif(is_numeric($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 Vatgroup($data);
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
if(array_key_exists("name", $filter)) {
|
||||
$name = $db->escape($filter['name']);
|
||||
if($name) {
|
||||
$where .= " AND Vatgroup.`name` = '$name'";
|
||||
}
|
||||
}
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user