File: res_currency_rate.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (36 lines) | stat: -rw-r--r-- 1,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from odoo import api, fields, models


class ResCurrencyRate(models.Model):
    _inherit = "res.currency.rate"

    @api.model
    def _get_rate_for_spreadsheet(self, currency_from_code, currency_to_code, date=None, company_id=None):
        if not currency_from_code or not currency_to_code:
            return False
        Currency = self.env["res.currency"].with_context({"active_test": False})
        currency_from = Currency.search([("name", "=", currency_from_code)])
        currency_to = Currency.search([("name", "=", currency_to_code)])
        if not currency_from or not currency_to:
            return False
        company = self.env["res.company"].browse(company_id) if company_id else self.env.company
        date = fields.Date.from_string(date) if date else fields.Date.context_today(self)
        return Currency._get_conversion_rate(currency_from, currency_to, company, date)

    @api.model
    def get_rates_for_spreadsheet(self, requests):
        result = []
        for request in requests:
            record = request.copy()
            record.update(
                {
                    "rate": self._get_rate_for_spreadsheet(
                        request["from"],
                        request["to"],
                        request.get("date"),
                        request.get("company_id"),
                    ),
                }
            )
            result.append(record)
        return result