blob: 462ef21c82d8df10eb72f0ec5c572783484e6af1 [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
Ankush Menat494bd9e2022-03-28 18:52:46 +053081 rate = __exchange_rates.get("{0}-{1}@{2}".format(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
Ankush Menat494bd9e2022-03-28 18:52:46 +053084 __exchange_rates["{0}-{1}@{2}".format(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()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530139def get_invoiced_item_gross_margin(
140 sales_invoice=None, item_code=None, company=None, with_item_data=False
141):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530142 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
143
Ankush Menat494bd9e2022-03-28 18:52:46 +0530144 sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
145 item_code = item_code or frappe.form_dict.get("item_code")
146 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530147
148 filters = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530149 "sales_invoice": sales_invoice,
150 "item_code": item_code,
151 "company": company,
152 "group_by": "Invoice",
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530153 }
154
155 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530156 result = gross_profit_data.grouped_data
157 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530158 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530159
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530160 return result
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530161
162
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530163def get_query_columns(report_columns):
164 if not report_columns:
165 return ""
166
167 columns = []
168 for column in report_columns:
169 fieldname = column["fieldname"]
170
171 if doctype := column.get("_doctype"):
172 columns.append(f"`{get_table_name(doctype)}`.`{fieldname}`")
173 else:
174 columns.append(fieldname)
175
176 return ", " + ", ".join(columns)
177
178
179def get_values_for_columns(report_columns, report_row):
180 values = {}
181
182 if not report_columns:
183 return values
184
185 for column in report_columns:
186 fieldname = column["fieldname"]
187 values[fieldname] = report_row.get(fieldname)
188
189 return values
190
191
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530192def get_party_details(party_type, party_list):
193 party_details = {}
194 party = frappe.qb.DocType(party_type)
195 query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
196 if party_type == "Supplier":
197 query = query.select(party.supplier_group)
198 else:
199 query = query.select(party.customer_group, party.territory)
200
201 party_detail_list = query.run(as_dict=True)
202 for party_dict in party_detail_list:
203 party_details[party_dict.name] = party_dict
204 return party_details
205
206
207def get_taxes_query(invoice_list, doctype, parenttype):
208 taxes = frappe.qb.DocType(doctype)
209
210 query = (
211 frappe.qb.from_(taxes)
212 .select(taxes.account_head)
213 .distinct()
214 .where(
215 (taxes.parenttype == parenttype)
216 & (taxes.docstatus == 1)
217 & (taxes.account_head.isnotnull())
218 & (taxes.parent.isin([inv.name for inv in invoice_list]))
219 )
220 .orderby(taxes.account_head)
221 )
222
223 if doctype == "Purchase Taxes and Charges":
224 return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
225 elif doctype == "Sales Taxes and Charges":
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530226 return query
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530227 return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530228
229
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530230def get_journal_entries(filters, args):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530231 je = frappe.qb.DocType("Journal Entry")
232 journal_account = frappe.qb.DocType("Journal Entry Account")
233 query = (
234 frappe.qb.from_(je)
235 .inner_join(journal_account)
236 .on(je.name == journal_account.parent)
237 .select(
238 je.voucher_type.as_("doctype"),
239 je.name,
240 je.posting_date,
241 journal_account.account.as_(args.account),
242 journal_account.party.as_(args.party),
243 journal_account.party.as_(args.party_name),
244 je.bill_no,
245 je.bill_date,
246 je.remark.as_("remarks"),
247 je.total_amount.as_("base_net_total"),
248 je.total_amount.as_("base_grand_total"),
249 je.mode_of_payment,
250 journal_account.project,
251 )
Gursheen Anand944244c2023-07-14 10:50:12 +0530252 .where(
253 (je.voucher_type == "Journal Entry")
254 & (journal_account.party == filters.get(args.party))
255 & (journal_account.account.isin(args.party_account))
256 )
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530257 .orderby(je.posting_date, je.name, order=Order.desc)
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530258 )
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530259 query = get_conditions(filters, query, [je], payments=True)
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,
272 pe.paid_to.as_(args.account),
273 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 Anand944244c2023-07-14 10:50:12 +0530282 .where((pe.party == filters.get(args.party)) & (pe.paid_to.isin(args.party_account)))
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530283 .orderby(pe.posting_date, pe.name, order=Order.desc)
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530284 )
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530285 query = get_conditions(filters, query, [pe], payments=True)
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530286 payment_entries = query.run(as_dict=True)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530287 return payment_entries
Gursheen Kaur Anand10943192023-07-12 11:00:35 +0530288
289
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530290def get_conditions(filters, query, docs, payments=False):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530291 parent_doc = docs[0]
292 if not payments:
293 child_doc = docs[1]
Sagar Vora30e40522023-07-04 17:41:30 +0530294
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530295 if parent_doc.get_table_name() == "tabSales Invoice":
296 if filters.get("owner"):
297 query = query.where(parent_doc.owner == filters.owner)
298 if filters.get("mode_of_payment"):
299 payment_doc = docs[2]
300 query = query.where(payment_doc.mode_of_payment == filters.mode_of_payment)
301 if not payments:
302 if filters.get("brand"):
303 query = query.where(child_doc.brand == filters.brand)
304 else:
305 if filters.get("mode_of_payment"):
306 query = query.where(parent_doc.mode_of_payment == filters.mode_of_payment)
Sagar Vora30e40522023-07-04 17:41:30 +0530307
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530308 if filters.get("company"):
309 query = query.where(parent_doc.company == filters.company)
310 if filters.get("from_date"):
311 query = query.where(parent_doc.posting_date >= filters.from_date)
312 if filters.get("to_date"):
313 query = query.where(parent_doc.posting_date <= filters.to_date)
Sagar Vora30e40522023-07-04 17:41:30 +0530314
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530315 if payments:
316 if filters.get("cost_center"):
317 query = query.where(parent_doc.cost_center == filters.cost_center)
318 else:
319 if filters.get("cost_center"):
320 query = query.where(child_doc.cost_center == filters.cost_center)
321 if filters.get("warehouse"):
322 query = query.where(child_doc.warehouse == filters.warehouse)
323 if filters.get("item_group"):
324 query = query.where(child_doc.item_group == filters.item_group)
Gursheen Anandc084fe62023-07-14 11:05:50 +0530325
326 if parent_doc.get_table_name() != "tabJournal Entry":
327 query = filter_invoices_based_on_dimensions(filters, query, parent_doc)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530328 return query
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530329
330
331def get_advance_taxes_and_charges(invoice_list):
332 adv_taxes = frappe.qb.DocType("Advance Taxes and Charges")
333 return (
334 frappe.qb.from_(adv_taxes)
335 .select(
336 adv_taxes.parent,
337 adv_taxes.account_head,
338 (
339 frappe.qb.terms.Case()
340 .when(adv_taxes.add_deduct_tax == "Add", Sum(adv_taxes.base_tax_amount))
341 .else_(Sum(adv_taxes.base_tax_amount) * -1)
342 ).as_("tax_amount"),
343 )
344 .where(
345 (adv_taxes.parent.isin([inv.name for inv in invoice_list]))
346 & (adv_taxes.charge_type.isin(["On Paid Amount", "Actual"]))
347 & (adv_taxes.base_tax_amount != 0)
348 )
349 .groupby(adv_taxes.parent, adv_taxes.account_head, adv_taxes.add_deduct_tax)
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530350 ).run(as_dict=True)
351
352
Gursheen Anandc084fe62023-07-14 11:05:50 +0530353def filter_invoices_based_on_dimensions(filters, query, parent_doc):
354 accounting_dimensions = get_accounting_dimensions(as_list=False)
355 if accounting_dimensions:
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530356 for dimension in accounting_dimensions:
357 if filters.get(dimension.fieldname):
358 if frappe.get_cached_value("DocType", dimension.document_type, "is_tree"):
359 filters[dimension.fieldname] = get_dimension_with_children(
360 dimension.document_type, filters.get(dimension.fieldname)
361 )
362 fieldname = dimension.fieldname
Gursheen Anandc084fe62023-07-14 11:05:50 +0530363 query = query.where(parent_doc[fieldname] == filters.fieldname)
364 return query
Gursheen Anand944244c2023-07-14 10:50:12 +0530365
366
367def get_opening_row(party_type, party, from_date, company):
368 party_account = get_party_account(party_type, party, company, include_advance=True)
369 gle = frappe.qb.DocType("GL Entry")
370 return (
371 frappe.qb.from_(gle)
372 .select(
373 ConstantColumn("Opening").as_("account"),
374 Sum(gle.debit).as_("debit"),
375 Sum(gle.credit).as_("credit"),
376 (Sum(gle.debit) - Sum(gle.credit)).as_("balance"),
377 )
378 .where((gle.account.isin(party_account)) & (gle.posting_date < from_date))
379 ).run(as_dict=True)