Debian Codegraph @

addons/sale_mrp/tests/test_sale_mrp_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 timedelta
5
6 from odoo import fields
7 from odoo.addons.stock.tests.common import TestStockCommon
8
9 from odoo.tests import Form
10
11
12 class TestSaleMrpLeadTime(TestStockCommon):
13
14 @classmethod
15 def setUpClass(cls):
16 super().setUpClass()
17 cls.route_mto.active = True
18 # Update the product_1 with type, route, Manufacturing Lead Time and Customer Lead Time
19 with Form(cls.product_1) as p1:
20 # `type` is invisible in the view,
21 # and it's a compute field based on `type` which is the field visible in the view
22 p1.is_storable = True
23 p1.sale_delay = 5.0
24 p1.route_ids.clear()
25 p1.route_ids.add(cls.warehouse_1.manufacture_pull_id.route_id)
26 p1.route_ids.add(cls.warehouse_1.mto_pull_id.route_id)
27
28 # Update the product_2 with type
29 with Form(cls.product_2) as p2:
30 # `type` is invisible in the view,
31 # and it's a compute field based on `type` which is the field visible in the view
32 p2.type = 'consu'
33
34 # Create Bill of materials for product_1
35 with Form(cls.env['mrp.bom']) as bom:
36 bom.product_tmpl_id = cls.product_1.product_tmpl_id
37 bom.product_qty = 2
38 bom.produce_delay = 5.0
39 with bom.bom_line_ids.new() as line:
40 line.product_id = cls.product_2
41 line.product_qty = 4
42
43 cls.warehouse_3_steps_pull = cls.env['stock.warehouse'].create({
44 'name': 'Warehouse 3 steps',
45 'code': '3S',
46 'delivery_steps': 'pick_pack_ship',
47 })
48 delivery_route_3 = cls.warehouse_3_steps_pull.delivery_route_id
49 delivery_route_3.rule_ids[0].write({
50 'location_dest_id': delivery_route_3.rule_ids[1].location_src_id.id,
51 })
52 delivery_route_3.rule_ids[1].write({'action': 'pull'})
53 delivery_route_3.rule_ids[2].write({'action': 'pull'})
54 cls.warehouse_3_steps_pull.mto_pull_id.write({
55 'location_dest_id': delivery_route_3.rule_ids[1].location_src_id.id,
56 })
57
58 def test_00_product_company_level_delays(self):
59 """ In order to check schedule date, set product's Manufacturing Lead Time
60 and Customer Lead Time and also set company's Manufacturing Lead Time
61 and Sales Safety Days."""
62
63 company = self.env.ref('base.main_company')
64
65 # Update company with Manufacturing Lead Time and Sales Safety Days
66 company.security_lead = 3
67
68 # Create sale order of product_1
69 order_form = Form(self.env['sale.order'])
70 order_form.partner_id = self.partner_1
71 with order_form.order_line.new() as line:
72 line.product_id = self.product_1
73 line.product_uom_qty = 10
74 order = order_form.save()
75 # Confirm sale order
76 order.action_confirm()
77
78 # Check manufacturing order created or not
79 manufacturing_order = self.env['mrp.production'].search([('product_id', '=', self.product_1.id), ('move_dest_ids', 'in', order.picking_ids[0].move_ids.ids)])
80 self.assertTrue(manufacturing_order, 'Manufacturing order should be created.')
81
82 # Check schedule date of picking
83 deadline_picking = fields.Datetime.from_string(order.date_order) + timedelta(days=self.product_1.sale_delay)
84 out_date = deadline_picking - timedelta(days=company.security_lead)
85 self.assertAlmostEqual(
86 order.picking_ids[0].scheduled_date, out_date,
87 delta=timedelta(seconds=1),
88 msg='Schedule date of picking should be equal to: Order date + Customer Lead Time - Sales Safety Days.'
89 )
90 self.assertAlmostEqual(
91 order.picking_ids[0].date_deadline, deadline_picking,
92 delta=timedelta(seconds=1),
93 msg='Deadline date of picking should be equal to: Order date + Customer Lead Time.'
94 )
95
96 # Check schedule date and deadline of manufacturing order
97 mo_date_start = out_date - timedelta(days=manufacturing_order.bom_id.produce_delay)
98 self.assertAlmostEqual(
99 fields.Datetime.from_string(manufacturing_order.date_start), mo_date_start,
100 delta=timedelta(seconds=1),
101 msg="Schedule date of manufacturing order should be equal to: Schedule date of picking - product's Manufacturing Lead Time - company's Manufacturing Lead Time."
102 )
103 self.assertAlmostEqual(
104 fields.Datetime.from_string(manufacturing_order.date_deadline), deadline_picking,
105 delta=timedelta(seconds=1),
106 msg="Deadline date of manufacturing order should be equal to the deadline of sale picking"
107 )
108
109 def test_01_product_route_mrp_delays(self):
110 """ In order to check schedule dates, set product's Manufacturing Lead Time
111 and Customer Lead Time and also set warehouse route's delay."""
112
113 warehouse = self.warehouse_3_steps_pull
114
115 # Set delay on pull rule
116 for pull_rule in warehouse.delivery_route_id.rule_ids:
117 pull_rule.write({'delay': 2})
118
119 # Create sale order of product_1
120 order_form = Form(self.env['sale.order'])
121 order_form.partner_id = self.partner_1
122 order_form.warehouse_id = warehouse
123 with order_form.order_line.new() as line:
124 line.product_id = self.product_1
125 line.product_uom_qty = 6
126 order = order_form.save()
127 # Confirm sale order
128 order.action_confirm()
129
130 # Run scheduler
131 self.env['stock.rule'].run_scheduler()
132
133 # Check manufacturing order created or not
134 manufacturing_order = self.env['mrp.production'].search([('product_id', '=', self.product_1.id)])
135 self.assertTrue(manufacturing_order, 'Manufacturing order should be created.')
136
137 # Check the picking crated or not
138 self.assertTrue(order.picking_ids, "Pickings should be created.")
139
140 # Check schedule date of ship type picking
141 out = order.picking_ids.filtered(lambda r: r.picking_type_id == warehouse.out_type_id)
142 out_min_date = fields.Datetime.from_string(out.scheduled_date)
143 out_date = fields.Datetime.from_string(order.date_order) + timedelta(days=self.product_1.sale_delay) - timedelta(days=out.move_ids[0].rule_id.delay)
144 self.assertAlmostEqual(
145 out_min_date, out_date,
146 delta=timedelta(seconds=10),
147 msg='Schedule date of ship type picking should be equal to: order date + Customer Lead Time - pull rule delay.'
148 )
149
150 # Check schedule date of pack type picking
151 pack = order.picking_ids.filtered(lambda r: r.picking_type_id == warehouse.pack_type_id)
152 pack_min_date = fields.Datetime.from_string(pack.scheduled_date)
153 pack_date = out_date - timedelta(days=pack.move_ids[0].rule_id.delay)
154 self.assertAlmostEqual(
155 pack_min_date, pack_date,
156 delta=timedelta(seconds=10),
157 msg='Schedule date of pack type picking should be equal to: Schedule date of ship type picking - pull rule delay.'
158 )
159
160 # Check schedule date of pick type picking
161 pick = order.picking_ids.filtered(lambda r: r.picking_type_id == warehouse.pick_type_id)
162 pick_min_date = fields.Datetime.from_string(pick.scheduled_date)
163 self.assertAlmostEqual(
164 pick_min_date, pack_date,
165 delta=timedelta(seconds=10),
166 msg='Schedule date of pick type picking should be equal to: Schedule date of pack type picking.'
167 )
168
169 # Check schedule date and deadline date of manufacturing order
170 mo_date_start = out_date - timedelta(days=manufacturing_order.bom_id.produce_delay) - timedelta(days=warehouse.delivery_route_id.rule_ids[0].delay)
171 self.assertAlmostEqual(
172 fields.Datetime.from_string(manufacturing_order.date_start), mo_date_start,
173 delta=timedelta(seconds=1),
174 msg="Schedule date of manufacturing order should be equal to: Schedule date of picking - product's Manufacturing Lead Time- delay pull_rule."
175 )
176 self.assertAlmostEqual(
177 manufacturing_order.date_deadline, order.picking_ids[0].date_deadline,
178 delta=timedelta(seconds=1),
179 msg="Deadline date of manufacturing order should be equal to the deadline of sale picking"
180 )