1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
24 import openerp.addons.decimal_precision as dp
26 class product_product(osv.osv):
27 _inherit = "product.product"
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 ('location_id.usage', '!=', 'internal'),
35 ('location_dest_id.usage', '=', 'internal'),
36 ('state','in',('confirmed','assigned','pending'))
37 ], ['product_id'], ['product_id'])
39 product_id = move['product_id'][0]
40 res[product_id]['reception_count'] = move['product_id_count']
41 moves = move_pool.read_group(cr, uid, [
42 ('product_id', 'in', ids),
43 ('location_id.usage', '=', 'internal'),
44 ('location_dest_id.usage', '!=', 'internal'),
45 ('state','in',('confirmed','assigned','pending'))
46 ], ['product_id'], ['product_id'])
48 product_id = move['product_id'][0]
49 res[product_id]['delivery_count'] = move['product_id_count']
52 def view_header_get(self, cr, user, view_id, view_type, context=None):
55 res = super(product_product, self).view_header_get(cr, user, view_id, view_type, context)
57 if (context.get('active_id', False)) and (context.get('active_model') == 'stock.location'):
58 return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['active_id'], context).name
62 # TODO: Needs to be rechecked
64 def _get_domain_locations(self, cr, uid, ids, context=None):
66 Parses the context and returns a list of location_ids based on it.
67 It will return all stock locations when no parameters are given
68 Possible parameters are shop, warehouse, location, force_company, compute_child
70 context = context or {}
72 location_obj = self.pool.get('stock.location')
73 warehouse_obj = self.pool.get('stock.warehouse')
76 if context.get('location', False):
77 if type(context['location']) == type(1):
78 location_ids = [context['location']]
79 elif type(context['location']) in (type(''), type(u'')):
80 domain = [('name','ilike',context['location'])]
81 if context.get('force_company', False):
82 domain += [('company_id', '=', context['force_company'])]
83 location_ids = location_obj.search(cr, uid, domain, context=context)
85 location_ids = context['location']
87 if context.get('warehouse', False):
88 wh = warehouse_obj.browse(cr, uid, [context['warehouse']], context=context)
90 wids = warehouse_obj.search(cr, uid, [], context=context)
91 wh = warehouse_obj.browse(cr, uid, wids, context=context)
93 for w in warehouse_obj.browse(cr, uid, wids, context=context):
94 location_ids.append(w.lot_stock_id.id)
96 operator = context.get('compute_child',True) and 'child_of' or 'in'
97 domain = context.get('force_company', False) and ['&', ('company_id', '=', context['force_company'])] or []
98 domain += [('product_id', 'in', ids)]
100 domain + [('location_id', operator, location_ids)],
101 domain + ['&', ('location_dest_id', operator, location_ids), '!', ('location_id', operator, location_ids)],
102 domain + ['&', ('location_id', operator, location_ids), '!', ('location_dest_id', operator, location_ids)]
105 def _get_domain_dates(self, cr, uid, ids, context):
106 from_date = context.get('from_date',False)
107 to_date = context.get('to_date',False)
110 domain.append(('date','>=',from_date))
112 domain.append(('date','<=',to_date))
115 def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None):
116 context = context or {}
117 field_names = field_names or []
119 domain_products = [('product_id', 'in', ids)]
120 domain_quant, domain_move_in, domain_move_out = self._get_domain_locations(cr, uid, ids, context=context)
121 domain_move_in += self._get_domain_dates(cr, uid, ids, context=context) + [('state','not in',('done','cancel'))] + domain_products
122 domain_move_out += self._get_domain_dates(cr, uid, ids, context=context) + [('state','not in',('done','cancel'))] + domain_products
123 domain_quant += domain_products
124 if context.get('lot_id') or context.get('owner_id') or context.get('package_id'):
125 if context.get('lot_id'):
126 domain_quant.append(('lot_id','=',context['lot_id']))
127 if context.get('owner_id'):
128 domain_quant.append(('owner_id','=',context['owner_id']))
129 if context.get('package_id'):
130 domain_quant.append(('package_id','=',context['package_id']))
134 # if field_names in ['incoming_qty', 'outgoing_qty', 'virtual_available']:
135 moves_in = self.pool.get('stock.move').read_group(cr, uid, domain_move_in, ['product_id', 'product_qty'], ['product_id'], context=context)
136 moves_out = self.pool.get('stock.move').read_group(cr, uid, domain_move_out, ['product_id', 'product_qty'], ['product_id'], context=context)
138 quants = self.pool.get('stock.quant').read_group(cr, uid, domain_quant, ['product_id', 'qty'], ['product_id'], context=context)
140 quants = dict(map(lambda x: (x['product_id'][0], x['qty']), quants))
142 moves_in = dict(map(lambda x: (x['product_id'][0], x['product_qty']), moves_in))
143 moves_out = dict(map(lambda x: (x['product_id'][0], x['product_qty']), moves_out))
148 'qty_available': quants.get(id, 0.0),
149 'incoming_qty': moves_in.get(id, 0.0),
150 'outgoing_qty': moves_out.get(id, 0.0),
151 'virtual_available': quants.get(id, 0.0) + moves_in.get(id, 0.0) - moves_out.get(id, 0.0),
156 'reception_count': fields.function(_stock_move_count, string="Reception", type='integer', multi='pickings'),
157 'delivery_count': fields.function(_stock_move_count, string="Delivery", type='integer', multi='pickings'),
158 'qty_available': fields.function(_product_available, multi='qty_available',
159 type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
160 string='Quantity On Hand',
161 help="Current quantity of products.\n"
162 "In a context with a single Stock Location, this includes "
163 "goods stored at this Location, or any of its children.\n"
164 "In a context with a single Warehouse, this includes "
165 "goods stored in the Stock Location of this Warehouse, or any "
167 "stored in the Stock Location of the Warehouse of this Shop, "
168 "or any of its children.\n"
169 "Otherwise, this includes goods stored in any Stock Location "
170 "with 'internal' type."),
171 'virtual_available': fields.function(_product_available, multi='qty_available',
172 type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
173 string='Forecasted Quantity',
174 help="Forecast quantity (computed as Quantity On Hand "
175 "- Outgoing + Incoming)\n"
176 "In a context with a single Stock Location, this includes "
177 "goods stored in this location, or any of its children.\n"
178 "In a context with a single Warehouse, this includes "
179 "goods stored in the Stock Location of this Warehouse, or any "
181 "stored in the Stock Location of the Warehouse of this Shop, "
182 "or any of its children.\n"
183 "Otherwise, this includes goods stored in any Stock Location "
184 "with 'internal' type."),
185 'incoming_qty': fields.function(_product_available, multi='qty_available',
186 type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
188 help="Quantity of products that are planned to arrive.\n"
189 "In a context with a single Stock Location, this includes "
190 "goods arriving to this Location, or any of its children.\n"
191 "In a context with a single Warehouse, this includes "
192 "goods arriving to the Stock Location of this Warehouse, or "
193 "any of its children.\n"
194 "In a context with a single Shop, this includes goods "
195 "arriving to the Stock Location of the Warehouse of this "
196 "Shop, or any of its children.\n"
197 "Otherwise, this includes goods arriving to any Stock "
198 "Location with 'internal' type."),
199 'outgoing_qty': fields.function(_product_available, multi='qty_available',
200 type='float', digits_compute=dp.get_precision('Product Unit of Measure'),
202 help="Quantity of products that are planned to leave.\n"
203 "In a context with a single Stock Location, this includes "
204 "goods leaving this Location, or any of its children.\n"
205 "In a context with a single Warehouse, this includes "
206 "goods leaving the Stock Location of this Warehouse, or "
207 "any of its children.\n"
208 "In a context with a single Shop, this includes goods "
209 "leaving the Stock Location of the Warehouse of this "
210 "Shop, or any of its children.\n"
211 "Otherwise, this includes goods leaving any Stock "
212 "Location with 'internal' type."),
213 '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"),
214 '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"),
215 '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"),
216 'location_id': fields.dummy(string='Location', relation='stock.location', type='many2one'),
217 'warehouse_id': fields.dummy(string='Warehouse', relation='stock.warehouse', type='many2one'),
218 'orderpoint_ids': fields.one2many('stock.warehouse.orderpoint', 'product_id', 'Minimum Stock Rules'),
219 'route_ids': fields.many2many('stock.location.route', 'stock_route_product', 'product_id', 'route_id', 'Routes',
220 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,..."),
223 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
224 res = super(product_product,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
227 if ('location' in context) and context['location']:
228 location_info = self.pool.get('stock.location').browse(cr, uid, context['location'])
229 fields=res.get('fields',{})
231 if location_info.usage == 'supplier':
232 if fields.get('virtual_available'):
233 res['fields']['virtual_available']['string'] = _('Future Receptions')
234 if fields.get('qty_available'):
235 res['fields']['qty_available']['string'] = _('Received Qty')
237 if location_info.usage == 'internal':
238 if fields.get('virtual_available'):
239 res['fields']['virtual_available']['string'] = _('Future Stock')
241 if location_info.usage == 'customer':
242 if fields.get('virtual_available'):
243 res['fields']['virtual_available']['string'] = _('Future Deliveries')
244 if fields.get('qty_available'):
245 res['fields']['qty_available']['string'] = _('Delivered Qty')
247 if location_info.usage == 'inventory':
248 if fields.get('virtual_available'):
249 res['fields']['virtual_available']['string'] = _('Future P&L')
250 if fields.get('qty_available'):
251 res['fields']['qty_available']['string'] = _('P&L Qty')
253 if location_info.usage == 'procurement':
254 if fields.get('virtual_available'):
255 res['fields']['virtual_available']['string'] = _('Future Qty')
256 if fields.get('qty_available'):
257 res['fields']['qty_available']['string'] = _('Unplanned Qty')
259 if location_info.usage == 'production':
260 if fields.get('virtual_available'):
261 res['fields']['virtual_available']['string'] = _('Future Productions')
262 if fields.get('qty_available'):
263 res['fields']['qty_available']['string'] = _('Produced Qty')
267 class product_template(osv.osv):
268 _name = 'product.template'
269 _inherit = 'product.template'
271 '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."),
272 'property_stock_procurement': fields.property(
274 relation='stock.location',
275 string="Procurement Location",
276 domain=[('usage','like','procurement')],
277 help="This stock location will be used, instead of the default one, as the source location for stock moves generated by procurements."),
278 'property_stock_production': fields.property(
280 relation='stock.location',
281 string="Production Location",
282 domain=[('usage','like','production')],
283 help="This stock location will be used, instead of the default one, as the source location for stock moves generated by manufacturing orders."),
284 'property_stock_inventory': fields.property(
286 relation='stock.location',
287 string="Inventory Location",
288 domain=[('usage','like','inventory')],
289 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."),
290 '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."),
291 'loc_rack': fields.char('Rack', size=16),
292 'loc_row': fields.char('Row', size=16),
293 'loc_case': fields.char('Case', size=16),
301 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: