tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 1 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | from frappe.utils import flt, formatdate, get_datetime_str |
| 3 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 4 | from erpnext import get_company_currency, get_default_company |
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 |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | from erpnext.setup.utils import get_exchange_rate |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 7 | |
| 8 | __exchange_rates = {} |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 9 | |
| 10 | def 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 Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 27 | |
| 28 | report_date = filters.get('to_date') |
| 29 | |
| 30 | if not report_date: |
Prateeksha Singh | 627d0d5 | 2018-07-18 18:09:28 +0530 | [diff] [blame] | 31 | 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] | 32 | report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy") |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 33 | |
| 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 | |
| 39 | def 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) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 49 | converted_value = flt(value) / (rate or 1) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 50 | return converted_value |
| 51 | |
| 52 | |
| 53 | def 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 Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 64 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 65 | 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 Hait | 111183d | 2020-08-22 12:31:06 +0530 | [diff] [blame] | 72 | def convert_to_presentation_currency(gl_entries, currency_info, company): |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 73 | """ |
| 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 | |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 84 | account_currencies = list(set(entry['account_currency'] for entry in gl_entries)) |
Nabin Hait | 111183d | 2020-08-22 12:31:06 +0530 | [diff] [blame] | 85 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 86 | for entry in gl_entries: |
| 87 | account = entry['account'] |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 88 | 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']) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 92 | account_currency = entry['account_currency'] |
| 93 | |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 94 | if len(account_currencies) == 1 and account_currency == presentation_currency: |
Saqib Ansari | 83a5fae | 2022-03-14 20:14:46 +0530 | [diff] [blame] | 95 | if debit_in_account_currency: |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 96 | entry['debit'] = debit_in_account_currency |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 97 | |
Saqib Ansari | 83a5fae | 2022-03-14 20:14:46 +0530 | [diff] [blame] | 98 | if credit_in_account_currency: |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 99 | entry['credit'] = credit_in_account_currency |
| 100 | else: |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 101 | date = currency_info['report_date'] |
Deepesh Garg | ceaa804 | 2021-09-05 17:21:29 +0530 | [diff] [blame] | 102 | converted_debit_value = convert(debit, presentation_currency, company_currency, date) |
| 103 | converted_credit_value = convert(credit, presentation_currency, company_currency, date) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 104 | |
| 105 | if entry.get('debit'): |
Deepesh Garg | ceaa804 | 2021-09-05 17:21:29 +0530 | [diff] [blame] | 106 | entry['debit'] = converted_debit_value |
Rohit Waghchaure | 376db4f | 2019-04-18 22:01:33 +0530 | [diff] [blame] | 107 | |
| 108 | if entry.get('credit'): |
Deepesh Garg | ceaa804 | 2021-09-05 17:21:29 +0530 | [diff] [blame] | 109 | entry['credit'] = converted_credit_value |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 110 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 111 | converted_gl_list.append(entry) |
| 112 | |
| 113 | return converted_gl_list |
| 114 | |
| 115 | |
| 116 | def 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 Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 123 | |
| 124 | @frappe.whitelist() |
Rohit Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 125 | def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False): |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 126 | 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 Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 140 | result = gross_profit_data.grouped_data |
| 141 | if not with_item_data: |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 142 | result = sum(d.gross_profit for d in result) |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 143 | |
Rohit Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 144 | return result |