1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import api, fields, models, _
5from odoo.exceptions import ValidationError
6
7
8class CouponReward(models.Model):
9    _name = 'coupon.reward'
10    _description = "Coupon Reward"
11    _rec_name = 'reward_description'
12
13    # VFE FIXME multi company
14    """Rewards are not restricted to a company...
15    You could have a reward_product_id limited to a specific company A.
16    But still use this reward as reward of a program of company B...
17    """
18    reward_description = fields.Char('Reward Description')
19    reward_type = fields.Selection([
20        ('discount', 'Discount'),
21        ('product', 'Free Product'),
22        ], string='Reward Type', default='discount',
23        help="Discount - Reward will be provided as discount.\n" +
24        "Free Product - Free product will be provide as reward \n" +
25        "Free Shipping - Free shipping will be provided as reward (Need delivery module)")
26    # Product Reward
27    reward_product_id = fields.Many2one('product.product', string="Free Product",
28        help="Reward Product")
29    reward_product_quantity = fields.Integer(string="Quantity", default=1, help="Reward product quantity")
30    # Discount Reward
31    discount_type = fields.Selection([
32        ('percentage', 'Percentage'),
33        ('fixed_amount', 'Fixed Amount')], default="percentage",
34        help="Percentage - Entered percentage discount will be provided\n" +
35        "Amount - Entered fixed amount discount will be provided")
36    discount_percentage = fields.Float(string="Discount", default=10,
37        help='The discount in percentage, between 1 and 100')
38    discount_apply_on = fields.Selection([
39        ('on_order', 'On Order'),
40        ('cheapest_product', 'On Cheapest Product'),
41        ('specific_products', 'On Specific Products')], default="on_order",
42        help="On Order - Discount on whole order\n" +
43        "Cheapest product - Discount on cheapest product of the order\n" +
44        "Specific products - Discount on selected specific products")
45    discount_specific_product_ids = fields.Many2many('product.product', string="Products",
46        help="Products that will be discounted if the discount is applied on specific products")
47    discount_max_amount = fields.Float(default=0,
48        help="Maximum amount of discount that can be provided")
49    discount_fixed_amount = fields.Float(string="Fixed Amount", help='The discount in fixed amount')
50    reward_product_uom_id = fields.Many2one(related='reward_product_id.product_tmpl_id.uom_id', string='Unit of Measure', readonly=True)
51    discount_line_product_id = fields.Many2one('product.product', string='Reward Line Product', copy=False,
52        help="Product used in the sales order to apply the discount. Each coupon program has its own reward product for reporting purpose")
53
54    @api.constrains('discount_percentage')
55    def _check_discount_percentage(self):
56        if self.filtered(lambda reward: reward.discount_type == 'percentage' and (reward.discount_percentage < 0 or reward.discount_percentage > 100)):
57            raise ValidationError(_('Discount percentage should be between 1-100'))
58
59    def name_get(self):
60        """
61        Returns a complete description of the reward
62        """
63        result = []
64        for reward in self:
65            reward_string = ""
66            if reward.reward_type == 'product':
67                reward_string = _("Free Product - %s", reward.reward_product_id.name)
68            elif reward.reward_type == 'discount':
69                if reward.discount_type == 'percentage':
70                    reward_percentage = str(reward.discount_percentage)
71                    if reward.discount_apply_on == 'on_order':
72                        reward_string = _("%s%% discount on total amount", reward_percentage)
73                    elif reward.discount_apply_on == 'specific_products':
74                        if len(reward.discount_specific_product_ids) > 1:
75                            reward_string = _("%s%% discount on products", reward_percentage)
76                        else:
77                            reward_string = _(
78                                "%(percentage)s%% discount on %(product_name)s",
79                                percentage=reward_percentage,
80                                product_name=reward.discount_specific_product_ids.name
81                            )
82                    elif reward.discount_apply_on == 'cheapest_product':
83                        reward_string = _("%s%% discount on cheapest product", reward_percentage)
84                elif reward.discount_type == 'fixed_amount':
85                    program = self.env['coupon.program'].search([('reward_id', '=', reward.id)])
86                    reward_string = _(
87                        "%(amount)s %(currency)s discount on total amount",
88                        amount=reward.discount_fixed_amount,
89                        currency=program.currency_id.name
90                    )
91            result.append((reward.id, reward_string))
92        return result
93