blob: 456bc7e41368ee55909446306ba62bcc9f9a5759 [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);
29 const address_fieldname = $card.closest('[data-fieldname]').attr('data-fieldname');
30 const address_name = $card.closest('[data-address-name]').attr('data-address-name');
Rushabh Mehta3d766862015-09-16 18:52:52 +053031
Faris Ansari5f8b3582019-03-19 11:48:32 +053032 return frappe.call({
33 type: "POST",
34 method: "erpnext.shopping_cart.cart.update_cart_address",
35 freeze: true,
36 args: {
37 address_fieldname,
38 address_name
39 },
40 callback: function(r) {
41 if(!r.exc) {
42 $(".cart-tax-items").html(r.message.taxes);
Rushabh Mehta3d766862015-09-16 18:52:52 +053043 }
Faris Ansari5f8b3582019-03-19 11:48:32 +053044 }
45 });
Rushabh Mehta3d766862015-09-16 18:52:52 +053046 });
Rushabh Mehta3d766862015-09-16 18:52:52 +053047 },
48
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053049 bind_place_order: function() {
50 $(".btn-place-order").on("click", function() {
51 shopping_cart.place_order(this);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053052 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053053 },
54
Faris Ansari5f8b3582019-03-19 11:48:32 +053055 bind_request_quotation: function() {
56 $('.btn-request-for-quotation').on('click', function() {
57 shopping_cart.request_quotation(this);
58 });
59 },
60
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053061 bind_change_qty: function() {
62 // bind update button
63 $(".cart-items").on("change", ".cart-qty", function() {
64 var item_code = $(this).attr("data-item-code");
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053065 var newVal = $(this).val();
Faris Ansari5f8b3582019-03-19 11:48:32 +053066 shopping_cart.shopping_cart_update({item_code, qty: newVal});
Kanchan Chauhan239b3512016-05-02 11:43:44 +053067 });
Faris Ansariab74ca72017-05-30 12:54:42 +053068
69 $(".cart-items").on('click', '.number-spinner button', function () {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053070 var btn = $(this),
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053071 input = btn.closest('.number-spinner').find('input'),
72 oldValue = input.val().trim(),
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053073 newVal = 0;
Faris Ansariab74ca72017-05-30 12:54:42 +053074
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053075 if (btn.attr('data-dir') == 'up') {
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053076 newVal = parseInt(oldValue) + 1;
77 } else {
78 if (oldValue > 1) {
79 newVal = parseInt(oldValue) - 1;
80 }
81 }
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053082 input.val(newVal);
Faris Ansariab74ca72017-05-30 12:54:42 +053083 var item_code = input.attr("data-item-code");
Faris Ansari5f8b3582019-03-19 11:48:32 +053084 shopping_cart.shopping_cart_update({item_code, qty: newVal});
85 });
86 },
87
88 bind_change_notes: function() {
89 $('.cart-items').on('change', 'textarea', function() {
90 const $textarea = $(this);
91 const item_code = $textarea.attr('data-item-code');
92 const qty = $textarea.closest('tr').find('.cart-qty').val();
93 const notes = $textarea.val();
94 shopping_cart.shopping_cart_update({
95 item_code,
96 qty,
97 additional_notes: notes
98 });
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053099 });
Kanchan Chauhan239b3512016-05-02 11:43:44 +0530100 },
Faris Ansariab74ca72017-05-30 12:54:42 +0530101
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530102 render_tax_row: function($cart_taxes, doc, shipping_rules) {
103 var shipping_selector;
104 if(shipping_rules) {
105 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530106 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
107 '</select>';
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530108 }
109
110 var $tax_row = $(repl('<div class="row">\
111 <div class="col-md-9 col-sm-9">\
112 <div class="row">\
113 <div class="col-md-9 col-md-offset-3">' +
114 (shipping_selector || '<p>%(description)s</p>') +
115 '</div>\
116 </div>\
117 </div>\
118 <div class="col-md-3 col-sm-3 text-right">\
119 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
120 </div>\
121 </div>', doc)).appendTo($cart_taxes);
122
123 if(shipping_selector) {
124 $tax_row.find('select option').each(function(i, opt) {
125 if($(opt).html() == doc.description) {
126 $(opt).attr("selected", "selected");
127 }
128 });
129 $tax_row.find('select').on("change", function() {
130 shopping_cart.apply_shipping_rule($(this).val(), this);
131 });
132 }
133 },
134
135 apply_shipping_rule: function(rule, btn) {
136 return frappe.call({
137 btn: btn,
138 type: "POST",
139 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
140 args: { shipping_rule: rule },
141 callback: function(r) {
142 if(!r.exc) {
143 shopping_cart.render(r.message);
144 }
145 }
146 });
147 },
148
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530149 place_order: function(btn) {
150 return frappe.call({
151 type: "POST",
152 method: "erpnext.shopping_cart.cart.place_order",
153 btn: btn,
154 callback: function(r) {
155 if(r.exc) {
156 var msg = "";
157 if(r._server_messages) {
158 msg = JSON.parse(r._server_messages || []).join("<br>");
159 }
160
161 $("#cart-error")
162 .empty()
163 .html(msg || frappe._("Something went wrong!"))
164 .toggle(true);
165 } else {
Faris Ansari358329d2019-05-01 14:56:50 +0530166 $('.cart-container table').hide();
167 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530168 window.location.href = '/orders/' + encodeURIComponent(r.message);
Faris Ansari5f8b3582019-03-19 11:48:32 +0530169 }
170 }
171 });
172 },
173
174 request_quotation: function(btn) {
175 return frappe.call({
176 type: "POST",
177 method: "erpnext.shopping_cart.cart.request_for_quotation",
178 btn: btn,
179 callback: function(r) {
180 if(r.exc) {
181 var msg = "";
182 if(r._server_messages) {
183 msg = JSON.parse(r._server_messages || []).join("<br>");
184 }
185
186 $("#cart-error")
187 .empty()
188 .html(msg || frappe._("Something went wrong!"))
189 .toggle(true);
190 } else {
Faris Ansari358329d2019-05-01 14:56:50 +0530191 $('.cart-container table').hide();
192 $(btn).hide();
Faris Ansari5f1eebe2019-05-01 14:32:15 +0530193 window.location.href = '/quotations/' + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530194 }
195 }
196 });
ashish-greycube74dc3c92019-08-12 13:39:25 +0530197 },
198
199 bind_coupon_code: function() {
200 $(".bt-coupon").on("click", function() {
201 shopping_cart.apply_coupon_code(this);
202 });
203 },
204
205 apply_coupon_code: function(btn) {
206 return frappe.call({
207 type: "POST",
208 method: "erpnext.shopping_cart.cart.apply_coupon_code",
209 btn: btn,
210 args : {
211 applied_code : $('.txtcoupon').val(),
212 applied_referral_sales_partner: $('.txtreferral_sales_partner').val()
213 },
214 callback: function(r) {
215 if (r && r.message){
216 location.reload();
217 }
218 }
219 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530220 }
221});
222
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530223frappe.ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530224 $(".cart-icon").hide();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530225 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530226});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530227
228function show_terms() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530229 var html = $(".cart-terms").html();
230 frappe.msgprint(html);
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530231}