I'm currently working on programmatic creation of Manufacturing Orders (MOs) in Odoo 15. Although I've been successful in generating the MOs themselves, I've encountered a challenge where the associated stock pickings are not being created as expected. In contrast, when creating an MO manually through the front-end interface, the associated stock pickings are correctly attached. I've attempted to address this issue by utilizing the onchange method, but so far, I haven't achieved the desired outcome. I'm looking for guidance on how to resolve this issue and ensure that the appropriate stock pickings are generated and linked to the MOs when created programmatically
import odoorpc
def create_mo(product_id, product_qty, picking_type_id):
# Define the connection parameters
db = ''
url = ''
username = ''
password = ''
# Initialize the connection
odoo = odoorpc.ODOO(url)
# Authenticate
odoo.login(db, username, password)
# Get the picking type record
picking_type = odoo.env['stock.picking.type'].browse(picking_type_id)
# Access the mrp.bom model
mrp_bom_model = odoo.env['mrp.bom']
# Get the product template ID from the product ID
product_template_id = odoo.env['product.product'].browse(product_id).product_tmpl_id.id
# Define the search domain based on the product template ID
search_domain = [('product_tmpl_id', '=', product_template_id)]
# Search for the BOM record
bom_ids = mrp_bom_model.search(search_domain)
# Check if a BOM was found
if bom_ids:
# Retrieve the BOM record
bom_record = mrp_bom_model.browse(bom_ids[0])
# Access the mrp.production model to create the MO
mrp_production_model = odoo.env['mrp.production']
# Define the values for the new MO record
values = {
'product_id': product_id,
'product_qty': product_qty,
'bom_id': bom_record.id,
'product_uom_id': bom_record.product_uom_id.id,
'picking_type_id': picking_type.id,
'location_src_id': 17,
'location_dest_id': 18,
}
# Calculate the components from the BOM lines
components = []
for line in bom_record.bom_line_ids:
components.append({
'name': line.product_id.name,
'product_id': line.product_id.id,
'product_uom': line.product_uom_id.id,
'product_uom_qty': line.product_qty * product_qty,
'location_id': picking_type.default_location_src_id.id,
'location_dest_id': picking_type.default_location_dest_id.id,
})
# Add the components to the MO values
values['move_raw_ids'] = [(0, 0, line) for line in components]
# Create the MO
new_mo_id = mrp_production_model.create(values)
# Confirm the MO
mrp_production_model.button_plan([new_mo_id])
# Browse the newly created MO
new_mo = mrp_production_model.browse(new_mo_id)
# Print the ID of the new MO
print("New Manufacturing Order created with ID:", new_mo_id)
else:
print("No BOM found for the specified product.")
create_mo(6666,1,8)
For anyone having the same issue, I end up just cloning an MO (with the picking type I wanted) and changing the product.id and bom to what I wanted. Not a clean solution but it worked.