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