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

  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. def _cooperator_share_type(self):
  18. """
  19. Return the share.type that correspond to the cooperator_type.
  20. """
  21. self.ensure_one()
  22. share_type = None
  23. if self.cooperator_type:
  24. share_type = (
  25. self.env['product.template']
  26. .search([('default_code', '=', self.cooperator_type)])
  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 _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 else False
  75. )
  76. @api.constrains('parent_eater_id')
  77. def _check_max_parent_eaters(self):
  78. """
  79. Check that the parent_eater_id in parnter in self doesn't exceed
  80. the maximum eater limit.
  81. See also: _check_max_child_eaters()
  82. """
  83. for rec in self:
  84. if rec.parent_eater_id:
  85. share_type = rec.parent_eater_id._cooperator_share_type()
  86. if (
  87. share_type
  88. and share_type.max_nb_eater_allowed >= 0
  89. and len(
  90. 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. )