Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | // js inside blog page |
| 5 | |
| 6 | // shopping cart |
| 7 | frappe.provide("shopping_cart"); |
| 8 | |
| 9 | $.extend(shopping_cart, { |
| 10 | show_error: function(title, text) { |
Rushabh Mehta | fc3d871 | 2015-07-10 10:11:07 +0530 | [diff] [blame] | 11 | $("#cart-container").html('<div class="msg-box"><h4>' + |
| 12 | title + '</h4><p class="text-muted">' + text + '</p></div>'); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 13 | }, |
| 14 | |
| 15 | bind_events: function() { |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 16 | shopping_cart.bind_address_select(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 17 | shopping_cart.bind_place_order(); |
| 18 | shopping_cart.bind_change_qty(); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 19 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 20 | }, |
| 21 | |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 22 | bind_address_select: function() { |
| 23 | $(".cart-addresses").find('input[data-address-name]').on("click", function() { |
| 24 | if($(this).prop("checked")) { |
| 25 | var me = this; |
| 26 | |
| 27 | return frappe.call({ |
| 28 | type: "POST", |
| 29 | method: "erpnext.shopping_cart.cart.update_cart_address", |
| 30 | args: { |
| 31 | address_fieldname: $(this).attr("data-fieldname"), |
| 32 | address_name: $(this).attr("data-address-name") |
| 33 | }, |
| 34 | callback: function(r) { |
| 35 | if(!r.exc) { |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 36 | $(".cart-tax-items").html(r.message.taxes); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | }); |
| 40 | } else { |
| 41 | return false; |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | }, |
| 46 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 47 | bind_place_order: function() { |
| 48 | $(".btn-place-order").on("click", function() { |
| 49 | shopping_cart.place_order(this); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 50 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 51 | }, |
| 52 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 53 | bind_change_qty: function() { |
| 54 | // bind update button |
| 55 | $(".cart-items").on("change", ".cart-qty", function() { |
| 56 | var item_code = $(this).attr("data-item-code"); |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 57 | frappe.freeze(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 58 | shopping_cart.update_cart({ |
| 59 | item_code: item_code, |
| 60 | qty: $(this).val(), |
| 61 | with_items: 1, |
| 62 | btn: this, |
| 63 | callback: function(r) { |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 64 | frappe.unfreeze(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 65 | if(!r.exc) { |
| 66 | $(".cart-items").html(r.message.items); |
| 67 | $(".cart-tax-items").html(r.message.taxes); |
| 68 | } |
| 69 | $(".tax-grand-total").temp_highlight(); |
| 70 | }, |
| 71 | }); |
| 72 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 73 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 74 | }, |
| 75 | |
| 76 | render_tax_row: function($cart_taxes, doc, shipping_rules) { |
| 77 | var shipping_selector; |
| 78 | if(shipping_rules) { |
| 79 | shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) { |
| 80 | return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") + |
| 81 | '</select>'; |
| 82 | } |
| 83 | |
| 84 | var $tax_row = $(repl('<div class="row">\ |
| 85 | <div class="col-md-9 col-sm-9">\ |
| 86 | <div class="row">\ |
| 87 | <div class="col-md-9 col-md-offset-3">' + |
| 88 | (shipping_selector || '<p>%(description)s</p>') + |
| 89 | '</div>\ |
| 90 | </div>\ |
| 91 | </div>\ |
| 92 | <div class="col-md-3 col-sm-3 text-right">\ |
| 93 | <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\ |
| 94 | </div>\ |
| 95 | </div>', doc)).appendTo($cart_taxes); |
| 96 | |
| 97 | if(shipping_selector) { |
| 98 | $tax_row.find('select option').each(function(i, opt) { |
| 99 | if($(opt).html() == doc.description) { |
| 100 | $(opt).attr("selected", "selected"); |
| 101 | } |
| 102 | }); |
| 103 | $tax_row.find('select').on("change", function() { |
| 104 | shopping_cart.apply_shipping_rule($(this).val(), this); |
| 105 | }); |
| 106 | } |
| 107 | }, |
| 108 | |
| 109 | apply_shipping_rule: function(rule, btn) { |
| 110 | return frappe.call({ |
| 111 | btn: btn, |
| 112 | type: "POST", |
| 113 | method: "erpnext.shopping_cart.cart.apply_shipping_rule", |
| 114 | args: { shipping_rule: rule }, |
| 115 | callback: function(r) { |
| 116 | if(!r.exc) { |
| 117 | shopping_cart.render(r.message); |
| 118 | } |
| 119 | } |
| 120 | }); |
| 121 | }, |
| 122 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 123 | place_order: function(btn) { |
| 124 | return frappe.call({ |
| 125 | type: "POST", |
| 126 | method: "erpnext.shopping_cart.cart.place_order", |
| 127 | btn: btn, |
| 128 | callback: function(r) { |
| 129 | if(r.exc) { |
| 130 | var msg = ""; |
| 131 | if(r._server_messages) { |
| 132 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 133 | } |
| 134 | |
| 135 | $("#cart-error") |
| 136 | .empty() |
| 137 | .html(msg || frappe._("Something went wrong!")) |
| 138 | .toggle(true); |
| 139 | } else { |
Anand Doshi | da858cc | 2015-02-24 17:50:44 +0530 | [diff] [blame] | 140 | window.location.href = "/orders/" + encodeURIComponent(r.message); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | }); |
| 146 | |
| 147 | $(document).ready(function() { |
| 148 | shopping_cart.bind_events(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 149 | }); |