Debian Codegraph @

addons/purchase_stock/tests/test_purchase_order_suggest.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from datetime import datetime
3 from dateutil.relativedelta import relativedelta
4
5 from odoo import Command, fields
6 from odoo.addons.purchase_stock.tests.common import PurchaseTestCommon
7 from odoo.tests import tagged, freeze_time
8 from odoo.tests.common import HttpCase
9
10
11 @freeze_time("2021-01-14 09:12:15")
12 @tagged('post_install', '-at_install')
13 class TestPurchaseOrderSuggest(PurchaseTestCommon, HttpCase):
14
15 @classmethod
16 def setUpClass(cls):
17 super().setUpClass()
18 # Create a product and a supplier info.
19 cls.product_1 = cls.env['product.product'].create({
20 'name': 'Other Product',
21 'standard_price': 115,
22 'is_storable': True,
23 })
24 cls.env['product.supplierinfo'].create([{
25 'partner_id': cls.vendor.id,
26 'price': 100,
27 'product_id': cls.product_1.id,
28 }])
29 cls.other_warehouse = cls.env['stock.warehouse'].create({
30 'name': 'Other Warehouse',
31 'code': 'TWH2',
32 'company_id': cls.env.company.id,
33 })
34
35 def assertEstimatedPrice(self, po, price, based_on='30_days', days=30, factor=100, warehouse=False, domain=[]):
36 """ This helper method does an assert for the `purchase.order.suggest` wizard
37 estimated price for the given parameters (use the default values if not set).
38 Note that the wizard fields are updated each time this method is called."""
39 base_warehouse = self.picking_type_out.default_location_src_id.warehouse_id
40 warehouse_id = (warehouse or base_warehouse).id
41 suggest_context = {
42 "order_id": po.id,
43 "partner_id": po.partner_id.id,
44 "warehouse_id": warehouse_id,
45 "suggest_based_on": based_on,
46 "suggest_days": days,
47 "suggest_percent": factor,
48 }
49 products = self.env["product.product"].with_context(suggest_context).search(domain)
50 # Invalidate so changes such as partner_id or new deliveries without changing the @api_depends of the computes
51 products.invalidate_recordset(["suggest_estimated_price", "suggested_qty"])
52 self.assertEqual(sum(products.mapped("suggest_estimated_price")), price)
53
54 def actionAddAll(self, po, based_on='30_days', days=30, factor=100, warehouse=False):
55 base_warehouse = self.picking_type_out.default_location_src_id.warehouse_id
56 warehouse_id = (warehouse or base_warehouse).id
57 suggest_context = {
58 "warehouse_id": warehouse_id,
59 "suggest_based_on": based_on,
60 "suggest_percent": factor,
61 "suggest_days": days,
62 }
63 po.with_context(suggest_context).action_purchase_order_suggest()
64
65 def _create_and_process_delivery_at_date(self, products_and_quantities, date=False, warehouse=False, to_validate=True):
66 date = date or datetime.now()
67 delivery_type = warehouse.out_type_id if warehouse else self.picking_type_out
68 with freeze_time(date):
69 delivery = self.env['stock.picking'].create({
70 'picking_type_id': self.picking_type_out.id,
71 'location_id': delivery_type.default_location_src_id.id,
72 'location_dest_id': delivery_type.default_location_dest_id.id,
73 'move_ids': [Command.create({
74 'location_id': delivery_type.default_location_src_id.id,
75 'location_dest_id': delivery_type.default_location_dest_id.id,
76 'product_id': product.id,
77 'product_uom': self.uom.id,
78 'product_uom_qty': qty,
79 }) for (product, qty) in products_and_quantities],
80 })
81 delivery.action_confirm()
82 if to_validate:
83 delivery.action_assign()
84 delivery.button_validate()
85 return delivery
86
87 def test_purchase_order_suggest_quantities(self):
88 """ Checks the suggest wizard adds right products with right quantities.
89 Also checks some values, like the products' quantity demand or the
90 suggest expected price, are rigthly computed too."""
91 today = fields.Datetime.now()
92 # Create some products.
93 product_2, product_3, product_4, product_5, product_6 = self.env['product.product'].create([{
94 'name': f'Product {i + 1}',
95 'standard_price': price,
96 'is_storable': True,
97 } for (i, price) in enumerate([25, 50, 100, 50, 25])])
98 self.env['stock.quant']._update_available_quantity(self.product_1, self.stock_location, 42)
99 self.env['stock.quant']._update_available_quantity(product_2, self.stock_location, 15)
100 self.env['stock.quant']._update_available_quantity(product_3, self.stock_location, 20)
101
102 # Create supplier info.
103 self.env['product.supplierinfo'].create([{
104 'partner_id': self.vendor.id,
105 'price': 20,
106 'product_id': product_2.id,
107 }, {
108 'partner_id': self.vendor.id,
109 'price': 50,
110 'product_id': product_3.id,
111 }])
112
113 # Create some delivery in the past.
114 self._create_and_process_delivery_at_date(
115 [(self.product_1, 12)], today - relativedelta(years=1)
116 )
117 self._create_and_process_delivery_at_date(
118 [(self.product_1, 5), (product_2, 5)], today - relativedelta(months=10, days=3)
119 )
120 self._create_and_process_delivery_at_date(
121 [(self.product_1, 5), (product_3, 10)], today - relativedelta(months=2, days=5)
122 )
123 self._create_and_process_delivery_at_date(
124 [(self.product_1, 10), (product_3, 10)], today - relativedelta(days=30)
125 )
126 self._create_and_process_delivery_at_date(
127 [(self.product_1, 10), (product_2, 5)], today - relativedelta(days=15)
128 )
129 self._create_and_process_delivery_at_date(
130 [(product_2, 5)], today - relativedelta(days=3)
131 )
132
133 # Check product demand quantity.
134 # With no dates given in the context, the monthly demand should take only last month.
135 self.assertEqual(self.product_1.monthly_demand, 20)
136 self.assertEqual(product_2.monthly_demand, 10)
137 self.assertEqual(product_3.monthly_demand, 10)
138 # Check for last week.
139 context = {'suggest_based_on': "one_week"}
140 self.assertEqual(self.product_1.with_context(context).monthly_demand, 0)
141 self.assertAlmostEqual(product_2.with_context(context).monthly_demand, 5 * (365.25 / 12) / 7, places=6)
142 self.assertEqual(product_3.with_context(context).monthly_demand, 0)
143 # Check for last three months.
144 context = {'suggest_based_on': "three_months"}
145 self.assertAlmostEqual(self.product_1.with_context(context).monthly_demand, 25 / 3, places=6)
146 self.assertAlmostEqual(product_2.with_context(context).monthly_demand, 10 / 3, places=6)
147 self.assertAlmostEqual(product_3.with_context(context).monthly_demand, 20 / 3, places=6)
148 # Check for last year months.
149 context = {'suggest_based_on': "one_year"}
150 self.assertAlmostEqual(self.product_1.with_context(context).monthly_demand, 42 / 12, places=6)
151 self.assertAlmostEqual(product_2.with_context(context).monthly_demand, 15 / 12, places=6)
152 self.assertAlmostEqual(product_3.with_context(context).monthly_demand, 20 / 12, places=6)
153 # Check for January 2020.
154 context = {'suggest_based_on': "last_year"}
155 self.assertEqual(self.product_1.with_context(context).monthly_demand, 12)
156 self.assertEqual(product_2.with_context(context).monthly_demand, 0)
157 self.assertEqual(product_3.with_context(context).monthly_demand, 0)
158 # Check for February 2020.
159 context = {'suggest_based_on': "last_year_m_plus_1"}
160 self.assertEqual(self.product_1.with_context(context).monthly_demand, 0)
161 self.assertEqual(product_2.with_context(context).monthly_demand, 0)
162 self.assertEqual(product_3.with_context(context).monthly_demand, 0)
163 # Check for March 2020.
164 context = {'suggest_based_on': "last_year_m_plus_2"}
165 self.assertEqual(self.product_1.with_context(context).monthly_demand, 5)
166 self.assertEqual(product_2.with_context(context).monthly_demand, 5)
167 self.assertEqual(product_3.with_context(context).monthly_demand, 0)
168
169 # Create a new PO for the vendor then check suggest wizard estimed price.
170 po = self.env['purchase.order'].create({'partner_id': self.vendor.id})
171 # Check estimed price for default values (30 days, based on last month, with 100% factor.)
172 self.assertEstimatedPrice(po, 2700)
173 self.assertEstimatedPrice(po, 1350, days=15)
174 self.assertEstimatedPrice(po, 3410, factor=125)
175 # Check estimed price for 3 months.
176 self.assertEstimatedPrice(po, 1330, based_on='three_months')
177 self.assertEstimatedPrice(po, 3700, based_on='three_months', days=90)
178 # Check estimed price for current year.
179 self.assertEstimatedPrice(po, 540, based_on='one_year')
180 self.assertEstimatedPrice(po, 5500, based_on='one_year', days=365)
181
182 # Use suggest to generate PO lines and check their values.
183 self.actionAddAll(po, based_on='30_days', days=30, factor=100)
184 self.assertRecordValues(po.order_line, [
185 {'product_id': self.product_1.id, 'product_qty': 20},
186 {'product_id': product_2.id, 'product_qty': 10},
187 {'product_id': product_3.id, 'product_qty': 10},
188 ])
189 # Regenerate PO lines for 3 months (existing lines qty must be updated.)
190 self.actionAddAll(po, based_on='three_months', days=30, factor=100)
191 self.assertRecordValues(po.order_line, [
192 {'product_id': self.product_1.id, 'product_qty': 9},
193 {'product_id': product_2.id, 'product_qty': 4},
194 {'product_id': product_3.id, 'product_qty': 7},
195 ])
196 self.actionAddAll(po, based_on='three_months', days=90, factor=100)
197 self.assertRecordValues(po.order_line, [
198 {'product_id': self.product_1.id, 'product_qty': 25},
199 {'product_id': product_2.id, 'product_qty': 10},
200 {'product_id': product_3.id, 'product_qty': 20},
201 ])
202
203 # Create supplier info.
204 self.env['product.supplierinfo'].create([{
205 'partner_id': self.vendor.id,
206 'price': 90,
207 'product_id': product_4.id,
208 }, {
209 'partner_id': self.vendor.id,
210 'price': 45,
211 'product_id': product_5.id,
212 }, {
213 'partner_id': self.vendor.id,
214 'price': 24,
215 'product_id': product_6.id,
216 }])
217
218 self.env['stock.quant']._update_available_quantity(product_4, self.stock_location, 1)
219 self.env['stock.quant']._update_available_quantity(product_5, self.stock_location, 2)
220 self.env['stock.quant']._update_available_quantity(product_6, self.stock_location, 10)
221
222 # Create some out delivery on the products and set different scheduled dates.
223 delivery_1 = self._create_and_process_delivery_at_date(
224 [(product_4, 6)], today, to_validate=False
225 )
226 delivery_1.scheduled_date = today + relativedelta(days=3)
227
228 delivery_2 = self._create_and_process_delivery_at_date(
229 [(product_5, 10)], today, to_validate=False
230 )
231 delivery_2.scheduled_date = today + relativedelta(days=5)
232
233 self._create_and_process_delivery_at_date(
234 [(product_6, 10)], today
235 )
236
237 context = {
238 'to_date': fields.Datetime.now() + relativedelta(days=2),
239 }
240 self.assertEqual(product_4.with_context(context).virtual_available, 1)
241 self.assertEqual(product_5.with_context(context).virtual_available, 2)
242 self.assertEqual(product_6.with_context(context).virtual_available, 0)
243
244 context = {
245 'to_date': fields.Datetime.now() + relativedelta(days=4),
246 }
247 self.assertEqual(product_4.with_context(context).virtual_available, -5)
248 self.assertEqual(product_5.with_context(context).virtual_available, 2)
249 self.assertEqual(product_6.with_context(context).virtual_available, 0)
250
251 context = {
252 'to_date': fields.Datetime.now() + relativedelta(days=8),
253 }
254 self.assertEqual(product_4.with_context(context).virtual_available, -5)
255 self.assertEqual(product_5.with_context(context).virtual_available, -8)
256 self.assertEqual(product_6.with_context(context).virtual_available, 0)
257
258 po = self.env['purchase.order'].create({'partner_id': self.vendor.id})
259
260 # Check estimed price when based on actual demand.
261 self.assertEstimatedPrice(po, 810, based_on='actual_demand')
262 self.assertEstimatedPrice(po, 1620, based_on='actual_demand', factor=200)
263 self.assertEstimatedPrice(po, 450, based_on='actual_demand', days=4)
264 self.assertEstimatedPrice(po, 270, based_on='actual_demand', days=4, factor=50)
265 self.assertEstimatedPrice(po, 0, based_on='actual_demand', days=2)
266
267 # Use suggest wizard to generate PO lines and check their values.
268 self.actionAddAll(po, based_on='actual_demand', days=30, factor=100)
269 self.assertRecordValues(po.order_line, [
270 {'product_id': product_4.id, 'product_qty': 5},
271 {'product_id': product_5.id, 'product_qty': 8},
272 ])
273
274 self.actionAddAll(po, based_on='actual_demand', days=30, factor=200)
275 self.assertRecordValues(po.order_line, [
276 {'product_id': product_4.id, 'product_qty': 10},
277 {'product_id': product_5.id, 'product_qty': 16},
278 ])
279
280 self.actionAddAll(po, based_on='actual_demand', days=4, factor=100)
281 self.assertRecordValues(po.order_line, [
282 {'product_id': product_4.id, 'product_qty': 5},
283 ])
284
285 self.actionAddAll(po, based_on='actual_demand', days=4, factor=50)
286 self.assertRecordValues(po.order_line, [
287 {'product_id': product_4.id, 'product_qty': 3},
288 ])
289
290 def test_purchase_order_suggest_quantities_for_consu(self):
291 """ Checks the suggest wizard works also with consumable products."""
292 today = fields.Datetime.now()
293 # Create a consumable product.
294 consu = self.env['product.product'].create({
295 'name': 'Product Consu',
296 'standard_price': 23,
297 })
298
299 # Create supplier info.
300 self.env['product.supplierinfo'].create({
301 'partner_id': self.vendor.id,
302 'price': 20,
303 'product_id': consu.id,
304 })
305
306 # Create some delivery in the past.
307 self._create_and_process_delivery_at_date([(consu, 55)], today - relativedelta(years=1, months=3))
308 self._create_and_process_delivery_at_date([(consu, 12)], today - relativedelta(years=1))
309 self._create_and_process_delivery_at_date([(consu, 5)], today - relativedelta(months=10, days=3))
310 self._create_and_process_delivery_at_date([(consu, 5)], today - relativedelta(months=2, days=5))
311 self._create_and_process_delivery_at_date([(consu, 10)], today - relativedelta(days=30))
312 self._create_and_process_delivery_at_date([(consu, 10)], today - relativedelta(days=15))
313
314 # Check product demand quantity.
315 # With no dates given in the context, the monthly demand should take only last month.
316 self.assertEqual(consu.monthly_demand, 20)
317 # Check for last week.
318 context = {'suggest_based_on': "one_week"}
319 self.assertEqual(consu.with_context(context).monthly_demand, 0)
320 # Check for last three months.
321 context = {'suggest_based_on': "three_months"}
322 self.assertAlmostEqual(consu.with_context(context).monthly_demand, 25 / 3, places=6)
323 # Check for last year months.
324 context = {'suggest_based_on': "one_year"}
325 self.assertAlmostEqual(consu.with_context(context).monthly_demand, 42 / 12, places=6)
326 # Check for January 2020.
327 context = {'suggest_based_on': "last_year"}
328 self.assertEqual(consu.with_context(context).monthly_demand, 12)
329 # Check for February 2020.
330 context = {'suggest_based_on': "last_year_m_plus_1"}
331 self.assertEqual(consu.with_context(context).monthly_demand, 0)
332 # Check for March 2020.
333 context = {'suggest_based_on': "last_year_m_plus_2"}
334 self.assertEqual(consu.with_context(context).monthly_demand, 5)
335
336 # Create a new PO for the vendor then check suggest wizard estimed price.
337 po = self.env['purchase.order'].create({'partner_id': self.vendor.id})
338 # Check estimed price for default values (30 days, based on last month, with 100% factor.)
339 self.assertEstimatedPrice(po, 400)
340 self.assertEstimatedPrice(po, 200, days=15)
341 self.assertEstimatedPrice(po, 500, factor=125)
342 # Check estimed price for 3 months.
343 self.assertEstimatedPrice(po, 180, based_on='three_months')
344 self.assertEstimatedPrice(po, 500, based_on='three_months', days=90)
345 # Check estimed price for current year.
346 self.assertEstimatedPrice(po, 80, based_on='one_year')
347 self.assertEstimatedPrice(po, 840, based_on='one_year', days=365)
348
349 # Use suggest wizard to generate PO lines and check their values.
350 self.actionAddAll(po, based_on='30_days', days=30, factor=100)
351 self.assertRecordValues(po.order_line, [
352 {'product_id': consu.id, 'product_qty': 20},
353 ])
354 # Regenerate PO lines for 3 months (existing lines qty must be updated.)
355 self.actionAddAll(po, based_on='three_months', days=30, factor=100)
356 self.assertRecordValues(po.order_line, [
357 {'product_id': consu.id, 'product_qty': 9},
358 ])
359 self.actionAddAll(po, based_on='three_months', days=90, factor=100)
360 self.assertRecordValues(po.order_line, [
361 {'product_id': consu.id, 'product_qty': 25},
362 ])
363
364 def test_purchase_order_suggest_quantities_deduce_forecast_quantity(self):
365 """ Ensures that when the forecast quantity is deduced from the suggested quantity"""
366 today = fields.Datetime.now()
367 self.env['stock.quant']._update_available_quantity(self.product_1, self.stock_location, 12)
368 # Do a delivery in the past.
369 self._create_and_process_delivery_at_date([(self.product_1, 12)], date=today - relativedelta(days=10))
370
371 # Create a new PO for the vendor then check suggest wizard estimed price.
372 po = self.env['purchase.order'].create({'partner_id': self.vendor.id})
373
374 # Check estimed price no forecast quantity.
375 self.assertEstimatedPrice(po, 1200)
376 self.actionAddAll(po, based_on='30_days', days=30, factor=100)
377 self.assertRecordValues(po.order_line, [
378 {'product_id': self.product_1.id, 'product_qty': 12},
379 ])
380
381 # Prepare a receipt for this product and confirm it.
382 receipt = self.env['stock.picking'].create({
383 'picking_type_id': self.picking_type_in.id,
384 'location_id': self.supplier_location.id,
385 'location_dest_id': self.stock_location.id,
386 'move_ids': [Command.create({
387 'location_id': self.supplier_location.id,
388 'location_dest_id': self.stock_location.id,
389 'product_id': self.product_1.id,
390 'product_uom': self.uom.id,
391 'product_uom_qty': 6,
392 })],
393 })
394 receipt.action_confirm()
395 receipt.action_assign()
396 # Check estimed price deduce the forecast quantity.
397 self.assertEstimatedPrice(po, 600, days=30)
398 self.actionAddAll(po, based_on='30_days', days=30, factor=100)
399 self.assertRecordValues(po.order_line, [
400 {'product_id': self.product_1.id, 'product_qty': 6},
401 ])
402
403 # Check the same with based_on actual demand.
404 product_ad = self.env['product.product'].create([{
405 'name': 'Product AD',
406 'standard_price': 60,
407 'is_storable': True,
408 }])
409
410 self.env['product.supplierinfo'].create([{
411 'partner_id': self.vendor.id,
412 'price': 55,
413 'product_id': product_ad.id,
414 }])
415 self.env['stock.quant']._update_available_quantity(product_ad, self.stock_location, 7)
416
417 delivery = self._create_and_process_delivery_at_date(
418 [(product_ad, 12)], today, to_validate=False
419 )
420 delivery.scheduled_date = today + relativedelta(days=3)
421
422 # Create a new PO for the vendor then check suggest wizard estimed price.
423 po = self.env['purchase.order'].create({'partner_id': self.vendor.id})
424 self.assertEstimatedPrice(po, 275, based_on='actual_demand', days=4)
425 self.actionAddAll(po, based_on='actual_demand', days=30, factor=100)
426 self.assertRecordValues(po.order_line, [
427 {'product_id': product_ad.id, 'product_qty': 5},
428 ])
429
430 # Prepare a receipt for this product and confirm it.
431 receipt = self.env['stock.picking'].create({
432 'picking_type_id': self.picking_type_in.id,
433 'location_id': self.supplier_location.id,
434 'location_dest_id': self.stock_location.id,
435 'move_ids': [Command.create({
436 'location_id': self.supplier_location.id,
437 'location_dest_id': self.stock_location.id,
438 'product_id': product_ad.id,
439 'product_uom': self.uom.id,
440 'product_uom_qty': 4,
441 })],
442 })
443 receipt.action_confirm()
444 receipt.action_assign()
445
446 self.assertEstimatedPrice(po, 55, based_on='actual_demand', days=4)
447 self.actionAddAll(po, based_on='actual_demand', days=30, factor=100)
448 self.assertRecordValues(po.order_line, [
449 {'product_id': product_ad.id, 'product_qty': 1},
450 ])
451
452 def test_purchase_order_suggest_quantities_multiwarehouse(self):
453 """ Ensure the product's qty demand is correctly computed for the right warehouse."""
454 date = fields.Datetime.now() - relativedelta(days=15)
455 self.env['stock.quant']._update_available_quantity(self.product_1, self.warehouse.lot_stock_id, 5)
456 self.env['stock.quant']._update_available_quantity(self.product_1, self.other_warehouse.lot_stock_id, 10)
457 # Make a delivery in each warehouse.
458 self._create_and_process_delivery_at_date([(self.product_1, 5)], date, warehouse=self.warehouse)
459 self._create_and_process_delivery_at_date([(self.product_1, 10)], date, warehouse=self.other_warehouse)
460 self.assertEqual(self.product_1.monthly_demand, 15)
461 self.assertEqual(self.product_1.with_context(warehouse_id=self.warehouse.id).monthly_demand, 5)
462 self.assertEqual(self.product_1.with_context(warehouse_id=self.other_warehouse.id).monthly_demand, 10)
463
464 # Create a PO for each warehouse and check the right quantity is added to the PO line.
465 po_1 = self.env['purchase.order'].create({
466 'partner_id': self.vendor.id,
467 'picking_type_id': self.warehouse.in_type_id.id,
468 })
469 self.assertEstimatedPrice(po_1, 500, warehouse=self.warehouse)
470 self.assertEstimatedPrice(po_1, 1000, warehouse=self.other_warehouse)
471 # Generate PO line for qty demand based on one specific warehouse.
472 self.actionAddAll(po_1, based_on='30_days', days=30, factor=100, warehouse=self.warehouse)
473 self.assertRecordValues(po_1.order_line, [
474 {'product_id': self.product_1.id, 'product_qty': 5},
475 ])
476 # Generate PO line for qty demand based on other warehouse
477 self.actionAddAll(po_1, based_on='30_days', days=30, factor=100, warehouse=self.other_warehouse)
478 self.assertRecordValues(po_1.order_line, [
479 {'product_id': self.product_1.id, 'product_qty': 10},
480 ])
481
482 po_2 = self.env['purchase.order'].create({
483 'partner_id': self.vendor.id,
484 'picking_type_id': self.other_warehouse.in_type_id.id,
485 })
486 self.assertEstimatedPrice(po_2, 500, warehouse=self.warehouse)
487 self.assertEstimatedPrice(po_2, 1000, warehouse=self.other_warehouse)
488 # Generate PO line for qty demand based on one specific warehouse.
489 self.actionAddAll(po_2, based_on='30_days', days=30, factor=100, warehouse=self.other_warehouse)
490 self.assertRecordValues(po_2.order_line, [
491 {'product_id': self.product_1.id, 'product_qty': 10},
492 ])
493
494 # Check the same with based_on actual demand.
495 product_ad = self.env['product.product'].create([{
496 'name': 'Product AD',
497 'standard_price': 60,
498 'is_storable': True,
499 }])
500
501 self.env['product.supplierinfo'].create([{
502 'partner_id': self.vendor.id,
503 'price': 55,
504 'product_id': product_ad.id,
505 }])
506 self.env['stock.quant']._update_available_quantity(product_ad, self.warehouse.lot_stock_id, 7)
507 self.env['stock.quant']._update_available_quantity(product_ad, self.other_warehouse.lot_stock_id, 5)
508
509 today = fields.Datetime.now()
510 delivery_1 = self._create_and_process_delivery_at_date(
511 [(product_ad, 10)], today, to_validate=False, warehouse=self.warehouse
512 )
513 delivery_1.scheduled_date = today + relativedelta(days=3)
514
515 delivery_2 = self._create_and_process_delivery_at_date(
516 [(product_ad, 9)], today, to_validate=False, warehouse=self.other_warehouse
517 )
518 delivery_2.scheduled_date = today + relativedelta(days=5)
519
520 context = {
521 'to_date': fields.Datetime.now() + relativedelta(days=6),
522 }
523 self.assertEqual(product_ad.with_context(context).virtual_available, -7)
524 context = {
525 'to_date': fields.Datetime.now() + relativedelta(days=6),
526 'warehouse_id': self.warehouse.id,
527 }
528 self.assertEqual(product_ad.with_context(context).virtual_available, -3)
529 context = {
530 'to_date': fields.Datetime.now() + relativedelta(days=6),
531 'warehouse_id': self.other_warehouse.id,
532 }
533 self.assertEqual(product_ad.with_context(context).virtual_available, -4)
534
535 # Create a PO for each warehouse and check the right quantity is added to the PO line.
536 po_1 = self.env['purchase.order'].create({
537 'partner_id': self.vendor.id,
538 'picking_type_id': self.warehouse.in_type_id.id,
539 })
540 self.assertEstimatedPrice(po_1, 165, based_on='actual_demand', warehouse=self.warehouse)
541 self.assertEstimatedPrice(po_1, 220, based_on='actual_demand', warehouse=self.other_warehouse)
542
543 # Generate PO line for qty demand based on 1st warehouse.
544 self.actionAddAll(po_1, based_on='actual_demand', days=30, factor=100, warehouse=self.other_warehouse)
545 self.assertRecordValues(po_1.order_line, [
546 {'product_id': product_ad.id, 'product_qty': 4},
547 ])
548 # Generate PO line for qty demand based on 2nd specific warehouse.
549 self.actionAddAll(po_1, based_on='actual_demand', days=30, factor=100, warehouse=self.warehouse)
550 self.assertRecordValues(po_1.order_line, [
551 {'product_id': product_ad.id, 'product_qty': 3},
552 ])
553
554 po_2 = self.env['purchase.order'].create({
555 'partner_id': self.vendor.id,
556 'picking_type_id': self.other_warehouse.in_type_id.id,
557 })
558 self.assertEstimatedPrice(po_2, 165, based_on='actual_demand', warehouse=self.warehouse)
559 self.assertEstimatedPrice(po_2, 220, based_on='actual_demand', warehouse=self.other_warehouse)
560
561 def test_purchase_order_suggest_pricelist_selection(self):
562 """ Pricelist selection for suggestion total price estimation
563 should follow:
564 1 - Best price discounted
565 2 - Least qty pricelist if no price match
566 3 - Product standard price
567 """
568 today = fields.Datetime.now()
569 po = self.env['purchase.order'].create({'partner_id': self.vendor.id})
570 product = self.env['product.product'].create({
571 'name': 'Product 7',
572 'standard_price': 20,
573 'is_storable': True,
574 })
575 self.env['product.supplierinfo'].create([{
576 'partner_id': self.vendor.id,
577 'price': 17,
578 'product_id': product.id,
579 'min_qty': 2
580 }, {
581 'partner_id': self.vendor.id,
582 'price': 13,
583 'product_id': product.id,
584 'min_qty': 3
585 }])
586 self.env['stock.quant']._update_available_quantity(product, self.stock_location, 1)
587 self._create_and_process_delivery_at_date(
588 [(product, 1)], today - relativedelta(days=1)
589 )
590 self.assertEstimatedPrice(po, 17, based_on='one_week', days=7) # suggested qty 1 --> should use lowest qty pricelist
591 self.assertEstimatedPrice(po, 34, based_on='one_week', days=14) # suggested qty 2 --> should matching pricelist
592 self.assertEstimatedPrice(po, 52, based_on='one_week', days=28) # suggested qty 4 --> should matching pricelist
593
594 partner_2 = self.env['res.partner'].create({'name': "No pricelist"})
595 po_2 = self.env['purchase.order'].create({'partner_id': partner_2.id})
596 self.assertEstimatedPrice(po_2, 20, based_on='one_week', days=7) # No pricelist --> should use standard price
597
598 def test_purchase_order_suggest_search_panel_ux(self):
599 """ Tests the purchase catalog suggest component, in particular:
600 - Suggest component: Hidding, Estimated price, Add all, Changing warehouse, Saving defaults
601 - Suggest record interactions: Monthly demand & forecast, Add button
602 - Suggest kanban interactions: Add All Filter, and kanban ordering
603 """
604 today = fields.Datetime.now()
605 test_category = self.env['product.category'].create({
606 'name': "Test Category",
607 })
608 test_category_goods = self.env['product.category'].create({
609 'name': "Goods",
610 })
611 self.product_1.categ_id = test_category_goods.id
612 test_product = self.env['product.product'].create([{
613 'name': "test_product",
614 'categ_id': test_category.id,
615 'is_storable': True,
616 }])
617 self.env['product.supplierinfo'].create([{
618 'partner_id': self.vendor.id,
619 'min_qty': 1,
620 'price': 20,
621 'product_id': test_product.id,
622 }])
623
624 # Create and confirm a move yesterday (used to check monthly_demand/suggest)
625 self.env['stock.quant']._update_available_quantity(test_product, self.stock_location, 24)
626 self._create_and_process_delivery_at_date(
627 [(test_product, 12)], date=fields.Datetime.now() - relativedelta(days=1)
628 )
629 # Create and confirm 10 days ago (used to check monthly_demand/suggest with 7 days)
630 self._create_and_process_delivery_at_date(
631 [(test_product, 12)], date=fields.Datetime.now() - relativedelta(days=10)
632 )
633 self.assertEqual(test_product.monthly_demand, 24)
634
635 # Create and mark as todo a move in 18 & 20 days (for checking forecast on records and suggestion with Forecasted mode)
636 self._create_and_process_delivery_at_date(
637 [(test_product, 50)], date=today + relativedelta(days=18), to_validate=False
638 )
639 self._create_and_process_delivery_at_date(
640 [(test_product, 50)], date=today + relativedelta(days=20), to_validate=False
641 )
642 # Create a move yesterday on another warehouse
643 other_warehouse = self.other_warehouse
644 self.env['stock.quant']._update_available_quantity(test_product, other_warehouse.lot_stock_id, 1)
645 self._create_and_process_delivery_at_date(
646 [(test_product, 1)], date=today - relativedelta(days=1), warehouse=other_warehouse
647 )
648 self.start_tour('/odoo/purchase', "test_purchase_order_suggest_search_panel_ux", login='admin')