| 1 |
|
| 2 |
|
| 3 |
from odoo import Command, http |
| 4 |
from odoo.tests import tagged |
| 5 |
|
| 6 |
from odoo.addons.website_sale.tests.common import MockRequest, WebsiteSaleCommon |
| 7 |
from odoo.addons.website_sale_loyalty.controllers.main import WebsiteSale |
| 8 |
|
| 9 |
|
| 10 |
@tagged('post_install', '-at_install') |
| 11 |
class TestClaimReward(WebsiteSaleCommon): |
| 12 |
|
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
super().setUpClass() |
| 16 |
|
| 17 |
cls.WebsiteSaleController = WebsiteSale() |
| 18 |
|
| 19 |
cls.user_portal = cls._create_new_portal_user() |
| 20 |
cls.partner_portal = cls.user_portal.partner_id |
| 21 |
|
| 22 |
cls.env['product.pricelist'].search([]).action_archive() |
| 23 |
|
| 24 |
tag = cls.env['product.tag'].create({ |
| 25 |
'name': 'multi reward', |
| 26 |
}) |
| 27 |
cls.product1, cls.product2 = cls.env['product.product'].create([ |
| 28 |
{ |
| 29 |
'name': 'Test Product', |
| 30 |
'list_price': 10.0, |
| 31 |
'taxes_id': False, |
| 32 |
'product_tag_ids': tag, |
| 33 |
}, { |
| 34 |
'name': 'Test Product 2', |
| 35 |
'list_price': 20.0, |
| 36 |
'taxes_id': False, |
| 37 |
'product_tag_ids': tag, |
| 38 |
}]) |
| 39 |
|
| 40 |
cls.promo_program, cls.coupon_program = cls.env['loyalty.program'].create([{ |
| 41 |
'name': 'Free Products', |
| 42 |
'program_type': 'promotion', |
| 43 |
'applies_on': 'current', |
| 44 |
'trigger': 'auto', |
| 45 |
'rule_ids': [Command.create({ |
| 46 |
'minimum_qty': 1, |
| 47 |
'minimum_amount': 0.00, |
| 48 |
'reward_point_amount': 3, |
| 49 |
})], |
| 50 |
'reward_ids': [Command.create({ |
| 51 |
'reward_type': 'product', |
| 52 |
'reward_product_tag_id': tag.id, |
| 53 |
'reward_product_qty': 1, |
| 54 |
'required_points': 1, |
| 55 |
})], |
| 56 |
}, { |
| 57 |
'name': "Multi-reward coupons", |
| 58 |
'program_type': 'coupons', |
| 59 |
'applies_on': 'current', |
| 60 |
'trigger': 'with_code', |
| 61 |
'reward_ids': [ |
| 62 |
Command.create({ |
| 63 |
'reward_type': 'product', |
| 64 |
'reward_product_tag_id': tag.id, |
| 65 |
'reward_product_qty': 1, |
| 66 |
'required_points': 1, |
| 67 |
'discount': None, |
| 68 |
}), |
| 69 |
Command.create({ |
| 70 |
'reward_type': 'discount', |
| 71 |
'discount': 10.0, |
| 72 |
'discount_mode': 'percent', |
| 73 |
'required_points': 1, |
| 74 |
}), |
| 75 |
], |
| 76 |
'coupon_ids': [Command.create({'points': 1})], |
| 77 |
}]) |
| 78 |
cls.coupon = cls.coupon_program.coupon_ids |
| 79 |
|
| 80 |
installed_modules = set(cls.env['ir.module.module'].search([ |
| 81 |
('state', '=', 'installed'), |
| 82 |
]).mapped('name')) |
| 83 |
for _ in http._generate_routing_rules(installed_modules, nodb_only=False): |
| 84 |
pass |
| 85 |
|
| 86 |
def test_claim_reward_with_multi_products(self): |
| 87 |
product1, product2 = self.product2, self.product2 |
| 88 |
order = self.empty_cart |
| 89 |
order.order_line = [Command.create({'product_id': product1.id})] |
| 90 |
order._update_programs_and_rewards() |
| 91 |
with MockRequest(self.env, website=self.website, sale_order_id=order.id): |
| 92 |
self.WebsiteSaleController.claim_reward( |
| 93 |
self.promo_program.reward_ids.id, |
| 94 |
product_id=str(product2.id), |
| 95 |
) |
| 96 |
self.assertEqual(len(order.order_line), 2, 'reward line should be added to order') |
| 97 |
self.assertEqual(order.order_line[1].product_id, product2, 'added reward line should should contain product 2') |
| 98 |
|
| 99 |
def test_apply_coupon_with_multiple_rewards_claim_discount(self): |
| 100 |
cart = self.empty_cart |
| 101 |
cart.update({ |
| 102 |
'partner_id': self.partner_portal.id, |
| 103 |
'order_line': [Command.create({'product_id': self.product1.id})], |
| 104 |
}) |
| 105 |
cart._update_programs_and_rewards() |
| 106 |
website = cart.website_id.with_user(self.user_portal) |
| 107 |
discount_reward = self.coupon_program.reward_ids.filtered('discount') |
| 108 |
|
| 109 |
with MockRequest(website.env, website=website, sale_order_id=cart.id): |
| 110 |
self.WebsiteSaleController.pricelist(promo=self.coupon.code) |
| 111 |
self.assertFalse(cart.order_line.reward_id) |
| 112 |
|
| 113 |
self.WebsiteSaleController.claim_reward(discount_reward.id, code=self.coupon.code) |
| 114 |
self.assertTrue(cart.order_line.reward_id) |
| 115 |
self.assertEqual( |
| 116 |
discount_reward, cart.order_line.reward_id, |
| 117 |
"Discount reward should be added to order", |
| 118 |
) |
| 119 |
self.assertAlmostEqual( |
| 120 |
cart.amount_untaxed, self.product1.list_price * 0.9, |
| 121 |
delta=cart.currency_id.rounding, |
| 122 |
msg="10% discount should be applied", |
| 123 |
) |
| 124 |
|
| 125 |
def test_apply_coupon_with_multiple_rewards_claim_multiproduct(self): |
| 126 |
cart = self.empty_cart |
| 127 |
cart.update({ |
| 128 |
'partner_id': self.partner_portal.id, |
| 129 |
'order_line': [Command.create({'product_id': self.product1.id})], |
| 130 |
}) |
| 131 |
cart._update_programs_and_rewards() |
| 132 |
website = cart.website_id.with_user(self.user_portal) |
| 133 |
multiproduct_reward = self.coupon_program.reward_ids.filtered('reward_product_tag_id') |
| 134 |
|
| 135 |
with MockRequest(website.env, website=website, sale_order_id=cart.id): |
| 136 |
self.WebsiteSaleController.pricelist(promo=self.coupon.code) |
| 137 |
self.assertFalse(cart.order_line.reward_id) |
| 138 |
|
| 139 |
self.WebsiteSaleController.claim_reward( |
| 140 |
multiproduct_reward.id, |
| 141 |
code=self.coupon.code, |
| 142 |
product_id=str(self.product1.id), |
| 143 |
) |
| 144 |
self.assertEqual( |
| 145 |
multiproduct_reward, cart.order_line.reward_id, |
| 146 |
"Product reward should be added", |
| 147 |
) |
| 148 |
self.assertIn( |
| 149 |
self.product1, cart.order_line.product_id, |
| 150 |
"Chosen reward product should be added to order", |
| 151 |
) |