Files
thetool/application/WarehouseAdministration/WarehouseAdministrationController.php

115 lines
4.6 KiB
PHP

<?php
class WarehouseAdministrationController extends mfBaseController {
private User $me;
protected function init(): void {
$me = new User();
$me->loadMe();
$this->layout()->set("me", $me);
$this->me = $me;
if (!$this->me->isAdmin()) {
$this->redirect("dashboard");
}
}
protected function indexAction(): void {
$this->layout()->set('additionalJS', ['js/pages/WarehouseHistory/WarehouseHistoryModal.js']);
Helper::renderVue($this, 'WarehouseAdministration', 'Administration-Tools', ["CREATE_URL" => $this::getUrl($this->mod . "/create"),
"TABLE_URL" => $this::getUrl($this->mod . "/get"),
"UPDATE_URL" => $this::getUrl($this->mod . "/update"),
"DELETE_URL" => $this::getUrl($this->mod . "/delete"),]);
}
//TODO: this needs improvement as it is inefficient but it doesnt matter as it doesnt get called very often
// and also maybe we should move it to WarehouseLocationController
protected function createLocationsAction(): void {
$existingLocations = WarehouseLocationModel::getAll();
$companyCars = TimerecordingCarModel::getAll();
$beforeCount = WarehouseLocationModel::count();
$wantedCarLocations = [];
foreach ($companyCars as $car) {
// check if $car->brand includes "Anhänger" or "Anhaenger", if yes then continue
if (strpos($car->brand, "Anhänger") !== false || strpos($car->brand, "Anhaenger") !== false) {
continue;
}
$carModelParts = explode(" ", $car->model);
if (count($carModelParts) > 1) {
$wantedCarLocations[] = "{$car->number_plate} {$car->brand} {$carModelParts[0]} {$carModelParts[1]}";
} else {
$wantedCarLocations[] = "{$car->number_plate} {$car->brand} {$carModelParts[0]}";
}
}
// create a warehouse location for each wantedcar but check if $existingLocations[]->title already exists with the same title
foreach ($wantedCarLocations as $wantedCarLocation) {
$locationExists = false;
foreach ($existingLocations as $existingLocation) {
if ($existingLocation->title === $wantedCarLocation) {
$locationExists = true;
break;
}
}
if (!$locationExists) {
$numberPlate = explode(" ", $wantedCarLocation)[0];
$assignedTo = 1;
foreach ($companyCars as $car) {
if ($car->number_plate === $numberPlate) {
$assignedTo = $car->user_id;
break;
}
}
if ($assignedTo === null) {
$assignedTo = 6;
}
WarehouseLocationModel::create([
"title" => $wantedCarLocation,
"description" => "Automatisch erstellt",
"assignedTo" => $assignedTo,
"createBy" => $this->me->id,
"create" => time()
]);
}
}
$existingLocations = WarehouseLocationModel::getAll();
$users = UserModel::search(['employee' => true]);
// now create a warehouse location for each user only if they dont already have one (for example if they have a company car)
foreach ($users as $user) {
$locationExists = false;
foreach ($existingLocations as $existingLocation) {
if (intval($existingLocation->assignedTo) === intval($user->id)) {
$locationExists = true;
break;
}
}
if (!$locationExists) {
WarehouseLocationModel::create([
"title" => $user->name . "'s Lagerort",
"description" => "Automatisch erstellt",
"assignedTo" => $user->id,
"createBy" => $this->me->id,
"create" => time()
]);
}
}
$afterCount = WarehouseLocationModel::count();
if ($afterCount - $beforeCount === 0) {
$this->returnJSON(["success" => true, "message" => "Alle Lagerorte existieren bereits"]);
} else {
$this->returnJSON(["success" => true, "message" => "Es wurden " . ($afterCount - $beforeCount) . " neue Lagerorte erstellt"]);
}
}
}