From 091b48168e228bbea378eb3b2ed5348e2601ac33 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 16 May 2014 17:36:42 +0530 Subject: [PATCH] Journal voucher debit != credit #fraction issue --- .../journal_voucher/journal_voucher.js | 6 +- .../journal_voucher/journal_voucher.py | 66 ++++++++++--------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.js b/erpnext/accounts/doctype/journal_voucher/journal_voucher.js index 25a65572ac..bc0108eba0 100644 --- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.js +++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.js @@ -140,13 +140,13 @@ cur_frm.cscript.update_totals = function(doc) { var td=0.0; var tc =0.0; var el = doc.entries || []; for(var i in el) { - td += flt(el[i].debit, 2); - tc += flt(el[i].credit, 2); + td += flt(el[i].debit, precision("debit", el[i])); + tc += flt(el[i].credit, precision("credit", el[i])); } var doc = locals[doc.doctype][doc.name]; doc.total_debit = td; doc.total_credit = tc; - doc.difference = flt((td - tc), 2); + doc.difference = flt((td - tc), precision("difference")); refresh_many(['total_debit','total_credit','difference']); } diff --git a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py index 62758c6172..70bee90016 100644 --- a/erpnext/accounts/doctype/journal_voucher/journal_voucher.py +++ b/erpnext/accounts/doctype/journal_voucher/journal_voucher.py @@ -120,18 +120,21 @@ class JournalVoucher(AccountsController): if flt(d.credit > 0): d.against_account = ", ".join(list(set(accounts_debited))) def validate_debit_and_credit(self): - self.total_debit, self.total_credit = 0, 0 + self.total_debit, self.total_credit, self.difference = 0, 0, 0 for d in self.get("entries"): if d.debit and d.credit: frappe.throw(_("You cannot credit and debit same account at the same time")) - self.total_debit = flt(self.total_debit) + flt(d.debit) - self.total_credit = flt(self.total_credit) + flt(d.credit) + self.total_debit = flt(self.total_debit) + flt(d.debit, self.precision("debit", "entries")) + self.total_credit = flt(self.total_credit) + flt(d.credit, self.precision("credit", "entries")) - if abs(self.total_debit-self.total_credit) > 0.001: + self.difference = flt(self.total_debit, self.precision("total_debit")) - \ + flt(self.total_credit, self.precision("total_credit")) + + if self.difference: frappe.throw(_("Total Debit must be equal to Total Credit. The difference is {0}") - .format(self.total_debit - self.total_credit)) + .format(self.difference)) def create_remarks(self): r = [] @@ -254,8 +257,8 @@ class JournalVoucher(AccountsController): self.get_gl_dict({ "account": d.account, "against": d.against_account, - "debit": d.debit, - "credit": d.credit, + "debit": flt(d.debit, self.precision("debit", "entries")), + "credit": flt(d.credit, self.precision("credit", "entries")), "against_voucher_type": ((d.against_voucher and "Purchase Invoice") or (d.against_invoice and "Sales Invoice") or (d.against_jv and "Journal Voucher")), @@ -279,7 +282,7 @@ class JournalVoucher(AccountsController): msgprint(_("'Entries' cannot be empty"), raise_exception=True) else: flag, self.total_debit, self.total_credit = 0, 0, 0 - diff = flt(self.difference, 2) + diff = flt(self.difference, self.precision("difference")) # If any row without amount, set the diff on that row for d in self.get('entries'): @@ -298,45 +301,44 @@ class JournalVoucher(AccountsController): elif diff<0: jd.debit = abs(diff) - # Set the total debit, total credit and difference - for d in self.get('entries'): - self.total_debit += flt(d.debit, 2) - self.total_credit += flt(d.credit, 2) - - self.difference = flt(self.total_debit, 2) - flt(self.total_credit, 2) + self.validate_debit_and_credit() def get_outstanding_invoices(self): self.set('entries', []) total = 0 for d in self.get_values(): - total += flt(d[2]) - jd = self.append('entries', {}) - jd.account = cstr(d[1]) + total += flt(d.outstanding_amount, self.precision("credit", "entries")) + jd1 = self.append('entries', {}) + jd1.account = d.account + if self.write_off_based_on == 'Accounts Receivable': - jd.credit = flt(d[2]) - jd.against_invoice = cstr(d[0]) + jd1.credit = flt(d.outstanding_amount, self.precision("credit", "entries")) + jd1.against_invoice = cstr(d.name) elif self.write_off_based_on == 'Accounts Payable': - jd.debit = flt(d[2]) - jd.against_voucher = cstr(d[0]) - jd.save(1) - jd = self.append('entries', {}) + jd1.debit = flt(d.outstanding_amount, self.precision("debit", "entries")) + jd1.against_voucher = cstr(d.name) + + jd2 = self.append('entries', {}) if self.write_off_based_on == 'Accounts Receivable': - jd.debit = total + jd2.debit = total elif self.write_off_based_on == 'Accounts Payable': - jd.credit = total - jd.save(1) + jd2.credit = total + + self.validate_debit_and_credit() + def get_values(self): - cond = (flt(self.write_off_amount) > 0) and \ - ' and outstanding_amount <= '+ self.write_off_amount or '' + cond = " and outstanding_amount <= {0}".format(self.write_off_amount) \ + if flt(self.write_off_amount) > 0 else "" + if self.write_off_based_on == 'Accounts Receivable': - return frappe.db.sql("""select name, debit_to, outstanding_amount + return frappe.db.sql("""select name, debit_to as account, outstanding_amount from `tabSales Invoice` where docstatus = 1 and company = %s - and outstanding_amount > 0 %s""" % ('%s', cond), self.company) + and outstanding_amount > 0 %s""" % ('%s', cond), self.company, as_dict=True) elif self.write_off_based_on == 'Accounts Payable': - return frappe.db.sql("""select name, credit_to, outstanding_amount + return frappe.db.sql("""select name, credit_to as account, outstanding_amount from `tabPurchase Invoice` where docstatus = 1 and company = %s - and outstanding_amount > 0 %s""" % ('%s', cond), self.company) + and outstanding_amount > 0 %s""" % ('%s', cond), self.company, as_dict=True) @frappe.whitelist() def get_default_bank_cash_account(company, voucher_type):