| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import _, models |
| 4 |
|
| 5 |
|
| 6 |
class SaleOrder(models.Model): |
| 7 |
_inherit = "sale.order" |
| 8 |
|
| 9 |
def _action_confirm(self): |
| 10 |
""" If the product of an order line is a 'course', we add the client of the sale_order |
| 11 |
as a member of the channel(s) on which this product is configured (see slide.channel.product_id). """ |
| 12 |
result = super(SaleOrder, self)._action_confirm() |
| 13 |
|
| 14 |
so_lines = self.env['sale.order.line'].search( |
| 15 |
[('order_id', 'in', self.ids)] |
| 16 |
) |
| 17 |
products = so_lines.mapped('product_id') |
| 18 |
related_channels = self.env['slide.channel'].search( |
| 19 |
[('product_id', 'in', products.ids), ('enroll', '=', 'payment')], |
| 20 |
) |
| 21 |
channel_products = related_channels.mapped('product_id') |
| 22 |
|
| 23 |
channels_per_so = {sale_order: self.env['slide.channel'] for sale_order in self} |
| 24 |
for so_line in so_lines: |
| 25 |
if so_line.product_id in channel_products: |
| 26 |
for related_channel in related_channels: |
| 27 |
if related_channel.product_id == so_line.product_id: |
| 28 |
channels_per_so[so_line.order_id] = channels_per_so[so_line.order_id] | related_channel |
| 29 |
|
| 30 |
for sale_order, channels in channels_per_so.items(): |
| 31 |
channels.sudo()._action_add_members(sale_order.partner_id) |
| 32 |
|
| 33 |
return result |
| 34 |
|
| 35 |
def _verify_updated_quantity(self, order_line, product_id, new_qty, uom_id, **kwargs): |
| 36 |
"""Forbid quantity updates on courses lines.""" |
| 37 |
product = self.env['product.product'].browse(product_id) |
| 38 |
if product.service_tracking == 'course' and new_qty > 1: |
| 39 |
return 1, _('You can only add a course once in your cart.') |
| 40 |
return super()._verify_updated_quantity(order_line, product_id, new_qty, uom_id, **kwargs) |