blob: 4a9af490cfcc886c0e05a0d7e1a6961532a9c9b0 [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
tundebabzyc8978252018-02-12 10:34:50 +01002import frappe
3from erpnext import get_company_currency, get_default_company
4from erpnext.setup.utils import get_exchange_rate
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05305from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Zarrar3523b772018-08-14 16:28:14 +05306from frappe.utils import cint, get_datetime_str, formatdate, flt
tundebabzyc8978252018-02-12 10:34:50 +01007
8__exchange_rates = {}
9P_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
14def 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 Singh8ecfaaa2018-07-18 18:03:27 +053031
32 report_date = filters.get('to_date')
33
34 if not report_date:
Prateeksha Singh627d0d52018-07-18 18:09:28 +053035 fiscal_year_to_date = get_from_and_to_date(filters.get('to_fiscal_year'))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053036 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010037
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
43def 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)
Zarrar3523b772018-08-14 16:28:14 +053053 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010054 return converted_value
55
56
57def 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 Singh8ecfaaa2018-07-18 18:03:27 +053068
tundebabzyc8978252018-02-12 10:34:50 +010069 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
77def 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
87def 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']
Zarrar3523b772018-08-14 16:28:14 +0530101 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'])
tundebabzyc8978252018-02-12 10:34:50 +0100105 account_currency = entry['account_currency']
106
Nabin Hait64470692019-02-12 16:41:20 +0530107 if account_currency != presentation_currency:
tundebabzyc8978252018-02-12 10:34:50 +0100108 value = debit or credit
109
110 date = currency_info['report_date'] if not is_p_or_l_account(account) else entry['posting_date']
tundebabzyc8978252018-02-12 10:34:50 +0100111 converted_value = convert(value, presentation_currency, company_currency, date)
112
113 if entry.get('debit'):
114 entry['debit'] = converted_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530115
116 if entry.get('credit'):
tundebabzyc8978252018-02-12 10:34:50 +0100117 entry['credit'] = converted_value
118
119 elif account_currency == presentation_currency:
120 if entry.get('debit'):
121 entry['debit'] = debit_in_account_currency
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530122
123 if entry.get('credit'):
tundebabzyc8978252018-02-12 10:34:50 +0100124 entry['credit'] = credit_in_account_currency
125
126 converted_gl_list.append(entry)
127
128 return converted_gl_list
129
130
131def get_appropriate_company(filters):
132 if filters.get('company'):
133 company = filters['company']
134 else:
135 company = get_default_company()
136
137 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530138
139@frappe.whitelist()
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530140def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530141 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
142
143 sales_invoice = sales_invoice or frappe.form_dict.get('sales_invoice')
144 item_code = item_code or frappe.form_dict.get('item_code')
145 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, 'company')
146
147 filters = {
148 'sales_invoice': sales_invoice,
149 'item_code': item_code,
150 'company': company,
151 'group_by': 'Invoice'
152 }
153
154 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530155 result = gross_profit_data.grouped_data
156 if not with_item_data:
157 result = sum([d.gross_profit for d in result])
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530158
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530159 return result