tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 1 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | from frappe.utils import flt, formatdate, get_datetime_str |
| 3 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 4 | from erpnext import get_company_currency, get_default_company |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 5 | from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | from erpnext.setup.utils import get_exchange_rate |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 7 | |
| 8 | __exchange_rates = {} |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 9 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 10 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 11 | def 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 27 | presentation_currency = ( |
| 28 | filters["presentation_currency"] if filters.get("presentation_currency") else company_currency |
| 29 | ) |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 30 | |
ruthra kumar | a6794c3 | 2022-12-06 13:44:54 +0530 | [diff] [blame] | 31 | report_date = filters.get("to_date") or filters.get("period_end_date") |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 32 | |
| 33 | if not report_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 34 | fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"] |
Prateeksha Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 35 | report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy") |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 36 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 37 | currency_map = dict( |
| 38 | company=company, |
| 39 | company_currency=company_currency, |
| 40 | presentation_currency=presentation_currency, |
| 41 | report_date=report_date, |
| 42 | ) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 43 | |
| 44 | return currency_map |
| 45 | |
| 46 | |
| 47 | def 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) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 57 | converted_value = flt(value) / (rate or 1) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 58 | return converted_value |
| 59 | |
| 60 | |
| 61 | def 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 Singh | 8ecfaaa | 2018-07-18 18:03:27 +0530 | [diff] [blame] | 72 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 73 | rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date)) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 74 | if not rate: |
| 75 | rate = get_exchange_rate(from_currency, to_currency, date) or 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | __exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 77 | |
| 78 | return rate |
| 79 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | |
Nabin Hait | 111183d | 2020-08-22 12:31:06 +0530 | [diff] [blame] | 81 | def convert_to_presentation_currency(gl_entries, currency_info, company): |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 82 | """ |
| 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 90 | presentation_currency = currency_info["presentation_currency"] |
| 91 | company_currency = currency_info["company_currency"] |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 92 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | account_currencies = list(set(entry["account_currency"] for entry in gl_entries)) |
Nabin Hait | 111183d | 2020-08-22 12:31:06 +0530 | [diff] [blame] | 94 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 95 | for entry in gl_entries: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 96 | 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"] |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 102 | |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 103 | if len(account_currencies) == 1 and account_currency == presentation_currency: |
ruthra kumar | 914b230 | 2023-01-02 14:33:14 +0530 | [diff] [blame] | 104 | entry["debit"] = debit_in_account_currency |
| 105 | entry["credit"] = credit_in_account_currency |
Afshan | 4d61fa2 | 2021-05-25 19:16:02 +0530 | [diff] [blame] | 106 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 107 | date = currency_info["report_date"] |
Deepesh Garg | ceaa804 | 2021-09-05 17:21:29 +0530 | [diff] [blame] | 108 | converted_debit_value = convert(debit, presentation_currency, company_currency, date) |
| 109 | converted_credit_value = convert(credit, presentation_currency, company_currency, date) |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 110 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 111 | if entry.get("debit"): |
| 112 | entry["debit"] = converted_debit_value |
Rohit Waghchaure | 376db4f | 2019-04-18 22:01:33 +0530 | [diff] [blame] | 113 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | if entry.get("credit"): |
| 115 | entry["credit"] = converted_credit_value |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 116 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 117 | converted_gl_list.append(entry) |
| 118 | |
| 119 | return converted_gl_list |
| 120 | |
| 121 | |
| 122 | def get_appropriate_company(filters): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 123 | if filters.get("company"): |
| 124 | company = filters["company"] |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 125 | else: |
| 126 | company = get_default_company() |
| 127 | |
| 128 | return company |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 129 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 130 | |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 131 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 132 | def get_invoiced_item_gross_margin( |
| 133 | sales_invoice=None, item_code=None, company=None, with_item_data=False |
| 134 | ): |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 135 | from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator |
| 136 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 137 | 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 Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 140 | |
| 141 | filters = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 142 | "sales_invoice": sales_invoice, |
| 143 | "item_code": item_code, |
| 144 | "company": company, |
| 145 | "group_by": "Invoice", |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | gross_profit_data = GrossProfitGenerator(filters) |
Rohit Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 149 | result = gross_profit_data.grouped_data |
| 150 | if not with_item_data: |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 151 | result = sum(d.gross_profit for d in result) |
Rohit Waghchaure | 1d6f2c3 | 2019-04-23 18:33:01 +0530 | [diff] [blame] | 152 | |
Rohit Waghchaure | 7bee502 | 2019-05-08 15:31:29 +0530 | [diff] [blame] | 153 | return result |
Gursheen Anand | cbef6c3 | 2023-07-10 18:39:35 +0530 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def 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 | |
| 171 | def 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 Anand | d5aa0e3 | 2023-07-11 14:47:23 +0530 | [diff] [blame] | 192 | |
| 193 | |
| 194 | def 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 Anand | 7650b00 | 2023-07-11 15:18:28 +0530 | [diff] [blame^] | 202 | where je.voucher_type='Journal Entry' and jea.party='{4}' {5} |
Gursheen Anand | d5aa0e3 | 2023-07-11 14:47:23 +0530 | [diff] [blame] | 203 | 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 Anand | 7650b00 | 2023-07-11 15:18:28 +0530 | [diff] [blame^] | 208 | filters.get(args.party), |
Gursheen Anand | d5aa0e3 | 2023-07-11 14:47:23 +0530 | [diff] [blame] | 209 | args.conditions, |
| 210 | ), |
| 211 | filters, |
| 212 | as_dict=1, |
| 213 | ) |
| 214 | |
| 215 | |
| 216 | def 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 Anand | 7650b00 | 2023-07-11 15:18:28 +0530 | [diff] [blame^] | 224 | where party='{4}' {5} |
Gursheen Anand | d5aa0e3 | 2023-07-11 14:47:23 +0530 | [diff] [blame] | 225 | order by posting_date desc, name desc""".format( |
| 226 | args.account, |
| 227 | args.party, |
| 228 | args.party_name, |
| 229 | args.additional_query_columns, |
Gursheen Anand | 7650b00 | 2023-07-11 15:18:28 +0530 | [diff] [blame^] | 230 | filters.get(args.party), |
Gursheen Anand | d5aa0e3 | 2023-07-11 14:47:23 +0530 | [diff] [blame] | 231 | args.conditions, |
| 232 | ), |
| 233 | filters, |
| 234 | as_dict=1, |
| 235 | ) |