2013-08-05 09:29:54 +00:00
|
|
|
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
|
|
|
|
// License: GNU General Public License v3. See license.txt
|
2013-06-13 05:51:35 +00:00
|
|
|
|
|
|
|
// js inside blog page
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
2013-06-19 11:49:20 +00:00
|
|
|
wn.cart.bind_events();
|
2013-07-29 14:00:39 +00:00
|
|
|
return wn.call({
|
2013-06-19 11:49:20 +00:00
|
|
|
type: "POST",
|
2013-06-19 09:27:14 +00:00
|
|
|
method: "website.helpers.cart.get_cart_quotation",
|
|
|
|
callback: function(r) {
|
|
|
|
console.log(r);
|
|
|
|
$("#cart-container").removeClass("hide");
|
|
|
|
$(".progress").remove();
|
|
|
|
if(r.exc) {
|
|
|
|
if(r.exc.indexOf("WebsitePriceListMissingError")!==-1) {
|
|
|
|
wn.cart.show_error("Oops!", "Price List not configured.");
|
2013-07-02 06:10:16 +00:00
|
|
|
} else if(r["403"]) {
|
|
|
|
wn.cart.show_error("Hey!", "You need to be logged in to view your cart.");
|
2013-06-19 09:27:14 +00:00
|
|
|
} else {
|
|
|
|
wn.cart.show_error("Oops!", "Something went wrong.");
|
|
|
|
}
|
|
|
|
} else {
|
2013-07-10 15:19:44 +00:00
|
|
|
wn.cart.set_cart_count();
|
2013-06-19 11:49:20 +00:00
|
|
|
wn.cart.render(r.message);
|
2013-06-19 09:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-06-13 05:51:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// shopping cart
|
|
|
|
if(!wn.cart) wn.cart = {};
|
|
|
|
$.extend(wn.cart, {
|
2013-06-19 09:27:14 +00:00
|
|
|
show_error: function(title, text) {
|
|
|
|
$("#cart-container").html('<div class="well"><h4>' + title + '</h4> ' + text + '</div>');
|
|
|
|
},
|
|
|
|
|
2013-06-19 11:49:20 +00:00
|
|
|
bind_events: function() {
|
|
|
|
// bind update button
|
|
|
|
$(document).on("click", ".item-update-cart button", function() {
|
|
|
|
var item_code = $(this).attr("data-item-code");
|
|
|
|
wn.cart.update_cart({
|
|
|
|
item_code: item_code,
|
|
|
|
qty: $('input[data-item-code="'+item_code+'"]').val(),
|
|
|
|
with_doclist: 1,
|
|
|
|
btn: this,
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
wn.cart.render(r.message);
|
|
|
|
var $button = $('button[data-item-code="'+item_code+'"]').addClass("btn-success");
|
|
|
|
setTimeout(function() { $button.removeClass("btn-success"); }, 1000);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2013-07-02 06:10:16 +00:00
|
|
|
|
|
|
|
$("#cart-add-shipping-address").on("click", function() {
|
|
|
|
window.location.href = "address?address_fieldname=shipping_address_name";
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#cart-add-billing-address").on("click", function() {
|
|
|
|
window.location.href = "address?address_fieldname=customer_address";
|
|
|
|
});
|
2013-07-10 15:19:44 +00:00
|
|
|
|
|
|
|
$(".btn-place-order").on("click", function() {
|
|
|
|
wn.cart.place_order();
|
|
|
|
});
|
2013-06-19 11:49:20 +00:00
|
|
|
},
|
|
|
|
|
2013-07-02 06:10:16 +00:00
|
|
|
render: function(out) {
|
|
|
|
var doclist = out.doclist;
|
|
|
|
var addresses = out.addresses;
|
2013-07-04 18:15:22 +00:00
|
|
|
|
2013-07-02 06:10:16 +00:00
|
|
|
var $cart_items = $("#cart-items").empty();
|
|
|
|
var $cart_taxes = $("#cart-taxes").empty();
|
2013-07-04 18:15:22 +00:00
|
|
|
var $cart_totals = $("#cart-totals").empty();
|
2013-07-02 06:10:16 +00:00
|
|
|
var $cart_billing_address = $("#cart-billing-address").empty();
|
|
|
|
var $cart_shipping_address = $("#cart-shipping-address").empty();
|
2013-06-19 11:49:20 +00:00
|
|
|
|
2013-07-02 06:10:16 +00:00
|
|
|
var no_items = $.map(doclist, function(d) { return d.item_code || null;}).length===0;
|
|
|
|
if(no_items) {
|
2013-06-19 11:49:20 +00:00
|
|
|
wn.cart.show_error("Empty :-(", "Go ahead and add something to your cart.");
|
2013-07-02 06:10:16 +00:00
|
|
|
$("#cart-addresses").toggle(false);
|
2013-06-19 11:49:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-04 18:15:22 +00:00
|
|
|
var shipping_rule_added = false;
|
|
|
|
var taxes_exist = false;
|
2013-07-08 13:20:33 +00:00
|
|
|
var shipping_rule_labels = $.map(out.shipping_rules || [], function(rule) { return rule[1]; });
|
2013-06-19 11:49:20 +00:00
|
|
|
$.each(doclist, function(i, doc) {
|
|
|
|
if(doc.doctype === "Quotation Item") {
|
2013-07-02 06:10:16 +00:00
|
|
|
wn.cart.render_item_row($cart_items, doc);
|
2013-07-04 18:15:22 +00:00
|
|
|
} else if (doc.doctype === "Sales Taxes and Charges") {
|
|
|
|
if(out.shipping_rules && out.shipping_rules.length &&
|
|
|
|
shipping_rule_labels.indexOf(doc.description)!==-1) {
|
|
|
|
shipping_rule_added = true;
|
|
|
|
wn.cart.render_tax_row($cart_taxes, doc, out.shipping_rules);
|
|
|
|
} else {
|
|
|
|
wn.cart.render_tax_row($cart_taxes, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
taxes_exist = true;
|
2013-06-19 11:49:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-04 18:15:22 +00:00
|
|
|
if(out.shipping_rules && out.shipping_rules.length && !shipping_rule_added) {
|
|
|
|
wn.cart.render_tax_row($cart_taxes, {description: "", formatted_tax_amount: ""},
|
|
|
|
out.shipping_rules);
|
|
|
|
taxes_exist = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(taxes_exist)
|
|
|
|
$('<hr>').appendTo($cart_taxes);
|
|
|
|
|
|
|
|
wn.cart.render_tax_row($cart_totals, {
|
|
|
|
description: "<strong>Total</strong>",
|
|
|
|
formatted_tax_amount: "<strong>" + doclist[0].formatted_grand_total_export + "</strong>"
|
|
|
|
});
|
|
|
|
|
2013-07-02 06:10:16 +00:00
|
|
|
if(!(addresses && addresses.length)) {
|
|
|
|
$cart_shipping_address.html('<div class="well">Hey! Go ahead and add an address</div>');
|
|
|
|
} else {
|
|
|
|
wn.cart.render_address($cart_shipping_address, addresses, doclist[0].shipping_address_name);
|
|
|
|
wn.cart.render_address($cart_billing_address, addresses, doclist[0].customer_address);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render_item_row: function($cart_items, doc) {
|
|
|
|
doc.image_html = doc.image ?
|
|
|
|
'<div style="height: 120px; overflow: hidden;"><img src="' + doc.image + '" /></div>' :
|
|
|
|
'{% include "app/website/templates/html/product_missing_image.html" %}';
|
2013-06-13 05:51:35 +00:00
|
|
|
|
2013-07-15 12:58:14 +00:00
|
|
|
if(doc.description === doc.item_name) doc.description = "";
|
2013-07-02 06:10:16 +00:00
|
|
|
|
|
|
|
$(repl('<div class="row">\
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-9 col-sm-9">\
|
2013-07-02 06:10:16 +00:00
|
|
|
<div class="row">\
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-3">%(image_html)s</div>\
|
|
|
|
<div class="col-md-9">\
|
2013-07-02 06:10:16 +00:00
|
|
|
<h4><a href="%(page_name)s">%(item_name)s</a></h4>\
|
2013-07-15 12:58:14 +00:00
|
|
|
<p>%(description)s</p>\
|
2013-06-13 05:51:35 +00:00
|
|
|
</div>\
|
2013-07-02 06:10:16 +00:00
|
|
|
</div>\
|
|
|
|
</div>\
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-3 col-sm-3 text-right">\
|
2013-07-02 06:10:16 +00:00
|
|
|
<div class="input-group item-update-cart">\
|
|
|
|
<input type="text" placeholder="Qty" value="%(qty)s" \
|
2013-08-21 14:06:23 +00:00
|
|
|
data-item-code="%(item_code)s" class="text-right form-control">\
|
2013-07-02 06:10:16 +00:00
|
|
|
<div class="input-group-btn">\
|
|
|
|
<button class="btn btn-primary" data-item-code="%(item_code)s">\
|
|
|
|
<i class="icon-ok"></i></button>\
|
2013-06-13 05:51:35 +00:00
|
|
|
</div>\
|
2013-07-02 06:10:16 +00:00
|
|
|
</div>\
|
|
|
|
<p style="margin-top: 10px;">at %(formatted_rate)s</p>\
|
|
|
|
<small class="text-muted" style="margin-top: 10px;">= %(formatted_amount)s</small>\
|
|
|
|
</div>\
|
|
|
|
</div><hr>', doc)).appendTo($cart_items);
|
2013-06-14 12:14:03 +00:00
|
|
|
},
|
|
|
|
|
2013-07-04 18:15:22 +00:00
|
|
|
render_tax_row: function($cart_taxes, doc, shipping_rules) {
|
|
|
|
var shipping_selector;
|
|
|
|
if(shipping_rules) {
|
2013-08-21 14:06:23 +00:00
|
|
|
shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
|
2013-07-04 18:15:22 +00:00
|
|
|
return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
|
|
|
|
'</select>';
|
|
|
|
}
|
|
|
|
|
|
|
|
var $tax_row = $(repl('<div class="row">\
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-9 col-sm-9">\
|
2013-07-04 18:15:22 +00:00
|
|
|
<div class="row">\
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-9 col-md-offset-3">' +
|
2013-07-04 18:15:22 +00:00
|
|
|
(shipping_selector || '<p>%(description)s</p>') +
|
|
|
|
'</div>\
|
|
|
|
</div>\
|
|
|
|
</div>\
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-3 col-sm-3 text-right">\
|
2013-07-04 18:15:22 +00:00
|
|
|
<p' + (shipping_selector ? ' style="margin-top: 5px;"' : "") + '>%(formatted_tax_amount)s</p>\
|
|
|
|
</div>\
|
|
|
|
</div>', doc)).appendTo($cart_taxes);
|
|
|
|
|
|
|
|
if(shipping_selector) {
|
|
|
|
$tax_row.find('select option').each(function(i, opt) {
|
|
|
|
if($(opt).html() == doc.description) {
|
|
|
|
$(opt).attr("selected", "selected");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$tax_row.find('select').on("change", function() {
|
|
|
|
wn.cart.apply_shipping_rule($(this).val(), this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
apply_shipping_rule: function(rule, btn) {
|
2013-07-29 14:00:39 +00:00
|
|
|
return wn.call({
|
2013-07-04 18:15:22 +00:00
|
|
|
btn: btn,
|
|
|
|
type: "POST",
|
|
|
|
method: "website.helpers.cart.apply_shipping_rule",
|
|
|
|
args: { shipping_rule: rule },
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
wn.cart.render(r.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-07-02 06:10:16 +00:00
|
|
|
render_address: function($address_wrapper, addresses, address_name) {
|
|
|
|
$.each(addresses, function(i, address) {
|
2013-08-21 14:06:23 +00:00
|
|
|
$(repl('<div class="panel panel-default"> \
|
|
|
|
<div class="panel-heading"> \
|
2013-07-02 06:10:16 +00:00
|
|
|
<div class="row"> \
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-10 address-title" \
|
2013-07-02 06:10:16 +00:00
|
|
|
data-address-name="%(name)s"><strong>%(name)s</strong></div> \
|
2013-08-21 12:18:08 +00:00
|
|
|
<div class="col-md-2"><input type="checkbox" \
|
2013-07-02 06:10:16 +00:00
|
|
|
data-address-name="%(name)s"></div> \
|
|
|
|
</div> \
|
|
|
|
</div> \
|
2013-08-21 14:06:23 +00:00
|
|
|
<div class="panel-collapse collapse" data-address-name="%(name)s"> \
|
|
|
|
<div class="panel-body">%(display)s</div> \
|
2013-07-02 06:10:16 +00:00
|
|
|
</div> \
|
|
|
|
</div>', address))
|
|
|
|
.css({"margin": "10px auto"})
|
|
|
|
.appendTo($address_wrapper);
|
|
|
|
});
|
|
|
|
|
2013-08-21 14:06:23 +00:00
|
|
|
$address_wrapper.find(".panel-heading")
|
2013-07-02 06:10:16 +00:00
|
|
|
.find(".address-title")
|
|
|
|
.css({"cursor": "pointer"})
|
|
|
|
.on("click", function() {
|
2013-08-21 14:06:23 +00:00
|
|
|
$address_wrapper.find('.panel-collapse[data-address-name="'
|
2013-07-02 06:10:16 +00:00
|
|
|
+$(this).attr("data-address-name")+'"]').collapse("toggle");
|
|
|
|
});
|
|
|
|
|
|
|
|
$address_wrapper.find('input[type="checkbox"]').on("click", function() {
|
2013-07-24 07:37:23 +00:00
|
|
|
if($(this).prop("checked")) {
|
2013-07-02 06:10:16 +00:00
|
|
|
var me = this;
|
|
|
|
$address_wrapper.find('input[type="checkbox"]').each(function(i, chk) {
|
|
|
|
if($(chk).attr("data-address-name")!=$(me).attr("data-address-name")) {
|
2013-07-24 07:37:23 +00:00
|
|
|
$(chk).prop("checked", false);
|
2013-07-02 06:10:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-29 14:00:39 +00:00
|
|
|
return wn.call({
|
2013-07-02 06:10:16 +00:00
|
|
|
type: "POST",
|
|
|
|
method: "website.helpers.cart.update_cart_address",
|
|
|
|
args: {
|
|
|
|
address_fieldname: $address_wrapper.attr("data-fieldname"),
|
|
|
|
address_name: $(this).attr("data-address-name")
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
wn.cart.render(r.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$address_wrapper.find('input[type="checkbox"][data-address-name="'+ address_name +'"]')
|
2013-07-24 07:37:23 +00:00
|
|
|
.prop("checked", true);
|
2013-07-02 06:10:16 +00:00
|
|
|
|
2013-08-21 14:06:23 +00:00
|
|
|
$address_wrapper.find(".panel-collapse").collapse({
|
2013-07-02 06:10:16 +00:00
|
|
|
parent: $address_wrapper,
|
|
|
|
toggle: false
|
|
|
|
});
|
|
|
|
|
2013-08-21 14:06:23 +00:00
|
|
|
$address_wrapper.find('.panel-collapse[data-address-name="'+ address_name +'"]')
|
2013-07-02 06:10:16 +00:00
|
|
|
.collapse("show");
|
2013-07-10 15:19:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
place_order: function() {
|
2013-07-29 14:00:39 +00:00
|
|
|
return wn.call({
|
2013-07-10 15:19:44 +00:00
|
|
|
type: "POST",
|
|
|
|
method: "website.helpers.cart.place_order",
|
|
|
|
callback: function(r) {
|
|
|
|
if(r.exc) {
|
|
|
|
var msg = "";
|
|
|
|
if(r._server_messages) {
|
|
|
|
msg = JSON.parse(r._server_messages || []).join("<br>");
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#cart-error")
|
|
|
|
.empty()
|
|
|
|
.html(msg || "Something went wrong!")
|
|
|
|
.toggle(true);
|
|
|
|
} else {
|
|
|
|
window.location.href = "order?name=" + encodeURIComponent(r.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-07-02 06:10:16 +00:00
|
|
|
}
|
2013-06-13 05:51:35 +00:00
|
|
|
});
|