Debian Codegraph @

addons/website_sale_slides/models/slide_channel.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import api, fields, models
5
6
7 class SlideChannel(models.Model):
8 _inherit = 'slide.channel'
9
10 def _get_default_product_id(self):
11 product_courses = self.env['product.product'].search(
12 [('service_tracking', '=', 'course')], limit=2)
13 return product_courses.id if len(product_courses) == 1 else False
14
15 enroll = fields.Selection(selection_add=[
16 ('payment', 'On payment')
17 ], ondelete={'payment': lambda recs: recs.write({'enroll': 'invite'})})
18 product_id = fields.Many2one('product.product', 'Product', domain=[('service_tracking', '=', 'course')],
19 index='btree_not_null', default=_get_default_product_id)
20 product_sale_revenues = fields.Monetary(
21 string='Total revenues', compute='_compute_product_sale_revenues',
22 groups="sales_team.group_sale_salesman")
23 currency_id = fields.Many2one(related='product_id.currency_id')
24
25 _product_id_check = models.Constraint(
26 "CHECK( enroll!='payment' OR product_id IS NOT NULL )",
27 'Product is required for on payment channels.',
28 )
29
30 @api.depends('product_id')
31 def _compute_product_sale_revenues(self):
32 domain = [
33 ('state', 'in', self.env['sale.report']._get_done_states()),
34 ('product_id', 'in', self.product_id.ids),
35 ]
36 rg_data = {
37 product.id: price_total
38 for product, price_total in self.env['sale.report']._read_group(domain, ['product_id'], ['price_total:sum'])
39 }
40 for channel in self:
41 channel.product_sale_revenues = rg_data.get(channel.product_id.id, 0)
42
43 @api.model_create_multi
44 def create(self, vals_list):
45 channels = super().create(vals_list)
46 channels.filtered(lambda channel: channel.enroll == 'payment')._synchronize_product_publish()
47 return channels
48
49 def write(self, vals):
50 res = super().write(vals)
51 if 'is_published' in vals:
52 self.filtered(lambda channel: channel.enroll == 'payment')._synchronize_product_publish()
53 return res
54
55 def _synchronize_product_publish(self):
56 """
57 Ensure that when publishing a course that its linked product is also published
58 If all courses linked to a product are unpublished, we also unpublished the product
59 """
60 if not self:
61 return
62 self.filtered(lambda channel: channel.is_published and not channel.product_id.is_published).sudo().product_id.write({'is_published': True})
63
64 unpublished_channel_products = self.filtered(lambda channel: not channel.is_published).product_id
65 group_data = self._read_group(
66 [('is_published', '=', True), ('product_id', 'in', unpublished_channel_products.ids)],
67 ['product_id'],
68 )
69 used_product_ids = {product.id for [product] in group_data}
70 product_to_unpublish = unpublished_channel_products.filtered(lambda product: product.id not in used_product_ids)
71 if product_to_unpublish:
72 product_to_unpublish.sudo().write({'is_published': False})
73
74 def action_view_sales(self):
75 action = self.env["ir.actions.actions"]._for_xml_id("website_sale_slides.sale_report_action_slides")
76 action['domain'] = [('product_id', 'in', self.product_id.ids)]
77 return action