~serpent-consulting-services/openerp-usa/fix-shipping_api_ups_cc

« back to all changes in this revision

Viewing changes to sale_negotiated_shipping/sale_negotiated_shipping.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
 
 
23
 
from osv import fields, osv
24
 
import netsvc
25
 
from tools.translate import _
26
 
 
27
 
class shipping_rate_card(osv.osv):
28
 
    _name = 'shipping.rate.card'
29
 
    _description = "Ground Shipping Calculation Table"
30
 
    _columns = {
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),
35
 
        }
36
 
    
37
 
shipping_rate_card()
38
 
 
39
 
class shipping_rate_config(osv.osv):
40
 
    _name = 'shipping.rate.config'
41
 
    _description = "Configuration for shipping rate"
42
 
    _rec_name = 'shipmethodname'
43
 
    
44
 
    _columns = {
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')
59
 
        }
60
 
    _defaults = {
61
 
        'calc_method': 'country_weight'
62
 
        }
63
 
 
64
 
shipping_rate_config()
65
 
 
66
 
class zone_map(osv.osv):
67
 
    _name = 'zone.map'
68
 
    _description = "Zone Mapping Table"
69
 
    _rec_name = 'zone'
70
 
    _columns = {
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')
74
 
        }
75
 
    
76
 
zone_map()
77
 
 
78
 
class shipping_rate(osv.osv):
79
 
    _name = 'shipping.rate'
80
 
    _description = "Shipping Calculation Table"
81
 
    _columns = {
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')
90
 
        }
91
 
    
92
 
    def find_cost(self, cr, uid, config_id, address, model_obj, context=None):
93
 
        """
94
 
        Function to calculate shipping cost
95
 
        """
96
 
        cost = 0
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)
107
 
            if table_ids:
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
111
 
                else:
112
 
                    cost = table_obj.charge
113
 
                    
114
 
            else:
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)
117
 
                if table_ids:
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:
122
 
                            table_obj = table
123
 
                    weight = model_obj.total_weight_net
124
 
                    if table_obj.charge > 0:
125
 
                        cost = table_obj.charge
126
 
                        weight -= table_obj.from_weight
127
 
                        if weight>0:
128
 
                            cost += weight * table_obj.over_cost
129
 
                    else:
130
 
                        cost = weight * table_obj.over_cost
131
 
                else:
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)))
134
 
                
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)
139
 
            if zone_ids:
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)
142
 
                if table_ids:
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
148
 
                        if weight > 0:
149
 
                            cost += weight*table_obj.over_cost
150
 
                    else:
151
 
                        cost = weight*table_obj.over_cost
152
 
                else:
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)))
155
 
            else:
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':
159
 
            cost = 0.0
160
 
        return cost
161
 
    
162
 
shipping_rate()
163
 
 
164
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'