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 |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 7 | frappe.provide("erpnext.shopping_cart"); |
| 8 | var shopping_cart = erpnext.shopping_cart; |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 9 | |
| 10 | $.extend(shopping_cart, { |
| 11 | show_error: function(title, text) { |
Rushabh Mehta | fc3d871 | 2015-07-10 10:11:07 +0530 | [diff] [blame] | 12 | $("#cart-container").html('<div class="msg-box"><h4>' + |
| 13 | title + '</h4><p class="text-muted">' + text + '</p></div>'); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 14 | }, |
| 15 | |
| 16 | bind_events: function() { |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 17 | shopping_cart.bind_address_select(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 18 | shopping_cart.bind_place_order(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 19 | shopping_cart.bind_request_quotation(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 20 | shopping_cart.bind_change_qty(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 21 | shopping_cart.bind_change_notes(); |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 22 | shopping_cart.bind_dropdown_cart_buttons(); |
ashish-greycube | 74dc3c9 | 2019-08-12 13:39:25 +0530 | [diff] [blame] | 23 | shopping_cart.bind_coupon_code(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 24 | }, |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 25 | |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 26 | bind_address_select: function() { |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 27 | $(".cart-addresses").on('click', '.address-card', function(e) { |
| 28 | const $card = $(e.currentTarget); |
| 29 | const address_fieldname = $card.closest('[data-fieldname]').attr('data-fieldname'); |
| 30 | const address_name = $card.closest('[data-address-name]').attr('data-address-name'); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 31 | |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 32 | return frappe.call({ |
| 33 | type: "POST", |
| 34 | method: "erpnext.shopping_cart.cart.update_cart_address", |
| 35 | freeze: true, |
| 36 | args: { |
| 37 | address_fieldname, |
| 38 | address_name |
| 39 | }, |
| 40 | callback: function(r) { |
| 41 | if(!r.exc) { |
| 42 | $(".cart-tax-items").html(r.message.taxes); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 43 | } |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 44 | } |
| 45 | }); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 46 | }); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 47 | }, |
| 48 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 49 | bind_place_order: function() { |
| 50 | $(".btn-place-order").on("click", function() { |
| 51 | shopping_cart.place_order(this); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 52 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 53 | }, |
| 54 | |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 55 | bind_request_quotation: function() { |
| 56 | $('.btn-request-for-quotation').on('click', function() { |
| 57 | shopping_cart.request_quotation(this); |
| 58 | }); |
| 59 | }, |
| 60 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 61 | bind_change_qty: function() { |
| 62 | // bind update button |
| 63 | $(".cart-items").on("change", ".cart-qty", function() { |
| 64 | var item_code = $(this).attr("data-item-code"); |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 65 | var newVal = $(this).val(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 66 | shopping_cart.shopping_cart_update({item_code, qty: newVal}); |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 67 | }); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 68 | |
| 69 | $(".cart-items").on('click', '.number-spinner button', function () { |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 70 | var btn = $(this), |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 71 | input = btn.closest('.number-spinner').find('input'), |
| 72 | oldValue = input.val().trim(), |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 73 | newVal = 0; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 74 | |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 75 | if (btn.attr('data-dir') == 'up') { |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 76 | newVal = parseInt(oldValue) + 1; |
| 77 | } else { |
| 78 | if (oldValue > 1) { |
| 79 | newVal = parseInt(oldValue) - 1; |
| 80 | } |
| 81 | } |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 82 | input.val(newVal); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 83 | var item_code = input.attr("data-item-code"); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 84 | shopping_cart.shopping_cart_update({item_code, qty: newVal}); |
| 85 | }); |
| 86 | }, |
| 87 | |
| 88 | bind_change_notes: function() { |
| 89 | $('.cart-items').on('change', 'textarea', function() { |
| 90 | const $textarea = $(this); |
| 91 | const item_code = $textarea.attr('data-item-code'); |
| 92 | const qty = $textarea.closest('tr').find('.cart-qty').val(); |
| 93 | const notes = $textarea.val(); |
| 94 | shopping_cart.shopping_cart_update({ |
| 95 | item_code, |
| 96 | qty, |
| 97 | additional_notes: notes |
| 98 | }); |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 99 | }); |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 100 | }, |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 101 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 102 | render_tax_row: function($cart_taxes, doc, shipping_rules) { |
| 103 | var shipping_selector; |
| 104 | if(shipping_rules) { |
| 105 | shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 106 | return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") + |
| 107 | '</select>'; |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | var $tax_row = $(repl('<div class="row">\ |
| 111 | <div class="col-md-9 col-sm-9">\ |
| 112 | <div class="row">\ |
| 113 | <div class="col-md-9 col-md-offset-3">' + |
| 114 | (shipping_selector || '<p>%(description)s</p>') + |
| 115 | '</div>\ |
| 116 | </div>\ |
| 117 | </div>\ |
| 118 | <div class="col-md-3 col-sm-3 text-right">\ |
| 119 | <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\ |
| 120 | </div>\ |
| 121 | </div>', doc)).appendTo($cart_taxes); |
| 122 | |
| 123 | if(shipping_selector) { |
| 124 | $tax_row.find('select option').each(function(i, opt) { |
| 125 | if($(opt).html() == doc.description) { |
| 126 | $(opt).attr("selected", "selected"); |
| 127 | } |
| 128 | }); |
| 129 | $tax_row.find('select').on("change", function() { |
| 130 | shopping_cart.apply_shipping_rule($(this).val(), this); |
| 131 | }); |
| 132 | } |
| 133 | }, |
| 134 | |
| 135 | apply_shipping_rule: function(rule, btn) { |
| 136 | return frappe.call({ |
| 137 | btn: btn, |
| 138 | type: "POST", |
| 139 | method: "erpnext.shopping_cart.cart.apply_shipping_rule", |
| 140 | args: { shipping_rule: rule }, |
| 141 | callback: function(r) { |
| 142 | if(!r.exc) { |
| 143 | shopping_cart.render(r.message); |
| 144 | } |
| 145 | } |
| 146 | }); |
| 147 | }, |
| 148 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 149 | place_order: function(btn) { |
| 150 | return frappe.call({ |
| 151 | type: "POST", |
| 152 | method: "erpnext.shopping_cart.cart.place_order", |
| 153 | btn: btn, |
| 154 | callback: function(r) { |
| 155 | if(r.exc) { |
| 156 | var msg = ""; |
| 157 | if(r._server_messages) { |
| 158 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 159 | } |
| 160 | |
| 161 | $("#cart-error") |
| 162 | .empty() |
| 163 | .html(msg || frappe._("Something went wrong!")) |
| 164 | .toggle(true); |
| 165 | } else { |
Faris Ansari | 358329d | 2019-05-01 14:56:50 +0530 | [diff] [blame] | 166 | $('.cart-container table').hide(); |
| 167 | $(btn).hide(); |
Faris Ansari | 5f1eebe | 2019-05-01 14:32:15 +0530 | [diff] [blame] | 168 | window.location.href = '/orders/' + encodeURIComponent(r.message); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | }); |
| 172 | }, |
| 173 | |
| 174 | request_quotation: function(btn) { |
| 175 | return frappe.call({ |
| 176 | type: "POST", |
| 177 | method: "erpnext.shopping_cart.cart.request_for_quotation", |
| 178 | btn: btn, |
| 179 | callback: function(r) { |
| 180 | if(r.exc) { |
| 181 | var msg = ""; |
| 182 | if(r._server_messages) { |
| 183 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 184 | } |
| 185 | |
| 186 | $("#cart-error") |
| 187 | .empty() |
| 188 | .html(msg || frappe._("Something went wrong!")) |
| 189 | .toggle(true); |
| 190 | } else { |
Faris Ansari | 358329d | 2019-05-01 14:56:50 +0530 | [diff] [blame] | 191 | $('.cart-container table').hide(); |
| 192 | $(btn).hide(); |
Faris Ansari | 5f1eebe | 2019-05-01 14:32:15 +0530 | [diff] [blame] | 193 | window.location.href = '/quotations/' + encodeURIComponent(r.message); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | }); |
ashish-greycube | 74dc3c9 | 2019-08-12 13:39:25 +0530 | [diff] [blame] | 197 | }, |
| 198 | |
| 199 | bind_coupon_code: function() { |
| 200 | $(".bt-coupon").on("click", function() { |
| 201 | shopping_cart.apply_coupon_code(this); |
| 202 | }); |
| 203 | }, |
| 204 | |
| 205 | apply_coupon_code: function(btn) { |
| 206 | return frappe.call({ |
| 207 | type: "POST", |
| 208 | method: "erpnext.shopping_cart.cart.apply_coupon_code", |
| 209 | btn: btn, |
| 210 | args : { |
| 211 | applied_code : $('.txtcoupon').val(), |
| 212 | applied_referral_sales_partner: $('.txtreferral_sales_partner').val() |
| 213 | }, |
| 214 | callback: function(r) { |
| 215 | if (r && r.message){ |
| 216 | location.reload(); |
| 217 | } |
| 218 | } |
| 219 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 220 | } |
| 221 | }); |
| 222 | |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 223 | frappe.ready(function() { |
Saurabh | 69f9975 | 2016-01-06 16:41:50 +0530 | [diff] [blame] | 224 | $(".cart-icon").hide(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 225 | shopping_cart.bind_events(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 226 | }); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 227 | |
| 228 | function show_terms() { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 229 | var html = $(".cart-terms").html(); |
| 230 | frappe.msgprint(html); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 231 | } |