2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2012-07-19 08:10:31 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
import frappe.defaults
|
|
|
|
from frappe.utils import flt
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.accounts.utils import get_balance_on
|
2015-04-29 07:37:36 +00:00
|
|
|
from erpnext.accounts.report.financial_statements import sort_root_accounts
|
2012-04-13 13:34:55 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2012-04-13 13:34:55 +00:00
|
|
|
def get_companies():
|
2012-09-10 08:43:01 +00:00
|
|
|
"""get a list of companies based on permission"""
|
2014-09-30 12:11:33 +00:00
|
|
|
return [d.name for d in frappe.get_list("Company", fields=["name"],
|
2013-08-05 10:51:04 +00:00
|
|
|
order_by="name")]
|
2012-12-05 08:57:50 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2012-04-13 13:34:55 +00:00
|
|
|
def get_children():
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe.local.form_dict
|
2012-05-22 11:24:46 +00:00
|
|
|
ctype, company = args['ctype'], args['comp']
|
2015-12-15 06:36:37 +00:00
|
|
|
fieldname = frappe.db.escape(ctype.lower().replace(' ','_'))
|
|
|
|
doctype = frappe.db.escape(ctype)
|
2014-09-30 12:11:33 +00:00
|
|
|
|
2012-04-13 13:34:55 +00:00
|
|
|
# root
|
2013-06-24 10:13:18 +00:00
|
|
|
if args['parent'] in ("Accounts", "Cost Centers"):
|
2015-12-15 06:36:37 +00:00
|
|
|
fields = ", root_type, report_type, account_currency" if ctype=="Account" else ""
|
2014-09-30 12:11:33 +00:00
|
|
|
acc = frappe.db.sql(""" select
|
2015-12-15 06:36:37 +00:00
|
|
|
name as value, is_group as expandable {fields}
|
|
|
|
from `tab{doctype}`
|
|
|
|
where ifnull(`parent_{fieldname}`,'') = ''
|
2014-09-30 12:11:33 +00:00
|
|
|
and `company` = %s and docstatus<2
|
2015-12-15 06:36:37 +00:00
|
|
|
order by name""".format(fields=fields, fieldname = fieldname, doctype=doctype),
|
2013-06-24 10:13:18 +00:00
|
|
|
company, as_dict=1)
|
2015-04-29 06:55:18 +00:00
|
|
|
|
2015-04-29 07:37:36 +00:00
|
|
|
if args["parent"]=="Accounts":
|
|
|
|
sort_root_accounts(acc)
|
2014-09-30 12:11:33 +00:00
|
|
|
else:
|
2012-05-22 11:24:46 +00:00
|
|
|
# other
|
2015-12-15 06:36:37 +00:00
|
|
|
fields = ", account_currency" if ctype=="Account" else ""
|
2014-09-30 12:11:33 +00:00
|
|
|
acc = frappe.db.sql("""select
|
2015-12-15 06:36:37 +00:00
|
|
|
name as value, is_group as expandable, parent_{fieldname} as parent {fields}
|
|
|
|
from `tab{doctype}`
|
|
|
|
where ifnull(`parent_{fieldname}`,'') = %s
|
2014-09-30 12:11:33 +00:00
|
|
|
and docstatus<2
|
2015-12-15 06:36:37 +00:00
|
|
|
order by name""".format(fields=fields, fieldname=fieldname, doctype=doctype),
|
2012-05-22 11:24:46 +00:00
|
|
|
args['parent'], as_dict=1)
|
2014-09-30 12:11:33 +00:00
|
|
|
|
2012-05-22 11:24:46 +00:00
|
|
|
if ctype == 'Account':
|
2015-12-02 05:43:20 +00:00
|
|
|
company_currency = frappe.db.get_value("Company", company, "default_currency")
|
2012-05-22 11:24:46 +00:00
|
|
|
for each in acc:
|
2015-12-02 05:43:20 +00:00
|
|
|
each["company_currency"] = company_currency
|
|
|
|
each["balance"] = flt(get_balance_on(each.get("value"), in_account_currency=False))
|
2015-12-15 06:36:37 +00:00
|
|
|
|
2015-12-02 05:43:20 +00:00
|
|
|
if each.account_currency != company_currency:
|
|
|
|
each["balance_in_account_currency"] = flt(get_balance_on(each.get("value")))
|
2014-09-30 12:11:33 +00:00
|
|
|
|
2012-05-22 11:24:46 +00:00
|
|
|
return acc
|