test_purchase_lead_time.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime, timedelta, time
  4. from unittest.mock import patch
  5. from odoo import fields
  6. from .common import PurchaseTestCommon
  7. from odoo.tests.common import Form
  8. class TestPurchaseLeadTime(PurchaseTestCommon):
  9. def test_00_product_company_level_delays(self):
  10. """ To check dates, set product's Delivery Lead Time
  11. and company's Purchase Lead Time."""
  12. company = self.env.ref('base.main_company')
  13. # Update company with Purchase Lead Time
  14. company.write({'po_lead': 3.00})
  15. # Make procurement request from product_1's form view, create procurement and check it's state
  16. date_planned = fields.datetime.now() + timedelta(days=10)
  17. self._create_make_procurement(self.product_1, 15.00, date_planned=date_planned)
  18. purchase = self.env['purchase.order.line'].search([('product_id', '=', self.product_1.id)], limit=1).order_id
  19. # Confirm purchase order
  20. purchase.button_confirm()
  21. # Check order date of purchase order
  22. order_date = fields.Datetime.from_string(date_planned) - timedelta(days=self.product_1.seller_ids.delay)
  23. self.assertEqual(purchase.date_order, order_date, 'Order date should be equal to: Date of the procurement order - Purchase Lead Time - Delivery Lead Time.')
  24. # Check scheduled date of purchase order
  25. schedule_date = order_date + timedelta(days=self.product_1.seller_ids.delay)
  26. self.assertEqual(purchase.order_line.date_planned, schedule_date, 'Schedule date should be equal to: Order date of Purchase order + Delivery Lead Time.')
  27. # check the picking created or not
  28. self.assertTrue(purchase.picking_ids, "Picking should be created.")
  29. # Check scheduled and deadline date of In Type shipment
  30. self.assertEqual(purchase.picking_ids.scheduled_date, schedule_date, 'Schedule date of In type shipment should be equal to: schedule date of purchase order.')
  31. self.assertEqual(purchase.picking_ids.date_deadline, schedule_date, 'Deadline date of should be equal to: schedule date of purchase order.')
  32. def test_01_product_level_delay(self):
  33. """ To check schedule dates of multiple purchase order line of the same purchase order,
  34. we create two procurements for the two different product with same vendor
  35. and different Delivery Lead Time."""
  36. company = self.env.ref('base.main_company')
  37. company.write({'po_lead': 0.00})
  38. # Make procurement request from product_1's form view, create procurement and check it's state
  39. date_planned1 = fields.datetime.now() + timedelta(days=10)
  40. self._create_make_procurement(self.product_1, 10.00, date_planned=date_planned1)
  41. purchase1 = self.env['purchase.order.line'].search([('product_id', '=', self.product_1.id)], limit=1).order_id
  42. # Make procurement request from product_2's form view, create procurement and check it's state
  43. date_planned2 = fields.datetime.now() + timedelta(days=10)
  44. self._create_make_procurement(self.product_2, 5.00, date_planned=date_planned2)
  45. purchase2 = self.env['purchase.order.line'].search([('product_id', '=', self.product_2.id)], limit=1).order_id
  46. # Check purchase order is same or not
  47. self.assertEqual(purchase1, purchase2, 'Purchase orders should be same for the two different product with same vendor.')
  48. # Confirm purchase order
  49. purchase1.button_confirm()
  50. # Check order date of purchase order
  51. order_line_pro_1 = purchase2.order_line.filtered(lambda r: r.product_id == self.product_1)
  52. order_line_pro_2 = purchase2.order_line.filtered(lambda r: r.product_id == self.product_2)
  53. order_date = date_planned1 - timedelta(days=self.product_1.seller_ids.delay)
  54. self.assertEqual(purchase2.date_order, order_date, 'Order date should be equal to: Date of the procurement order - Delivery Lead Time.')
  55. # Check scheduled date of purchase order line for product_1
  56. 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.')
  57. # Check scheduled date of purchase order line for product_2
  58. 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.')
  59. # Check the picking created or not
  60. self.assertTrue(purchase2.picking_ids, "Picking should be created.")
  61. # Check scheduled date of In Type shipment
  62. picking_schedule_date = min(date_planned1, date_planned2)
  63. self.assertEqual(purchase2.picking_ids.scheduled_date, picking_schedule_date,
  64. 'Schedule date of In type shipment should be same as schedule date of purchase order.')
  65. # Check deadline of pickings
  66. 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")
  67. purchase_form = Form(purchase2)
  68. purchase_form.date_planned = purchase2.date_planned + timedelta(days=2)
  69. purchase_form.save()
  70. self.assertEqual(purchase2.picking_ids.date_deadline, purchase2.date_planned, "Deadline of pickings should be propagate")
  71. def test_02_product_route_level_delays(self):
  72. """ In order to check dates, set product's Delivery Lead Time
  73. and warehouse route's delay."""
  74. company = self.env.ref('base.main_company')
  75. company.write({'po_lead': 1.00})
  76. # Update warehouse_1 with Incoming Shipments 3 steps
  77. self.warehouse_1.write({'reception_steps': 'three_steps'})
  78. # Set delay on push rule
  79. for push_rule in self.warehouse_1.reception_route_id.rule_ids:
  80. push_rule.write({'delay': 2})
  81. rule_delay = sum(self.warehouse_1.reception_route_id.rule_ids.mapped('delay'))
  82. date_planned = fields.Datetime.to_string(fields.datetime.now() + timedelta(days=10))
  83. # Create procurement order of product_1
  84. self.env['procurement.group'].run([self.env['procurement.group'].Procurement(
  85. self.product_1, 5.000, self.uom_unit, self.warehouse_1.lot_stock_id, 'Test scheduler for RFQ', '/', self.env.company,
  86. {
  87. 'warehouse_id': self.warehouse_1,
  88. 'date_planned': date_planned, # 10 days added to current date of procurement to get future schedule date and order date of purchase order.
  89. 'date_deadline': date_planned, # 10 days added to current date of procurement to get future schedule date and order date of purchase order.
  90. 'rule_id': self.warehouse_1.buy_pull_id,
  91. 'group_id': False,
  92. 'route_ids': [],
  93. }
  94. )])
  95. # Confirm purchase order
  96. purchase = self.env['purchase.order.line'].search([('product_id', '=', self.product_1.id)], limit=1).order_id
  97. purchase.button_confirm()
  98. # Check order date of purchase order
  99. order_date = fields.Datetime.from_string(date_planned) - timedelta(days=self.product_1.seller_ids.delay + rule_delay)
  100. self.assertEqual(purchase.date_order, order_date, 'Order date should be equal to: Date of the procurement order - Delivery Lead Time(supplier and pull rules).')
  101. # Check scheduled date of purchase order
  102. schedule_date = order_date + timedelta(days=self.product_1.seller_ids.delay + rule_delay)
  103. self.assertEqual(date_planned, str(schedule_date), 'Schedule date should be equal to: Order date of Purchase order + Delivery Lead Time(supplier and pull rules).')
  104. # Check the picking crated or not
  105. self.assertTrue(purchase.picking_ids, "Picking should be created.")
  106. # Check scheduled date of Internal Type shipment
  107. incoming_shipment1 = self.env['stock.picking'].search([('move_ids.product_id', 'in', (self.product_1.id, self.product_2.id)), ('picking_type_id', '=', self.warehouse_1.int_type_id.id), ('location_id', '=', self.warehouse_1.wh_input_stock_loc_id.id), ('location_dest_id', '=', self.warehouse_1.wh_qc_stock_loc_id.id)])
  108. incoming_shipment1_date = order_date + timedelta(days=self.product_1.seller_ids.delay)
  109. self.assertEqual(incoming_shipment1.scheduled_date, incoming_shipment1_date, 'Schedule date of Internal Type shipment for input stock location should be equal to: schedule date of purchase order + push rule delay.')
  110. self.assertEqual(incoming_shipment1.date_deadline, incoming_shipment1_date)
  111. old_deadline1 = incoming_shipment1.date_deadline
  112. incoming_shipment2 = self.env['stock.picking'].search([('picking_type_id', '=', self.warehouse_1.int_type_id.id), ('location_id', '=', self.warehouse_1.wh_qc_stock_loc_id.id), ('location_dest_id', '=', self.warehouse_1.lot_stock_id.id)])
  113. incoming_shipment2_date = schedule_date - timedelta(days=incoming_shipment2.move_ids[0].rule_id.delay)
  114. self.assertEqual(incoming_shipment2.scheduled_date, incoming_shipment2_date, 'Schedule date of Internal Type shipment for quality control stock location should be equal to: schedule date of Internal type shipment for input stock location + push rule delay..')
  115. self.assertEqual(incoming_shipment2.date_deadline, incoming_shipment2_date)
  116. old_deadline2 = incoming_shipment2.date_deadline
  117. # Modify the date_planned of the purchase -> propagate the deadline
  118. purchase_form = Form(purchase)
  119. purchase_form.date_planned = purchase.date_planned + timedelta(days=1)
  120. purchase_form.save()
  121. self.assertEqual(incoming_shipment2.date_deadline, old_deadline2 + timedelta(days=1), 'Deadline should be propagate')
  122. self.assertEqual(incoming_shipment1.date_deadline, old_deadline1 + timedelta(days=1), 'Deadline should be propagate')
  123. def test_merge_po_line(self):
  124. """Change that merging po line for same procurement is done."""
  125. # create a product with manufacture route
  126. product_1 = self.env['product.product'].create({
  127. 'name': 'AAA',
  128. 'route_ids': [(4, self.route_buy)],
  129. 'seller_ids': [(0, 0, {'partner_id': self.partner_1.id, 'delay': 5})]
  130. })
  131. # create a move for product_1 from stock to output and reserve to trigger the
  132. # rule
  133. move_1 = self.env['stock.move'].create({
  134. 'name': 'move_1',
  135. 'product_id': product_1.id,
  136. 'product_uom': self.ref('uom.product_uom_unit'),
  137. 'location_id': self.ref('stock.stock_location_stock'),
  138. 'location_dest_id': self.ref('stock.stock_location_output'),
  139. 'product_uom_qty': 10,
  140. 'procure_method': 'make_to_order'
  141. })
  142. move_1._action_confirm()
  143. po_line = self.env['purchase.order.line'].search([
  144. ('product_id', '=', product_1.id),
  145. ])
  146. self.assertEqual(len(po_line), 1, 'the purchase order line is not created')
  147. self.assertEqual(po_line.product_qty, 10, 'the purchase order line has a wrong quantity')
  148. move_2 = self.env['stock.move'].create({
  149. 'name': 'move_2',
  150. 'product_id': product_1.id,
  151. 'product_uom': self.ref('uom.product_uom_unit'),
  152. 'location_id': self.ref('stock.stock_location_stock'),
  153. 'location_dest_id': self.ref('stock.stock_location_output'),
  154. 'product_uom_qty': 5,
  155. 'procure_method': 'make_to_order'
  156. })
  157. move_2._action_confirm()
  158. po_line = self.env['purchase.order.line'].search([
  159. ('product_id', '=', product_1.id),
  160. ])
  161. self.assertEqual(len(po_line), 1, 'the purchase order lines should be merged')
  162. self.assertEqual(po_line.product_qty, 15, 'the purchase order line has a wrong quantity')
  163. def test_merge_po_line_3(self):
  164. """Change merging po line if same procurement is done depending on custom values."""
  165. company = self.env.ref('base.main_company')
  166. company.write({'po_lead': 0.00})
  167. # The seller has a specific product name and code which must be kept in the PO line
  168. self.t_shirt.seller_ids.write({
  169. 'product_name': 'Vendor Name',
  170. 'product_code': 'Vendor Code',
  171. })
  172. partner = self.t_shirt.seller_ids[:1].partner_id
  173. t_shirt = self.t_shirt.with_context(
  174. lang=partner.lang,
  175. partner_id=partner.id,
  176. )
  177. # Create procurement order of product_1
  178. ProcurementGroup = self.env['procurement.group']
  179. procurement_values = {
  180. 'warehouse_id': self.warehouse_1,
  181. 'rule_id': self.warehouse_1.buy_pull_id,
  182. 'date_planned': fields.datetime.now() + timedelta(days=10),
  183. 'group_id': False,
  184. 'route_ids': [],
  185. }
  186. procurement_values['product_description_variants'] = 'Color (Red)'
  187. order_1_values = procurement_values
  188. ProcurementGroup.run([self.env['procurement.group'].Procurement(
  189. self.t_shirt, 5, self.uom_unit, self.warehouse_1.lot_stock_id,
  190. self.t_shirt.name, '/', self.env.company, order_1_values)
  191. ])
  192. purchase_order = self.env['purchase.order.line'].search([('product_id', '=', self.t_shirt.id)], limit=1).order_id
  193. order_line_description = purchase_order.order_line.product_id.description_pickingin or ''
  194. self.assertEqual(len(purchase_order.order_line), 1, 'wrong number of order line is created')
  195. self.assertEqual(purchase_order.order_line.name, t_shirt.display_name + "\n" + "Color (Red)", 'wrong description in po lines')
  196. procurement_values['product_description_variants'] = 'Color (Red)'
  197. order_2_values = procurement_values
  198. ProcurementGroup.run([self.env['procurement.group'].Procurement(
  199. self.t_shirt, 10, self.uom_unit, self.warehouse_1.lot_stock_id,
  200. self.t_shirt.name, '/', self.env.company, order_2_values)
  201. ])
  202. self.env['procurement.group'].run_scheduler()
  203. self.assertEqual(len(purchase_order.order_line), 1, 'line with same custom value should be merged')
  204. self.assertEqual(purchase_order.order_line[0].product_qty, 15, 'line with same custom value should be merged and qty should be update')
  205. procurement_values['product_description_variants'] = 'Color (Green)'
  206. order_3_values = procurement_values
  207. ProcurementGroup.run([self.env['procurement.group'].Procurement(
  208. self.t_shirt, 10, self.uom_unit, self.warehouse_1.lot_stock_id,
  209. self.t_shirt.name, '/', self.env.company, order_3_values)
  210. ])
  211. self.assertEqual(len(purchase_order.order_line), 2, 'line with different custom value should not be merged')
  212. 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')
  213. 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')
  214. purchase_order.button_confirm()
  215. self.assertEqual(purchase_order.picking_ids[0].move_ids_without_package.filtered(lambda x: x.product_uom_qty == 15).description_picking, t_shirt.display_name + "\n" + order_line_description + "Color (Red)", 'wrong description in picking')
  216. self.assertEqual(purchase_order.picking_ids[0].move_ids_without_package.filtered(lambda x: x.product_uom_qty == 10).description_picking, t_shirt.display_name + "\n" + order_line_description + "Color (Green)", 'wrong description in picking')
  217. def test_reordering_days_to_purchase(self):
  218. company = self.env.ref('base.main_company')
  219. company2 = self.env['res.company'].create({
  220. 'name': 'Second Company',
  221. })
  222. company.write({'po_lead': 0.00})
  223. self.patcher = patch('odoo.addons.stock.models.stock_orderpoint.fields.Date', wraps=fields.Date)
  224. self.mock_date = self.startPatcher(self.patcher)
  225. vendor = self.env['res.partner'].create({
  226. 'name': 'Colruyt'
  227. })
  228. vendor2 = self.env['res.partner'].create({
  229. 'name': 'Delhaize'
  230. })
  231. self.env.company.days_to_purchase = 2.0
  232. # Test if the orderpoint is created when opening the replenishment view
  233. prod = self.env['product.product'].create({
  234. 'name': 'Carrot',
  235. 'type': 'product',
  236. 'seller_ids': [
  237. (0, 0, {'partner_id': vendor.id, 'delay': 1.0, 'company_id': company.id})
  238. ]
  239. })
  240. warehouse = self.env['stock.warehouse'].search([], limit=1)
  241. self.env['stock.move'].create({
  242. 'name': 'Delivery',
  243. 'date': datetime.today() + timedelta(days=3),
  244. 'product_id': prod.id,
  245. 'product_uom': prod.uom_id.id,
  246. 'product_uom_qty': 5.0,
  247. 'location_id': warehouse.lot_stock_id.id,
  248. 'location_dest_id': self.ref('stock.stock_location_customers'),
  249. })._action_confirm()
  250. self.env['stock.warehouse.orderpoint'].action_open_orderpoints()
  251. replenishment = self.env['stock.warehouse.orderpoint'].search([
  252. ('product_id', '=', prod.id),
  253. ])
  254. self.assertEqual(len(replenishment), 1)
  255. # Test if purchase orders are created according to the days to purchase
  256. product = self.env['product.product'].create({
  257. 'name': 'Chicory',
  258. 'type': 'product',
  259. 'seller_ids': [
  260. (0, 0, {'partner_id': vendor2.id, 'delay': 15.0, 'company_id': company2.id}),
  261. (0, 0, {'partner_id': vendor.id, 'delay': 1.0, 'company_id': company.id})
  262. ]
  263. })
  264. orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
  265. orderpoint_form.product_id = product
  266. orderpoint_form.product_min_qty = 0.0
  267. orderpoint_form.visibility_days = 1.0
  268. orderpoint = orderpoint_form.save()
  269. orderpoint_form = Form(self.env['stock.warehouse.orderpoint'].with_company(company2))
  270. orderpoint_form.product_id = product
  271. orderpoint_form.product_min_qty = 0.0
  272. orderpoint = orderpoint_form.save()
  273. delivery_moves = self.env['stock.move']
  274. for i in range(0, 6):
  275. delivery_moves |= self.env['stock.move'].create({
  276. 'name': 'Delivery',
  277. 'date': datetime.today() + timedelta(days=i),
  278. 'product_id': product.id,
  279. 'product_uom': product.uom_id.id,
  280. 'product_uom_qty': 5.0,
  281. 'location_id': warehouse.lot_stock_id.id,
  282. 'location_dest_id': self.ref('stock.stock_location_customers'),
  283. })
  284. delivery_moves._action_confirm()
  285. self.env['procurement.group'].run_scheduler()
  286. po_line = self.env['purchase.order.line'].search([('product_id', '=', product.id)])
  287. expected_date_order = fields.Date.today() + timedelta(days=2)
  288. self.assertEqual(fields.Date.to_date(po_line.order_id.date_order), expected_date_order)
  289. self.assertEqual(len(po_line), 1)
  290. self.assertEqual(po_line.product_uom_qty, 25.0)
  291. self.assertEqual(len(po_line.order_id), 1)
  292. orderpoint_form = Form(orderpoint)
  293. orderpoint_form.save()
  294. self.mock_date.today.return_value = fields.Date.today() + timedelta(days=2)
  295. orderpoint._compute_qty()
  296. self.env['procurement.group'].run_scheduler()
  297. po_line02 = self.env['purchase.order.line'].search([('product_id', '=', product.id)])
  298. self.assertEqual(po_line02, po_line, 'The orderpoint execution should not create a new POL')
  299. self.assertEqual(fields.Date.to_date(po_line.order_id.date_order), expected_date_order, 'The Order Deadline should not change')
  300. self.assertEqual(po_line.product_uom_qty, 30.0, 'The existing POL should be updated with the quantity of the last execution')
  301. def test_supplier_lead_time(self):
  302. """ Basic stock configuration and a supplier with a minimum qty and a lead time """
  303. self.env['stock.warehouse.orderpoint'].search([]).unlink()
  304. orderpoint_form = Form(self.env['stock.warehouse.orderpoint'])
  305. orderpoint_form.product_id = self.product_1
  306. orderpoint_form.product_min_qty = 10
  307. orderpoint_form.product_max_qty = 50
  308. orderpoint_form.save()
  309. self.env['product.supplierinfo'].search([('product_tmpl_id', '=', self.product_1.product_tmpl_id.id)]).unlink()
  310. self.env['product.supplierinfo'].create({
  311. 'partner_id': self.partner_1.id,
  312. 'min_qty': 1,
  313. 'price': 1,
  314. 'delay': 7,
  315. 'product_tmpl_id': self.product_1.product_tmpl_id.id,
  316. })
  317. self.env['procurement.group'].run_scheduler()
  318. purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.partner_1.id)])
  319. today = fields.Datetime.start_of(fields.Datetime.now(), 'day')
  320. self.assertEqual(purchase_order.date_order, today)
  321. self.assertEqual(fields.Datetime.start_of(purchase_order.date_planned, 'day'), today + timedelta(days=7))