Files
thetool/application/WarehouseItem/WarehouseItemController.php
2024-07-16 06:55:46 +00:00

77 lines
3.0 KiB
PHP

<?php
class WarehouseItemController extends TTCrud {
protected string $headerTitle = 'Eintrag';
protected string $createText = 'Eintrag erstellen';
// TODO: change articleId and warehouseLocationId to autocomplete
// @formatter:off
protected array $columns = [
['key' => 'articleId', 'text' => 'Artikel', 'required' => true, 'type' => 'select','table' => ['class' => 'text-nowrap'], 'modal' => ['items' => [], 'type' => 'select']],
['key' => 'warehouseLocationId', 'text' => 'Lagerort', 'required' => true, 'type' => 'select', 'modal' => ['items' => [], 'type' => 'select']],
['key' => 'quantity', 'text' => 'Menge', 'required' => true, 'type' => 'number'],
['key' => 'serialNumber', 'text' => 'Seriennummer', 'required' => false],
['key' => 'note', 'text' => 'Notiz', 'required' => false],
['key' => 'actions', 'text' => 'Aktionen', 'table' => ['filter' => false], 'required' => false, 'modal' => false]
];
// @formatter:on
protected array $additionalActions = [
['key' => 'openHistory', 'title' => 'Historie', 'class' => 'fas fa-history text-primary'],
];
protected array $infoMessages = [
'create' => 'Eintrag wurde erstellt',
'update' => 'Eintrag wurde aktualisiert',
'delete' => 'Eintrag wurde gelöscht',
'noChanges' => 'Keine Änderungen',
'alreadyExists' => 'Eintrag existiert bereits an diesem Lagerort.'
];
protected function checkExistingItemOnLocation($postData): bool {
$count = WarehouseItemModel::count(['articleId' => $postData['articleId'],
'warehouseLocationId' => $postData['warehouseLocationId'],
'serialNumber' => null]);
if ($count > 0) {
self::returnJson(['success' => false, 'message' => $this->infoMessages['alreadyExists']]);
return false;
}
return true;
}
protected function beforeCreate($postData): bool {
return $this->checkExistingItemOnLocation($postData);
}
protected function beforeUpdate($postData): bool {
$existing = $this->checkExistingItemOnLocation($postData);
if (!$existing) {
return false;
}
(new WarehouseHistoryController)->create($postData, $this->mod);
return true;
}
public function prepareCrudConfig() {
$articles = array_map(function($article) {
return ['value' => $article->id, 'text' => $article->title];
}, WarehouseArticleModel::getAll());
$this->columns[0]['modal']['items'] = $articles;
$locations = array_map(function($location) {
return ['value' => $location->id, 'text' => $location->title];
}, WarehouseLocationModel::getAll());
$this->columns[1]['modal']['items'] = $locations;
}
protected function getHistoryAction() {
self::returnJson((new WarehouseHistoryController)->getHistory($this->request->id, $this->mod, $this->columns));
}
}