|
| 1 | +# Copyright 2026 Quartile (https://www.quartile.co) |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from markupsafe import Markup |
| 5 | + |
| 6 | +from odoo import fields, models |
| 7 | +from odoo.tools.image import image_data_uri |
| 8 | + |
| 9 | +FIRST_PAGE_ONLY_CLASS = "first-page-only" |
| 10 | +FIRST_PAGE_HIDE_SCRIPT = """ |
| 11 | +<script> |
| 12 | +var page = window.location.search.match(/[?&]page=(\\d+)/); |
| 13 | +if (page && parseInt(page[1]) > 1) {{ |
| 14 | + var elements = document.getElementsByClassName('{css_class}'); |
| 15 | + for (var i = 0; i < elements.length; i++) {{ |
| 16 | + elements[i].style.display = 'none'; |
| 17 | + }} |
| 18 | +}} |
| 19 | +</script> |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +class IrActionsReport(models.Model): |
| 24 | + _inherit = "ir.actions.report" |
| 25 | + |
| 26 | + image_mode = fields.Selection( |
| 27 | + selection=[ |
| 28 | + ("company", "Company-level Images"), |
| 29 | + ("custom", "Report-specific Images"), |
| 30 | + ], |
| 31 | + string="Report Image", |
| 32 | + ) |
| 33 | + report_positioned_image_ids = fields.Many2many( |
| 34 | + comodel_name="report.positioned.image", |
| 35 | + relation="ir_actions_report_positioned_image_rel", |
| 36 | + column1="report_id", |
| 37 | + column2="image_id", |
| 38 | + string="Custom Images", |
| 39 | + ) |
| 40 | + |
| 41 | + def _render_qweb_pdf(self, report_ref, res_ids=None, data=None): |
| 42 | + """Set company context so _get_positioned_image_configs uses the |
| 43 | + correct company. |
| 44 | + """ |
| 45 | + company = self._get_report_company(res_ids) |
| 46 | + return super(IrActionsReport, self.with_company(company))._render_qweb_pdf( |
| 47 | + report_ref, res_ids, data |
| 48 | + ) |
| 49 | + |
| 50 | + def _prepare_html(self, html, report_model=False): |
| 51 | + image_configs = self._get_positioned_image_configs() |
| 52 | + if not image_configs: |
| 53 | + return super()._prepare_html(html, report_model=report_model) |
| 54 | + first_page_images, all_page_images = self._split_images_by_page(image_configs) |
| 55 | + result = super()._prepare_html(html, report_model=report_model) |
| 56 | + if not isinstance(result, tuple): |
| 57 | + return result |
| 58 | + bodies, res_ids, header, footer, specific_paperformat_args = result |
| 59 | + if first_page_images or all_page_images: |
| 60 | + header = self._inject_images_into_header( |
| 61 | + header, all_page_images, first_page_images |
| 62 | + ) |
| 63 | + return bodies, res_ids, header, footer, specific_paperformat_args |
| 64 | + |
| 65 | + def _split_images_by_page(self, image_configs): |
| 66 | + first_page_images = [] |
| 67 | + all_page_images = [] |
| 68 | + for config in image_configs: |
| 69 | + if config.get("first_page_only"): |
| 70 | + first_page_images.append(config) |
| 71 | + else: |
| 72 | + all_page_images.append(config) |
| 73 | + return first_page_images, all_page_images |
| 74 | + |
| 75 | + def _inject_images_into_header(self, header, all_page_images, first_page_images): |
| 76 | + header_parts = [] |
| 77 | + if all_page_images: |
| 78 | + header_parts.append(self._build_image_html(all_page_images)) |
| 79 | + if first_page_images: |
| 80 | + header_parts.append( |
| 81 | + self._build_image_html(first_page_images, FIRST_PAGE_ONLY_CLASS) |
| 82 | + ) |
| 83 | + header_parts.append( |
| 84 | + Markup(FIRST_PAGE_HIDE_SCRIPT.format(css_class=FIRST_PAGE_ONLY_CLASS)) |
| 85 | + ) |
| 86 | + combined_html = Markup("").join(header_parts) |
| 87 | + return self._insert_html_into_header(header, combined_html) |
| 88 | + |
| 89 | + def _insert_html_into_header(self, header, html_to_inject): |
| 90 | + if Markup("</body>") in header: |
| 91 | + return header.replace( |
| 92 | + Markup("</body>"), html_to_inject + Markup("</body>"), 1 |
| 93 | + ) |
| 94 | + if Markup("<body>") in header: |
| 95 | + return header.replace( |
| 96 | + Markup("<body>"), Markup("<body>") + html_to_inject, 1 |
| 97 | + ) |
| 98 | + return header + html_to_inject |
| 99 | + |
| 100 | + @staticmethod |
| 101 | + def _build_image_html(images, css_class=""): |
| 102 | + parts = [] |
| 103 | + for image in images: |
| 104 | + image_content = image.get("image") |
| 105 | + if not image_content: |
| 106 | + continue |
| 107 | + style_parts = [ |
| 108 | + "position: fixed", |
| 109 | + f"top: {image.get('pos_top', 5)}mm", |
| 110 | + f"right: {image.get('pos_right', 5)}mm", |
| 111 | + f"width: {image.get('width', 20)}mm", |
| 112 | + f"height: {image.get('height', 20)}mm", |
| 113 | + ] |
| 114 | + style = "; ".join(style_parts) + ";" |
| 115 | + data_uri = image_data_uri(image_content) |
| 116 | + class_attr = f' class="{css_class}"' if css_class else "" |
| 117 | + parts.append( |
| 118 | + f'<div{class_attr} style="{style}">' |
| 119 | + f'<img src="{data_uri}" style="width: 100%; height: 100%;"/>' |
| 120 | + "</div>" |
| 121 | + ) |
| 122 | + return Markup("".join(parts)) |
| 123 | + |
| 124 | + def _get_report_company(self, res_ids): |
| 125 | + """Resolve the company used for report images. |
| 126 | +
|
| 127 | + Prefer the company from the report records when available. |
| 128 | + Fallback to the current environment company. |
| 129 | + """ |
| 130 | + if not res_ids or not self.model: |
| 131 | + return self.env.company |
| 132 | + model = self.env[self.model] |
| 133 | + if "company_id" not in model._fields: |
| 134 | + return self.env.company |
| 135 | + records = model.browse(res_ids).exists() |
| 136 | + companies = records.mapped("company_id") |
| 137 | + return companies[0] if len(companies) == 1 else self.env.company |
| 138 | + |
| 139 | + def _get_positioned_image_configs(self): |
| 140 | + if not self.image_mode: |
| 141 | + return [] |
| 142 | + company = self.env.company |
| 143 | + images = ( |
| 144 | + company.report_positioned_image_ids |
| 145 | + if self.image_mode == "company" |
| 146 | + else self.report_positioned_image_ids.filtered( |
| 147 | + lambda img: img.company_id == company |
| 148 | + ) |
| 149 | + ) |
| 150 | + return [ |
| 151 | + { |
| 152 | + "image": img.image, |
| 153 | + "pos_top": img.pos_top, |
| 154 | + "pos_right": img.pos_right, |
| 155 | + "width": img.width, |
| 156 | + "height": img.height, |
| 157 | + "first_page_only": img.first_page_only, |
| 158 | + } |
| 159 | + for img in images |
| 160 | + if img.image |
| 161 | + ] |
0 commit comments