0dd44afdbda147ac0d6b3f1a5a80f9c59e2a2be7
[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 from openerp.tools.safe_eval import safe_eval as eval
25 import openerp.addons.decimal_precision as dp
26
27 class product_product(osv.osv):
28     _inherit = "product.product"
29         
30     def _stock_move_count(self, cr, uid, ids, field_name, arg, context=None):
31         res = dict([(id, {'reception_count': 0, 'delivery_count': 0}) for id in ids])
32         move_pool=self.pool.get('stock.move')
33         moves = move_pool.read_group(cr, uid, [
34             ('product_id', 'in', ids),
35             ('location_id.usage', '!=', 'internal'),
36             ('location_dest_id.usage', '=', 'internal'),
37             ('state','in',('confirmed','assigned','pending'))
38         ], ['product_id'], ['product_id'])
39         for move in moves:
40             product_id = move['product_id'][0]
41             res[product_id]['reception_count'] = move['product_id_count']
42         moves = move_pool.read_group(cr, uid, [
43             ('product_id', 'in', ids),
44             ('location_id.usage', '=', 'internal'),
45             ('location_dest_id.usage', '!=', 'internal'),
46             ('state','in',('confirmed','assigned','pending'))
47         ], ['product_id'], ['product_id'])
48         for move in moves:
49             product_id = move['product_id'][0]
50             res[product_id]['delivery_count'] = move['product_id_count']
51         return res
52
53     def view_header_get(self, cr, user, view_id, view_type, context=None):
54         if context is None:
55             context = {}
56         res = super(product_product, self).view_header_get(cr, user, view_id, view_type, context)
57         if res: return res
58         if (context.get('active_id', False)) and (context.get('active_model') == 'stock.location'):
59             return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['active_id'], context).name
60         return res
61
62     def _get_domain_locations(self, cr, uid, ids, context=None):
63         '''
64         Parses the context and returns a list of location_ids based on it.
65         It will return all stock locations when no parameters are given
66         Possible parameters are shop, warehouse, location, force_company, compute_child
67         '''
68         context = context or {}
69
70         location_obj = self.pool.get('stock.location')
71         warehouse_obj = self.pool.get('stock.warehouse')
72
73         location_ids = []
74         if context.get('location', False):
75             if type(context['location']) == type(1):
76                 location_ids = [context['location']]
77             elif type(context['location']) in (type(''), type(u'')):
78                 domain = [('complete_name','ilike',context['location'])]
79                 if context.get('force_company', False):
80                     domain += [('company_id', '=', context['force_company'])]
81                 location_ids = location_obj.search(cr, uid, domain, context=context)
82             else:
83                 location_ids = context['location']
84         else:
85             if context.get('warehouse', False):
86                 wids = [context['warehouse']]
87             else:
88                 wids = warehouse_obj.search(cr, uid, [], context=context)
89
90             for w in warehouse_obj.browse(cr, uid, wids, context=context):
91                 location_ids.append(w.view_location_id.id)
92
93         operator = context.get('compute_child', True) and 'child_of' or 'in'
94         domain = context.get('force_company', False) and ['&', ('company_id', '=', context['force_company'])] or []
95         return (
96             domain + [('location_id', operator, location_ids)],
97             domain + ['&', ('location_dest_id', operator, location_ids), '!', ('location_id', operator, location_ids)],
98             domain + ['&', ('location_id', operator, location_ids), '!', ('location_dest_id', operator, location_ids)]
99         )
100
101     def _get_domain_dates(self, cr, uid, ids, context):
102         from_date = context.get('from_date', False)
103         to_date = context.get('to_date', False)
104         domain = []
105         if from_date:
106             domain.append(('date', '>=', from_date))
107         if to_date:
108             domain.append(('date', '<=', to_date))
109         return domain
110
111     def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None):
112         context = context or {}
113         field_names = field_names or []
114
115         domain_products = [('product_id', 'in', ids)]
116         domain_quant, domain_move_in, domain_move_out = self._get_domain_locations(cr, uid, ids, context=context)
117         domain_move_in += self._get_domain_dates(cr, uid, ids, context=context) + [('state', 'not in', ('done', 'cancel'))] + domain_products
118         domain_move_out += self._get_domain_dates(cr, uid, ids, context=context) + [('state', 'not in', ('done', 'cancel'))] + domain_products
119         domain_quant += domain_products
120         if context.get('lot_id') or context.get('owner_id') or context.get('package_id'):
121             if context.get('lot_id'):
122                 domain_quant.append(('lot_id', '=', context['lot_id']))
123             if context.get('owner_id'):
124                 domain_quant.append(('owner_id', '=', context['owner_id']))
125             if context.get('package_id'):
126                 domain_quant.append(('package_id', '=', context['package_id']))
127             moves_in = []
128             moves_out = []
129         else:
130             moves_in = self.pool.get('stock.move').read_group(cr, uid, domain_move_in, ['product_id', 'product_qty'], ['product_id'], context=context)
131             moves_out = self.pool.get('stock.move').read_group(cr, uid, domain_move_out, ['product_id', 'product_qty'], ['product_id'], context=context)
132
133         quants = self.pool.get('stock.quant').read_group(cr, uid, domain_quant, ['product_id', 'qty'], ['product_id'], context=context)
134         quants = dict(map(lambda x: (x['product_id'][0], x['qty']), quants))
135
136         moves_in = dict(map(lambda x: (x['product_id'][0], x['product_qty']), moves_in))
137         moves_out = dict(map(lambda x: (x['product_id'][0], x['product_qty']), moves_out))
138         res = {}
139         for id in ids:
140             res[id] = {
141                 'qty_available': quants.get(id, 0.0),
142                 'incoming_qty': moves_in.get(id, 0.0),
143                 'outgoing_qty': moves_out.get(id, 0.0),
144                 'virtual_available': quants.get(id, 0.0) + moves_in.get(id, 0.0) - moves_out.get(id, 0.0),
145             }
146
147         return res
148
149     def _search_product_quantity(self, cr, uid, obj, name, domain, context):
150         res = []
151         for field, operator, value in domain:
152             #to prevent sql injections
153             assert field in ('qty_available', 'virtual_available', 'incoming_qty', 'outgoing_qty'), 'Invalid domain left operand'
154             assert operator in ('<', '>', '=', '!=', '<=', '>='), 'Invalid domain operator'
155             assert isinstance(value, (float, int)), 'Invalid domain right operand'
156
157             if operator == '=':
158                 operator = '=='
159
160             product_ids = self.search(cr, uid, [], context=context)
161             ids = []
162             if product_ids:
163                 #TODO: use a query instead of this browse record which is probably making the too much requests, but don't forget
164                 #the context that can be set with a location, an owner...
165                 for element in self.browse(cr, uid, product_ids, context=context):
166                     if eval(str(element[field]) + operator + str(value)):
167                         ids.append(element.id)
168             res.append(('id', 'in', ids))
169         return res
170
171     def _product_available_text(self, cr, uid, ids, field_names=None, arg=False, context=None):
172         res = {}
173         for product in self.browse(cr, uid, ids, context=context):
174             res[product.id] = str(product.qty_available) +  _(" On Hand")
175         return res
176
177     _columns = {
178         'reception_count': fields.function(_stock_move_count, string="Receipt", type='integer', multi='pickings'),
179         'delivery_count': fields.function(_stock_move_count, string="Delivery", type='integer', multi='pickings'),
180         'qty_available_text': fields.function(_product_available_text, type='char'),
181         'qty_available': fields.function(_product_available, multi='qty_available',
182             type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
183             string='Quantity On Hand',
184             fnct_search=_search_product_quantity,
185             help="Current quantity of products.\n"
186                  "In a context with a single Stock Location, this includes "
187                  "goods stored at this Location, or any of its children.\n"
188                  "In a context with a single Warehouse, this includes "
189                  "goods stored in the Stock Location of this Warehouse, or any "
190                  "of its children.\n"
191                  "stored in the Stock Location of the Warehouse of this Shop, "
192                  "or any of its children.\n"
193                  "Otherwise, this includes goods stored in any Stock Location "
194                  "with 'internal' type."),
195         'virtual_available': fields.function(_product_available, multi='qty_available',
196             type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
197             string='Forecast Quantity',
198             fnct_search=_search_product_quantity,
199             help="Forecast quantity (computed as Quantity On Hand "
200                  "- Outgoing + Incoming)\n"
201                  "In a context with a single Stock Location, this includes "
202                  "goods stored in this location, or any of its children.\n"
203                  "In a context with a single Warehouse, this includes "
204                  "goods stored in the Stock Location of this Warehouse, or any "
205                  "of its children.\n"
206                  "Otherwise, this includes goods stored in any Stock Location "
207                  "with 'internal' type."),
208         'incoming_qty': fields.function(_product_available, multi='qty_available',
209             type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
210             string='Incoming',
211             fnct_search=_search_product_quantity,
212             help="Quantity of products that are planned to arrive.\n"
213                  "In a context with a single Stock Location, this includes "
214                  "goods arriving to this Location, or any of its children.\n"
215                  "In a context with a single Warehouse, this includes "
216                  "goods arriving to the Stock Location of this Warehouse, or "
217                  "any of its children.\n"
218                  "Otherwise, this includes goods arriving to any Stock "
219                  "Location with 'internal' type."),
220         'outgoing_qty': fields.function(_product_available, multi='qty_available',
221             type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
222             string='Outgoing',
223             fnct_search=_search_product_quantity,
224             help="Quantity of products that are planned to leave.\n"
225                  "In a context with a single Stock Location, this includes "
226                  "goods leaving this Location, or any of its children.\n"
227                  "In a context with a single Warehouse, this includes "
228                  "goods leaving the Stock Location of this Warehouse, or "
229                  "any of its children.\n"
230                  "Otherwise, this includes goods leaving any Stock "
231                  "Location with 'internal' type."),
232         'location_id': fields.dummy(string='Location', relation='stock.location', type='many2one'),
233         'warehouse_id': fields.dummy(string='Warehouse', relation='stock.warehouse', type='many2one'),
234         'orderpoint_ids': fields.one2many('stock.warehouse.orderpoint', 'product_id', 'Minimum Stock Rules'),
235     }
236
237     _defaults = {
238         # Dirty hack to avoid empty stat button in create view
239         'qty_available_text': lambda *x: '0.0 On Hand',
240     }
241
242     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
243         res = super(product_product,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
244         if context is None:
245             context = {}
246         if ('location' in context) and context['location']:
247             location_info = self.pool.get('stock.location').browse(cr, uid, context['location'])
248             fields=res.get('fields',{})
249             if fields:
250                 if location_info.usage == 'supplier':
251                     if fields.get('virtual_available'):
252                         res['fields']['virtual_available']['string'] = _('Future Receipts')
253                     if fields.get('qty_available'):
254                         res['fields']['qty_available']['string'] = _('Received Qty')
255
256                 if location_info.usage == 'internal':
257                     if fields.get('virtual_available'):
258                         res['fields']['virtual_available']['string'] = _('Future Stock')
259
260                 if location_info.usage == 'customer':
261                     if fields.get('virtual_available'):
262                         res['fields']['virtual_available']['string'] = _('Future Deliveries')
263                     if fields.get('qty_available'):
264                         res['fields']['qty_available']['string'] = _('Delivered Qty')
265
266                 if location_info.usage == 'inventory':
267                     if fields.get('virtual_available'):
268                         res['fields']['virtual_available']['string'] = _('Future P&L')
269                     if fields.get('qty_available'):
270                         res['fields']['qty_available']['string'] = _('P&L Qty')
271
272                 if location_info.usage == 'procurement':
273                     if fields.get('virtual_available'):
274                         res['fields']['virtual_available']['string'] = _('Future Qty')
275                     if fields.get('qty_available'):
276                         res['fields']['qty_available']['string'] = _('Unplanned Qty')
277
278                 if location_info.usage == 'production':
279                     if fields.get('virtual_available'):
280                         res['fields']['virtual_available']['string'] = _('Future Productions')
281                     if fields.get('qty_available'):
282                         res['fields']['qty_available']['string'] = _('Produced Qty')
283         return res
284
285
286     def action_view_routes(self, cr, uid, ids, context=None):
287         template_obj = self.pool.get("product.template")
288         templ_ids = list(set([x.product_tmpl_id.id for x in self.browse(cr, uid, ids, context=context)]))
289         return template_obj.action_view_routes(cr, uid, templ_ids, context=context)
290
291 class product_template(osv.osv):
292     _name = 'product.template'
293     _inherit = 'product.template'
294     
295     def _product_available(self, cr, uid, ids, name, arg, context=None):
296         res = dict.fromkeys(ids, 0)
297         for product in self.browse(cr, uid, ids, context=context):
298             res[product.id] = {
299                 # "reception_count": sum([p.reception_count for p in product.product_variant_ids]),
300                 # "delivery_count": sum([p.delivery_count for p in product.product_variant_ids]),
301                 "qty_available": sum([p.qty_available for p in product.product_variant_ids]),
302                 "virtual_available": sum([p.virtual_available for p in product.product_variant_ids]),
303                 "incoming_qty": sum([p.incoming_qty for p in product.product_variant_ids]),
304                 "outgoing_qty": sum([p.outgoing_qty for p in product.product_variant_ids]),
305             }
306         return res
307
308     def _search_product_quantity(self, cr, uid, obj, name, domain, context):
309         prod = self.pool.get("product.product")
310         res = []
311         for field, operator, value in domain:
312             #to prevent sql injections
313             assert field in ('qty_available', 'virtual_available', 'incoming_qty', 'outgoing_qty'), 'Invalid domain left operand'
314             assert operator in ('<', '>', '=', '!=', '<=', '>='), 'Invalid domain operator'
315             assert isinstance(value, (float, int)), 'Invalid domain right operand'
316
317             if operator == '=':
318                 operator = '=='
319
320             product_ids = prod.search(cr, uid, [], context=context)
321             ids = []
322             if product_ids:
323                 #TODO: use a query instead of this browse record which is probably making the too much requests, but don't forget
324                 #the context that can be set with a location, an owner...
325                 for element in prod.browse(cr, uid, product_ids, context=context):
326                     if eval(str(element[field]) + operator + str(value)):
327                         ids.append(element.id)
328             res.append(('product_variant_ids', 'in', ids))
329         return res
330
331
332     def _product_available_text(self, cr, uid, ids, field_names=None, arg=False, context=None):
333         res = {}
334         for product in self.browse(cr, uid, ids, context=context):
335             res[product.id] = str(product.qty_available) +  _(" On Hand")
336         return res
337
338
339
340     _columns = {
341         'type': fields.selection([('product', 'Stockable Product'), ('consu', 'Consumable'), ('service', 'Service')], 'Product Type', required=True, help="Consumable: Will not imply stock management for this product. \nStockable product: Will imply stock management for this product."),
342         'qty_available_text': fields.function(_product_available_text, type='char'),
343         'property_stock_procurement': fields.property(
344             type='many2one',
345             relation='stock.location',
346             string="Procurement Location",
347             domain=[('usage','like','procurement')],
348             help="This stock location will be used, instead of the default one, as the source location for stock moves generated by procurements."),
349         'property_stock_production': fields.property(
350             type='many2one',
351             relation='stock.location',
352             string="Production Location",
353             domain=[('usage','like','production')],
354             help="This stock location will be used, instead of the default one, as the source location for stock moves generated by manufacturing orders."),
355         'property_stock_inventory': fields.property(
356             type='many2one',
357             relation='stock.location',
358             string="Inventory Location",
359             domain=[('usage','like','inventory')],
360             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."),
361         '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."),
362         'loc_rack': fields.char('Rack', size=16),
363         'loc_row': fields.char('Row', size=16),
364         'loc_case': fields.char('Case', size=16),
365         '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"),
366         '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"),
367         'track_all': fields.boolean('Full Lots Traceability', help="Forces to specify a Serial Number on each and every operation related to this product"),
368         
369         # sum of product variant qty
370         # 'reception_count': fields.function(_product_available, multi='qty_available',
371         #     fnct_search=_search_product_quantity, type='float', string='Quantity On Hand'),
372         # 'delivery_count': fields.function(_product_available, multi='qty_available',
373         #     fnct_search=_search_product_quantity, type='float', string='Quantity On Hand'),
374         'qty_available': fields.function(_product_available, multi='qty_available',
375             fnct_search=_search_product_quantity, type='float', string='Quantity On Hand'),
376         'virtual_available': fields.function(_product_available, multi='qty_available',
377             fnct_search=_search_product_quantity, type='float', string='Quantity Available'),
378         'incoming_qty': fields.function(_product_available, multi='qty_available',
379             fnct_search=_search_product_quantity, type='float', string='Incoming'),
380         'outgoing_qty': fields.function(_product_available, multi='qty_available',
381             fnct_search=_search_product_quantity, type='float', string='Outgoing'),
382         
383         'route_ids': fields.many2many('stock.location.route', 'stock_route_product', 'product_id', 'route_id', 'Routes', domain="[('product_selectable', '=', True)]",
384                                     help="Depending on the modules installed, this will allow you to define the route of the product: whether it will be bought, manufactured, MTO/MTS,..."),
385     }
386
387     _defaults = {
388         'sale_delay': 7,
389         # Dirty hack to avoid empty stat button in create view
390         'qty_available_text': lambda *x: '0.0 On Hand',
391     }
392
393     def action_view_routes(self, cr, uid, ids, context=None):
394         route_obj = self.pool.get("stock.location.route")
395         act_obj = self.pool.get('ir.actions.act_window')
396         mod_obj = self.pool.get('ir.model.data')
397         product_route_ids = set()
398         for product in self.browse(cr, uid, ids, context=context):
399             product_route_ids |= set([r.id for r in product.route_ids])
400             product_route_ids |= set([r.id for r in product.categ_id.total_route_ids])
401         route_ids = route_obj.search(cr, uid, ['|', ('id', 'in', list(product_route_ids)), ('warehouse_selectable', '=', True)], context=context)
402         result = mod_obj.xmlid_to_res_id(cr, uid, 'stock.action_routes_form', raise_if_not_found=True)
403         result = act_obj.read(cr, uid, [result], context=context)[0]
404         result['domain'] = "[('id','in',[" + ','.join(map(str, route_ids)) + "])]"
405         return result
406
407
408     def _get_products(self, cr, uid, ids, context=None):
409         products = []
410         for prodtmpl in self.browse(cr, uid, ids, context=None):
411             products += [x.id for x in prodtmpl.product_variant_ids]
412         return products
413     
414     def _get_act_window_dict(self, cr, uid, name, context=None):
415         mod_obj = self.pool.get('ir.model.data')
416         act_obj = self.pool.get('ir.actions.act_window')
417         result = mod_obj.xmlid_to_res_id(cr, uid, name, raise_if_not_found=True)
418         result = act_obj.read(cr, uid, [result], context=context)[0]
419         return result
420     
421     def action_open_quants(self, cr, uid, ids, context=None):
422         products = self._get_products(cr, uid, ids, context=context)
423         result = self._get_act_window_dict(cr, uid, 'stock.product_open_quants', context=context)
424         result['domain'] = "[('product_id','in',[" + ','.join(map(str, products)) + "])]"
425         result['context'] = "{'search_default_locationgroup': 1, 'search_default_internal_loc': 1}"
426         return result
427     
428     def action_view_orderpoints(self, cr, uid, ids, context=None):
429         products = self._get_products(cr, uid, ids, context=context)
430         result = self._get_act_window_dict(cr, uid, 'stock.product_open_orderpoint', context=context)
431         if len(ids) == 1 and len(products) == 1:
432             result['context'] = "{'default_product_id': " + str(products[0]) + ", 'search_default_product_id': " + str(products[0]) + "}"
433         else:
434             result['domain'] = "[('product_id','in',[" + ','.join(map(str, products)) + "])]"
435             result['context'] = "{}"
436         return result
437
438
439     def action_view_stock_moves(self, cr, uid, ids, context=None):
440         products = self._get_products(cr, uid, ids, context=context)
441         result = self._get_act_window_dict(cr, uid, 'stock.act_product_stock_move_open', context=context)
442         if len(ids) == 1 and len(products) == 1:
443             ctx = "{'tree_view_ref':'stock.view_move_tree', \
444                   'default_product_id': %s, 'search_default_product_id': %s}" \
445                   % (products[0], products[0])
446             result['context'] = ctx
447         else:
448             result['domain'] = "[('product_id','in',[" + ','.join(map(str, products)) + "])]"
449             result['context'] = "{'tree_view_ref':'stock.view_move_tree'}"
450         return result
451
452     def write(self, cr, uid, ids, vals, context=None):
453         if 'uom_po_id' in vals:
454             product_ids = self.pool.get('product.product').search(cr, uid, [('product_tmpl_id', 'in', ids)], context=context)
455             if self.pool.get('stock.move').search(cr, uid, [('product_id', 'in', product_ids)], context=context, limit=1):
456                 raise osv.except_osv(_('Error!'), _("You can not change the unit of measure of a product that has already been used in a stock move. If you need to change the unit of measure, you may deactivate this product.") % ())
457         return super(product_template, self).write(cr, uid, ids, vals, context=context)
458
459
460 class product_removal_strategy(osv.osv):
461     _name = 'product.removal'
462     _description = 'Removal Strategy'
463
464     _columns = {
465         'name': fields.char('Name', required=True),
466         'method': fields.char("Method", required=True, help="FIFO, LIFO..."),
467     }
468
469
470 class product_putaway_strategy(osv.osv):
471     _name = 'product.putaway'
472     _description = 'Put Away Strategy'
473
474     def _get_putaway_options(self, cr, uid, context=None):
475         return [('fixed', 'Fixed Location')]
476
477     _columns = {
478         'name': fields.char('Name', required=True),
479         'method': fields.selection(_get_putaway_options, "Method", required=True),
480         'fixed_location_ids': fields.one2many('stock.fixed.putaway.strat', 'putaway_id', 'Fixed Locations Per Product Category', help="When the method is fixed, this location will be used to store the products", copy=True),
481     }
482
483     _defaults = {
484         'method': 'fixed',
485     }
486
487     def putaway_apply(self, cr, uid, putaway_strat, product, context=None):
488         if putaway_strat.method == 'fixed':
489             for strat in putaway_strat.fixed_location_ids:
490                 categ = product.categ_id
491                 while categ:
492                     if strat.category_id.id == categ.id:
493                         return strat.fixed_location_id.id
494                     categ = categ.parent_id
495
496
497 class fixed_putaway_strat(osv.osv):
498     _name = 'stock.fixed.putaway.strat'
499     _order = 'sequence'
500     _columns = {
501         'putaway_id': fields.many2one('product.putaway', 'Put Away Method', required=True),
502         'category_id': fields.many2one('product.category', 'Product Category', required=True),
503         'fixed_location_id': fields.many2one('stock.location', 'Location', required=True),
504         'sequence': fields.integer('Priority', help="Give to the more specialized category, a higher priority to have them in top of the list."),
505     }
506
507
508 class product_category(osv.osv):
509     _inherit = 'product.category'
510
511     def calculate_total_routes(self, cr, uid, ids, name, args, context=None):
512         res = {}
513         for categ in self.browse(cr, uid, ids, context=context):
514             categ2 = categ
515             routes = [x.id for x in categ.route_ids]
516             while categ2.parent_id:
517                 categ2 = categ2.parent_id
518                 routes += [x.id for x in categ2.route_ids]
519             res[categ.id] = routes
520         return res
521
522     _columns = {
523         'route_ids': fields.many2many('stock.location.route', 'stock_location_route_categ', 'categ_id', 'route_id', 'Routes', domain="[('product_categ_selectable', '=', True)]"),
524         'removal_strategy_id': fields.many2one('product.removal', 'Force Removal Strategy', help="Set a specific removal strategy that will be used regardless of the source location for this product category"),
525         'total_route_ids': fields.function(calculate_total_routes, relation='stock.location.route', type='many2many', string='Total routes', readonly=True),
526     }
527
528
529 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: