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
##############################################################################
23
from osv import fields, osv
25
from tools.translate import _
27
class shipping_rate_card(osv.osv):
28
_name = 'shipping.rate.card'
29
_description = "Ground Shipping Calculation Table"
31
'name': fields.char('Shipping Method', size=128, required=True),
32
'from_date': fields.datetime('From Date'),
33
'to_date': fields.datetime('To Date'),
34
'rate_ids': fields.one2many('shipping.rate', 'card_id', 'Shipping Rates', required=True),
39
class shipping_rate_config(osv.osv):
40
_name = 'shipping.rate.config'
41
_description = "Configuration for shipping rate"
42
_rec_name = 'shipmethodname'
45
'real_id': fields.integer('ID', readonly=True, ),
46
'shipmethodname': fields.char('Shipping Method Name', size=128, help='Shipping method name. Displayed in the wizard.'),
47
'active': fields.boolean('Active', help='Indicates whether a shipping method is active'),
48
'use': fields.boolean('Select'),
49
'calc_method': fields.selection([
50
('country_weight', 'Country & Weight'),
51
('state_zone_weight', 'State-Zone-Weight'),
52
('manual', 'Manually Calculate')
53
], 'Shipping Calculation Method', help='Shipping method name. Displayed in the wizard.'),
54
'shipping_wizard': fields.integer('Shipping Wizard'),
55
'zone_map_ids': fields.one2many('zone.map', 'rate_config_id', 'Zone Map'),
56
'account_id': fields.many2one('account.account', 'Account', help='This account represents the g/l account for booking shipping income.'),
57
'shipment_tax_ids': fields.many2many('account.tax', 'shipment_tax_rel', 'shipment_id', 'tax_id', 'Taxes', domain=[('parent_id', '=', False)]),
58
'rate_card_id': fields.many2one('shipping.rate.card', 'Shipping Rate Card')
61
'calc_method': 'country_weight'
64
shipping_rate_config()
66
class zone_map(osv.osv):
68
_description = "Zone Mapping Table"
71
'zone': fields.integer('Zone'),
72
'state_id': fields.many2one('res.country.state', 'State / Zone'),
73
'rate_config_id': fields.many2one('shipping.rate.config', 'Shipping Rate Configuration')
78
class shipping_rate(osv.osv):
79
_name = 'shipping.rate'
80
_description = "Shipping Calculation Table"
82
'name': fields.char('Shipping Method', size=128),
83
'from_weight': fields.integer('From Weight', required=True),
84
'to_weight': fields.integer('To Weight'),
85
'charge': fields.float('Shipping Charge'),
86
'over_cost': fields.float('Shipping Charge per pound over'),
87
'country_id': fields.many2one('res.country', 'Country'),
88
'zone': fields.integer('Zone', required=True),
89
'card_id': fields.many2one('shipping.rate.card', 'Shipping Table')
92
def find_cost(self, cr, uid, config_id, address, model_obj, context=None):
94
Function to calculate shipping cost
97
table_pool = self.pool.get('shipping.rate')
98
config_pool = self.pool.get('shipping.rate.config')
99
logger = netsvc.Logger()
100
config_obj = config_pool.browse(cr, uid, config_id, context=context)
101
rate_card_id = config_obj.rate_card_id.id
102
if config_obj.calc_method == 'country_weight':
103
country_id = address.country_id.id
104
weight_net = model_obj.total_weight_net
105
table_ids = table_pool.search(cr, uid, [('card_id', '=', rate_card_id),('country_id', '=', country_id),
106
('from_weight', '<=', weight_net), ('to_weight', '>', weight_net)],context=context)
108
table_obj = table_pool.browse(cr, uid, table_ids[0], context=context)
109
if table_obj.charge == 0.0 and table_obj.over_cost:
110
cost = model_obj.total_weight_net * table_obj.over_cost
112
cost = table_obj.charge
115
search_list = [('card_id', '=', rate_card_id), ('country_id', '=', country_id), ('over_cost', '>', 0)]
116
table_ids = table_pool.search(cr, uid, search_list, context=context)
118
table_objs = table_pool.browse(cr, uid, table_ids, context=context)
119
table_obj = table_objs[0]
120
for table in table_objs:
121
if table_obj.from_weight < table.from_weight:
123
weight = model_obj.total_weight_net
124
if table_obj.charge > 0:
125
cost = table_obj.charge
126
weight -= table_obj.from_weight
128
cost += weight * table_obj.over_cost
130
cost = weight * table_obj.over_cost
132
logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find rate table with Shipping Table = %s and \
133
Country = %s and Over Cost > 0."%(config_obj.rate_card_id.name, address.country_id.name)))
135
elif config_obj.calc_method == 'state_zone_weight':
136
zone_pool = self.pool.get('zone.map')
137
state_id = address.state_id.id
138
zone_ids = zone_pool.search(cr, uid, [('rate_config_id', '=', config_obj.id),('state_id', '=', state_id)], context=context)
140
zone = zone_pool.read(cr, uid, zone_ids, ['zone'], context=context)[0]['zone']
141
table_ids = table_pool.search(cr, uid, [('card_id', '=', rate_card_id), ('zone', '=', zone)], context=context)
143
table_obj = table_pool.browse(cr, uid, table_ids, context=context)[0]
144
weight = model_obj.total_weight_net
145
if table_obj.charge > 0:
146
cost = table_obj.charge
147
weight -= table_obj.to_weight
149
cost += weight*table_obj.over_cost
151
cost = weight*table_obj.over_cost
153
logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find rate table with Shipping Table = %s and \
154
Zone = %s."%(config_obj.shipmethodname, zone)))
156
logger.notifyChannel(_("Calculate Shipping"), netsvc.LOG_WARNING, _("Unable to find Zone Mapping Table with Shipping Rate \
157
Configuration = %s and State = %s."%(config_obj.shipmethodname, address.state_id.name)))
158
elif config_obj.calc_method == 'manual':
164
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
b'\\ No newline at end of file'