blob: 97cc1c4a130fee294a0dba84f4f5546474cfac9e [file] [log] [blame]
tundebabzyc8978252018-02-12 10:34:50 +01001import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05302from frappe.utils import flt, formatdate, get_datetime_str
3
tundebabzyc8978252018-02-12 10:34:50 +01004from erpnext import get_company_currency, get_default_company
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05305from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Chillar Anand915b3432021-09-02 16:44:59 +05306from erpnext.setup.utils import get_exchange_rate
tundebabzyc8978252018-02-12 10:34:50 +01007
8__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +01009
Ankush Menat494bd9e2022-03-28 18:52:46 +053010
tundebabzyc8978252018-02-12 10:34:50 +010011def get_currency(filters):
12 """
13 Returns a dictionary containing currency information. The keys of the dict are
14 - company: The company for which we are fetching currency information. if no
15 company is specified, it will fallback to the default company.
16 - company currency: The functional currency of the said company.
17 - presentation currency: The presentation currency to use. Only currencies that
18 have been used for transactions will be allowed.
19 - report date: The report date.
20 :param filters: Report filters
21 :type filters: dict
22
23 :return: str - Currency
24 """
25 company = get_appropriate_company(filters)
26 company_currency = get_company_currency(company)
Ankush Menat494bd9e2022-03-28 18:52:46 +053027 presentation_currency = (
28 filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
29 )
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053030
ruthra kumara6794c32022-12-06 13:44:54 +053031 report_date = filters.get("to_date") or filters.get("period_end_date")
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053032
33 if not report_date:
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053035 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010036
Ankush Menat494bd9e2022-03-28 18:52:46 +053037 currency_map = dict(
38 company=company,
39 company_currency=company_currency,
40 presentation_currency=presentation_currency,
41 report_date=report_date,
42 )
tundebabzyc8978252018-02-12 10:34:50 +010043
44 return currency_map
45
46
47def convert(value, from_, to, date):
48 """
49 convert `value` from `from_` to `to` on `date`
50 :param value: Amount to be converted
51 :param from_: Currency of `value`
52 :param to: Currency to convert to
53 :param date: exchange rate as at this date
54 :return: Result of converting `value`
55 """
56 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053057 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010058 return converted_value
59
60
61def get_rate_as_at(date, from_currency, to_currency):
62 """
63 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
64 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
65 the user's Accounts Settings.
66 It is made efficient by memoising results to `__exchange_rates`
67 :param date: exchange rate as at this date
68 :param from_currency: Base currency
69 :param to_currency: Quote currency
70 :return: Retrieved exchange rate
71 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053072
Ankush Menat494bd9e2022-03-28 18:52:46 +053073 rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date))
tundebabzyc8978252018-02-12 10:34:50 +010074 if not rate:
75 rate = get_exchange_rate(from_currency, to_currency, date) or 1
Ankush Menat494bd9e2022-03-28 18:52:46 +053076 __exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate
tundebabzyc8978252018-02-12 10:34:50 +010077
78 return rate
79
Ankush Menat494bd9e2022-03-28 18:52:46 +053080
Nabin Hait111183d2020-08-22 12:31:06 +053081def convert_to_presentation_currency(gl_entries, currency_info, company):
tundebabzyc8978252018-02-12 10:34:50 +010082 """
83 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
84 in `currency_info`.
85 :param gl_entries:
86 :param currency_info:
87 :return:
88 """
89 converted_gl_list = []
Ankush Menat494bd9e2022-03-28 18:52:46 +053090 presentation_currency = currency_info["presentation_currency"]
91 company_currency = currency_info["company_currency"]
tundebabzyc8978252018-02-12 10:34:50 +010092
Ankush Menat494bd9e2022-03-28 18:52:46 +053093 account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +053094
tundebabzyc8978252018-02-12 10:34:50 +010095 for entry in gl_entries:
Ankush Menat494bd9e2022-03-28 18:52:46 +053096 account = entry["account"]
97 debit = flt(entry["debit"])
98 credit = flt(entry["credit"])
99 debit_in_account_currency = flt(entry["debit_in_account_currency"])
100 credit_in_account_currency = flt(entry["credit_in_account_currency"])
101 account_currency = entry["account_currency"]
tundebabzyc8978252018-02-12 10:34:50 +0100102
Afshan4d61fa22021-05-25 19:16:02 +0530103 if len(account_currencies) == 1 and account_currency == presentation_currency:
ruthra kumar914b2302023-01-02 14:33:14 +0530104 entry["debit"] = debit_in_account_currency
105 entry["credit"] = credit_in_account_currency
Afshan4d61fa22021-05-25 19:16:02 +0530106 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530107 date = currency_info["report_date"]
Deepesh Gargceaa8042021-09-05 17:21:29 +0530108 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
109 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100110
Ankush Menat494bd9e2022-03-28 18:52:46 +0530111 if entry.get("debit"):
112 entry["debit"] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530113
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114 if entry.get("credit"):
115 entry["credit"] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100116
tundebabzyc8978252018-02-12 10:34:50 +0100117 converted_gl_list.append(entry)
118
119 return converted_gl_list
120
121
122def get_appropriate_company(filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530123 if filters.get("company"):
124 company = filters["company"]
tundebabzyc8978252018-02-12 10:34:50 +0100125 else:
126 company = get_default_company()
127
128 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530129
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530131@frappe.whitelist()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530132def get_invoiced_item_gross_margin(
133 sales_invoice=None, item_code=None, company=None, with_item_data=False
134):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530135 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
136
Ankush Menat494bd9e2022-03-28 18:52:46 +0530137 sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
138 item_code = item_code or frappe.form_dict.get("item_code")
139 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530140
141 filters = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530142 "sales_invoice": sales_invoice,
143 "item_code": item_code,
144 "company": company,
145 "group_by": "Invoice",
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530146 }
147
148 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530149 result = gross_profit_data.grouped_data
150 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530151 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530152
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530153 return result