Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions addons/l10n_it_edi/models/account_edi_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ def _import_fattura_pa(self, tree, invoice):
invoice = self.env['account.move']
first_run = False

# Type must be present in the context to get the right behavior of the _default_journal method (account.move).
# journal_id must be present in the context to get the right behavior of the _default_account method (account.move.line).
elements = tree.xpath('//CessionarioCommittente//IdCodice')
company = elements and self.env['res.company'].search([('vat', 'ilike', elements[0].text)], limit=1)
if not company:
elements = tree.xpath('//CessionarioCommittente//CodiceFiscale')
company = elements and self.env['res.company'].search([('l10n_it_codice_fiscale', 'ilike', elements[0].text)], limit=1)
if not company:
# Only invoices with a correct VAT or Codice Fiscale can be imported
_logger.warning('No company found with VAT or Codice Fiscale like %r.', elements[0].text)
continue

# Refund type.
# TD01 == invoice
# TD02 == advance/down payment on invoice
Expand All @@ -123,32 +135,14 @@ def _import_fattura_pa(self, tree, invoice):
move_type = 'in_refund'
elif elements and elements[0].text and elements[0].text != 'TD01':
_logger.info('Document type not managed: %s. Invoice type is set by default.', elements[0].text)
invoice_ctx = invoice.with_context(default_move_type=move_type)

# type must be present in the context to get the right behavior of the _default_journal method (account.move).
# journal_id must be present in the context to get the right behavior of the _default_account method (account.move.line).

elements = tree.xpath('//CessionarioCommittente//IdCodice')
company = elements and self.env['res.company'].search([('vat', 'ilike', elements[0].text)], limit=1)
if not company:
elements = tree.xpath('//CessionarioCommittente//CodiceFiscale')
company = elements and self.env['res.company'].search([('l10n_it_codice_fiscale', 'ilike', elements[0].text)], limit=1)

if company:
invoice_ctx = invoice_ctx.with_context(company_id=company.id)
else:
company = self.env.company
if elements:
_logger.info('No company found with codice fiscale: %s. The user\'s company is set by default.', elements[0].text)
else:
_logger.info('Company not found. The user\'s company is set by default.')

if not self.env.is_superuser():
if self.env.company != company:
raise UserError(_("You can only import invoice concern your current company: %s", self.env.company.display_name))
# Setup the context for the Invoice Form
invoice_ctx = invoice.with_company(company) \
.with_context(default_move_type=move_type,
account_predictive_bills_disable_prediction=True)

# move could be a single record (editing) or be empty (new).
with Form(invoice_ctx.with_context(account_predictive_bills_disable_prediction=True)) as invoice_form:
with Form(invoice_ctx) as invoice_form:
message_to_log = []

# Partner (first step to avoid warning 'Warning! You must first select a partner.'). <1.2>
Expand Down
16 changes: 11 additions & 5 deletions addons/l10n_it_edi/models/ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,22 @@ def _attachment_invoice(self, msg_txt):
# we have a receipt
self._message_receipt_invoice(split_underscore[1], attachment)
else:
match = re.search("([A-Z]{2}[A-Za-z0-9]{2,28}_[A-Za-z0-9]{0,5}.(xml.p7m|xml))", attachment.fname)
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", attachment.fname)
_logger.warning("E-invoice couldn't be read: %s", att_filename)
continue
# Otherwise, it should be an utf-8 encoded XML string
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, attachment.fname, from_address)
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
Expand All @@ -174,7 +176,7 @@ def _create_invoice_from_mail(self, att_content_data, att_name, from_address):
return invoices

# Create the new attachment for the file
self.env['ir.attachment'].create({
attachment = self.env['ir.attachment'].create({
'name': att_name,
'raw': att_content_data,
'res_model': 'account.move',
Expand All @@ -195,6 +197,10 @@ def _create_invoice_from_mail(self, att_content_data, att_name, from_address):

invoices.l10n_it_send_state = 'new'
invoices.invoice_source_email = from_address
for invoice in invoices:
invoice.with_context(no_new_invoice=True, default_res_id=invoice.id) \
.message_post(body=(_("Original E-invoice XML file")), attachment_ids=[attachment.id])

self._cr.commit()

_logger.info('New E-invoices (%s), ids: %s', att_name, [x.id for x in invoices])
Expand Down
5 changes: 4 additions & 1 deletion addons/l10n_it_edi/tests/test_ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def setUpClass(cls):
super().setUpClass(chart_template_ref='l10n_it.l10n_it_chart_template_generic',
edi_format_ref='l10n_it_edi.edi_fatturaPA')

# Use the company_data_2 to test that the e-invoice is imported for the right company
cls.company = cls.company_data_2['company']

# Initialize the company's codice fiscale
cls.company = cls.company_data['company']
cls.company.l10n_it_codice_fiscale = 'IT01234560157'

# Build test data.
Expand Down Expand Up @@ -108,6 +110,7 @@ def test_receive_signed_vendor_bill(self):
""" Test a signed (P7M) sample e-invoice file from https://www.fatturapa.gov.it/export/documenti/fatturapa/v1.2/IT01234567890_FPR01.xml """
invoices = self._create_invoice(self.signed_invoice_content, self.signed_invoice_filename)
self.assertRecordValues(invoices, [{
'company_id': self.company.id,
'name': 'BILL/2014/12/0001',
'date': datetime.date(2014, 12, 18),
'ref': '01234567890',
Expand Down