blob: c6dfd35e29ec1acd0168bac481147623f0eb142d [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehta3daa49a2014-10-21 16:16:30 +05302// License: GNU General Public License v3. See license.txt
3
4// js inside blog page
5
6// shopping cart
Faris Ansariab74ca72017-05-30 12:54:42 +05307frappe.provide("erpnext.shopping_cart");
8var shopping_cart = erpnext.shopping_cart;
Rushabh Mehta3daa49a2014-10-21 16:16:30 +05309
10$.extend(shopping_cart, {
11 show_error: function(title, text) {
Rushabh Mehtafc3d8712015-07-10 10:11:07 +053012 $("#cart-container").html('<div class="msg-box"><h4>' +
13 title + '</h4><p class="text-muted">' + text + '</p></div>');
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053014 },
15
16 bind_events: function() {
Rushabh Mehta3d766862015-09-16 18:52:52 +053017 shopping_cart.bind_address_select();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053018 shopping_cart.bind_place_order();
Faris Ansari5f8b3582019-03-19 11:48:32 +053019 shopping_cart.bind_request_quotation();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053020 shopping_cart.bind_change_qty();
Faris Ansari5f8b3582019-03-19 11:48:32 +053021 shopping_cart.bind_change_notes();
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053022 shopping_cart.bind_dropdown_cart_buttons();
ashish-greycube74dc3c92019-08-12 13:39:25 +053023 shopping_cart.bind_coupon_code();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053024 },
Faris Ansariab74ca72017-05-30 12:54:42 +053025
Rushabh Mehta3d766862015-09-16 18:52:52 +053026 bind_address_select: function() {
Faris Ansari5f8b3582019-03-19 11:48:32 +053027 $(".cart-addresses").on('click', '.address-card', function(e) {
28 const $card = $(e.currentTarget);
Myuddin khatri4635ff92020-03-12 14:30:28 +053029 const address_type = $card.closest('[data-address-type]').attr('data-address-type');
Faris Ansari5f8b3582019-03-19 11:48:32 +053030 const address_name = $card.closest('[data-address-name]').attr('data-address-name');
Faris Ansari5f8b3582019-03-19 11:48:32 +053031 return frappe.call({
32 type: "POST",
33 method: "erpnext.shopping_cart.cart.update_cart_address",
34 freeze: true,
35 args: {
Myuddin khatri4635ff92020-03-12 14:30:28 +053036 address_type,
Faris Ansari5f8b3582019-03-19 11:48:32 +053037 address_name
38 },
39 callback: function(r) {
40 if(!r.exc) {
41 $(".cart-tax-items").html(r.message.taxes);
Rushabh Mehta3d766862015-09-16 18:52:52 +053042 }
Faris Ansari5f8b3582019-03-19 11:48:32 +053043 }
44 });
Rushabh Mehta3d766862015-09-16 18:52:52 +053045 });
Rushabh Mehta3d766862015-09-16 18:52:52 +053046 },
47
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053048 bind_place_order: function() {
49 $(".btn-place-order").on("click", function() {
50 shopping_cart.place_order(this);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053051 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053052 },
53
Faris Ansari5f8b3582019-03-19 11:48:32 +053054 bind_request_quotation: function() {
55 $('.btn-request-for-quotation').on('click', function() {
56 shopping_cart.request_quotation(this);
57 });
58 },
59
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053060 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 Chauhana756e3f2016-06-22 15:51:42 +053064 var newVal = $(this).val();
Faris Ansari5f8b3582019-03-19 11:48:32 +053065 shopping_cart.shopping_cart_update({item_code, qty: newVal});
Kanchan Chauhan239b3512016-05-02 11:43:44 +053066 });
Faris Ansariab74ca72017-05-30 12:54:42 +053067
68 $(".cart-items").on('click', '.number-spinner button', function () {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053069 var btn = $(this),
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053070 input = btn.closest('.number-spinner').find('input'),
71 oldValue = input.val().trim(),
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053072 newVal = 0;
Faris Ansariab74ca72017-05-30 12:54:42 +053073
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053074 if (btn.attr('data-dir') == 'up') {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053075 newVal = parseInt(oldValue) + 1;
76 } else {
77 if (oldValue > 1) {
78 newVal = parseInt(oldValue) - 1;
79 }
80 }
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053081 input.val(newVal);
Faris Ansariab74ca72017-05-30 12:54:42 +053082 var item_code = input.attr("data-item-code");
Faris Ansari5f8b3582019-03-19 11:48:32 +053083 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 Chauhanc8d47da2016-06-22 15:46:38 +053098 });
Kanchan Chauhan239b3512016-05-02 11:43:44 +053099 },
Faris Ansariab74ca72017-05-30 12:54:42 +0530100
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530101 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 Ansariab74ca72017-05-30 12:54:42 +0530105 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
106 '</select>';
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530107 }
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 Mehta3daa49a2014-10-21 16:16:30 +0530148 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 Ansari358329d2019-05-01 14:56:50 +0530165 $('.cart-container table').hide();
166 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530167 window.location.href = '/orders/' + encodeURIComponent(r.message);
Faris Ansari5f8b3582019-03-19 11:48:32 +0530168 }
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 Ansari358329d2019-05-01 14:56:50 +0530190 $('.cart-container table').hide();
191 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530192 window.location.href = '/quotations/' + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530193 }
194 }
195 });
ashish-greycube74dc3c92019-08-12 13:39:25 +0530196 },
197
198 bind_coupon_code: function() {
199 $(".bt-coupon").on("click", function() {
200 shopping_cart.apply_coupon_code(this);
201 });
202 },
203
204 apply_coupon_code: function(btn) {
205 return frappe.call({
206 type: "POST",
207 method: "erpnext.shopping_cart.cart.apply_coupon_code",
208 btn: btn,
209 args : {
210 applied_code : $('.txtcoupon').val(),
211 applied_referral_sales_partner: $('.txtreferral_sales_partner').val()
212 },
213 callback: function(r) {
214 if (r && r.message){
215 location.reload();
216 }
217 }
218 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530219 }
220});
221
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530222frappe.ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530223 $(".cart-icon").hide();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530224 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530225});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530226
227function show_terms() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530228 var html = $(".cart-terms").html();
229 frappe.msgprint(html);
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530230}