blob: 931df7a1bafe73f465d11bc8889fb1589dd65154 [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 Chauhan239b3512016-05-02 11:43:44 +053019 shopping_cart.bind_remove_item();
Rushabh Mehta3d766862015-09-16 18:52:52 +053020
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053021 },
22
Rushabh Mehta3d766862015-09-16 18:52:52 +053023 bind_address_select: function() {
24 $(".cart-addresses").find('input[data-address-name]').on("click", function() {
25 if($(this).prop("checked")) {
26 var me = this;
27
28 return frappe.call({
29 type: "POST",
30 method: "erpnext.shopping_cart.cart.update_cart_address",
31 args: {
32 address_fieldname: $(this).attr("data-fieldname"),
33 address_name: $(this).attr("data-address-name")
34 },
35 callback: function(r) {
36 if(!r.exc) {
Rushabh Mehta72fbf902015-09-17 18:29:44 +053037 $(".cart-tax-items").html(r.message.taxes);
Rushabh Mehta3d766862015-09-16 18:52:52 +053038 }
39 }
40 });
41 } else {
42 return false;
43 }
44 });
45
46 },
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
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053054 bind_change_qty: function() {
55 // bind update button
56 $(".cart-items").on("change", ".cart-qty", function() {
57 var item_code = $(this).attr("data-item-code");
Rushabh Mehta72fbf902015-09-17 18:29:44 +053058 frappe.freeze();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053059 shopping_cart.update_cart({
60 item_code: item_code,
61 qty: $(this).val(),
62 with_items: 1,
63 btn: this,
64 callback: function(r) {
Rushabh Mehta72fbf902015-09-17 18:29:44 +053065 frappe.unfreeze();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053066 if(!r.exc) {
67 $(".cart-items").html(r.message.items);
68 $(".cart-tax-items").html(r.message.taxes);
Saurabh69f99752016-01-06 16:41:50 +053069 $(".cart-icon").hide();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053070 }
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053071 },
72 });
73 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053074 },
Kanchan Chauhan239b3512016-05-02 11:43:44 +053075
76 bind_remove_item: function() {
77 $(".cart-items").on("click", ".remove-item", function() {
78 var item_code = $(this).attr("data-item-code");
79 shopping_cart.update_cart({
80 item_code: item_code,
81 qty: "0",
82 callback: function(r) {
83 location.reload();
84 if(!r.exc) {
85 $(".cart-items").html(r.message.items);
86 $(".cart-tax-items").html(r.message.taxes);
87 $(".cart-icon").hide();
88 }
89 },
90 });
91 });
92 },
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053093
94 render_tax_row: function($cart_taxes, doc, shipping_rules) {
95 var shipping_selector;
96 if(shipping_rules) {
97 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
98 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
99 '</select>';
100 }
101
102 var $tax_row = $(repl('<div class="row">\
103 <div class="col-md-9 col-sm-9">\
104 <div class="row">\
105 <div class="col-md-9 col-md-offset-3">' +
106 (shipping_selector || '<p>%(description)s</p>') +
107 '</div>\
108 </div>\
109 </div>\
110 <div class="col-md-3 col-sm-3 text-right">\
111 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
112 </div>\
113 </div>', doc)).appendTo($cart_taxes);
114
115 if(shipping_selector) {
116 $tax_row.find('select option').each(function(i, opt) {
117 if($(opt).html() == doc.description) {
118 $(opt).attr("selected", "selected");
119 }
120 });
121 $tax_row.find('select').on("change", function() {
122 shopping_cart.apply_shipping_rule($(this).val(), this);
123 });
124 }
125 },
126
127 apply_shipping_rule: function(rule, btn) {
128 return frappe.call({
129 btn: btn,
130 type: "POST",
131 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
132 args: { shipping_rule: rule },
133 callback: function(r) {
134 if(!r.exc) {
135 shopping_cart.render(r.message);
136 }
137 }
138 });
139 },
140
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530141 place_order: function(btn) {
142 return frappe.call({
143 type: "POST",
144 method: "erpnext.shopping_cart.cart.place_order",
145 btn: btn,
146 callback: function(r) {
147 if(r.exc) {
148 var msg = "";
149 if(r._server_messages) {
150 msg = JSON.parse(r._server_messages || []).join("<br>");
151 }
152
153 $("#cart-error")
154 .empty()
155 .html(msg || frappe._("Something went wrong!"))
156 .toggle(true);
157 } else {
Anand Doshida858cc2015-02-24 17:50:44 +0530158 window.location.href = "/orders/" + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530159 }
160 }
161 });
162 }
163});
164
165$(document).ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530166 $(".cart-icon").hide();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530167 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530168});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530169
170function show_terms() {
171 var html = $(".cart-terms").html();
172 frappe.msgprint(html);
173}