res_currency_rate.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. from odoo import api, fields, models
  2. class ResCurrencyRate(models.Model):
  3. _inherit = "res.currency.rate"
  4. @api.model
  5. def _get_rate_for_spreadsheet(self, currency_from_code, currency_to_code, date=None):
  6. if not currency_from_code or not currency_to_code:
  7. return False
  8. Currency = self.env["res.currency"].with_context({"active_test": False})
  9. currency_from = Currency.search([("name", "=", currency_from_code)])
  10. currency_to = Currency.search([("name", "=", currency_to_code)])
  11. if not currency_from or not currency_to:
  12. return False
  13. company = self.env.company
  14. date = fields.Date.from_string(date) if date else fields.Date.context_today(self)
  15. return Currency._get_conversion_rate(currency_from, currency_to, company, date)
  16. @api.model
  17. def get_rates_for_spreadsheet(self, requests):
  18. result = []
  19. for request in requests:
  20. record = request.copy()
  21. record.update({
  22. "rate": self._get_rate_for_spreadsheet(request["from"], request["to"], request.get("date")),
  23. })
  24. result.append(record)
  25. return result