blob: cc321579bfd319cb0db7610e6c3f3f11074db37e [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();
Rushabh Mehta3d766862015-09-16 18:52:52 +053019
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053020 },
21
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
27 return frappe.call({
28 type: "POST",
29 method: "erpnext.shopping_cart.cart.update_cart_address",
30 args: {
31 address_fieldname: $(this).attr("data-fieldname"),
32 address_name: $(this).attr("data-address-name")
33 },
34 callback: function(r) {
35 if(!r.exc) {
Rushabh Mehta72fbf902015-09-17 18:29:44 +053036 $(".cart-tax-items").html(r.message.taxes);
Rushabh Mehta3d766862015-09-16 18:52:52 +053037 }
38 }
39 });
40 } else {
41 return false;
42 }
43 });
44
45 },
46
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053047 bind_place_order: function() {
48 $(".btn-place-order").on("click", function() {
49 shopping_cart.place_order(this);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053050 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053051 },
52
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053053 bind_change_qty: function() {
54 // bind update button
55 $(".cart-items").on("change", ".cart-qty", function() {
56 var item_code = $(this).attr("data-item-code");
Rushabh Mehta72fbf902015-09-17 18:29:44 +053057 frappe.freeze();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053058 shopping_cart.update_cart({
59 item_code: item_code,
60 qty: $(this).val(),
61 with_items: 1,
62 btn: this,
63 callback: function(r) {
Rushabh Mehta72fbf902015-09-17 18:29:44 +053064 frappe.unfreeze();
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053065 if(!r.exc) {
66 $(".cart-items").html(r.message.items);
67 $(".cart-tax-items").html(r.message.taxes);
68 }
69 $(".tax-grand-total").temp_highlight();
70 },
71 });
72 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053073
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053074 },
75
76 render_tax_row: function($cart_taxes, doc, shipping_rules) {
77 var shipping_selector;
78 if(shipping_rules) {
79 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
80 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
81 '</select>';
82 }
83
84 var $tax_row = $(repl('<div class="row">\
85 <div class="col-md-9 col-sm-9">\
86 <div class="row">\
87 <div class="col-md-9 col-md-offset-3">' +
88 (shipping_selector || '<p>%(description)s</p>') +
89 '</div>\
90 </div>\
91 </div>\
92 <div class="col-md-3 col-sm-3 text-right">\
93 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
94 </div>\
95 </div>', doc)).appendTo($cart_taxes);
96
97 if(shipping_selector) {
98 $tax_row.find('select option').each(function(i, opt) {
99 if($(opt).html() == doc.description) {
100 $(opt).attr("selected", "selected");
101 }
102 });
103 $tax_row.find('select').on("change", function() {
104 shopping_cart.apply_shipping_rule($(this).val(), this);
105 });
106 }
107 },
108
109 apply_shipping_rule: function(rule, btn) {
110 return frappe.call({
111 btn: btn,
112 type: "POST",
113 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
114 args: { shipping_rule: rule },
115 callback: function(r) {
116 if(!r.exc) {
117 shopping_cart.render(r.message);
118 }
119 }
120 });
121 },
122
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530123 place_order: function(btn) {
124 return frappe.call({
125 type: "POST",
126 method: "erpnext.shopping_cart.cart.place_order",
127 btn: btn,
128 callback: function(r) {
129 if(r.exc) {
130 var msg = "";
131 if(r._server_messages) {
132 msg = JSON.parse(r._server_messages || []).join("<br>");
133 }
134
135 $("#cart-error")
136 .empty()
137 .html(msg || frappe._("Something went wrong!"))
138 .toggle(true);
139 } else {
Anand Doshida858cc2015-02-24 17:50:44 +0530140 window.location.href = "/orders/" + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530141 }
142 }
143 });
144 }
145});
146
147$(document).ready(function() {
148 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530149});