From 8b509f56b7b900c1508c27d13aa5095b4c6185ea Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 28 May 2013 16:52:30 +0530 Subject: [PATCH] [utility function] fix total debit/credit due to floating point issue --- accounts/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/accounts/utils.py b/accounts/utils.py index 755546c0e1..382a33762b 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -322,3 +322,20 @@ def get_stock_rbnb_value(company): and exists(select name from `tabPurchase Invoice` where name = pi_item.parent and company = %s)""", company) return flt(total_received_amount[0][0]) - flt(total_billed_amount[0][0]) + + +def fix_total_debit_credit(): + vouchers = webnotes.conn.sql("""select voucher_type, voucher_no, + sum(debit) - sum(credit) as diff + from `tabGL Entry` + group by voucher_type, voucher_no + having sum(ifnull(debit, 0)) != sum(ifnull(credit, 0))""", as_dict=1) + + for d in vouchers: + if abs(d.diff) > 0: + dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit" + + webnotes.conn.sql("""update `tabGL Entry` set %s = %s + %s + where voucher_type = %s and voucher_no = %s and %s > 0 limit 1""" % + (dr_or_cr, dr_or_cr, '%s', '%s', '%s', dr_or_cr), + (d.diff, d.voucher_type, d.voucher_no), debug=1) \ No newline at end of file