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.

42 lines
1.3 KiB

  1. # Copyright 2019-2020: Druidoo (<https://www.druidoo.io>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class ResPartner(models.Model):
  5. _inherit = "res.partner"
  6. age_range_id = fields.Many2one(
  7. "res.partner.age.range",
  8. "Age Range",
  9. compute="_compute_age_range_id",
  10. store=True,
  11. )
  12. @api.depends("age")
  13. def _compute_age_range_id(self):
  14. age_ranges = self.env["res.partner.age.range"].search([])
  15. for record in self:
  16. if not record.age:
  17. age_range = False
  18. else:
  19. age_range = (
  20. age_ranges.filtered(
  21. lambda age_range: age_range.age_from
  22. <= record.age
  23. <= age_range.age_to
  24. )
  25. or False
  26. )
  27. if record.age_range_id != age_range:
  28. record.age_range_id = age_range and age_range.id or age_range
  29. @api.model
  30. def _cron_update_age_range_id(self):
  31. """
  32. This method is called from a cron job.
  33. It is used to update age range on contact
  34. """
  35. partners = self.search([("birthdate_date", "!=", False)])
  36. partners._compute_age_range_id()