| 1 |
|
| 2 |
|
| 3 |
from odoo import _, fields, models |
| 4 |
from odoo.exceptions import ValidationError |
| 5 |
|
| 6 |
|
| 7 |
class SaleLoyaltyCouponWizard(models.TransientModel): |
| 8 |
_name = 'sale.loyalty.coupon.wizard' |
| 9 |
_description = 'Sale Loyalty - Apply Coupon Wizard' |
| 10 |
|
| 11 |
order_id = fields.Many2one('sale.order', default=lambda self: self.env.context.get('active_id'), required=True) |
| 12 |
|
| 13 |
coupon_code = fields.Char(required=True) |
| 14 |
|
| 15 |
def action_apply(self): |
| 16 |
self.ensure_one() |
| 17 |
if not self.order_id: |
| 18 |
raise ValidationError(_('Invalid sales order.')) |
| 19 |
status = self.order_id._try_apply_code(self.coupon_code) |
| 20 |
if 'error' in status: |
| 21 |
raise ValidationError(status['error']) |
| 22 |
all_rewards = self.env['loyalty.reward'] |
| 23 |
for rewards in status.values(): |
| 24 |
all_rewards |= rewards |
| 25 |
action = self.env['ir.actions.actions']._for_xml_id('sale_loyalty.sale_loyalty_reward_wizard_action') |
| 26 |
action['context'] = { |
| 27 |
'active_id': self.order_id.id, |
| 28 |
'default_reward_ids': all_rewards.ids, |
| 29 |
} |
| 30 |
return action |