diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website_sale_slides/models | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_sale_slides/models')
| -rw-r--r-- | addons/website_sale_slides/models/__init__.py | 5 | ||||
| -rw-r--r-- | addons/website_sale_slides/models/product_product.py | 10 | ||||
| -rw-r--r-- | addons/website_sale_slides/models/sale_order.py | 34 | ||||
| -rw-r--r-- | addons/website_sale_slides/models/slide_channel.py | 71 |
4 files changed, 120 insertions, 0 deletions
diff --git a/addons/website_sale_slides/models/__init__.py b/addons/website_sale_slides/models/__init__.py new file mode 100644 index 00000000..a7ae607d --- /dev/null +++ b/addons/website_sale_slides/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- + +from . import product_product +from . import slide_channel +from . import sale_order diff --git a/addons/website_sale_slides/models/product_product.py b/addons/website_sale_slides/models/product_product.py new file mode 100644 index 00000000..94fc0c6e --- /dev/null +++ b/addons/website_sale_slides/models/product_product.py @@ -0,0 +1,10 @@ +# -*- 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') diff --git a/addons/website_sale_slides/models/sale_order.py b/addons/website_sale_slides/models/sale_order.py new file mode 100644 index 00000000..266e2576 --- /dev/null +++ b/addons/website_sale_slides/models/sale_order.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, api + + +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)] + ) + 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._action_add_members(sale_order.partner_id) + + return result diff --git a/addons/website_sale_slides/models/slide_channel.py b/addons/website_sale_slides/models/slide_channel.py new file mode 100644 index 00000000..fd6ea6d1 --- /dev/null +++ b/addons/website_sale_slides/models/slide_channel.py @@ -0,0 +1,71 @@ +# -*- 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' + + enroll = fields.Selection(selection_add=[ + ('payment', 'On payment') + ], ondelete={'payment': lambda recs: recs.write({'enroll': 'invite'})}) + product_id = fields.Many2one('product.product', 'Product', index=True) + 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 + def create(self, vals): + channel = super(Channel, self).create(vals) + if channel.enroll == 'payment': + channel._synchronize_product_publish() + return channel + + 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): + self.filtered(lambda channel: channel.is_published and not channel.product_id.is_published).sudo().product_id.write({'is_published': True}) + self.filtered(lambda channel: not channel.is_published and channel.product_id.is_published).sudo().product_id.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 |
