mirror of
https://github.com/bringout/oca-ocb-report.git
synced 2026-04-22 03:21:59 +02:00
Initial commit: Report packages
This commit is contained in:
commit
bc5e1e9efa
604 changed files with 474102 additions and 0 deletions
|
|
@ -0,0 +1,69 @@
|
|||
/** @odoo-module */
|
||||
|
||||
import { _t } from "@web/core/l10n/translation";
|
||||
import { ServerData } from "../data_sources/server_data";
|
||||
import { toServerDateString } from "../helpers/helpers";
|
||||
|
||||
/**
|
||||
* @typedef Currency
|
||||
* @property {string} name
|
||||
* @property {string} code
|
||||
* @property {string} symbol
|
||||
* @property {number} decimalPlaces
|
||||
* @property {"before" | "after"} position
|
||||
*/
|
||||
export class CurrencyDataSource {
|
||||
constructor(services) {
|
||||
this.serverData = new ServerData(services.orm, {
|
||||
whenDataIsFetched: () => services.notify(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currency rate between the two given currencies
|
||||
* @param {string} from Currency from
|
||||
* @param {string} to Currency to
|
||||
* @param {string|undefined} date
|
||||
* @returns {number|undefined}
|
||||
*/
|
||||
getCurrencyRate(from, to, date) {
|
||||
const data = this.serverData.batch.get("res.currency.rate", "get_rates_for_spreadsheet", {
|
||||
from,
|
||||
to,
|
||||
date: date ? toServerDateString(date) : undefined,
|
||||
});
|
||||
const rate = data !== undefined ? data.rate : undefined;
|
||||
if (rate === false) {
|
||||
throw new Error(_t("Currency rate unavailable."));
|
||||
}
|
||||
return rate;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number|undefined} companyId
|
||||
* @returns {Currency}
|
||||
*/
|
||||
getCompanyCurrencyFormat(companyId) {
|
||||
const result = this.serverData.get("res.currency", "get_company_currency_for_spreadsheet", [
|
||||
companyId,
|
||||
]);
|
||||
if (result === false) {
|
||||
throw new Error(_t("Currency not available for this company."));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all currencies from the server
|
||||
* @param {string} currencyName
|
||||
* @returns {Currency}
|
||||
*/
|
||||
getCurrency(currencyName) {
|
||||
return this.serverData.batch.get(
|
||||
"res.currency",
|
||||
"get_currencies_for_spreadsheet",
|
||||
currencyName
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/** @odoo-module **/
|
||||
|
||||
import { _t } from "@web/core/l10n/translation";
|
||||
import spreadsheet from "../o_spreadsheet/o_spreadsheet_extended";
|
||||
const { args, toString, toJsDate } = spreadsheet.helpers;
|
||||
const { functionRegistry } = spreadsheet.registries;
|
||||
|
||||
functionRegistry.add("ODOO.CURRENCY.RATE", {
|
||||
description: _t(
|
||||
"This function takes in two currency codes as arguments, and returns the exchange rate from the first currency to the second as float."
|
||||
),
|
||||
compute: function (currencyFrom, currencyTo, date) {
|
||||
const from = toString(currencyFrom);
|
||||
const to = toString(currencyTo);
|
||||
const _date = date ? toJsDate(date) : undefined;
|
||||
return this.getters.getCurrencyRate(from, to, _date);
|
||||
},
|
||||
args: args(`
|
||||
currency_from (string) ${_t("First currency code.")}
|
||||
currency_to (string) ${_t("Second currency code.")}
|
||||
date (date, optional) ${_t("Date of the rate.")}
|
||||
`),
|
||||
returns: ["NUMBER"],
|
||||
});
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
/** @odoo-module */
|
||||
|
||||
import spreadsheet from "../../o_spreadsheet/o_spreadsheet_extended";
|
||||
import { CurrencyDataSource } from "../currency_data_source";
|
||||
const { uiPluginRegistry } = spreadsheet.registries;
|
||||
|
||||
const DATA_SOURCE_ID = "CURRENCIES";
|
||||
|
||||
/**
|
||||
* @typedef {import("../currency_data_source").Currency} Currency
|
||||
*/
|
||||
|
||||
class CurrencyPlugin extends spreadsheet.UIPlugin {
|
||||
constructor(getters, history, dispatch, config) {
|
||||
super(getters, history, dispatch, config);
|
||||
this.dataSources = config.dataSources;
|
||||
if (this.dataSources) {
|
||||
this.dataSources.add(DATA_SOURCE_ID, CurrencyDataSource);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Getters
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get the currency rate between the two given currencies
|
||||
* @param {string} from Currency from
|
||||
* @param {string} to Currency to
|
||||
* @param {string} date
|
||||
* @returns {number|string}
|
||||
*/
|
||||
getCurrencyRate(from, to, date) {
|
||||
return (
|
||||
this.dataSources && this.dataSources.get(DATA_SOURCE_ID).getCurrencyRate(from, to, date)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Currency | undefined} currency
|
||||
* @private
|
||||
*
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
computeFormatFromCurrency(currency) {
|
||||
if (!currency) {
|
||||
return undefined;
|
||||
}
|
||||
const decimalFormatPart = currency.decimalPlaces
|
||||
? "." + "0".repeat(currency.decimalPlaces)
|
||||
: "";
|
||||
const numberFormat = "#,##0" + decimalFormatPart;
|
||||
const symbolFormatPart = "[$" + currency.symbol + "]";
|
||||
return currency.position === "after"
|
||||
? numberFormat + symbolFormatPart
|
||||
: symbolFormatPart + numberFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default display format of a given currency
|
||||
* @param {string} currencyName
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
getCurrencyFormat(currencyName) {
|
||||
const currency =
|
||||
currencyName &&
|
||||
this.dataSources &&
|
||||
this.dataSources.get(DATA_SOURCE_ID).getCurrency(currencyName);
|
||||
return this.computeFormatFromCurrency(currency);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default display format of a the company currency
|
||||
* @param {number|undefined} companyId
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
getCompanyCurrencyFormat(companyId) {
|
||||
const currency =
|
||||
this.dataSources &&
|
||||
this.dataSources.get(DATA_SOURCE_ID).getCompanyCurrencyFormat(companyId);
|
||||
return this.computeFormatFromCurrency(currency);
|
||||
}
|
||||
}
|
||||
|
||||
CurrencyPlugin.modes = ["normal", "headless"];
|
||||
CurrencyPlugin.getters = ["getCurrencyRate", "getCurrencyFormat", "getCompanyCurrencyFormat"];
|
||||
|
||||
uiPluginRegistry.add("odooCurrency", CurrencyPlugin);
|
||||
Loading…
Add table
Add a link
Reference in a new issue