Files
thetool/application/WarehouseArticlePrice/WarehouseArticlePriceController.php

98 lines
4.0 KiB
PHP

<?php
class WarehouseArticlePriceController extends TTCrud {
protected string $headerTitle = 'Artikel Verkaufspreise';
protected string $createText = 'Artikel Verkaufspreis erstellen';
// @formatter:off
protected array $columns = [
['key' => 'articleId', 'text' => 'Artikel', 'required' => true],
['key' => 'articlePriceTypeId', 'text' => 'Preistyp', 'required' => true],
['key' => 'priceMultiplier', 'text' => 'Preismultiplikator', 'required' => false],
['key' => 'priceOverride', 'text' => 'Preisüberschreibung', 'required' => false]
];
// @formatter:on
protected array $infoMessages = ['create' => 'Artikel Verkaufspreis wurde erstellt',
'update' => 'Artikel Verkaufspreis wurde aktualisiert',
'delete' => 'Artikel Verkaufspreis wurde gelöscht',
'noChanges' => 'Keine Änderungen'];
protected function beforeCreate($postData): bool {
return $this->validate($postData);
}
//TODO: phpdoc and simplify
protected function validate($postData): bool {
// if either priceOverride or priceMultiplier is empty set it to null
if (isset($postData['priceOverride']) && $postData['priceOverride'] === '') {
$postData['priceOverride'] = null;
}
if (isset($postData['priceMultiplier']) && $postData['priceMultiplier'] === '') {
$postData['priceMultiplier'] = null;
}
// check if postData priceOverride or priceMultiplier is set but only one of them
if (isset($postData['priceOverride']) && isset($postData['priceMultiplier'])) {
self::returnJson(['success' => false,
'message' => 'Entweder Preismultiplikator oder Preisüberschreibung kann gesetzt werden.']);
return false;
} else if (!isset($postData['priceOverride']) && !isset($postData['priceMultiplier'])) {
self::returnJson(['success' => false,
'message' => 'Entweder Preismultiplikator oder Preisüberschreibung muss gesetzt werden.']);
return false;
}
if (isset($postData['id'])) {
$count = WarehouseArticlePriceModel::count(['articleId' => $postData['articleId'],
'articlePriceTypeId' => $postData['articlePriceTypeId'],
'id' => $postData['id']]);
if ($count > 0) return true;
} else {
$count = WarehouseArticlePriceModel::count(['articleId' => $postData['articleId'],
'articlePriceTypeId' => $postData['articlePriceTypeId']]);
if ($count > 0) {
self::returnJson(['success' => false,
'message' => 'Es existiert bereits ein Preis für diesen Artikel und diesen Preistyp.']);
return false;
}
}
return true;
}
protected function beforeUpdate($postData): bool {
$existing = $this->validate($postData);
if (!$existing) {
return false;
}
(new WarehouseHistoryController)->create($postData, $this->mod);
return true;
}
protected function getHistoryAction() {
$history = WarehouseHistoryModel::getByRowId($this->request->id, 'WarehouseArticleDistributor');
$history = array_map(function ($item) {
$item = (array) $item;
$item['columnHeader'] = $this->columns[array_search($item['key'], array_column($this->columns, 'key'))]['text'];
return $item;
}, $history);
self::returnJson($history);
}
public function afterCreate($postData) {
WarehouseArticleController::updateSellPrices($postData['articleId']);
}
public function afterUpdate($postData) {
WarehouseArticleController::updateSellPrices($postData['articleId']);
}
}