diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 9f965769008..c10a4486cfc 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -4322,6 +4322,15 @@ class AccountMove(models.Model): # from_cron=True to log errors in chatter instead of raise composer.action_send_and_print(from_cron=from_cron, allow_fallback_pdf=allow_fallback_pdf) + def generate_pdf_and_send_invoice(self): + """ Calls `_generate_pdf_and_send_invoice` and returns the (pdf, filename)""" + template = self.env.ref(self._get_mail_template()) + self.with_context(skip_invoice_sync=True)._generate_pdf_and_send_invoice(template) + if len(self) < 2: + return self.invoice_pdf_report_id.raw, self.invoice_pdf_report_id.name + else: + return self.env['ir.actions.report']._render('account.account_invoices', self.ids)[0], "Invoices.pdf" + def _get_invoice_pdf_report_filename(self): """ Get the filename of the generated PDF invoice report. """ self.ensure_one() diff --git a/addons/point_of_sale/static/src/app/utils/report_service.js b/addons/point_of_sale/static/src/app/utils/report_service.js index 74a1d5ffd30..e1672ebaa30 100644 --- a/addons/point_of_sale/static/src/app/utils/report_service.js +++ b/addons/point_of_sale/static/src/app/utils/report_service.js @@ -1,27 +1,45 @@ /** @odoo-module */ import { registry } from "@web/core/registry"; +import { downloadFile } from "@web/core/network/download"; import { downloadReport } from "@web/webclient/actions/reports/utils"; export const reportService = { - dependencies: ["rpc", "user", "ui"], - start(env, { rpc, user, ui }) { + dependencies: ["rpc", "user", "ui", "orm"], + start(env, { rpc, user, ui, orm }) { const reportActionsCache = {}; return { async download(reportXmlId, active_ids) { ui.block(); try { - reportActionsCache[reportXmlId] ||= rpc("/web/action/load", { + if (reportXmlId === "account.account_invoices"){ + // Send and Print the invoices + let [pdf, filename] = await orm.call( + "account.move", + "generate_pdf_and_send_invoice", + [active_ids], + ); + // Convert pdf to a Blob + const byteArray = new Uint8Array(pdf.length); + for (let i = 0; i < pdf.length; i++) { + byteArray[i] = pdf.charCodeAt(i); + } + const blob = new Blob([byteArray], {type: "application/pdf"}); + // Show the popup to save the pdf + await downloadFile(blob, filename, "application/pdf"); + } else { + reportActionsCache[reportXmlId] ||= rpc("/web/action/load", { action_id: reportXmlId, - }); - const reportAction = await reportActionsCache[reportXmlId]; - // await instead of return because we want the ui to stay blocked - await downloadReport( - rpc, - { ...reportAction, context: { active_ids } }, - "pdf", - user.context - ); + }); + const reportAction = await reportActionsCache[reportXmlId]; + // await instead of return because we want the ui to stay blocked + await downloadReport( + rpc, + { ...reportAction, context: { active_ids } }, + "pdf", + user.context + ); + } } finally { ui.unblock(); }