blob: 2a6570f0523bac246853ca517d6396f677841058 [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();
Kanchan Chauhan239b3512016-05-02 11:43:44 +053066 location.reload();
67 if(!r.exc) {
68 $(".cart-items").html(r.message.items);
69 $(".cart-tax-items").html(r.message.taxes);
70 $(".cart-icon").hide();
71 }
72 },
73 });
74 });
75 },
Kanchan Chauhanb3573a82016-05-09 12:30:58 +053076
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053077 render_tax_row: function($cart_taxes, doc, shipping_rules) {
78 var shipping_selector;
79 if(shipping_rules) {
80 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
81 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
82 '</select>';
83 }
84
85 var $tax_row = $(repl('<div class="row">\
86 <div class="col-md-9 col-sm-9">\
87 <div class="row">\
88 <div class="col-md-9 col-md-offset-3">' +
89 (shipping_selector || '<p>%(description)s</p>') +
90 '</div>\
91 </div>\
92 </div>\
93 <div class="col-md-3 col-sm-3 text-right">\
94 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
95 </div>\
96 </div>', doc)).appendTo($cart_taxes);
97
98 if(shipping_selector) {
99 $tax_row.find('select option').each(function(i, opt) {
100 if($(opt).html() == doc.description) {
101 $(opt).attr("selected", "selected");
102 }
103 });
104 $tax_row.find('select').on("change", function() {
105 shopping_cart.apply_shipping_rule($(this).val(), this);
106 });
107 }
108 },
109
110 apply_shipping_rule: function(rule, btn) {
111 return frappe.call({
112 btn: btn,
113 type: "POST",
114 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
115 args: { shipping_rule: rule },
116 callback: function(r) {
117 if(!r.exc) {
118 shopping_cart.render(r.message);
119 }
120 }
121 });
122 },
123
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530124 place_order: function(btn) {
125 return frappe.call({
126 type: "POST",
127 method: "erpnext.shopping_cart.cart.place_order",
128 btn: btn,
129 callback: function(r) {
130 if(r.exc) {
131 var msg = "";
132 if(r._server_messages) {
133 msg = JSON.parse(r._server_messages || []).join("<br>");
134 }
135
136 $("#cart-error")
137 .empty()
138 .html(msg || frappe._("Something went wrong!"))
139 .toggle(true);
140 } else {
Anand Doshida858cc2015-02-24 17:50:44 +0530141 window.location.href = "/orders/" + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530142 }
143 }
144 });
145 }
146});
147
148$(document).ready(function() {
Saurabh69f99752016-01-06 16:41:50 +0530149 $(".cart-icon").hide();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530150 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530151});
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530152
153function show_terms() {
154 var html = $(".cart-terms").html();
155 frappe.msgprint(html);
156}