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