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.

79 lines
2.5 KiB

  1. # Copyright 2019-2020 Coop IT Easy SCRLfs
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import models, fields, api, _
  4. from odoo.exceptions import ValidationError
  5. class Partner(models.Model):
  6. _inherit = 'res.partner'
  7. info_session_confirmed = fields.Boolean(
  8. string="Confirmed presence to info session",
  9. default=False,
  10. )
  11. is_worker = fields.Boolean(
  12. compute="_is_worker",
  13. search="_search_worker",
  14. readonly=True,
  15. related=""
  16. )
  17. @api.depends(
  18. 'share_ids',
  19. 'share_ids.share_product_id',
  20. 'share_ids.share_product_id.default_code',
  21. 'share_ids.share_number',
  22. )
  23. def _is_worker(self):
  24. """
  25. Return True if the partner can participate tho the shift system.
  26. This is defined on the share type.
  27. """
  28. for rec in self:
  29. share_type = None
  30. if rec.cooperator_type:
  31. share_type = (
  32. self.env['product.template']
  33. .search([('default_code', '=', rec.cooperator_type)])
  34. )[0]
  35. if share_type:
  36. rec.is_worker = share_type.allow_working
  37. rec.worker_store = share_type.allow_working
  38. else:
  39. rec.is_worker = False
  40. rec.worker_store = False
  41. def _search_worker(self, operator, value):
  42. return [('worker_store', operator, value)]
  43. @api.constrains('child_eater_ids', 'parent_eater_id')
  44. def _check_number_of_eaters(self):
  45. """
  46. Check the maximum number of eaters that can be assigned to a
  47. share owner.
  48. """
  49. self.ensure_one()
  50. share_type = None
  51. if self.cooperator_type:
  52. share_type = (
  53. self.env['product.template']
  54. .search([('default_code', '=', self.cooperator_type)])
  55. )[0]
  56. # If the current partner owns no share, check his parent.
  57. if not share_type:
  58. share_type = (
  59. self.env['product.template']
  60. .search([
  61. ('default_code', '=', self.parent_eater_id.cooperator_type)
  62. ])
  63. )[0]
  64. if (
  65. share_type
  66. and share_type.max_nb_eater_allowed >= 0
  67. and len(self.child_eater_ids) > share_type.max_nb_eater_allowed
  68. ):
  69. raise ValidationError(
  70. _('You can only set %d additional eaters per worker')
  71. % share_type.max_nb_eater_allowed
  72. )