Skip to content

Commit

Permalink
Merge PR #3785 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by sergiocorato
  • Loading branch information
OCA-git-bot committed Aug 30, 2024
2 parents 2b474b8 + f143cf4 commit cff59dc
Show file tree
Hide file tree
Showing 73 changed files with 2,617 additions and 610 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ repos:
- id: pylint_odoo
args:
- --rcfile=.pylintrc-mandatory
- --ignore=l10n_it_fatturapa_in/tests/data/
1 change: 1 addition & 0 deletions l10n_it_fatturapa/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"views/account_view.xml",
"views/company_view.xml",
"views/partner_view.xml",
"views/fatturapa_attachment_views.xml",
"views/related_document_type_views.xml",
"security/ir.model.access.csv",
],
Expand Down
3 changes: 2 additions & 1 deletion l10n_it_fatturapa/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ class FatturaElettronicaController(Controller):
)
def pdf_preview(self, attachment_id, **data):
attach = request.env["ir.attachment"].browse(int(attachment_id))
html = attach.get_fattura_elettronica_preview()
fatturapa_attachment_model = request.env["fatturapa.attachment"]
html = fatturapa_attachment_model.get_fattura_elettronica_preview(attach)
return request.make_response(html)
53 changes: 42 additions & 11 deletions l10n_it_fatturapa/models/ir_attachment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright 2022 Simone Rubino - TAKOBI
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64
import binascii
import logging
Expand All @@ -6,9 +9,9 @@

import lxml.etree as ET

from odoo import fields, models
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.modules import get_resource_path
from odoo.modules import get_module_resource
from odoo.tools.translate import _

_logger = logging.getLogger(__name__)
Expand All @@ -28,17 +31,38 @@ def is_base64(s):
return re_base64.match(s)


class Attachment(models.Model):
_inherit = "ir.attachment"

class FatturaPAAttachment(models.Model):
_name = "fatturapa.attachment"
_description = "SdI file"
_inherits = {
"ir.attachment": "ir_attachment_id",
}
_inherit = [
"mail.thread",
"l10n_it_fatturapa.attachment.e_invoice.link",
]
_order = "id desc"

id = fields.Id()
ir_attachment_id = fields.Many2one(
comodel_name="ir.attachment",
string="Attachment",
required=True,
ondelete="cascade",
)
att_name = fields.Char(
string="SdI file name",
related="ir_attachment_id.name",
store=True,
)
ftpa_preview_link = fields.Char(
"Preview link", readonly=True, compute="_compute_ftpa_preview_link"
)

def _compute_ftpa_preview_link(self):
for att in self:
att.ftpa_preview_link = (
att.get_base_url() + "/fatturapa/preview/%s" % att.id
att.get_base_url() + "/fatturapa/preview/%s" % att.ir_attachment_id.id
)

def ftpa_preview(self):
Expand All @@ -49,6 +73,7 @@ def ftpa_preview(self):
"target": "new",
}

@api.model
def remove_xades_sign(self, xml):
# Recovering parser is needed for files where strings like
# xmlns:ds="http://www.w3.org/2000/09/xmldsig#""
Expand All @@ -66,6 +91,7 @@ def remove_xades_sign(self, xml):
ET.cleanup_namespaces(elem)
return ET.tostring(root)

@api.model
def strip_xml_content(self, xml):
recovering_parser = ET.XMLParser(recover=True)
root = ET.XML(xml, parser=recovering_parser)
Expand All @@ -76,14 +102,18 @@ def extract_cades(data):
info = cms.ContentInfo.load(data)
return info["content"]["encap_content_info"]["content"].native

@api.model
def cleanup_xml(self, xml_string):
xml_string = self.remove_xades_sign(xml_string)
xml_string = self.strip_xml_content(xml_string)
return xml_string

def get_xml_string(self):
def get_xml_string(self, attachment=None):
if not attachment:
self.ensure_one()
attachment = self.ir_attachment_id
try:
data = base64.b64decode(self.datas)
data = base64.b64decode(attachment.datas)
except binascii.Error as e:
raise UserError(_("Corrupted attachment %s.") % e.args) from e

Expand Down Expand Up @@ -113,8 +143,9 @@ def get_xml_string(self):
except AttributeError as e:
raise UserError(_("Invalid xml %s.") % e.args) from e

def get_fattura_elettronica_preview(self):
xml_string = self.get_xml_string()
@api.model
def get_fattura_elettronica_preview(self, attachment):
xml_string = self.get_xml_string(attachment)
xml_file = BytesIO(xml_string)
recovering_parser = ET.XMLParser(recover=True)
dom = ET.parse(xml_file, parser=recovering_parser)
Expand All @@ -132,7 +163,7 @@ def get_fattura_elettronica_preview(self):
else:
raise ValidationError(_("Unexpected root element: %s", root_tag))

xsl_path = get_resource_path("l10n_it_fatturapa", "data", preview_style)
xsl_path = get_module_resource("l10n_it_fatturapa", "data", preview_style)
xslt = ET.parse(xsl_path)
transform = ET.XSLT(xslt)
newdom = transform(dom)
Expand Down
1 change: 1 addition & 0 deletions l10n_it_fatturapa/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ access_fatturapa_summary_data_manager,access_fatturapa_summary_data_manager,mode
access_fatturapa_summary_data,access_fatturapa_summary_data,model_fatturapa_summary_data,account.group_account_invoice,1,0,0,0
access_withholding_data_line_manager,access_withholding_data_line_manager,model_withholding_data_line,account.group_account_manager,1,1,1,1
access_withholding_data_line,access_withholding_data_line,model_withholding_data_line,account.group_account_invoice,1,0,0,0
access_fatturapa_attachment,access_fatturapa_attachment,model_fatturapa_attachment,account.group_account_invoice,1,1,1,1
59 changes: 59 additions & 0 deletions l10n_it_fatturapa/views/fatturapa_attachment_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright 2022 Simone Rubino - TAKOBI
~ License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="fatturapa_attachment_view_form" model="ir.ui.view">
<field name="name">Form view for SdI file</field>
<field name="model">fatturapa.attachment</field>
<field name="arch" type="xml">
<form string="SdI file">
<sheet>
<group>
<group name="preview">
<field name='id' invisible="1" />
<label for="datas" />
<div>
<div>
<field name="datas" filename="name" />
</div>
<div>
<button
type="object"
name="ftpa_preview"
string="Show preview"
style="margin-bottom:10px;"
attrs="{'invisible': [('id', '=', False)]}"
/>
</div>
</div>
<field name="name" invisible="True" />
</group>
</group>
<notebook>
<page name="history" string="History">
<label for="create_uid" string="Created by" />
<div name="creation_div">
<field
name="create_uid"
readonly="1"
class="oe_inline"
/> on
<field
name="create_date"
readonly="1"
class="oe_inline"
/>
</div>
</page>
</notebook>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" />
<field name="message_ids" widget="mail_thread" />
</div>
</form>
</field>
</record>
</odoo>
4 changes: 2 additions & 2 deletions l10n_it_fatturapa_import_zip/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,6 @@ class FatturaPAAttachmentOut(models.Model):
ondelete="restrict",
)

def get_invoice_obj(self, fatturapa_attachment):
xml_string = fatturapa_attachment.get_xml_string()
def get_invoice_obj(self):
xml_string = self.get_xml_string()
return efattura.CreateFromDocument(xml_string)
Loading

0 comments on commit cff59dc

Please sign in to comment.