1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import common
from odoo import Command
from odoo.exceptions import UserError
from odoo.tests import Form
class TestWarehouseMrp(common.TestMrpCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
unit = cls.env.ref("uom.product_uom_unit")
cls.stock_location = cls.env.ref('stock.stock_location_stock')
cls.depot_location = cls.env['stock.location'].create({
'name': 'Depot',
'usage': 'internal',
'location_id': cls.stock_location.id,
})
cls.env["stock.putaway.rule"].create({
"location_in_id": cls.stock_location.id,
"location_out_id": cls.depot_location.id,
'category_id': cls.env.ref('product.product_category_all').id,
})
cls.env['mrp.workcenter'].create({
'name': 'Assembly Line 1',
'resource_calendar_id': cls.env.ref('resource.resource_calendar_std').id,
})
cls.env['stock.quant'].create({
'location_id': cls.stock_location_14.id,
'product_id': cls.graphics_card.id,
'inventory_quantity': 16.0
}).action_apply_inventory()
cls.bom_laptop = cls.env['mrp.bom'].create({
'product_tmpl_id': cls.laptop.product_tmpl_id.id,
'product_qty': 1,
'product_uom_id': unit.id,
'consumption': 'flexible',
'bom_line_ids': [(0, 0, {
'product_id': cls.graphics_card.id,
'product_qty': 1,
'product_uom_id': unit.id
})],
'operation_ids': [
(0, 0, {'name': 'Cutting Machine', 'workcenter_id': cls.workcenter_1.id, 'time_cycle': 12, 'sequence': 1}),
],
})
def new_mo_laptop(self):
form = Form(self.env['mrp.production'])
form.product_id = self.laptop
form.product_qty = 1
form.bom_id = self.bom_laptop
p = form.save()
p.action_confirm()
p.action_assign()
return p
def test_manufacturing_route(self):
warehouse_1_stock_manager = self.warehouse_1.with_user(self.user_stock_manager)
manu_rule = self.env['stock.rule'].search([
('action', '=', 'manufacture'),
('warehouse_id', '=', self.warehouse_1.id)])
self.assertEqual(self.warehouse_1.manufacture_pull_id, manu_rule)
manu_route = manu_rule.route_id
self.assertIn(manu_route, warehouse_1_stock_manager._get_all_routes())
warehouse_1_stock_manager.write({
'manufacture_to_resupply': False
})
self.assertFalse(self.warehouse_1.manufacture_pull_id.active)
self.assertFalse(self.warehouse_1.manu_type_id.active)
self.assertNotIn(manu_route, warehouse_1_stock_manager._get_all_routes())
warehouse_1_stock_manager.write({
'manufacture_to_resupply': True
})
manu_rule = self.env['stock.rule'].search([
('action', '=', 'manufacture'),
('warehouse_id', '=', self.warehouse_1.id)])
self.assertEqual(self.warehouse_1.manufacture_pull_id, manu_rule)
self.assertTrue(self.warehouse_1.manu_type_id.active)
self.assertIn(manu_route, warehouse_1_stock_manager._get_all_routes())
def test_multi_warehouse_resupply(self):
""" test a multi warehouse flow give a correct date delay
product_6 is sold from warehouse_1, its component (product_4) is
resupplied from warehouse_2 and manufactured in warehouse_2.
Everything in mto """
mto = self.env.ref('stock.route_warehouse0_mto')
mto.active = True
warehouse_2 = self.env['stock.warehouse'].create({
'name': 'Warehouse 2',
'code': 'WH2',
})
# product 4 can only be manufacture in WH2
self.bom_1.picking_type_id = warehouse_2.manu_type_id
self.warehouse_1.manufacture_steps = "pbm"
self.warehouse_1.resupply_wh_ids = [(6, 0, [warehouse_2.id])]
self.product_6.route_ids = [(6, 0, [
self.env.ref('mrp.route_warehouse0_manufacture').id,
mto.id,
])]
self.product_4.route_ids = [(6, 0, [
self.warehouse_1.resupply_route_ids.id,
self.env.ref('stock.route_warehouse0_mto').id,
mto.id,
])]
warehouse_2.resupply_route_ids.rule_ids.procure_method = 'make_to_order'
customer_location = self.env.ref('stock.stock_location_customers')
pg = self.env['procurement.group'].create({'name': 'Test-pg-mtso-mto'})
self.env['procurement.group'].run([
pg.Procurement(
self.product_6,
5.0,
self.product_6.uom_id,
customer_location,
'test_ressuply',
'test_ressuply',
self.warehouse_1.company_id,
{
'warehouse_id': self.warehouse_1,
'group_id': pg,
},
),
])
def test_manufacturing_scrap(self):
"""
Testing to do a scrap of consumed material.
"""
# Update demo products
(self.product_4 | self.product_2).write({
'tracking': 'lot',
})
# Update Bill Of Material to remove product with phantom bom.
self.bom_3.bom_line_ids.filtered(lambda x: x.product_id == self.product_5).unlink()
# Create Inventory Adjustment For Stick and Stone Tools with lot.
lot_product_4 = self.env['stock.lot'].create({
'name': '0000000000001',
'product_id': self.product_4.id,
})
lot_product_2 = self.env['stock.lot'].create({
'name': '0000000000002',
'product_id': self.product_2.id,
})
# Inventory for Stick
self.env['stock.quant'].create({
'location_id': self.stock_location_14.id,
'product_id': self.product_4.id,
'inventory_quantity': 8,
'lot_id': lot_product_4.id
}).action_apply_inventory()
# Inventory for Stone Tools
self.env['stock.quant'].create({
'location_id': self.stock_location_14.id,
'product_id': self.product_2.id,
'inventory_quantity': 12,
'lot_id': lot_product_2.id
}).action_apply_inventory()
#Create Manufacturing order.
production_form = Form(self.env['mrp.production'])
production_form.product_id = self.product_6
production_form.bom_id = self.bom_3
production_form.product_qty = 12
production_form.product_uom_id = self.product_6.uom_id
production_3 = production_form.save()
production_3.action_confirm()
production_3.action_assign()
# Check Manufacturing order's availability.
self.assertEqual(production_3.reservation_state, 'assigned', "Production order's availability should be Available.")
location_id = production_3.move_raw_ids.filtered(lambda x: x.state not in ('done', 'cancel')) and production_3.location_src_id.id or production_3.location_dest_id.id,
# Scrap Product Wood without lot to check assert raise ?.
scrap_id = self.env['stock.scrap'].with_context(active_model='mrp.production', active_id=production_3.id).create({'product_id': self.product_2.id, 'scrap_qty': 1.0, 'product_uom_id': self.product_2.uom_id.id, 'location_id': location_id, 'production_id': production_3.id})
with self.assertRaises(UserError):
scrap_id.do_scrap()
# Scrap Product Wood with lot.
scrap_id = self.env['stock.scrap'].with_context(active_model='mrp.production', active_id=production_3.id).create({'product_id': self.product_2.id, 'scrap_qty': 1.0, 'product_uom_id': self.product_2.uom_id.id, 'location_id': location_id, 'lot_id': lot_product_2.id, 'production_id': production_3.id})
scrap_id.do_scrap()
scrap_move = scrap_id.move_ids[0]
self.assertTrue(scrap_move.raw_material_production_id)
self.assertTrue(scrap_move.scrapped)
self.assertEqual(scrap_move.location_dest_id, scrap_id.scrap_location_id)
self.assertEqual(scrap_move.price_unit, scrap_move.product_id.standard_price)
#Check scrap move is created for this production order.
#TODO: should check with scrap objects link in between
# scrap_move = production_3.move_raw_ids.filtered(lambda x: x.product_id == self.product_2 and x.scrapped)
# self.assertTrue(scrap_move, "There are no any scrap move created for production order.")
def test_putaway_after_manufacturing_3(self):
""" This test checks a tracked manufactured product will go to location
defined in putaway strategy when the production is recorded with
product.produce wizard.
"""
self.laptop.tracking = 'serial'
mo_laptop = self.new_mo_laptop()
serial = self.env['stock.lot'].create({'product_id': self.laptop.id})
mo_form = Form(mo_laptop)
mo_form.qty_producing = 1
mo_form.lot_producing_id = serial
mo_laptop = mo_form.save()
mo_laptop.button_mark_done()
# We check if the laptop go in the depot and not in the stock
move = mo_laptop.move_finished_ids
location_dest = move.move_line_ids.location_dest_id
self.assertEqual(location_dest.id, self.depot_location.id)
self.assertNotEqual(location_dest.id, self.stock_location.id)
def test_backorder_unpacking(self):
""" Test that movement of pack in backorder is correctly handled. """
warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
warehouse.write({'manufacture_steps': 'pbm'})
self.product_1.is_storable = True
self.env['stock.quant']._update_available_quantity(self.product_1, self.stock_location, 100)
mo_form = Form(self.env['mrp.production'])
mo_form.bom_id = self.bom_4
mo_form.product_qty = 100
mo = mo_form.save()
mo.action_confirm()
package = self.env['stock.quant.package'].create({})
picking = mo.picking_ids
picking.move_line_ids.write({
'quantity': 20,
'result_package_id': package.id,
})
Form.from_action(self.env, picking.button_validate()).save().process()
backorder = picking.backorder_ids
backorder.move_line_ids.quantity = 80
backorder.button_validate()
self.assertEqual(picking.state, 'done')
self.assertEqual(backorder.state, 'done')
self.assertEqual(mo.move_raw_ids.move_line_ids.mapped('quantity_product_uom'), [20, 80])
class TestKitPicking(common.TestMrpCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
def create_product(name):
p = Form(cls.env['product.product'])
p.name = name
p.is_storable = True
return p.save()
# Create a kit 'kit_parent' :
# ---------------------------
#
# kit_parent --|- kit_2 x2 --|- component_d x1
# | |- kit_1 x2 -------|- component_a x2
# | |- component_b x1
# | |- component_c x3
# |
# |- kit_3 x1 --|- component_f x1
# | |- component_g x2
# |
# |- component_e x1
# Creating all components
component_a = create_product('Comp A')
component_b = create_product('Comp B')
component_c = create_product('Comp C')
component_d = create_product('Comp D')
component_e = create_product('Comp E')
component_f = create_product('Comp F')
component_g = create_product('Comp G')
# Creating all kits
kit_1 = create_product('Kit 1')
kit_2 = create_product('Kit 2')
kit_3 = create_product('kit 3')
cls.kit_parent = create_product('Kit Parent')
# Linking the kits and the components via some 'phantom' BoMs
bom_kit_1 = cls.env['mrp.bom'].create({
'product_tmpl_id': kit_1.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})
BomLine = cls.env['mrp.bom.line']
BomLine.create({
'product_id': component_a.id,
'product_qty': 2.0,
'bom_id': bom_kit_1.id})
BomLine.create({
'product_id': component_b.id,
'product_qty': 1.0,
'bom_id': bom_kit_1.id})
BomLine.create({
'product_id': component_c.id,
'product_qty': 3.0,
'bom_id': bom_kit_1.id})
bom_kit_2 = cls.env['mrp.bom'].create({
'product_tmpl_id': kit_2.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})
BomLine.create({
'product_id': component_d.id,
'product_qty': 1.0,
'bom_id': bom_kit_2.id})
BomLine.create({
'product_id': kit_1.id,
'product_qty': 2.0,
'bom_id': bom_kit_2.id})
bom_kit_parent = cls.env['mrp.bom'].create({
'product_tmpl_id': cls.kit_parent.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})
BomLine.create({
'product_id': component_e.id,
'product_qty': 1.0,
'bom_id': bom_kit_parent.id})
BomLine.create({
'product_id': kit_2.id,
'product_qty': 2.0,
'bom_id': bom_kit_parent.id})
bom_kit_3 = cls.env['mrp.bom'].create({
'product_tmpl_id': kit_3.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})
BomLine.create({
'product_id': component_f.id,
'product_qty': 1.0,
'bom_id': bom_kit_3.id})
BomLine.create({
'product_id': component_g.id,
'product_qty': 2.0,
'bom_id': bom_kit_3.id})
BomLine.create({
'product_id': kit_3.id,
'product_qty': 1.0,
'bom_id': bom_kit_parent.id})
# We create an 'immediate transfer' receipt for x3 kit_parent
cls.test_partner = cls.env['res.partner'].create({
'name': 'Notthat Guyagain',
})
cls.test_supplier = cls.env['stock.location'].create({
'name': 'supplier',
'usage': 'supplier',
'location_id': cls.env.ref('stock.stock_location_stock').id,
})
cls.expected_quantities = {
component_a: 24,
component_b: 12,
component_c: 36,
component_d: 6,
component_e: 3,
component_f: 3,
component_g: 6
}
def test_kit_immediate_transfer(self):
""" Make sure a kit is split in the corrects quantity by components in case of an
immediate transfer.
"""
picking = self.env['stock.picking'].create({
'location_id': self.test_supplier.id,
'location_dest_id': self.warehouse_1.wh_input_stock_loc_id.id,
'partner_id': self.test_partner.id,
'picking_type_id': self.env.ref('stock.picking_type_in').id,
})
self.env['stock.move'].create({
'name': self.kit_parent.name,
'product_id': self.kit_parent.id,
'quantity': 3,
'picked': True,
'product_uom': self.kit_parent.uom_id.id,
'picking_id': picking.id,
'picking_type_id': self.env.ref('stock.picking_type_in').id,
'location_id': self.test_supplier.id,
'location_dest_id': self.warehouse_1.wh_input_stock_loc_id.id,
})
picking.button_validate()
# We check that the picking has the correct quantities after its move were splitted.
self.assertEqual(len(picking.move_ids), 7)
for move in picking.move_ids:
self.assertEqual(move.quantity, self.expected_quantities[move.product_id])
self.assertEqual(move.state, 'done')
def test_kit_planned_transfer(self):
""" Make sure a kit is split in the corrects product_qty by components in case of a
planned transfer.
"""
picking = self.env['stock.picking'].create({
'location_id': self.test_supplier.id,
'location_dest_id': self.warehouse_1.wh_input_stock_loc_id.id,
'partner_id': self.test_partner.id,
'picking_type_id': self.env.ref('stock.picking_type_in').id,
})
move_receipt_1 = self.env['stock.move'].create({
'name': self.kit_parent.name,
'product_id': self.kit_parent.id,
'product_uom_qty': 3,
'product_uom': self.kit_parent.uom_id.id,
'picking_id': picking.id,
'picking_type_id': self.env.ref('stock.picking_type_in').id,
'location_id': self.test_supplier.id,
'location_dest_id': self.warehouse_1.wh_input_stock_loc_id.id,
})
picking.action_confirm()
# We check that the picking has the correct quantities after its move were splitted.
self.assertEqual(len(picking.move_ids), 7)
for move_line in picking.move_ids:
self.assertEqual(move_line.product_qty, self.expected_quantities[move_line.product_id])
def test_add_sml_with_kit_to_confirmed_picking(self):
warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
customer_location = self.env.ref('stock.stock_location_customers')
stock_location = warehouse.lot_stock_id
in_type = warehouse.in_type_id
self.bom_4.type = 'phantom'
kit = self.bom_4.product_id
compo = self.bom_4.bom_line_ids.product_id
product = self.env['product.product'].create({'name': 'Super Product', 'is_storable': True})
receipt = self.env['stock.picking'].create({
'picking_type_id': in_type.id,
'location_id': customer_location.id,
'location_dest_id': stock_location.id,
'move_ids': [(0, 0, {
'name': product.name,
'product_id': product.id,
'product_uom_qty': 1,
'product_uom': product.uom_id.id,
'location_id': customer_location.id,
'location_dest_id': stock_location.id,
})]
})
receipt.action_confirm()
receipt.move_line_ids.quantity = 1
receipt.move_line_ids = [(0, 0, {
'product_id': kit.id,
'quantity': 1,
'product_uom_id': kit.uom_id.id,
'location_id': customer_location.id,
'location_dest_id': stock_location.id,
})]
receipt.move_ids.picked = True
receipt.button_validate()
self.assertEqual(receipt.state, 'done')
self.assertRecordValues(receipt.move_ids, [
{'product_id': product.id, 'quantity': 1, 'state': 'done'},
{'product_id': compo.id, 'quantity': 1, 'state': 'done'},
])
def test_move_line_aggregated_product_quantities_with_kit(self):
""" Test the `stock.move.line` method `_get_aggregated_product_quantities`,
who returns data used to print delivery slips, using kits.
"""
uom_unit = self.env.ref('uom.product_uom_unit')
kit, kit_component_1, kit_component_2, not_kit_1, not_kit_2 = self.env['product.product'].create([{
'name': name,
'is_storable': True,
'uom_id': uom_unit.id,
} for name in ['Kit', 'Kit Component 1', 'Kit Component 2', 'Not Kit 1', 'Not Kit 2']])
bom_kit = self.env['mrp.bom'].create({
'product_tmpl_id': kit.product_tmpl_id.id,
'product_uom_id': kit.product_tmpl_id.uom_id.id,
'product_id': kit.id,
'product_qty': 1.0,
'type': 'phantom',
'bom_line_ids': [
Command.create({
'product_id': kit_component_1.id,
'product_qty': 1,
}),
Command.create({
'product_id': kit_component_2.id,
'product_qty': 1,
}),
]
})
delivery_form = Form(self.env['stock.picking'])
delivery_form.picking_type_id = self.env.ref('stock.picking_type_in')
with delivery_form.move_ids_without_package.new() as move:
move.product_id = bom_kit.product_id
move.product_uom_qty = 4
with delivery_form.move_ids_without_package.new() as move:
move.product_id = not_kit_1
move.product_uom_qty = 4
with delivery_form.move_ids_without_package.new() as move:
move.product_id = not_kit_2
move.product_uom_qty = 3
delivery = delivery_form.save()
delivery.action_confirm()
delivery.move_line_ids.filtered(lambda ml: ml.product_id == kit_component_1).quantity = 3
delivery.move_line_ids.filtered(lambda ml: ml.product_id == kit_component_2).quantity = 3
delivery.move_line_ids.filtered(lambda ml: ml.product_id == not_kit_1).quantity = 4
delivery.move_line_ids.filtered(lambda ml: ml.product_id == not_kit_2).quantity = 2
backorder_wizard_dict = delivery.button_validate()
backorder_wizard_form = Form.from_action(self.env, backorder_wizard_dict)
backorder_wizard_form.save().process_cancel_backorder()
aggregate_not_kit_values = delivery.move_line_ids._get_aggregated_product_quantities()
self.assertEqual(len(aggregate_not_kit_values.keys()), 2)
self.assertTrue(all('Not' in val for val in aggregate_not_kit_values), 'Only non kit products should be included')
aggregate_kit_values = delivery.move_line_ids._get_aggregated_product_quantities(kit_name=bom_kit.product_id.name)
self.assertEqual(len(aggregate_kit_values.keys()), 2)
self.assertTrue(all('Component' in val for val in aggregate_kit_values), 'Only kit products should be included')
def test_scrap_consu_kit_not_available(self):
"""
Scrap a consumable kit with one product not available in stock
"""
self._test_scrap_kit_not_available(False)
def test_scrap_storable_kit_not_available(self):
"""
Scrap a storable kit with one product not available in stock
"""
self._test_scrap_kit_not_available(True)
def _test_scrap_kit_not_available(self, storable):
bom = self.bom_4
bom.type = 'phantom'
kit = bom.product_id
component = bom.bom_line_ids.product_id
kit.is_storable = storable
component.is_storable = True
scrap = self.env['stock.scrap'].create({
'product_id': kit.id,
'product_uom_id': kit.uom_id.id,
'scrap_qty': 1,
'bom_id': bom.id,
})
Form.from_action(self.env, scrap.action_validate()).save().action_done()
self.assertEqual(scrap.state, 'done')
self.assertRecordValues(scrap.move_ids, [
{'product_id': component.id, 'quantity': 1, 'state': 'done'}
])
|