From 9641b66346ce8ceb559dec51da6f9f53be353f62 Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Mon, 6 Oct 2014 13:33:23 +0200 Subject: [PATCH] Correctly update order total --- main/views.py | 2 +- static/js/app.js | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/main/views.py b/main/views.py index 4848e09..e15a65f 100755 --- a/main/views.py +++ b/main/views.py @@ -73,7 +73,7 @@ def register(request): int(order_sum - profile.balance))) return HttpResponseRedirect(reverse('main.views.register')) # substract order from balance - profile.balance -= order.order_sum + profile.balance -= order_sum profile.save() order.order_sum = order_sum diff --git a/static/js/app.js b/static/js/app.js index a0e6772..489661b 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -43,6 +43,24 @@ function reset_qtys() { $(this).val("0"); }); } +function update_total() { + var total = 0; + $('li.product').each(function() { + var amount = Number($(this).find('[name$=amount]').val()); + + var price_selector = '.sale_price_int'; + if($("#id_customer_typeahead").val() === "Ekstern") { + price_selector = '.sale_price_ext'; + } + var sale_price = Number($(this).find(price_selector).text()); + + total += amount * sale_price; + }); + if(total === 0) { + total = ""; + } + $("span.total_text").html(total); +} $(document).ready(function() { /* Hide alert messages */ @@ -79,7 +97,7 @@ $(document).ready(function() { var id = $(this).children("span.id").text(); $("#id_customer option, #id_user option").removeAttr('selected'); $("#id_customer option[value="+id+"], #id_user option[value="+id+"]").attr('selected','selected'); - console.log(id); + update_total(this); }); @@ -98,9 +116,7 @@ $(document).ready(function() { form_field.val(qty); display.html(qty); /* Update total*/ - var sale_price_int = Number($(this).children("span.sale_price_int").text()); - var cur_total = Number($("span.total_text").text()); - $("span.total_text").html(cur_total + sale_price_int); + update_total(this); }); /* Form reset */ $("#register button[type=reset]").click( function() {