Skip to content

Commit

Permalink
[REF] l10n_it_fatturapa_pec: Use SdI channel's generic method to rece…
Browse files Browse the repository at this point in the history
…ive FE from SdI
  • Loading branch information
SirTakobi committed Jul 4, 2022
1 parent d64c69e commit 3fb298f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 61 deletions.
9 changes: 0 additions & 9 deletions l10n_it_fatturapa_pec/models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
class ResCompany(models.Model):
_inherit = 'res.company'

e_invoice_user_id = fields.Many2one(
"res.users", "E-bill creator",
help="This user will be used at supplier e-bill creation.",
default=lambda self: self.env.user.id
)
email_from_for_fatturaPA = fields.Char(
string="Sender Email Address",
related="sdi_channel_id.pec_server_id.email_from_for_fatturaPA",
Expand All @@ -26,10 +21,6 @@ class ResCompany(models.Model):
class AccountConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

e_invoice_user_id = fields.Many2one(
related='company_id.e_invoice_user_id',
readonly=False,
)
email_from_for_fatturaPA = fields.Char(
string="Sender Email Address",
related="company_id.sdi_channel_id.pec_server_id.email_from_for_fatturaPA",
Expand Down
68 changes: 20 additions & 48 deletions l10n_it_fatturapa_pec/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

import logging
import re
import base64
import zipfile
import io

from odoo import api, models, _
from odoo.exceptions import UserError
Expand Down Expand Up @@ -168,66 +165,41 @@ def find_attachment_by_subject(self, subject):
return attachment_out_model.browse()

def create_fatturapa_attachment_in(self, attachment, message_dict=None):
decoded = base64.b64decode(attachment.datas)
fatturapa_attachment_in = self.env['fatturapa.attachment.in']
fetchmail_server_id = self.env.context.get('fetchmail_server_id')
received_date = False
if message_dict is not None and 'date' in message_dict:
received_date = message_dict['date']
company_id = False
e_invoice_user_id = False
# The incoming supplier e-bill doesn't carry which company
# we must use to create the given fatturapa.attachment.in record,
# so we expect fetchmail_server_id coming in the context
# see fetchmail.py.
# With this information we search which SDI is actually using it,
# finally the SDI contain both company and user we would need to use
sdi_channel_model = self.env['sdi.channel']
if fetchmail_server_id:
sdi_chan = self.env['sdi.channel'].search([
sdi_chan = sdi_channel_model.search([
('fetch_pec_server_id', '=', fetchmail_server_id)], limit=1)
if sdi_chan:
# See check_fetch_pec_server_id
company_id = sdi_chan.company_id.id
e_invoice_user_id = sdi_chan.company_id.e_invoice_user_id.id
if e_invoice_user_id:
fatturapa_attachment_in = fatturapa_attachment_in.sudo(
e_invoice_user_id)
fatturapa_attachment = fatturapa_attachment_in.browse()
if attachment.mimetype == 'application/zip':
with zipfile.ZipFile(io.BytesIO(decoded)) as zf:
for file_name in zf.namelist():
inv_file = zf.open(file_name)
if fatturapa_regex.match(file_name):
# check if this invoice is already
# in other fatturapa.attachment.in
fatturapa_atts = fatturapa_attachment_in.search([
('name', '=', file_name)])
if fatturapa_atts:
_logger.info("In invoice %s already processed"
% fatturapa_atts.mapped('name'))
else:
fatturapa_attachment = fatturapa_attachment_in.create({
'name': file_name,
'datas_fname': file_name,
'datas': base64.encodestring(inv_file.read()),
'company_id': company_id,
'e_invoice_received_date': received_date,
})
else:
fatturapa_atts = fatturapa_attachment_in.search(
[('name', '=', attachment.name)])
if fatturapa_atts:
_logger.info(
"Invoice xml already processed in %s"
% fatturapa_atts.mapped('name'))
else:
fatturapa_attachment = fatturapa_attachment_in.create({
'ir_attachment_id': attachment.id,
'company_id': company_id,
'e_invoice_received_date': received_date,
})
file_name_content_dict = {
attachment.name: attachment.datas,
}
default_values = {
'company_id': company_id,
'e_invoice_received_date': received_date,
}
attachments = sdi_channel_model.receive_fe(
file_name_content_dict,
dict(), # Not managing metadata files for now
**default_values,
)

# Notify if there was an error
# during automatic import of invoices from PEC.
parsing_error = fatturapa_attachment.e_invoice_parsing_error
if parsing_error:
raise Exception(parsing_error)
for attachment in attachments:
parsing_error = attachment.e_invoice_parsing_error
if parsing_error:
raise Exception(parsing_error)
return attachments
4 changes: 0 additions & 4 deletions l10n_it_fatturapa_pec/views/company_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
class="col-lg-6"
/>
</div>
<div class="row">
<label for="e_invoice_user_id" class="col-lg-6 o_light_label"/>
<field name="e_invoice_user_id" class="col-lg-6"/>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 3fb298f

Please sign in to comment.