| 1 |
|
| 2 |
|
| 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 |
|
| 19 |
with Form(cls.product_1) as p1: |
| 20 |
|
| 21 |
|
| 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 |
|
| 29 |
with Form(cls.product_2) as p2: |
| 30 |
|
| 31 |
|
| 32 |
p2.type = 'consu' |
| 33 |
|
| 34 |
|
| 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 |
|
| 66 |
company.security_lead = 3 |
| 67 |
|
| 68 |
|
| 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 |
|
| 76 |
order.action_confirm() |
| 77 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 116 |
for pull_rule in warehouse.delivery_route_id.rule_ids: |
| 117 |
pull_rule.write({'delay': 2}) |
| 118 |
|
| 119 |
|
| 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 |
|
| 128 |
order.action_confirm() |
| 129 |
|
| 130 |
|
| 131 |
self.env['stock.rule'].run_scheduler() |
| 132 |
|
| 133 |
|
| 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 |
|
| 138 |
self.assertTrue(order.picking_ids, "Pickings should be created.") |
| 139 |
|
| 140 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
) |