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.

110 lines
3.7 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. def _cooperator_share_type(self):
  12. """
  13. Return the share.type that correspond to the cooperator_type.
  14. """
  15. self.ensure_one()
  16. share_type = None
  17. if self.cooperator_type:
  18. share_type = (
  19. self.env['product.template']
  20. .search([('default_code', '=', self.cooperator_type)])
  21. )[0]
  22. return share_type
  23. @api.depends(
  24. "cooperator_type"
  25. )
  26. def _compute_is_worker(self):
  27. """
  28. Return True if the partner can participate tho the shift system.
  29. This is defined on the share type.
  30. """
  31. for rec in self:
  32. share_type = rec._cooperator_share_type()
  33. if share_type:
  34. rec.is_worker = share_type.allow_working
  35. else:
  36. rec.is_worker = False
  37. @api.depends(
  38. "cooperative_status_ids",
  39. "cooperative_status_ids.status",
  40. "cooperative_status_ids.can_shop",
  41. "share_ids",
  42. "share_ids.share_product_id",
  43. "share_ids.share_product_id.default_code",
  44. "share_ids.share_number",
  45. )
  46. def _compute_can_shop(self):
  47. """
  48. Overwrite default behavior to take the owned share into account.
  49. """
  50. for rec in self:
  51. share_type = rec._cooperator_share_type()
  52. if share_type:
  53. rec.can_shop = (
  54. rec.cooperative_status_ids.can_shop
  55. if rec.is_worker and rec.cooperative_status_ids
  56. else share_type.allow_shopping
  57. )
  58. else:
  59. rec.can_shop = (
  60. rec.cooperative_status_ids.can_shop
  61. if rec.is_worker and rec.cooperative_status_ids else False
  62. )
  63. @api.constrains('parent_eater_id')
  64. def _check_max_parent_eaters(self):
  65. """
  66. Check that the parent_eater_id in parnter in self doesn't exceed
  67. the maximum eater limit.
  68. See also: _check_max_child_eaters()
  69. """
  70. for rec in self:
  71. if rec.parent_eater_id:
  72. share_type = rec.parent_eater_id._cooperator_share_type()
  73. if (
  74. share_type
  75. and share_type.max_nb_eater_allowed >= 0
  76. and len(
  77. rec.parent_eater_id.child_eater_ids
  78. ) > share_type.max_nb_eater_allowed
  79. ):
  80. raise ValidationError(
  81. _('You can only set %d additional eaters per worker')
  82. % share_type.max_nb_eater_allowed
  83. )
  84. @api.constrains('child_eater_ids')
  85. def _check_max_child_eaters(self):
  86. """
  87. Check the maximum number of eaters that can be assigned to a
  88. share owner.
  89. See also: _check_max_parent_eaters()
  90. """
  91. for rec in self:
  92. share_type = rec._cooperator_share_type()
  93. if (
  94. share_type
  95. and share_type.max_nb_eater_allowed >= 0
  96. and len(rec.child_eater_ids) > share_type.max_nb_eater_allowed
  97. ):
  98. raise ValidationError(
  99. _('You can only set %d additional eaters per worker')
  100. % share_type.max_nb_eater_allowed
  101. )