blob: 6c7338e724ddf5771365d4c317758207970845ee [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 (
9 get_dimension_with_children,
10)
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053011from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
Gursheen Anand944244c2023-07-14 10:50:12 +053012from erpnext.accounts.party import get_party_account
Chillar Anand915b3432021-09-02 16:44:59 +053013from erpnext.setup.utils import get_exchange_rate
tundebabzyc8978252018-02-12 10:34:50 +010014
15__exchange_rates = {}
tundebabzyc8978252018-02-12 10:34:50 +010016
Ankush Menat494bd9e2022-03-28 18:52:46 +053017
tundebabzyc8978252018-02-12 10:34:50 +010018def get_currency(filters):
19 """
20 Returns a dictionary containing currency information. The keys of the dict are
21 - company: The company for which we are fetching currency information. if no
22 company is specified, it will fallback to the default company.
23 - company currency: The functional currency of the said company.
24 - presentation currency: The presentation currency to use. Only currencies that
25 have been used for transactions will be allowed.
26 - report date: The report date.
27 :param filters: Report filters
28 :type filters: dict
29
30 :return: str - Currency
31 """
32 company = get_appropriate_company(filters)
33 company_currency = get_company_currency(company)
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 presentation_currency = (
35 filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
36 )
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053037
ruthra kumara6794c32022-12-06 13:44:54 +053038 report_date = filters.get("to_date") or filters.get("period_end_date")
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053039
40 if not report_date:
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053042 report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
tundebabzyc8978252018-02-12 10:34:50 +010043
Ankush Menat494bd9e2022-03-28 18:52:46 +053044 currency_map = dict(
45 company=company,
46 company_currency=company_currency,
47 presentation_currency=presentation_currency,
48 report_date=report_date,
49 )
tundebabzyc8978252018-02-12 10:34:50 +010050
51 return currency_map
52
53
54def convert(value, from_, to, date):
55 """
56 convert `value` from `from_` to `to` on `date`
57 :param value: Amount to be converted
58 :param from_: Currency of `value`
59 :param to: Currency to convert to
60 :param date: exchange rate as at this date
61 :return: Result of converting `value`
62 """
63 rate = get_rate_as_at(date, from_, to)
Zarrar3523b772018-08-14 16:28:14 +053064 converted_value = flt(value) / (rate or 1)
tundebabzyc8978252018-02-12 10:34:50 +010065 return converted_value
66
67
68def get_rate_as_at(date, from_currency, to_currency):
69 """
70 Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
71 This calls `get_exchange_rate` so that we can get the correct exchange rate as per
72 the user's Accounts Settings.
73 It is made efficient by memoising results to `__exchange_rates`
74 :param date: exchange rate as at this date
75 :param from_currency: Base currency
76 :param to_currency: Quote currency
77 :return: Retrieved exchange rate
78 """
Prateeksha Singh8ecfaaa2018-07-18 18:03:27 +053079
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date))
tundebabzyc8978252018-02-12 10:34:50 +010081 if not rate:
82 rate = get_exchange_rate(from_currency, to_currency, date) or 1
Ankush Menat494bd9e2022-03-28 18:52:46 +053083 __exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate
tundebabzyc8978252018-02-12 10:34:50 +010084
85 return rate
86
Ankush Menat494bd9e2022-03-28 18:52:46 +053087
Deepesh Garg4d07e202023-07-09 20:16:12 +053088def convert_to_presentation_currency(gl_entries, currency_info):
tundebabzyc8978252018-02-12 10:34:50 +010089 """
90 Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
91 in `currency_info`.
92 :param gl_entries:
93 :param currency_info:
94 :return:
95 """
96 converted_gl_list = []
Ankush Menat494bd9e2022-03-28 18:52:46 +053097 presentation_currency = currency_info["presentation_currency"]
98 company_currency = currency_info["company_currency"]
tundebabzyc8978252018-02-12 10:34:50 +010099
Ankush Menat494bd9e2022-03-28 18:52:46 +0530100 account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
Nabin Hait111183d2020-08-22 12:31:06 +0530101
tundebabzyc8978252018-02-12 10:34:50 +0100102 for entry in gl_entries:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530103 debit = flt(entry["debit"])
104 credit = flt(entry["credit"])
105 debit_in_account_currency = flt(entry["debit_in_account_currency"])
106 credit_in_account_currency = flt(entry["credit_in_account_currency"])
107 account_currency = entry["account_currency"]
tundebabzyc8978252018-02-12 10:34:50 +0100108
Afshan4d61fa22021-05-25 19:16:02 +0530109 if len(account_currencies) == 1 and account_currency == presentation_currency:
ruthra kumar914b2302023-01-02 14:33:14 +0530110 entry["debit"] = debit_in_account_currency
111 entry["credit"] = credit_in_account_currency
Afshan4d61fa22021-05-25 19:16:02 +0530112 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530113 date = currency_info["report_date"]
Deepesh Gargceaa8042021-09-05 17:21:29 +0530114 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
115 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100116
Ankush Menat494bd9e2022-03-28 18:52:46 +0530117 if entry.get("debit"):
118 entry["debit"] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530119
Ankush Menat494bd9e2022-03-28 18:52:46 +0530120 if entry.get("credit"):
121 entry["credit"] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100122
tundebabzyc8978252018-02-12 10:34:50 +0100123 converted_gl_list.append(entry)
124
125 return converted_gl_list
126
127
128def get_appropriate_company(filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530129 if filters.get("company"):
130 company = filters["company"]
tundebabzyc8978252018-02-12 10:34:50 +0100131 else:
132 company = get_default_company()
133
134 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530135
Ankush Menat494bd9e2022-03-28 18:52:46 +0530136
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530137@frappe.whitelist()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530138def get_invoiced_item_gross_margin(
139 sales_invoice=None, item_code=None, company=None, with_item_data=False
140):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530141 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
142
Ankush Menat494bd9e2022-03-28 18:52:46 +0530143 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")
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530146
147 filters = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530148 "sales_invoice": sales_invoice,
149 "item_code": item_code,
150 "company": company,
151 "group_by": "Invoice",
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530152 }
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:
Ankush Menat98917802021-06-11 18:40:22 +0530157 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
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530160
161
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530162def get_query_columns(report_columns):
163 if not report_columns:
164 return ""
165
166 columns = []
167 for column in report_columns:
168 fieldname = column["fieldname"]
169
170 if doctype := column.get("_doctype"):
171 columns.append(f"`{get_table_name(doctype)}`.`{fieldname}`")
172 else:
173 columns.append(fieldname)
174
175 return ", " + ", ".join(columns)
176
177
178def get_values_for_columns(report_columns, report_row):
179 values = {}
180
181 if not report_columns:
182 return values
183
184 for column in report_columns:
185 fieldname = column["fieldname"]
186 values[fieldname] = report_row.get(fieldname)
187
188 return values
189
190
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530191def get_party_details(party_type, party_list):
192 party_details = {}
193 party = frappe.qb.DocType(party_type)
194 query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
195 if party_type == "Supplier":
196 query = query.select(party.supplier_group)
197 else:
198 query = query.select(party.customer_group, party.territory)
199
200 party_detail_list = query.run(as_dict=True)
201 for party_dict in party_detail_list:
202 party_details[party_dict.name] = party_dict
203 return party_details
204
205
206def get_taxes_query(invoice_list, doctype, parenttype):
207 taxes = frappe.qb.DocType(doctype)
208
209 query = (
210 frappe.qb.from_(taxes)
211 .select(taxes.account_head)
212 .distinct()
213 .where(
214 (taxes.parenttype == parenttype)
215 & (taxes.docstatus == 1)
216 & (taxes.account_head.isnotnull())
217 & (taxes.parent.isin([inv.name for inv in invoice_list]))
218 )
219 .orderby(taxes.account_head)
220 )
221
222 if doctype == "Purchase Taxes and Charges":
223 return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
224 elif doctype == "Sales Taxes and Charges":
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530225 return query
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530226 return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530227
228
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530229def get_journal_entries(filters, args):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530230 je = frappe.qb.DocType("Journal Entry")
231 journal_account = frappe.qb.DocType("Journal Entry Account")
232 query = (
233 frappe.qb.from_(je)
234 .inner_join(journal_account)
235 .on(je.name == journal_account.parent)
236 .select(
237 je.voucher_type.as_("doctype"),
238 je.name,
239 je.posting_date,
240 journal_account.account.as_(args.account),
241 journal_account.party.as_(args.party),
242 journal_account.party.as_(args.party_name),
243 je.bill_no,
244 je.bill_date,
245 je.remark.as_("remarks"),
246 je.total_amount.as_("base_net_total"),
247 je.total_amount.as_("base_grand_total"),
248 je.mode_of_payment,
249 journal_account.project,
250 )
Gursheen Anand944244c2023-07-14 10:50:12 +0530251 .where(
252 (je.voucher_type == "Journal Entry")
253 & (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 )
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530258 query = get_conditions(filters, query, [je], payments=True)
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530259 journal_entries = query.run(as_dict=True)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530260 return journal_entries
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530261
262
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530263def get_payment_entries(filters, args):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530264 pe = frappe.qb.DocType("Payment Entry")
265 query = (
266 frappe.qb.from_(pe)
267 .select(
268 ConstantColumn("Payment Entry").as_("doctype"),
269 pe.name,
270 pe.posting_date,
271 pe.paid_to.as_(args.account),
272 pe.party.as_(args.party),
273 pe.party_name.as_(args.party_name),
274 pe.remarks,
275 pe.paid_amount.as_("base_net_total"),
276 pe.paid_amount_after_tax.as_("base_grand_total"),
277 pe.mode_of_payment,
278 pe.project,
279 pe.cost_center,
280 )
Gursheen Anand944244c2023-07-14 10:50:12 +0530281 .where((pe.party == filters.get(args.party)) & (pe.paid_to.isin(args.party_account)))
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530282 .orderby(pe.posting_date, pe.name, order=Order.desc)
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530283 )
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530284 query = get_conditions(filters, query, [pe], payments=True)
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530285 payment_entries = query.run(as_dict=True)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530286 return payment_entries
Gursheen Kaur Anand10943192023-07-12 11:00:35 +0530287
288
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530289def get_conditions(filters, query, docs, payments=False):
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530290 parent_doc = docs[0]
291 if not payments:
292 child_doc = docs[1]
Sagar Vora30e40522023-07-04 17:41:30 +0530293
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530294 if parent_doc.get_table_name() == "tabSales Invoice":
295 if filters.get("owner"):
296 query = query.where(parent_doc.owner == filters.owner)
297 if filters.get("mode_of_payment"):
298 payment_doc = docs[2]
299 query = query.where(payment_doc.mode_of_payment == filters.mode_of_payment)
300 if not payments:
301 if filters.get("brand"):
302 query = query.where(child_doc.brand == filters.brand)
303 else:
304 if filters.get("mode_of_payment"):
305 query = query.where(parent_doc.mode_of_payment == filters.mode_of_payment)
Sagar Vora30e40522023-07-04 17:41:30 +0530306
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530307 if filters.get("company"):
308 query = query.where(parent_doc.company == filters.company)
309 if filters.get("from_date"):
310 query = query.where(parent_doc.posting_date >= filters.from_date)
311 if filters.get("to_date"):
312 query = query.where(parent_doc.posting_date <= filters.to_date)
Sagar Vora30e40522023-07-04 17:41:30 +0530313
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530314 if payments:
315 if filters.get("cost_center"):
316 query = query.where(parent_doc.cost_center == filters.cost_center)
317 else:
318 if filters.get("cost_center"):
319 query = query.where(child_doc.cost_center == filters.cost_center)
320 if filters.get("warehouse"):
321 query = query.where(child_doc.warehouse == filters.warehouse)
322 if filters.get("item_group"):
323 query = query.where(child_doc.item_group == filters.item_group)
Gursheen Anand6c11ca12023-07-12 14:43:18 +0530324 return query
Gursheen Anandf5027fd2023-07-12 16:42:58 +0530325
326
327def get_advance_taxes_and_charges(invoice_list):
328 adv_taxes = frappe.qb.DocType("Advance Taxes and Charges")
329 return (
330 frappe.qb.from_(adv_taxes)
331 .select(
332 adv_taxes.parent,
333 adv_taxes.account_head,
334 (
335 frappe.qb.terms.Case()
336 .when(adv_taxes.add_deduct_tax == "Add", Sum(adv_taxes.base_tax_amount))
337 .else_(Sum(adv_taxes.base_tax_amount) * -1)
338 ).as_("tax_amount"),
339 )
340 .where(
341 (adv_taxes.parent.isin([inv.name for inv in invoice_list]))
342 & (adv_taxes.charge_type.isin(["On Paid Amount", "Actual"]))
343 & (adv_taxes.base_tax_amount != 0)
344 )
345 .groupby(adv_taxes.parent, adv_taxes.account_head, adv_taxes.add_deduct_tax)
Gursheen Anandbf08aa72023-07-12 17:17:58 +0530346 ).run(as_dict=True)
347
348
349def filter_invoices_based_on_dimensions(filters, accounting_dimensions, invoices):
350 invoices_with_acc_dimensions = []
351 for inv in invoices:
352 for dimension in accounting_dimensions:
353 if filters.get(dimension.fieldname):
354 if frappe.get_cached_value("DocType", dimension.document_type, "is_tree"):
355 filters[dimension.fieldname] = get_dimension_with_children(
356 dimension.document_type, filters.get(dimension.fieldname)
357 )
358 fieldname = dimension.fieldname
359 if inv.fieldname != filters[fieldname]:
360 break
361 else:
362 invoices_with_acc_dimensions.append(inv)
363 return invoices_with_acc_dimensions
Gursheen Anand944244c2023-07-14 10:50:12 +0530364
365
366def get_opening_row(party_type, party, from_date, company):
367 party_account = get_party_account(party_type, party, company, include_advance=True)
368 gle = frappe.qb.DocType("GL Entry")
369 return (
370 frappe.qb.from_(gle)
371 .select(
372 ConstantColumn("Opening").as_("account"),
373 Sum(gle.debit).as_("debit"),
374 Sum(gle.credit).as_("credit"),
375 (Sum(gle.debit) - Sum(gle.credit)).as_("balance"),
376 )
377 .where((gle.account.isin(party_account)) & (gle.posting_date < from_date))
378 ).run(as_dict=True)