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 |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 3 | |
marination | 017a882 | 2022-02-03 21:34:48 +0530 | [diff] [blame] | 4 | // JS exclusive to /cart page |
marination | 1bb135b | 2021-08-26 19:14:10 +0530 | [diff] [blame] | 5 | frappe.provide("erpnext.e_commerce.shopping_cart"); |
| 6 | var shopping_cart = erpnext.e_commerce.shopping_cart; |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 7 | |
| 8 | $.extend(shopping_cart, { |
| 9 | show_error: function(title, text) { |
Rushabh Mehta | fc3d871 | 2015-07-10 10:11:07 +0530 | [diff] [blame] | 10 | $("#cart-container").html('<div class="msg-box"><h4>' + |
| 11 | title + '</h4><p class="text-muted">' + text + '</p></div>'); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 12 | }, |
| 13 | |
| 14 | bind_events: function() { |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 15 | shopping_cart.bind_address_picker_dialog(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 16 | shopping_cart.bind_place_order(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 17 | shopping_cart.bind_request_quotation(); |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 18 | shopping_cart.bind_change_qty(); |
marination | 53bb7a9 | 2021-05-27 18:53:11 +0530 | [diff] [blame] | 19 | shopping_cart.bind_remove_cart_item(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 20 | shopping_cart.bind_change_notes(); |
ashish-greycube | 74dc3c9 | 2019-08-12 13:39:25 +0530 | [diff] [blame] | 21 | shopping_cart.bind_coupon_code(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 22 | }, |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 23 | |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 24 | bind_address_picker_dialog: function() { |
| 25 | const d = this.get_update_address_dialog(); |
| 26 | this.parent.find('.btn-change-address').on('click', (e) => { |
| 27 | const type = $(e.currentTarget).parents('.address-container').attr('data-address-type'); |
| 28 | $(d.get_field('address_picker').wrapper).html( |
| 29 | this.get_address_template(type) |
| 30 | ); |
| 31 | d.show(); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 32 | }); |
Rushabh Mehta | 3d76686 | 2015-09-16 18:52:52 +0530 | [diff] [blame] | 33 | }, |
| 34 | |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 35 | get_update_address_dialog() { |
prssanna | a3585e4 | 2021-02-01 19:50:27 +0530 | [diff] [blame] | 36 | let d = new frappe.ui.Dialog({ |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 37 | title: "Select Address", |
| 38 | fields: [{ |
| 39 | 'fieldtype': 'HTML', |
| 40 | 'fieldname': 'address_picker', |
| 41 | }], |
| 42 | primary_action_label: __('Set Address'), |
| 43 | primary_action: () => { |
| 44 | const $card = d.$wrapper.find('.address-card.active'); |
| 45 | const address_type = $card.closest('[data-address-type]').attr('data-address-type'); |
| 46 | const address_name = $card.closest('[data-address-name]').attr('data-address-name'); |
| 47 | frappe.call({ |
| 48 | type: "POST", |
marination | 22f41a1 | 2021-02-25 13:56:38 +0530 | [diff] [blame] | 49 | method: "erpnext.e_commerce.shopping_cart.cart.update_cart_address", |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 50 | freeze: true, |
| 51 | args: { |
| 52 | address_type, |
| 53 | address_name |
| 54 | }, |
| 55 | callback: function(r) { |
| 56 | d.hide(); |
prssanna | a3585e4 | 2021-02-01 19:50:27 +0530 | [diff] [blame] | 57 | if (!r.exc) { |
Marica | 4096b14 | 2021-09-15 14:00:14 +0530 | [diff] [blame] | 58 | $(".cart-tax-items").html(r.message.total); |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 59 | shopping_cart.parent.find( |
| 60 | `.address-container[data-address-type="${address_type}"]` |
| 61 | ).html(r.message.address); |
| 62 | } |
| 63 | } |
| 64 | }); |
| 65 | } |
| 66 | }); |
prssanna | a3585e4 | 2021-02-01 19:50:27 +0530 | [diff] [blame] | 67 | |
| 68 | return d; |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 69 | }, |
| 70 | |
| 71 | get_address_template(type) { |
| 72 | return { |
| 73 | shipping: `<div class="mb-3" data-section="shipping-address"> |
| 74 | <div class="row no-gutters" data-fieldname="shipping_address_name"> |
| 75 | {% for address in shipping_addresses %} |
| 76 | <div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="shipping" |
| 77 | {% if doc.shipping_address_name == address.name %} data-active {% endif %}> |
| 78 | {% include "templates/includes/cart/address_picker_card.html" %} |
| 79 | </div> |
| 80 | {% endfor %} |
| 81 | </div> |
| 82 | </div>`, |
| 83 | billing: `<div class="mb-3" data-section="billing-address"> |
| 84 | <div class="row no-gutters" data-fieldname="customer_address"> |
| 85 | {% for address in billing_addresses %} |
| 86 | <div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="billing" |
| 87 | {% if doc.shipping_address_name == address.name %} data-active {% endif %}> |
| 88 | {% include "templates/includes/cart/address_picker_card.html" %} |
| 89 | </div> |
| 90 | {% endfor %} |
| 91 | </div> |
| 92 | </div>`, |
| 93 | }[type]; |
| 94 | }, |
| 95 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 96 | bind_place_order: function() { |
| 97 | $(".btn-place-order").on("click", function() { |
| 98 | shopping_cart.place_order(this); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 99 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 100 | }, |
| 101 | |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 102 | bind_request_quotation: function() { |
| 103 | $('.btn-request-for-quotation').on('click', function() { |
| 104 | shopping_cart.request_quotation(this); |
| 105 | }); |
| 106 | }, |
| 107 | |
Rushabh Mehta | 8ffd483 | 2015-09-17 16:28:30 +0530 | [diff] [blame] | 108 | bind_change_qty: function() { |
| 109 | // bind update button |
| 110 | $(".cart-items").on("change", ".cart-qty", function() { |
| 111 | var item_code = $(this).attr("data-item-code"); |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 112 | var newVal = $(this).val(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 113 | shopping_cart.shopping_cart_update({item_code, qty: newVal}); |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 114 | }); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 115 | |
| 116 | $(".cart-items").on('click', '.number-spinner button', function () { |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 117 | var btn = $(this), |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 118 | input = btn.closest('.number-spinner').find('input'), |
| 119 | oldValue = input.val().trim(), |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 120 | newVal = 0; |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 121 | |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 122 | if (btn.attr('data-dir') == 'up') { |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 123 | newVal = parseInt(oldValue) + 1; |
| 124 | } else { |
| 125 | if (oldValue > 1) { |
| 126 | newVal = parseInt(oldValue) - 1; |
| 127 | } |
| 128 | } |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 129 | input.val(newVal); |
marination | 423d751 | 2021-05-19 21:17:47 +0530 | [diff] [blame] | 130 | |
| 131 | let notes = input.closest("td").siblings().find(".notes").text().trim(); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 132 | var item_code = input.attr("data-item-code"); |
marination | 423d751 | 2021-05-19 21:17:47 +0530 | [diff] [blame] | 133 | shopping_cart.shopping_cart_update({ |
| 134 | item_code, |
| 135 | qty: newVal, |
| 136 | additional_notes: notes |
| 137 | }); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 138 | }); |
| 139 | }, |
| 140 | |
| 141 | bind_change_notes: function() { |
| 142 | $('.cart-items').on('change', 'textarea', function() { |
| 143 | const $textarea = $(this); |
| 144 | const item_code = $textarea.attr('data-item-code'); |
| 145 | const qty = $textarea.closest('tr').find('.cart-qty').val(); |
| 146 | const notes = $textarea.val(); |
| 147 | shopping_cart.shopping_cart_update({ |
| 148 | item_code, |
| 149 | qty, |
| 150 | additional_notes: notes |
| 151 | }); |
Kanchan Chauhan | c8d47da | 2016-06-22 15:46:38 +0530 | [diff] [blame] | 152 | }); |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 153 | }, |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 154 | |
marination | 53bb7a9 | 2021-05-27 18:53:11 +0530 | [diff] [blame] | 155 | bind_remove_cart_item: function() { |
| 156 | $(".cart-items").on("click", ".remove-cart-item", (e) => { |
| 157 | const $remove_cart_item_btn = $(e.currentTarget); |
| 158 | var item_code = $remove_cart_item_btn.data("item-code"); |
| 159 | |
| 160 | shopping_cart.shopping_cart_update({ |
| 161 | item_code: item_code, |
| 162 | qty: 0 |
| 163 | }); |
marination | b0d7e32 | 2021-06-01 12:44:49 +0530 | [diff] [blame] | 164 | }); |
marination | 53bb7a9 | 2021-05-27 18:53:11 +0530 | [diff] [blame] | 165 | }, |
| 166 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 167 | render_tax_row: function($cart_taxes, doc, shipping_rules) { |
| 168 | var shipping_selector; |
| 169 | if(shipping_rules) { |
| 170 | shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 171 | return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") + |
| 172 | '</select>'; |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | var $tax_row = $(repl('<div class="row">\ |
| 176 | <div class="col-md-9 col-sm-9">\ |
| 177 | <div class="row">\ |
| 178 | <div class="col-md-9 col-md-offset-3">' + |
| 179 | (shipping_selector || '<p>%(description)s</p>') + |
| 180 | '</div>\ |
| 181 | </div>\ |
| 182 | </div>\ |
| 183 | <div class="col-md-3 col-sm-3 text-right">\ |
| 184 | <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\ |
| 185 | </div>\ |
| 186 | </div>', doc)).appendTo($cart_taxes); |
| 187 | |
| 188 | if(shipping_selector) { |
| 189 | $tax_row.find('select option').each(function(i, opt) { |
| 190 | if($(opt).html() == doc.description) { |
| 191 | $(opt).attr("selected", "selected"); |
| 192 | } |
| 193 | }); |
| 194 | $tax_row.find('select').on("change", function() { |
| 195 | shopping_cart.apply_shipping_rule($(this).val(), this); |
| 196 | }); |
| 197 | } |
| 198 | }, |
| 199 | |
| 200 | apply_shipping_rule: function(rule, btn) { |
| 201 | return frappe.call({ |
| 202 | btn: btn, |
| 203 | type: "POST", |
marination | 22f41a1 | 2021-02-25 13:56:38 +0530 | [diff] [blame] | 204 | method: "erpnext.e_commerce.shopping_cart.cart.apply_shipping_rule", |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 205 | args: { shipping_rule: rule }, |
| 206 | callback: function(r) { |
| 207 | if(!r.exc) { |
| 208 | shopping_cart.render(r.message); |
| 209 | } |
| 210 | } |
| 211 | }); |
| 212 | }, |
| 213 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 214 | place_order: function(btn) { |
Marica | 4096b14 | 2021-09-15 14:00:14 +0530 | [diff] [blame] | 215 | shopping_cart.freeze(); |
| 216 | |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 217 | return frappe.call({ |
| 218 | type: "POST", |
marination | 22f41a1 | 2021-02-25 13:56:38 +0530 | [diff] [blame] | 219 | method: "erpnext.e_commerce.shopping_cart.cart.place_order", |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 220 | btn: btn, |
| 221 | callback: function(r) { |
| 222 | if(r.exc) { |
Marica | 4096b14 | 2021-09-15 14:00:14 +0530 | [diff] [blame] | 223 | shopping_cart.unfreeze(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 224 | var msg = ""; |
| 225 | if(r._server_messages) { |
| 226 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 227 | } |
| 228 | |
| 229 | $("#cart-error") |
| 230 | .empty() |
| 231 | .html(msg || frappe._("Something went wrong!")) |
| 232 | .toggle(true); |
| 233 | } else { |
Faris Ansari | 358329d | 2019-05-01 14:56:50 +0530 | [diff] [blame] | 234 | $(btn).hide(); |
Faris Ansari | 5f1eebe | 2019-05-01 14:32:15 +0530 | [diff] [blame] | 235 | window.location.href = '/orders/' + encodeURIComponent(r.message); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | }); |
| 239 | }, |
| 240 | |
| 241 | request_quotation: function(btn) { |
Marica | 4096b14 | 2021-09-15 14:00:14 +0530 | [diff] [blame] | 242 | shopping_cart.freeze(); |
| 243 | |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 244 | return frappe.call({ |
| 245 | type: "POST", |
marination | 22f41a1 | 2021-02-25 13:56:38 +0530 | [diff] [blame] | 246 | method: "erpnext.e_commerce.shopping_cart.cart.request_for_quotation", |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 247 | btn: btn, |
| 248 | callback: function(r) { |
| 249 | if(r.exc) { |
Marica | 4096b14 | 2021-09-15 14:00:14 +0530 | [diff] [blame] | 250 | shopping_cart.unfreeze(); |
Faris Ansari | 5f8b358 | 2019-03-19 11:48:32 +0530 | [diff] [blame] | 251 | var msg = ""; |
| 252 | if(r._server_messages) { |
| 253 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 254 | } |
| 255 | |
| 256 | $("#cart-error") |
| 257 | .empty() |
| 258 | .html(msg || frappe._("Something went wrong!")) |
| 259 | .toggle(true); |
| 260 | } else { |
Faris Ansari | 358329d | 2019-05-01 14:56:50 +0530 | [diff] [blame] | 261 | $(btn).hide(); |
Faris Ansari | 5f1eebe | 2019-05-01 14:32:15 +0530 | [diff] [blame] | 262 | window.location.href = '/quotations/' + encodeURIComponent(r.message); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | }); |
ashish-greycube | 74dc3c9 | 2019-08-12 13:39:25 +0530 | [diff] [blame] | 266 | }, |
| 267 | |
| 268 | bind_coupon_code: function() { |
| 269 | $(".bt-coupon").on("click", function() { |
| 270 | shopping_cart.apply_coupon_code(this); |
| 271 | }); |
| 272 | }, |
| 273 | |
| 274 | apply_coupon_code: function(btn) { |
| 275 | return frappe.call({ |
| 276 | type: "POST", |
marination | 22f41a1 | 2021-02-25 13:56:38 +0530 | [diff] [blame] | 277 | method: "erpnext.e_commerce.shopping_cart.cart.apply_coupon_code", |
ashish-greycube | 74dc3c9 | 2019-08-12 13:39:25 +0530 | [diff] [blame] | 278 | btn: btn, |
| 279 | args : { |
| 280 | applied_code : $('.txtcoupon').val(), |
| 281 | applied_referral_sales_partner: $('.txtreferral_sales_partner').val() |
| 282 | }, |
| 283 | callback: function(r) { |
| 284 | if (r && r.message){ |
| 285 | location.reload(); |
| 286 | } |
| 287 | } |
| 288 | }); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 289 | } |
| 290 | }); |
| 291 | |
Kanchan Chauhan | a756e3f | 2016-06-22 15:51:42 +0530 | [diff] [blame] | 292 | frappe.ready(function() { |
marination | f47db26 | 2022-02-03 12:54:59 +0530 | [diff] [blame] | 293 | if (window.location.pathname === "/cart") { |
| 294 | $(".cart-icon").hide(); |
| 295 | } |
prssanna | b00eb1b | 2021-01-20 17:52:54 +0530 | [diff] [blame] | 296 | shopping_cart.parent = $(".cart-container"); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 297 | shopping_cart.bind_events(); |
Rushabh Mehta | 3daa49a | 2014-10-21 16:16:30 +0530 | [diff] [blame] | 298 | }); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 299 | |
| 300 | function show_terms() { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 301 | var html = $(".cart-terms").html(); |
| 302 | frappe.msgprint(html); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 303 | } |