Files
thetool/application/WarehouseArticlePacket/WarehouseArticlePacketController.php
2025-07-21 13:08:54 +02:00

123 lines
6.3 KiB
PHP

<?php
class WarehouseArticlePacketController extends TTCrud {
protected string $headerTitle = 'Artikel-Pakete';
protected string $createText = 'Artikel-Paket erstellen';
// @formatter:off
protected array $columns = [
['key' => 'title', 'text' => 'Titel', 'required' => true],
['key' => 'externalArticleNumber', 'text' => 'Artikel Nummer Trio', 'required' => false],
['key' => 'description', 'text' => 'Beschreibung', 'required' => true],
['key' => 'category', 'text' => 'Kategorie', 'required' => true],
['key' => 'overrideSellPrice', 'text' => 'Überschriebener Verkaufspreis', 'required' => false, 'modal' => ['type' => 'number'], 'table' => false],
['key' => 'calculatedSellPrice', 'text' => 'Verkaufspreis', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false]],
['key' => 'subItems', 'text' => 'Unterartikel', 'required' => true],
['key' => 'isEShop', 'text' => 'E-Shop', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'isEShopHide', 'text' => 'Hide', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'isSbidiShop', 'text' => 'S-Shop', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'isSbidiShopHide', 'text' => 'Hide', 'required' => false, 'modal' => ['type' => 'checkbox', 'items' => [['value' => 1, 'icon' => 'fas fa-check-circle text-success', 'text' => 'Ja'], ['value' => 0, 'icon' => 'fas fa-times-circle text-danger', 'text' => 'Nein']]], 'table' => ['filter' => 'iconSelect']],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center', 'priority' => 10]],
];
// @formatter:on
protected array $infoMessages = ['create' => 'Artikel-Paket wurde erstellt',
'update' => 'Artikel-Paket wurde aktualisiert',
'delete' => 'Artikel-Paket wurde gelöscht',
'noChanges' => 'Keine Änderungen'];
protected function prepareCrudConfig() {
$articles = array_map(function ($article) {
return ['value' => $article->id, 'text' => $article->title];
}, WarehouseArticleModel::getAll(
// Filter articles based on the user's address_id for the shop context
($this->user->address_id === 209) ? ['isEShop' => 1] : (($this->user->address_id === 210) ? ['isSbidiShop' => 1] : []),
));
$this->columns[6]['modal']['items'] = $articles;
}
//TODO: make this so it does not update all packets at the same time
protected function updatePacketPricesAction() {
$packets = WarehouseArticlePacketModel::getAll();
// Determine which shop's articles to use for price calculation based on the current user's shop context
// This is a simplification; in a multi-tenant system, this might need to be more robust,
// e.g., by iterating through all possible shop types or having a dedicated price calculation service.
$shopPriceTitle = '';
$articleFilter = [];
if ($this->user->address_id === 209) {
$shopPriceTitle = 'Energie Steiermark';
$articleFilter['isEShop'] = 1;
} elseif ($this->user->address_id === 210) {
$shopPriceTitle = 'Sbidi';
$articleFilter['isSbidiShop'] = 1;
}
$articles = WarehouseArticleModel::getAll($articleFilter);
foreach ($packets as $packet) {
if ($packet->overrideSellPrice) {
$calculatedSellPrice = $packet->overrideSellPrice;
} else {
$subItems = json_decode($packet->subItems);
$calculatedSellPrice = 0;
foreach ($subItems as $subItem) {
$article = null;
// Find the article by ID from the already fetched articles to avoid N+1 queries
foreach ($articles as $a) {
if ($a->id == $subItem->id) {
$article = $a;
break;
}
}
if ($article) {
$cheapestSellPrices = json_decode($article->cheapestSellPrice, true);
$articlePrice = 0;
// Find price for the specific shop
$foundPrice = array_values(array_filter($cheapestSellPrices, function ($cheapestSellPrice) use ($shopPriceTitle) {
return $cheapestSellPrice['title'] === $shopPriceTitle;
}));
$articlePrice = $foundPrice[0]['price'] ?? 0;
$calculatedSellPrice += $subItem->amount * $articlePrice;
}
}
}
WarehouseArticlePacketModel::update(array_merge(get_object_vars($packet), ['calculatedSellPrice' => $calculatedSellPrice]));
}
return true;
}
protected function afterUpdate(): bool {
return $this->updatePacketPricesAction();
}
protected function afterCreate(): bool {
return $this->updatePacketPricesAction();
}
protected function beforeUpdate($postData): bool {
(new WarehouseHistoryController)->create($postData, $this->mod);
return true;
}
protected function getHistoryAction() {
$history = WarehouseHistoryModel::getByRowId($this->request->id, $this->mod);
$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);
}
}