blob: ee98b23a922489328b6ecda9bf6d191d4edd37cf [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
7frappe.provide("shopping_cart");
8
9$.extend(shopping_cart, {
10 show_error: function(title, text) {
Rushabh Mehtafc3d8712015-07-10 10:11:07 +053011 $("#cart-container").html('<div class="msg-box"><h4>' +
12 title + '</h4><p class="text-muted">' + text + '</p></div>');
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053013 },
14
15 bind_events: function() {
Rushabh Mehta3d766862015-09-16 18:52:52 +053016 shopping_cart.bind_address_select();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053017 shopping_cart.bind_place_order();
18 shopping_cart.bind_change_qty();
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053019 shopping_cart.bind_dropdown_cart_buttons();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053020 },
Kanchan Chauhana756e3f2016-06-22 15:51:42 +053021
Rushabh Mehta3d766862015-09-16 18:52:52 +053022 bind_address_select: function() {
23 $(".cart-addresses").find('input[data-address-name]').on("click", function() {
24 if($(this).prop("checked")) {
25 var me = this;
26
Robert Kirschner0a33d4f2017-03-21 14:52:59 +010027 // uncheck other shipping or billing addresses:
28 if ( $(this).is('input[data-fieldname=customer_address]') ) {
29 $('input[data-fieldname=customer_address]').not(this).prop('checked', false);
30 } else {
31 $('input[data-fieldname=shipping_address_name]').not(this).prop('checked', false);
32 }
33
Rushabh Mehta3d766862015-09-16 18:52:52 +053034 return frappe.call({
35 type: "POST",
36 method: "erpnext.shopping_cart.cart.update_cart_address",
Robert Kirschnerc6b52a32017-03-22 08:10:30 +010037 freeze: true,
Rushabh Mehta3d766862015-09-16 18:52:52 +053038 args: {
39 address_fieldname: $(this).attr("data-fieldname"),
40 address_name: $(this).attr("data-address-name")
41 },
42 callback: function(r) {
43 if(!r.exc) {
Rushabh Mehta72fbf902015-09-17 18:29:44 +053044 $(".cart-tax-items").html(r.message.taxes);
Rushabh Mehta3d766862015-09-16 18:52:52 +053045 }
46 }
47 });
48 } else {
49 return false;
50 }
51 });
52
53 },
54
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053055 bind_place_order: function() {
56 $(".btn-place-order").on("click", function() {
57 shopping_cart.place_order(this);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053058 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053059 },
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();
66 shopping_cart.shopping_cart_update(item_code, newVal);
Kanchan Chauhan239b3512016-05-02 11:43:44 +053067 });
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053068
69 $(".cart-items").on('click', '.number-spinner button', function () {
70 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;
74
75 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);
83 var item_code = input.attr("data-item-code");
84 shopping_cart.shopping_cart_update(item_code, newVal);
Kanchan Chauhanc8d47da2016-06-22 15:46:38 +053085 });
Kanchan Chauhan239b3512016-05-02 11:43:44 +053086 },
Kanchan Chauhanb3573a82016-05-09 12:30:58 +053087
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053088 render_tax_row: function($cart_taxes, doc, shipping_rules) {
89 var shipping_selector;
90 if(shipping_rules) {
91 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
92 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
93 '</select>';
94 }
95
96 var $tax_row = $(repl('<div class="row">\
97 <div class="col-md-9 col-sm-9">\
98 <div class="row">\
99 <div class="col-md-9 col-md-offset-3">' +
100 (shipping_selector || '<p>%(description)s</p>') +
101 '</div>\
102 </div>\
103 </div>\
104 <div class="col-md-3 col-sm-3 text-right">\
105 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
106 </div>\
107 </div>', doc)).appendTo($cart_taxes);
108
109 if(shipping_selector) {
110 $tax_row.find('select option').each(function(i, opt) {
111 if($(opt).html() == doc.description) {
112 $(opt).attr("selected", "selected");
113 }
114 });
115 $tax_row.find('select').on("change", function() {
116 shopping_cart.apply_shipping_rule($(this).val(), this);
117 });
118 }
119 },
120
121 apply_shipping_rule: function(rule, btn) {
122 return frappe.call({
123 btn: btn,
124 type: "POST",
125 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
126 args: { shipping_rule: rule },
127 callback: function(r) {
128 if(!r.exc) {
129 shopping_cart.render(r.message);
130 }
131 }
132 });
133 },
134
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530135 place_order: function(btn) {
136 return frappe.call({
137 type: "POST",
138 method: "erpnext.shopping_cart.cart.place_order",
139 btn: btn,
140 callback: function(r) {
141 if(r.exc) {
142 var msg = "";
143 if(r._server_messages) {
144 msg = JSON.parse(r._server_messages || []).join("<br>");
145 }
146
147 $("#cart-error")
148 .empty()
149 .html(msg || frappe._("Something went wrong!"))
150 .toggle(true);
151 } else {
Anand Doshida858cc2015-02-24 17:50:44 +0530152 window.location.href = "/orders/" + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530153 }
154 }
155 });
156 }
157});
158
Kanchan Chauhana756e3f2016-06-22 15:51:42 +0530159frappe.ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530160 $(".cart-icon").hide();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530161 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530162});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530163
164function show_terms() {
165 var html = $(".cart-terms").html();
166 frappe.msgprint(html);
167}