refactor: submit and make JV through background job
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 598db64..c52ea24 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 @@
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 9000b0d..31473db 100644
--- a/erpnext/accounts/utils.py
+++ b/erpnext/accounts/utils.py
@@ -1408,6 +1408,50 @@
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 a6d939e..d02d318 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -415,6 +415,10 @@
"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 7e55ca4..ed2852e 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",