refactor: delete transactions in background
This commit is contained in:
parent
32738637ce
commit
a50808a077
@ -140,38 +140,48 @@ frappe.ui.form.on("Company", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
delete_company_transactions: function(frm) {
|
delete_company_transactions: function(frm) {
|
||||||
frappe.verify_password(function() {
|
frappe.call({
|
||||||
var d = frappe.prompt({
|
method: "erpnext.setup.doctype.company.company.is_deletion_job_running",
|
||||||
fieldtype:"Data",
|
args: {
|
||||||
fieldname: "company_name",
|
company: frm.doc.name
|
||||||
label: __("Please enter the company name to confirm"),
|
|
||||||
reqd: 1,
|
|
||||||
description: __("Please make sure you really want to delete all the transactions for this company. Your master data will remain as it is. This action cannot be undone.")
|
|
||||||
},
|
},
|
||||||
function(data) {
|
freeze: true,
|
||||||
if(data.company_name !== frm.doc.name) {
|
callback: function(r) {
|
||||||
frappe.msgprint(__("Company name not same"));
|
if(!r.exc) {
|
||||||
return;
|
frappe.verify_password(function() {
|
||||||
|
var d = frappe.prompt({
|
||||||
|
fieldtype:"Data",
|
||||||
|
fieldname: "company_name",
|
||||||
|
label: __("Please enter the company name to confirm"),
|
||||||
|
reqd: 1,
|
||||||
|
description: __("Please make sure you really want to delete all the transactions for this company. Your master data will remain as it is. This action cannot be undone.")
|
||||||
|
},
|
||||||
|
function(data) {
|
||||||
|
if(data.company_name !== frm.doc.name) {
|
||||||
|
frappe.msgprint(__("Company name not same"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
frappe.call({
|
||||||
|
method: "erpnext.setup.doctype.company.company.create_transaction_deletion_request",
|
||||||
|
args: {
|
||||||
|
company: data.company_name
|
||||||
|
},
|
||||||
|
freeze: true,
|
||||||
|
callback: function(r, rt) { },
|
||||||
|
onerror: function() {
|
||||||
|
frappe.msgprint(__("Wrong Password"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
__("Delete all the Transactions for this Company"), __("Delete")
|
||||||
|
);
|
||||||
|
d.get_primary_btn().addClass("btn-danger");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
frappe.call({
|
|
||||||
method: "erpnext.setup.doctype.company.company.create_transaction_deletion_request",
|
|
||||||
args: {
|
|
||||||
company: data.company_name
|
|
||||||
},
|
|
||||||
freeze: true,
|
|
||||||
callback: function(r, rt) {
|
|
||||||
if(!r.exc)
|
|
||||||
frappe.msgprint(__("Successfully deleted all transactions related to this company!"));
|
|
||||||
},
|
|
||||||
onerror: function() {
|
|
||||||
frappe.msgprint(__("Wrong Password"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
__("Delete all the Transactions for this Company"), __("Delete")
|
|
||||||
);
|
|
||||||
d.get_primary_btn().addClass("btn-danger");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -11,7 +11,8 @@ from frappe.cache_manager import clear_defaults_cache
|
|||||||
from frappe.contacts.address_and_contact import load_address_and_contact
|
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.custom.doctype.property_setter.property_setter import make_property_setter
|
||||||
from frappe.desk.page.setup_wizard.setup_wizard import make_records
|
from frappe.desk.page.setup_wizard.setup_wizard import make_records
|
||||||
from frappe.utils import cint, formatdate, get_timestamp, today
|
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 frappe.utils.nestedset import NestedSet, rebuild_tree
|
||||||
|
|
||||||
from erpnext.accounts.doctype.account.account import get_account_currency
|
from erpnext.accounts.doctype.account.account import get_account_currency
|
||||||
@ -900,8 +901,37 @@ def get_default_company_address(name, sort_key="is_primary_address", existing_ad
|
|||||||
return None
|
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)
|
||||||
|
job_name = get_job(job_id).get_id() # job name will have site prefix
|
||||||
|
if is_job_enqueued(job_id):
|
||||||
|
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()
|
@frappe.whitelist()
|
||||||
def create_transaction_deletion_request(company):
|
def create_transaction_deletion_request(company):
|
||||||
|
is_deletion_job_running(company)
|
||||||
|
job_id = generate_id_for_deletion_job(company)
|
||||||
|
|
||||||
tdr = frappe.get_doc({"doctype": "Transaction Deletion Record", "company": company})
|
tdr = frappe.get_doc({"doctype": "Transaction Deletion Record", "company": company})
|
||||||
tdr.insert()
|
tdr.insert()
|
||||||
tdr.submit()
|
|
||||||
|
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 Job is triggered for {0}").format(frappe.bold(company)))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user