[fix] gl entry report if not filtered

This commit is contained in:
Rushabh Mehta 2015-09-24 15:03:53 +05:30
parent 65b6762247
commit 8f2b8afcb7

View File

@ -12,10 +12,10 @@ def execute(filters=None):
account_details.setdefault(acc.name, acc) account_details.setdefault(acc.name, acc)
validate_filters(filters, account_details) validate_filters(filters, account_details)
validate_party(filters) validate_party(filters)
filters = set_account_currency(filters) filters = set_account_currency(filters)
columns = get_columns(filters) columns = get_columns(filters)
@ -46,49 +46,49 @@ def validate_party(filters):
frappe.throw(_("To filter based on Party, select Party Type first")) frappe.throw(_("To filter based on Party, select Party Type first"))
elif not frappe.db.exists(party_type, party): elif not frappe.db.exists(party_type, party):
frappe.throw(_("Invalid {0}: {1}").format(party_type, party)) frappe.throw(_("Invalid {0}: {1}").format(party_type, party))
def set_account_currency(filters): def set_account_currency(filters):
if not (filters.get("account") or filters.get("party")): if not (filters.get("account") or filters.get("party")):
return filters return filters
else: else:
filters["company_currency"] = frappe.db.get_value("Company", filters.company, "default_currency") filters["company_currency"] = frappe.db.get_value("Company", filters.company, "default_currency")
account_currency = None account_currency = None
if filters.get("account"): if filters.get("account"):
account_currency = frappe.db.get_value("Account", filters.account, "account_currency") account_currency = frappe.db.get_value("Account", filters.account, "account_currency")
elif filters.get("party"): elif filters.get("party"):
gle_currency = frappe.db.get_value("GL Entry", {"party_type": filters.party_type, gle_currency = frappe.db.get_value("GL Entry", {"party_type": filters.party_type,
"party": filters.party, "company": filters.company}, "account_currency") "party": filters.party, "company": filters.company}, "account_currency")
if gle_currency: if gle_currency:
account_currency = gle_currency account_currency = gle_currency
else: else:
account_currency = frappe.db.get_value(filters.party_type, filters.party, "default_currency") account_currency = frappe.db.get_value(filters.party_type, filters.party, "default_currency")
filters["account_currency"] = account_currency or filters.company_currency filters["account_currency"] = account_currency or filters.company_currency
if filters.account_currency != filters.company_currency: if filters.account_currency != filters.company_currency:
filters["show_in_account_currency"] = 1 filters["show_in_account_currency"] = 1
return filters return filters
def get_columns(filters): def get_columns(filters):
columns = [ columns = [
_("Posting Date") + ":Date:90", _("Account") + ":Link/Account:200", _("Posting Date") + ":Date:90", _("Account") + ":Link/Account:200",
_("Debit") + ":Float:100", _("Credit") + ":Float:100" _("Debit") + ":Float:100", _("Credit") + ":Float:100"
] ]
if filters.get("show_in_account_currency"): if filters.get("show_in_account_currency"):
columns += [ columns += [
_("Debit") + " (" + filters.account_currency + ")" + ":Float:100", _("Debit") + " (" + filters.account_currency + ")" + ":Float:100",
_("Credit") + " (" + filters.account_currency + ")" + ":Float:100" _("Credit") + " (" + filters.account_currency + ")" + ":Float:100"
] ]
columns += [ columns += [
_("Voucher Type") + "::120", _("Voucher No") + ":Dynamic Link/Voucher Type:160", _("Voucher Type") + "::120", _("Voucher No") + ":Dynamic Link/Voucher Type:160",
_("Against Account") + "::120", _("Party Type") + "::80", _("Party") + "::150", _("Against Account") + "::120", _("Party Type") + "::80", _("Party") + "::150",
_("Cost Center") + ":Link/Cost Center:100", _("Remarks") + "::400" _("Cost Center") + ":Link/Cost Center:100", _("Remarks") + "::400"
] ]
return columns return columns
def get_result(filters, account_details): def get_result(filters, account_details):
@ -101,21 +101,21 @@ def get_result(filters, account_details):
return result return result
def get_gl_entries(filters): def get_gl_entries(filters):
select_fields = """, sum(ifnull(debit_in_account_currency, 0)) as debit_in_account_currency, select_fields = """, sum(ifnull(debit_in_account_currency, 0)) as debit_in_account_currency,
sum(ifnull(credit_in_account_currency, 0)) as credit_in_account_currency""" \ sum(ifnull(credit_in_account_currency, 0)) as credit_in_account_currency""" \
if filters.get("show_in_account_currency") else "" if filters.get("show_in_account_currency") else ""
group_by_condition = "group by voucher_type, voucher_no, account, cost_center" \ group_by_condition = "group by voucher_type, voucher_no, account, cost_center" \
if filters.get("group_by_voucher") else "group by name" if filters.get("group_by_voucher") else "group by name"
gl_entries = frappe.db.sql("""select posting_date, account, party_type, party, gl_entries = frappe.db.sql("""select posting_date, account, party_type, party,
sum(ifnull(debit, 0)) as debit, sum(ifnull(credit, 0)) as credit, sum(ifnull(debit, 0)) as debit, sum(ifnull(credit, 0)) as credit,
voucher_type, voucher_no, cost_center, remarks, against, is_opening {select_fields} voucher_type, voucher_no, cost_center, remarks, against, is_opening {select_fields}
from `tabGL Entry` from `tabGL Entry`
where company=%(company)s {conditions} where company=%(company)s {conditions}
{group_by_condition} {group_by_condition}
order by posting_date, account"""\ order by posting_date, account"""\
.format(select_fields=select_fields, conditions=get_conditions(filters), .format(select_fields=select_fields, conditions=get_conditions(filters),
group_by_condition=group_by_condition), filters, as_dict=1) group_by_condition=group_by_condition), filters, as_dict=1)
return gl_entries return gl_entries
@ -135,7 +135,7 @@ def get_conditions(filters):
if filters.get("party"): if filters.get("party"):
conditions.append("party=%(party)s") conditions.append("party=%(party)s")
if not (filters.get("account") or filters.get("party") or filters.get("group_by_account")): if not (filters.get("account") or filters.get("party") or filters.get("group_by_account")):
conditions.append("posting_date >=%(from_date)s") conditions.append("posting_date >=%(from_date)s")
@ -156,33 +156,52 @@ def get_data_with_opening_closing(filters, account_details, gl_entries):
if filters.get("account") or filters.get("party"): if filters.get("account") or filters.get("party"):
data += [get_balance_row(_("Opening"), opening, opening_in_account_currency), {}] data += [get_balance_row(_("Opening"), opening, opening_in_account_currency), {}]
for acc, acc_dict in gle_map.items(): if filters.get("group_by_account"):
if acc_dict.entries: for acc, acc_dict in gle_map.items():
# Opening for individual ledger, if grouped by account if acc_dict.entries:
if filters.get("group_by_account"): # Opening for individual ledger, if grouped by account
data.append(get_balance_row(_("Opening"), acc_dict.opening, if filters.get("group_by_account"):
acc_dict.opening_in_account_currency)) data.append(get_balance_row(_("Opening"), acc_dict.opening,
acc_dict.opening_in_account_currency))
data += acc_dict.entries data += acc_dict.entries
# Totals and closing for individual ledger, if grouped by account # Totals and closing for individual ledger, if grouped by account
if filters.get("group_by_account"): if filters.get("group_by_account"):
account_closing = acc_dict.opening + acc_dict.total_debit - acc_dict.total_credit account_closing = acc_dict.opening + acc_dict.total_debit - acc_dict.total_credit
account_closing_in_account_currency = acc_dict.opening_in_account_currency \ 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 + acc_dict.total_debit_in_account_currency - acc_dict.total_credit_in_account_currency
data += [{"account": "'" + _("Totals") + "'", "debit": acc_dict.total_debit, data += [{"account": "'" + _("Totals") + "'", "debit": acc_dict.total_debit,
"credit": acc_dict.total_credit}, "credit": acc_dict.total_credit},
get_balance_row(_("Closing (Opening + Totals)"), get_balance_row(_("Closing (Opening + Totals)"),
account_closing, account_closing_in_account_currency), {}] account_closing, account_closing_in_account_currency), {}]
else:
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
opening_debit = opening_credit = 0.0
for gl in gl_entries:
if gl.posting_date < from_date:
opening_debit += flt(gl.debit, 3)
opening_credit += flt(gl.credit, 3)
else:
data.append(gl)
if not (filters.get("account") or filters.get("party")):
data = [{
"account": "'" + _("Opening") + "'",
"debit": opening_debit,
"credit": opening_credit
}] + data
# Total debit and credit between from and to date # Total debit and credit between from and to date
if total_debit or total_credit: if total_debit or total_credit:
data.append({ data.append({
"account": "'" + _("Totals") + "'", "account": "'" + _("Totals") + "'",
"debit": total_debit, "debit": total_debit,
"credit": total_credit, "credit": total_credit,
"debit_in_account_currency": total_debit_in_account_currency, "debit_in_account_currency": total_debit_in_account_currency,
"credit_in_account_currency": total_credit_in_account_currency "credit_in_account_currency": total_credit_in_account_currency
}) })
@ -191,7 +210,7 @@ def get_data_with_opening_closing(filters, account_details, gl_entries):
closing = opening + total_debit - total_credit closing = opening + total_debit - total_credit
closing_in_account_currency = opening_in_account_currency + \ closing_in_account_currency = opening_in_account_currency + \
total_debit_in_account_currency - total_credit_in_account_currency total_debit_in_account_currency - total_credit_in_account_currency
data.append(get_balance_row(_("Closing (Opening + Totals)"), data.append(get_balance_row(_("Closing (Opening + Totals)"),
closing, closing_in_account_currency)) closing, closing_in_account_currency))
@ -216,38 +235,38 @@ def initialize_gle_map(gl_entries):
def get_accountwise_gle(filters, gl_entries, gle_map): def get_accountwise_gle(filters, gl_entries, gle_map):
opening, total_debit, total_credit = 0, 0, 0 opening, total_debit, total_credit = 0, 0, 0
opening_in_account_currency, total_debit_in_account_currency, total_credit_in_account_currency = 0, 0, 0 opening_in_account_currency, total_debit_in_account_currency, total_credit_in_account_currency = 0, 0, 0
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date) from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
for gle in gl_entries: for gle in gl_entries:
amount = flt(gle.debit, 3) - flt(gle.credit, 3) amount = flt(gle.debit, 3) - flt(gle.credit, 3)
amount_in_account_currency = flt(gle.debit_in_account_currency, 3) - flt(gle.credit_in_account_currency, 3) amount_in_account_currency = flt(gle.debit_in_account_currency, 3) - flt(gle.credit_in_account_currency, 3)
if (filters.get("account") or filters.get("party") or filters.get("group_by_account")) \ if (filters.get("account") or filters.get("party") or filters.get("group_by_account")) \
and (gle.posting_date < from_date or cstr(gle.is_opening) == "Yes"): and (gle.posting_date < from_date or cstr(gle.is_opening) == "Yes"):
gle_map[gle.account].opening += amount gle_map[gle.account].opening += amount
if filters.get("show_in_account_currency"): if filters.get("show_in_account_currency"):
gle_map[gle.account].opening_in_account_currency += amount_in_account_currency gle_map[gle.account].opening_in_account_currency += amount_in_account_currency
if filters.get("account") or filters.get("party"): if filters.get("account") or filters.get("party"):
opening += amount opening += amount
if filters.get("show_in_account_currency"): if filters.get("show_in_account_currency"):
opening_in_account_currency += amount_in_account_currency opening_in_account_currency += amount_in_account_currency
elif gle.posting_date <= to_date: elif gle.posting_date <= to_date:
gle_map[gle.account].entries.append(gle) gle_map[gle.account].entries.append(gle)
gle_map[gle.account].total_debit += flt(gle.debit, 3) gle_map[gle.account].total_debit += flt(gle.debit, 3)
gle_map[gle.account].total_credit += flt(gle.credit, 3) gle_map[gle.account].total_credit += flt(gle.credit, 3)
total_debit += flt(gle.debit, 3) total_debit += flt(gle.debit, 3)
total_credit += flt(gle.credit, 3) total_credit += flt(gle.credit, 3)
if filters.get("show_in_account_currency"): 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_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) gle_map[gle.account].total_credit_in_account_currency += flt(gle.credit_in_account_currency, 3)
total_debit_in_account_currency += flt(gle.debit_in_account_currency, 3) total_debit_in_account_currency += flt(gle.debit_in_account_currency, 3)
total_credit_in_account_currency += flt(gle.credit_in_account_currency, 3) total_credit_in_account_currency += flt(gle.credit_in_account_currency, 3)
return opening, total_debit, total_credit, opening_in_account_currency, \ return opening, total_debit, total_credit, opening_in_account_currency, \
total_debit_in_account_currency, total_credit_in_account_currency, gle_map total_debit_in_account_currency, total_credit_in_account_currency, gle_map
@ -258,27 +277,27 @@ def get_balance_row(label, balance, balance_in_account_currency=None):
"debit": balance if balance > 0 else 0, "debit": balance if balance > 0 else 0,
"credit": -1*balance if balance < 0 else 0 "credit": -1*balance if balance < 0 else 0
} }
if balance_in_account_currency != None: if balance_in_account_currency != None:
balance_row.update({ balance_row.update({
"debit_in_account_currency": balance_in_account_currency if balance_in_account_currency > 0 else 0, "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 "credit_in_account_currency": -1*balance_in_account_currency if balance_in_account_currency < 0 else 0
}) })
return balance_row return balance_row
def get_result_as_list(data, filters): def get_result_as_list(data, filters):
result = [] result = []
for d in data: for d in data:
row = [d.get("posting_date"), d.get("account"), d.get("debit"), d.get("credit")] row = [d.get("posting_date"), d.get("account"), d.get("debit"), d.get("credit")]
if filters.get("show_in_account_currency"): if filters.get("show_in_account_currency"):
row += [d.get("debit_in_account_currency"), d.get("credit_in_account_currency")] row += [d.get("debit_in_account_currency"), d.get("credit_in_account_currency")]
row += [d.get("voucher_type"), d.get("voucher_no"), d.get("against"), row += [d.get("voucher_type"), d.get("voucher_no"), d.get("against"),
d.get("party_type"), d.get("party"), d.get("cost_center"), d.get("remarks") d.get("party_type"), d.get("party"), d.get("cost_center"), d.get("remarks")
] ]
result.append(row) result.append(row)
return result return result