Debian Codegraph @

addons/purchase_stock/tests/test_purchase_lead_time.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from datetime import datetime, timedelta, time
5 from unittest.mock import patch
6
7 from odoo import Command, fields
8 from .common import PurchaseTestCommon
9 from odoo.tests import Form, freeze_time
10
11
12 class TestPurchaseLeadTime(PurchaseTestCommon):
13
14 def test_00_product_company_level_delays(self):
15 """ To check dates, set product's Delivery Lead Time
16 and company's Purchase Lead Time."""
17 self._use_route_buy(self.product)
18 # Make procurement request from product_1's form view, create procurement and check it's state
19 date_planned = fields.Datetime.now() + timedelta(days=10)
20 self._make_procurement(self.product, 15.00, date_planned=date_planned)
21 purchase = self.env['purchase.order.line'].search([('product_id', '=', self.product.id)], limit=1).order_id
22
23 # Confirm purchase order
24 purchase.button_confirm()
25
26 # Check order date of purchase order
27 order_date = fields.Datetime.from_string(date_planned) - timedelta(days=self.product.seller_ids.delay)
28 self.assertEqual(purchase.date_order, order_date, 'Order date should be equal to: Date of the procurement order - Purchase Lead Time - Delivery Lead Time.')
29
30 # Check scheduled date of purchase order
31 schedule_date = order_date + timedelta(days=self.product.seller_ids.delay)
32 self.assertEqual(purchase.order_line.date_planned, schedule_date, 'Schedule date should be equal to: Order date of Purchase order + Delivery Lead Time.')
33
34 # check the picking created or not
35 self.assertTrue(purchase.picking_ids, "Picking should be created.")
36
37 # Check scheduled and deadline date of In Type shipment
38 self.assertEqual(purchase.picking_ids.scheduled_date, schedule_date, 'Schedule date of In type shipment should be equal to: schedule date of purchase order.')
39 self.assertEqual(purchase.picking_ids.date_deadline, schedule_date, 'Deadline date of should be equal to: schedule date of purchase order.')
40
41 def test_01_product_level_delay(self):
42 """ To check schedule dates of multiple purchase order line of the same purchase order,
43 we create two procurements for the two different product with same vendor
44 and different Delivery Lead Time."""
45 self._use_route_buy(self.product)
46 self.product_2 = self.product.copy()
47 self._use_route_buy(self.product_2)
48 # Make procurement request from product_1's form view, create procurement and check it's state
49 date_planned1 = fields.Datetime.now() + timedelta(days=5)
50 self._make_procurement(self.product, 10.00, date_planned=date_planned1)
51 purchase1 = self.env['purchase.order.line'].search([('product_id', '=', self.product.id)], limit=1).order_id
52
53 # Make procurement request from product_2's form view, create procurement and check it's state
54 date_planned2 = fields.Datetime.now() + timedelta(days=10)
55 self._make_procurement(self.product_2, 5.00, date_planned=date_planned2)
56 purchase2 = self.env['purchase.order.line'].search([('product_id', '=', self.product_2.id)], limit=1).order_id
57
58 # Check purchase order is same or not
59 self.assertEqual(purchase1, purchase2, 'Purchase orders should be same for the two different product with same vendor.')
60
61 # Confirm purchase order
62 purchase1.button_confirm()
63
64 # Check order date of purchase order
65 order_line_pro_1 = purchase2.order_line.filtered(lambda r: r.product_id == self.product)
66 order_line_pro_2 = purchase2.order_line.filtered(lambda r: r.product_id == self.product_2)
67 order_date = date_planned1 - timedelta(days=self.product.seller_ids.delay)
68 self.assertEqual(purchase2.date_order, order_date, 'Order date should be equal to: Date of the procurement order - Delivery Lead Time.')
69
70 # Check scheduled date of purchase order line for product_1
71 self.assertEqual(order_line_pro_1.date_planned, date_planned1, 'Schedule date of purchase order line for product_1 should be equal to: Order date of purchase order + Delivery Lead Time of product_1.')
72
73 # Check scheduled date of purchase order line for product_2
74 self.assertEqual(order_line_pro_2.date_planned, date_planned2, 'Schedule date of purchase order line for product_2 should be equal to: Order date of purchase order + Delivery Lead Time of product_2.')
75
76 # Check the picking created or not
77 self.assertTrue(purchase2.picking_ids, "Picking should be created.")
78
79 # Check scheduled date of In Type shipment
80 picking_schedule_date = min(date_planned1, date_planned2)
81 self.assertEqual(purchase2.picking_ids.scheduled_date, picking_schedule_date,
82 'Schedule date of In type shipment should be same as schedule date of purchase order.')
83
84 # Check deadline of pickings
85 self.assertEqual(fields.Date.to_date(purchase2.picking_ids.date_deadline), fields.Date.to_date(purchase1.date_planned), "Deadline of pickings should be equals to the receipt date of purchase")
86 purchase_form = Form(purchase2)
87 purchase_form.date_planned = purchase2.date_planned + timedelta(days=2)
88 purchase_form.save()
89 self.assertEqual(purchase2.picking_ids.date_deadline, purchase2.date_planned, "Deadline of pickings should be propagate")
90
91 def test_02_product_level_delay(self):
92 """ To check schedule dates of multiple purchase order line of the same purchase order,
93 we create two procurements for the two different product with same vendor
94 and different supplier Lead Time. Vendor grouping rfq option is 'by day'."""
95 self._use_route_buy(self.product)
96 self.product_2 = self.product.copy()
97 self._use_route_buy(self.product_2)
98
99 self.vendor.group_rfq = 'day'
100 # Make procurement request from product_1's form view, create procurement and check it's state
101 date_planned = fields.Datetime.now()
102 ref1, ref2 = self.env['stock.reference'].create([
103 {'name': 'SO001'},
104 {'name': 'SO002'},
105 ])
106 self._make_procurement(self.product, 10.00, date_planned=date_planned, procurement_values={'reference_ids': ref1})
107 purchase1 = self.env['purchase.order.line'].search([('product_id', '=', self.product.id)], limit=1).order_id
108
109 # Make procurement request from product_2's form view, create procurement and check it's state
110 self._make_procurement(self.product_2, 5.00, date_planned=date_planned, procurement_values={'reference_ids': ref2})
111 purchase2 = self.env['purchase.order.line'].search([('product_id', '=', self.product_2.id)], limit=1).order_id
112
113 # Check purchase order is same or not
114 self.assertEqual(purchase1, purchase2, 'Purchase orders should be same for the two different product with same vendor.')
115
116 # Confirm purchase order
117 purchase1.button_confirm()
118
119 # Check order date of purchase order
120 order_line_pro_1 = purchase1.order_line.filtered(lambda r: r.product_id == self.product)
121 order_line_pro_2 = purchase2.order_line.filtered(lambda r: r.product_id == self.product_2)
122 self.assertEqual(purchase2.date_planned, date_planned, 'planned date should be equal to procurement date')
123 deadline = date_planned - timedelta(days=max((self.product | self.product_2).seller_ids.mapped('delay')))
124 self.assertEqual(purchase2.date_order, deadline, 'Deadline date should be equal to: Date of the procurement order - max Lead Time.')
125
126 # Check scheduled date of purchase order line for product_1
127 self.assertEqual(order_line_pro_1.date_planned, date_planned, 'Schedule date of purchase order line for product_1 should be equal to: Order date of purchase order + Delivery Lead Time of product_1.')
128
129 # Check scheduled date of purchase order line for product_2
130 self.assertEqual(order_line_pro_2.date_planned, date_planned, 'Schedule date of purchase order line for product_2 should be equal to: Order date of purchase order + Delivery Lead Time of product_2.')
131
132 @freeze_time("2025-09-10 10:00:00")
133 def test_03_group_week(self):
134 """ Make the rfq for the supplier group by week on a specific day. Check the planned date
135 and deadline date are compute accordingly.
136
137 8(Mon) -- 9(Tue) -- 10(Wed) -- 11(Thu) -- 12(Fri) -- 13(Sat) -- 14(Sun)
138 today planned 1 planned 2
139
140 15(Mon) -- 16(Tue) -- 17(Wed) -- 18(Thu) -- 19(Fri) -- 20(Sat) -- 21(Sun)
141 PO
142
143
144 """
145 self._use_route_buy(self.product)
146 self.product.seller_ids.delay = 0
147 self.product_2 = self.product.copy()
148 self._use_route_buy(self.product_2)
149 self.product_2.seller_ids.delay = 2
150 self.vendor.group_rfq = 'week'
151 self.vendor.group_on = '2' # Tuesday
152 # Make procurement request from product_1's form view, create procurement and check it's state
153 date_planned = fields.Datetime.now() + timedelta(days=2)
154 ref1, ref2 = self.env['stock.reference'].create([
155 {'name': 'SO001'},
156 {'name': 'SO002'},
157 ])
158 self._make_procurement(self.product, 10.00, date_planned=date_planned, procurement_values={'reference_ids': ref1})
159 purchase1 = self.env['purchase.order.line'].search([('product_id', '=', self.product.id)], limit=1).order_id
160
161 # Make procurement request from product_2's form view, create procurement and check it's state
162 date_planned = fields.Datetime.now() + timedelta(days=3)
163 self._make_procurement(self.product_2, 5.00, date_planned=date_planned, procurement_values={'reference_ids': ref2})
164 purchase2 = self.env['purchase.order.line'].search([('product_id', '=', self.product_2.id)], limit=1).order_id
165
166 self.assertEqual(purchase1, purchase2, 'Purchase orders should be same for the two different product with same vendor.')
167
168 purchase1.button_confirm()
169
170 # Check date deadline and date planned of purchase order. Supplier of product 2 has a delay of 2 days.
171 # The purchase order is planned on Tuesday 16th, so the date deadline is 2 days before, on Sunday 14th.
172 date_p = fields.Datetime.from_string('2025-09-16 10:00:00')
173 date_d = fields.Datetime.from_string('2025-09-14 10:00:00')
174 self.assertRecordValues(purchase1, [{
175 'date_planned': date_p, 'date_order': date_d,
176 }])
177 self.assertRecordValues(purchase1.order_line, [
178 {'date_planned': date_p},
179 {'date_planned': date_p},
180 ])
181
182 def test_merge_po_line(self):
183 """Change that merging po line for same procurement is done."""
184
185 # create a product with manufacture route
186 product_1 = self.env['product.product'].create({
187 'name': 'AAA',
188 'route_ids': [Command.link(self.route_buy.id)],
189 'seller_ids': [Command.create({'partner_id': self.partner.id, 'delay': 5})]
190 })
191
192 # create a move for product_1 from stock to output and reserve to trigger the
193 # rule
194 move_1 = self.env['stock.move'].create({
195 'product_id': product_1.id,
196 'product_uom': self.uom.id,
197 'location_id': self.stock_location.id,
198 'location_dest_id': self.warehouse.wh_output_stock_loc_id.id,
199 'product_uom_qty': 10,
200 'procure_method': 'make_to_order'
201 })
202
203 move_1._action_confirm()
204 po_line = self.env['purchase.order.line'].search([
205 ('product_id', '=', product_1.id),
206 ])
207 self.assertEqual(len(po_line), 1, 'the purchase order line is not created')
208 self.assertEqual(po_line.product_qty, 10, 'the purchase order line has a wrong quantity')
209
210 move_2 = self.env['stock.move'].create({
211 'product_id': product_1.id,
212 'product_uom': self.uom.id,
213 'location_id': self.stock_location.id,
214 'location_dest_id': self.warehouse.wh_output_stock_loc_id.id,
215 'product_uom_qty': 5,
216 'procure_method': 'make_to_order'
217 })
218
219 move_2._action_confirm()
220 po_line = self.env['purchase.order.line'].search([
221 ('product_id', '=', product_1.id),
222 ])
223 self.assertEqual(len(po_line), 1, 'the purchase order lines should be merged')
224 self.assertEqual(po_line.product_qty, 15, 'the purchase order line has a wrong quantity')
225
226 def test_merge_po_line_3(self):
227 """Change merging po line if same procurement is done depending on custom values."""
228 self.t_shirt = self.env['product.product'].create({
229 'name': 'T-shirt',
230 'description': 'Internal Notes',
231 })
232 self._use_route_buy(self.t_shirt)
233 # The seller has a specific product name and code which must be kept in the PO line
234 self.t_shirt.seller_ids.write({
235 'delay': 5,
236 'product_name': 'Vendor Name',
237 'product_code': 'Vendor Code',
238 })
239 partner = self.t_shirt.seller_ids[:1].partner_id
240 t_shirt = self.t_shirt.with_context(
241 lang=partner.lang,
242 partner_id=partner.id,
243 )
244 t_shirt.description_pickingin = 'Receive with care'
245
246 # Create procurement order of product_1
247 StockRule = self.env['stock.rule']
248 procurement_values = {
249 'warehouse_id': self.warehouse,
250 'rule_id': self.warehouse.buy_pull_id,
251 'date_planned': fields.Datetime.now() + timedelta(days=10),
252 'group_id': False,
253 'route_ids': [],
254 }
255
256 procurement_values['product_description_variants'] = 'Color (Red)'
257 order_1_values = procurement_values
258 StockRule.run([self.env['stock.rule'].Procurement(
259 self.t_shirt, 5, self.uom, self.warehouse.lot_stock_id,
260 self.t_shirt.name, '/', self.env.company, order_1_values)
261 ])
262 purchase_order = self.env['purchase.order.line'].search([('product_id', '=', self.t_shirt.id)], limit=1).order_id
263 self.assertEqual(len(purchase_order.order_line), 1, 'wrong number of order line is created')
264 self.assertEqual(purchase_order.order_line.name, t_shirt.display_name + "\n" + "Color (Red)", 'wrong description in po lines')
265
266 procurement_values['product_description_variants'] = 'Color (Red)'
267 order_2_values = procurement_values
268 StockRule.run([self.env['stock.rule'].Procurement(
269 self.t_shirt, 10, self.uom, self.warehouse.lot_stock_id,
270 self.t_shirt.name, '/', self.env.company, order_2_values)
271 ])
272 self.env['stock.rule'].run_scheduler()
273 self.assertEqual(len(purchase_order.order_line), 1, 'line with same custom value should be merged')
274 self.assertEqual(purchase_order.order_line[0].product_qty, 15, 'line with same custom value should be merged and qty should be update')
275
276 procurement_values['product_description_variants'] = 'Color (Green)'
277
278 order_3_values = procurement_values
279 StockRule.run([self.env['stock.rule'].Procurement(
280 self.t_shirt, 10, self.uom, self.warehouse.lot_stock_id,
281 self.t_shirt.name, '/', self.env.company, order_3_values)
282 ])
283 self.assertEqual(len(purchase_order.order_line), 2, 'line with different custom value should not be merged')
284 self.assertEqual(purchase_order.order_line.filtered(lambda x: x.product_qty == 15).name, t_shirt.display_name + "\n" + "Color (Red)", 'wrong description in po lines')
285 self.assertEqual(purchase_order.order_line.filtered(lambda x: x.product_qty == 10).name, t_shirt.display_name + "\n" + "Color (Green)", 'wrong description in po lines')
286
287 purchase_order.button_confirm()
288 self.assertEqual(purchase_order.picking_ids[0].move_ids.filtered(lambda x: x.product_uom_qty == 15).description_picking, t_shirt.display_name + "\n" + "Receive with care", 'wrong description in picking')
289 self.assertEqual(purchase_order.picking_ids[0].move_ids.filtered(lambda x: x.product_uom_qty == 10).description_picking, t_shirt.display_name + "\n" + "Receive with care", 'wrong description in picking')
290
291 def test_reordering_days_to_purchase(self):
292 self.company.horizon_days = 0
293 self.patcher = patch('odoo.addons.stock.models.stock_orderpoint.fields.Date', wraps=fields.Date)
294 self.mock_date = self.startPatcher(self.patcher)
295
296 vendor = self.env['res.partner'].create({
297 'name': 'Colruyt'
298 })
299 vendor2 = self.env['res.partner'].create({
300 'name': 'Delhaize'
301 })
302
303 self.company.days_to_purchase = 2.0
304
305 # Test if the orderpoint is created when opening the replenishment view
306 prod = self.env['product.product'].create({
307 'name': 'Carrot',
308 'is_storable': True,
309 'seller_ids': [
310 Command.create({'partner_id': vendor.id, 'delay': 1.0, 'company_id': self.company.id})
311 ]
312 })
313
314 self.env['stock.move'].create({
315 'date': datetime.today() + timedelta(days=3),
316 'product_id': prod.id,
317 'product_uom': prod.uom_id.id,
318 'product_uom_qty': 5.0,
319 'location_id': self.stock_location.id,
320 'location_dest_id': self.customer_location.id,
321 })._action_confirm()
322 self.env['stock.warehouse.orderpoint'].action_open_orderpoints()
323 replenishment = self.env['stock.warehouse.orderpoint'].search([
324 ('product_id', '=', prod.id),
325 ])
326 self.assertEqual(len(replenishment), 1)
327
328 # Test if purchase orders are created according to the days to purchase
329 product = self.env['product.product'].create({
330 'name': 'Chicory',
331 'is_storable': True,
332 'seller_ids': [
333 Command.create({'partner_id': vendor2.id, 'delay': 15.0, 'company_id': self.other_company.id}),
334 Command.create({'partner_id': vendor.id, 'delay': 1.0, 'company_id': self.company.id})
335 ]
336 })
337 orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
338 orderpoint_form.product_id = product
339 orderpoint_form.product_min_qty = 0.0
340 self.company.horizon_days = 1
341 orderpoint_form.save()
342
343 orderpoint_form = Form(self.env['stock.warehouse.orderpoint'].with_company(self.other_company))
344 orderpoint_form.product_id = product
345 orderpoint_form.product_min_qty = 0.0
346 orderpoint_form.save()
347
348 delivery_moves = self.env['stock.move']
349 for i in range(0, 6):
350 delivery_moves |= self.env['stock.move'].create({
351 'date': datetime.today() + timedelta(days=i),
352 'product_id': product.id,
353 'product_uom': product.uom_id.id,
354 'product_uom_qty': 5.0,
355 'location_id': self.stock_location.id,
356 'location_dest_id': self.customer_location.id,
357 })
358 delivery_moves._action_confirm()
359
360 self.env['stock.rule'].run_scheduler()
361 po_line = self.env['purchase.order.line'].search([('product_id', '=', product.id)])
362 expected_date_order = fields.Date.today() + timedelta(days=2)
363 self.assertEqual(fields.Date.to_date(po_line.order_id.date_order), expected_date_order)
364 self.assertEqual(len(po_line), 1)
365 self.assertEqual(po_line.product_uom_qty, 25.0)
366 self.assertEqual(len(po_line.order_id), 1)
367
368 self.mock_date.today.return_value = fields.Date.today() + timedelta(days=2)
369 self.env.invalidate_all()
370 self.env['stock.rule'].run_scheduler()
371 po_line02 = self.env['purchase.order.line'].search([('product_id', '=', product.id)])
372 self.assertEqual(po_line02, po_line, 'The orderpoint execution should not create a new POL')
373 self.assertEqual(fields.Date.to_date(po_line.order_id.date_order), expected_date_order, 'The Order Deadline should not change')
374 self.assertEqual(po_line.product_uom_qty, 30.0, 'The existing POL should be updated with the quantity of the last execution')
375
376 def test_supplier_lead_time(self):
377 """ Basic stock configuration and a supplier with a minimum qty and a lead time """
378 self.env['stock.warehouse.orderpoint'].search([]).unlink()
379 orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
380 orderpoint_form.product_id = self.product
381 orderpoint_form.product_min_qty = 10
382 orderpoint_form.product_max_qty = 50
383 orderpoint_form.save()
384
385 self.env['product.supplierinfo'].search([('product_tmpl_id', '=', self.product.product_tmpl_id.id)]).unlink()
386 self.env['product.supplierinfo'].create({
387 'partner_id': self.partner.id,
388 'min_qty': 1,
389 'price': 1,
390 'delay': 7,
391 'product_tmpl_id': self.product.product_tmpl_id.id,
392 })
393
394 self.env['stock.rule'].run_scheduler()
395 purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.partner.id)])
396
397 today = datetime.combine(fields.Datetime.now(), time(12))
398 self.assertEqual(purchase_order.date_order, today)
399 self.assertEqual(purchase_order.date_planned, today + timedelta(days=7))
400
401 def test_lead_time_with_no_supplier(self):
402 """Test that lead time is incremented by 365 days (1 year) when there
403 is no supplier defined on a product with buy route.
404 """
405 self._use_route_buy(self.product, create_seller=False)
406 orderpoint = self.env['stock.warehouse.orderpoint'].create({
407 'name': 'test',
408 'location_id': self.stock_location.id,
409 'product_id': self.product.id,
410 'product_min_qty': 0,
411 'product_max_qty': 5,
412 })
413
414 self.assertEqual(orderpoint.lead_days, 365)