brotherton-erpnext/erpnext/accounts/report/profitability_analysis/profitability_analysis.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

225 lines
6.4 KiB
Python
Raw Normal View History

2016-08-24 10:52:30 +00:00
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
2016-08-24 10:52:30 +00:00
import frappe
from frappe import _
from frappe.utils import cstr, flt
2016-08-24 12:26:09 +00:00
from erpnext.accounts.report.financial_statements import (
filter_accounts,
filter_out_zero_value_rows,
)
2016-08-24 10:52:30 +00:00
from erpnext.accounts.report.trial_balance.trial_balance import validate_filters
2016-08-26 07:09:54 +00:00
value_fields = ("income", "expense", "gross_profit_loss")
2016-08-24 10:52:30 +00:00
def execute(filters=None):
2017-02-08 05:50:04 +00:00
if not filters.get('based_on'): filters["based_on"] = 'Cost Center'
2016-08-24 12:26:09 +00:00
based_on = filters.based_on.replace(' ', '_').lower()
2016-08-24 10:52:30 +00:00
validate_filters(filters)
2017-02-08 05:50:04 +00:00
accounts = get_accounts_data(based_on, filters.get("company"))
2016-08-24 12:26:09 +00:00
data = get_data(accounts, filters, based_on)
columns = get_columns(filters)
2016-08-24 10:52:30 +00:00
return columns, data
2016-08-24 12:26:09 +00:00
def get_accounts_data(based_on, company):
if based_on == 'cost_center':
return frappe.db.sql("""select name, parent_cost_center as parent_account, cost_center_name as account_name, lft, rgt
from `tabCost Center` where company=%s order by name""", company, as_dict=True)
elif based_on == 'project':
return frappe.get_all('Project', fields = ["name"], filters = {'company': company}, order_by = 'name')
else:
filters = {}
doctype = frappe.unscrub(based_on)
has_company = frappe.db.has_column(doctype, 'company')
if has_company:
filters.update({'company': company})
return frappe.get_all(doctype, fields = ["name"], filters = filters, order_by = 'name')
2016-08-24 10:52:30 +00:00
2016-08-24 12:26:09 +00:00
def get_data(accounts, filters, based_on):
2016-08-24 10:52:30 +00:00
if not accounts:
2017-02-08 05:50:04 +00:00
return []
2016-08-24 10:52:30 +00:00
accounts, accounts_by_name, parent_children_map = filter_accounts(accounts)
gl_entries_by_account = {}
2017-02-08 05:50:04 +00:00
set_gl_entries_by_account(filters.get("company"), filters.get("from_date"),
filters.get("to_date"), based_on, gl_entries_by_account, ignore_closing_entries=not flt(filters.get("with_period_closing_entry")))
2016-08-24 10:52:30 +00:00
total_row = calculate_values(accounts, gl_entries_by_account, filters)
accumulate_values_into_parents(accounts, accounts_by_name)
data = prepare_data(accounts, filters, total_row, parent_children_map, based_on)
data = filter_out_zero_value_rows(data, parent_children_map,
2016-08-24 12:26:09 +00:00
show_zero_values=filters.get("show_zero_values"))
2016-08-24 10:52:30 +00:00
return data
def calculate_values(accounts, gl_entries_by_account, filters):
init = {
"income": 0.0,
"expense": 0.0,
2016-08-26 07:09:54 +00:00
"gross_profit_loss": 0.0
2016-08-24 10:52:30 +00:00
}
total_row = {
"cost_center": None,
"account_name": "'" + _("Total") + "'",
"warn_if_negative": True,
"income": 0.0,
"expense": 0.0,
2017-03-28 04:49:11 +00:00
"gross_profit_loss": 0.0,
"account": "'" + _("Total") + "'",
"parent_account": None,
"indent": 0,
"has_value": True
2016-08-24 10:52:30 +00:00
}
for d in accounts:
d.update(init.copy())
# add opening
for entry in gl_entries_by_account.get(d.name, []):
if cstr(entry.is_opening) != "Yes":
if entry.type == 'Income':
d["income"] += flt(entry.credit) - flt(entry.debit)
if entry.type == 'Expense':
d["expense"] += flt(entry.debit) - flt(entry.credit)
2016-08-26 07:09:54 +00:00
d["gross_profit_loss"] = d.get("income") - d.get("expense")
2016-08-24 10:52:30 +00:00
total_row["income"] += d["income"]
total_row["expense"] += d["expense"]
2016-08-26 07:09:54 +00:00
total_row["gross_profit_loss"] = total_row.get("income") - total_row.get("expense")
2016-08-24 10:52:30 +00:00
return total_row
def accumulate_values_into_parents(accounts, accounts_by_name):
for d in reversed(accounts):
if d.parent_account:
for key in value_fields:
accounts_by_name[d.parent_account][key] += d[key]
def prepare_data(accounts, filters, total_row, parent_children_map, based_on):
2016-08-24 10:52:30 +00:00
data = []
feat(Cost Center): Distributed Cost Center (#21531) * feat: Distributed Cost Center Squashed commit of the following: commit 274236eb92dfddfd015583ee1e6b5e1937366d28 Author: Vignesh <kaviya.3111999@gmail.com> Date: Mon Mar 23 18:32:26 2020 +0530 Fix: Indentation commit beae0b9849f333dd1d9f29c27192ea7de2e4e809 Author: Vignesh <kaviya.3111999@gmail.com> Date: Mon Mar 23 18:26:50 2020 +0530 The validations are added to distributed cost center. commit 3f66943adacb5797338ef3510251bf6633be3dba Author: Vignesh <kaviya.3111999@gmail.com> Date: Thu Mar 19 22:17:25 2020 +0530 The distributed cost centers are shown with the percentage allocation in the reports commit 1ac11df522096fa187d2f5863b4c970e18b37fac Author: Vignesh <kaviya.3111999@gmail.com> Date: Thu Mar 19 13:36:04 2020 +0530 The child table distributed cost center are added to cost center doctype and then validate percentage allocation. * fix(Distributed Cost Center): validation and filters * fix(Distributed Cost Center): financial statement query * fix(Distributed Cost Center): add test cases Co-authored-by: Vignesh S <svignesh0308@gmail.com> * fix(Distributed Cost Center): budget variance, general ledger, profitability analysis reports. * Merge branch 'develop' into develop-distributed-cost-center * fix(Distributed Cost Center): Added DCC reflection in actual amount column of Budget Variance Report. * Update creation field in general ledger report Co-authored-by: Nabin Hait <nabinhait@gmail.com> * creation field updated in gl_entries query Co-authored-by: Nabin Hait <nabinhait@gmail.com> Co-authored-by: KaviyaPeriyasamy <kaviya.3111999@gmail.com> Co-authored-by: Vignesh S <svignesh0308@gmail.com> Co-authored-by: KaviyaPeriyasamy <kaviyaperiyasamy22@gmail.com> Co-authored-by: Kaviya Periyasamy <36359901+KaviyaPeriyasamy@users.noreply.github.com> Co-authored-by: Nabin Hait <nabinhait@gmail.com>
2020-06-08 06:50:21 +00:00
new_accounts = accounts
2018-08-08 11:07:31 +00:00
company_currency = frappe.get_cached_value('Company', filters.get("company"), "default_currency")
2016-08-24 10:52:30 +00:00
for d in accounts:
has_value = False
row = {
2016-08-24 12:26:09 +00:00
"account_name": d.account_name or d.name,
2016-08-24 10:52:30 +00:00
"account": d.name,
"parent_account": d.parent_account,
"indent": d.indent,
2017-02-08 05:50:04 +00:00
"fiscal_year": filters.get("fiscal_year"),
2016-08-24 12:26:09 +00:00
"currency": company_currency,
"based_on": based_on
2016-08-24 10:52:30 +00:00
}
feat(Cost Center): Distributed Cost Center (#21531) * feat: Distributed Cost Center Squashed commit of the following: commit 274236eb92dfddfd015583ee1e6b5e1937366d28 Author: Vignesh <kaviya.3111999@gmail.com> Date: Mon Mar 23 18:32:26 2020 +0530 Fix: Indentation commit beae0b9849f333dd1d9f29c27192ea7de2e4e809 Author: Vignesh <kaviya.3111999@gmail.com> Date: Mon Mar 23 18:26:50 2020 +0530 The validations are added to distributed cost center. commit 3f66943adacb5797338ef3510251bf6633be3dba Author: Vignesh <kaviya.3111999@gmail.com> Date: Thu Mar 19 22:17:25 2020 +0530 The distributed cost centers are shown with the percentage allocation in the reports commit 1ac11df522096fa187d2f5863b4c970e18b37fac Author: Vignesh <kaviya.3111999@gmail.com> Date: Thu Mar 19 13:36:04 2020 +0530 The child table distributed cost center are added to cost center doctype and then validate percentage allocation. * fix(Distributed Cost Center): validation and filters * fix(Distributed Cost Center): financial statement query * fix(Distributed Cost Center): add test cases Co-authored-by: Vignesh S <svignesh0308@gmail.com> * fix(Distributed Cost Center): budget variance, general ledger, profitability analysis reports. * Merge branch 'develop' into develop-distributed-cost-center * fix(Distributed Cost Center): Added DCC reflection in actual amount column of Budget Variance Report. * Update creation field in general ledger report Co-authored-by: Nabin Hait <nabinhait@gmail.com> * creation field updated in gl_entries query Co-authored-by: Nabin Hait <nabinhait@gmail.com> Co-authored-by: KaviyaPeriyasamy <kaviya.3111999@gmail.com> Co-authored-by: Vignesh S <svignesh0308@gmail.com> Co-authored-by: KaviyaPeriyasamy <kaviyaperiyasamy22@gmail.com> Co-authored-by: Kaviya Periyasamy <36359901+KaviyaPeriyasamy@users.noreply.github.com> Co-authored-by: Nabin Hait <nabinhait@gmail.com>
2020-06-08 06:50:21 +00:00
if based_on == 'cost_center':
cost_center_doc = frappe.get_doc("Cost Center",d.name)
if not cost_center_doc.enable_distributed_cost_center:
DCC_allocation = frappe.db.sql("""SELECT parent, sum(percentage_allocation) as percentage_allocation
FROM `tabDistributed Cost Center`
WHERE cost_center IN %(cost_center)s
AND parent NOT IN %(cost_center)s
GROUP BY parent""",{'cost_center': [d.name]})
if DCC_allocation:
for account in new_accounts:
if account['name'] == DCC_allocation[0][0]:
for value in value_fields:
d[value] += account[value]*(DCC_allocation[0][1]/100)
2016-08-24 10:52:30 +00:00
for key in value_fields:
row[key] = flt(d.get(key, 0.0), 3)
2016-08-24 10:52:30 +00:00
if abs(row[key]) >= 0.005:
# ignore zero values
has_value = True
row["has_value"] = has_value
data.append(row)
2016-08-24 10:52:30 +00:00
data.extend([{},total_row])
return data
2016-08-24 12:26:09 +00:00
def get_columns(filters):
2016-08-24 10:52:30 +00:00
return [
{
"fieldname": "account",
2017-02-08 05:50:04 +00:00
"label": _(filters.get("based_on")),
2016-08-24 10:52:30 +00:00
"fieldtype": "Link",
2017-02-08 05:50:04 +00:00
"options": filters.get("based_on"),
2016-08-24 10:52:30 +00:00
"width": 300
},
{
"fieldname": "currency",
"label": _("Currency"),
"fieldtype": "Link",
"options": "Currency",
"hidden": 1
},
2016-08-24 10:52:30 +00:00
{
"fieldname": "income",
"label": _("Income"),
"fieldtype": "Currency",
"options": "currency",
"width": 305
2016-08-24 10:52:30 +00:00
},
{
"fieldname": "expense",
"label": _("Expense"),
"fieldtype": "Currency",
"options": "currency",
"width": 305
2016-08-24 10:52:30 +00:00
},
{
2016-08-26 07:09:54 +00:00
"fieldname": "gross_profit_loss",
2016-08-24 10:52:30 +00:00
"label": _("Gross Profit / Loss"),
"fieldtype": "Currency",
"options": "currency",
"width": 307
2016-08-24 10:52:30 +00:00
}
]
2016-08-24 12:26:09 +00:00
def set_gl_entries_by_account(company, from_date, to_date, based_on, gl_entries_by_account,
2016-08-24 10:52:30 +00:00
ignore_closing_entries=False):
"""Returns a dict like { "account": [gl entries], ... }"""
additional_conditions = []
if ignore_closing_entries:
additional_conditions.append("and ifnull(voucher_type, '')!='Period Closing Voucher'")
if from_date:
additional_conditions.append("and posting_date >= %(from_date)s")
gl_entries = frappe.db.sql("""select posting_date, {based_on} as based_on, debit, credit,
2016-08-24 10:52:30 +00:00
is_opening, (select root_type from `tabAccount` where name = account) as type
from `tabGL Entry` where company=%(company)s
{additional_conditions}
and posting_date <= %(to_date)s
2016-08-24 12:26:09 +00:00
and {based_on} is not null
order by {based_on}, posting_date""".format(additional_conditions="\n".join(additional_conditions), based_on= based_on),
2016-08-24 10:52:30 +00:00
{
"company": company,
"from_date": from_date,
2016-08-24 12:26:09 +00:00
"to_date": to_date
2016-08-24 10:52:30 +00:00
},
as_dict=True)
for entry in gl_entries:
2016-08-24 12:26:09 +00:00
gl_entries_by_account.setdefault(entry.based_on, []).append(entry)
2016-08-24 10:52:30 +00:00
return gl_entries_by_account