Thursday, 15 May 2014

How to validate address of customer in openerp

It is answer to the questions like -
    a- how to verify address of partner in openerp
    b- how to correct address entered by user

We need varified and valid address in several cases like for - shipping address. 
Here I am going to tell you how to verify your address with TAX CLOUD (https://taxcloud.net/default.aspx).It is a short integration.
Follow these simple steps:-

1. for this you have to install a python package "suds" -
  can use command - pip install suds

2. then import suds into your code -
    import suds

3. then get instance of client -
    client = suds.client.Client(wsdl)

4. put this wsdl address into your code -
    wsdl = "https://api.taxcloud.net/1.0/?wsdl"

5. now create a usps id -
    go to address - https://www.usps.com/business/web-tools-apis/developers-center.htm
    register your self . You will get USPS user id. put that into variable usps_id into your code
    usps_id = "269SONUCXXXX" // example

6. now you can use "VerifyAddress" method of client class , to validate and correct address .
    Here is sample code


    vals ={}
    try:
        verifiedAddress = client.service.VerifyAddress(usps_id, 'street' ,'street2', 'city',state_code, 'zip')
        if verifiedAddress.ErrNumber!='0':
        vals.update({'address_valid_message':str(verifiedAddress.ErrDescription)})
        raise osv.except_osv(_('Address Validation Error'), _(str(verifiedAddress.ErrDescription)))
        else:
        vals.update({
                'street': verifiedAddress.Address1,
                'street2': verifiedAddress.Address2 if verifiedAddress.__contains__('Address2') else '',
                'city': verifiedAddress.City,
                'state_id': self.get_state_id(cr, uid, verifiedAddress.State, context=context),
                'zip': verifiedAddress.Zip5+'-'+verifiedAddress.Zip4,
                'address_valid_message': 'Valid Address',
                #'valid_address':True,
                 })
    except Exception, e:
        print "Exception E......",e.args
        raise osv.except_osv(_('Address Validation Error'), _("Address is not Valid"
        " %s") % str((e.args)))
        pass
    return vals

    it retuns ErrNumber non-zero on failure and zero on success
    so you can identify whether address was valid or not .

It returns corrected address ,which you can put back into your form
  To access corrected street
     corrected_street = verifiedAddress.Address1
     example - vals.update({
                'street': verifiedAddress.Address1,
                'street2': verifiedAddress.Address2 if verifiedAddress.__contains__('Address2') else '',
                'city': verifiedAddress.City,
                'state_id': self.get_state_id(cr, uid, verifiedAddress.State, context=context),
                'zip': verifiedAddress.Zip5+'-'+verifiedAddress.Zip4,
                'address_valid_message': 'Valid Address',
                #'valid_address':True,
                 })
Note - It requires a working internet connection

Thanks
Sonu Chaudhary

2 comments:

  1. Great Post. Just wondering is there a way to get the right tax amount via TaxCloud once the address has been verified? As you know in the USA many States have City, County and State Taxes and I need a way to calculate accurate taxes for my customers. How would I go about implementing this in Odoo v8?

    Thanks for your time.

    ReplyDelete
  2. Why is the address verification done using usps_id instead of using API available at Taxcloud?

    ReplyDelete