| 1 |
|
| 2 |
|
| 3 |
from odoo.exceptions import ValidationError |
| 4 |
from odoo.fields import Command |
| 5 |
from odoo.tests import new_test_user, tagged |
| 6 |
from odoo.tools.float_utils import float_compare |
| 7 |
|
| 8 |
from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon |
| 9 |
|
| 10 |
|
| 11 |
@tagged('post_install', '-at_install') |
| 12 |
class TestLoyalty(TestSaleCouponCommon): |
| 13 |
|
| 14 |
@classmethod |
| 15 |
def setUpClass(cls): |
| 16 |
super().setUpClass() |
| 17 |
cls.env['loyalty.program'].search([]).write({'active': False}) |
| 18 |
|
| 19 |
cls.product_a, cls.product_b = cls.env['product.product'].create([ |
| 20 |
{ |
| 21 |
'name': 'Product C', |
| 22 |
'list_price': 100, |
| 23 |
'sale_ok': True, |
| 24 |
'taxes_id': [Command.set([])], |
| 25 |
}, |
| 26 |
{ |
| 27 |
'name': "Product B", |
| 28 |
'sale_ok': True, |
| 29 |
} |
| 30 |
]) |
| 31 |
|
| 32 |
cls.ewallet_program = cls.env['loyalty.program'].create({ |
| 33 |
'name': 'eWallet Program', |
| 34 |
'program_type': 'ewallet', |
| 35 |
'trigger': 'auto', |
| 36 |
'applies_on': 'future', |
| 37 |
'reward_ids': [Command.create({ |
| 38 |
'reward_type': 'discount', |
| 39 |
'discount_mode': 'per_point', |
| 40 |
'discount': 1, |
| 41 |
})], |
| 42 |
'rule_ids': [Command.create({ |
| 43 |
'reward_point_amount': '1', |
| 44 |
'reward_point_mode': 'money', |
| 45 |
'product_ids': cls.env.ref('loyalty.ewallet_product_50'), |
| 46 |
})], |
| 47 |
'trigger_product_ids': cls.env.ref('loyalty.ewallet_product_50'), |
| 48 |
}) |
| 49 |
|
| 50 |
cls.ewallet = cls.env['loyalty.card'].create({ |
| 51 |
'program_id': cls.ewallet_program.id, |
| 52 |
'partner_id': cls.partner.id, |
| 53 |
'points': 10, |
| 54 |
}) |
| 55 |
cls.ewallet_program.coupon_ids = [Command.set([cls.ewallet.id])] |
| 56 |
|
| 57 |
cls.user_salemanager = new_test_user(cls.env, login='user_salemanager', groups='sales_team.group_sale_manager') |
| 58 |
|
| 59 |
cls.promotion_code_10pc = cls.env['loyalty.program'].create({ |
| 60 |
'name': "Code for 10% on orders", |
| 61 |
'trigger': 'with_code', |
| 62 |
'program_type': 'promotion', |
| 63 |
'applies_on': 'current', |
| 64 |
'rule_ids': [Command.create({ |
| 65 |
'mode': 'with_code', |
| 66 |
'code': 'test_10pc', |
| 67 |
})], |
| 68 |
'reward_ids': [Command.create({ |
| 69 |
'reward_type': 'discount', |
| 70 |
'discount_mode': 'percent', |
| 71 |
'discount': 10, |
| 72 |
'discount_applicability': 'order', |
| 73 |
'required_points': 1, |
| 74 |
})], |
| 75 |
}) |
| 76 |
|
| 77 |
def test_nominative_programs(self): |
| 78 |
loyalty_program = self.env['loyalty.program'].create({ |
| 79 |
'name': 'Loyalty Program', |
| 80 |
'program_type': 'loyalty', |
| 81 |
'trigger': 'auto', |
| 82 |
'applies_on': 'both', |
| 83 |
'rule_ids': [(0, 0, { |
| 84 |
'reward_point_mode': 'unit', |
| 85 |
'reward_point_amount': 1, |
| 86 |
'product_ids': [self.product_a.id], |
| 87 |
})], |
| 88 |
'reward_ids': [(0, 0, { |
| 89 |
'reward_type': 'discount', |
| 90 |
'discount': 1.5, |
| 91 |
'discount_mode': 'per_point', |
| 92 |
'discount_applicability': 'order', |
| 93 |
'required_points': 3, |
| 94 |
})], |
| 95 |
}) |
| 96 |
|
| 97 |
order = self.empty_order |
| 98 |
order._update_programs_and_rewards() |
| 99 |
claimable_rewards = order._get_claimable_rewards() |
| 100 |
|
| 101 |
self.assertFalse(claimable_rewards, "No program should be applicable") |
| 102 |
loyalty_card = self.env['loyalty.card'].create({ |
| 103 |
'program_id': loyalty_program.id, |
| 104 |
'partner_id': self.partner.id, |
| 105 |
'points': 10, |
| 106 |
}) |
| 107 |
self.ewallet.points = 0 |
| 108 |
|
| 109 |
order.write({ |
| 110 |
'order_line': [(0, 0, { |
| 111 |
'product_id': self.product_a.id, |
| 112 |
'product_uom_qty': 1, |
| 113 |
})] |
| 114 |
}) |
| 115 |
order._update_programs_and_rewards() |
| 116 |
claimable_rewards = order._get_claimable_rewards() |
| 117 |
self.assertEqual(len(claimable_rewards), 1, "The ewallet program should not be applicable since the card has no points.") |
| 118 |
vals = order._get_reward_values_discount(loyalty_program.reward_ids[0], loyalty_card) |
| 119 |
self.assertEqual( |
| 120 |
vals[0]['points_cost'] % loyalty_program.reward_ids.required_points, |
| 121 |
0, |
| 122 |
"Can only use a whole number of required points", |
| 123 |
) |
| 124 |
self.assertEqual(vals[0]['points_cost'], 9, "Use maximum available points for the reward") |
| 125 |
self.ewallet.points = 50 |
| 126 |
order._update_programs_and_rewards() |
| 127 |
claimable_rewards = order._get_claimable_rewards() |
| 128 |
self.assertEqual(len(claimable_rewards), 2, "Now that the ewallet has some points they should both be applicable.") |
| 129 |
|
| 130 |
def test_cancel_order_with_coupons(self): |
| 131 |
"""This test ensure that creating an order with coupons will not |
| 132 |
raise an access error on POS line modele when canceling the order.""" |
| 133 |
|
| 134 |
self.env['loyalty.program'].create({ |
| 135 |
'name': '10% Discount', |
| 136 |
'program_type': 'coupons', |
| 137 |
'applies_on': 'current', |
| 138 |
'trigger': 'auto', |
| 139 |
'rule_ids': [(0, 0, {})], |
| 140 |
'reward_ids': [(0, 0, { |
| 141 |
'reward_type': 'discount', |
| 142 |
'discount': 10, |
| 143 |
'discount_mode': 'percent', |
| 144 |
'discount_applicability': 'order', |
| 145 |
})] |
| 146 |
}) |
| 147 |
|
| 148 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 149 |
'partner_id': self.partner.id, |
| 150 |
'order_line': [ |
| 151 |
(0, 0, { |
| 152 |
'product_id': self.product_a.id, |
| 153 |
}) |
| 154 |
] |
| 155 |
}) |
| 156 |
|
| 157 |
order._update_programs_and_rewards() |
| 158 |
self.assertTrue(order.coupon_point_ids) |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
order._action_cancel() |
| 165 |
self.assertFalse(order.coupon_point_ids) |
| 166 |
|
| 167 |
def test_distribution_amount_payment_programs(self): |
| 168 |
""" |
| 169 |
Check how the amount of a payment reward is distributed. |
| 170 |
An ewallet should not be used to refund taxes. |
| 171 |
Its amount must be distributed between the products. |
| 172 |
""" |
| 173 |
|
| 174 |
|
| 175 |
product_a, product_b = self.env['product.product'].create([ |
| 176 |
{ |
| 177 |
'name': 'Product A', |
| 178 |
'list_price': 100, |
| 179 |
'sale_ok': True, |
| 180 |
'taxes_id': [Command.set(self.tax_15pc_excl.ids)], |
| 181 |
}, |
| 182 |
{ |
| 183 |
'name': 'Product B', |
| 184 |
'list_price': 100, |
| 185 |
'sale_ok': True, |
| 186 |
'taxes_id': [Command.set(self.tax_15pc_excl.ids)], |
| 187 |
}, |
| 188 |
]) |
| 189 |
|
| 190 |
|
| 191 |
coupon_program, ewallet_program = self.env['loyalty.program'].create([ |
| 192 |
{ |
| 193 |
'name': 'Coupon Program', |
| 194 |
'program_type': 'coupons', |
| 195 |
'trigger': 'with_code', |
| 196 |
'applies_on': 'both', |
| 197 |
'reward_ids': [Command.create({ |
| 198 |
'reward_type': 'discount', |
| 199 |
'discount': 100.0, |
| 200 |
'discount_applicability': 'specific', |
| 201 |
'discount_product_domain': '[("name", "=", "Product A")]', |
| 202 |
})], |
| 203 |
}, |
| 204 |
{ |
| 205 |
'name': 'eWallet Program', |
| 206 |
'program_type': 'ewallet', |
| 207 |
'applies_on': 'future', |
| 208 |
'trigger': 'auto', |
| 209 |
'rule_ids': [Command.create({ |
| 210 |
'reward_point_mode': 'money', |
| 211 |
})], |
| 212 |
'reward_ids': [Command.create({ |
| 213 |
'discount_mode': 'per_point', |
| 214 |
'discount': 1, |
| 215 |
'discount_applicability': 'order', |
| 216 |
})], |
| 217 |
} |
| 218 |
]) |
| 219 |
|
| 220 |
coupon_partner, _ = self.env['loyalty.card'].create([ |
| 221 |
{ |
| 222 |
'program_id': coupon_program.id, |
| 223 |
'partner_id': self.partner.id, |
| 224 |
'points': 1, |
| 225 |
'code': '5555', |
| 226 |
}, |
| 227 |
{ |
| 228 |
'program_id': ewallet_program.id, |
| 229 |
'partner_id': self.partner.id, |
| 230 |
'points': 115, |
| 231 |
}, |
| 232 |
]) |
| 233 |
|
| 234 |
|
| 235 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 236 |
'partner_id': self.partner.id, |
| 237 |
'order_line': [ |
| 238 |
Command.create({ |
| 239 |
'product_id': product_a.id, |
| 240 |
}), |
| 241 |
Command.create({ |
| 242 |
'product_id': product_b.id, |
| 243 |
}), |
| 244 |
] |
| 245 |
}) |
| 246 |
|
| 247 |
self.assertEqual(order.amount_total, 230.0) |
| 248 |
self.assertEqual(order.amount_untaxed, 200.0) |
| 249 |
self.assertEqual(order.amount_tax, 30.0) |
| 250 |
|
| 251 |
|
| 252 |
order._update_programs_and_rewards() |
| 253 |
self._claim_reward(order, ewallet_program) |
| 254 |
|
| 255 |
self.assertEqual(order.amount_total, 115.0) |
| 256 |
self.assertEqual(order.amount_untaxed, 85.0) |
| 257 |
self.assertEqual(order.amount_tax, 30.0) |
| 258 |
self.assertEqual(order.reward_amount, -115.0) |
| 259 |
|
| 260 |
|
| 261 |
self._apply_promo_code(order, coupon_partner.code) |
| 262 |
|
| 263 |
self.assertEqual(order.amount_total, 0.0) |
| 264 |
self.assertEqual(order.amount_untaxed, -15.0) |
| 265 |
self.assertEqual(order.amount_tax, 15.0) |
| 266 |
self.assertEqual(order.reward_amount, -215.0) |
| 267 |
|
| 268 |
def test_discount_max_amount_on_specific_product(self): |
| 269 |
product_a = self.product_A |
| 270 |
product_b = self.product_B |
| 271 |
product_a.write({'taxes_id': [Command.set(self.tax_20pc_excl.ids)]}) |
| 272 |
product_b.write({'list_price': -20, 'taxes_id': [Command.set(self.tax_20pc_excl.ids)]}) |
| 273 |
|
| 274 |
self.env['loyalty.program'].search([]).write({'active': False}) |
| 275 |
promotion = self.env['loyalty.program'].create({ |
| 276 |
'name': '10% Discount', |
| 277 |
'program_type': 'promotion', |
| 278 |
'trigger': 'auto', |
| 279 |
'rule_ids': [Command.create({'reward_point_amount': 1, 'reward_point_mode': 'unit'})], |
| 280 |
'reward_ids': [Command.create({ |
| 281 |
'discount': 10.0, |
| 282 |
'discount_max_amount': 9, |
| 283 |
'discount_applicability': 'specific', |
| 284 |
'discount_product_ids': [product_a.id], |
| 285 |
})], |
| 286 |
}) |
| 287 |
|
| 288 |
order = self.env['sale.order'].create({ |
| 289 |
'partner_id': self.partner.id, |
| 290 |
'order_line': [Command.create({'product_id': product_a.id})], |
| 291 |
}) |
| 292 |
self.assertEqual(order.reward_amount, 0) |
| 293 |
|
| 294 |
self._auto_rewards(order, promotion) |
| 295 |
reward_amount_tax_included = sum(l.price_total for l in order.order_line if l.reward_id) |
| 296 |
msg = "Max discount amount reached, the reward amount should be the max amount value." |
| 297 |
self.assertEqual(reward_amount_tax_included, -9, msg) |
| 298 |
|
| 299 |
order.order_line = [Command.clear(), Command.create({'product_id': product_b.id})] |
| 300 |
self._auto_rewards(order, promotion) |
| 301 |
reward_amount_tax_included = sum(l.price_total for l in order.order_line if l.reward_id) |
| 302 |
msg = "This product is not eligible to the discount." |
| 303 |
self.assertEqual(reward_amount_tax_included, 0, msg=msg) |
| 304 |
|
| 305 |
order.order_line = [ |
| 306 |
Command.clear(), |
| 307 |
Command.create({'product_id': product_a.id}), |
| 308 |
Command.create({'product_id': product_b.id}), |
| 309 |
] |
| 310 |
self._auto_rewards(order, promotion) |
| 311 |
reward_amount_tax_included = sum(l.price_total for l in order.order_line if l.reward_id) |
| 312 |
msg = "Reward amount above the max amount, the reward should be the max amount value." |
| 313 |
self.assertEqual(reward_amount_tax_included, -9, msg) |
| 314 |
|
| 315 |
order.order_line = [ |
| 316 |
Command.clear(), |
| 317 |
Command.create({'product_id': product_a.id}), |
| 318 |
Command.create({'product_id': product_b.id, 'price_unit': -95}), |
| 319 |
] |
| 320 |
self._auto_rewards(order, promotion) |
| 321 |
reward_amount_tax_included = sum(l.price_total for l in order.order_line if l.reward_id) |
| 322 |
msg = "Reward amount should never surpass the order's current total amount." |
| 323 |
self.assertEqual(reward_amount_tax_included, -6, msg) |
| 324 |
|
| 325 |
order.order_line = [ |
| 326 |
Command.clear(), |
| 327 |
Command.create({'product_id': product_a.id, 'price_unit': 50}), |
| 328 |
Command.create({'product_id': product_b.id, 'price_unit': -5}), |
| 329 |
] |
| 330 |
self._auto_rewards(order, promotion) |
| 331 |
reward_amount_tax_included = sum(l.price_total for l in order.order_line if l.reward_id) |
| 332 |
msg = "Reward amount should be the percentage one if under the max amount discount." |
| 333 |
self.assertEqual(reward_amount_tax_included, -6, msg) |
| 334 |
|
| 335 |
def test_points_awarded_global_discount_code_no_domain_program(self): |
| 336 |
""" |
| 337 |
Check the calculation for points awarded when there is a global discount applied and the |
| 338 |
loyalty program applies on all products (no domain). |
| 339 |
""" |
| 340 |
LoyaltyProgram = self.env['loyalty.program'] |
| 341 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 342 |
|
| 343 |
loyalty_card = self.env['loyalty.card'].create({ |
| 344 |
'program_id': loyalty_program.id, |
| 345 |
'partner_id': self.partner.id, |
| 346 |
'points': 0, |
| 347 |
}) |
| 348 |
|
| 349 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 350 |
'partner_id': self.partner.id, |
| 351 |
'order_line': [ |
| 352 |
Command.create({ |
| 353 |
'product_id': self.product_A.id, |
| 354 |
'tax_ids': False, |
| 355 |
}), |
| 356 |
] |
| 357 |
}) |
| 358 |
|
| 359 |
promotion_program = self.env['loyalty.program'].create([{ |
| 360 |
'name': "Coupon Program", |
| 361 |
'program_type': 'promotion', |
| 362 |
'trigger': 'auto', |
| 363 |
'applies_on': 'current', |
| 364 |
'rule_ids': [Command.create({ |
| 365 |
'reward_point_amount': 1, |
| 366 |
'reward_point_mode': 'order', |
| 367 |
'minimum_amount': 10, |
| 368 |
})], |
| 369 |
'reward_ids': [Command.create({ |
| 370 |
'reward_type': 'discount', |
| 371 |
'discount': 10.0, |
| 372 |
'discount_applicability': 'order', |
| 373 |
'required_points': 1, |
| 374 |
})], |
| 375 |
}]) |
| 376 |
|
| 377 |
self.assertEqual(order.amount_total, 100) |
| 378 |
self._auto_rewards(order, promotion_program) |
| 379 |
self.assertEqual(order.amount_total, 90) |
| 380 |
order.action_confirm() |
| 381 |
self.assertEqual(loyalty_card.points, 90) |
| 382 |
|
| 383 |
def test_multiple_rewards_after_confirm(self): |
| 384 |
""" |
| 385 |
Check that multiple rewards from a loyalty promotion program are correctly applied to a SO |
| 386 |
after its confirmation by asserting that: |
| 387 |
- Both rewards are applied to the order lines. |
| 388 |
- The total points cost matches the rule's requirement. |
| 389 |
- The coupon's points are fully consumed after applying the rewards. |
| 390 |
""" |
| 391 |
promo_program = self.env['loyalty.program'].create({ |
| 392 |
'name': 'Multiple Rewards Promotion', |
| 393 |
'program_type': 'promotion', |
| 394 |
'applies_on': 'current', |
| 395 |
'company_id': self.env.company.id, |
| 396 |
'trigger': 'auto', |
| 397 |
'rule_ids': [ |
| 398 |
Command.create({ |
| 399 |
'product_ids': self.product_A, |
| 400 |
'reward_point_amount': 1, |
| 401 |
'reward_point_mode': 'order', |
| 402 |
'minimum_qty': 1, |
| 403 |
}), |
| 404 |
], |
| 405 |
'reward_ids': [ |
| 406 |
Command.create({ |
| 407 |
'discount': 10, |
| 408 |
'discount_applicability': 'specific', |
| 409 |
'discount_product_ids': [self.product_A.id], |
| 410 |
'required_points': 0.5, |
| 411 |
}), |
| 412 |
Command.create({ |
| 413 |
'discount': 15, |
| 414 |
'discount_applicability': 'specific', |
| 415 |
'discount_product_ids': [self.product_B.id], |
| 416 |
'required_points': 0.5, |
| 417 |
}), |
| 418 |
], |
| 419 |
}) |
| 420 |
|
| 421 |
order = self.empty_order |
| 422 |
order.order_line = [ |
| 423 |
Command.create({'product_id': self.product_A.id, 'product_uom_qty': 1}), |
| 424 |
Command.create({'product_id': self.product_B.id, 'product_uom_qty': 1}), |
| 425 |
] |
| 426 |
order.action_confirm() |
| 427 |
|
| 428 |
order._update_programs_and_rewards() |
| 429 |
coupon = order.coupon_point_ids.coupon_id.filtered(lambda c: c.program_id == promo_program) |
| 430 |
reward1, reward2 = rewards = promo_program.reward_ids |
| 431 |
order._apply_program_reward(reward1, coupon) |
| 432 |
order._apply_program_reward(reward2, coupon) |
| 433 |
|
| 434 |
self.assertEqual(order.order_line.reward_id, rewards, "All rewards should be applied") |
| 435 |
self.assertEqual(sum(order.order_line.mapped('points_cost')), 1) |
| 436 |
self.assertEqual(coupon.points, 0) |
| 437 |
|
| 438 |
def test_points_awarded_discount_code_no_domain_program(self): |
| 439 |
""" |
| 440 |
Check the calculation for points awarded when there is a discount coupon applied and the |
| 441 |
loyalty program applies on all products (no domain). |
| 442 |
""" |
| 443 |
LoyaltyProgram = self.env['loyalty.program'] |
| 444 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 445 |
loyalty_card = self.env['loyalty.card'].create({ |
| 446 |
'program_id': loyalty_program.id, |
| 447 |
'partner_id': self.partner.id, |
| 448 |
'points': 0, |
| 449 |
}) |
| 450 |
|
| 451 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 452 |
'partner_id': self.partner.id, |
| 453 |
'order_line': [ |
| 454 |
Command.create({ |
| 455 |
'product_id': self.product_A.id, |
| 456 |
'tax_ids': False, |
| 457 |
}), |
| 458 |
] |
| 459 |
}) |
| 460 |
|
| 461 |
self.assertEqual(order.amount_total, 100) |
| 462 |
self._apply_promo_code(order, "test_10pc") |
| 463 |
self.assertEqual(order.amount_total, 90) |
| 464 |
order.action_confirm() |
| 465 |
self.assertEqual(loyalty_card.points, 90) |
| 466 |
|
| 467 |
def test_points_awarded_general_discount_code_specific_domain_program(self): |
| 468 |
""" |
| 469 |
Check the calculation for points awarded when there is a discount coupon applied and the |
| 470 |
loyalty program applies on a specific domain. The discount code has no domain. The product |
| 471 |
related to that discount is not in the domain of the loyalty program. |
| 472 |
Expected behavior: The discount is not included in the computation of points |
| 473 |
""" |
| 474 |
product_category_food = self.env['product.category'].create({ |
| 475 |
'name': "Food", |
| 476 |
}) |
| 477 |
|
| 478 |
self.product_A.categ_id = product_category_food |
| 479 |
self.product_B.list_price = 50 |
| 480 |
|
| 481 |
LoyaltyProgram = self.env['loyalty.program'] |
| 482 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 483 |
loyalty_program.rule_ids.product_category_id = product_category_food.id |
| 484 |
loyalty_card = self.env['loyalty.card'].create({ |
| 485 |
'program_id': loyalty_program.id, |
| 486 |
'partner_id': self.partner.id, |
| 487 |
'points': 0, |
| 488 |
}) |
| 489 |
|
| 490 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 491 |
'partner_id': self.partner.id, |
| 492 |
'order_line': [ |
| 493 |
Command.create({ |
| 494 |
'product_id': self.product_A.id, |
| 495 |
'tax_ids': False, |
| 496 |
}), |
| 497 |
Command.create({ |
| 498 |
'product_id': self.product_B.id, |
| 499 |
'tax_ids': False, |
| 500 |
}), |
| 501 |
] |
| 502 |
}) |
| 503 |
|
| 504 |
self.assertEqual(order.amount_total, 150) |
| 505 |
self._apply_promo_code(order, "test_10pc") |
| 506 |
self.assertEqual(order.amount_total, 135) |
| 507 |
order.action_confirm() |
| 508 |
self.assertEqual(loyalty_card.points, 100) |
| 509 |
|
| 510 |
def test_points_awarded_specific_discount_code_specific_domain_program(self): |
| 511 |
""" |
| 512 |
Check the calculation for points awarded when there is a discount coupon applied and the |
| 513 |
loyalty program applies on a specific domain. The discount code has the same domain as the |
| 514 |
loyalty program. The product related to that discount code is set up to be included in the |
| 515 |
domain of the loyalty program. |
| 516 |
Expected behavior: The discount is included in the computation of points |
| 517 |
""" |
| 518 |
product_category_food = self.env['product.category'].create({ |
| 519 |
'name': "Food", |
| 520 |
}) |
| 521 |
|
| 522 |
self.product_A.categ_id = product_category_food |
| 523 |
self.product_B.list_price = 50 |
| 524 |
|
| 525 |
LoyaltyProgram = self.env['loyalty.program'] |
| 526 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 527 |
loyalty_program.rule_ids.product_category_id = product_category_food.id |
| 528 |
loyalty_card = self.env['loyalty.card'].create({ |
| 529 |
'program_id': loyalty_program.id, |
| 530 |
'partner_id': self.partner.id, |
| 531 |
'points': 0, |
| 532 |
}) |
| 533 |
|
| 534 |
self.promotion_code_10pc.rule_ids.product_category_id = product_category_food.id |
| 535 |
self.promotion_code_10pc.reward_ids.discount_applicability = 'specific' |
| 536 |
self.promotion_code_10pc.reward_ids.discount_product_category_id = product_category_food.id |
| 537 |
|
| 538 |
discount_product = self.env['product.product'].search([('id', '=', self.promotion_code_10pc.reward_ids.discount_line_product_id.id)]) |
| 539 |
discount_product.categ_id = product_category_food.id |
| 540 |
|
| 541 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 542 |
'partner_id': self.partner.id, |
| 543 |
'order_line': [ |
| 544 |
Command.create({ |
| 545 |
'product_id': self.product_A.id, |
| 546 |
'tax_ids': False, |
| 547 |
}), |
| 548 |
Command.create({ |
| 549 |
'product_id': self.product_B.id, |
| 550 |
'tax_ids': False, |
| 551 |
}), |
| 552 |
] |
| 553 |
}) |
| 554 |
|
| 555 |
self.assertEqual(order.amount_total, 150) |
| 556 |
self._apply_promo_code(order, "test_10pc") |
| 557 |
self.assertEqual(order.amount_total, 140) |
| 558 |
order.action_confirm() |
| 559 |
self.assertEqual(loyalty_card.points, 90) |
| 560 |
|
| 561 |
def test_points_awarded_ewallet(self): |
| 562 |
""" |
| 563 |
Check the calculation for point awarded when using ewallet |
| 564 |
""" |
| 565 |
LoyaltyProgram = self.env['loyalty.program'] |
| 566 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 567 |
loyalty_card = self.env['loyalty.card'].create({ |
| 568 |
'program_id': loyalty_program.id, |
| 569 |
'partner_id': self.partner.id, |
| 570 |
'points': 0, |
| 571 |
}) |
| 572 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 573 |
'partner_id': self.partner.id, |
| 574 |
'order_line': [ |
| 575 |
Command.create({ |
| 576 |
'product_id': self.product_A.id, |
| 577 |
'tax_ids': False, |
| 578 |
}), |
| 579 |
] |
| 580 |
}) |
| 581 |
|
| 582 |
self.assertEqual(order.amount_total, 100) |
| 583 |
order._update_programs_and_rewards() |
| 584 |
self._claim_reward(order, self.ewallet_program, coupon=self.ewallet) |
| 585 |
self.assertEqual(order.amount_total, 90) |
| 586 |
order.action_confirm() |
| 587 |
self.assertEqual(loyalty_card.points, 100) |
| 588 |
|
| 589 |
def test_points_awarded_giftcard(self): |
| 590 |
""" |
| 591 |
Check the calculation for point awarded when using a gift card |
| 592 |
""" |
| 593 |
LoyaltyProgram = self.env['loyalty.program'] |
| 594 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 595 |
loyalty_card = self.env['loyalty.card'].create({ |
| 596 |
'program_id': loyalty_program.id, |
| 597 |
'partner_id': self.partner.id, |
| 598 |
'points': 0, |
| 599 |
}) |
| 600 |
|
| 601 |
program_gift_card = self.env['loyalty.program'].create({ |
| 602 |
'name': "Gift Cards", |
| 603 |
'applies_on': 'future', |
| 604 |
'program_type': 'gift_card', |
| 605 |
'trigger': 'auto', |
| 606 |
'reward_ids': [(0, 0, { |
| 607 |
'reward_type': 'discount', |
| 608 |
'discount': 1, |
| 609 |
'discount_mode': 'per_point', |
| 610 |
'discount_applicability': 'order', |
| 611 |
})] |
| 612 |
}) |
| 613 |
|
| 614 |
self.env['loyalty.generate.wizard'].with_context(active_id=program_gift_card.id).create({ |
| 615 |
'coupon_qty': 1, |
| 616 |
'points_granted': 50, |
| 617 |
}).generate_coupons() |
| 618 |
gift_card = program_gift_card.coupon_ids[0] |
| 619 |
|
| 620 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 621 |
'partner_id': self.partner.id, |
| 622 |
'order_line': [ |
| 623 |
Command.create({ |
| 624 |
'product_id': self.product_A.id, |
| 625 |
'tax_ids': False, |
| 626 |
}), |
| 627 |
] |
| 628 |
}) |
| 629 |
|
| 630 |
self.assertEqual(order.amount_total, 100) |
| 631 |
self._apply_promo_code(order, gift_card.code) |
| 632 |
self.assertEqual(order.amount_total, 50) |
| 633 |
order.action_confirm() |
| 634 |
self.assertEqual(loyalty_card.points, 100) |
| 635 |
|
| 636 |
def test_multiple_discount_specific(self): |
| 637 |
""" |
| 638 |
Check the discount calculation if it is based on the remaining amount |
| 639 |
""" |
| 640 |
|
| 641 |
product_A = self.env['product.product'].create({ |
| 642 |
'name': 'Product A', |
| 643 |
'list_price': 100, |
| 644 |
'sale_ok': True, |
| 645 |
'taxes_id': [], |
| 646 |
}) |
| 647 |
|
| 648 |
coupon_program = self.env['loyalty.program'].create([{ |
| 649 |
'name': 'Coupon Program', |
| 650 |
'program_type': 'promotion', |
| 651 |
'trigger': 'auto', |
| 652 |
'applies_on': 'current', |
| 653 |
'rule_ids': [Command.create({ |
| 654 |
'reward_point_amount': 1, |
| 655 |
'reward_point_mode': 'unit', |
| 656 |
})], |
| 657 |
'reward_ids': [Command.create({ |
| 658 |
'reward_type': 'discount', |
| 659 |
'discount': 10.0, |
| 660 |
'discount_applicability': 'specific', |
| 661 |
'required_points': 1, |
| 662 |
})], |
| 663 |
}]) |
| 664 |
|
| 665 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 666 |
'partner_id': self.partner.id, |
| 667 |
'order_line': [Command.create({ |
| 668 |
'product_id': product_A.id, |
| 669 |
'product_uom_qty': 3, |
| 670 |
})] |
| 671 |
}) |
| 672 |
|
| 673 |
self.assertEqual(float_compare(order.amount_total, 300, precision_rounding=3), 0) |
| 674 |
|
| 675 |
order._update_programs_and_rewards() |
| 676 |
self._claim_reward(order, coupon_program) |
| 677 |
self.assertEqual(float_compare(order.amount_total, 270, precision_rounding=3), 0, "300 * 0.9 = 270") |
| 678 |
|
| 679 |
order._update_programs_and_rewards() |
| 680 |
self._claim_reward(order, coupon_program) |
| 681 |
self.assertEqual(float_compare(order.amount_total, 243, precision_rounding=3), 0, "300 * 0.9 * 0.9 = 243") |
| 682 |
|
| 683 |
order._update_programs_and_rewards() |
| 684 |
self._claim_reward(order, coupon_program) |
| 685 |
self.assertEqual(float_compare(order.amount_total, 218.7, precision_rounding=3), 0, "300 * 0.9 * 0.9 * 0.9 = 218.7") |
| 686 |
|
| 687 |
def test_promotion_program_restricted_to_pricelists(self): |
| 688 |
self.env['product.pricelist'].search([]).action_archive() |
| 689 |
company_currency = self.env.company.currency_id |
| 690 |
pricelist_1, pricelist_2 = self.env['product.pricelist'].create([ |
| 691 |
{'name': 'Basic company_currency pricelist', 'currency_id': company_currency.id}, |
| 692 |
{'name': 'Other company_currency pricelist', 'currency_id': company_currency.id}, |
| 693 |
]) |
| 694 |
self.immediate_promotion_program.active = True |
| 695 |
order = self.empty_order.copy() |
| 696 |
order.write({'order_line': [ |
| 697 |
(0, False, { |
| 698 |
'product_id': self.product_A.id, |
| 699 |
'name': '1 Product A', |
| 700 |
'product_uom_qty': 1.0, |
| 701 |
}), |
| 702 |
(0, False, { |
| 703 |
'product_id': self.product_B.id, |
| 704 |
'name': '2 Product B', |
| 705 |
'product_uom_qty': 1.0, |
| 706 |
}), |
| 707 |
]}) |
| 708 |
|
| 709 |
applied_message = "The promo offer should have been applied." |
| 710 |
not_applied_message = "The promo offer should not have been applied because the order's " \ |
| 711 |
"pricelist is not eligible to this promotion." |
| 712 |
|
| 713 |
order.pricelist_id = self.env['product.pricelist'] |
| 714 |
order._update_programs_and_rewards() |
| 715 |
self._claim_reward(order, self.immediate_promotion_program) |
| 716 |
self.assertEqual(len(order.order_line.ids), 3, applied_message) |
| 717 |
|
| 718 |
order.pricelist_id = pricelist_1 |
| 719 |
order._update_programs_and_rewards() |
| 720 |
self._claim_reward(order, self.immediate_promotion_program) |
| 721 |
self.assertEqual(len(order.order_line.ids), 3, applied_message) |
| 722 |
|
| 723 |
self.immediate_promotion_program.pricelist_ids = [pricelist_1.id] |
| 724 |
order.pricelist_id = self.env['product.pricelist'] |
| 725 |
order._update_programs_and_rewards() |
| 726 |
self._claim_reward(order, self.immediate_promotion_program) |
| 727 |
self.assertEqual(len(order.order_line.ids), 2, not_applied_message) |
| 728 |
|
| 729 |
order.pricelist_id = pricelist_1 |
| 730 |
order._update_programs_and_rewards() |
| 731 |
self._claim_reward(order, self.immediate_promotion_program) |
| 732 |
self.assertEqual(len(order.order_line.ids), 3, applied_message) |
| 733 |
|
| 734 |
order.pricelist_id = pricelist_2 |
| 735 |
order._update_programs_and_rewards() |
| 736 |
self._claim_reward(order, self.immediate_promotion_program) |
| 737 |
self.assertEqual(len(order.order_line.ids), 2, not_applied_message) |
| 738 |
|
| 739 |
self.immediate_promotion_program.pricelist_ids = [pricelist_1.id, pricelist_2.id] |
| 740 |
order.pricelist_id = self.env['product.pricelist'] |
| 741 |
order._update_programs_and_rewards() |
| 742 |
self._claim_reward(order, self.immediate_promotion_program) |
| 743 |
self.assertEqual(len(order.order_line.ids), 2, not_applied_message) |
| 744 |
|
| 745 |
order.pricelist_id = pricelist_1 |
| 746 |
order._update_programs_and_rewards() |
| 747 |
self._claim_reward(order, self.immediate_promotion_program) |
| 748 |
self.assertEqual(len(order.order_line.ids), 3, applied_message) |
| 749 |
|
| 750 |
def test_coupon_program_restricted_to_pricelists(self): |
| 751 |
self.env['product.pricelist'].search([]).action_archive() |
| 752 |
company_currency = self.env.company.currency_id |
| 753 |
pricelist_1, pricelist_2 = self.env['product.pricelist'].create([ |
| 754 |
{'name': 'Basic company_currency pricelist', 'currency_id': company_currency.id}, |
| 755 |
{'name': 'Other company_currency pricelist', 'currency_id': company_currency.id}, |
| 756 |
]) |
| 757 |
|
| 758 |
self.code_promotion_program.active = True |
| 759 |
self.env['loyalty.generate.wizard'].with_context( |
| 760 |
active_id=self.code_promotion_program.id |
| 761 |
).create({'coupon_qty': 7, 'points_granted': 1}).generate_coupons() |
| 762 |
coupons = self.code_promotion_program.coupon_ids |
| 763 |
|
| 764 |
order_no_pricelist = self.empty_order.copy() |
| 765 |
order_no_pricelist.write({'pricelist_id': None, 'order_line': [ |
| 766 |
(0, False, { |
| 767 |
'product_id': self.product_A.id, |
| 768 |
'name': '1 Product A', |
| 769 |
'product_uom_qty': 1.0, |
| 770 |
}), |
| 771 |
]}) |
| 772 |
order_pricelist_1 = order_no_pricelist.copy() |
| 773 |
order_pricelist_1.pricelist_id = pricelist_1 |
| 774 |
order_pricelist_2 = order_no_pricelist.copy() |
| 775 |
order_pricelist_2.pricelist_id = pricelist_2 |
| 776 |
|
| 777 |
applied_message = "The coupon code should have been applied." |
| 778 |
not_applied_message = "The coupon code should not have been applied because the order's " \ |
| 779 |
"pricelist is not eligible to this promotion." |
| 780 |
|
| 781 |
order_0 = order_no_pricelist.copy() |
| 782 |
self._apply_promo_code(order_0, coupons[0].code) |
| 783 |
self.assertEqual(len(order_0.order_line.ids), 2, applied_message) |
| 784 |
|
| 785 |
order_1 = order_pricelist_1.copy() |
| 786 |
self._apply_promo_code(order_1, coupons[1].code) |
| 787 |
self.assertEqual(len(order_1.order_line.ids), 2, applied_message) |
| 788 |
|
| 789 |
self.code_promotion_program.pricelist_ids = [pricelist_1.id] |
| 790 |
order_2 = order_no_pricelist.copy() |
| 791 |
with self.assertRaises(ValidationError): |
| 792 |
self._apply_promo_code(order_2, coupons[2].code) |
| 793 |
self.assertEqual(len(order_2.order_line.ids), 1, not_applied_message) |
| 794 |
|
| 795 |
order_3 = order_pricelist_1.copy() |
| 796 |
self._apply_promo_code(order_3, coupons[3].code) |
| 797 |
self.assertEqual(len(order_3.order_line.ids), 2, applied_message) |
| 798 |
|
| 799 |
order_4 = order_pricelist_2.copy() |
| 800 |
with self.assertRaises(ValidationError): |
| 801 |
self._apply_promo_code(order_4, coupons[4].code) |
| 802 |
self.assertEqual(len(order_4.order_line.ids), 1, not_applied_message) |
| 803 |
|
| 804 |
self.code_promotion_program.pricelist_ids = [pricelist_1.id, pricelist_2.id] |
| 805 |
order_5 = order_no_pricelist.copy() |
| 806 |
with self.assertRaises(ValidationError): |
| 807 |
self._apply_promo_code(order_5, coupons[5].code) |
| 808 |
self.assertEqual(len(order_5.order_line.ids), 1, not_applied_message) |
| 809 |
|
| 810 |
order_6 = order_pricelist_1.copy() |
| 811 |
self._apply_promo_code(order_6, coupons[6].code) |
| 812 |
self.assertEqual(len(order_6.order_line.ids), 2, applied_message) |
| 813 |
|
| 814 |
def test_specific_promotion_on_free_product(self): |
| 815 |
|
| 816 |
product_A = self.env['product.product'].create({ |
| 817 |
'name': 'Product A', |
| 818 |
'list_price': 100, |
| 819 |
'sale_ok': True, |
| 820 |
'taxes_id': [], |
| 821 |
}) |
| 822 |
|
| 823 |
promotion_program = self.env['loyalty.program'].create([{ |
| 824 |
'name': 'Promotion Program', |
| 825 |
'program_type': 'promotion', |
| 826 |
'trigger': 'auto', |
| 827 |
'applies_on': 'current', |
| 828 |
'rule_ids': [Command.create({ |
| 829 |
'reward_point_amount': 1, |
| 830 |
'reward_point_mode': 'unit', |
| 831 |
})], |
| 832 |
'reward_ids': [Command.create({ |
| 833 |
'reward_type': 'discount', |
| 834 |
'discount': 10.0, |
| 835 |
'discount_applicability': 'specific', |
| 836 |
'discount_product_ids': [product_A.id], |
| 837 |
'required_points': 1, |
| 838 |
})], |
| 839 |
}]) |
| 840 |
|
| 841 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 842 |
'partner_id': self.partner.id, |
| 843 |
'order_line': [ |
| 844 |
Command.create({ |
| 845 |
'product_id': product_A.id, |
| 846 |
}), |
| 847 |
Command.create({ |
| 848 |
'product_id': product_A.id, |
| 849 |
'discount': 100, |
| 850 |
}), |
| 851 |
] |
| 852 |
}) |
| 853 |
|
| 854 |
order._update_programs_and_rewards() |
| 855 |
self._claim_reward(order, promotion_program) |
| 856 |
self.assertEqual(order.amount_total, 90) |
| 857 |
|
| 858 |
def test_gift_card_program_without_product(self): |
| 859 |
product_A = self.env['product.product'].create({ |
| 860 |
'name': 'Product A', |
| 861 |
'list_price': 100, |
| 862 |
'sale_ok': True, |
| 863 |
'taxes_id': [], |
| 864 |
}) |
| 865 |
|
| 866 |
giftcard_program = self.env['loyalty.program'].create([{ |
| 867 |
'name': 'Gift Card Program', |
| 868 |
'program_type': 'gift_card', |
| 869 |
'trigger': 'auto', |
| 870 |
'applies_on': 'current', |
| 871 |
'rule_ids': [Command.create({ |
| 872 |
'reward_point_amount': 1, |
| 873 |
'reward_point_mode': 'unit', |
| 874 |
})], |
| 875 |
}]) |
| 876 |
|
| 877 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 878 |
'partner_id': self.partner.id, |
| 879 |
'order_line': [ |
| 880 |
Command.create({ |
| 881 |
'product_id': product_A.id, |
| 882 |
}), |
| 883 |
] |
| 884 |
}) |
| 885 |
|
| 886 |
order._update_programs_and_rewards() |
| 887 |
self._claim_reward(order, giftcard_program) |
| 888 |
|
| 889 |
self.assertEqual(giftcard_program.coupon_count, 0) |
| 890 |
|
| 891 |
def test_ewallet_code_use_restriction(self): |
| 892 |
self.env['loyalty.generate.wizard'].with_context(active_id=self.ewallet_program.id).create({ |
| 893 |
'coupon_qty': 1, |
| 894 |
'points_granted': 100, |
| 895 |
}).generate_coupons() |
| 896 |
|
| 897 |
order = self.env['sale.order'].with_user(self.user_salemanager).create({ |
| 898 |
'partner_id': self.partner.id, |
| 899 |
'order_line': [ |
| 900 |
Command.create({ |
| 901 |
'product_id': self.product_a.id, |
| 902 |
}), |
| 903 |
], |
| 904 |
}) |
| 905 |
|
| 906 |
with self.assertRaises(ValidationError): |
| 907 |
self._apply_promo_code(order, self.ewallet_program.coupon_ids[0].code) |
| 908 |
|
| 909 |
def test_100_percent_discount(self): |
| 910 |
""" |
| 911 |
Check whether a program offering 100% discount on an order reduces the order's total amount |
| 912 |
to zero. |
| 913 |
|
| 914 |
Assumes global tax rounding, as there's no good way to ensure the tax of the reward product |
| 915 |
equals the sum of taxes of the lines when each of them gets rounded. |
| 916 |
""" |
| 917 |
self.env.company.tax_calculation_rounding_method = 'round_globally' |
| 918 |
loyalty_program = self.env['loyalty.program'].create([{ |
| 919 |
'name': 'Full Discount', |
| 920 |
'program_type': 'loyalty', |
| 921 |
'trigger': 'auto', |
| 922 |
'applies_on': 'both', |
| 923 |
'rule_ids': [(0, 0, { |
| 924 |
'reward_point_mode': 'unit', |
| 925 |
'reward_point_amount': 1, |
| 926 |
'product_ids': [self.product_a.id], |
| 927 |
})], |
| 928 |
'reward_ids': [(0, 0, { |
| 929 |
'reward_type': 'discount', |
| 930 |
'discount': 100, |
| 931 |
'discount_mode': 'percent', |
| 932 |
'discount_applicability': 'order', |
| 933 |
'required_points': 1, |
| 934 |
})], |
| 935 |
}]) |
| 936 |
self.env['loyalty.card'].create({ |
| 937 |
'program_id': loyalty_program.id, 'partner_id': self.partner.id, 'points': 2 |
| 938 |
}) |
| 939 |
order = self.env['sale.order'].create({ |
| 940 |
'partner_id': self.partner.id, |
| 941 |
'order_line': [(0, 0, { |
| 942 |
'product_id': self.product_A.id, 'product_uom_qty': 1, 'price_unit': price |
| 943 |
}) for price in (5.60, 8.92, 44.91, 217.26, 2400.00)], |
| 944 |
}) |
| 945 |
|
| 946 |
order._update_programs_and_rewards() |
| 947 |
self._claim_reward(order, loyalty_program) |
| 948 |
msg = "100% discount on order should reduce total amount to 0" |
| 949 |
self.assertEqual(order.amount_total, 0, msg=msg) |
| 950 |
|
| 951 |
def test_discount_on_taxes_with_child_tax(self): |
| 952 |
""" |
| 953 |
Check whether a program discount properly apply when product contain group of tax. |
| 954 |
""" |
| 955 |
self.env.company.tax_calculation_rounding_method = 'round_globally' |
| 956 |
loyalty_program = self.env['loyalty.program'].create([{ |
| 957 |
'name': '90% Discount', |
| 958 |
'program_type': 'loyalty', |
| 959 |
'trigger': 'auto', |
| 960 |
'applies_on': 'both', |
| 961 |
'rule_ids': [(0, 0, { |
| 962 |
'reward_point_mode': 'unit', |
| 963 |
'reward_point_amount': 1, |
| 964 |
'product_ids': [self.product_a.id], |
| 965 |
})], |
| 966 |
'reward_ids': [(0, 0, { |
| 967 |
'reward_type': 'discount', |
| 968 |
'discount': 90, |
| 969 |
'discount_mode': 'percent', |
| 970 |
'discount_applicability': 'order', |
| 971 |
'required_points': 1, |
| 972 |
})], |
| 973 |
}]) |
| 974 |
self.env['loyalty.card'].create({'program_id': loyalty_program.id, 'partner_id': self.partner.id, 'points': 2}) |
| 975 |
order = self.env['sale.order'].create({ |
| 976 |
'partner_id': self.partner.id, |
| 977 |
'order_line': [(0, 0, {'product_id': self.product_D.id, 'product_uom_qty': 1})], |
| 978 |
}) |
| 979 |
|
| 980 |
order._update_programs_and_rewards() |
| 981 |
self._claim_reward(order, loyalty_program) |
| 982 |
msg = "Discountable should take child tax amount into account" |
| 983 |
self.assertEqual(order.amount_total, 10, msg=msg) |
| 984 |
|
| 985 |
def test_ewallet_program_without_trigger_product(self): |
| 986 |
self.ewallet_program.trigger_product_ids = [Command.clear()] |
| 987 |
self.ewallet.points = 1000 |
| 988 |
|
| 989 |
order = self.env['sale.order'].create({ |
| 990 |
'partner_id': self.partner.id, |
| 991 |
'order_line': [Command.create({ |
| 992 |
'product_id': self.product_a.id, |
| 993 |
'points_cost': 100, |
| 994 |
'product_uom_qty': 1, |
| 995 |
})], |
| 996 |
}) |
| 997 |
order._update_programs_and_rewards() |
| 998 |
self._claim_reward(order, self.ewallet_program, coupon=self.ewallet) |
| 999 |
order.action_confirm() |
| 1000 |
|
| 1001 |
self.assertEqual(self.ewallet.points, 900) |
| 1002 |
|
| 1003 |
def test_ewallet_applied_ewallet_topup_in_order(self): |
| 1004 |
self.ewallet.points = 10 |
| 1005 |
|
| 1006 |
order = self.env['sale.order'].create({ |
| 1007 |
'partner_id': self.partner.id, |
| 1008 |
'order_line': [Command.create({ |
| 1009 |
'product_id': self.product_a.id, |
| 1010 |
'points_cost': 100, |
| 1011 |
'product_uom_qty': 1, |
| 1012 |
}), Command.create({ |
| 1013 |
'product_id': self.env.ref('loyalty.ewallet_product_50').id, |
| 1014 |
'product_uom_qty': 1, |
| 1015 |
})], |
| 1016 |
}) |
| 1017 |
order._update_programs_and_rewards() |
| 1018 |
self._claim_reward(order, self.ewallet_program, coupon=self.ewallet) |
| 1019 |
order.action_confirm() |
| 1020 |
|
| 1021 |
self.assertEqual(self.ewallet.points, 50) |
| 1022 |
|
| 1023 |
def test_discount_reward_claimable_only_once(self): |
| 1024 |
""" |
| 1025 |
Check that discount rewards already applied won't be shown in the claimable rewards anymore. |
| 1026 |
""" |
| 1027 |
program = self.env['loyalty.program'].create({ |
| 1028 |
'name': "10% Discount & Gift", |
| 1029 |
'applies_on': 'current', |
| 1030 |
'trigger': 'with_code', |
| 1031 |
'program_type': 'promotion', |
| 1032 |
'rule_ids': [Command.create({'mode': 'with_code', 'code': "10PERCENT&GIFT"})], |
| 1033 |
'reward_ids': [ |
| 1034 |
Command.create({ |
| 1035 |
'reward_type': 'product', |
| 1036 |
'reward_product_id': self.product_B.id, |
| 1037 |
'reward_product_qty': 1, |
| 1038 |
}), |
| 1039 |
Command.create({ |
| 1040 |
'reward_type': 'discount', |
| 1041 |
'discount': 10, |
| 1042 |
'discount_mode': 'percent', |
| 1043 |
'discount_applicability': 'specific', |
| 1044 |
}), |
| 1045 |
], |
| 1046 |
}) |
| 1047 |
|
| 1048 |
coupon = self.env['loyalty.card'].create({ |
| 1049 |
'program_id': program.id, 'points': 20, 'code': 'GIFT_CARD' |
| 1050 |
}) |
| 1051 |
|
| 1052 |
order = self.env['sale.order'].create({ |
| 1053 |
'partner_id': self.partner.id, |
| 1054 |
'order_line': [Command.create({'product_id': self.product_a.id})] |
| 1055 |
}) |
| 1056 |
|
| 1057 |
product_reward = program.reward_ids.filtered(lambda reward: reward.reward_type == 'product') |
| 1058 |
discount_reward = program.reward_ids - product_reward |
| 1059 |
order._apply_program_reward(discount_reward, coupon) |
| 1060 |
rewards = order._get_claimable_rewards()[coupon] |
| 1061 |
msg = "Only the free product should be applicable, as the discount was already applied." |
| 1062 |
self.assertEqual(rewards, product_reward, msg) |
| 1063 |
|
| 1064 |
def test_archived_reward_products(self): |
| 1065 |
""" |
| 1066 |
Check that we do not use loyalty rewards that have no active reward product. |
| 1067 |
In the case where the reward is based on reward_product_tag_id we also check |
| 1068 |
the case where at least one reward is active. |
| 1069 |
""" |
| 1070 |
|
| 1071 |
LoyaltyProgram = self.env['loyalty.program'] |
| 1072 |
loyalty_program = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 1073 |
loyalty_program_tag = LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty']) |
| 1074 |
|
| 1075 |
free_product_tag = self.env['product.tag'].create({'name': 'Free Product'}) |
| 1076 |
self.product_b.write({'product_tag_ids': [(4, free_product_tag.id)]}) |
| 1077 |
product_c = self.env['product.template'].create( |
| 1078 |
{ |
| 1079 |
'name': 'Free Product C', |
| 1080 |
'list_price': 1, |
| 1081 |
'product_tag_ids': [(4, free_product_tag.id)], |
| 1082 |
} |
| 1083 |
) |
| 1084 |
|
| 1085 |
loyalty_program.reward_ids[0].write({ |
| 1086 |
'reward_type': 'product', |
| 1087 |
'required_points': 1, |
| 1088 |
'reward_product_id': self.product_b, |
| 1089 |
}) |
| 1090 |
loyalty_program_tag.reward_ids[0].write({ |
| 1091 |
'reward_type': 'product', |
| 1092 |
'required_points': 1, |
| 1093 |
'reward_product_tag_id': free_product_tag.id, |
| 1094 |
}) |
| 1095 |
self.product_b.active = False |
| 1096 |
product_c.active = False |
| 1097 |
|
| 1098 |
order = self.env['sale.order'].create({ |
| 1099 |
'partner_id': self.partner.id, |
| 1100 |
'order_line': [ |
| 1101 |
Command.create({ |
| 1102 |
'product_id': self.product_a.id, |
| 1103 |
}), |
| 1104 |
] |
| 1105 |
}) |
| 1106 |
|
| 1107 |
order._update_programs_and_rewards() |
| 1108 |
rewards = [value.ids for value in order._get_claimable_rewards().values()] |
| 1109 |
self.assertTrue(all(loyalty_program.reward_ids[0].id not in r for r in rewards)) |
| 1110 |
self.assertTrue(all(loyalty_program_tag.reward_ids[0].id not in r for r in rewards)) |
| 1111 |
|
| 1112 |
product_c.active = True |
| 1113 |
order._update_programs_and_rewards() |
| 1114 |
rewards = [value.ids for value in order._get_claimable_rewards().values()] |
| 1115 |
self.assertTrue(any(loyalty_program_tag.reward_ids[0].id in r for r in rewards)) |
| 1116 |
|
| 1117 |
def test_domain_on_cheapest_reward(self): |
| 1118 |
product_tag = self.env['product.tag'].create({'name': "Discountable"}) |
| 1119 |
self.env['loyalty.program'].create({ |
| 1120 |
'name': "10% Discount", |
| 1121 |
'program_type': 'promo_code', |
| 1122 |
'rule_ids': [Command.create({'code': "10discount"})], |
| 1123 |
'reward_ids': [ |
| 1124 |
Command.create({ |
| 1125 |
'reward_type': 'discount', |
| 1126 |
'discount': 10, |
| 1127 |
'discount_mode': 'percent', |
| 1128 |
'discount_applicability': 'cheapest', |
| 1129 |
'discount_product_tag_id': product_tag.id, |
| 1130 |
}), |
| 1131 |
], |
| 1132 |
}) |
| 1133 |
self.product_A.product_tag_ids = product_tag |
| 1134 |
order = self.empty_order |
| 1135 |
order.write({ |
| 1136 |
'order_line':[ |
| 1137 |
|
| 1138 |
Command.create({'product_id': self.product_A.id}), |
| 1139 |
|
| 1140 |
Command.create({'product_id': self.product_B.id}), |
| 1141 |
] |
| 1142 |
}) |
| 1143 |
self._apply_promo_code(order, '10discount') |
| 1144 |
msg = "Discount should only be applied to the line with a correctly tagged product." |
| 1145 |
self.assertEqual(order.order_line[2].price_total, -11.5, msg) |
| 1146 |
|
| 1147 |
self.product_C.write({ |
| 1148 |
'list_price': 50, |
| 1149 |
'product_tag_ids': product_tag, |
| 1150 |
}) |
| 1151 |
order.order_line[2:].unlink() |
| 1152 |
order.write({ |
| 1153 |
'order_line':[ |
| 1154 |
|
| 1155 |
Command.create({'product_id': self.product_C.id}), |
| 1156 |
] |
| 1157 |
}) |
| 1158 |
self._apply_promo_code(order, '10discount') |
| 1159 |
msg = "Discount should be applied to the line with the cheapest valid product." |
| 1160 |
self.assertEqual(order.order_line[3].price_total, -5.0, msg) |
| 1161 |
|
| 1162 |
def test_sol_free_product_description_equals_reward_description(self): |
| 1163 |
""" |
| 1164 |
Ensure that if a "Free Product" reward is added to a sale order, |
| 1165 |
its line description matches the reward description. |
| 1166 |
""" |
| 1167 |
loyalty_program = self.env['loyalty.program'].create( |
| 1168 |
self.env['loyalty.program']._get_template_values()['buy_x_get_y'] |
| 1169 |
) |
| 1170 |
reward = loyalty_program.reward_ids[0] |
| 1171 |
updated_description = f"{reward.description} Adding manual description" |
| 1172 |
reward.description = updated_description |
| 1173 |
|
| 1174 |
order = self.empty_order |
| 1175 |
order.write({ |
| 1176 |
'order_line': [ |
| 1177 |
Command.create({ |
| 1178 |
'product_id': reward.reward_product_id.id, |
| 1179 |
'name': '1 Product', |
| 1180 |
'product_uom_qty': 4.0, |
| 1181 |
}), |
| 1182 |
] |
| 1183 |
}) |
| 1184 |
order._update_programs_and_rewards() |
| 1185 |
self._claim_reward(order, loyalty_program) |
| 1186 |
|
| 1187 |
self.assertEqual(len(order.order_line.ids), 2) |
| 1188 |
self.assertEqual(order.order_line[1].name, updated_description) |
| 1189 |
|
| 1190 |
def test_archiving_loyalty_card_unlinks_draft_points_from_sale_order(self): |
| 1191 |
""" |
| 1192 |
When a loyalty card has points accrued from a draft sale order, archiving the |
| 1193 |
card should unlink those draft points so they are no longer claimable on that order |
| 1194 |
""" |
| 1195 |
loyalty_program = self.env['loyalty.program'].create({ |
| 1196 |
'name': 'Loyalty Program', |
| 1197 |
'program_type': 'loyalty', |
| 1198 |
'trigger': 'auto', |
| 1199 |
'applies_on': 'both', |
| 1200 |
'rule_ids': [ |
| 1201 |
Command.create({ |
| 1202 |
'reward_point_mode': 'unit', |
| 1203 |
'reward_point_amount': 100, |
| 1204 |
'product_ids': [self.product_a.id], |
| 1205 |
}), |
| 1206 |
], |
| 1207 |
'reward_ids': [ |
| 1208 |
Command.create({ |
| 1209 |
'reward_type': 'discount', |
| 1210 |
'discount': 50, |
| 1211 |
'discount_mode': 'percent', |
| 1212 |
'discount_applicability': 'order', |
| 1213 |
'required_points': 10, |
| 1214 |
}), |
| 1215 |
], |
| 1216 |
}) |
| 1217 |
loyalty_card = self.env['loyalty.card'].create({ |
| 1218 |
'program_id': loyalty_program.id, |
| 1219 |
'partner_id': self.partner.id, |
| 1220 |
'points': 0, |
| 1221 |
}) |
| 1222 |
sale_order = self.empty_order |
| 1223 |
sale_order.write({ |
| 1224 |
'order_line': [ |
| 1225 |
Command.create({ |
| 1226 |
'product_id': self.product_a.id, |
| 1227 |
}), |
| 1228 |
] |
| 1229 |
}) |
| 1230 |
sale_order._update_programs_and_rewards() |
| 1231 |
claimable_rewards = sale_order._get_claimable_rewards() |
| 1232 |
self.assertTrue(claimable_rewards[loyalty_card]) |
| 1233 |
loyalty_card.action_archive() |
| 1234 |
claimable_rewards = sale_order._get_claimable_rewards() |
| 1235 |
self.assertFalse(claimable_rewards.get(loyalty_card)) |
| 1236 |
|
| 1237 |
def test_free_product_sol_is_zero_price(self): |
| 1238 |
self.env['res.config.settings'].create({ |
| 1239 |
'group_discount_per_so_line': True, |
| 1240 |
}).execute() |
| 1241 |
loyalty_program = self.env['loyalty.program'].create({ |
| 1242 |
'name': 'Loyalty Program', |
| 1243 |
'program_type': 'promotion', |
| 1244 |
'trigger': 'auto', |
| 1245 |
'applies_on': 'both', |
| 1246 |
'rule_ids': [ |
| 1247 |
Command.create({ |
| 1248 |
'reward_point_mode': 'unit', |
| 1249 |
'reward_point_amount': 1, |
| 1250 |
'product_ids': [self.product_a.id], |
| 1251 |
}), |
| 1252 |
], |
| 1253 |
'reward_ids': [ |
| 1254 |
Command.create({ |
| 1255 |
'reward_type': 'product', |
| 1256 |
'reward_product_id': self.product_B.id, |
| 1257 |
'reward_product_qty': 1, |
| 1258 |
'required_points': 1, |
| 1259 |
}), |
| 1260 |
], |
| 1261 |
}) |
| 1262 |
sale_order = self.empty_order |
| 1263 |
sale_order.write({ |
| 1264 |
'order_line': [ |
| 1265 |
Command.create({ |
| 1266 |
'product_id': self.product_a.id, |
| 1267 |
'product_uom_qty': 1, |
| 1268 |
}), |
| 1269 |
], |
| 1270 |
}) |
| 1271 |
sale_order._update_programs_and_rewards() |
| 1272 |
self._claim_reward(sale_order, loyalty_program) |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
sale_order.order_line._compute_discount() |
| 1278 |
reward_line = sale_order.order_line.filtered('reward_id') |
| 1279 |
self.assertEqual(reward_line.discount, 100) |
| 1280 |
self.assertEqual(reward_line.price_total, 0) |
| 1281 |
|
| 1282 |
def test_reapplying_reward_keeps_reward_price_unit(self): |
| 1283 |
""" |
| 1284 |
Ensure that re-applying a reward doesn't reset the existing reward line unit price to zero |
| 1285 |
""" |
| 1286 |
self.immediate_promotion_program.active = True |
| 1287 |
sale_order = self.empty_order |
| 1288 |
sale_order.write({ |
| 1289 |
'order_line': [ |
| 1290 |
Command.create({ |
| 1291 |
'product_id': self.product_A.id, |
| 1292 |
'product_uom_qty': 1, |
| 1293 |
}), |
| 1294 |
], |
| 1295 |
}) |
| 1296 |
sale_order._update_programs_and_rewards() |
| 1297 |
self._claim_reward(sale_order, self.immediate_promotion_program) |
| 1298 |
reward_line = sale_order.order_line.filtered('reward_id') |
| 1299 |
reward_line_price_unit = reward_line.price_unit |
| 1300 |
sale_order._update_programs_and_rewards() |
| 1301 |
self._claim_reward(sale_order, self.immediate_promotion_program) |
| 1302 |
self.assertEqual(reward_line.price_unit, reward_line_price_unit) |