blob: 3e6a344c1c1ae20946a03f1ffd9e31ee7243a632 [file] [log] [blame]
Anand Doshiab690292013-06-13 11:21:35 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17// js inside blog page
18
19$(document).ready(function() {
20 // make list of items in the cart
Anand Doshi3dceb842013-06-19 14:57:14 +053021 // wn.cart.render();
Anand Doshic2a35272013-06-19 17:19:20 +053022 wn.cart.bind_events();
Anand Doshi3dceb842013-06-19 14:57:14 +053023 wn.call({
Anand Doshic2a35272013-06-19 17:19:20 +053024 type: "POST",
Anand Doshi3dceb842013-06-19 14:57:14 +053025 method: "website.helpers.cart.get_cart_quotation",
Anand Doshi3dceb842013-06-19 14:57:14 +053026 callback: function(r) {
27 console.log(r);
28 $("#cart-container").removeClass("hide");
29 $(".progress").remove();
30 if(r.exc) {
31 if(r.exc.indexOf("WebsitePriceListMissingError")!==-1) {
32 wn.cart.show_error("Oops!", "Price List not configured.");
33 } else {
34 wn.cart.show_error("Oops!", "Something went wrong.");
35 }
36 } else {
Anand Doshic2a35272013-06-19 17:19:20 +053037 wn.cart.render(r.message);
Anand Doshi3dceb842013-06-19 14:57:14 +053038 }
39
40 }
41 });
Anand Doshiab690292013-06-13 11:21:35 +053042});
43
44// shopping cart
45if(!wn.cart) wn.cart = {};
46$.extend(wn.cart, {
Anand Doshi3dceb842013-06-19 14:57:14 +053047 show_error: function(title, text) {
48 $("#cart-container").html('<div class="well"><h4>' + title + '</h4> ' + text + '</div>');
49 },
50
Anand Doshic2a35272013-06-19 17:19:20 +053051 bind_events: function() {
52 // bind update button
53 $(document).on("click", ".item-update-cart button", function() {
Anand Doshic2a35272013-06-19 17:19:20 +053054 var item_code = $(this).attr("data-item-code");
55 wn.cart.update_cart({
56 item_code: item_code,
57 qty: $('input[data-item-code="'+item_code+'"]').val(),
58 with_doclist: 1,
59 btn: this,
60 callback: function(r) {
61 if(!r.exc) {
62 wn.cart.render(r.message);
63 var $button = $('button[data-item-code="'+item_code+'"]').addClass("btn-success");
64 setTimeout(function() { $button.removeClass("btn-success"); }, 1000);
65 }
66 },
67 });
68 });
69 },
70
Anand Doshi3dceb842013-06-19 14:57:14 +053071 render: function(doclist) {
Anand Doshi3dceb842013-06-19 14:57:14 +053072 var $cart_wrapper = $("#cart-items").empty();
Anand Doshic2a35272013-06-19 17:19:20 +053073
74 if($.map(doclist, function(d) { return d.item_code || null;}).length===0) {
75 wn.cart.show_error("Empty :-(", "Go ahead and add something to your cart.");
76 return;
77 }
78
79 $.each(doclist, function(i, doc) {
80 if(doc.doctype === "Quotation Item") {
81 doc.image_html = doc.image ?
82 '<div style="height: 120px; overflow: hidden;"><img src="' + doc.image + '" /></div>' :
83 '{% include "app/website/templates/html/product_missing_image.html" %}';
84
85 if(!doc.web_short_description) doc.web_short_description = doc.description;
Anand Doshicd6dcac2013-06-20 12:39:25 +053086
Anand Doshic2a35272013-06-19 17:19:20 +053087 $(repl('<div class="row">\
88 <div class="col col-lg-9 col-sm-9">\
89 <div class="row">\
90 <div class="col col-lg-3">%(image_html)s</div>\
91 <div class="col col-lg-9">\
92 <h4><a href="%(page_name)s">%(item_name)s</a></h4>\
93 <p>%(web_short_description)s</p>\
94 </div>\
95 </div>\
Anand Doshiab690292013-06-13 11:21:35 +053096 </div>\
Anand Doshic2a35272013-06-19 17:19:20 +053097 <div class="col col-lg-3 col-sm-3">\
98 <div class="input-group item-update-cart">\
99 <input type="text" placeholder="Qty" value="%(qty)s" \
100 data-item-code="%(item_code)s">\
101 <div class="input-group-btn">\
102 <button class="btn btn-primary" data-item-code="%(item_code)s">\
Anand Doshicd6dcac2013-06-20 12:39:25 +0530103 <i class="icon-ok"></i></button>\
Anand Doshic2a35272013-06-19 17:19:20 +0530104 </div>\
105 </div>\
106 <p style="margin-top: 10px;">at %(formatted_rate)s</p>\
107 <small class="text-muted" style="margin-top: 10px;">= %(formatted_amount)s</small>\
108 </div>\
109 </div><hr>', doc)).appendTo($cart_wrapper);
110
111 }
112 });
113
Anand Doshicd6dcac2013-06-20 12:39:25 +0530114
115
Anand Doshic2a35272013-06-19 17:19:20 +0530116 return;
117
118 if(Object.keys(wn.cart.get_cart()).length) {
Anand Doshiab690292013-06-13 11:21:35 +0530119
120 $.each(wn.cart.get_cart(), function(item_code, item) {
121 item.image_html = item.image ?
122 '<div style="height: 120px; overflow: hidden;"><img src="' + item.image + '" /></div>' :
123 '{% include "app/website/templates/html/product_missing_image.html" %}';
Anand Doshiabc10032013-06-14 17:44:03 +0530124 item.price_html = item.price ? ('<p>at ' + item.price + '</p>') : "";
Anand Doshiab690292013-06-13 11:21:35 +0530125
126 $(repl('<div class="row">\
Anand Doshiabc10032013-06-14 17:44:03 +0530127 <div class="col col-lg-9 col-sm-9">\
Anand Doshiab690292013-06-13 11:21:35 +0530128 <div class="row">\
129 <div class="col col-lg-3">%(image_html)s</div>\
130 <div class="col col-lg-9">\
131 <h4><a href="%(url)s">%(item_name)s</a></h4>\
132 <p>%(description)s</p>\
133 </div>\
134 </div>\
135 </div>\
Anand Doshiabc10032013-06-14 17:44:03 +0530136 <div class="col col-lg-3 col-sm-3">\
137 <p><input type="text" placeholder="Qty" value="%(qty)s" \
138 item_code="%(item_code)s" class="cart-input-qty"></p>\
Anand Doshiab690292013-06-13 11:21:35 +0530139 %(price_html)s\
140 </div>\
141 </div><hr>', item)).appendTo($cart_wrapper);
142 });
Anand Doshiabc10032013-06-14 17:44:03 +0530143
144 $('<p class="text-right"><button type="button" class="btn btn-success checkout-btn">\
145 <span class="icon-ok"></span> Checkout</button></p>')
146 .appendTo($cart_wrapper);
147
Anand Doshiab690292013-06-13 11:21:35 +0530148 } else {
149 $('<p class="alert">No Items added to cart.</p>').appendTo($cart_wrapper);
150 }
Anand Doshiabc10032013-06-14 17:44:03 +0530151 },
152
Anand Doshic2a35272013-06-19 17:19:20 +0530153 // bind_events: function() {
154 // // on change of qty
155 // $(".cart-input-qty").on("change", function on_change_of_qty() {
156 // wn.cart.set_value_in_cart($(this).attr("item_code"), "qty", $(this).val());
157 // });
158 //
159 // // shopping cart button
160 // $(".checkout-btn").on("click", function() {
161 // console.log("checkout!");
162 // console.log(wn.cart.get_cart());
163 //
164 // var user_is_logged_in = getCookie("full_name");
165 // if(user_is_logged_in) {
166 // wn.call({
167 // method: "website.helpers.cart.checkout",
168 // args: {cart: wn.cart.get_cart()},
169 // btn: this,
170 // callback: function(r) {
171 // console.log(r);
172 // }
173 // });
174 // } else {
175 // window.location.href = "login?from=cart";
176 // }
177 // });
178 // }
Anand Doshiab690292013-06-13 11:21:35 +0530179});