1# -*- coding: utf-8 -*- 2# Part of Odoo. See LICENSE file for full copyright and licensing details. 3 4from odoo import api, fields, models 5 6 7class Channel(models.Model): 8 _inherit = 'slide.channel' 9 10 enroll = fields.Selection(selection_add=[ 11 ('payment', 'On payment') 12 ], ondelete={'payment': lambda recs: recs.write({'enroll': 'invite'})}) 13 product_id = fields.Many2one('product.product', 'Product', index=True) 14 product_sale_revenues = fields.Monetary( 15 string='Total revenues', compute='_compute_product_sale_revenues', 16 groups="sales_team.group_sale_salesman") 17 currency_id = fields.Many2one(related='product_id.currency_id') 18 19 _sql_constraints = [ 20 ('product_id_check', "CHECK( enroll!='payment' OR product_id IS NOT NULL )", "Product is required for on payment channels.") 21 ] 22 23 @api.depends('product_id') 24 def _compute_product_sale_revenues(self): 25 domain = [ 26 ('state', 'in', self.env['sale.report']._get_done_states()), 27 ('product_id', 'in', self.product_id.ids), 28 ] 29 rg_data = dict( 30 (item['product_id'][0], item['price_total']) 31 for item in self.env['sale.report'].read_group(domain, ['product_id', 'price_total'], ['product_id']) 32 ) 33 for channel in self: 34 channel.product_sale_revenues = rg_data.get(channel.product_id.id, 0) 35 36 @api.model 37 def create(self, vals): 38 channel = super(Channel, self).create(vals) 39 if channel.enroll == 'payment': 40 channel._synchronize_product_publish() 41 return channel 42 43 def write(self, vals): 44 res = super(Channel, self).write(vals) 45 if 'is_published' in vals: 46 self.filtered(lambda channel: channel.enroll == 'payment')._synchronize_product_publish() 47 return res 48 49 def _synchronize_product_publish(self): 50 self.filtered(lambda channel: channel.is_published and not channel.product_id.is_published).sudo().product_id.write({'is_published': True}) 51 self.filtered(lambda channel: not channel.is_published and channel.product_id.is_published).sudo().product_id.write({'is_published': False}) 52 53 def action_view_sales(self): 54 action = self.env["ir.actions.actions"]._for_xml_id("website_sale_slides.sale_report_action_slides") 55 action['domain'] = [('product_id', 'in', self.product_id.ids)] 56 return action 57 58 def _filter_add_members(self, target_partners, **member_values): 59 """ Overridden to add 'payment' channels to the filtered channels. People 60 that can write on payment-based channels can add members. """ 61 result = super(Channel, self)._filter_add_members(target_partners, **member_values) 62 on_payment = self.filtered(lambda channel: channel.enroll == 'payment') 63 if on_payment: 64 try: 65 on_payment.check_access_rights('write') 66 on_payment.check_access_rule('write') 67 except: 68 pass 69 else: 70 result |= on_payment 71 return result 72