A veces necesitamos mostrar el tipo de cambio al USD de cada compañía siempre.
Lo que vamos a lograr usando OWL especificamente con systray menu es mantener el tipo de cambio del día en la moneda de la compañía en la que nos encontremos al dolar (USD)
res_currency.py
# -*- coding: utf-8 -*-from odoo import modelsfrom datetime import dateclass ResCurrency(models.Model):_inherit = "res.currency"def exchange_rate_usd(self):currency_usd = self.env.ref("base.USD")company = self.env.companyrate = company.currency_id._convert(from_amount=1.0,to_currency=currency_usd,company=company,date=date.today(),)return {"rate": rate,"symbol": currency_usd.symbol,"currency_id": currency_usd.id,}
currency_rate.js
/** @odoo-module **/import { Component, useState, onWillStart } from "@odoo/owl";import { registry } from "@web/core/registry";import { useService } from "@web/core/utils/hooks";import { Dropdown } from "@web/core/dropdown/dropdown";import { DropdownItem } from "@web/core/dropdown/dropdown_item";import { session } from "@web/session";class CurrencyRate extends Component {setup() {super.setup(...arguments);this.state = useState({rate_usd: { rate: 0.0, symbol: "", currency_id: 0 },});this.action = useService("action");this.orm = useService("orm");onWillStart(async () => {const rate_usd = await this.orm.call("res.currency","exchange_rate_usd",[session.company_id]);this.state.rate_usd = rate_usd;});}get rate_usd() {const { rate, symbol } = this.state.rate_usd;return `${symbol} ${rate}`;}_openCurrencyUSD() {const { currency_id } = this.state.rate_usd;if (currency_id) {this.action.doAction({type: "ir.actions.act_window",name: "Monedas - USD",res_model: "res.currency",view_mode: "form",views: [[false, "form"]],res_id: currency_id,target: "new",});}}}CurrencyRate.template = "CurrencyRate";CurrencyRate.components = { Dropdown, DropdownItem };export const currencyRate = { Component: CurrencyRate };registry.category("systray").add("CurrencyRate", currencyRate, { sequence: 1 });
currency_rate.xml
<?xml version="1.0" encoding="utf-8"?>
<templates xml:space="preserve">
<t t-name="CurrencyRate">
<Dropdown>
<button>
<i class="fa fa-lg fa-exchange" role="img" aria-label="Currency USD"></i>
<span class="badge rounded-pill">
<t t-esc="rate_usd"/>
</span>
</button>
<t t-set-slot="content">
<DropdownItem >
<div t-on-click="_openCurrencyUSD">
Moneda - USD
</div>
</DropdownItem>
</t>
</Dropdown>
</t>
</templates>
Resultado: