2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-07-18 12:35:26 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
2016-02-02 13:35:30 +00:00
|
|
|
from frappe import _
|
|
|
|
from frappe.utils import (flt, getdate, get_first_day, get_last_day,
|
2014-07-21 10:43:06 +00:00
|
|
|
add_months, add_days, formatdate)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def get_period_list(fiscal_year, periodicity):
|
|
|
|
"""Get a list of dict {"from_date": from_date, "to_date": to_date, "key": key, "label": label}
|
2014-07-18 12:35:26 +00:00
|
|
|
Periodicity can be (Yearly, Quarterly, Monthly)"""
|
|
|
|
|
2014-08-06 10:17:55 +00:00
|
|
|
fy_start_end_date = frappe.db.get_value("Fiscal Year", fiscal_year, ["year_start_date", "year_end_date"])
|
|
|
|
if not fy_start_end_date:
|
|
|
|
frappe.throw(_("Fiscal Year {0} not found.").format(fiscal_year))
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
# start with first day, so as to avoid year to_dates like 2-April if ever they occur]
|
|
|
|
year_start_date = get_first_day(getdate(fy_start_end_date[0]))
|
|
|
|
year_end_date = getdate(fy_start_end_date[1])
|
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
if periodicity == "Yearly":
|
2016-02-02 13:35:30 +00:00
|
|
|
period_list = [frappe._dict({"from_date": year_start_date, "to_date": year_end_date,
|
|
|
|
"key": fiscal_year, "label": fiscal_year})]
|
2014-07-18 12:35:26 +00:00
|
|
|
else:
|
|
|
|
months_to_add = {
|
2016-02-01 01:43:45 +00:00
|
|
|
"Half-Yearly": 6,
|
2014-07-18 12:35:26 +00:00
|
|
|
"Quarterly": 3,
|
|
|
|
"Monthly": 1
|
|
|
|
}[periodicity]
|
|
|
|
|
|
|
|
period_list = []
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
start_date = year_start_date
|
2014-07-18 12:35:26 +00:00
|
|
|
for i in xrange(12 / months_to_add):
|
2016-02-02 13:35:30 +00:00
|
|
|
period = frappe._dict({
|
|
|
|
"from_date": start_date
|
|
|
|
})
|
|
|
|
to_date = add_months(start_date, months_to_add)
|
|
|
|
start_date = to_date
|
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
if to_date == get_first_day(to_date):
|
|
|
|
# if to_date is the first day, get the last day of previous month
|
|
|
|
to_date = add_days(to_date, -1)
|
|
|
|
else:
|
|
|
|
# to_date should be the last day of the new to_date's month
|
|
|
|
to_date = get_last_day(to_date)
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
if to_date <= year_end_date:
|
2014-07-18 12:35:26 +00:00
|
|
|
# the normal case
|
2016-02-02 13:35:30 +00:00
|
|
|
period.to_date = to_date
|
2014-07-18 12:35:26 +00:00
|
|
|
else:
|
|
|
|
# if a fiscal year ends before a 12 month period
|
2016-02-02 13:35:30 +00:00
|
|
|
period.to_date = year_end_date
|
|
|
|
|
|
|
|
period_list.append(period)
|
|
|
|
|
|
|
|
if period.to_date == year_end_date:
|
2014-07-18 12:35:26 +00:00
|
|
|
break
|
2016-02-02 13:35:30 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
# common processing
|
|
|
|
for opts in period_list:
|
2014-07-21 10:43:06 +00:00
|
|
|
key = opts["to_date"].strftime("%b_%Y").lower()
|
2016-02-01 01:43:45 +00:00
|
|
|
if periodicity == "Monthly":
|
|
|
|
label = formatdate(opts["to_date"], "MMM YYYY")
|
|
|
|
else:
|
2016-02-02 13:35:30 +00:00
|
|
|
label = get_label(periodicity, opts["from_date"], opts["to_date"])
|
|
|
|
|
2014-07-21 10:43:06 +00:00
|
|
|
opts.update({
|
|
|
|
"key": key.replace(" ", "_").replace("-", "_"),
|
|
|
|
"label": label,
|
2016-02-02 13:35:30 +00:00
|
|
|
"year_start_date": year_start_date,
|
|
|
|
"year_end_date": year_end_date
|
2014-07-21 10:43:06 +00:00
|
|
|
})
|
2014-07-18 12:35:26 +00:00
|
|
|
|
|
|
|
return period_list
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def get_label(periodicity, from_date, to_date):
|
2016-02-01 01:43:45 +00:00
|
|
|
if periodicity=="Yearly":
|
2016-02-02 13:35:30 +00:00
|
|
|
if formatdate(from_date, "YYYY") == formatdate(to_date, "YYYY"):
|
|
|
|
label = formatdate(from_date, "YYYY")
|
|
|
|
else:
|
|
|
|
label = formatdate(from_date, "YYYY") + "-" + formatdate(to_date, "YYYY")
|
2016-02-01 01:43:45 +00:00
|
|
|
else:
|
2016-02-02 13:35:30 +00:00
|
|
|
label = formatdate(from_date, "MMM YY") + "-" + formatdate(to_date, "MMM YY")
|
2016-02-01 01:43:45 +00:00
|
|
|
|
|
|
|
return label
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def get_data(company, root_type, balance_must_be, period_list,
|
|
|
|
accumulated_values=1, only_current_fiscal_year=True, ignore_closing_entries=False):
|
2014-07-18 12:35:26 +00:00
|
|
|
accounts = get_accounts(company, root_type)
|
|
|
|
if not accounts:
|
|
|
|
return None
|
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
accounts, accounts_by_name, parent_children_map = filter_accounts(accounts)
|
2016-01-12 06:26:31 +00:00
|
|
|
|
|
|
|
company_currency = frappe.db.get_value("Company", company, "default_currency")
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-11-30 12:23:08 +00:00
|
|
|
gl_entries_by_account = {}
|
2015-12-01 12:47:18 +00:00
|
|
|
for root in frappe.db.sql("""select lft, rgt from tabAccount
|
2015-11-30 12:23:08 +00:00
|
|
|
where root_type=%s and ifnull(parent_account, '') = ''""", root_type, as_dict=1):
|
2016-02-02 13:35:30 +00:00
|
|
|
|
|
|
|
set_gl_entries_by_account(company,
|
|
|
|
period_list[0]["year_start_date"] if only_current_fiscal_year else None,
|
|
|
|
period_list[-1]["to_date"],
|
|
|
|
root.lft, root.rgt,
|
|
|
|
gl_entries_by_account, ignore_closing_entries=ignore_closing_entries)
|
|
|
|
|
|
|
|
calculate_values(accounts_by_name, gl_entries_by_account, period_list, accumulated_values)
|
|
|
|
accumulate_values_into_parents(accounts, accounts_by_name, period_list, accumulated_values)
|
2016-01-12 06:26:31 +00:00
|
|
|
out = prepare_data(accounts, balance_must_be, period_list, company_currency)
|
2016-03-04 07:02:33 +00:00
|
|
|
out = filter_out_zero_value_rows(out, parent_children_map)
|
2016-02-02 13:35:30 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
if out:
|
2016-01-12 06:26:31 +00:00
|
|
|
add_total_row(out, balance_must_be, period_list, company_currency)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def calculate_values(accounts_by_name, gl_entries_by_account, period_list, accumulated_values):
|
2015-06-14 12:19:29 +00:00
|
|
|
for entries in gl_entries_by_account.values():
|
|
|
|
for entry in entries:
|
|
|
|
d = accounts_by_name.get(entry.account)
|
|
|
|
for period in period_list:
|
|
|
|
# check if posting date is within the period
|
|
|
|
if entry.posting_date <= period.to_date:
|
2016-02-02 13:35:30 +00:00
|
|
|
if accumulated_values or entry.posting_date >= period.from_date:
|
|
|
|
d[period.key] = d.get(period.key, 0.0) + flt(entry.debit) - flt(entry.credit)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def accumulate_values_into_parents(accounts, accounts_by_name, period_list, accumulated_values):
|
2014-07-18 12:35:26 +00:00
|
|
|
"""accumulate children's values in parent accounts"""
|
|
|
|
for d in reversed(accounts):
|
|
|
|
if d.parent_account:
|
|
|
|
for period in period_list:
|
|
|
|
accounts_by_name[d.parent_account][period.key] = accounts_by_name[d.parent_account].get(period.key, 0.0) + \
|
|
|
|
d.get(period.key, 0.0)
|
|
|
|
|
2016-01-12 06:26:31 +00:00
|
|
|
def prepare_data(accounts, balance_must_be, period_list, company_currency):
|
2016-03-04 07:02:33 +00:00
|
|
|
data = []
|
2014-07-21 10:43:06 +00:00
|
|
|
year_start_date = period_list[0]["year_start_date"].strftime("%Y-%m-%d")
|
|
|
|
year_end_date = period_list[-1]["year_end_date"].strftime("%Y-%m-%d")
|
2016-03-04 07:02:33 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
for d in accounts:
|
|
|
|
# add to output
|
|
|
|
has_value = False
|
2016-02-02 13:35:30 +00:00
|
|
|
total = 0
|
2016-03-04 07:02:33 +00:00
|
|
|
row = frappe._dict({
|
2014-07-18 12:35:26 +00:00
|
|
|
"account_name": d.account_name,
|
|
|
|
"account": d.name,
|
|
|
|
"parent_account": d.parent_account,
|
2014-07-21 10:43:06 +00:00
|
|
|
"indent": flt(d.indent),
|
2016-02-02 13:35:30 +00:00
|
|
|
"year_start_date": year_start_date,
|
|
|
|
"year_end_date": year_end_date,
|
2016-01-12 06:26:31 +00:00
|
|
|
"currency": company_currency
|
2016-03-04 07:02:33 +00:00
|
|
|
})
|
2014-07-18 12:35:26 +00:00
|
|
|
for period in period_list:
|
|
|
|
if d.get(period.key):
|
|
|
|
# change sign based on Debit or Credit, since calculation is done using (debit - credit)
|
|
|
|
d[period.key] *= (1 if balance_must_be=="Debit" else -1)
|
|
|
|
|
|
|
|
row[period.key] = flt(d.get(period.key, 0.0), 3)
|
|
|
|
|
2014-07-18 12:50:44 +00:00
|
|
|
if abs(row[period.key]) >= 0.005:
|
|
|
|
# ignore zero values
|
|
|
|
has_value = True
|
2016-02-02 13:35:30 +00:00
|
|
|
total += flt(row[period.key])
|
2014-07-18 12:50:44 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
row["has_value"] = has_value
|
|
|
|
row["total"] = total
|
|
|
|
data.append(row)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2016-03-10 06:31:01 +00:00
|
|
|
def filter_out_zero_value_rows(data, parent_children_map, show_zero_values=False):
|
2016-03-04 07:02:33 +00:00
|
|
|
data_with_value = []
|
|
|
|
for d in data:
|
2016-03-10 06:31:01 +00:00
|
|
|
if show_zero_values or d.get("has_value"):
|
2016-03-04 07:02:33 +00:00
|
|
|
data_with_value.append(d)
|
|
|
|
else:
|
2016-03-10 06:31:01 +00:00
|
|
|
# show group with zero balance, if there are balances against child
|
|
|
|
children = [child.name for child in parent_children_map.get(d.get("account")) or []]
|
|
|
|
if children:
|
|
|
|
for row in data:
|
|
|
|
if row.get("account") in children and row.get("has_value"):
|
|
|
|
data_with_value.append(d)
|
|
|
|
break
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
return data_with_value
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-01-12 06:26:31 +00:00
|
|
|
def add_total_row(out, balance_must_be, period_list, company_currency):
|
2015-11-30 12:23:08 +00:00
|
|
|
total_row = {
|
2015-05-21 08:08:03 +00:00
|
|
|
"account_name": "'" + _("Total ({0})").format(balance_must_be) + "'",
|
2016-01-12 06:26:31 +00:00
|
|
|
"account": None,
|
|
|
|
"currency": company_currency
|
2014-07-18 12:35:26 +00:00
|
|
|
}
|
2015-12-01 12:47:18 +00:00
|
|
|
|
2015-11-30 12:23:08 +00:00
|
|
|
for row in out:
|
|
|
|
if not row.get("parent_account"):
|
|
|
|
for period in period_list:
|
|
|
|
total_row.setdefault(period.key, 0.0)
|
|
|
|
total_row[period.key] += row.get(period.key, 0.0)
|
2016-02-02 13:35:30 +00:00
|
|
|
row[period.key] = ""
|
|
|
|
|
|
|
|
total_row.setdefault("total", 0.0)
|
|
|
|
total_row["total"] += flt(row["total"])
|
|
|
|
row["total"] = ""
|
2016-02-01 01:43:45 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
if total_row.has_key("total"):
|
|
|
|
out.append(total_row)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
# blank row after Total
|
|
|
|
out.append({})
|
2014-07-18 12:35:26 +00:00
|
|
|
|
|
|
|
def get_accounts(company, root_type):
|
2015-06-14 12:19:29 +00:00
|
|
|
return frappe.db.sql("""select name, parent_account, lft, rgt, root_type, report_type, account_name from `tabAccount`
|
|
|
|
where company=%s and root_type=%s order by lft""", (company, root_type), as_dict=True)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2014-07-22 13:32:11 +00:00
|
|
|
def filter_accounts(accounts, depth=10):
|
2014-07-18 12:35:26 +00:00
|
|
|
parent_children_map = {}
|
|
|
|
accounts_by_name = {}
|
|
|
|
for d in accounts:
|
|
|
|
accounts_by_name[d.name] = d
|
|
|
|
parent_children_map.setdefault(d.parent_account or None, []).append(d)
|
|
|
|
|
|
|
|
filtered_accounts = []
|
2015-04-29 06:55:18 +00:00
|
|
|
|
2014-07-18 12:35:26 +00:00
|
|
|
def add_to_list(parent, level):
|
|
|
|
if level < depth:
|
2015-04-29 07:37:36 +00:00
|
|
|
children = parent_children_map.get(parent) or []
|
|
|
|
if parent == None:
|
|
|
|
sort_root_accounts(children)
|
2015-04-29 06:55:18 +00:00
|
|
|
|
2015-04-29 07:37:36 +00:00
|
|
|
for child in children:
|
2014-07-18 12:35:26 +00:00
|
|
|
child.indent = level
|
|
|
|
filtered_accounts.append(child)
|
|
|
|
add_to_list(child.name, level + 1)
|
|
|
|
|
|
|
|
add_to_list(None, 0)
|
|
|
|
|
2016-03-04 07:02:33 +00:00
|
|
|
return filtered_accounts, accounts_by_name, parent_children_map
|
2015-05-20 11:51:53 +00:00
|
|
|
|
2015-04-29 07:37:36 +00:00
|
|
|
def sort_root_accounts(roots):
|
|
|
|
"""Sort root types as Asset, Liability, Equity, Income, Expense"""
|
|
|
|
|
|
|
|
def compare_roots(a, b):
|
|
|
|
if a.report_type != b.report_type and a.report_type == "Balance Sheet":
|
|
|
|
return -1
|
|
|
|
if a.root_type != b.root_type and a.root_type == "Asset":
|
|
|
|
return -1
|
|
|
|
if a.root_type == "Liability" and b.root_type == "Equity":
|
|
|
|
return -1
|
|
|
|
if a.root_type == "Income" and b.root_type == "Expense":
|
|
|
|
return -1
|
|
|
|
return 1
|
|
|
|
|
|
|
|
roots.sort(compare_roots)
|
2014-07-18 12:35:26 +00:00
|
|
|
|
2015-12-01 12:47:18 +00:00
|
|
|
def set_gl_entries_by_account(company, from_date, to_date, root_lft, root_rgt, gl_entries_by_account,
|
2015-11-30 12:23:08 +00:00
|
|
|
ignore_closing_entries=False):
|
2014-07-18 12:35:26 +00:00
|
|
|
"""Returns a dict like { "account": [gl entries], ... }"""
|
|
|
|
additional_conditions = []
|
|
|
|
|
2014-07-21 10:43:06 +00:00
|
|
|
if ignore_closing_entries:
|
|
|
|
additional_conditions.append("and ifnull(voucher_type, '')!='Period Closing Voucher'")
|
2014-07-18 12:35:26 +00:00
|
|
|
|
|
|
|
if from_date:
|
|
|
|
additional_conditions.append("and posting_date >= %(from_date)s")
|
|
|
|
|
2015-06-17 10:05:06 +00:00
|
|
|
gl_entries = frappe.db.sql("""select posting_date, account, debit, credit, is_opening from `tabGL Entry`
|
2014-07-18 12:35:26 +00:00
|
|
|
where company=%(company)s
|
|
|
|
{additional_conditions}
|
|
|
|
and posting_date <= %(to_date)s
|
|
|
|
and account in (select name from `tabAccount`
|
|
|
|
where lft >= %(lft)s and rgt <= %(rgt)s)
|
|
|
|
order by account, posting_date""".format(additional_conditions="\n".join(additional_conditions)),
|
|
|
|
{
|
|
|
|
"company": company,
|
|
|
|
"from_date": from_date,
|
|
|
|
"to_date": to_date,
|
|
|
|
"lft": root_lft,
|
|
|
|
"rgt": root_rgt
|
|
|
|
},
|
|
|
|
as_dict=True)
|
|
|
|
|
|
|
|
for entry in gl_entries:
|
|
|
|
gl_entries_by_account.setdefault(entry.account, []).append(entry)
|
|
|
|
|
|
|
|
return gl_entries_by_account
|
|
|
|
|
2016-02-02 13:35:30 +00:00
|
|
|
def get_columns(periodicity, period_list, accumulated_values=1, company=None):
|
2014-07-18 12:35:26 +00:00
|
|
|
columns = [{
|
|
|
|
"fieldname": "account",
|
|
|
|
"label": _("Account"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Account",
|
|
|
|
"width": 300
|
|
|
|
}]
|
2016-02-02 13:35:30 +00:00
|
|
|
if company:
|
|
|
|
columns.append({
|
|
|
|
"fieldname": "currency",
|
|
|
|
"label": _("Currency"),
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Currency",
|
|
|
|
"hidden": 1
|
|
|
|
})
|
2014-07-18 12:35:26 +00:00
|
|
|
for period in period_list:
|
|
|
|
columns.append({
|
|
|
|
"fieldname": period.key,
|
|
|
|
"label": period.label,
|
|
|
|
"fieldtype": "Currency",
|
2016-02-02 13:35:30 +00:00
|
|
|
"options": "currency",
|
2014-07-18 12:35:26 +00:00
|
|
|
"width": 150
|
|
|
|
})
|
2016-02-01 01:43:45 +00:00
|
|
|
if periodicity!="Yearly":
|
2016-02-02 13:35:30 +00:00
|
|
|
if not accumulated_values:
|
2016-02-01 01:43:45 +00:00
|
|
|
columns.append({
|
|
|
|
"fieldname": "total",
|
|
|
|
"label": _("Total"),
|
|
|
|
"fieldtype": "Currency",
|
|
|
|
"width": 150
|
|
|
|
})
|
|
|
|
|
|
|
|
return columns
|