blob: d3f6390d6db4ae6ac1bf8f20f9fc26a07064d34c [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
3
Nabin Hait362455e2013-02-13 15:50:39 +05304def execute():
5 import webnotes
6 from webnotes.utils import flt
Nabin Hait66ea0862013-02-19 15:13:23 +05307 records = webnotes.conn.sql("""
8 select against_voucher_type, against_voucher,
9 sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) as outstanding from `tabGL Entry`
10 where ifnull(is_cancelled, 'No') = 'No'
11 and against_voucher_type in ("Sales Invoice", "Purchase Invoice")
12 and ifnull(against_voucher, '') != ''
13 group by against_voucher_type, against_voucher""", as_dict=1)
14 for r in records:
15 outstanding = webnotes.conn.sql("""select name, outstanding_amount from `tab%s`
16 where name = %s and docstatus = 1""" %
17 (r["against_voucher_type"], '%s'), (r["against_voucher"]))
18
19 if outstanding and abs(flt(r["outstanding"])) != flt(outstanding[0][1]):
20 if ((r["against_voucher_type"]=='Sales Invoice' and flt(r["outstanding"]) >= 0) \
21 or (r["against_voucher_type"]=="Purchase Invoice" and flt(["outstanding"]) <= 0)):
22 webnotes.conn.set_value(r["against_voucher_type"], r["against_voucher"],
Nabin Hait97f68432013-04-09 17:43:36 +053023 "outstanding_amount", abs(flt(r["outstanding"])))