From 25f5f98b75c546969cc83c019678cde16bb6af03 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Wed, 16 Dec 2020 17:44:51 +0100 Subject: [PATCH] Fixed undoing a consume transaction of an opened item added it back to stock unopened (fixes #1191) --- changelog/60_UNRELEASED_2020-xx-xx.md | 1 + services/StockService.php | 13 ++++--------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/changelog/60_UNRELEASED_2020-xx-xx.md b/changelog/60_UNRELEASED_2020-xx-xx.md index c97ef2ea..81477153 100644 --- a/changelog/60_UNRELEASED_2020-xx-xx.md +++ b/changelog/60_UNRELEASED_2020-xx-xx.md @@ -82,6 +82,7 @@ - Fixed that consuming (and editing the amount of) products with enabled tare weight handling did not work on the stock entries page - Fixed that the recipes dropdown on the consume page also displayed internal recipes (thanks @kriddles) - Fixed that opening tare weight handling enabled products is not possible via the UI and the API (as this makes no sense) +- Fixed that undoing a consume transaction of an opened item added it back to stock unopened ### Shopping list improvements - Decimal amounts are now allowed (for any product, rounded by two decimal places) diff --git a/services/StockService.php b/services/StockService.php index 809610e9..8bcb6f2f 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -1125,7 +1125,6 @@ class StockService extends BaseService public function UndoBooking($bookingId, $skipCorrelatedBookings = false) { $logRow = $this->getDatabase()->stock_log()->where('id = :1 AND undone = 0', $bookingId)->fetch(); - if ($logRow == null) { throw new \Exception('Booking does not exist or was already undone'); @@ -1171,7 +1170,8 @@ class StockService extends BaseService 'purchased_date' => $logRow->purchased_date, 'stock_id' => $logRow->stock_id, 'price' => $logRow->price, - 'opened_date' => $logRow->opened_date + 'opened_date' => $logRow->opened_date, + 'open' => $logRow->opened_date !== null ]); $stockRow->save(); @@ -1184,14 +1184,12 @@ class StockService extends BaseService elseif ($logRow->transaction_type === self::TRANSACTION_TYPE_TRANSFER_TO) { $stockRow = $this->getDatabase()->stock()->where('stock_id = :1 AND location_id = :2', $logRow->stock_id, $logRow->location_id)->fetch(); - if ($stockRow === null) { throw new \Exception('Booking does not exist or was already undone'); } $newAmount = $stockRow->amount - $logRow->amount; - if ($newAmount == 0) { $stockRow->delete(); @@ -1212,10 +1210,8 @@ class StockService extends BaseService } elseif ($logRow->transaction_type === self::TRANSACTION_TYPE_TRANSFER_FROM) { - // Add corresponding amount back to stock or - // create a row if missing + // Add corresponding amount back to stock $stockRow = $this->getDatabase()->stock()->where('stock_id = :1 AND location_id = :2', $logRow->stock_id, $logRow->location_id)->fetch(); - if ($stockRow === null) { $stockRow = $this->getDatabase()->stock()->createRow([ @@ -1244,7 +1240,7 @@ class StockService extends BaseService } elseif ($logRow->transaction_type === self::TRANSACTION_TYPE_PRODUCT_OPENED) { - // Remove opened flag from corresponding log entry + // Remove opened flag from corresponding stock entry $stockRows = $this->getDatabase()->stock()->where('stock_id = :1 AND amount = :2 AND purchased_date = :3', $logRow->stock_id, $logRow->amount, $logRow->purchased_date)->limit(1); $stockRows->update([ 'open' => 0, @@ -1277,7 +1273,6 @@ class StockService extends BaseService $openedDate = $logRow->opened_date; $open = true; - if ($openedDate == null) { $open = false;