Files
grocy/public/viewjs/purchase.js

216 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-04-15 23:16:20 +02:00
$('#save-purchase-button').on('click', function(e)
{
e.preventDefault();
var jsonForm = $('#purchase-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("purchase-form");
2017-04-15 23:16:20 +02:00
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(productDetails)
2017-04-15 23:16:20 +02:00
{
2017-04-20 17:10:21 +02:00
var amount = jsonForm.amount * productDetails.product.qu_factor_purchase_to_stock;
2017-04-15 23:16:20 +02:00
var price = "";
if (!jsonForm.price.toString().isEmpty())
{
price = parseFloat(jsonForm.price).toFixed(2);
}
var jsonData = {};
jsonData.amount = amount;
jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue();
jsonData.price = price;
Grocy.Api.Post('stock/products/' + jsonForm.product_id + '/add', jsonData,
function(result)
{
2018-04-14 11:10:38 +02:00
var addBarcode = GetUriParam('addbarcodetoselection');
if (addBarcode !== undefined)
{
var existingBarcodes = productDetails.product.barcode || '';
if (existingBarcodes.length === 0)
{
productDetails.product.barcode = addBarcode;
}
else
{
productDetails.product.barcode += ',' + addBarcode;
}
Grocy.Api.Put('objects/products/' + productDetails.product.id, productDetails.product,
function (result) { },
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("purchase-form");
console.error(xhr);
}
);
}
var successMessage = L('Added #1 #2 of #3 to stock', amount, Pluralize(amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + result.booking_id + ')"><i class="fas fa-undo"></i> ' + L("Undo") + '</a>';
2017-04-16 23:11:03 +02:00
if (addBarcode !== undefined)
{
window.location.href = U('/purchase');
}
else if (GetUriParam("flow") === "shoppinglistitemtostock" && typeof GetUriParam("embedded") !== undefined)
{
window.parent.postMessage(WindowMessageBag("AfterItemAdded", GetUriParam("listitemid")), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("Ready"), Grocy.BaseUrl);
}
else
{
Grocy.FrontendHelpers.EndUiBusy("purchase-form");
toastr.success(successMessage);
$('#amount').val(0);
$('#price').val('');
Grocy.Components.DateTimePicker.Clear();
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.SetValue('');
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.FrontendHelpers.ValidateForm('purchase-form');
}
2017-04-15 23:16:20 +02:00
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("purchase-form");
2017-04-15 23:16:20 +02:00
console.error(xhr);
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("purchase-form");
2017-04-15 23:16:20 +02:00
console.error(xhr);
}
);
});
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
2017-04-15 23:16:20 +02:00
{
var productId = $(e.target).val();
2017-04-16 23:11:03 +02:00
if (productId)
{
2018-04-14 11:10:38 +02:00
Grocy.Components.ProductCard.Refresh(productId);
Grocy.Api.Get('stock/products/' + productId,
function(productDetails)
2017-04-16 23:11:03 +02:00
{
2017-04-21 19:02:00 +02:00
$('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name);
$('#price').val(productDetails.last_price);
2017-04-20 17:10:21 +02:00
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'));
}
$('#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)
{
$("#save-purchase-button").click();
}
2017-04-20 17:10:21 +02:00
}
else
{
Grocy.Components.DateTimePicker.GetInputElement().focus();
2018-04-14 11:10:38 +02:00
}
2017-04-16 23:11:03 +02:00
},
function(xhr)
{
console.error(xhr);
}
);
}
2017-04-15 23:16:20 +02:00
});
2018-04-16 19:11:32 +02:00
$('#amount').val(0);
Grocy.FrontendHelpers.ValidateForm('purchase-form');
2017-11-10 22:45:53 +01:00
2018-07-14 14:43:57 +02:00
if (Grocy.Components.ProductPicker.InProductAddWorkflow() === false)
{
Grocy.Components.ProductPicker.GetInputElement().focus();
}
else
{
Grocy.Components.ProductPicker.GetPicker().trigger('change');
}
2018-04-16 19:11:32 +02:00
$('#amount').on('focus', function(e)
{
2018-07-14 14:43:57 +02:00
if (Grocy.Components.ProductPicker.GetValue().length === 0)
2017-11-10 22:45:53 +01:00
{
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.GetInputElement().focus();
2018-04-16 19:11:32 +02:00
}
else
{
2018-04-16 19:11:32 +02:00
$(this).select();
}
});
$('#purchase-form input').keyup(function(event)
{
Grocy.FrontendHelpers.ValidateForm('purchase-form');
});
2018-04-16 19:11:32 +02:00
$('#purchase-form input').keydown(function(event)
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('purchase-form').checkValidity() === false) //There is at least one validation error
{
2018-04-16 19:11:32 +02:00
return false;
}
else
{
$('#save-purchase-button').click();
}
2018-04-16 19:11:32 +02:00
}
});
Grocy.Components.DateTimePicker.GetInputElement().on('change', function(e)
{
Grocy.FrontendHelpers.ValidateForm('purchase-form');
});
Grocy.Components.DateTimePicker.GetInputElement().on('keypress', function(e)
{
Grocy.FrontendHelpers.ValidateForm('purchase-form');
});
$('#amount').on('change', function(e)
{
Grocy.FrontendHelpers.ValidateForm('purchase-form');
2017-04-15 23:16:20 +02:00
});
if (GetUriParam("flow") === "shoppinglistitemtostock")
{
$('#amount').val(GetUriParam("amount"));
}
function UndoStockBooking(bookingId)
{
Grocy.Api.Post('stock/bookings/' + bookingId.toString() + '/undo', { },
function(result)
{
toastr.success(L("Booking successfully undone"));
},
function(xhr)
{
console.error(xhr);
}
);
};