diff --git a/report_async/models/report_async.py b/report_async/models/report_async.py index d678fd000..e79847f49 100644 --- a/report_async/models/report_async.py +++ b/report_async/models/report_async.py @@ -144,7 +144,8 @@ class ReportAsync(models.Model): @api.model def print_document_async( - self, record_ids, report_name, html=None, data=None, to_email="" + self, record_ids, report_name, html=None, data=None, to_email="", + save_attachment_to_records=False ): """Generate a document async, do not return the document file""" user_email = to_email or self.env.user.email @@ -157,6 +158,7 @@ class ReportAsync(models.Model): email_notify=True, to_email=user_email, session_id=request.session.sid, + save_attachment_to_records=save_attachment_to_records ) @api.model @@ -169,6 +171,7 @@ class ReportAsync(models.Model): email_notify=False, to_email=None, session_id=None, + save_attachment_to_records=False ): report = self.env["ir.actions.report"].browse(report_id) func = REPORT_TYPES_FUNC[report.report_type] @@ -199,6 +202,22 @@ class ReportAsync(models.Model): } ) ) + # save attachment to records + if save_attachment_to_records: + model = report.model + records = self.env[model].browse(docids) + for record in records: + attachment = self.env["ir.attachment"].sudo().create( + { + "name": out_name, + "datas": out_file, + "type": "binary", + "res_model": model, + "res_id": record.id, + } + ) + if hasattr(record, 'message_post'): + record.message_post(attachment_ids=[attachment.id]) self._cr.execute( """ UPDATE ir_attachment SET create_uid = %s, write_uid = %s diff --git a/report_async/static/src/js/components/action_menus.js b/report_async/static/src/js/components/action_menus.js index 1f790ca77..e95e0dfb4 100644 --- a/report_async/static/src/js/components/action_menus.js +++ b/report_async/static/src/js/components/action_menus.js @@ -46,6 +46,9 @@ odoo.define("report_async.ActionMenus", function (require) { const is_report_async = this.$( "#async_report_checker" ).prop("checked"); + const save_report_attachment = this.$( + "#async-save-report-checker" + ).prop("checked"); const user_email = this.$("#async-user-email").val(); if (user_email !== "" && is_report_async) { // Try basic email validation @@ -64,6 +67,7 @@ odoo.define("report_async.ActionMenus", function (require) { to_email: user_email, data: action.data || {}, context: action.context || {}, + save_attachment_to_records: save_report_attachment }, }) .then(() => { diff --git a/report_async/static/src/xml/report_async.xml b/report_async/static/src/xml/report_async.xml index 291e562cd..dc6823301 100644 --- a/report_async/static/src/xml/report_async.xml +++ b/report_async/static/src/xml/report_async.xml @@ -20,6 +20,26 @@ via queue job and sent to a below email address. + +
+
+ + +
+ + Checker enables async report attachment to be created and + saved to the records. NB: Records should support attachments + +