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() { |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 20 | wn.cart.bind_events(); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 21 | wn.call({ |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 22 | type: "POST", |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 23 | method: "website.helpers.cart.get_cart_quotation", |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 24 | callback: function(r) { |
| 25 | console.log(r); |
| 26 | $("#cart-container").removeClass("hide"); |
| 27 | $(".progress").remove(); |
| 28 | if(r.exc) { |
| 29 | if(r.exc.indexOf("WebsitePriceListMissingError")!==-1) { |
| 30 | wn.cart.show_error("Oops!", "Price List not configured."); |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 31 | } else if(r["403"]) { |
| 32 | wn.cart.show_error("Hey!", "You need to be logged in to view your cart."); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 33 | } else { |
| 34 | wn.cart.show_error("Oops!", "Something went wrong."); |
| 35 | } |
| 36 | } else { |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 37 | wn.cart.set_cart_count(); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 38 | wn.cart.render(r.message); |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 39 | } |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 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 | }); |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 69 | |
| 70 | $("#cart-add-shipping-address").on("click", function() { |
| 71 | window.location.href = "address?address_fieldname=shipping_address_name"; |
| 72 | }); |
| 73 | |
| 74 | $("#cart-add-billing-address").on("click", function() { |
| 75 | window.location.href = "address?address_fieldname=customer_address"; |
| 76 | }); |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 77 | |
| 78 | $(".btn-place-order").on("click", function() { |
| 79 | wn.cart.place_order(); |
| 80 | }); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 81 | }, |
| 82 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 83 | render: function(out) { |
| 84 | var doclist = out.doclist; |
| 85 | var addresses = out.addresses; |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 86 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 87 | var $cart_items = $("#cart-items").empty(); |
| 88 | var $cart_taxes = $("#cart-taxes").empty(); |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 89 | var $cart_totals = $("#cart-totals").empty(); |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 90 | var $cart_billing_address = $("#cart-billing-address").empty(); |
| 91 | var $cart_shipping_address = $("#cart-shipping-address").empty(); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 92 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 93 | var no_items = $.map(doclist, function(d) { return d.item_code || null;}).length===0; |
| 94 | if(no_items) { |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 95 | wn.cart.show_error("Empty :-(", "Go ahead and add something to your cart."); |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 96 | $("#cart-addresses").toggle(false); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 100 | var shipping_rule_added = false; |
| 101 | var taxes_exist = false; |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 102 | var shipping_rule_labels = $.map(out.shipping_rules || [], function(rule) { return rule[1]; }); |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 103 | $.each(doclist, function(i, doc) { |
| 104 | if(doc.doctype === "Quotation Item") { |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 105 | wn.cart.render_item_row($cart_items, doc); |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 106 | } else if (doc.doctype === "Sales Taxes and Charges") { |
| 107 | if(out.shipping_rules && out.shipping_rules.length && |
| 108 | shipping_rule_labels.indexOf(doc.description)!==-1) { |
| 109 | shipping_rule_added = true; |
| 110 | wn.cart.render_tax_row($cart_taxes, doc, out.shipping_rules); |
| 111 | } else { |
| 112 | wn.cart.render_tax_row($cart_taxes, doc); |
| 113 | } |
| 114 | |
| 115 | taxes_exist = true; |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 116 | } |
| 117 | }); |
| 118 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 119 | if(out.shipping_rules && out.shipping_rules.length && !shipping_rule_added) { |
| 120 | wn.cart.render_tax_row($cart_taxes, {description: "", formatted_tax_amount: ""}, |
| 121 | out.shipping_rules); |
| 122 | taxes_exist = true; |
| 123 | } |
| 124 | |
| 125 | if(taxes_exist) |
| 126 | $('<hr>').appendTo($cart_taxes); |
| 127 | |
| 128 | wn.cart.render_tax_row($cart_totals, { |
| 129 | description: "<strong>Total</strong>", |
| 130 | formatted_tax_amount: "<strong>" + doclist[0].formatted_grand_total_export + "</strong>" |
| 131 | }); |
| 132 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 133 | if(!(addresses && addresses.length)) { |
| 134 | $cart_shipping_address.html('<div class="well">Hey! Go ahead and add an address</div>'); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 135 | } else { |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 136 | wn.cart.render_address($cart_shipping_address, addresses, doclist[0].shipping_address_name); |
| 137 | wn.cart.render_address($cart_billing_address, addresses, doclist[0].customer_address); |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 138 | } |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 139 | }, |
| 140 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 141 | render_item_row: function($cart_items, doc) { |
| 142 | doc.image_html = doc.image ? |
| 143 | '<div style="height: 120px; overflow: hidden;"><img src="' + doc.image + '" /></div>' : |
| 144 | '{% include "app/website/templates/html/product_missing_image.html" %}'; |
| 145 | |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 146 | if(doc.description === doc.item_name) doc.description = ""; |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 147 | |
| 148 | $(repl('<div class="row">\ |
| 149 | <div class="col col-lg-9 col-sm-9">\ |
| 150 | <div class="row">\ |
| 151 | <div class="col col-lg-3">%(image_html)s</div>\ |
| 152 | <div class="col col-lg-9">\ |
| 153 | <h4><a href="%(page_name)s">%(item_name)s</a></h4>\ |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 154 | <p>%(description)s</p>\ |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 155 | </div>\ |
| 156 | </div>\ |
| 157 | </div>\ |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 158 | <div class="col col-lg-3 col-sm-3 text-right">\ |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 159 | <div class="input-group item-update-cart">\ |
| 160 | <input type="text" placeholder="Qty" value="%(qty)s" \ |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 161 | data-item-code="%(item_code)s" class="text-right">\ |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 162 | <div class="input-group-btn">\ |
| 163 | <button class="btn btn-primary" data-item-code="%(item_code)s">\ |
| 164 | <i class="icon-ok"></i></button>\ |
| 165 | </div>\ |
| 166 | </div>\ |
| 167 | <p style="margin-top: 10px;">at %(formatted_rate)s</p>\ |
| 168 | <small class="text-muted" style="margin-top: 10px;">= %(formatted_amount)s</small>\ |
| 169 | </div>\ |
| 170 | </div><hr>', doc)).appendTo($cart_items); |
| 171 | }, |
| 172 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 173 | render_tax_row: function($cart_taxes, doc, shipping_rules) { |
| 174 | var shipping_selector; |
| 175 | if(shipping_rules) { |
| 176 | shipping_selector = '<select>' + $.map(shipping_rules, function(rule) { |
| 177 | return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") + |
| 178 | '</select>'; |
| 179 | } |
| 180 | |
| 181 | var $tax_row = $(repl('<div class="row">\ |
| 182 | <div class="col col-lg-9 col-sm-9">\ |
| 183 | <div class="row">\ |
| 184 | <div class="col col-lg-9 col-offset-3">' + |
| 185 | (shipping_selector || '<p>%(description)s</p>') + |
| 186 | '</div>\ |
| 187 | </div>\ |
| 188 | </div>\ |
| 189 | <div class="col col-lg-3 col-sm-3 text-right">\ |
| 190 | <p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\ |
| 191 | </div>\ |
| 192 | </div>', doc)).appendTo($cart_taxes); |
| 193 | |
| 194 | if(shipping_selector) { |
| 195 | $tax_row.find('select option').each(function(i, opt) { |
| 196 | if($(opt).html() == doc.description) { |
| 197 | $(opt).attr("selected", "selected"); |
| 198 | } |
| 199 | }); |
| 200 | $tax_row.find('select').on("change", function() { |
| 201 | wn.cart.apply_shipping_rule($(this).val(), this); |
| 202 | }); |
| 203 | } |
| 204 | }, |
| 205 | |
| 206 | apply_shipping_rule: function(rule, btn) { |
| 207 | wn.call({ |
| 208 | btn: btn, |
| 209 | type: "POST", |
| 210 | method: "website.helpers.cart.apply_shipping_rule", |
| 211 | args: { shipping_rule: rule }, |
| 212 | callback: function(r) { |
| 213 | if(!r.exc) { |
| 214 | wn.cart.render(r.message); |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | }, |
| 219 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 220 | render_address: function($address_wrapper, addresses, address_name) { |
| 221 | $.each(addresses, function(i, address) { |
| 222 | $(repl('<div class="accordion-group"> \ |
| 223 | <div class="accordion-heading"> \ |
| 224 | <div class="row"> \ |
| 225 | <div class="col col-lg-10 address-title" \ |
| 226 | data-address-name="%(name)s"><strong>%(name)s</strong></div> \ |
| 227 | <div class="col col-lg-2"><input type="checkbox" \ |
| 228 | data-address-name="%(name)s"></div> \ |
| 229 | </div> \ |
| 230 | </div> \ |
| 231 | <div class="accordion-body collapse" data-address-name="%(name)s"> \ |
| 232 | <div class="accordion-inner">%(display)s</div> \ |
| 233 | </div> \ |
| 234 | </div>', address)) |
| 235 | .css({"margin": "10px auto"}) |
| 236 | .appendTo($address_wrapper); |
| 237 | }); |
| 238 | |
| 239 | $address_wrapper.find(".accordion-heading") |
| 240 | .css({ |
| 241 | "background-color": "#eee", |
| 242 | "padding": "10px", |
| 243 | }) |
| 244 | .find(".address-title") |
| 245 | .css({"cursor": "pointer"}) |
| 246 | .on("click", function() { |
| 247 | $address_wrapper.find('.accordion-body[data-address-name="' |
| 248 | +$(this).attr("data-address-name")+'"]').collapse("toggle"); |
| 249 | }); |
| 250 | |
| 251 | $address_wrapper.find('input[type="checkbox"]').on("click", function() { |
| 252 | if($(this).is(":checked")) { |
| 253 | var me = this; |
| 254 | $address_wrapper.find('input[type="checkbox"]').each(function(i, chk) { |
| 255 | if($(chk).attr("data-address-name")!=$(me).attr("data-address-name")) { |
| 256 | $(chk).removeAttr("checked"); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | wn.call({ |
| 261 | type: "POST", |
| 262 | method: "website.helpers.cart.update_cart_address", |
| 263 | args: { |
| 264 | address_fieldname: $address_wrapper.attr("data-fieldname"), |
| 265 | address_name: $(this).attr("data-address-name") |
| 266 | }, |
| 267 | callback: function(r) { |
| 268 | if(!r.exc) { |
| 269 | wn.cart.render(r.message); |
| 270 | } |
| 271 | } |
| 272 | }); |
| 273 | } else { |
| 274 | return false; |
| 275 | } |
| 276 | }); |
| 277 | |
| 278 | $address_wrapper.find('input[type="checkbox"][data-address-name="'+ address_name +'"]') |
| 279 | .attr("checked", "checked"); |
| 280 | |
| 281 | $address_wrapper.find(".accordion-body").collapse({ |
| 282 | parent: $address_wrapper, |
| 283 | toggle: false |
| 284 | }); |
| 285 | |
| 286 | $address_wrapper.find('.accordion-body[data-address-name="'+ address_name +'"]') |
| 287 | .collapse("show"); |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 288 | }, |
| 289 | |
| 290 | place_order: function() { |
| 291 | wn.call({ |
| 292 | type: "POST", |
| 293 | method: "website.helpers.cart.place_order", |
| 294 | callback: function(r) { |
| 295 | if(r.exc) { |
| 296 | var msg = ""; |
| 297 | if(r._server_messages) { |
| 298 | msg = JSON.parse(r._server_messages || []).join("<br>"); |
| 299 | } |
| 300 | |
| 301 | $("#cart-error") |
| 302 | .empty() |
| 303 | .html(msg || "Something went wrong!") |
| 304 | .toggle(true); |
| 305 | } else { |
| 306 | window.location.href = "order?name=" + encodeURIComponent(r.message); |
| 307 | } |
| 308 | } |
| 309 | }); |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 310 | } |
Anand Doshi | ab69029 | 2013-06-13 11:21:35 +0530 | [diff] [blame] | 311 | }); |