DEV Community

Fabian Anguiano
Fabian Anguiano

Posted on

Managing Duplicate Sales Records in Odoo Using Python

I recently had a situation where an Odoo instance was importing sales from multiple sales channels, due to the nature of the import process some sales got created twice. Technically sales records with the same name should not allowed, but alas here we are.

I made this script to find any records with the same name.

import xmlrpc.client

# Odoo details
url = 'your_url'
db = 'your_db'
username = 'your_username'
password = 'your_password'

# Get the uid
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(db, username, password, {})

# Get the models
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

# Search for sales order records
sales_order_ids = models.execute_kw(db, uid, password,
    'sale.order', 'search',
    [[]])

# Read the sales order records
sales_orders = models.execute_kw(db, uid, password,
    'sale.order', 'read',
    [sales_order_ids])

# Dictionary to store the count of each record
record_count = {}

# Go through each sales order
for order in sales_orders:
    # If the order name is not in the record_count
    if order['name'] not in record_count:
        # Add it with a count of 1
        record_count[order['name']] = 1
    else:
        # If it is already there, increment the count
        record_count[order['name']] += 1

# Print the repeated records
for record, count in record_count.items():
    if count > 1:
        print(f"\033[91m Multiple sales records: {record} \033[0m")

Enter fullscreen mode Exit fullscreen mode

Top comments (0)