blob: 0de02676afe8fd42b26ed1f15d83fe5896936797 [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() {
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();
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
prssannab00eb1b2021-01-20 17:52:54 +053026 bind_address_picker_dialog: function() {
27 const d = this.get_update_address_dialog();
28 this.parent.find('.btn-change-address').on('click', (e) => {
29 const type = $(e.currentTarget).parents('.address-container').attr('data-address-type');
30 $(d.get_field('address_picker').wrapper).html(
31 this.get_address_template(type)
32 );
33 d.show();
Rushabh Mehta3d766862015-09-16 18:52:52 +053034 });
Rushabh Mehta3d766862015-09-16 18:52:52 +053035 },
36
prssannab00eb1b2021-01-20 17:52:54 +053037 get_update_address_dialog() {
38 return new frappe.ui.Dialog({
39 title: "Select Address",
40 fields: [{
41 'fieldtype': 'HTML',
42 'fieldname': 'address_picker',
43 }],
44 primary_action_label: __('Set Address'),
45 primary_action: () => {
46 const $card = d.$wrapper.find('.address-card.active');
47 const address_type = $card.closest('[data-address-type]').attr('data-address-type');
48 const address_name = $card.closest('[data-address-name]').attr('data-address-name');
49 frappe.call({
50 type: "POST",
51 method: "erpnext.shopping_cart.cart.update_cart_address",
52 freeze: true,
53 args: {
54 address_type,
55 address_name
56 },
57 callback: function(r) {
58 d.hide();
59 if(!r.exc) {
60 $(".cart-tax-items").html(r.message.taxes);
61 shopping_cart.parent.find(
62 `.address-container[data-address-type="${address_type}"]`
63 ).html(r.message.address);
64 }
65 }
66 });
67 }
68 });
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 Mehta8ffd4832015-09-17 16:28:30 +053096 bind_place_order: function() {
97 $(".btn-place-order").on("click", function() {
98 shopping_cart.place_order(this);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053099 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530100 },
101
Faris Ansari5f8b3582019-03-19 11:48:32 +0530102 bind_request_quotation: function() {
103 $('.btn-request-for-quotation').on('click', function() {
104 shopping_cart.request_quotation(this);
105 });
106 },
107
Rushabh Mehta8ffd4832015-09-17 16:28:30 +0530108 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 Chauhana756e3f2016-06-22 15:51:42 +0530112 var newVal = $(this).val();
Faris Ansari5f8b3582019-03-19 11:48:32 +0530113 shopping_cart.shopping_cart_update({item_code, qty: newVal});
Kanchan Chauhan239b3512016-05-02 11:43:44 +0530114 });
Faris Ansariab74ca72017-05-30 12:54:42 +0530115
116 $(".cart-items").on('click', '.number-spinner button', function () {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530117 var btn = $(this),
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530118 input = btn.closest('.number-spinner').find('input'),
119 oldValue = input.val().trim(),
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530120 newVal = 0;
Faris Ansariab74ca72017-05-30 12:54:42 +0530121
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530122 if (btn.attr('data-dir') == 'up') {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530123 newVal = parseInt(oldValue) + 1;
124 } else {
125 if (oldValue > 1) {
126 newVal = parseInt(oldValue) - 1;
127 }
128 }
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530129 input.val(newVal);
Faris Ansariab74ca72017-05-30 12:54:42 +0530130 var item_code = input.attr("data-item-code");
Faris Ansari5f8b3582019-03-19 11:48:32 +0530131 shopping_cart.shopping_cart_update({item_code, qty: newVal});
132 });
133 },
134
135 bind_change_notes: function() {
136 $('.cart-items').on('change', 'textarea', function() {
137 const $textarea = $(this);
138 const item_code = $textarea.attr('data-item-code');
139 const qty = $textarea.closest('tr').find('.cart-qty').val();
140 const notes = $textarea.val();
141 shopping_cart.shopping_cart_update({
142 item_code,
143 qty,
144 additional_notes: notes
145 });
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +0530146 });
Kanchan Chauhan239b3512016-05-02 11:43:44 +0530147 },
Faris Ansariab74ca72017-05-30 12:54:42 +0530148
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530149 render_tax_row: function($cart_taxes, doc, shipping_rules) {
150 var shipping_selector;
151 if(shipping_rules) {
152 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530153 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
154 '</select>';
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530155 }
156
157 var $tax_row = $(repl('<div class="row">\
158 <div class="col-md-9 col-sm-9">\
159 <div class="row">\
160 <div class="col-md-9 col-md-offset-3">' +
161 (shipping_selector || '<p>%(description)s</p>') +
162 '</div>\
163 </div>\
164 </div>\
165 <div class="col-md-3 col-sm-3 text-right">\
166 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
167 </div>\
168 </div>', doc)).appendTo($cart_taxes);
169
170 if(shipping_selector) {
171 $tax_row.find('select option').each(function(i, opt) {
172 if($(opt).html() == doc.description) {
173 $(opt).attr("selected", "selected");
174 }
175 });
176 $tax_row.find('select').on("change", function() {
177 shopping_cart.apply_shipping_rule($(this).val(), this);
178 });
179 }
180 },
181
182 apply_shipping_rule: function(rule, btn) {
183 return frappe.call({
184 btn: btn,
185 type: "POST",
186 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
187 args: { shipping_rule: rule },
188 callback: function(r) {
189 if(!r.exc) {
190 shopping_cart.render(r.message);
191 }
192 }
193 });
194 },
195
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530196 place_order: function(btn) {
197 return frappe.call({
198 type: "POST",
199 method: "erpnext.shopping_cart.cart.place_order",
200 btn: btn,
201 callback: function(r) {
202 if(r.exc) {
203 var msg = "";
204 if(r._server_messages) {
205 msg = JSON.parse(r._server_messages || []).join("<br>");
206 }
207
208 $("#cart-error")
209 .empty()
210 .html(msg || frappe._("Something went wrong!"))
211 .toggle(true);
212 } else {
Faris Ansari358329d2019-05-01 14:56:50 +0530213 $('.cart-container table').hide();
214 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530215 window.location.href = '/orders/' + encodeURIComponent(r.message);
Faris Ansari5f8b3582019-03-19 11:48:32 +0530216 }
217 }
218 });
219 },
220
221 request_quotation: function(btn) {
222 return frappe.call({
223 type: "POST",
224 method: "erpnext.shopping_cart.cart.request_for_quotation",
225 btn: btn,
226 callback: function(r) {
227 if(r.exc) {
228 var msg = "";
229 if(r._server_messages) {
230 msg = JSON.parse(r._server_messages || []).join("<br>");
231 }
232
233 $("#cart-error")
234 .empty()
235 .html(msg || frappe._("Something went wrong!"))
236 .toggle(true);
237 } else {
Faris Ansari358329d2019-05-01 14:56:50 +0530238 $('.cart-container table').hide();
239 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530240 window.location.href = '/quotations/' + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530241 }
242 }
243 });
ashish-greycube74dc3c92019-08-12 13:39:25 +0530244 },
245
246 bind_coupon_code: function() {
247 $(".bt-coupon").on("click", function() {
248 shopping_cart.apply_coupon_code(this);
249 });
250 },
251
252 apply_coupon_code: function(btn) {
253 return frappe.call({
254 type: "POST",
255 method: "erpnext.shopping_cart.cart.apply_coupon_code",
256 btn: btn,
257 args : {
258 applied_code : $('.txtcoupon').val(),
259 applied_referral_sales_partner: $('.txtreferral_sales_partner').val()
260 },
261 callback: function(r) {
262 if (r && r.message){
263 location.reload();
264 }
265 }
266 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530267 }
268});
269
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530270frappe.ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530271 $(".cart-icon").hide();
prssannab00eb1b2021-01-20 17:52:54 +0530272 shopping_cart.parent = $(".cart-container");
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530273 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530274});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530275
276function show_terms() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530277 var html = $(".cart-terms").html();
278 frappe.msgprint(html);
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530279}