You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # Houssine Bakkali <houssine@coopiteasy.be>
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. from datetime import datetime
  6. from odoo import api, fields, models
  7. class ResPartner(models.Model):
  8. _inherit = "res.partner"
  9. age = fields.Integer(
  10. string="Age", compute="_compute_age", search="_search_age"
  11. )
  12. def _search_age(self, operator, value):
  13. if operator not in ("=", "!=", "<", "<=", ">", ">=", "in", "not in"):
  14. return []
  15. # pylint: disable=sql-injection
  16. # fixme while you're here, please fix the query to pass
  17. # pylint sql-injection
  18. query = """SELECT id
  19. FROM "%s"
  20. WHERE extract(year from age(CURRENT_DATE,
  21. birthdate_date)) %s %%s""" % (
  22. self._table,
  23. operator,
  24. )
  25. self.env.cr.execute(query, (value,))
  26. ids = [t[0] for t in self.env.cr.fetchall()]
  27. return [("id", "in", ids)]
  28. @api.multi
  29. @api.depends("birthdate_date")
  30. def _compute_age(self):
  31. for partner in self:
  32. if partner.birthdate_date:
  33. birthday = partner.birthdate_date
  34. today = datetime.now().date()
  35. partner.age = (
  36. today.year
  37. - birthday.year
  38. - (
  39. (today.month, today.day)
  40. < (birthday.month, birthday.day)
  41. )
  42. )