| 1 |
|
| 2 |
|
| 3 |
from odoo import _, models |
| 4 |
from odoo.fields import Command |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrder(models.Model): |
| 8 |
_inherit = 'sale.order' |
| 9 |
|
| 10 |
def _cart_find_product_line(self, *args, event_booth_pending_ids=None, **kwargs): |
| 11 |
"""Check if there is another sale order line which already contains the requested event_booth_pending_ids |
| 12 |
to overwrite it with the newly requested booths to avoid having multiple so_line related to the same booths""" |
| 13 |
lines = super()._cart_find_product_line( |
| 14 |
*args, event_booth_pending_ids=event_booth_pending_ids, **kwargs, |
| 15 |
) |
| 16 |
|
| 17 |
if not event_booth_pending_ids: |
| 18 |
return lines |
| 19 |
|
| 20 |
return lines.filtered( |
| 21 |
lambda line: any(booth.id in event_booth_pending_ids for booth in line.event_booth_pending_ids) |
| 22 |
) |
| 23 |
|
| 24 |
def _verify_updated_quantity(self, order_line, product_id, new_qty, uom_id, **kwargs): |
| 25 |
"""Forbid quantity updates on event booth lines.""" |
| 26 |
product = self.env['product.product'].browse(product_id) |
| 27 |
if product.service_tracking == 'event_booth' and new_qty > 1: |
| 28 |
return 1, _('You cannot manually change the quantity of an Event Booth product.') |
| 29 |
return super()._verify_updated_quantity(order_line, product_id, new_qty, uom_id, **kwargs) |
| 30 |
|
| 31 |
def _prepare_order_line_values( |
| 32 |
self, *args, event_booth_pending_ids=False, registration_values=None, |
| 33 |
**kwargs |
| 34 |
): |
| 35 |
"""Add corresponding event to the SOline creation values (if booths are provided).""" |
| 36 |
values = super()._prepare_order_line_values( |
| 37 |
*args, |
| 38 |
event_booth_pending_ids=event_booth_pending_ids, |
| 39 |
registration_values=registration_values, |
| 40 |
**kwargs, |
| 41 |
) |
| 42 |
|
| 43 |
if not event_booth_pending_ids: |
| 44 |
return values |
| 45 |
|
| 46 |
booths = self.env['event.booth'].browse(event_booth_pending_ids) |
| 47 |
|
| 48 |
values['event_id'] = booths.event_id.id |
| 49 |
values['event_booth_registration_ids'] = [ |
| 50 |
Command.create({ |
| 51 |
'event_booth_id': booth.id, |
| 52 |
**registration_values, |
| 53 |
}) for booth in booths |
| 54 |
] |
| 55 |
|
| 56 |
return values |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def _prepare_order_line_update_values( |
| 61 |
self, order_line, quantity, *, event_booth_pending_ids=False, registration_values=None, |
| 62 |
**kwargs |
| 63 |
): |
| 64 |
"""Delete existing booth registrations and create new ones with the update values.""" |
| 65 |
values = super()._prepare_order_line_update_values(order_line, quantity, **kwargs) |
| 66 |
|
| 67 |
if not event_booth_pending_ids: |
| 68 |
return values |
| 69 |
|
| 70 |
booths = self.env['event.booth'].browse(event_booth_pending_ids) |
| 71 |
values['event_booth_registration_ids'] = [ |
| 72 |
Command.delete(registration.id) |
| 73 |
for registration in order_line.event_booth_registration_ids |
| 74 |
] + [ |
| 75 |
Command.create({ |
| 76 |
'event_booth_id': booth.id, |
| 77 |
**registration_values, |
| 78 |
}) for booth in booths |
| 79 |
] |