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 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 10 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 11 | def get_currency(filters): |
| 12 | """ |
| 13 | Returns a dictionary containing currency information. The keys of the dict are |
| 14 | - company: The company for which we are fetching currency information. if no |
| 15 | company is specified, it will fallback to the default company. |
| 16 | - company currency: The functional currency of the said company. |
| 17 | - presentation currency: The presentation currency to use. Only currencies that |
| 18 | have been used for transactions will be allowed. |
| 19 | - report date: The report date. |
| 20 | :param filters: Report filters |
| 21 | :type filters: dict |
| 22 | |
| 23 | :return: str - Currency |
| 24 | """ |
| 25 | company = get_appropriate_company(filters) |
| 26 | company_currency = get_company_currency(company) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 27 | presentation_currency = ( |
| 28 | filters["presentation_currency"] if filters.get("presentation_currency") else company_currency |
| 29 | ) |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 30 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 31 | report_date = filters.get("to_date") |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 32 | |
| 33 | if not report_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 34 | 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] | 35 | report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy") |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 36 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 37 | currency_map = dict( |
| 38 | company=company, |
| 39 | company_currency=company_currency, |
| 40 | presentation_currency=presentation_currency, |
| 41 | report_date=report_date, |
| 42 | ) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 43 | |
| 44 | return currency_map |
| 45 | |
| 46 | |
| 47 | def convert(value, from_, to, date): |
| 48 | """ |
| 49 | convert `value` from `from_` to `to` on `date` |
| 50 | :param value: Amount to be converted |
| 51 | :param from_: Currency of `value` |
| 52 | :param to: Currency to convert to |
| 53 | :param date: exchange rate as at this date |
| 54 | :return: Result of converting `value` |
| 55 | """ |
| 56 | rate = get_rate_as_at(date, from_, to) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 57 | converted_value = flt(value) / (rate or 1) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 58 | return converted_value |
| 59 | |
| 60 | |
| 61 | def get_rate_as_at(date, from_currency, to_currency): |
| 62 | """ |
| 63 | Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate. |
| 64 | This calls `get_exchange_rate` so that we can get the correct exchange rate as per |
| 65 | the user's Accounts Settings. |
| 66 | It is made efficient by memoising results to `__exchange_rates` |
| 67 | :param date: exchange rate as at this date |
| 68 | :param from_currency: Base currency |
| 69 | :param to_currency: Quote currency |
| 70 | :return: Retrieved exchange rate |
| 71 | """ |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 72 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 73 | rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date)) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 74 | if not rate: |
| 75 | rate = get_exchange_rate(from_currency, to_currency, date) or 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | __exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 77 | |
| 78 | return rate |
| 79 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | |
Nabin Hait | 111183d | 2020-08-22 12:31:06 +0530 | [diff] [blame] | 81 | def convert_to_presentation_currency(gl_entries, currency_info, company): |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 82 | """ |
| 83 | Take a list of GL Entries and change the 'debit' and 'credit' values to currencies |
| 84 | in `currency_info`. |
| 85 | :param gl_entries: |
| 86 | :param currency_info: |
| 87 | :return: |
| 88 | """ |
| 89 | converted_gl_list = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 90 | presentation_currency = currency_info["presentation_currency"] |
| 91 | company_currency = currency_info["company_currency"] |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 92 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | account_currencies = list(set(entry["account_currency"] for entry in gl_entries)) |
Nabin Hait | 111183d | 2020-08-22 12:31:06 +0530 | [diff] [blame] | 94 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 95 | for entry in gl_entries: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 96 | account = entry["account"] |
| 97 | debit = flt(entry["debit"]) |
| 98 | credit = flt(entry["credit"]) |
| 99 | debit_in_account_currency = flt(entry["debit_in_account_currency"]) |
| 100 | credit_in_account_currency = flt(entry["credit_in_account_currency"]) |
| 101 | account_currency = entry["account_currency"] |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 102 | |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 103 | if len(account_currencies) == 1 and account_currency == presentation_currency: |
Saqib Ansari | 83a5fae | 2022-03-14 20:14:46 +0530 | [diff] [blame] | 104 | if debit_in_account_currency: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 105 | entry["debit"] = debit_in_account_currency |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 106 | |
Saqib Ansari | 83a5fae | 2022-03-14 20:14:46 +0530 | [diff] [blame] | 107 | if credit_in_account_currency: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 108 | entry["credit"] = credit_in_account_currency |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 109 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 110 | date = currency_info["report_date"] |
Deepesh Garg | ceaa804 | 2021-09-05 17:21:29 +0530 | [diff] [blame] | 111 | converted_debit_value = convert(debit, presentation_currency, company_currency, date) |
| 112 | converted_credit_value = convert(credit, presentation_currency, company_currency, date) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 113 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | if entry.get("debit"): |
| 115 | entry["debit"] = converted_debit_value |
Rohit Waghchaure | 376db4f | 2019-04-18 22:01:33 +0530 | [diff] [blame] | 116 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 117 | if entry.get("credit"): |
| 118 | entry["credit"] = converted_credit_value |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 119 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 120 | converted_gl_list.append(entry) |
| 121 | |
| 122 | return converted_gl_list |
| 123 | |
| 124 | |
| 125 | def get_appropriate_company(filters): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 126 | if filters.get("company"): |
| 127 | company = filters["company"] |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 128 | else: |
| 129 | company = get_default_company() |
| 130 | |
| 131 | return company |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 132 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 133 | |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 134 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 135 | def get_invoiced_item_gross_margin( |
| 136 | sales_invoice=None, item_code=None, company=None, with_item_data=False |
| 137 | ): |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 138 | from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator |
| 139 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 140 | sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice") |
| 141 | item_code = item_code or frappe.form_dict.get("item_code") |
| 142 | company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company") |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 143 | |
| 144 | filters = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 145 | "sales_invoice": sales_invoice, |
| 146 | "item_code": item_code, |
| 147 | "company": company, |
| 148 | "group_by": "Invoice", |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | gross_profit_data = GrossProfitGenerator(filters) |
Rohit Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 152 | result = gross_profit_data.grouped_data |
| 153 | if not with_item_data: |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 154 | result = sum(d.gross_profit for d in result) |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 155 | |
Rohit Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 156 | return result |