Skip to content

Commit

Permalink
[IMP] brand_external_report_layout : keep original company for report…
Browse files Browse the repository at this point in the history
…s and just temporarily use brand data

  - do not use company details and footer from brand, so that it could be used in multi-company
  - this avoid conflicts with other modules overriding company to add information in reports
Fixes OCA#167
Fixes OCA#179
  • Loading branch information
metaminux committed Oct 31, 2023
1 parent 3d60df8 commit 82d73ce
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 41 deletions.
1 change: 1 addition & 0 deletions brand_external_report_layout/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import res_brand
from . import res_company
27 changes: 15 additions & 12 deletions brand_external_report_layout/models/res_brand.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ def _get_default_brand_logo(self):
external_report_layout_id = fields.Many2one(
comodel_name="ir.ui.view", string="Document Template"
)
report_header = fields.Text(
help="Appears by default on the top right corner of your printed "
"documents (report header).",
)
report_footer = fields.Text(
translate=True,
help="Footer text displayed at the bottom of all reports.",
)
report_header = fields.Html(
string="Brand Tagline",
help="Appears by default on the top right corner of your printed documents "
"(report header).")
paperformat_id = fields.Many2one(
"report.paperformat",
"Paper format",
Expand All @@ -64,10 +60,6 @@ def _get_default_brand_logo(self):
)
primary_color = fields.Char()
secondary_color = fields.Char()
company_details = fields.Html(
string="Brand Details",
help="Header text displayed at the top of all reports.",
)
layout_background = fields.Selection(
[("Blank", "Blank"), ("Geometric", "Geometric"), ("Custom", "Custom")],
default="Blank",
Expand Down Expand Up @@ -112,6 +104,17 @@ def _update_asset_style(self):
def _get_style_fields(self):
return {"external_report_layout_id", "font", "primary_color", "secondary_color"}

@api.model
def _get_company_overriden_fields(self):
return {"name", "logo", "external_report_layout_id", "report_header",
"paperformat_id", "font", "primary_color", "secondary_color",
"layout_background", "layout_background_image"}

def _get_style_vals(self):
res = self.read(self._get_company_overriden_fields())[0]
res.pop("id")
return res

@api.model_create_multi
def create(self, vals_list):
companies = super().create(vals_list)
Expand Down
18 changes: 18 additions & 0 deletions brand_external_report_layout/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 Guillaume Masson <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class ResCompany(models.Model):

_inherit = "res.company"

def _get_style_vals(self):
res = self.read(self.env["res.brand"]._get_company_overriden_fields())[0]
res.pop("id")
return res

def update_style(self, vals):
self.write(vals)
return self
38 changes: 25 additions & 13 deletions brand_external_report_layout/views/report_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
<template id="brand_external_layout" inherit_id="web.external_layout">
<xpath expr="//t[@t-if='not company']" position="after">
<t t-if="doc and not o" t-set="o" t-value="doc" />
<t t-if="brand" t-set="brand_id" t-value="brand.id" />
<t
t-elif="o and 'brand_id' in o.fields_get() and o.brand_id and o.brand_id.external_report_layout_id"
t-set="brand_id"
t-value="o.brand_id.id"
/>
<t t-else="" t-set="brand_id" t-value="0" />
<t
t-if="o and 'brand_id' in o.fields_get() and o.brand_id and o.brand_id.external_report_layout_id"
t-call="{{o.brand_id.external_report_layout_id.sudo().key}}"
>
<t t-set="company" t-value="o.brand_id.sudo()" />
<t t-raw="0" />
<t t-if="brand_document_layout" t-set="brand_id" t-value="brand_document_layout.id" />
<t t-elif="o and 'brand_id' in o.fields_get() and o.brand_id">
<t t-set="brand" t-value="o.brand_id.sudo()" />
<t t-set="brand_id" t-value="brand.id" />
</t>
<t t-else="">
<t t-set="brand" t-value="0" />
<t t-set="brand_id" t-value="0" />
</t>
<t t-if="brand and brand.external_report_layout_id">
<t t-if="not company">
<t t-set="company" t-value="brand.sudo()" />
<t t-set="company_was_set" t-value="0" />
</t>
<t t-else="else">
<t t-set="company_was_set" t-value="1" />
<t t-set="brand_style_vals"
t-value="brand._get_style_vals()" />
<t t-set="company_style_vals"
t-value="company.sudo()._get_style_vals()" />
<t t-set="company" t-value="company.update_style(brand_style_vals)" />
</t>
<t t-call="{{brand.external_report_layout_id.sudo().key}}"><t t-out="0"/></t>
<t t-if="company_was_set" t-set="company"
t-value="company.update_style(company_style_vals)"/>
</t>
<t t-else="else" name="brand_not_set" />
</xpath>
Expand Down
20 changes: 4 additions & 16 deletions brand_external_report_layout/wizards/brand_document_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class BrandDocumentLayout(models.TransientModel):
brand_id = fields.Many2one("res.brand", required=True)

logo = fields.Binary(related="brand_id.logo", readonly=False)
report_header = fields.Text(related="brand_id.report_header", readonly=False)
report_footer = fields.Text(related="brand_id.report_footer", readonly=False)
report_header = fields.Html(related="brand_id.report_header", readonly=False)
report_footer = fields.Html(readonly=True)
paperformat_id = fields.Many2one(related="brand_id.paperformat_id", readonly=False)
external_report_layout_id = fields.Many2one(
related="brand_id.external_report_layout_id", readonly=False
Expand All @@ -30,7 +30,7 @@ class BrandDocumentLayout(models.TransientModel):
font = fields.Selection(related="brand_id.font", readonly=False)
primary_color = fields.Char(related="brand_id.primary_color", readonly=False)
secondary_color = fields.Char(related="brand_id.secondary_color", readonly=False)
company_details = fields.Html(related="brand_id.company_details", readonly=False)
company_details = fields.Html(readonly=True)
layout_background = fields.Selection(
related="brand_id.layout_background", readonly=False
)
Expand All @@ -47,16 +47,6 @@ def _onchange_brand_id(self):
for wizard in self:
wizard.logo = wizard.brand_id.logo
wizard.report_header = wizard.brand_id.report_header
wizard.report_footer = (
wizard.brand_id.report_footer
if isinstance(wizard.brand_id.report_footer, str)
else wizard.report_footer
)
wizard.company_details = (
wizard.brand_id.company_details
if isinstance(wizard.brand_id.company_details, str)
else wizard.company_details
)
wizard.paperformat_id = wizard.brand_id.paperformat_id
wizard.external_report_layout_id = wizard.brand_id.external_report_layout_id
wizard.font = wizard.brand_id.font
Expand Down Expand Up @@ -104,10 +94,8 @@ def _get_asset_style(self):
"primary_color",
"secondary_color",
"report_header",
"report_footer",
"layout_background",
"layout_background_image",
"company_details",
)
def _compute_preview(self):
styles = self._get_asset_style()
Expand All @@ -124,7 +112,7 @@ def _compute_preview(self):
wizard.preview = ir_ui_view._render_template(
"web.report_invoice_wizard_preview",
{
"brand": wizard_with_logo,
"brand_document_layout": wizard_with_logo,
"company": wizard_with_logo,
"preview_css": preview_css,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<xpath expr="//field[@name='company_id']" position="after">
<field name="brand_id" invisible="1" />
</xpath>
<field name="company_details" position="attributes">
<attribute name="invisible">1</attribute>
</field>
<field name="report_footer" position="attributes">
<attribute name="invisible">1</attribute>
</field>
</field>
</record>
</odoo>

0 comments on commit 82d73ce

Please sign in to comment.