1
# -*- coding: utf-8 -*-
2
##############################################################################
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>)
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.
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.
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/>
21
##############################################################################
22
from osv import fields,osv
24
from tools.translate import _
26
class shipping_rate_wizard(osv.osv_memory):
27
_name = "shipping.rate.wizard"
28
_description = "Calculates shipping charges"
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'),
36
def _get_default_val(self,cr, uid, ids,context={}):
38
Function to initialize shipping methods in shipping charge calculating wizard
41
ship_conf_obj = self.pool.get('shipping.rate.config')
42
ship_conf_ids = ship_conf_obj.search(cr,uid,[])
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})
49
_defaults = {'name':_get_default_val,
53
def update_sale_order(self, cr, uid, ids,context={}):
55
Function to update sale order and invoice with new shipping cost and method
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)
70
account_id = datas.account_id.id
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,
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')])
83
# self.pool.get('account.invoice').write(cr,uid,invoice_id,{
84
# 'shipcharge':datas.shipping_cost,
85
# 'ship_method':datas.last_used,
87
# self.pool.get('account.invoice.line').write(cr,uid,inv_line_ids,{'price_unit':datas.shipping_cost})
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',
96
# 'account_id':datas.account_id.id,
97
# 'price_unit':datas.shipping_cost})]
100
# raise osv.except_osv(_('Warning !'), _('No account defined for this shipping rate configuration.'))
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={}):
107
Function to calculate shipping cost
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),])
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
121
cost = table_obj.charge
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)])
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:
131
weight = model_obj.total_weight_net
132
if table_obj.charge > 0:
133
cost = table_obj.charge
134
weight -= table_obj.from_weight
136
cost += weight*table_obj.over_cost
138
cost = weight*table_obj.over_cost
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)))
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),])
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),])
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
155
cost += weight*table_obj.over_cost
157
cost = weight*table_obj.over_cost
159
logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find rate table with Shipping Table = %s and Zone = %s."%(config_obj.shipmethodname,zone)))
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':
166
def onchange_select_ups(self, cr, uid, ids, name, last_used, context={}):
168
Function to update shipping charge when clicking on different types of shipping method
171
new_last_used = last_used
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):
182
if line[2]['shipmethodname'] == last_used:
184
line[2]['shipmethodname'] and new_list.append(line[2])
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]),))
194
if adr.get('delivery',False):
195
adr_id = adr['delivery']
196
elif adr.get('default',False):
197
adr_id = adr['default']
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):
213
if line[2]['shipmethodname'] == last_used:
215
line[2]['shipmethodname'] and new_list.append(line[2])
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]),))
225
if adr.get('delivery',False):
226
adr_id = adr['delivery']
227
elif adr.get('default',False):
228
adr_id = adr['default']
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}}
241
def onchange_select(self, cr, uid, ids, name, last_used, rate_select, context={}):
243
ship_conf_obj = self.pool.get('shipping.rate.config')
244
ship_conf_ids = ship_conf_obj.search(cr,uid,[])
246
for line in ship_conf_obj.browse(cr,uid,ship_conf_ids):
248
if line.id == rate_select:
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)
254
shipping_rate_wizard()
257
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: