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.

124 lines
4.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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_number",
  34. )
  35. def _compute_is_worker(self):
  36. """
  37. Return True if the partner can participate tho the shift system.
  38. This is defined on the share type.
  39. """
  40. for rec in self:
  41. share_type = rec._cooperator_share_type()
  42. if share_type:
  43. rec.is_worker = share_type.allow_working
  44. rec.worker_store = share_type.allow_working
  45. else:
  46. rec.is_worker = False
  47. rec.worker_store = False
  48. def _search_worker(self, operator, value):
  49. return [("worker_store", operator, value)]
  50. @api.depends(
  51. "cooperative_status_ids",
  52. "cooperative_status_ids.status",
  53. "cooperative_status_ids.can_shop",
  54. "share_ids",
  55. "share_ids.share_product_id",
  56. "share_ids.share_product_id.default_code",
  57. "share_ids.share_number",
  58. )
  59. def _compute_can_shop(self):
  60. """
  61. Overwrite default behavior to take the owned share into account.
  62. """
  63. for rec in self:
  64. share_type = rec._cooperator_share_type()
  65. if share_type:
  66. rec.can_shop = (
  67. rec.cooperative_status_ids.can_shop
  68. if rec.is_worker and rec.cooperative_status_ids
  69. else share_type.allow_shopping
  70. )
  71. else:
  72. rec.can_shop = (
  73. rec.cooperative_status_ids.can_shop
  74. if rec.is_worker and rec.cooperative_status_ids
  75. else False
  76. )
  77. @api.constrains("parent_eater_id")
  78. def _check_max_parent_eaters(self):
  79. """
  80. Check that the parent_eater_id in parnter in self doesn't exceed
  81. the maximum eater limit.
  82. See also: _check_max_child_eaters()
  83. """
  84. for rec in self:
  85. if rec.parent_eater_id:
  86. share_type = rec.parent_eater_id._cooperator_share_type()
  87. if (
  88. share_type
  89. and share_type.max_nb_eater_allowed >= 0
  90. and len(rec.parent_eater_id.child_eater_ids)
  91. > share_type.max_nb_eater_allowed
  92. ):
  93. raise ValidationError(
  94. _("You can only set %d additional eaters per worker")
  95. % share_type.max_nb_eater_allowed
  96. )
  97. @api.constrains("child_eater_ids")
  98. def _check_max_child_eaters(self):
  99. """
  100. Check the maximum number of eaters that can be assigned to a
  101. share owner.
  102. See also: _check_max_parent_eaters()
  103. """
  104. for rec in self:
  105. share_type = rec._cooperator_share_type()
  106. if (
  107. share_type
  108. and share_type.max_nb_eater_allowed >= 0
  109. and len(rec.child_eater_ids) > share_type.max_nb_eater_allowed
  110. ):
  111. raise ValidationError(
  112. _("You can only set %d additional eaters per worker")
  113. % share_type.max_nb_eater_allowed
  114. )