~serpent-consulting-services/openerp-usa/shipping_api_6-1

« back to all changes in this revision

Viewing changes to account_salestax_avatax/wizard/account_salestax_avatax_address_validate.py

  • Committer: npgllc
  • Date: 2012-08-02 17:13:27 UTC
  • Revision ID: npgllc-20120802171327-2xgyyjjb5d1kx26y
Removed all the 6.0 compatible modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
 
#    Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU Affero General Public License as
10
 
#    published by the Free Software Foundation, either version 3 of the
11
 
#    License, or (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU Affero General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU Affero General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
##############################################################################
22
 
import time
23
 
 
24
 
from osv import osv, fields
25
 
from tools.translate import _
26
 
 
27
 
class account_salestax_avatax_address_validate(osv.osv_memory):
28
 
    _name = 'account.salestax.avatax.address.validate'
29
 
    _description = 'Address Validation using AvaTax'
30
 
    _columns = {
31
 
        'original_street': fields.char('Street', size=64, readonly=True),
32
 
        'original_street2': fields.char('Street2', size=64, readonly=True),
33
 
        'original_city': fields.char('City', size=64, readonly=True),
34
 
        'original_zip': fields.char('Zip', size=64, readonly=True),
35
 
        'original_state': fields.char('State', size=64, readonly=True),
36
 
        'original_country': fields.char('Country', size=64, readonly=True),
37
 
        'street': fields.char('Street', size=64),
38
 
        'street2': fields.char('Street2', size=64),
39
 
        'city': fields.char('City', size=64),
40
 
        'zip': fields.char('Zip', size=64),
41
 
        'state': fields.char('State', size=64),
42
 
        'country': fields.char('Country', size=64),
43
 
        'latitude': fields.char('Laitude', size=64),
44
 
        'longitude': fields.char('Longitude', size=64)
45
 
    }
46
 
 
47
 
    def view_init(self, cr, uid, fields, context=None):
48
 
        """ Checks for precondition before wizard executes. """
49
 
        address_obj = self.pool.get('res.partner.address')
50
 
        avatax_config_obj= self.pool.get('account.salestax.avatax')
51
 
 
52
 
        if context is None:
53
 
            context = {}
54
 
 
55
 
        # Check if there is avatax tax service active for the user company.
56
 
        # Prevent validating the address if the address validation is disabled by the administrator.
57
 
 
58
 
        if context.get('active_id', False) and context.get('active_model', False) == 'res.partner.address':
59
 
            avatax_config = avatax_config_obj._get_avatax_config_company(cr, uid, context=context)
60
 
            if not avatax_config:
61
 
                raise osv.except_osv(_('Service Not Setup'), _("The AvaTax Tax Service is not active. To sign-up for the AvaTax Tax Service please visit www.novapointgroup.com."))
62
 
            address = address_obj.browse(cr, uid, context['active_id'], context=context)
63
 
            if address.validated_on_save and avatax_config.validation_on_save:
64
 
                raise osv.except_osv(_('Address Already Validated'), _("Address Validation on Save is already active in the AvaTax Configuration."))
65
 
            address_obj.check_avatax_support(cr, uid, avatax_config, address.country_id and address.country_id.id or False, context=context)
66
 
        return True
67
 
 
68
 
    def default_get(self, cr, uid, fields, context=None):
69
 
        """  Returns the default values for the fields. """
70
 
 
71
 
        res = super(account_salestax_avatax_address_validate, self).default_get(cr, uid, fields, context)
72
 
 
73
 
        if context.get('active_id', False) and context.get('active_model', False) == 'res.partner.address':
74
 
            address_obj = self.pool.get('res.partner.address')
75
 
            address = address_obj.read(cr, uid, context['active_id'], ['street', 'street2', 'city', 'state_id', 'zip', 'country_id'], context=context)
76
 
            address['state_id'] = address.get('state_id') and address['state_id'][0]
77
 
            address['country_id'] = address.get('country_id') and address['country_id'][0]
78
 
 
79
 
            # Get the valid result from the AvaTax Address Validation Service
80
 
            valid_address = address_obj._validate_address(cr, uid, address, context=context)
81
 
 
82
 
            if 'original_street' in fields:
83
 
                res.update({'original_street': address['street']})
84
 
            if 'original_street2' in fields:
85
 
                res.update({'original_street2': address['street2']})
86
 
            if 'original_city' in fields:
87
 
                res.update({'original_city': address['city']})
88
 
            if 'original_state' in fields:
89
 
                res.update({'original_state': address_obj.get_state_code(cr, uid, address['state_id'], context=context)})
90
 
            if 'original_zip' in fields:
91
 
                res.update({'original_zip': address['zip']})
92
 
            if 'original_country' in fields:
93
 
                res.update({'original_country': address_obj.get_country_code(cr, uid, address['country_id'], context=context)})
94
 
            if 'street' in fields:
95
 
                res.update({'street': str(valid_address.Line1 or '')})
96
 
            if 'street2' in fields:
97
 
                res.update({'street2': str(valid_address.Line2 or '')})
98
 
            if 'city' in fields:
99
 
                res.update({'city': str(valid_address.City or '')})
100
 
            if 'state' in fields:
101
 
                res.update({'state': str(valid_address.Region or '')})
102
 
            if 'zip' in fields:
103
 
                res.update({'zip': str(valid_address.PostalCode or '')})
104
 
            if 'country' in fields:
105
 
                res.update({'country': str(valid_address.Country or '')})
106
 
            if 'latitude' in fields:
107
 
                res.update({'latitude': str(valid_address.Latitude or '')})
108
 
            if 'longitude' in fields:
109
 
                res.update({'longitude': str(valid_address.Longitude or '')})
110
 
        return res
111
 
 
112
 
    def accept_valid_address(self, cr, uid, ids, context=None):
113
 
        """ Updates the existing address with the valid address returned by the service. """
114
 
 
115
 
        valid_address = self.read(cr, uid, ids, context=context)[0]
116
 
        if context.get('active_id', False):
117
 
            address_obj = self.pool.get('res.partner.address')
118
 
            address_result = {
119
 
                'street': valid_address['street'],
120
 
                'street2': valid_address['street2'],
121
 
                'city': valid_address['city'],
122
 
                'state_id': address_obj.get_state_id(cr, uid, valid_address['state'], context=context),
123
 
                'zip': valid_address['zip'],
124
 
                'country_id': address_obj.get_country_id(cr, uid, valid_address['country'], context=context),
125
 
                'latitude': valid_address['latitude'],
126
 
                'longitude': valid_address['longitude'],
127
 
                'date_validation': time.strftime('%Y-%m-%d'),
128
 
                'validation_method': 'avatax'
129
 
            }
130
 
            address_obj.write(cr, uid, [context['active_id']], address_result, context=context)
131
 
        return {'type': 'ir.actions.act_window_close'}
132
 
 
133
 
account_salestax_avatax_address_validate()
134
 
 
135
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: