From 4f51c5a433ca8cf3fd688e262e100f63c98b65f9 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 3 Jul 2023 14:35:33 +0530 Subject: [PATCH] refactor: submit and make JV through background job --- .../exchange_rate_revaluation.py | 6 +++ erpnext/accounts/utils.py | 44 +++++++++++++++++++ erpnext/hooks.py | 4 ++ erpnext/setup/doctype/company/company.json | 9 +++- 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py index 598db642f3..c52ea24f25 100644 --- a/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py +++ b/erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py @@ -93,6 +93,12 @@ class ExchangeRateRevaluation(Document): return True + def fetch_and_calculate_accounts_data(self): + accounts = self.get_accounts_data() + if accounts: + for acc in accounts: + self.append("accounts", acc) + @frappe.whitelist() def get_accounts_data(self): self.validate_mandatory() diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 9000b0d32e..31473db675 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1408,6 +1408,50 @@ def check_and_delete_linked_reports(report): frappe.delete_doc("Desktop Icon", icon) +def create_err_and_its_journals(companies: list = None) -> None: + if companies: + for company in companies: + err = frappe.new_doc("Exchange Rate Revaluation") + err.company = company.name + err.posting_date = nowdate() + err.rounding_loss_allowance = 0.0 + + err.fetch_and_calculate_accounts_data() + if err.accounts: + err.save().submit() + response = err.make_jv_entries() + + if company.submit_err_jv: + jv = response.get("revaluation_jv", None) + jv and frappe.get_doc("Journal Entry", jv).submit() + jv = response.get("zero_balance_jv", None) + jv and frappe.get_doc("Journal Entry", jv).submit() + + +def auto_create_exchange_rate_revaluation_daily() -> None: + """ + Executed by background job + """ + companies = frappe.db.get_all( + "Company", + filters={"auto_exchange_rate_revaluation": 1, "auto_err_frequency": "Daily"}, + fields=["name", "submit_err_jv"], + ) + create_err_and_its_journals(companies) + + +def auto_create_exchange_rate_revaluation_weekly() -> None: + """ + Executed by background job + """ + companies = frappe.db.get_all( + "Company", + filters={"auto_exchange_rate_revaluation": 1, "auto_err_frequency": "Weekly"}, + fields=["name", "submit_err_jv"], + ) + create_err_and_its_journals(companies) + + def get_payment_ledger_entries(gl_entries, cancel=0): ple_map = [] if gl_entries: diff --git a/erpnext/hooks.py b/erpnext/hooks.py index a6d939e74f..d02d318b2d 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -415,6 +415,10 @@ scheduler_events = { "erpnext.selling.doctype.quotation.quotation.set_expired_status", "erpnext.buying.doctype.supplier_quotation.supplier_quotation.set_expired_status", "erpnext.accounts.doctype.process_statement_of_accounts.process_statement_of_accounts.send_auto_email", + "erpnext.accounts.utils.auto_create_exchange_rate_revaluation_daily", + ], + "weekly": [ + "erpnext.accounts.utils.auto_create_exchange_rate_revaluation_weekly", ], "daily_long": [ "erpnext.setup.doctype.email_digest.email_digest.send", diff --git a/erpnext/setup/doctype/company/company.json b/erpnext/setup/doctype/company/company.json index 7e55ca4fc0..ed2852e87a 100644 --- a/erpnext/setup/doctype/company/company.json +++ b/erpnext/setup/doctype/company/company.json @@ -98,6 +98,7 @@ "exchange_rate_revaluation_settings_section", "auto_exchange_rate_revaluation", "auto_err_frequency", + "submit_err_jv", "budget_detail", "exception_budget_approver_role", "registration_info", @@ -751,6 +752,12 @@ "fieldtype": "Select", "label": "Frequency", "options": "Daily\nWeekly" + }, + { + "default": "0", + "fieldname": "submit_err_jv", + "fieldtype": "Check", + "label": "Submit ERR Journals?" } ], "icon": "fa fa-building", @@ -758,7 +765,7 @@ "image_field": "company_logo", "is_tree": 1, "links": [], - "modified": "2023-07-03 14:29:56.262188", + "modified": "2023-07-07 05:41:41.537256", "modified_by": "Administrator", "module": "Setup", "name": "Company",