| 1 |
|
| 2 |
|
| 3 |
from odoo import fields, models, _ |
| 4 |
from odoo.exceptions import ValidationError |
| 5 |
from odoo.fields import Domain |
| 6 |
|
| 7 |
|
| 8 |
class SaleOrder(models.Model): |
| 9 |
_inherit = "sale.order" |
| 10 |
|
| 11 |
attendee_count = fields.Integer('Attendee Count', compute='_compute_attendee_count') |
| 12 |
|
| 13 |
def write(self, vals): |
| 14 |
""" Synchronize partner from SO to registrations. This is done notably |
| 15 |
in website_sale controller shop/address that updates customer, but not |
| 16 |
only. """ |
| 17 |
result = super(SaleOrder, self).write(vals) |
| 18 |
if any(line.service_tracking == 'event' for line in self.order_line) and vals.get('partner_id'): |
| 19 |
registrations_toupdate = self.env['event.registration'].sudo().search([('sale_order_id', 'in', self.ids)]) |
| 20 |
registrations_toupdate.write({'partner_id': vals['partner_id']}) |
| 21 |
return result |
| 22 |
|
| 23 |
def action_confirm(self): |
| 24 |
res = super(SaleOrder, self).action_confirm() |
| 25 |
|
| 26 |
for so in self: |
| 27 |
if not any(line.service_tracking == 'event' for line in so.order_line): |
| 28 |
continue |
| 29 |
so_lines_missing_events = so.order_line.filtered(lambda line: line.service_tracking == 'event' and not line.event_id) |
| 30 |
if so_lines_missing_events: |
| 31 |
so_lines_descriptions = "".join(f"\n- {so_line_description.name}" for so_line_description in so_lines_missing_events) |
| 32 |
raise ValidationError(_("Please make sure all your event related lines are configured before confirming this order:%s", so_lines_descriptions)) |
| 33 |
|
| 34 |
so.order_line._init_registrations() |
| 35 |
if len(self) == 1: |
| 36 |
return self.env['ir.actions.act_window'].with_context( |
| 37 |
default_sale_order_id=so.id |
| 38 |
)._for_xml_id('event_sale.action_sale_order_event_registration') |
| 39 |
return res |
| 40 |
|
| 41 |
def action_view_attendee_list(self): |
| 42 |
action = self.env["ir.actions.actions"]._for_xml_id("event.event_registration_action_tree") |
| 43 |
action['domain'] = [('sale_order_id', 'in', self.ids)] |
| 44 |
return action |
| 45 |
|
| 46 |
def _compute_attendee_count(self): |
| 47 |
sale_orders_data = self.env['event.registration']._read_group( |
| 48 |
[('sale_order_id', 'in', self.ids), |
| 49 |
('state', '!=', 'cancel')], |
| 50 |
['sale_order_id'], ['__count'], |
| 51 |
) |
| 52 |
attendee_count_data = { |
| 53 |
sale_order.id: count for sale_order, count in sale_orders_data |
| 54 |
} |
| 55 |
for sale_order in self: |
| 56 |
sale_order.attendee_count = attendee_count_data.get(sale_order.id, 0) |
| 57 |
|
| 58 |
def _get_product_catalog_domain(self): |
| 59 |
return super()._get_product_catalog_domain() & Domain('service_tracking', '!=', 'event') |