| 1 |
import { EvaluationError, helpers, registries } from "@odoo/o-spreadsheet"; |
| 2 |
import { OdooUIPlugin } from "@spreadsheet/plugins"; |
| 3 |
import { toServerDateString } from "@spreadsheet/helpers/helpers"; |
| 4 |
import { _t } from "@web/core/l10n/translation"; |
| 5 |
const { featurePluginRegistry } = registries; |
| 6 |
const { createCurrencyFormat } = helpers; |
| 7 |
|
| 8 |
/** |
| 9 |
* @typedef Currency |
| 10 |
* @property {string} name |
| 11 |
* @property {string} code |
| 12 |
* @property {string} symbol |
| 13 |
* @property {number} decimalPlaces |
| 14 |
* @property {"before" | "after"} position |
| 15 |
*/ |
| 16 |
|
| 17 |
export class CurrencyPlugin extends OdooUIPlugin { |
| 18 |
static getters = /** @type {const} */ ([ |
| 19 |
"getCurrencyRate", |
| 20 |
"computeFormatFromCurrency", |
| 21 |
"getCompanyCurrencyFormat", |
| 22 |
]); |
| 23 |
|
| 24 |
constructor(config) { |
| 25 |
super(config); |
| 26 |
/** @type {string | undefined} */ |
| 27 |
this.currentCompanyCurrency = config.defaultCurrency; |
| 28 |
/** @type {import("@spreadsheet/data_sources/server_data").ServerData} */ |
| 29 |
this._serverData = config.custom.odooDataProvider?.serverData; |
| 30 |
} |
| 31 |
|
| 32 |
get serverData() { |
| 33 |
if (!this._serverData) { |
| 34 |
throw new Error( |
| 35 |
"'serverData' is not defined, please make sure a 'OdooDataProvider' instance is provided to the model." |
| 36 |
); |
| 37 |
} |
| 38 |
return this._serverData; |
| 39 |
} |
| 40 |
|
| 41 |
// ------------------------------------------------------------------------- |
| 42 |
// Getters |
| 43 |
// ------------------------------------------------------------------------- |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the currency rate between the two given currencies |
| 47 |
* @param {string} from Currency from |
| 48 |
* @param {string} to Currency to |
| 49 |
* @param {string | undefined} date |
| 50 |
* @param {number | undefined} companyId |
| 51 |
* @returns {number|string} |
| 52 |
*/ |
| 53 |
getCurrencyRate(from, to, date, companyId) { |
| 54 |
const data = this.serverData.batch.get("res.currency.rate", "get_rates_for_spreadsheet", { |
| 55 |
from, |
| 56 |
to, |
| 57 |
date: date ? toServerDateString(date) : undefined, |
| 58 |
company_id: companyId, |
| 59 |
}); |
| 60 |
const rate = data !== undefined ? data.rate : undefined; |
| 61 |
if (rate === false) { |
| 62 |
throw new EvaluationError(_t("Currency rate unavailable.")); |
| 63 |
} |
| 64 |
return rate; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param {Currency | undefined} currency |
| 69 |
* @returns {string | undefined} |
| 70 |
*/ |
| 71 |
computeFormatFromCurrency(currency) { |
| 72 |
if (!currency) { |
| 73 |
return undefined; |
| 74 |
} |
| 75 |
return createCurrencyFormat({ |
| 76 |
symbol: currency.symbol, |
| 77 |
position: currency.position, |
| 78 |
decimalPlaces: currency.decimalPlaces, |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the default display format of a the company currency |
| 84 |
* @param {number} [companyId] |
| 85 |
* @returns {string | undefined} |
| 86 |
*/ |
| 87 |
getCompanyCurrencyFormat(companyId) { |
| 88 |
if (!companyId && this.currentCompanyCurrency) { |
| 89 |
return this.computeFormatFromCurrency(this.currentCompanyCurrency); |
| 90 |
} |
| 91 |
const currency = this.serverData.get( |
| 92 |
"res.currency", |
| 93 |
"get_company_currency_for_spreadsheet", |
| 94 |
[companyId] |
| 95 |
); |
| 96 |
if (currency === false) { |
| 97 |
throw new EvaluationError(_t("Currency not available for this company.")); |
| 98 |
} |
| 99 |
return this.computeFormatFromCurrency(currency); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
featurePluginRegistry.add("odooCurrency", CurrencyPlugin); |