我目前正在Odoo 15中的制造订单(mo)的程序化创建工作。尽管我已经成功地生成了mo本身,但我遇到了一个挑战,即相关的股票选择没有像预期的那样创建。相反,当通过前端界面手动创建MO时,将正确地附加相关的股票选择。我试图通过使用onchange方法来解决这个问题,但到目前为止,我还没有达到预期的结果。我正在寻找有关如何解决此问题的指导,并确保在以编程方式创建时生成适当的股票选择并链接到MOs
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)
对于任何有同样问题的人,我最终只是克隆一个MO(带有我想要的采摘类型)并更改产品。我生来就是我想要的。这不是一个干净的解决方案,但它有效。