From fc032efc2deff3e638a68d8cff8e1344863a6fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Tue, 30 Jan 2024 10:19:04 +0100 Subject: [PATCH] [PERF] spreadsheet: avoid company currency rpc Pivot/list monetary fields needs the company currency to display the value in the said currency format. Until now, a RPC was made to fetch the currency. However, since odoo/o-spreadsheet@8710839 and odoo/enterprise@8c0a785 the currency format is already in the model config. There's no need for the RPC. This saves one network request and one full spreadsheet evaluation (which would have occured after the request is done) Note: see next commit for dashboards. Part-of: odoo/odoo#151725 --- .../static/src/currency/plugins/currency.js | 4 ++ .../tests/currency/currency_plugin_test.js | 58 +++++++++++++++++++ ...test.js => currency_rate_function_test.js} | 2 +- .../spreadsheet/static/tests/utils/model.js | 4 ++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 addons/spreadsheet/static/tests/currency/currency_plugin_test.js rename addons/spreadsheet/static/tests/currency/{currency_test.js => currency_rate_function_test.js} (98%) diff --git a/addons/spreadsheet/static/src/currency/plugins/currency.js b/addons/spreadsheet/static/src/currency/plugins/currency.js index c3bb3ca6194..4fdc8958c50 100644 --- a/addons/spreadsheet/static/src/currency/plugins/currency.js +++ b/addons/spreadsheet/static/src/currency/plugins/currency.js @@ -14,6 +14,7 @@ const DATA_SOURCE_ID = "CURRENCIES"; class CurrencyPlugin extends UIPlugin { constructor(config) { super(config); + this.currentCompanyCurrencyFormat = config.defaultCurrencyFormat; this.dataSources = config.custom.dataSources; if (this.dataSources) { this.dataSources.add(DATA_SOURCE_ID, CurrencyDataSource); @@ -74,6 +75,9 @@ class CurrencyPlugin extends UIPlugin { * @returns {string | undefined} */ getCompanyCurrencyFormat(companyId) { + if (!companyId && this.currentCompanyCurrencyFormat) { + return this.currentCompanyCurrencyFormat; + } const currency = this.dataSources && this.dataSources.get(DATA_SOURCE_ID).getCompanyCurrencyFormat(companyId); diff --git a/addons/spreadsheet/static/tests/currency/currency_plugin_test.js b/addons/spreadsheet/static/tests/currency/currency_plugin_test.js new file mode 100644 index 00000000000..eb54c368959 --- /dev/null +++ b/addons/spreadsheet/static/tests/currency/currency_plugin_test.js @@ -0,0 +1,58 @@ +/** @odoo-module */ + +import { nextTick } from "@web/../tests/helpers/utils"; +import { createModelWithDataSource } from "../utils/model"; + +QUnit.module("spreadsheet currency plugin"); + +QUnit.test("get default currency format when it's in the config", async (assert) => { + const model = await createModelWithDataSource({ + modelConfig: { + defaultCurrencyFormat: "#,##0.00[$θ]", + }, + mockRPC: async function (route, args) { + throw new Error("Should not make any RPC"); + }, + }); + assert.strictEqual(model.getters.getCompanyCurrencyFormat(), "#,##0.00[$θ]"); +}); + +QUnit.test("get default currency format when it's not in the config", async (assert) => { + const model = await createModelWithDataSource({ + mockRPC: async function (route, args) { + if (args.method === "get_company_currency_for_spreadsheet") { + return { + code: "Odoo", + symbol: "θ", + position: "after", + decimalPlaces: 2, + }; + } + }, + }); + assert.throws(() => model.getters.getCompanyCurrencyFormat(), "Data is loading"); + await nextTick(); + assert.strictEqual(model.getters.getCompanyCurrencyFormat(), "#,##0.00[$θ]"); + assert.verifySteps([]); +}); + +QUnit.test("get specific currency format", async (assert) => { + const model = await createModelWithDataSource({ + modelConfig: { + defaultCurrencyFormat: "#,##0.00[$θ]", + }, + mockRPC: async function (route, args) { + if (args.method === "get_company_currency_for_spreadsheet" && args.args[0] === 42) { + return { + code: "Odoo", + symbol: "O", + position: "after", + decimalPlaces: 2, + }; + } + }, + }); + assert.throws(() => model.getters.getCompanyCurrencyFormat(42), "Data is loading"); + await nextTick(); + assert.strictEqual(model.getters.getCompanyCurrencyFormat(42), "#,##0.00[$O]"); +}); diff --git a/addons/spreadsheet/static/tests/currency/currency_test.js b/addons/spreadsheet/static/tests/currency/currency_rate_function_test.js similarity index 98% rename from addons/spreadsheet/static/tests/currency/currency_test.js rename to addons/spreadsheet/static/tests/currency/currency_rate_function_test.js index 0227336615d..269dd8af2bd 100644 --- a/addons/spreadsheet/static/tests/currency/currency_test.js +++ b/addons/spreadsheet/static/tests/currency/currency_rate_function_test.js @@ -7,7 +7,7 @@ import { waitForDataSourcesLoaded, } from "@spreadsheet/../tests/utils/model"; -QUnit.module("spreadsheet > Currency"); +QUnit.module("spreadsheet > ODOO.CURRENCY.RATE function"); QUnit.test("Basic exchange formula", async (assert) => { const model = await createModelWithDataSource({ diff --git a/addons/spreadsheet/static/tests/utils/model.js b/addons/spreadsheet/static/tests/utils/model.js index 38e3919cbad..30ca135fb7a 100644 --- a/addons/spreadsheet/static/tests/utils/model.js +++ b/addons/spreadsheet/static/tests/utils/model.js @@ -28,6 +28,7 @@ export function setupDataSourceEvaluation(model) { * * @param {object} params * @param {object} [params.spreadsheetData] Spreadsheet data to import + * @param {object} [params.modelConfig] * @param {ServerData} [params.serverData] Data to be injected in the mock server * @param {function} [params.mockRPC] Mock rpc function */ @@ -41,10 +42,13 @@ export async function createModelWithDataSource(params = {}) { serverData: params.serverData || getBasicServerData(), mockRPC: params.mockRPC, }); + const config = params.modelConfig; const model = new Model(params.spreadsheetData, { + ...config, custom: { env, dataSources: new DataSources(env), + ...config?.custom, }, }); setupDataSourceEvaluation(model);