brotherton-erpnext/erpnext/templates/pages/cart.js

304 lines
8.2 KiB
JavaScript
Raw Normal View History

// 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 exclusive to /cart page
frappe.provide("erpnext.e_commerce.shopping_cart");
var shopping_cart = erpnext.e_commerce.shopping_cart;
2014-10-21 10:46:30 +00:00
$.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() {
2021-01-20 12:22:54 +00:00
shopping_cart.bind_address_picker_dialog();
shopping_cart.bind_place_order();
shopping_cart.bind_request_quotation();
shopping_cart.bind_change_qty();
shopping_cart.bind_remove_cart_item();
shopping_cart.bind_change_notes();
shopping_cart.bind_coupon_code();
2015-09-16 13:22:52 +00:00
},
2017-05-30 07:24:42 +00:00
2021-01-20 12:22:54 +00:00
bind_address_picker_dialog: function() {
const d = this.get_update_address_dialog();
this.parent.find('.btn-change-address').on('click', (e) => {
const type = $(e.currentTarget).parents('.address-container').attr('data-address-type');
$(d.get_field('address_picker').wrapper).html(
this.get_address_template(type)
);
d.show();
});
},
get_update_address_dialog() {
2021-02-01 14:20:27 +00:00
let d = new frappe.ui.Dialog({
2021-01-20 12:22:54 +00:00
title: "Select Address",
fields: [{
'fieldtype': 'HTML',
'fieldname': 'address_picker',
}],
primary_action_label: __('Set Address'),
primary_action: () => {
const $card = d.$wrapper.find('.address-card.active');
const address_type = $card.closest('[data-address-type]').attr('data-address-type');
const address_name = $card.closest('[data-address-name]').attr('data-address-name');
frappe.call({
type: "POST",
method: "erpnext.e_commerce.shopping_cart.cart.update_cart_address",
2021-01-20 12:22:54 +00:00
freeze: true,
args: {
address_type,
address_name
},
callback: function(r) {
d.hide();
2021-02-01 14:20:27 +00:00
if (!r.exc) {
$(".cart-tax-items").html(r.message.total);
2021-01-20 12:22:54 +00:00
shopping_cart.parent.find(
`.address-container[data-address-type="${address_type}"]`
).html(r.message.address);
}
2015-09-16 13:22:52 +00:00
}
2021-01-20 12:22:54 +00:00
});
}
2014-10-21 10:46:30 +00:00
});
2021-02-01 14:20:27 +00:00
return d;
2014-10-21 10:46:30 +00:00
},
2021-01-20 12:22:54 +00:00
get_address_template(type) {
return {
shipping: `<div class="mb-3" data-section="shipping-address">
<div class="row no-gutters" data-fieldname="shipping_address_name">
{% for address in shipping_addresses %}
<div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="shipping"
{% if doc.shipping_address_name == address.name %} data-active {% endif %}>
{% include "templates/includes/cart/address_picker_card.html" %}
</div>
{% endfor %}
</div>
</div>`,
billing: `<div class="mb-3" data-section="billing-address">
<div class="row no-gutters" data-fieldname="customer_address">
{% for address in billing_addresses %}
<div class="mr-3 mb-3 w-100" data-address-name="{{address.name}}" data-address-type="billing"
{% if doc.shipping_address_name == address.name %} data-active {% endif %}>
{% include "templates/includes/cart/address_picker_card.html" %}
</div>
{% endfor %}
</div>
</div>`,
}[type];
},
bind_place_order: function() {
$(".btn-place-order").on("click", function() {
shopping_cart.place_order(this);
2014-10-21 10:46:30 +00:00
});
},
bind_request_quotation: function() {
$('.btn-request-for-quotation').on('click', function() {
shopping_cart.request_quotation(this);
});
},
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, qty: newVal});
2016-05-02 06:13:44 +00:00
});
2017-05-30 07:24:42 +00:00
$(".cart-items").on('click', '.number-spinner button', function () {
2016-06-22 10:16:38 +00:00
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;
2017-05-30 07:24:42 +00:00
2016-06-22 10:16:38 +00:00
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);
let notes = input.closest("td").siblings().find(".notes").text().trim();
2017-05-30 07:24:42 +00:00
var item_code = input.attr("data-item-code");
shopping_cart.shopping_cart_update({
item_code,
qty: newVal,
additional_notes: notes
});
});
},
bind_change_notes: function() {
$('.cart-items').on('change', 'textarea', function() {
const $textarea = $(this);
const item_code = $textarea.attr('data-item-code');
const qty = $textarea.closest('tr').find('.cart-qty').val();
const notes = $textarea.val();
shopping_cart.shopping_cart_update({
item_code,
qty,
additional_notes: notes
});
2016-06-22 10:16:38 +00:00
});
2016-05-02 06:13:44 +00:00
},
2017-05-30 07:24:42 +00:00
bind_remove_cart_item: function() {
$(".cart-items").on("click", ".remove-cart-item", (e) => {
const $remove_cart_item_btn = $(e.currentTarget);
var item_code = $remove_cart_item_btn.data("item-code");
shopping_cart.shopping_cart_update({
item_code: item_code,
qty: 0
});
});
},
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) {
2017-05-30 07:24:42 +00:00
return '<option value="' + rule[0] + '">' + rule[1] + '</option>' }).join("\n") +
'</select>';
2014-10-21 10:46:30 +00:00
}
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.e_commerce.shopping_cart.cart.apply_shipping_rule",
2014-10-21 10:46:30 +00:00
args: { shipping_rule: rule },
callback: function(r) {
if(!r.exc) {
shopping_cart.render(r.message);
}
}
});
},
place_order: function(btn) {
shopping_cart.freeze();
2014-10-21 10:46:30 +00:00
return frappe.call({
type: "POST",
method: "erpnext.e_commerce.shopping_cart.cart.place_order",
2014-10-21 10:46:30 +00:00
btn: btn,
callback: function(r) {
if(r.exc) {
shopping_cart.unfreeze();
2014-10-21 10:46:30 +00:00
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 {
$(btn).hide();
window.location.href = '/orders/' + encodeURIComponent(r.message);
}
}
});
},
request_quotation: function(btn) {
shopping_cart.freeze();
return frappe.call({
type: "POST",
method: "erpnext.e_commerce.shopping_cart.cart.request_for_quotation",
btn: btn,
callback: function(r) {
if(r.exc) {
shopping_cart.unfreeze();
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 {
$(btn).hide();
window.location.href = '/quotations/' + encodeURIComponent(r.message);
2014-10-21 10:46:30 +00:00
}
}
});
},
bind_coupon_code: function() {
$(".bt-coupon").on("click", function() {
shopping_cart.apply_coupon_code(this);
});
},
apply_coupon_code: function(btn) {
return frappe.call({
type: "POST",
method: "erpnext.e_commerce.shopping_cart.cart.apply_coupon_code",
btn: btn,
args : {
applied_code : $('.txtcoupon').val(),
applied_referral_sales_partner: $('.txtreferral_sales_partner').val()
},
callback: function(r) {
if (r && r.message){
location.reload();
}
}
});
2014-10-21 10:46:30 +00:00
}
});
2016-06-22 10:21:42 +00:00
frappe.ready(function() {
if (window.location.pathname === "/cart") {
$(".cart-icon").hide();
}
2021-01-20 12:22:54 +00:00
shopping_cart.parent = $(".cart-container");
2014-10-21 10:46:30 +00:00
shopping_cart.bind_events();
});
function show_terms() {
2017-05-30 07:24:42 +00:00
var html = $(".cart-terms").html();
frappe.msgprint(html);
}