Merge pull request #1671 from nabinhait/v4-hotfix

V4 hotfix
This commit is contained in:
Nabin Hait 2014-05-20 15:25:20 +05:30
commit f5ac91da06
2 changed files with 49 additions and 49 deletions

View File

@ -94,7 +94,8 @@ def validate_account_for_auto_accounting_for_stock(gl_map):
for entry in gl_map: for entry in gl_map:
if entry.account in aii_accounts: if entry.account in aii_accounts:
frappe.throw(_("Account {0} can only be updated via Stock Transactions"), StockAccountInvalidTransaction) frappe.throw(_("Account: {0} can only be updated via \
Stock Transactions").format(entry.account), StockAccountInvalidTransaction)
def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,

View File

@ -10,81 +10,81 @@ def execute(filters=None):
account_details = {} account_details = {}
for acc in frappe.db.sql("""select name, group_or_ledger from tabAccount""", as_dict=1): for acc in frappe.db.sql("""select name, group_or_ledger from tabAccount""", as_dict=1):
account_details.setdefault(acc.name, acc) account_details.setdefault(acc.name, acc)
validate_filters(filters, account_details) validate_filters(filters, account_details)
columns = get_columns() columns = get_columns()
res = get_result(filters, account_details) res = get_result(filters, account_details)
return columns, res return columns, res
def validate_filters(filters, account_details): def validate_filters(filters, account_details):
if filters.get("account") and filters.get("group_by_account") \ if filters.get("account") and filters.get("group_by_account") \
and account_details[filters.account].group_or_ledger == "Ledger": and account_details[filters.account].group_or_ledger == "Ledger":
frappe.throw(_("Can not filter based on Account, if grouped by Account")) frappe.throw(_("Can not filter based on Account, if grouped by Account"))
if filters.get("voucher_no") and filters.get("group_by_voucher"): if filters.get("voucher_no") and filters.get("group_by_voucher"):
frappe.throw(_("Can not filter based on Voucher No, if grouped by Voucher")) frappe.throw(_("Can not filter based on Voucher No, if grouped by Voucher"))
if filters.from_date > filters.to_date: if filters.from_date > filters.to_date:
frappe.throw(_("From Date must be before To Date")) frappe.throw(_("From Date must be before To Date"))
def get_columns(): def get_columns():
return ["Posting Date:Date:100", "Account:Link/Account:200", "Debit:Float:100", return ["Posting Date:Date:100", "Account:Link/Account:200", "Debit:Float:100",
"Credit:Float:100", "Voucher Type::120", "Voucher No::160", "Link::20", "Credit:Float:100", "Voucher Type::120", "Voucher No::160", "Link::20",
"Against Account::120", "Cost Center:Link/Cost Center:100", "Remarks::400"] "Against Account::120", "Cost Center:Link/Cost Center:100", "Remarks::400"]
def get_result(filters, account_details): def get_result(filters, account_details):
gl_entries = get_gl_entries(filters) gl_entries = get_gl_entries(filters)
data = get_data_with_opening_closing(filters, account_details, gl_entries) data = get_data_with_opening_closing(filters, account_details, gl_entries)
result = get_result_as_list(data) result = get_result_as_list(data)
return result return result
def get_gl_entries(filters): def get_gl_entries(filters):
group_by_condition = "group by voucher_type, voucher_no, account" \ group_by_condition = "group by voucher_type, voucher_no, account" \
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, gl_entries = frappe.db.sql("""select posting_date, account,
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, is_opening, against voucher_type, voucher_no, cost_center, remarks, is_opening, against
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(conditions=get_conditions(filters), group_by_condition=group_by_condition), .format(conditions=get_conditions(filters), group_by_condition=group_by_condition),
filters, as_dict=1) filters, as_dict=1)
return gl_entries return gl_entries
def get_conditions(filters): def get_conditions(filters):
conditions = [] conditions = []
if filters.get("account"): if filters.get("account"):
lft, rgt = frappe.db.get_value("Account", filters["account"], ["lft", "rgt"]) lft, rgt = frappe.db.get_value("Account", filters["account"], ["lft", "rgt"])
conditions.append("""account in (select name from tabAccount conditions.append("""account in (select name from tabAccount
where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt)) where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))
else: else:
conditions.append("posting_date between %(from_date)s and %(to_date)s") conditions.append("posting_date between %(from_date)s and %(to_date)s")
if filters.get("voucher_no"): if filters.get("voucher_no"):
conditions.append("voucher_no=%(voucher_no)s") conditions.append("voucher_no=%(voucher_no)s")
from frappe.widgets.reportview import build_match_conditions from frappe.widgets.reportview import build_match_conditions
match_conditions = build_match_conditions("GL Entry") match_conditions = build_match_conditions("GL Entry")
if match_conditions: conditions.append(match_conditions) if match_conditions: conditions.append(match_conditions)
return "and {}".format(" and ".join(conditions)) if conditions else "" return "and {}".format(" and ".join(conditions)) if conditions else ""
def get_data_with_opening_closing(filters, account_details, gl_entries): def get_data_with_opening_closing(filters, account_details, gl_entries):
data = [] data = []
gle_map = initialize_gle_map(gl_entries) gle_map = initialize_gle_map(gl_entries)
opening, total_debit, total_credit, gle_map = get_accountwise_gle(filters, gl_entries, gle_map) opening, total_debit, total_credit, gle_map = get_accountwise_gle(filters, gl_entries, gle_map)
# Opening for filtered account # Opening for filtered account
if filters.get("account"): if filters.get("account"):
data += [get_balance_row("Opening", opening), {}] data += [get_balance_row("Opening", opening), {}]
@ -96,23 +96,23 @@ def get_data_with_opening_closing(filters, account_details, gl_entries):
data.append(get_balance_row("Opening", acc_dict.opening)) data.append(get_balance_row("Opening", acc_dict.opening))
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"):
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)",
(acc_dict.opening + acc_dict.total_debit - acc_dict.total_credit)), {}] (acc_dict.opening + acc_dict.total_debit - acc_dict.total_credit)), {}]
# 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({"account": "Totals", "debit": total_debit, "credit": total_credit}) data.append({"account": "Totals", "debit": total_debit, "credit": total_credit})
# Closing for filtered account # Closing for filtered account
if filters.get("account"): if filters.get("account"):
data.append(get_balance_row("Closing (Opening + Totals)", data.append(get_balance_row("Closing (Opening + Totals)",
(opening + total_debit - total_credit))) (opening + total_debit - total_credit)))
return data return data
def initialize_gle_map(gl_entries): def initialize_gle_map(gl_entries):
@ -129,21 +129,20 @@ 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
for gle in gl_entries: for gle in gl_entries:
amount = flt(gle.debit) - flt(gle.credit) amount = flt(gle.debit) - flt(gle.credit)
if filters.get("account") and (gle.posting_date < filters.from_date if filters.get("account") and (gle.posting_date<filters.from_date or cstr(gle.is_opening)=="Yes"):
or cstr(gle.is_opening) == "Yes"):
gle_map[gle.account].opening += amount gle_map[gle.account].opening += amount
opening += amount opening += amount
elif gle.posting_date <= filters.to_date: elif gle.posting_date <= filters.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) gle_map[gle.account].total_debit += flt(gle.debit)
gle_map[gle.account].total_credit += flt(gle.credit) gle_map[gle.account].total_credit += flt(gle.credit)
total_debit += flt(gle.debit) total_debit += flt(gle.debit)
total_credit += flt(gle.credit) total_credit += flt(gle.credit)
return opening, total_debit, total_credit, gle_map return opening, total_debit, total_credit, gle_map
def get_balance_row(label, balance): def get_balance_row(label, balance):
@ -152,21 +151,21 @@ def get_balance_row(label, balance):
"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,
} }
def get_result_as_list(data): def get_result_as_list(data):
result = [] result = []
for d in data: for d in data:
result.append([d.get("posting_date"), d.get("account"), d.get("debit"), result.append([d.get("posting_date"), d.get("account"), d.get("debit"),
d.get("credit"), d.get("voucher_type"), d.get("voucher_no"), d.get("credit"), d.get("voucher_type"), d.get("voucher_no"),
get_voucher_link(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")]) d.get("against"), d.get("cost_center"), d.get("remarks")])
return result return result
def get_voucher_link(voucher_type, voucher_no): def get_voucher_link(voucher_type, voucher_no):
icon = "" icon = ""
if voucher_type and voucher_no: if voucher_type and voucher_no:
icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"> icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;">
</i></a>""" % ("/".join(["#Form", voucher_type, voucher_no])) </i></a>""" % ("/".join(["#Form", voucher_type, voucher_no]))
return icon return icon