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

This commit is contained in:
Graham Christensen 2021-11-07 17:55:20 -05:00
parent 8105dea17f
commit 9578f1696b
2 changed files with 31 additions and 4 deletions

View File

@ -288,9 +288,19 @@ if (Grocy.Components.ProductPicker !== undefined)
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING)
{
if (productDetails.product.default_best_before_days.toString() !== '0')
var best_before;
if (productDetails.location.is_freezer.toString() == "1")
{
if (productDetails.product.default_best_before_days == -1)
best_before = productDetails.product.default_best_before_days_after_freezing;
}
else
{
best_before = productDetails.product.default_best_before_days;
}
if (best_before.toString() !== '0')
{
if (best_before == -1)
{
if (!$("#datetimepicker-shortcut").is(":checked"))
{
@ -299,7 +309,7 @@ if (Grocy.Components.ProductPicker !== undefined)
}
else
{
Grocy.Components.DateTimePicker.SetValue(moment().add(productDetails.product.default_best_before_days, 'days').format('YYYY-MM-DD'));
Grocy.Components.DateTimePicker.SetValue(moment().add(best_before, 'days').format('YYYY-MM-DD'));
}
}
}

View File

@ -144,7 +144,24 @@ 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 && $productDetails->product->default_best_before_days_after_freezing >= -1)
{
if ($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');
}