Skip to Content
Menu
This question has been flagged
1 Reply
6075 Views

I'm trying to create a manufacturing order using API. I can create a new order, but it doesn't have any components. The bom I'm using has components defined, by they are not populated when this manufacturing order is created.

Here is how I'm creating it: 

models.execute_kw(db, uid, password, 
'mrp.production', 'create', [{
  product_id: 69,
  bom_id: 9,
  product_uom_id: 1,
}]
)
Thanks!
Avatar
Discard

Hey there, did you work this out? I am facing the same issue. I can create the MO but cannot proceed as it has no components, I believe the stock.move has something to do with it. as this line is in mrp_production.py

move_raw_ids = fields.One2many(

'stock.move', 'raw_material_production_id', 'Components',

copy=True, states={'done': [('readonly', True)], 'cancel': [('readonly', True)]},

domain=[('scrapped', '=', False)])

Author

I ended up using python to create manufacturing order.

Here is what I do:

```

bom_description = self.get_bom_description(product_id)

manufacturing_order = self.env['mrp.production'].create([{

'product_id': product_id,

'product_qty': 1,

'product_uom_qty': 1,

'qty_produced': 1,

'product_uom_id': bom_description['product_uom_id'][0],

'bom_id': bom_description['bom_id'][0],

'origin': origin,

'move_finished_ids': [

[0, '', {

'product_id': move_finished_id[2]['product_id'][0],

'product_uom': move_finished_id[2]['product_uom'][0],

'product_uom_qty': move_finished_id[2]['product_uom_qty'],

'location_id': move_finished_id[2]['location_id'][0],

'location_dest_id': destination_location.id,

'name': move_finished_id[2]['name'],

'byproduct_id': False,

}]

for move_finished_id in bom_description['move_finished_ids'][1:]

],

'move_raw_ids': [

[0, '', {

'product_id': move_raw_id[2]['product_id'][0],

'bom_line_id': move_raw_id[2]['bom_line_id'][0],

'product_uom': move_raw_id[2]['product_uom'][0],

'product_uom_qty': move_raw_id[2]['product_uom_qty'],

'location_id': part_source_location.id,

'location_dest_id': move_raw_id[2]['location_dest_id'][0],

'name': move_raw_id[2]['name'],

}]

for move_raw_id in bom_description['move_raw_ids'][1:]

]

}])

manufacturing_order.action_confirm()

manufacturing_order.update({'lot_producing_id': lot.id})

immediate_production = self.env['mrp.immediate.production'].create({

'immediate_production_line_ids': [[0, '', {

'production_id': manufacturing_order.id,

'to_immediate': True

}]]

})

immediate_production.process()

manufacturing_order.button_mark_done()

def get_bom_description(self, product_id):

onchange_res = self.env['mrp.production'].onchange(

{

'product_id': product_id,

},

'product_id',

{

'company_id': '1',

'product_id': '1',

'product_qty': '1',

'product_uom_id': '1',

'bom_id': '1',

'move_finished_ids': '1',

'move_finished_ids.product_id': '1',

'move_finished_ids.product_uom': '1',

'move_finished_ids.product_uom_qty': '1',

'move_finished_ids.location_id': '1',

'move_finished_ids.location_dest_id': '1',

'move_finished_ids.name': '',

'move_raw_ids': '1',

'move_raw_ids.product_id': '1',

'move_raw_ids.name': '',

'move_raw_ids.bom_line_id': '',

'move_raw_ids.location_id': '1',

'move_raw_ids.location_dest_id': '1',

'move_raw_ids.product_uom_qty': '1',

'move_raw_ids.product_uom': '1',

}

)

return onchange_res['value']

```

Same thing can be done using API. The important part for me was that I needed to call [mrp.production].onchange to get information about all components from the bom and then pass them in as move_finished_ids and move_raw_ids.

Author Best Answer

I ended up using python to create manufacturing order.

Here is what I do:

    def assemble_product(self, product_id, part_source_location, destination_location, origin=None):
bom_description = self.get_bom_description(product_id)
manufacturing_order = self.env['mrp.production'].create([{
'product_id': product_id,
'product_qty': 1,
'product_uom_qty': 1,
'qty_produced': 1,
'product_uom_id': bom_description['product_uom_id'][0],
'bom_id': bom_description['bom_id'][0],
'origin': origin,
'move_finished_ids': [
[0, '', {
'product_id': move_finished_id[2]['product_id'][0],
'product_uom': move_finished_id[2]['product_uom'][0],
'product_uom_qty': move_finished_id[2]['product_uom_qty'],
'location_id': move_finished_id[2]['location_id'][0],
'location_dest_id': destination_location.id,
'name': move_finished_id[2]['name'],
'byproduct_id': False,
}]
for move_finished_id in bom_description['move_finished_ids'][1:]
],
'move_raw_ids': [
[0, '', {
'product_id': move_raw_id[2]['product_id'][0],
'bom_line_id': move_raw_id[2]['bom_line_id'][0],
'product_uom': move_raw_id[2]['product_uom'][0],
'product_uom_qty': move_raw_id[2]['product_uom_qty'],
'location_id': part_source_location.id,
'location_dest_id': move_raw_id[2]['location_dest_id'][0],
'name': move_raw_id[2]['name'],
}]
for move_raw_id in bom_description['move_raw_ids'][1:]
]
}])
manufacturing_order.action_confirm()
immediate_production = self.env['mrp.immediate.production'].create({
'immediate_production_line_ids': [[0, '', {
'production_id': manufacturing_order.id,
'to_immediate': True
}]]
})
immediate_production.process()
manufacturing_order.button_mark_done()

def get_bom_description(self, product_id):
onchange_res = self.env['mrp.production'].onchange(
{
'product_id': product_id,
},
'product_id',
{
'company_id': '1',
'product_id': '1',
'product_qty': '1',
'product_uom_id': '1',
'bom_id': '1',
'move_finished_ids': '1',
'move_finished_ids.product_id': '1',
'move_finished_ids.product_uom': '1',
'move_finished_ids.product_uom_qty': '1',
'move_finished_ids.location_id': '1',
'move_finished_ids.location_dest_id': '1',
'move_finished_ids.name': '',
'move_raw_ids': '1',
'move_raw_ids.product_id': '1',
'move_raw_ids.name': '',
'move_raw_ids.bom_line_id': '',
'move_raw_ids.location_id': '1',
'move_raw_ids.location_dest_id': '1',
'move_raw_ids.product_uom_qty': '1',
'move_raw_ids.product_uom': '1',
}
)
return onchange_res['value']

def assemble_product_with_serial_number(self, product_id, serial_number, part_source_location, destination_location, origin=None):
if not serial_number:
raise UserError('non-empty serial_number must be provided')

_logger.info(f'Assembling: {serial_number}')

lot_name = serial_number
existing_lot = self.env['stock.production.lot'].search([('name', '=', lot_name)], limit=1)
if existing_lot and existing_lot.product_qty > 0:
raise UserError(f'Device with serial number {lot_name} already exists')

lot = existing_lot or self.env['stock.production.lot'].create({
'product_id': product_id,
'name': lot_name,
'company_id': self.env.company.id
})

bom_description = self.get_bom_description(product_id)
manufacturing_order = self.env['mrp.production'].create([{
'product_id': product_id,
'product_qty': 1,
'product_uom_qty': 1,
'qty_produced': 1,
'product_uom_id': bom_description['product_uom_id'][0],
'bom_id': bom_description['bom_id'][0],
'origin': origin,
'move_finished_ids': [
[0, '', {
'product_id': move_finished_id[2]['product_id'][0],
'product_uom': move_finished_id[2]['product_uom'][0],
'product_uom_qty': move_finished_id[2]['product_uom_qty'],
'location_id': move_finished_id[2]['location_id'][0],
'location_dest_id': destination_location.id,
'name': move_finished_id[2]['name'],
'byproduct_id': False,
}]
for move_finished_id in bom_description['move_finished_ids'][1:]
],
'move_raw_ids': [
[0, '', {
'product_id': move_raw_id[2]['product_id'][0],
'bom_line_id': move_raw_id[2]['bom_line_id'][0],
'product_uom': move_raw_id[2]['product_uom'][0],
'product_uom_qty': move_raw_id[2]['product_uom_qty'],
'location_id': part_source_location.id,
'location_dest_id': move_raw_id[2]['location_dest_id'][0],
'name': move_raw_id[2]['name'],
}]
for move_raw_id in bom_description['move_raw_ids'][1:]
]
}])
manufacturing_order.action_confirm()
manufacturing_order.update({'lot_producing_id': lot.id})
immediate_production = self.env['mrp.immediate.production'].create({
'immediate_production_line_ids': [[0, '', {
'production_id': manufacturing_order.id,
'to_immediate': True
}]]
})
immediate_production.process()
manufacturing_order.button_mark_done()
_logger.info(f'Completed Manufacturing Order {manufacturing_order.name}')

return self.env['stock.production.lot'].browse(lot.id)



Same thing can be done using API. The important part for me was that I needed to call [mrp.production].onchange to get information about all components from the bom and then pass them in as move_finished_ids and move_raw_ids.

(edit: added example for assembling a product with serial number)
Avatar
Discard

Awesome thank you for the script! this works as needed, i don't think i would have ever worked this out! I would mark your answer as the correct answer as i'm sure people will look for this in the future!

Another quick question if you have any suggestions, it works great for products that dont need a Serial/Lot ID but gives me an error for products that do need a lot.

i can generate a randomised serial using the commented code, manufacturing_order.action_generate_serial()

but id really like to use the Lot_id provided by the user to create the lot.

ive tried passing 'lot_producing_id' and it actually appears in the produced MO's but i must be missing a variable or prior function because the MO's cannot be advanced to 'Done' even if you change to a different/create a new Serial on the Manufacturing order direclty. it always says user Error a lot/serial number must be supplied.

ive also tried passing 'Lot_Ids' ,'order_finished_lot_ids' on the 'move_finished_ids' variable but this doesnt help or progress it in any way...

wondering if i pass a var in the XML. as i have seen some Xpath solutions.

Code is :::

Lot_id = self.env['stock.production.lot'].create({

'name': self.Serial_Number,

'product_id': WIP_check,

'company_id': company,

})

## https://www.odoo.com/forum/help-1/how-to-create-manufacturing-order-using-api-182874

bom_description = self.get_bom_description(WIP_check)

print('str(self.package_qty)',str(self.package_qty))

manufacturing_order = self.env['mrp.production'].create([{

'product_id': WIP_check,

'product_qty': str(self.package_qty),

'product_uom_id': bom_description['product_uom_id'][0],

'qty_producing': self.package_qty,

'product_uom_qty': str(self.package_qty),

'bom_id': bom_description['bom_id'][0],

'origin': self.reference,

'location_src_id': Source_Location,

'lot_producing_id': Lot_id.id,

'move_finished_ids': [

[0, '', {

'product_id': move_finished_id[2]['product_id'][0],

'product_uom': move_finished_id[2]['product_uom'][0],

'product_uom_qty': str(int(move_finished_id[2]['product_uom_qty'])*int(self.package_qty)),

'location_id': move_finished_id[2]['location_id'][0],

'location_dest_id': move_finished_id[2]['location_dest_id'][0],

'name': move_finished_id[2]['name'],

'byproduct_id': False,

'quantity_done': str(int(move_finished_id[2]['product_uom_qty']) * int(self.package_qty)),

}]

for move_finished_id in bom_description['move_finished_ids'][1:]

],

'move_raw_ids': [ [0, '', {

'product_id': move_raw_id[2]['product_id'][0],

'bom_line_id': move_raw_id[2]['bom_line_id'][0],

'product_uom': move_raw_id[2]['product_uom'][0],

'product_uom_qty': str(int(move_raw_id[2]['product_uom_qty'])*int(self.package_qty)),

'location_id': Source_Location,

'location_dest_id':Source_Location,

'name': move_raw_id[2]['name'],

'quantity_done': str(int(move_raw_id[2]['product_uom_qty']) * int(self.package_qty)),

}]

for move_raw_id in bom_description['move_raw_ids'][1:]

]

}])

manufacturing_order.action_confirm()

# manufacturing_order.action_generate_serial()

manufacturing_order.action_assign()

manufacturing_order.button_mark_done()

def get_bom_description(self, product_id):

onchange_res = self.env['mrp.production'].onchange(

{

'product_id': product_id

},

'product_id',

{

'company_id': '1',

'product_id': '1',

'product_qty': '1',

'product_uom_id': '1',

'bom_id': '1',

'move_finished_ids': '1',

'move_finished_ids.product_id': '1',

'move_finished_ids.product_uom': '1',

'move_finished_ids.product_uom_qty': '1',

'move_finished_ids.location_id': '1',

'move_finished_ids.location_dest_id': '1',

'move_finished_ids.name': '',

'move_raw_ids': '1',

'move_raw_ids.product_id': '1',

'move_raw_ids.name': '',

'move_raw_ids.bom_line_id': '',

'move_raw_ids.location_id': '1',

'move_raw_ids.location_dest_id': '1',

'move_raw_ids.product_uom_qty': '1',

'move_raw_ids.product_uom': '1',

}

)

return onchange_res['value']

also @ray i setup this and it is helpful, but when i compare my code running to odoo code manufacturing i see the same thing.

working MO : 'lot_producing_id': (185, '0000011'),

my test : 'lot_producing_id': (195, '207'),

when I look at PGadmin i can see the lot_producing_id in the correct table.

select * from mrp_production mp

inner join

stock_production_lot spl

on

mp.lot_producing_id = spl.id;

my lack of odoo understanding and where to find solutions is the issue hahahah.

im not sure which triggers run when Odoo runs its own MO because from what i can tell im doing the right things.

Ive found a ton of your answers for automating stuff using auto assigned Lots but unfortunately not much for user assigned lots.

Hoping your expertise will come in handy here

Author

I've edited the answer to add another example for manufacturing a product with a serial number

Thank you Ruben very professional! I hope to be as skilled as you someday! any advice on how to learn to reverse engineer Odoo so I can learn how to do this myself?

Author

It took me around three months to figure this out and to implement odoo in our organization. Basically I had to read a lot of the source code to understand how it works (e.g. https://github.com/odoo/odoo/tree/14.0/addons/mrp/models). I also inspect what calls are made by the odoo UI and that gives me an idea of where to look at.

A little extra. this code will consume products where lot assignment is turned on for consumed goods. thanks to Ray and Ruben for the advice that got me there.

def assemble_product_with_serial_number(self):

lot_name = self.Serial_Number

existing_lot = self.env['stock.production.lot'].search([('name', '=', lot_name)], limit=1)

if existing_lot and existing_lot.product_qty > 0:

raise exceptions.UserError(f'Device with serial number {lot_name} already exists')

lot = existing_lot or self.env['stock.production.lot'].create({

'product_id': WIP_check,

'name': lot_name,

'company_id': self.env.company.id

})

bom_description = self.get_bom_description(WIP_check)

for move_finished_id in bom_description['move_finished_ids']:

print(move_finished_id)

manufacturing_order = self.env['mrp.production'].create([{

'product_id': WIP_check,

'product_qty': str(self.package_qty),

'product_uom_qty': 1,

'product_uom_id': bom_description['product_uom_id'][0],

'bom_id': bom_description['bom_id'][0],

'origin': self.reference,

'location_src_id': Source_Location,

'move_finished_ids': [

[0, '', {

'product_id': move_finished_id[2]['product_id'][0],

'product_uom': move_finished_id[2]['product_uom'][0],

'product_uom_qty': str(int(move_finished_id[2]['product_uom_qty'])*int(self.package_qty)),

'location_id': move_finished_id[2]['location_id'][0],

'location_dest_id': Source_Location,

'name': move_finished_id[2]['name'],

'byproduct_id': False,

}]

for move_finished_id in bom_description['move_finished_ids'][1:]

],

'move_raw_ids': [

[0, '', {

'product_id': move_raw_id[2]['product_id'][0],

'bom_line_id': move_raw_id[2]['bom_line_id'][0],

'product_uom': move_raw_id[2]['product_uom'][0],

'product_uom_qty': str(int(move_raw_id[2]['product_uom_qty'])*int(self.package_qty)),

'location_id': Source_Location,

'location_dest_id': move_raw_id[2]['location_dest_id'][0],

'name': move_raw_id[2]['name'],

}]

for move_raw_id in bom_description['move_raw_ids'][1:]

]

}])

manufacturing_order.action_confirm()

# get actual lot ID's in WIP, - how to handle half lots?

# get actual consumption value - DONE

tracking=[]

for raw_ids in bom_description['move_raw_ids'][1:]:

if raw_ids[2]['has_tracking'] != 'none':

sm = self.env['stock.move'].search([('reference', '=', manufacturing_order.name),('product_id','=',raw_ids[2]['product_id'][0])])

self._cr.execute("""

select

lot_id,

sq.quantity,

reserved_quantity,

sl.complete_name

from

stock_quant sq

inner join

stock_location sl

on

sq.location_id = sl.id

inner join

stock_move sm

on

sl.id = sm.location_id

and

sq.product_id = sm.product_id

where

sl.complete_name like %s

and

sm.id = %s

and

lot_id is not null

""",

('%WIP%',sm.id))

Lots_Avail_in_WIP = self._cr.fetchall()

product_to_consume = int(self.package_qty)

for lot_id, sq_quantity, reserved_quantity, sl_complete_name in Lots_Avail_in_WIP:

print('--', lot_id, sq_quantity, reserved_quantity, sl_complete_name)

print('--int(raw_ids[2][product_uom_qty]) * product_to_consume',int(raw_ids[2]['product_uom_qty']) * product_to_consume)

if sq_quantity < 1:

continue

elif sq_quantity > int(raw_ids[2]['product_uom_qty']) * product_to_consume:

print('---Enough goods for single lot consumption')

self.env['stock.move'].search([('id', '=', sm.id)]).update({'lot_ids': [lot_id],

'product_uom_qty': str(int(raw_ids[2]['product_uom_qty'])*product_to_consume),

'quantity_done': str(int(raw_ids[2]['product_uom_qty'])*product_to_consume),

'should_consume_qty': str(int(raw_ids[2]['product_uom_qty'])*product_to_consume),

})

break

else:

#to be completed

print('---not enough goods for single lot consumption')

self.env['stock.move'].search([('id', '=', sm.id)]).update({'lot_ids': [lot_id],

'product_uom_qty': str(int(

raw_ids[2][

'product_uom_qty']) * product_to_consume),

'quantity_done': str(int(raw_ids[2][

'product_uom_qty']) * product_to_consume),

'should_consume_qty': str(int(

raw_ids[2][

'product_uom_qty']) * product_to_consume),

})

product_to_consume -= sq_quantity

manufacturing_order.update({'lot_producing_id': lot.id})

immediate_production = self.env['mrp.immediate.production'].create({

'immediate_production_line_ids': [[0, '', {

'production_id': manufacturing_order.id,

'to_immediate': True

}]]

})

immediate_production.process()

manufacturing_order.button_mark_done()

_logger.info(f'Completed Manufacturing Order {manufacturing_order.name}')

Related Posts Replies Views Activity
2
Jan 20
1927
6
Oct 18
4643
0
Mar 23
947
1
May 22
3952
2
Nov 19
3950