Report presentation currency (#12670)
* Add new Select to filters * get the currencies from database rather than hardcoded * label report columns: - If presentation currency is available in filters, use it or - If company is available in filters, use it or - use default company currency * add new function - get_currency * tweak new function `get_currency` * add new function `convert` to convert a value to another currency * add new function `convert_to_presentation_currency` * clean up `get_currency` first pass * memoise the exchange rates * limit fetched GL entries to to_date * check if account type is p&l or not and use appropriate exchange rate based on that * change EXCHANGE RATE to a dict, use for memoisation * rename EXCHANGE_RATE * cache exchange rates and use them as needed * add docstrings * add presentation currency logic to financial statement reports * move new functions from `general_ledger.py` to new module * clean up * PEP 8 clean up * move function to util.py * PEP 8 clean up * remove presentation currency option from cashflow * adjust currency as needed * allow users to save presentation currency in Accounts Settings * add new function `get_presentation_currency_list` * refactor query_report modules with no promises * Revert "allow users to save presentation currency in Accounts Settings" This reverts commit 3b58a6296cf3f7b4d80ac55b03f9d5d4887b796b. * show print page in correct currency * Update utils.py
This commit is contained in:
parent
621740efd9
commit
c89782502c
@ -11,5 +11,3 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() {
|
|||||||
"default": 1
|
"default": 1
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,11 @@ frappe.require("assets/erpnext/js/financial_statements.js", function() {
|
|||||||
frappe.query_reports["Cash Flow"] = $.extend({},
|
frappe.query_reports["Cash Flow"] = $.extend({},
|
||||||
erpnext.financial_statements);
|
erpnext.financial_statements);
|
||||||
|
|
||||||
|
// The last item in the array is the definition for Presentation Currency
|
||||||
|
// filter. It won't be used in cash flow for now so we pop it. Please take
|
||||||
|
// of this if you are working here.
|
||||||
|
frappe.query_reports["Cash Flow"]["filters"].pop();
|
||||||
|
|
||||||
frappe.query_reports["Cash Flow"]["filters"].push({
|
frappe.query_reports["Cash Flow"]["filters"].push({
|
||||||
"fieldname": "accumulated_values",
|
"fieldname": "accumulated_values",
|
||||||
"label": __("Accumulated Values"),
|
"label": __("Accumulated Values"),
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
# License: GNU General Public License v3. See license.txt
|
# License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
|
||||||
import re
|
import re
|
||||||
from frappe import _
|
|
||||||
from frappe.utils import (flt, getdate, get_first_day, get_last_day, date_diff,
|
import frappe
|
||||||
add_months, add_days, formatdate, cint)
|
from erpnext.accounts.report.utils import get_currency, convert_to_presentation_currency
|
||||||
from erpnext.accounts.utils import get_fiscal_year
|
from erpnext.accounts.utils import get_fiscal_year
|
||||||
|
from frappe import _
|
||||||
|
from frappe.utils import (flt, getdate, get_first_day, add_months, add_days, formatdate)
|
||||||
|
|
||||||
|
|
||||||
def get_period_list(from_fiscal_year, to_fiscal_year, periodicity, accumulated_values=False,
|
def get_period_list(from_fiscal_year, to_fiscal_year, periodicity, accumulated_values=False,
|
||||||
@ -84,6 +86,7 @@ def get_period_list(from_fiscal_year, to_fiscal_year, periodicity, accumulated_v
|
|||||||
|
|
||||||
return period_list
|
return period_list
|
||||||
|
|
||||||
|
|
||||||
def get_fiscal_year_data(from_fiscal_year, to_fiscal_year):
|
def get_fiscal_year_data(from_fiscal_year, to_fiscal_year):
|
||||||
fiscal_year = frappe.db.sql("""select min(year_start_date) as year_start_date,
|
fiscal_year = frappe.db.sql("""select min(year_start_date) as year_start_date,
|
||||||
max(year_end_date) as year_end_date from `tabFiscal Year` where
|
max(year_end_date) as year_end_date from `tabFiscal Year` where
|
||||||
@ -92,16 +95,19 @@ def get_fiscal_year_data(from_fiscal_year, to_fiscal_year):
|
|||||||
|
|
||||||
return fiscal_year[0] if fiscal_year else {}
|
return fiscal_year[0] if fiscal_year else {}
|
||||||
|
|
||||||
|
|
||||||
def validate_fiscal_year(fiscal_year, from_fiscal_year, to_fiscal_year):
|
def validate_fiscal_year(fiscal_year, from_fiscal_year, to_fiscal_year):
|
||||||
if not fiscal_year.get('year_start_date') and not fiscal_year.get('year_end_date'):
|
if not fiscal_year.get('year_start_date') and not fiscal_year.get('year_end_date'):
|
||||||
frappe.throw(_("End Year cannot be before Start Year"))
|
frappe.throw(_("End Year cannot be before Start Year"))
|
||||||
|
|
||||||
|
|
||||||
def get_months(start_date, end_date):
|
def get_months(start_date, end_date):
|
||||||
diff = (12 * end_date.year + end_date.month) - (12 * start_date.year + start_date.month)
|
diff = (12 * end_date.year + end_date.month) - (12 * start_date.year + start_date.month)
|
||||||
return diff + 1
|
return diff + 1
|
||||||
|
|
||||||
|
|
||||||
def get_label(periodicity, from_date, to_date):
|
def get_label(periodicity, from_date, to_date):
|
||||||
if periodicity=="Yearly":
|
if periodicity == "Yearly":
|
||||||
if formatdate(from_date, "YYYY") == formatdate(to_date, "YYYY"):
|
if formatdate(from_date, "YYYY") == formatdate(to_date, "YYYY"):
|
||||||
label = formatdate(from_date, "YYYY")
|
label = formatdate(from_date, "YYYY")
|
||||||
else:
|
else:
|
||||||
@ -111,28 +117,34 @@ def get_label(periodicity, from_date, to_date):
|
|||||||
|
|
||||||
return label
|
return label
|
||||||
|
|
||||||
def get_data(company, root_type, balance_must_be, period_list, filters=None,
|
|
||||||
|
def get_data(
|
||||||
|
company, root_type, balance_must_be, period_list, filters=None,
|
||||||
accumulated_values=1, only_current_fiscal_year=True, ignore_closing_entries=False,
|
accumulated_values=1, only_current_fiscal_year=True, ignore_closing_entries=False,
|
||||||
ignore_accumulated_values_for_fy=False):
|
ignore_accumulated_values_for_fy=False):
|
||||||
|
|
||||||
accounts = get_accounts(company, root_type)
|
accounts = get_accounts(company, root_type)
|
||||||
if not accounts:
|
if not accounts:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
accounts, accounts_by_name, parent_children_map = filter_accounts(accounts)
|
accounts, accounts_by_name, parent_children_map = filter_accounts(accounts)
|
||||||
|
|
||||||
company_currency = frappe.db.get_value("Company", company, "default_currency")
|
company_currency = get_appropriate_currency(company, filters)
|
||||||
|
|
||||||
gl_entries_by_account = {}
|
gl_entries_by_account = {}
|
||||||
for root in frappe.db.sql("""select lft, rgt from tabAccount
|
for root in frappe.db.sql("""select lft, rgt from tabAccount
|
||||||
where root_type=%s and ifnull(parent_account, '') = ''""", root_type, as_dict=1):
|
where root_type=%s and ifnull(parent_account, '') = ''""", root_type, as_dict=1):
|
||||||
|
|
||||||
set_gl_entries_by_account(company,
|
set_gl_entries_by_account(
|
||||||
|
company,
|
||||||
period_list[0]["year_start_date"] if only_current_fiscal_year else None,
|
period_list[0]["year_start_date"] if only_current_fiscal_year else None,
|
||||||
period_list[-1]["to_date"],
|
period_list[-1]["to_date"],
|
||||||
root.lft, root.rgt, filters,
|
root.lft, root.rgt, filters,
|
||||||
gl_entries_by_account, ignore_closing_entries=ignore_closing_entries)
|
gl_entries_by_account, ignore_closing_entries=ignore_closing_entries
|
||||||
|
)
|
||||||
|
|
||||||
calculate_values(accounts_by_name, gl_entries_by_account, period_list, accumulated_values, ignore_accumulated_values_for_fy)
|
calculate_values(
|
||||||
|
accounts_by_name, gl_entries_by_account, period_list, accumulated_values, ignore_accumulated_values_for_fy)
|
||||||
accumulate_values_into_parents(accounts, accounts_by_name, period_list, accumulated_values)
|
accumulate_values_into_parents(accounts, accounts_by_name, period_list, accumulated_values)
|
||||||
out = prepare_data(accounts, balance_must_be, period_list, company_currency)
|
out = prepare_data(accounts, balance_must_be, period_list, company_currency)
|
||||||
out = filter_out_zero_value_rows(out, parent_children_map)
|
out = filter_out_zero_value_rows(out, parent_children_map)
|
||||||
@ -143,7 +155,15 @@ def get_data(company, root_type, balance_must_be, period_list, filters=None,
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def calculate_values(accounts_by_name, gl_entries_by_account, period_list, accumulated_values, ignore_accumulated_values_for_fy):
|
def get_appropriate_currency(company, filters=None):
|
||||||
|
if filters and filters.get("presentation_currency"):
|
||||||
|
return filters["presentation_currency"]
|
||||||
|
else:
|
||||||
|
return frappe.db.get_value("Company", company, "default_currency")
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_values(
|
||||||
|
accounts_by_name, gl_entries_by_account, period_list, accumulated_values, ignore_accumulated_values_for_fy):
|
||||||
for entries in gl_entries_by_account.values():
|
for entries in gl_entries_by_account.values():
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
d = accounts_by_name.get(entry.account)
|
d = accounts_by_name.get(entry.account)
|
||||||
@ -164,6 +184,7 @@ def calculate_values(accounts_by_name, gl_entries_by_account, period_list, accum
|
|||||||
if entry.posting_date < period_list[0].year_start_date:
|
if entry.posting_date < period_list[0].year_start_date:
|
||||||
d["opening_balance"] = d.get("opening_balance", 0.0) + flt(entry.debit) - flt(entry.credit)
|
d["opening_balance"] = d.get("opening_balance", 0.0) + flt(entry.debit) - flt(entry.credit)
|
||||||
|
|
||||||
|
|
||||||
def accumulate_values_into_parents(accounts, accounts_by_name, period_list, accumulated_values):
|
def accumulate_values_into_parents(accounts, accounts_by_name, period_list, accumulated_values):
|
||||||
"""accumulate children's values in parent accounts"""
|
"""accumulate children's values in parent accounts"""
|
||||||
for d in reversed(accounts):
|
for d in reversed(accounts):
|
||||||
@ -175,6 +196,7 @@ def accumulate_values_into_parents(accounts, accounts_by_name, period_list, accu
|
|||||||
accounts_by_name[d.parent_account]["opening_balance"] = \
|
accounts_by_name[d.parent_account]["opening_balance"] = \
|
||||||
accounts_by_name[d.parent_account].get("opening_balance", 0.0) + d.get("opening_balance", 0.0)
|
accounts_by_name[d.parent_account].get("opening_balance", 0.0) + d.get("opening_balance", 0.0)
|
||||||
|
|
||||||
|
|
||||||
def prepare_data(accounts, balance_must_be, period_list, company_currency):
|
def prepare_data(accounts, balance_must_be, period_list, company_currency):
|
||||||
data = []
|
data = []
|
||||||
year_start_date = period_list[0]["year_start_date"].strftime("%Y-%m-%d")
|
year_start_date = period_list[0]["year_start_date"].strftime("%Y-%m-%d")
|
||||||
@ -192,10 +214,10 @@ def prepare_data(accounts, balance_must_be, period_list, company_currency):
|
|||||||
"year_start_date": year_start_date,
|
"year_start_date": year_start_date,
|
||||||
"year_end_date": year_end_date,
|
"year_end_date": year_end_date,
|
||||||
"currency": company_currency,
|
"currency": company_currency,
|
||||||
"opening_balance": d.get("opening_balance", 0.0) * (1 if balance_must_be=="Debit" else -1)
|
"opening_balance": d.get("opening_balance", 0.0) * (1 if balance_must_be == "Debit" else -1)
|
||||||
})
|
})
|
||||||
for period in period_list:
|
for period in period_list:
|
||||||
if d.get(period.key) and balance_must_be=="Credit":
|
if d.get(period.key) and balance_must_be == "Credit":
|
||||||
# change sign based on Debit or Credit, since calculation is done using (debit - credit)
|
# change sign based on Debit or Credit, since calculation is done using (debit - credit)
|
||||||
d[period.key] *= -1
|
d[period.key] *= -1
|
||||||
|
|
||||||
@ -212,6 +234,7 @@ def prepare_data(accounts, balance_must_be, period_list, company_currency):
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def filter_out_zero_value_rows(data, parent_children_map, show_zero_values=False):
|
def filter_out_zero_value_rows(data, parent_children_map, show_zero_values=False):
|
||||||
data_with_value = []
|
data_with_value = []
|
||||||
for d in data:
|
for d in data:
|
||||||
@ -228,6 +251,7 @@ def filter_out_zero_value_rows(data, parent_children_map, show_zero_values=False
|
|||||||
|
|
||||||
return data_with_value
|
return data_with_value
|
||||||
|
|
||||||
|
|
||||||
def add_total_row(out, root_type, balance_must_be, period_list, company_currency):
|
def add_total_row(out, root_type, balance_must_be, period_list, company_currency):
|
||||||
total_row = {
|
total_row = {
|
||||||
"account_name": "'" + _("Total {0} ({1})").format(_(root_type), _(balance_must_be)) + "'",
|
"account_name": "'" + _("Total {0} ({1})").format(_(root_type), _(balance_must_be)) + "'",
|
||||||
@ -246,16 +270,19 @@ def add_total_row(out, root_type, balance_must_be, period_list, company_currency
|
|||||||
total_row["total"] += flt(row["total"])
|
total_row["total"] += flt(row["total"])
|
||||||
row["total"] = ""
|
row["total"] = ""
|
||||||
|
|
||||||
if total_row.has_key("total"):
|
if "total" in total_row:
|
||||||
out.append(total_row)
|
out.append(total_row)
|
||||||
|
|
||||||
# blank row after Total
|
# blank row after Total
|
||||||
out.append({})
|
out.append({})
|
||||||
|
|
||||||
|
|
||||||
def get_accounts(company, root_type):
|
def get_accounts(company, root_type):
|
||||||
return frappe.db.sql("""select name, parent_account, lft, rgt, root_type, report_type, account_name from `tabAccount`
|
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)
|
where company=%s and root_type=%s order by lft""", (company, root_type), as_dict=True)
|
||||||
|
|
||||||
|
|
||||||
def filter_accounts(accounts, depth=10):
|
def filter_accounts(accounts, depth=10):
|
||||||
parent_children_map = {}
|
parent_children_map = {}
|
||||||
accounts_by_name = {}
|
accounts_by_name = {}
|
||||||
@ -280,6 +307,7 @@ def filter_accounts(accounts, depth=10):
|
|||||||
|
|
||||||
return filtered_accounts, accounts_by_name, parent_children_map
|
return filtered_accounts, accounts_by_name, parent_children_map
|
||||||
|
|
||||||
|
|
||||||
def sort_root_accounts(roots):
|
def sort_root_accounts(roots):
|
||||||
"""Sort root types as Asset, Liability, Equity, Income, Expense"""
|
"""Sort root types as Asset, Liability, Equity, Income, Expense"""
|
||||||
|
|
||||||
@ -299,13 +327,14 @@ def sort_root_accounts(roots):
|
|||||||
|
|
||||||
roots.sort(compare_roots)
|
roots.sort(compare_roots)
|
||||||
|
|
||||||
def set_gl_entries_by_account(company, from_date, to_date, root_lft, root_rgt, filters, gl_entries_by_account,
|
|
||||||
ignore_closing_entries=False):
|
def set_gl_entries_by_account(
|
||||||
|
company, from_date, to_date, root_lft, root_rgt, filters, gl_entries_by_account, ignore_closing_entries=False):
|
||||||
"""Returns a dict like { "account": [gl entries], ... }"""
|
"""Returns a dict like { "account": [gl entries], ... }"""
|
||||||
|
|
||||||
additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters)
|
additional_conditions = get_additional_conditions(from_date, ignore_closing_entries, filters)
|
||||||
|
|
||||||
gl_entries = frappe.db.sql("""select posting_date, account, debit, credit, is_opening, fiscal_year from `tabGL Entry`
|
gl_entries = frappe.db.sql("""select posting_date, account, debit, credit, is_opening, fiscal_year, debit_in_account_currency, credit_in_account_currency, account_currency from `tabGL Entry`
|
||||||
where company=%(company)s
|
where company=%(company)s
|
||||||
{additional_conditions}
|
{additional_conditions}
|
||||||
and posting_date <= %(to_date)s
|
and posting_date <= %(to_date)s
|
||||||
@ -321,11 +350,15 @@ def set_gl_entries_by_account(company, from_date, to_date, root_lft, root_rgt, f
|
|||||||
},
|
},
|
||||||
as_dict=True)
|
as_dict=True)
|
||||||
|
|
||||||
|
if filters and filters.get('presentation_currency'):
|
||||||
|
convert_to_presentation_currency(gl_entries, get_currency(filters))
|
||||||
|
|
||||||
for entry in gl_entries:
|
for entry in gl_entries:
|
||||||
gl_entries_by_account.setdefault(entry.account, []).append(entry)
|
gl_entries_by_account.setdefault(entry.account, []).append(entry)
|
||||||
|
|
||||||
return gl_entries_by_account
|
return gl_entries_by_account
|
||||||
|
|
||||||
|
|
||||||
def get_additional_conditions(from_date, ignore_closing_entries, filters):
|
def get_additional_conditions(from_date, ignore_closing_entries, filters):
|
||||||
additional_conditions = []
|
additional_conditions = []
|
||||||
|
|
||||||
@ -337,15 +370,17 @@ def get_additional_conditions(from_date, ignore_closing_entries, filters):
|
|||||||
|
|
||||||
if filters:
|
if filters:
|
||||||
if filters.get("project"):
|
if filters.get("project"):
|
||||||
additional_conditions.append("project = '%s'"%(frappe.db.escape(filters.get("project"))))
|
additional_conditions.append("project = '%s'" % (frappe.db.escape(filters.get("project"))))
|
||||||
if filters.get("cost_center"):
|
if filters.get("cost_center"):
|
||||||
additional_conditions.append(get_cost_center_cond(filters.get("cost_center")))
|
additional_conditions.append(get_cost_center_cond(filters.get("cost_center")))
|
||||||
|
|
||||||
return " and {}".format(" and ".join(additional_conditions)) if additional_conditions else ""
|
return " and {}".format(" and ".join(additional_conditions)) if additional_conditions else ""
|
||||||
|
|
||||||
|
|
||||||
def get_cost_center_cond(cost_center):
|
def get_cost_center_cond(cost_center):
|
||||||
lft, rgt = frappe.db.get_value("Cost Center", cost_center, ["lft", "rgt"])
|
lft, rgt = frappe.db.get_value("Cost Center", cost_center, ["lft", "rgt"])
|
||||||
return (""" cost_center in (select name from `tabCost Center` where lft >=%s and rgt <=%s)"""%(lft, rgt))
|
return """ cost_center in (select name from `tabCost Center` where lft >=%s and rgt <=%s)""" % (lft, rgt)
|
||||||
|
|
||||||
|
|
||||||
def get_columns(periodicity, period_list, accumulated_values=1, company=None):
|
def get_columns(periodicity, period_list, accumulated_values=1, company=None):
|
||||||
columns = [{
|
columns = [{
|
||||||
|
@ -44,44 +44,24 @@
|
|||||||
<br>{%= __("Supplier Invoice No") %}: {%= data[i].bill_no %}
|
<br>{%= __("Supplier Invoice No") %}: {%= data[i].bill_no %}
|
||||||
{% } %}
|
{% } %}
|
||||||
</td>
|
</td>
|
||||||
{% if(filters.print_in_account_currency) { %}
|
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
{%= format_currency(data[i].debit_in_account_currency, data[i].account_currency) %}
|
{%= format_currency(data[i].debit, filters.presentation_currency) %}</td>
|
||||||
</td>
|
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
{%= format_currency(data[i].credit_in_account_currency, data[i].account_currency) %}
|
{%= format_currency(data[i].credit, filters.presentation_currency) %}</td>
|
||||||
</td>
|
|
||||||
{% } else { %}
|
|
||||||
<td style="text-align: right">
|
|
||||||
{%= format_currency(data[i].debit) %}</td>
|
|
||||||
<td style="text-align: right">
|
|
||||||
{%= format_currency(data[i].credit) %}</td>
|
|
||||||
{% } %}
|
|
||||||
{% } else { %}
|
{% } else { %}
|
||||||
<td></td>
|
<td></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td><b>{%= frappe.format(data[i].account, {fieldtype: "Link"}) || " " %}</b></td>
|
<td><b>{%= frappe.format(data[i].account, {fieldtype: "Link"}) || " " %}</b></td>
|
||||||
{% if(filters.print_in_account_currency) { %}
|
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
{%= data[i].account && format_currency(data[i].debit_in_account_currency, data[i].account_currency) %}</td>
|
{%= data[i].account && format_currency(data[i].debit, filters.presentation_currency) %}
|
||||||
<td style="text-align: right">
|
|
||||||
{%= data[i].account && format_currency(data[i].credit_in_account_currency, data[i].account_currency) %}</td>
|
|
||||||
{% } else { %}
|
|
||||||
<td style="text-align: right">
|
|
||||||
{%= data[i].account && format_currency(data[i].debit) %}
|
|
||||||
</td>
|
</td>
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
{%= data[i].account && format_currency(data[i].credit) %}
|
{%= data[i].account && format_currency(data[i].credit, filters.presentation_currency) %}
|
||||||
</td>
|
</td>
|
||||||
{% } %}
|
{% } %}
|
||||||
{% } %}
|
|
||||||
{% if(filters.print_in_account_currency) { %}
|
|
||||||
<td style="text-align: right">
|
<td style="text-align: right">
|
||||||
{%= format_currency(data[i].balance_in_account_currency, data[i].account_currency) %}
|
{%= format_currency(data[i].balance, filters.presentation_currency) %}
|
||||||
</td>
|
</td>
|
||||||
{% } else { %}
|
|
||||||
<td style="text-align: right">{%= format_currency(data[i].balance) %}</td>
|
|
||||||
{% } %}
|
|
||||||
</tr>
|
</tr>
|
||||||
{% } %}
|
{% } %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
// Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
// License: GNU General Public License v3. See license.txt
|
// License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
frappe.query_reports["General Ledger"] = {
|
frappe.query_reports["General Ledger"] = {
|
||||||
@ -107,9 +107,10 @@ frappe.query_reports["General Ledger"] = {
|
|||||||
"fieldtype": "Check",
|
"fieldtype": "Check",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"fieldname":"print_in_account_currency",
|
"fieldname": "presentation_currency",
|
||||||
"label": __("Print in Account Currency"),
|
"label": __("Currency"),
|
||||||
"fieldtype": "Check",
|
"fieldtype": "Select",
|
||||||
|
"options": erpnext.get_presentation_currency_list()
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,13 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
|
from erpnext import get_company_currency, get_default_company
|
||||||
|
from erpnext.accounts.report.utils import get_currency, convert_to_presentation_currency
|
||||||
from frappe.utils import getdate, cstr, flt, fmt_money
|
from frappe.utils import getdate, cstr, flt, fmt_money
|
||||||
from frappe import _, _dict
|
from frappe import _, _dict
|
||||||
from erpnext.accounts.utils import get_account_currency
|
from erpnext.accounts.utils import get_account_currency
|
||||||
|
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
account_details = {}
|
account_details = {}
|
||||||
|
|
||||||
@ -29,6 +32,7 @@ def execute(filters=None):
|
|||||||
|
|
||||||
return columns, res
|
return columns, res
|
||||||
|
|
||||||
|
|
||||||
def validate_filters(filters, account_details):
|
def validate_filters(filters, account_details):
|
||||||
if not filters.get('company'):
|
if not filters.get('company'):
|
||||||
frappe.throw(_('{0} is mandatory').format(_('Company')))
|
frappe.throw(_('{0} is mandatory').format(_('Company')))
|
||||||
@ -56,6 +60,7 @@ def validate_party(filters):
|
|||||||
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
|
||||||
@ -66,8 +71,13 @@ def set_account_currency(filters):
|
|||||||
if filters.get("account"):
|
if filters.get("account"):
|
||||||
account_currency = get_account_currency(filters.account)
|
account_currency = get_account_currency(filters.account)
|
||||||
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(
|
||||||
"party": filters.party, "company": filters.company}, "account_currency")
|
"GL Entry", {
|
||||||
|
"party_type": filters.party_type, "party": filters.party, "company": filters.company
|
||||||
|
},
|
||||||
|
"account_currency"
|
||||||
|
)
|
||||||
|
|
||||||
if gle_currency:
|
if gle_currency:
|
||||||
account_currency = gle_currency
|
account_currency = gle_currency
|
||||||
else:
|
else:
|
||||||
@ -90,30 +100,40 @@ def get_result(filters, account_details):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_gl_entries(filters):
|
def get_gl_entries(filters):
|
||||||
|
currency_map = get_currency(filters)
|
||||||
select_fields = """, sum(debit_in_account_currency) as debit_in_account_currency,
|
select_fields = """, sum(debit_in_account_currency) as debit_in_account_currency,
|
||||||
sum(credit_in_account_currency) as credit_in_account_currency""" \
|
sum(credit_in_account_currency) as credit_in_account_currency""" \
|
||||||
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("""
|
gl_entries = frappe.db.sql(
|
||||||
|
"""
|
||||||
select
|
select
|
||||||
posting_date, account, party_type, party,
|
posting_date, account, party_type, party,
|
||||||
sum(debit) as debit, sum(credit) as credit,
|
sum(debit) as debit, sum(credit) as credit,
|
||||||
voucher_type, voucher_no, cost_center, project,
|
voucher_type, voucher_no, cost_center, project,
|
||||||
against_voucher_type, against_voucher,
|
against_voucher_type, against_voucher, account_currency,
|
||||||
remarks, against, is_opening {select_fields}
|
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(
|
||||||
group_by_condition=group_by_condition), filters, as_dict=1)
|
select_fields=select_fields, conditions=get_conditions(filters),
|
||||||
|
group_by_condition=group_by_condition
|
||||||
|
),
|
||||||
|
filters, as_dict=1)
|
||||||
|
|
||||||
|
if filters.get('presentation_currency'):
|
||||||
|
return convert_to_presentation_currency(gl_entries, currency_map)
|
||||||
|
else:
|
||||||
return gl_entries
|
return gl_entries
|
||||||
|
|
||||||
|
|
||||||
def get_conditions(filters):
|
def get_conditions(filters):
|
||||||
conditions = []
|
conditions = []
|
||||||
if filters.get("account"):
|
if filters.get("account"):
|
||||||
@ -132,16 +152,20 @@ def get_conditions(filters):
|
|||||||
|
|
||||||
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")
|
||||||
|
conditions.append("posting_date <=%(to_date)s")
|
||||||
|
|
||||||
if filters.get("project"):
|
if filters.get("project"):
|
||||||
conditions.append("project=%(project)s")
|
conditions.append("project=%(project)s")
|
||||||
|
|
||||||
from frappe.desk.reportview import build_match_conditions
|
from frappe.desk.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)
|
||||||
@ -178,14 +202,15 @@ def get_data_with_opening_closing(filters, account_details, gl_entries):
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_totals_dict():
|
def get_totals_dict():
|
||||||
def _get_debit_credit_dict(label):
|
def _get_debit_credit_dict(label):
|
||||||
return _dict(
|
return _dict(
|
||||||
account = "'{0}'".format(label),
|
account="'{0}'".format(label),
|
||||||
debit = 0.0,
|
debit=0.0,
|
||||||
credit = 0.0,
|
credit=0.0,
|
||||||
debit_in_account_currency = 0.0,
|
debit_in_account_currency=0.0,
|
||||||
credit_in_account_currency = 0.0
|
credit_in_account_currency=0.0
|
||||||
)
|
)
|
||||||
return _dict(
|
return _dict(
|
||||||
opening = _get_debit_credit_dict(_('Opening')),
|
opening = _get_debit_credit_dict(_('Opening')),
|
||||||
@ -193,12 +218,14 @@ def get_totals_dict():
|
|||||||
closing = _get_debit_credit_dict(_('Closing (Opening + Total)'))
|
closing = _get_debit_credit_dict(_('Closing (Opening + Total)'))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def initialize_gle_map(gl_entries):
|
def initialize_gle_map(gl_entries):
|
||||||
gle_map = frappe._dict()
|
gle_map = frappe._dict()
|
||||||
for gle in gl_entries:
|
for gle in gl_entries:
|
||||||
gle_map.setdefault(gle.account, _dict(totals = get_totals_dict(), entries = []))
|
gle_map.setdefault(gle.account, _dict(totals=get_totals_dict(), entries=[]))
|
||||||
return gle_map
|
return gle_map
|
||||||
|
|
||||||
|
|
||||||
def get_accountwise_gle(filters, gl_entries, gle_map):
|
def get_accountwise_gle(filters, gl_entries, gle_map):
|
||||||
totals = get_totals_dict()
|
totals = get_totals_dict()
|
||||||
entries = []
|
entries = []
|
||||||
@ -210,7 +237,6 @@ def get_accountwise_gle(filters, gl_entries, gle_map):
|
|||||||
data[key].debit_in_account_currency += flt(gle.debit_in_account_currency)
|
data[key].debit_in_account_currency += flt(gle.debit_in_account_currency)
|
||||||
data[key].credit_in_account_currency += flt(gle.credit_in_account_currency)
|
data[key].credit_in_account_currency += flt(gle.credit_in_account_currency)
|
||||||
|
|
||||||
|
|
||||||
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:
|
||||||
if gle.posting_date < from_date or cstr(gle.is_opening) == "Yes":
|
if gle.posting_date < from_date or cstr(gle.is_opening) == "Yes":
|
||||||
@ -233,6 +259,7 @@ def get_accountwise_gle(filters, gl_entries, gle_map):
|
|||||||
|
|
||||||
return totals, entries
|
return totals, entries
|
||||||
|
|
||||||
|
|
||||||
def get_result_as_list(data, filters):
|
def get_result_as_list(data, filters):
|
||||||
balance, balance_in_account_currency = 0, 0
|
balance, balance_in_account_currency = 0, 0
|
||||||
inv_details = get_supplier_invoice_details()
|
inv_details = get_supplier_invoice_details()
|
||||||
@ -272,6 +299,15 @@ def get_balance(row, balance, debit_field, credit_field):
|
|||||||
return balance
|
return balance
|
||||||
|
|
||||||
def get_columns(filters):
|
def get_columns(filters):
|
||||||
|
if filters.get("presentation_currency"):
|
||||||
|
currency = filters["presentation_currency"]
|
||||||
|
else:
|
||||||
|
if filters.get("company"):
|
||||||
|
currency = get_company_currency(filters["company"])
|
||||||
|
else:
|
||||||
|
company = get_default_company()
|
||||||
|
currency = get_company_currency(company)
|
||||||
|
|
||||||
columns = [
|
columns = [
|
||||||
{
|
{
|
||||||
"label": _("Posting Date"),
|
"label": _("Posting Date"),
|
||||||
@ -287,47 +323,25 @@ def get_columns(filters):
|
|||||||
"width": 180
|
"width": 180
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Debit"),
|
"label": _("Debit ({0})".format(currency)),
|
||||||
"fieldname": "debit",
|
"fieldname": "debit",
|
||||||
"fieldtype": "Float",
|
"fieldtype": "Float",
|
||||||
"width": 100
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Credit"),
|
"label": _("Credit ({0})".format(currency)),
|
||||||
"fieldname": "credit",
|
"fieldname": "credit",
|
||||||
"fieldtype": "Float",
|
"fieldtype": "Float",
|
||||||
"width": 100
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Balance (Dr - Cr)"),
|
"label": _("Balance ({0})".format(currency)),
|
||||||
"fieldname": "balance",
|
"fieldname": "balance",
|
||||||
"fieldtype": "Float",
|
"fieldtype": "Float",
|
||||||
"width": 130
|
"width": 130
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
if filters.get("show_in_account_currency"):
|
|
||||||
columns.extend([
|
|
||||||
{
|
|
||||||
"label": _("Debit") + " (" + filters.account_currency + ")",
|
|
||||||
"fieldname": "debit_in_account_currency",
|
|
||||||
"fieldtype": "Float",
|
|
||||||
"width": 100
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": _("Credit") + " (" + filters.account_currency + ")",
|
|
||||||
"fieldname": "credit_in_account_currency",
|
|
||||||
"fieldtype": "Float",
|
|
||||||
"width": 100
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": _("Balance") + " (" + filters.account_currency + ")",
|
|
||||||
"fieldname": "balance_in_account_currency",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"width": 100
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
columns.extend([
|
columns.extend([
|
||||||
{
|
{
|
||||||
"label": _("Voucher Type"),
|
"label": _("Voucher Type"),
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||||
// License: GNU General Public License v3. See license.txt
|
// License: GNU General Public License v3. See license.txt
|
||||||
|
|
||||||
|
|
||||||
frappe.require("assets/erpnext/js/financial_statements.js", function() {
|
frappe.require("assets/erpnext/js/financial_statements.js", function() {
|
||||||
frappe.query_reports["Profit and Loss Statement"] = $.extend({},
|
frappe.query_reports["Profit and Loss Statement"] = $.extend({},
|
||||||
erpnext.financial_statements);
|
erpnext.financial_statements);
|
||||||
|
128
erpnext/accounts/report/utils.py
Normal file
128
erpnext/accounts/report/utils.py
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
import frappe
|
||||||
|
from erpnext import get_company_currency, get_default_company
|
||||||
|
from erpnext.setup.utils import get_exchange_rate
|
||||||
|
from frappe.utils import cint
|
||||||
|
|
||||||
|
__exchange_rates = {}
|
||||||
|
P_OR_L_ACCOUNTS = list(
|
||||||
|
sum(frappe.get_list('Account', fields=['name'], or_filters=[{'root_type': 'Income'}, {'root_type': 'Expense'}], as_list=True), ())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_currency(filters):
|
||||||
|
"""
|
||||||
|
Returns a dictionary containing currency information. The keys of the dict are
|
||||||
|
- company: The company for which we are fetching currency information. if no
|
||||||
|
company is specified, it will fallback to the default company.
|
||||||
|
- company currency: The functional currency of the said company.
|
||||||
|
- presentation currency: The presentation currency to use. Only currencies that
|
||||||
|
have been used for transactions will be allowed.
|
||||||
|
- report date: The report date.
|
||||||
|
:param filters: Report filters
|
||||||
|
:type filters: dict
|
||||||
|
|
||||||
|
:return: str - Currency
|
||||||
|
"""
|
||||||
|
company = get_appropriate_company(filters)
|
||||||
|
company_currency = get_company_currency(company)
|
||||||
|
presentation_currency = filters['presentation_currency'] if filters.get('presentation_currency') else company_currency
|
||||||
|
report_date = filters.get('to_date') or filters.get('to_fiscal_year')
|
||||||
|
|
||||||
|
currency_map = dict(company=company, company_currency=company_currency, presentation_currency=presentation_currency, report_date=report_date)
|
||||||
|
|
||||||
|
return currency_map
|
||||||
|
|
||||||
|
|
||||||
|
def convert(value, from_, to, date):
|
||||||
|
"""
|
||||||
|
convert `value` from `from_` to `to` on `date`
|
||||||
|
:param value: Amount to be converted
|
||||||
|
:param from_: Currency of `value`
|
||||||
|
:param to: Currency to convert to
|
||||||
|
:param date: exchange rate as at this date
|
||||||
|
:return: Result of converting `value`
|
||||||
|
"""
|
||||||
|
rate = get_rate_as_at(date, from_, to)
|
||||||
|
converted_value = value / (rate or 1)
|
||||||
|
return converted_value
|
||||||
|
|
||||||
|
|
||||||
|
def get_rate_as_at(date, from_currency, to_currency):
|
||||||
|
"""
|
||||||
|
Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
|
||||||
|
This calls `get_exchange_rate` so that we can get the correct exchange rate as per
|
||||||
|
the user's Accounts Settings.
|
||||||
|
It is made efficient by memoising results to `__exchange_rates`
|
||||||
|
:param date: exchange rate as at this date
|
||||||
|
:param from_currency: Base currency
|
||||||
|
:param to_currency: Quote currency
|
||||||
|
:return: Retrieved exchange rate
|
||||||
|
"""
|
||||||
|
rate = __exchange_rates.get('{0}-{1}@{2}'.format(from_currency, to_currency, date))
|
||||||
|
if not rate:
|
||||||
|
rate = get_exchange_rate(from_currency, to_currency, date) or 1
|
||||||
|
__exchange_rates['{0}-{1}@{2}'.format(from_currency, to_currency, date)] = rate
|
||||||
|
|
||||||
|
return rate
|
||||||
|
|
||||||
|
|
||||||
|
def is_p_or_l_account(account_name):
|
||||||
|
"""
|
||||||
|
Check if the given `account name` is an `Account` with `root_type` of either 'Income'
|
||||||
|
or 'Expense'.
|
||||||
|
:param account_name:
|
||||||
|
:return: Boolean
|
||||||
|
"""
|
||||||
|
return account_name in P_OR_L_ACCOUNTS
|
||||||
|
|
||||||
|
|
||||||
|
def convert_to_presentation_currency(gl_entries, currency_info):
|
||||||
|
"""
|
||||||
|
Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
|
||||||
|
in `currency_info`.
|
||||||
|
:param gl_entries:
|
||||||
|
:param currency_info:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
converted_gl_list = []
|
||||||
|
presentation_currency = currency_info['presentation_currency']
|
||||||
|
company_currency = currency_info['company_currency']
|
||||||
|
|
||||||
|
for entry in gl_entries:
|
||||||
|
account = entry['account']
|
||||||
|
debit = cint(entry['debit'])
|
||||||
|
credit = cint(entry['credit'])
|
||||||
|
debit_in_account_currency = cint(entry['debit_in_account_currency'])
|
||||||
|
credit_in_account_currency = cint(entry['credit_in_account_currency'])
|
||||||
|
account_currency = entry['account_currency']
|
||||||
|
|
||||||
|
if account_currency != presentation_currency or (account_currency == presentation_currency and not is_p_or_l_account(account)):
|
||||||
|
value = debit or credit
|
||||||
|
|
||||||
|
date = currency_info['report_date'] if not is_p_or_l_account(account) else entry['posting_date']
|
||||||
|
|
||||||
|
converted_value = convert(value, presentation_currency, company_currency, date)
|
||||||
|
|
||||||
|
if entry.get('debit'):
|
||||||
|
entry['debit'] = converted_value
|
||||||
|
else:
|
||||||
|
entry['credit'] = converted_value
|
||||||
|
|
||||||
|
elif account_currency == presentation_currency:
|
||||||
|
if entry.get('debit'):
|
||||||
|
entry['debit'] = debit_in_account_currency
|
||||||
|
else:
|
||||||
|
entry['credit'] = credit_in_account_currency
|
||||||
|
|
||||||
|
converted_gl_list.append(entry)
|
||||||
|
|
||||||
|
return converted_gl_list
|
||||||
|
|
||||||
|
|
||||||
|
def get_appropriate_company(filters):
|
||||||
|
if filters.get('company'):
|
||||||
|
company = filters['company']
|
||||||
|
else:
|
||||||
|
company = get_default_company()
|
||||||
|
|
||||||
|
return company
|
@ -98,6 +98,16 @@ function get_filters(){
|
|||||||
],
|
],
|
||||||
"default": "Monthly",
|
"default": "Monthly",
|
||||||
"reqd": 1
|
"reqd": 1
|
||||||
|
},
|
||||||
|
// Note:
|
||||||
|
// If you are modifying this array such that the presentation_currency object
|
||||||
|
// is no longer the last object, please make adjustments in cash_flow.js
|
||||||
|
// accordingly.
|
||||||
|
{
|
||||||
|
"fieldname": "presentation_currency",
|
||||||
|
"label": __("Currency"),
|
||||||
|
"fieldtype": "Select",
|
||||||
|
"options": erpnext.get_presentation_currency_list()
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,12 @@ $.extend(erpnext, {
|
|||||||
return frappe.boot.sysdefaults.currency;
|
return frappe.boot.sysdefaults.currency;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
get_presentation_currency_list: () => {
|
||||||
|
const docs = frappe.boot.docs;
|
||||||
|
const currency_list = docs.filter(d => d.doctype === ":Currency").map(d => d.name);
|
||||||
|
return currency_list;
|
||||||
|
},
|
||||||
|
|
||||||
toggle_naming_series: function() {
|
toggle_naming_series: function() {
|
||||||
if(cur_frm.fields_dict.naming_series) {
|
if(cur_frm.fields_dict.naming_series) {
|
||||||
cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
|
cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user