blob: 474ae3490a97ac9915267d6571efdd845d13b0eb [file] [log] [blame]
tundebabzyc8978252018-02-12 10:34:50 +01001import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05302from frappe.utils import flt, formatdate, get_datetime_str
3
tundebabzyc8978252018-02-12 10:34:50 +01004from erpnext import get_company_currency, get_default_company
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +05305from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Chillar Anand915b3432021-09-02 16:44:59 +05306from erpnext.setup.utils import get_exchange_rate
tundebabzyc8978252018-02-12 10:34:50 +01007
8__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +01009
Ankush Menat494bd9e2022-03-28 18:52:46 +053010
tundebabzyc8978252018-02-12 10:34:50 +010011def 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 Menat494bd9e2022-03-28 18:52:46 +053027 presentation_currency = (
28 filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
29 )
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053030
ruthra kumara6794c32022-12-06 13:44:54 +053031 report_date = filters.get("to_date") or filters.get("period_end_date")
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053032
33 if not report_date:
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053035 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010036
Ankush Menat494bd9e2022-03-28 18:52:46 +053037 currency_map = dict(
38 company=company,
39 company_currency=company_currency,
40 presentation_currency=presentation_currency,
41 report_date=report_date,
42 )
tundebabzyc8978252018-02-12 10:34:50 +010043
44 return currency_map
45
46
47def 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)
Zarrar3523b772018-08-14 16:28:14 +053057 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010058 return converted_value
59
60
61def 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 Singh8ecfaaa2018-07-18 18:03:27 +053072
Ankush Menat494bd9e2022-03-28 18:52:46 +053073 rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date))
tundebabzyc8978252018-02-12 10:34:50 +010074 if not rate:
75 rate = get_exchange_rate(from_currency, to_currency, date) or 1
Ankush Menat494bd9e2022-03-28 18:52:46 +053076 __exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate
tundebabzyc8978252018-02-12 10:34:50 +010077
78 return rate
79
Ankush Menat494bd9e2022-03-28 18:52:46 +053080
Nabin Hait111183d2020-08-22 12:31:06 +053081def convert_to_presentation_currency(gl_entries, currency_info, company):
tundebabzyc8978252018-02-12 10:34:50 +010082 """
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 Menat494bd9e2022-03-28 18:52:46 +053090 presentation_currency = currency_info["presentation_currency"]
91 company_currency = currency_info["company_currency"]
tundebabzyc8978252018-02-12 10:34:50 +010092
Ankush Menat494bd9e2022-03-28 18:52:46 +053093 account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +053094
tundebabzyc8978252018-02-12 10:34:50 +010095 for entry in gl_entries:
Ankush Menat494bd9e2022-03-28 18:52:46 +053096 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"]
tundebabzyc8978252018-02-12 10:34:50 +0100102
Afshan4d61fa22021-05-25 19:16:02 +0530103 if len(account_currencies) == 1 and account_currency == presentation_currency:
ruthra kumar914b2302023-01-02 14:33:14 +0530104 entry["debit"] = debit_in_account_currency
105 entry["credit"] = credit_in_account_currency
Afshan4d61fa22021-05-25 19:16:02 +0530106 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530107 date = currency_info["report_date"]
Deepesh Gargceaa8042021-09-05 17:21:29 +0530108 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
109 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100110
Ankush Menat494bd9e2022-03-28 18:52:46 +0530111 if entry.get("debit"):
112 entry["debit"] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530113
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114 if entry.get("credit"):
115 entry["credit"] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100116
tundebabzyc8978252018-02-12 10:34:50 +0100117 converted_gl_list.append(entry)
118
119 return converted_gl_list
120
121
122def get_appropriate_company(filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530123 if filters.get("company"):
124 company = filters["company"]
tundebabzyc8978252018-02-12 10:34:50 +0100125 else:
126 company = get_default_company()
127
128 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530129
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530131@frappe.whitelist()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530132def get_invoiced_item_gross_margin(
133 sales_invoice=None, item_code=None, company=None, with_item_data=False
134):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530135 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
136
Ankush Menat494bd9e2022-03-28 18:52:46 +0530137 sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
138 item_code = item_code or frappe.form_dict.get("item_code")
139 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530140
141 filters = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530142 "sales_invoice": sales_invoice,
143 "item_code": item_code,
144 "company": company,
145 "group_by": "Invoice",
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530146 }
147
148 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530149 result = gross_profit_data.grouped_data
150 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530151 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530152
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530153 return result
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530154
155
156def get_party_details(party_type, party_list):
157 party_details = {}
158 party = frappe.qb.DocType(party_type)
159 query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
160 if party_type == "Supplier":
161 query = query.select(party.supplier_group)
162 else:
163 query = query.select(party.customer_group, party.territory)
164
165 party_detail_list = query.run(as_dict=True)
166 for party_dict in party_detail_list:
167 party_details[party_dict.name] = party_dict
168 return party_details
169
170
171def get_taxes_query(invoice_list, doctype, parenttype):
172 taxes = frappe.qb.DocType(doctype)
173
174 query = (
175 frappe.qb.from_(taxes)
176 .select(taxes.account_head)
177 .distinct()
178 .where(
179 (taxes.parenttype == parenttype)
180 & (taxes.docstatus == 1)
181 & (taxes.account_head.isnotnull())
182 & (taxes.parent.isin([inv.name for inv in invoice_list]))
183 )
184 .orderby(taxes.account_head)
185 )
186
187 if doctype == "Purchase Taxes and Charges":
188 return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
189 elif doctype == "Sales Taxes and Charges":
190 return query.where(taxes.charge_type.isin(["Total", "Valuation and Total"]))
191 return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530192
193
194def get_journal_entries(filters, args):
195 return frappe.db.sql(
196 """
197 select je.voucher_type as doctype, je.name, je.posting_date,
198 jea.account as {0}, jea.party as {1}, jea.party as {2},
199 je.bill_no, je.bill_date, je.remark, je.total_amount as base_net_total,
200 je.total_amount as base_grand_total, je.mode_of_payment, jea.project {3}
201 from `tabJournal Entry` je left join `tabJournal Entry Account` jea on jea.parent=je.name
Gursheen Anand7650b002023-07-11 15:18:28 +0530202 where je.voucher_type='Journal Entry' and jea.party='{4}' {5}
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530203 order by je.posting_date desc, je.name desc""".format(
204 args.account,
205 args.party,
206 args.party_name,
207 args.additional_query_columns,
Gursheen Anand7650b002023-07-11 15:18:28 +0530208 filters.get(args.party),
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530209 args.conditions,
210 ),
211 filters,
212 as_dict=1,
213 )
214
215
216def get_payment_entries(filters, args):
217 return frappe.db.sql(
218 """
219 select 'Payment Entry' as doctype, name, posting_date, paid_to as {0},
220 party as {1}, party_name as {2}, remarks,
221 paid_amount as base_net_total, paid_amount_after_tax as base_grand_total,
222 mode_of_payment, project, cost_center {3}
223 from `tabPayment Entry`
Gursheen Anand7650b002023-07-11 15:18:28 +0530224 where party='{4}' {5}
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530225 order by posting_date desc, name desc""".format(
226 args.account,
227 args.party,
228 args.party_name,
229 args.additional_query_columns,
Gursheen Anand7650b002023-07-11 15:18:28 +0530230 filters.get(args.party),
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530231 args.conditions,
232 ),
233 filters,
234 as_dict=1,
235 )