Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 1 | // 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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 21 | // wn.cart.render(); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 22 | wn.cart.bind_events(); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 23 | wn.call({ |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 24 | type: "POST", |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 25 | method: "website.helpers.cart.get_cart_quotation", |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 26 | 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 Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 37 | wn.cart.render(r.message); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | } |
| 41 | }); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 42 | }); |
| 43 | |
| 44 | // shopping cart |
| 45 | if(!wn.cart) wn.cart = {}; |
| 46 | $.extend(wn.cart, { |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 47 | show_error: function(title, text) { |
| 48 | $("#cart-container").html('<div class="well"><h4>' + title + '</h4> ' + text + '</div>'); |
| 49 | }, |
| 50 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 51 | bind_events: function() { |
| 52 | // bind update button |
| 53 | $(document).on("click", ".item-update-cart button", function() { |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 54 | 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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 71 | render: function(doclist) { |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 72 | var $cart_wrapper = $("#cart-items").empty(); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 73 | |
| 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 Doshi | cd6dcac | 2013-06-20 12:39:25 +0530 | [diff] [blame] | 86 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 87 | $(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 Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 96 | </div>\ |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 97 | <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 Doshi | cd6dcac | 2013-06-20 12:39:25 +0530 | [diff] [blame] | 103 | <i class="icon-ok"></i></button>\ |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 104 | </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 Doshi | cd6dcac | 2013-06-20 12:39:25 +0530 | [diff] [blame] | 114 | |
| 115 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 116 | return; |
| 117 | |
| 118 | if(Object.keys(wn.cart.get_cart()).length) { |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 119 | |
| 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 124 | item.price_html = item.price ? ('<p>at ' + item.price + '</p>') : ""; |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 125 | |
| 126 | $(repl('<div class="row">\ |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 127 | <div class="col col-lg-9 col-sm-9">\ |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 128 | <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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 136 | <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 Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 139 | %(price_html)s\ |
| 140 | </div>\ |
| 141 | </div><hr>', item)).appendTo($cart_wrapper); |
| 142 | }); |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 143 | |
| 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 Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 148 | } else { |
| 149 | $('<p class="alert">No Items added to cart.</p>').appendTo($cart_wrapper); |
| 150 | } |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 151 | }, |
| 152 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 153 | // 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 Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 179 | }); |