def _create_invoice(self, content, filename): """ Create an invoice from given attachment content """ with patch.object(self.server._cr, 'commit', return_value=None): if filename.endswith(".p7m"): content = remove_signature(content) return self.server._create_invoice_from_mail( content, filename, '*****@*****.**')
def _attachment_invoice(self, msg_txt): parsed_values = self.env['mail.thread']._message_parse_extract_payload( msg_txt) body, attachments = parsed_values['body'], parsed_values['attachments'] from_address = msg_txt.get('from') for attachment in attachments: split_attachment = attachment.fname.rpartition('.') if len(split_attachment) < 3: _logger.info('E-invoice filename not compliant: %s', attachment.fname) continue attachment_name = split_attachment[0] attachment_ext = split_attachment[2] split_underscore = attachment_name.rsplit('_', 2) if len(split_underscore) < 2: _logger.info('E-invoice filename not compliant: %s', attachment.fname) continue if attachment_ext != 'zip': if split_underscore[1] in [ 'RC', 'NS', 'MC', 'MT', 'EC', 'SE', 'NE', 'DT' ]: # we have a receipt self._message_receipt_invoice(split_underscore[1], attachment) else: att_filename = attachment.fname match = re.search( "([A-Z]{2}[A-Za-z0-9]{2,28}_[A-Za-z0-9]{0,5}.(xml.p7m|xml))", att_filename) # If match, we have an invoice. if match: # If it's signed, the content has a bytes type and we just remove the signature's envelope if match.groups()[1] == 'xml.p7m': att_content_data = remove_signature( attachment.content) # If the envelope cannot be removed, the remove_signature returns None, so we skip if not att_content_data: _logger.warning( "E-invoice couldn't be read: %s", att_filename) continue att_filename = att_filename.replace( '.xml.p7m', '.xml') else: # Otherwise, it should be an utf-8 encoded XML string att_content_data = attachment.content.encode() self._create_invoice_from_mail(att_content_data, att_filename, from_address) else: if split_underscore[1] == 'AT': # Attestazione di avvenuta trasmissione della fattura con impossibilità di recapito self._message_AT_invoice(attachment) else: _logger.info('New E-invoice in zip file: %s', attachment.fname) self._create_invoice_from_mail_with_zip( attachment, from_address)
def _decode_p7m_to_xml(self, filename, content): decoded_content = remove_signature(content) if not decoded_content: return None try: # Some malformed XML are accepted by FatturaPA, this expends compatibility parser = etree.XMLParser(recover=True) xml_tree = etree.fromstring(decoded_content, parser) except Exception as e: _logger.exception("Error when converting the xml content to etree: %s", e) return None if not len(xml_tree): return None return xml_tree