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

« back to all changes in this revision

Viewing changes to sale_negotiated_shipping/wizard/wizard_shipping_rate_calculation.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 General Public License as published by
10
 
#    the Free Software Foundation, either version 3 of the License, or
11
 
#    (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 General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 
#
21
 
##############################################################################
22
 
from osv import fields,osv
23
 
import netsvc
24
 
from tools.translate import _
25
 
 
26
 
class shipping_rate_wizard(osv.osv_memory):
27
 
    _name = "shipping.rate.wizard"
28
 
    _description = "Calculates shipping charges"
29
 
    _columns = {
30
 
                'name': fields.one2many('shipping.rate.config','ups_shipping_wizard','Shipping Method'),
31
 
                'shipping_cost':fields.float('Shipping Cost'),
32
 
                'last_used':fields.char('Last Used',size="128"),
33
 
                'account_id':fields.many2one('account.account', 'Account'),
34
 
                'rate_select': fields.many2one('shipping.rate.config','Select'),
35
 
    }
36
 
    def _get_default_val(self,cr, uid, ids,context={}):
37
 
        '''
38
 
        Function to initialize shipping methods in shipping charge calculating wizard
39
 
        '''
40
 
        ret=[]
41
 
        ship_conf_obj = self.pool.get('shipping.rate.config')
42
 
        ship_conf_ids = ship_conf_obj.search(cr,uid,[])
43
 
        account_id = False
44
 
        for ship_conf in ship_conf_obj.browse(cr,uid,ship_conf_ids):
45
 
            if ship_conf.account_id:
46
 
                account_id = ship_conf.account_id.id
47
 
            ret.append({'shipmethodname':ship_conf.shipmethodname,'use': 0,'real_id':ship_conf.id,'account_id':account_id})
48
 
        return ret
49
 
    _defaults = {'name':_get_default_val,
50
 
                'last_used':'',
51
 
                
52
 
                    }
53
 
    def update_sale_order(self, cr, uid, ids,context={}):
54
 
        '''
55
 
        Function to update sale order and invoice with new shipping cost and method
56
 
        '''
57
 
        datas = self.browse(cr, uid, ids[0], context=context)
58
 
        if context.get('active_model',False) == 'sale.order':
59
 
            sale_id = context.get('active_id',False)
60
 
            sale_id and self.pool.get('sale.order').write(cr,uid,[sale_id],{'shipcharge':datas.shipping_cost,
61
 
                                                                            'ship_method':datas.last_used,
62
 
                                                                            'sale_account_id':datas.account_id.id,
63
 
                                                                            'ship_method_id':datas.rate_select.id,})
64
 
            self.pool.get('sale.order').button_dummy(cr, uid, [sale_id], context=context)
65
 
            return {'nodestroy':False,'type': 'ir.actions.act_window_close'}
66
 
        elif context.get('active_model',False) == 'account.invoice':
67
 
            invoice_id = context.get('active_id',False)
68
 
            if invoice_id:
69
 
                if datas.account_id:
70
 
                    account_id = datas.account_id.id
71
 
                else:
72
 
                    account_id = False
73
 
                self.pool.get('account.invoice').write(cr,uid,[invoice_id],{
74
 
                                                            'shipcharge':datas.shipping_cost,
75
 
                                                            'ship_method':datas.last_used,
76
 
                                                            'ship_method_id':datas.rate_select.id,
77
 
                                                            'sale_account_id':datas.account_id.id,
78
 
                                                            })
79
 
                self.pool.get('account.invoice').button_reset_taxes(cr, uid, [invoice_id], context=context)
80
 
#                For future development to add invoice line for shipping method
81
 
#                inv_line_ids = self.pool.get('account.invoice.line').search(cr,uid,[('invoice_id','=',invoice_id),('name','=','Shipping Charge')])
82
 
#                if inv_line_ids:
83
 
#                    self.pool.get('account.invoice').write(cr,uid,invoice_id,{
84
 
#                                                                'shipcharge':datas.shipping_cost,
85
 
#                                                                'ship_method':datas.last_used,
86
 
#                                                                })
87
 
#                    self.pool.get('account.invoice.line').write(cr,uid,inv_line_ids,{'price_unit':datas.shipping_cost})
88
 
#                else:
89
 
#                    if datas.account_id:
90
 
#                        self.pool.get('account.invoice').write(cr,uid,invoice_id,{
91
 
#                                                                'shipcharge':datas.shipping_cost,
92
 
#                                                                'ship_method':datas.last_used,
93
 
#                                                                'invoice_line':[(0,0,{'name':'Shipping Charge',
94
 
#                                                                                 'quantity':1,
95
 
#                                                                                 'state':'article',
96
 
#                                                                                 'account_id':datas.account_id.id,
97
 
#                                                                                 'price_unit':datas.shipping_cost})]
98
 
#                                                                })
99
 
#                    else:
100
 
#                        raise osv.except_osv(_('Warning !'), _('No account defined for this shipping rate configuration.')) 
101
 
                            
102
 
                                   
103
 
            
104
 
        return {'nodestroy':False,'type': 'ir.actions.act_window_close'}
105
 
    def find_cost(self, cr, uid, config_id, address, model_obj, type='sale_order', context={}):
106
 
        '''
107
 
        Function to calculate shipping cost
108
 
        '''
109
 
        cost=0
110
 
        table_pool = self.pool.get('ups.shipping.rate')
111
 
        config_pool = self.pool.get('shipping.rate.config')
112
 
        logger = netsvc.Logger()
113
 
        config_obj = config_pool.browse(cr,uid,config_id, context=context)        
114
 
        if config_obj.calc_method == 'country_weight':
115
 
            table_id = table_pool.search(cr,uid,[('card_id','=',config_obj.rate_card_id.id),('country_id','=',address.country_id.id),('from_weight','<=',model_obj.total_weight_net),('to_weight','>',model_obj.total_weight_net),])
116
 
            if table_id:
117
 
                table_obj = table_pool.browse(cr,uid,table_id[0],)
118
 
                if table_obj.charge == 0.0 and table_obj.over_cost:
119
 
                    cost = model_obj.total_weight_net*table_obj.over_cost
120
 
                else:
121
 
                    cost = table_obj.charge
122
 
                    
123
 
            else:
124
 
                table_ids = table_pool.search(cr,uid,[('card_id','=',config_obj.rate_card_id.id),('country_id','=',address.country_id.id),('over_cost','>',0)])
125
 
                if table_ids:
126
 
                    table_objs = table_pool.browse(cr,uid,table_ids)
127
 
                    table_obj = table_objs[0]
128
 
                    for table in table_objs:
129
 
                        if table_obj.from_weight < table.from_weight:
130
 
                            table_obj = table
131
 
                    weight = model_obj.total_weight_net
132
 
                    if table_obj.charge > 0:
133
 
                        cost = table_obj.charge
134
 
                        weight -= table_obj.from_weight
135
 
                        if weight>0:
136
 
                            cost += weight*table_obj.over_cost
137
 
                    else:
138
 
                        cost = weight*table_obj.over_cost
139
 
                else:
140
 
                    logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find rate table with Shipping Table = %s and Country = %s and Over Cost > 0."%(config_obj.rate_card_id.name,address.country_id.name)))
141
 
                
142
 
        elif config_obj.calc_method == 'state_zone_weight':
143
 
            zone_pool = self.pool.get('ups.zone.map')
144
 
            zone_id = zone_pool.search(cr,uid,[('rate_config_id','=',config_obj.id),('state_id','=',address.state_id.id),])
145
 
            if zone_id:
146
 
                zone = zone_pool.read(cr,uid,zone_id,['zone'])[0]['zone']
147
 
                table_id = table_pool.search(cr,uid,[('card_id','=',config_obj.rate_card_id.id),('zone','=',zone),])
148
 
                if table_id:
149
 
                    table_obj = table_pool.browse(cr,uid,table_id)[0]
150
 
                    weight = model_obj.total_weight_net
151
 
                    if table_obj.charge > 0:
152
 
                        cost = table_obj.charge
153
 
                        weight -= table_obj.to_weight
154
 
                        if weight>0:
155
 
                            cost += weight*table_obj.over_cost
156
 
                    else:
157
 
                        cost = weight*table_obj.over_cost
158
 
                else:
159
 
                    logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find rate table with Shipping Table = %s and Zone = %s."%(config_obj.shipmethodname,zone)))
160
 
            else:
161
 
                logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find Zone Mapping Table with Shipping Rate Configuration = %s and State = %s."%(config_obj.shipmethodname,address.state_id.name)))
162
 
        elif config_obj.calc_method == 'manual':
163
 
            cost = 0.0
164
 
        return cost
165
 
    
166
 
    def onchange_select_ups(self, cr, uid, ids, name, last_used, context={}):
167
 
        '''
168
 
        Function to update shipping charge when clicking on different types of shipping method
169
 
        '''
170
 
        new_list = []
171
 
        new_last_used = last_used
172
 
        cost = 0
173
 
        account_id = False
174
 
        ret = {}
175
 
        ship_conf_obj = self.pool.get('shipping.rate.config')
176
 
        ship_conf_ids = ship_conf_obj.search(cr,uid,[])
177
 
        if context.get('active_model',False) == 'sale.order' and context.get('active_id',False):
178
 
            if name and len(name) == len(ship_conf_ids):
179
 
                new_last_used = ''
180
 
                value = 0
181
 
                for line in name:
182
 
                    if line[2]['shipmethodname'] == last_used:
183
 
                        line[2]['use'] = 0
184
 
                    line[2]['shipmethodname'] and new_list.append(line[2])
185
 
                    if line[2]['use']:
186
 
                        new_last_used = line[2]['shipmethodname']
187
 
                        account_id = line[2]['account_id']
188
 
                        sale_id = context.get('active_id',False)
189
 
                        sale_order = self.pool.get('sale.order').browse(cr,uid,sale_id,context=context)
190
 
                        cr.execute('select type,id from res_partner_address where partner_id IN %s',(tuple([sale_order.partner_id.id]),))
191
 
                        res = cr.fetchall()
192
 
                        adr = dict(res)
193
 
                        if adr:
194
 
                            if adr.get('delivery',False):
195
 
                                adr_id = adr['delivery']
196
 
                            elif adr.get('default',False):
197
 
                                adr_id = adr['default']
198
 
                            else:
199
 
                                adr_id = adr.values()[0]
200
 
                            address = self.pool.get('res.partner.address').browse(cr, uid,adr_id,context=context)
201
 
                            config_id = line[2]['real_id']
202
 
                            cost=self.find_cost(cr, uid, config_id, address,sale_order, type='sale_order', context=context)
203
 
                ret = {'value':{'shipping_cost':cost,'last_used':new_last_used,'name':new_list,'account_id':account_id}}
204
 
            elif len(name) > len(ship_conf_ids):
205
 
                for ship_conf in ship_conf_obj.browse(cr,uid,ship_conf_ids):
206
 
                    new_list.append({'shipmethodname':ship_conf.shipmethodname,'use': 0})
207
 
                ret = {'value':{'last_used':new_last_used,'name':new_list}}
208
 
        elif context.get('active_model',False) == 'account.invoice' and context.get('active_id',False):
209
 
            if name and len(name) == len(ship_conf_ids):
210
 
                new_last_used = ''
211
 
                value = 0
212
 
                for line in name:
213
 
                    if line[2]['shipmethodname'] == last_used:
214
 
                        line[2]['use'] = 0
215
 
                    line[2]['shipmethodname'] and new_list.append(line[2])
216
 
                    if line[2]['use']:
217
 
                        new_last_used = line[2]['shipmethodname']
218
 
                        account_id = line[2]['account_id']
219
 
                        invoice_id = context.get('active_id',False)
220
 
                        invoice = self.pool.get('account.invoice').browse(cr,uid,invoice_id,context=context)
221
 
                        cr.execute('select type,id from res_partner_address where partner_id IN %s',(tuple([invoice.partner_id.id]),))
222
 
                        res = cr.fetchall()
223
 
                        adr = dict(res)
224
 
                        if adr:
225
 
                            if adr.get('delivery',False):
226
 
                                adr_id = adr['delivery']
227
 
                            elif adr.get('default',False):
228
 
                                adr_id = adr['default']
229
 
                            else:
230
 
                                adr_id = adr.values()[0]
231
 
                            address = self.pool.get('res.partner.address').browse(cr, uid,adr_id,context=context)
232
 
                            config_id = line[2]['real_id']
233
 
                            cost=self.find_cost(cr, uid, config_id, address,invoice, type='invoice', context=context)
234
 
                ret = {'value':{'shipping_cost':cost,'last_used':new_last_used,'name':new_list,'account_id':account_id,}}
235
 
            elif len(name) > len(ship_conf_ids):
236
 
                for ship_conf in ship_conf_obj.browse(cr,uid,ship_conf_ids):
237
 
                    new_list.append({'shipmethodname':ship_conf.shipmethodname,'use': 0})
238
 
                ret = {'value':{'last_used':new_last_used,'name':new_list}}
239
 
        return ret
240
 
    
241
 
    def onchange_select(self, cr, uid, ids, name, last_used, rate_select, context={}):
242
 
        new_list = []
243
 
        ship_conf_obj = self.pool.get('shipping.rate.config')
244
 
        ship_conf_ids = ship_conf_obj.search(cr,uid,[])
245
 
        account_id = False
246
 
        for line in ship_conf_obj.browse(cr,uid,ship_conf_ids):
247
 
            use = 0
248
 
            if line.id == rate_select:
249
 
                use=1
250
 
            new_list.append((0,0,{'use':use,'real_id':line.id,'account_id':line.account_id.id,'shipmethodname':line.shipmethodname}))
251
 
        ret = self.onchange_select_ups(cr, uid, ids, new_list, last_used, context=context)
252
 
        return ret
253
 
    
254
 
shipping_rate_wizard()
255
 
 
256
 
 
257
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: