Debian Codegraph @

addons/sale_management/tests/test_sale_order.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from itertools import chain
4
5 from odoo.fields import Command
6 from odoo.tests import Form, tagged
7
8 from odoo.addons.sale_management.tests.common import SaleManagementCommon
9
10
11 @tagged('-at_install', 'post_install')
12 class TestSaleOrder(SaleManagementCommon):
13
14 @classmethod
15 def setUpClass(cls):
16 super().setUpClass()
17
18 # some variables to ease asserts in tests
19 cls.pub_product_price = 100.0
20 cls.pl_product_price = 80.0
21 cls._enable_discounts()
22 cls.tpl_discount = 10.0
23 cls.pl_discount = (cls.pub_product_price - cls.pl_product_price) * 100 / cls.pub_product_price
24 cls.merged_discount = 100.0 - (100.0 - cls.pl_discount) * (100.0 - cls.tpl_discount) / 100.0
25
26 cls.pub_option_price = 200.0
27 cls.pl_option_price = 100.0
28 cls.tpl_option_discount = 20.0
29 cls.pl_option_discount = (cls.pub_option_price - cls.pl_option_price) * 100 / cls.pub_option_price
30 cls.merged_option_discount = 100.0 - (100.0 - cls.pl_option_discount) * (100.0 - cls.tpl_option_discount) / 100.0
31
32 # create some products
33 cls.product_1, cls.optional_product = cls.env['product.product'].create([
34 {
35 'name': 'Product 1',
36 'lst_price': cls.pub_product_price,
37 'description_sale': "This is a product description"
38 }, {
39 'name': 'Optional product',
40 'lst_price': cls.pub_option_price,
41 }
42 ])
43
44 # create some quotation templates
45 cls.quotation_template_no_discount = cls.env['sale.order.template'].create({
46 'name': 'A quotation template',
47 'sale_order_template_line_ids': [
48 Command.create({
49 'product_id': cls.product_1.id,
50 }),
51 Command.create({
52 'name': 'Optional products',
53 'display_type': 'line_section',
54 'is_optional': True,
55 'sequence': 11, # to be sure optional products are last in the template
56 }),
57 Command.create({
58 'product_id': cls.optional_product.id,
59 'sequence': 12,
60 }),
61 ],
62 })
63
64 # create two pricelist with different discount policies (same total price)
65 pricelist_rule_values = [
66 Command.create({
67 'name': 'Product 1 premium price',
68 'applied_on': '1_product',
69 'product_tmpl_id': cls.product_1.product_tmpl_id.id,
70 'compute_price': 'fixed',
71 'fixed_price': cls.pl_product_price,
72 }),
73 Command.create({
74 'name': 'Optional product premium price',
75 'applied_on': '1_product',
76 'product_tmpl_id': cls.optional_product.product_tmpl_id.id,
77 'compute_price': 'fixed',
78 'fixed_price': cls.pl_option_price,
79 }),
80 ]
81 percentage_pricelist_rule_values = [
82 Command.create({
83 'name': 'Product 1 premium price',
84 'applied_on': '1_product',
85 'product_tmpl_id': cls.product_1.product_tmpl_id.id,
86 'compute_price': 'percentage',
87 'percent_price': cls.pl_discount,
88 }),
89 Command.create({
90 'name': 'Optional product premium price',
91 'applied_on': '1_product',
92 'product_tmpl_id': cls.optional_product.product_tmpl_id.id,
93 'compute_price': 'percentage',
94 'percent_price': cls.pl_option_discount,
95 }),
96 ]
97
98 (
99 cls.discount_included_price_list,
100 cls.discount_excluded_price_list
101 ) = cls.env['product.pricelist'].create([
102 {
103 'name': 'Discount included Pricelist',
104 'item_ids': pricelist_rule_values,
105 }, {
106 'name': 'Discount excluded Pricelist',
107 'item_ids': percentage_pricelist_rule_values,
108 }
109 ])
110
111 # variable kept to reduce code diff
112 cls.sale_order = cls.empty_order
113
114 def test_01_template_without_pricelist(self):
115 """
116 This test checks that without any rule in the pricelist, the public price
117 of the product is used in the sale order after selecting a
118 quotation template.
119 """
120 # first case, without discount in the quotation template
121 self.sale_order.write({
122 'sale_order_template_id': self.quotation_template_no_discount.id
123 })
124 self.sale_order._onchange_sale_order_template_id()
125
126 self.assertEqual(
127 len(self.sale_order.order_line),
128 3,
129 "The sale order shall contains the same number of lines as"
130 "the quotation template.")
131
132 self.assertEqual(
133 self.sale_order.order_line[0].product_id.id,
134 self.product_1.id,
135 "The sale order shall contains the same products as the"
136 "quotation template.")
137
138 self.assertEqual(
139 self.sale_order.order_line[0].price_unit,
140 self.pub_product_price,
141 "Without any price list and discount, the public price of"
142 "the product shall be used.")
143
144 optional_lines = self._get_optional_product_lines(self.sale_order)
145
146 self.assertEqual(
147 len(optional_lines),
148 1,
149 "The sale order shall contains the same number of optional products as"
150 "the quotation template.")
151
152 self.assertEqual(
153 optional_lines[0].product_id.id,
154 self.optional_product.id,
155 "The sale order shall contains the same optional products as the"
156 "quotation template.")
157
158 self.assertEqual(
159 optional_lines[0].price_unit,
160 self.pub_option_price,
161 "Without any price list and discount, the public price of"
162 "the optional product shall be used.")
163
164 self.assertEqual(
165 self.sale_order.order_line[2].product_id.id,
166 self.optional_product.id,
167 "The sale order shall contains the same products as the"
168 "quotation template.")
169
170 self.assertEqual(
171 self.sale_order.order_line[2].price_unit,
172 self.pub_option_price,
173 "Without any price list and discount, the public price of"
174 "the optional product shall be used.")
175
176 def test_02_template_with_discount_included_pricelist(self):
177 """
178 This test checks that with a 'discount included' price list,
179 the price used in the sale order is computed according to the
180 price list.
181 """
182
183 # first case, without discount in the quotation template
184 self.sale_order.write({
185 'pricelist_id': self.discount_included_price_list.id,
186 'sale_order_template_id': self.quotation_template_no_discount.id
187 })
188 self.sale_order._onchange_sale_order_template_id()
189
190 self.assertEqual(
191 self.sale_order.order_line[0].price_unit,
192 self.pl_product_price,
193 "If a pricelist is set, the product price shall be computed"
194 "according to it.")
195
196 optional_lines = self._get_optional_product_lines(self.sale_order)
197
198 self.assertEqual(
199 optional_lines[0].price_unit,
200 self.pl_option_price,
201 "If a pricelist is set, the optional product price shall"
202 "be computed according to it.")
203
204 self.assertEqual(
205 self.sale_order.order_line[2].price_unit,
206 self.pl_option_price,
207 "If a pricelist is set, the optional product price shall"
208 "be computed according to it.")
209
210 def test_03_template_with_discount_excluded_pricelist(self):
211 """
212 This test checks that with a 'discount excluded' price list,
213 the price used in the sale order is the product public price and
214 the discount is computed according to the price list.
215 """
216 self.sale_order.write({
217 'pricelist_id': self.discount_excluded_price_list.id,
218 'sale_order_template_id': self.quotation_template_no_discount.id
219 })
220 self.sale_order._onchange_sale_order_template_id()
221
222 self.assertEqual(
223 self.sale_order.order_line[0].price_unit,
224 self.pub_product_price,
225 "If a pricelist is set without discount included, the unit "
226 "price shall be the public product price.")
227
228 self.assertEqual(
229 self.sale_order.order_line[0].price_subtotal,
230 self.pl_product_price,
231 "If a pricelist is set without discount included, the subtotal "
232 "price shall be the price computed according to the price list.")
233
234 self.assertEqual(
235 self.sale_order.order_line[0].discount,
236 self.pl_discount,
237 "If a pricelist is set without discount included, the discount "
238 "shall be computed according to the price unit and the subtotal."
239 "price")
240
241 optional_lines = self._get_optional_product_lines(self.sale_order)
242
243 self.assertEqual(
244 optional_lines[0].price_unit,
245 self.pub_option_price,
246 "If a pricelist is set without discount included, the unit "
247 "price shall be the public optional product price.")
248
249 self.assertEqual(
250 optional_lines[0].discount,
251 self.pl_option_discount,
252 "If a pricelist is set without discount included, the discount "
253 "shall be computed according to the optional price unit and"
254 "the subtotal price.")
255
256 self.assertEqual(
257 self.sale_order.order_line[2].price_unit,
258 self.pub_option_price,
259 "If a pricelist is set without discount included, the unit "
260 "price shall be the public optional product price.")
261
262 self.assertEqual(
263 self.sale_order.order_line[2].price_subtotal,
264 self.pl_option_price,
265 "If a pricelist is set without discount included, the subtotal "
266 "price shall be the price computed according to the price list.")
267
268 self.assertEqual(
269 self.sale_order.order_line[2].discount,
270 self.pl_option_discount,
271 "If a pricelist is set without discount included, the discount "
272 "shall be computed according to the price unit and the subtotal."
273 "price")
274
275 def test_04_update_pricelist_option_line(self):
276 """
277 This test checks that option line's values are correctly
278 updated after a pricelist update
279 """
280 self.sale_order.write({
281 'sale_order_template_id': self.quotation_template_no_discount.id
282 })
283 self.sale_order._onchange_sale_order_template_id()
284
285 optional_lines = self._get_optional_product_lines(self.sale_order)
286
287 self.assertEqual(
288 optional_lines[0].price_unit,
289 self.pub_option_price,
290 "If no pricelist is set, the unit price shall be the option's product price.")
291
292 self.assertEqual(
293 optional_lines[0].discount, 0,
294 "If no pricelist is set, the discount should be 0.")
295
296 self.sale_order.write({
297 'pricelist_id': self.discount_included_price_list.id,
298 })
299 self.sale_order._recompute_prices()
300
301 self.assertEqual(
302 optional_lines[0].price_unit,
303 self.pl_option_price,
304 "If a pricelist is set with discount included,"
305 " the unit price shall be the option's product discounted price.")
306
307 self.assertEqual(
308 optional_lines[0].discount, 0,
309 "If a pricelist is set with discount included,"
310 " the discount should be 0.")
311
312 self.sale_order.write({
313 'pricelist_id': self.discount_excluded_price_list.id,
314 })
315 self.sale_order._recompute_prices()
316
317 self.assertEqual(
318 optional_lines[0].price_unit,
319 self.pub_option_price,
320 "If a pricelist is set without discount included,"
321 " the unit price shall be the option's product sale price.")
322
323 self.assertEqual(
324 optional_lines[0].discount,
325 self.pl_option_discount,
326 "If a pricelist is set without discount included,"
327 " the discount should be correctly computed.")
328
329 def test_option_price_unit_is_not_recomputed(self):
330 """
331 Verifies that user defined price unit for optional products remains the same after
332 update of quantities.
333 """
334
335 sale_order_with_option = self.env['sale.order'].create({
336 'partner_id': self.partner.id,
337 'order_line': [
338 Command.create({
339 'display_type': 'line_section',
340 'name': "Optional products",
341 'is_optional': True,
342 }),
343 Command.create({
344 'product_id': self.optional_product.id,
345 }),
346 ],
347 })
348
349 optional_product_line = self._get_optional_product_lines(sale_order_with_option)
350
351 optional_product_line.price_unit = 100
352 # after changing the quantity of the product, the price unit should not be recomputed
353 optional_product_line.product_uom_qty = 10
354 self.assertEqual(optional_product_line.price_unit, 100)
355
356 def test_reload_template_translations(self):
357 """
358 Check that quotation template gets reloaded with correct translations on partner change.
359 """
360 # Add some display type lines to the template
361 self.quotation_template_no_discount.sale_order_template_line_ids = [
362 Command.create({
363 'name': "Section 1",
364 'display_type': 'line_section',
365 }),
366 Command.create({
367 'name': "Note 1",
368 'display_type': 'line_note',
369 }),
370 ]
371 # Remove product description to ease comparing before/after translations
372 self.product_1.description_sale = None
373
374 # Commence activation of Dutch vernacular
375 self.env['res.lang']._activate_lang('nl_NL')
376 partner_NL = self.partner.copy({'lang': 'nl_NL', 'name': "Pieter-Jan Hollandman"})
377 names_EN = ["Product 1", "Section 1", "Note 1", "Optional products", "Optional product"]
378 names_NL = ["Artikel 1", "Sectie 1", "Nota 1", "Optionele producten", "Optioneel product"]
379 trans_dict = dict(zip(names_EN, names_NL))
380 for record in chain(
381 self.quotation_template_no_discount.sale_order_template_line_ids,
382 self.quotation_template_no_discount.sale_order_template_line_ids.product_id,
383 ):
384 if not record.name:
385 continue
386 record.with_context(lang='nl_NL').name = trans_dict[record.name]
387
388 # Create sale order form (and a way to retrieve line names)
389 def get_form_field_names(form):
390 return [
391 form.order_line.edit(0).name,
392 form.order_line.edit(1).name,
393 form.order_line.edit(2).name,
394 form.order_line.edit(3).name,
395 form.order_line.edit(4).name,
396 ]
397
398 order_form = Form(self.sale_order.browse())
399 order_form.sale_order_template_id = self.quotation_template_no_discount
400
401 # Sanity check English names
402 self.assertSequenceEqual(
403 get_form_field_names(order_form),
404 names_EN,
405 "Lines should be displayed in English for an American partner",
406 )
407
408 # Go Dutch
409 order_form.partner_id = partner_NL
410 self.assertSequenceEqual(
411 get_form_field_names(order_form),
412 names_NL,
413 "Lines should be displayed in Dutch for a Dutch partner",
414 )
415
416 # Edit a line & change back to American partner
417 with order_form.order_line.edit(0) as order_line:
418 order_line.product_uom_qty += 1
419 order_form.partner_id = self.partner
420 self.assertSequenceEqual(
421 get_form_field_names(order_form),
422 names_NL,
423 "Lines shouldn't change when edited",
424 )
425
426 # Reload template manually
427 order_form.sale_order_template_id = self.quotation_template_no_discount
428 self.assertSequenceEqual(
429 get_form_field_names(order_form),
430 names_EN,
431 "Lines should change after manual template reload",
432 )
433
434 order_form.partner_id = partner_NL
435
436 # Reload template, save, and change partner again
437 order_form.sale_order_template_id = self.quotation_template_no_discount
438 order_form.save()
439 order_form.partner_id = self.partner
440 self.assertSequenceEqual(
441 get_form_field_names(order_form),
442 names_NL,
443 "Lines shouldn't change once saved",
444 )
445
446 def test_product_description_no_template_description(self):
447 """
448 Test case for when the product has a description, but the quotation template line does not.
449 The final sale order line should use the product's description.
450 """
451 quotation_template_no_description = self.empty_order_template
452 quotation_template_no_description.sale_order_template_line_ids = [
453 Command.create({
454 'product_id': self.product_1.id,
455 'name': False,
456 }),
457 ]
458 sale_order = self.empty_order
459 sale_order.sale_order_template_id = quotation_template_no_description
460 sale_order._onchange_sale_order_template_id()
461 self.assertEqual(
462 sale_order.order_line[0].name,
463 f"{self.product_1.name}\n{self.product_1.description_sale}",
464 "Sale order line should use product's description when no quotation template \
465 description is set."
466 )
467
468 def test_product_description_with_template_description(self):
469 """
470 Test case for when both the product and the quotation template line have descriptions.
471 The final sale order line should use the template's description.
472 """
473 quotation_template_with_description = self.empty_order_template
474 quotation_template_with_description.sale_order_template_line_ids = [
475 Command.create({
476 'product_id': self.product_1.id,
477 'name': "This is a template description",
478 }),
479 ]
480 sale_order = self.empty_order
481 sale_order.sale_order_template_id = quotation_template_with_description
482 sale_order._onchange_sale_order_template_id()
483 self.assertEqual(
484 sale_order.order_line[0].name,
485 quotation_template_with_description.sale_order_template_line_ids[0].name,
486 "The sale order line should use the quotation template's description when both \
487 product and the quotation template descriptions are set."
488 )
489
490 def test_warning_quotation(self):
491 """
492 ensure "warning for the change of your quotation's company" isn't triggered
493 during the creation of a quotation when a quotation template is set as default
494 """
495 quotation_template = self.empty_order_template
496 quotation_template.sale_order_template_line_ids = [
497 Command.create({'product_id': self.product.id})
498 ]
499 self.env['ir.default'].set('sale.order', 'sale_order_template_id', quotation_template.id)
500 try:
501 with self.assertLogs('odoo.tests.form.onchange') as log_catcher:
502 Form(self.env['sale.order'])
503 except AssertionError:
504 pass
505 self.assertEqual(len(log_catcher.output), 0, "Form creation shouldn't trigger a warning")
506
507 def test_show_update_pricelist_false_on_sale_order_open(self):
508 """Ensure the update pricelist button is disabled when opening a sale order
509 with a default quotation template applied.
510 """
511 quotation_template = self.env['sale.order.template'].create({
512 'name': 'Test Quotation Template',
513 'sale_order_template_line_ids': [
514 Command.create({
515 'product_id': self.product.id,
516 }),
517 ],
518 })
519 self.env['ir.default'].set('sale.order', 'sale_order_template_id', quotation_template.id)
520 with Form(self.env['sale.order']) as sale_order_form:
521 self.assertTrue(sale_order_form.sale_order_template_id)
522 self.assertTrue(sale_order_form.order_line)
523 self.assertFalse(sale_order_form.show_update_pricelist)
524 sale_order_form.partner_id = self.partner