diff --git a/edocument_integration/api.py b/edocument_integration/api.py index f442f0a..d97f2a8 100644 --- a/edocument_integration/api.py +++ b/edocument_integration/api.py @@ -133,7 +133,7 @@ def get_edocument_integration_settings(profile, company=None): @frappe.whitelist() -def transmit_edocument(edocument_name): +def transmit_edocument(edocument_name: str): # Transmit E-document using the configured integrator try: edocument_doc = frappe.get_doc("EDocument", edocument_name) @@ -190,13 +190,11 @@ def transmit_edocument(edocument_name): edocument_doc.add_comment(comment_type="Info", text="\n".join(parts)) # Set status to Transmission Successful and store reference ID - frappe.db.set_value( - "EDocument", - edocument_name, - {"status": "Transmission Successful", "error": None, "reference": transmission_id}, - update_modified=False, - ) - frappe.db.commit() + edocument_doc.reload() + edocument_doc.status = "Transmission Successful" + edocument_doc.error = None + edocument_doc.reference = transmission_id + edocument_doc.save() return transmission_result except frappe.ValidationError: @@ -204,13 +202,10 @@ def transmit_edocument(edocument_name): raise except Exception as e: # Set status to Transmission Failed - frappe.db.set_value( - "EDocument", - edocument_name, - {"status": "Transmission Failed", "error": str(e)}, - update_modified=False, - ) - frappe.db.commit() + edocument_doc.reload() + edocument_doc.status = "Transmission Failed" + edocument_doc.error = str(e) + edocument_doc.save() frappe.log_error( f"E-document transmission failed for document {edocument_name}: {e!s}", @@ -335,6 +330,10 @@ def webhook(**kwargs): } ) file_doc.save(ignore_permissions=True) + + # Save EDocument again to trigger field detection (company, etc.) from attached XML + edocument.reload() + edocument.save(ignore_permissions=True) frappe.db.commit() # nosemgrep: Webhook must persist before returning result = {"edocument": edocument.name, "document_id": document_id} @@ -356,7 +355,7 @@ def webhook(**kwargs): @frappe.whitelist() -def poll_incoming_invoices(profile=None, company=None): +def poll_incoming_invoices(profile: str | None = None, company: str | None = None): """ Poll Recommand inbox for incoming invoices and create EDocument records. diff --git a/edocument_integration/hooks.py b/edocument_integration/hooks.py index 08ea080..c7f9ce0 100644 --- a/edocument_integration/hooks.py +++ b/edocument_integration/hooks.py @@ -145,23 +145,11 @@ # Scheduled Tasks # --------------- -# scheduler_events = { -# "all": [ -# "edocument_integration.tasks.all" -# ], -# "daily": [ -# "edocument_integration.tasks.daily" -# ], -# "hourly": [ -# "edocument_integration.tasks.hourly" -# ], -# "weekly": [ -# "edocument_integration.tasks.weekly" -# ], -# "monthly": [ -# "edocument_integration.tasks.monthly" -# ], -# } +scheduler_events = { + "hourly": [ + "edocument_integration.tasks.poll_all_incoming_documents", + ], +} # Testing # ------- diff --git a/edocument_integration/tasks.py b/edocument_integration/tasks.py new file mode 100644 index 0000000..c5368e4 --- /dev/null +++ b/edocument_integration/tasks.py @@ -0,0 +1,21 @@ +import frappe + + +def poll_all_incoming_documents(): + """Poll incoming documents for all EDocument Integration Settings.""" + settings_list = frappe.get_all( + "EDocument Integration Settings", + pluck="name", + ) + + for settings_name in settings_list: + try: + settings = frappe.get_doc("EDocument Integration Settings", settings_name) + settings.poll_incoming_documents() + frappe.db.commit() + except Exception: + frappe.db.rollback() + frappe.log_error( + f"Auto-poll failed for {settings_name}", + "EDocument Auto-Poll Error", + )