brotherton-erpnext/erpnext/public/js/payment/payments.js

236 lines
7.1 KiB
JavaScript
Raw Normal View History

2016-04-09 09:01:09 +00:00
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
erpnext.payments = class payments extends erpnext.stock.StockController {
make_payment() {
2016-04-09 09:01:09 +00:00
var me = this;
this.dialog = new frappe.ui.Dialog({
title: 'Payment'
});
2016-04-09 09:01:09 +00:00
this.dialog.show();
this.$body = this.dialog.body;
this.set_payment_primary_action();
this.make_keyboard();
2021-05-16 12:16:07 +00:00
this.select_text();
}
2021-05-20 11:38:57 +00:00
select_text() {
2021-05-13 08:34:51 +00:00
$(this.$body).find('.form-control').click(function() {
$(this).select();
2021-05-20 11:38:57 +00:00
});
}
2016-04-09 09:01:09 +00:00
set_payment_primary_action() {
2016-04-09 09:01:09 +00:00
var me = this;
2016-04-09 09:01:09 +00:00
this.dialog.set_primary_action(__("Submit"), function() {
// Allow no ZERO payment
$.each(me.frm.doc.payments, function (index, data) {
if (data.amount != 0) {
me.dialog.hide();
me.submit_invoice();
return;
}
});
2017-11-22 15:12:27 +00:00
})
}
2016-04-09 09:01:09 +00:00
make_keyboard(){
2016-04-09 09:01:09 +00:00
var me = this;
$(this.$body).empty();
$(this.$body).html(frappe.render_template('pos_payment', this.frm.doc))
this.show_payment_details();
this.bind_keyboard_event()
this.clear_amount()
}
2016-04-09 09:01:09 +00:00
make_multimode_payment(){
2016-04-09 09:01:09 +00:00
var me = this;
2021-05-16 12:16:07 +00:00
if (this.frm.doc.change_amount > 0) {
me.payment_val = me.doc.outstanding_amount;
2016-04-09 09:01:09 +00:00
}
this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments");
this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value();
this.payments.amount = flt(this.payment_val);
}
2016-04-09 09:01:09 +00:00
show_payment_details(){
2016-04-09 09:01:09 +00:00
var me = this;
2017-05-30 07:24:42 +00:00
var multimode_payments = $(this.$body).find('.multimode-payments').empty();
2021-05-20 11:38:57 +00:00
if (this.frm.doc.payments.length) {
$.each(this.frm.doc.payments, function(index, data) {
2016-04-09 09:01:09 +00:00
$(frappe.render_template('payment_details', {
mode_of_payment: data.mode_of_payment,
amount: data.amount,
idx: data.idx,
currency: me.frm.doc.currency,
type: data.type
})).appendTo(multimode_payments)
2016-07-30 11:19:48 +00:00
if (data.type == 'Cash' && data.amount == me.frm.doc.paid_amount) {
me.idx = data.idx;
me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
me.highlight_selected_row();
me.bind_amount_change_event();
}
2016-04-09 09:01:09 +00:00
})
}else{
$("<p>No payment mode selected in pos profile</p>").appendTo(multimode_payments)
}
}
set_outstanding_amount(){
this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']",{'idx': this.idx}));
2021-05-21 05:46:26 +00:00
this.highlight_selected_row();
this.payment_val = 0.0;
2021-05-20 11:38:57 +00:00
if (this.frm.doc.outstanding_amount > 0 && flt(this.selected_mode.val()) == 0.0) {
//When user first time click on row
this.payment_val = flt(this.frm.doc.outstanding_amount / this.frm.doc.conversion_rate, precision("outstanding_amount"))
2016-08-30 20:34:37 +00:00
this.selected_mode.val(format_currency(this.payment_val, this.frm.doc.currency));
2021-05-20 11:38:57 +00:00
this.update_payment_amount();
} else if (flt(this.selected_mode.val()) > 0) {
//If user click on existing row which has value
this.payment_val = flt(this.selected_mode.val());
}
this.selected_mode.select()
this.bind_amount_change_event();
}
bind_keyboard_event(){
2016-04-09 09:01:09 +00:00
var me = this;
this.payment_val = '';
this.bind_form_control_event();
this.bind_numeric_keys_event();
}
bind_form_control_event() {
2016-04-09 09:01:09 +00:00
var me = this;
2021-05-13 08:34:51 +00:00
$(this.$body).find('.pos-payment-row').click(function() {
me.idx = $(this).attr("idx");
2021-05-16 12:16:07 +00:00
me.set_outstanding_amount();
2021-05-13 08:34:51 +00:00
});
2021-05-13 08:34:51 +00:00
$(this.$body).find('.form-control').click(function() {
me.idx = $(this).attr("idx");
me.set_outstanding_amount();
2016-08-22 14:19:17 +00:00
me.update_paid_amount(true);
2021-05-13 08:34:51 +00:00
});
2021-05-13 08:34:51 +00:00
$(this.$body).find('.write_off_amount').change(function() {
2016-08-22 14:19:17 +00:00
me.write_off_amount(flt($(this).val()), precision("write_off_amount"));
2021-05-13 08:34:51 +00:00
});
2021-05-13 08:34:51 +00:00
$(this.$body).find('.change_amount').change(function() {
2016-08-22 14:19:17 +00:00
me.change_amount(flt($(this).val()), precision("change_amount"));
2021-05-13 08:34:51 +00:00
});
}
2021-05-20 11:38:57 +00:00
highlight_selected_row() {
2021-05-13 08:34:51 +00:00
var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']", {'idx': this.idx}));
2021-05-16 12:16:07 +00:00
$(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode');
selected_row.addClass('selected-payment-mode');
2016-04-09 09:01:09 +00:00
$(this.$body).find('.amount').attr('disabled', true);
this.selected_mode.attr('disabled', false);
}
bind_numeric_keys_event() {
2016-04-09 09:01:09 +00:00
var me = this;
$(this.$body).find('.pos-keyboard-key').click(function(){
me.payment_val += $(this).text();
2021-05-16 12:16:07 +00:00
me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
me.idx = me.selected_mode.attr("idx");
me.update_paid_amount();
});
2021-05-13 08:34:51 +00:00
$(this.$body).find('.delete-btn').click(function() {
2016-04-09 09:01:09 +00:00
me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1);
2016-08-30 20:34:37 +00:00
me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
2021-05-16 12:16:07 +00:00
me.idx = me.selected_mode.attr("idx");
2016-04-09 09:01:09 +00:00
me.update_paid_amount();
})
}
2021-05-20 11:38:57 +00:00
bind_amount_change_event() {
2016-04-09 09:01:09 +00:00
var me = this;
2021-05-13 08:34:51 +00:00
this.selected_mode.change(function() {
2016-07-06 10:39:26 +00:00
me.payment_val = flt($(this).val()) || 0.0;
2021-05-16 12:16:07 +00:00
me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency));
me.idx = me.selected_mode.attr("idx");
me.update_payment_amount();
});
}
clear_amount() {
var me = this;
2021-05-13 08:34:51 +00:00
$(this.$body).find('.clr').click(function(e) {
e.stopPropagation();
me.idx = $(this).attr("idx");
me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']",{'idx': me.idx}));
me.payment_val = 0.0;
me.selected_mode.val(0.0);
2016-07-30 11:19:48 +00:00
me.highlight_selected_row();
me.update_payment_amount();
2021-05-16 12:16:07 +00:00
});
}
write_off_amount(write_off_amount) {
this.frm.doc.write_off_amount = flt(write_off_amount, precision("write_off_amount"));
this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate,
precision("base_write_off_amount"));
2021-05-16 12:16:07 +00:00
this.calculate_outstanding_amount(false);
this.show_amounts();
}
change_amount(change_amount) {
var me = this;
this.frm.doc.change_amount = flt(change_amount, precision("change_amount"));
2021-05-16 12:16:07 +00:00
this.calculate_write_off_amount();
this.show_amounts();
}
update_paid_amount(update_write_off) {
var me = this;
2021-05-16 12:16:07 +00:00
if (in_list(['change_amount', 'write_off_amount'], this.idx)) {
2017-05-30 07:24:42 +00:00
var value = me.selected_mode.val();
2021-05-16 12:16:07 +00:00
if (me.idx == 'change_amount') {
me.change_amount(value);
2021-05-20 11:38:57 +00:00
} else {
2016-08-30 20:39:15 +00:00
if(flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) {
value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx));
}
2021-05-16 12:16:07 +00:00
me.write_off_amount(value);
}
2021-05-16 12:16:07 +00:00
} else {
this.update_payment_amount();
}
}
update_payment_amount(){
var me = this;
2021-05-13 08:34:51 +00:00
$.each(this.frm.doc.payments, function(index, data) {
2021-05-20 11:38:57 +00:00
if (cint(me.idx) == cint(data.idx)) {
2021-05-16 12:16:07 +00:00
data.amount = flt(me.selected_mode.val(), 2);
2016-04-09 09:01:09 +00:00
}
})
this.calculate_outstanding_amount(false);
2016-04-09 09:01:09 +00:00
this.show_amounts();
}
show_amounts(){
2016-04-09 09:01:09 +00:00
var me = this;
2016-08-30 20:34:37 +00:00
$(this.$body).find(".write_off_amount").val(format_currency(this.frm.doc.write_off_amount, this.frm.doc.currency));
2016-04-09 09:01:09 +00:00
$(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency));
2021-05-16 12:16:07 +00:00
$(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency));
$(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, frappe.get_doc(":Company", this.frm.doc.company).default_currency));
2016-04-09 09:01:09 +00:00
this.update_invoice();
}
}