diff --git a/facho/facho.py b/facho/facho.py index 752af73..c49e423 100644 --- a/facho/facho.py +++ b/facho/facho.py @@ -142,11 +142,16 @@ class FachoXML: elem = self.find_or_create_element('/' + root_tag + xpath) self.builder.append(elem, new_element) - def fragment(self, xpath, append=False): + def fragment(self, xpath, append=False, append_not_exists=False): nodes = xpath.split('/') nodes.pop() root_prefix = '/'.join(nodes) - parent = self.find_or_create_element(xpath, append=append) + parent = None + if append_not_exists: + parent = self.get_element(xpath) + + if parent is None: + parent = self.find_or_create_element(xpath, append=append) return FachoXML(parent, nsmap=self.nsmap, fragment_prefix=root_prefix) def register_alias_xpath(self, alias, xpath): @@ -208,6 +213,10 @@ class FachoXML: self.builder.set_attribute(elem, k, v) return elem + def get_element(self, xpath): + xpath = self.fragment_prefix + self._normalize_xpath(xpath) + return self.builder.xpath(self.root, xpath) + def get_element_text(self, xpath, format_=str): xpath = self.fragment_prefix + self._normalize_xpath(xpath) elem = self.builder.xpath(self.root, xpath) diff --git a/facho/fe/fe.py b/facho/fe/fe.py index 6a79eac..40133b4 100644 --- a/facho/fe/fe.py +++ b/facho/fe/fe.py @@ -136,12 +136,13 @@ class DianXMLExtensionSoftwareSecurityCode(FachoXMLExtension): self.pin = pin self.invoice_ident = invoice_ident - def build(self, fachoxml): - dian_path = '/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/sts:DianExtensions/sts:SoftwareSecurityCode' + def build(self, fexml): + dian_path = '/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/sts:DianExtensions/sts:SoftwareSecurityCode' code = str(self.id_software) + str(self.pin) + str(self.invoice_ident) m = hashlib.sha384() m.update(code.encode('utf-8')) - return dian_path, m.hexdigest() + fexml.set_element(dian_path, m.hexdigest()) + return '', [] class DianXMLExtensionSigner(FachoXMLExtension): @@ -226,8 +227,7 @@ class DianXMLExtensionSigner(FachoXMLExtension): #xmlsig take parent root fachoxml.root.remove(signature) - - ublextension = fachoxml.fragment('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension', append=True) + ublextension = fachoxml.fragment('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension', append_not_exists=True) extcontent = ublextension.find_or_create_element('/ext:UBLExtension:/ext:ExtensionContent') fachoxml.append_element(extcontent, signature) diff --git a/tests/test_fe_form.py b/tests/test_fe_form.py index 3ec23f0..56fb27f 100644 --- a/tests/test_fe_form.py +++ b/tests/test_fe_form.py @@ -77,18 +77,12 @@ def test_invoicesimple_build(simple_invoice): assert invoice_validator.validate(simple_invoice) == True xml = form.DIANInvoiceXML(simple_invoice) - supplier_name = xml.get_element_text('/fe:Invoice/fe:AccountingSupplierParty/fe:Party/cac:PartyName/cbc:Name') + supplier_name = xml.get_element_text('/fe:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name') assert supplier_name == simple_invoice.invoice_supplier.name - supplier_identification_number = xml.get_element_text('/fe:Invoice/fe:AccountingSupplierParty/fe:Party/cac:PartyIdentification/cbc:ID') - assert int(supplier_identification_number) == simple_invoice.invoice_supplier.ident - - customer_name = xml.get_element_text('/fe:Invoice/fe:AccountingCustomerParty/fe:Party/cac:PartyName/cbc:Name') + customer_name = xml.get_element_text('/fe:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name') assert customer_name == simple_invoice.invoice_customer.name - customer_identification_number = xml.get_element_text('/fe:Invoice/fe:AccountingCustomerParty/fe:Party/cac:PartyIdentification/cbc:ID') - assert int(customer_identification_number) == simple_invoice.invoice_customer.ident - def test_invoicesimple_build_with_cufe(simple_invoice): invoice_validator = form.DianResolucion0001Validator() @@ -130,7 +124,7 @@ def test_invoicesimple_zip(simple_invoice): def test_bug_cbcid_empty_on_invoice_line(simple_invoice): xml_invoice = form.DIANInvoiceXML(simple_invoice) - cbc_id = xml_invoice.get_element_text('/fe:Invoice/fe:InvoiceLine[1]/cbc:ID', format_=int) + cbc_id = xml_invoice.get_element_text('/fe:Invoice/cac:InvoiceLine[1]/cbc:ID', format_=int) assert cbc_id == 1 def test_invoice_line_count_numeric(simple_invoice):