tap/app/assets/javascripts/orders.js

83 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-09-18 13:20:57 +02:00
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
// You can use CoffeeScript in this file: http://coffeescript.org/
ready = function() {
/* INITIALIZE */
$('tr.order_item_wrapper').hide();
$('tr.order_item_wrapper').filter(function() {
return parseIntNaN($(this).find('[type=number]').val()) > 0;
}).show();
/* HELPERS */
increment_product = function(product_id) {
input = $("#current_order").find(".order_item_wrapper[data-product=" + product_id + "]").find("input[type=number]");
$(input).val(parseIntNaN($(input).val()) + 1).change();
}
recalculate = function() {
/* Total Price */
array = $('tr.order_item_wrapper').map(function() {
return parseIntNaN($(this).data("price")) * parseIntNaN($(this).find("input[type=number]").val());
})
sum = 0;
array.each(function(i, el) { sum += el; });
$("#current_order .total_price").html((sum / 100.0).toFixed(2));
/* Message when no product has been choosen */
$("#current_order #empty").toggle(!($('tr.order_item_wrapper input[type=number]').filter(function() {
return parseIntNaN($(this).val()) > 0;
}).length));
}
/* PRODUCT MODAL */
2015-09-20 21:21:18 +02:00
products_ordered = $('#product_search').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('[data-name]').hide();
$('[data-name]').filter(function () {
return rex.test($(this).data("name"));
}).show();
})
$('#products_modal').on('hidden.bs.modal', function () {
$('#product_search').val('');
2015-09-18 13:20:57 +02:00
});
2015-09-20 21:21:18 +02:00
$("#products_modal button").click(function() {
increment_product($(this).data("product"))
})
/* BARCODE SCAN */
$("#from_barcode_form").submit(function(event) {
event.preventDefault();
barcode = $(this).find("input[type=number]").val();
2015-10-27 16:52:06 +01:00
$("#from_barcode_form")[0].reset();
$.ajax({
2015-11-05 19:52:34 +01:00
url: $("#from_barcode_form").data("url") + "/" + barcode,
success: function(data) {
2015-10-27 16:52:06 +01:00
if (data != null) {
increment_product(data["id"]);
$("#from_barcode_form")[0].reset();
} else {
alert("Barcode '" + barcode + "' was not found in the database system.");
}
},
dataMethod: "json"
}).fail(function() {
alert("Barcode '" + barcode + "' was not found in the database system.");
});
});
2015-09-18 13:20:57 +02:00
/* CURRENT ORDER CHANGE */
2015-09-20 21:21:18 +02:00
$('tr.order_item_wrapper input[type=number]').change(function() {
tr = $(this).closest('tr.order_item_wrapper')
2015-09-25 17:11:26 +02:00
$(tr).toggle(parseIntNaN($(this).val()) > 0);
$(tr).find("td").last().html(((parseIntNaN($(tr).data("price")) * parseIntNaN($(this).val())) / 100.0).toFixed(2))
2015-09-20 21:21:18 +02:00
recalculate();
})
2015-09-18 13:20:57 +02:00
2015-09-20 21:21:18 +02:00
recalculate();
2015-09-18 13:20:57 +02:00
}
$(document).ready(ready);
$(document).on('page:load', ready);