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.

116 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. 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. "share_ids",
  53. "share_ids.share_product_id",
  54. "share_ids.share_product_id.default_code",
  55. "share_ids.share_number",
  56. )
  57. def _compute_can_shop(self):
  58. """
  59. Overwrite default behavior to take the owned share into account.
  60. """
  61. for rec in self:
  62. share_type = rec._cooperator_share_type()
  63. if share_type:
  64. rec.can_shop = (
  65. rec.cooperative_status_ids.can_shop
  66. if rec.cooperative_status_ids
  67. else share_type.allow_shopping
  68. )
  69. else:
  70. rec.can_shop = (
  71. rec.cooperative_status_ids.can_shop
  72. if rec.cooperative_status_ids else False
  73. )
  74. @api.constrains('child_eater_ids', 'parent_eater_id')
  75. def _check_number_of_eaters(self):
  76. """
  77. Check the maximum number of eaters that can be assigned to a
  78. share owner.
  79. """
  80. for rec in self:
  81. share_type = None
  82. if rec.cooperator_type:
  83. share_type = (
  84. self.env['product.template']
  85. .search([('default_code', '=', rec.cooperator_type)])
  86. )[0]
  87. # If the current partner owns no share, check his parent.
  88. if not share_type:
  89. share_type = (
  90. self.env['product.template']
  91. .search([
  92. (
  93. 'default_code',
  94. '=',
  95. rec.parent_eater_id.cooperator_type
  96. )
  97. ])
  98. )[0]
  99. if (
  100. share_type
  101. and share_type.max_nb_eater_allowed >= 0
  102. and len(rec.child_eater_ids) > share_type.max_nb_eater_allowed
  103. ):
  104. raise ValidationError(
  105. _('You can only set %d additional eaters per worker')
  106. % share_type.max_nb_eater_allowed
  107. )