From ba083ea55ede9f8b8362a860748a9282f144683c Mon Sep 17 00:00:00 2001 From: Laurent Stukkens Date: Fri, 22 Dec 2023 19:23:53 +0100 Subject: [PATCH] [IMP] mis_builder: allow passing analytic account field name This commit allows passing the name of a field corresponding to the analytic account to be passed into the domain. This is usefull when the component is directly used in a view of a model containing the analytical account. --- .../src/components/mis_report_widget.esm.js | 59 ++++++++++++++++--- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/mis_builder/static/src/components/mis_report_widget.esm.js b/mis_builder/static/src/components/mis_report_widget.esm.js index ea2e0d96f..1d90a201d 100644 --- a/mis_builder/static/src/components/mis_report_widget.esm.js +++ b/mis_builder/static/src/components/mis_report_widget.esm.js @@ -9,6 +9,7 @@ import {SearchBar} from "@web/search/search_bar/search_bar"; import {SearchModel} from "@web/search/search_model"; import {parseDate} from "@web/core/l10n/dates"; import {registry} from "@web/core/registry"; +import {standardFieldProps} from "@web/views/fields/standard_field_props"; export class MisReportWidget extends Component { setup() { @@ -69,6 +70,10 @@ export class MisReportWidget extends Component { this.refresh(); } + get mis_analytic_domain_context_key() { + return "mis_analytic_domain"; + } + get showSearchBar() { return ( this.source_aml_model_name && @@ -106,16 +111,12 @@ export class MisReportWidget extends Component { get context() { let ctx = this.props.record.context; + this.set_mis_analytic_domain_context_key(ctx); if (this.showSearchBar && this.searchModel.searchDomain) { - ctx = { - ...ctx, - mis_analytic_domain: Domain.and([ - new Domain( - this.props.record.context.mis_analytic_domain || Domain.TRUE - ), - new Domain(this.searchModel.searchDomain), - ]).toList(), - }; + ctx[this.mis_analytic_domain_context_key] = Domain.and([ + ctx[this.mis_analytic_domain_context_key], + new Domain(this.searchModel.searchDomain), + ]).toList(); } if (this.showPivotDate && this.state.pivot_date) { ctx = { @@ -146,6 +147,36 @@ export class MisReportWidget extends Component { ); } + set_mis_analytic_domain_context_key(context) { + if ( + this.props.analytic_account_id_field && + !(this.mis_analytic_domain_context_key in context) + ) { + let analytic_account_id = false; + const analytic_account_id_field_type = + this.props.record.fields[this.props.analytic_account_id_field].type; + switch (analytic_account_id_field_type) { + case "many2one": + analytic_account_id = + this.props.record.data[this.props.analytic_account_id_field][0]; + break; + case "integer": + analytic_account_id = + this.props.record.data[this.props.analytic_account_id_field]; + break; + default: + throw new Error( + ```Unsupported field type for analytic_account_id: ${analytic_account_id_field_type}``` + ); + } + if (analytic_account_id) { + context[this.mis_analytic_domain_context_key] = [ + ["analytic_account_id", "=", analytic_account_id], + ]; + } + } + } + async printPdf() { const action = await this.orm.call( "mis.report.instance", @@ -184,5 +215,15 @@ export class MisReportWidget extends Component { MisReportWidget.components = {FilterMenu, SearchBar, DatePicker}; MisReportWidget.template = "mis_builder.MisReportWidget"; +MisReportWidget.props = { + ...standardFieldProps, + analytic_account_id_field: { + type: String, + optional: true, + }, +}; +MisReportWidget.extractProps = ({attrs}) => ({ + analytic_account_id_field: attrs.analytic_account_id_field, +}); registry.category("fields").add("mis_report_widget", MisReportWidget);