Skip to content

Commit

Permalink
feat(E Invoice Import): extract XML from PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Sep 9, 2024
1 parent 0b14680 commit d2a86d6
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import frappe
from drafthorse.models.document import Document as DrafthorseDocument
from erpnext import get_default_company
from facturx import get_xml_from_pdf
from frappe import _, _dict, get_site_path
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc
Expand Down Expand Up @@ -86,7 +87,13 @@ def before_submit(self):

def parse_einvoice(self):
path_to_einvoice = Path(get_site_path(self.einvoice.lstrip("/"))).resolve()
xml = path_to_einvoice.read_bytes()
if path_to_einvoice.suffix == ".pdf":
xml_filename, xml = get_xml_from_pdf(path_to_einvoice.read_bytes())
elif path_to_einvoice.suffix == ".xml":
xml = path_to_einvoice.read_bytes()
else:
frappe.throw(_("Unsupported file format '{0}'").format(path_to_einvoice.suffix))

doc = DrafthorseDocument.parse(xml)
self.id = str(doc.header.id)
self.issue_date = str(doc.header.issue_date_time)
Expand Down

0 comments on commit d2a86d6

Please sign in to comment.