Skip to content

Commit

Permalink
fix: button to unlink and delete a sales order (#454)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MhmdSinanKT authored Oct 16, 2024
1 parent 5f0e8a4 commit 0859108
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
33 changes: 33 additions & 0 deletions one_compliance/one_compliance/doc_events/sales_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
41 changes: 41 additions & 0 deletions one_compliance/public/js/sales_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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 <b>${frm.doc.name}</b>?`
),
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.")
);
}
},
});
}
);
});
}
});
}
}

0 comments on commit 0859108

Please sign in to comment.