| 1 |
|
| 2 |
|
| 3 |
from odoo.exceptions import ValidationError |
| 4 |
from odoo.fields import Command |
| 5 |
|
| 6 |
from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon |
| 7 |
|
| 8 |
|
| 9 |
class TestProgramWithCodeOperations(TestSaleCouponCommon): |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
super().setUpClass() |
| 16 |
|
| 17 |
cls.discount_with_multi_rewards = cls.env['loyalty.program'].create({ |
| 18 |
'name': 'Loyalty program with multiple discount rewards', |
| 19 |
'program_type': 'coupons', |
| 20 |
'reward_ids': [ |
| 21 |
Command.create({ |
| 22 |
'reward_type': 'discount', |
| 23 |
'discount_mode': 'percent', |
| 24 |
'discount': 20, |
| 25 |
}), |
| 26 |
Command.create({ |
| 27 |
'reward_type': 'discount', |
| 28 |
'discount_mode': 'percent', |
| 29 |
'discount': 5, |
| 30 |
}), |
| 31 |
], |
| 32 |
}) |
| 33 |
|
| 34 |
def test_program_usability(self): |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
self.env['loyalty.generate.wizard'].with_context(active_id=self.code_promotion_program.id).create({ |
| 43 |
'mode': 'selected', |
| 44 |
}).generate_coupons() |
| 45 |
self.assertEqual(len(self.code_promotion_program.coupon_ids), len(self.env['res.partner'].search([])), "It should have generated a coupon for every partner") |
| 46 |
|
| 47 |
def test_program_basic_operation_coupon_code(self): |
| 48 |
|
| 49 |
|
| 50 |
self.immediate_promotion_program.active = False |
| 51 |
self.code_promotion_program.reward_ids.reward_type = 'discount' |
| 52 |
self.code_promotion_program.reward_ids.discount = 10 |
| 53 |
|
| 54 |
self.env['loyalty.generate.wizard'].with_context(active_id=self.code_promotion_program.id).create({ |
| 55 |
'mode': 'selected', |
| 56 |
'customer_ids': self.partner, |
| 57 |
'points_granted': 1, |
| 58 |
}).generate_coupons() |
| 59 |
coupon = self.code_promotion_program.coupon_ids |
| 60 |
|
| 61 |
|
| 62 |
wrong_partner_order = self.env['sale.order'].create({ |
| 63 |
'partner_id': self.env['res.partner'].create({'name': 'My Partner'}).id, |
| 64 |
}) |
| 65 |
with self.assertRaises(ValidationError): |
| 66 |
self._apply_promo_code(wrong_partner_order, coupon.code) |
| 67 |
|
| 68 |
|
| 69 |
order = self.empty_order |
| 70 |
order.write({'order_line': [ |
| 71 |
(0, False, { |
| 72 |
'product_id': self.product_A.id, |
| 73 |
'name': '1 Product A', |
| 74 |
'product_uom_qty': 1.0, |
| 75 |
}) |
| 76 |
]}) |
| 77 |
self._apply_promo_code(order, coupon.code) |
| 78 |
self.assertEqual(len(order.order_line.ids), 2) |
| 79 |
|
| 80 |
|
| 81 |
order.write({'order_line': [(2, order.order_line[0].id, False)]}) |
| 82 |
order._update_programs_and_rewards() |
| 83 |
self.assertEqual(len(order.order_line.ids), 0) |
| 84 |
|
| 85 |
def test_program_coupon_double_consuming(self): |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
self.immediate_promotion_program.active = False |
| 93 |
self.code_promotion_program.applies_on = 'future' |
| 94 |
self.code_promotion_program.reward_ids.reward_type = 'discount' |
| 95 |
self.code_promotion_program.reward_ids.discount = 10 |
| 96 |
|
| 97 |
self.env['loyalty.generate.wizard'].with_context(active_id=self.code_promotion_program.id).create({ |
| 98 |
'coupon_qty': 1, |
| 99 |
'points_granted': 1, |
| 100 |
}).generate_coupons() |
| 101 |
coupon = self.code_promotion_program.coupon_ids |
| 102 |
|
| 103 |
sale_order_a = self.empty_order.copy() |
| 104 |
sale_order_b = self.empty_order.copy() |
| 105 |
|
| 106 |
sale_order_a.write({'order_line': [ |
| 107 |
(0, False, { |
| 108 |
'product_id': self.product_A.id, |
| 109 |
'name': '1 Product A', |
| 110 |
'product_uom_qty': 1.0, |
| 111 |
}) |
| 112 |
]}) |
| 113 |
self._apply_promo_code(sale_order_a, coupon.code) |
| 114 |
self.assertEqual(len(sale_order_a.order_line.ids), 2) |
| 115 |
|
| 116 |
sale_order_a._action_cancel() |
| 117 |
|
| 118 |
sale_order_b.write({'order_line': [ |
| 119 |
(0, False, { |
| 120 |
'product_id': self.product_A.id, |
| 121 |
'name': '1 Product A', |
| 122 |
'product_uom_qty': 1.0, |
| 123 |
}) |
| 124 |
]}) |
| 125 |
self._apply_promo_code(sale_order_b, coupon.code) |
| 126 |
self.assertEqual(len(sale_order_b.order_line.ids), 2) |
| 127 |
|
| 128 |
sale_order_b.action_confirm() |
| 129 |
|
| 130 |
sale_order_a.action_draft() |
| 131 |
sale_order_a.action_confirm() |
| 132 |
|
| 133 |
self.assertEqual(len(sale_order_a.order_line.ids), 1) |
| 134 |
|
| 135 |
def test_coupon_code_with_pricelist(self): |
| 136 |
|
| 137 |
|
| 138 |
self.code_promotion_program_with_discount.applies_on = 'future' |
| 139 |
self.env['loyalty.generate.wizard'].with_context(active_id=self.code_promotion_program_with_discount.id).create({ |
| 140 |
'coupon_qty': 1, |
| 141 |
'points_granted': 1, |
| 142 |
}).generate_coupons() |
| 143 |
coupon = self.code_promotion_program_with_discount.coupon_ids |
| 144 |
|
| 145 |
first_pricelist = self.env['product.pricelist'].create({ |
| 146 |
'name': 'First pricelist', |
| 147 |
'item_ids': [(0, 0, { |
| 148 |
'compute_price': 'percentage', |
| 149 |
'base': 'list_price', |
| 150 |
'percent_price': 10, |
| 151 |
'applied_on': '3_global', |
| 152 |
'name': 'First discount' |
| 153 |
})] |
| 154 |
}) |
| 155 |
|
| 156 |
order = self.empty_order |
| 157 |
order.pricelist_id = first_pricelist |
| 158 |
order.write({'order_line': [ |
| 159 |
(0, False, { |
| 160 |
'product_id': self.product_C.id, |
| 161 |
'name': '1 Product C', |
| 162 |
'product_uom_qty': 1.0, |
| 163 |
}) |
| 164 |
]}) |
| 165 |
self._apply_promo_code(order, coupon.code) |
| 166 |
self.assertEqual(len(order.order_line.ids), 2) |
| 167 |
self.assertEqual(order.amount_total, 81, "SO total should be 81: (10% of 100 with pricelist) + 10% of 90 with coupon code") |
| 168 |
|
| 169 |
def test_on_next_order_reward_promotion_program(self): |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
self.immediate_promotion_program.write({ |
| 186 |
'applies_on': 'future', |
| 187 |
'trigger': 'with_code', |
| 188 |
}) |
| 189 |
self.immediate_promotion_program.rule_ids.write({ |
| 190 |
'mode': 'with_code', |
| 191 |
'code': 'free_B_on_next_order', |
| 192 |
}) |
| 193 |
|
| 194 |
self.p1 = self.env['loyalty.program'].create({ |
| 195 |
'name': 'Code for 10% on next order', |
| 196 |
'program_type': 'promotion', |
| 197 |
'applies_on': 'future', |
| 198 |
'trigger': 'auto', |
| 199 |
'rule_ids': [(0, 0, {})], |
| 200 |
'reward_ids': [(0, 0, { |
| 201 |
'reward_type': 'discount', |
| 202 |
'discount': 10, |
| 203 |
'discount_mode': 'percent', |
| 204 |
'discount_applicability': 'order', |
| 205 |
})], |
| 206 |
}) |
| 207 |
|
| 208 |
order = self.empty_order.copy() |
| 209 |
self.third_product = self.env['product.product'].create({ |
| 210 |
'name': 'Thrid Product', |
| 211 |
'list_price': 5, |
| 212 |
'sale_ok': True |
| 213 |
}) |
| 214 |
order.write({'order_line': [ |
| 215 |
(0, False, { |
| 216 |
'product_id': self.third_product.id, |
| 217 |
'name': '1 Third Product', |
| 218 |
'product_uom_qty': 1.0, |
| 219 |
}) |
| 220 |
]}) |
| 221 |
order._update_programs_and_rewards() |
| 222 |
self.assertEqual(len(self.p1.coupon_ids.ids), 1, "You should get a coupon for you next order that will offer 10% discount") |
| 223 |
|
| 224 |
with self.assertRaises(ValidationError): |
| 225 |
self._apply_promo_code(order, 'free_B_on_next_order') |
| 226 |
|
| 227 |
order.write({'order_line': [ |
| 228 |
(0, False, { |
| 229 |
'product_id': self.product_A.id, |
| 230 |
'name': '1 Product A', |
| 231 |
'product_uom_qty': 1.0, |
| 232 |
}) |
| 233 |
]}) |
| 234 |
self._apply_promo_code(order, 'free_B_on_next_order', no_reward_fail=False) |
| 235 |
|
| 236 |
self.assertEqual(len(order._get_reward_coupons()), 2, "You should get a second coupon for your next order that will offer a free Product B") |
| 237 |
order.action_confirm() |
| 238 |
|
| 239 |
order_bis = self.empty_order |
| 240 |
|
| 241 |
|
| 242 |
order_bis.write({'order_line': [ |
| 243 |
(0, False, { |
| 244 |
'product_id': self.product_B.id, |
| 245 |
'name': '1 Product B', |
| 246 |
'product_uom_qty': 1.0, |
| 247 |
}) |
| 248 |
]}) |
| 249 |
|
| 250 |
self._apply_promo_code(order_bis, order._get_reward_coupons()[1].code) |
| 251 |
self.assertEqual(len(order_bis.order_line), 2, "You should get a free Product B") |
| 252 |
|
| 253 |
self._apply_promo_code(order_bis, order._get_reward_coupons()[0].code) |
| 254 |
self.assertEqual(len(order_bis.order_line), 3, "You should get a 10% discount line") |
| 255 |
self.assertAlmostEqual(order_bis.amount_total, order_bis.order_line[0].price_total * 0.9, 2, "SO total should be null: (Paid product - Free product = 0) + 10% of nothing") |
| 256 |
|
| 257 |
def test_on_next_order_reward_promotion_program_with_requirements(self): |
| 258 |
self.immediate_promotion_program.write({ |
| 259 |
'applies_on': 'future', |
| 260 |
'trigger': 'with_code', |
| 261 |
}) |
| 262 |
self.immediate_promotion_program.rule_ids.write({ |
| 263 |
'minimum_amount': 700, |
| 264 |
'minimum_amount_tax_mode': 'excl', |
| 265 |
'mode': 'with_code', |
| 266 |
'code': 'free_B_on_next_order', |
| 267 |
}) |
| 268 |
order = self.empty_order.copy() |
| 269 |
self.product_A.lst_price = 700 |
| 270 |
order.write({'order_line': [ |
| 271 |
(0, False, { |
| 272 |
'product_id': self.product_A.id, |
| 273 |
'name': '1 Product A', |
| 274 |
'product_uom_qty': 1.0, |
| 275 |
}) |
| 276 |
]}) |
| 277 |
self._apply_promo_code(order, 'free_B_on_next_order', no_reward_fail=False) |
| 278 |
self.assertEqual(len(self.immediate_promotion_program.coupon_ids.ids), 1, "You should get a coupon for you next order that will offer a free product B") |
| 279 |
order_bis = self.empty_order |
| 280 |
order_bis.write({'order_line': [ |
| 281 |
(0, False, { |
| 282 |
'product_id': self.product_B.id, |
| 283 |
'name': '1 Product B', |
| 284 |
'product_uom_qty': 1.0, |
| 285 |
}) |
| 286 |
]}) |
| 287 |
with self.assertRaises(ValidationError): |
| 288 |
|
| 289 |
self._apply_promo_code(order_bis, order._get_reward_coupons()[0].code) |
| 290 |
order.action_confirm() |
| 291 |
|
| 292 |
self._apply_promo_code(order_bis, order._get_reward_coupons()[0].code, no_reward_fail=False) |
| 293 |
self.assertEqual(len(order_bis.order_line), 2, "You should get 1 regular product_B and 1 free product_B") |
| 294 |
order_bis._update_programs_and_rewards() |
| 295 |
self.assertEqual(len(order_bis.order_line), 2, "Free product from a coupon generated from a promotion program on next order should not dissapear") |
| 296 |
|
| 297 |
def test_partner_assigned_to_next_order_coupon(self): |
| 298 |
""" Test the assignment of a partner on coupons with program type `next_order_coupons`. |
| 299 |
|
| 300 |
1. Create a loyalty program of type `next_order_coupons`. |
| 301 |
2. Create a sale order and add a product to it. |
| 302 |
3. Apply the loyalty program to the sale order. |
| 303 |
4. Verify that the generated coupon is assigned to the order's partner. |
| 304 |
""" |
| 305 |
loyalty_program = self.env['loyalty.program'].create({ |
| 306 |
'name': '10% Discount on Next Order', |
| 307 |
'program_type': 'next_order_coupons', |
| 308 |
'applies_on': 'future', |
| 309 |
'trigger': 'auto', |
| 310 |
'rule_ids': [Command.create({})], |
| 311 |
'reward_ids': [Command.create({ |
| 312 |
'reward_type': 'discount', |
| 313 |
'discount': 10, |
| 314 |
'discount_mode': 'percent', |
| 315 |
'discount_applicability': 'order', |
| 316 |
})], |
| 317 |
}) |
| 318 |
order = self.empty_order |
| 319 |
order.write({'order_line': [ |
| 320 |
Command.create({ |
| 321 |
'product_id': self.product_A.id, |
| 322 |
'name': '1 Product A', |
| 323 |
'product_uom_qty': 1.0, |
| 324 |
}) |
| 325 |
]}) |
| 326 |
generated_coupons = order._try_apply_program(loyalty_program).get('coupon') |
| 327 |
self.assertTrue(generated_coupons, "A coupon should have been generated") |
| 328 |
self.assertEqual(generated_coupons.partner_id, order.partner_id, |
| 329 |
"The partner should be set on the coupon with program type 'next_order_coupons'" |
| 330 |
) |
| 331 |
|
| 332 |
def test_public_partner_updated_in_next_order_coupon(self): |
| 333 |
""" Test the update of a partner on coupons with program type `next_order_coupons`. |
| 334 |
|
| 335 |
1. Create a loyalty program of type `next_order_coupons`. |
| 336 |
2. Create a sale order for a public user and add a product to it. |
| 337 |
3. Apply the loyalty program to the sale order. |
| 338 |
4. Verify that the generated coupon is assigned to the public user. |
| 339 |
5. Change the partner. |
| 340 |
6. Verify that the generated coupon was updated to this new user. |
| 341 |
""" |
| 342 |
loyalty_program = self.env['loyalty.program'].create({ |
| 343 |
'name': "10% Discount on Next Order", |
| 344 |
'program_type': 'next_order_coupons', |
| 345 |
'applies_on': 'future', |
| 346 |
'trigger': 'auto', |
| 347 |
'rule_ids': [Command.create({})], |
| 348 |
'reward_ids': [Command.create({ |
| 349 |
'reward_type': 'discount', |
| 350 |
'discount': 10, |
| 351 |
'discount_mode': 'percent', |
| 352 |
'discount_applicability': 'order', |
| 353 |
})], |
| 354 |
}) |
| 355 |
order = self.empty_order |
| 356 |
order.write({ |
| 357 |
'partner_id': self.env.ref('base.public_partner').id, |
| 358 |
'order_line': [Command.create({'product_id': self.product_A.id})], |
| 359 |
}) |
| 360 |
generated_coupons = order._try_apply_program(loyalty_program).get('coupon') |
| 361 |
self.assertTrue(generated_coupons, "A coupon should have been generated") |
| 362 |
self.assertEqual( |
| 363 |
generated_coupons.partner_id, order.partner_id, |
| 364 |
"The partner should be set on the coupon with program type 'next_order_coupons'", |
| 365 |
) |
| 366 |
self.assertTrue(generated_coupons.partner_id.is_public) |
| 367 |
|
| 368 |
|
| 369 |
order.partner_id = self.partner |
| 370 |
order._update_programs_and_rewards() |
| 371 |
self.assertEqual( |
| 372 |
generated_coupons.partner_id, self.partner, |
| 373 |
"The coupon's partner_id should be updated if it was created for a Public User", |
| 374 |
) |
| 375 |
|
| 376 |
def test_change_reward_on_confirmed_order(self): |
| 377 |
"""Check that changing rewards on a confirmed order restores points on the coupon. |
| 378 |
Tested flow: |
| 379 |
- have a coupon program with 2 discount rewards; |
| 380 |
- have confirmed order; |
| 381 |
- apply a 10% discount reward, costing 1 point; |
| 382 |
- change to a 50% discount reward, costing 5 points; |
| 383 |
- check that there are still 5 points left on the coupon. |
| 384 |
""" |
| 385 |
program = self.code_promotion_program_with_discount |
| 386 |
program.update({ |
| 387 |
'rule_ids': [Command.clear()], |
| 388 |
'reward_ids': [Command.create({ |
| 389 |
'discount': 50, |
| 390 |
'discount_mode': 'percent', |
| 391 |
'discount_applicability': 'order', |
| 392 |
'required_points': 5, |
| 393 |
})], |
| 394 |
}) |
| 395 |
discount10, discount50 = program.reward_ids |
| 396 |
|
| 397 |
self.env['loyalty.generate.wizard'].with_context(active_id=program.id).create({ |
| 398 |
'coupon_qty': 1, |
| 399 |
'points_granted': 10, |
| 400 |
}).generate_coupons() |
| 401 |
coupon = program.coupon_ids |
| 402 |
|
| 403 |
order = self.empty_order |
| 404 |
order.order_line = [Command.create({'product_id': self.product_C.id})] |
| 405 |
order.action_confirm() |
| 406 |
|
| 407 |
order.order_line.product_updatable = True |
| 408 |
order._apply_program_reward(discount10, coupon) |
| 409 |
reward_line = order.order_line.filtered('is_reward_line') |
| 410 |
self.assertEqual(order.amount_total, 90, "10% discount should be applied") |
| 411 |
self.assertEqual(coupon.points, 9, "10% discount reward should use 1 point") |
| 412 |
|
| 413 |
order.order_line.product_updatable = True |
| 414 |
order._apply_program_reward(discount50, coupon) |
| 415 |
self.assertIn(reward_line, order.order_line, "Reward line should be re-used") |
| 416 |
self.assertEqual(order.amount_total, 50, "50% discount should be applied") |
| 417 |
self.assertEqual(coupon.points, 5, "50% discount reward should use 5 points") |
| 418 |
|
| 419 |
def test_edit_and_reapply_promotion_program(self): |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
self.immediate_promotion_program.active = False |
| 427 |
|
| 428 |
self.p1 = self.env['loyalty.program'].create({ |
| 429 |
'name': 'Promo fixed amount', |
| 430 |
'trigger': 'auto', |
| 431 |
'program_type': 'promotion', |
| 432 |
'rule_ids': [(0, 0, {})], |
| 433 |
'reward_ids': [(0, 0, { |
| 434 |
'reward_type': 'discount', |
| 435 |
'discount': 10, |
| 436 |
'discount_mode': 'per_point', |
| 437 |
'discount_applicability': 'order', |
| 438 |
})] |
| 439 |
}) |
| 440 |
|
| 441 |
order = self.empty_order.copy() |
| 442 |
order.write({'order_line': [ |
| 443 |
(0, False, { |
| 444 |
'product_id': self.product_A.id, |
| 445 |
'name': '1 Product A', |
| 446 |
'product_uom_qty': 1.0, |
| 447 |
}) |
| 448 |
]}) |
| 449 |
order._update_programs_and_rewards() |
| 450 |
self._claim_reward(order, self.p1) |
| 451 |
self.assertEqual(len(order.order_line), 2, "You should get a discount line") |
| 452 |
|
| 453 |
self.p1.write({ |
| 454 |
'trigger': 'with_code', |
| 455 |
}) |
| 456 |
self.p1.rule_ids.write({ |
| 457 |
'mode': 'with_code', |
| 458 |
'code': 'test', |
| 459 |
}) |
| 460 |
order._update_programs_and_rewards() |
| 461 |
self.assertEqual(len(order.order_line), 1, "You loose a discount line") |
| 462 |
|
| 463 |
self._apply_promo_code(order, 'test') |
| 464 |
|
| 465 |
self.assertEqual(len(order.order_line), 2, "You should get a discount line") |
| 466 |
|
| 467 |
def test_reapply_multiple_global_rewards_when_new_discount_greater(self): |
| 468 |
""" Test applying the maximum reward discount from multiple rewards when the applied |
| 469 |
coupon discount is lower. |
| 470 |
|
| 471 |
1. Create a two loyalty program of type `coupons`. |
| 472 |
2. Add multiple rewards to the second program. |
| 473 |
3. Generate a coupon for each program. |
| 474 |
2. Create a sale order and add a product to it. |
| 475 |
3. Apply the Coupon to the sale order. |
| 476 |
4. Try to apply the second coupon with multiple rewards. |
| 477 |
5. Reward with best discount will be shown. |
| 478 |
""" |
| 479 |
self.code_promotion_program_with_discount.rule_ids.unlink() |
| 480 |
coupon_1 = self._generate_coupons(self.code_promotion_program_with_discount) |
| 481 |
coupon_2 = self._generate_coupons(self.discount_with_multi_rewards) |
| 482 |
|
| 483 |
order = self.empty_order |
| 484 |
self.assertEqual(order.amount_total, 0.0) |
| 485 |
order.write({'order_line': [ |
| 486 |
Command.create({ |
| 487 |
'product_id': self.product_A.id, |
| 488 |
'name': '1 Product A', |
| 489 |
'product_uom_qty': 1.0, |
| 490 |
}), |
| 491 |
Command.create({ |
| 492 |
'product_id': self.product_B.id, |
| 493 |
'name': '1 Product B', |
| 494 |
'product_uom_qty': 1.0, |
| 495 |
}), |
| 496 |
]}) |
| 497 |
|
| 498 |
|
| 499 |
self.assertEqual(len(order.order_line.ids), 2) |
| 500 |
self.assertEqual(order.order_line[0].price_unit, 100.0) |
| 501 |
self.assertEqual(order.order_line[1].price_unit, 5.0) |
| 502 |
expected_total = order.amount_total * 0.80 |
| 503 |
|
| 504 |
|
| 505 |
self._apply_promo_code(order, coupon_1.code) |
| 506 |
self.assertEqual(len(order.order_line.ids), 3) |
| 507 |
msg = "The discount line should be the 10% discount on the sale order total." |
| 508 |
self.assertEqual(order.order_line[2].price_unit, -10.5, msg=msg) |
| 509 |
|
| 510 |
|
| 511 |
self._apply_promo_code(order, coupon_2.code) |
| 512 |
self.assertEqual(len(order.order_line.ids), 3) |
| 513 |
msg = "The discount line should be the 20% discount on the sale order total." |
| 514 |
self.assertEqual(order.order_line[2].price_unit, -21.0, msg=msg) |
| 515 |
msg = "Order total should reflect the 20% discount" |
| 516 |
self.assertAlmostEqual(order.amount_total, expected_total, msg=msg) |
| 517 |
|
| 518 |
def test_reapply_multiple_higher_global_rewards_lets_choose_best(self): |
| 519 |
""" Test applying the maximum reward discount from multiple rewards when the applied |
| 520 |
coupon discount is lower. |
| 521 |
|
| 522 |
1. Create a two loyalty program of type `coupons`. |
| 523 |
2. Add multiple rewards of higher discount than the applied discount. |
| 524 |
3. Generate a coupon for each program. |
| 525 |
2. Create a sale order and add a product to it. |
| 526 |
3. Apply the Coupon to the sale order. |
| 527 |
4. Try to apply the second coupon with multiple rewards. |
| 528 |
5. Reward with max discount will be shown. |
| 529 |
""" |
| 530 |
self.code_promotion_program_with_discount.rule_ids.unlink() |
| 531 |
coupon_1 = self._generate_coupons(self.code_promotion_program_with_discount) |
| 532 |
self.discount_with_multi_rewards.reward_ids[1].discount = 15 |
| 533 |
coupon_2 = self._generate_coupons(self.discount_with_multi_rewards) |
| 534 |
|
| 535 |
order = self.empty_order |
| 536 |
self.assertEqual(order.amount_total, 0.0) |
| 537 |
order.write({'order_line': [ |
| 538 |
Command.create({ |
| 539 |
'product_id': self.product_A.id, |
| 540 |
'name': '1 Product A', |
| 541 |
'product_uom_qty': 1.0, |
| 542 |
}) |
| 543 |
]}) |
| 544 |
|
| 545 |
|
| 546 |
self.assertEqual(len(order.order_line.ids), 1) |
| 547 |
self.assertEqual(order.order_line[0].price_unit, 100.0) |
| 548 |
expected_total = order.amount_total * 0.80 |
| 549 |
|
| 550 |
|
| 551 |
self._apply_promo_code(order, coupon_1.code) |
| 552 |
self.assertEqual(len(order.order_line.ids), 2) |
| 553 |
msg = "The discount line should be the 10% discount on the sale order total." |
| 554 |
self.assertEqual(order.order_line[1].price_unit, -10.0, msg=msg) |
| 555 |
|
| 556 |
|
| 557 |
rewards = self._apply_promo_code(order, coupon_2.code) |
| 558 |
self.assertEqual(len(rewards), 2) |
| 559 |
|
| 560 |
|
| 561 |
chosen_reward = rewards.filtered(lambda r: r.discount == 20) |
| 562 |
order._apply_program_reward(chosen_reward, coupon_2) |
| 563 |
self.assertEqual(len(order.order_line), 2) |
| 564 |
msg = "The discount line should be the 20% discount on the sale order total." |
| 565 |
self.assertEqual(order.order_line[1].price_unit, -20.0, msg=msg) |
| 566 |
msg = "Order total should reflect the 20% discount" |
| 567 |
self.assertAlmostEqual(order.amount_total, expected_total, msg=msg) |
| 568 |
|
| 569 |
def test_reapplying_new_multiple_lower_global_rewards_discount_raise_validation(self): |
| 570 |
""" Test raising validation when the new coupon discount from multiple rewards |
| 571 |
is less than the applied coupon discount. |
| 572 |
|
| 573 |
1. Create a two loyalty program of type `coupons`. |
| 574 |
2. Add multiple rewards to the second program. |
| 575 |
3. Generate a coupon for each program. |
| 576 |
2. Create a sale order and add a product to it. |
| 577 |
3. Apply the Coupon to the sale order. |
| 578 |
4. Try to apply the second coupon with multiple rewards. |
| 579 |
5. Verify that it raises a validation error. |
| 580 |
""" |
| 581 |
self.code_promotion_program_with_discount.rule_ids.unlink() |
| 582 |
coupon_1 = self._generate_coupons(self.code_promotion_program_with_discount) |
| 583 |
self.discount_with_multi_rewards.reward_ids[0].discount = 7 |
| 584 |
coupon_2 = self._generate_coupons(self.discount_with_multi_rewards) |
| 585 |
|
| 586 |
order = self.empty_order |
| 587 |
self.assertEqual(order.amount_total, 0.0) |
| 588 |
order.write({'order_line': [ |
| 589 |
Command.create({ |
| 590 |
'product_id': self.product_A.id, |
| 591 |
'name': '1 Product A', |
| 592 |
'product_uom_qty': 1.0, |
| 593 |
}) |
| 594 |
]}) |
| 595 |
|
| 596 |
|
| 597 |
self.assertEqual(len(order.order_line.ids), 1) |
| 598 |
self.assertEqual(order.order_line[0].price_unit, 100.0) |
| 599 |
|
| 600 |
|
| 601 |
self._apply_promo_code(order, coupon_1.code) |
| 602 |
msg = "The discount line should be the 10% discount on the sale order total." |
| 603 |
self.assertEqual(order.order_line[1].price_unit, -10.0, msg=msg) |
| 604 |
self.assertEqual(len(order.order_line.ids), 2) |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
msg = "The new coupon discount should be greater than the applied coupon discount" |
| 609 |
with self.assertRaises(ValidationError, msg=msg): |
| 610 |
self._apply_promo_code(order, coupon_2.code) |