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
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2015-06-17 10:05:06 +00:00
|
|
|
from frappe.utils import flt, getdate, cstr
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe import _
|
2015-09-28 08:01:17 +00:00
|
|
|
from erpnext.accounts.utils import get_account_currency
|
2013-12-26 05:37:58 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
2013-12-27 12:03:55 +00:00
|
|
|
account_details = {}
|
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
|
|
|
|
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
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def validate_filters(filters, account_details):
|
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))
|
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
if filters.get("account") and filters.get("group_by_account") \
|
2015-04-23 07:44:17 +00:00
|
|
|
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
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
if filters.get("voucher_no") and filters.get("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
|
|
|
|
2015-04-20 11:43:43 +00:00
|
|
|
|
|
|
|
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"))
|
|
|
|
elif not frappe.db.exists(party_type, party):
|
|
|
|
frappe.throw(_("Invalid {0}: {1}").format(party_type, party))
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
def set_account_currency(filters):
|
|
|
|
if not (filters.get("account") or filters.get("party")):
|
|
|
|
return filters
|
|
|
|
else:
|
|
|
|
filters["company_currency"] = frappe.db.get_value("Company", filters.company, "default_currency")
|
|
|
|
account_currency = None
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("account"):
|
2015-09-28 08:01:17 +00:00
|
|
|
account_currency = get_account_currency(filters.account)
|
2015-08-31 10:09:03 +00:00
|
|
|
elif filters.get("party"):
|
2015-09-28 08:01:17 +00:00
|
|
|
gle_currency = frappe.db.get_value("GL Entry", {"party_type": filters.party_type,
|
2015-08-31 10:09:03 +00:00
|
|
|
"party": filters.party, "company": filters.company}, "account_currency")
|
|
|
|
if gle_currency:
|
|
|
|
account_currency = gle_currency
|
|
|
|
else:
|
|
|
|
account_currency = frappe.db.get_value(filters.party_type, filters.party, "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
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.account_currency != filters.company_currency:
|
|
|
|
filters["show_in_account_currency"] = 1
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
return filters
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
def get_columns(filters):
|
|
|
|
columns = [
|
|
|
|
_("Posting Date") + ":Date:90", _("Account") + ":Link/Account:200",
|
|
|
|
_("Debit") + ":Float:100", _("Credit") + ":Float:100"
|
|
|
|
]
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("show_in_account_currency"):
|
|
|
|
columns += [
|
2015-09-28 08:01:17 +00:00
|
|
|
_("Debit") + " (" + filters.account_currency + ")" + ":Float:100",
|
2015-08-31 10:09:03 +00:00
|
|
|
_("Credit") + " (" + filters.account_currency + ")" + ":Float:100"
|
|
|
|
]
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
columns += [
|
2015-12-09 07:45:40 +00:00
|
|
|
_("Voucher Type") + "::120", _("Voucher No") + ":Dynamic Link/"+_("Voucher Type")+":160",
|
2015-04-23 07:44:17 +00:00
|
|
|
_("Against Account") + "::120", _("Party Type") + "::80", _("Party") + "::150",
|
2015-08-31 10:09:03 +00:00
|
|
|
_("Cost Center") + ":Link/Cost Center:100", _("Remarks") + "::400"
|
|
|
|
]
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
return columns
|
2014-05-20 09:37:18 +00:00
|
|
|
|
|
|
|
def get_result(filters, account_details):
|
2013-12-27 12:03:55 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_gl_entries(filters):
|
2015-11-16 13:35:46 +00:00
|
|
|
select_fields = """, sum(debit_in_account_currency) as debit_in_account_currency,
|
|
|
|
sum(credit_in_account_currency) as credit_in_account_currency""" \
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("show_in_account_currency") else ""
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-24 11:51:01 +00:00
|
|
|
group_by_condition = "group by voucher_type, voucher_no, account, cost_center" \
|
2013-12-27 12:03:55 +00:00
|
|
|
if filters.get("group_by_voucher") else "group by name"
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2015-04-20 11:43:43 +00:00
|
|
|
gl_entries = frappe.db.sql("""select posting_date, account, party_type, party,
|
2015-11-16 13:35:46 +00:00
|
|
|
sum(debit) as debit, sum(credit) as credit,
|
2015-08-31 10:09:03 +00:00
|
|
|
voucher_type, voucher_no, cost_center, remarks, against, is_opening {select_fields}
|
2013-12-26 05:37:58 +00:00
|
|
|
from `tabGL Entry`
|
2013-12-27 12:03:55 +00:00
|
|
|
where company=%(company)s {conditions}
|
|
|
|
{group_by_condition}
|
2013-12-26 05:37:58 +00:00
|
|
|
order by posting_date, account"""\
|
2015-09-28 08:01:17 +00:00
|
|
|
.format(select_fields=select_fields, conditions=get_conditions(filters),
|
2015-08-31 10:09:03 +00:00
|
|
|
group_by_condition=group_by_condition), filters, as_dict=1)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2015-04-20 11:43:43 +00:00
|
|
|
if filters.get("party_type"):
|
|
|
|
conditions.append("party_type=%(party_type)s")
|
|
|
|
|
|
|
|
if filters.get("party"):
|
|
|
|
conditions.append("party=%(party)s")
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
if not (filters.get("account") or filters.get("party") or filters.get("group_by_account")):
|
|
|
|
conditions.append("posting_date >=%(from_date)s")
|
2014-05-20 09:37:18 +00:00
|
|
|
|
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")
|
|
|
|
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 ""
|
2013-12-27 12:03:55 +00:00
|
|
|
|
|
|
|
def get_data_with_opening_closing(filters, account_details, gl_entries):
|
2013-12-26 05:37:58 +00:00
|
|
|
data = []
|
2013-12-27 12:03:55 +00:00
|
|
|
gle_map = initialize_gle_map(gl_entries)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
opening, total_debit, total_credit, opening_in_account_currency, total_debit_in_account_currency, \
|
|
|
|
total_credit_in_account_currency, gle_map = get_accountwise_gle(filters, gl_entries, gle_map)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
# Opening for filtered account
|
2015-05-27 07:31:38 +00:00
|
|
|
if filters.get("account") or filters.get("party"):
|
2015-08-31 10:09:03 +00:00
|
|
|
data += [get_balance_row(_("Opening"), opening, opening_in_account_currency), {}]
|
2013-12-27 12:03:55 +00:00
|
|
|
|
2015-09-24 09:33:53 +00:00
|
|
|
if filters.get("group_by_account"):
|
|
|
|
for acc, acc_dict in gle_map.items():
|
|
|
|
if acc_dict.entries:
|
|
|
|
# Opening for individual ledger, if grouped by account
|
2015-10-01 05:52:09 +00:00
|
|
|
data.append(get_balance_row(_("Opening"), acc_dict.opening,
|
|
|
|
acc_dict.opening_in_account_currency))
|
2015-09-24 09:33:53 +00:00
|
|
|
|
|
|
|
data += acc_dict.entries
|
|
|
|
|
|
|
|
# Totals and closing for individual ledger, if grouped by account
|
2015-10-01 05:52:09 +00:00
|
|
|
account_closing = acc_dict.opening + acc_dict.total_debit - acc_dict.total_credit
|
|
|
|
account_closing_in_account_currency = acc_dict.opening_in_account_currency \
|
|
|
|
+ acc_dict.total_debit_in_account_currency - acc_dict.total_credit_in_account_currency
|
2015-09-24 09:33:53 +00:00
|
|
|
|
2015-10-01 05:52:09 +00:00
|
|
|
data += [{"account": "'" + _("Totals") + "'", "debit": acc_dict.total_debit,
|
|
|
|
"credit": acc_dict.total_credit},
|
|
|
|
get_balance_row(_("Closing (Opening + Totals)"),
|
|
|
|
account_closing, account_closing_in_account_currency), {}]
|
2015-09-24 09:33:53 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
for gl in gl_entries:
|
2015-12-15 09:31:58 +00:00
|
|
|
if gl.posting_date >= getdate(filters.from_date) and gl.posting_date <= getdate(filters.to_date) \
|
|
|
|
and gl.is_opening == "No":
|
2015-09-24 09:33:53 +00:00
|
|
|
data.append(gl)
|
|
|
|
|
2014-05-20 09:37:18 +00:00
|
|
|
|
|
|
|
# Total debit and credit between from and to date
|
2013-12-27 12:03:55 +00:00
|
|
|
if total_debit or total_credit:
|
2015-08-31 10:09:03 +00:00
|
|
|
data.append({
|
2015-09-28 08:01:17 +00:00
|
|
|
"account": "'" + _("Totals") + "'",
|
|
|
|
"debit": total_debit,
|
2015-08-31 10:09:03 +00:00
|
|
|
"credit": total_credit,
|
2015-09-28 08:01:17 +00:00
|
|
|
"debit_in_account_currency": total_debit_in_account_currency,
|
2015-08-31 10:09:03 +00:00
|
|
|
"credit_in_account_currency": total_credit_in_account_currency
|
|
|
|
})
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
# Closing for filtered account
|
2015-05-27 07:31:38 +00:00
|
|
|
if filters.get("account") or filters.get("party"):
|
2015-08-31 10:09:03 +00:00
|
|
|
closing = opening + total_debit - total_credit
|
|
|
|
closing_in_account_currency = opening_in_account_currency + \
|
|
|
|
total_debit_in_account_currency - total_credit_in_account_currency
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-05-21 08:08:03 +00:00
|
|
|
data.append(get_balance_row(_("Closing (Opening + Totals)"),
|
2015-08-31 10:09:03 +00:00
|
|
|
closing, closing_in_account_currency))
|
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
|
|
|
|
|
|
|
def initialize_gle_map(gl_entries):
|
2014-02-14 10:17:51 +00:00
|
|
|
gle_map = frappe._dict()
|
2013-12-27 12:03:55 +00:00
|
|
|
for gle in gl_entries:
|
2014-02-14 10:17:51 +00:00
|
|
|
gle_map.setdefault(gle.account, frappe._dict({
|
2013-12-27 12:03:55 +00:00
|
|
|
"opening": 0,
|
2015-08-31 10:09:03 +00:00
|
|
|
"opening_in_account_currency": 0,
|
2013-12-27 12:03:55 +00:00
|
|
|
"entries": [],
|
|
|
|
"total_debit": 0,
|
2015-08-31 10:09:03 +00:00
|
|
|
"total_debit_in_account_currency": 0,
|
2013-12-27 12:03:55 +00:00
|
|
|
"total_credit": 0,
|
2015-08-31 10:09:03 +00:00
|
|
|
"total_credit_in_account_currency": 0,
|
|
|
|
"closing": 0,
|
|
|
|
"closing_in_account_currency": 0
|
2013-12-27 12:03:55 +00:00
|
|
|
}))
|
|
|
|
return gle_map
|
|
|
|
|
|
|
|
def get_accountwise_gle(filters, gl_entries, gle_map):
|
|
|
|
opening, total_debit, total_credit = 0, 0, 0
|
2015-08-31 10:09:03 +00:00
|
|
|
opening_in_account_currency, total_debit_in_account_currency, total_credit_in_account_currency = 0, 0, 0
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
|
2013-12-27 12:03:55 +00:00
|
|
|
for gle in gl_entries:
|
2014-08-22 09:07:55 +00:00
|
|
|
amount = flt(gle.debit, 3) - flt(gle.credit, 3)
|
2015-08-31 10:09:03 +00:00
|
|
|
amount_in_account_currency = flt(gle.debit_in_account_currency, 3) - flt(gle.credit_in_account_currency, 3)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
if (filters.get("account") or filters.get("party") or filters.get("group_by_account")) \
|
2015-06-17 09:39:33 +00:00
|
|
|
and (gle.posting_date < from_date or cstr(gle.is_opening) == "Yes"):
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-05-12 12:10:14 +00:00
|
|
|
gle_map[gle.account].opening += amount
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("show_in_account_currency"):
|
|
|
|
gle_map[gle.account].opening_in_account_currency += amount_in_account_currency
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-05-27 07:31:38 +00:00
|
|
|
if filters.get("account") or filters.get("party"):
|
|
|
|
opening += amount
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("show_in_account_currency"):
|
|
|
|
opening_in_account_currency += amount_in_account_currency
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-06-14 12:19:29 +00:00
|
|
|
elif gle.posting_date <= to_date:
|
2013-12-27 12:03:55 +00:00
|
|
|
gle_map[gle.account].entries.append(gle)
|
2014-08-22 09:07:55 +00:00
|
|
|
gle_map[gle.account].total_debit += flt(gle.debit, 3)
|
|
|
|
gle_map[gle.account].total_credit += flt(gle.credit, 3)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2014-08-22 09:07:55 +00:00
|
|
|
total_debit += flt(gle.debit, 3)
|
|
|
|
total_credit += flt(gle.credit, 3)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("show_in_account_currency"):
|
|
|
|
gle_map[gle.account].total_debit_in_account_currency += flt(gle.debit_in_account_currency, 3)
|
|
|
|
gle_map[gle.account].total_credit_in_account_currency += flt(gle.credit_in_account_currency, 3)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
total_debit_in_account_currency += flt(gle.debit_in_account_currency, 3)
|
2015-09-28 08:01:17 +00:00
|
|
|
total_credit_in_account_currency += flt(gle.credit_in_account_currency, 3)
|
2015-08-31 10:09:03 +00:00
|
|
|
|
|
|
|
return opening, total_debit, total_credit, opening_in_account_currency, \
|
|
|
|
total_debit_in_account_currency, total_credit_in_account_currency, gle_map
|
|
|
|
|
|
|
|
def get_balance_row(label, balance, balance_in_account_currency=None):
|
|
|
|
balance_row = {
|
2015-05-20 11:51:53 +00:00
|
|
|
"account": "'" + label + "'",
|
2014-03-03 13:10:24 +00:00
|
|
|
"debit": balance if balance > 0 else 0,
|
2015-08-31 10:09:03 +00:00
|
|
|
"credit": -1*balance if balance < 0 else 0
|
2013-12-27 12:03:55 +00:00
|
|
|
}
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if balance_in_account_currency != None:
|
|
|
|
balance_row.update({
|
|
|
|
"debit_in_account_currency": balance_in_account_currency if balance_in_account_currency > 0 else 0,
|
|
|
|
"credit_in_account_currency": -1*balance_in_account_currency if balance_in_account_currency < 0 else 0
|
|
|
|
})
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
return balance_row
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
def get_result_as_list(data, filters):
|
2013-12-27 12:03:55 +00:00
|
|
|
result = []
|
|
|
|
for d in data:
|
2015-08-31 10:09:03 +00:00
|
|
|
row = [d.get("posting_date"), d.get("account"), d.get("debit"), d.get("credit")]
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
if filters.get("show_in_account_currency"):
|
|
|
|
row += [d.get("debit_in_account_currency"), d.get("credit_in_account_currency")]
|
2015-09-28 08:01:17 +00:00
|
|
|
|
|
|
|
row += [d.get("voucher_type"), d.get("voucher_no"), d.get("against"),
|
2015-08-31 10:09:03 +00:00
|
|
|
d.get("party_type"), d.get("party"), d.get("cost_center"), d.get("remarks")
|
|
|
|
]
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2015-08-31 10:09:03 +00:00
|
|
|
result.append(row)
|
2014-05-20 09:37:18 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
return result
|