Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Preview style di fatturapa non è legato correttamente alla company #4349

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion l10n_it_fatturapa/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@


class FatturaElettronicaController(Controller):
def _get_allowed_company_ids(self):
"""Return list of allowed companies for request.

Default to the user's default company.
"""
# The allowed companies are set in the cookies in JS,
# specifically in https://github.com/odoo/odoo/
# blob/441c94270a4935d2b0b67a9f8201fcba9d15d957/
# addons/web/static/src/legacy/js/core/session.js#L338
allowed_company_ids_string = request.httprequest.cookies.get("cids")
if allowed_company_ids_string is not None:
allowed_company_ids = [
int(allowed_company_id_string)
for allowed_company_id_string in allowed_company_ids_string.split(",")
]
else:
allowed_company_ids = [

Check warning on line 21 in l10n_it_fatturapa/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_fatturapa/controllers/main.py#L21

Added line #L21 was not covered by tests
request.env.user.company_id.id,
]
return allowed_company_ids

@route(
[
"/fatturapa/preview/<attachment_id>",
Expand All @@ -13,5 +34,8 @@
def pdf_preview(self, attachment_id, **data):
attach = request.env["ir.attachment"].browse(int(attachment_id))
fatturapa_attachment_model = request.env["fatturapa.attachment"]
html = fatturapa_attachment_model.get_fattura_elettronica_preview(attach)
allowed_company_ids = self._get_allowed_company_ids()
html = fatturapa_attachment_model.with_context(
allowed_company_ids=allowed_company_ids,
).get_fattura_elettronica_preview(attach)
return request.make_response(html)
1 change: 1 addition & 0 deletions l10n_it_fatturapa_in/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import fatturapa_common
from . import test_import_fatturapa_xml
from . import test_fix_bad_uris
from . import test_preview
5 changes: 5 additions & 0 deletions l10n_it_fatturapa_in/tests/fatturapa_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ def _set_it_user(cls):
user = mail_new_test_user(
cls.env,
login="it_account_manager",
password="it_account_manager",
groups=",".join(groups_xml_id_dict.values()),
)

Expand All @@ -481,6 +482,10 @@ def setUpClass(cls):
country_id=cls.env.ref("base.it").id,
)
it_company = cls.company_data_it["company"]
cls.other_company_data = AccountTestInvoicingCommon.setup_company_data(
"Other company",
)
cls.other_company = cls.other_company_data["company"]
cls.env.user.company_id = it_company
cls.env.user.company_ids = it_company
cls.env["res.lang"]._activate_lang("it_IT")
Expand Down
61 changes: 61 additions & 0 deletions l10n_it_fatturapa_in/tests/test_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2024 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests import HttpCase

from .fatturapa_common import FatturapaCommon


class TestPreview(FatturapaCommon, HttpCase):
def _get_preview(self, e_invoice):
preview_action = e_invoice.ftpa_preview()
preview_url = preview_action["url"]
response = self.url_open(preview_url)
return response.content.decode()

def test_selected_company(self):
"""
The preview set in the current company
is different from the preview set in the user's default company;
the preview in the current company is used."""
# Arrange
user = self.env.user
other_company = self.other_company
user.company_ids += other_company
default_user_company = user.company_id
env = self.env(
context=dict(
**self.env.context,
allowed_company_ids=[
other_company.id,
default_user_company.id,
],
),
)
current_company = env.company
current_company.fatturapa_preview_style = "FoglioStileAssoSoftware.xsl"
default_user_company.fatturapa_preview_style = (
"Foglio_di_stile_fatturaordinaria_v1.2.2.xsl"
)
# pre-condition
self.assertNotEqual(current_company, default_user_company)
self.assertNotEqual(
current_company.fatturapa_preview_style,
default_user_company.fatturapa_preview_style,
)

# Act
e_invoice = self.create_attachment(
"preview_selected_company", "IT01234567890_FPR03.xml"
)
self.authenticate(user.login, user.login)
self.opener.cookies["cids"] = ",".join(
map(str, env.context["allowed_company_ids"])
)
preview = self._get_preview(e_invoice)

# Assert
expected_preview = env["fatturapa.attachment"].get_fattura_elettronica_preview(
e_invoice
)
self.assertIn(expected_preview, preview)
Loading