From 0859108ac6244e00cf1fb4cd9bda09c68d9c85dc Mon Sep 17 00:00:00 2001 From: Muhammed Sinan K T <91651425+MhmdSinanKT@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:54:50 +0530 Subject: [PATCH] fix: button to unlink and delete a sales order (#454) * fix: button to unlink and delete a sales order * fix: handling deletion of payment entries * fix: set the button to show based on role * fix: ignoring permissions for deletion and cancellation --- .../one_compliance/doc_events/sales_order.py | 33 +++++++++++++++ .../compliance_settings.json | 9 +++- one_compliance/public/js/sales_order.js | 41 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/one_compliance/one_compliance/doc_events/sales_order.py b/one_compliance/one_compliance/doc_events/sales_order.py index d376c8e..00183fb 100644 --- a/one_compliance/one_compliance/doc_events/sales_order.py +++ b/one_compliance/one_compliance/doc_events/sales_order.py @@ -282,3 +282,36 @@ def set_total_reimbursement_amount(doc): total_reimbursement_amount += row.amount doc.custom_total_reimbursement_amount = total_reimbursement_amount frappe.db.set_value('Sales Order', doc.name, 'custom_total_reimbursement_amount', total_reimbursement_amount) + +@frappe.whitelist() +def delete_linked_records(sales_order): + """ + Deletes all records linked with the specified sales order. + + Args: + project (str): The name of the project to delete linked records for. + """ + sales_invoice = frappe.db.get_value( + "Sales Invoice Item", {"sales_order": sales_order}, "parent" + ) + if frappe.db.exists("Sales Invoice", sales_invoice): + frappe.throw("Cannot proceed with this operation as it is invoiced") + + project = frappe.db.get_value("Sales Order", sales_order, "project") + + if frappe.db.exists("Project", project): + linked_tasks = frappe.get_all("Task", filters={"project": project}) + for task in linked_tasks: + frappe.delete_doc("Task", task["name"], ignore_permissions=True) + + project_doc = frappe.get_doc("Project", project) + project_doc.sales_order = "" + project_doc.save(ignore_permissions=True) + frappe.delete_doc("Project", project, ignore_permissions=True) + + doc = frappe.get_doc("Sales Order", sales_order) + doc.flags.ignore_permissions = True + doc.cancel() + frappe.delete_doc("Sales Order", sales_order, ignore_permissions=True) + + return "success" diff --git a/one_compliance/one_compliance/doctype/compliance_settings/compliance_settings.json b/one_compliance/one_compliance/doctype/compliance_settings/compliance_settings.json index 84fe657..2104890 100644 --- a/one_compliance/one_compliance/doctype/compliance_settings/compliance_settings.json +++ b/one_compliance/one_compliance/doctype/compliance_settings/compliance_settings.json @@ -15,6 +15,7 @@ "delete_project_along_with_compliance_agreement", "section_break_saeum", "role_of_project_creator", + "role_allowed_to_unlink_and_delete_sales_orders", "compliance_service_item_group", "default_invoice_due_day", "column_break_gnzz", @@ -250,12 +251,18 @@ "fieldtype": "Link", "label": "Default Tds Account", "options": "Account" + }, + { + "fieldname": "role_allowed_to_unlink_and_delete_sales_orders", + "fieldtype": "Link", + "label": "Role Allowed to Unlink and Delete Sales Orders", + "options": "Role" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2024-07-26 15:51:25.595909", + "modified": "2024-10-16 12:17:17.395229", "modified_by": "Administrator", "module": "One Compliance", "name": "Compliance Settings", diff --git a/one_compliance/public/js/sales_order.js b/one_compliance/public/js/sales_order.js index 76599a5..497a8e7 100644 --- a/one_compliance/public/js/sales_order.js +++ b/one_compliance/public/js/sales_order.js @@ -27,6 +27,8 @@ frappe.ui.form.on('Sales Order', { frm.remove_custom_button('Purchase Order', 'Create'); frm.remove_custom_button('Project', 'Create'); }, 500); + + handle_unlinking(frm); } }); @@ -197,3 +199,42 @@ frappe.ui.form.on('Reimbursement Details', { }); } }); + +function handle_unlinking(frm) { + if (!frm.is_new() && frm.doc.workflow_state == "In Progress") { + frappe.db + .get_single_value("Compliance Settings", "role_allowed_to_unlink_and_delete_sales_orders") + .then((role) => { + if (role && frappe.user_roles.includes(role)) { + frm.add_custom_button("Unlink and Delete", () => { + frappe.confirm( + __( + `Are you sure you want to unlink and delete all linked records of ${frm.doc.name}?` + ), + function () { + frappe.call({ + method: + "one_compliance.one_compliance.doc_events.sales_order.delete_linked_records", + args: { + sales_order: frm.doc.name, + }, + callback: function (r) { + if (r.message === "success") { + frappe.msgprint( + __("All linked records have been successfully deleted.") + ); + frappe.set_route("List", "Sales Order"); + } else { + frappe.throw( + __("An error occurred while deleting linked records.") + ); + } + }, + }); + } + ); + }); + } + }); + } +}