brotherton-erpnext/accounts/page/accounts_browser/accounts_browser.py

57 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2012-04-13 13:34:55 +00:00
import webnotes
from webnotes.utils import get_defaults, fmt_money
from accounts.utils import get_balance_on
2012-04-13 13:34:55 +00:00
@webnotes.whitelist()
def get_companies():
"""get a list of companies based on permission"""
# check if match permission exists
res = webnotes.conn.sql("""select role, `match` from `tabDocPerm`
where parent='Account' and permlevel=0 and `read`=1""", as_dict=1)
match = any((r["match"] for r in res
if r["role"] in webnotes.user.roles and r["match"]=="company"))
# if match == company is specified and companies are specified in user defaults
if match and webnotes.user.get_defaults().get("company"):
return webnotes.user.get_defaults().get("company")
else:
return [r[0] for r in webnotes.conn.sql("""select name from tabCompany
where docstatus!=2""")]
2012-12-05 08:57:50 +00:00
2012-04-13 13:34:55 +00:00
@webnotes.whitelist()
def get_children():
args = webnotes.form_dict
2012-05-22 11:24:46 +00:00
ctype, company = args['ctype'], args['comp']
2012-04-13 13:34:55 +00:00
2012-05-22 11:24:46 +00:00
company_field = ctype=='Account' and 'company' or 'company_name'
2012-04-13 13:34:55 +00:00
# root
2012-05-22 11:24:46 +00:00
if args['parent'] == company:
acc = webnotes.conn.sql(""" select
2012-04-13 13:34:55 +00:00
name as value, if(group_or_ledger='Group', 1, 0) as expandable
from `tab%s`
where ifnull(parent_%s,'') = ''
2012-05-22 11:24:46 +00:00
and %s = %s and docstatus<2
2012-04-13 13:34:55 +00:00
order by name""" % (ctype, ctype.lower().replace(' ','_'), company_field, '%s'),
args['parent'], as_dict=1)
2012-05-22 11:24:46 +00:00
else:
# other
acc = webnotes.conn.sql("""select
name as value, if(group_or_ledger='Group', 1, 0) as expandable
from `tab%s`
where ifnull(parent_%s,'') = %s
and docstatus<2
order by name""" % (ctype, ctype.lower().replace(' ','_'), '%s'),
args['parent'], as_dict=1)
if ctype == 'Account':
currency = webnotes.conn.sql("select default_currency from `tabCompany` where name = %s", company)[0][0]
for each in acc:
bal = get_balance_on(each.get("value"))
each['balance'] = currency + ' ' + fmt_money(bal)
2012-05-22 11:24:46 +00:00
return acc