2013-12-26 05:37:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import cstr, flt
|
|
|
|
from frappe import _
|
2013-12-26 05:37:58 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
2013-12-27 12:03:55 +00:00
|
|
|
account_details = {}
|
2014-02-14 10:17:51 +00:00
|
|
|
for acc in frappe.conn.sql("""select name, debit_or_credit, group_or_ledger
|
2013-12-27 12:03:55 +00:00
|
|
|
from tabAccount""", as_dict=1):
|
|
|
|
account_details.setdefault(acc.name, acc)
|
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
validate_filters(filters, account_details)
|
|
|
|
|
|
|
|
columns = get_columns()
|
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
|
2013-12-26 05:37:58 +00:00
|
|
|
|
|
|
|
def validate_filters(filters, account_details):
|
2013-12-27 12:03:55 +00:00
|
|
|
if filters.get("account") and filters.get("group_by_account") \
|
|
|
|
and account_details[filters.account].group_or_ledger == "Ledger":
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(_("Can not filter based on Account, if grouped by Account"))
|
2013-12-26 05:37:58 +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"))
|
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"))
|
2013-12-26 05:37:58 +00:00
|
|
|
|
|
|
|
def get_columns():
|
|
|
|
return ["Posting Date:Date:100", "Account:Link/Account:200", "Debit:Float:100",
|
|
|
|
"Credit:Float:100", "Voucher Type::120", "Voucher No::160", "Link::20",
|
2014-01-20 12:58:48 +00:00
|
|
|
"Against Account::120", "Cost Center:Link/Cost Center:100", "Remarks::400"]
|
2013-12-26 05:37:58 +00:00
|
|
|
|
2013-12-27 12:03:55 +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)
|
2013-12-26 05:37:58 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
result = get_result_as_list(data)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_gl_entries(filters):
|
2013-12-27 12:03:55 +00:00
|
|
|
group_by_condition = "group by voucher_type, voucher_no, account" \
|
|
|
|
if filters.get("group_by_voucher") else "group by name"
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
gl_entries = frappe.conn.sql("""select posting_date, account,
|
2013-12-27 12:03:55 +00:00
|
|
|
sum(ifnull(debit, 0)) as debit, sum(ifnull(credit, 0)) as credit,
|
2014-01-22 06:22:54 +00:00
|
|
|
voucher_type, voucher_no, cost_center, remarks, is_opening, against
|
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"""\
|
2013-12-27 12:03:55 +00:00
|
|
|
.format(conditions=get_conditions(filters), group_by_condition=group_by_condition),
|
|
|
|
filters, as_dict=1)
|
2013-12-26 05:37:58 +00:00
|
|
|
|
|
|
|
return gl_entries
|
2013-12-27 12:03:55 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_conditions(filters):
|
|
|
|
conditions = []
|
|
|
|
if filters.get("account"):
|
2014-02-14 10:17:51 +00:00
|
|
|
lft, rgt = frappe.conn.get_value("Account", filters["account"], ["lft", "rgt"])
|
2013-12-26 05:37:58 +00:00
|
|
|
conditions.append("""account in (select name from tabAccount
|
|
|
|
where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))
|
2013-12-27 12:03:55 +00:00
|
|
|
else:
|
|
|
|
conditions.append("posting_date between %(from_date)s and %(to_date)s")
|
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
if filters.get("voucher_no"):
|
|
|
|
conditions.append("voucher_no=%(voucher_no)s")
|
2014-01-09 07:14:44 +00:00
|
|
|
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.widgets.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)
|
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)
|
|
|
|
|
|
|
|
opening, total_debit, total_credit, gle_map = get_accountwise_gle(filters, gl_entries, gle_map)
|
|
|
|
|
|
|
|
# Opening for filtered account
|
|
|
|
if filters.get("account"):
|
|
|
|
data += [get_balance_row("Opening", account_details[filters.account].debit_or_credit,
|
|
|
|
opening), {}]
|
|
|
|
|
|
|
|
for acc, acc_dict in gle_map.items():
|
|
|
|
if acc_dict.entries:
|
|
|
|
# Opening for individual ledger, if grouped by account
|
|
|
|
if filters.get("group_by_account"):
|
|
|
|
data.append(get_balance_row("Opening", account_details[acc].debit_or_credit,
|
|
|
|
acc_dict.opening))
|
|
|
|
|
|
|
|
data += acc_dict.entries
|
|
|
|
|
|
|
|
# Totals and closing for individual ledger, if grouped by account
|
|
|
|
if filters.get("group_by_account"):
|
|
|
|
data += [{"account": "Totals", "debit": acc_dict.total_debit,
|
|
|
|
"credit": acc_dict.total_credit},
|
|
|
|
get_balance_row("Closing (Opening + Totals)",
|
|
|
|
account_details[acc].debit_or_credit, (acc_dict.opening
|
|
|
|
+ acc_dict.total_debit - acc_dict.total_credit)), {}]
|
|
|
|
|
|
|
|
# Total debit and credit between from and to date
|
|
|
|
if total_debit or total_credit:
|
|
|
|
data.append({"account": "Totals", "debit": total_debit, "credit": total_credit})
|
|
|
|
|
|
|
|
# Closing for filtered account
|
|
|
|
if filters.get("account"):
|
|
|
|
data.append(get_balance_row("Closing (Opening + Totals)",
|
|
|
|
account_details[filters.account].debit_or_credit,
|
|
|
|
(opening + total_debit - total_credit)))
|
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,
|
|
|
|
"entries": [],
|
|
|
|
"total_debit": 0,
|
|
|
|
"total_credit": 0,
|
|
|
|
"closing": 0
|
|
|
|
}))
|
|
|
|
return gle_map
|
|
|
|
|
|
|
|
def get_accountwise_gle(filters, gl_entries, gle_map):
|
|
|
|
opening, total_debit, total_credit = 0, 0, 0
|
|
|
|
|
|
|
|
for gle in gl_entries:
|
|
|
|
amount = flt(gle.debit) - flt(gle.credit)
|
|
|
|
if filters.get("account") and (gle.posting_date < filters.from_date
|
2014-01-21 06:28:17 +00:00
|
|
|
or cstr(gle.is_opening) == "Yes"):
|
2013-12-27 12:03:55 +00:00
|
|
|
gle_map[gle.account].opening += amount
|
|
|
|
opening += amount
|
2014-01-03 07:00:24 +00:00
|
|
|
elif gle.posting_date <= filters.to_date:
|
2013-12-27 12:03:55 +00:00
|
|
|
gle_map[gle.account].entries.append(gle)
|
|
|
|
gle_map[gle.account].total_debit += flt(gle.debit)
|
|
|
|
gle_map[gle.account].total_credit += flt(gle.credit)
|
|
|
|
|
|
|
|
total_debit += flt(gle.debit)
|
|
|
|
total_credit += flt(gle.credit)
|
|
|
|
|
|
|
|
return opening, total_debit, total_credit, gle_map
|
|
|
|
|
|
|
|
def get_balance_row(label, debit_or_credit, balance):
|
|
|
|
return {
|
|
|
|
"account": label,
|
|
|
|
"debit": balance if debit_or_credit=="Debit" else 0,
|
|
|
|
"credit": -1*balance if debit_or_credit=="Credit" else 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_result_as_list(data):
|
|
|
|
result = []
|
|
|
|
for d in data:
|
|
|
|
result.append([d.get("posting_date"), d.get("account"), d.get("debit"),
|
|
|
|
d.get("credit"), d.get("voucher_type"), d.get("voucher_no"),
|
|
|
|
get_voucher_link(d.get("voucher_type"), d.get("voucher_no")),
|
|
|
|
d.get("against"), d.get("cost_center"), d.get("remarks")])
|
|
|
|
|
|
|
|
return result
|
2013-12-26 05:37:58 +00:00
|
|
|
|
2013-12-27 12:03:55 +00:00
|
|
|
def get_voucher_link(voucher_type, voucher_no):
|
|
|
|
icon = ""
|
|
|
|
if voucher_type and voucher_no:
|
|
|
|
icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;">
|
|
|
|
</i></a>""" % ("/".join(["#Form", voucher_type, voucher_no]))
|
2013-12-26 05:37:58 +00:00
|
|
|
|
2013-12-31 10:39:22 +00:00
|
|
|
return icon
|