2018-02-12 09:34:50 +00:00
|
|
|
import frappe
|
2023-07-04 12:11:30 +00:00
|
|
|
from frappe.utils import flt, formatdate, get_datetime_str, get_table_name
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
from erpnext import get_company_currency, get_default_company
|
2018-07-18 12:33:27 +00:00
|
|
|
from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
|
2021-09-02 11:14:59 +00:00
|
|
|
from erpnext.setup.utils import get_exchange_rate
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
__exchange_rates = {}
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
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)
|
2022-03-28 13:22:46 +00:00
|
|
|
presentation_currency = (
|
|
|
|
filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
|
|
|
|
)
|
2018-07-18 12:33:27 +00:00
|
|
|
|
2022-12-06 08:14:54 +00:00
|
|
|
report_date = filters.get("to_date") or filters.get("period_end_date")
|
2018-07-18 12:33:27 +00:00
|
|
|
|
|
|
|
if not report_date:
|
2022-03-28 13:22:46 +00:00
|
|
|
fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
|
2018-07-18 12:33:27 +00:00
|
|
|
report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
currency_map = dict(
|
|
|
|
company=company,
|
|
|
|
company_currency=company_currency,
|
|
|
|
presentation_currency=presentation_currency,
|
|
|
|
report_date=report_date,
|
|
|
|
)
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
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)
|
2018-08-14 10:58:14 +00:00
|
|
|
converted_value = flt(value) / (rate or 1)
|
2018-02-12 09:34:50 +00:00
|
|
|
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
|
|
|
|
"""
|
2018-07-18 12:33:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date))
|
2018-02-12 09:34:50 +00:00
|
|
|
if not rate:
|
|
|
|
rate = get_exchange_rate(from_currency, to_currency, date) or 1
|
2022-03-28 13:22:46 +00:00
|
|
|
__exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
return rate
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2023-07-09 14:46:12 +00:00
|
|
|
def convert_to_presentation_currency(gl_entries, currency_info):
|
2018-02-12 09:34:50 +00:00
|
|
|
"""
|
|
|
|
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 = []
|
2022-03-28 13:22:46 +00:00
|
|
|
presentation_currency = currency_info["presentation_currency"]
|
|
|
|
company_currency = currency_info["company_currency"]
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
|
2020-08-22 07:01:06 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
for entry in gl_entries:
|
2022-03-28 13:22:46 +00:00
|
|
|
debit = flt(entry["debit"])
|
|
|
|
credit = flt(entry["credit"])
|
|
|
|
debit_in_account_currency = flt(entry["debit_in_account_currency"])
|
|
|
|
credit_in_account_currency = flt(entry["credit_in_account_currency"])
|
|
|
|
account_currency = entry["account_currency"]
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2021-05-25 13:46:02 +00:00
|
|
|
if len(account_currencies) == 1 and account_currency == presentation_currency:
|
2023-01-02 09:03:14 +00:00
|
|
|
entry["debit"] = debit_in_account_currency
|
|
|
|
entry["credit"] = credit_in_account_currency
|
2021-05-25 13:46:02 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
date = currency_info["report_date"]
|
2021-09-05 11:51:29 +00:00
|
|
|
converted_debit_value = convert(debit, presentation_currency, company_currency, date)
|
|
|
|
converted_credit_value = convert(credit, presentation_currency, company_currency, date)
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if entry.get("debit"):
|
|
|
|
entry["debit"] = converted_debit_value
|
2019-04-18 16:31:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if entry.get("credit"):
|
|
|
|
entry["credit"] = converted_credit_value
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
converted_gl_list.append(entry)
|
|
|
|
|
|
|
|
return converted_gl_list
|
|
|
|
|
|
|
|
|
|
|
|
def get_appropriate_company(filters):
|
2022-03-28 13:22:46 +00:00
|
|
|
if filters.get("company"):
|
|
|
|
company = filters["company"]
|
2018-02-12 09:34:50 +00:00
|
|
|
else:
|
|
|
|
company = get_default_company()
|
|
|
|
|
|
|
|
return company
|
2019-04-23 13:03:01 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-04-23 13:03:01 +00:00
|
|
|
@frappe.whitelist()
|
2022-03-28 13:22:46 +00:00
|
|
|
def get_invoiced_item_gross_margin(
|
|
|
|
sales_invoice=None, item_code=None, company=None, with_item_data=False
|
|
|
|
):
|
2019-04-23 13:03:01 +00:00
|
|
|
from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
|
|
|
|
item_code = item_code or frappe.form_dict.get("item_code")
|
|
|
|
company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
|
2019-04-23 13:03:01 +00:00
|
|
|
|
|
|
|
filters = {
|
2022-03-28 13:22:46 +00:00
|
|
|
"sales_invoice": sales_invoice,
|
|
|
|
"item_code": item_code,
|
|
|
|
"company": company,
|
|
|
|
"group_by": "Invoice",
|
2019-04-23 13:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gross_profit_data = GrossProfitGenerator(filters)
|
2019-05-08 10:01:29 +00:00
|
|
|
result = gross_profit_data.grouped_data
|
|
|
|
if not with_item_data:
|
2021-06-11 13:10:22 +00:00
|
|
|
result = sum(d.gross_profit for d in result)
|
2019-04-23 13:03:01 +00:00
|
|
|
|
2019-05-08 10:01:29 +00:00
|
|
|
return result
|
2023-07-04 12:11:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_query_columns(report_columns):
|
|
|
|
if not report_columns:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
columns = []
|
|
|
|
for column in report_columns:
|
|
|
|
fieldname = column["fieldname"]
|
|
|
|
|
|
|
|
if doctype := column.get("_doctype"):
|
|
|
|
columns.append(f"`{get_table_name(doctype)}`.`{fieldname}`")
|
|
|
|
else:
|
|
|
|
columns.append(fieldname)
|
|
|
|
|
|
|
|
return ", " + ", ".join(columns)
|
|
|
|
|
|
|
|
|
|
|
|
def get_values_for_columns(report_columns, report_row):
|
|
|
|
values = {}
|
|
|
|
|
|
|
|
if not report_columns:
|
|
|
|
return values
|
|
|
|
|
|
|
|
for column in report_columns:
|
|
|
|
fieldname = column["fieldname"]
|
|
|
|
values[fieldname] = report_row.get(fieldname)
|
|
|
|
|
|
|
|
return values
|