64 lines
2.8 KiB
Python
64 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api
|
|
from odoo.fields import Domain
|
|
|
|
class LoyaltyReward(models.Model):
|
|
_inherit = 'loyalty.reward'
|
|
|
|
reward_product_applicability = fields.Selection([
|
|
('specific', 'Specific Product(s)'),
|
|
('cheapest', 'Cheapest Product on Order')
|
|
], string='Reward Product Applicability', default='specific', required=True)
|
|
|
|
reward_product_category_id = fields.Many2one(
|
|
'product.category', string='Reward Product Category',
|
|
help='If specified, only products under this category (including subcategories) will be considered for the cheapest product reward.'
|
|
)
|
|
|
|
@api.depends('reward_product_id', 'reward_product_tag_id', 'reward_type', 'reward_product_applicability', 'reward_product_category_id')
|
|
def _compute_multi_product(self):
|
|
super()._compute_multi_product()
|
|
for reward in self:
|
|
if reward.reward_type == 'product' and reward.reward_product_applicability == 'cheapest':
|
|
products = reward.reward_product_id + reward.reward_product_tag_id.product_ids.filtered(
|
|
lambda product: product.type != 'combo'
|
|
)
|
|
if reward.reward_product_category_id:
|
|
category_ids = self.env['product.category'].search([
|
|
('id', 'child_of', reward.reward_product_category_id.ids)
|
|
]).ids
|
|
cat_products = self.env['product.product'].search([
|
|
('categ_id', 'in', category_ids)
|
|
])
|
|
products |= cat_products.filtered(lambda product: product.type != 'combo')
|
|
reward.multi_product = False
|
|
reward.reward_product_ids = products
|
|
|
|
@api.model
|
|
def _load_pos_data_fields(self, config):
|
|
fields = super()._load_pos_data_fields(config)
|
|
if 'reward_product_applicability' not in fields:
|
|
fields.append('reward_product_applicability')
|
|
if 'reward_product_category_id' not in fields:
|
|
fields.append('reward_product_category_id')
|
|
return fields
|
|
|
|
@api.model
|
|
def _load_pos_data_domain(self, data, config):
|
|
reward_product_tag_domain = [
|
|
('reward_product_tag_id', '!=', False),
|
|
'|',
|
|
('reward_product_tag_id.product_template_ids.active', '=', True),
|
|
('reward_product_tag_id.product_product_ids.active', '=', True),
|
|
]
|
|
return Domain.AND([
|
|
[('program_id', 'in', config._get_program_ids().ids)],
|
|
Domain.OR([
|
|
[('reward_type', '!=', 'product')],
|
|
[('reward_product_id.active', '=', True)],
|
|
[('reward_product_applicability', '=', 'cheapest')],
|
|
reward_product_tag_domain,
|
|
]),
|
|
])
|