2010-12-03 13:06:21 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields , osv
2011-10-31 11:38:30 +00:00
import decimal_precision as dp
2010-12-03 13:06:21 +00:00
from tools . translate import _
2011-01-27 11:47:01 +00:00
import tools
2010-12-03 13:06:21 +00:00
class stock_change_product_qty ( osv . osv_memory ) :
_name = " stock.change.product.qty "
2012-03-06 13:38:16 +00:00
_inherit = [ ' mail.thread ' ]
2010-12-03 13:06:21 +00:00
_description = " Change Product Quantity "
_columns = {
2010-12-22 08:52:18 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' ) ,
2012-04-25 12:09:08 +00:00
' new_quantity ' : fields . float ( ' Quantity ' , digits_compute = dp . get_precision ( ' Product Unit of Measure ' ) , required = True , help = ' This quantity is expressed in the Default Unit of Measure of the product. ' ) ,
2012-04-24 13:08:05 +00:00
' prodlot_id ' : fields . many2one ( ' stock.production.lot ' , ' Serial Number ' , domain = " [( ' product_id ' , ' = ' ,product_id)] " ) ,
2010-12-22 11:05:42 +00:00
' location_id ' : fields . many2one ( ' stock.location ' , ' Location ' , required = True , domain = " [( ' usage ' , ' = ' , ' internal ' )] " ) ,
2010-12-03 13:06:21 +00:00
}
2011-01-13 10:41:00 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
2012-06-05 11:12:29 +00:00
if context is None : context = { }
2011-12-22 13:46:20 +00:00
fvg = super ( stock_change_product_qty , self ) . fields_view_get ( cr , uid , view_id , view_type , context , toolbar , submenu )
2011-01-13 10:41:00 +00:00
product_id = context and context . get ( ' active_id ' , False ) or False
2011-12-22 13:46:20 +00:00
if view_type == ' form ' and ( context . get ( ' active_model ' ) == ' product.product ' ) and product_id :
2011-01-13 13:08:50 +00:00
prod_obj = self . pool . get ( ' product.product ' ) . browse ( cr , uid , product_id , context = context )
2011-12-22 13:46:20 +00:00
fvg [ ' fields ' ] [ ' prodlot_id ' ] [ ' required ' ] = prod_obj . track_production
return fvg
2011-01-13 10:41:00 +00:00
2010-12-03 13:06:21 +00:00
def default_get ( self , cr , uid , fields , context ) :
""" To get default values for the object.
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
@param fields : List of fields for which we want default values
@param context : A standard dictionary
@return : A dictionary which of fields with values .
"""
2010-12-22 08:52:18 +00:00
product_id = context and context . get ( ' active_id ' , False ) or False
2010-12-03 13:06:21 +00:00
res = super ( stock_change_product_qty , self ) . default_get ( cr , uid , fields , context = context )
if ' new_quantity ' in fields :
2010-12-08 07:17:22 +00:00
res . update ( { ' new_quantity ' : 1 } )
2010-12-22 08:52:18 +00:00
if ' product_id ' in fields :
res . update ( { ' product_id ' : product_id } )
2010-12-03 13:06:21 +00:00
return res
2010-12-08 07:17:22 +00:00
def change_product_qty ( self , cr , uid , ids , context = None ) :
""" Changes the Product Quantity by making a Physical Inventory.
2010-12-03 13:06:21 +00:00
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
@param ids : List of IDs selected
@param context : A standard dictionary
@return :
"""
2010-12-08 07:17:22 +00:00
if context is None :
context = { }
2010-12-03 13:06:21 +00:00
rec_id = context and context . get ( ' active_id ' , False )
assert rec_id , _ ( ' Active ID is not set in Context ' )
2010-12-08 07:17:22 +00:00
inventry_obj = self . pool . get ( ' stock.inventory ' )
inventry_line_obj = self . pool . get ( ' stock.inventory.line ' )
2010-12-03 13:06:21 +00:00
prod_obj_pool = self . pool . get ( ' product.product ' )
2010-12-08 07:17:22 +00:00
res_original = prod_obj_pool . browse ( cr , uid , rec_id , context = context )
for data in self . browse ( cr , uid , ids , context = context ) :
2011-05-30 16:31:58 +00:00
if data . new_quantity < 0 :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Quantity cannot be negative. ' ) )
2011-03-03 13:27:14 +00:00
inventory_id = inventry_obj . create ( cr , uid , { ' name ' : _ ( ' INV: %s ' ) % tools . ustr ( res_original . name ) } , context = context )
2010-12-08 07:17:22 +00:00
line_data = {
2010-12-22 08:52:18 +00:00
' inventory_id ' : inventory_id ,
' product_qty ' : data . new_quantity ,
' location_id ' : data . location_id . id ,
' product_id ' : rec_id ,
' product_uom ' : res_original . uom_id . id ,
' prod_lot_id ' : data . prodlot_id . id
2010-12-08 07:17:22 +00:00
}
2010-12-31 06:26:40 +00:00
inventry_line_obj . create ( cr , uid , line_data , context = context )
2010-12-22 08:52:18 +00:00
2010-12-21 14:23:10 +00:00
inventry_obj . action_confirm ( cr , uid , [ inventory_id ] , context = context )
inventry_obj . action_done ( cr , uid , [ inventory_id ] , context = context )
2012-03-22 09:01:54 +00:00
self . change_product_qty_send_note ( cr , uid , [ data . id ] , context )
2011-07-19 11:19:18 +00:00
return { }
2010-12-08 07:17:22 +00:00
2012-07-09 16:21:24 +00:00
def change_product_qty_send_note ( self , cr , uid , ids , context = None ) :
2012-03-06 13:38:16 +00:00
prod_obj = self . pool . get ( ' product.product ' )
location_obj = self . pool . get ( ' stock.location ' )
2012-03-07 13:07:13 +00:00
2012-03-06 13:38:16 +00:00
for data in self . browse ( cr , uid , ids , context = context ) :
2012-07-09 16:21:24 +00:00
location_name = location_obj . browse ( cr , uid , data . location_id . id , context = context ) . name
message = _ ( " <b>Quantity has been changed</b> to <em> %s %s </em> for <em> %s </em> location. " ) % ( data . new_quantity , data . product_id . uom_id . name , location_name )
prod_obj . message_append_note ( cr , uid , [ data . product_id . id ] , body = message , context = context )
2012-03-06 13:38:16 +00:00
2010-12-03 13:06:21 +00:00
stock_change_product_qty ( )
2010-12-08 07:17:22 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: