move parsed-data code to new file, deny convert of factur-x-basic-wl
This commit is contained in:
parent
6b32c610f5
commit
ef859e91b2
3 changed files with 399 additions and 175 deletions
58
document.py
58
document.py
|
@ -28,7 +28,7 @@ xml_types = [
|
|||
(['xsd', 'Factur-X_1.07.2_BASIC', 'Factur-X_1.07.2_BASIC.xsd'],
|
||||
'Factur-X basic', 'facturx_basic'),
|
||||
(['xsd', 'Factur-X_1.07.2_BASICWL', 'Factur-X_1.07.2_BASICWL.xsd'],
|
||||
'Factur-X basic-wl', ''),
|
||||
'Factur-X basicwl', 'facturx_basicwl'),
|
||||
(['xsd', 'Factur-X_1.07.2_EN16931', 'Factur-X_1.07.2_EN16931.xsd'],
|
||||
'Factur-X EN16931', ''),
|
||||
(['xsd', 'Factur-X_1.07.2_EXTENDED', 'Factur-X_1.07.2_EXTENDED.xsd'],
|
||||
|
@ -63,20 +63,25 @@ class Incoming(metaclass=PoolMeta):
|
|||
with_children (bool, optional): convert sub-documents.
|
||||
Defaults to False.
|
||||
"""
|
||||
print('\n## process:', documents)
|
||||
for document in documents:
|
||||
cls._readxml_check_content(document)
|
||||
document._facturx_detect_content()
|
||||
print('-- process-2:')
|
||||
super().process(documents, with_children)
|
||||
print('-- process-3:')
|
||||
|
||||
def _process_supplier_invoice(self):
|
||||
""" try to detect content of 'data', read values
|
||||
"""
|
||||
invoice = super()._process_supplier_invoice()
|
||||
print('\n## _process_supplier_invoice:', self)
|
||||
|
||||
if self.mime_type == 'application/xml':
|
||||
# detect xml-content
|
||||
xml_info = self._facturx_detect_content()
|
||||
if xml_info:
|
||||
(xsd_type, funcname, xmltree) = xml_info
|
||||
print('-- _process_supplier_invoice-xml_info:', xml_info)
|
||||
xml_read_func = getattr(self, '_readxml_%s' % funcname, None)
|
||||
if not xml_read_func:
|
||||
raise UserError(gettext(
|
||||
|
@ -90,6 +95,7 @@ class Incoming(metaclass=PoolMeta):
|
|||
invoice.save()
|
||||
self._readxml_check_invoice(invoice)
|
||||
# raise ValueError('stop')
|
||||
print('-- _process_supplier_invoice-FIN:', invoice)
|
||||
return invoice
|
||||
|
||||
def _readxml_check_invoice(self, invoice):
|
||||
|
@ -422,6 +428,14 @@ class Incoming(metaclass=PoolMeta):
|
|||
"""
|
||||
self._readxml_facturx_extended(xmltree)
|
||||
|
||||
def _readxml_facturx_basicwl(self, xmltree):
|
||||
""" read facturx - basic-wl
|
||||
"""
|
||||
# deny usage of factur-x basicwl because it contains no invoice-lines
|
||||
raise UserError(gettext(
|
||||
'document_incoming_invoice_xml.msg_convert_error',
|
||||
msg='factur-x basic-wl not supported'))
|
||||
|
||||
def _readxml_facturx_extended(self, xmltree):
|
||||
""" read factur-x extended
|
||||
"""
|
||||
|
@ -1016,29 +1030,7 @@ class Incoming(metaclass=PoolMeta):
|
|||
msg='cannot convert %(val)s' % {
|
||||
'val': date_string}))
|
||||
|
||||
@classmethod
|
||||
def _readxml_check_content(cls, document):
|
||||
""" try to detect content, fire exception if fail
|
||||
|
||||
Args:
|
||||
document (record): model document.incoming
|
||||
"""
|
||||
xml_data = etree.fromstring(document.data)
|
||||
try:
|
||||
fx_flavour = facturx.get_flavor(xml_data)
|
||||
fx_level = facturx.get_level(xml_data)
|
||||
document._facturx_detect_content(fx_flavour, fx_level)
|
||||
document.save()
|
||||
except Exception as e1:
|
||||
raise UserError(gettext(
|
||||
'document_incoming_invoice_xml.msg_convert_error',
|
||||
msg='%(name)s [%(flavour)s|%(level)s] %(msg)s' % {
|
||||
'name': document.name or '-',
|
||||
'flavour': fx_flavour,
|
||||
'level': fx_level,
|
||||
'msg': str(e1)}))
|
||||
|
||||
def _facturx_detect_content(self, flavour=None, level=None):
|
||||
def _facturx_detect_content(self):
|
||||
""" check xml-data against xsd of XRechnung and FacturX,
|
||||
begin with extended, goto minimal, then xrechnung
|
||||
|
||||
|
@ -1047,11 +1039,13 @@ class Incoming(metaclass=PoolMeta):
|
|||
defaults to None if not detected
|
||||
"""
|
||||
xml_data = etree.fromstring(self.data)
|
||||
fx_flavour = facturx.get_flavor(xml_data)
|
||||
fx_level = facturx.get_level(xml_data)
|
||||
|
||||
for xsdpath, xsdtype, funcname in xml_types:
|
||||
if flavour == 'factur-x' and level:
|
||||
if fx_flavour == 'factur-x' and fx_level:
|
||||
if not (xsdtype.lower().startswith('factur-x') and
|
||||
xsdtype.lower().endswith(level)):
|
||||
xsdtype.lower().endswith(fx_level)):
|
||||
# skip check if xml-content is already known
|
||||
continue
|
||||
|
||||
|
@ -1063,8 +1057,14 @@ class Incoming(metaclass=PoolMeta):
|
|||
return (xsdtype, funcname, xml_data)
|
||||
except etree.DocumentInvalid as e1:
|
||||
# fire exception for knows xml-content
|
||||
if flavour == 'factur-x' and level:
|
||||
raise e1
|
||||
if fx_flavour == 'factur-x' and fx_level:
|
||||
raise UserError(gettext(
|
||||
'document_incoming_invoice_xml.msg_convert_error',
|
||||
msg='%(name)s [%(flavour)s|%(level)s] %(msg)s' % {
|
||||
'name': self.name or '-',
|
||||
'flavour': fx_flavour,
|
||||
'level': fx_level,
|
||||
'msg': str(e1)}))
|
||||
pass
|
||||
return None
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue