How to Programmatically Cancel MRP Records in Odoo
Tags: odoo, python
Published: 2024-04-04 21:50:35


# How to Programmatically Cancel MRP Records in Odoo

Sometimes, it's necessary to cancel specific manufacturing orders (MRPs) due to various reasons such as production issues, changes in demand, or material shortages. This article demonstrates how to programmatically cancel MRP records in Odoo using Python.

## Pre-requisites

Before proceeding, ensure you have the following:
- Odoo instance accessible via network
- Administrative rights or API access credentials
- Python environment with `xmlrpc.client` and `requests` libraries installed

## Step 1: Disable SSL Verification

In a development or testing environment, you might encounter SSL verification issues. The first part of the code disables SSL verification to avoid these problems:

```python
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context
```

**Note:** Disabling SSL verification in a production environment is not recommended due to security risks.

## Step 2: Authenticate with Odoo

You need to authenticate with the Odoo server to access its API. Replace the `url`, `db`, `username`, and `password` with your Odoo server's details:

```python
import xmlrpc.client

db = 'your_db_name'
url = 'http://your_odoo_server_ip:8069'
username = 'your_username'
password = 'your_password'

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url), allow_none=True, verbose=False, use_datetime=True, context=ssl._create_unverified_context())
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url), allow_none=True, verbose=False, use_datetime=True, context=ssl._create_unverified_context())

uid = common.authenticate(db, username, password, {})
```

## Step 3: Cancel MRP Records

The following code searches for MRP records in the state 'to_close', cancels the associated picking records, and then cancels the MRP records:

```python
mrp_active = models.execute_kw(db, uid, password, 'mrp.production', 'search_read', [[['state', '=', 'to_close']]])

for pick in mrp_active:
    for i in pick['picking_ids']:
        try:
            models.execute_kw(db, uid, password, 'stock.picking', 'action_cancel', [i])
        except Exception as e:
            print('Error canceling picking:', e)
    try:
        models.execute_kw(db, uid, password, 'mrp.production', 'action_cancel', [pick['id']])
        models.execute_kw(db, uid, password, 'mrp.production', 'write', [pick['id'], {'state': 'cancel'}])
        print('MRP record cancelled:', pick['id'])
    except Exception as e:
        print('Error canceling MRP record:', e)
```

**Conclusion**

Automating the cancellation of MRP records in Odoo can save time and reduce errors, especially when dealing with multiple records. The approach outlined above provides a basic framework for interacting with Odoo's API, which you can expand based on your specific requirements. Always test your scripts in a development environment before applying them to production to avoid unintended consequences.