|
| 1 | +import { |
| 2 | + IOT_REPORT_PREFERENCE_LOCAL_STORAGE_KEY, |
| 3 | + setReportIdInBrowserLocalStorage, |
| 4 | +} from "@iot/client_action/delete_local_storage"; |
| 5 | +import { browser } from "@web/core/browser/browser"; |
| 6 | +import { registry } from "@web/core/registry"; |
| 7 | +import { user } from "@web/core/user"; |
| 8 | + |
| 9 | +import { getSelectedPrintersForReport, printReport } from "@iot/iot_report_action"; |
| 10 | + |
| 11 | + |
| 12 | +export async function getSelectedPrintersForReportUx(reportId, env) { |
| 13 | + const { orm, action, ui } = env.services; |
| 14 | + const deviceSettingsByReportId = JSON.parse(browser.localStorage.getItem(IOT_REPORT_PREFERENCE_LOCAL_STORAGE_KEY)); |
| 15 | + const deviceSettings = deviceSettingsByReportId?.[reportId]; |
| 16 | + if (!deviceSettings ) { |
| 17 | + const rule= await orm.call( |
| 18 | + "ir.actions.report.iot.rule", |
| 19 | + "search_read", |
| 20 | + [[['report_id', '=', reportId], ['user_id', '=', user.userId], ['device_id', '!=', false]], |
| 21 | + ["skip_dialog", "device_id"]]); |
| 22 | + if (rule.length > 0) { |
| 23 | + const newDeviceSettings = { |
| 24 | + selectedDevices: [rule[0].device_id[0]], |
| 25 | + skipDialog: rule[0].skip_dialog, |
| 26 | + }; |
| 27 | + setReportIdInBrowserLocalStorage(reportId, newDeviceSettings); |
| 28 | + } |
| 29 | + } |
| 30 | + return await getSelectedPrintersForReport(reportId, env); |
| 31 | +} |
| 32 | + |
| 33 | +async function iotReportActionHandler(action, options, env) { |
| 34 | + if (action.device_ids && action.device_ids.length) { |
| 35 | + action.data ??= {}; |
| 36 | + const args = [action.id, action.context.active_ids, action.data]; |
| 37 | + const reportId = action.id; |
| 38 | + const printerIds = await getSelectedPrintersForReportUx(reportId, env); |
| 39 | + |
| 40 | + if (!printerIds) { |
| 41 | + // If the user does not select any printer, fall back to normal printing |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + env.services.ui.block(); |
| 46 | + // Try longpolling then websocket |
| 47 | + await printReport(env, args, printerIds); |
| 48 | + env.services.ui.unblock(); |
| 49 | + |
| 50 | + options.onClose?.(); |
| 51 | + return true; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +registry |
| 56 | + .category("ir.actions.report handlers") |
| 57 | + .add("iot_report_action_handler", iotReportActionHandler, {force: true}); |
0 commit comments