[IMP]:mrp:Improved Cost Structure with set title.
[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={}):
31         if not len(ids):
32             return []
33         order_id = context.get('order_id',False)
34         if not order_id:
35             res = super(delivery_carrier, self).name_get(cr, uid, ids, context=context)
36         else:
37             order = self.pool.get('sale.order').browse(cr, uid, [order_id])[0]
38             currency = order.pricelist_id.currency_id.name or ''
39             res = [(r['id'], r['name']+' ('+(str(r['price']))+' '+currency+')') for r in self.read(cr, uid, ids, ['name', 'price'], context)]
40         return res
41     def get_price(self, cr, uid, ids, field_name, arg=None, context={}):
42         res={}
43         sale_obj=self.pool.get('sale.order')
44         grid_obj=self.pool.get('delivery.grid')
45         for carrier in self.browse(cr,uid,ids,context):
46             order_id=context.get('order_id',False)
47             price=False
48             if order_id:
49               order = sale_obj.browse(cr, uid, [order_id])[0]
50               carrier_grid=self.grid_get(cr,uid,[carrier.id],order.partner_shipping_id.id,context)
51               if carrier_grid:
52                   price=grid_obj.get_price(cr, uid, carrier_grid, order, time.strftime('%Y-%m-%d'), context)
53               else:
54                   price = 0.0
55             res[carrier.id]=price
56         return res
57     _columns = {
58         'name': fields.char('Carrier', size=64, required=True),
59         'partner_id': fields.many2one('res.partner', 'Carrier Partner', required=True),
60         'product_id': fields.many2one('product.product', 'Delivery Product', required=True),
61         'grids_id': fields.one2many('delivery.grid', 'carrier_id', 'Delivery Grids'),
62         'price' : fields.function(get_price, method=True,string='Price'),
63         'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the delivery carrier without removing it.")
64     }
65     _defaults = {
66         'active': lambda *args:1
67     }
68     def grid_get(self, cr, uid, ids, contact_id, context={}):
69         contact = self.pool.get('res.partner.address').browse(cr, uid, [contact_id])[0]
70         for carrier in self.browse(cr, uid, ids):
71             for grid in carrier.grids_id:
72                 get_id = lambda x: x.id
73                 country_ids = map(get_id, grid.country_ids)
74                 state_ids = map(get_id, grid.state_ids)
75                 if country_ids and not contact.country_id.id in country_ids:
76                     continue
77                 if state_ids and not contact.state_id.id in state_ids:
78                     continue
79                 if grid.zip_from and (contact.zip or '')< grid.zip_from:
80                     continue
81                 if grid.zip_to and (contact.zip or '')> grid.zip_to:
82                     continue
83                 return grid.id
84         return False
85 delivery_carrier()
86
87 class delivery_grid(osv.osv):
88     _name = "delivery.grid"
89     _description = "Delivery Grid"
90     _columns = {
91         'name': fields.char('Grid Name', size=64, required=True),
92         'sequence': fields.integer('Sequence', size=64, required=True, help="Gives the sequence order when displaying a list of delivery grid."),
93         'carrier_id': fields.many2one('delivery.carrier', 'Carrier', required=True, ondelete='cascade'),
94         'country_ids': fields.many2many('res.country', 'delivery_grid_country_rel', 'grid_id', 'country_id', 'Countries'),
95         'state_ids': fields.many2many('res.country.state', 'delivery_grid_state_rel', 'grid_id', 'state_id', 'States'),
96         'zip_from': fields.char('Start Zip', size=12),
97         'zip_to': fields.char('To Zip', size=12),
98         'line_ids': fields.one2many('delivery.grid.line', 'grid_id', 'Grid Line'),
99         'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the delivery grid without removing it."),
100     }
101     _defaults = {
102         'active': lambda *a: 1,
103         'sequence': lambda *a: 1,
104     }
105     _order = 'sequence'
106
107     def get_price(self, cr, uid, id, order, dt, context):
108         total = 0
109         weight = 0
110         volume = 0
111         for line in order.order_line:
112             if not line.product_id:
113                 continue
114             total += line.price_subtotal or 0.0
115             weight += (line.product_id.weight or 0.0) * line.product_uom_qty
116             volume += (line.product_id.volume or 0.0) * line.product_uom_qty
117
118
119         return self.get_price_from_picking(cr, uid, id, total,weight, volume, context)
120
121     def get_price_from_picking(self, cr, uid, id, total, weight, volume, context={}):
122         grid = self.browse(cr, uid, id, context)
123         price = 0.0
124         ok = False
125
126         for line in grid.line_ids:
127             price_dict = {'price': total, 'volume':volume, 'weight': weight, 'wv':volume*weight}
128             test = eval(line.type+line.operator+str(line.max_value), price_dict)
129             if test:
130                 if line.price_type=='variable':
131                     price = line.list_price * price_dict[line.variable_factor]
132                 else:
133                     price = line.list_price
134                 ok = True
135                 break
136         if not ok:
137             raise osv.except_osv(_('No price available !'), _('No line matched this order in the choosed delivery grids !'))
138
139         return price
140
141
142 delivery_grid()
143
144 class delivery_grid_line(osv.osv):
145     _name = "delivery.grid.line"
146     _description = "Delivery Grid Line"
147     _columns = {
148         'name': fields.char('Name', size=32, required=True),
149         'grid_id': fields.many2one('delivery.grid', 'Grid',required=True),
150         'type': fields.selection([('weight','Weight'),('volume','Volume'),('wv','Weight * Volume'), ('price','Price')], 'Variable', required=True),
151         'operator': fields.selection([('==','='),('<=','<='),('>=','>=')], 'Operator', required=True),
152         'max_value': fields.float('Maximum Value', required=True),
153         'price_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Price Type', required=True),
154         'variable_factor': fields.selection([('weight','Weight'),('volume','Volume'),('wv','Weight * Volume'), ('price','Price')], 'Variable Factor', required=True),
155         'list_price': fields.float('Sale Price', required=True),
156         'standard_price': fields.float('Cost Price', required=True),
157     }
158     _defaults = {
159         'type': lambda *args: 'weight',
160         'operator': lambda *args: '<=',
161         'price_type': lambda *args: 'fixed',
162         'variable_factor': lambda *args: 'weight',
163     }
164     _order = 'list_price'
165
166
167 delivery_grid_line()
168
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
171