From e4427c9eac73a7951815413563f17435f1bef76e Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Thu, 25 Jul 2024 08:59:41 +0200 Subject: [PATCH] fixed some price handling aswell as tt-autocomplete and tt-table excel export always showing --- .../WarehouseArticleController.php | 107 +++++++++--------- .../WarehouseArticleDistributorController.php | 35 +++--- .../WarehouseArticlePriceController.php | 11 +- .../WarehouseArticlePriceTypeController.php | 9 +- lib/TTCrud/TTCrud.php | 2 +- .../WarehouseArticle/WarehouseArticle.js | 2 +- .../vue/tt-components/tt-autocomplete.js | 12 +- public/plugins/vue/tt-components/tt-table.js | 2 +- 8 files changed, 106 insertions(+), 74 deletions(-) diff --git a/application/WarehouseArticle/WarehouseArticleController.php b/application/WarehouseArticle/WarehouseArticleController.php index f26422b41..643fe6f8b 100644 --- a/application/WarehouseArticle/WarehouseArticleController.php +++ b/application/WarehouseArticle/WarehouseArticleController.php @@ -11,7 +11,7 @@ class WarehouseArticleController extends TTCrud { ['key' => 'category', 'text' => 'Kategorie', 'required' => true], ['key' => 'unit', 'text' => 'Einheit', 'required' => true,'table' => false], // Boolean value ['key' => 'defaultSellMultiplier', 'text' => 'Standard Multiplikator','regex' => '/^[0-9]*$/' , 'required' => true,'modal' => ['type' => 'number'], 'table' => false], // Boolean value - ['key' => 'revenueAccount', 'text' => 'Erlöskonto', 'required' => true,'modal' => ['type' => 'number'], 'table' => false], // Boolean value + ['key' => 'revenueAccount', 'text' => 'Erlöskonto', 'required' => true,'modal' => ['type' => 'select'], 'table' => false], // Boolean value ['key' => 'cheapestPurchasePrice', 'text' => 'Einkauf', 'modal' => false, 'table' => ['class' => 'text-center', 'suffix' => ' €']], ['key' => 'cheapestSellPrice', 'text' => 'Verkauf', 'modal' => false, 'table' => ['class' => 'text-center', 'suffix' => ' €']], ['key' => 'warningAmount', 'text' => 'Warnmenge', 'required' => true,'modal' => ['type' => 'number'], 'table' => ['class' => 'text-center']], // Stock/inventory related @@ -35,6 +35,15 @@ class WarehouseArticleController extends TTCrud { 'delete' => 'Artikel wurde gelöscht', 'noChanges' => 'Keine Änderungen',]; + public function prepareCrudConfig() { + $revenueAccounts = WarehouseRevenueAccountModel::getAll(); + $revenueAccounts = array_map(function ($revenueAccount) { + return ['value' => $revenueAccount->id, 'text' => $revenueAccount->title]; + }, $revenueAccounts); + + $this->columns[5]['modal']['items'] = $revenueAccounts; + } + protected function beforeUpdate($postData): bool { (new WarehouseHistoryController)->create($postData, $this->mod); return true; @@ -44,79 +53,75 @@ class WarehouseArticleController extends TTCrud { self::updateCheapestPurchasePrice($postData['id']); } - public static function updateCheapestPurchasePrice($articleId) { - $cheapestPurchasePrice = WarehouseArticleDistributorModel::getAll(['articleId' => $articleId], 1, 0, - ['key' => 'purchasePrice', 'order' => 'ASC'])[0]->purchasePrice; - - + /** + * Updates the cheapest purchase price for a given article from WarehouseArticleDistributorModel prices. + * + * @param int $articleId The ID of the article to update. + * @return void + * @throws Exception If the article is not an instance of WarehouseArticleModel. + */ + public static function updateCheapestPurchasePrice(int $articleId): void { $article = WarehouseArticleModel::get($articleId); if (!$article instanceof WarehouseArticleModel) { throw new Exception("Article is not an instance of WarehouseArticleModel"); } - //TODO: think of a new way as we have multiple sell prices now and article sellPriceOverride and sellPriceMultiplier do not exist anymore - // $cheapestSellPrice = $article->sellPriceOverride ?? $article->sellPriceMultiplier * $cheapestPurchasePrice; + $order = ['key' => 'purchasePrice', 'order' => 'ASC']; + $cheapestDistributorEntry = WarehouseArticleDistributorModel::getAll(['articleId' => $articleId], 1, 0, $order); - if ($article->cheapestPurchasePrice != $cheapestPurchasePrice) { - try { - WarehouseArticleModel::update(array_merge(get_object_vars($article), ['cheapestPurchasePrice' => $cheapestPurchasePrice])); - } catch (Exception $e) { - print_r($article); - echo PHP_EOL; - die("WarehouseArticleController::updateCheapestPurchasePrice: " . $e->getMessage()); - } + if (empty($cheapestDistributorEntry)) return; + + $cheapestPurchasePrice = $cheapestDistributorEntry[0]->purchasePrice; + if ($article->cheapestPurchasePrice == $cheapestPurchasePrice) return; + WarehouseArticleModel::update(array_merge(get_object_vars($article), ['cheapestPurchasePrice' => $cheapestPurchasePrice])); + } + + /** + * Updates the sell prices for a given article. + * + * @param int $articleId The ID of the article to update. + * @return void + * @throws Exception If the article is not an instance of WarehouseArticleModel. + */ + public static function updateSellPrices(int $articleId) { + $article = WarehouseArticleModel::get($articleId); + if (!$article instanceof WarehouseArticleModel) { + throw new Exception("Article is not an instance of WarehouseArticleModel"); } - //TODO: start update cheapestSellPrice for each PriceType - - $priceTypes = WarehouseArticlePriceTypeModel::getAll(); + $priceTypes = WarehouseArticlePriceTypeModel::getAll(); $articlePriceTypes = WarehouseArticlePriceModel::getAll(['articleId' => $articleId]); - //priceType has id, title $cheapestSellPrices = []; - + // Calculate sell prices for each price type, use default sell multiplier if no specific price is set foreach ($priceTypes as $priceType) { - // check if priceType exists in articlePriceTypes else use $article->defaultSellMultiplier for price type - $articlePriceType = array_filter($articlePriceTypes, function ($articlePriceType) use ($priceType) { - return $articlePriceType->articlePriceTypeId == $priceType->id; + $articlePriceType = array_filter($articlePriceTypes, function ($apt) use ($priceType) { + return $apt->articlePriceTypeId == $priceType->id; }); - if (empty($articlePriceType)) { - $sellPrice = $article->defaultSellMultiplier * $cheapestPurchasePrice; - } else { + $sellPrice = $article->defaultSellMultiplier * $article->cheapestPurchasePrice; + if (!empty($articlePriceType)) { $articlePriceType = $articlePriceType[0]; - - if ($articlePriceType->priceOverride) { - $sellPrice = $articlePriceType->priceOverride; - } else { - $sellPrice = $articlePriceType->priceMultiplier * $cheapestPurchasePrice; - } + $sellPrice = $articlePriceType->priceOverride ?: $articlePriceType->priceMultiplier * $article->cheapestPurchasePrice; } - $cheapestSellPrices[] = [ - 'title' => $priceType->title, - 'price' => $sellPrice, - ]; + $cheapestSellPrices[$priceType->id] = ['title' => $priceType->title, 'price' => $sellPrice]; } - // save cheapestSellPrices column on article - $article->cheapestSellPrice = json_encode($cheapestSellPrices); WarehouseArticleModel::update(get_object_vars($article)); } - protected function afterCreate() { - $last5Articles = WarehouseArticleModel::getAll([], 5, 0, ['key' => 'id', 'order' => 'DESC']); - foreach ($last5Articles as $article) { - self::updateCheapestPurchasePrice($article->id); - } + protected function afterCreate($postData) { + self::updateCheapestPurchasePrice($postData['id']); + self::updateSellPrices($postData['id']); } - protected function updateAllCheapestPurchasePricesAction() { - $articles = WarehouseArticleModel::getAll(); - foreach ($articles as $article) { + protected function updatePricesAction() { + foreach (WarehouseArticleModel::getAll() as $article) { self::updateCheapestPurchasePrice($article->id); + self::updateSellPrices($article->id); } } @@ -226,12 +231,12 @@ class WarehouseArticleController extends TTCrud { // Check if ArticleDistributor exists $articleDistributor = WarehouseArticleDistributorModel::getAll(['articleId' => $articleId, - 'distributorId' => $distributorId]); + 'distributorId' => $distributorId]); if (empty($articleDistributor)) { WarehouseArticleDistributorModel::create(['articleId' => $articleId, - 'distributorId' => $distributorId, - 'purchasePrice' => $purchasePrice, - 'externalArticleNumber' => $item['Lieferant/ Hersteller Artikelnr:'],]); + 'distributorId' => $distributorId, + 'purchasePrice' => $purchasePrice, + 'externalArticleNumber' => $item['Lieferant/ Hersteller Artikelnr:'],]); } } diff --git a/application/WarehouseArticleDistributor/WarehouseArticleDistributorController.php b/application/WarehouseArticleDistributor/WarehouseArticleDistributorController.php index 3c7fe4503..73aa7ca14 100644 --- a/application/WarehouseArticleDistributor/WarehouseArticleDistributorController.php +++ b/application/WarehouseArticleDistributor/WarehouseArticleDistributorController.php @@ -20,25 +20,33 @@ class WarehouseArticleDistributorController extends TTCrud { 'delete' => 'Lieferanteintrag wurde gelöscht', 'noChanges' => 'Keine Änderungen',]; - protected function checkExistingDistributorEntry($postData): bool { - $count = WarehouseArticleDistributorModel::count(['articleId' => $postData['articleId'], - 'distributorId' => $postData['distributorId']]); - - if ($count > 0) { - self::returnJson(['success' => false, - 'message' => 'Es existiert bereits ein Eintrag mit dieser Artikelnummer und diesem Lieferanten.']); - return false; - } - - return true; - } - protected function beforeCreate($postData): bool { return $this->checkExistingDistributorEntry($postData); } + protected function checkExistingDistributorEntry($postData): bool { + if (isset($postData['id'])) { + $count = WarehouseArticleDistributorModel::count(['articleId' => $postData['articleId'], + 'distributorId' => $postData['articlePriceTypeId'], + 'id' => $postData['id']]); + + if ($count > 0) return true; + } else { + $count = WarehouseArticleDistributorModel::count(['articleId' => $postData['articleId'], + 'distributorId' => $postData['distributorId']]); + if ($count > 0) { + self::returnJson(['success' => false, + 'message' => 'Es existiert bereits ein Eintrag mit dieser Artikelnummer und diesem Lieferanten.']); + return false; + + } + } + return true; + } + protected function afterCreate($postData) { WarehouseArticleController::updateCheapestPurchasePrice($postData['articleId']); + WarehouseArticleController::updateSellPrices($postData['articleId']); } protected function beforeUpdate($postData): bool { @@ -55,6 +63,7 @@ class WarehouseArticleDistributorController extends TTCrud { protected function afterUpdate($postData) { WarehouseArticleController::updateCheapestPurchasePrice($postData['articleId']); + WarehouseArticleController::updateSellPrices($postData['articleId']); } protected function getHistoryAction() { diff --git a/application/WarehouseArticlePrice/WarehouseArticlePriceController.php b/application/WarehouseArticlePrice/WarehouseArticlePriceController.php index 91cd91ba2..5f27f25fa 100644 --- a/application/WarehouseArticlePrice/WarehouseArticlePriceController.php +++ b/application/WarehouseArticlePrice/WarehouseArticlePriceController.php @@ -22,6 +22,7 @@ class WarehouseArticlePriceController extends TTCrud { 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'] === '') { @@ -42,11 +43,11 @@ class WarehouseArticlePriceController extends TTCrud { 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'], @@ -86,4 +87,12 @@ class WarehouseArticlePriceController extends TTCrud { self::returnJson($history); } + public function afterCreate($postData) { + WarehouseArticleController::updateSellPrices($postData['articleId']); + } + + public function afterUpdate($postData) { + WarehouseArticleController::updateSellPrices($postData['articleId']); + } + } \ No newline at end of file diff --git a/application/WarehouseArticlePriceType/WarehouseArticlePriceTypeController.php b/application/WarehouseArticlePriceType/WarehouseArticlePriceTypeController.php index affb82be4..e9cc254fb 100644 --- a/application/WarehouseArticlePriceType/WarehouseArticlePriceTypeController.php +++ b/application/WarehouseArticlePriceType/WarehouseArticlePriceTypeController.php @@ -51,13 +51,12 @@ class WarehouseArticlePriceTypeController extends TTCrud { return true; } - protected function afterUpdate($postData) { - WarehouseArticleController::updateCheapestPurchasePrice($postData['articleId']); + public function afterCreate($postData) { + WarehouseArticleController::updateSellPrices($postData['articleId']); } - protected function afterCreate($postData) { - //TODO: fix this - WarehouseArticleController::updateCheapestPurchasePrice($postData['articleId']); + public function afterUpdate($postData) { + WarehouseArticleController::updateSellPrices($postData['articleId']); } protected function getHistoryAction() { diff --git a/lib/TTCrud/TTCrud.php b/lib/TTCrud/TTCrud.php index 22be1fda7..fde700436 100644 --- a/lib/TTCrud/TTCrud.php +++ b/lib/TTCrud/TTCrud.php @@ -123,7 +123,7 @@ class TTCrud extends mfBaseController { $id = $this->model::create($this->postData); if (method_exists($this, 'afterCreate')) { - $this->afterCreate($this->postData); + $this->afterCreate(array_merge($this->postData, ['id' => $id])); } self::returnJson(['success' => true, diff --git a/public/js/pages/WarehouseArticle/WarehouseArticle.js b/public/js/pages/WarehouseArticle/WarehouseArticle.js index 34149983b..0d9b6d676 100644 --- a/public/js/pages/WarehouseArticle/WarehouseArticle.js +++ b/public/js/pages/WarehouseArticle/WarehouseArticle.js @@ -37,7 +37,7 @@ Vue.component('warehouse-distributor-modal', { this.rows = response.data.rows }, addRow() { - this.rows.push({distributor: null, price: null, externalArticleNumber: null}); + this.rows.push({distributorId: undefined, price: null, externalArticleNumber: null}); }, async saveRow(index) { // post to /WarehouseArticleDistributor/save with rows data and articleId const row = this.rows[index]; diff --git a/public/plugins/vue/tt-components/tt-autocomplete.js b/public/plugins/vue/tt-components/tt-autocomplete.js index b214800b5..9cd962e4f 100644 --- a/public/plugins/vue/tt-components/tt-autocomplete.js +++ b/public/plugins/vue/tt-components/tt-autocomplete.js @@ -25,7 +25,12 @@ Vue.component('tt-autocomplete', {