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 |
| 21 | wn.cart.render(); |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 22 | wn.cart.bind_events(); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 23 | }); |
| 24 | |
| 25 | // shopping cart |
| 26 | if(!wn.cart) wn.cart = {}; |
| 27 | $.extend(wn.cart, { |
| 28 | render: function() { |
| 29 | var $cart_wrapper = $("#cart-added-items").empty(); |
| 30 | if(Object.keys(wn.cart.get_cart()).length) { |
| 31 | $('<div class="row">\ |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 32 | <div class="col col-lg-9 col-sm-9">\ |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 33 | <div class="row">\ |
| 34 | <div class="col col-lg-3"></div>\ |
| 35 | <div class="col col-lg-9"><strong>Item Details</strong></div>\ |
| 36 | </div>\ |
| 37 | </div>\ |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 38 | <div class="col col-lg-3 col-sm-3"><strong>Qty</strong></div>\ |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 39 | </div><hr>').appendTo($cart_wrapper); |
| 40 | |
| 41 | $.each(wn.cart.get_cart(), function(item_code, item) { |
| 42 | item.image_html = item.image ? |
| 43 | '<div style="height: 120px; overflow: hidden;"><img src="' + item.image + '" /></div>' : |
| 44 | '{% include "app/website/templates/html/product_missing_image.html" %}'; |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 45 | item.price_html = item.price ? ('<p>at ' + item.price + '</p>') : ""; |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 46 | |
| 47 | $(repl('<div class="row">\ |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 48 | <div class="col col-lg-9 col-sm-9">\ |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 49 | <div class="row">\ |
| 50 | <div class="col col-lg-3">%(image_html)s</div>\ |
| 51 | <div class="col col-lg-9">\ |
| 52 | <h4><a href="%(url)s">%(item_name)s</a></h4>\ |
| 53 | <p>%(description)s</p>\ |
| 54 | </div>\ |
| 55 | </div>\ |
| 56 | </div>\ |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 57 | <div class="col col-lg-3 col-sm-3">\ |
| 58 | <p><input type="text" placeholder="Qty" value="%(qty)s" \ |
| 59 | item_code="%(item_code)s" class="cart-input-qty"></p>\ |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 60 | %(price_html)s\ |
| 61 | </div>\ |
| 62 | </div><hr>', item)).appendTo($cart_wrapper); |
| 63 | }); |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 64 | |
| 65 | $('<p class="text-right"><button type="button" class="btn btn-success checkout-btn">\ |
| 66 | <span class="icon-ok"></span> Checkout</button></p>') |
| 67 | .appendTo($cart_wrapper); |
| 68 | |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 69 | } else { |
| 70 | $('<p class="alert">No Items added to cart.</p>').appendTo($cart_wrapper); |
| 71 | } |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 72 | }, |
| 73 | |
| 74 | bind_events: function() { |
| 75 | // on change of qty |
| 76 | $(".cart-input-qty").on("change", function on_change_of_qty() { |
| 77 | wn.cart.set_value_in_cart($(this).attr("item_code"), "qty", $(this).val()); |
| 78 | }); |
| 79 | |
| 80 | // shopping cart button |
| 81 | $(".checkout-btn").on("click", function() { |
| 82 | console.log("checkout!"); |
| 83 | console.log(wn.cart.get_cart()); |
| 84 | |
| 85 | var user_is_logged_in = getCookie("full_name"); |
| 86 | if(user_is_logged_in) { |
| 87 | wn.call({ |
| 88 | method: "website.helpers.cart.checkout", |
| 89 | args: {cart: wn.cart.get_cart()}, |
| 90 | btn: this, |
| 91 | callback: function(r) { |
| 92 | console.log(r); |
| 93 | } |
| 94 | }); |
| 95 | } else { |
| 96 | window.location.href = "login?from=cart"; |
| 97 | } |
| 98 | }); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 99 | } |
| 100 | }); |