blob: 8711e7ee23ac36b2d4eae61ca36fa2323f3e03a9 [file] [log] [blame]
tundebabzyc8978252018-02-12 10:34:50 +01001import frappe
Sagar Vora30e40522023-07-04 17:41:30 +05302from frappe.utils import flt, formatdate, get_datetime_str, get_table_name
Chillar Anand915b3432021-09-02 16:44:59 +05303
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
Deepesh Garg4d07e202023-07-09 20:16:12 +053081def convert_to_presentation_currency(gl_entries, currency_info):
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 debit = flt(entry["debit"])
97 credit = flt(entry["credit"])
98 debit_in_account_currency = flt(entry["debit_in_account_currency"])
99 credit_in_account_currency = flt(entry["credit_in_account_currency"])
100 account_currency = entry["account_currency"]
tundebabzyc8978252018-02-12 10:34:50 +0100101
Afshan4d61fa22021-05-25 19:16:02 +0530102 if len(account_currencies) == 1 and account_currency == presentation_currency:
ruthra kumar914b2302023-01-02 14:33:14 +0530103 entry["debit"] = debit_in_account_currency
104 entry["credit"] = credit_in_account_currency
Afshan4d61fa22021-05-25 19:16:02 +0530105 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530106 date = currency_info["report_date"]
Deepesh Gargceaa8042021-09-05 17:21:29 +0530107 converted_debit_value = convert(debit, presentation_currency, company_currency, date)
108 converted_credit_value = convert(credit, presentation_currency, company_currency, date)
tundebabzyc8978252018-02-12 10:34:50 +0100109
Ankush Menat494bd9e2022-03-28 18:52:46 +0530110 if entry.get("debit"):
111 entry["debit"] = converted_debit_value
Rohit Waghchaure376db4f2019-04-18 22:01:33 +0530112
Ankush Menat494bd9e2022-03-28 18:52:46 +0530113 if entry.get("credit"):
114 entry["credit"] = converted_credit_value
tundebabzyc8978252018-02-12 10:34:50 +0100115
tundebabzyc8978252018-02-12 10:34:50 +0100116 converted_gl_list.append(entry)
117
118 return converted_gl_list
119
120
121def get_appropriate_company(filters):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530122 if filters.get("company"):
123 company = filters["company"]
tundebabzyc8978252018-02-12 10:34:50 +0100124 else:
125 company = get_default_company()
126
127 return company
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530128
Ankush Menat494bd9e2022-03-28 18:52:46 +0530129
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530130@frappe.whitelist()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530131def get_invoiced_item_gross_margin(
132 sales_invoice=None, item_code=None, company=None, with_item_data=False
133):
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530134 from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
135
Ankush Menat494bd9e2022-03-28 18:52:46 +0530136 sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
137 item_code = item_code or frappe.form_dict.get("item_code")
138 company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530139
140 filters = {
Ankush Menat494bd9e2022-03-28 18:52:46 +0530141 "sales_invoice": sales_invoice,
142 "item_code": item_code,
143 "company": company,
144 "group_by": "Invoice",
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530145 }
146
147 gross_profit_data = GrossProfitGenerator(filters)
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530148 result = gross_profit_data.grouped_data
149 if not with_item_data:
Ankush Menat98917802021-06-11 18:40:22 +0530150 result = sum(d.gross_profit for d in result)
Rohit Waghchaure1d6f2c32019-04-23 18:33:01 +0530151
Rohit Waghchaure7bee5022019-05-08 15:31:29 +0530152 return result
Gursheen Anandcbef6c32023-07-10 18:39:35 +0530153
154
155def get_party_details(party_type, party_list):
156 party_details = {}
157 party = frappe.qb.DocType(party_type)
158 query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
159 if party_type == "Supplier":
160 query = query.select(party.supplier_group)
161 else:
162 query = query.select(party.customer_group, party.territory)
163
164 party_detail_list = query.run(as_dict=True)
165 for party_dict in party_detail_list:
166 party_details[party_dict.name] = party_dict
167 return party_details
168
169
170def get_taxes_query(invoice_list, doctype, parenttype):
171 taxes = frappe.qb.DocType(doctype)
172
173 query = (
174 frappe.qb.from_(taxes)
175 .select(taxes.account_head)
176 .distinct()
177 .where(
178 (taxes.parenttype == parenttype)
179 & (taxes.docstatus == 1)
180 & (taxes.account_head.isnotnull())
181 & (taxes.parent.isin([inv.name for inv in invoice_list]))
182 )
183 .orderby(taxes.account_head)
184 )
185
186 if doctype == "Purchase Taxes and Charges":
187 return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
188 elif doctype == "Sales Taxes and Charges":
189 return query.where(taxes.charge_type.isin(["Total", "Valuation and Total"]))
190 return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530191
192
193def get_journal_entries(filters, args):
194 return frappe.db.sql(
195 """
196 select je.voucher_type as doctype, je.name, je.posting_date,
197 jea.account as {0}, jea.party as {1}, jea.party as {2},
198 je.bill_no, je.bill_date, je.remark, je.total_amount as base_net_total,
199 je.total_amount as base_grand_total, je.mode_of_payment, jea.project {3}
200 from `tabJournal Entry` je left join `tabJournal Entry Account` jea on jea.parent=je.name
Gursheen Anand7650b002023-07-11 15:18:28 +0530201 where je.voucher_type='Journal Entry' and jea.party='{4}' {5}
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530202 order by je.posting_date desc, je.name desc""".format(
203 args.account,
204 args.party,
205 args.party_name,
206 args.additional_query_columns,
Gursheen Anand7650b002023-07-11 15:18:28 +0530207 filters.get(args.party),
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530208 args.conditions,
209 ),
210 filters,
211 as_dict=1,
212 )
213
214
215def get_payment_entries(filters, args):
216 return frappe.db.sql(
217 """
218 select 'Payment Entry' as doctype, name, posting_date, paid_to as {0},
219 party as {1}, party_name as {2}, remarks,
220 paid_amount as base_net_total, paid_amount_after_tax as base_grand_total,
221 mode_of_payment, project, cost_center {3}
222 from `tabPayment Entry`
Gursheen Anand7650b002023-07-11 15:18:28 +0530223 where party='{4}' {5}
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530224 order by posting_date desc, name desc""".format(
225 args.account,
226 args.party,
227 args.party_name,
228 args.additional_query_columns,
Gursheen Anand7650b002023-07-11 15:18:28 +0530229 filters.get(args.party),
Gursheen Anandd5aa0e32023-07-11 14:47:23 +0530230 args.conditions,
231 ),
232 filters,
233 as_dict=1,
234 )
Gursheen Kaur Anand10943192023-07-12 11:00:35 +0530235
236
Sagar Vora30e40522023-07-04 17:41:30 +0530237def get_query_columns(report_columns):
238 if not report_columns:
239 return ""
240
241 columns = []
242 for column in report_columns:
243 fieldname = column["fieldname"]
244
245 if doctype := column.get("_doctype"):
246 columns.append(f"`{get_table_name(doctype)}`.`{fieldname}`")
247 else:
248 columns.append(fieldname)
249
250 return ", " + ", ".join(columns)
251
252
253def get_values_for_columns(report_columns, report_row):
254 values = {}
255
256 if not report_columns:
257 return values
258
259 for column in report_columns:
260 fieldname = column["fieldname"]
261 values[fieldname] = report_row.get(fieldname)
262
263 return values