From ac3b2aa91361871ebb676c11796b0cc44764cc43 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 30 May 2017 15:35:01 +0530 Subject: [PATCH] Set change amount automatically only if it is a cash transaction (#9065) --- .../doctype/sales_invoice/sales_invoice.js | 1 + erpnext/controllers/taxes_and_totals.py | 18 ++++++++++-------- .../public/js/controllers/taxes_and_totals.js | 17 ++++++++++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 007afe4b63b..1f8aa1d03c3 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -299,6 +299,7 @@ erpnext.accounts.SalesInvoiceController = erpnext.selling.SellingController.exte this.calculate_write_off_amount() }else { this.frm.set_value("change_amount", 0.0) + this.frm.set_value("base_change_amount", 0.0) } this.frm.refresh_fields(); diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py index 822d50bfdf8..50f6b61c32b 100644 --- a/erpnext/controllers/taxes_and_totals.py +++ b/erpnext/controllers/taxes_and_totals.py @@ -440,16 +440,16 @@ class calculate_taxes_and_totals(object): self.doc.conversion_rate, self.doc.precision("grand_total")) - self.doc.total_advance - flt(self.doc.base_write_off_amount), self.doc.precision("grand_total")) - if self.doc.doctype == "Sales Invoice": + if self.doc.doctype == "Sales Invoice": self.doc.round_floats_in(self.doc, ["paid_amount"]) - paid_amount = self.doc.paid_amount \ - if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount - - change_amount = self.doc.change_amount \ - if self.doc.party_account_currency == self.doc.currency else self.doc.base_change_amount - self.calculate_write_off_amount() self.calculate_change_amount() + + paid_amount = self.doc.paid_amount \ + if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount + + change_amount = self.doc.change_amount \ + if self.doc.party_account_currency == self.doc.currency else self.doc.base_change_amount self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount) + flt(change_amount), self.doc.precision("outstanding_amount")) @@ -475,7 +475,9 @@ class calculate_taxes_and_totals(object): def calculate_change_amount(self): self.doc.change_amount = 0.0 self.doc.base_change_amount = 0.0 - if self.doc.paid_amount > self.doc.grand_total: + if self.doc.paid_amount > self.doc.grand_total and not self.doc.is_return \ + and any([d.type == "Cash" for d in self.doc.payments]): + self.doc.change_amount = flt(self.doc.paid_amount - self.doc.grand_total + self.doc.write_off_amount, self.doc.precision("change_amount")) diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js index 849275f02cd..dc1db3554b4 100644 --- a/erpnext/public/js/controllers/taxes_and_totals.js +++ b/erpnext/public/js/controllers/taxes_and_totals.js @@ -604,11 +604,18 @@ erpnext.taxes_and_totals = erpnext.payments.extend({ calculate_change_amount: function(){ this.frm.doc.change_amount = 0.0; - if(this.frm.doc.paid_amount > this.frm.doc.grand_total && !this.frm.doc.is_return){ - this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - this.frm.doc.grand_total + - this.frm.doc.write_off_amount, precision("change_amount")); - this.frm.doc.base_change_amount = flt(this.frm.doc.base_paid_amount - this.frm.doc.base_grand_total + - this.frm.doc.base_write_off_amount, precision("base_change_amount")); + this.frm.doc.base_change_amount = 0.0; + if(this.frm.doc.paid_amount > this.frm.doc.grand_total && !this.frm.doc.is_return) { + var payment_types = $.map(cur_frm.doc.payments, function(d) { return d.type }); + if (in_list(payment_types, 'Cash')) { + this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - this.frm.doc.grand_total + + this.frm.doc.write_off_amount, precision("change_amount")); + + this.frm.doc.base_change_amount = flt(this.frm.doc.base_paid_amount - + this.frm.doc.base_grand_total + this.frm.doc.base_write_off_amount, + precision("base_change_amount")); + + } } },