2014-01-07 11:04:09 +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 openerp . osv import fields , osv , orm
import openerp . addons . decimal_precision as dp
from openerp . tools . translate import _
from openerp import tools
class stock_change_product_qty ( osv . osv_memory ) :
_name = " stock.change.product.qty "
_description = " Change Product Quantity "
_columns = {
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' ) ,
' new_quantity ' : fields . float ( ' New Quantity on Hand ' , 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. ' ) ,
' lot_id ' : fields . many2one ( ' stock.production.lot ' , ' Serial Number ' , domain = " [( ' product_id ' , ' = ' ,product_id)] " ) ,
' location_id ' : fields . many2one ( ' stock.location ' , ' Location ' , required = True , domain = " [( ' usage ' , ' = ' , ' internal ' )] " ) ,
}
2014-03-20 18:02:40 +00:00
_defaults = {
' new_quantity ' : 1 ,
' product_id ' : lambda self , cr , uid , ctx : ctx and ctx . get ( ' active_id ' , False ) or False
}
2014-01-07 11:04:09 +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 .
"""
2014-03-20 18:02:40 +00:00
2014-01-07 11:04:09 +00:00
res = super ( stock_change_product_qty , self ) . default_get ( cr , uid , fields , context = context )
2014-07-07 14:58:47 +00:00
if context . get ( ' active_model ' ) == ' product.template ' :
product_ids = self . pool . get ( ' product.product ' ) . search ( cr , uid , [ ( ' product_tmpl_id ' , ' = ' , context . get ( ' active_id ' ) ) ] , context = context )
if len ( product_ids ) == 1 :
res [ ' product_id ' ] = product_ids [ 0 ]
else :
2014-08-22 16:48:57 +00:00
raise orm . except_orm ( _ ( ' Warning ' ) , _ ( ' Please use the Product Variant view to update the product quantity. ' ) )
2014-07-07 14:58:47 +00:00
2014-01-07 11:04:09 +00:00
if ' location_id ' in fields :
2014-03-20 18:02:40 +00:00
location_id = res . get ( ' location_id ' , False )
if not location_id :
try :
model , location_id = self . pool . get ( ' ir.model.data ' ) . get_object_reference ( cr , uid , ' stock ' , ' stock_location_stock ' )
except ( orm . except_orm , ValueError ) :
pass
if location_id :
try :
self . pool . get ( ' stock.location ' ) . check_access_rule ( cr , uid , [ location_id ] , ' read ' , context = context )
except ( orm . except_orm , ValueError ) :
2015-10-19 15:02:30 +00:00
location_id = False
2014-03-20 18:02:40 +00:00
res [ ' location_id ' ] = location_id
2014-01-07 11:04:09 +00:00
return res
def change_product_qty ( self , cr , uid , ids , context = None ) :
""" Changes the Product Quantity by making a Physical Inventory.
@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 :
"""
if context is None :
context = { }
2014-01-24 13:56:25 +00:00
inventory_obj = self . pool . get ( ' stock.inventory ' )
inventory_line_obj = self . pool . get ( ' stock.inventory.line ' )
2014-01-07 11:04:09 +00:00
for data in self . browse ( cr , uid , ids , context = context ) :
if data . new_quantity < 0 :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Quantity cannot be negative. ' ) )
2014-04-16 14:53:13 +00:00
ctx = context . copy ( )
ctx [ ' location ' ] = data . location_id . id
ctx [ ' lot_id ' ] = data . lot_id . id
2015-05-07 14:54:18 +00:00
if data . product_id . id and data . lot_id . id :
filter = ' none '
elif data . product_id . id :
filter = ' product '
else :
filter = ' none '
2014-07-07 14:58:47 +00:00
inventory_id = inventory_obj . create ( cr , uid , {
' name ' : _ ( ' INV: %s ' ) % tools . ustr ( data . product_id . name ) ,
2015-05-07 14:54:18 +00:00
' filter ' : filter ,
2014-07-07 14:58:47 +00:00
' product_id ' : data . product_id . id ,
' location_id ' : data . location_id . id ,
' lot_id ' : data . lot_id . id } , context = context )
2014-12-11 16:13:25 +00:00
product = data . product_id . with_context ( location = data . location_id . id , lot_id = data . lot_id . id )
2014-09-12 14:31:45 +00:00
th_qty = product . qty_available
2014-04-16 14:53:13 +00:00
line_data = {
' inventory_id ' : inventory_id ,
' product_qty ' : data . new_quantity ,
' location_id ' : data . location_id . id ,
2014-07-07 14:58:47 +00:00
' product_id ' : data . product_id . id ,
' product_uom_id ' : data . product_id . uom_id . id ,
2014-06-25 13:03:11 +00:00
' theoretical_qty ' : th_qty ,
2014-04-16 14:53:13 +00:00
' prod_lot_id ' : data . lot_id . id
2014-01-07 11:04:09 +00:00
}
2014-01-30 13:44:57 +00:00
inventory_line_obj . create ( cr , uid , line_data , context = context )
2014-01-24 13:56:25 +00:00
inventory_obj . action_done ( cr , uid , [ inventory_id ] , context = context )
2014-01-07 11:04:09 +00:00
return { }
2015-05-15 13:47:35 +00:00
def onchange_location_id ( self , cr , uid , ids , location_id , product_id , context = None ) :
if location_id :
qty_wh = 0.0
qty = self . pool . get ( ' product.product ' ) . _product_available ( cr , uid , [ product_id ] , context = dict ( context or { } , location = location_id ) )
if product_id in qty :
qty_wh = qty [ product_id ] [ ' qty_available ' ]
return { ' value ' : { ' new_quantity ' : qty_wh } }
2014-01-07 11:04:09 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: