diff --git a/report_pdf_options/__init__.py b/report_pdf_options/__init__.py new file mode 100644 index 0000000..027f7d8 --- /dev/null +++ b/report_pdf_options/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +# +from . import models diff --git a/report_pdf_options/__manifest__.py b/report_pdf_options/__manifest__.py new file mode 100644 index 0000000..9e23905 --- /dev/null +++ b/report_pdf_options/__manifest__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +{ + 'name': 'Pdf report options', + 'summary': """shows a modal window with options for printing, downloading or opening pdf reports""", + 'description': """ + Choose one of the following options when printing a pdf report: + - print. print the pdf report directly with the browser + - download. download the pdf report on your computer + - open. open the pdf report in a new tab + You can also set a default options for each report + """, + 'author': 'Luis Rodrigo Mejia Mateus', + 'category': 'Productivity', + 'images': ['images/main_1.png', 'images/main_screenshot.png'], + 'depends': ['web'], + 'data': [ + 'views/ir_actions_report.xml', + ], + 'installable': True, + 'auto_install': False, + 'license': 'LGPL-3', + 'assets': { + 'web.assets_backend': [ + 'report_pdf_options/static/src/js/PdfOptionsModal.js', + 'report_pdf_options/static/src/js/qwebactionmanager.js', + 'report_pdf_options/static/src/**/*.xml' + ] + } +} diff --git a/report_pdf_options/i18n/es_PE.po b/report_pdf_options/i18n/es_PE.po new file mode 100644 index 0000000..3c73ffe --- /dev/null +++ b/report_pdf_options/i18n/es_PE.po @@ -0,0 +1,87 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * report_pdf_options +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2021-09-18 04:38+0000\n" +"PO-Revision-Date: 2021-09-17 23:44-0500\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: \n" +"Language: es_PE\n" +"X-Generator: Poedit 3.0\n" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/js/qwebactionmanager.js:0 +#, python-format +msgid "Close" +msgstr "Cerrar" + +#. module: report_pdf_options +#: model:ir.model.fields,field_description:report_pdf_options.field_ir_actions_report__default_print_option +msgid "Default printing option" +msgstr "Opción de impresión por defecto" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/xml/report_pdf_options.xml:0 +#: model:ir.model.fields.selection,name:report_pdf_options.selection__ir_actions_report__default_print_option__download +#, python-format +msgid "Download" +msgstr "Descargar" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/xml/report_pdf_options.xml:0 +#: model:ir.model.fields.selection,name:report_pdf_options.selection__ir_actions_report__default_print_option__open +#, python-format +msgid "Open" +msgstr "Abrir" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/xml/report_pdf_options.xml:0 +#: model:ir.model.fields.selection,name:report_pdf_options.selection__ir_actions_report__default_print_option__print +#, python-format +msgid "Print" +msgstr "Imprimir" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/js/qwebactionmanager.js:0 +#, python-format +msgid "Report" +msgstr "Reporte" + +#. module: report_pdf_options +#: model:ir.model,name:report_pdf_options.model_ir_actions_report +msgid "Report Action" +msgstr "Acción de informe" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/js/qwebactionmanager.js:0 +#, python-format +msgid "What do you want to do?" +msgstr "Que acción quiere ejecutar?" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/xml/report_pdf_options.xml:0 +#, python-format +msgid "report" +msgstr "reporte" + +#. module: report_pdf_options +#. openerp-web +#: code:addons/report_pdf_options/static/src/xml/report_pdf_options.xml:0 +#, python-format +msgid "set a default option for this" +msgstr "Establecer una opción por defecto para este" diff --git a/report_pdf_options/images/main_1.png b/report_pdf_options/images/main_1.png new file mode 100644 index 0000000..81a7c63 Binary files /dev/null and b/report_pdf_options/images/main_1.png differ diff --git a/report_pdf_options/images/main_screenshot.png b/report_pdf_options/images/main_screenshot.png new file mode 100644 index 0000000..2d270c2 Binary files /dev/null and b/report_pdf_options/images/main_screenshot.png differ diff --git a/report_pdf_options/models/__init__.py b/report_pdf_options/models/__init__.py new file mode 100644 index 0000000..bdf49f6 --- /dev/null +++ b/report_pdf_options/models/__init__.py @@ -0,0 +1 @@ +from . import ir_actions \ No newline at end of file diff --git a/report_pdf_options/models/ir_actions.py b/report_pdf_options/models/ir_actions.py new file mode 100644 index 0000000..ec4ee83 --- /dev/null +++ b/report_pdf_options/models/ir_actions.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +from odoo import fields, models + + +class IrActionsReportXml(models.Model): + _inherit = 'ir.actions.report' + + default_print_option = fields.Selection(selection=[ + ('print', 'Print'), + ('download', 'Download'), + ('open', 'Open') + ], string='Default printing option') + + def _get_readable_fields(self): + data = super()._get_readable_fields() + data.add('default_print_option') + return data + + def report_action(self, docids, data=None, config=True): + data = super(IrActionsReportXml, self).report_action(docids, data, config) + data['id'] = self.id + data['default_print_option'] = self.default_print_option + return data diff --git a/report_pdf_options/static/description/defaultoption.gif b/report_pdf_options/static/description/defaultoption.gif new file mode 100644 index 0000000..5a81248 Binary files /dev/null and b/report_pdf_options/static/description/defaultoption.gif differ diff --git a/report_pdf_options/static/description/index.html b/report_pdf_options/static/description/index.html new file mode 100644 index 0000000..8d2bfe5 --- /dev/null +++ b/report_pdf_options/static/description/index.html @@ -0,0 +1,41 @@ + + + + Report Print Options + Report direct print, download or open in new tab + + + + + + + + + + + + + + + Report Print Configuration + Set an option to perform by default + + + + + + + + + + + + The default option is executed + + + + + + + + \ No newline at end of file diff --git a/report_pdf_options/static/description/printoptions.gif b/report_pdf_options/static/description/printoptions.gif new file mode 100644 index 0000000..bb1ed38 Binary files /dev/null and b/report_pdf_options/static/description/printoptions.gif differ diff --git a/report_pdf_options/static/description/printoptions2.gif b/report_pdf_options/static/description/printoptions2.gif new file mode 100644 index 0000000..5267dd4 Binary files /dev/null and b/report_pdf_options/static/description/printoptions2.gif differ diff --git a/report_pdf_options/static/description/setupoptions.gif b/report_pdf_options/static/description/setupoptions.gif new file mode 100644 index 0000000..a6a616b Binary files /dev/null and b/report_pdf_options/static/description/setupoptions.gif differ diff --git a/report_pdf_options/static/src/js/PdfOptionsModal.js b/report_pdf_options/static/src/js/PdfOptionsModal.js new file mode 100644 index 0000000..902f401 --- /dev/null +++ b/report_pdf_options/static/src/js/PdfOptionsModal.js @@ -0,0 +1,17 @@ +/** @odoo-module */ + +import { _t } from "@web/core/l10n/translation"; +import { Dialog } from "@web/core/dialog/dialog"; +import { Component } from "@odoo/owl"; + +export class PdfOptionsModal extends Component { + setup() { + this.title = _t("What do you want to do?"); + } + executePdfAction(option) { + this.props.onSelectOption(option); + } +} + +PdfOptionsModal.template = "report_pdf_options.ButtonOptions"; +PdfOptionsModal.components = { Dialog }; \ No newline at end of file diff --git a/report_pdf_options/static/src/js/qwebactionmanager.js b/report_pdf_options/static/src/js/qwebactionmanager.js new file mode 100644 index 0000000..dd6b4be --- /dev/null +++ b/report_pdf_options/static/src/js/qwebactionmanager.js @@ -0,0 +1,133 @@ +/** @odoo-module **/ + +import { _t } from "@web/core/l10n/translation"; +import { registry } from "@web/core/registry"; +import { PdfOptionsModal } from "./PdfOptionsModal"; + +function getWKHTMLTOPDF_MESSAGES(status) { + const link = 'wkhtmltopdf.org'; // FIXME missing markup + const _status = { + broken: + _t( + "Your installation of Wkhtmltopdf seems to be broken. The report will be shown in html." + ) + link, + install: + _t("Unable to find Wkhtmltopdf on this system. The report will be shown in html.") + + link, + upgrade: + _t( + "You should upgrade your version of Wkhtmltopdf to at least 0.12.0 in order to get a correct display of headers and footers as well as support for table-breaking between pages." + ) + link, + workers: _t( + "You need to start Odoo with at least two workers to print a pdf version of the reports." + ), + }; + return _status[status]; +} + +let iframeForReport; + +function printPdf(url, callback) { + let iframe = iframeForReport; + if (!iframe) { + iframe = iframeForReport = document.createElement('iframe'); + iframe.className = 'pdfIframe' + document.body.appendChild(iframe); + iframe.style.display = 'none'; + iframe.onload = function () { + setTimeout(function () { + iframe.focus(); + iframe.contentWindow.print(); + URL.revokeObjectURL(url) + callback(); + }, 1); + }; + } + iframe.src = url; +} + +function getReportUrl(action, type) { + let url = `/report/${type}/${action.report_name}`; + const actionContext = action.context || {}; + if (action.data && JSON.stringify(action.data) !== "{}") { + // build a query string with `action.data` (it's the place where reports + // using a wizard to customize the output traditionally put their options) + const options = encodeURIComponent(JSON.stringify(action.data)); + const context = encodeURIComponent(JSON.stringify(actionContext)); + url += `?options=${options}&context=${context}`; + } else { + if (actionContext.active_ids) { + url += `/${actionContext.active_ids.join(",")}`; + } + if (type === "html") { + const context = encodeURIComponent(JSON.stringify(env.services.user.context)); + url += `?context=${context}`; + } + } + return url; +} + +let wkhtmltopdfStateProm; + +registry + .category("ir.actions.report handlers") + .add("pdf_report_options_handler", async function (action, options, env) { + let { default_print_option, report_type } = action; + if (report_type !== "qweb-pdf" || default_print_option === "download") + return false; + if (!default_print_option) { + let removeDialog; + default_print_option = await new Promise(resolve => { + removeDialog = env.services.dialog.add( + PdfOptionsModal, + { + onSelectOption: (option) => { + return resolve(option); + } + }, + { + onClose: () => { + resolve("close"); + } + } + ); + }); + removeDialog(); + if (default_print_option === "close") + return true; + if (default_print_option === "download") + return false; + } + + // check the state of wkhtmltopdf before proceeding + if (!wkhtmltopdfStateProm) { + wkhtmltopdfStateProm = await env.services.rpc("/report/check_wkhtmltopdf"); + } + const state = wkhtmltopdfStateProm; + // display a notification according to wkhtmltopdf's state + const message = getWKHTMLTOPDF_MESSAGES(state) + if (message) { + env.services.notification.add(message, { + sticky: true, + title: _t("Report"), + }); + } + if (["upgrade", "ok"].includes(state)) { + // trigger the download of the PDF report + //return _triggerDownload(action, options, "pdf"); + const url = getReportUrl(action, "pdf"); + if (default_print_option === "print") { + env.services.ui.block(); + printPdf(url, () => { + env.services.ui.unblock(); + }); + } + if (default_print_option === "open") { + window.open(url); + } + return true; + } else { + // open the report in the client action if generating the PDF is not possible + return _executeReportClientAction(action, options); + } + }) diff --git a/report_pdf_options/static/src/xml/report_pdf_options.xml b/report_pdf_options/static/src/xml/report_pdf_options.xml new file mode 100644 index 0000000..aeeab57 --- /dev/null +++ b/report_pdf_options/static/src/xml/report_pdf_options.xml @@ -0,0 +1,23 @@ + + + + + + + + + Print + + + + Download + + + + Open + + + + + + diff --git a/report_pdf_options/views/ir_actions_report.xml b/report_pdf_options/views/ir_actions_report.xml new file mode 100644 index 0000000..d803c2c --- /dev/null +++ b/report_pdf_options/views/ir_actions_report.xml @@ -0,0 +1,17 @@ + + + + + ir.actions.report.inherit + ir.actions.report + + + + + + + + + + + \ No newline at end of file