brotherton-erpnext/erpnext/accounts/report/general_ledger/general_ledger.py

449 lines
13 KiB
Python
Raw Normal View History

# 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
import frappe, erpnext
from erpnext import get_company_currency, get_default_company
from erpnext.accounts.report.utils import get_currency, convert_to_presentation_currency
from frappe.utils import getdate, cstr, flt, fmt_money
from frappe import _, _dict
2015-09-28 08:01:17 +00:00
from erpnext.accounts.utils import get_account_currency
from erpnext.accounts.report.financial_statements import get_cost_centers_with_children
from six import iteritems
2013-12-26 05:37:58 +00:00
def execute(filters=None):
2018-05-16 18:01:09 +00:00
if not filters:
return [], []
account_details = {}
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
if filters.get('party'):
parties = str(filters.get("party")).strip()
filters.party = [d.strip() for d in parties.split(',') if d]
2013-12-26 05:37:58 +00:00
validate_filters(filters, account_details)
2015-09-28 08:01:17 +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
columns = get_columns(filters)
2014-05-20 09:37:18 +00:00
res = get_result(filters, account_details)
2013-12-26 05:37:58 +00:00
return columns, res
2014-05-20 09:37:18 +00:00
2013-12-26 05:37:58 +00:00
def validate_filters(filters, account_details):
if not filters.get('company'):
frappe.throw(_('{0} is mandatory').format(_('Company')))
2014-08-18 10:08:04 +00:00
if filters.get("account") and not account_details.get(filters.account):
frappe.throw(_("Account {0} does not exists").format(filters.account))
if (filters.get("account") and filters.get("group_by") == _('Group by Account')
and account_details[filters.account].is_group == 0):
2014-02-14 10:17:51 +00:00
frappe.throw(_("Can not filter based on Account, if grouped by Account"))
2014-05-20 09:37:18 +00:00
if (filters.get("voucher_no")
and filters.get("group_by") in [_('Group by Voucher'), _('Group by Voucher (Consolidated)')]):
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
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
if filters.get('project'):
projects = str(filters.get("project")).strip()
filters.project = [d.strip() for d in projects.split(',') if d]
if filters.get('cost_center'):
cost_centers = str(filters.get("cost_center")).strip()
filters.cost_center = [d.strip() for d in cost_centers.split(',') if d]
def validate_party(filters):
party_type, party = filters.get("party_type"), filters.get("party")
if party:
if not party_type:
frappe.throw(_("To filter based on Party, select Party Type first"))
else:
for d in party:
if not frappe.db.exists(party_type, d):
frappe.throw(_("Invalid {0}: {1}").format(party_type, d))
def set_account_currency(filters):
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")
account_currency = None
2015-09-28 08:01:17 +00:00
if filters.get("account"):
2015-09-28 08:01:17 +00:00
account_currency = get_account_currency(filters.account)
elif filters.get("party"):
gle_currency = frappe.db.get_value(
"GL Entry", {
"party_type": filters.party_type, "party": filters.party[0], "company": filters.company
},
"account_currency"
)
if gle_currency:
account_currency = gle_currency
else:
account_currency = (None if filters.party_type in ["Employee", "Student", "Shareholder", "Member"] else
frappe.db.get_value(filters.party_type, filters.party[0], "default_currency"))
2015-09-28 08:01:17 +00:00
filters["account_currency"] = account_currency or filters.company_currency
if filters.account_currency != filters.company_currency:
filters.presentation_currency = filters.account_currency
2015-09-28 08:01:17 +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):
gl_entries = get_gl_entries(filters)
data = get_data_with_opening_closing(filters, account_details, gl_entries)
2014-05-20 09:37:18 +00:00
result = get_result_as_list(data, filters)
return result
2014-05-20 09:37:18 +00:00
2013-12-26 05:37:58 +00:00
def get_gl_entries(filters):
currency_map = get_currency(filters)
select_fields = """, debit, credit, debit_in_account_currency,
credit_in_account_currency """
group_by_statement = ''
order_by_statement = "order by posting_date, account"
if filters.get("group_by") == _("Group by Voucher"):
order_by_statement = "order by posting_date, voucher_type, voucher_no"
if filters.get("group_by") == _("Group by Voucher (Consolidated)"):
group_by_statement = "group by voucher_type, voucher_no, account, cost_center"
select_fields = """, sum(debit) as debit, sum(credit) as credit,
sum(debit_in_account_currency) as debit_in_account_currency,
sum(credit_in_account_currency) as credit_in_account_currency"""
2014-05-20 09:37:18 +00:00
gl_entries = frappe.db.sql(
"""
select
posting_date, account, party_type, party,
voucher_type, voucher_no, cost_center, project,
against_voucher_type, against_voucher, account_currency,
remarks, against, is_opening {select_fields}
2013-12-26 05:37:58 +00:00
from `tabGL Entry`
where company=%(company)s {conditions} {group_by_statement}
{order_by_statement}
""".format(
select_fields=select_fields, conditions=get_conditions(filters),
group_by_statement=group_by_statement,
order_by_statement=order_by_statement
),
filters, as_dict=1)
if filters.get('presentation_currency'):
return convert_to_presentation_currency(gl_entries, currency_map)
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 = []
if filters.get("account"):
2014-02-26 07:05:33 +00:00
lft, rgt = frappe.db.get_value("Account", filters["account"], ["lft", "rgt"])
2014-05-20 09:37:18 +00:00
conditions.append("""account in (select name from tabAccount
2013-12-26 05:37:58 +00:00
where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))
2014-05-20 09:37:18 +00:00
if filters.get("cost_center"):
filters.cost_center = get_cost_centers_with_children(filters.cost_center)
conditions.append("cost_center in %(cost_center)s")
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
if filters.get("group_by") == "Group by Party" and not filters.get("party_type"):
conditions.append("party_type in ('Customer', 'Supplier')")
if filters.get("party_type"):
conditions.append("party_type=%(party_type)s")
if filters.get("party"):
conditions.append("party in %(party)s")
2015-09-28 08:01:17 +00:00
if not (filters.get("account") or filters.get("party") or
filters.get("group_by") in ["Group by Account", "Group by Party"]):
conditions.append("posting_date >=%(from_date)s")
conditions.append("posting_date <=%(to_date)s")
2014-05-20 09:37:18 +00:00
if filters.get("project"):
conditions.append("project in %(project)s")
company_finance_book = erpnext.get_default_finance_book(filters.get("company"))
if not filters.get("finance_book") or (filters.get("finance_book") == company_finance_book):
filters['finance_book'] = company_finance_book
conditions.append("ifnull(finance_book, '') in (%(finance_book)s, '')")
elif filters.get("finance_book"):
conditions.append("ifnull(finance_book, '') = %(finance_book)s")
2014-09-09 10:45:35 +00:00
from frappe.desk.reportview import build_match_conditions
match_conditions = build_match_conditions("GL Entry")
if match_conditions:
conditions.append(match_conditions)
2014-05-20 09:37:18 +00:00
2013-12-26 05:37:58 +00:00
return "and {}".format(" and ".join(conditions)) if conditions else ""
def get_data_with_opening_closing(filters, account_details, gl_entries):
2013-12-26 05:37:58 +00:00
data = []
gle_map = initialize_gle_map(gl_entries, filters)
2014-05-20 09:37:18 +00:00
totals, entries = get_accountwise_gle(filters, gl_entries, gle_map)
2014-05-20 09:37:18 +00:00
# Opening for filtered account
data.append(totals.opening)
if filters.get("group_by") != _('Group by Voucher (Consolidated)'):
for acc, acc_dict in iteritems(gle_map):
# acc
2015-09-24 09:33:53 +00:00
if acc_dict.entries:
# opening
data.append({})
if filters.get("group_by") != _("Group by Voucher"):
data.append(acc_dict.totals.opening)
2015-09-24 09:33:53 +00:00
data += acc_dict.entries
# totals
data.append(acc_dict.totals.total)
2015-09-24 09:33:53 +00:00
# closing
if filters.get("group_by") != _("Group by Voucher"):
data.append(acc_dict.totals.closing)
data.append({})
2015-09-24 09:33:53 +00:00
else:
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
def get_totals_dict():
def _get_debit_credit_dict(label):
return _dict(
account="'{0}'".format(label),
debit=0.0,
credit=0.0,
debit_in_account_currency=0.0,
credit_in_account_currency=0.0
)
return _dict(
opening = _get_debit_credit_dict(_('Opening')),
total = _get_debit_credit_dict(_('Total')),
closing = _get_debit_credit_dict(_('Closing (Opening + Total)'))
)
def group_by_field(group_by):
if group_by == _('Group by Party'):
return 'party'
elif group_by in [_('Group by Voucher (Consolidated)'), _('Group by Account')]:
return 'account'
else:
return 'voucher_no'
def initialize_gle_map(gl_entries, filters):
2014-02-14 10:17:51 +00:00
gle_map = frappe._dict()
group_by = group_by_field(filters.get('group_by'))
for gle in gl_entries:
gle_map.setdefault(gle.get(group_by), _dict(totals=get_totals_dict(), entries=[]))
return gle_map
def get_accountwise_gle(filters, gl_entries, gle_map):
totals = get_totals_dict()
entries = []
group_by = group_by_field(filters.get('group_by'))
2015-09-28 08:01:17 +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
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
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
for gle in gl_entries:
if gle.posting_date < from_date or cstr(gle.is_opening) == "Yes":
update_value_in_dict(gle_map[gle.get(group_by)].totals, 'opening', gle)
update_value_in_dict(totals, 'opening', gle)
update_value_in_dict(gle_map[gle.get(group_by)].totals, 'closing', gle)
update_value_in_dict(totals, 'closing', gle)
2015-09-28 08:01:17 +00:00
elif gle.posting_date <= to_date:
update_value_in_dict(gle_map[gle.get(group_by)].totals, 'total', gle)
update_value_in_dict(totals, 'total', gle)
if filters.get("group_by") != _('Group by Voucher (Consolidated)'):
gle_map[gle.get(group_by)].entries.append(gle)
else:
entries.append(gle)
2015-09-28 08:01:17 +00:00
update_value_in_dict(gle_map[gle.get(group_by)].totals, 'closing', gle)
update_value_in_dict(totals, 'closing', gle)
2015-09-28 08:01:17 +00:00
return totals, entries
2014-05-20 09:37:18 +00:00
def get_result_as_list(data, filters):
balance, balance_in_account_currency = 0, 0
inv_details = get_supplier_invoice_details()
for d in data:
2018-01-19 13:36:39 +00:00
if not d.get('posting_date'):
balance, balance_in_account_currency = 0, 0
balance = get_balance(d, balance, 'debit', 'credit')
d['balance'] = balance
2015-09-28 08:01:17 +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
return data
2014-05-20 09:37:18 +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))
return balance
def get_columns(filters):
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)
columns = [
{
"label": _("Posting Date"),
"fieldname": "posting_date",
"fieldtype": "Date",
"width": 90
},
{
"label": _("Account"),
"fieldname": "account",
"fieldtype": "Link",
"options": "Account",
"width": 180
},
{
"label": _("Debit ({0})".format(currency)),
"fieldname": "debit",
"fieldtype": "Float",
"width": 100
},
{
"label": _("Credit ({0})".format(currency)),
"fieldname": "credit",
"fieldtype": "Float",
"width": 100
},
{
"label": _("Balance ({0})".format(currency)),
"fieldname": "balance",
"fieldtype": "Float",
"width": 130
}
]
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
},
{
"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