Skip to content
Open
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
31 changes: 15 additions & 16 deletions edocument_integration/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -190,27 +190,22 @@ 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:
# Validation errors (e.g., from _validate_source_docstatus) should be re-raised as-is
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}",
Expand Down Expand Up @@ -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}
Expand All @@ -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.

Expand Down
22 changes: 5 additions & 17 deletions edocument_integration/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -------
Expand Down
21 changes: 21 additions & 0 deletions edocument_integration/tasks.py
Original file line number Diff line number Diff line change
@@ -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",
)
Loading