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.

147 lines
5.0 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
4 years ago
4 years ago
4 years ago
4 years ago
  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class Partner(models.Model):
  6. _inherit = "res.partner"
  7. eater = fields.Selection(
  8. [("eater", "Eater"), ("worker_eater", "Worker and Eater")],
  9. string="Eater/Worker",
  10. )
  11. child_eater_ids = fields.One2many(
  12. "res.partner",
  13. "parent_eater_id",
  14. domain=[("customer", "=", True), ("eater", "=", "eater")],
  15. )
  16. parent_eater_id = fields.Many2one(
  17. "res.partner", string="Parent Worker", readonly=True
  18. )
  19. barcode = fields.Char(
  20. compute="_compute_bar_code", string="Barcode", store=True
  21. )
  22. parent_barcode = fields.Char(
  23. compute="_compute_bar_code", string="Parent Barcode", store=True
  24. )
  25. member_card_ids = fields.One2many("member.card", "partner_id")
  26. country_id = fields.Many2one(
  27. required=True, default=lambda self: self.env.ref("base.be")
  28. )
  29. member_card_to_be_printed = fields.Boolean("Print BEES card?")
  30. last_printed = fields.Datetime("Last printed on")
  31. cooperator_type = fields.Selection(
  32. [
  33. ("share_a", "Share A"),
  34. ("share_b", "Share B"),
  35. ("share_c", "Share C"),
  36. ],
  37. store=True,
  38. compute=None,
  39. )
  40. @api.multi
  41. @api.depends(
  42. "parent_eater_id",
  43. "parent_eater_id.barcode",
  44. "eater",
  45. "member_card_ids",
  46. )
  47. def _compute_bar_code(self):
  48. for partner in self:
  49. if partner.eater == "eater":
  50. partner.parent_barcode = partner.parent_eater_id.barcode
  51. elif partner.member_card_ids:
  52. for c in partner.member_card_ids:
  53. if c.valid:
  54. partner.barcode = c.barcode
  55. @api.multi
  56. @api.constrains("child_eater_ids", "parent_eater_id")
  57. def _check_number_of_eaters(self):
  58. """The owner of an A share can have a maximum of two eaters but
  59. the owner of a B share can have a maximum of three eaters.
  60. """
  61. for partner in self:
  62. # Get the default_code of the share for the current eater and his parent
  63. share_type_code = partner.cooperator_type
  64. parent_share_type_code = partner.parent_eater_id.cooperator_type
  65. # Raise exception
  66. if (
  67. share_type_code == "share_b"
  68. or parent_share_type_code == "share_b"
  69. ):
  70. if (
  71. len(partner.child_eater_ids) > 3
  72. or len(partner.parent_eater_id.child_eater_ids) > 3
  73. ):
  74. raise ValidationError(
  75. _(
  76. "You can only set three additional eaters per worker"
  77. )
  78. )
  79. else:
  80. if (
  81. len(partner.child_eater_ids) > 2
  82. or len(partner.parent_eater_id.child_eater_ids) > 2
  83. ):
  84. raise ValidationError(
  85. _("You can only set two additional eaters per worker")
  86. )
  87. @api.multi
  88. def write(self, values):
  89. for partner in self:
  90. if (
  91. values.get("parent_eater_id")
  92. and partner.parent_eater_id
  93. and partner.parent_eater_id.id != values.get("parent_eater_id")
  94. ):
  95. raise ValidationError(
  96. _(
  97. "You try to assign a eater to a worker but this eater "
  98. "is already assign to %s please remove it before "
  99. )
  100. % partner.parent_eater_id.name
  101. )
  102. # replace many2many command when writing on child_eater_ids to just
  103. # remove the link
  104. if "child_eater_ids" in values:
  105. for command in values["child_eater_ids"]:
  106. if command[0] == 2:
  107. command[0] = 3
  108. return super(Partner, self).write(values)
  109. @api.multi
  110. def _deactivate_active_cards(self):
  111. self.ensure_one()
  112. for card in self.member_card_ids.filtered("valid"):
  113. card.valid = False
  114. card.end_date = fields.Date.today()
  115. @api.multi
  116. def _new_card(self, reason, user_id, barcode=False):
  117. card_data = {
  118. "partner_id": self.id,
  119. "responsible_id": user_id,
  120. "comment": reason,
  121. }
  122. if barcode:
  123. card_data["barcode"] = barcode
  124. self.env["member.card"].create(card_data)
  125. @api.multi
  126. def _new_eater(self, surname, name, email):
  127. partner_data = {
  128. "lastname": name,
  129. "firstname": surname,
  130. "is_customer": True,
  131. "eater": "eater",
  132. "parent_eater_id": self.id,
  133. "email": email,
  134. "country_id": self.country_id.id,
  135. }
  136. return self.env["res.partner"].create(partner_data)