StockService: Set the product's bestBeforeDate to the freezer date if it is being purchased to a freezer (#1672)

* StockService: Set the product's bestBeforeDate to the freezer date if it is being purchased to a freezer

* Formatting / feature flag checks / proper data type comparision

* Prefill due date also on location change

Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
Graham Christensen 2021-11-13 11:41:04 -05:00 committed by GitHub
parent 10bd5ce900
commit a7f3f64d89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 18 deletions

View File

@ -286,23 +286,7 @@ if (Grocy.Components.ProductPicker !== undefined)
$("#tare-weight-handling-info").addClass("d-none");
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING)
{
if (productDetails.product.default_best_before_days.toString() !== '0')
{
if (productDetails.product.default_best_before_days == -1)
{
if (!$("#datetimepicker-shortcut").is(":checked"))
{
$("#datetimepicker-shortcut").click();
}
}
else
{
Grocy.Components.DateTimePicker.SetValue(moment().add(productDetails.product.default_best_before_days, 'days').format('YYYY-MM-DD'));
}
}
}
PrefillBestBeforeDate(productDetails.product, productDetails.location);
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_LABEL_PRINTER)
{
@ -396,6 +380,53 @@ if (Grocy.Components.ProductPicker !== undefined)
});
}
function PrefillBestBeforeDate(product, location)
{
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING)
{
var dueDays;
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRODUCT_FREEZING && BoolVal(location.is_freezer))
{
dueDays = product.default_best_before_days_after_freezing;
}
else
{
dueDays = product.default_best_before_days;
}
dueDays = parseFloat(dueDays);
if (dueDays != 0)
{
if (dueDays == -1)
{
if (!$("#datetimepicker-shortcut").is(":checked"))
{
$("#datetimepicker-shortcut").click();
}
}
else
{
Grocy.Components.DateTimePicker.SetValue(moment().add(dueDays, 'days').format('YYYY-MM-DD'));
}
}
}
}
Grocy.Components.LocationPicker.GetPicker().on('change', function(e)
{
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRODUCT_FREEZING)
{
Grocy.Api.Get('objects/locations/' + Grocy.Components.LocationPicker.GetValue(),
function(location)
{
PrefillBestBeforeDate(CurrentProductDetails.product, location);
},
function(xhr)
{ }
);
}
});
$('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_purchase_amount));
RefreshLocaleNumberInput();
$(".input-group-productamountpicker").trigger("change");

View File

@ -144,7 +144,27 @@ class StockService extends BaseService
//Set the default due date, if none is supplied
if ($bestBeforeDate == null)
{
if (intval($productDetails->product->default_best_before_days) == -1)
if ($locationId !== null && !$this->LocationExists($locationId))
{
throw new \Exception('Location does not exist');
}
else
{
$location = $this->getDatabase()->locations()->where('id', $locationId)->fetch();
}
if (GROCY_FEATURE_FLAG_STOCK_PRODUCT_FREEZING && $locationId !== null && intval($location->is_freezer) === 1 && intval($productDetails->product->default_best_before_days_after_freezing) >= -1)
{
if (intval($productDetails->product->default_best_before_days_after_freezing) == -1)
{
$bestBeforeDate = date('2999-12-31');
}
else
{
$bestBeforeDate = date('Y-m-d', strtotime('+' . $productDetails->product->default_best_before_days_after_freezing . ' days'));
}
}
elseif (intval($productDetails->product->default_best_before_days) == -1)
{
$bestBeforeDate = date('2999-12-31');
}