| 1 |
|
| 2 |
|
| 3 |
from odoo.fields import Command |
| 4 |
from odoo.tests import tagged |
| 5 |
|
| 6 |
from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon |
| 7 |
|
| 8 |
|
| 9 |
@tagged('post_install', '-at_install') |
| 10 |
class TestLoyaltyhistory(TestSaleCouponCommon): |
| 11 |
|
| 12 |
@classmethod |
| 13 |
def setUpClass(cls): |
| 14 |
super().setUpClass() |
| 15 |
cls.partner_a = cls.env['res.partner'].create({'name': 'Jean Jacques'}) |
| 16 |
cls.loyalty_program = cls.env['loyalty.program'].create({ |
| 17 |
'name': 'Full Discount', |
| 18 |
'program_type': 'loyalty', |
| 19 |
'trigger': 'auto', |
| 20 |
'applies_on': 'both', |
| 21 |
'rule_ids': [Command.create({ |
| 22 |
'reward_point_mode': 'unit', |
| 23 |
'reward_point_amount': 1, |
| 24 |
'product_ids': [cls.product_A.id], |
| 25 |
})], |
| 26 |
'reward_ids': [ |
| 27 |
Command.create({ |
| 28 |
'reward_type': 'discount', |
| 29 |
'discount': 10, |
| 30 |
'discount_mode': 'percent', |
| 31 |
'discount_applicability': 'order', |
| 32 |
'required_points': 1, |
| 33 |
}), |
| 34 |
Command.create({ |
| 35 |
'active': False, |
| 36 |
'reward_type': 'product', |
| 37 |
'reward_product_id': cls.product_B.id, |
| 38 |
'required_points': 2, |
| 39 |
}), |
| 40 |
], |
| 41 |
}) |
| 42 |
cls.loyalty_card = cls.env['loyalty.card'].create({ |
| 43 |
'program_id': cls.loyalty_program.id, 'partner_id': cls.partner_a.id, 'points': 2 |
| 44 |
}) |
| 45 |
|
| 46 |
def test_add_loyalty_history_line_with_reward(self): |
| 47 |
order = self.empty_order |
| 48 |
order.write({ |
| 49 |
'order_line': [ |
| 50 |
Command.create({ |
| 51 |
'product_id': self.product_A.id, |
| 52 |
'name': 'Ordinary Product A', |
| 53 |
'product_uom_qty': 1.0, |
| 54 |
}), |
| 55 |
], |
| 56 |
}) |
| 57 |
order._update_programs_and_rewards() |
| 58 |
self._auto_rewards(order, self.immediate_promotion_program) |
| 59 |
|
| 60 |
order.action_confirm() |
| 61 |
coupon_applied = self.immediate_promotion_program.coupon_ids.filtered(lambda x: x.order_id == order) |
| 62 |
history_records = len(coupon_applied.history_ids.filtered(lambda history: history.order_id == order.id)) |
| 63 |
self.assertEqual(history_records, 1, "A history line should be created on confirmation of order") |
| 64 |
|
| 65 |
def test_add_loyalty_history_line_without_reward(self): |
| 66 |
order = self.empty_order |
| 67 |
order.write({ |
| 68 |
'partner_id': self.partner_a.id, |
| 69 |
'order_line': [ |
| 70 |
Command.create({ |
| 71 |
'product_id': self.product_A.id, |
| 72 |
'tax_ids': False, |
| 73 |
}), |
| 74 |
] |
| 75 |
}) |
| 76 |
order.action_confirm() |
| 77 |
order._update_programs_and_rewards() |
| 78 |
self._claim_reward(order, self.loyalty_program) |
| 79 |
history_records = self.loyalty_card.history_ids.filtered(lambda history: history.order_id == order.id) |
| 80 |
self.assertEqual(history_records.used, 1.0, |
| 81 |
"The history line should be updated on change of order lines in a confirmed order") |
| 82 |
|
| 83 |
def test_delete_loyalty_history_line_on_cancel(self): |
| 84 |
order = self.empty_order |
| 85 |
order.write({ |
| 86 |
'partner_id': self.partner_a.id, |
| 87 |
'order_line': [ |
| 88 |
Command.create({ |
| 89 |
'product_id': self.product_A.id, |
| 90 |
'tax_ids': False, |
| 91 |
}), |
| 92 |
] |
| 93 |
}) |
| 94 |
order._update_programs_and_rewards() |
| 95 |
self._claim_reward(order, self.loyalty_program) |
| 96 |
order.action_confirm() |
| 97 |
lines_before_cancel = len(self.loyalty_card.history_ids) |
| 98 |
order._action_cancel() |
| 99 |
self.assertEqual(lines_before_cancel - 1, len(self.loyalty_card.history_ids), |
| 100 |
"History line should be deleted after order cancel") |
| 101 |
|
| 102 |
def test_loyalty_history_multi_reward(self): |
| 103 |
"""Verify that applying multiple rewards sums up the total points cost.""" |
| 104 |
self.loyalty_card.points = initial_points = 4 |
| 105 |
self.loyalty_program.with_context(active_test=False).reward_ids.active = True |
| 106 |
order = self.empty_order |
| 107 |
order.write({ |
| 108 |
'partner_id': self.partner_a.id, |
| 109 |
'order_line': [ |
| 110 |
Command.create({ |
| 111 |
'product_id': self.product_A.id, |
| 112 |
'tax_ids': False, |
| 113 |
}), |
| 114 |
], |
| 115 |
}) |
| 116 |
for reward in self.loyalty_program.reward_ids: |
| 117 |
order._apply_program_reward(reward, self.loyalty_card) |
| 118 |
self.assertEqual(len(order.order_line.filtered('reward_id')), 2) |
| 119 |
self.assertEqual(order.order_line.mapped('points_cost'), [0, 1, 2]) |
| 120 |
|
| 121 |
order.action_confirm() |
| 122 |
loyalty_history = self.loyalty_card.history_ids |
| 123 |
self.assertEqual(loyalty_history.issued, 1, "1 point should be rewarded") |
| 124 |
self.assertEqual(loyalty_history.used, 3, "A total of 3 points should be used") |
| 125 |
self.assertEqual( |
| 126 |
self.loyalty_card.points, |
| 127 |
initial_points + loyalty_history.issued - loyalty_history.used, |
| 128 |
"Loyalty points should equal initial points + points issued - points used", |
| 129 |
) |