Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] mis_builder: take field context into account #591

Open
wants to merge 3 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mis_builder/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
"mis_builder/static/src/components/mis_report_widget.esm.js",
"mis_builder/static/src/components/mis_report_widget.xml",
"mis_builder/static/src/components/mis_report_widget.css",
"mis_builder/static/src/views/form/mis_report_form_controller.esm.js",
"mis_builder/static/src/views/form/mis_report_form_view.esm.js",
],
"web.report_assets_common": [
"/mis_builder/static/src/css/report.css",
],
},
"qweb": ["static/src/xml/mis_report_widget.xml"],
"installable": True,
"application": True,
"license": "AGPL-3",
Expand Down
94 changes: 89 additions & 5 deletions mis_builder/static/src/components/mis_report_widget.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import {Component, onWillStart, useState, useSubEnv} from "@odoo/owl";
import {useBus, useService} from "@web/core/utils/hooks";
import {DatePicker} from "@web/core/datepicker/datepicker";
import {Domain} from "@web/core/domain";
import {FilterMenu} from "@web/search/filter_menu/filter_menu";
import {SearchBar} from "@web/search/search_bar/search_bar";
import {SearchModel} from "@web/search/search_model";
import {evaluateExpr} from "@web/core/py_js/py";
import {parseDate} from "@web/core/l10n/dates";
import {registry} from "@web/core/registry";
import {useSetupAction} from "@web/webclient/actions/action_hook";

const misAnalyticDomainKey = "mis_analytic_domain";

export class MisReportWidget extends Component {
setup() {
Expand All @@ -32,6 +37,18 @@ export class MisReportWidget extends Component {
this.refresh();
});
onWillStart(this.willStart);
useSetupAction({
getGlobalState: () => {
if (!this.showSearchBar) {
return {};
}
return {
misReportSearchModelState: JSON.stringify(
this.searchModel.exportState()
),
};
},
});
}

// Lifecycle
Expand All @@ -58,10 +75,14 @@ export class MisReportWidget extends Component {
this.widget_show_pivot_date = result.widget_show_pivot_date;
if (this.showSearchBar) {
// Initialize the search model
await this.searchModel.load({
const config = {
resModel: this.source_aml_model_name,
searchViewId: this.widget_search_view_id,
});
};
if (this.env.misReportSearchModelState) {
config.state = JSON.parse(this.env.misReportSearchModelState);
}
await this.searchModel.load(config);
}

// Compute the report
Expand Down Expand Up @@ -103,12 +124,75 @@ export class MisReportWidget extends Component {
}
}

/**
* Get the record data in a form that is usable for the domain eval. All the
* `many2one` values are returned as id instead of a list (id, value).
*
* @returns {Object}
* @private
*/
_getRecordDataForDomainResolution() {
const recordData = {};
for (const [fieldName, value] of Object.entries(this.props.record.data)) {
if (this.props.record.fields[fieldName].type !== "one2many") {
if (this.props.record.fields[fieldName].type === "many2one") {
recordData[fieldName] = value[0];
continue;
}
recordData[fieldName] = value;
}
}
return recordData;
}

/**
* The domain built from both the context that is passed through the action
* and the one that is set on the field in the view. The last one being evaluated
* against the context populated with a self property populated with the record data.
*
* @returns {Domain}
*/
get miscAnalyticDomain() {
let domain = Domain.TRUE;
// Get the domain that is set on the action
const recordContext = this.props.record.context;
if (misAnalyticDomainKey in recordContext) {
const recordDomain = new Domain(recordContext[misAnalyticDomainKey]);
domain = Domain.and([domain, recordDomain]);
}
// Get the domain that is set on the field and evaluate it with both the context
// and the record data mounted on a self property.
if ("context" in this.props.record.activeFields[this.props.name]) {
const contextAttr = this.props.record.activeFields[this.props.name].context;
const evaluation_context = {
...recordContext,
self: this._getRecordDataForDomainResolution(),
};
const fieldContext = evaluateExpr(contextAttr, evaluation_context);
if (misAnalyticDomainKey in fieldContext) {
const fieldDomain = new Domain(fieldContext[misAnalyticDomainKey]);
domain = Domain.and([domain, fieldDomain]);
}
}
return domain;
}

get context() {
var ctx = super.context;
if (this.showSearchBar) {
let ctx = this.props.record.context;
const misAnalyticDomain = this.miscAnalyticDomain;
if (misAnalyticDomain !== Domain.TRUE) {
ctx = {
...ctx,
[misAnalyticDomainKey]: misAnalyticDomain.toList(),
};
}
if (this.showSearchBar && this.searchModel.searchDomain) {
ctx = {
...ctx,
mis_analytic_domain: this.searchModel.searchDomain,
[misAnalyticDomainKey]: Domain.and([
new Domain(ctx[misAnalyticDomainKey] || Domain.TRUE),
new Domain(this.searchModel.searchDomain),
]).toList(),
};
}
if (this.showPivotDate && this.state.pivot_date) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @odoo-module */

import {FormController} from "@web/views/form/form_controller";
import {useSubEnv} from "@odoo/owl";

export class MisReportFormController extends FormController {
setup() {
super.setup();
useSubEnv({
misReportSearchModelState:
this.props.globalState &&
this.props.globalState.misReportSearchModelState,
});
}
}
14 changes: 14 additions & 0 deletions mis_builder/static/src/views/form/mis_report_form_view.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @odoo-module **/

import {MisReportFormController} from "@mis_builder/views/form/mis_report_form_controller.esm";
import {formView} from "@web/views/form/form_view";
import {registry} from "@web/core/registry";

// The view to use when mounting `mis_report_widget` widget in order to preserve the
// filters when returning from the drill-down views.
export const misReportFormView = {
...formView,
Controller: MisReportFormController,
};

registry.category("views").add("mis_report_form_view", misReportFormView);
3 changes: 2 additions & 1 deletion mis_builder/views/mis_report_instance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
edit="false"
create="false"
delete="false"
js_class="mis_report_form_view"
>
<sheet>
<field
name="id"
widget="mis_report_widget"
nolabel="1"
style="width:100%"
class="w-100"
/>
</sheet>
</form>
Expand Down
Loading