-
-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] mis-builder: set analytic account constraint in the filter when…
… displayed This commit extracts, when the filters are displayed, the domain info from the `mis_analytic_domain` context key or from the analyticAccountIdField props and add it to the active filters. The system prevents the user from removing the constraint.
- Loading branch information
Laurent Stukkens
committed
Dec 27, 2023
1 parent
dcc1903
commit 9a8add9
Showing
3 changed files
with
150 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** @odoo-module **/ | ||
|
||
import {SearchModel} from "@web/search/search_model"; | ||
import {useService} from "@web/core/utils/hooks"; | ||
|
||
export class MisReportSearchModel extends SearchModel { | ||
/** | ||
* @override | ||
*/ | ||
setup() { | ||
this.notificationService = useService("notification"); | ||
super.setup(...arguments); | ||
} | ||
|
||
/** | ||
* @override | ||
*/ | ||
deactivateGroup(groupId) { | ||
// Prevent removing the analytic account filter and let the user know. | ||
let reactivateAnalyticAccountFilter = false; | ||
if (this.analyticAccountSearchItem.groupId === groupId) { | ||
if ( | ||
this.query.filter( | ||
(query) => query.searchItemId === this.analyticAccountSearchItem.id | ||
).length === 1 | ||
) { | ||
this.notificationService.add( | ||
this.env._t("The analytic account filter cannot be removed"), | ||
{type: "info"} | ||
); | ||
return; | ||
} | ||
// As there are more than one filter on the analytic account, let | ||
// super remove them and add it back after. | ||
reactivateAnalyticAccountFilter = true; | ||
} | ||
super.deactivateGroup(groupId); | ||
if (reactivateAnalyticAccountFilter) { | ||
this._addAnalyticAccountFilter(); | ||
} | ||
} | ||
|
||
/** | ||
* @override | ||
*/ | ||
async load(config) { | ||
// Store analytic account id in the SearchModel for reuse in other functions. | ||
this.analyticAccountId = config.analyticAccountId; | ||
const analyticAccountNamePromise = this._loadAnalyticAccountName(); | ||
await Promise.all([analyticAccountNamePromise, super.load(...arguments)]); | ||
this._determineAnalyticAccountSearchItem(); | ||
this._addAnalyticAccountFilter(); | ||
} | ||
|
||
/** | ||
* Add the filter regarding the analytic account in the search model. | ||
* @private | ||
*/ | ||
_addAnalyticAccountFilter() { | ||
if (!this.analyticAccountSearchItem || !this.analyticAccountName) { | ||
return; | ||
} | ||
this.addAutoCompletionValues(this.analyticAccountSearchItem.id, { | ||
label: this.analyticAccountName, | ||
operator: "=", | ||
value: this.analyticAccountId, | ||
}); | ||
} | ||
|
||
/** | ||
* Find the searchItem that correspond to the analytic account field and store it | ||
* under analyticAccountSearchItem. | ||
* @private | ||
*/ | ||
_determineAnalyticAccountSearchItem() { | ||
// Store analytic account searchItem in the SearchModel for reuse in other functions. | ||
for (const searchItem of Object.values(this.searchItems)) { | ||
if ( | ||
searchItem.type === "field" && | ||
searchItem.fieldName === "analytic_account_id" | ||
) { | ||
this.analyticAccountSearchItem = searchItem; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Load the analytic account name and store it under analyticAccountName. | ||
* @returns {Promise<void>} | ||
* @private | ||
*/ | ||
async _loadAnalyticAccountName() { | ||
if (!this.analyticAccountId) { | ||
return; | ||
} | ||
const readResult = await this.orm.read( | ||
"account.analytic.account", | ||
[this.analyticAccountId], | ||
["display_name"] | ||
); | ||
const analyticAccount = readResult[0]; | ||
this.analyticAccountName = analyticAccount.display_name; | ||
} | ||
} |