| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import _, models |
| 4 |
from odoo.exceptions import UserError |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrder(models.Model): |
| 8 |
_inherit = "sale.order" |
| 9 |
|
| 10 |
def _cart_find_product_line(self, *args, event_slot_id=False, event_ticket_id=False, **kwargs): |
| 11 |
lines = super()._cart_find_product_line( |
| 12 |
*args, event_slot_id=event_slot_id, event_ticket_id=event_ticket_id, **kwargs, |
| 13 |
) |
| 14 |
if not event_slot_id and not event_ticket_id: |
| 15 |
return lines |
| 16 |
|
| 17 |
return lines.filtered(lambda line: line.event_slot_id.id == event_slot_id and line.event_ticket_id.id == event_ticket_id) |
| 18 |
|
| 19 |
def _verify_updated_quantity( |
| 20 |
self, order_line, product_id, new_qty, uom_id, *, event_slot_id=False, event_ticket_id=False, **kwargs |
| 21 |
): |
| 22 |
"""Restrict quantity updates for event tickets according to available seats.""" |
| 23 |
new_qty, warning = super()._verify_updated_quantity( |
| 24 |
order_line, |
| 25 |
product_id, |
| 26 |
new_qty, |
| 27 |
uom_id, |
| 28 |
event_slot_id=event_slot_id, |
| 29 |
event_ticket_id=event_ticket_id, |
| 30 |
**kwargs, |
| 31 |
) |
| 32 |
|
| 33 |
if not event_ticket_id: |
| 34 |
if not order_line.event_ticket_id or new_qty < order_line.product_uom_qty: |
| 35 |
return new_qty, warning |
| 36 |
else: |
| 37 |
return order_line.product_uom_qty, _("You cannot raise manually the event ticket quantity in your cart") |
| 38 |
|
| 39 |
# Adding new ticket to the cart (might be automatically linked to an existing line) |
| 40 |
ticket = self.env['event.event.ticket'].browse(event_ticket_id).exists() |
| 41 |
if not ticket: |
| 42 |
raise UserError(_("The provided ticket doesn't exist")) |
| 43 |
slot = self.env['event.slot'].browse(event_slot_id).exists() |
| 44 |
if event_slot_id and not slot: |
| 45 |
raise UserError(_("The provided ticket slot doesn't exist")) |
| 46 |
|
| 47 |
# TODO TDE consider full cart qty and not only added qty |
| 48 |
# if event seats are not auto confirmed. |
| 49 |
# Since created registrations are automatically reserved |
| 50 |
# We should only consider new added qty and not full quantity |
| 51 |
# when checking for seat availability |
| 52 |
existing_qty = order_line.product_uom_qty if order_line else 0 |
| 53 |
qty_added = new_qty - existing_qty |
| 54 |
warning = '' |
| 55 |
ticket_seats_available = ticket.event_id._get_seats_availability([(slot, ticket)])[0] if slot else ticket.seats_available |
| 56 |
if ticket.seats_limited and ticket_seats_available <= 0: |
| 57 |
# Remove existing line if exists and do not add a new one |
| 58 |
# if no ticket is available anymore |
| 59 |
new_qty = existing_qty |
| 60 |
warning = _( |
| 61 |
'Sorry, The %(ticket)s tickets for the %(event)s event are sold out.', |
| 62 |
ticket=ticket.name, |
| 63 |
event=ticket.event_id.name, |
| 64 |
) |
| 65 |
elif ticket.seats_limited and qty_added > ticket_seats_available: |
| 66 |
new_qty = existing_qty + ticket_seats_available |
| 67 |
warning = _( |
| 68 |
'Sorry, only %(remaining_seats)d seats are still available for the %(ticket)s ticket for the %(event)s event%(slot)s.', |
| 69 |
remaining_seats=ticket_seats_available, |
| 70 |
slot=f' on {slot.name}' if slot else '', |
| 71 |
ticket=ticket.name, |
| 72 |
event=ticket.event_id.name, |
| 73 |
) |
| 74 |
|
| 75 |
return new_qty, warning |
| 76 |
|
| 77 |
def _prepare_order_line_values(self, product_id, *args, event_slot_id=False, event_ticket_id=False, **kwargs): |
| 78 |
"""Add corresponding event to the SOline creation values (if ticket is provided).""" |
| 79 |
values = super()._prepare_order_line_values( |
| 80 |
product_id, *args, event_ticket_id=event_ticket_id, **kwargs, |
| 81 |
) |
| 82 |
|
| 83 |
if not event_ticket_id: |
| 84 |
return values |
| 85 |
|
| 86 |
ticket = self.env['event.event.ticket'].browse(event_ticket_id) |
| 87 |
|
| 88 |
if ticket.product_id.id != product_id: |
| 89 |
raise UserError(_("The ticket doesn't match with this product.")) |
| 90 |
|
| 91 |
values['event_id'] = ticket.event_id.id |
| 92 |
values['event_ticket_id'] = ticket.id |
| 93 |
values['event_slot_id'] = event_slot_id |
| 94 |
|
| 95 |
return values |
| 96 |
|
| 97 |
def _cart_update_order_line(self, order_line, quantity, **kwargs): |
| 98 |
old_qty = order_line.product_uom_qty |
| 99 |
|
| 100 |
updated_line = super()._cart_update_order_line(order_line, quantity, **kwargs) |
| 101 |
|
| 102 |
# Remove event registrations on quantity decrease. |
| 103 |
if ( |
| 104 |
updated_line |
| 105 |
and updated_line.event_ticket_id |
| 106 |
and (diff := old_qty - updated_line.product_uom_qty) > 0 |
| 107 |
): |
| 108 |
attendees = self.env['event.registration'].search( |
| 109 |
domain=[ |
| 110 |
('state', '!=', 'cancel'), |
| 111 |
('sale_order_id', '=', self.id), |
| 112 |
('event_slot_id', '=', order_line.event_slot_id.id), |
| 113 |
('event_ticket_id', '=', order_line.event_ticket_id.id), |
| 114 |
], |
| 115 |
offset=updated_line.product_uom_qty, |
| 116 |
limit=diff, |
| 117 |
order='create_date asc', |
| 118 |
) |
| 119 |
attendees.action_cancel() |
| 120 |
|
| 121 |
return updated_line |
| 122 |
|
| 123 |
def _filter_can_send_abandoned_cart_mail(self): |
| 124 |
# Prevent carts with expired/sold out tickets from being subject of reminder emails |
| 125 |
return super()._filter_can_send_abandoned_cart_mail().filtered( |
| 126 |
lambda so: all(ticket.sale_available for ticket in so.order_line.event_ticket_id), |
| 127 |
) |