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(); |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 19 | shopping_cart.bind_remove_item(); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 21 | }, |
| 22 | |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 23 | bind_address_select: function() { |
| 24 | $(".cart-addresses").find('input[data-address-name]').on("click", function() { |
| 25 | if($(this).prop("checked")) { |
| 26 | var me = this; |
| 27 | |
| 28 | return frappe.call({ |
| 29 | type: "POST", |
| 30 | method: "erpnext.shopping_cart.cart.update_cart_address", |
| 31 | args: { |
| 32 | address_fieldname: $(this).attr("data-fieldname"), |
| 33 | address_name: $(this).attr("data-address-name") |
| 34 | }, |
| 35 | callback: function(r) { |
| 36 | if(!r.exc) { |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 37 | $(".cart-tax-items").html(r.message.taxes); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | }); |
| 41 | } else { |
| 42 | return false; |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | }, |
| 47 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 48 | bind_place_order: function() { |
| 49 | $(".btn-place-order").on("click", function() { |
| 50 | shopping_cart.place_order(this); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 51 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 52 | }, |
| 53 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 54 | bind_change_qty: function() { |
| 55 | // bind update button |
| 56 | $(".cart-items").on("change", ".cart-qty", function() { |
| 57 | var item_code = $(this).attr("data-item-code"); |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 58 | frappe.freeze(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 59 | shopping_cart.update_cart({ |
| 60 | item_code: item_code, |
| 61 | qty: $(this).val(), |
| 62 | with_items: 1, |
| 63 | btn: this, |
| 64 | callback: function(r) { |
Rushabh Mehta | 72fbf90 | 2015-09-17 18:29:44 +0530 | [diff] [blame] | 65 | frappe.unfreeze(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 66 | if(!r.exc) { |
| 67 | $(".cart-items").html(r.message.items); |
| 68 | $(".cart-tax-items").html(r.message.taxes); |
Saurabh | 69f9975 | 2016-01-06 16:41:50 +0530 | [diff] [blame] | 69 | $(".cart-icon").hide(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 70 | } |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 71 | }, |
| 72 | }); |
| 73 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 74 | }, |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 75 | |
| 76 | bind_remove_item: function() { |
| 77 | $(".cart-items").on("click", ".remove-item", function() { |
| 78 | var item_code = $(this).attr("data-item-code"); |
| 79 | shopping_cart.update_cart({ |
| 80 | item_code: item_code, |
| 81 | qty: "0", |
| 82 | callback: function(r) { |
| 83 | location.reload(); |
| 84 | if(!r.exc) { |
| 85 | $(".cart-items").html(r.message.items); |
| 86 | $(".cart-tax-items").html(r.message.taxes); |
| 87 | $(".cart-icon").hide(); |
| 88 | } |
| 89 | }, |
| 90 | }); |
| 91 | }); |
| 92 | }, |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 93 | |
| 94 | render_tax_row: function($cart_taxes, doc, shipping_rules) { |
| 95 | var shipping_selector; |
| 96 | if(shipping_rules) { |
| 97 | shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) { |
| 98 | return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") + |
| 99 | '</select>'; |
| 100 | } |
| 101 | |
| 102 | var $tax_row = $(repl('<div class="row">\ |
| 103 | <div class="col-md-9 col-sm-9">\ |
| 104 | <div class="row">\ |
| 105 | <div class="col-md-9 col-md-offset-3">' + |
| 106 | (shipping_selector || '<p>%(description)s</p>') + |
| 107 | '</div>\ |
| 108 | </div>\ |
| 109 | </div>\ |
| 110 | <div class="col-md-3 col-sm-3 text-right">\ |
| 111 | <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\ |
| 112 | </div>\ |
| 113 | </div>', doc)).appendTo($cart_taxes); |
| 114 | |
| 115 | if(shipping_selector) { |
| 116 | $tax_row.find('select option').each(function(i, opt) { |
| 117 | if($(opt).html() == doc.description) { |
| 118 | $(opt).attr("selected", "selected"); |
| 119 | } |
| 120 | }); |
| 121 | $tax_row.find('select').on("change", function() { |
| 122 | shopping_cart.apply_shipping_rule($(this).val(), this); |
| 123 | }); |
| 124 | } |
| 125 | }, |
| 126 | |
| 127 | apply_shipping_rule: function(rule, btn) { |
| 128 | return frappe.call({ |
| 129 | btn: btn, |
| 130 | type: "POST", |
| 131 | method: "erpnext.shopping_cart.cart.apply_shipping_rule", |
| 132 | args: { shipping_rule: rule }, |
| 133 | callback: function(r) { |
| 134 | if(!r.exc) { |
| 135 | shopping_cart.render(r.message); |
| 136 | } |
| 137 | } |
| 138 | }); |
| 139 | }, |
| 140 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 141 | place_order: function(btn) { |
| 142 | return frappe.call({ |
| 143 | type: "POST", |
| 144 | method: "erpnext.shopping_cart.cart.place_order", |
| 145 | btn: btn, |
| 146 | callback: function(r) { |
| 147 | if(r.exc) { |
| 148 | var msg = ""; |
| 149 | if(r._server_messages) { |
| 150 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 151 | } |
| 152 | |
| 153 | $("#cart-error") |
| 154 | .empty() |
| 155 | .html(msg || frappe._("Something went wrong!")) |
| 156 | .toggle(true); |
| 157 | } else { |
Anand Doshi | da858cc | 2015-02-24 17:50:44 +0530 | [diff] [blame] | 158 | window.location.href = "/orders/" + encodeURIComponent(r.message); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | }); |
| 164 | |
| 165 | $(document).ready(function() { |
Saurabh | 69f9975 | 2016-01-06 16:41:50 +0530 | [diff] [blame] | 166 | $(".cart-icon").hide(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 167 | shopping_cart.bind_events(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 168 | }); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 169 | |
| 170 | function show_terms() { |
| 171 | var html = $(".cart-terms").html(); |
| 172 | frappe.msgprint(html); |
| 173 | } |