blob: 983898b457b6d978adce8c6dadd73845b3a85d71 [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();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053023 },
Faris Ansariab74ca72017-05-30 12:54:42 +053024
Rushabh Mehta3d766862015-09-16 18:52:52 +053025 bind_address_select: function() {
Faris Ansari5f8b3582019-03-19 11:48:32 +053026 $(".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 Mehta3d766862015-09-16 18:52:52 +053030
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: {
36 address_fieldname,
37 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 Ansari5f8b3582019-03-19 11:48:32 +0530165 window.open('/orders/' + encodeURIComponent(r.message), '_blank');
166 window.location.reload();
167 }
168 }
169 });
170 },
171
172 request_quotation: function(btn) {
173 return frappe.call({
174 type: "POST",
175 method: "erpnext.shopping_cart.cart.request_for_quotation",
176 btn: btn,
177 callback: function(r) {
178 if(r.exc) {
179 var msg = "";
180 if(r._server_messages) {
181 msg = JSON.parse(r._server_messages || []).join("<br>");
182 }
183
184 $("#cart-error")
185 .empty()
186 .html(msg || frappe._("Something went wrong!"))
187 .toggle(true);
188 } else {
189 window.open('/printview?doctype=Quotation&name=' + r.message, '_blank');
190 window.location.reload();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530191 }
192 }
193 });
194 }
195});
196
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530197frappe.ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530198 $(".cart-icon").hide();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530199 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530200});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530201
202function show_terms() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530203 var html = $(".cart-terms").html();
204 frappe.msgprint(html);
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530205}