blob: ba461edaf868b67d85fa7937a1db2cf6d879ac83 [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
tundebabzyc8978252018-02-12 10:34:50 +01002import frappe
3from erpnext import get_company_currency, get_default_company
4from erpnext.setup.utils import get_exchange_rate
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05305from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Zarrar3523b772018-08-14 16:28:14 +05306from frappe.utils import cint, get_datetime_str, formatdate, flt
tundebabzyc8978252018-02-12 10:34:50 +01007
8__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +01009
10def get_currency(filters):
11 """
12 Returns a dictionary containing currency information. The keys of the dict are
13 - company: The company for which we are fetching currency information. if no
14 company is specified, it will fallback to the default company.
15 - company currency: The functional currency of the said company.
16 - presentation currency: The presentation currency to use. Only currencies that
17 have been used for transactions will be allowed.
18 - report date: The report date.
19 :param filters: Report filters
20 :type filters: dict
21
22 :return: str - Currency
23 """
24 company = get_appropriate_company(filters)
25 company_currency = get_company_currency(company)
26 presentation_currency = filters['presentation_currency'] if filters.get('presentation_currency') else company_currency
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053027
28 report_date = filters.get('to_date')
29
30 if not report_date:
Prateeksha Singh627d0d52018-07-18 18:09:28 +053031 fiscal_year_to_date = get_from_and_to_date(filters.get('to_fiscal_year'))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053032 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010033
34 currency_map = dict(company=company, company_currency=company_currency, presentation_currency=presentation_currency, report_date=report_date)
35
36 return currency_map
37
38
39def convert(value, from_, to, date):
40 """
41 convert `value` from `from_` to `to` on `date`
42 :param value: Amount to be converted
43 :param from_: Currency of `value`
44 :param to: Currency to convert to
45 :param date: exchange rate as at this date
46 :return: Result of converting `value`
47 """
48 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053049 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010050 return converted_value
51
52
53def get_rate_as_at(date, from_currency, to_currency):
54 """
55 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
56 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
57 the user's Accounts Settings.
58 It is made efficient by memoising results to `__exchange_rates`
59 :param date: exchange rate as at this date
60 :param from_currency: Base currency
61 :param to_currency: Quote currency
62 :return: Retrieved exchange rate
63 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053064
tundebabzyc8978252018-02-12 10:34:50 +010065 rate = __exchange_rates.get('{0}-{1}@{2}'.format(from_currency, to_currency, date))
66 if not rate:
67 rate = get_exchange_rate(from_currency, to_currency, date) or 1
68 __exchange_rates['{0}-{1}@{2}'.format(from_currency, to_currency, date)] = rate
69
70 return rate
71
Nabin Hait111183d2020-08-22 12:31:06 +053072def convert_to_presentation_currency(gl_entries, currency_info, company):
tundebabzyc8978252018-02-12 10:34:50 +010073 """
74 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
75 in `currency_info`.
76 :param gl_entries:
77 :param currency_info:
78 :return:
79 """
80 converted_gl_list = []
81 presentation_currency = currency_info['presentation_currency']
82 company_currency = currency_info['company_currency']
83
Afshan4d61fa22021-05-25 19:16:02 +053084 account_currencies = list(set(entry['account_currency'] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +053085
tundebabzyc8978252018-02-12 10:34:50 +010086 for entry in gl_entries:
87 account = entry['account']
Zarrar3523b772018-08-14 16:28:14 +053088 debit = flt(entry['debit'])
89 credit = flt(entry['credit'])
90 debit_in_account_currency = flt(entry['debit_in_account_currency'])
91 credit_in_account_currency = flt(entry['credit_in_account_currency'])
tundebabzyc8978252018-02-12 10:34:50 +010092 account_currency = entry['account_currency']
93
Afshan4d61fa22021-05-25 19:16:02 +053094 if len(account_currencies) == 1 and account_currency == presentation_currency:
95 if entry.get('debit'):
96 entry['debit'] = debit_in_account_currency
tundebabzyc8978252018-02-12 10:34:50 +010097
Afshan4d61fa22021-05-25 19:16:02 +053098 if entry.get('credit'):
99 entry['credit'] = credit_in_account_currency
100 else:
101 value = debit or credit
102 date = currency_info['report_date']
tundebabzyc8978252018-02-12 10:34:50 +0100103 converted_value = convert(value, presentation_currency, company_currency, date)
104
105 if entry.get('debit'):
106 entry['debit'] = converted_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530107
108 if entry.get('credit'):
tundebabzyc8978252018-02-12 10:34:50 +0100109 entry['credit'] = converted_value
110
tundebabzyc8978252018-02-12 10:34:50 +0100111 converted_gl_list.append(entry)
112
113 return converted_gl_list
114
115
116def get_appropriate_company(filters):
117 if filters.get('company'):
118 company = filters['company']
119 else:
120 company = get_default_company()
121
122 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530123
124@frappe.whitelist()
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530125def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530126 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
127
128 sales_invoice = sales_invoice or frappe.form_dict.get('sales_invoice')
129 item_code = item_code or frappe.form_dict.get('item_code')
130 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, 'company')
131
132 filters = {
133 'sales_invoice': sales_invoice,
134 'item_code': item_code,
135 'company': company,
136 'group_by': 'Invoice'
137 }
138
139 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530140 result = gross_profit_data.grouped_data
141 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530142 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530143
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530144 return result