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.

95 lines
3.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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 is already assign to %s please remove it before"
  51. )
  52. % rec.parent_eater_id.name
  53. )
  54. # replace many2many command when writing on child_eater_ids to just remove the link
  55. if "child_eater_ids" in values:
  56. for command in values["child_eater_ids"]:
  57. if command[0] == 2:
  58. command[0] = 3
  59. return super(Partner, self).write(values)
  60. def _deactivate_active_cards(self):
  61. self.ensure_one()
  62. for card in self.member_card_ids.filtered("valid"):
  63. card.valid = False
  64. card.end_date = fields.Date.today()
  65. @api.multi
  66. def _new_card(self, reason, user_id, barcode=False):
  67. card_data = {
  68. "partner_id": self.id,
  69. "responsible_id": user_id,
  70. "comment": reason,
  71. }
  72. if barcode:
  73. card_data["barcode"] = barcode
  74. self.env["member.card"].create(card_data)
  75. @api.multi
  76. def _new_eater(self, surname, name, email):
  77. partner_data = {
  78. "lastname": name,
  79. "firstname": surname,
  80. "is_customer": True,
  81. "eater": "eater",
  82. "parent_eater_id": self.id,
  83. "email": email,
  84. "country_id": self.country_id.id,
  85. }
  86. return self.env["res.partner"].create(partner_data)