From 9bbcdafab982b99b05bdc362ac294b4beb1ffe54 Mon Sep 17 00:00:00 2001 From: kriddles <54413450+kriddles@users.noreply.github.com> Date: Thu, 29 Oct 2020 11:04:34 -0500 Subject: [PATCH] Add purchased date to inventory (#1102) * Add purchased date to inventory * Clarify stock settings label Co-authored-by: Bernd Bestel --- controllers/StockApiController.php | 9 ++++++++- localization/strings.pot | 2 +- public/viewjs/inventory.js | 4 ++++ services/StockService.php | 4 ++-- views/inventory.blade.php | 21 +++++++++++++++++++-- views/stocksettings.blade.php | 2 +- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index f923491c..eede7ef1 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -427,6 +427,13 @@ class StockApiController extends BaseApiController $bestBeforeDate = $requestBody['best_before_date']; } + $purchasedDate = null; + + if (array_key_exists('purchased_date', $requestBody) && IsIsoDate($requestBody['purchased_date'])) + { + $bestBeforeDate = $requestBody['purchased_date']; + } + $locationId = null; if (array_key_exists('location_id', $requestBody) && is_numeric($requestBody['location_id'])) @@ -448,7 +455,7 @@ class StockApiController extends BaseApiController $shoppingLocationId = $requestBody['shopping_location_id']; } - $bookingId = $this->getStockService()->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price, $shoppingLocationId); + $bookingId = $this->getStockService()->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price, $shoppingLocationId, $purchasedDate); return $this->ApiResponse($response, $this->getDatabase()->stock_log($bookingId)); } catch (\Exception $ex) diff --git a/localization/strings.pot b/localization/strings.pot index 497df404..7da4e95c 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -1922,7 +1922,7 @@ msgstr "" msgid "%s total value" msgstr "" -msgid "Show purchased date on purchase form (otherwise the purchased date defaults to today)" +msgid "Show purchased date on purchase and inventory page (otherwise the purchased date defaults to today)" msgstr "" msgid "Common" diff --git a/public/viewjs/inventory.js b/public/viewjs/inventory.js index b5e33906..f5995955 100644 --- a/public/viewjs/inventory.js +++ b/public/viewjs/inventory.js @@ -25,6 +25,10 @@ { jsonData.location_id = Grocy.Components.LocationPicker.GetValue(); } + if (Grocy.UserSettings.show_purchased_date_on_purchase) + { + jsonData.purchased_date = Grocy.Components.DateTimePicker2.GetValue(); + } jsonData.price = price; diff --git a/services/StockService.php b/services/StockService.php index 1a6d5c38..6bf3e9c5 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -677,7 +677,7 @@ class StockService extends BaseService return $this->getDatabase()->stock()->where('id', $entryId)->fetch(); } - public function InventoryProduct(int $productId, float $newAmount, $bestBeforeDate, $locationId = null, $price = null, $shoppingLocationId = null) + public function InventoryProduct(int $productId, float $newAmount, $bestBeforeDate, $locationId = null, $price = null, $shoppingLocationId = null, $purchasedDate) { if (!$this->ProductExists($productId)) { @@ -720,7 +720,7 @@ class StockService extends BaseService $bookingAmount = $newAmount; } - return $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, date('Y-m-d'), $price, null, $locationId, $shoppingLocationId); + return $this->AddProduct($productId, $bookingAmount, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION, $purchasedDate, $price, null, $locationId, $shoppingLocationId); } elseif ($newAmount < $productDetails->stock_amount + $containerWeight) { diff --git a/views/inventory.blade.php b/views/inventory.blade.php index f680f386..eea17855 100644 --- a/views/inventory.blade.php +++ b/views/inventory.blade.php @@ -20,7 +20,7 @@ @include('components.numberpicker', array( 'id' => 'new_amount', - 'label' => 'New amount', + 'label' => 'New stock amount', 'hintId' => 'new_amount_qu_unit', 'min' => 0, 'decimals' => $userSettings['stock_decimal_places_amounts'], @@ -33,6 +33,23 @@ class="text-small text-info font-italic d-none">' . $__t('Tare weight handling enabled - please weigh the whole container, the amount to be posted will be automatically calculcated') . '' )) + @if(boolval($userSettings['show_purchased_date_on_purchase'])) + @include('components.datetimepicker2', array( + 'id' => 'purchased_date', + 'label' => 'Purchased date', + 'format' => 'YYYY-MM-DD', + 'hint' => 'This will apply to added products', + 'initWithNow' => true, + 'limitEndToNow' => false, + 'limitStartToNow' => false, + 'invalidFeedback' => $__t('A purchased date is required'), + 'nextInputSelector' => '#best_before_date', + 'additionalCssClasses' => 'date-only-datetimepicker2', + 'additionalGroupCssClasses' => $additionalGroupCssClasses, + 'activateNumberPad' => GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_FIELD_NUMBER_PAD + )) + @endif + @php $additionalGroupCssClasses = ''; if (!GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING) @@ -63,7 +80,7 @@ @if(GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING) @include('components.numberpicker', array( 'id' => 'price', - 'label' => 'Price', + 'label' => 'Price per stock unit', 'min' => 0, 'decimals' => $userSettings['stock_decimal_places_prices'], 'value' => '', diff --git a/views/stocksettings.blade.php b/views/stocksettings.blade.php index fc34d560..86a82748 100644 --- a/views/stocksettings.blade.php +++ b/views/stocksettings.blade.php @@ -123,7 +123,7 @@ {{ $__t('Show purchased date on purchase form (otherwise the purchased date defaults to today)') }} + data-setting-key="show_purchased_date_on_purchase"> {{ $__t('Show purchased date on purchase and inventory page (otherwise the purchased date defaults to today)') }}