Files
thetool/application/Vatgroup/VatgroupController.php
2024-02-27 14:35:25 +01:00

113 lines
3.6 KiB
PHP

<?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"]) || !$me->can("Fibu")) {
$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["legacy_account"] = (trim($rate["legacy_account"])) ? trim($rate["legacy_account"]) : null;
$data["rate"] = str_replace(",",".", trim($rate["rate"]));
$data["taxcode"] = ($rate["taxcode"]) ? trim($rate["taxcode"]) : null;
$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($data["legacy_account"] && !is_numeric($data["legacy_account"])) {
$this->layout()->setFlash("Ungültige Legacy Erlöskontonummer", "error");
$this->redirect("Vatgroup");
}
if(!is_numeric($data["rate"])) {
$this->layout()->setFlash("Ungültiger Steuersatz", "error");
$this->redirect("Vatgroup");
}
if($data["taxcode"] && !is_numeric($data["taxcode"])) {
$this->layout()->setFlash("Ungültiger Steuercode", "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");
}
}