diff --git a/audit_management/audit_management/doctype/my_audits/my_audits.py b/audit_management/audit_management/doctype/my_audits/my_audits.py index e533ede..83e2f1f 100644 --- a/audit_management/audit_management/doctype/my_audits/my_audits.py +++ b/audit_management/audit_management/doctype/my_audits/my_audits.py @@ -236,10 +236,16 @@ def sync_old_to_new(self): ) if self.status != "Draft" and old_status and row.status != old_status: - # Prevent fake Responded sync - if old_status == "Responded": + # Never downgrade responded rows if legacy field is lagging + if row.status == "Responded" and old_status in ["Pending", "No Response"]: + pass + + # Only sync Responded from legacy if a valid response exists + elif old_status == "Responded": if old_resp and str(old_resp).strip(): row.status = old_status + + # Normal sync for other cases (e.g., Pending -> No Response) else: row.status = old_status @@ -247,7 +253,10 @@ def sync_old_to_new(self): if ( old_resp and str(old_resp).strip() - and not row.response + and ( + not row.response + or not str(row.response).strip() + ) ): row.response = old_resp @@ -277,8 +286,8 @@ def sync_new_to_old(self): self.set(f"{prefix}_name", row.employee_name) self.set(f"{prefix}_mail", row.email) - # Update status only if it's currently empty in legacy fields - if not self.get(f"{prefix}_user_status"): + # Update status if different + if self.get(f"{prefix}_user_status") != row.status: self.set(f"{prefix}_user_status", row.status) self.set(f"{prefix}_response_box", row.response) diff --git a/audit_management/patches.txt b/audit_management/patches.txt index c22c1bf..98e4f2a 100755 --- a/audit_management/patches.txt +++ b/audit_management/patches.txt @@ -13,4 +13,5 @@ audit_management.patches.migrate_production_data_v4 audit_management.patches.migrate_hardcoded_to_child_table audit_management.patches.update_audit_level_branch_fields audit_management.patches.fix_status_mispelling -audit_management.patches.fix_fake_responded_status \ No newline at end of file +audit_management.patches.fix_fake_responded_status +audit_management.patches.fix_incorrect_responded_status \ No newline at end of file diff --git a/audit_management/patches/fix_incorrect_responded_status.py b/audit_management/patches/fix_incorrect_responded_status.py new file mode 100644 index 0000000..5ff2c81 --- /dev/null +++ b/audit_management/patches/fix_incorrect_responded_status.py @@ -0,0 +1,111 @@ +import frappe + + +def execute(): + """ + Patch to fix incorrect Pending / No Response statuses. + + Converts: + - Pending -> Responded if valid response exists. + + """ + + mapping = { + "BM": "bm", + "DH": "dh", + "COM": "com", + "RM": "rm", + "ROM": "rom", + "ZM": "zm", + "ZOM": "zom", + "GM": "gm", + "HR": "hr", + "CHRO": "chro", + "COO": "coo", + "CFO": "cfo", + "CEO": "ceo", + } + + audit_docs = frappe.get_all( + "My Audits", + pluck="name" + ) + + count = 0 + + for docname in audit_docs: + + try: + doc = frappe.get_doc("My Audits", docname) + + for row in doc.audit_stages: + + response_text = (row.response or "").strip() + + is_valid_response = ( + response_text + and response_text not in [ + "


", + "

", + "

", + "
", + ] + ) + + if ( + row.status == "Pending" + and is_valid_response + ): + + # ----------------------------- + # Update Child Table + # ----------------------------- + frappe.db.set_value( + "Audit Items", + row.name, + { + "status": "Responded", + }, + update_modified=False, + ) + + # ----------------------------- + # Sync Legacy Fields + # ----------------------------- + prefix = mapping.get( + (row.stage_name or "").strip().upper() + ) + + if prefix: + frappe.db.set_value( + "My Audits", + doc.name, + { + f"{prefix}_user_status": "Responded", + }, + update_modified=False, + ) + + count += 1 + + frappe.log_error( + title="Patch Fix: Pending To Responded", + message=( + f"Audit: {docname}\n" + f"Stage: {row.stage_name}\n" + f"User: {row.user_id}\n" + f"Row ID: {row.name}" + ), + ) + + except Exception: + frappe.log_error( + frappe.get_traceback(), + f"Patch Failed For Audit: {docname}" + ) + + frappe.db.commit() + + frappe.logger().info( + f"Patch complete. {count} audit stage records updated." + ) \ No newline at end of file