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