2015-03-03 09:25:30 +00:00
|
|
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-10-21 10:46:30 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
// js inside blog page
|
|
|
|
|
|
|
|
// shopping cart
|
|
|
|
frappe.provide("shopping_cart");
|
|
|
|
|
|
|
|
$.extend(shopping_cart, {
|
|
|
|
show_error: function(title, text) {
|
2015-07-10 04:41:07 +00:00
|
|
|
$("#cart-container").html('<div class="msg-box"><h4>' +
|
|
|
|
title + '</h4><p class="text-muted">' + text + '</p></div>');
|
2014-10-21 10:46:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
bind_events: function() {
|
2015-09-16 13:22:52 +00:00
|
|
|
shopping_cart.bind_address_select();
|
2015-09-17 10:58:30 +00:00
|
|
|
shopping_cart.bind_place_order();
|
|
|
|
shopping_cart.bind_change_qty();
|
2016-06-22 10:21:42 +00:00
|
|
|
shopping_cart.bind_dropdown_cart_buttons();
|
2015-09-16 13:22:52 +00:00
|
|
|
},
|
2016-06-22 10:21:42 +00:00
|
|
|
|
2015-09-16 13:22:52 +00:00
|
|
|
bind_address_select: function() {
|
|
|
|
$(".cart-addresses").find('input[data-address-name]').on("click", function() {
|
|
|
|
if($(this).prop("checked")) {
|
|
|
|
var me = this;
|
2014-10-21 10:46:30 +00:00
|
|
|
|
2015-09-16 13:22:52 +00:00
|
|
|
return frappe.call({
|
|
|
|
type: "POST",
|
|
|
|
method: "erpnext.shopping_cart.cart.update_cart_address",
|
|
|
|
args: {
|
|
|
|
address_fieldname: $(this).attr("data-fieldname"),
|
|
|
|
address_name: $(this).attr("data-address-name")
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
2015-09-17 12:59:44 +00:00
|
|
|
$(".cart-tax-items").html(r.message.taxes);
|
2015-09-16 13:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-21 10:46:30 +00:00
|
|
|
});
|
2015-09-16 13:22:52 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
},
|
|
|
|
|
2015-09-17 10:58:30 +00:00
|
|
|
bind_place_order: function() {
|
|
|
|
$(".btn-place-order").on("click", function() {
|
|
|
|
shopping_cart.place_order(this);
|
2014-10-21 10:46:30 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-17 10:58:30 +00:00
|
|
|
bind_change_qty: function() {
|
|
|
|
// bind update button
|
|
|
|
$(".cart-items").on("change", ".cart-qty", function() {
|
|
|
|
var item_code = $(this).attr("data-item-code");
|
2016-06-22 10:21:42 +00:00
|
|
|
var newVal = $(this).val();
|
|
|
|
shopping_cart.shopping_cart_update(item_code, newVal);
|
2016-05-02 06:13:44 +00:00
|
|
|
});
|
2016-06-22 10:16:38 +00:00
|
|
|
|
|
|
|
$(".cart-items").on('click', '.number-spinner button', function () {
|
|
|
|
var btn = $(this),
|
2016-06-22 10:21:42 +00:00
|
|
|
input = btn.closest('.number-spinner').find('input'),
|
|
|
|
oldValue = input.val().trim(),
|
2016-06-22 10:16:38 +00:00
|
|
|
newVal = 0;
|
|
|
|
|
|
|
|
if (btn.attr('data-dir') == 'up') {
|
|
|
|
newVal = parseInt(oldValue) + 1;
|
|
|
|
} else {
|
|
|
|
if (oldValue > 1) {
|
|
|
|
newVal = parseInt(oldValue) - 1;
|
|
|
|
}
|
|
|
|
}
|
2016-06-22 10:21:42 +00:00
|
|
|
input.val(newVal);
|
|
|
|
var item_code = input.attr("data-item-code");
|
|
|
|
shopping_cart.shopping_cart_update(item_code, newVal);
|
2016-06-22 10:16:38 +00:00
|
|
|
});
|
2016-05-02 06:13:44 +00:00
|
|
|
},
|
2016-05-09 07:00:58 +00:00
|
|
|
|
2014-10-21 10:46:30 +00:00
|
|
|
render_tax_row: function($cart_taxes, doc, shipping_rules) {
|
|
|
|
var shipping_selector;
|
|
|
|
if(shipping_rules) {
|
|
|
|
shipping_selector = '<select class="form-control">' + $.map(shipping_rules, function(rule) {
|
|
|
|
return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
|
|
|
|
'</select>';
|
|
|
|
}
|
|
|
|
|
|
|
|
var $tax_row = $(repl('<div class="row">\
|
|
|
|
<div class="col-md-9 col-sm-9">\
|
|
|
|
<div class="row">\
|
|
|
|
<div class="col-md-9 col-md-offset-3">' +
|
|
|
|
(shipping_selector || '<p>%(description)s</p>') +
|
|
|
|
'</div>\
|
|
|
|
</div>\
|
|
|
|
</div>\
|
|
|
|
<div class="col-md-3 col-sm-3 text-right">\
|
|
|
|
<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() {
|
|
|
|
shopping_cart.apply_shipping_rule($(this).val(), this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
apply_shipping_rule: function(rule, btn) {
|
|
|
|
return frappe.call({
|
|
|
|
btn: btn,
|
|
|
|
type: "POST",
|
|
|
|
method: "erpnext.shopping_cart.cart.apply_shipping_rule",
|
|
|
|
args: { shipping_rule: rule },
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
shopping_cart.render(r.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
place_order: function(btn) {
|
|
|
|
return frappe.call({
|
|
|
|
type: "POST",
|
|
|
|
method: "erpnext.shopping_cart.cart.place_order",
|
|
|
|
btn: btn,
|
|
|
|
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 || frappe._("Something went wrong!"))
|
|
|
|
.toggle(true);
|
|
|
|
} else {
|
2015-02-24 12:20:44 +00:00
|
|
|
window.location.href = "/orders/" + encodeURIComponent(r.message);
|
2014-10-21 10:46:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-22 10:21:42 +00:00
|
|
|
frappe.ready(function() {
|
2016-01-06 11:11:50 +00:00
|
|
|
$(".cart-icon").hide();
|
2014-10-21 10:46:30 +00:00
|
|
|
shopping_cart.bind_events();
|
|
|
|
});
|
2016-03-16 12:31:22 +00:00
|
|
|
|
|
|
|
function show_terms() {
|
|
|
|
var html = $(".cart-terms").html();
|
|
|
|
frappe.msgprint(html);
|
|
|
|
}
|