blob: bd1b35559eaaf27f36ee0c6709a88338f6982983 [file] [log] [blame]
tundebabzyc8978252018-02-12 10:34:50 +01001import frappe
Gursheen Anand6c11ca12023-07-12 14:43:18 +05302from frappe.query_builder.custom import ConstantColumn
Gursheen Anandf5027fd2023-07-12 16:42:58 +05303from frappe.query_builder.functions import Sum
Sagar Vora30e40522023-07-04 17:41:30 +05304from frappe.utils import flt, formatdate, get_datetime_str, get_table_name
Gursheen Anand6c11ca12023-07-12 14:43:18 +05305from pypika import Order
Chillar Anand915b3432021-09-02 16:44:59 +05306
tundebabzyc8978252018-02-12 10:34:50 +01007from erpnext import get_company_currency, get_default_company
Gursheen Anand6c11ca12023-07-12 14:43:18 +05308from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
Gursheen Anandc084fe62023-07-14 11:05:50 +05309 get_accounting_dimensions,
Gursheen Anand6c11ca12023-07-12 14:43:18 +053010 get_dimension_with_children,
11)
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053012from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Gursheen Anand944244c2023-07-14 10:50:12 +053013from erpnext.accounts.party import get_party_account
Chillar Anand915b3432021-09-02 16:44:59 +053014from erpnext.setup.utils import get_exchange_rate
tundebabzyc8978252018-02-12 10:34:50 +010015
16__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +010017
Ankush Menat494bd9e2022-03-28 18:52:46 +053018
tundebabzyc8978252018-02-12 10:34:50 +010019def get_currency(filters):
20 """
21 Returns a dictionary containing currency information. The keys of the dict are
22 - company: The company for which we are fetching currency information. if no
23 company is specified, it will fallback to the default company.
24 - company currency: The functional currency of the said company.
25 - presentation currency: The presentation currency to use. Only currencies that
26 have been used for transactions will be allowed.
27 - report date: The report date.
28 :param filters: Report filters
29 :type filters: dict
30
31 :return: str - Currency
32 """
33 company = get_appropriate_company(filters)
34 company_currency = get_company_currency(company)
Ankush Menat494bd9e2022-03-28 18:52:46 +053035 presentation_currency = (
36 filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
37 )
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053038
ruthra kumara6794c32022-12-06 13:44:54 +053039 report_date = filters.get("to_date") or filters.get("period_end_date")
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053040
41 if not report_date:
Ankush Menat494bd9e2022-03-28 18:52:46 +053042 fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053043 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010044
Ankush Menat494bd9e2022-03-28 18:52:46 +053045 currency_map = dict(
46 company=company,
47 company_currency=company_currency,
48 presentation_currency=presentation_currency,
49 report_date=report_date,
50 )
tundebabzyc8978252018-02-12 10:34:50 +010051
52 return currency_map
53
54
55def convert(value, from_, to, date):
56 """
57 convert `value` from `from_` to `to` on `date`
58 :param value: Amount to be converted
59 :param from_: Currency of `value`
60 :param to: Currency to convert to
61 :param date: exchange rate as at this date
62 :return: Result of converting `value`
63 """
64 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053065 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010066 return converted_value
67
68
69def get_rate_as_at(date, from_currency, to_currency):
70 """
71 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
72 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
73 the user's Accounts Settings.
74 It is made efficient by memoising results to `__exchange_rates`
75 :param date: exchange rate as at this date
76 :param from_currency: Base currency
77 :param to_currency: Quote currency
78 :return: Retrieved exchange rate
79 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053080
Akhil Narang3effaf22024-03-27 11:37:26 +053081 rate = __exchange_rates.get(f"{from_currency}-{to_currency}@{date}")
tundebabzyc8978252018-02-12 10:34:50 +010082 if not rate:
83 rate = get_exchange_rate(from_currency, to_currency, date) or 1
Akhil Narang3effaf22024-03-27 11:37:26 +053084 __exchange_rates[f"{from_currency}-{to_currency}@{date}"] = rate
tundebabzyc8978252018-02-12 10:34:50 +010085
86 return rate
87
Ankush Menat494bd9e2022-03-28 18:52:46 +053088
Deepesh Garg4d07e202023-07-09 20:16:12 +053089def convert_to_presentation_currency(gl_entries, currency_info):
tundebabzyc8978252018-02-12 10:34:50 +010090 """
91 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
92 in `currency_info`.
93 :param gl_entries:
94 :param currency_info:
95 :return:
96 """
97 converted_gl_list = []
Ankush Menat494bd9e2022-03-28 18:52:46 +053098 presentation_currency = currency_info["presentation_currency"]
99 company_currency = currency_info["company_currency"]
tundebabzyc8978252018-02-12 10:34:50 +0100100
Ankush Menat494bd9e2022-03-28 18:52:46 +0530101 account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +0530102
tundebabzyc8978252018-02-12 10:34:50 +0100103 for entry in gl_entries:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530104 debit = flt(entry["debit"])
105 credit = flt(entry["credit"])
106 debit_in_account_currency = flt(entry["debit_in_account_currency"])
107 credit_in_account_currency = flt(entry["credit_in_account_currency"])
108 account_currency = entry["account_currency"]
tundebabzyc8978252018-02-12 10:34:50 +0100109
Afshan4d61fa22021-05-25 19:16:02 +0530110 if len(account_currencies) == 1 and account_currency == presentation_currency:
ruthra kumar914b2302023-01-02 14:33:14 +0530111 entry["debit"] = debit_in_account_currency
112 entry["credit"] = credit_in_account_currency
Afshan4d61fa22021-05-25 19:16:02 +0530113 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114 date = currency_info["report_date"]
Deepesh Gargceaa8042021-09-05 17:21:29 +0530115 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
116 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100117
Ankush Menat494bd9e2022-03-28 18:52:46 +0530118 if entry.get("debit"):
119 entry["debit"] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530120
Ankush Menat494bd9e2022-03-28 18:52:46 +0530121 if entry.get("credit"):
122 entry["credit"] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100123
tundebabzyc8978252018-02-12 10:34:50 +0100124 converted_gl_list.append(entry)
125
126 return converted_gl_list
127
128
129def get_appropriate_company(filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130 if filters.get("company"):
131 company = filters["company"]
tundebabzyc8978252018-02-12 10:34:50 +0100132 else:
133 company = get_default_company()
134
135 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530136
Ankush Menat494bd9e2022-03-28 18:52:46 +0530137
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530138@frappe.whitelist()
Akhil Narang3effaf22024-03-27 11:37:26 +0530139def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530140 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
141
Ankush Menat494bd9e2022-03-28 18:52:46 +0530142 sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
143 item_code = item_code or frappe.form_dict.get("item_code")
144 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530145
146 filters = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530147 "sales_invoice": sales_invoice,
148 "item_code": item_code,
149 "company": company,
150 "group_by": "Invoice",
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530151 }
152
153 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530154 result = gross_profit_data.grouped_data
155 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530156 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530157
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530158 return result
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530159
160
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530161def get_query_columns(report_columns):
162 if not report_columns:
163 return ""
164
165 columns = []
166 for column in report_columns:
167 fieldname = column["fieldname"]
168
169 if doctype := column.get("_doctype"):
170 columns.append(f"`{get_table_name(doctype)}`.`{fieldname}`")
171 else:
172 columns.append(fieldname)
173
Gursheen Anand0d89bfa2023-07-14 13:03:22 +0530174 return columns
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530175
176
177def get_values_for_columns(report_columns, report_row):
178 values = {}
179
180 if not report_columns:
181 return values
182
183 for column in report_columns:
184 fieldname = column["fieldname"]
185 values[fieldname] = report_row.get(fieldname)
186
187 return values
188
189
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530190def get_party_details(party_type, party_list):
191 party_details = {}
192 party = frappe.qb.DocType(party_type)
193 query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
194 if party_type == "Supplier":
195 query = query.select(party.supplier_group)
196 else:
197 query = query.select(party.customer_group, party.territory)
198
199 party_detail_list = query.run(as_dict=True)
200 for party_dict in party_detail_list:
201 party_details[party_dict.name] = party_dict
202 return party_details
203
204
205def get_taxes_query(invoice_list, doctype, parenttype):
206 taxes = frappe.qb.DocType(doctype)
207
208 query = (
209 frappe.qb.from_(taxes)
210 .select(taxes.account_head)
211 .distinct()
212 .where(
213 (taxes.parenttype == parenttype)
214 & (taxes.docstatus == 1)
215 & (taxes.account_head.isnotnull())
216 & (taxes.parent.isin([inv.name for inv in invoice_list]))
217 )
218 .orderby(taxes.account_head)
219 )
220
221 if doctype == "Purchase Taxes and Charges":
222 return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
223 elif doctype == "Sales Taxes and Charges":
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530224 return query
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530225 return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530226
227
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530228def get_journal_entries(filters, args):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530229 je = frappe.qb.DocType("Journal Entry")
230 journal_account = frappe.qb.DocType("Journal Entry Account")
231 query = (
232 frappe.qb.from_(je)
233 .inner_join(journal_account)
234 .on(je.name == journal_account.parent)
235 .select(
236 je.voucher_type.as_("doctype"),
237 je.name,
238 je.posting_date,
239 journal_account.account.as_(args.account),
240 journal_account.party.as_(args.party),
241 journal_account.party.as_(args.party_name),
242 je.bill_no,
243 je.bill_date,
244 je.remark.as_("remarks"),
245 je.total_amount.as_("base_net_total"),
246 je.total_amount.as_("base_grand_total"),
247 je.mode_of_payment,
248 journal_account.project,
249 )
Gursheen Anand944244c2023-07-14 10:50:12 +0530250 .where(
251 (je.voucher_type == "Journal Entry")
ruthra kumar0f1be032024-01-03 20:56:12 +0530252 & (je.docstatus == 1)
Gursheen Anand944244c2023-07-14 10:50:12 +0530253 & (journal_account.party == filters.get(args.party))
254 & (journal_account.account.isin(args.party_account))
255 )
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530256 .orderby(je.posting_date, je.name, order=Order.desc)
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530257 )
Deepesh Garg92e503f2023-08-29 20:45:57 +0530258 query = apply_common_conditions(filters, query, doctype="Journal Entry", payments=True)
259
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530260 journal_entries = query.run(as_dict=True)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530261 return journal_entries
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530262
263
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530264def get_payment_entries(filters, args):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530265 pe = frappe.qb.DocType("Payment Entry")
266 query = (
267 frappe.qb.from_(pe)
268 .select(
269 ConstantColumn("Payment Entry").as_("doctype"),
270 pe.name,
271 pe.posting_date,
Gursheen Anandb1818132023-07-27 10:08:26 +0530272 pe[args.account_fieldname].as_(args.account),
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530273 pe.party.as_(args.party),
274 pe.party_name.as_(args.party_name),
275 pe.remarks,
276 pe.paid_amount.as_("base_net_total"),
277 pe.paid_amount_after_tax.as_("base_grand_total"),
278 pe.mode_of_payment,
279 pe.project,
280 pe.cost_center,
281 )
Gursheen Anandb1818132023-07-27 10:08:26 +0530282 .where(
ruthra kumar0f1be032024-01-03 20:56:12 +0530283 (pe.docstatus == 1)
284 & (pe.party == filters.get(args.party))
285 & (pe[args.account_fieldname].isin(args.party_account))
Gursheen Anandb1818132023-07-27 10:08:26 +0530286 )
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530287 .orderby(pe.posting_date, pe.name, order=Order.desc)
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530288 )
Deepesh Garg92e503f2023-08-29 20:45:57 +0530289 query = apply_common_conditions(filters, query, doctype="Payment Entry", payments=True)
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530290 payment_entries = query.run(as_dict=True)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530291 return payment_entries
Gursheen Kaur Anand10943192023-07-12 11:00:35 +0530292
293
Deepesh Garg92e503f2023-08-29 20:45:57 +0530294def apply_common_conditions(filters, query, doctype, child_doctype=None, payments=False):
Gursheen Anand33f8f7d2023-07-21 10:57:55 +0530295 parent_doc = frappe.qb.DocType(doctype)
296 if child_doctype:
297 child_doc = frappe.qb.DocType(child_doctype)
Sagar Vora30e40522023-07-04 17:41:30 +0530298
Deepesh Garg92e503f2023-08-29 20:45:57 +0530299 join_required = False
Sagar Vora30e40522023-07-04 17:41:30 +0530300
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530301 if filters.get("company"):
302 query = query.where(parent_doc.company == filters.company)
303 if filters.get("from_date"):
304 query = query.where(parent_doc.posting_date >= filters.from_date)
305 if filters.get("to_date"):
306 query = query.where(parent_doc.posting_date <= filters.to_date)
Sagar Vora30e40522023-07-04 17:41:30 +0530307
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530308 if payments:
309 if filters.get("cost_center"):
310 query = query.where(parent_doc.cost_center == filters.cost_center)
311 else:
312 if filters.get("cost_center"):
313 query = query.where(child_doc.cost_center == filters.cost_center)
Deepesh Garg92e503f2023-08-29 20:45:57 +0530314 join_required = True
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530315 if filters.get("warehouse"):
316 query = query.where(child_doc.warehouse == filters.warehouse)
Deepesh Garg92e503f2023-08-29 20:45:57 +0530317 join_required = True
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530318 if filters.get("item_group"):
319 query = query.where(child_doc.item_group == filters.item_group)
Deepesh Garg92e503f2023-08-29 20:45:57 +0530320 join_required = True
321
322 if not payments:
323 if filters.get("brand"):
324 query = query.where(child_doc.brand == filters.brand)
325 join_required = True
326
327 if join_required:
328 query = query.inner_join(child_doc).on(parent_doc.name == child_doc.parent)
329 query = query.distinct()
Gursheen Anandc084fe62023-07-14 11:05:50 +0530330
331 if parent_doc.get_table_name() != "tabJournal Entry":
332 query = filter_invoices_based_on_dimensions(filters, query, parent_doc)
Deepesh Garg92e503f2023-08-29 20:45:57 +0530333
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530334 return query
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530335
336
337def get_advance_taxes_and_charges(invoice_list):
338 adv_taxes = frappe.qb.DocType("Advance Taxes and Charges")
339 return (
340 frappe.qb.from_(adv_taxes)
341 .select(
342 adv_taxes.parent,
343 adv_taxes.account_head,
344 (
345 frappe.qb.terms.Case()
346 .when(adv_taxes.add_deduct_tax == "Add", Sum(adv_taxes.base_tax_amount))
347 .else_(Sum(adv_taxes.base_tax_amount) * -1)
348 ).as_("tax_amount"),
349 )
350 .where(
351 (adv_taxes.parent.isin([inv.name for inv in invoice_list]))
352 & (adv_taxes.charge_type.isin(["On Paid Amount", "Actual"]))
353 & (adv_taxes.base_tax_amount != 0)
354 )
355 .groupby(adv_taxes.parent, adv_taxes.account_head, adv_taxes.add_deduct_tax)
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530356 ).run(as_dict=True)
357
358
Gursheen Anandc084fe62023-07-14 11:05:50 +0530359def filter_invoices_based_on_dimensions(filters, query, parent_doc):
360 accounting_dimensions = get_accounting_dimensions(as_list=False)
361 if accounting_dimensions:
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530362 for dimension in accounting_dimensions:
363 if filters.get(dimension.fieldname):
364 if frappe.get_cached_value("DocType", dimension.document_type, "is_tree"):
365 filters[dimension.fieldname] = get_dimension_with_children(
366 dimension.document_type, filters.get(dimension.fieldname)
367 )
368 fieldname = dimension.fieldname
ruthra kumar7b3f9382024-01-11 16:24:43 +0530369 query = query.where(parent_doc[fieldname].isin(filters[fieldname]))
Gursheen Anandc084fe62023-07-14 11:05:50 +0530370 return query
Gursheen Anand944244c2023-07-14 10:50:12 +0530371
372
373def get_opening_row(party_type, party, from_date, company):
374 party_account = get_party_account(party_type, party, company, include_advance=True)
375 gle = frappe.qb.DocType("GL Entry")
376 return (
377 frappe.qb.from_(gle)
378 .select(
379 ConstantColumn("Opening").as_("account"),
380 Sum(gle.debit).as_("debit"),
381 Sum(gle.credit).as_("credit"),
382 (Sum(gle.debit) - Sum(gle.credit)).as_("balance"),
383 )
Gursheen Anand33f8f7d2023-07-21 10:57:55 +0530384 .where(
385 (gle.account.isin(party_account))
386 & (gle.party == party)
387 & (gle.posting_date < from_date)
Gursheen Anand59a2a042023-07-21 13:22:01 +0530388 & (gle.is_cancelled == 0)
Gursheen Anand33f8f7d2023-07-21 10:57:55 +0530389 )
Gursheen Anand944244c2023-07-14 10:50:12 +0530390 ).run(as_dict=True)