Add functionality to move a product when it is opened

This commit is contained in:
Rosemary Orchard 2022-04-18 09:06:31 +01:00
parent d4bd6b2fb3
commit 051ecc657e
6 changed files with 59 additions and 22 deletions

View File

@ -2326,3 +2326,10 @@ msgstr ""
msgid "Stock entries at this location will be consumed first"
msgstr ""
msgid "Move on open"
msgstr ""
msgid "When checked, opening the product will move one unit to the default consume location"
msgstr ""

2
migrations/0189.sql Normal file
View File

@ -0,0 +1,2 @@
ALTER TABLE products
ADD move_on_open TINYINT NOT NULL DEFAULT 0 CHECK(move_on_open IN (0, 1));

View File

@ -373,9 +373,11 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
$(".input-group-productamountpicker").trigger("change");
var defaultLocationId = productDetails.location.id;
if (productDetails.product.default_consume_location_id != null && !productDetails.product.default_consume_location_id.isEmpty())
var defaultConsumeLocationId = productDetails.product.default_consume_location_id;
if (defaultConsumeLocationId != null && !defaultConsumeLocationId.isEmpty())
{
defaultLocationId = productDetails.product.default_consume_location_id;
defaultLocationId = defaultConsumeLocationId;
}
$("#location_id").find("option").remove().end().append("<option></option>");
@ -386,26 +388,22 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
var stockAmountAtDefaultLocation = 0;
stockLocations.forEach(stockLocation =>
{
if (stockLocation.location_id == defaultLocationId)
{
$("#location_id").append($("<option>", {
value: stockLocation.location_id,
text: stockLocation.location_name + " (" + __t("Default location") + ")"
}));
$("#location_id").val(defaultLocationId);
$("#location_id").trigger('change');
var stockLocationName = stockLocation.location_name;
if (stockLocation.location_id == defaultLocationId) {
stockLocationName += +" (" + __t("Default location") + ")";
setDefault = 1;
stockAmountAtDefaultLocation += Number.parseFloat(stockLocation.amount);
}
else
{
$("#location_id").append($("<option>", {
value: stockLocation.location_id,
text: stockLocation.location_name
}));
}
$("#location_id").append($("<option>", {
value: stockLocation.location_id,
text: stockLocationName
}));
$("#location_id").val(defaultLocationId);
$("#location_id").trigger('change');
setDefault = 1;
stockAmountAtDefaultLocation += Number.parseFloat(stockLocation.amount);
if (setDefault == 0)
if (!setDefault)
{
$("#location_id").val(defaultLocationId);
$("#location_id").trigger('change');

View File

@ -240,6 +240,18 @@ $('#location_id').change(function(event)
{
Grocy.FrontendHelpers.ValidateForm('product-form');
});
$('#default_consume_location_id').change(function(event) {
var defaultLocation = $("#location_id :selected").val();
var consumeLocationLocation = document.getElementById('default_consume_location_id');
console.log('defaultLocation', defaultLocation);
console.log('consumeLocationLocation', consumeLocationLocation);
var moveOnOpen = document.getElementById('move_on_open');
if (consumeLocationLocation && defaultLocation !== consumeLocationLocation) {
moveOnOpen.setAttribute('disabled', "");
} else {
moveOnOpen.removeAttribute('disabled');
}
});
$('#product-form input').keydown(function(event)
{

View File

@ -1007,11 +1007,11 @@ class StockService extends BaseService
]);
$logRow->save();
$stockEntry->update([
$update = [
'open' => 1,
'opened_date' => date('Y-m-d'),
'best_before_date' => $newBestBeforeDate
]);
];
$amount -= $stockEntry->amount;
}
@ -1048,15 +1048,20 @@ class StockService extends BaseService
]);
$logRow->save();
$stockEntry->update([
$update = [
'amount' => $amount,
'open' => 1,
'opened_date' => date('Y-m-d'),
'best_before_date' => $newBestBeforeDate
]);
];
$amount = 0;
}
if ($product->move_on_open) {
$update['location_id'] = $product->default_consume_location_id ?? $stockEntry->location_id;
}
$stockEntry->update($update);
}
return $transactionId;

View File

@ -146,6 +146,19 @@
$location->id == $product->default_consume_location_id) selected="selected" @endif value="{{ $location->id }}">{{ $location->name }}</option>
@endforeach
</select>
@if(GROCY_FEATURE_FLAG_STOCK_PRODUCT_OPENED_TRACKING)
<div class="custom-control custom-checkbox">
<input @if($mode=='edit'
&&
$product->move_on_open == 1) checked @endif class="form-check-input custom-control-input" type="checkbox" id="move_on_open" name="move_on_open" value="1">
<label class="form-check-label custom-control-label"
for="move_on_open">{{ $__t('Move on open') }}&nbsp;<i class="fa-solid fa-question-circle text-muted"
data-toggle="tooltip"
data-trigger="hover click"
title="{{ $__t("When checked, opening the product will move one unit to the default consume location") }}"></i>
</label>
</div>
@endif
</div>
@else
<input type="hidden"