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 osv import fields, osv
23 from tools.translate import _
24 import decimal_precision as dp
26 class product_product(osv.osv):
27 _inherit = "product.product"
29 def get_product_accounts(self, cr, uid, product_id, context=None):
30 """ To get the stock input account, stock output account and stock journal related to product.
31 @param product_id: product id
32 @return: dictionary which contains information regarding stock input account, stock output account and stock journal
36 product_obj = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
38 stock_input_acc = product_obj.property_stock_account_input and product_obj.property_stock_account_input.id or False
39 if not stock_input_acc:
40 stock_input_acc = product_obj.categ_id.property_stock_account_input_categ and product_obj.categ_id.property_stock_account_input_categ.id or False
42 stock_output_acc = product_obj.property_stock_account_output and product_obj.property_stock_account_output.id or False
43 if not stock_output_acc:
44 stock_output_acc = product_obj.categ_id.property_stock_account_output_categ and product_obj.categ_id.property_stock_account_output_categ.id or False
46 journal_id = product_obj.categ_id.property_stock_journal and product_obj.categ_id.property_stock_journal.id or False
47 account_valuation = product_obj.categ_id.property_stock_valuation_account_id and product_obj.categ_id.property_stock_valuation_account_id.id or False
49 'stock_account_input': stock_input_acc,
50 'stock_account_output': stock_output_acc,
51 'stock_journal': journal_id,
52 'property_stock_valuation_account_id': account_valuation
55 def do_change_standard_price(self, cr, uid, ids, datas, context=None):
56 """ Changes the Standard Price of Product and creates an account move accordingly.
57 @param datas : dict. contain default datas like new_price, stock_output_account, stock_input_account, stock_journal
58 @param context: A standard dictionary
62 location_obj = self.pool.get('stock.location')
63 move_obj = self.pool.get('account.move')
64 move_line_obj = self.pool.get('account.move.line')
68 new_price = datas.get('new_price', 0.0)
69 stock_output_acc = datas.get('stock_output_account', False)
70 stock_input_acc = datas.get('stock_input_account', False)
71 journal_id = datas.get('stock_journal', False)
72 product_obj=self.browse(cr, uid, ids, context=context)[0]
73 account_valuation = product_obj.categ_id.property_stock_valuation_account_id
74 account_valuation_id = account_valuation and account_valuation.id or False
75 if not account_valuation_id: raise osv.except_osv(_('Error!'), _('Valuation Account is not specified for Product Category: %s') % (product_obj.categ_id.name))
77 loc_ids = location_obj.search(cr, uid,[('usage','=','internal')])
79 for location in location_obj.browse(cr, uid, loc_ids, context=context):
82 'location': location.id,
83 'compute_child': False
86 product = self.browse(cr, uid, rec_id, context=c)
87 qty = product.qty_available
88 diff = product.standard_price - new_price
89 if not diff: raise osv.except_osv(_('Error!'), _("Could not find any difference between standard price and new price!"))
91 company_id = location.company_id and location.company_id.id or False
92 if not company_id: raise osv.except_osv(_('Error!'), _('Company is not specified in Location'))
97 journal_id = product.categ_id.property_stock_journal and product.categ_id.property_stock_journal.id or False
99 raise osv.except_osv(_('Error!'),
100 _('There is no journal defined '\
101 'on the product category: "%s" (id: %d)') % \
102 (product.categ_id.name,
103 product.categ_id.id,))
104 move_id = move_obj.create(cr, uid, {
105 'journal_id': journal_id,
106 'company_id': company_id
109 move_ids.append(move_id)
113 if not stock_input_acc:
114 stock_input_acc = product.product_tmpl_id.\
115 property_stock_account_input.id
116 if not stock_input_acc:
117 stock_input_acc = product.categ_id.\
118 property_stock_account_input_categ.id
119 if not stock_input_acc:
120 raise osv.except_osv(_('Error!'),
121 _('There is no stock input account defined ' \
122 'for this product: "%s" (id: %d)') % \
125 amount_diff = qty * diff
126 move_line_obj.create(cr, uid, {
127 'name': product.name,
128 'account_id': stock_input_acc,
129 'debit': amount_diff,
132 move_line_obj.create(cr, uid, {
133 'name': product.categ_id.name,
134 'account_id': account_valuation_id,
135 'credit': amount_diff,
139 if not stock_output_acc:
140 stock_output_acc = product.product_tmpl_id.\
141 property_stock_account_output.id
142 if not stock_output_acc:
143 stock_output_acc = product.categ_id.\
144 property_stock_account_output_categ.id
145 if not stock_output_acc:
146 raise osv.except_osv(_('Error!'),
147 _('There is no stock output account defined ' \
148 'for this product: "%s" (id: %d)') % \
151 amount_diff = qty * -diff
152 move_line_obj.create(cr, uid, {
153 'name': product.name,
154 'account_id': stock_output_acc,
155 'credit': amount_diff,
158 move_line_obj.create(cr, uid, {
159 'name': product.categ_id.name,
160 'account_id': account_valuation_id,
161 'debit': amount_diff,
165 self.write(cr, uid, rec_id, {'standard_price': new_price})
169 def view_header_get(self, cr, user, view_id, view_type, context=None):
172 res = super(product_product, self).view_header_get(cr, user, view_id, view_type, context)
174 if (context.get('active_id', False)) and (context.get('active_model') == 'stock.location'):
175 return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['active_id'], context).name
178 def get_product_available(self, cr, uid, ids, context=None):
179 """ Finds whether product is available or not in particular warehouse.
180 @return: Dictionary of values
185 location_obj = self.pool.get('stock.location')
186 warehouse_obj = self.pool.get('stock.warehouse')
188 states = context.get('states',[])
189 what = context.get('what',())
191 ids = self.search(cr, uid, [])
192 res = {}.fromkeys(ids, 0.0)
196 # TODO: write in more ORM way, less queries, more pg84 magic
197 if context.get('shop', False):
198 cr.execute('select warehouse_id from sale_shop where id=%s', (int(context['shop']),))
201 context['warehouse'] = res2[0]
203 if context.get('warehouse', False):
204 cr.execute('select lot_stock_id from stock_warehouse where id=%s', (int(context['warehouse']),))
207 context['location'] = res2[0]
209 if context.get('location', False):
210 if type(context['location']) == type(1):
211 location_ids = [context['location']]
212 elif type(context['location']) in (type(''), type(u'')):
213 location_ids = location_obj.search(cr, uid, [('name','ilike',context['location'])], context=context)
215 location_ids = context['location']
218 wids = warehouse_obj.search(cr, uid, [], context=context)
219 for w in warehouse_obj.browse(cr, uid, wids, context=context):
220 location_ids.append(w.lot_stock_id.id)
222 # build the list of ids of children of the location given by id
223 if context.get('compute_child',True):
224 child_location_ids = location_obj.search(cr, uid, [('location_id', 'child_of', location_ids)])
225 location_ids = child_location_ids or location_ids
227 # this will be a dictionary of the UoM resources we need for conversion purposes, by UoM id
229 # this will be a dictionary of the product UoM by product id
231 for product in self.browse(cr, uid, ids, context=context):
232 product2uom[product.id] = product.uom_id.id
233 uoms_o[product.uom_id.id] = product.uom_id
238 from_date = context.get('from_date',False)
239 to_date = context.get('to_date',False)
242 where = [tuple(location_ids),tuple(location_ids),tuple(ids),tuple(states)]
243 if from_date and to_date:
244 date_str = "date>=%s and date<=%s"
245 where.append(tuple([from_date]))
246 where.append(tuple([to_date]))
248 date_str = "date>=%s"
249 date_values = [from_date]
251 date_str = "date<=%s"
252 date_values = [to_date]
254 prodlot_id = context.get('prodlot_id', False)
256 # TODO: perhaps merge in one query.
258 where.append(tuple(date_values))
260 # all moves from a location out of the set to a location in the set
262 'select sum(product_qty), product_id, product_uom '\
264 'where location_id NOT IN %s '\
265 'and location_dest_id IN %s '\
266 'and product_id IN %s '\
267 '' + (prodlot_id and ('and prodlot_id = ' + str(prodlot_id)) or '') + ' '\
268 'and state IN %s ' + (date_str and 'and '+date_str+' ' or '') +' '\
269 'group by product_id,product_uom',tuple(where))
270 results = cr.fetchall()
272 # all moves from a location in the set to a location out of the set
274 'select sum(product_qty), product_id, product_uom '\
276 'where location_id IN %s '\
277 'and location_dest_id NOT IN %s '\
278 'and product_id IN %s '\
279 '' + (prodlot_id and ('and prodlot_id = ' + str(prodlot_id)) or '') + ' '\
280 'and state in %s ' + (date_str and 'and '+date_str+' ' or '') + ' '\
281 'group by product_id,product_uom',tuple(where))
282 results2 = cr.fetchall()
284 # Get the missing UoM resources
285 uom_obj = self.pool.get('product.uom')
286 uoms = map(lambda x: x[2], results) + map(lambda x: x[2], results2)
287 if context.get('uom', False):
288 uoms += [context['uom']]
289 uoms = filter(lambda x: x not in uoms_o.keys(), uoms)
291 uoms = uom_obj.browse(cr, uid, list(set(uoms)), context=context)
295 #TOCHECK: before change uom of product, stock move line are in old uom.
296 context.update({'raise-exception': False})
297 # Count the incoming quantities
298 for amount, prod_id, prod_uom in results:
299 amount = uom_obj._compute_qty_obj(cr, uid, uoms_o[prod_uom], amount,
300 uoms_o[context.get('uom', False) or product2uom[prod_id]], context=context)
301 res[prod_id] += amount
302 # Count the outgoing quantities
303 for amount, prod_id, prod_uom in results2:
304 amount = uom_obj._compute_qty_obj(cr, uid, uoms_o[prod_uom], amount,
305 uoms_o[context.get('uom', False) or product2uom[prod_id]], context=context)
306 res[prod_id] -= amount
309 def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None):
310 """ Finds the incoming and outgoing quantity of product.
311 @return: Dictionary of values
319 res[id] = {}.fromkeys(field_names, 0.0)
320 for f in field_names:
322 if f == 'qty_available':
323 c.update({ 'states': ('done',), 'what': ('in', 'out') })
324 if f == 'virtual_available':
325 c.update({ 'states': ('confirmed','waiting','assigned','done'), 'what': ('in', 'out') })
326 if f == 'incoming_qty':
327 c.update({ 'states': ('confirmed','waiting','assigned'), 'what': ('in',) })
328 if f == 'outgoing_qty':
329 c.update({ 'states': ('confirmed','waiting','assigned'), 'what': ('out',) })
330 stock = self.get_product_available(cr, uid, ids, context=c)
332 res[id][f] = stock.get(id, 0.0)
336 'qty_available': fields.function(_product_available, multi='qty_available',
337 type='float', digits_compute=dp.get_precision('Product UoM'),
338 string='Quantity On Hand',
339 help="Current quantity of products.\n"
340 "In a context with a single Stock Location, this includes "
341 "goods stored at this Location, or any of its children.\n"
342 "In a context with a single Warehouse, this includes "
343 "goods stored in the Stock Location of this Warehouse, or any "
345 "In a context with a single Shop, this includes goods "
346 "stored in the Stock Location of the Warehouse of this Shop, "
347 "or any of its children.\n"
348 "Otherwise, this includes goods stored in any Stock Location "
349 "typed as 'internal'."),
350 'virtual_available': fields.function(_product_available, multi='qty_available',
351 type='float', digits_compute=dp.get_precision('Product UoM'),
352 string='Quantity Available',
353 help="Forecast quantity (computed as Quantity On Hand "
354 "- Outgoing + Incoming)\n"
355 "In a context with a single Stock Location, this includes "
356 "goods stored at this Location, or any of its children.\n"
357 "In a context with a single Warehouse, this includes "
358 "goods stored in the Stock Location of this Warehouse, or any "
360 "In a context with a single Shop, this includes goods "
361 "stored in the Stock Location of the Warehouse of this Shop, "
362 "or any of its children.\n"
363 "Otherwise, this includes goods stored in any Stock Location "
364 "typed as 'internal'."),
365 'incoming_qty': fields.function(_product_available, multi='qty_available',
366 type='float', digits_compute=dp.get_precision('Product UoM'),
368 help="Quantity of products that are planned to arrive.\n"
369 "In a context with a single Stock Location, this includes "
370 "goods arriving to this Location, or any of its children.\n"
371 "In a context with a single Warehouse, this includes "
372 "goods arriving to the Stock Location of this Warehouse, or "
373 "any of its children.\n"
374 "In a context with a single Shop, this includes goods "
375 "arriving to the Stock Location of the Warehouse of this "
376 "Shop, or any of its children.\n"
377 "Otherwise, this includes goods arriving to any Stock "
378 "Location typed as 'internal'."),
379 'outgoing_qty': fields.function(_product_available, multi='qty_available',
380 type='float', digits_compute=dp.get_precision('Product UoM'),
382 help="Quantity of products that are planned to leave.\n"
383 "In a context with a single Stock Location, this includes "
384 "goods leaving from this Location, or any of its children.\n"
385 "In a context with a single Warehouse, this includes "
386 "goods leaving from the Stock Location of this Warehouse, or "
387 "any of its children.\n"
388 "In a context with a single Shop, this includes goods "
389 "leaving from the Stock Location of the Warehouse of this "
390 "Shop, or any of its children.\n"
391 "Otherwise, this includes goods leaving from any Stock "
392 "Location typed as 'internal'."),
393 'track_production': fields.boolean('Track Manufacturing Lots', help="Forces to specify a Production Lot for all moves containing this product and generated by a Manufacturing Order"),
394 'track_incoming': fields.boolean('Track Incoming Lots', help="Forces to specify a Production Lot for all moves containing this product and coming from a Supplier Location"),
395 'track_outgoing': fields.boolean('Track Outgoing Lots', help="Forces to specify a Production Lot for all moves containing this product and going to a Customer Location"),
396 'location_id': fields.dummy(string='Location', relation='stock.location', type='many2one'),
397 'warehouse_id': fields.dummy(string='Warehouse', relation='stock.warehouse', type='many2one'),
398 'valuation':fields.selection([('manual_periodic', 'Periodical (manual)'),
399 ('real_time','Real Time (automated)'),], 'Inventory Valuation',
400 help="If real-time valuation is enabled for a product, the system will automatically write journal entries corresponding to stock moves." \
401 "The inventory variation account set on the product category will represent the current inventory value, and the stock input and stock output account will hold the counterpart moves for incoming and outgoing products."
406 'valuation': 'manual_periodic',
409 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
410 res = super(product_product,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
413 if ('location' in context) and context['location']:
414 location_info = self.pool.get('stock.location').browse(cr, uid, context['location'])
415 fields=res.get('fields',{})
417 if location_info.usage == 'supplier':
418 if fields.get('virtual_available'):
419 res['fields']['virtual_available']['string'] = _('Future Receptions')
420 if fields.get('qty_available'):
421 res['fields']['qty_available']['string'] = _('Received Qty')
423 if location_info.usage == 'internal':
424 if fields.get('virtual_available'):
425 res['fields']['virtual_available']['string'] = _('Future Stock')
427 if location_info.usage == 'customer':
428 if fields.get('virtual_available'):
429 res['fields']['virtual_available']['string'] = _('Future Deliveries')
430 if fields.get('qty_available'):
431 res['fields']['qty_available']['string'] = _('Delivered Qty')
433 if location_info.usage == 'inventory':
434 if fields.get('virtual_available'):
435 res['fields']['virtual_available']['string'] = _('Future P&L')
436 if fields.get('qty_available'):
437 res['fields']['qty_available']['string'] = _('P&L Qty')
439 if location_info.usage == 'procurement':
440 if fields.get('virtual_available'):
441 res['fields']['virtual_available']['string'] = _('Future Qty')
442 if fields.get('qty_available'):
443 res['fields']['qty_available']['string'] = _('Unplanned Qty')
445 if location_info.usage == 'production':
446 if fields.get('virtual_available'):
447 res['fields']['virtual_available']['string'] = _('Future Productions')
448 if fields.get('qty_available'):
449 res['fields']['qty_available']['string'] = _('Produced Qty')
454 class product_template(osv.osv):
455 _name = 'product.template'
456 _inherit = 'product.template'
458 'property_stock_procurement': fields.property(
461 relation='stock.location',
462 string="Procurement Location",
464 domain=[('usage','like','procurement')],
465 help="For the current product, this stock location will be used, instead of the default one, as the source location for stock moves generated by procurements"),
466 'property_stock_production': fields.property(
469 relation='stock.location',
470 string="Production Location",
472 domain=[('usage','like','production')],
473 help="For the current product, this stock location will be used, instead of the default one, as the source location for stock moves generated by production orders"),
474 'property_stock_inventory': fields.property(
477 relation='stock.location',
478 string="Inventory Location",
480 domain=[('usage','like','inventory')],
481 help="For the current product, this stock location will be used, instead of the default one, as the source location for stock moves generated when you do an inventory"),
482 'property_stock_account_input': fields.property('account.account',
483 type='many2one', relation='account.account',
484 string='Stock Input Account', view_load=True,
485 help="When doing real-time inventory valuation, counterpart journal items for all incoming stock moves will be posted in this account, unless "
486 "there is a specific valuation account set on the source location. When not set on the product, the one from the product category is used."),
487 'property_stock_account_output': fields.property('account.account',
488 type='many2one', relation='account.account',
489 string='Stock Output Account', view_load=True,
490 help="When doing real-time inventory valuation, counterpart journal items for all outgoing stock moves will be posted in this account, unless "
491 "there is a specific valuation account set on the destination location. When not set on the product, the one from the product category is used."),
496 class product_category(osv.osv):
498 _inherit = 'product.category'
500 'property_stock_journal': fields.property('account.journal',
501 relation='account.journal', type='many2one',
502 string='Stock journal', view_load=True,
503 help="When doing real-time inventory valuation, this is the Accounting Journal in which entries will be automatically posted when stock moves are processed."),
504 'property_stock_account_input_categ': fields.property('account.account',
505 type='many2one', relation='account.account',
506 string='Stock Input Account', view_load=True,
507 help="When doing real-time inventory valuation, counterpart journal items for all incoming stock moves will be posted in this account, unless "
508 "there is a specific valuation account set on the source location. This is the default value for all products in this category. It "
509 "can also directly be set on each product"),
510 'property_stock_account_output_categ': fields.property('account.account',
511 type='many2one', relation='account.account',
512 string='Stock Output Account', view_load=True,
513 help="When doing real-time inventory valuation, counterpart journal items for all outgoing stock moves will be posted in this account, unless "
514 "there is a specific valuation account set on the destination location. This is the default value for all products in this category. It "
515 "can also directly be set on each product"),
516 'property_stock_valuation_account_id': fields.property('account.account',
518 relation='account.account',
519 string="Stock Valuation Account",
521 help="When real-time inventory valuation is enabled on a product, this account will hold the current value of the products.",),
526 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: