Debian Codegraph @

addons/sale_loyalty/tests/common.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from collections import defaultdict
4
5 from odoo.exceptions import ValidationError
6 from odoo.fields import Command
7
8 from odoo.addons.sale.tests.common import SaleCommon
9
10
11 class TestSaleCouponCommon(SaleCommon):
12
13 @classmethod
14 def setUpClass(cls):
15 super().setUpClass()
16
17 # set currency to not rely on demo data and avoid possible race condition
18 cls.currency_ratio = 1.0
19
20 # Set all the existing programs to active=False to avoid interference
21 cls.env['loyalty.program'].search([]).sudo().write({'active': False})
22
23 # Taxes
24 tax_group_group = cls.env['account.tax.group'].create({
25 'name': 'Test Account Tax Group'
26 })
27 cls.tax_15pc_excl = cls.env['account.tax'].create({
28 'name': "Tax 15%",
29 'amount_type': 'percent',
30 'amount': 15,
31 'type_tax_use': 'sale',
32 'tax_group_id': tax_group_group.id,
33 })
34
35 cls.tax_10pc_incl = cls.env['account.tax'].create({
36 'name': "10% Tax incl",
37 'amount_type': 'percent',
38 'amount': 10,
39 'price_include_override': 'tax_included',
40 'tax_group_id': tax_group_group.id,
41 })
42
43 cls.tax_10pc_base_incl = cls.env['account.tax'].create({
44 'name': "10% Tax incl base amount",
45 'amount_type': 'percent',
46 'amount': 10,
47 'price_include_override': 'tax_included',
48 'include_base_amount': True,
49 'tax_group_id': tax_group_group.id,
50 })
51
52 cls.tax_10pc_excl = cls.env['account.tax'].create({
53 'name': "10% Tax excl",
54 'amount_type': 'percent',
55 'amount': 10,
56 'price_include_override': 'tax_excluded',
57 'tax_group_id': tax_group_group.id,
58 })
59
60 cls.tax_20pc_excl = cls.env['account.tax'].create({
61 'name': "20% Tax excl",
62 'amount_type': 'percent',
63 'amount': 20,
64 'price_include_override': 'tax_excluded',
65 'tax_group_id': tax_group_group.id,
66 })
67
68 cls.tax_group = cls.env['account.tax'].create({
69 'name': "tax_group",
70 'amount_type': 'group',
71 'children_tax_ids': [Command.set((cls.tax_10pc_incl + cls.tax_10pc_base_incl).ids)],
72 'tax_group_id': tax_group_group.id,
73 })
74
75 #products
76 cls.product_A = cls.env['product.product'].create({
77 'name': 'Product A',
78 'list_price': 100,
79 'sale_ok': True,
80 'taxes_id': [(6, 0, [cls.tax_15pc_excl.id])],
81 })
82
83 cls.product_B = cls.env['product.product'].create({
84 'name': 'Product B',
85 'list_price': 5,
86 'sale_ok': True,
87 'taxes_id': [(6, 0, [cls.tax_15pc_excl.id])],
88 })
89
90 cls.product_C = cls.env['product.product'].create({
91 'name': 'Product C',
92 'list_price': 100,
93 'sale_ok': True,
94 'taxes_id': [(6, 0, [])],
95 })
96
97 cls.product_D = cls.env['product.product'].create({
98 'name': 'Product D',
99 'list_price': 100,
100 'sale_ok': True,
101 'taxes_id': [(6, 0, [cls.tax_group.id])],
102 })
103
104 cls.product_gift_card = cls.env['product.product'].create({
105 'name': 'Gift Card 50',
106 'type': 'service',
107 'list_price': 50,
108 'sale_ok': True,
109 'taxes_id': False,
110 })
111
112 # Immediate Program By A + B: get B free
113 # No Conditions
114 cls.program_gift_card = cls.env['loyalty.program'].create({
115 'name': 'Gift Cards',
116 'applies_on': 'future',
117 'program_type': 'gift_card',
118 'trigger': 'auto',
119 'rule_ids': [(0, 0, {
120 'product_ids': cls.product_gift_card,
121 'reward_point_amount': 1,
122 'reward_point_mode': 'money',
123 'reward_point_split': True,
124 })],
125 'reward_ids': [(0, 0, {
126 'reward_type': 'discount',
127 'discount': 1,
128 'discount_mode': 'per_point',
129 'discount_applicability': 'order',
130 })]
131 })
132 cls.immediate_promotion_program = cls.env['loyalty.program'].create({
133 'name': 'Buy A + 1 B, 1 B are free',
134 'program_type': 'promotion',
135 'applies_on': 'current',
136 'company_id': cls.env.company.id,
137 'trigger': 'auto',
138 'rule_ids': [(0, 0, {
139 'product_ids': cls.product_A,
140 'reward_point_amount': 1,
141 'reward_point_mode': 'order',
142 'minimum_qty': 1,
143 })],
144 'reward_ids': [(0, 0, {
145 'reward_type': 'product',
146 'reward_product_id': cls.product_B.id,
147 'reward_product_qty': 1,
148 'required_points': 1,
149 })],
150 })
151 cls.code_promotion_program = cls.env['loyalty.program'].create({
152 'name': 'Buy 1 A + Enter code, 1 A is free',
153 'program_type': 'coupons',
154 'trigger': 'with_code',
155 'applies_on': 'current',
156 'company_id': cls.env.company.id,
157 'rule_ids': [(0, 0, {
158 'product_ids': cls.product_A,
159 'reward_point_amount': 1,
160 'reward_point_mode': 'order',
161 'minimum_qty': 1,
162 })],
163 'reward_ids': [(0, 0, {
164 'reward_type': 'product',
165 'reward_product_id': cls.product_A.id,
166 'reward_product_qty': 1,
167 'required_points': 1,
168 })],
169 })
170 cls.code_promotion_program_with_discount = cls.env['loyalty.program'].create({
171 'name': 'Buy 1 C + Enter code, 10 percent discount on C',
172 'program_type': 'coupons',
173 'trigger': 'with_code',
174 'applies_on': 'current',
175 'company_id': cls.env.company.id,
176 'rule_ids': [(0, 0, {
177 'mode': 'with_code',
178 'code': 'promotion_code_disc',
179 'product_ids': cls.product_C,
180 'reward_point_amount': 1,
181 'reward_point_mode': 'order',
182 'minimum_qty': 1,
183 })],
184 'reward_ids': [(0, 0, {
185 'reward_type': 'discount',
186 'discount_mode': 'percent',
187 'discount': 10,
188 'discount_applicability': 'order',
189 'required_points': 1,
190 })],
191 })
192
193 def _extract_rewards_from_claimable(self, status):
194 rewards = self.env['loyalty.reward']
195 for info in status.values():
196 for reward_count in info['rewards']:
197 rewards |= reward_count[0]
198
199 def _apply_promo_code(self, order, code, no_reward_fail=True):
200 status = order._try_apply_code(code)
201 if 'error' in status:
202 raise ValidationError(status['error'])
203 if not status and no_reward_fail:
204 # Can happen if global discount got filtered out in `_get_claimable_rewards`
205 raise ValidationError('No reward to claim with this coupon')
206 coupons = self.env['loyalty.card']
207 rewards = self.env['loyalty.reward']
208 for coupon, coupon_rewards in status.items():
209 coupons |= coupon
210 rewards |= coupon_rewards
211 if len(coupons) == 1 and len(rewards) == 1:
212 status = order._apply_program_reward(rewards, coupons)
213 if 'error' in status:
214 raise ValidationError(status['error'])
215 elif len(coupons) == 1 and len(rewards) > 1:
216 return rewards
217
218 def _claim_reward(self, order, program, coupon=False):
219 if len(program.reward_ids) != 1:
220 return False
221 coupon = coupon or order.coupon_point_ids.coupon_id.filtered(lambda c: c.program_id == program)
222 if len(coupon) != 1:
223 return False
224 status = order._apply_program_reward(program.reward_ids, coupon)
225 return 'error' not in status
226
227 def _auto_rewards(self, order, programs):
228 order._update_programs_and_rewards()
229 coupons_per_program = defaultdict(lambda: self.env['loyalty.card'])
230 for coupon in order.coupon_point_ids.coupon_id:
231 coupons_per_program[coupon.program_id] |= coupon
232 for program in programs:
233 if len(program.reward_ids) > 1 or len(coupons_per_program[program]) != 1 or not program.active:
234 continue
235 self._claim_reward(order, program, coupons_per_program[program])
236
237 def _generate_coupons(self, loyality_program, coupon_qty=1):
238 self.env['loyalty.generate.wizard'].with_context(active_id=loyality_program.id).create({
239 'coupon_qty': coupon_qty,
240 }).generate_coupons()
241 return loyality_program.coupon_ids
242
243 class TestSaleCouponNumbersCommon(TestSaleCouponCommon):
244 @classmethod
245 def setUpClass(cls):
246 super().setUpClass()
247
248 cls.largeCabinet = cls.env['product.product'].create({
249 'name': 'Large Cabinet',
250 'list_price': 320.0,
251 'taxes_id': False,
252 })
253 cls.conferenceChair = cls.env['product.product'].create({
254 'name': 'Conference Chair',
255 'list_price': 16.5,
256 'taxes_id': False,
257 })
258 cls.pedalBin = cls.env['product.product'].create({
259 'name': 'Pedal Bin',
260 'list_price': 47.0,
261 'taxes_id': False,
262 })
263 cls.drawerBlack = cls.env['product.product'].create({
264 'name': 'Drawer Black',
265 'list_price': 25.0,
266 'taxes_id': False,
267 })
268 cls.largeMeetingTable = cls.env['product.product'].create({
269 'name': 'Large Meeting Table',
270 'list_price': 40000.0,
271 'taxes_id': False,
272 })
273
274 cls.p1 = cls.env['loyalty.program'].create({
275 'name': 'Code for 10% on orders',
276 'trigger': 'with_code',
277 'program_type': 'promotion',
278 'applies_on': 'current',
279 'rule_ids': [(0, 0, {
280 'mode': 'with_code',
281 'code': 'test_10pc',
282 })],
283 'reward_ids': [(0, 0, {
284 'reward_type': 'discount',
285 'discount_mode': 'percent',
286 'discount': 10,
287 'discount_applicability': 'order',
288 'required_points': 1,
289 })],
290 })
291 cls.p2 = cls.env['loyalty.program'].create({
292 'name': 'Buy 3 cabinets, get one for free',
293 'trigger': 'auto',
294 'program_type': 'promotion',
295 'applies_on': 'current',
296 'rule_ids': [(0, 0, {
297 'product_ids': cls.largeCabinet,
298 'reward_point_mode': 'unit',
299 'minimum_qty': 3,
300 })],
301 'reward_ids': [(0, 0, {
302 'reward_type': 'product',
303 'reward_product_id': cls.largeCabinet.id,
304 'reward_product_qty': 1,
305 'required_points': 3,
306 })],
307 })
308 cls.p3 = cls.env['loyalty.program'].create({
309 'name': 'Buy 1 drawer black, get a free Large Meeting Table',
310 'trigger': 'auto',
311 'program_type': 'promotion',
312 'applies_on': 'current',
313 'rule_ids': [(0, 0, {
314 'product_ids': cls.drawerBlack,
315 'reward_point_mode': 'order',
316 'minimum_qty': 1,
317 })],
318 'reward_ids': [(0, 0, {
319 'reward_type': 'product',
320 'reward_product_id': cls.largeMeetingTable.id,
321 'reward_product_qty': 1,
322 'required_points': 1,
323 })],
324 })
325 cls.discount_coupon_program = cls.env['loyalty.program'].create({
326 'name': '$100 coupon',
327 'program_type': 'coupons',
328 'trigger': 'with_code',
329 'applies_on': 'current',
330 'rule_ids': [(0, 0, {
331 'minimum_amount': 100,
332 })],
333 'reward_ids': [(0, 0, {
334 'reward_type': 'discount',
335 'discount_mode': 'per_point',
336 'discount': 100,
337 'discount_applicability': 'order',
338 'required_points': 1,
339 })],
340 })
341 cls.all_programs = cls.env['loyalty.program'].search([])