blob: e33bd832afb3c07613e557d1d128585f4270f417 [file] [log] [blame]
tundebabzyc8978252018-02-12 10:34:50 +01001import frappe
2from erpnext import get_company_currency, get_default_company
3from erpnext.setup.utils import get_exchange_rate
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05304from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Zarrar3523b772018-08-14 16:28:14 +05305from frappe.utils import cint, get_datetime_str, formatdate, flt
tundebabzyc8978252018-02-12 10:34:50 +01006
7__exchange_rates = {}
8P_OR_L_ACCOUNTS = list(
9 sum(frappe.get_list('Account', fields=['name'], or_filters=[{'root_type': 'Income'}, {'root_type': 'Expense'}], as_list=True), ())
10)
11
12
13def get_currency(filters):
14 """
15 Returns a dictionary containing currency information. The keys of the dict are
16 - company: The company for which we are fetching currency information. if no
17 company is specified, it will fallback to the default company.
18 - company currency: The functional currency of the said company.
19 - presentation currency: The presentation currency to use. Only currencies that
20 have been used for transactions will be allowed.
21 - report date: The report date.
22 :param filters: Report filters
23 :type filters: dict
24
25 :return: str - Currency
26 """
27 company = get_appropriate_company(filters)
28 company_currency = get_company_currency(company)
29 presentation_currency = filters['presentation_currency'] if filters.get('presentation_currency') else company_currency
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053030
31 report_date = filters.get('to_date')
32
33 if not report_date:
Prateeksha Singh627d0d52018-07-18 18:09:28 +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
37 currency_map = dict(company=company, company_currency=company_currency, presentation_currency=presentation_currency, report_date=report_date)
38
39 return currency_map
40
41
42def convert(value, from_, to, date):
43 """
44 convert `value` from `from_` to `to` on `date`
45 :param value: Amount to be converted
46 :param from_: Currency of `value`
47 :param to: Currency to convert to
48 :param date: exchange rate as at this date
49 :return: Result of converting `value`
50 """
51 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053052 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010053 return converted_value
54
55
56def get_rate_as_at(date, from_currency, to_currency):
57 """
58 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
59 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
60 the user's Accounts Settings.
61 It is made efficient by memoising results to `__exchange_rates`
62 :param date: exchange rate as at this date
63 :param from_currency: Base currency
64 :param to_currency: Quote currency
65 :return: Retrieved exchange rate
66 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053067
tundebabzyc8978252018-02-12 10:34:50 +010068 rate = __exchange_rates.get('{0}-{1}@{2}'.format(from_currency, to_currency, date))
69 if not rate:
70 rate = get_exchange_rate(from_currency, to_currency, date) or 1
71 __exchange_rates['{0}-{1}@{2}'.format(from_currency, to_currency, date)] = rate
72
73 return rate
74
75
76def is_p_or_l_account(account_name):
77 """
78 Check if the given `account name` is an `Account` with `root_type` of either 'Income'
79 or 'Expense'.
80 :param account_name:
81 :return: Boolean
82 """
83 return account_name in P_OR_L_ACCOUNTS
84
85
86def convert_to_presentation_currency(gl_entries, currency_info):
87 """
88 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
89 in `currency_info`.
90 :param gl_entries:
91 :param currency_info:
92 :return:
93 """
94 converted_gl_list = []
95 presentation_currency = currency_info['presentation_currency']
96 company_currency = currency_info['company_currency']
97
98 for entry in gl_entries:
99 account = entry['account']
Zarrar3523b772018-08-14 16:28:14 +0530100 debit = flt(entry['debit'])
101 credit = flt(entry['credit'])
102 debit_in_account_currency = flt(entry['debit_in_account_currency'])
103 credit_in_account_currency = flt(entry['credit_in_account_currency'])
tundebabzyc8978252018-02-12 10:34:50 +0100104 account_currency = entry['account_currency']
105
106 if account_currency != presentation_currency or (account_currency == presentation_currency and not is_p_or_l_account(account)):
107 value = debit or credit
108
109 date = currency_info['report_date'] if not is_p_or_l_account(account) else entry['posting_date']
tundebabzyc8978252018-02-12 10:34:50 +0100110 converted_value = convert(value, presentation_currency, company_currency, date)
111
112 if entry.get('debit'):
113 entry['debit'] = converted_value
114 else:
115 entry['credit'] = converted_value
116
117 elif account_currency == presentation_currency:
118 if entry.get('debit'):
119 entry['debit'] = debit_in_account_currency
120 else:
121 entry['credit'] = credit_in_account_currency
122
123 converted_gl_list.append(entry)
124
125 return converted_gl_list
126
127
128def get_appropriate_company(filters):
129 if filters.get('company'):
130 company = filters['company']
131 else:
132 company = get_default_company()
133
134 return company