|
| 1 | +// © 2017 Creu Blanca |
| 2 | +// Copyright 2024 Quartile (https://www.quartile.co) |
| 3 | +// License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 4 | +odoo.define("batch_report_pdf_zip.report", function (require) { |
| 5 | + "use strict"; |
| 6 | + |
| 7 | + var core = require("web.core"); |
| 8 | + var ActionManager = require("web.ActionManager"); |
| 9 | + var crash_manager = require("web.crash_manager"); |
| 10 | + var framework = require("web.framework"); |
| 11 | + var session = require("web.session"); |
| 12 | + var _t = core._t; |
| 13 | + |
| 14 | + ActionManager.include({ |
| 15 | + |
| 16 | + _downloadReportZIP: function (url, actions) { |
| 17 | + framework.blockUI(); |
| 18 | + var def = $.Deferred(); |
| 19 | + var type = "zip"; |
| 20 | + var cloned_action = _.clone(actions); |
| 21 | + |
| 22 | + if (_.isUndefined(cloned_action.data) || |
| 23 | + _.isNull(cloned_action.data) || |
| 24 | + (_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data))) |
| 25 | + { |
| 26 | + if (cloned_action.context.active_ids) { |
| 27 | + url += "/" + cloned_action.context.active_ids.join(','); |
| 28 | + } |
| 29 | + } else { |
| 30 | + url += "?options=" + encodeURIComponent(JSON.stringify(cloned_action.data)); |
| 31 | + url += "&context=" + encodeURIComponent(JSON.stringify(cloned_action.context)); |
| 32 | + } |
| 33 | + |
| 34 | + var blocked = !session.get_file({ |
| 35 | + url: url, |
| 36 | + data: { |
| 37 | + data: JSON.stringify([url, type]), |
| 38 | + }, |
| 39 | + success: def.resolve.bind(def), |
| 40 | + error: function () { |
| 41 | + crash_manager.rpc_error.apply(crash_manager, arguments); |
| 42 | + def.reject(); |
| 43 | + }, |
| 44 | + complete: framework.unblockUI, |
| 45 | + }); |
| 46 | + if (blocked) { |
| 47 | + var message = _t('A popup window with your report was blocked. You ' + |
| 48 | + 'may need to change your browser settings to allow ' + |
| 49 | + 'popup windows for this page.'); |
| 50 | + this.do_warn(_t('Warning'), message, true); |
| 51 | + } |
| 52 | + return def; |
| 53 | + }, |
| 54 | + |
| 55 | + _triggerDownload: function (action, options, type) { |
| 56 | + var self = this; |
| 57 | + var reportUrls = this._makeReportUrls(action); |
| 58 | + if (type === "zip") { |
| 59 | + return this._downloadReportZIP(reportUrls[type], action).then(function () { |
| 60 | + if (action.close_on_report_download) { |
| 61 | + var closeAction = {type: 'ir.actions.act_window_close'}; |
| 62 | + return self.doAction(closeAction, _.pick(options, 'on_close')); |
| 63 | + } else { |
| 64 | + return options.on_close(); |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + return this._super.apply(this, arguments); |
| 69 | + }, |
| 70 | + |
| 71 | + _makeReportUrls: function (action) { |
| 72 | + var reportUrls = this._super.apply(this, arguments); |
| 73 | + reportUrls.zip = '/report/zip/' + action.report_name; |
| 74 | + return reportUrls; |
| 75 | + }, |
| 76 | + |
| 77 | + _executeReportAction: function (action, options) { |
| 78 | + var self = this; |
| 79 | + if (action.context.active_ids && action.context.active_ids.length > 1) { |
| 80 | + if (action.report_type === 'qweb-pdf' && action.zip_download === true) { |
| 81 | + return self._triggerDownload(action, options, 'zip'); |
| 82 | + } |
| 83 | + } |
| 84 | + return this._super.apply(this, arguments); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | +}); |
0 commit comments