[WIP] split stock module in stock and stock_account that installs itself only if...
[odoo/odoo.git] / addons / stock / product.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 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
24 import openerp.addons.decimal_precision as dp
25
26 class product_product(osv.osv):
27     _inherit = "product.product"
28
29     def _stock_move_count(self, cr, uid, ids, field_name, arg, context=None):
30         res = dict([(id, {'reception_count': 0, 'delivery_count': 0}) for id in ids])
31         move_pool=self.pool.get('stock.move')
32         moves = move_pool.read_group(cr, uid, [
33             ('product_id', 'in', ids),
34             ('picking_id.type', '=', 'in'),
35             ('state','in',('confirmed','assigned','pending'))
36         ], ['product_id'], ['product_id'])
37         for move in moves:
38             product_id = move['product_id'][0]
39             res[product_id]['reception_count'] = move['product_id_count']
40         moves = move_pool.read_group(cr, uid, [
41             ('product_id', 'in', ids),
42             ('picking_id.type', '=', 'out'),
43             ('state','in',('confirmed','assigned','pending'))
44         ], ['product_id'], ['product_id'])
45         for move in moves:
46             product_id = move['product_id'][0]
47             res[product_id]['delivery_count'] = move['product_id_count']
48         return res
49
50     def view_header_get(self, cr, user, view_id, view_type, context=None):
51         if context is None:
52             context = {}
53         res = super(product_product, self).view_header_get(cr, user, view_id, view_type, context)
54         if res: return res
55         if (context.get('active_id', False)) and (context.get('active_model') == 'stock.location'):
56             return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['active_id'], context).name
57         return res
58
59     def _get_domain_locations(self, cr, uid, ids, context=None):
60         '''
61         Parses the context and returns a list of location_ids based on it.
62         It will return all stock locations when no parameters are given
63         Possible parameters are shop, warehouse, location, force_company, compute_child
64         '''
65         context = context or {}
66
67         location_obj = self.pool.get('stock.location')
68         warehouse_obj = self.pool.get('stock.warehouse')
69
70         location_ids = []
71         if context.get('location', False):
72             if type(context['location']) == type(1):
73                 location_ids = [context['location']]
74             elif type(context['location']) in (type(''), type(u'')):
75                 domain = [('name','ilike',context['location'])]
76                 if context.get('force_company', False):
77                     domain += [('company_id', '=', context['force_company'])]
78                 location_ids = location_obj.search(cr, uid, domain, context=context)
79             else:
80                 location_ids = context['location']
81         else:
82             if context.get('warehouse', False):
83                 wh = warehouse_obj.browse(cr, uid, [context['warehouse']], context=context)
84             else:
85                 wids = warehouse_obj.search(cr, uid, [], context=context)
86                 wh = warehouse_obj.browse(cr, uid, wids, context=context)
87
88             for w in warehouse_obj.browse(cr, uid, wids, context=context):
89                 location_ids.append(w.lot_stock_id.id)
90
91         operator = context.get('compute_child',True) and 'child_of' or 'in'
92         domain = context.get('force_company', False) and ['&', ('company_id', '=', context['force_company'])] or []
93         domain += [('product_id', 'in', ids)]
94         return (
95             domain + [('location_id', operator, location_ids)],
96             domain + ['&', ('location_dest_id', operator, location_ids), '!', ('location_id', operator, location_ids)],
97             domain + ['&', ('location_id', operator, location_ids), '!', ('location_dest_id', operator, location_ids)]
98         )
99
100     def _get_domain_dates(self, cr, uid, ids, context):
101         from_date = context.get('from_date',False)
102         to_date = context.get('to_date',False)
103         domain = []
104         if from_date:
105             domain.append(('date','>=',from_date))
106         if to_date:
107             domain.append(('date','<=',to_date))
108         return domain
109
110     def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None):
111         context = context or {}
112         field_names = field_names or []
113
114         domain_quant, domain_move_in, domain_move_out = self._get_domain_locations(cr, uid, ids, context=context)
115         domain_move_in += self._get_domain_dates(cr, uid, ids, context=context) + [('state','not in',('done','cancel'))]
116         domain_move_out += self._get_domain_dates(cr, uid, ids, context=context) + [('state','not in',('done','cancel'))]
117
118         if context.get('lot_id', False):
119             domain_quants.append(('lot_id','=',context['lot_id']))
120             moves_in  = []
121             moves_out = []
122         else:
123             moves_in  = self.pool.get('stock.move').read_group(cr, uid, domain_move_in, ['product_id', 'product_qty'], ['product_id'], context=context)
124             moves_out = self.pool.get('stock.move').read_group(cr, uid, domain_move_out, ['product_id', 'product_qty'], ['product_id'], context=context)
125
126         quants = self.pool.get('stock.quant').read_group(cr, uid, domain_quant, ['product_id', 'qty'], ['product_id'], context=context)
127
128         quants = dict(map(lambda x: (x['product_id'][0], x['qty']), quants))
129         moves_in = dict(map(lambda x: (x['product_id'][0], x['product_qty']), moves_in))
130         moves_out = dict(map(lambda x: (x['product_id'][0], x['product_qty']), moves_out))
131
132         res = {}
133         for id in ids:
134             res[id] = {
135                 'qty_available': quants.get(id, 0.0),
136                 'incoming_qty': moves_in.get(id, 0.0),
137                 'outgoing_qty': moves_out.get(id, 0.0),
138                 'virtual_available': quants.get(id, 0.0) + moves_in.get(id, 0.0) - moves_out.get(id, 0.0),
139             }
140         return res
141
142     _columns = {
143         'reception_count': fields.function(_stock_move_count, string="Reception", type='integer', multi='pickings'),
144         'delivery_count': fields.function(_stock_move_count, string="Delivery", type='integer', multi='pickings'),
145         'qty_available': fields.function(_product_available, multi='qty_available',
146             type='float',  digits_compute=dp.get_precision('Product Unit of Measure'),
147             string='Quantity On Hand',
148             help="Current quantity of products.\n"
149                  "In a context with a single Stock Location, this includes "
150                  "goods stored at this Location, or any of its children.\n"
151                  "In a context with a single Warehouse, this includes "
152                  "goods stored in the Stock Location of this Warehouse, or any "
153                  "of its children.\n"
154                  "stored in the Stock Location of the Warehouse of this Shop, "
155                  "or any of its children.\n"
156                  "Otherwise, this includes goods stored in any Stock Location "
157                  "with 'internal' type."),
158         'virtual_available': fields.function(_product_available, multi='qty_available',
159             type='float',  digits_compute=dp.get_precision('Product Unit of Measure'),
160             string='Forecasted Quantity',
161             help="Forecast quantity (computed as Quantity On Hand "
162                  "- Outgoing + Incoming)\n"
163                  "In a context with a single Stock Location, this includes "
164                  "goods stored in this location, or any of its children.\n"
165                  "In a context with a single Warehouse, this includes "
166                  "goods stored in the Stock Location of this Warehouse, or any "
167                  "of its children.\n"
168                  "stored in the Stock Location of the Warehouse of this Shop, "
169                  "or any of its children.\n"
170                  "Otherwise, this includes goods stored in any Stock Location "
171                  "with 'internal' type."),
172         'incoming_qty': fields.function(_product_available, multi='qty_available',
173             type='float',  digits_compute=dp.get_precision('Product Unit of Measure'),
174             string='Incoming',
175             help="Quantity of products that are planned to arrive.\n"
176                  "In a context with a single Stock Location, this includes "
177                  "goods arriving to this Location, or any of its children.\n"
178                  "In a context with a single Warehouse, this includes "
179                  "goods arriving to the Stock Location of this Warehouse, or "
180                  "any of its children.\n"
181                  "In a context with a single Shop, this includes goods "
182                  "arriving to the Stock Location of the Warehouse of this "
183                  "Shop, or any of its children.\n"
184                  "Otherwise, this includes goods arriving to any Stock "
185                  "Location with 'internal' type."),
186         'outgoing_qty': fields.function(_product_available, multi='qty_available',
187             type='float',  digits_compute=dp.get_precision('Product Unit of Measure'),
188             string='Outgoing',
189             help="Quantity of products that are planned to leave.\n"
190                  "In a context with a single Stock Location, this includes "
191                  "goods leaving this Location, or any of its children.\n"
192                  "In a context with a single Warehouse, this includes "
193                  "goods leaving the Stock Location of this Warehouse, or "
194                  "any of its children.\n"
195                  "In a context with a single Shop, this includes goods "
196                  "leaving the Stock Location of the Warehouse of this "
197                  "Shop, or any of its children.\n"
198                  "Otherwise, this includes goods leaving any Stock "
199                  "Location with 'internal' type."),
200         'track_production': fields.boolean('Track Manufacturing Lots', help="Forces to specify a Serial Number for all moves containing this product and generated by a Manufacturing Order"),
201         'track_incoming': fields.boolean('Track Incoming Lots', help="Forces to specify a Serial Number for all moves containing this product and coming from a Supplier Location"),
202         'track_outgoing': fields.boolean('Track Outgoing Lots', help="Forces to specify a Serial Number for all moves containing this product and going to a Customer Location"),
203         'location_id': fields.dummy(string='Location', relation='stock.location', type='many2one'),
204         'warehouse_id': fields.dummy(string='Warehouse', relation='stock.warehouse', type='many2one'),
205     }
206
207     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
208         res = super(product_product,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
209         if context is None:
210             context = {}
211         if ('location' in context) and context['location']:
212             location_info = self.pool.get('stock.location').browse(cr, uid, context['location'])
213             fields=res.get('fields',{})
214             if fields:
215                 if location_info.usage == 'supplier':
216                     if fields.get('virtual_available'):
217                         res['fields']['virtual_available']['string'] = _('Future Receptions')
218                     if fields.get('qty_available'):
219                         res['fields']['qty_available']['string'] = _('Received Qty')
220
221                 if location_info.usage == 'internal':
222                     if fields.get('virtual_available'):
223                         res['fields']['virtual_available']['string'] = _('Future Stock')
224
225                 if location_info.usage == 'customer':
226                     if fields.get('virtual_available'):
227                         res['fields']['virtual_available']['string'] = _('Future Deliveries')
228                     if fields.get('qty_available'):
229                         res['fields']['qty_available']['string'] = _('Delivered Qty')
230
231                 if location_info.usage == 'inventory':
232                     if fields.get('virtual_available'):
233                         res['fields']['virtual_available']['string'] = _('Future P&L')
234                     if fields.get('qty_available'):
235                         res['fields']['qty_available']['string'] = _('P&L Qty')
236
237                 if location_info.usage == 'procurement':
238                     if fields.get('virtual_available'):
239                         res['fields']['virtual_available']['string'] = _('Future Qty')
240                     if fields.get('qty_available'):
241                         res['fields']['qty_available']['string'] = _('Unplanned Qty')
242
243                 if location_info.usage == 'production':
244                     if fields.get('virtual_available'):
245                         res['fields']['virtual_available']['string'] = _('Future Productions')
246                     if fields.get('qty_available'):
247                         res['fields']['qty_available']['string'] = _('Produced Qty')
248         return res
249
250
251 class product_template(osv.osv):
252     _name = 'product.template'
253     _inherit = 'product.template'
254     _columns = {
255         'property_stock_procurement': fields.property(
256             type='many2one',
257             relation='stock.location',
258             string="Procurement Location",
259             domain=[('usage','like','procurement')],
260             help="This stock location will be used, instead of the default one, as the source location for stock moves generated by procurements."),
261         'property_stock_production': fields.property(
262             type='many2one',
263             relation='stock.location',
264             string="Production Location",
265             domain=[('usage','like','production')],
266             help="This stock location will be used, instead of the default one, as the source location for stock moves generated by manufacturing orders."),
267         'property_stock_inventory': fields.property(
268             type='many2one',
269             relation='stock.location',
270             string="Inventory Location",
271             domain=[('usage','like','inventory')],
272             help="This stock location will be used, instead of the default one, as the source location for stock moves generated when you do an inventory."),
273         'sale_delay': fields.float('Customer Lead Time', help="The average delay in days between the confirmation of the customer order and the delivery of the finished products. It's the time you promise to your customers."),
274         'loc_rack': fields.char('Rack', size=16),
275         'loc_row': fields.char('Row', size=16),
276         'loc_case': fields.char('Case', size=16),
277     }
278
279     _defaults = {
280         'sale_delay': 7,
281     }
282
283
284 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: