Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions audit_management/audit_management/doctype/my_audits/my_audits.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,40 @@ frappe.ui.form.on("My Audits", {
const current_user = (frappe.session.user || "").toLowerCase();
const audit_table = frm.doc.auditstages || frm.doc.audit_stages || [];

// View Audit History Button (Prominent)
if (!frm.is_new()) {
frm.add_custom_button(__('Audit History'), function() {
frappe.call({
method: 'audit_management.audit_management.doctype.my_audits.my_audits.get_audit_history_summary',
args: { docname: frm.doc.name },
callback: function(r) {
let rows = r.message;
let html = `<table class='table table-bordered table-striped' id='audit-history-table'>
<thead><tr><th>Sr.</th><th>Event</th><th>User</th><th>Date/Time</th><th>Status</th></tr></thead>
<tbody>`;
rows.forEach((row, index) => {
html += `<tr><td>${index + 1}</td><td>${row.event}</td><td>${row.user}</td><td>${row.date}</td><td>${row.status}</td></tr>`;
});
html += `</tbody></table>`;

let d = new frappe.ui.Dialog({
title: __('Audit History'),
size: 'extra-large'
});

// Add Export button to header
d.header.append(`<button class="btn btn-sm btn-primary" style="margin-right: 40px;">Export to CSV</button>`);
d.header.find('.btn-primary').on('click', () => {
frappe.tools.downloadify(rows, ["event", "user", "date", "status"], "AuditHistory");
});

d.show();
$(d.body).html(html);
}
});
}).css({ "background-color": "#4a90e2", "color": "white" });
}

// 0. REOPEN LOGIC: Only Audit Team can reopen a Closed query
frm.trigger("reopen_query");

Expand Down
56 changes: 53 additions & 3 deletions audit_management/audit_management/doctype/my_audits/my_audits.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,9 +827,59 @@ def get_permission_query_conditions(user=None):
)
"""

# -------------------------------------------------------------
# WHITELISTED METHODS (Add these outside the MyAudits class)
# -------------------------------------------------------------
from frappe.utils import now, time_diff_in_seconds, time_diff_in_hours, getdate, nowdate, format_datetime

@frappe.whitelist()
def get_audit_history_summary(docname):
doc = frappe.get_doc("My Audits", docname)

def get_full_name(user_id):
if not user_id: return ""
if user_id == "System/Audit Team": return "System/Audit Team"
return frappe.db.get_value("User", user_id, "full_name") or user_id

# Formatted History List
history = []

# Creator Info
history.append({
"event": "Query Created",
"user": doc.query_generated_by_name or get_full_name(doc.owner),
"date": format_datetime(doc.creation, "dd-MM-yyyy hh:mm a"),
"status": "Created"
})

# Stages Info
creator_name = doc.query_generated_by_name or get_full_name(doc.owner)
for row in doc.audit_stages:
if row.status or row.pending_time:
history.append({
"event": f"Assigned to {row.stage_name} ({row.employee_name})",
"user": creator_name,
"date": format_datetime(row.pending_time, "dd-MM-yyyy hh:mm a") if row.pending_time else "",
"status": "Pending"
})
if row.status == "Responded":
history.append({
"event": f"Response from {row.stage_name}",
"user": row.employee_name,
"date": format_datetime(row.response_time, "dd-MM-yyyy hh:mm a") if row.response_time else "",
"status": "Responded"
})

# Closure Info
if doc.status == "Closed":
# Attempt to find who closed it
closed_by_id = frappe.db.get_value("Version", {"ref_doctype": "My Audits", "docname": doc.name, "data": ["like", "%status%Closed%"]}, "owner")
history.append({
"event": "Query Closed",
"user": get_full_name(closed_by_id) or "System/Audit Team",
"date": format_datetime(str(doc.closing_date) + " 00:00:00", "dd-MM-yyyy hh:mm a") if doc.closing_date else "",
"status": "Closed"
})

return history



@frappe.whitelist()
Expand Down
Loading