blob: 89cc0a8c8cc081eb56fdae95cd1e4aa57fe2d199 [file] [log] [blame]
Chillar Anand915b3432021-09-02 16:44:59 +05301
tundebabzyc8978252018-02-12 10:34:50 +01002import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05303from frappe.utils import flt, formatdate, get_datetime_str
4
tundebabzyc8978252018-02-12 10:34:50 +01005from erpnext import get_company_currency, get_default_company
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05306from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Chillar Anand915b3432021-09-02 16:44:59 +05307from erpnext.setup.utils import get_exchange_rate
tundebabzyc8978252018-02-12 10:34:50 +01008
9__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +010010
11def 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)
27 presentation_currency = filters['presentation_currency'] if filters.get('presentation_currency') else company_currency
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053028
29 report_date = filters.get('to_date')
30
31 if not report_date:
Prateeksha Singh627d0d52018-07-18 18:09:28 +053032 fiscal_year_to_date = get_from_and_to_date(filters.get('to_fiscal_year'))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053033 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010034
35 currency_map = dict(company=company, company_currency=company_currency, presentation_currency=presentation_currency, report_date=report_date)
36
37 return currency_map
38
39
40def convert(value, from_, to, date):
41 """
42 convert `value` from `from_` to `to` on `date`
43 :param value: Amount to be converted
44 :param from_: Currency of `value`
45 :param to: Currency to convert to
46 :param date: exchange rate as at this date
47 :return: Result of converting `value`
48 """
49 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053050 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010051 return converted_value
52
53
54def get_rate_as_at(date, from_currency, to_currency):
55 """
56 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
57 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
58 the user's Accounts Settings.
59 It is made efficient by memoising results to `__exchange_rates`
60 :param date: exchange rate as at this date
61 :param from_currency: Base currency
62 :param to_currency: Quote currency
63 :return: Retrieved exchange rate
64 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053065
tundebabzyc8978252018-02-12 10:34:50 +010066 rate = __exchange_rates.get('{0}-{1}@{2}'.format(from_currency, to_currency, date))
67 if not rate:
68 rate = get_exchange_rate(from_currency, to_currency, date) or 1
69 __exchange_rates['{0}-{1}@{2}'.format(from_currency, to_currency, date)] = rate
70
71 return rate
72
Nabin Hait111183d2020-08-22 12:31:06 +053073def convert_to_presentation_currency(gl_entries, currency_info, company):
tundebabzyc8978252018-02-12 10:34:50 +010074 """
75 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
76 in `currency_info`.
77 :param gl_entries:
78 :param currency_info:
79 :return:
80 """
81 converted_gl_list = []
82 presentation_currency = currency_info['presentation_currency']
83 company_currency = currency_info['company_currency']
84
Afshan4d61fa22021-05-25 19:16:02 +053085 account_currencies = list(set(entry['account_currency'] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +053086
tundebabzyc8978252018-02-12 10:34:50 +010087 for entry in gl_entries:
88 account = entry['account']
Zarrar3523b772018-08-14 16:28:14 +053089 debit = flt(entry['debit'])
90 credit = flt(entry['credit'])
91 debit_in_account_currency = flt(entry['debit_in_account_currency'])
92 credit_in_account_currency = flt(entry['credit_in_account_currency'])
tundebabzyc8978252018-02-12 10:34:50 +010093 account_currency = entry['account_currency']
94
Afshan4d61fa22021-05-25 19:16:02 +053095 if len(account_currencies) == 1 and account_currency == presentation_currency:
96 if entry.get('debit'):
97 entry['debit'] = debit_in_account_currency
tundebabzyc8978252018-02-12 10:34:50 +010098
Afshan4d61fa22021-05-25 19:16:02 +053099 if entry.get('credit'):
100 entry['credit'] = credit_in_account_currency
101 else:
Afshan4d61fa22021-05-25 19:16:02 +0530102 date = currency_info['report_date']
Deepesh Gargceaa8042021-09-05 17:21:29 +0530103 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
104 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100105
106 if entry.get('debit'):
Deepesh Gargceaa8042021-09-05 17:21:29 +0530107 entry['debit'] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530108
109 if entry.get('credit'):
Deepesh Gargceaa8042021-09-05 17:21:29 +0530110 entry['credit'] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100111
tundebabzyc8978252018-02-12 10:34:50 +0100112 converted_gl_list.append(entry)
113
114 return converted_gl_list
115
116
117def get_appropriate_company(filters):
118 if filters.get('company'):
119 company = filters['company']
120 else:
121 company = get_default_company()
122
123 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530124
125@frappe.whitelist()
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530126def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530127 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
128
129 sales_invoice = sales_invoice or frappe.form_dict.get('sales_invoice')
130 item_code = item_code or frappe.form_dict.get('item_code')
131 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, 'company')
132
133 filters = {
134 'sales_invoice': sales_invoice,
135 'item_code': item_code,
136 'company': company,
137 'group_by': 'Invoice'
138 }
139
140 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530141 result = gross_profit_data.grouped_data
142 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530143 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530144
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530145 return result