From 4ebad302a40082e4eeeb73df873761c933402672 Mon Sep 17 00:00:00 2001 From: "Tiffany Chang (tic)" Date: Fri, 10 Feb 2023 17:28:14 +0100 Subject: [PATCH] [IMP] mrp,stock: allow auto-printing + chained (report) actions This commit adds two linked features: 1- the ability to chain (report) actions 2- the ability to auto-print delivery report at picking validation Because feature 2 results in the possibility of 2 actions to be returned at picking validation (due to pre-existing auto-open reception report at validation option), feature 1 is required to support it. It is expected that chaining will primarily occur with report actions since chaining of the other action types does not currently have an applicable use case. Since this feature is only needed within stock for now, it is encapsulated within a custom client_action for now that is a lot of copy paste of action_service.js This is the first commit to support a series of potential auto-printable reports. Part of task: 3046178 - Operation Type ENT PR: odoo/enterprise#43362 Part-of: odoo/odoo#126791 --- addons/stock/models/stock_picking.py | 29 ++++- .../static/src/client_actions/multi_print.js | 38 ++++++ .../stock/views/stock_picking_type_views.xml | 108 +++++++++++------- 3 files changed, 130 insertions(+), 45 deletions(-) create mode 100644 addons/stock/static/src/client_actions/multi_print.js diff --git a/addons/stock/models/stock_picking.py b/addons/stock/models/stock_picking.py index 751cdd1acc8..7f9780edc28 100644 --- a/addons/stock/models/stock_picking.py +++ b/addons/stock/models/stock_picking.py @@ -10,6 +10,7 @@ from markupsafe import escape from odoo import SUPERUSER_ID, _, api, fields, models from odoo.addons.stock.models.stock_move import PROCUREMENT_PRIORITIES +from odoo.addons.web.controllers.utils import clean_action from odoo.exceptions import UserError, ValidationError from odoo.osv import expression from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, format_datetime, format_date, groupby @@ -84,6 +85,9 @@ class PickingType(models.Model): auto_show_reception_report = fields.Boolean( "Show Reception Report at Validation", help="If this checkbox is ticked, Odoo will automatically show the reception report (if there are moves to allocate to) when validating.") + auto_print_delivery_slip = fields.Boolean( + "Auto Print Delivery Slip", + help="If this checkbox is ticked, Odoo will automatically print the delivery slip of a picking when it is validated.") count_picking_draft = fields.Integer(compute='_compute_picking_count') count_picking_ready = fields.Integer(compute='_compute_picking_count') @@ -1150,7 +1154,8 @@ class Picking(models.Model): pickings_to_backorder = self - pickings_not_to_backorder pickings_not_to_backorder.with_context(cancel_backorder=True)._action_done() pickings_to_backorder.with_context(cancel_backorder=False)._action_done() - + report_actions = self._get_autoprint_report_actions() + another_action = False if self.user_has_groups('stock.group_reception_report'): pickings_show_report = self.filtered(lambda p: p.picking_type_id.auto_show_reception_report) lines = pickings_show_report.move_ids.filtered(lambda m: m.product_id.type == 'product' and m.state != 'cancel' and m.quantity_done and not m.move_dest_ids) @@ -1166,7 +1171,18 @@ class Picking(models.Model): ('product_id', 'in', lines.product_id.ids)], limit=1): action = pickings_show_report.action_view_reception_report() action['context'] = {'default_picking_ids': pickings_show_report.ids} - return action + if not report_actions: + return action + another_action = action + if report_actions: + return { + 'type': 'ir.actions.client', + 'tag': 'do_multi_print', + 'params': { + 'reports': report_actions, + 'anotherAction': another_action, + } + } return True def action_set_quantities_to_reservation(self): @@ -1690,3 +1706,12 @@ class Picking(models.Model): def _get_report_lang(self): return self.move_ids and self.move_ids[0].partner_id.lang or self.partner_id.lang or self.env.lang + + def _get_autoprint_report_actions(self): + report_actions = [] + pickings_to_print = self.filtered(lambda p: p.picking_type_id.auto_print_delivery_slip) + if pickings_to_print: + action = self.env.ref("stock.action_report_delivery").report_action(pickings_to_print.ids, config=False) + clean_action(action, self.env) + report_actions.append(action) + return report_actions diff --git a/addons/stock/static/src/client_actions/multi_print.js b/addons/stock/static/src/client_actions/multi_print.js new file mode 100644 index 00000000000..586f625f275 --- /dev/null +++ b/addons/stock/static/src/client_actions/multi_print.js @@ -0,0 +1,38 @@ +/** @odoo-module **/ + +import { _t } from "@web/core/l10n/translation"; +import { registry } from "@web/core/registry"; +import { sprintf } from "@web/core/utils/strings"; + +async function doMultiPrint(env, action) { + for (const report of action.params.reports) { + if (report.type != "ir.actions.report") { + env.services.notification.add(_t("Incorrect type of action submitted as a report, skipping action"), { + title: _t("Report Printing Error"), + }); + continue + } else if (report.report_type === "qweb-html") { + env.services.notification.add( + sprintf( + _t("HTML reports cannot be auto-printed, skipping report: %s"), + report.name) + , { + title: _t("Report Printing Error"), + }); + continue + } + // WARNING: potential issue if pdf generation fails, then action_service defaults + // to HTML and rest of the action chain will break w/potentially never resolving promise + await env.services.action.doAction({ type: "ir.actions.report", ...report }); + } + if (action.params.anotherAction) { + return env.services.action.doAction(action.params.anotherAction); + } else if (action.params.on_close) { + // handle special cases such as barcode + action.params.on_close() + } else { + return env.services.action.doAction("reload_context"); + } +} + +registry.category("actions").add("do_multi_print", doMultiPrint); diff --git a/addons/stock/views/stock_picking_type_views.xml b/addons/stock/views/stock_picking_type_views.xml index 00c14c789c2..6e0e530e9cc 100644 --- a/addons/stock/views/stock_picking_type_views.xml +++ b/addons/stock/views/stock_picking_type_views.xml @@ -78,49 +78,71 @@