in odoo i want to add a button to create a new manufacturing order
- User
in odoo i want to add a button to create a new manufacturing order
- Assistant
in odoo i want to add a button to create a new manufacturing order
Adding a button in Odoo to create a new Manufacturing Order involves customizing the relevant view and model. Below is a step-by-step guide to help you achieve this. The process includes modifying the XML view to add the button and adding corresponding Python code to handle the button's action.
If you haven't already created a custom module, you'll need to create one. Here's a brief overview:
Create Module Structure:
your_module/
├── __init__.py
├── __manifest__.py
├── models/
│ ├── __init__.py
│ └── your_model.py
└── views/
└── your_view.xml
Define __manifest__.py:
{
'name': 'Manufacturing Order Button',
'version': '1.0',
'depends': ['mrp'], # Ensure you depend on the manufacturing module
'data': [
'views/your_view.xml',
],
'installable': True,
'application': False,
}
Identify the view where you want to add the button. For example, if you want to add the button to the product.template form view, you would inherit and extend that view.
Example XML (your_view.xml):
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_product_template_form_inherit" model="ir.ui.view">
<field name="name">product.template.form.inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<!-- Decide where to place the button, e.g., in the header -->
<header position="inside">
<button name="action_create_manufacturing_order"
type="object"
string="Create Manufacturing Order"
class="btn-primary"
attrs="{'invisible': [('type', '!=', 'product')]}"/>
</header>
</field>
</record>
</odoo>
Explanation:
inherit_id: Reference the existing view you want to modify.button Attributes:
name: The name of the method to call on the server side.type: Set to object to call a Python method.string: The label of the button.class: CSS classes for styling (optional).attrs: Conditional display logic (optional).In your model file (your_model.py), define the method that the button will trigger. This method will create a new Manufacturing Order (mrp.production).
Example Python (your_model.py):
from odoo import models, fields, api
from odoo.exceptions import UserError
class ProductTemplate(models.Model):
_inherit = 'product.template'
def action_create_manufacturing_order(self):
self.ensure_one() # Ensure a single record is processed
# Define default values for the manufacturing order
mo_vals = {
'product_id': self.product_variant_id.id,
'product_qty': 1.0, # You can customize this as needed
'product_uom_id': self.uom_id.id,
'location_src_id': self.env.ref('stock.stock_location_production').id,
'location_dest_id': self.env.ref('stock.stock_location_production').id,
'company_id': self.env.company.id,
}
# Create the Manufacturing Order
mo = self.env['mrp.production'].create(mo_vals)
# Optionally, open the newly created Manufacturing Order form view
return {
'type': 'ir.actions.act_window',
'name': 'Manufacturing Order',
'view_mode': 'form',
'res_model': 'mrp.production',
'res_id': mo.id,
'target': 'current',
}
Explanation:
ensure_one(): Ensures that the method is called on a single record.mo_vals: Dictionary containing default values for the new Manufacturing Order.
product_id: The product for which the MO is created.product_qty: Quantity to produce. Customize as needed or make it dynamic (e.g., via a wizard).product_uom_id: Unit of measure.location_src_id and location_dest_id: Source and destination locations.company_id: Current company.create method to instantiate a new record.__init__.py FilesEnsure that your Python files are loaded correctly by updating the __init__.py files.
In models/__init__.py:
from . import your_model
In __init__.py at the module root:
from . import models
Define access rights to ensure that users can create Manufacturing Orders.
Create security/ir.model.access.csv:
id, name, model_id:id, group_id:id, perm_read, perm_write, perm_create, perm_unlink
access_mrp_production_custom, access_mrp_production_custom, model_mrp_production, base.group_user, 1,1,1,1
Update __manifest__.py to include security file:
'data': [
'security/ir.model.access.csv',
'views/your_view.xml',
],
Note: Adjust the access rights as per your requirements.
If you want users to input the quantity when creating the Manufacturing Order, you can implement a wizard.
Define a Wizard Model:
from odoo import models, fields, api
class CreateMoWizard(models.TransientModel):
_name = 'create.mo.wizard'
_description = 'Wizard to Create Manufacturing Order'
product_id = fields.Many2one('product.product', string='Product', required=True)
product_qty = fields.Float(string='Quantity', required=True, default=1.0)
def create_mo(self):
mo_vals = {
'product_id': self.product_id.id,
'product_qty': self.product_qty,
'product_uom_id': self.product_id.uom_id.id,
'location_src_id': self.env.ref('stock.stock_location_production').id,
'location_dest_id': self.env.ref('stock.stock_location_production').id,
'company_id': self.env.company.id,
}
mo = self.env['mrp.production'].create(mo_vals)
return {
'type': 'ir.actions.act_window',
'name': 'Manufacturing Order',
'view_mode': 'form',
'res_model': 'mrp.production',
'res_id': mo.id,
'target': 'current',
}
Define Wizard's View (wizard_view.xml):
This enhancement provides a more flexible and user-friendly way to create Manufacturing Orders with customizable parameters.
By following the steps outlined above, you can successfully add a button in Odoo to create new Manufacturing Orders. Customize the button's placement and functionality as per your business requirements. Remember to test thoroughly in a development environment before deploying to production.
If you're not comfortable with module development, consider using Odoo Studio, which allows some level of customization without writing code. However, for more advanced functionalities like creating records in other models, module development provides greater flexibility and control.
Not yet included in a published release.
<odoo>
<record id="view_create_mo_wizard" model="ir.ui.view">
<field name="name">create.mo.wizard.form</field>
<field name="model">create.mo.wizard</field>
<field name="arch" type="xml">
<form string="Create Manufacturing Order">
<group>
<field name="product_id"/>
<field name="product_qty"/>
</group>
<footer>
<button string="Create"
type="object"
name="create_mo"
class="btn-primary"/>
<button string="Cancel"
class="btn-secondary"
special="cancel"/>
</footer>
</form>
</field>
</record>
</odoo>
Modify the Button Action to Open the Wizard:
def action_create_manufacturing_order(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': 'Create Manufacturing Order',
'view_mode': 'form',
'res_model': 'create.mo.wizard',
'target': 'new',
'context': {
'default_product_id': self.product_variant_id.id,
},
}
Update __manifest__.py to Include Wizard Views:
'data': [
'security/ir.model.access.csv',
'views/your_view.xml',
'views/wizard_view.xml',
],