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.

131 lines
4.3 KiB

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