blob: e413f0b2e9991097150f04d7dc156f27b46b601b [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 }
Rushabh Mehta8ffd4832015-09-17 16:28:30 +053069 },
70 });
71 });
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053072
Rushabh Mehta3daa49a2014-10-21 16:16:30 +053073 },
74
75 render_tax_row: function($cart_taxes, doc, shipping_rules) {
76 var shipping_selector;
77 if(shipping_rules) {
78 shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
79 return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
80 '</select>';
81 }
82
83 var $tax_row = $(repl('<div class="row">\
84 <div class="col-md-9 col-sm-9">\
85 <div class="row">\
86 <div class="col-md-9 col-md-offset-3">' +
87 (shipping_selector || '<p>%(description)s</p>') +
88 '</div>\
89 </div>\
90 </div>\
91 <div class="col-md-3 col-sm-3 text-right">\
92 <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
93 </div>\
94 </div>', doc)).appendTo($cart_taxes);
95
96 if(shipping_selector) {
97 $tax_row.find('select option').each(function(i, opt) {
98 if($(opt).html() == doc.description) {
99 $(opt).attr("selected", "selected");
100 }
101 });
102 $tax_row.find('select').on("change", function() {
103 shopping_cart.apply_shipping_rule($(this).val(), this);
104 });
105 }
106 },
107
108 apply_shipping_rule: function(rule, btn) {
109 return frappe.call({
110 btn: btn,
111 type: "POST",
112 method: "erpnext.shopping_cart.cart.apply_shipping_rule",
113 args: { shipping_rule: rule },
114 callback: function(r) {
115 if(!r.exc) {
116 shopping_cart.render(r.message);
117 }
118 }
119 });
120 },
121
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530122 place_order: function(btn) {
123 return frappe.call({
124 type: "POST",
125 method: "erpnext.shopping_cart.cart.place_order",
126 btn: btn,
127 callback: function(r) {
128 if(r.exc) {
129 var msg = "";
130 if(r._server_messages) {
131 msg = JSON.parse(r._server_messages || []).join("<br>");
132 }
133
134 $("#cart-error")
135 .empty()
136 .html(msg || frappe._("Something went wrong!"))
137 .toggle(true);
138 } else {
Anand Doshida858cc2015-02-24 17:50:44 +0530139 window.location.href = "/orders/" + encodeURIComponent(r.message);
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530140 }
141 }
142 });
143 }
144});
145
146$(document).ready(function() {
147 shopping_cart.bind_events();
Rushabh Mehta3daa49a2014-10-21 16:16:30 +0530148});