Search code examples
pythonodooxml-rpcodoo-9

Odoo XML RPC search and insert


I want to search for customers in res.partner from xl using their names then if yes i assign their partner id in the sales order im creating in xmlrpc else insert that partner and use his id in the sales order im creating.note that the purpose is to migrate sales order from xls file to odoo,for now the actual code is the following.

import psycopg2
import psycopg2.extras
import pyexcel_xls
import pyexcel as pe
from pyexcel_xls import get_data
from datetime import datetime
import xmlrpclib
import json

url = 'http://localhost:8070'
db = 'Docker'
username = 'admin'
password = 'odoo'
#data = get_data("salesorder.xls")
#print(json.dumps(data))
records = pe.get_records(file_name="salesorder.xls")
for record in records:
    print record['name']
    names = record['name']
    print record['location']
    print record['zip']
    print record['republic']
    dates = record['date']
    print dates
    print datetime.strptime(dates,'%d/%M/%Y')
    lastdat=datetime.strptime(dates,'%d/%M/%Y')


    common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
    output = common.version()
    models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
    ids = models.execute_kw(db, uid, password,
                            'res.partner', 'search',
                            ['name', '=', "names"])



    uid = common.authenticate(db, username, password, {})
    print output
    models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
    id = models.execute_kw(db, uid, password, 'sale.order', 'create',     [{
        'name': names,
        'validity_date':"2016-01-18"
        #'payment_term_id':"1"
        # 'user_id':"1"
        # 'state':"sale"

    }])
    print id

Solution

  • Here is a modification of your script which I think should work. I did not look into what values should be passed into the creation of a sales order. You will have to ensure you are passing the correct values. You also import a few packages that you do not use, but I left those in as I assume you plan on using them. Bottom line you need to search for your contact in the system. If you find them use the id, if not create the contact and use the new contact id for the creation of your sales order.

    import psycopg2
    import psycopg2.extras
    import pyexcel_xls
    import pyexcel as pe
    from pyexcel_xls import get_data
    from datetime import datetime
    import xmlrpclib
    import json
    
    url = 'http://localhost:8070'
    db = 'Docker'
    username = 'admin'
    password = 'odoo'
    
    models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
    common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
    uid = common.authenticate(db, username, password, {})
    
    records = pe.get_records(file_name="salesorder.xls")
    
    for record in records:
        print record['name']
        # DEFINE THE VALUES YOU PLAN ON PASSING INTO YOUR SALES ORDER HERE 
        vals = {
            'name': record['name'],
            'validity_date':"2016-01-18"
        }
        ids = models.execute_kw(db, uid, password, 'res.partner', 'search', [[['name', '=', names]]])
        # IF YOU FIND THE CONTACT IN THE SYSTEM ADD THEIR ID TO YOUR VALS OBJECT
        # IF YOU ARE CONFIDENT YOU DO NOT HAVE DUPLICATES OF NAMES THIS SHOULD WORK, OTHERWISE A MORE PRECISE SEARCH DOMAIN IS NECESSARY
        if len(ids) > 0:
            vals['partner_id'] = ids[0]
        # IF YOU DONT FIND A MATCH YOU CAN CREATE A NEW PARTNER AND USE THAT ID
        else:
            vals['partner_id'] = models.execute_kw(db, uid, password, 'res.partner', 'create', [{ 'name': record['name'] }])
        sale_order_id = models.execute_kw(db, uid, password, 'sale.order', 'create',[vals])
        print sale_order_id