| 1 |
|
| 2 |
|
| 3 |
from datetime import timedelta |
| 4 |
|
| 5 |
from odoo.fields import Command, Date |
| 6 |
from odoo.tests.common import tagged |
| 7 |
|
| 8 |
from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon |
| 9 |
|
| 10 |
|
| 11 |
@tagged('-at_install', 'post_install') |
| 12 |
class TestUnlinkReward(TestSaleCouponCommon): |
| 13 |
|
| 14 |
@classmethod |
| 15 |
def setUpClass(cls): |
| 16 |
super().setUpClass() |
| 17 |
cls.promotion_program = cls.env['loyalty.program'].create({ |
| 18 |
'name': 'Buy A + 1 B, 1 B are free', |
| 19 |
'program_type': 'promotion', |
| 20 |
'applies_on': 'current', |
| 21 |
'company_id': cls.env.company.id, |
| 22 |
'trigger': 'auto', |
| 23 |
'rule_ids': [Command.create({ |
| 24 |
'product_ids': cls.product_A, |
| 25 |
'reward_point_amount': 1, |
| 26 |
'reward_point_mode': 'order', |
| 27 |
'minimum_qty': 1, |
| 28 |
})], |
| 29 |
}) |
| 30 |
cls.reward = cls.env['loyalty.reward'].create({ |
| 31 |
'program_id': cls.promotion_program.id, |
| 32 |
'reward_type': 'discount', |
| 33 |
}) |
| 34 |
|
| 35 |
def test_sale_unlink_reward(self): |
| 36 |
order = self.empty_order |
| 37 |
order.write({'order_line': [ |
| 38 |
Command.create({ |
| 39 |
'product_id': self.product_A.id, |
| 40 |
'name': 'Ordinary Product A', |
| 41 |
'product_uom_qty': 1.0, |
| 42 |
}), |
| 43 |
Command.create({ |
| 44 |
'product_id': self.product_B.id, |
| 45 |
'name': '2 Product B', |
| 46 |
'product_uom_qty': 1.0, |
| 47 |
}), |
| 48 |
]}) |
| 49 |
order._update_programs_and_rewards() |
| 50 |
self._claim_reward(order, self.promotion_program) |
| 51 |
self.reward.unlink() |
| 52 |
|
| 53 |
|
| 54 |
self.assertTrue(self.reward.exists()) |
| 55 |
self.assertFalse(self.reward.active) |
| 56 |
|
| 57 |
def test_unlink_expired_coupon_line(self): |
| 58 |
"""Ensure that lines linked to expired coupons get unlinked from the order.""" |
| 59 |
order = self.empty_order |
| 60 |
order.order_line = [Command.create({'product_id': self.product_A.id})] |
| 61 |
coupon_program = self.code_promotion_program |
| 62 |
self.env['loyalty.generate.wizard'].with_context(active_id=coupon_program.id).create({ |
| 63 |
'coupon_qty': 1, |
| 64 |
'points_granted': 1, |
| 65 |
}).generate_coupons() |
| 66 |
coupon = coupon_program.coupon_ids |
| 67 |
self._apply_promo_code(order, coupon.code) |
| 68 |
self.assertTrue(order.order_line.coupon_id) |
| 69 |
coupon.expiration_date = Date.today() - timedelta(days=1) |
| 70 |
order._update_programs_and_rewards() |
| 71 |
self.assertFalse(order.order_line.coupon_id) |