blob: 4490398d0d0ba58433accdffaa10a0bcf0257cd9 [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
4from frappe.utils import cint
5
6__exchange_rates = {}
7P_OR_L_ACCOUNTS = list(
8 sum(frappe.get_list('Account', fields=['name'], or_filters=[{'root_type': 'Income'}, {'root_type': 'Expense'}], as_list=True), ())
9)
10
11
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
29 report_date = filters.get('to_date') or filters.get('to_fiscal_year')
30
31 currency_map = dict(company=company, company_currency=company_currency, presentation_currency=presentation_currency, report_date=report_date)
32
33 return currency_map
34
35
36def convert(value, from_, to, date):
37 """
38 convert `value` from `from_` to `to` on `date`
39 :param value: Amount to be converted
40 :param from_: Currency of `value`
41 :param to: Currency to convert to
42 :param date: exchange rate as at this date
43 :return: Result of converting `value`
44 """
45 rate = get_rate_as_at(date, from_, to)
46 converted_value = value / (rate or 1)
47 return converted_value
48
49
50def get_rate_as_at(date, from_currency, to_currency):
51 """
52 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
53 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
54 the user's Accounts Settings.
55 It is made efficient by memoising results to `__exchange_rates`
56 :param date: exchange rate as at this date
57 :param from_currency: Base currency
58 :param to_currency: Quote currency
59 :return: Retrieved exchange rate
60 """
61 rate = __exchange_rates.get('{0}-{1}@{2}'.format(from_currency, to_currency, date))
62 if not rate:
63 rate = get_exchange_rate(from_currency, to_currency, date) or 1
64 __exchange_rates['{0}-{1}@{2}'.format(from_currency, to_currency, date)] = rate
65
66 return rate
67
68
69def is_p_or_l_account(account_name):
70 """
71 Check if the given `account name` is an `Account` with `root_type` of either 'Income'
72 or 'Expense'.
73 :param account_name:
74 :return: Boolean
75 """
76 return account_name in P_OR_L_ACCOUNTS
77
78
79def convert_to_presentation_currency(gl_entries, currency_info):
80 """
81 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
82 in `currency_info`.
83 :param gl_entries:
84 :param currency_info:
85 :return:
86 """
87 converted_gl_list = []
88 presentation_currency = currency_info['presentation_currency']
89 company_currency = currency_info['company_currency']
90
91 for entry in gl_entries:
92 account = entry['account']
93 debit = cint(entry['debit'])
94 credit = cint(entry['credit'])
95 debit_in_account_currency = cint(entry['debit_in_account_currency'])
96 credit_in_account_currency = cint(entry['credit_in_account_currency'])
97 account_currency = entry['account_currency']
98
99 if account_currency != presentation_currency or (account_currency == presentation_currency and not is_p_or_l_account(account)):
100 value = debit or credit
101
102 date = currency_info['report_date'] if not is_p_or_l_account(account) else entry['posting_date']
103
104 converted_value = convert(value, presentation_currency, company_currency, date)
105
106 if entry.get('debit'):
107 entry['debit'] = converted_value
108 else:
109 entry['credit'] = converted_value
110
111 elif account_currency == presentation_currency:
112 if entry.get('debit'):
113 entry['debit'] = debit_in_account_currency
114 else:
115 entry['credit'] = credit_in_account_currency
116
117 converted_gl_list.append(entry)
118
119 return converted_gl_list
120
121
122def get_appropriate_company(filters):
123 if filters.get('company'):
124 company = filters['company']
125 else:
126 company = get_default_company()
127
128 return company