2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-12-26 05:37:58 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
from collections import OrderedDict
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-01-18 04:02:43 +00:00
|
|
|
import frappe
|
2017-11-15 10:59:53 +00:00
|
|
|
from frappe import _, _dict
|
2015-09-28 08:01:17 +00:00
|
|
|
from frappe.utils import cstr, flt, getdate
|
2018-05-23 06:01:24 +00:00
|
|
|
from six import iteritems
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-03-17 05:23:24 +00:00
|
|
|
from erpnext import get_company_currency, get_default_company
|
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
|
|
|
get_accounting_dimensions,
|
|
|
|
get_dimension_with_children,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2019-06-21 11:50:37 +00:00
|
|
|
from erpnext.accounts.report.financial_statements import get_cost_centers_with_children
|
|
|
|
from erpnext.accounts.report.utils import convert_to_presentation_currency, get_currency
|
2018-09-07 07:47:11 +00:00
|
|
|
from erpnext.accounts.utils import get_account_currency
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def execute(filters=None):
|
2018-05-16 18:01:09 +00:00
|
|
|
if not filters:
|
|
|
|
return [], []
|
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
account_details = {}
|
2018-01-18 04:02:43 +00:00
|
|
|
|
|
|
|
if filters and filters.get('print_in_account_currency') and \
|
|
|
|
not filters.get('account'):
|
|
|
|
frappe.throw(_("Select an account to print in account currency"))
|
|
|
|
|
2015-04-23 07:44:17 +00:00
|
|
|
for acc in frappe.db.sql("""select name, is_group from tabAccount""", as_dict=1):
|
2015-05-27 07:31:38 +00:00
|
|
|
account_details.setdefault(acc.name, acc)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-05-03 13:30:43 +00:00
|
|
|
if filters.get('party'):
|
2019-06-19 06:45:37 +00:00
|
|
|
filters.party = frappe.parse_json(filters.get("party"))
|
2018-05-03 13:30:43 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
validate_filters(filters, account_details)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-04-20 11:43:43 +00:00
|
|
|
validate_party(filters)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
|
|
|
filters = set_account_currency(filters)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
columns = get_columns(filters)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
res = get_result(filters, account_details)
|
2013-12-26 05:37:58 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
return columns, res
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def validate_filters(filters, account_details):
|
2020-08-17 07:14:52 +00:00
|
|
|
if not filters.get("company"):
|
|
|
|
frappe.throw(_("{0} is mandatory").format(_("Company")))
|
|
|
|
|
|
|
|
if not filters.get("from_date") and not filters.get("to_date"):
|
|
|
|
frappe.throw(_("{0} and {1} are mandatory").format(frappe.bold(_("From Date")), frappe.bold(_("To Date"))))
|
2021-08-19 08:11:10 +00:00
|
|
|
|
2021-06-14 07:52:44 +00:00
|
|
|
if filters.get('account'):
|
|
|
|
filters.account = frappe.parse_json(filters.get('account'))
|
2021-07-12 05:40:00 +00:00
|
|
|
for account in filters.account:
|
|
|
|
if not account_details.get(account):
|
|
|
|
frappe.throw(_("Account {0} does not exists").format(account))
|
2014-08-18 10:08:04 +00:00
|
|
|
|
2021-09-08 10:58:05 +00:00
|
|
|
if (filters.get("account") and filters.get("group_by") == 'Group by Account'):
|
2021-07-14 14:29:11 +00:00
|
|
|
filters.account = frappe.parse_json(filters.get('account'))
|
|
|
|
for account in filters.account:
|
|
|
|
if account_details[account].is_group == 0:
|
|
|
|
frappe.throw(_("Can not filter based on Child Account, if grouped by Account"))
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-11-12 11:28:24 +00:00
|
|
|
if (filters.get("voucher_no")
|
2021-09-08 10:58:05 +00:00
|
|
|
and filters.get("group_by") in ['Group by Voucher']):
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(_("Can not filter based on Voucher No, if grouped by Voucher"))
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
if filters.from_date > filters.to_date:
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(_("From Date must be before To Date"))
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-08-27 06:38:53 +00:00
|
|
|
if filters.get('project'):
|
2019-06-19 06:45:37 +00:00
|
|
|
filters.project = frappe.parse_json(filters.get('project'))
|
2018-08-27 06:38:53 +00:00
|
|
|
|
|
|
|
if filters.get('cost_center'):
|
2019-06-19 06:45:37 +00:00
|
|
|
filters.cost_center = frappe.parse_json(filters.get('cost_center'))
|
2018-08-27 06:38:53 +00:00
|
|
|
|
2015-04-20 11:43:43 +00:00
|
|
|
|
|
|
|
def validate_party(filters):
|
|
|
|
party_type, party = filters.get("party_type"), filters.get("party")
|
|
|
|
|
2021-08-25 06:27:24 +00:00
|
|
|
if party and party_type:
|
|
|
|
for d in party:
|
|
|
|
if not frappe.db.exists(party_type, d):
|
|
|
|
frappe.throw(_("Invalid {0}: {1}").format(party_type, d))
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
def set_account_currency(filters):
|
2018-05-03 13:30:43 +00:00
|
|
|
if filters.get("account") or (filters.get('party') and len(filters.party) == 1):
|
2018-08-08 11:07:31 +00:00
|
|
|
filters["company_currency"] = frappe.get_cached_value('Company', filters.company, "default_currency")
|
2015-08-31 10:09:03 +00:00
|
|
|
account_currency = None
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("account"):
|
2021-06-16 08:53:02 +00:00
|
|
|
if len(filters.get("account")) == 1:
|
|
|
|
account_currency = get_account_currency(filters.account[0])
|
|
|
|
else:
|
|
|
|
currency = get_account_currency(filters.account[0])
|
|
|
|
is_same_account_currency = True
|
|
|
|
for account in filters.get("account"):
|
|
|
|
if get_account_currency(account) != currency:
|
|
|
|
is_same_account_currency = False
|
|
|
|
break
|
|
|
|
|
|
|
|
if is_same_account_currency:
|
|
|
|
account_currency = currency
|
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
elif filters.get("party"):
|
2018-02-12 09:34:50 +00:00
|
|
|
gle_currency = frappe.db.get_value(
|
|
|
|
"GL Entry", {
|
2018-05-03 13:30:43 +00:00
|
|
|
"party_type": filters.party_type, "party": filters.party[0], "company": filters.company
|
2018-02-12 09:34:50 +00:00
|
|
|
},
|
|
|
|
"account_currency"
|
|
|
|
)
|
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if gle_currency:
|
|
|
|
account_currency = gle_currency
|
|
|
|
else:
|
2018-05-22 03:40:01 +00:00
|
|
|
account_currency = (None if filters.party_type in ["Employee", "Student", "Shareholder", "Member"] else
|
2018-08-14 10:58:14 +00:00
|
|
|
frappe.db.get_value(filters.party_type, filters.party[0], "default_currency"))
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
filters["account_currency"] = account_currency or filters.company_currency
|
2019-02-12 11:11:20 +00:00
|
|
|
if filters.account_currency != filters.company_currency and not filters.presentation_currency:
|
2018-08-14 10:58:14 +00:00
|
|
|
filters.presentation_currency = filters.account_currency
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2018-05-03 13:30:43 +00:00
|
|
|
return filters
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2014-05-20 09:37:18 +00:00
|
|
|
def get_result(filters, account_details):
|
2020-08-14 06:42:54 +00:00
|
|
|
accounting_dimensions = []
|
|
|
|
if filters.get("include_dimensions"):
|
|
|
|
accounting_dimensions = get_accounting_dimensions()
|
2013-12-27 12:03:55 +00:00
|
|
|
|
2020-08-14 06:42:54 +00:00
|
|
|
gl_entries = get_gl_entries(filters, accounting_dimensions)
|
|
|
|
|
|
|
|
data = get_data_with_opening_closing(filters, account_details,
|
|
|
|
accounting_dimensions, gl_entries)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
result = get_result_as_list(data, filters)
|
2013-12-27 12:03:55 +00:00
|
|
|
|
|
|
|
return result
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2020-08-14 06:42:54 +00:00
|
|
|
def get_gl_entries(filters, accounting_dimensions):
|
2018-02-12 09:34:50 +00:00
|
|
|
currency_map = get_currency(filters)
|
2018-11-12 11:28:24 +00:00
|
|
|
select_fields = """, debit, credit, debit_in_account_currency,
|
|
|
|
credit_in_account_currency """
|
|
|
|
|
2020-01-23 12:12:57 +00:00
|
|
|
order_by_statement = "order by posting_date, account, creation"
|
2018-11-12 11:28:24 +00:00
|
|
|
|
2021-03-09 15:01:57 +00:00
|
|
|
if filters.get("include_dimensions"):
|
|
|
|
order_by_statement = "order by posting_date, creation"
|
|
|
|
|
2021-09-08 10:58:05 +00:00
|
|
|
if filters.get("group_by") == "Group by Voucher":
|
2018-11-12 11:28:24 +00:00
|
|
|
order_by_statement = "order by posting_date, voucher_type, voucher_no"
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2019-06-18 13:04:14 +00:00
|
|
|
if filters.get("include_default_book_entries"):
|
|
|
|
filters['company_fb'] = frappe.db.get_value("Company",
|
|
|
|
filters.get("company"), 'default_finance_book')
|
|
|
|
|
2020-08-14 06:42:54 +00:00
|
|
|
dimension_fields = ""
|
|
|
|
if accounting_dimensions:
|
|
|
|
dimension_fields = ', '.join(accounting_dimensions) + ','
|
|
|
|
|
2020-06-08 06:50:21 +00:00
|
|
|
distributed_cost_center_query = ""
|
|
|
|
if filters and filters.get('cost_center'):
|
2021-03-09 15:01:57 +00:00
|
|
|
select_fields_with_percentage = """, debit*(DCC_allocation.percentage_allocation/100) as debit,
|
|
|
|
credit*(DCC_allocation.percentage_allocation/100) as credit,
|
|
|
|
debit_in_account_currency*(DCC_allocation.percentage_allocation/100) as debit_in_account_currency,
|
2020-06-08 06:50:21 +00:00
|
|
|
credit_in_account_currency*(DCC_allocation.percentage_allocation/100) as credit_in_account_currency """
|
2020-08-17 07:14:52 +00:00
|
|
|
|
2020-06-08 06:50:21 +00:00
|
|
|
distributed_cost_center_query = """
|
|
|
|
UNION ALL
|
|
|
|
SELECT name as gl_entry,
|
|
|
|
posting_date,
|
|
|
|
account,
|
|
|
|
party_type,
|
|
|
|
party,
|
|
|
|
voucher_type,
|
2020-08-14 06:42:54 +00:00
|
|
|
voucher_no, {dimension_fields}
|
2020-06-08 06:50:21 +00:00
|
|
|
cost_center, project,
|
|
|
|
against_voucher_type,
|
|
|
|
against_voucher,
|
|
|
|
account_currency,
|
2020-08-17 07:14:52 +00:00
|
|
|
remarks, against,
|
2020-06-08 06:50:21 +00:00
|
|
|
is_opening, `tabGL Entry`.creation {select_fields_with_percentage}
|
|
|
|
FROM `tabGL Entry`,
|
|
|
|
(
|
|
|
|
SELECT parent, sum(percentage_allocation) as percentage_allocation
|
|
|
|
FROM `tabDistributed Cost Center`
|
|
|
|
WHERE cost_center IN %(cost_center)s
|
|
|
|
AND parent NOT IN %(cost_center)s
|
|
|
|
GROUP BY parent
|
|
|
|
) as DCC_allocation
|
|
|
|
WHERE company=%(company)s
|
|
|
|
{conditions}
|
|
|
|
AND posting_date <= %(to_date)s
|
|
|
|
AND cost_center = DCC_allocation.parent
|
2020-08-14 06:42:54 +00:00
|
|
|
""".format(dimension_fields=dimension_fields,select_fields_with_percentage=select_fields_with_percentage, conditions=get_conditions(filters).replace("and cost_center in %(cost_center)s ", ''))
|
2020-06-08 06:50:21 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
gl_entries = frappe.db.sql(
|
|
|
|
"""
|
2016-08-09 11:13:15 +00:00
|
|
|
select
|
2020-03-26 08:07:40 +00:00
|
|
|
name as gl_entry, posting_date, account, party_type, party,
|
2020-08-14 06:42:54 +00:00
|
|
|
voucher_type, voucher_no, {dimension_fields}
|
|
|
|
cost_center, project,
|
2018-02-12 09:34:50 +00:00
|
|
|
against_voucher_type, against_voucher, account_currency,
|
2020-06-08 06:50:21 +00:00
|
|
|
remarks, against, is_opening, creation {select_fields}
|
2013-12-26 05:37:58 +00:00
|
|
|
from `tabGL Entry`
|
2019-08-20 07:05:15 +00:00
|
|
|
where company=%(company)s {conditions}
|
2020-06-08 06:50:21 +00:00
|
|
|
{distributed_cost_center_query}
|
2018-11-12 11:28:24 +00:00
|
|
|
{order_by_statement}
|
2018-02-12 09:34:50 +00:00
|
|
|
""".format(
|
2020-08-14 06:42:54 +00:00
|
|
|
dimension_fields=dimension_fields, select_fields=select_fields, conditions=get_conditions(filters), distributed_cost_center_query=distributed_cost_center_query,
|
2018-11-12 11:28:24 +00:00
|
|
|
order_by_statement=order_by_statement
|
2018-02-12 09:34:50 +00:00
|
|
|
),
|
2021-06-24 14:21:23 +00:00
|
|
|
filters, as_dict=1)
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
if filters.get('presentation_currency'):
|
2020-08-22 07:01:06 +00:00
|
|
|
return convert_to_presentation_currency(gl_entries, currency_map, filters.get('company'))
|
2018-02-12 09:34:50 +00:00
|
|
|
else:
|
|
|
|
return gl_entries
|
2014-05-20 09:37:18 +00:00
|
|
|
|
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_conditions(filters):
|
|
|
|
conditions = []
|
2021-06-14 07:52:44 +00:00
|
|
|
|
2021-06-24 14:18:11 +00:00
|
|
|
if filters.get("account"):
|
2021-06-16 09:10:14 +00:00
|
|
|
filters.account = get_accounts_with_children(filters.account)
|
|
|
|
conditions.append("account in %(account)s")
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-09-06 09:09:35 +00:00
|
|
|
if filters.get("cost_center"):
|
2018-09-07 07:47:11 +00:00
|
|
|
filters.cost_center = get_cost_centers_with_children(filters.cost_center)
|
|
|
|
conditions.append("cost_center in %(cost_center)s")
|
2018-09-06 09:09:35 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
if filters.get("voucher_no"):
|
|
|
|
conditions.append("voucher_no=%(voucher_no)s")
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-04-17 09:42:26 +00:00
|
|
|
if filters.get("group_by") == "Group by Party" and not filters.get("party_type"):
|
|
|
|
conditions.append("party_type in ('Customer', 'Supplier')")
|
|
|
|
|
2015-04-20 11:43:43 +00:00
|
|
|
if filters.get("party_type"):
|
|
|
|
conditions.append("party_type=%(party_type)s")
|
|
|
|
|
|
|
|
if filters.get("party"):
|
2018-05-03 13:30:43 +00:00
|
|
|
conditions.append("party in %(party)s")
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2018-04-17 09:42:26 +00:00
|
|
|
if not (filters.get("account") or filters.get("party") or
|
|
|
|
filters.get("group_by") in ["Group by Account", "Group by Party"]):
|
2015-06-14 12:19:29 +00:00
|
|
|
conditions.append("posting_date >=%(from_date)s")
|
2019-08-20 07:05:15 +00:00
|
|
|
|
|
|
|
conditions.append("(posting_date <=%(to_date)s or is_opening = 'Yes')")
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2017-01-24 09:42:24 +00:00
|
|
|
if filters.get("project"):
|
2018-08-27 06:38:53 +00:00
|
|
|
conditions.append("project in %(project)s")
|
|
|
|
|
2019-04-16 13:46:14 +00:00
|
|
|
if filters.get("finance_book"):
|
2019-06-18 13:04:14 +00:00
|
|
|
if filters.get("include_default_book_entries"):
|
2020-01-27 09:48:51 +00:00
|
|
|
conditions.append("(finance_book in (%(finance_book)s, %(company_fb)s, '') OR finance_book IS NULL)")
|
2019-06-18 13:04:14 +00:00
|
|
|
else:
|
|
|
|
conditions.append("finance_book in (%(finance_book)s)")
|
2018-04-22 20:59:48 +00:00
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
if not filters.get("show_cancelled_entries"):
|
|
|
|
conditions.append("is_cancelled = 0")
|
|
|
|
|
2014-09-09 10:45:35 +00:00
|
|
|
from frappe.desk.reportview import build_match_conditions
|
2014-01-09 07:14:44 +00:00
|
|
|
match_conditions = build_match_conditions("GL Entry")
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
if match_conditions:
|
|
|
|
conditions.append(match_conditions)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2021-03-09 15:01:57 +00:00
|
|
|
if filters.get("include_dimensions"):
|
|
|
|
accounting_dimensions = get_accounting_dimensions(as_list=False)
|
|
|
|
|
|
|
|
if accounting_dimensions:
|
|
|
|
for dimension in accounting_dimensions:
|
|
|
|
if not dimension.disabled:
|
|
|
|
if filters.get(dimension.fieldname):
|
|
|
|
if frappe.get_cached_value('DocType', dimension.document_type, 'is_tree'):
|
|
|
|
filters[dimension.fieldname] = get_dimension_with_children(dimension.document_type,
|
|
|
|
filters.get(dimension.fieldname))
|
|
|
|
conditions.append("{0} in %({0})s".format(dimension.fieldname))
|
|
|
|
else:
|
|
|
|
conditions.append("{0} in (%({0})s)".format(dimension.fieldname))
|
2019-05-12 13:04:23 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
return "and {}".format(" and ".join(conditions)) if conditions else ""
|
2013-12-27 12:03:55 +00:00
|
|
|
|
2021-06-16 09:10:14 +00:00
|
|
|
def get_accounts_with_children(accounts):
|
|
|
|
if not isinstance(accounts, list):
|
|
|
|
accounts = [d.strip() for d in accounts.strip().split(',') if d]
|
|
|
|
|
|
|
|
all_accounts = []
|
|
|
|
for d in accounts:
|
|
|
|
if frappe.db.exists("Account", d):
|
|
|
|
lft, rgt = frappe.db.get_value("Account", d, ["lft", "rgt"])
|
|
|
|
children = frappe.get_all("Account", filters={"lft": [">=", lft], "rgt": ["<=", rgt]})
|
|
|
|
all_accounts += [c.name for c in children]
|
|
|
|
else:
|
|
|
|
frappe.throw(_("Account: {0} does not exist").format(d))
|
|
|
|
|
|
|
|
return list(set(all_accounts))
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2020-08-14 06:42:54 +00:00
|
|
|
def get_data_with_opening_closing(filters, account_details, accounting_dimensions, gl_entries):
|
2013-12-26 05:37:58 +00:00
|
|
|
data = []
|
2018-04-17 09:42:26 +00:00
|
|
|
|
|
|
|
gle_map = initialize_gle_map(gl_entries, filters)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2020-08-14 06:42:54 +00:00
|
|
|
totals, entries = get_accountwise_gle(filters, accounting_dimensions, gl_entries, gle_map)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
# Opening for filtered account
|
2017-11-15 10:59:53 +00:00
|
|
|
data.append(totals.opening)
|
2013-12-27 12:03:55 +00:00
|
|
|
|
2021-09-08 10:58:05 +00:00
|
|
|
if filters.get("group_by") != 'Group by Voucher (Consolidated)':
|
2018-05-23 06:01:24 +00:00
|
|
|
for acc, acc_dict in iteritems(gle_map):
|
2018-04-17 09:42:26 +00:00
|
|
|
# acc
|
2015-09-24 09:33:53 +00:00
|
|
|
if acc_dict.entries:
|
2017-11-15 10:59:53 +00:00
|
|
|
# opening
|
|
|
|
data.append({})
|
2021-09-08 10:58:05 +00:00
|
|
|
if filters.get("group_by") != "Group by Voucher":
|
2018-08-20 12:19:29 +00:00
|
|
|
data.append(acc_dict.totals.opening)
|
2015-09-24 09:33:53 +00:00
|
|
|
|
|
|
|
data += acc_dict.entries
|
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
# totals
|
|
|
|
data.append(acc_dict.totals.total)
|
2015-09-24 09:33:53 +00:00
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
# closing
|
2021-09-08 10:58:05 +00:00
|
|
|
if filters.get("group_by") != "Group by Voucher":
|
2018-08-20 12:19:29 +00:00
|
|
|
data.append(acc_dict.totals.closing)
|
2017-11-15 10:59:53 +00:00
|
|
|
data.append({})
|
2015-09-24 09:33:53 +00:00
|
|
|
else:
|
2017-11-15 10:59:53 +00:00
|
|
|
data += entries
|
|
|
|
|
|
|
|
# totals
|
|
|
|
data.append(totals.total)
|
|
|
|
|
|
|
|
# closing
|
|
|
|
data.append(totals.closing)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
return data
|
2013-12-27 12:03:55 +00:00
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
def get_totals_dict():
|
|
|
|
def _get_debit_credit_dict(label):
|
|
|
|
return _dict(
|
2018-02-12 09:34:50 +00:00
|
|
|
account="'{0}'".format(label),
|
|
|
|
debit=0.0,
|
|
|
|
credit=0.0,
|
|
|
|
debit_in_account_currency=0.0,
|
|
|
|
credit_in_account_currency=0.0
|
2017-11-15 10:59:53 +00:00
|
|
|
)
|
|
|
|
return _dict(
|
|
|
|
opening = _get_debit_credit_dict(_('Opening')),
|
|
|
|
total = _get_debit_credit_dict(_('Total')),
|
2018-01-18 04:02:43 +00:00
|
|
|
closing = _get_debit_credit_dict(_('Closing (Opening + Total)'))
|
2017-11-15 10:59:53 +00:00
|
|
|
)
|
|
|
|
|
2018-08-20 12:19:29 +00:00
|
|
|
def group_by_field(group_by):
|
2021-09-08 10:58:05 +00:00
|
|
|
if group_by == 'Group by Party':
|
2018-08-20 12:19:29 +00:00
|
|
|
return 'party'
|
2021-09-08 10:58:05 +00:00
|
|
|
elif group_by in ['Group by Voucher (Consolidated)', 'Group by Account']:
|
2018-08-20 12:19:29 +00:00
|
|
|
return 'account'
|
2018-11-12 11:28:24 +00:00
|
|
|
else:
|
|
|
|
return 'voucher_no'
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2018-04-17 09:42:26 +00:00
|
|
|
def initialize_gle_map(gl_entries, filters):
|
2019-06-21 11:50:37 +00:00
|
|
|
gle_map = OrderedDict()
|
2018-08-20 12:19:29 +00:00
|
|
|
group_by = group_by_field(filters.get('group_by'))
|
2018-04-17 09:42:26 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
for gle in gl_entries:
|
2018-04-17 09:42:26 +00:00
|
|
|
gle_map.setdefault(gle.get(group_by), _dict(totals=get_totals_dict(), entries=[]))
|
2013-12-27 12:03:55 +00:00
|
|
|
return gle_map
|
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2020-08-14 06:42:54 +00:00
|
|
|
def get_accountwise_gle(filters, accounting_dimensions, gl_entries, gle_map):
|
2017-11-15 10:59:53 +00:00
|
|
|
totals = get_totals_dict()
|
|
|
|
entries = []
|
2019-08-20 07:05:15 +00:00
|
|
|
consolidated_gle = OrderedDict()
|
2018-08-20 12:19:29 +00:00
|
|
|
group_by = group_by_field(filters.get('group_by'))
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2021-05-25 15:09:17 +00:00
|
|
|
if filters.get('show_net_values_in_party_account'):
|
|
|
|
account_type_map = get_account_type_map(filters.get('company'))
|
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
def update_value_in_dict(data, key, gle):
|
|
|
|
data[key].debit += flt(gle.debit)
|
|
|
|
data[key].credit += flt(gle.credit)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
data[key].debit_in_account_currency += flt(gle.debit_in_account_currency)
|
|
|
|
data[key].credit_in_account_currency += flt(gle.credit_in_account_currency)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2021-05-25 15:09:17 +00:00
|
|
|
if filters.get('show_net_values_in_party_account') and \
|
|
|
|
account_type_map.get(data[key].account) in ('Receivable', 'Payable'):
|
|
|
|
net_value = flt(data[key].debit) - flt(data[key].credit)
|
|
|
|
net_value_in_account_currency = flt(data[key].debit_in_account_currency) \
|
|
|
|
- flt(data[key].credit_in_account_currency)
|
|
|
|
|
|
|
|
if net_value < 0:
|
|
|
|
dr_or_cr = 'credit'
|
|
|
|
rev_dr_or_cr = 'debit'
|
|
|
|
else:
|
|
|
|
dr_or_cr = 'debit'
|
|
|
|
rev_dr_or_cr = 'credit'
|
|
|
|
|
|
|
|
data[key][dr_or_cr] = abs(net_value)
|
|
|
|
data[key][dr_or_cr+'_in_account_currency'] = abs(net_value_in_account_currency)
|
|
|
|
data[key][rev_dr_or_cr] = 0
|
|
|
|
data[key][rev_dr_or_cr+'_in_account_currency'] = 0
|
|
|
|
|
2020-05-19 15:21:30 +00:00
|
|
|
if data[key].against_voucher and gle.against_voucher:
|
2020-05-05 10:47:15 +00:00
|
|
|
data[key].against_voucher += ', ' + gle.against_voucher
|
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
|
|
|
|
for gle in gl_entries:
|
2019-05-20 06:55:53 +00:00
|
|
|
if (gle.posting_date < from_date or
|
|
|
|
(cstr(gle.is_opening) == "Yes" and not filters.get("show_opening_entries"))):
|
2018-04-17 09:42:26 +00:00
|
|
|
update_value_in_dict(gle_map[gle.get(group_by)].totals, 'opening', gle)
|
2017-11-15 10:59:53 +00:00
|
|
|
update_value_in_dict(totals, 'opening', gle)
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2018-04-17 09:42:26 +00:00
|
|
|
update_value_in_dict(gle_map[gle.get(group_by)].totals, 'closing', gle)
|
2017-12-15 14:10:55 +00:00
|
|
|
update_value_in_dict(totals, 'closing', gle)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
elif gle.posting_date <= to_date:
|
2021-09-08 10:58:05 +00:00
|
|
|
if filters.get("group_by") != 'Group by Voucher (Consolidated)':
|
2018-04-17 09:42:26 +00:00
|
|
|
gle_map[gle.get(group_by)].entries.append(gle)
|
2021-09-08 10:58:05 +00:00
|
|
|
elif filters.get("group_by") == 'Group by Voucher (Consolidated)':
|
2020-08-14 06:42:54 +00:00
|
|
|
keylist = [gle.get("voucher_type"), gle.get("voucher_no"), gle.get("account")]
|
|
|
|
for dim in accounting_dimensions:
|
|
|
|
keylist.append(gle.get(dim))
|
|
|
|
keylist.append(gle.get("cost_center"))
|
|
|
|
key = tuple(keylist)
|
2019-08-20 07:05:15 +00:00
|
|
|
if key not in consolidated_gle:
|
|
|
|
consolidated_gle.setdefault(key, gle)
|
|
|
|
else:
|
|
|
|
update_value_in_dict(consolidated_gle, key, gle)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2019-08-20 07:05:15 +00:00
|
|
|
for key, value in consolidated_gle.items():
|
2021-10-19 09:38:05 +00:00
|
|
|
update_value_in_dict(gle_map[value.get(group_by)].totals, 'total', value)
|
|
|
|
update_value_in_dict(totals, 'total', value)
|
|
|
|
update_value_in_dict(gle_map[value.get(group_by)].totals, 'closing', value)
|
|
|
|
update_value_in_dict(totals, 'closing', value)
|
2019-08-20 07:05:15 +00:00
|
|
|
entries.append(value)
|
|
|
|
|
2017-11-15 10:59:53 +00:00
|
|
|
return totals, entries
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2021-05-25 15:09:17 +00:00
|
|
|
def get_account_type_map(company):
|
|
|
|
account_type_map = frappe._dict(frappe.get_all('Account', fields=['name', 'account_type'],
|
|
|
|
filters={'company': company}, as_list=1))
|
|
|
|
|
|
|
|
return account_type_map
|
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
def get_result_as_list(data, filters):
|
2018-01-18 04:02:43 +00:00
|
|
|
balance, balance_in_account_currency = 0, 0
|
|
|
|
inv_details = get_supplier_invoice_details()
|
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
for d in data:
|
2018-01-19 13:36:39 +00:00
|
|
|
if not d.get('posting_date'):
|
2018-01-18 04:02:43 +00:00
|
|
|
balance, balance_in_account_currency = 0, 0
|
|
|
|
|
2018-02-07 06:40:14 +00:00
|
|
|
balance = get_balance(d, balance, 'debit', 'credit')
|
|
|
|
d['balance'] = balance
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2018-01-18 04:02:43 +00:00
|
|
|
d['account_currency'] = filters.account_currency
|
2018-01-19 13:36:39 +00:00
|
|
|
d['bill_no'] = inv_details.get(d.get('against_voucher'), '')
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2018-01-18 04:02:43 +00:00
|
|
|
return data
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2018-01-18 04:02:43 +00:00
|
|
|
def get_supplier_invoice_details():
|
|
|
|
inv_details = {}
|
|
|
|
for d in frappe.db.sql(""" select name, bill_no from `tabPurchase Invoice`
|
|
|
|
where docstatus = 1 and bill_no is not null and bill_no != '' """, as_dict=1):
|
|
|
|
inv_details[d.name] = d.bill_no
|
|
|
|
|
|
|
|
return inv_details
|
|
|
|
|
|
|
|
def get_balance(row, balance, debit_field, credit_field):
|
|
|
|
balance += (row.get(debit_field, 0) - row.get(credit_field, 0))
|
|
|
|
|
2018-02-07 06:40:14 +00:00
|
|
|
return balance
|
2018-01-18 04:02:43 +00:00
|
|
|
|
|
|
|
def get_columns(filters):
|
2018-02-12 09:34:50 +00:00
|
|
|
if filters.get("presentation_currency"):
|
|
|
|
currency = filters["presentation_currency"]
|
|
|
|
else:
|
|
|
|
if filters.get("company"):
|
|
|
|
currency = get_company_currency(filters["company"])
|
|
|
|
else:
|
|
|
|
company = get_default_company()
|
|
|
|
currency = get_company_currency(company)
|
|
|
|
|
2018-01-18 04:02:43 +00:00
|
|
|
columns = [
|
2020-03-26 08:07:40 +00:00
|
|
|
{
|
2020-04-17 14:22:47 +00:00
|
|
|
"label": _("GL Entry"),
|
2020-03-26 08:07:40 +00:00
|
|
|
"fieldname": "gl_entry",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "GL Entry",
|
|
|
|
"hidden": 1
|
|
|
|
},
|
2018-01-18 04:02:43 +00:00
|
|
|
{
|
|
|
|
"label": _("Posting Date"),
|
|
|
|
"fieldname": "posting_date",
|
|
|
|
"fieldtype": "Date",
|
|
|
|
"width": 90
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Account"),
|
|
|
|
"fieldname": "account",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Account",
|
|
|
|
"width": 180
|
|
|
|
},
|
|
|
|
{
|
2020-01-29 09:36:18 +00:00
|
|
|
"label": _("Debit ({0})").format(currency),
|
2018-01-18 04:02:43 +00:00
|
|
|
"fieldname": "debit",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
2020-01-29 09:36:18 +00:00
|
|
|
"label": _("Credit ({0})").format(currency),
|
2018-01-18 04:02:43 +00:00
|
|
|
"fieldname": "credit",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
2020-01-29 09:36:18 +00:00
|
|
|
"label": _("Balance ({0})").format(currency),
|
2018-01-18 04:02:43 +00:00
|
|
|
"fieldname": "balance",
|
2018-02-07 06:40:14 +00:00
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 130
|
2018-01-18 04:02:43 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
columns.extend([
|
|
|
|
{
|
|
|
|
"label": _("Voucher Type"),
|
|
|
|
"fieldname": "voucher_type",
|
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Voucher No"),
|
|
|
|
"fieldname": "voucher_no",
|
|
|
|
"fieldtype": "Dynamic Link",
|
|
|
|
"options": "voucher_type",
|
|
|
|
"width": 180
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Against Account"),
|
|
|
|
"fieldname": "against",
|
|
|
|
"width": 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Party Type"),
|
|
|
|
"fieldname": "party_type",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Party"),
|
|
|
|
"fieldname": "party",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Project"),
|
|
|
|
"options": "Project",
|
|
|
|
"fieldname": "project",
|
|
|
|
"width": 100
|
2020-08-14 06:42:54 +00:00
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
if filters.get("include_dimensions"):
|
|
|
|
for dim in get_accounting_dimensions(as_list = False):
|
|
|
|
columns.append({
|
|
|
|
"label": _(dim.label),
|
|
|
|
"options": dim.label,
|
|
|
|
"fieldname": dim.fieldname,
|
|
|
|
"width": 100
|
|
|
|
})
|
|
|
|
|
|
|
|
columns.extend([
|
2018-01-18 04:02:43 +00:00
|
|
|
{
|
|
|
|
"label": _("Cost Center"),
|
|
|
|
"options": "Cost Center",
|
|
|
|
"fieldname": "cost_center",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Against Voucher Type"),
|
|
|
|
"fieldname": "against_voucher_type",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Against Voucher"),
|
|
|
|
"fieldname": "against_voucher",
|
|
|
|
"fieldtype": "Dynamic Link",
|
|
|
|
"options": "against_voucher_type",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Supplier Invoice No"),
|
|
|
|
"fieldname": "bill_no",
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Remarks"),
|
|
|
|
"fieldname": "remarks",
|
|
|
|
"width": 400
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
return columns
|