Debian Codegraph @

addons/sale_loyalty/tests/test_program_numbers.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo.exceptions import ValidationError
4 from odoo.fields import Command
5 from odoo.tests import tagged
6 from odoo.tools.float_utils import float_compare
7
8 from odoo.addons.sale_loyalty.tests.common import TestSaleCouponNumbersCommon
9
10
11 @tagged('post_install', '-at_install')
12 class TestSaleCouponProgramNumbers(TestSaleCouponNumbersCommon):
13
14 def test_program_numbers_free_and_paid_product_qty(self):
15 # These tests will focus on numbers (free product qty, SO total, reduction total..)
16 order = self.empty_order
17 sol1 = self.env['sale.order.line'].create({
18 'product_id': self.largeCabinet.id,
19 'name': 'Large Cabinet',
20 'product_uom_qty': 3.0,
21 'order_id': order.id,
22 })
23
24 # Check we correctly get a free product
25 self._auto_rewards(order, self.all_programs)
26 self.assertEqual(len(order.order_line.ids), 2, "We should have 2 lines as we now have one 'Free Large Cabinet' line as we bought 3 of them")
27
28 # Check free product's price is not added to total when applying reduction (Or the discount will also be applied on the free product's price)
29 self._apply_promo_code(order, 'test_10pc')
30 self.assertEqual(len(order.order_line.ids), 3, "We should have 3 lines as we should have a new line for promo code reduction")
31 self.assertEqual(order.amount_total, 864, "Only paid product should have their price discounted")
32 order.order_line.filtered(lambda x: 'Discount' in x.name).unlink() # Remove Discount
33 order._remove_program_from_points(self.p1)
34
35 # Check free product is removed since we are below minimum required quantity
36 sol1.product_uom_qty = 2
37 self._auto_rewards(order, self.all_programs)
38 self.assertEqual(len(order.order_line.ids), 1, "Free Large Cabinet should have been removed")
39
40 # Free product in cart will be considered as paid product when changing quantity of paid product, so the free product quantity computation will be wrong.
41 # 75 Large Cabinet in cart, 25 free, set quantity to 6 Large Cabinet, you should have 2 free Large Cabinet but you get 8 because it add the 25 initial free Large Cabinet to the total paid Large Cabinet when computing (25+10 > 35 > /4 = 8 free Large Cabinet)
42 sol1.product_uom_qty = 75
43 self._auto_rewards(order, self.all_programs)
44 self.assertEqual(sum(order.order_line.filtered(lambda x: x.is_reward_line).mapped('product_uom_qty')), 25, "We should have 25 Free Large Cabinet")
45 sol1.product_uom_qty = 6
46 self._auto_rewards(order, self.all_programs)
47 self.assertEqual(sum(order.order_line.filtered(lambda x: x.is_reward_line).mapped('product_uom_qty')), 2, "We should have 2 Free Large Cabinet")
48
49 def test_program_numbers_check_eligibility(self):
50 # These tests will focus on numbers (free product qty, SO total, reduction total..)
51
52 # Check if we have enough paid product to receive free product in case of a free product that is different from the paid product required
53 # Buy A, get free b. (remember we need a paid B in cart to receive free b). If your cart is 4A 1B then you should receive 1b (you are eligible to receive 4 because you have 4A but since you dont have enought B in your cart, you are limited to the B quantity)
54 order = self.empty_order
55 sol1 = self.env['sale.order.line'].create({
56 'product_id': self.drawerBlack.id,
57 'name': 'drawer black',
58 'product_uom_qty': 3.0,
59 'order_id': order.id,
60 })
61 sol2 = self.env['sale.order.line'].create({
62 'product_id': self.largeMeetingTable.id,
63 'name': 'Large Meeting Table',
64 'product_uom_qty': 1.0,
65 'order_id': order.id,
66 })
67 self._auto_rewards(order, self.all_programs)
68 self.assertEqual(len(order.order_line.ids), 3, "We should have a 'Free Large Meeting Table' promotion line")
69 self.assertEqual(sum(order.order_line.filtered(lambda x: x.is_reward_line).mapped('product_uom_qty')), 1, "We should receive one and only one free Large Meeting Table")
70
71 # Check the required value amount to be eligible for the program is correctly computed (eg: it does not add negative value (from free product) to total)
72 # A = free b | Have your cart with A 2B b | cart value should be A + 1B but in code it is only A (free b value is subsstract 2 times)
73 # This is because _amount_all() is summing all SO lines (so + (-b.value)) and again in _check_promo_code() order.amount_untaxed + order.reward_amount | amount_untaxed has already free product value substracted (_amount_all)
74 sol1.product_uom_qty = 1
75 sol2.product_uom_qty = 2
76 self.p1.rule_ids.minimum_amount = 5000
77 self._auto_rewards(order, self.all_programs)
78 self._apply_promo_code(order, 'test_10pc')
79 self.assertEqual(len(order.order_line.ids), 4, "We should have 4 lines as we should have a new line for promo code reduction")
80
81 # Check you can still have auto applied promotion if you have a promo code set to the order
82 self.env['sale.order.line'].create({
83 'product_id': self.largeCabinet.id,
84 'name': 'Large Cabinet',
85 'product_uom_qty': 4.0,
86 'order_id': order.id,
87 })
88 self._auto_rewards(order, self.all_programs)
89 self.assertEqual(len(order.order_line.ids), 6, "We should have 2 more lines as we now have one 'Free Large Cabinet' line since we bought 4 of them")
90
91 def test_program_numbers_taxes_and_rules(self):
92 percent_tax = self.env['account.tax'].create({
93 'name': "15% Tax",
94 'amount_type': 'percent',
95 'amount': 15,
96 'price_include_override': 'tax_included',
97 })
98 p_specific_product = self.env['loyalty.program'].create({
99 'name': '20% reduction on Large Cabinet in cart',
100 'trigger': 'auto',
101 'program_type': 'promotion',
102 'applies_on': 'current',
103 'rule_ids': [(0, 0, {
104 'reward_point_mode': 'order',
105 'minimum_amount_tax_mode': 'excl',
106 'minimum_amount': 320.00,
107 })],
108 'reward_ids': [(0, 0, {
109 'reward_type': 'discount',
110 'discount': 20,
111 'discount_mode': 'percent',
112 'discount_applicability': 'specific',
113 'discount_product_ids': self.largeCabinet,
114 'required_points': 1,
115 })],
116 })
117 self.all_programs |= p_specific_product
118 order = self.empty_order
119 self.largeCabinet.taxes_id = percent_tax
120 sol1 = self.env['sale.order.line'].create({
121 'product_id': self.largeCabinet.id,
122 'name': 'Large Cabinet',
123 'product_uom_qty': 1.0,
124 'order_id': order.id,
125 })
126
127 self._auto_rewards(order, self.all_programs)
128 self.assertEqual(len(order.order_line.ids), 1, "We should not get the reduction line since we dont have 320$ tax excluded (cabinet is 320$ tax included)")
129 sol1.tax_ids.price_include_override = 'tax_excluded'
130 sol1._compute_tax_ids()
131 self.env.flush_all()
132 self._auto_rewards(order, self.all_programs)
133 self.assertEqual(len(order.order_line.ids), 2, "We should now get the reduction line since we have 320$ tax included (cabinet is 320$ tax included)")
134 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
135 # --------------------------------------------------------------------------------
136 # Conference Chair | 1 | 320.00 | 15% excl | 320.00 | 368.00 | 48.00
137 # 20% discount on | 1 | -64.00 | 15% excl | -64.00 | -73.60 | -9.60
138 # large cabinet |
139 # --------------------------------------------------------------------------------
140 # TOTAL | 256.00 | 294.40 | 38.40
141 self.assertAlmostEqual(order.amount_total, 294.4, 2, "Check discount has been applied correctly (eg: on taxes aswell)")
142
143 # test coupon with code works the same as auto applied_programs
144 p_specific_product.write({'trigger': 'with_code'})
145 p_specific_product.rule_ids.write({'mode': 'with_code', 'code': '20pc'})
146 order.order_line.filtered(lambda l: l.is_reward_line).unlink()
147 order._remove_program_from_points(p_specific_product)
148 self._auto_rewards(order, self.all_programs)
149 self.assertEqual(len(order.order_line.ids), 1, "Reduction should be removed since we deleted it and it is now a promo code usage, it shouldn't be automatically reapplied")
150
151 self._apply_promo_code(order, '20pc')
152 self._auto_rewards(order, self.all_programs)
153 self.assertEqual(len(order.order_line.ids), 2, "We should now get the reduction line since we have 320$ tax included (cabinet is 320$ tax included)")
154
155 # check discount applied only on Large Cabinet
156 self.env['sale.order.line'].create({
157 'product_id': self.drawerBlack.id,
158 'name': 'Drawer Black',
159 'product_uom_qty': 10.0,
160 'order_id': order.id,
161 })
162 self._auto_rewards(order, self.all_programs)
163 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
164 # --------------------------------------------------------------------------------
165 # Drawer Black | 10 | 25.00 | / | 250.00 | 250.00 | /
166 # Large Cabinet | 1 | 320.00 | 15% excl | 320.00 | 368.00 | 48.00
167 # 20% discount on | 1 | -64.00 | 15% excl | -64.00 | -73.60 | -9.60
168 # large cabinet |
169 # --------------------------------------------------------------------------------
170 # TOTAL | 506.00 | 544.40 | 38.40
171 self.assertEqual(order.amount_total, 544.4, "We should only get reduction on cabinet")
172 sol1.product_uom_qty = 8
173 self._auto_rewards(order, self.all_programs)
174 # Note: Since we now have 2 free Large Cabinet, we should discount only 8 of the 10 Large Cabinet in carts since we don't want to discount free Large Cabinet
175 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
176 # --------------------------------------------------------------------------------
177 # Drawer Black | 10 | 25.00 | / | 250.00 | 250.00 | /
178 # Large Cabinet | 8 | 320.00 | 15% excl | 2560.00 | 2944.00 | 384.00
179 # Free Large Cabinet | 2 | 0.00 | 15% excl | 0.00 | 0.00 | 0.00
180 # 20% discount on | 1 | -512.00 | 15% excl | -512.00 | -588.80 | -78.80
181 # large cabinet |
182 # --------------------------------------------------------------------------------
183 # TOTAL | 2298.00 | 2605.20 | 305.20
184 self.assertAlmostEqual(order.amount_total, 2605.20, 2, "Changing cabinet quantity should change discount amount correctly")
185
186 p_specific_product.reward_ids.discount_max_amount = 200
187 self._auto_rewards(order, self.all_programs)
188 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
189 # --------------------------------------------------------------------------------
190 # Drawer Black | 10 | 25.00 | / | 250.00 | 250.00 | /
191 # Large Cabinet | 8 | 320.00 | 15% excl | 2560.00 | 2944.00 | 384.00
192 # Free Large Cabinet | 2 | 0.00 | 15% excl | 0.00 | 0.00 | 0.00
193 # 20% discount on | 1 | -173.91 | 15% excl | -173.91 | -200.00 | -26.09
194 # large cabinet |
195 # limited to 200 HTVA
196 # --------------------------------------------------------------------------------
197 # TOTAL | 2636.09 | 2994.00 | 357.91
198 self.assertEqual(order.amount_total, 2994.0, "The discount should be limited to $200 tax included")
199 self.assertEqual(order.amount_untaxed, 2636.09, "The discount should be limited to $200 tax included (2)")
200
201 def test_program_numbers_one_discount_line_per_tax(self):
202 order = self.empty_order
203 self.env['ir.config_parameter'].set_param('loyalty.compute_all_discount_product_ids', 'enabled')
204 # Create taxes
205 self.tax_15pc_excl = self.env['account.tax'].create({
206 'name': "15% Tax excl",
207 'amount_type': 'percent',
208 'amount': 15,
209 })
210 self.tax_50pc_excl = self.env['account.tax'].create({
211 'name': "50% Tax excl",
212 'amount_type': 'percent',
213 'amount': 50,
214 })
215 self.tax_35pc_incl = self.env['account.tax'].create({
216 'name': "35% Tax incl",
217 'amount_type': 'percent',
218 'amount': 35,
219 'price_include_override': 'tax_included',
220 })
221
222 # Set tax and prices on products as neeed for the test
223 (self.product_A + self.largeCabinet + self.conferenceChair + self.pedalBin + self.drawerBlack).write({'list_price': 100})
224 (self.largeCabinet + self.drawerBlack).write({'taxes_id': [(4, self.tax_15pc_excl.id, False)]})
225 self.conferenceChair.taxes_id = self.tax_10pc_incl
226 self.pedalBin.taxes_id = None
227 self.product_A.taxes_id = (self.tax_35pc_incl + self.tax_50pc_excl)
228
229 # Add products in order
230 self.env['sale.order.line'].create({
231 'product_id': self.largeCabinet.id,
232 'name': 'Large Cabinet',
233 'product_uom_qty': 4.0,
234 'order_id': order.id,
235 })
236 sol2 = self.env['sale.order.line'].create({
237 'product_id': self.conferenceChair.id,
238 'name': 'Conference Chair',
239 'product_uom_qty': 3.0,
240 'order_id': order.id,
241 })
242 self.env['sale.order.line'].create({
243 'product_id': self.pedalBin.id,
244 'name': 'Pedal Bin',
245 'product_uom_qty': 5.0,
246 'order_id': order.id,
247 })
248 self.env['sale.order.line'].create({
249 'product_id': self.product_A.id,
250 'name': 'product A with multiple taxes',
251 'product_uom_qty': 3.0,
252 'order_id': order.id,
253 })
254 self.env['sale.order.line'].create({
255 'product_id': self.drawerBlack.id,
256 'name': 'Drawer Black',
257 'product_uom_qty': 2.0,
258 'order_id': order.id,
259 })
260
261 # Create needed programs
262 self.immediate_promotion_program.active = False
263 self.p2.active = False
264 self.p3.active = False
265
266 # NOTE: programs may not make much sense but they have been modified in order to validate the result since the change from coupon to loyalty.
267 self.p_large_cabinet = self.env['loyalty.program'].create({
268 'name': 'Buy 1 large cabinet, get 3/4 for free',
269 'trigger': 'auto',
270 'program_type': 'promotion',
271 'applies_on': 'current',
272 'rule_ids': [(0, 0, {
273 'product_ids': self.largeCabinet,
274 'reward_point_mode': 'unit',
275 'minimum_qty': 1,
276 'reward_point_amount': 0.752,
277 })],
278 'reward_ids': [(0, 0, {
279 'reward_type': 'product',
280 'reward_product_id': self.largeCabinet.id,
281 'reward_product_qty': 1,
282 'required_points': 1,
283 })],
284 })
285 self.p_conference_chair = self.env['loyalty.program'].create({
286 'name': 'Buy 1 chair, get one for free',
287 'trigger': 'auto',
288 'program_type': 'promotion',
289 'applies_on': 'current',
290 'rule_ids': [(0, 0, {
291 'product_ids': self.conferenceChair,
292 'reward_point_mode': 'unit',
293 'minimum_qty': 1,
294 'reward_point_amount': 0.4,
295 })],
296 'reward_ids': [(0, 0, {
297 'reward_type': 'product',
298 'reward_product_id': self.conferenceChair.id,
299 'reward_product_qty': 1,
300 'required_points': 1,
301 })],
302 })
303 self.p_pedal_bin = self.env['loyalty.program'].create({
304 'name': 'Buy 1 bin, get one for free',
305 'trigger': 'auto',
306 'program_type': 'promotion',
307 'applies_on': 'current',
308 'rule_ids': [(0, 0, {
309 'product_ids': self.pedalBin,
310 'reward_point_mode': 'unit',
311 'minimum_qty': 1,
312 })],
313 'reward_ids': [(0, 0, {
314 'reward_type': 'product',
315 'reward_product_id': self.pedalBin.id,
316 'reward_product_qty': 1,
317 'required_points': 1,
318 })],
319 })
320 self.all_programs |= (self.p_large_cabinet | self.p_conference_chair | self.p_pedal_bin)
321 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
322 # --------------------------------------------------------------------------------
323 # Conference Chair | 5 | 100.00 | 10% incl | 454.55 | 500.00 | 45.45
324 # Pedal bin | 10 | 100.00 | / | 1000.00 | 1000.00 | /
325 # Large Cabinet | 7 | 100.00 | 15% excl | 700.00 | 805.00 | 105.00
326 # Drawer Black | 2 | 100.00 | 15% excl | 200.00 | 230.00 | 30.00
327 # Product A | 3 | 100.00 | 35% incl | 222.22 | 411.11 | 188.89
328 # 50% excl
329 # --------------------------------------------------------------------------------
330 # TOTAL | 2576.77 | 2946.11 | 369.34
331
332 self.assertRecordValues(order, [{
333 'amount_total': 1901.11,
334 'amount_untaxed': 1594.95,
335 }])
336 self.assertEqual(len(order.order_line.ids), 5, "The order without any programs should have 5 lines")
337
338 # Apply all the programs
339 self._auto_rewards(order, self.all_programs)
340
341 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
342 # --------------------------------------------------------------------------------
343 # Free ConferenceChair | 2 | -100.00 | 10% incl | -181.82 | -200.00 | -18.18
344 # Free Pedal Bin | 5 | -100.00 | / | -500.00 | -500.00 | /
345 # Free Large Cabinet | 3 | -100.00 | 15% excl | -300.00 | -345.00 | -45.00
346 # --------------------------------------------------------------------------------
347 # TOTAL AFTER APPLYING FREE PRODUCT PROGRAMS | 1594.95 | 1901.11 | 306.16
348
349 self.assertRecordValues(order, [{
350 'amount_total': 1901.11,
351 'amount_untaxed': 1594.95,
352 }])
353 self.assertEqual(len(order.order_line.ids), 8, "Order should contains 5 regular product lines and 3 free product lines")
354
355 # Apply 10% on top of everything
356 self._apply_promo_code(order, 'test_10pc')
357
358 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
359 # --------------------------------------------------------------------------------
360 # 10% on tax 10% incl | 1 | -30.00 | 10% incl | -27.27 | -30.00 | -2.73
361 # 10% on no tax | 1 | -50.00 | / | -50.00 | -50.00 | /
362 # 10% on tax 15% excl | 1 | -60.00 | 15% excl | -60.00 | -69.00 | -9.00
363 # 10% on tax 35%+50% | 1 | -30.00 | 35% incl | -22.22 | -41.11 | -18.89
364 # 50% excl
365 # --------------------------------------------------------------------------------
366 # TOTAL AFTER APPLYING 10% GLOBAL PROGRAM | 1435.46 | 1711.00 | 275.54
367
368 self.assertRecordValues(order, [{
369 'amount_total': 1711.0,
370 'amount_untaxed': 1435.45,
371 }])
372 self.assertEqual(len(order.order_line.ids), 12, "Order should contains 5 regular product lines, 3 free product lines and 4 discount lines (one for every tax)")
373
374 # -- This is a test inside the test
375 order.order_line._compute_tax_ids()
376 self.assertRecordValues(order, [{
377 'amount_total': 1711.0,
378 'amount_untaxed': 1435.45,
379 }])
380 self.assertEqual(len(order.order_line.ids), 12, "Recomputing tax on sale order lines should not change number of order line")
381 self._auto_rewards(order, self.all_programs)
382 self.assertRecordValues(order, [{
383 'amount_total': 1711.0,
384 'amount_untaxed': 1435.45,
385 }])
386 self.assertEqual(len(order.order_line.ids), 12, "Recomputing tax on sale order lines should not change number of order line")
387 # -- End test inside the test
388
389 # Now we want to apply a 20% discount only on Large Cabinet
390 self.all_programs |= self.env['loyalty.program'].create({
391 'name': '20% reduction on Large Cabinet in cart',
392 'trigger': 'auto',
393 'program_type': 'promotion',
394 'applies_on': 'current',
395 'rule_ids': [(0, 0, {})],
396 'reward_ids': [(0, 0, {
397 'reward_type': 'discount',
398 'discount': 20,
399 'discount_applicability': 'specific',
400 'discount_product_ids': self.largeCabinet,
401 'required_points': 1,
402 'clear_wallet': 1,
403 })],
404 })
405 self._auto_rewards(order, self.all_programs)
406
407 # 20% on large cabinet which are already discounted by 10%
408 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
409 # --------------------------------------------------------------------------------
410 # 20% on Large Cabinet | 1 | -72.00 | 15% excl | -72.00 | -82.8 | -10.80
411 # --------------------------------------------------------------------------------
412 # TOTAL AFTER APPLYING 20% ON LARGE CABINET | 1363.46 | 1628.2 | 264.74
413
414 self.assertRecordValues(order, [{
415 'amount_total': 1628.2,
416 'amount_untaxed': 1363.45,
417 }])
418 self.assertEqual(len(order.order_line.ids), 13, "Order should have a new discount line for 20% on Large Cabinet")
419
420 # Check that if you delete one of the discount tax line, the others tax lines from the same promotion got deleted as well.
421 order.order_line.filtered(lambda l: '10%' in l.name)[0].unlink()
422 order._remove_program_from_points(self.p1)
423 self.assertEqual(len(order.order_line.ids), 9, "All of the 10% discount line per tax should be removed")
424 # At this point, removing the Conference Chair's discount line (split per tax) removed also the others discount lines
425 # linked to the same program (eg: other taxes lines). So the coupon got removed from the SO since there were no discount lines left
426
427 # Add back the coupon to continue the test flow
428 self._apply_promo_code(order, 'test_10pc')
429 self._auto_rewards(order, self.all_programs)
430 self.assertEqual(len(order.order_line.ids), 13, "The 10% discount line should be back")
431
432 # Check that if you change a product qty, his discount tax line got updated
433 self.p_conference_chair.rule_ids.reward_point_amount = 0.752
434 sol2.product_uom_qty = 4
435 self._auto_rewards(order, self.all_programs)
436 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
437 # --------------------------------------------------------------------------------
438 # Large Cabinet | 4 | 100.00 | 15% excl | 400.00 | 460.00 | 60.00
439 # Conference Chair | 4 | 100.00 | 10% incl | 363.63 | 400.00 | 36.36
440 # Pedal Bins | 5 | 100.00 | / | 500.00 | 500.00 | /
441 # Drawer Black | 2 | 100.00 | 15% excl | 200.00 | 230.00 | 30.00
442 # Product A | 3 | 100.00 | 35% incl | 222.22 | 411.11 | 188.89
443 # 50% excl
444 # Free - Large Cabinet | 3 | 0.00 | 15% excl | 0.00 | 0.00 | 0.00
445 # Free - Conference Ch | 3 | 0.00 | 10% incl | 0.00 | 0.00 | 0.00
446 # Free - Pedal Bins | 5 | 0.00 | / | 0.00 | 0.00 | /
447 # 20% on Large Cabinet | 1 | -80.00 | 15% excl | -80.00 | -92.00 | -12.00
448 # 10% on tax 15% excl | 1 | -52.00 | 15% excl | -52.00 | -59.80 | -7.80
449 # 10% on tax 10% excl | 1 | -40.00 | 15% excl | -36.36 | -40.00 | -3.64
450 # 10% on no tax | 1 | -50.00 | / | -50.00 | -50.00 | /
451 # 10% on tax 35+50% | 1 | -30.00 | 35% incl | -22.22 | -41.11 | -18.89
452 # 50% excl
453 # --------------------------------------------------------------------------------
454 # TOTAL | 1445.27 | 1718.20 | 272.92
455
456 self.assertEqual(order.amount_untaxed, 1445.27, "The order should have one more paid Conference Chair with 10% incl tax and discounted by 10%")
457
458 # Check that if you remove a product, his reward lines got removed, especially the discount per tax one
459 sol2.unlink()
460 self._auto_rewards(order, self.all_programs)
461 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
462 # --------------------------------------------------------------------------------
463 # Pedal Bins | 5 | 100.00 | / | 500.00 | 500.00 | /
464 # Large Cabinet | 4 | 100.00 | 15% excl | 400.00 | 460.00 | 60.00
465 # Drawer Black | 2 | 100.00 | 15% excl | 200.00 | 230.00 | 30.00
466 # Product A | 3 | 100.00 | 35% incl | 222.22 | 411.11 | 188.89
467 # 50% excl
468 # Pedal Bins | 5 | 0.00 | / | 0.00 | 0.00 | /
469 # Large Cabinet | 3 | 0.00 | 15% excl | 0.00 | 0.00 | 0.00
470 # 20% on Large Cabinet | 1 | -80.00 | 15% excl | -80.00 | -92.00 | -12.00
471 # 10% on tax 15% excl | 1 | -52.00 | 15% excl | -52.00 | -59.80 | -7.80
472 # 10% on no tax | 1 | -50.00 | / | -50.00 | -50.00 | /
473 # 10% on tax 35+50% | 1 | -30.00 | 35% incl | -22.22 | -41.11 | -18.89
474 # 50% excl
475 # --------------------------------------------------------------------------------
476 # TOTAL | 1118.00 | 1349.00 | 240.20
477
478 self.assertRecordValues(order, [{
479 'amount_total': 1358.2,
480 'amount_untaxed': 1118.0,
481 }])
482 self.assertEqual(len(order.order_line.ids), 10, "Order should contains 10 lines: 4 products lines, 2 free products lines and 4 discount lines")
483
484 def test_program_numbers_extras(self):
485 # Check that you can't apply a global discount promo code if there is already an auto applied global discount
486 p1_copy = self.p1.copy({'trigger': 'auto', 'name': 'Auto applied 10% global discount', 'rule_ids': [(0, 0, {})]})
487 self.all_programs |= p1_copy
488 order = self.empty_order
489 self.env['sale.order.line'].create({
490 'product_id': self.largeCabinet.id,
491 'name': 'Large Cabinet',
492 'product_uom_qty': 1.0,
493 'order_id': order.id,
494 })
495 self._auto_rewards(order, self.all_programs)
496 self.assertEqual(len(order.order_line.ids), 2, "We should get 1 Large Cabinet line and 1 10% auto applied global discount line")
497 self.assertEqual(order.amount_total, 288, "320$ - 10%")
498 with self.assertRaises(ValidationError):
499 # Can't apply a second global discount
500 self._apply_promo_code(order, 'test_10pc')
501
502 def test_program_fixed_price(self):
503 # Check fixed amount discount
504 order = self.empty_order
505 self.p3.active = False
506 fixed_amount_program = self.env['loyalty.program'].create({
507 'name': '$249 discount',
508 'trigger': 'auto',
509 'program_type': 'promotion',
510 'applies_on': 'current',
511 'rule_ids': [(0, 0, {
512 'reward_point_mode': 'order',
513 'reward_point_amount': 1,
514 })],
515 'reward_ids': [(0, 0, {
516 'reward_type': 'discount',
517 'discount': 249,
518 'discount_mode': 'per_point',
519 'discount_applicability': 'order',
520 'required_points': 1,
521 })],
522 })
523 self.all_programs |= fixed_amount_program
524 self.tax_0pc_excl = self.env['account.tax'].create({
525 'name': "0% Tax excl",
526 'amount_type': 'percent',
527 'amount': 0,
528 })
529 fixed_amount_program.reward_ids.discount_line_product_id.write({'taxes_id': [(4, self.tax_0pc_excl.id, False)]})
530 sol1 = self.env['sale.order.line'].create({
531 'product_id': self.drawerBlack.id,
532 'name': 'Drawer Black',
533 'product_uom_qty': 1.0,
534 'order_id': order.id,
535 'tax_ids': [(4, self.tax_0pc_excl.id)]
536 })
537 self._auto_rewards(order, self.all_programs)
538 self.assertEqual(order.amount_total, 0, "Total should be null. The fixed amount discount is higher than the SO total, it should be reduced to the SO total")
539 self.assertEqual(len(order.order_line.ids), 2, "There should be the product line and the reward line")
540 sol1.product_uom_qty = 17
541 self._auto_rewards(order, self.all_programs)
542 self.assertEqual(order.amount_total, 176, "Fixed amount discount should be totally deduced")
543 self.assertEqual(len(order.order_line.ids), 2, "Number of lines should be unchanged as we just recompute the reward line")
544 fixed_amount_program.write({'active': False}) # Check archived product will remove discount lines on recompute
545 self._auto_rewards(order, self.all_programs)
546 self.assertEqual(len(order.order_line.ids), 1, "Archiving the program should remove the program reward line")
547
548 def test_program_next_order(self):
549 order = self.empty_order
550 self.all_programs |= self.env['loyalty.program'].create({
551 'name': 'Free Pedal Bin if at least 1 article',
552 'trigger': 'auto',
553 'applies_on': 'future',
554 'program_type': 'promotion',
555 'rule_ids': [(0, 0, {
556 'minimum_qty': 2,
557 })],
558 'reward_ids': [(0, 0, {
559 'reward_type': 'product',
560 'reward_product_id': self.pedalBin.id,
561 'reward_product_qty': 1,
562 'required_points': 1,
563 })],
564 })
565 sol1 = self.env['sale.order.line'].create({
566 'product_id': self.largeCabinet.id,
567 'name': 'Large Cabinet',
568 'product_uom_qty': 1.0,
569 'order_id': order.id,
570 })
571 self._auto_rewards(order, self.all_programs)
572 self.assertEqual(len(order.order_line.ids), 1, "Nothing should be added to the cart")
573 self.assertEqual(len(order._get_reward_coupons()), 0, "No coupon should have been generated yet")
574
575 sol1.product_uom_qty = 2
576 self._auto_rewards(order, self.all_programs)
577 generated_coupon = order._get_reward_coupons()
578 self.assertEqual(len(order.order_line.ids), 1, "Nothing should be added to the cart (2)")
579 self.assertEqual(len(generated_coupon), 1, "A coupon should have been generated")
580 self.assertEqual(generated_coupon.points, 0, "The coupon should not have it's points already.")
581
582 sol1.product_uom_qty = 1
583 self._auto_rewards(order, self.all_programs)
584 generated_coupon = order._get_reward_coupons()
585 self.assertEqual(len(order.order_line.ids), 1, "Nothing should be added to the cart (3)")
586 self.assertEqual(len(generated_coupon), 0, "No more coupon should have been generated and the existing one should not have been deleted")
587
588 sol1.product_uom_qty = 2
589 self._auto_rewards(order, self.all_programs)
590 generated_coupon = order._get_reward_coupons()
591 self.assertEqual(len(generated_coupon), 1, "We should still have only 1 coupon as we now benefit again from the program but no need to create a new one (see next assert)")
592 self.assertEqual(generated_coupon.points, 0, "The coupon should not have it's points already.")
593 self.assertFalse(order._get_claimable_rewards(), "No rewards should be claimable")
594
595 order.action_confirm()
596 self.assertEqual(
597 generated_coupon.points, 1,
598 "The coupon should have 1 point after confirmation",
599 )
600 self.assertFalse(
601 order._get_claimable_rewards(),
602 "Next-order coupon rewards shouldn't be claimable on current order",
603 )
604
605 def test_coupon_rule_minimum_amount(self):
606 """ Ensure coupon with minimum amount rule are correctly
607 applied on orders
608 """
609 order = self.empty_order
610 self.env['sale.order.line'].create({
611 'product_id': self.conferenceChair.id,
612 'name': 'Conference Chair',
613 'product_uom_qty': 10.0,
614 'order_id': order.id,
615 })
616 self.assertEqual(order.amount_total, 165.0, "The order amount is not correct")
617 self.env['loyalty.generate.wizard'].with_context(active_id=self.discount_coupon_program.id).create({
618 'coupon_qty': 1,
619 'points_granted': 1,
620 }).generate_coupons()
621 coupon = self.discount_coupon_program.coupon_ids[0]
622 self._apply_promo_code(order, coupon.code)
623 self.assertEqual(order.amount_total, 65.0, "The coupon should be correctly applied")
624 self._auto_rewards(order, self.all_programs)
625 self.assertEqual(order.amount_total, 65.0, "The coupon should not be removed from the order")
626
627 def test_coupon_and_program_discount_fixed_amount(self):
628 """ Ensure coupon and program discount both with
629 minimum amount rule can cohexists without making
630 the order go below 0
631 """
632 order = self.empty_order
633 orderline = self.env['sale.order.line'].create({
634 'product_id': self.conferenceChair.id,
635 'name': 'Conference Chair',
636 'product_uom_qty': 10.0,
637 'order_id': order.id,
638 })
639 self.assertEqual(order.amount_total, 165.0, "The order amount is not correct")
640
641 self.env['loyalty.program'].create({
642 'name': '$100 promotion program',
643 'program_type': 'promotion',
644 'trigger': 'with_code',
645 'rule_ids': [(0, 0, {
646 'mode': 'with_code',
647 'code': 'testpromo',
648 'minimum_amount': 100,
649 })],
650 'reward_ids': [(0, 0, {
651 'reward_type': 'discount',
652 'discount': 100,
653 'discount_mode': 'per_point',
654 'discount_applicability': 'order',
655 })],
656 })
657
658 self._apply_promo_code(order, 'testpromo')
659 self.assertEqual(order.amount_total, 65.0, "The promotion program should be correctly applied")
660 self._auto_rewards(order, self.all_programs)
661 self.assertEqual(order.amount_total, 65.0, "The promotion program should not be removed after recomputation")
662
663 self.env['loyalty.generate.wizard'].with_context(active_id=self.discount_coupon_program.id).create({
664 'coupon_qty': 1,
665 'points_granted': 1,
666 }).generate_coupons()
667 coupon = self.discount_coupon_program.coupon_ids[0]
668 with self.assertRaises(ValidationError):
669 self._apply_promo_code(order, coupon.code)
670 orderline.write({'product_uom_qty': 15})
671 self._apply_promo_code(order, coupon.code)
672 self.assertEqual(order.amount_total, 47.5, "The promotion program should now be correctly applied")
673
674 orderline.write({'product_uom_qty': 5})
675 self._auto_rewards(order, self.all_programs)
676 self.assertEqual(order.amount_total, 82.5, "The promotion programs should have been removed from the order to avoid negative amount")
677
678 def test_coupon_and_coupon_discount_fixed_amount_tax_excl(self):
679 """ Ensure multiple coupon can cohexists without making
680 the order go below 0
681 * Have an order of 300 (3 lines: 1 tax excl 15%, 2 notax)
682 * Apply a coupon A of 10% discount, unconditioned
683 * Apply a coupon B of 288.5 discount, unconditioned
684 * Order should not go below 0
685 * Even applying the coupon in reverse order should yield same result
686 """
687
688 self.immediate_promotion_program.active = False
689 coupon_program = self.env['loyalty.program'].create({
690 'name': '$288.5 coupon',
691 'program_type': 'coupons',
692 'trigger': 'with_code',
693 'applies_on': 'current',
694 'reward_ids': [(0, 0, {
695 'reward_type': 'discount',
696 'discount_mode': 'per_point',
697 'discount': 288.5,
698 'discount_applicability': 'order',
699 'required_points': 1,
700 })],
701 })
702
703 order = self.empty_order
704 self.env['sale.order.line'].create([
705 {
706 'product_id': self.conferenceChair.id,
707 'name': 'Conference Chair',
708 'product_uom_qty': 1.0,
709 'price_unit': 100.0,
710 'order_id': order.id,
711 'tax_ids': [(6, 0, (self.tax_15pc_excl.id,))],
712 },
713 {
714 'product_id': self.pedalBin.id,
715 'name': 'Computer Case',
716 'product_uom_qty': 1.0,
717 'price_unit': 100.0,
718 'order_id': order.id,
719 'tax_ids': [(6, 0, [])],
720 },
721 {
722 'product_id': self.product_A.id,
723 'name': 'Computer Case',
724 'product_uom_qty': 1.0,
725 'price_unit': 100.0,
726 'order_id': order.id,
727 'tax_ids': [(6, 0, [])],
728 },
729 ])
730
731 self._apply_promo_code(order, 'test_10pc')
732 self.assertEqual(order.amount_total, 283.5, "The promotion program should be correctly applied")
733
734 self.env['loyalty.generate.wizard'].with_context(active_id=coupon_program.id).create({
735 'coupon_qty': 1,
736 'points_granted': 1,
737 }).generate_coupons()
738 coupon = coupon_program.coupon_ids
739 self._apply_promo_code(order, coupon.code)
740 self._auto_rewards(order, self.all_programs)
741 self.assertEqual(order.amount_tax, 0.0)
742 self.assertEqual(order.amount_untaxed, 0.0, "The untaxed amount should not go below 0")
743 self.assertEqual(order.amount_total, 0.0, "The promotion program should not make the order total go below 0")
744
745 order.order_line[3:].unlink() #remove all coupon
746 order._remove_program_from_points(coupon_program)
747 order._remove_program_from_points(self.p1)
748
749 self._auto_rewards(order, self.all_programs)
750 self.assertEqual(len(order.order_line), 3, "The promotion program should be removed")
751 self._apply_promo_code(order, coupon.code)
752 self.assertEqual(order.amount_total, 26.5, "The promotion program should be correctly applied")
753 self._auto_rewards(order, self.all_programs)
754 self._apply_promo_code(order, 'test_10pc')
755 self._auto_rewards(order, self.all_programs)
756 self.assertAlmostEqual(order.amount_tax, 1.14, 2)
757 self.assertEqual(order.amount_untaxed, 22.71)
758 self.assertEqual(order.amount_total, 23.85, "The promotion program should not make the order total go below 0be altered after recomputation")
759 # It should stay the same after a recompute, order matters
760 self._auto_rewards(order, self.all_programs)
761 self.assertAlmostEqual(order.amount_tax, 1.14, 2)
762 self.assertEqual(order.amount_untaxed, 22.71)
763 self.assertEqual(order.amount_total, 23.85, "The promotion program should not make the order total go below 0be altered after recomputation")
764
765 def test_coupon_and_coupon_discount_fixed_amount_tax_incl(self):
766 """ Ensure multiple coupon can cohexists without making
767 the order go below 0
768 * Have an order of 300 (3 lines: 1 tax incl 10%, 2 notax)
769 * Apply a coupon A of 10% discount, unconditioned
770 * Apply a coupon B of 290 discount, unconditioned
771 * Order should not go below 0
772 * Even applying the coupon in reverse order should yield same result
773 """
774
775 self.immediate_promotion_program.active = False
776 coupon_program = self.env['loyalty.program'].create({
777 'name': '$290 coupon',
778 'program_type': 'coupons',
779 'trigger': 'with_code',
780 'applies_on': 'current',
781 'reward_ids': [(0, 0, {
782 'reward_type': 'discount',
783 'discount_mode': 'per_point',
784 'discount': 290,
785 'discount_applicability': 'order',
786 'required_points': 1,
787 })],
788 })
789
790 order = self.empty_order
791 self.env['sale.order.line'].create([
792 {
793 'product_id': self.conferenceChair.id,
794 'name': 'Conference Chair',
795 'product_uom_qty': 1.0,
796 'price_unit': 100.0,
797 'order_id': order.id,
798 'tax_ids': [(6, 0, (self.tax_10pc_incl.id,))],
799 },
800 {
801 'product_id': self.pedalBin.id,
802 'name': 'Computer Case',
803 'product_uom_qty': 1.0,
804 'price_unit': 100.0,
805 'order_id': order.id,
806 'tax_ids': [(6, 0, [])],
807 },
808 {
809 'product_id': self.product_A.id,
810 'name': 'Computer Case',
811 'product_uom_qty': 1.0,
812 'price_unit': 100.0,
813 'order_id': order.id,
814 'tax_ids': [(6, 0, [])],
815 },
816 ])
817
818 self._apply_promo_code(order, 'test_10pc')
819 self.assertEqual(order.amount_total, 270.0, "The promotion program should be correctly applied")
820
821 self.env['loyalty.generate.wizard'].with_context(active_id=coupon_program.id).create({
822 'coupon_qty': 1,
823 'points_granted': 1,
824 }).generate_coupons()
825 coupon = coupon_program.coupon_ids
826 self._apply_promo_code(order, coupon.code)
827 self.assertEqual(order.amount_total, 0, "The promotion program should not make the order total go below 0")
828 self.assertEqual(order.amount_tax, 0)
829 self._auto_rewards(order, self.all_programs)
830 self.assertEqual(order.amount_total, 0, "The promotion program should not be altered after recomputation")
831 self.assertEqual(order.amount_tax, 0)
832
833 order.order_line[3:].unlink() #remove all coupon
834 order._remove_program_from_points(coupon_program)
835 order._remove_program_from_points(self.p1)
836
837 self._auto_rewards(order, self.all_programs)
838 self.assertEqual(len(order.order_line), 3, "The promotion program should be removed")
839 self._apply_promo_code(order, coupon.code)
840 self.assertEqual(order.amount_total, 10.0, "The promotion program should be correctly applied")
841 self._apply_promo_code(order, 'test_10pc')
842 self._auto_rewards(order, self.all_programs)
843 self.assertEqual(order.amount_total, 9.0, "The promotion program should not make the order total go below 0")
844 self.assertEqual(order.amount_tax, 0.27)
845 self.assertEqual(order.amount_untaxed, 8.73)
846 # It should stay the same after a recompute, order matters
847 self._auto_rewards(order, self.all_programs)
848 self.assertEqual(order.amount_total, 9.0, "The promotion program should not make the order total go below 0")
849 self.assertEqual(order.amount_tax, 0.27)
850 self.assertEqual(order.amount_untaxed, 8.73)
851
852 def test_program_discount_on_multiple_specific_products(self):
853 """ Ensure a discount on multiple specific products is correctly computed.
854 - Simple: Discount must be applied on all the products set on the promotion
855 - Advanced: This discount must be split by different taxes
856 """
857 order = self.empty_order
858 self.p3.active = False
859 p_specific_products = self.env['loyalty.program'].create({
860 'name': '20% reduction on Conference Chair and Drawer Black in cart',
861 'program_type': 'promotion',
862 'trigger': 'auto',
863 'applies_on': 'current',
864 'rule_ids': [(0, 0, {})],
865 'reward_ids': [(0, 0, {
866 'reward_type': 'discount',
867 'discount_mode': 'percent',
868 'discount': 25,
869 'discount_applicability': 'specific',
870 'discount_product_ids': [(6, 0, [self.conferenceChair.id, self.drawerBlack.id])],
871 'required_points': 1,
872 })],
873 })
874 self.all_programs |= p_specific_products
875
876 self.env['sale.order.line'].create({
877 'product_id': self.conferenceChair.id,
878 'name': 'Conference Chair',
879 'product_uom_qty': 4.0,
880 'order_id': order.id,
881 })
882 sol2 = self.env['sale.order.line'].create({
883 'product_id': self.drawerBlack.id,
884 'name': 'Drawer Black',
885 'product_uom_qty': 2.0,
886 'order_id': order.id,
887 })
888
889 self._auto_rewards(order, self.all_programs)
890 self.assertEqual(len(order.order_line.ids), 3, "Conference Chair + Drawer Black + 20% discount line")
891 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
892 # --------------------------------------------------------------------------------
893 # Conference Chair | 4 | 16.50 | / | 66.00 | 66.00 | 0.00
894 # Drawer Black | 2 | 25.00 | / | 50.00 | 50.00 | 0.00
895 # 25% discount | 1 | -29.00 | / | -29.00 | -29.00 | 0.00
896 # --------------------------------------------------------------------------------
897 # TOTAL | 87.00 | 87.00 | 0.00
898 self.assertEqual(order.amount_total, 87.00, "Total should be 87.00, see above comment")
899
900 # remove Drawer Black case from promotion
901 p_specific_products.reward_ids.discount_product_ids = [(6, 0, [self.conferenceChair.id])]
902 self._auto_rewards(order, self.all_programs)
903 self.assertEqual(len(order.order_line.ids), 3, "Should still be Conference Chair + Drawer Black + 20% discount line")
904 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
905 # --------------------------------------------------------------------------------
906 # Conference Chair | 4 | 16.50 | / | 66.00 | 66.00 | 0.00
907 # Drawer Black | 2 | 25.00 | / | 50.00 | 50.00 | 0.00
908 # 25% discount | 1 | -16.50 | / | -16.50 | -16.50 | 0.00
909 # --------------------------------------------------------------------------------
910 # TOTAL | 99.50 | 99.50 | 0.00
911 self.assertEqual(order.amount_total, 99.50, "The 12.50 discount from the drawer black should be gone")
912
913 # =========================================================================
914 # PART 2: Same flow but with different taxes on products to ensure discount is split per VAT
915 # Add back Drawer Black in promotion
916 p_specific_products.reward_ids.discount_product_ids = [(6, 0, [self.conferenceChair.id, self.drawerBlack.id])]
917
918 percent_tax = self.env['account.tax'].create({
919 'name': "30% Tax",
920 'amount_type': 'percent',
921 'amount': 30,
922 'price_include_override': 'tax_included',
923 })
924 sol2.tax_ids = percent_tax
925
926 self._auto_rewards(order, self.all_programs)
927 self.assertEqual(len(order.order_line.ids), 4, "Conference Chair + Drawer Black + 20% on no TVA product (Conference Chair) + 20% on 15% tva product (Drawer Black)")
928 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
929 # --------------------------------------------------------------------------------
930 # Conference Chair | 4 | 16.50 | / | 66.00 | 66.00 | 0.00
931 # Drawer Black | 2 | 25.00 | 30% incl | 38.46 | 50.00 | 11.54
932 # 25% discount | 1 | -16.50 | / | -16.50 | -16.50 | 0.00
933 # 25% discount | 1 | -12.50 | 30% incl | -9.62 | -12.50 | -2.88
934 # --------------------------------------------------------------------------------
935 # TOTAL | 78.35 | 87.00 | 8.66
936 self.assertEqual(order.amount_total, 87.00, "Total untaxed should be as per above comment")
937 self.assertEqual(order.amount_untaxed, 78.35, "Total with taxes should be as per above comment")
938
939 def test_program_numbers_free_prod_with_min_amount_and_qty_on_same_prod(self):
940 # This test focus on giving a free product based on both
941 # minimum amount and quantity condition on an
942 # auto applied promotion program
943
944 order = self.empty_order
945 self.p3.active = False
946 self.all_programs |= self.env['loyalty.program'].create({
947 'name': 'Buy 2 Chairs, get 1 free',
948 'trigger': 'auto',
949 'program_type': 'promotion',
950 'applies_on': 'current',
951 'rule_ids': [(0, 0, {
952 'product_ids': self.conferenceChair,
953 'reward_point_mode': 'order',
954 'minimum_qty': 2,
955 'minimum_amount': self.conferenceChair.lst_price * 2,
956 })],
957 'reward_ids': [(0, 0, {
958 'reward_type': 'product',
959 'reward_product_id': self.conferenceChair.id,
960 'reward_product_qty': 1,
961 'required_points': 1,
962 })],
963 })
964 sol1 = self.env['sale.order.line'].create({
965 'product_id': self.conferenceChair.id,
966 'name': 'Conf Chair',
967 'product_uom_qty': 1.0,
968 'order_id': order.id,
969 })
970 sol2 = self.env['sale.order.line'].create({
971 'product_id': self.drawerBlack.id,
972 'name': 'Drawer',
973 'product_uom_qty': 1.0,
974 'order_id': order.id,
975 }) # dummy line
976
977 self._auto_rewards(order, self.all_programs)
978 self.assertEqual(len(order.order_line.ids), 2, "The promotion lines should not be applied")
979 sol1.write({'product_uom_qty': 2.0})
980 self._auto_rewards(order, self.all_programs)
981 self.assertEqual(len(order.order_line.ids), 3, "The promotion lines should have been added")
982 self.assertEqual(order.amount_total, self.conferenceChair.lst_price * (sol1.product_uom_qty) + self.drawerBlack.lst_price * sol2.product_uom_qty, "The promotion line was not applied to the amount total")
983 sol2.unlink()
984 self._auto_rewards(order, self.all_programs)
985 self.assertEqual(len(order.order_line.ids), 2, "The other product should not affect the promotion")
986 self.assertEqual(order.amount_total, self.conferenceChair.lst_price * (sol1.product_uom_qty), "The promotion line was not applied to the amount total")
987 sol1.write({'product_uom_qty': 1.0})
988 self._auto_rewards(order, self.all_programs)
989 self.assertEqual(len(order.order_line.ids), 1, "The promotion lines should have been removed")
990
991 def test_program_step_percentages(self):
992 # test step-like percentages increase over amount
993 testprod = self.env['product.product'].create({
994 'name': 'testprod',
995 'lst_price': 118.0,
996 })
997
998 self.all_programs |= self.env['loyalty.program'].create({
999 'name': '10% discount',
1000 'trigger': 'auto',
1001 'program_type': 'promotion',
1002 'applies_on': 'current',
1003 'rule_ids': [(0, 0, {
1004 'reward_point_mode': 'order',
1005 'minimum_amount': 1500.00,
1006 'minimum_amount_tax_mode': 'incl',
1007 })],
1008 'reward_ids': [(0, 0, {
1009 'reward_type': 'discount',
1010 'discount': 10,
1011 'discount_mode': 'percent',
1012 'discount_applicability': 'order',
1013 'required_points': 1,
1014 })],
1015 })
1016 self.all_programs |= self.env['loyalty.program'].create({
1017 'name': '15% discount',
1018 'trigger': 'auto',
1019 'program_type': 'promotion',
1020 'applies_on': 'current',
1021 'rule_ids': [(0, 0, {
1022 'reward_point_mode': 'order',
1023 'minimum_amount': 1750.00,
1024 'minimum_amount_tax_mode': 'incl',
1025 })],
1026 'reward_ids': [(0, 0, {
1027 'reward_type': 'discount',
1028 'discount': 15,
1029 'discount_mode': 'percent',
1030 'discount_applicability': 'order',
1031 'required_points': 1,
1032 })],
1033 })
1034 self.all_programs |= self.env['loyalty.program'].create({
1035 'name': '20% discount',
1036 'trigger': 'auto',
1037 'program_type': 'promotion',
1038 'applies_on': 'current',
1039 'rule_ids': [(0, 0, {
1040 'reward_point_mode': 'order',
1041 'minimum_amount': 2000.00,
1042 'minimum_amount_tax_mode': 'incl',
1043 })],
1044 'reward_ids': [(0, 0, {
1045 'reward_type': 'discount',
1046 'discount': 20,
1047 'discount_mode': 'percent',
1048 'discount_applicability': 'order',
1049 'required_points': 1,
1050 })],
1051 })
1052 self.all_programs |= self.env['loyalty.program'].create({
1053 'name': '25% discount',
1054 'trigger': 'auto',
1055 'program_type': 'promotion',
1056 'applies_on': 'current',
1057 'rule_ids': [(0, 0, {
1058 'reward_point_mode': 'order',
1059 'minimum_amount': 2500.00,
1060 'minimum_amount_tax_mode': 'incl',
1061 })],
1062 'reward_ids': [(0, 0, {
1063 'reward_type': 'discount',
1064 'discount': 25,
1065 'discount_mode': 'percent',
1066 'discount_applicability': 'order',
1067 'required_points': 1,
1068 })],
1069 })
1070
1071 #apply 10%
1072 order = self.empty_order
1073 order_line = self.env['sale.order.line'].create({
1074 'product_id': testprod.id,
1075 'name': 'testprod',
1076 'product_uom_qty': 14.0,
1077 'price_unit': 118.0,
1078 'order_id': order.id,
1079 'tax_ids': False,
1080 })
1081 self._auto_rewards(order, self.all_programs)
1082 self.assertEqual(order.amount_total, 1486.80, "10% discount should be applied")
1083 self.assertEqual(len(order.order_line.ids), 2, "discount should be applied")
1084
1085 #switch to 15%
1086 order_line.write({'product_uom_qty': 15})
1087 self.assertEqual(order.amount_total, 1604.8, "Discount improperly applied")
1088 self.assertEqual(len(order.order_line.ids), 2, "No discount applied while it should")
1089
1090 #switch to 20%
1091 order_line.write({'product_uom_qty': 17})
1092 self._auto_rewards(order, self.all_programs)
1093 self.assertEqual(order.amount_total, 1604.8, "Discount improperly applied")
1094 self.assertEqual(len(order.order_line.ids), 2, "No discount applied while it should")
1095
1096 #still 20%
1097 order_line.write({'product_uom_qty': 20})
1098 self._auto_rewards(order, self.all_programs)
1099 self.assertEqual(order.amount_total, 1888.0, "Discount improperly applied")
1100 self.assertEqual(len(order.order_line.ids), 2, "No discount applied while it should")
1101
1102 #back to 10%
1103 order_line.write({'product_uom_qty': 14})
1104 self._auto_rewards(order, self.all_programs)
1105 self.assertEqual(order.amount_total, 1486.80, "Discount improperly applied")
1106 self.assertEqual(len(order.order_line.ids), 2, "No discount applied while it should")
1107
1108 def test_program_free_prods_with_min_qty_and_reward_qty_and_rule(self):
1109 order = self.empty_order
1110 coupon_program = self.env['loyalty.program'].create({
1111 'name': '2 free conference chair if at least 1 large cabinet',
1112 'trigger': 'with_code',
1113 'program_type': 'coupons',
1114 'applies_on': 'current',
1115 'rule_ids': [(0, 0, {
1116 'product_ids': self.largeCabinet,
1117 'reward_point_mode': 'order',
1118 'minimum_qty': 1,
1119 })],
1120 'reward_ids': [(0, 0, {
1121 'reward_type': 'discount',
1122 'discount': 100,
1123 'discount_mode': 'percent',
1124 'discount_applicability': 'specific',
1125 'discount_product_ids': self.conferenceChair,
1126 'discount_max_amount': 200,
1127 'required_points': 1,
1128 })],
1129 })
1130 # set large cabinet and conference chair prices
1131 self.largeCabinet.write({'list_price': 500, 'sale_ok': True,})
1132 self.conferenceChair.write({'list_price': 100, 'sale_ok': True})
1133
1134 # create SOL
1135 self.env['sale.order.line'].create({
1136 'product_id': self.largeCabinet.id,
1137 'name': 'Large Cabinet',
1138 'product_uom_qty': 1.0,
1139 'order_id': order.id,
1140 })
1141 sol2 = self.env['sale.order.line'].create({
1142 'product_id': self.conferenceChair.id,
1143 'name': 'Conference chair',
1144 'product_uom_qty': 2.0,
1145 'order_id': order.id,
1146 })
1147
1148 self.assertEqual(len(order.order_line), 2, 'The order must contain 2 order lines since the coupon is not yet applied')
1149 self.assertEqual(order.amount_total, 700.0, 'The price must be 500.0 since the coupon is not yet applied')
1150
1151 # generate and apply coupon
1152 self.env['loyalty.generate.wizard'].with_context(active_id=coupon_program.id).create({
1153 'coupon_qty': 1,
1154 'points_granted': 1,
1155 }).generate_coupons()
1156 coupon = coupon_program.coupon_ids
1157 self._apply_promo_code(order, coupon.code)
1158
1159 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
1160 # --------------------------------------------------------------------------------
1161 # Conference Chair | 2 | 100.00 | / | 200.00 | 200.00 | /
1162 # Large Cabinet | 1 | 500.00 | / | 500.00 | 500.00 | /
1163 #
1164 # Free Conference Chair | 2 | -100.00 | / | -200.00 | -200.00 | /
1165 # --------------------------------------------------------------------------------
1166 # TOTAL | 500.00 | 500.00 | /
1167
1168 self.assertEqual(len(order.order_line), 3, 'The order must contain 3 order lines including one for free conference chair')
1169 self.assertEqual(order.amount_total, 500.0, 'The price must be 500.0 since two conference chairs are free')
1170 self.assertEqual(order.order_line[2].price_total, -200.0, 'The last order line should apply a reduction of 200.0 since there are two conference chairs that cost 100.0 each')
1171
1172 # prevent user to get illicite discount by decreasing the to 1 the reward product qty after applying the coupon
1173 sol2.product_uom_qty = 1.0
1174 self._auto_rewards(order, self.all_programs)
1175
1176 # in this case user should not have -200.0
1177 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
1178 # --------------------------------------------------------------------------------
1179 # Conference Chair | 1 | 100.00 | / | 100.00 | 100.00 | /
1180 # Large Cabine | 1 | 500.00 | / | 500.00 | 500.00 | /
1181 #
1182 # Free Conference Chair | 2 | -100.00 | / | -200.00 | -200.00 | /
1183 # --------------------------------------------------------------------------------
1184 # TOTAL | 400.00 | 400.00 | /
1185
1186
1187 # he should rather have this one
1188 # Name | Qty | price_unit | Tax | HTVA | TVAC | TVA |
1189 # --------------------------------------------------------------------------------
1190 # Conference Chair | 1 | 100.00 | / | 100.00 | 100.00 | /
1191 # Large Cabinet | 1 | 500.00 | / | 500.00 | 500.00 | /
1192 #
1193 # Free Conference Chair | 1 | -100.00 | / | -100.00 | -100.00 | /
1194 # --------------------------------------------------------------------------------
1195 # TOTAL | 500.00 | 500.00 | /
1196
1197 self.assertEqual(order.amount_total, 500.0, 'The price must be 500.0 since two conference chairs are free and the user only bought one')
1198 self.assertEqual(order.order_line[2].price_total, -100.0, 'The last order line should apply a reduction of 100.0 since there is one conference chair that cost 100.0')
1199
1200 def test_program_free_product_different_than_rule_product_with_multiple_application(self):
1201 order = self.empty_order
1202
1203 self.p3.active = False
1204 self.all_programs |= self.env['loyalty.program'].create({
1205 'name': 'Buy 1 drawer black, get a free Large Meeting Table',
1206 'trigger': 'auto',
1207 'program_type': 'promotion',
1208 'applies_on': 'current',
1209 'rule_ids': [(0, 0, {
1210 'product_ids': self.drawerBlack,
1211 'reward_point_mode': 'order',
1212 'minimum_qty': 1,
1213 })],
1214 'reward_ids': [(0, 0, {
1215 'reward_type': 'discount',
1216 'discount': 100,
1217 'discount_mode': 'percent',
1218 'discount_applicability': 'specific',
1219 'discount_product_ids': self.largeMeetingTable,
1220 'required_points': 1,
1221 })],
1222 })
1223
1224 self.env['sale.order.line'].create({
1225 'product_id': self.drawerBlack.id,
1226 'product_uom_qty': 2.0,
1227 'order_id': order.id,
1228 })
1229 sol_B = self.env['sale.order.line'].create({
1230 'product_id': self.largeMeetingTable.id,
1231 'product_uom_qty': 1.0,
1232 'order_id': order.id,
1233 })
1234
1235 self._auto_rewards(order, self.all_programs)
1236
1237 self.assertEqual(len(order.order_line), 3, 'The order must contain 3 order lines: 1x for Black Drawer, 1x for Large Meeting Table and 1x for free Large Meeting Table')
1238 self.assertEqual(order.amount_total, self.drawerBlack.list_price * 2, 'The price must be 50.0 since the Large Meeting Table is free: 2*25.00 (Black Drawer) + 1*40000.00 (Large Meeting Table) - 1*40000.00 (free Large Meeting Table)')
1239
1240 sol_B.product_uom_qty = 2
1241
1242 self._auto_rewards(order, self.all_programs)
1243
1244 self.assertEqual(len(order.order_line), 3, 'The order must contain 3 order lines: 1x for Black Drawer, 1x for Large Meeting Table and 1x for free Large Meeting Table')
1245 self.assertEqual(order.amount_total, self.drawerBlack.list_price * 2, 'The price must be 50.0 since the 2 Large Meeting Table are free: 2*25.00 (Black Drawer) + 2*40000.00 (Large Meeting Table) - 2*40000.00 (free Large Meeting Table)')
1246
1247 def test_program_modify_reward_line_qty(self):
1248 order = self.empty_order
1249 product_F = self.env['product.product'].create({
1250 'name': 'Product F',
1251 'list_price': 100,
1252 'sale_ok': True,
1253 'taxes_id': [(6, 0, [])],
1254 })
1255 self.all_programs |= self.env['loyalty.program'].create({
1256 'name': '1 Product F = 5$ discount',
1257 'trigger': 'auto',
1258 'program_type': 'promotion',
1259 'applies_on': 'current',
1260 'rule_ids': [(0, 0, {
1261 'product_ids': product_F,
1262 'reward_point_mode': 'order',
1263 'minimum_qty': 1,
1264 })],
1265 'reward_ids': [(0, 0, {
1266 'reward_type': 'discount',
1267 'discount': 5,
1268 'discount_mode': 'per_point',
1269 'required_points': 1,
1270 })],
1271 })
1272
1273 self.env['sale.order.line'].create({
1274 'product_id': product_F.id,
1275 'product_uom_qty': 2.0,
1276 'order_id': order.id,
1277 })
1278
1279 self._auto_rewards(order, self.all_programs)
1280
1281 self.assertEqual(len(order.order_line), 2, 'The order must contain 2 order lines: 1x Product F and 1x 5$ discount')
1282 self.assertEqual(order.amount_total, 195.0, 'The price must be 195.0 since there is a 5$ discount and 2x Product F')
1283 self.assertEqual(sum(order.order_line.filtered(lambda x: x.is_reward_line).mapped('product_uom_qty')), 1, 'The reward line should have a quantity of 1 since Fixed Amount discounts apply only once per Sale Order')
1284
1285 order.order_line[1].product_uom_qty = 2
1286
1287 self.assertEqual(len(order.order_line), 2, 'The order must contain 2 order lines: 1x Product F and 1x 5$ discount')
1288 self.assertEqual(order.amount_total, 190.0, 'The price must be 190.0 since there is now 2x 5$ discount and 2x Product F')
1289 self.assertEqual(order.order_line.filtered(lambda x: x.is_reward_line).price_unit, -5, 'The discount unit price should still be -5 after the quantity was manually changed')
1290
1291 def test_program_multi_product_max_discount(self):
1292 order = self.empty_order
1293 coupon_program = self.env['loyalty.program'].create({
1294 'name': "50% off for cheapest product(max $30)",
1295 'trigger': 'with_code',
1296 'program_type': 'coupons',
1297 'reward_ids': [(0, 0, {
1298 'reward_type': 'discount',
1299 'discount': 50,
1300 'discount_mode': 'percent',
1301 'discount_applicability': 'cheapest',
1302 'discount_max_amount': 30,
1303 })],
1304 })
1305
1306 # create SOL
1307 self.env['sale.order.line'].create({
1308 'product_id': self.largeCabinet.id,
1309 'product_uom_qty': 2.0,
1310 'order_id': order.id,
1311 })
1312
1313 # generate and apply coupon
1314 self.env['loyalty.generate.wizard'].with_context(active_id=coupon_program.id).create({
1315 'coupon_qty': 1,
1316 'points_granted': 1,
1317 }).generate_coupons()
1318
1319 coupon = coupon_program.coupon_ids
1320 self._apply_promo_code(order, coupon.code)
1321
1322 self.assertEqual(len(order.order_line), 2, "The order must contain 2 order lines")
1323 self.assertEqual(
1324 order.amount_total, 610.0, "The price must be 610.0 since the max discount is 30"
1325 )
1326
1327 def test_specific_discount_product_group(self):
1328 # Tests the following:
1329 # 1 program: -5$ on [A, B]
1330 # 1 program: -10$ on A
1331 # Order with A (6$) B (4$) C (10$)
1332 # Apply both coupons -> order total should be 10$
1333 # Apply a 10% discount -> order total should be 9$
1334 # Redo the same process but discount first
1335 product_a, product_b, product_c = self.env['product.product'].create([
1336 {
1337 'name': 'Product A',
1338 'list_price': 6,
1339 'sale_ok': True,
1340 'taxes_id': [(6, 0, [])],
1341 },
1342 {
1343 'name': 'Product B',
1344 'list_price': 4,
1345 'sale_ok': True,
1346 'taxes_id': [(6, 0, [])],
1347 },
1348 {
1349 'name': 'Product C',
1350 'list_price': 10,
1351 'sale_ok': True,
1352 'taxes_id': [(6, 0, [])],
1353 },
1354 ])
1355 programs = self.env['loyalty.program'].create([
1356 {
1357 'name': '-5 USD on [A, B]',
1358 'trigger': 'auto',
1359 'program_type': 'promotion',
1360 'applies_on': 'current',
1361 'rule_ids': [(0, 0, {
1362 })],
1363 'reward_ids': [(0, 0, {
1364 'reward_type': 'discount',
1365 'discount': 5,
1366 'discount_mode': 'per_point',
1367 'discount_applicability': 'specific',
1368 'discount_product_ids': product_a | product_b,
1369 'required_points': 1,
1370 })],
1371 },
1372 {
1373 'name': '-10 USD on A',
1374 'trigger': 'auto',
1375 'program_type': 'promotion',
1376 'applies_on': 'current',
1377 'rule_ids': [(0, 0, {
1378 })],
1379 'reward_ids': [(0, 0, {
1380 'reward_type': 'discount',
1381 'discount': 10,
1382 'discount_mode': 'per_point',
1383 'discount_applicability': 'specific',
1384 'discount_product_ids': product_a,
1385 'required_points': 1,
1386 })],
1387 },
1388 ])
1389 order = self.empty_order
1390 self.env['sale.order.line'].create([
1391 {
1392 'product_id': product_a.id,
1393 'name': 'Product A',
1394 'product_uom_qty': 1,
1395 'order_id': order.id,
1396 },
1397 {
1398 'product_id': product_b.id,
1399 'name': 'Product B',
1400 'product_uom_qty': 1,
1401 'order_id': order.id,
1402 },
1403 {
1404 'product_id': product_c.id,
1405 'name': 'Product C',
1406 'product_uom_qty': 1,
1407 'order_id': order.id,
1408 },
1409 ])
1410 self._auto_rewards(order, programs)
1411 self.assertEqual(order.amount_total, 10, "The total should be 10$.")
1412 # Try to apply another 10%
1413 self._apply_promo_code(order, 'test_10pc')
1414 self.assertEqual(order.amount_total, 9, "The total should be 9$.")
1415 # Now the order way around
1416 order.order_line.filtered('reward_id').unlink()
1417 self._apply_promo_code(order, 'test_10pc')
1418 self.assertEqual(order.amount_total, 18, "The total should be 9$.")
1419 self._auto_rewards(order, programs)
1420 self.assertEqual(order.amount_total, 9, "The total should be 9$.")
1421
1422 def test_specific_discount_multiple_taxes(self):
1423 # Check the following setup
1424 # Product A 10$ 10% tva excl
1425 # Product B 10$ 20% tva excl
1426 # Program A -100% on product A
1427 # Program B -5$ fixed on both products
1428 # Applying both programs in a different order should result in a different
1429 # outcome since discountable amounts are computed per tax
1430 # Applying program A before B should yield a better final price
1431 product_a, product_b = self.env['product.product'].create([
1432 {
1433 'name': 'Product A',
1434 'list_price': 10,
1435 'sale_ok': True,
1436 'taxes_id': [(6, 0, [self.tax_10pc_excl.id])],
1437 },
1438 {
1439 'name': 'Product B',
1440 'list_price': 10,
1441 'sale_ok': True,
1442 'taxes_id': [(6, 0, [self.tax_20pc_excl.id])],
1443 },
1444 ])
1445 program_a, program_b = self.env['loyalty.program'].create([
1446 {
1447 'name': '-100% on A',
1448 'trigger': 'auto',
1449 'program_type': 'promotion',
1450 'applies_on': 'current',
1451 'rule_ids': [(0, 0, {
1452 })],
1453 'reward_ids': [(0, 0, {
1454 'reward_type': 'discount',
1455 'discount': 100,
1456 'discount_mode': 'percent',
1457 'discount_applicability': 'specific',
1458 'discount_product_ids': product_a,
1459 'required_points': 1,
1460 })],
1461 },
1462 {
1463 'name': '-5 USD on [A, B]',
1464 'trigger': 'auto',
1465 'program_type': 'promotion',
1466 'applies_on': 'current',
1467 'rule_ids': [(0, 0, {
1468 })],
1469 'reward_ids': [(0, 0, {
1470 'reward_type': 'discount',
1471 'discount': 5,
1472 'discount_mode': 'per_point',
1473 'discount_applicability': 'specific',
1474 'discount_product_ids': product_a | product_b,
1475 'required_points': 1,
1476 })],
1477 },
1478 ])
1479
1480 order = self.empty_order
1481 self.env['sale.order.line'].create([
1482 {
1483 'product_id': product_a.id,
1484 'name': 'Product A',
1485 'product_uom_qty': 1,
1486 'order_id': order.id,
1487 },
1488 {
1489 'product_id': product_b.id,
1490 'name': 'Product B',
1491 'product_uom_qty': 1,
1492 'order_id': order.id,
1493 },
1494 ])
1495 self._auto_rewards(order, program_a)
1496 self.assertEqual(order.amount_total, 12, 'Total should be 12$')
1497 self._auto_rewards(order, program_b)
1498 self.assertAlmostEqual(order.amount_total, 7, 0, 'Total should be 7$')
1499 # Now the order way around
1500 order.order_line.filtered('reward_id').unlink()
1501 self._auto_rewards(order, program_b)
1502 self.assertAlmostEqual(order.amount_total, 18, 0, 'Total should be 18$')
1503 self._auto_rewards(order, program_a)
1504 # We essentially create a discount of -100% off of an already discounted product
1505 # (11 - 2.4) = 8.6$ discount ~
1506 self.assertAlmostEqual(order.amount_total, 9.4, 1, 'Total should be 9.4$')
1507
1508 def test_fixed_amount_taxes_attribution(self):
1509 program = self.env['loyalty.program'].create({
1510 'name': '-5 USD',
1511 'trigger': 'auto',
1512 'program_type': 'promotion',
1513 'applies_on': 'current',
1514 'rule_ids': [(0, 0, {
1515 })],
1516 'reward_ids': [(0, 0, {
1517 'reward_type': 'discount',
1518 'discount': 5,
1519 'discount_mode': 'per_point',
1520 'discount_applicability': 'order',
1521 'required_points': 1,
1522 })],
1523 })
1524
1525 order = self.empty_order
1526 sol = self.env['sale.order.line'].create({
1527 'product_id': self.drawerBlack.id,
1528 'price_unit': 10,
1529 'product_uom_qty': 1.0,
1530 'order_id': order.id,
1531 })
1532
1533 self._auto_rewards(order, program)
1534
1535 self.assertEqual(order.amount_total, 5, 'Price should be 10$ - 5$(discount) = 5$')
1536 self.assertEqual(order.amount_tax, 0, 'No taxes are applied yet')
1537
1538 sol.tax_ids = self.tax_10pc_base_incl
1539 self._auto_rewards(order, program)
1540
1541 self.assertEqual(order.amount_total, 5, 'Price should be 10$ - 5$(discount) = 5$')
1542 self.assertEqual(float_compare(order.amount_tax, 5 / 11, precision_rounding=3), 0, '10% Tax included in 5$')
1543
1544 sol.tax_ids = self.tax_10pc_excl
1545 self._auto_rewards(order, program)
1546
1547 # Value is 5.99 instead of 6 because you cannot have 6 with 10% tax excluded and a precision rounding of 2
1548 self.assertAlmostEqual(order.amount_total, 6, 1, msg='Price should be 11$ - 5$(discount) = 6$')
1549 self.assertEqual(float_compare(order.amount_tax, 6 / 11, precision_rounding=3), 0, '10% Tax included in 6$')
1550
1551 sol.tax_ids = self.tax_20pc_excl
1552 self._auto_rewards(order, program)
1553
1554 self.assertEqual(order.amount_total, 7, 'Price should be 12$ - 5$(discount) = 7$')
1555 self.assertEqual(float_compare(order.amount_tax, 7 / 12, precision_rounding=3), 0, '20% Tax included on 7$')
1556
1557 sol.tax_ids = self.tax_10pc_base_incl + self.tax_10pc_excl
1558 self._auto_rewards(order, program)
1559
1560 self.assertAlmostEqual(order.amount_total, 6, 1, msg='Price should be 11$ - 5$(discount) = 6$')
1561 self.assertEqual(float_compare(order.amount_tax, 6 / 12, precision_rounding=3), 0, '20% Tax included on 6$')
1562
1563 def test_fixed_amount_taxes_attribution_multiline(self):
1564
1565 program = self.env['loyalty.program'].create({
1566 'name': '-5 USD',
1567 'trigger': 'auto',
1568 'program_type': 'promotion',
1569 'applies_on': 'current',
1570 'rule_ids': [(0, 0, {
1571 })],
1572 'reward_ids': [(0, 0, {
1573 'reward_type': 'discount',
1574 'discount': 5,
1575 'discount_mode': 'per_point',
1576 'discount_applicability': 'order',
1577 'required_points': 1,
1578 })],
1579 })
1580
1581 order = self.empty_order
1582 sol1 = self.env['sale.order.line'].create({
1583 'product_id': self.drawerBlack.id,
1584 'price_unit': 10,
1585 'product_uom_qty': 1.0,
1586 'order_id': order.id,
1587 })
1588 sol2 = self.env['sale.order.line'].create({
1589 'product_id': self.drawerBlack.id,
1590 'price_unit': 10,
1591 'product_uom_qty': 1.0,
1592 'order_id': order.id,
1593 })
1594
1595 self._auto_rewards(order, program)
1596
1597 self.assertAlmostEqual(order.amount_total, 15, 1, msg='Price should be 20$ - 5$(discount) = 15$')
1598 self.assertEqual(order.amount_tax, 0, 'No taxes are applied yet')
1599
1600 sol1.tax_ids = self.tax_10pc_base_incl
1601 self._auto_rewards(order, program)
1602
1603 self.assertAlmostEqual(order.amount_total, 15, 1, msg='Price should be 20$ - 5$(discount) = 15$')
1604 self.assertEqual(float_compare(order.amount_tax, 5 / 11 + 0, precision_rounding=3), 0,
1605 '10% Tax included in 5$ in sol1 (highest cost) and 0 in sol2')
1606
1607 sol2.tax_ids = self.tax_10pc_excl
1608 self._auto_rewards(order, program)
1609
1610 self.assertAlmostEqual(order.amount_total, 16, 1, msg='Price should be 21$ - 5$(discount) = 16$')
1611 # Tax amount = 10% in 10$ + 10% in 11$ - 10% in 5$ (apply on excluded)
1612 self.assertEqual(float_compare(order.amount_tax, 5 / 11, precision_rounding=3), 0)
1613
1614 sol2.tax_ids = self.tax_10pc_base_incl + self.tax_10pc_excl
1615 self._auto_rewards(order, program)
1616
1617 self.assertAlmostEqual(order.amount_total, 16, 1, msg='Price should be 21$ - 5$(discount) = 16$')
1618 # Promo apply on line 2 (10% inc + 10% exc)
1619 # Tax amount = 10% in 10$ + 10% in 10$ + 10% in 11 - 10% in 5$ - 10% in 4.55$ (100/110*5)
1620 # = 10/11 + 10/11 + 11/11 - 5/11 - 4.55/11
1621 # = 21.45/11
1622 self.assertEqual(float_compare(order.amount_tax, 21.45 / 11, precision_rounding=3), 0)
1623
1624 sol3 = self.env['sale.order.line'].create({
1625 'product_id': self.drawerBlack.id,
1626 'price_unit': 10,
1627 'product_uom_qty': 1.0,
1628 'order_id': order.id,
1629 })
1630 sol3.tax_ids = self.tax_10pc_excl
1631 self._auto_rewards(order, program)
1632
1633 self.assertAlmostEqual(order.amount_total, 27, 1, msg='Price should be 32$ - 5$(discount) = 27$')
1634 # Promo apply on line 2 (10% inc + 10% exc)
1635 # Tax amount = 10% in 10$ + 10% in 10$ + 10% in 11$ + 10% in 11$ - 10% in 5$ - 10% in 4.55$ (100/110*5)
1636 # = 10/11 + 10/11 + 11/11 + 11/11 - 5/11 - 4.55/11
1637 # = 32.45/11
1638 self.assertEqual(float_compare(order.amount_tax, 32.45 / 11, precision_rounding=3), 0)
1639
1640 def test_fixed_amount_with_negative_cost(self):
1641 program = self.env['loyalty.program'].create({
1642 'name': '-10 USD',
1643 'trigger': 'auto',
1644 'program_type': 'promotion',
1645 'applies_on': 'current',
1646 'rule_ids': [(0, 0, {
1647 })],
1648 'reward_ids': [(0, 0, {
1649 'reward_type': 'discount',
1650 'discount': 10,
1651 'discount_mode': 'per_point',
1652 'discount_applicability': 'order',
1653 'required_points': 1,
1654 })],
1655 })
1656
1657 order = self.empty_order
1658
1659 sol1 = self.env['sale.order.line'].create({
1660 'product_id': self.drawerBlack.id,
1661 'price_unit': 10,
1662 'product_uom_qty': 1.0,
1663 'order_id': order.id,
1664 })
1665
1666 self.env['sale.order.line'].create({
1667 'product_id': self.drawerBlack.id,
1668 'name': 'hand discount',
1669 'price_unit': -5,
1670 'product_uom_qty': 1.0,
1671 'order_id': order.id,
1672 })
1673
1674 self._auto_rewards(order, program)
1675
1676 self.assertEqual(len(order.order_line), 3, 'Promotion should add 1 line')
1677 self.assertEqual(order.amount_total, 0, '10$ discount should cover the whole price')
1678
1679 sol1.price_unit = 20
1680 self._auto_rewards(order, program)
1681
1682 self.assertEqual(len(order.order_line), 3, 'Promotion should add 1 line')
1683 self.assertEqual(order.amount_total, 5, '10$ discount should be applied on top of the 15$ original price')
1684
1685 def test_fixed_amount_change_promo_amount(self):
1686 program = self.env['loyalty.program'].create({
1687 'name': '-10 USD',
1688 'trigger': 'auto',
1689 'program_type': 'promotion',
1690 'applies_on': 'current',
1691 'rule_ids': [(0, 0, {
1692 })],
1693 'reward_ids': [(0, 0, {
1694 'reward_type': 'discount',
1695 'discount': 10,
1696 'discount_mode': 'per_point',
1697 'discount_applicability': 'order',
1698 'required_points': 1,
1699 })],
1700 })
1701
1702 order = self.empty_order
1703
1704 self.env['sale.order.line'].create({
1705 'product_id': self.drawerBlack.id,
1706 'price_unit': 10,
1707 'product_uom_qty': 1.0,
1708 'order_id': order.id,
1709 })
1710
1711 self._auto_rewards(order, program)
1712
1713 self.assertEqual(len(order.order_line), 2, 'Promotion should add 1 line')
1714 self.assertEqual(order.amount_total, 0, '10$ - 10$(discount) = 0$(total) ')
1715
1716 program.reward_ids.discount = 5
1717 self._auto_rewards(order, program)
1718
1719 self.assertEqual(len(order.order_line), 2, 'Promotion should add 1 line')
1720 self.assertEqual(order.amount_total, 5, '10$ - 5$(discount) = 5$(total) ')
1721
1722 def test_fixed_tax_not_affected(self):
1723 program = self.env['loyalty.program'].create({
1724 'name': '50% discount',
1725 'program_type': 'promotion',
1726 'trigger': 'auto',
1727 'applies_on': 'current',
1728 'rule_ids': [(0, 0, {
1729 })],
1730 'reward_ids': [(0, 0, {
1731 'reward_type': 'discount',
1732 'discount_mode': 'percent',
1733 'discount': 50,
1734 'discount_applicability': 'order',
1735 'required_points': 1,
1736 })],
1737 })
1738
1739 order = self.empty_order
1740 # Create taxes
1741 self.tax_15pc_excl = self.env['account.tax'].create({
1742 'name': "15% Tax excl",
1743 'amount_type': 'percent',
1744 'amount': 15,
1745 })
1746 self.tax_10_fixed = self.env['account.tax'].create({
1747 'name': "10$ Fixed tax",
1748 'amount_type': 'fixed',
1749 'amount': 10,
1750 })
1751
1752 # Set tax and prices on products as neeed for the test
1753 self.product_A.write({'list_price': 100})
1754 self.product_A.taxes_id = (self.tax_15pc_excl + self.tax_10_fixed)
1755
1756 # Add products in order
1757 self.env['sale.order.line'].create({
1758 'product_id': self.product_A.id,
1759 'name': 'product A',
1760 'product_uom_qty': 1.0,
1761 'order_id': order.id,
1762 })
1763
1764 self._auto_rewards(order, program)
1765
1766 self.assertEqual(len(order.order_line), 2, 'Promotion should add 1 line')
1767 self.assertEqual(order.amount_total, 67.5, '100$ + 15% tax + 10$ tax - 50%(discount) = 67.5$(total) ')
1768 self.assertEqual(order.amount_tax, 17.5, '15% tax + 10$ tax$ - 50%$(discount) = 17.5$(total) ')
1769
1770 def test_fixed_tax_not_affected_2(self):
1771 program = self.env['loyalty.program'].create({
1772 'name': '50$ discount',
1773 'program_type': 'promotion',
1774 'trigger': 'auto',
1775 'applies_on': 'current',
1776 'rule_ids': [(0, 0, {
1777 })],
1778 'reward_ids': [(0, 0, {
1779 'reward_type': 'discount',
1780 'discount_mode': 'per_order',
1781 'discount': 50,
1782 'discount_applicability': 'order',
1783 'required_points': 1,
1784 })],
1785 })
1786
1787 order = self.empty_order
1788 # Create taxes
1789 self.tax_15pc_excl = self.env['account.tax'].create({
1790 'name': "15% Tax excl",
1791 'amount_type': 'percent',
1792 'amount': 15,
1793 })
1794 self.tax_10_fixed = self.env['account.tax'].create({
1795 'name': "10$ Fixed tax",
1796 'amount_type': 'fixed',
1797 'amount': 10,
1798 })
1799
1800 # Set tax and prices on products as neeed for the test
1801 self.product_A.write({'list_price': 100})
1802 self.product_A.taxes_id = (self.tax_15pc_excl + self.tax_10_fixed)
1803
1804 # Add products in order
1805 self.env['sale.order.line'].create({
1806 'product_id': self.product_A.id,
1807 'name': 'product A',
1808 'product_uom_qty': 1.0,
1809 'order_id': order.id,
1810 })
1811
1812 self._auto_rewards(order, program)
1813
1814 self.assertEqual(len(order.order_line), 2, 'Promotion should add 1 line')
1815 self.assertEqual(order.amount_total, 75, '100$ + 15% tax + 10$ tax - 50$(discount) = 75$(total) ')
1816
1817 def test_loyalty_card_tax_total(self):
1818 loyalty_program = self.env['loyalty.program'].create({
1819 'name': 'Test loyalty card',
1820 'program_type': 'loyalty',
1821 'trigger': 'auto',
1822 'applies_on': 'both',
1823 'rule_ids': [Command.create({
1824 'reward_point_mode': 'money',
1825 'reward_point_amount': 0.01,
1826 })],
1827 'reward_ids': [Command.create({
1828 'reward_type': 'discount',
1829 'discount_mode': 'per_point',
1830 'discount': 1,
1831 'discount_applicability': 'cheapest',
1832 'required_points': 1,
1833 })],
1834 })
1835 order = self.empty_order
1836 self.env['loyalty.card'].create([{
1837 'program_id': loyalty_program.id,
1838 'partner_id': order.partner_id.id,
1839 'points': 3.39,
1840 }])
1841
1842 # Create taxes
1843 tax_15pc_excl = self.env['account.tax'].create({
1844 'name': "15% Tax excl",
1845 'amount_type': 'percent',
1846 'amount': 15,
1847 })
1848
1849 # Set tax and prices on products as neeed for the test
1850 self.product_A.write({
1851 'list_price': 140.0,
1852 'taxes_id': [Command.set(tax_15pc_excl.ids)]
1853 })
1854
1855 order.order_line = [
1856 Command.create({
1857 'product_id': self.product_A.id,
1858 }),
1859 ]
1860
1861 self._auto_rewards(order, loyalty_program)
1862
1863 self.assertEqual(len(order.order_line), 2, 'Promotion should add 1 line')
1864 self.assertEqual(order.order_line[0].tax_ids, tax_15pc_excl)
1865 self.assertEqual(order.order_line[1].tax_ids, tax_15pc_excl)
1866 self.assertEqual(order.amount_total, 156.0, '140$ + 15% - 5$ = 156$')
1867
1868 def test_rounded_used_loyalty_points(self):
1869 """Check that the loyalty points used in a reward are rounded according to the currency."""
1870 loyalty_program = self.env['loyalty.program'].create({
1871 'name': 'Test loyalty card',
1872 'program_type': 'loyalty',
1873 'trigger': 'auto',
1874 'applies_on': 'both',
1875 'rule_ids': [Command.set([])],
1876 'reward_ids': [Command.create({
1877 'reward_type': 'discount',
1878 'discount_mode': 'per_point',
1879 'discount': 0.03,
1880 'discount_applicability': 'order',
1881 'required_points': 1,
1882 })],
1883 })
1884 order = self.empty_order
1885 self.env['loyalty.card'].create([{
1886 'program_id': loyalty_program.id,
1887 'partner_id': order.partner_id.id,
1888 'points': 3030,
1889 }])
1890 product_a = self._create_product(
1891 name='product_a',
1892 lst_price=3000.0,
1893 taxes_id=[Command.set([])],
1894 )
1895 order.order_line = [Command.create({'product_id': product_a.id})]
1896
1897 coupon = loyalty_program.coupon_ids[0]
1898 order._apply_program_reward(loyalty_program.reward_ids[0], coupon)
1899 order.action_confirm()
1900 self.assertEqual(len(order.order_line), 2, 'Promotion should add 1 line')
1901 used_points = coupon.history_ids[0].used
1902 self.assertEqual(used_points, coupon.currency_id.round(used_points))
1903
1904 def test_apply_order_and_specific_discounts(self):
1905 """Ensure you can apply a full-order discount, and then a product-specific discount."""
1906 order_program, specific_program = self.env['loyalty.program'].create([
1907 {
1908 'name': "$50 discount",
1909 'program_type': 'promotion',
1910 'trigger': 'auto',
1911 'applies_on': 'current',
1912 'rule_ids': [Command.create({})],
1913 'reward_ids': [Command.create({
1914 'reward_type': 'discount',
1915 'discount_mode': 'per_order',
1916 'discount': 50,
1917 'discount_applicability': 'order',
1918 'required_points': 1,
1919 })],
1920 },
1921 {
1922 'name': "$10 discount on Pedal Bin",
1923 'program_type': 'promotion',
1924 'trigger': 'auto',
1925 'applies_on': 'current',
1926 'rule_ids': [Command.create({})],
1927 'reward_ids': [Command.create({
1928 'reward_type': 'discount',
1929 'discount_mode': 'per_order',
1930 'discount': 10,
1931 'discount_applicability': 'specific',
1932 'discount_product_ids': self.pedalBin.ids,
1933 'required_points': 1,
1934 })],
1935 },
1936 ])
1937 order = self.empty_order
1938 order.order_line = [Command.create({
1939 'product_id': self.pedalBin.id,
1940 'tax_ids': self.tax_20pc_excl.ids,
1941 })]
1942
1943 self.assertAlmostEqual(
1944 order.amount_total,
1945 self.pedalBin.list_price * (1 + self.tax_20pc_excl.amount / 100), # $56.4
1946 msg="Order total should equal product list price plus taxes",
1947 )
1948
1949 self._auto_rewards(order, order_program)
1950 self.assertAlmostEqual(
1951 order.amount_total,
1952 self.pedalBin.list_price * (1 + self.tax_20pc_excl.amount / 100) - 50, # $6.4
1953 msg="The order total should be $50 less than initially after the discount is applied.",
1954 )
1955
1956 self._auto_rewards(order, specific_program)
1957 self.assertFalse(
1958 order.amount_total,
1959 "Order total should be 0, as a specific discount should have been applied.",
1960 )