blob: f0781ab68f5b464959ddc38400bd22a78f257c56 [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
marination48b3ce82021-05-13 01:22:05 +05307frappe.provide("e_commerce.shopping_cart");
8var shopping_cart = e_commerce.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() {
prssannab00eb1b2021-01-20 17:52:54 +053017 shopping_cart.bind_address_picker_dialog();
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();
ashish-greycube74dc3c92019-08-12 13:39:25 +053022 shopping_cart.bind_coupon_code();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053023 },
Faris Ansariab74ca72017-05-30 12:54:42 +053024
prssannab00eb1b2021-01-20 17:52:54 +053025 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 Mehta3d766862015-09-16 18:52:52 +053033 });
Rushabh Mehta3d766862015-09-16 18:52:52 +053034 },
35
prssannab00eb1b2021-01-20 17:52:54 +053036 get_update_address_dialog() {
prssannaa3585e42021-02-01 19:50:27 +053037 let d = new frappe.ui.Dialog({
prssannab00eb1b2021-01-20 17:52:54 +053038 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",
marination22f41a12021-02-25 13:56:38 +053050 method: "erpnext.e_commerce.shopping_cart.cart.update_cart_address",
prssannab00eb1b2021-01-20 17:52:54 +053051 freeze: true,
52 args: {
53 address_type,
54 address_name
55 },
56 callback: function(r) {
57 d.hide();
prssannaa3585e42021-02-01 19:50:27 +053058 if (!r.exc) {
prssannab00eb1b2021-01-20 17:52:54 +053059 $(".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 });
prssannaa3585e42021-02-01 19:50:27 +053068
69 return d;
prssannab00eb1b2021-01-20 17:52:54 +053070 },
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 Mehta8ffd4832015-09-17 16:28:30 +053097 bind_place_order: function() {
98 $(".btn-place-order").on("click", function() {
99 shopping_cart.place_order(this);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530100 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530101 },
102
Faris Ansari5f8b3582019-03-19 11:48:32 +0530103 bind_request_quotation: function() {
104 $('.btn-request-for-quotation').on('click', function() {
105 shopping_cart.request_quotation(this);
106 });
107 },
108
Rushabh Mehta8ffd4832015-09-17 16:28:30 +0530109 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 Chauhana756e3f2016-06-22 15:51:42 +0530113 var newVal = $(this).val();
Faris Ansari5f8b3582019-03-19 11:48:32 +0530114 shopping_cart.shopping_cart_update({item_code, qty: newVal});
Kanchan Chauhan239b3512016-05-02 11:43:44 +0530115 });
Faris Ansariab74ca72017-05-30 12:54:42 +0530116
117 $(".cart-items").on('click', '.number-spinner button', function () {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530118 var btn = $(this),
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530119 input = btn.closest('.number-spinner').find('input'),
120 oldValue = input.val().trim(),
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530121 newVal = 0;
Faris Ansariab74ca72017-05-30 12:54:42 +0530122
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530123 if (btn.attr('data-dir') == 'up') {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530124 newVal = parseInt(oldValue) + 1;
125 } else {
126 if (oldValue > 1) {
127 newVal = parseInt(oldValue) - 1;
128 }
129 }
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530130 input.val(newVal);
marination423d7512021-05-19 21:17:47 +0530131
132 let notes = input.closest("td").siblings().find(".notes").text().trim();
Faris Ansariab74ca72017-05-30 12:54:42 +0530133 var item_code = input.attr("data-item-code");
marination423d7512021-05-19 21:17:47 +0530134 shopping_cart.shopping_cart_update({
135 item_code,
136 qty: newVal,
137 additional_notes: notes
138 });
Faris Ansari5f8b3582019-03-19 11:48:32 +0530139 });
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 Chauhanc8d47da2016-06-22 15:46:38 +0530153 });
Kanchan Chauhan239b3512016-05-02 11:43:44 +0530154 },
Faris Ansariab74ca72017-05-30 12:54:42 +0530155
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530156 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 Ansariab74ca72017-05-30 12:54:42 +0530160 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
161 '</select>';
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530162 }
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",
marination22f41a12021-02-25 13:56:38 +0530193 method: "erpnext.e_commerce.shopping_cart.cart.apply_shipping_rule",
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530194 args: { shipping_rule: rule },
195 callback: function(r) {
196 if(!r.exc) {
197 shopping_cart.render(r.message);
198 }
199 }
200 });
201 },
202
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530203 place_order: function(btn) {
204 return frappe.call({
205 type: "POST",
marination22f41a12021-02-25 13:56:38 +0530206 method: "erpnext.e_commerce.shopping_cart.cart.place_order",
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530207 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 Ansari358329d2019-05-01 14:56:50 +0530220 $('.cart-container table').hide();
221 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530222 window.location.href = '/orders/' + encodeURIComponent(r.message);
Faris Ansari5f8b3582019-03-19 11:48:32 +0530223 }
224 }
225 });
226 },
227
228 request_quotation: function(btn) {
229 return frappe.call({
230 type: "POST",
marination22f41a12021-02-25 13:56:38 +0530231 method: "erpnext.e_commerce.shopping_cart.cart.request_for_quotation",
Faris Ansari5f8b3582019-03-19 11:48:32 +0530232 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 Ansari358329d2019-05-01 14:56:50 +0530245 $('.cart-container table').hide();
246 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530247 window.location.href = '/quotations/' + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530248 }
249 }
250 });
ashish-greycube74dc3c92019-08-12 13:39:25 +0530251 },
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",
marination22f41a12021-02-25 13:56:38 +0530262 method: "erpnext.e_commerce.shopping_cart.cart.apply_coupon_code",
ashish-greycube74dc3c92019-08-12 13:39:25 +0530263 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 Mehta3daa49a2014-10-21 16:16:30 +0530274 }
275});
276
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530277frappe.ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530278 $(".cart-icon").hide();
prssannab00eb1b2021-01-20 17:52:54 +0530279 shopping_cart.parent = $(".cart-container");
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530280 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530281});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530282
283function show_terms() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530284 var html = $(".cart-terms").html();
285 frappe.msgprint(html);
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530286}