| 1 |
|
| 2 |
|
| 3 |
from odoo import _, api, fields, models |
| 4 |
from odoo.exceptions import ValidationError |
| 5 |
|
| 6 |
|
| 7 |
class SaleLoyaltyRewardWizard(models.TransientModel): |
| 8 |
_name = 'sale.loyalty.reward.wizard' |
| 9 |
_description = 'Sale Loyalty - Reward Selection Wizard' |
| 10 |
|
| 11 |
order_id = fields.Many2one('sale.order', default=lambda self: self.env.context.get('active_id'), required=True) |
| 12 |
|
| 13 |
reward_ids = fields.Many2many('loyalty.reward', compute='_compute_claimable_reward_ids') |
| 14 |
selected_reward_id = fields.Many2one('loyalty.reward', domain="[('id', 'in', reward_ids)]") |
| 15 |
|
| 16 |
multi_product_reward = fields.Boolean(related='selected_reward_id.multi_product') |
| 17 |
reward_product_ids = fields.Many2many(related='selected_reward_id.reward_product_ids') |
| 18 |
selected_product_id = fields.Many2one('product.product', domain="[('id', 'in', reward_product_ids)]", |
| 19 |
compute='_compute_selected_product_id', readonly=False, store=True,) |
| 20 |
|
| 21 |
@api.depends('order_id') |
| 22 |
def _compute_claimable_reward_ids(self): |
| 23 |
for wizard in self: |
| 24 |
if not wizard.order_id: |
| 25 |
wizard.reward_ids = False |
| 26 |
else: |
| 27 |
claimable_reward = wizard.order_id._get_claimable_rewards() |
| 28 |
reward_ids = self.env['loyalty.reward'] |
| 29 |
for rewards in claimable_reward.values(): |
| 30 |
reward_ids |= rewards |
| 31 |
wizard.reward_ids = reward_ids |
| 32 |
|
| 33 |
@api.depends('reward_product_ids') |
| 34 |
def _compute_selected_product_id(self): |
| 35 |
for wizard in self: |
| 36 |
if not wizard.selected_reward_id.reward_type == 'product': |
| 37 |
wizard.selected_product_id = False |
| 38 |
else: |
| 39 |
wizard.selected_product_id = wizard.reward_product_ids[:1] |
| 40 |
|
| 41 |
def action_apply(self): |
| 42 |
self.ensure_one() |
| 43 |
if not self.selected_reward_id: |
| 44 |
raise ValidationError(_('No reward selected.')) |
| 45 |
claimable_rewards = self.order_id._get_claimable_rewards() |
| 46 |
selected_coupon = False |
| 47 |
for coupon, rewards in claimable_rewards.items(): |
| 48 |
if self.selected_reward_id in rewards: |
| 49 |
selected_coupon = coupon |
| 50 |
break |
| 51 |
if not selected_coupon: |
| 52 |
raise ValidationError(_('Coupon not found while trying to add the following reward: %s', self.selected_reward_id.description)) |
| 53 |
self.order_id._apply_program_reward(self.selected_reward_id, coupon, product=self.selected_product_id) |
| 54 |
self.order_id._update_programs_and_rewards() |
| 55 |
self._unlink_unused_coupon_ids() |
| 56 |
return True |
| 57 |
|
| 58 |
def action_cancel(self): |
| 59 |
self.ensure_one() |
| 60 |
self._unlink_unused_coupon_ids() |
| 61 |
|
| 62 |
def _unlink_unused_coupon_ids(self): |
| 63 |
reward_coupons = self.order_id.order_line.coupon_id |
| 64 |
self.order_id.coupon_point_ids.filtered( |
| 65 |
lambda points: ( |
| 66 |
points.coupon_id.program_id.applies_on == 'current' and |
| 67 |
points.coupon_id not in reward_coupons |
| 68 |
) |
| 69 |
).coupon_id.sudo().unlink() |