From 9a921cfc86c4dfdfc95115f536b3ba1807b73b9c Mon Sep 17 00:00:00 2001 From: kriddles <54413450+kriddles@users.noreply.github.com> Date: Wed, 22 Apr 2020 10:38:24 -0500 Subject: [PATCH] Purchase Price Hints (#767) * productcard update last price with per qu purchase name * Purchase price hints * purchase set default to 2999-12-31 if not best before date tracking * purchase- move amount above best buy date and focus amount after product selection --- public/viewjs/components/productcard.js | 2 +- public/viewjs/purchase.js | 76 ++++++++++++++++++++----- views/purchase.blade.php | 25 ++++---- 3 files changed, 76 insertions(+), 27 deletions(-) diff --git a/public/viewjs/components/productcard.js b/public/viewjs/components/productcard.js index 951207d1..9b6c3ba4 100644 --- a/public/viewjs/components/productcard.js +++ b/public/viewjs/components/productcard.js @@ -80,7 +80,7 @@ Grocy.Components.ProductCard.Refresh = function(productId) if (productDetails.last_price !== null) { - $('#productcard-product-last-price').text(Number.parseFloat(productDetails.last_price).toLocaleString() + ' ' + Grocy.Currency); + $('#productcard-product-last-price').text(Number.parseFloat(productDetails.last_price).toLocaleString() + ' ' + Grocy.Currency + ' per ' + productDetails.quantity_unit_purchase.name); } else { diff --git a/public/viewjs/purchase.js b/public/viewjs/purchase.js index 3501e03a..ea2245aa 100644 --- a/public/viewjs/purchase.js +++ b/public/viewjs/purchase.js @@ -161,6 +161,8 @@ if (Grocy.Components.ProductPicker !== undefined) Grocy.Components.LocationPicker.SetId(productDetails.location.id); } + $('#amount_qu_unit').attr("qu-factor-purchase-to-stock", productDetails.product.qu_factor_purchase_to_stock); + $('#amount_qu_unit').attr("quantity-unit-stock-name", productDetails.quantity_unit_stock.name); if (productDetails.product.qu_id_purchase === productDetails.product.qu_id_stock) { $('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name); @@ -170,6 +172,12 @@ if (Grocy.Components.ProductPicker !== undefined) $('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name + " (" + __t("will be multiplied by a factor of %1$s to get %2$s", parseFloat(productDetails.product.qu_factor_purchase_to_stock).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: 2 }), __n(2, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural)) + ")"); } + var priceTypeUnitPrice = $("#price-type-unit-price"); + var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]"); + priceTypeUnitPriceLabel.text(productDetails.quantity_unit_purchase.name + " price"); + + refreshPriceHint(); + if (productDetails.product.allow_partial_units_in_stock == 1) { $("#amount").attr("min", "0.01"); @@ -198,7 +206,7 @@ if (Grocy.Components.ProductPicker !== undefined) if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING) { - Grocy.Components.DateTimePicker.SetValue(moment().format('YYYY-MM-DD')); + Grocy.Components.DateTimePicker.SetValue('2999-12-31'); } if (productDetails.product.default_best_before_days.toString() !== '0') @@ -214,20 +222,10 @@ if (Grocy.Components.ProductPicker !== undefined) { Grocy.Components.DateTimePicker.SetValue(moment().add(productDetails.product.default_best_before_days, 'days').format('YYYY-MM-DD')); } - $('#amount').focus(); - } - else - { - if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING) - { - Grocy.Components.DateTimePicker.GetInputElement().focus(); - } - else - { - $('#amount').focus(); - } } + $("#amount").focus(); + Grocy.FrontendHelpers.ValidateForm('purchase-form'); if (GetUriParam("flow") === "shoppinglistitemtostock" && BoolVal(Grocy.UserSettings.shopping_list_to_stock_workflow_auto_submit_when_prefilled) && document.getElementById("purchase-form").checkValidity() === true) { @@ -320,8 +318,24 @@ if (Grocy.Components.DateTimePicker) }); } +$('#price').on('change', function(e) +{ + refreshPriceHint(); +}); + +$('#price-type-unit-price').on('change', function(e) +{ + refreshPriceHint(); +}); + +$('#price-type-total-price').on('change', function(e) +{ + refreshPriceHint(); +}); + $('#amount').on('change', function(e) { + refreshPriceHint(); Grocy.FrontendHelpers.ValidateForm('purchase-form'); }); @@ -330,6 +344,42 @@ if (GetUriParam("flow") === "shoppinglistitemtostock") $('#amount').val(parseFloat(GetUriParam("amount")).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: 4 })); } +function refreshPriceHint() +{ + + if ($('#amount').val() == 0) + { + $('#price-hint').text(""); + return; + } + if ($('#price').val() == 0) + { + $('#price-hint').text(""); + return; + } + + if ($("input[name='price-type']:checked").val() == "total-price") + { + var priceTypeUnitPrice = $("#price-type-unit-price"); + var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]"); + var price = $('#price').val() / $('#amount').val(); + + $('#price-hint').text('(will result with ' + priceTypeUnitPriceLabel.text() + ' cost of ' + price.toFixed(2) + ')'); + } + else + { + if (document.getElementById("amount_qu_unit").getAttribute("qu-factor-purchase-to-stock") > 1) + { + var price = $('#price').val() / document.getElementById("amount_qu_unit").getAttribute("qu-factor-purchase-to-stock"); + $('#price-hint').text('(will result with ' + document.getElementById("amount_qu_unit").getAttribute("quantity-unit-stock-name") + ' cost of ' + price.toFixed(2) + ')'); + } + else + { + $('#price-hint').text(""); + } + } +}; + function UndoStockBooking(bookingId) { Grocy.Api.Post('stock/bookings/' + bookingId.toString() + '/undo', { }, diff --git a/views/purchase.blade.php b/views/purchase.blade.php index 10d72bff..d9068c07 100644 --- a/views/purchase.blade.php +++ b/views/purchase.blade.php @@ -30,9 +30,17 @@ @include('components.productpicker', array( 'products' => $products, - 'nextInputSelector' => '#best_before_date .datetimepicker-input' + 'nextInputSelector' => '#amount' )) + @include('components.numberpicker', array( + 'id' => 'amount', + 'label' => 'Amount', + 'hintId' => 'amount_qu_unit', + 'min' => 1, + 'invalidFeedback' => $__t('The amount cannot be lower than %s', '1'), + 'additionalHtmlContextHelp' => '
' . $__t('Tare weight handling enabled - please weigh the whole container, the amount to be posted will be automatically calculcated') . '
' + )) @php $additionalGroupCssClasses = ''; @@ -49,7 +57,7 @@ 'limitEndToNow' => false, 'limitStartToNow' => false, 'invalidFeedback' => $__t('A best before date is required'), - 'nextInputSelector' => '#amount', + 'nextInputSelector' => '#price', 'additionalCssClasses' => 'date-only-datetimepicker', 'shortcutValue' => '2999-12-31', 'shortcutLabel' => 'Never expires', @@ -60,15 +68,6 @@ )) @php $additionalGroupCssClasses = ''; @endphp - @include('components.numberpicker', array( - 'id' => 'amount', - 'label' => 'Amount', - 'hintId' => 'amount_qu_unit', - 'min' => 1, - 'invalidFeedback' => $__t('The amount cannot be lower than %s', '1'), - 'additionalHtmlContextHelp' => '
' . $__t('Tare weight handling enabled - please weigh the whole container, the amount to be posted will be automatically calculcated') . '
' - )) - @if(GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING) @include('components.numberpicker', array( 'id' => 'price', @@ -76,14 +75,14 @@ 'min' => 0, 'step' => 0.01, 'value' => '', - 'hint' => $__t('in %s and based on the purchase quantity unit', GROCY_CURRENCY), + 'hintId' => 'price-hint', 'invalidFeedback' => $__t('The price cannot be lower than %s', '0'), 'isRequired' => false, 'additionalGroupCssClasses' => 'mb-1' ))
- +