read description + comment of invoice

This commit is contained in:
Frederik Jaeckel 2025-01-07 11:23:54 +01:00
parent 0024f76192
commit a63cdf5fcd
3 changed files with 50 additions and 33 deletions

View file

@ -72,15 +72,19 @@ class Incoming(metaclass=PoolMeta):
def _readxml_getvalue( def _readxml_getvalue(
self, xmltree, tags, vtype=None, allow_list=False, self, xmltree, tags, vtype=None, allow_list=False,
text_field=True): childs=[]):
""" read 'text'-part from xml-xpath, """ read 'text'-part from xml-xpath,
convert to 'vtype' convert to 'vtype'
Args: Args:
tags (list): list of tags to build xpath tags (list): list of tags to build xpath
vtype (type-class, optional): to convert value. Defaults to None. vtype (type-class, optional): to convert value of text-part
allow_list (boolean): get result as list of values (if not childs). Defaults to None.
text_field (boolean): return content of 'text'-part of node allow_list (boolean, optional): get result as list of values,
Defaults to False.
childs (list): read child-items of selected node
[('ns', 'tag', <type>),...],
if emtpy: read text-part of selected node, Defaults to []
Returns: Returns:
various: converted value or None various: converted value or None
@ -88,15 +92,29 @@ class Incoming(metaclass=PoolMeta):
result = [] result = []
xpath = '/' + '/'.join(tags) xpath = '/' + '/'.join(tags)
nodes = xmltree.xpath(xpath, namespaces=xmltree.nsmap) nodes = xmltree.xpath(xpath, namespaces=xmltree.nsmap)
# dict to find children of selected node
childs_dict = {
'{%(ns)s}%(tag)s' % {'ns': xmltree.nsmap[x[0]], 'tag': x[1]}: {
'type': x[2], 'tag': x[1]}
for x in childs}
if nodes: if nodes:
if not text_field:
result.extend(nodes)
else:
for node1 in nodes: for node1 in nodes:
if node1.text: if not childs_dict:
result.append( result.append(
node1.text if vtype is None node1.text if vtype is None else vtype(node1.text))
else vtype(node1.text)) else:
values = {}
for x in node1.getchildren():
if x.tag in childs_dict.keys():
values[childs_dict[x.tag]['tag']] = (
x.text if childs_dict[x.tag]['type'] is None
else childs_dict[x.tag]['type'](x.text))
result.append(values)
if not allow_list:
break
if not allow_list: if not allow_list:
return result[0] return result[0]
return result return result
@ -164,28 +182,21 @@ class Incoming(metaclass=PoolMeta):
note_list = self._readxml_getvalue(xmltree, [ note_list = self._readxml_getvalue(xmltree, [
'rsm:CrossIndustryInvoice', 'rsm:CrossIndustryInvoice',
'rsm:ExchangedDocument', 'ram:IncludedNote'], 'rsm:ExchangedDocument', 'ram:IncludedNote'],
allow_list=True, text_field=False) allow_list=True,
print('\n## note_list:', (note_list,)) childs=[('ram', 'Content', str), ('ram', 'ContentCode', str),
if note_list: ('ram', 'SubjectCode', str)])
invoice.description = self._readxml_getvalue(
note_list[0], ['ram:Content'], allow_list=False) if note_list:
if len(note_list) > 1: invoice.description = note_list[0].get('Content', None)
for x in note_list[1:]: invoice.comment = '\n'.join([
print('- x:', x) '%(code)s%(subj)s%(msg)s' % {
cont_code = self._readxml_getvalue( 'code': ('Code=%s, ' % x.get('ContentCode', ''))
x, ['ram:ContentCode'], allow_list=False) if x.get('ContentCode', '') else '',
print('- cont_code:', cont_code) 'subj': ('Subject=%s, ' % x.get('SubjectCode', ''))
cont_str = self._readxml_getvalue( if x.get('SubjectCode', '') else '',
x, ['ram:Content'], allow_list=False) 'msg': x.get('Content', ''),
} for x in note_list[1:]])
# descr_list = self._readxml_getvalue(xmltree, [
# 'rsm:CrossIndustryInvoice',
# 'rsm:ExchangedDocument', 'ram:IncludedNote',
# 'ram:Content'], allow_list=True)
# if descr_list:
# invoice.description = descr_list[0]
# if len(descr_list) > 1:
# invoice.comment = '\n'.join(descr_list[1:])
return invoice return invoice
def _readxml_convertdate(self, date_string): def _readxml_convertdate(self, date_string):

View file

@ -59,6 +59,11 @@ class DocumentTestCase(object):
self.assertEqual(invoice.invoice_date, date(2024, 6, 17)) self.assertEqual(invoice.invoice_date, date(2024, 6, 17))
self.assertEqual(invoice.currency.name, 'usd') self.assertEqual(invoice.currency.name, 'usd')
self.assertEqual(invoice.company.rec_name, 'm-ds') self.assertEqual(invoice.company.rec_name, 'm-ds')
self.assertEqual(invoice.description, 'Description of invoice')
self.assertEqual(
invoice.comment,
'Code=1, Some notes to the customer.\n' +
'Code=22, Subject=42, Goes to field comment.')
invoice.save() invoice.save()
print('\n## invoice:', invoice) print('\n## invoice:', invoice)

View file

@ -21,6 +21,7 @@
<ram:IncludedNote> <ram:IncludedNote>
<ram:Content>Goes to field comment.</ram:Content> <ram:Content>Goes to field comment.</ram:Content>
<ram:ContentCode>22</ram:ContentCode> <ram:ContentCode>22</ram:ContentCode>
<ram:SubjectCode>42</ram:SubjectCode>
</ram:IncludedNote> </ram:IncludedNote>
</rsm:ExchangedDocument> </rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction> <rsm:SupplyChainTradeTransaction>