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