Feature/add e shop
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?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' => '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' => '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(
|
||||
['isEShop' => 1],
|
||||
));
|
||||
|
||||
$this->columns[5]['modal']['items'] = $articles;
|
||||
}
|
||||
|
||||
//TODO: make this so it does not update all packets at the same time
|
||||
protected function updatePacketPricesAction() {
|
||||
$packets = WarehouseArticlePacketModel::getAll();
|
||||
$articles = WarehouseArticleModel::getAll(['isEShop' => 1]);
|
||||
|
||||
// packet has $calculatedSellPrice for this but when overrideSellPrice is set, it should be used
|
||||
|
||||
foreach ($packets as $packet) {
|
||||
if ($packet->overrideSellPrice) {
|
||||
$calculatedSellPrice = $packet->overrideSellPrice;
|
||||
} else {
|
||||
$subItems = json_decode($packet->subItems);
|
||||
$calculatedSellPrice = 0;
|
||||
|
||||
foreach ($subItems as $subItem) {
|
||||
$article = WarehouseArticleModel::get($subItem->id);
|
||||
$cheapestSellPrices = json_decode($article->cheapestSellPrice);
|
||||
// find in array cheapestSellPrices by title === 'Energie Steiermark' and get the price
|
||||
$articlePrice = array_values(array_filter($cheapestSellPrices, function ($cheapestSellPrice) {
|
||||
return $cheapestSellPrice->title === 'Energie Steiermark';
|
||||
}))[0]->price;
|
||||
|
||||
$calculatedSellPrice += $subItem->amount * $articlePrice;
|
||||
}
|
||||
}
|
||||
|
||||
WarehouseArticlePacketModel::update([...get_object_vars($packet), // Unpack properties into an array
|
||||
'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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user