viewjs consume: implement location and update stock specific

This commit is contained in:
Kurt Riddlesperger 2019-10-11 12:15:59 -05:00
parent 0be1994c02
commit c7e763fd5c
3 changed files with 161 additions and 50 deletions

View File

@ -50,7 +50,8 @@ class StockController extends BaseController
{
return $this->AppContainer->view->render($response, 'consume', [
'products' => $this->Database->products()->orderBy('name'),
'recipes' => $this->Database->recipes()->orderBy('name')
'recipes' => $this->Database->recipes()->orderBy('name'),
'locations' => $this->Database->locations()->orderBy('name')
]);
}

View File

@ -5,11 +5,6 @@
var jsonForm = $('#consume-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("consume-form");
if ($("#use_specific_stock_entry").is(":checked"))
{
jsonForm.amount = 1;
}
var apiUrl = 'stock/products/' + jsonForm.product_id + '/consume';
var jsonData = {};
@ -21,6 +16,15 @@
jsonData.stock_entry_id = jsonForm.specific_stock_entry;
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
jsonData.location_id = $("#location_id").val();
}
else
{
jsonData.location_id = 1;
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES && Grocy.Components.RecipePicker.GetValue().toString().length > 0)
{
jsonData.recipe_id = Grocy.Components.RecipePicker.GetValue();
@ -102,6 +106,10 @@
{
Grocy.Components.RecipePicker.Clear();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
$("#location_id").find("option").remove().end().append("<option></option>");
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.FrontendHelpers.ValidateForm('consume-form');
}
@ -128,11 +136,6 @@ $('#save-mark-as-open-button').on('click', function(e)
var jsonForm = $('#consume-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("consume-form");
if ($("#use_specific_stock_entry").is(":checked"))
{
jsonForm.amount = 1;
}
var apiUrl = 'stock/products/' + jsonForm.product_id + '/open';
jsonData = { };
@ -178,6 +181,59 @@ $('#save-mark-as-open-button').on('click', function(e)
);
});
$("#location_id").on('change', function(e)
{
var locationId = $(e.target).val();
var sumValue = 0;
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
if ($("#use_specific_stock_entry").is(":checked"))
{
$("#use_specific_stock_entry").click();
}
if (locationId)
{
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries',
function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{
if (stockEntry.location_id == locationId)
{
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount remaining: %1$s, Best Before: %2$s", stockEntry.amount, stockEntry.best_before_date)
}));
sumValue = sumValue + parseFloat(stockEntry.amount);
}
if (stockEntry.location_id === null)
{
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount remaining: %1$s, Best Before: %2$s", stockEntry.amount, stockEntry.best_before_date)
}));
sumValue = sumValue + parseFloat(stockEntry.amount);
}
});
$("#amount").attr("max", sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
} else {
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr);
}
);
}
});
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
@ -185,6 +241,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
$("#use_specific_stock_entry").click();
}
$("#location_id").val("");
var productId = $(e.target).val();
@ -195,9 +252,41 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
Grocy.Api.Get('stock/products/' + productId,
function(productDetails)
{
$('#amount').attr('max', productDetails.stock_amount);
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
$("#location_id").find("option").remove().end().append("<option></option>");
Grocy.Api.Get("stock/products/" + productId + '/locations',
function(stockLocations)
{
var setDefault = 0;
stockLocations.forEach(stockLocation =>
{
if (productDetails.location.id == stockLocation.location_id) {
$("#location_id").append($("<option>", {
value: stockLocation.location_id,
text: __t("%1$s (default location)", stockLocation.name)
}));
$("#location_id").val(productDetails.location.id);
$("#location_id").trigger('change');
setDefault = 1;
} else {
$("#location_id").append($("<option>", {
value: stockLocation.location_id,
text: __t("%1$s", stockLocation.name)
}));
}
if (setDefault == 0) {
$("#location_id").val(stockLocation.location_id);
$("#location_id").trigger('change');
}
});
},
function(xhr)
{
console.error(xhr);
}
);
if (productDetails.product.allow_partial_units_in_stock == 1)
{
$("#amount").attr("min", "0.01");
@ -251,38 +340,6 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
console.error(xhr);
}
);
Grocy.Api.Get("stock/products/" + productId + '/entries',
function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{
var openTxt = __t("Not opened");
if (stockEntry.open == 1)
{
openTxt = __t("Opened");
}
for (i = 0; i < stockEntry.amount; i++)
{
// Do this only for the first 50 entries to prevent a very long loop (is more anytime needed)?
if (i > 50)
{
break;
}
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
text: __t("Expires on %1$s; Bought on %2$s", moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
}));
}
});
},
function(xhr)
{
console.error(xhr);
}
);
}
});
@ -323,22 +380,53 @@ $('#consume-form input').keydown(function(event)
}
});
$("#specific_stock_entry").on("change", function(e)
{
if ($(e.target).val() == "")
{
var sumValue = 0;
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries',
function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{
if (stockEntry.location_id == $("#location_id").val() || stockEntry.location_id == "")
{
sumValue = sumValue + parseFloat(stockEntry.amount);
}
});
$("#amount").attr("max", sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
} else {
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr);
}
);
} else {
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", $('option:selected', this).attr('amount')));
$("#amount").attr("max", $('option:selected', this).attr('amount'));
}
});
$("#use_specific_stock_entry").on("change", function()
{
var value = $(this).is(":checked");
if (value)
{
$("#specific_stock_entry").removeAttr("disabled");
$("#amount").attr("disabled", "");
$("#amount").val(1);
$("#amount").removeAttr("required");
$("#specific_stock_entry").attr("required", "");
}
else
{
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
$("#specific_stock_entry").attr("disabled", "");
$("#amount").removeAttr("disabled");
$("#amount").attr("required", "");
$("#specific_stock_entry").removeAttr("required");
}

View File

@ -22,11 +22,33 @@
'label' => 'Amount',
'hintId' => 'amount_qu_unit',
'min' => 1,
'value' => 1,
'value' => 0,
'invalidFeedback' => $__t('The amount cannot be lower than %s', '1'),
'additionalHtmlContextHelp' => '<div id="tare-weight-handling-info" class="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') . '</div>'
))
@if(GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
@php /*@include('components.locationpicker', array(
'id' => 'location_id',
'locations' => $locations,
'isRequired' => true,
'label' => 'Location'
))*/ @endphp
<div class="form-group">
<label for="location_id">{{ $__t('Location') }}</label>
<select required class="form-control location-combobox" id="location_id" name="location_id">
<option></option>
@foreach($locations as $location)
<option value="{{ $location->id }}">{{ $location->name }}</option>
@endforeach
</select>
<div class="invalid-feedback">{{ $__t('A location is required') }}</div>
</div>
@else
<input type="hidden" name="location_id" id="location_id" value="1">
@endif
<div class="form-group">
<label for="use_specific_stock_entry">
<input type="checkbox" id="use_specific_stock_entry" name="use_specific_stock_entry"> {{ $__t('Use a specific stock item') }}