Debian Codegraph @

addons/event_sale/tests/test_event_sale.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo.addons.event_sale.tests.common import TestEventSaleCommon
5 from odoo.addons.mail.tests.common import mail_new_test_user
6 from odoo.exceptions import ValidationError
7 from odoo.tests import tagged
8 from odoo.tests.common import users
9
10
11 @tagged('event_flow')
12 class TestEventSale(TestEventSaleCommon):
13
14 @classmethod
15 def setUpClass(cls):
16 super(TestEventSale, cls).setUpClass()
17
18 product = cls.env['product.product'].create({
19 'name': 'Event',
20 'type': 'service',
21 'service_tracking': 'event',
22 })
23
24 cls.user_salesperson = mail_new_test_user(cls.env, login='user_salesman', groups='sales_team.group_sale_salesman')
25
26 cls.ticket = cls.env['event.event.ticket'].create({
27 'name': 'First Ticket',
28 'product_id': cls.event_product.id,
29 'seats_max': 30,
30 'event_id': cls.event_0.id,
31 })
32
33 cls.event_0.write({
34 'event_ticket_ids': [
35 (6, 0, cls.ticket.ids),
36 (0, 0, {
37 'name': 'Second Ticket',
38 'product_id': cls.event_product.id,
39 })
40 ],
41 })
42
43 cls.sale_order = cls.env['sale.order'].create({
44 'partner_id': cls.env['res.partner'].create({'name': 'Test Partner'}).id,
45 'note': 'Invoice after delivery',
46 'payment_term_id': cls.env.ref('account.account_payment_term_end_following_month').id
47 })
48
49 # In the sales order I add some sales order lines. i choose event product
50 cls.env['sale.order.line'].create({
51 'product_id': product.id,
52 'price_unit': 190.50,
53 'order_id': cls.sale_order.id,
54 'event_id': cls.event_0.id,
55 'event_ticket_id': cls.ticket.id,
56 })
57
58 cls.register_person = cls.env['registration.editor'].create({
59 'sale_order_id': cls.sale_order.id,
60 'event_registration_ids': [(0, 0, {
61 'event_id': cls.event_0.id,
62 'name': 'Administrator',
63 'email': '[email protected]',
64 'sale_order_line_id': cls.sale_order.order_line.id,
65 })],
66 })
67
68 # make a SO for a customer, selling some tickets
69 cls.customer_so = cls.env['sale.order'].with_user(cls.user_sales_salesman).create({
70 'partner_id': cls.event_customer.id,
71 })
72
73 cls.env['account.tax.group'].create(
74 {'name': 'Test Account Tax Group', 'company_id': cls.env.company.id}
75 )
76
77 @users('user_sales_salesman')
78 def test_adjusted_quantity_in_sale_order(self):
79 """ This test ensures that when reducing the quantity of tickets for an event, we will cancel the
80 registrations for those tickets too.
81 """
82 customer_so = self.customer_so.with_user(self.env.user)
83 ticket = self.event_0.event_ticket_ids[0]
84
85 customer_so.write({
86 'order_line': [
87 (0, 0, {
88 'event_id': self.event_0.id,
89 'event_ticket_id': ticket.id,
90 'product_id': ticket.product_id.id,
91 'product_uom_qty': 3,
92 'price_unit': 10,
93 })
94 ]
95 })
96
97 editor = self.env['registration.editor'].with_context({'default_sale_order_id': customer_so.id}).create({})
98 editor.action_make_registration()
99
100 registration_to_cancel = self.event_0.registration_ids[0]
101 registration_to_cancel.action_cancel()
102
103 registrations = self.env['event.registration'].search([('sale_order_id', '=', customer_so.id)])
104
105 expected_states = ['draft', 'draft', 'cancel']
106 actual_states = registrations.sorted('id').mapped('state')
107
108 self.assertListEqual(actual_states, expected_states, "One of the registrations should be cancelled.")
109
110 @users('user_sales_salesman')
111 def test_event_crm_sale(self):
112 TICKET1_COUNT, TICKET2_COUNT = 3, 1
113 customer_so = self.customer_so.with_user(self.env.user)
114 ticket1 = self.event_0.event_ticket_ids[0]
115 ticket2 = self.event_0.event_ticket_ids[1]
116
117 # PREPARE SO DATA
118 # ------------------------------------------------------------
119
120 # adding some tickets to SO
121 customer_so.write({
122 'order_line': [
123 (0, 0, {
124 'event_id': self.event_0.id,
125 'event_ticket_id': ticket1.id,
126 'product_id': ticket1.product_id.id,
127 'product_uom_qty': TICKET1_COUNT,
128 'price_unit': 10,
129 }), (0, 0, {
130 'event_id': self.event_0.id,
131 'event_ticket_id': ticket2.id,
132 'product_id': ticket2.product_id.id,
133 'product_uom_qty': TICKET2_COUNT,
134 'price_unit': 50,
135 })
136 ]
137 })
138 ticket1_line = customer_so.order_line.filtered(lambda line: line.event_ticket_id == ticket1)
139 ticket2_line = customer_so.order_line.filtered(lambda line: line.event_ticket_id == ticket2)
140 self.assertEqual(customer_so.amount_untaxed, TICKET1_COUNT * 10 + TICKET2_COUNT * 50)
141
142 # one existing registration for first ticket
143 ticket1_reg1 = self.env['event.registration'].create({
144 'event_id': self.event_0.id,
145 'event_ticket_id': ticket1.id,
146 'partner_id': self.event_customer2.id,
147 'sale_order_id': customer_so.id,
148 'sale_order_line_id': ticket1_line.id,
149 })
150 self.assertEqual(ticket1_reg1.partner_id, self.event_customer)
151 for field in ['name', 'email', 'phone']:
152 self.assertEqual(ticket1_reg1[field], self.event_customer[field])
153
154 # EVENT REGISTRATION EDITOR
155 # ------------------------------------------------------------
156
157 # use event registration editor to create missing lines and update details
158 editor = self.env['registration.editor'].with_context({
159 'default_sale_order_id': customer_so.id
160 }).create({})
161 self.assertEqual(len(editor.event_registration_ids), TICKET1_COUNT + TICKET2_COUNT)
162 self.assertEqual(editor.sale_order_id, customer_so)
163 self.assertEqual(editor.event_registration_ids.sale_order_line_id, ticket1_line | ticket2_line)
164
165 # check line linked to existing registration (ticket1_reg1)
166 ticket1_editor_reg1 = editor.event_registration_ids.filtered(lambda line: line.registration_id)
167 for field in ['name', 'email', 'phone']:
168 self.assertEqual(ticket1_editor_reg1[field], ticket1_reg1[field])
169
170 # check new lines
171 ticket1_editor_other = editor.event_registration_ids.filtered(lambda line: not line.registration_id and line.event_ticket_id == ticket1)
172 self.assertEqual(len(ticket1_editor_other), 2)
173 ticket2_editor_other = editor.event_registration_ids.filtered(lambda line: not line.registration_id and line.event_ticket_id == ticket2)
174 self.assertEqual(len(ticket2_editor_other), 1)
175
176 # update lines in editor and save them
177 ticket1_editor_other[0].write({
178 'name': 'ManualEntry1',
179 'email': '[email protected]',
180 'phone': '+32456111111',
181 })
182 ticket1_editor_other[1].write({
183 'name': 'ManualEntry2',
184 'email': '[email protected]',
185 })
186
187 editor.action_make_registration()
188
189 # check editor correctly created new registrations with information coming from it or SO as fallback
190 self.assertEqual(len(self.event_0.registration_ids), TICKET1_COUNT + TICKET2_COUNT)
191 new_registrations = self.event_0.registration_ids - ticket1_reg1
192 self.assertEqual(new_registrations.sale_order_id, customer_so)
193 ticket1_new_reg = new_registrations.filtered(lambda reg: reg.event_ticket_id == ticket1)
194 ticket2_new_reg = new_registrations.filtered(lambda reg: reg.event_ticket_id == ticket2)
195 self.assertEqual(len(ticket1_new_reg), 2)
196 self.assertEqual(len(ticket2_new_reg), 1)
197 self.assertEqual(
198 set(ticket1_new_reg.mapped('name')),
199 set(['ManualEntry1', 'ManualEntry2'])
200 )
201 self.assertEqual(
202 set(ticket1_new_reg.mapped('email')),
203 set(['[email protected]', '[email protected]'])
204 )
205 self.assertEqual(
206 set(ticket1_new_reg.mapped('phone')),
207 set(['+32456111111', self.event_customer._phone_format(fname='phone')])
208 )
209 for field in ['name', 'email']:
210 self.assertEqual(ticket2_new_reg[field], self.event_customer[field])
211 self.assertEqual(ticket2_new_reg['phone'], self.event_customer._phone_format(fname='phone'))
212
213 # ADDING MANUAL LINES ON SO
214 # ------------------------------------------------------------
215
216 ticket2_line.write({'product_uom_qty': 3})
217
218 # Whenever the quantity is modified the price is recomputed in function of the ticket,
219 # so we reapply the wanted price.
220 ticket2_line.write({'price_unit': 50})
221
222 editor_action = customer_so.action_confirm()
223 self.assertEqual(customer_so.state, 'sale')
224 self.assertEqual(customer_so.amount_untaxed, TICKET1_COUNT * 10 + (TICKET2_COUNT + 2) * 50)
225
226 # check confirm of SO correctly created new registrations with information coming from SO
227 self.assertEqual(len(self.event_0.registration_ids), 6) # 3 for each ticket now
228 new_registrations = self.event_0.registration_ids - (ticket1_reg1 | ticket1_new_reg | ticket2_new_reg)
229 self.assertEqual(new_registrations.event_ticket_id, ticket2)
230 self.assertEqual(new_registrations.partner_id, self.customer_so.partner_id)
231
232 self.assertEqual(editor_action['type'], 'ir.actions.act_window')
233 self.assertEqual(editor_action['res_model'], 'registration.editor')
234
235 @users('user_sales_salesman')
236 def test_registration_state_on_so_confirmation(self):
237 """Test that registration stays in draft after SO confirmation
238 and only moves to open after registration editor confirmation.
239 """
240 customer_so = self.customer_so.with_user(self.env.user)
241 ticket = self.event_0.event_ticket_ids[0]
242
243 # Create SO in draft with a ticket
244 customer_so.write({
245 'order_line': [(0, 0, {
246 'event_id': self.event_0.id,
247 'event_ticket_id': ticket.id,
248 'product_id': ticket.product_id.id,
249 'product_uom_qty': 1,
250 'price_unit': 10,
251 })]
252 })
253 customer_so.action_confirm()
254
255 registrations = self.env['event.registration'].search([
256 ('sale_order_id', '=', customer_so.id)
257 ])
258 self.assertEqual(registrations.state, 'draft')
259
260 editor = self.env['registration.editor'].with_context({
261 'default_sale_order_id': customer_so.id
262 }).create({})
263 editor.action_make_registration()
264
265 registrations = self.env['event.registration'].search([
266 ('sale_order_id', '=', customer_so.id)
267 ])
268 self.assertEqual(len(registrations), 1)
269 self.assertEqual(registrations.state, 'open')
270
271 @users('user_sales_salesman')
272 def test_event_sale_free_confirm(self):
273 """Check that free registrations are immediately
274 confirmed if the seats are available.
275 """
276 TICKET_COUNT = 3
277 customer_so = self.customer_so.with_user(self.env.user)
278 ticket = self.event_0.event_ticket_ids[0]
279
280 # Limiting seats
281 self.event_0.write({
282 "seats_limited": True,
283 "seats_max": 5
284 })
285
286 customer_so.write({
287 'order_line': [
288 (0, 0, {
289 'event_id': self.event_0.id,
290 'event_ticket_id': ticket.id,
291 'product_id': ticket.product_id.id,
292 'product_uom_qty': TICKET_COUNT,
293 'price_unit': 0,
294 })
295 ]
296 })
297
298 editor = self.env['registration.editor'].with_context({
299 'default_sale_order_id': customer_so.id
300 }).create({})
301
302 editor.action_make_registration()
303 self.assertEqual(len(self.event_0.registration_ids), TICKET_COUNT)
304 self.assertTrue(all(reg.state == "open" for reg in self.event_0.registration_ids))
305
306 def test_event_sale_free_no_saleorder(self):
307 registration = self.env['event.registration'].create({
308 'event_id': self.event_0.id,
309 'partner_id': self.event_customer2.id,
310 })
311 self.assertEqual(registration.sale_status, 'free')
312
313 @users('user_sales_salesman')
314 def test_event_sale_free_full_event_no_confirm(self):
315 """Check that even free registrations are not confirmed if there are not
316 enough seats available for the event.
317 """
318 TICKET_COUNT = 3
319 customer_so = self.customer_so.with_user(self.env.user)
320 ticket = self.event_0.event_ticket_ids[0]
321
322 # Limiting event seats
323 self.event_0.write({
324 "seats_limited": True,
325 "seats_max": 2
326 })
327
328 # adding too many tickets to SO in two different lines
329 customer_so.write({
330 'order_line': [
331 (0, 0, {
332 'event_id': self.event_0.id,
333 'event_ticket_id': ticket.id,
334 'product_id': ticket.product_id.id,
335 'product_uom_qty': TICKET_COUNT - 1,
336 'price_unit': 0,
337 }),
338 (0, 0, {
339 'event_id': self.event_0.id,
340 'event_ticket_id': ticket.id,
341 'product_id': ticket.product_id.id,
342 'product_uom_qty': 1,
343 'price_unit': 0,
344 })
345 ]
346 })
347
348 # Confirming the SO will raise an error if there is not enough seats
349 with self.assertRaises(ValidationError):
350 customer_so.action_confirm()
351
352 editor = self.env['registration.editor'].with_context({
353 'default_sale_order_id': customer_so.id
354 }).create({})
355
356 with self.assertRaises(ValidationError):
357 editor.action_make_registration()
358
359 @users('user_sales_salesman')
360 def test_event_sale_free_full_ticket_no_confirm(self):
361 """Check that even free registrations are not confirmed if there are not enough
362 seats available for the requested tickets.
363 """
364 TICKET_COUNT = 3
365 customer_so = self.customer_so.with_user(self.env.user)
366 ticket = self.event_0.event_ticket_ids[0]
367
368 # Limiting ticket seats
369 ticket.write({
370 "seats_limited": True,
371 "seats_max": 2,
372 })
373 # adding too many tickets to SO in two different lines
374 customer_so.write({
375 'order_line': [
376 (0, 0, {
377 'event_id': self.event_0.id,
378 'event_ticket_id': ticket.id,
379 'product_id': ticket.product_id.id,
380 'product_uom_qty': TICKET_COUNT - 1,
381 'price_unit': 0,
382 }),
383 (0, 0, {
384 'event_id': self.event_0.id,
385 'event_ticket_id': ticket.id,
386 'product_id': ticket.product_id.id,
387 'product_uom_qty': 1,
388 'price_unit': 0,
389 })
390 ]
391 })
392
393 # Confirming the SO will raise an error if there is not enough seats
394 with self.assertRaises(ValidationError):
395 customer_so.action_confirm()
396
397 editor = self.env['registration.editor'].with_context({
398 'default_sale_order_id': customer_so.id
399 }).create({})
400
401 with self.assertRaises(ValidationError):
402 editor.action_make_registration()
403
404 def test_event_sale_price_total(self):
405 """ Test that the `sale_amount_total` matches the total amount of all confirmed orders.
406 """
407 self.env['sale.order.line'].create({
408 'product_id': self.event_product.id,
409 'order_id': self.sale_order.id,
410 'event_id': self.event_0.id,
411 'event_ticket_id': self.ticket.id,
412 })
413 self.sale_order.action_confirm()
414 expected_price = sum(self.event_0.sale_order_lines_ids.mapped('price_total'))
415 self.assertEqual(self.event_0.sale_price_total, expected_price)
416
417 def test_ticket_price_with_currency_conversion(self):
418 def _prepare_currency(self, currency_name):
419 currency = self.env['res.currency'].with_context(active_test=False).search(
420 [('name', '=', currency_name)]
421 )
422 currency.action_unarchive()
423 return currency
424
425 currency_VEF = _prepare_currency(self, 'VEF')
426 currency_USD = _prepare_currency(self, 'USD')
427
428 company_test = self.env['res.company'].create({
429 'name': 'TestCompany',
430 'country_id': self.env.ref('base.be').id,
431 'currency_id': currency_USD.id,
432 })
433 self.env.user.company_ids += company_test
434 self.env.user.company_id = company_test
435
436 pricelist_USD = self.env['product.pricelist'].create({
437 'name': 'pricelist_USD',
438 'currency_id': currency_USD.id,
439 })
440 pricelist_VEF = self.env['product.pricelist'].create({
441 'name': 'pricelist_VEF',
442 'currency_id': currency_VEF.id,
443 })
444
445 event_product = self.env['product.template'].create({
446 'name': 'Event Product',
447 'list_price': 10.0,
448 'taxes_id': False,
449 })
450
451 event = self.env['event.event'].create({
452 'name': 'New Event',
453 'date_begin': '2020-02-02',
454 'date_end': '2020-04-04',
455 })
456 event_ticket = self.env['event.event.ticket'].create({
457 'name': 'VIP',
458 'price': 1000.0,
459 'event_id': event.id,
460 'product_id': event_product.product_variant_id.id,
461 })
462
463 so = self.env['sale.order'].create({
464 'partner_id': self.env.user.partner_id.id,
465 'pricelist_id': pricelist_USD.id,
466 })
467 self.env['sale.order.line'].create({
468 'product_id': event_product.product_variant_id.id,
469 'order_id': so.id,
470 'event_id': event.id,
471 'event_ticket_id': event_ticket.id,
472 })
473 self.assertEqual(so.amount_total, event_ticket.price)
474
475 so.pricelist_id = pricelist_VEF
476 so.action_update_prices()
477
478 self.assertAlmostEqual(
479 so.amount_total,
480 currency_USD._convert(
481 event_ticket.price, currency_VEF, self.env.user.company_id, so.date_order
482 ),
483 delta=1,
484 )
485
486 def test_ticket_price_with_pricelist_and_tax(self):
487 self.env.user.partner_id.country_id = False
488 pricelist = self.env['product.pricelist'].create({'name': 'Base Pricelist'})
489
490 tax = self.env['account.tax'].create({
491 'name': "Tax 10",
492 'amount': 10,
493 })
494
495 event_product = self.env['product.template'].create({
496 'name': 'Event Product',
497 'list_price': 10.0,
498 })
499
500 event_product.taxes_id = tax
501
502 event = self.env['event.event'].create({
503 'name': 'New Event',
504 'date_begin': '2020-02-02',
505 'date_end': '2020-04-04',
506 })
507 event_ticket = self.env['event.event.ticket'].create({
508 'name': 'VIP',
509 'price': 1000.0,
510 'event_id': event.id,
511 'product_id': event_product.product_variant_id.id,
512 })
513
514 pricelist.item_ids = self.env['product.pricelist.item'].create({
515 'applied_on': "1_product",
516 'base': "list_price",
517 'compute_price': "fixed",
518 'fixed_price': 6.0,
519 'product_tmpl_id': event_product.id,
520 })
521
522 so = self.env['sale.order'].create({
523 'partner_id': self.env.user.partner_id.id,
524 'pricelist_id': pricelist.id,
525 })
526 sol = self.env['sale.order.line'].create({
527 'product_id': event_product.product_variant_id.id,
528 'order_id': so.id,
529 'event_id': event.id,
530 'event_ticket_id': event_ticket.id,
531 })
532 self.assertEqual(so.amount_total, 660.0, "Ticket is $1000 but the event product is on a pricelist 10 -> 6. So, $600 + a 10% tax.")
533
534 @users('user_salesman')
535 def test_unlink_so(self):
536 """ This test ensures that when deleting a sale order, if the latter is linked to an event registration,
537 it is also deleted """
538 event = self.env['event.event'].browse(self.event_0.ids)
539 self.register_person.action_make_registration()
540 self.assertEqual(len(event.registration_ids), 1)
541 self.sale_order.unlink()
542 self.assertEqual(len(event.registration_ids), 0)
543
544 @users('user_salesman')
545 def test_unlink_soline(self):
546 """ This test ensures that when deleting a sale order line, if the latter is linked to an event registration,
547 it is also deleted """
548 event = self.env['event.event'].browse(self.event_0.ids)
549 self.register_person.action_make_registration()
550 self.assertEqual(len(event.registration_ids), 1)
551 self.sale_order.order_line.unlink()
552 self.assertEqual(len(event.registration_ids), 0)
553
554 @users('user_salesman')
555 def test_cancel_so(self):
556 """ This test ensures that when canceling a sale order, if the latter is linked to an event registration,
557 it is also cancelled """
558 event = self.env['event.event'].browse(self.event_0.ids)
559 self.register_person.action_make_registration()
560 self.assertEqual(len(event.registration_ids), 1)
561 self.sale_order._action_cancel()
562 self.assertEqual(len(event.registration_ids), 1)
563 self.assertEqual(event.registration_ids.state, 'cancel')
564
565 @users('user_salesman')
566 def test_compute_sale_status(self):
567 self.register_person.action_make_registration()
568 registration = self.event_0.registration_ids
569 self.assertEqual(registration.sale_status, 'to_pay')
570 registration.sale_order_line_id.price_total = 0.0
571 self.assertEqual(registration.sale_status, 'to_pay', "Free SO line on SO with non zero amount should be paid")
572 registration.sale_order_id.amount_total = 0.0
573 self.assertEqual(registration.sale_status, 'free', "Free SO line on SO with zero amount should be free")
574 # Remove the SO and so_line relations to test the "free ticket without SO" case
575 sale_order = registration.sale_order_id
576 so_line = registration.sale_order_line_id
577 registration.sale_order_id = False
578 registration.sale_order_line_id = False
579 self.assertEqual(registration.sale_status, 'free', "Price of $0.00 without so_line should be free")
580 registration.sale_order_id = sale_order
581 registration.sale_order_line_id = so_line
582 registration.sale_order_id.amount_total = 0.01
583 self.assertEqual(registration.sale_status, 'to_pay', "Price of $0.01 should be paid")
584 registration.sale_order_id.action_confirm()
585 self.assertEqual(registration.sale_status, 'sold')