Debian Codegraph @

addons/sale_loyalty/tests/test_pay_with_gift_card.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
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('-at_install', 'post_install')
10 class TestPayWithGiftCard(TestSaleCouponCommon):
11
12 def test_paying_with_single_gift_card_over(self):
13 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
14 'coupon_qty': 1,
15 'points_granted': 100,
16 }).generate_coupons()
17 gift_card = self.program_gift_card.coupon_ids[0]
18 order = self.empty_order
19 order.write({'order_line': [
20 Command.create({
21 'product_id': self.product_A.id,
22 'name': 'Ordinary Product A',
23 'product_uom_qty': 1.0,
24 })
25 ]})
26 before_gift_card_payment = order.amount_total
27 self.assertNotEqual(before_gift_card_payment, 0)
28 self._apply_promo_code(order, gift_card.code)
29 order.action_confirm()
30 self.assertEqual(before_gift_card_payment - order.amount_total, 100 - gift_card.points)
31
32 def test_paying_with_single_gift_card_under(self):
33 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
34 'coupon_qty': 1,
35 'points_granted': 100,
36 }).generate_coupons()
37 gift_card = self.program_gift_card.coupon_ids[0]
38 order = self.empty_order
39 order.write({'order_line': [
40 Command.create({
41 'product_id': self.product_B.id,
42 'name': 'Ordinary Product b',
43 'product_uom_qty': 1.0,
44 })
45 ]})
46 before_gift_card_payment = order.amount_total
47 self.assertNotEqual(before_gift_card_payment, 0)
48 self._apply_promo_code(order, gift_card.code)
49 order.action_confirm()
50 self.assertEqual(before_gift_card_payment - order.amount_total, 100 - gift_card.points)
51
52 def test_paying_with_multiple_gift_card(self):
53 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
54 'coupon_qty': 2,
55 'points_granted': 100,
56 }).generate_coupons()
57 gift_card_1, gift_card_2 = self.program_gift_card.coupon_ids
58 order = self.empty_order
59 order.write({'order_line': [
60 Command.create({
61 'product_id': self.product_A.id,
62 'name': 'Ordinary Product A',
63 'product_uom_qty': 20.0,
64 })
65 ]})
66 before_gift_card_payment = order.amount_total
67 self._apply_promo_code(order, gift_card_1.code)
68 self._apply_promo_code(order, gift_card_2.code)
69 self.assertEqual(order.amount_total, before_gift_card_payment - 200)
70
71 def test_paying_with_gift_card_and_discount(self):
72 # Test that discounts take precedence on payment rewards
73 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
74 'coupon_qty': 1,
75 'points_granted': 50,
76 }).generate_coupons()
77 gift_card_1 = self.program_gift_card.coupon_ids
78 order = self.empty_order
79 order.write({'order_line': [
80 Command.create({
81 'product_id': self.product_C.id,
82 'name': 'Ordinary Product C',
83 'product_uom_qty': 1.0,
84 })
85 ]})
86 self.env['loyalty.program'].create({
87 'name': 'Code for 10% on orders',
88 'trigger': 'with_code',
89 'program_type': 'promotion',
90 'applies_on': 'current',
91 'rule_ids': [(0, 0, {
92 'mode': 'with_code',
93 'code': 'test_10pc',
94 })],
95 'reward_ids': [(0, 0, {
96 'reward_type': 'discount',
97 'discount_mode': 'percent',
98 'discount': 10,
99 'discount_applicability': 'order',
100 'required_points': 1,
101 })],
102 })
103 self.assertEqual(order.amount_total, 100)
104 self._apply_promo_code(order, gift_card_1.code)
105 self.assertEqual(order.amount_total, 50)
106 self._apply_promo_code(order, "test_10pc")
107 # real flows also have to update the programs and rewards
108 order._update_programs_and_rewards()
109 self.assertEqual(order.amount_total, 40) # 100 - 10% - 50
110
111 def test_paying_with_gift_card_blocking_discount(self):
112 # Test that a payment program making the order total 0 still allows the user to claim discounts
113 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
114 'coupon_qty': 1,
115 'points_granted': 100,
116 }).generate_coupons()
117 gift_card_1 = self.program_gift_card.coupon_ids
118 order = self.empty_order
119 order.write({'order_line': [
120 Command.create({
121 'product_id': self.product_C.id,
122 'name': 'Ordinary Product C',
123 'product_uom_qty': 1.0,
124 })
125 ]})
126 self.env['loyalty.program'].create({
127 'name': 'Code for 10% on orders',
128 'trigger': 'with_code',
129 'program_type': 'promotion',
130 'applies_on': 'current',
131 'rule_ids': [(0, 0, {
132 'mode': 'with_code',
133 'code': 'test_10pc',
134 })],
135 'reward_ids': [(0, 0, {
136 'reward_type': 'discount',
137 'discount_mode': 'percent',
138 'discount': 10,
139 'discount_applicability': 'order',
140 'required_points': 1,
141 })],
142 })
143 self.assertEqual(order.amount_total, 100)
144 self._apply_promo_code(order, gift_card_1.code)
145 self.assertEqual(order.amount_total, 0)
146 self._apply_promo_code(order, "test_10pc")
147 # real flows also have to update the programs and rewards
148 order._update_programs_and_rewards()
149 self.assertEqual(order.amount_total, 0) # 100 - 10% - 90
150
151 def test_gift_card_product_has_no_taxes_on_creation(self):
152 gift_card_program = self.env['loyalty.program'].create({
153 'name': 'Gift Cards',
154 'applies_on': 'future',
155 'program_type': 'gift_card',
156 'trigger': 'auto',
157 'rule_ids': [Command.create({
158 'product_ids': self.product_gift_card,
159 'reward_point_amount': 1,
160 'reward_point_mode': 'money',
161 'reward_point_split': True,
162 })],
163 'reward_ids': [Command.create({
164 'reward_type': 'discount',
165 'discount': 1,
166 'discount_mode': 'per_point',
167 'discount_applicability': 'order',
168 })]
169 })
170 self.assertFalse(gift_card_program.reward_ids.discount_line_product_id.taxes_id)
171
172 def test_paying_with_gift_card_uses_gift_card_product_taxes(self):
173 order = self.empty_order
174 order.order_line = [
175 Command.create({
176 'product_id': self.product_B.id,
177 'name': 'Ordinary Product b',
178 'product_uom_qty': 1.0,
179 'price_unit': 200.0,
180 })
181 ]
182 sol = order.order_line
183 before_gift_card_payment = order.amount_total
184 self.assertNotEqual(before_gift_card_payment, 0)
185
186 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
187 'coupon_qty': 1,
188 'points_granted': 100,
189 }).generate_coupons()
190 gift_card = self.program_gift_card.coupon_ids[0]
191
192 # TODO check amount total of gift_card_line
193
194 # TAX EXCL
195 self.program_gift_card.reward_ids.discount_line_product_id.taxes_id = [
196 Command.link(self.tax_15pc_excl.id)
197 ]
198 self._apply_promo_code(order, gift_card.code)
199 gift_card_line = order.order_line - sol
200 self.assertAlmostEqual(gift_card_line.price_total, -100.0)
201 self.assertAlmostEqual(order.amount_total, before_gift_card_payment - 100.0)
202 self.assertTrue(all(line.tax_ids for line in order.order_line))
203 self.assertEqual(order.order_line.tax_ids, self.tax_15pc_excl)
204
205 # TAX INCL
206 gift_card_line.unlink() # Remove gift card
207 self.program_gift_card.reward_ids.discount_line_product_id.taxes_id = [
208 Command.set(self.tax_10pc_incl.ids)
209 ]
210 self._apply_promo_code(order, gift_card.code)
211 gift_card_line = order.order_line - sol
212 self.assertAlmostEqual(gift_card_line.price_total, -100.0)
213 self.assertAlmostEqual(order.amount_total, before_gift_card_payment - 100.0)
214 self.assertTrue(all(line.tax_ids for line in order.order_line))
215 self.assertEqual(gift_card_line.tax_ids, self.tax_10pc_incl)
216
217 # TAX INCL + TAX EXCL
218 gift_card_line.unlink() # Remove gift card
219 self.program_gift_card.reward_ids.discount_line_product_id.taxes_id = [
220 Command.link(self.tax_15pc_excl.id)
221 ]
222 self._apply_promo_code(order, gift_card.code)
223 gift_card_line = order.order_line - sol
224 self.assertAlmostEqual(gift_card_line.price_total, -100.0)
225 self.assertAlmostEqual(order.amount_total, before_gift_card_payment - 100.0)
226 self.assertTrue(all(line.tax_ids for line in order.order_line))
227 self.assertEqual(gift_card_line.tax_ids, self.tax_10pc_incl + self.tax_15pc_excl)
228
229 def test_paying_with_gift_card_fixed_tax(self):
230 """ Test payment of sale order with fixed tax using gift card """
231 self.env['loyalty.generate.wizard'].with_context(active_id=self.program_gift_card.id).create({
232 'coupon_qty': 1,
233 'points_granted': 100,
234 }).generate_coupons()
235 gift_card = self.program_gift_card.coupon_ids[0]
236
237 tax_10_fixed = self.env['account.tax'].create({
238 'name': "10$ Fixed tax",
239 'amount_type': 'fixed',
240 'amount': 10,
241 })
242 self.product_A.write({'list_price': 90})
243 self.product_A.taxes_id = tax_10_fixed
244
245 order = self.empty_order
246 order.write({'order_line': [
247 Command.create({
248 'product_id': self.product_A.id,
249 'name': "Ordinary Product A",
250 'product_uom_qty': 1.0,
251 })
252 ]})
253 self._apply_promo_code(order, gift_card.code)
254 order.action_confirm()
255 self.assertEqual(order.amount_total, 0, "The order should be totally paid")