[IMP]delivery,hr_timesheet,hr_holidays:Improvement is Done
[odoo/odoo.git] / addons / delivery / delivery.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23 from osv import fields,osv
24 from tools.translate import _
25
26 class delivery_carrier(osv.osv):
27     _name = "delivery.carrier"
28     _description = "Carrier"
29
30     def name_get(self, cr, uid, ids, context=None):
31         if not len(ids):
32             return []
33         if context is None:
34             context = {}
35         order_id = context.get('order_id',False)
36         if not order_id:
37             res = super(delivery_carrier, self).name_get(cr, uid, ids, context=context)
38         else:
39             order = self.pool.get('sale.order').browse(cr, uid, order_id, context=context)
40             currency = order.pricelist_id.currency_id.name or ''
41             res = [(r['id'], r['name']+' ('+(str(r['price']))+' '+currency+')') for r in self.read(cr, uid, ids, ['name', 'price'], context)]
42         return res
43     def get_price(self, cr, uid, ids, field_name, arg=None, context=None):
44         res={}
45         if context is None:
46             context = {}
47         sale_obj=self.pool.get('sale.order')
48         grid_obj=self.pool.get('delivery.grid')
49         for carrier in self.browse(cr, uid, ids, context=context):
50             order_id=context.get('order_id',False)
51             price=False
52             if order_id:
53               order = sale_obj.browse(cr, uid, order_id, context=context)
54               carrier_grid=self.grid_get(cr,uid,[carrier.id],order.partner_shipping_id.id,context)
55               if carrier_grid:
56                   price=grid_obj.get_price(cr, uid, carrier_grid, order, time.strftime('%Y-%m-%d'), context)
57               else:
58                   price = 0.0
59             res[carrier.id]=price
60         return res
61     _columns = {
62         'name': fields.char('Carrier', size=64, required=True),
63         'partner_id': fields.many2one('res.partner', 'Carrier Partner', required=True),
64         'product_id': fields.many2one('product.product', 'Delivery Product', required=True),
65         'grids_id': fields.one2many('delivery.grid', 'carrier_id', 'Delivery Grids'),
66         'price' : fields.function(get_price, method=True,string='Price'),
67         'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the delivery carrier without removing it."),
68         'normal_price': fields.float('Normal Price'),
69         'free_if_more_than': fields.boolean('Free If More Than'),
70         'amount': fields.float('Amount'),
71         'use_detailed_pricelist': fields.boolean('Use Detailed Pricelist'),
72         'pricelist_ids': fields.one2many('delivery.grid', 'carrier_id', 'Price List'),
73
74     }
75
76     _defaults = {
77         'active': lambda *args:1,
78         'free_if_more_than': lambda *args: False
79     }
80
81     def grid_get(self, cr, uid, ids, contact_id, context=None):
82         contact = self.pool.get('res.partner.address').browse(cr, uid, contact_id, context=context)
83         for carrier in self.browse(cr, uid, ids, context=context):
84             for grid in carrier.grids_id:
85                 get_id = lambda x: x.id
86                 country_ids = map(get_id, grid.country_ids)
87                 state_ids = map(get_id, grid.state_ids)
88                 if country_ids and not contact.country_id.id in country_ids:
89                     continue
90                 if state_ids and not contact.state_id.id in state_ids:
91                     continue
92                 if grid.zip_from and (contact.zip or '')< grid.zip_from:
93                     continue
94                 if grid.zip_to and (contact.zip or '')> grid.zip_to:
95                     continue
96                 return grid.id
97         return False
98
99     def create_grid_lines(self, cr, uid, ids, vals, context=None):
100         if context == None:
101             context = {}
102         grid_line_pool = self.pool.get('delivery.grid.line')
103         grid_pool = self.pool.get('delivery.grid')
104         for record in self.browse(cr, uid, ids, context=context):
105             grid_id = grid_pool.search(cr, uid, [('carrier_id', '=', record.id)], context=context)
106             if not grid_id:
107                 record_data = {
108                     'name': vals.get('name', False),
109                     'carrier_id': record.id,
110                     'seqeunce': 10,
111                 }
112                 new_grid_id = grid_pool.create(cr, uid, record_data, context=context)
113                 grid_id = [new_grid_id]
114
115             if record.free_if_more_than:
116                 grid_lines = []
117                 for line in grid_pool.browse(cr, uid, grid_id[0]).line_ids:
118                     if line.type == 'price':
119                         grid_lines.append(line.id)
120                 grid_line_pool.unlink(cr, uid, grid_lines, context=context)
121                 data = {
122                     'grid_id': grid_id and grid_id[0],
123                     'name': _('Free if more than %d') % record.amount,
124                     'type': 'price',
125                     'operator': '>=',
126                     'max_value': record.amount,
127                     'standard_price': 0.0,
128                     'list_price': 0.0,
129                 }
130                 grid_line_pool.create(cr, uid, data, context=context)
131             else:
132                 _lines = []
133                 for line in grid_pool.browse(cr, uid, grid_id[0], context=context).line_ids:
134                     if line.type == 'price':
135                         _lines.append(line.id)
136                 grid_line_pool.unlink(cr, uid, _lines, context=context)
137
138             if record.normal_price:
139                 default_data = {
140                     'grid_id': grid_id and grid_id[0],
141                     'name': _('Default price'),
142                     'type': 'price',
143                     'operator': '>=',
144                     'max_value': 0.0,
145                     'standard_price': record.normal_price,
146                     'list_price': record.normal_price,
147                 }
148                 grid_line_pool.create(cr, uid, default_data, context=context)
149
150         return True
151
152     def write(self, cr, uid, ids, vals, context=None):
153         if context == None:
154             context = {}
155         res_id = super(delivery_carrier, self).write(cr, uid, ids, vals, context=context)
156         self.create_grid_lines(cr, uid, ids, vals, context=context)
157         return res_id
158
159     def create(self, cr, uid, vals, context=None):
160         if context == None:
161             context = {}
162         res_id = super(delivery_carrier, self).create(cr, uid, vals, context=context)
163         self.create_grid_lines(cr, uid, [res_id], vals, context=context)
164         return res_id
165
166 delivery_carrier()
167
168 class delivery_grid(osv.osv):
169     _name = "delivery.grid"
170     _description = "Delivery Grid"
171     _columns = {
172         'name': fields.char('Grid Name', size=64, required=True),
173         'sequence': fields.integer('Sequence', size=64, required=True, help="Gives the sequence order when displaying a list of delivery grid."),
174         'carrier_id': fields.many2one('delivery.carrier', 'Carrier', required=True, ondelete='cascade'),
175         'country_ids': fields.many2many('res.country', 'delivery_grid_country_rel', 'grid_id', 'country_id', 'Countries'),
176         'state_ids': fields.many2many('res.country.state', 'delivery_grid_state_rel', 'grid_id', 'state_id', 'States'),
177         'zip_from': fields.char('Start Zip', size=12),
178         'zip_to': fields.char('To Zip', size=12),
179         'line_ids': fields.one2many('delivery.grid.line', 'grid_id', 'Grid Line'),
180         'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the delivery grid without removing it."),
181     }
182     _defaults = {
183         'active': lambda *a: 1,
184         'sequence': lambda *a: 1,
185     }
186     _order = 'sequence'
187
188     def get_price(self, cr, uid, id, order, dt, context=None):
189         total = 0
190         weight = 0
191         volume = 0
192         for line in order.order_line:
193             if not line.product_id:
194                 continue
195             total += line.price_subtotal or 0.0
196             weight += (line.product_id.weight or 0.0) * line.product_uom_qty
197             volume += (line.product_id.volume or 0.0) * line.product_uom_qty
198
199
200         return self.get_price_from_picking(cr, uid, id, total,weight, volume, context=context)
201
202     def get_price_from_picking(self, cr, uid, id, total, weight, volume, context=None):
203         grid = self.browse(cr, uid, id, context=context)
204         price = 0.0
205         ok = False
206
207         for line in grid.line_ids:
208             price_dict = {'price': total, 'volume':volume, 'weight': weight, 'wv':volume*weight}
209             test = eval(line.type+line.operator+str(line.max_value), price_dict)
210             if test:
211                 if line.price_type=='variable':
212                     price = line.list_price * price_dict[line.variable_factor]
213                 else:
214                     price = line.list_price
215                 ok = True
216                 break
217         if not ok:
218             raise osv.except_osv(_('No price available !'), _('No line matched this order in the choosed delivery grids !'))
219
220         return price
221
222
223 delivery_grid()
224
225 class delivery_grid_line(osv.osv):
226     _name = "delivery.grid.line"
227     _description = "Delivery Grid Line"
228     _columns = {
229         'name': fields.char('Name', size=32, required=True),
230         'grid_id': fields.many2one('delivery.grid', 'Grid',required=True),
231         'type': fields.selection([('weight','Weight'),('volume','Volume'),\
232                                   ('wv','Weight * Volume'), ('price','Price')],\
233                                   'Variable', required=True),
234         'operator': fields.selection([('==','='),('<=','<='),('>=','>=')], 'Operator', required=True),
235         'max_value': fields.float('Maximum Value', required=True),
236         'price_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Price Type', required=True),
237         'variable_factor': fields.selection([('weight','Weight'),('volume','Volume'),('wv','Weight * Volume'), ('price','Price')], 'Variable Factor', required=True),
238         'list_price': fields.float('Sale Price', required=True),
239         'standard_price': fields.float('Cost Price', required=True),
240     }
241     _defaults = {
242         'type': lambda *args: 'weight',
243         'operator': lambda *args: '<=',
244         'price_type': lambda *args: 'fixed',
245         'variable_factor': lambda *args: 'weight',
246     }
247     _order = 'list_price'
248
249 delivery_grid_line()
250
251
252 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
253