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.

97 lines
3.1 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. from odoo import _, api, fields, models
  2. from odoo.exceptions import ValidationError
  3. class Partner(models.Model):
  4. _inherit = "res.partner"
  5. eater = fields.Selection(
  6. [("eater", "Eater"), ("worker_eater", "Worker and Eater")],
  7. string="Eater/Worker",
  8. )
  9. child_eater_ids = fields.One2many(
  10. "res.partner",
  11. "parent_eater_id",
  12. domain=[("customer", "=", True), ("eater", "=", "eater")],
  13. )
  14. parent_eater_id = fields.Many2one(
  15. "res.partner", string="Parent Worker", readonly=True
  16. )
  17. barcode = fields.Char(
  18. compute="_compute_bar_code", string="Barcode", store=True
  19. )
  20. parent_barcode = fields.Char(
  21. compute="_compute_bar_code", string="Parent Barcode", store=True
  22. )
  23. member_card_ids = fields.One2many("member.card", "partner_id")
  24. member_card_to_be_printed = fields.Boolean("Print BEES card?")
  25. last_printed = fields.Datetime("Last printed on")
  26. @api.depends(
  27. "parent_eater_id",
  28. "parent_eater_id.barcode",
  29. "eater",
  30. "member_card_ids",
  31. )
  32. def _compute_bar_code(self):
  33. for rec in self:
  34. if rec.eater == "eater":
  35. rec.parent_barcode = rec.parent_eater_id.barcode
  36. elif rec.member_card_ids:
  37. for c in rec.member_card_ids:
  38. if c.valid:
  39. rec.barcode = c.barcode
  40. @api.multi
  41. def write(self, values):
  42. for rec in self:
  43. if (
  44. values.get("parent_eater_id")
  45. and rec.parent_eater_id
  46. and rec.parent_eater_id.id != values.get("parent_eater_id")
  47. ):
  48. raise ValidationError(
  49. _(
  50. "You try to assign a eater to a worker but this eater "
  51. "is already assign to %s please remove it before "
  52. )
  53. % rec.parent_eater_id.name
  54. )
  55. # replace many2many command when writing on child_eater_ids to just
  56. # remove the link
  57. if "child_eater_ids" in values:
  58. for command in values["child_eater_ids"]:
  59. if command[0] == 2:
  60. command[0] = 3
  61. return super(Partner, self).write(values)
  62. def _deactivate_active_cards(self):
  63. self.ensure_one()
  64. for card in self.member_card_ids.filtered("valid"):
  65. card.valid = False
  66. card.end_date = fields.Date.today()
  67. @api.multi
  68. def _new_card(self, reason, user_id, barcode=False):
  69. card_data = {
  70. "partner_id": self.id,
  71. "responsible_id": user_id,
  72. "comment": reason,
  73. }
  74. if barcode:
  75. card_data["barcode"] = barcode
  76. self.env["member.card"].create(card_data)
  77. @api.multi
  78. def _new_eater(self, surname, name, email):
  79. partner_data = {
  80. "lastname": name,
  81. "firstname": surname,
  82. "is_customer": True,
  83. "eater": "eater",
  84. "parent_eater_id": self.id,
  85. "email": email,
  86. "country_id": self.country_id.id,
  87. }
  88. return self.env["res.partner"].create(partner_data)