2010-03-10 09:42:47 +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
from tools . translate import _
class stock_fill_inventory ( osv . osv_memory ) :
_name = " stock.fill.inventory "
2010-09-08 13:43:53 +00:00
_description = " Import Inventory "
2010-03-10 09:42:47 +00:00
_columns = {
2010-08-13 12:20:05 +00:00
' location_id ' : fields . many2one ( ' stock.location ' , ' Location ' , required = True ) ,
2010-09-09 06:15:10 +00:00
' recursive ' : fields . boolean ( " Include children " , help = " If checked, products contained in child locations of selected location will be included as well. " ) ,
2010-09-09 12:05:14 +00:00
' set_stock_zero ' : fields . boolean ( " Set to zero " , help = " If checked, all product quantities will be set to zero to help ensure a real physical inventory is done " ) ,
2010-05-26 12:59:30 +00:00
}
2010-09-13 11:46:02 +00:00
def view_init ( self , cr , uid , fields_list , context = None ) :
"""
Creates view dynamically and adding fields at runtime .
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
@param context : A standard dictionary
@return : New arch of view with new columns .
"""
2011-04-18 07:31:10 +00:00
if context is None :
context = { }
super ( stock_fill_inventory , self ) . view_init ( cr , uid , fields_list , context = context )
2011-05-02 18:46:43 +00:00
2011-04-21 09:00:39 +00:00
if len ( context . get ( ' active_ids ' , [ ] ) ) > 1 :
2011-04-19 08:42:00 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' You cannot perform this operation on more than one Stock Inventories. ' ) )
2011-05-02 18:46:43 +00:00
2010-09-14 06:26:43 +00:00
if context . get ( ' active_id ' , False ) :
stock = self . pool . get ( ' stock.inventory ' ) . browse ( cr , uid , context . get ( ' active_id ' , False ) )
2011-05-02 18:46:43 +00:00
2011-04-19 08:42:00 +00:00
if stock . state == ' done ' :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Stock Inventory is already Validated. ' ) )
return True
2010-03-10 09:42:47 +00:00
2010-09-14 06:26:43 +00:00
def fill_inventory ( self , cr , uid , ids , context = None ) :
2010-09-08 13:43:53 +00:00
""" To Import stock inventory according to products available in the selected locations.
2010-05-26 12:59:30 +00:00
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
2010-08-13 12:20:05 +00:00
@param ids : the ID or list of IDs if we want more than one
@param context : A standard dictionary
@return :
"""
2010-11-23 07:05:05 +00:00
if context is None :
2011-03-04 11:21:02 +00:00
context = { }
2011-04-18 07:31:10 +00:00
2010-03-10 09:42:47 +00:00
inventory_line_obj = self . pool . get ( ' stock.inventory.line ' )
location_obj = self . pool . get ( ' stock.location ' )
2010-03-11 07:47:54 +00:00
product_obj = self . pool . get ( ' product.product ' )
stock_location_obj = self . pool . get ( ' stock.location ' )
2011-03-04 11:21:02 +00:00
move_obj = self . pool . get ( ' stock.move ' )
2011-09-16 10:55:53 +00:00
uom_obj = self . pool . get ( ' product.uom ' )
2011-03-04 11:21:02 +00:00
if ids and len ( ids ) :
2010-09-09 14:05:55 +00:00
ids = ids [ 0 ]
else :
2011-03-04 11:21:02 +00:00
return { ' type ' : ' ir.actions.act_window_close ' }
2010-11-22 10:37:53 +00:00
fill_inventory = self . browse ( cr , uid , ids , context = context )
2010-09-09 14:05:55 +00:00
res = { }
res_location = { }
2011-05-02 18:46:43 +00:00
2011-04-19 08:42:00 +00:00
if fill_inventory . recursive :
2010-09-09 14:05:55 +00:00
location_ids = location_obj . search ( cr , uid , [ ( ' location_id ' ,
2011-12-08 05:58:41 +00:00
' child_of ' , [ fill_inventory . location_id . id ] ) ] , order = " id " ,
2011-05-02 18:46:43 +00:00
context = context )
2010-09-09 14:05:55 +00:00
else :
2011-03-08 13:32:36 +00:00
location_ids = [ fill_inventory . location_id . id ]
2011-03-04 11:21:02 +00:00
res = { }
2011-03-08 13:32:36 +00:00
flag = False
2011-05-02 18:46:43 +00:00
2011-03-08 13:32:36 +00:00
for location in location_ids :
2011-03-04 11:21:02 +00:00
datas = { }
res [ location ] = { }
2011-10-10 15:42:41 +00:00
move_ids = move_obj . search ( cr , uid , [ ' | ' , ( ' location_dest_id ' , ' = ' , location ) , ( ' location_id ' , ' = ' , location ) , ( ' state ' , ' = ' , ' done ' ) ] , context = context )
2011-03-04 11:21:02 +00:00
for move in move_obj . browse ( cr , uid , move_ids , context = context ) :
lot_id = move . prodlot_id . id
prod_id = move . product_id . id
2011-10-10 15:42:41 +00:00
if move . location_dest_id . id == location :
qty = uom_obj . _compute_qty ( cr , uid , move . product_uom . id , move . product_qty , move . product_id . uom_id . id )
else :
qty = - uom_obj . _compute_qty ( cr , uid , move . product_uom . id , move . product_qty , move . product_id . uom_id . id )
2011-05-02 18:46:43 +00:00
2011-03-04 11:21:02 +00:00
if datas . get ( ( prod_id , lot_id ) ) :
2011-04-18 07:31:10 +00:00
qty + = datas [ ( prod_id , lot_id ) ] [ ' product_qty ' ]
2011-05-02 18:46:43 +00:00
2011-04-18 07:31:10 +00:00
datas [ ( prod_id , lot_id ) ] = { ' product_id ' : prod_id , ' location_id ' : location , ' product_qty ' : qty , ' product_uom ' : move . product_id . uom_id . id , ' prod_lot_id ' : lot_id }
2011-05-02 18:46:43 +00:00
2011-03-08 13:32:36 +00:00
if datas :
flag = True
res [ location ] = datas
if not flag :
2012-08-07 11:34:14 +00:00
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' No product in this location. ' ) )
2011-04-18 07:31:10 +00:00
for stock_move in res . values ( ) :
for stock_move_details in stock_move . values ( ) :
stock_move_details . update ( { ' inventory_id ' : context [ ' active_ids ' ] [ 0 ] } )
domain = [ ]
2011-05-02 18:46:43 +00:00
2011-04-18 07:31:10 +00:00
if fill_inventory . set_stock_zero :
stock_move_details . update ( { ' product_qty ' : 0 } )
for field , value in stock_move_details . items ( ) :
domain . append ( ( field , ' = ' , value ) )
2011-04-06 11:45:29 +00:00
2011-04-18 07:31:10 +00:00
line_ids = inventory_line_obj . search ( cr , uid , domain , context = context )
2011-04-06 11:45:29 +00:00
2011-04-18 07:31:10 +00:00
if not line_ids :
inventory_line_obj . create ( cr , uid , stock_move_details , context = context )
2010-03-10 09:42:47 +00:00
2011-03-08 13:32:36 +00:00
return { ' type ' : ' ir.actions.act_window_close ' }
2010-03-10 09:42:47 +00:00
stock_fill_inventory ( )
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: