sale_order.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import Command
  4. from odoo import models, _
  5. class SaleOrder(models.Model):
  6. _inherit = 'sale.order'
  7. def _cart_find_product_line(
  8. self, product_id=None, line_id=None,
  9. event_booth_pending_ids=None, **kwargs
  10. ):
  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(product_id, line_id, **kwargs)
  14. if not event_booth_pending_ids or line_id:
  15. return lines
  16. return lines.filtered(
  17. lambda line: any(booth.id in event_booth_pending_ids for booth in line.event_booth_pending_ids)
  18. )
  19. def _verify_updated_quantity(self, order_line, product_id, new_qty, **kwargs):
  20. """Forbid quantity updates on event booth lines."""
  21. product = self.env['product.product'].browse(product_id)
  22. if product.detailed_type == 'event_booth' and new_qty > 1:
  23. return 1, _('You cannot manually change the quantity of an Event Booth product.')
  24. return super()._verify_updated_quantity(order_line, product_id, new_qty, **kwargs)
  25. def _prepare_order_line_values(
  26. self, product_id, quantity, event_booth_pending_ids=False, registration_values=None,
  27. **kwargs
  28. ):
  29. """Add corresponding event to the SOline creation values (if booths are provided)."""
  30. values = super()._prepare_order_line_values(product_id, quantity, **kwargs)
  31. if not event_booth_pending_ids:
  32. return values
  33. booths = self.env['event.booth'].browse(event_booth_pending_ids)
  34. values['event_id'] = booths.event_id.id
  35. values['event_booth_registration_ids'] = [
  36. Command.create({
  37. 'event_booth_id': booth.id,
  38. **registration_values,
  39. }) for booth in booths
  40. ]
  41. return values
  42. # FIXME VFE investigate if it ever happens.
  43. # Probably not
  44. def _prepare_order_line_update_values(
  45. self, order_line, quantity, event_booth_pending_ids=False, registration_values=None,
  46. **kwargs
  47. ):
  48. """Delete existing booth registrations and create new ones with the update values."""
  49. values = super()._prepare_order_line_update_values(order_line, quantity, **kwargs)
  50. if not event_booth_pending_ids:
  51. return values
  52. booths = self.env['event.booth'].browse(event_booth_pending_ids)
  53. values['event_booth_registration_ids'] = [
  54. Command.delete(registration.id)
  55. for registration in order_line.event_booth_registration_ids
  56. ] + [
  57. Command.create({
  58. 'event_booth_id': booth.id,
  59. **registration_values,
  60. }) for booth in booths
  61. ]