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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay