diff --git a/document.py b/document.py index f060463..93882e2 100644 --- a/document.py +++ b/document.py @@ -87,9 +87,41 @@ class Incoming(metaclass=PoolMeta): # update invoice with imported data if self.parsed_data: invoice = self._readxml_update_invoice(invoice) + invoice.save() + self._readxml_check_invoice(invoice) # raise ValueError('stop') return invoice + def _readxml_check_invoice(self, invoice): + """ check if calculated totals match with data from xml + + Raises: + UserError: if calculated values dont match + with xml-values + """ + totals = self.parsed_data.get('total', None) + if not totals: + raise UserError(gettext( + 'msg_convert_error.msg_convert_error', + msg='no totals-section in xml-data')) + + for xfield, inv_field in [ + ('taxbase', 'untaxed_amount'), + ('taxtotal', 'tax_amount'), + ('grand', 'total_amount')]: + xml_val = totals.get('taxbase', Decimal('0.0')) + inv_val = getattr(invoice, inv_field) + if xml_val != inv_val: + raise UserError(gettext( + 'document_incoming_invoice_xml.msg_convert_error', + msg=' '.join([ + inv_field + ' mismatch', + 'from-xml=' + Report.format_currency( + xml_val, None, invoice.currency), + 'calculated=' + Report.format_currency( + inv_val, None, invoice.currency) + ]))) + def _readxml_xpath(self, tags): """ generate xpath