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.

130 lines
4.4 KiB

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