res_currency.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, models, fields, _
  4. from odoo.exceptions import UserError
  5. class ResCurrency(models.Model):
  6. _inherit = 'res.currency'
  7. display_rounding_warning = fields.Boolean(string="Display Rounding Warning", compute='_compute_display_rounding_warning',
  8. help="The warning informs a rounding factor change might be dangerous on res.currency's form view.")
  9. @api.depends('rounding')
  10. def _compute_display_rounding_warning(self):
  11. for record in self:
  12. record.display_rounding_warning = record.id \
  13. and record._origin.rounding != record.rounding \
  14. and record._origin._has_accounting_entries()
  15. def write(self, vals):
  16. if 'rounding' in vals:
  17. rounding_val = vals['rounding']
  18. for record in self:
  19. if (rounding_val > record.rounding or rounding_val == 0) and record._has_accounting_entries():
  20. raise UserError(_("You cannot reduce the number of decimal places of a currency which has already been used to make accounting entries."))
  21. return super(ResCurrency, self).write(vals)
  22. def _has_accounting_entries(self):
  23. """ Returns True iff this currency has been used to generate (hence, round)
  24. some move lines (either as their foreign currency, or as the main currency
  25. of their company).
  26. """
  27. self.ensure_one()
  28. return bool(self.env['account.move.line'].search_count(['|', ('currency_id', '=', self.id), ('company_currency_id', '=', self.id)]))
  29. @api.model
  30. def _get_query_currency_table(self, options):
  31. ''' Construct the currency table as a mapping company -> rate to convert the amount to the user's company
  32. currency in a multi-company/multi-currency environment.
  33. The currency_table is a small postgresql table construct with VALUES.
  34. :param options: The report options.
  35. :return: The query representing the currency table.
  36. '''
  37. user_company = self.env.company
  38. user_currency = user_company.currency_id
  39. if options.get('multi_company', False):
  40. companies = self.env.companies
  41. conversion_date = options['date']['date_to']
  42. currency_rates = companies.mapped('currency_id')._get_rates(user_company, conversion_date)
  43. else:
  44. companies = user_company
  45. currency_rates = {user_currency.id: 1.0}
  46. conversion_rates = []
  47. for company in companies:
  48. conversion_rates.extend((
  49. company.id,
  50. currency_rates[user_company.currency_id.id] / currency_rates[company.currency_id.id],
  51. user_currency.decimal_places,
  52. ))
  53. query = '(VALUES %s) AS currency_table(company_id, rate, precision)' % ','.join('(%s, %s, %s)' for i in companies)
  54. return self.env.cr.mogrify(query, conversion_rates).decode(self.env.cr.connection.encoding)