refactor: link running doc validation to company master

(cherry picked from commit 5a3afea8c7)
This commit is contained in:
ruthra kumar
2024-03-20 14:32:27 +05:30
committed by Mergify
parent 29804f6a41
commit 1104deb58e
3 changed files with 34 additions and 41 deletions

View File

@@ -168,7 +168,7 @@ frappe.ui.form.on("Company", {
delete_company_transactions: function (frm) {
frappe.call({
method: "erpnext.setup.doctype.company.company.is_deletion_job_running",
method: "erpnext.setup.doctype.transaction_deletion_record.transaction_deletion_record.is_deletion_doc_running",
args: {
company: frm.doc.name,
},

View File

@@ -12,7 +12,6 @@ from frappe.contacts.address_and_contact import load_address_and_contact
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from frappe.desk.page.setup_wizard.setup_wizard import make_records
from frappe.utils import cint, formatdate, get_link_to_form, get_timestamp, today
from frappe.utils.background_jobs import get_job, is_job_enqueued
from frappe.utils.nestedset import NestedSet, rebuild_tree
from erpnext.accounts.doctype.account.account import get_account_currency
@@ -904,37 +903,21 @@ def get_default_company_address(name, sort_key="is_primary_address", existing_ad
return None
def generate_id_for_deletion_job(company):
return "delete_company_transactions_" + company
@frappe.whitelist()
def is_deletion_job_running(company):
job_id = generate_id_for_deletion_job(company)
if is_job_enqueued(job_id):
job_name = get_job(job_id).get_id() # job name will have site prefix
frappe.throw(
_("A Transaction Deletion Job: {0} is already running for {1}").format(
frappe.bold(get_link_to_form("RQ Job", job_name)), frappe.bold(company)
)
)
@frappe.whitelist()
def create_transaction_deletion_request(company):
is_deletion_job_running(company)
job_id = generate_id_for_deletion_job(company)
from erpnext.setup.doctype.transaction_deletion_record.transaction_deletion_record import (
is_deletion_doc_running,
)
is_deletion_doc_running(company)
tdr = frappe.get_doc({"doctype": "Transaction Deletion Record", "company": company})
tdr.insert()
tdr.submit()
tdr.start_deletion_tasks()
frappe.enqueue(
"frappe.utils.background_jobs.run_doc_method",
doctype=tdr.doctype,
name=tdr.name,
doc_method="submit",
job_id=job_id,
queue="long",
enqueue_after_commit=True,
frappe.msgprint(
_("A Transaction Deletion Document: {0} is triggered for {0}").format(
get_link_to_form("Transaction Deletion Record", tdr.name)
),
frappe.bold(company),
)
frappe.msgprint(_("A Transaction Deletion Job is triggered for {0}").format(frappe.bold(company)))

View File

@@ -474,7 +474,25 @@ def get_doctypes_to_be_ignored():
return doctypes_to_be_ignored
@frappe.whitelist()
def is_deletion_doc_running(company: str | None = None, err_msg: str | None = None):
if company:
if running_deletion_jobs := frappe.db.get_all(
"Transaction Deletion Record",
filters={"docstatus": 1, "company": company, "status": "Running"},
):
if not err_msg:
err_msg = ""
frappe.throw(
title=_("Deletion in Progress!"),
msg=_("Transaction Deletion Document: {0} is running for this Company. {1}").format(
get_link_to_form("Transaction Deletion Record", running_deletion_jobs[0].name), err_msg
),
)
def check_for_running_deletion_job(doc, method=None):
# Check if DocType has 'company' field
df = qb.DocType("DocField")
if (
not_allowed := qb.from_(df)
@@ -482,14 +500,6 @@ def check_for_running_deletion_job(doc, method=None):
.where((df.fieldname == "company") & (df.parent == doc.doctype))
.run()
):
if running_deletion_jobs := frappe.db.get_all(
"Transaction Deletion Record",
filters={"docstatus": 1, "company": doc.company, "status": "Running"},
):
frappe.throw(
_(
"Transaction Deletion job {0} is running for this Company. Cannot make any transactions until the deletion job is completed"
).format(
get_link_to_form("Transaction Deletion Record", running_deletion_jobs[0].name)
)
)
is_deletion_doc_running(
doc.company, _("Cannot make any transactions until the deletion job is completed")
)