blob: 2f9e9578ccb1df47616f627a9f60b2386b4375f2 [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
tundebabzyc8978252018-02-12 10:34:50 +01003import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05304from frappe.utils import flt, formatdate, get_datetime_str
5
tundebabzyc8978252018-02-12 10:34:50 +01006from erpnext import get_company_currency, get_default_company
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05307from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Chillar Anand915b3432021-09-02 16:44:59 +05308from erpnext.setup.utils import get_exchange_rate
tundebabzyc8978252018-02-12 10:34:50 +01009
10__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +010011
12def get_currency(filters):
13 """
14 Returns a dictionary containing currency information. The keys of the dict are
15 - company: The company for which we are fetching currency information. if no
16 company is specified, it will fallback to the default company.
17 - company currency: The functional currency of the said company.
18 - presentation currency: The presentation currency to use. Only currencies that
19 have been used for transactions will be allowed.
20 - report date: The report date.
21 :param filters: Report filters
22 :type filters: dict
23
24 :return: str - Currency
25 """
26 company = get_appropriate_company(filters)
27 company_currency = get_company_currency(company)
28 presentation_currency = filters['presentation_currency'] if filters.get('presentation_currency') else company_currency
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053029
30 report_date = filters.get('to_date')
31
32 if not report_date:
Prateeksha Singh627d0d52018-07-18 18:09:28 +053033 fiscal_year_to_date = get_from_and_to_date(filters.get('to_fiscal_year'))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053034 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010035
36 currency_map = dict(company=company, company_currency=company_currency, presentation_currency=presentation_currency, report_date=report_date)
37
38 return currency_map
39
40
41def convert(value, from_, to, date):
42 """
43 convert `value` from `from_` to `to` on `date`
44 :param value: Amount to be converted
45 :param from_: Currency of `value`
46 :param to: Currency to convert to
47 :param date: exchange rate as at this date
48 :return: Result of converting `value`
49 """
50 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053051 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010052 return converted_value
53
54
55def get_rate_as_at(date, from_currency, to_currency):
56 """
57 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
58 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
59 the user's Accounts Settings.
60 It is made efficient by memoising results to `__exchange_rates`
61 :param date: exchange rate as at this date
62 :param from_currency: Base currency
63 :param to_currency: Quote currency
64 :return: Retrieved exchange rate
65 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053066
tundebabzyc8978252018-02-12 10:34:50 +010067 rate = __exchange_rates.get('{0}-{1}@{2}'.format(from_currency, to_currency, date))
68 if not rate:
69 rate = get_exchange_rate(from_currency, to_currency, date) or 1
70 __exchange_rates['{0}-{1}@{2}'.format(from_currency, to_currency, date)] = rate
71
72 return rate
73
Nabin Hait111183d2020-08-22 12:31:06 +053074def convert_to_presentation_currency(gl_entries, currency_info, company):
tundebabzyc8978252018-02-12 10:34:50 +010075 """
76 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
77 in `currency_info`.
78 :param gl_entries:
79 :param currency_info:
80 :return:
81 """
82 converted_gl_list = []
83 presentation_currency = currency_info['presentation_currency']
84 company_currency = currency_info['company_currency']
85
Afshan4d61fa22021-05-25 19:16:02 +053086 account_currencies = list(set(entry['account_currency'] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +053087
tundebabzyc8978252018-02-12 10:34:50 +010088 for entry in gl_entries:
89 account = entry['account']
Zarrar3523b772018-08-14 16:28:14 +053090 debit = flt(entry['debit'])
91 credit = flt(entry['credit'])
92 debit_in_account_currency = flt(entry['debit_in_account_currency'])
93 credit_in_account_currency = flt(entry['credit_in_account_currency'])
tundebabzyc8978252018-02-12 10:34:50 +010094 account_currency = entry['account_currency']
95
Afshan4d61fa22021-05-25 19:16:02 +053096 if len(account_currencies) == 1 and account_currency == presentation_currency:
97 if entry.get('debit'):
98 entry['debit'] = debit_in_account_currency
tundebabzyc8978252018-02-12 10:34:50 +010099
Afshan4d61fa22021-05-25 19:16:02 +0530100 if entry.get('credit'):
101 entry['credit'] = credit_in_account_currency
102 else:
Afshan4d61fa22021-05-25 19:16:02 +0530103 date = currency_info['report_date']
Deepesh Gargceaa8042021-09-05 17:21:29 +0530104 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
105 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100106
107 if entry.get('debit'):
Deepesh Gargceaa8042021-09-05 17:21:29 +0530108 entry['debit'] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530109
110 if entry.get('credit'):
Deepesh Gargceaa8042021-09-05 17:21:29 +0530111 entry['credit'] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100112
tundebabzyc8978252018-02-12 10:34:50 +0100113 converted_gl_list.append(entry)
114
115 return converted_gl_list
116
117
118def get_appropriate_company(filters):
119 if filters.get('company'):
120 company = filters['company']
121 else:
122 company = get_default_company()
123
124 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530125
126@frappe.whitelist()
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530127def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530128 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
129
130 sales_invoice = sales_invoice or frappe.form_dict.get('sales_invoice')
131 item_code = item_code or frappe.form_dict.get('item_code')
132 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, 'company')
133
134 filters = {
135 'sales_invoice': sales_invoice,
136 'item_code': item_code,
137 'company': company,
138 'group_by': 'Invoice'
139 }
140
141 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530142 result = gross_profit_data.grouped_data
143 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530144 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530145
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530146 return result