Initial commit: Sale packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:49 +02:00
commit 14e3d26998
6469 changed files with 2479670 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from . import product_product
from . import product_template
from . import sale_order
from . import slide_channel
from . import website

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, fields, models
class Product(models.Model):
_inherit = "product.product"
channel_ids = fields.One2many('slide.channel', 'product_id', string='Courses')
def get_product_multiline_description_sale(self):
payment_channels = self.channel_ids.filtered(lambda course: course.enroll == 'payment')
if not payment_channels:
return super(Product, self).get_product_multiline_description_sale()
new_line = '' if len(payment_channels) == 1 else '\n'
return _('Access to:%s%s', new_line, '\n'.join(payment_channels.mapped('name')))

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class ProductTemplate(models.Model):
_inherit = 'product.template'
detailed_type = fields.Selection(selection_add=[
('course', 'Course'),
], ondelete={'course': 'set service'})
def _detailed_type_mapping(self):
type_mapping = super(ProductTemplate, self)._detailed_type_mapping()
type_mapping['course'] = 'service'
return type_mapping
@api.model
def _get_product_types_allow_zero_price(self):
return super()._get_product_types_allow_zero_price() + ["course"]

View file

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, _
class SaleOrder(models.Model):
_inherit = "sale.order"
def _action_confirm(self):
""" If the product of an order line is a 'course', we add the client of the sale_order
as a member of the channel(s) on which this product is configured (see slide.channel.product_id). """
result = super(SaleOrder, self)._action_confirm()
so_lines = self.env['sale.order.line'].search(
[('order_id', 'in', self.ids)]
)
products = so_lines.mapped('product_id')
related_channels = self.env['slide.channel'].search(
[('product_id', 'in', products.ids), ('enroll', '=', 'payment')],
)
channel_products = related_channels.mapped('product_id')
channels_per_so = {sale_order: self.env['slide.channel'] for sale_order in self}
for so_line in so_lines:
if so_line.product_id in channel_products:
for related_channel in related_channels:
if related_channel.product_id == so_line.product_id:
channels_per_so[so_line.order_id] = channels_per_so[so_line.order_id] | related_channel
for sale_order, channels in channels_per_so.items():
channels.sudo()._action_add_members(sale_order.partner_id)
return result
def _verify_updated_quantity(self, order_line, product_id, new_qty, **kwargs):
"""Forbid quantity updates on courses lines."""
product = self.env['product.product'].browse(product_id)
if product.detailed_type == 'course' and new_qty > 1:
return 1, _('You can only add a course once in your cart.')
return super()._verify_updated_quantity(order_line, product_id, new_qty, **kwargs)

View file

@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class Channel(models.Model):
_inherit = 'slide.channel'
def _get_default_product_id(self):
product_courses = self.env['product.product'].search(
[('detailed_type', '=', 'course')], limit=2)
return product_courses.id if len(product_courses) == 1 else False
enroll = fields.Selection(selection_add=[
('payment', 'On payment')
], ondelete={'payment': lambda recs: recs.write({'enroll': 'invite'})})
product_id = fields.Many2one('product.product', 'Product', domain=[('detailed_type', '=', 'course')],
default=_get_default_product_id)
product_sale_revenues = fields.Monetary(
string='Total revenues', compute='_compute_product_sale_revenues',
groups="sales_team.group_sale_salesman")
currency_id = fields.Many2one(related='product_id.currency_id')
_sql_constraints = [
('product_id_check', "CHECK( enroll!='payment' OR product_id IS NOT NULL )", "Product is required for on payment channels.")
]
@api.depends('product_id')
def _compute_product_sale_revenues(self):
domain = [
('state', 'in', self.env['sale.report']._get_done_states()),
('product_id', 'in', self.product_id.ids),
]
rg_data = dict(
(item['product_id'][0], item['price_total'])
for item in self.env['sale.report']._read_group(domain, ['product_id', 'price_total'], ['product_id'])
)
for channel in self:
channel.product_sale_revenues = rg_data.get(channel.product_id.id, 0)
@api.model_create_multi
def create(self, vals_list):
channels = super(Channel, self).create(vals_list)
channels.filtered(lambda channel: channel.enroll == 'payment')._synchronize_product_publish()
return channels
def write(self, vals):
res = super(Channel, self).write(vals)
if 'is_published' in vals:
self.filtered(lambda channel: channel.enroll == 'payment')._synchronize_product_publish()
return res
def _synchronize_product_publish(self):
"""
Ensure that when publishing a course that its linked product is also published
If all courses linked to a product are unpublished, we also unpublished the product
"""
if not self:
return
self.filtered(lambda channel: channel.is_published and not channel.product_id.is_published).sudo().product_id.write({'is_published': True})
unpublished_channel_products = self.filtered(lambda channel: not channel.is_published).product_id
group_data = self.read_group(
[('is_published', '=', True), ('product_id', 'in', unpublished_channel_products.ids)],
['product_id'],
['product_id'],
)
used_product_ids = [product['product_id'][0] for product in group_data if product['product_id_count'] > 0]
product_to_unpublish = unpublished_channel_products.filtered(lambda product: product.id not in used_product_ids)
if product_to_unpublish:
product_to_unpublish.sudo().write({'is_published': False})
def action_view_sales(self):
action = self.env["ir.actions.actions"]._for_xml_id("website_sale_slides.sale_report_action_slides")
action['domain'] = [('product_id', 'in', self.product_id.ids)]
return action
def _filter_add_members(self, target_partners, **member_values):
""" Overridden to add 'payment' channels to the filtered channels. People
that can write on payment-based channels can add members. """
result = super(Channel, self)._filter_add_members(target_partners, **member_values)
on_payment = self.filtered(lambda channel: channel.enroll == 'payment')
if on_payment:
try:
on_payment.check_access_rights('write')
on_payment.check_access_rule('write')
except:
pass
else:
result |= on_payment
return result

View file

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
from odoo.osv import expression
class Website(models.Model):
_inherit = 'website'
def sale_product_domain(self):
return expression.AND([
super(Website, self).sale_product_domain(),
[('detailed_type', '!=', 'course')],
])