event_booth.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. from odoo.osv import expression
  5. class EventBooth(models.Model):
  6. _name = 'event.booth'
  7. _description = 'Event Booth'
  8. _inherit = [
  9. 'event.type.booth',
  10. 'mail.thread',
  11. 'mail.activity.mixin'
  12. ]
  13. # owner
  14. event_type_id = fields.Many2one(ondelete='set null', required=False)
  15. event_id = fields.Many2one('event.event', string='Event', ondelete='cascade', required=True)
  16. # customer
  17. partner_id = fields.Many2one('res.partner', string='Renter', tracking=True, copy=False)
  18. contact_name = fields.Char('Renter Name', compute='_compute_contact_name', readonly=False, store=True, copy=False)
  19. contact_email = fields.Char('Renter Email', compute='_compute_contact_email', readonly=False, store=True, copy=False)
  20. contact_mobile = fields.Char('Renter Mobile', compute='_compute_contact_mobile', readonly=False, store=True, copy=False)
  21. contact_phone = fields.Char('Renter Phone', compute='_compute_contact_phone', readonly=False, store=True, copy=False)
  22. # state
  23. state = fields.Selection(
  24. [('available', 'Available'), ('unavailable', 'Unavailable')],
  25. string='Status', group_expand='_group_expand_states',
  26. default='available', required=True, tracking=True)
  27. is_available = fields.Boolean(compute='_compute_is_available', search='_search_is_available')
  28. @api.depends('partner_id')
  29. def _compute_contact_name(self):
  30. for booth in self:
  31. if not booth.contact_name:
  32. booth.contact_name = booth.partner_id.name or False
  33. @api.depends('partner_id')
  34. def _compute_contact_email(self):
  35. for booth in self:
  36. if not booth.contact_email:
  37. booth.contact_email = booth.partner_id.email or False
  38. @api.depends('partner_id')
  39. def _compute_contact_mobile(self):
  40. for booth in self:
  41. if not booth.contact_mobile:
  42. booth.contact_mobile = booth.partner_id.mobile or False
  43. @api.depends('partner_id')
  44. def _compute_contact_phone(self):
  45. for booth in self:
  46. if not booth.contact_phone:
  47. booth.contact_phone = booth.partner_id.phone or False
  48. @api.depends('state')
  49. def _compute_is_available(self):
  50. for booth in self:
  51. booth.is_available = booth.state == 'available'
  52. def _search_is_available(self, operator, operand):
  53. negative = operator in expression.NEGATIVE_TERM_OPERATORS
  54. if (negative and operand) or not operand:
  55. return [('state', '=', 'unavailable')]
  56. return [('state', '=', 'available')]
  57. def _group_expand_states(self, states, domain, order):
  58. return [key for key, val in type(self).state.selection]
  59. @api.model_create_multi
  60. def create(self, vals_list):
  61. res = super(EventBooth, self.with_context(mail_create_nosubscribe=True)).create(vals_list)
  62. unavailable_booths = res.filtered(lambda booth: not booth.is_available)
  63. unavailable_booths._post_confirmation_message()
  64. return res
  65. def write(self, vals):
  66. to_confirm = self.filtered(lambda booth: booth.state == 'available')
  67. wpartner = {}
  68. if 'state' in vals or 'partner_id' in vals:
  69. wpartner = dict(
  70. (booth, booth.partner_id.ids)
  71. for booth in self.filtered(lambda booth: booth.partner_id)
  72. )
  73. res = super(EventBooth, self).write(vals)
  74. if vals.get('state') == 'unavailable' or vals.get('partner_id'):
  75. for booth in self:
  76. booth.message_subscribe(booth.partner_id.ids)
  77. for booth in self:
  78. if wpartner.get(booth) and booth.partner_id.id not in wpartner[booth]:
  79. booth.message_unsubscribe(wpartner[booth])
  80. if vals.get('state') == 'unavailable':
  81. to_confirm._action_post_confirm(vals)
  82. return res
  83. def _post_confirmation_message(self):
  84. for booth in self:
  85. booth.event_id.message_post_with_view(
  86. 'event_booth.event_booth_booked_template',
  87. values={
  88. 'booth': booth,
  89. },
  90. subtype_id=self.env.ref('event_booth.mt_event_booth_booked').id,
  91. )
  92. def action_confirm(self, additional_values=None):
  93. write_vals = dict({'state': 'unavailable'}, **additional_values or {})
  94. self.write(write_vals)
  95. def _action_post_confirm(self, write_vals):
  96. self._post_confirmation_message()