Odoo modules related to events management
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.

87 lines
3.5 KiB

  1. from odoo import models, fields, api
  2. class ProductTemplate(models.Model):
  3. _inherit = "product.template"
  4. is_event_deposit = fields.Boolean(
  5. string="Event deposit",
  6. compute="_compute_is_event_deposit",
  7. inverse="_set_is_event_deposit",
  8. search="_search_is_event_deposit",
  9. )
  10. balance_variant_id = fields.Many2one(
  11. comodel_name="product.product",
  12. string="Balance variant",
  13. compute="_compute_balance_variant_id",
  14. inverse="_set_balance_variant_id",
  15. search="_search_balance_variant_id",
  16. )
  17. @api.depends("product_variant_ids.is_event_deposit")
  18. def _compute_is_event_deposit(self):
  19. self.is_event_deposit = False
  20. for template in self:
  21. variant_count = len(template.product_variant_ids)
  22. if variant_count == 1:
  23. template.is_event_deposit = (
  24. template.product_variant_ids.is_event_deposit
  25. )
  26. elif variant_count == 0:
  27. archived_variants = template.with_context(
  28. active_test=False
  29. ).product_variant_ids
  30. if len(archived_variants) == 1:
  31. template.is_event_deposit = archived_variants.is_event_deposit
  32. def _search_is_event_deposit(self, operator, value):
  33. query = self.with_context(active_test=False)._search(
  34. [("product_variant_ids.is_event_deposit", operator, value)]
  35. )
  36. return [("id", "in", query)]
  37. def _set_is_event_deposit(self):
  38. variant_count = len(self.product_variant_ids)
  39. if variant_count == 1:
  40. self.product_variant_ids.is_event_deposit = self.is_event_deposit
  41. elif variant_count == 0:
  42. archived_variants = self.with_context(active_test=False).product_variant_ids
  43. if len(archived_variants) == 1:
  44. archived_variants.is_event_deposit = self.is_event_deposit
  45. @api.depends("product_variant_ids.balance_variant_id")
  46. def _compute_balance_variant_id(self):
  47. self.balance_variant_id = False
  48. for template in self:
  49. variant_count = len(template.product_variant_ids)
  50. if variant_count == 1:
  51. template.balance_variant_id = (
  52. template.product_variant_ids.balance_variant_id
  53. )
  54. elif variant_count == 0:
  55. archived_variants = template.with_context(
  56. active_test=False
  57. ).product_variant_ids
  58. if len(archived_variants) == 1:
  59. template.balance_variant_id = archived_variants.balance_variant_id
  60. def _search_balance_variant_id(self, operator, value):
  61. query = self.with_context(active_test=False)._search(
  62. [("product_variant_ids.balance_variant_id", operator, value)]
  63. )
  64. return [("id", "in", query)]
  65. def _set_balance_variant_id(self):
  66. variant_count = len(self.product_variant_ids)
  67. if variant_count == 1:
  68. self.product_variant_ids.balance_variant_id = self.balance_variant_id
  69. elif variant_count == 0:
  70. archived_variants = self.with_context(active_test=False).product_variant_ids
  71. if len(archived_variants) == 1:
  72. archived_variants.balance_variant_id = self.balance_variant_id
  73. def _get_related_fields_variant_template(self):
  74. return super()._get_related_fields_variant_template() + [
  75. "is_event_deposit",
  76. "balance_variant_id",
  77. ]