Files
gzth/docx_report_pro/static/src/js/report.js
T
2025-04-14 09:28:56 +08:00

56 lines
1.9 KiB
JavaScript

/** @odoo-module **/
import { registry } from "@web/core/registry";
import { download } from "@web/core/network/download";
registry
.category("ir.actions.report handlers")
.add("custom_docx", async (action, options, env) => {
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;
}
async function _triggerDownload(action, options, type) {
const url = _getReportUrl(action, type);
env.services.ui.block();
try {
await download({
url: "/report/download",
data: {
data: JSON.stringify([url, action.report_type]),
context: JSON.stringify(env.services.user.context),
},
});
} finally {
env.services.ui.unblock();
}
const onClose = options.onClose;
if (action.close_on_report_download) {
return doAction({ type: "ir.actions.act_window_close" }, { onClose });
} else if (onClose) {
onClose();
}
}
if (action.report_type === "docx") {
return _triggerDownload(action, options, "docx");
}
});