2006-12-07 13:41:40 +00:00
##############################################################################
2009-11-13 05:41:16 +00:00
#
2008-11-04 05:29:09 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# 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.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-13 05:41:16 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
2010-03-01 05:05:14 +00:00
from datetime import datetime
from dateutil . relativedelta import relativedelta
2010-07-06 08:59:22 +00:00
import time
2010-03-01 05:05:14 +00:00
2009-08-28 09:40:49 +00:00
from osv import fields , osv
2007-08-07 14:30:19 +00:00
from tools import config
2008-07-08 08:13:12 +00:00
from tools . translate import _
2010-02-22 07:23:42 +00:00
import netsvc
2008-09-16 10:37:53 +00:00
import tools
2010-03-06 20:52:19 +00:00
import decimal_precision as dp
2006-12-07 13:41:40 +00:00
#----------------------------------------------------------
# Incoterms
#----------------------------------------------------------
class stock_incoterms ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.incoterms "
_description = " Incoterms "
_columns = {
2010-07-06 11:21:27 +00:00
' name ' : fields . char ( ' Name ' , size = 64 , required = True , help = " Incoterms are series of sales terms.They are used to divide transaction costs and responsibilities between buyer and seller and reflect state-of-the-art transportation practices. " ) ,
' code ' : fields . char ( ' Code ' , size = 3 , required = True , help = " Code for Incoterms " ) ,
2009-12-21 12:21:32 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the incoterms without removing it. " ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-07-06 11:21:27 +00:00
' active ' : True ,
2008-07-22 15:11:28 +00:00
}
2009-08-28 09:40:49 +00:00
2006-12-07 13:41:40 +00:00
stock_incoterms ( )
2010-06-15 08:14:32 +00:00
class stock_journal ( osv . osv ) :
_name = " stock.journal "
_description = " Stock Journal "
_columns = {
' name ' : fields . char ( ' Stock Journal ' , size = 32 , required = True ) ,
2010-07-06 11:21:27 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
2010-06-15 08:14:32 +00:00
}
_defaults = {
2010-07-06 11:21:27 +00:00
' user_id ' : lambda s , c , u , ctx : u
2010-06-15 08:14:32 +00:00
}
2010-07-06 11:21:27 +00:00
2010-06-15 08:14:32 +00:00
stock_journal ( )
2009-08-28 09:40:49 +00:00
2006-12-07 13:41:40 +00:00
#----------------------------------------------------------
# Stock Location
#----------------------------------------------------------
class stock_location ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.location "
_description = " Location "
_parent_name = " location_id "
2008-09-17 07:49:51 +00:00
_parent_store = True
2009-01-17 22:16:45 +00:00
_parent_order = ' id '
2008-09-17 07:49:51 +00:00
_order = ' parent_left '
2010-07-06 11:21:27 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
res = [ ]
if context is None :
context = { }
2009-09-24 10:46:21 +00:00
if not len ( ids ) :
return [ ]
2010-07-06 11:21:27 +00:00
reads = self . read ( cr , uid , ids , [ ' name ' , ' location_id ' ] , context = context )
2009-09-24 10:46:21 +00:00
for record in reads :
name = record [ ' name ' ]
if context . get ( ' full ' , False ) :
if record [ ' location_id ' ] :
2010-07-06 11:21:27 +00:00
name = record [ ' location_id ' ] [ 1 ] + ' / ' + name
2009-09-24 10:46:21 +00:00
res . append ( ( record [ ' id ' ] , name ) )
else :
res . append ( ( record [ ' id ' ] , name ) )
return res
2010-07-06 11:21:27 +00:00
def _complete_name ( self , cr , uid , ids , name , args , context = None ) :
2010-05-26 12:59:30 +00:00
""" Forms complete name of location from parent location to child location.
@return : Dictionary of values
"""
2008-09-17 07:49:51 +00:00
def _get_one_full_name ( location , level = 4 ) :
if location . location_id :
parent_path = _get_one_full_name ( location . location_id , level - 1 ) + " / "
else :
parent_path = ' '
return parent_path + location . name
res = { }
for m in self . browse ( cr , uid , ids , context = context ) :
res [ m . id ] = _get_one_full_name ( m )
return res
2010-07-06 11:21:27 +00:00
def _product_qty_available ( self , cr , uid , ids , field_names , arg , context = None ) :
2010-05-26 12:59:30 +00:00
""" Finds real and virtual quantity for product available at particular location.
@return : Dictionary of values
"""
2008-10-21 16:28:19 +00:00
res = { }
2008-09-17 08:59:36 +00:00
for id in ids :
2008-10-21 16:28:19 +00:00
res [ id ] = { } . fromkeys ( field_names , 0.0 )
if ( ' product_id ' not in context ) or not ids :
return res
2008-10-21 16:31:56 +00:00
for loc in ids :
2008-10-21 16:28:19 +00:00
context [ ' location ' ] = [ loc ]
prod = self . pool . get ( ' product.product ' ) . browse ( cr , uid , context [ ' product_id ' ] , context )
if ' stock_real ' in field_names :
res [ loc ] [ ' stock_real ' ] = prod . qty_available
if ' stock_virtual ' in field_names :
res [ loc ] [ ' stock_virtual ' ] = prod . virtual_available
2008-09-17 08:59:36 +00:00
return res
2009-02-11 13:32:54 +00:00
2010-07-06 11:21:27 +00:00
def product_detail ( self , cr , uid , id , field , context = None ) :
2010-06-16 08:40:57 +00:00
""" Finds detail of product like price type, currency and then calculates its price.
2010-05-26 12:59:30 +00:00
@param field : Field name
@return : Calculated price
"""
2009-01-21 12:38:28 +00:00
res = { }
res [ id ] = { }
final_value = 0.0
field_to_read = ' virtual_available '
2010-07-06 11:44:51 +00:00
if context is None :
context = { }
2009-01-21 12:38:28 +00:00
if field == ' stock_real_value ' :
field_to_read = ' qty_available '
2009-08-28 09:40:49 +00:00
cr . execute ( ' select distinct product_id from stock_move where (location_id= %s ) or (location_dest_id= %s ) ' , ( id , id ) )
2009-01-21 12:38:28 +00:00
result = cr . dictfetchall ( )
if result :
2009-12-23 16:34:53 +00:00
# Choose the right filed standard_price to read
# Take the user company
2010-05-26 12:59:30 +00:00
price_type_id = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . property_valuation_price_type . id
2010-07-06 11:21:27 +00:00
pricetype = self . pool . get ( ' product.price.type ' ) . browse ( cr , uid , price_type_id , context = context )
2009-01-21 12:38:28 +00:00
for r in result :
2009-06-10 00:24:29 +00:00
c = ( context or { } ) . copy ( )
c [ ' location ' ] = id
2010-03-25 10:59:36 +00:00
product = self . pool . get ( ' product.product ' ) . read ( cr , uid , r [ ' product_id ' ] , [ field_to_read ] , context = c )
# Compute the amount_unit in right currency
2010-05-26 12:59:30 +00:00
context [ ' currency_id ' ] = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . currency_id . id
amount_unit = self . pool . get ( ' product.product ' ) . browse ( cr , uid , r [ ' product_id ' ] ) . price_get ( pricetype . field , context ) [ r [ ' product_id ' ] ]
2010-03-25 10:59:36 +00:00
final_value + = ( product [ field_to_read ] * amount_unit )
2009-01-21 12:38:28 +00:00
return final_value
2009-02-11 13:32:54 +00:00
2010-07-06 11:21:27 +00:00
def _product_value ( self , cr , uid , ids , field_names , arg , context = None ) :
2010-05-26 12:59:30 +00:00
""" Calculates real and virtual stock value of a product.
2010-06-16 08:40:57 +00:00
@param field_names : Name of field
2010-05-26 12:59:30 +00:00
@return : Dictionary of values
"""
2009-01-21 12:38:28 +00:00
result = { }
for id in ids :
result [ id ] = { } . fromkeys ( field_names , 0.0 )
for field_name in field_names :
for loc in ids :
2009-08-28 09:40:49 +00:00
ret_dict = self . product_detail ( cr , uid , loc , field = field_name )
2009-01-21 12:38:28 +00:00
result [ loc ] [ field_name ] = ret_dict
return result
2009-02-11 13:32:54 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
2008-08-26 22:18:32 +00:00
' name ' : fields . char ( ' Location Name ' , size = 64 , required = True , translate = True ) ,
2009-12-21 12:21:32 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the stock location without removing it. " ) ,
2010-08-30 12:03:58 +00:00
' usage ' : fields . selection ( [ ( ' supplier ' , ' Supplier Location ' ) , ( ' view ' , ' View ' ) , ( ' internal ' , ' Internal Location ' ) , ( ' customer ' , ' Customer Location ' ) , ( ' inventory ' , ' Inventory ' ) , ( ' procurement ' , ' Procurement ' ) , ( ' production ' , ' Production ' ) , ( ' transit ' , ' Transit Location for Inter-Companies Transfers ' ) ] , ' Location Type ' , required = True ,
help = ' * The \' Supplier Location \' Source of products received from suppliers. \
\n * The \' View \' Shows that the location is only an organizational node for the hierarchical structure, and can be involved in stock moves itself. \
\n * The \' Internal Location \' Locations for your own stock,. \
\n * The \' Customer Location \' Destination for products sent to customers. \
\n * The \' Inventory \' The counterpart for inventory operations used to correct stock levels,. \
\n * The \' Procurement \' The counterpart for procurement operations when you dont yet know the source (supplier or production). Products in this location should be zero after the scheduler run completes. \
\n * The \' Production \' The counterpart for production operations; receipt of raw material and sending finished products. \
' ),
2009-08-28 09:40:49 +00:00
' allocation_method ' : fields . selection ( [ ( ' fifo ' , ' FIFO ' ) , ( ' lifo ' , ' LIFO ' ) , ( ' nearest ' , ' Nearest ' ) ] , ' Allocation Method ' , required = True ) ,
2008-09-17 07:49:51 +00:00
' complete_name ' : fields . function ( _complete_name , method = True , type = ' char ' , size = 100 , string = " Location Name " ) ,
2008-09-17 08:59:36 +00:00
2008-10-21 16:28:19 +00:00
' stock_real ' : fields . function ( _product_qty_available , method = True , type = ' float ' , string = ' Real Stock ' , multi = " stock " ) ,
' stock_virtual ' : fields . function ( _product_qty_available , method = True , type = ' float ' , string = ' Virtual Stock ' , multi = " stock " ) ,
2008-09-17 07:49:51 +00:00
2008-09-17 08:59:36 +00:00
' location_id ' : fields . many2one ( ' stock.location ' , ' Parent Location ' , select = True , ondelete = ' cascade ' ) ,
2008-07-22 15:11:28 +00:00
' child_ids ' : fields . one2many ( ' stock.location ' , ' location_id ' , ' Contains ' ) ,
2010-08-30 12:55:05 +00:00
' chained_journal_id ' : fields . many2one ( ' stock.journal ' , ' Chaining Journal ' , help = " Inventory Journal in which the chained move will be written, if the Chaining Type is not Transparent (no journal is used if left empty) " ) ,
2008-07-22 15:11:28 +00:00
' chained_location_id ' : fields . many2one ( ' stock.location ' , ' Chained Location If Fixed ' ) ,
2009-08-28 09:40:49 +00:00
' chained_location_type ' : fields . selection ( [ ( ' none ' , ' None ' ) , ( ' customer ' , ' Customer ' ) , ( ' fixed ' , ' Fixed Location ' ) ] ,
2010-08-19 07:25:01 +00:00
' Chained Location Type ' , required = True ,
help = " This field is set to determine the destination location. \n " \
" If the field is set to ' customer ' , the location is given by the properties of the partner form. \n " \
" If the field is set to ' fixed ' , the destination location is given by the field Location if link is fixed. " ) ,
2008-09-07 23:24:39 +00:00
' chained_auto_packing ' : fields . selection (
2009-08-28 09:40:49 +00:00
[ ( ' auto ' , ' Automatic Move ' ) , ( ' manual ' , ' Manual Operation ' ) , ( ' transparent ' , ' Automatic No Step Added ' ) ] ,
2010-08-30 12:55:05 +00:00
' Chaining Type ' ,
2008-09-17 09:10:14 +00:00
required = True ,
2009-12-21 09:50:54 +00:00
help = " This is used only if you select a chained location type. \n " \
2008-09-07 23:24:39 +00:00
" The ' Automatic Move ' value will create a stock move after the current one that will be " \
" validated automatically. With ' Manual Operation ' , the stock move has to be validated " \
" by a worker. With ' Automatic No Step Added ' , the location is replaced in the original move. "
) ,
2010-08-30 12:55:05 +00:00
' chained_picking_type ' : fields . selection ( [ ( ' out ' , ' Sending Goods ' ) , ( ' in ' , ' Getting Goods ' ) , ( ' internal ' , ' Internal ' ) , ( ' delivery ' , ' Delivery ' ) ] , ' Shipping Type ' , help = " Shipping Type of the Picking List that will contain the chained move (leave empty to automatically detect the type based on the source and destination locations). " ) ,
' chained_company_id ' : fields . many2one ( ' res.company ' , ' Chained Company ' , help = ' The company the Picking List containing the chained move will belong to (leave empty to use the default company determination rules ' ) ,
' chained_delay ' : fields . integer ( ' Chaining Lead Time ' , help = " Delay between original move and chained move in days " ) ,
2008-07-22 15:11:28 +00:00
' address_id ' : fields . many2one ( ' res.partner.address ' , ' Location Address ' ) ,
2010-08-30 12:55:05 +00:00
' icon ' : fields . selection ( tools . icons , ' Icon ' , size = 64 , help = " Icon show in hierarchical tree view " ) ,
2008-07-22 15:11:28 +00:00
' comment ' : fields . text ( ' Additional Information ' ) ,
2008-09-07 23:24:39 +00:00
' posx ' : fields . integer ( ' Corridor (X) ' ) ,
' posy ' : fields . integer ( ' Shelves (Y) ' ) ,
' posz ' : fields . integer ( ' Height (Z) ' ) ,
2008-09-17 08:59:36 +00:00
' parent_left ' : fields . integer ( ' Left Parent ' , select = 1 ) ,
' parent_right ' : fields . integer ( ' Right Parent ' , select = 1 ) ,
2009-01-21 12:38:28 +00:00
' stock_real_value ' : fields . function ( _product_value , method = True , type = ' float ' , string = ' Real Stock Value ' , multi = " stock " ) ,
' stock_virtual_value ' : fields . function ( _product_value , method = True , type = ' float ' , string = ' Virtual Stock Value ' , multi = " stock " ) ,
2010-06-10 12:44:52 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 , help = ' Let this field empty if this location is shared for every companies ' ) ,
2010-08-30 11:33:00 +00:00
' scrap_location ' : fields . boolean ( ' Scrap Location ' , help = ' Check this box to allow using this location to put scrapped/damaged goods. ' ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-07-06 11:44:51 +00:00
' active ' : True ,
2010-07-06 11:21:27 +00:00
' usage ' : ' internal ' ,
' allocation_method ' : ' fifo ' ,
' chained_location_type ' : ' none ' ,
' chained_auto_packing ' : ' manual ' ,
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' stock.location ' , context = c ) ,
' posx ' : 0 ,
' posy ' : 0 ,
' posz ' : 0 ,
' icon ' : False ,
' scrap_location ' : False ,
2008-07-22 15:11:28 +00:00
}
2010-07-06 11:21:27 +00:00
def chained_location_get ( self , cr , uid , location , partner = None , product = None , context = None ) :
2010-05-26 12:59:30 +00:00
""" Finds chained location
@param location : Location id
@param partner : Partner id
@param product : Product id
@return : List of values
"""
2008-07-22 15:11:28 +00:00
result = None
2009-08-28 09:40:49 +00:00
if location . chained_location_type == ' customer ' :
2008-09-07 23:24:39 +00:00
if partner :
result = partner . property_stock_customer
2009-08-28 09:40:49 +00:00
elif location . chained_location_type == ' fixed ' :
2008-07-22 15:11:28 +00:00
result = location . chained_location_id
2008-09-07 23:24:39 +00:00
if result :
2010-06-15 10:37:07 +00:00
return result , location . chained_auto_packing , location . chained_delay , location . chained_journal_id and location . chained_journal_id . id or False , location . chained_company_id and location . chained_company_id . id or False , location . chained_picking_type
2008-07-22 15:11:28 +00:00
return result
2010-07-06 11:21:27 +00:00
def picking_type_get ( self , cr , uid , from_location , to_location , context = None ) :
2010-05-26 12:59:30 +00:00
""" Gets type of picking.
@param from_location : Source location
@param to_location : Destination location
@return : Location type
"""
2008-07-22 15:11:28 +00:00
result = ' internal '
2009-08-28 09:40:49 +00:00
if ( from_location . usage == ' internal ' ) and ( to_location and to_location . usage in ( ' customer ' , ' supplier ' ) ) :
2008-07-22 15:11:28 +00:00
result = ' delivery '
2010-07-06 11:21:27 +00:00
elif ( from_location . usage in ( ' supplier ' , ' customer ' ) ) and ( to_location . usage == ' internal ' ) :
2008-07-22 15:11:28 +00:00
result = ' in '
return result
2010-07-06 11:21:27 +00:00
def _product_get_all_report ( self , cr , uid , ids , product_ids = False , context = None ) :
return self . _product_get_report ( cr , uid , ids , product_ids , context , recursive = True )
2008-07-22 15:11:28 +00:00
def _product_get_report ( self , cr , uid , ids , product_ids = False ,
context = None , recursive = False ) :
2010-05-26 12:59:30 +00:00
""" Finds the product quantity and price for particular location.
@param product_ids : Ids of product
@param recursive : True or False
@return : Dictionary of values
"""
2008-07-22 15:11:28 +00:00
if context is None :
context = { }
product_obj = self . pool . get ( ' product.product ' )
2009-12-23 16:34:53 +00:00
# Take the user company and pricetype
2010-05-26 12:59:30 +00:00
price_type_id = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . property_valuation_price_type . id
pricetype = self . pool . get ( ' product.price.type ' ) . browse ( cr , uid , price_type_id )
context [ ' currency_id ' ] = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . currency_id . id
2010-03-18 11:25:02 +00:00
2008-07-22 15:11:28 +00:00
if not product_ids :
product_ids = product_obj . search ( cr , uid , [ ] )
products = product_obj . browse ( cr , uid , product_ids , context = context )
products_by_uom = { }
products_by_id = { }
for product in products :
products_by_uom . setdefault ( product . uom_id . id , [ ] )
products_by_uom [ product . uom_id . id ] . append ( product )
products_by_id . setdefault ( product . id , [ ] )
products_by_id [ product . id ] = product
2009-05-28 13:11:45 +00:00
result = { }
result [ ' product ' ] = [ ]
2008-07-22 15:11:28 +00:00
for id in ids :
2009-05-28 13:11:45 +00:00
quantity_total = 0.0
total_price = 0.0
2008-07-22 15:11:28 +00:00
for uom_id in products_by_uom . keys ( ) :
fnc = self . _product_get
if recursive :
fnc = self . _product_all_get
ctx = context . copy ( )
ctx [ ' uom ' ] = uom_id
qty = fnc ( cr , uid , id , [ x . id for x in products_by_uom [ uom_id ] ] ,
context = ctx )
for product_id in qty . keys ( ) :
if not qty [ product_id ] :
continue
product = products_by_id [ product_id ]
2009-05-28 13:11:45 +00:00
quantity_total + = qty [ product_id ]
2010-03-18 11:25:02 +00:00
2009-12-23 16:34:53 +00:00
# Compute based on pricetype
# Choose the right filed standard_price to read
2010-05-26 12:59:30 +00:00
amount_unit = product . price_get ( pricetype . field , context ) [ product . id ]
2009-12-23 16:34:53 +00:00
price = qty [ product_id ] * amount_unit
2009-05-28 13:11:45 +00:00
total_price + = price
result [ ' product ' ] . append ( {
2009-12-23 16:34:53 +00:00
' price ' : amount_unit ,
2009-05-28 13:11:45 +00:00
' prod_name ' : product . name ,
2008-07-22 15:11:28 +00:00
' code ' : product . default_code , # used by lot_overview_all report!
' variants ' : product . variants or ' ' ,
' uom ' : product . uom_id . name ,
2009-05-28 13:11:45 +00:00
' prod_qty ' : qty [ product_id ] ,
2009-08-28 09:40:49 +00:00
' price_value ' : price ,
2008-07-22 15:11:28 +00:00
} )
2009-05-28 13:11:45 +00:00
result [ ' total ' ] = quantity_total
result [ ' total_price ' ] = total_price
2008-07-22 15:11:28 +00:00
return result
2010-07-06 11:44:51 +00:00
def _product_get_multi_location ( self , cr , uid , ids , product_ids = False , context = None ,
2010-05-26 12:59:30 +00:00
states = [ ' done ' ] , what = ( ' in ' , ' out ' ) ) :
2010-06-16 08:40:57 +00:00
"""
2010-05-26 12:59:30 +00:00
@param product_ids : Ids of product
@param states : List of states
@param what : Tuple of
2010-06-16 08:40:57 +00:00
@return :
2010-05-26 12:59:30 +00:00
"""
2008-11-03 14:40:49 +00:00
product_obj = self . pool . get ( ' product.product ' )
2010-07-06 11:44:51 +00:00
if context is None :
context = { }
2008-09-23 13:24:11 +00:00
context . update ( {
2009-08-28 09:40:49 +00:00
' states ' : states ,
' what ' : what ,
' location ' : ids
2008-11-03 14:40:49 +00:00
} )
2009-08-28 09:40:49 +00:00
return product_obj . get_product_available ( cr , uid , product_ids , context = context )
2008-07-22 15:11:28 +00:00
2010-07-06 11:44:51 +00:00
def _product_get ( self , cr , uid , id , product_ids = False , context = None , states = [ ' done ' ] ) :
2010-05-26 12:59:30 +00:00
"""
@param product_ids :
@param states :
2010-06-16 08:40:57 +00:00
@return :
2010-05-26 12:59:30 +00:00
"""
2008-07-22 15:11:28 +00:00
ids = id and [ id ] or [ ]
2010-07-06 11:44:51 +00:00
return self . _product_get_multi_location ( cr , uid , ids , product_ids , context = context , states = states )
2008-07-22 15:11:28 +00:00
2010-07-06 11:44:51 +00:00
def _product_all_get ( self , cr , uid , id , product_ids = False , context = None , states = [ ' done ' ] ) :
2008-07-22 15:11:28 +00:00
# build the list of ids of children of the location given by id
ids = id and [ id ] or [ ]
location_ids = self . search ( cr , uid , [ ( ' location_id ' , ' child_of ' , ids ) ] )
return self . _product_get_multi_location ( cr , uid , location_ids , product_ids , context , states )
2010-07-06 11:44:51 +00:00
def _product_virtual_get ( self , cr , uid , id , product_ids = False , context = None , states = [ ' done ' ] ) :
2009-08-28 09:40:49 +00:00
return self . _product_all_get ( cr , uid , id , product_ids , context , [ ' confirmed ' , ' waiting ' , ' assigned ' , ' done ' ] )
2008-07-22 15:11:28 +00:00
2010-07-06 11:44:51 +00:00
def _product_reserve ( self , cr , uid , ids , product_id , product_qty , context = None ) :
2010-06-16 08:40:57 +00:00
"""
2010-05-26 12:59:30 +00:00
@param product_id : Id of product
@param product_qty : Quantity of product
@return : List of Values or False
"""
2008-07-22 15:11:28 +00:00
result = [ ]
amount = 0.0
2010-07-06 11:44:51 +00:00
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
for id in self . search ( cr , uid , [ ( ' location_id ' , ' child_of ' , ids ) ] ) :
2009-08-28 09:40:49 +00:00
cr . execute ( " select product_uom,sum(product_qty) as product_qty from stock_move where location_dest_id= %s and location_id<> %s and product_id= %s and state= ' done ' group by product_uom " , ( id , id , product_id ) )
2008-07-22 15:11:28 +00:00
results = cr . dictfetchall ( )
2009-08-28 09:40:49 +00:00
cr . execute ( " select product_uom,-sum(product_qty) as product_qty from stock_move where location_id= %s and location_dest_id<> %s and product_id= %s and state in ( ' done ' , ' assigned ' ) group by product_uom " , ( id , id , product_id ) )
2008-07-22 15:11:28 +00:00
results + = cr . dictfetchall ( )
total = 0.0
results2 = 0.0
for r in results :
2009-08-28 09:40:49 +00:00
amount = self . pool . get ( ' product.uom ' ) . _compute_qty ( cr , uid , r [ ' product_uom ' ] , r [ ' product_qty ' ] , context . get ( ' uom ' , False ) )
2008-07-22 15:11:28 +00:00
results2 + = amount
total + = amount
2009-08-28 09:40:49 +00:00
if total < = 0.0 :
2008-07-22 15:11:28 +00:00
continue
amount = results2
2009-08-28 09:40:49 +00:00
if amount > 0 :
if amount > min ( total , product_qty ) :
amount = min ( product_qty , total )
result . append ( ( amount , id ) )
2008-07-22 15:11:28 +00:00
product_qty - = amount
total - = amount
2009-08-28 09:40:49 +00:00
if product_qty < = 0.0 :
2008-07-22 15:11:28 +00:00
return result
2009-08-28 09:40:49 +00:00
if total < = 0.0 :
2008-07-22 15:11:28 +00:00
continue
return False
2009-08-28 09:40:49 +00:00
2006-12-07 13:41:40 +00:00
stock_location ( )
2009-08-28 09:40:49 +00:00
2006-12-07 13:41:40 +00:00
class stock_tracking ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.tracking "
2010-06-26 15:15:23 +00:00
_description = " Packs "
2010-05-28 05:18:58 +00:00
2008-07-22 15:11:28 +00:00
def checksum ( sscc ) :
salt = ' 31 ' * 8 + ' 3 '
sum = 0
for sscc_part , salt_part in zip ( sscc , salt ) :
sum + = int ( sscc_part ) * int ( salt_part )
return ( 10 - ( sum % 10 ) ) % 10
checksum = staticmethod ( checksum )
2010-07-06 11:21:27 +00:00
def make_sscc ( self , cr , uid , context = None ) :
2008-07-22 15:11:28 +00:00
sequence = self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' stock.lot.tracking ' )
return sequence + str ( self . checksum ( sequence ) )
_columns = {
2009-12-21 09:50:54 +00:00
' name ' : fields . char ( ' Tracking ID ' , size = 64 , required = True ) ,
2010-06-26 15:15:23 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to true, it will allow you to hide the pack without removing it. " ) ,
2008-07-22 15:11:28 +00:00
' serial ' : fields . char ( ' Reference ' , size = 64 ) ,
2009-08-28 09:40:49 +00:00
' move_ids ' : fields . one2many ( ' stock.move ' , ' tracking_id ' , ' Moves Tracked ' ) ,
2010-02-23 16:05:50 +00:00
' date ' : fields . datetime ( ' Created Date ' , required = True ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-07-06 11:21:27 +00:00
' active ' : 1 ,
2009-08-28 09:40:49 +00:00
' name ' : make_sscc ,
2010-07-06 11:21:27 +00:00
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2008-07-22 15:11:28 +00:00
}
2009-12-09 11:43:34 +00:00
def name_search ( self , cr , user , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2008-07-22 15:11:28 +00:00
if not args :
2009-08-28 09:40:49 +00:00
args = [ ]
2008-07-22 15:11:28 +00:00
if not context :
2009-08-28 09:40:49 +00:00
context = { }
ids = self . search ( cr , user , [ ( ' serial ' , ' = ' , name ) ] + args , limit = limit , context = context )
ids + = self . search ( cr , user , [ ( ' name ' , operator , name ) ] + args , limit = limit , context = context )
2008-07-22 15:11:28 +00:00
return self . name_get ( cr , user , ids , context )
2010-07-06 11:21:27 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
if not len ( ids ) :
return [ ]
2009-08-28 09:40:49 +00:00
res = [ ( r [ ' id ' ] , r [ ' name ' ] + ' [ ' + ( r [ ' serial ' ] or ' ' ) + ' ] ' ) for r in self . read ( cr , uid , ids , [ ' name ' , ' serial ' ] , context ) ]
2008-07-22 15:11:28 +00:00
return res
2009-08-28 09:40:49 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2009-01-06 12:44:51 +00:00
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' You can not remove a lot line ! ' ) )
2009-08-28 09:40:49 +00:00
2006-12-07 13:41:40 +00:00
stock_tracking ( )
#----------------------------------------------------------
# Stock Picking
#----------------------------------------------------------
class stock_picking ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.picking "
2009-11-13 05:41:16 +00:00
_description = " Picking List "
2009-08-28 09:40:49 +00:00
2010-07-06 11:21:27 +00:00
def _set_maximum_date ( self , cr , uid , ids , name , value , arg , context = None ) :
2010-05-26 12:59:30 +00:00
""" Calculates planned date if it is greater than ' value ' .
@param name : Name of field
@param value : Value of field
2010-06-16 08:40:57 +00:00
@param arg : User defined argument
2010-05-26 12:59:30 +00:00
@return : True or False
"""
2009-08-28 09:40:49 +00:00
if not value :
return False
2008-10-13 06:22:33 +00:00
if isinstance ( ids , ( int , long ) ) :
2009-08-28 09:40:49 +00:00
ids = [ ids ]
2008-09-16 10:37:53 +00:00
for pick in self . browse ( cr , uid , ids , context ) :
2009-08-28 09:40:49 +00:00
sql_str = """ update stock_move set
2008-10-14 13:23:55 +00:00
date_planned = ' %s '
2008-09-16 10:37:53 +00:00
where
2009-08-28 09:40:49 +00:00
picking_id = % d """ % (value, pick.id)
2008-11-03 14:40:49 +00:00
2008-10-14 13:23:55 +00:00
if pick . max_date :
2009-08-28 09:40:49 +00:00
sql_str + = " and (date_planned= ' " + pick . max_date + " ' or date_planned> ' " + value + " ' ) "
2008-10-14 13:23:55 +00:00
cr . execute ( sql_str )
2008-09-16 10:37:53 +00:00
return True
2008-09-05 07:12:28 +00:00
2010-07-06 11:21:27 +00:00
def _set_minimum_date ( self , cr , uid , ids , name , value , arg , context = None ) :
2010-05-26 12:59:30 +00:00
""" Calculates planned date if it is less than ' value ' .
@param name : Name of field
@param value : Value of field
2010-06-16 08:40:57 +00:00
@param arg : User defined argument
2010-05-26 12:59:30 +00:00
@return : True or False
"""
2009-08-28 09:40:49 +00:00
if not value :
return False
2008-10-13 06:22:33 +00:00
if isinstance ( ids , ( int , long ) ) :
2009-08-28 09:40:49 +00:00
ids = [ ids ]
2010-07-06 11:21:27 +00:00
for pick in self . browse ( cr , uid , ids , context = context ) :
2009-08-28 09:40:49 +00:00
sql_str = """ update stock_move set
2008-11-03 14:40:49 +00:00
date_planned = ' %s '
2008-09-16 10:37:53 +00:00
where
2009-08-28 09:40:49 +00:00
picking_id = % s """ % (value, pick.id)
2008-10-14 13:23:55 +00:00
if pick . min_date :
2009-08-28 09:40:49 +00:00
sql_str + = " and (date_planned= ' " + pick . min_date + " ' or date_planned< ' " + value + " ' ) "
2008-10-14 13:23:55 +00:00
cr . execute ( sql_str )
2008-09-16 10:37:53 +00:00
return True
2008-09-05 07:12:28 +00:00
2010-07-06 11:21:27 +00:00
def get_min_max_date ( self , cr , uid , ids , field_name , arg , context = None ) :
2010-05-26 12:59:30 +00:00
""" Finds minimum and maximum dates for picking.
@return : Dictionary of values
"""
2008-09-05 07:12:28 +00:00
res = { }
2008-09-16 10:37:53 +00:00
for id in ids :
2009-08-28 09:40:49 +00:00
res [ id ] = { ' min_date ' : False , ' max_date ' : False }
2008-09-16 10:37:53 +00:00
if not ids :
return res
cr . execute ( """ select
picking_id ,
min ( date_planned ) ,
max ( date_planned )
from
2008-11-03 14:40:49 +00:00
stock_move
2008-09-16 10:37:53 +00:00
where
2010-06-16 11:51:39 +00:00
picking_id IN % s
2008-09-16 10:37:53 +00:00
group by
2010-06-16 11:51:39 +00:00
picking_id """ ,(tuple(ids),))
2009-08-28 09:40:49 +00:00
for pick , dt1 , dt2 in cr . fetchall ( ) :
2008-09-16 10:37:53 +00:00
res [ pick ] [ ' min_date ' ] = dt1
res [ pick ] [ ' max_date ' ] = dt2
2008-09-05 07:12:28 +00:00
return res
2008-09-16 10:37:53 +00:00
2009-02-27 07:04:59 +00:00
def create ( self , cr , user , vals , context = None ) :
2009-08-16 20:16:51 +00:00
if ( ' name ' not in vals ) or ( vals . get ( ' name ' ) == ' / ' ) :
2010-05-24 09:32:50 +00:00
seq_obj_name = ' stock.picking. ' + vals [ ' type ' ]
vals [ ' name ' ] = self . pool . get ( ' ir.sequence ' ) . get ( cr , user , seq_obj_name )
2010-06-10 09:32:15 +00:00
new_id = super ( stock_picking , self ) . create ( cr , user , vals , context )
return new_id
2009-01-09 13:48:17 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
2009-02-05 16:41:33 +00:00
' name ' : fields . char ( ' Reference ' , size = 64 , select = True ) ,
2010-02-17 15:16:52 +00:00
' origin ' : fields . char ( ' Origin ' , size = 64 , help = " Reference of the document that produced this picking. " ) ,
2010-08-31 07:07:18 +00:00
' backorder_id ' : fields . many2one ( ' stock.picking ' , ' Back Order of ' , help = " If this picking was split this field links to the picking that contains the other part that has been processed already. " ) ,
2009-09-24 10:46:21 +00:00
' type ' : fields . selection ( [ ( ' out ' , ' Sending Goods ' ) , ( ' in ' , ' Getting Goods ' ) , ( ' internal ' , ' Internal ' ) , ( ' delivery ' , ' Delivery ' ) ] , ' Shipping Type ' , required = True , select = True , help = " Shipping type specify, goods coming in or going out. " ) ,
2008-07-22 15:11:28 +00:00
' note ' : fields . text ( ' Notes ' ) ,
2010-06-15 08:14:32 +00:00
' stock_journal_id ' : fields . many2one ( ' stock.journal ' , ' Stock Journal ' ) ,
2009-09-24 10:46:21 +00:00
' location_id ' : fields . many2one ( ' stock.location ' , ' Location ' , help = " Keep empty if you produce at the location where the finished products are needed. " \
" Set a location if you produce at a fixed location. This can be a partner location " \
" if you subcontract the manufacturing operations. " ) ,
' location_dest_id ' : fields . many2one ( ' stock.location ' , ' Dest. Location ' , help = " Location where the system will stock the finished products. " ) ,
' move_type ' : fields . selection ( [ ( ' direct ' , ' Direct Delivery ' ) , ( ' one ' , ' All at once ' ) ] , ' Delivery Method ' , required = True , help = " It specifies goods to be delivered all at once or by direct delivery " ) ,
2008-07-22 15:11:28 +00:00
' state ' : fields . selection ( [
2009-08-28 09:40:49 +00:00
( ' draft ' , ' Draft ' ) ,
( ' auto ' , ' Waiting ' ) ,
( ' confirmed ' , ' Confirmed ' ) ,
( ' assigned ' , ' Available ' ) ,
( ' done ' , ' Done ' ) ,
2009-09-24 10:46:21 +00:00
( ' cancel ' , ' Cancelled ' ) ,
2009-11-23 13:52:38 +00:00
] , ' State ' , readonly = True , select = True ,
2010-08-30 10:51:18 +00:00
help = ' * The \' Draft \' Not confirmed yet and will not be scheduled until confirmed picking. \
\n * The \' Confirmed \' Still waiting for the availability of products. \
\n * The \' Available \' Products reserved, simply waiting for confirmation. \
\n * The \' Waiting \' Waiting for another move to proceed before it becomes automatically available. (e.g. in Make-To-Order flows). \
\n * The \' Done \' Has been processed, can not be modified or cancelled anymore. \
\n * The \' Cancelled \' Has been cancelled, can not be confirmed anymore. \
2010-08-30 06:14:07 +00:00
' ),
2008-09-16 10:37:53 +00:00
' min_date ' : fields . function ( get_min_max_date , fnct_inv = _set_minimum_date , multi = " min_max_date " ,
2010-08-30 07:12:29 +00:00
method = True , store = True , type = ' datetime ' , string = ' Expected Date ' , select = 1 , help = " Expected date for the picking to be processed. Will be set to date of actual processing if not specified. " ) ,
2010-02-17 15:16:52 +00:00
' date ' : fields . datetime ( ' Order Date ' , help = " Date of Order " ) ,
2010-05-11 21:27:24 +00:00
' date_done ' : fields . datetime ( ' Date Done ' , help = " Date of Completion " ) ,
2008-09-16 10:37:53 +00:00
' max_date ' : fields . function ( get_min_max_date , fnct_inv = _set_maximum_date , multi = " min_max_date " ,
2010-03-05 20:47:58 +00:00
method = True , store = True , type = ' datetime ' , string = ' Max. Expected Date ' , select = 2 ) ,
2010-05-10 05:46:20 +00:00
' move_lines ' : fields . one2many ( ' stock.move ' , ' picking_id ' , ' Internal Moves ' , states = { ' done ' : [ ( ' readonly ' , True ) ] , ' cancel ' : [ ( ' readonly ' , True ) ] } ) ,
2009-11-13 05:41:16 +00:00
' auto_picking ' : fields . boolean ( ' Auto-Picking ' ) ,
2009-09-24 10:46:21 +00:00
' address_id ' : fields . many2one ( ' res.partner.address ' , ' Partner ' , help = " Address of partner " ) ,
2009-08-28 09:40:49 +00:00
' invoice_state ' : fields . selection ( [
( " invoiced " , " Invoiced " ) ,
( " 2binvoiced " , " To Be Invoiced " ) ,
2009-11-13 05:41:16 +00:00
( " none " , " Not from Picking " ) ] , " Invoice Status " ,
2009-08-28 09:40:49 +00:00
select = True , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-06-16 10:08:18 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True , select = 1 ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2009-08-28 09:40:49 +00:00
' name ' : lambda self , cr , uid , context : ' / ' ,
2010-07-06 11:21:27 +00:00
' state ' : ' draft ' ,
' move_type ' : ' direct ' ,
' type ' : ' in ' ,
' invoice_state ' : ' none ' ,
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' stock.picking ' , context = c )
2008-07-22 15:11:28 +00:00
}
2009-08-28 09:40:49 +00:00
2010-07-06 11:21:27 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
2009-02-05 16:41:33 +00:00
if default is None :
default = { }
default = default . copy ( )
2010-05-28 07:37:54 +00:00
picking_obj = self . browse ( cr , uid , [ id ] , context ) [ 0 ]
2010-05-28 09:35:21 +00:00
if ( ' name ' not in default ) or ( picking_obj . name == ' / ' ) :
2010-05-28 07:37:54 +00:00
seq_obj_name = ' stock.picking. ' + picking_obj . type
2010-06-16 08:40:57 +00:00
default [ ' name ' ] = self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , seq_obj_name )
2010-08-30 06:49:19 +00:00
default [ ' origin ' ] = ' '
default [ ' backorder_id ' ] = False
2009-02-05 16:41:33 +00:00
return super ( stock_picking , self ) . copy ( cr , uid , id , default , context )
2008-09-07 23:24:39 +00:00
2010-07-06 11:21:27 +00:00
def onchange_partner_in ( self , cr , uid , context = None , partner_id = None ) :
2009-08-28 09:40:49 +00:00
return { }
2008-07-22 15:11:28 +00:00
2010-07-06 11:21:27 +00:00
def action_explode ( self , cr , uid , moves , context = None ) :
2008-09-07 23:24:39 +00:00
return moves
2008-07-22 15:11:28 +00:00
2010-07-06 11:21:27 +00:00
def action_confirm ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Confirms picking.
2010-06-16 08:40:57 +00:00
@return : True
2010-05-26 12:59:30 +00:00
"""
2009-02-05 17:11:04 +00:00
self . write ( cr , uid , ids , { ' state ' : ' confirmed ' } )
2008-07-22 15:11:28 +00:00
todo = [ ]
2010-05-19 20:54:46 +00:00
for picking in self . browse ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
for r in picking . move_lines :
2009-08-28 09:40:49 +00:00
if r . state == ' draft ' :
2008-11-05 09:25:04 +00:00
todo . append ( r . id )
2010-07-23 10:11:19 +00:00
self . log_picking ( cr , uid , ids , context = context )
2008-09-07 23:24:39 +00:00
todo = self . action_explode ( cr , uid , todo , context )
2008-07-22 15:11:28 +00:00
if len ( todo ) :
2010-07-06 11:21:27 +00:00
self . pool . get ( ' stock.move ' ) . action_confirm ( cr , uid , todo , context = context )
2008-07-22 15:11:28 +00:00
return True
def test_auto_picking ( self , cr , uid , ids ) :
# TODO: Check locations to see if in the same location ?
return True
def action_assign ( self , cr , uid , ids , * args ) :
2010-05-26 12:59:30 +00:00
""" Changes state of picking to available if all moves are confirmed.
@return : True
"""
2008-07-22 15:11:28 +00:00
for pick in self . browse ( cr , uid , ids ) :
2009-08-28 09:40:49 +00:00
move_ids = [ x . id for x in pick . move_lines if x . state == ' confirmed ' ]
2010-03-18 07:12:23 +00:00
if not move_ids :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Not Available. Moves are not confirmed. ' ) )
2008-07-22 15:11:28 +00:00
self . pool . get ( ' stock.move ' ) . action_assign ( cr , uid , move_ids )
return True
def force_assign ( self , cr , uid , ids , * args ) :
2010-05-26 12:59:30 +00:00
""" Changes state of picking to available if moves are confirmed or waiting.
@return : True
"""
2008-07-22 15:11:28 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for pick in self . browse ( cr , uid , ids ) :
2009-12-10 11:55:52 +00:00
move_ids = [ x . id for x in pick . move_lines if x . state in [ ' confirmed ' , ' waiting ' ] ]
2008-07-22 15:11:28 +00:00
self . pool . get ( ' stock.move ' ) . force_assign ( cr , uid , move_ids )
wf_service . trg_write ( uid , ' stock.picking ' , pick . id , cr )
return True
2008-11-03 14:40:49 +00:00
2008-09-17 11:54:26 +00:00
def draft_force_assign ( self , cr , uid , ids , * args ) :
2010-05-26 12:59:30 +00:00
""" Confirms picking directly from draft state.
@return : True
"""
2008-09-17 11:54:26 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for pick in self . browse ( cr , uid , ids ) :
2010-06-21 08:53:40 +00:00
if not pick . move_lines :
2010-06-21 10:44:15 +00:00
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' You can not process picking without stock moves ' ) )
2008-09-17 11:54:26 +00:00
wf_service . trg_validate ( uid , ' stock.picking ' , pick . id ,
' button_confirm ' , cr )
return True
2008-11-03 14:40:49 +00:00
2008-09-17 11:54:26 +00:00
def draft_validate ( self , cr , uid , ids , * args ) :
2010-05-26 12:59:30 +00:00
""" Validates picking directly from draft state.
@return : True
"""
2008-09-17 11:54:26 +00:00
wf_service = netsvc . LocalService ( " workflow " )
self . draft_force_assign ( cr , uid , ids )
for pick in self . browse ( cr , uid , ids ) :
2008-12-14 17:23:47 +00:00
move_ids = [ x . id for x in pick . move_lines ]
self . pool . get ( ' stock.move ' ) . force_assign ( cr , uid , move_ids )
wf_service . trg_write ( uid , ' stock.picking ' , pick . id , cr )
2008-09-17 11:54:26 +00:00
self . action_move ( cr , uid , [ pick . id ] )
2009-08-28 09:40:49 +00:00
wf_service . trg_validate ( uid , ' stock.picking ' , pick . id , ' button_done ' , cr )
2008-09-17 11:54:26 +00:00
return True
2008-11-03 14:40:49 +00:00
2008-07-22 15:11:28 +00:00
def cancel_assign ( self , cr , uid , ids , * args ) :
2010-06-16 08:40:57 +00:00
""" Cancels picking and moves.
2010-05-26 12:59:30 +00:00
@return : True
"""
2008-07-22 15:11:28 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for pick in self . browse ( cr , uid , ids ) :
move_ids = [ x . id for x in pick . move_lines ]
self . pool . get ( ' stock.move ' ) . cancel_assign ( cr , uid , move_ids )
wf_service . trg_write ( uid , ' stock.picking ' , pick . id , cr )
return True
2010-05-19 20:02:36 +00:00
def action_assign_wkf ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Changes picking state to assigned.
@return : True
"""
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , ids , { ' state ' : ' assigned ' } )
2010-07-23 10:11:19 +00:00
self . log_picking ( cr , uid , ids , context = context )
2008-07-22 15:11:28 +00:00
return True
def test_finnished ( self , cr , uid , ids ) :
2010-05-26 12:59:30 +00:00
""" Tests whether the move is in done or cancel state or not.
@return : True or False
"""
2009-08-28 09:40:49 +00:00
move_ids = self . pool . get ( ' stock.move ' ) . search ( cr , uid , [ ( ' picking_id ' , ' in ' , ids ) ] )
for move in self . pool . get ( ' stock.move ' ) . browse ( cr , uid , move_ids ) :
if move . state not in ( ' done ' , ' cancel ' ) :
2008-07-22 15:11:28 +00:00
if move . product_qty != 0.0 :
return False
else :
2009-08-28 09:40:49 +00:00
move . write ( cr , uid , [ move . id ] , { ' state ' : ' done ' } )
2008-07-22 15:11:28 +00:00
return True
def test_assigned ( self , cr , uid , ids ) :
2010-05-26 12:59:30 +00:00
""" Tests whether the move is in assigned state or not.
@return : True or False
"""
2008-07-22 15:11:28 +00:00
ok = True
for pick in self . browse ( cr , uid , ids ) :
mt = pick . move_type
for move in pick . move_lines :
2010-05-26 12:59:30 +00:00
if ( move . state in ( ' confirmed ' , ' draft ' ) ) and ( mt == ' one ' ) :
2008-07-22 15:11:28 +00:00
return False
2010-05-26 12:59:30 +00:00
if ( mt == ' direct ' ) and ( move . state == ' assigned ' ) and ( move . product_qty ) :
2008-07-22 15:11:28 +00:00
return True
2009-08-28 09:40:49 +00:00
ok = ok and ( move . state in ( ' cancel ' , ' done ' , ' assigned ' ) )
2008-07-22 15:11:28 +00:00
return ok
2010-07-06 11:44:51 +00:00
def action_cancel ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Changes picking state to cancel.
@return : True
"""
2008-07-22 15:11:28 +00:00
for pick in self . browse ( cr , uid , ids ) :
ids2 = [ move . id for move in pick . move_lines ]
self . pool . get ( ' stock.move ' ) . action_cancel ( cr , uid , ids2 , context )
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , ids , { ' state ' : ' cancel ' , ' invoice_state ' : ' none ' } )
2010-07-23 10:11:19 +00:00
self . log_picking ( cr , uid , ids , context = context )
2008-07-22 15:11:28 +00:00
return True
#
# TODO: change and create a move if not parents
#
def action_done ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Changes picking state to done.
@return : True
"""
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , ids , { ' state ' : ' done ' , ' date_done ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } )
2008-07-22 15:11:28 +00:00
return True
2010-07-06 11:44:51 +00:00
def action_move ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Changes move state to assigned.
@return : True
"""
2008-07-22 15:11:28 +00:00
for pick in self . browse ( cr , uid , ids ) :
todo = [ ]
for move in pick . move_lines :
2009-08-28 09:40:49 +00:00
if move . state == ' assigned ' :
2008-07-22 15:11:28 +00:00
todo . append ( move . id )
if len ( todo ) :
self . pool . get ( ' stock.move ' ) . action_done ( cr , uid , todo ,
context = context )
return True
2010-03-29 14:29:43 +00:00
def get_currency_id ( self , cr , uid , picking ) :
2008-11-05 16:36:08 +00:00
return False
2010-03-29 14:29:43 +00:00
def _get_payment_term ( self , cr , uid , picking ) :
2010-06-16 08:40:57 +00:00
""" Gets payment term from partner.
2010-05-26 12:59:30 +00:00
@return : Payment term
"""
2008-11-06 22:15:58 +00:00
partner_obj = self . pool . get ( ' res.partner ' )
partner = picking . address_id . partner_id
return partner . property_payment_term and partner . property_payment_term . id or False
2010-03-29 14:29:43 +00:00
def _get_address_invoice ( self , cr , uid , picking ) :
2010-05-26 12:59:30 +00:00
""" Gets invoice address of a partner
@return { ' contact ' : address , ' invoice ' : address } for invoice
"""
2008-07-22 15:11:28 +00:00
partner_obj = self . pool . get ( ' res.partner ' )
partner = picking . address_id . partner_id
2010-03-29 14:29:43 +00:00
return partner_obj . address_get ( cr , uid , [ partner . id ] ,
2008-07-22 15:11:28 +00:00
[ ' contact ' , ' invoice ' ] )
2010-03-29 14:29:43 +00:00
def _get_comment_invoice ( self , cr , uid , picking ) :
2010-05-26 12:59:30 +00:00
"""
@return : comment string for invoice
"""
2008-07-22 15:11:28 +00:00
return picking . note or ' '
2010-03-29 14:29:43 +00:00
def _get_price_unit_invoice ( self , cr , uid , move_line , type , context = None ) :
2010-05-26 12:59:30 +00:00
""" Gets price unit for invoice
@param move_line : Stock move lines
@param type : Type of invoice
@return : The price unit for the move line
"""
2010-03-29 14:29:43 +00:00
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
if type in ( ' in_invoice ' , ' in_refund ' ) :
2009-12-23 16:34:53 +00:00
# Take the user company and pricetype
2010-03-29 14:29:43 +00:00
price_type_id = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . property_valuation_price_type . id
pricetype = self . pool . get ( ' product.price.type ' ) . browse ( cr , uid , price_type_id )
context [ ' currency_id ' ] = move_line . company_id . currency_id . id
2010-03-29 11:37:53 +00:00
2010-03-29 14:29:43 +00:00
amount_unit = move_line . product_id . price_get ( pricetype . field , context ) [ move_line . product_id . id ]
2009-12-23 16:34:53 +00:00
return amount_unit
2008-07-22 15:11:28 +00:00
else :
return move_line . product_id . list_price
2008-11-03 14:40:49 +00:00
2010-03-29 14:29:43 +00:00
def _get_discount_invoice ( self , cr , uid , move_line ) :
2008-07-22 15:11:28 +00:00
''' Return the discount for the move line '''
return 0.0
2010-03-29 14:29:43 +00:00
def _get_taxes_invoice ( self , cr , uid , move_line , type ) :
2010-05-26 12:59:30 +00:00
""" Gets taxes on invoice
@param move_line : Stock move lines
2010-06-16 08:40:57 +00:00
@param type : Type of invoice
2010-05-26 12:59:30 +00:00
@return : Taxes Ids for the move line
"""
2008-07-22 15:11:28 +00:00
if type in ( ' in_invoice ' , ' in_refund ' ) :
2008-10-21 16:28:19 +00:00
taxes = move_line . product_id . supplier_taxes_id
else :
taxes = move_line . product_id . taxes_id
if move_line . picking_id and move_line . picking_id . address_id and move_line . picking_id . address_id . partner_id :
return self . pool . get ( ' account.fiscal.position ' ) . map_tax (
2010-03-29 14:29:43 +00:00
cr ,
uid ,
2009-01-20 08:48:19 +00:00
move_line . picking_id . address_id . partner_id . property_account_position ,
2008-10-21 16:28:19 +00:00
taxes
)
2008-07-22 15:11:28 +00:00
else :
2008-10-21 16:28:19 +00:00
return map ( lambda x : x . id , taxes )
2008-07-22 15:11:28 +00:00
2010-03-29 14:29:43 +00:00
def _get_account_analytic_invoice ( self , cr , uid , picking , move_line ) :
2008-07-22 15:11:28 +00:00
return False
2010-03-29 14:29:43 +00:00
def _invoice_line_hook ( self , cr , uid , move_line , invoice_line_id ) :
2008-07-22 15:11:28 +00:00
''' Call after the creation of the invoice line '''
return
2010-03-29 14:29:43 +00:00
def _invoice_hook ( self , cr , uid , picking , invoice_id ) :
2008-07-22 15:11:28 +00:00
''' Call after the creation of the invoice '''
return
2010-03-29 14:29:43 +00:00
def action_invoice_create ( self , cr , uid , ids , journal_id = False ,
2008-07-22 15:11:28 +00:00
group = False , type = ' out_invoice ' , context = None ) :
2010-05-26 12:59:30 +00:00
""" Creates invoice based on the invoice state selected for picking.
@param journal_id : Id of journal
@param group : Whether to create a group invoice or not
@param type : Type invoice to be created
@return : Ids of created invoices for the pickings
"""
2010-03-29 14:29:43 +00:00
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
invoice_obj = self . pool . get ( ' account.invoice ' )
invoice_line_obj = self . pool . get ( ' account.invoice.line ' )
invoices_group = { }
res = { }
2008-11-03 14:40:49 +00:00
2010-03-29 14:29:43 +00:00
for picking in self . browse ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
if picking . invoice_state != ' 2binvoiced ' :
continue
2008-08-25 11:54:56 +00:00
payment_term_id = False
2008-09-26 10:25:10 +00:00
partner = picking . address_id and picking . address_id . partner_id
2008-10-28 12:10:01 +00:00
if not partner :
raise osv . except_osv ( _ ( ' Error, no partner ! ' ) ,
2008-10-27 07:23:22 +00:00
_ ( ' Please put a partner on the picking list if you want to generate invoice. ' ) )
2008-07-22 15:11:28 +00:00
if type in ( ' out_invoice ' , ' out_refund ' ) :
account_id = partner . property_account_receivable . id
2010-03-29 14:29:43 +00:00
payment_term_id = self . _get_payment_term ( cr , uid , picking )
2008-07-22 15:11:28 +00:00
else :
account_id = partner . property_account_payable . id
address_contact_id , address_invoice_id = \
2010-03-29 14:29:43 +00:00
self . _get_address_invoice ( cr , uid , picking ) . values ( )
2008-07-22 15:11:28 +00:00
2010-03-29 14:29:43 +00:00
comment = self . _get_comment_invoice ( cr , uid , picking )
2008-07-22 15:11:28 +00:00
if group and partner . id in invoices_group :
invoice_id = invoices_group [ partner . id ]
2010-03-29 14:29:43 +00:00
invoice = invoice_obj . browse ( cr , uid , invoice_id )
2008-11-18 06:46:13 +00:00
invoice_vals = {
2009-08-28 09:40:49 +00:00
' name ' : ( invoice . name or ' ' ) + ' , ' + ( picking . name or ' ' ) ,
' origin ' : ( invoice . origin or ' ' ) + ' , ' + ( picking . name or ' ' ) + ( picking . origin and ( ' : ' + picking . origin ) or ' ' ) ,
' comment ' : ( comment and ( invoice . comment and invoice . comment + " \n " + comment or comment ) ) or ( invoice . comment and invoice . comment or ' ' ) ,
2010-06-03 09:47:02 +00:00
' date_invoice ' : context . get ( ' date_inv ' , False ) ,
2010-08-13 12:20:05 +00:00
' user_id ' : uid
2009-01-20 08:48:19 +00:00
}
2010-03-29 14:29:43 +00:00
invoice_obj . write ( cr , uid , [ invoice_id ] , invoice_vals , context = context )
2008-07-22 15:11:28 +00:00
else :
invoice_vals = {
' name ' : picking . name ,
2009-02-26 16:59:01 +00:00
' origin ' : ( picking . name or ' ' ) + ( picking . origin and ( ' : ' + picking . origin ) or ' ' ) ,
2008-07-22 15:11:28 +00:00
' type ' : type ,
' account_id ' : account_id ,
' partner_id ' : partner . id ,
' address_invoice_id ' : address_invoice_id ,
' address_contact_id ' : address_contact_id ,
' comment ' : comment ,
' payment_term ' : payment_term_id ,
2009-09-24 10:46:21 +00:00
' fiscal_position ' : partner . property_account_position . id ,
2009-12-01 14:54:18 +00:00
' date_invoice ' : context . get ( ' date_inv ' , False ) ,
' company_id ' : picking . company_id . id ,
2010-08-04 06:44:30 +00:00
' user_id ' : uid
2010-08-13 12:20:05 +00:00
}
2010-03-29 14:29:43 +00:00
cur_id = self . get_currency_id ( cr , uid , picking )
2008-11-05 16:36:08 +00:00
if cur_id :
invoice_vals [ ' currency_id ' ] = cur_id
2008-07-22 15:11:28 +00:00
if journal_id :
invoice_vals [ ' journal_id ' ] = journal_id
2010-03-29 14:29:43 +00:00
invoice_id = invoice_obj . create ( cr , uid , invoice_vals ,
2008-07-22 15:11:28 +00:00
context = context )
invoices_group [ partner . id ] = invoice_id
res [ picking . id ] = invoice_id
for move_line in picking . move_lines :
2010-06-08 07:12:11 +00:00
origin = move_line . picking_id . name or ' '
2008-11-18 06:46:13 +00:00
if move_line . picking_id . origin :
2009-08-28 09:40:49 +00:00
origin + = ' : ' + move_line . picking_id . origin
2008-07-22 15:11:28 +00:00
if group :
2009-03-15 23:39:34 +00:00
name = ( picking . name or ' ' ) + ' - ' + move_line . name
2008-07-22 15:11:28 +00:00
else :
name = move_line . name
if type in ( ' out_invoice ' , ' out_refund ' ) :
account_id = move_line . product_id . product_tmpl_id . \
property_account_income . id
if not account_id :
account_id = move_line . product_id . categ_id . \
property_account_income_categ . id
else :
account_id = move_line . product_id . product_tmpl_id . \
property_account_expense . id
if not account_id :
account_id = move_line . product_id . categ_id . \
property_account_expense_categ . id
2010-03-29 14:29:43 +00:00
price_unit = self . _get_price_unit_invoice ( cr , uid ,
2008-07-22 15:11:28 +00:00
move_line , type )
2010-03-29 14:29:43 +00:00
discount = self . _get_discount_invoice ( cr , uid , move_line )
tax_ids = self . _get_taxes_invoice ( cr , uid , move_line , type )
account_analytic_id = self . _get_account_analytic_invoice ( cr , uid , picking , move_line )
2009-12-08 12:58:25 +00:00
2009-11-24 12:53:28 +00:00
#set UoS if it's a sale and the picking doesn't have one
uos_id = move_line . product_uos and move_line . product_uos . id or False
if not uos_id and type in ( ' out_invoice ' , ' out_refund ' ) :
uos_id = move_line . product_uom . id
2008-07-22 15:11:28 +00:00
2010-03-29 14:29:43 +00:00
account_id = self . pool . get ( ' account.fiscal.position ' ) . map_account ( cr , uid , partner . property_account_position , account_id )
2009-09-24 10:46:21 +00:00
notes = False
2010-06-08 11:23:27 +00:00
if ( ' sale_line_id ' in move_line . _columns . keys ( ) ) and move_line . sale_line_id :
2009-09-24 10:46:21 +00:00
notes = move_line . sale_line_id . notes
2010-06-08 11:23:27 +00:00
elif ( ' purchase_line_id ' in move_line . _columns . keys ( ) ) and move_line . purchase_line_id :
2009-09-24 10:46:21 +00:00
notes = move_line . purchase_line_id . notes
2009-11-13 05:41:16 +00:00
2010-03-29 14:29:43 +00:00
invoice_line_id = invoice_line_obj . create ( cr , uid , {
2008-07-22 15:11:28 +00:00
' name ' : name ,
2009-08-28 09:40:49 +00:00
' origin ' : origin ,
2008-07-22 15:11:28 +00:00
' invoice_id ' : invoice_id ,
2009-11-24 12:53:28 +00:00
' uos_id ' : uos_id ,
2008-07-22 15:11:28 +00:00
' product_id ' : move_line . product_id . id ,
' account_id ' : account_id ,
' price_unit ' : price_unit ,
' discount ' : discount ,
2008-10-21 16:28:19 +00:00
' quantity ' : move_line . product_uos_qty or move_line . product_qty ,
2008-07-22 15:11:28 +00:00
' invoice_line_tax_id ' : [ ( 6 , 0 , tax_ids ) ] ,
' account_analytic_id ' : account_analytic_id ,
2009-11-13 05:41:16 +00:00
' note ' : notes ,
2010-08-13 12:20:05 +00:00
} , context = context )
2010-03-29 14:29:43 +00:00
self . _invoice_line_hook ( cr , uid , move_line , invoice_line_id )
2008-07-22 15:11:28 +00:00
2010-03-29 14:29:43 +00:00
invoice_obj . button_compute ( cr , uid , [ invoice_id ] , context = context ,
2008-07-22 15:11:28 +00:00
set_total = ( type in ( ' in_invoice ' , ' in_refund ' ) ) )
2010-03-29 14:29:43 +00:00
self . write ( cr , uid , [ picking . id ] , {
2008-07-22 15:11:28 +00:00
' invoice_state ' : ' invoiced ' ,
} , context = context )
2010-03-29 14:29:43 +00:00
self . _invoice_hook ( cr , uid , picking , invoice_id )
self . write ( cr , uid , res . keys ( ) , {
2008-07-22 15:11:28 +00:00
' invoice_state ' : ' invoiced ' ,
} , context = context )
return res
2007-09-26 05:20:30 +00:00
2010-07-06 11:44:51 +00:00
def test_done ( self , cr , uid , ids , context = None ) :
2010-07-07 09:05:22 +00:00
""" Test whether the move lines are done or not.
@return : True or False
"""
2010-06-25 22:34:40 +00:00
ok = False
for pick in self . browse ( cr , uid , ids , context = context ) :
if not pick . move_lines :
return True
for move in pick . move_lines :
if move . state not in ( ' cancel ' , ' done ' ) :
return False
if move . state == ' done ' :
ok = True
return ok
2010-07-06 11:44:51 +00:00
def test_cancel ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Test whether the move lines are canceled or not.
@return : True or False
"""
2009-06-24 08:55:52 +00:00
for pick in self . browse ( cr , uid , ids , context = context ) :
if not pick . move_lines :
return False
for move in pick . move_lines :
if move . state not in ( ' cancel ' , ) :
return False
return True
2009-08-28 09:40:49 +00:00
2009-12-02 07:15:24 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2010-05-11 09:17:05 +00:00
move_obj = self . pool . get ( ' stock.move ' )
if not context :
context = { }
2009-12-02 07:15:24 +00:00
for pick in self . browse ( cr , uid , ids , context = context ) :
if pick . state in [ ' done ' , ' cancel ' ] :
raise osv . except_osv ( _ ( ' Error ' ) , _ ( ' You cannot remove the picking which is in %s state ! ' ) % ( pick . state , ) )
2010-05-11 09:17:05 +00:00
elif pick . state in [ ' confirmed ' , ' assigned ' , ' draft ' ] :
2009-12-02 07:15:24 +00:00
ids2 = [ move . id for move in pick . move_lines ]
2010-05-11 09:17:05 +00:00
ctx = context . copy ( )
ctx . update ( { ' call_unlink ' : True } )
if pick . state != ' draft ' :
#Cancelling the move in order to affect Virtual stock of product
move_obj . action_cancel ( cr , uid , ids2 , ctx )
#Removing the move
move_obj . unlink ( cr , uid , ids2 , ctx )
2010-05-28 05:18:58 +00:00
2009-12-02 07:15:24 +00:00
return super ( stock_picking , self ) . unlink ( cr , uid , ids , context = context )
2009-08-28 09:40:49 +00:00
2010-07-06 11:44:51 +00:00
def do_partial ( self , cr , uid , ids , partial_datas , context = None ) :
2010-05-26 12:59:30 +00:00
""" Makes partial picking and moves done.
@param partial_datas : Dictionary containing details of partial picking
2010-06-16 08:40:57 +00:00
like partner_id , address_id , delivery_date ,
2010-05-26 12:59:30 +00:00
delivery moves with product_id , product_qty , uom
@return : Dictionary of values
2010-03-25 12:08:31 +00:00
"""
res = { }
move_obj = self . pool . get ( ' stock.move ' )
product_obj = self . pool . get ( ' product.product ' )
currency_obj = self . pool . get ( ' res.currency ' )
users_obj = self . pool . get ( ' res.users ' )
uom_obj = self . pool . get ( ' product.uom ' )
price_type_obj = self . pool . get ( ' product.price.type ' )
2010-03-26 09:25:02 +00:00
sequence_obj = self . pool . get ( ' ir.sequence ' )
2010-03-25 12:08:31 +00:00
wf_service = netsvc . LocalService ( " workflow " )
partner_id = partial_datas . get ( ' partner_id ' , False )
address_id = partial_datas . get ( ' address_id ' , False )
delivery_date = partial_datas . get ( ' delivery_date ' , False )
for pick in self . browse ( cr , uid , ids , context = context ) :
new_picking = None
new_moves = [ ]
2010-03-26 09:25:02 +00:00
complete , too_many , too_few = [ ] , [ ] , [ ]
move_product_qty = { }
2010-03-25 12:08:31 +00:00
for move in pick . move_lines :
2010-03-26 14:52:01 +00:00
if move . state in ( ' done ' , ' cancel ' ) :
continue
2010-03-29 11:37:53 +00:00
partial_data = partial_datas . get ( ' move %s ' % ( move . id ) , False )
2010-03-25 12:08:31 +00:00
assert partial_data , _ ( ' Do not Found Partial data of Stock Move Line : %s ' % ( move . id ) )
product_qty = partial_data . get ( ' product_qty ' , 0.0 )
2010-03-26 09:25:02 +00:00
move_product_qty [ move . id ] = product_qty
2010-03-29 11:37:53 +00:00
product_uom = partial_data . get ( ' product_uom ' , False )
2010-03-25 12:08:31 +00:00
product_price = partial_data . get ( ' product_price ' , 0.0 )
product_currency = partial_data . get ( ' product_currency ' , False )
if move . product_qty == product_qty :
complete . append ( move )
elif move . product_qty > product_qty :
too_few . append ( move )
else :
too_many . append ( move )
# Average price computation
if ( pick . type == ' in ' ) and ( move . product_id . cost_method == ' average ' ) :
product = product_obj . browse ( cr , uid , move . product_id . id )
2010-03-29 11:37:53 +00:00
user = users_obj . browse ( cr , uid , uid )
context [ ' currency_id ' ] = move . company_id . currency_id . id
2010-03-25 12:08:31 +00:00
qty = uom_obj . _compute_qty ( cr , uid , product_uom , product_qty , product . uom_id . id )
pricetype = False
if user . company_id . property_valuation_price_type :
pricetype = price_type_obj . browse ( cr , uid , user . company_id . property_valuation_price_type . id )
if pricetype and qty > 0 :
2010-03-26 11:27:31 +00:00
new_price = currency_obj . compute ( cr , uid , product_currency ,
2010-03-25 12:08:31 +00:00
user . company_id . currency_id . id , product_price )
new_price = uom_obj . _compute_price ( cr , uid , product_uom , new_price ,
2010-03-29 11:37:53 +00:00
product . uom_id . id )
2010-03-25 12:08:31 +00:00
if product . qty_available < = 0 :
new_std_price = new_price
else :
# Get the standard price
amount_unit = product . price_get ( pricetype . field , context ) [ product . id ]
new_std_price = ( ( amount_unit * product . qty_available ) \
+ ( new_price * qty ) ) / ( product . qty_available + qty )
2010-03-29 11:37:53 +00:00
2010-03-25 12:08:31 +00:00
# Write the field according to price type field
product_obj . write ( cr , uid , [ product . id ] ,
{ pricetype . field : new_std_price } )
move_obj . write ( cr , uid , [ move . id ] , { ' price_unit ' : new_price } )
2010-03-29 11:37:53 +00:00
2010-03-25 12:08:31 +00:00
for move in too_few :
2010-03-26 09:25:02 +00:00
product_qty = move_product_qty [ move . id ]
2010-03-25 12:08:31 +00:00
if not new_picking :
new_picking = self . copy ( cr , uid , pick . id ,
{
2010-03-26 09:25:02 +00:00
' name ' : sequence_obj . get ( cr , uid , ' stock.picking. %s ' % ( pick . type ) ) ,
2010-03-25 12:08:31 +00:00
' move_lines ' : [ ] ,
' state ' : ' draft ' ,
} )
if product_qty != 0 :
2010-03-29 11:37:53 +00:00
2010-03-25 12:08:31 +00:00
new_obj = move_obj . copy ( cr , uid , move . id ,
{
' product_qty ' : product_qty ,
' product_uos_qty ' : product_qty , #TODO: put correct uos_qty
2010-03-29 11:37:53 +00:00
' picking_id ' : new_picking ,
2010-03-25 12:08:31 +00:00
' state ' : ' assigned ' ,
2010-03-29 11:37:53 +00:00
' move_dest_id ' : False ,
2010-03-25 12:08:31 +00:00
' price_unit ' : move . price_unit ,
} )
2010-03-29 11:37:53 +00:00
2010-03-25 12:08:31 +00:00
move_obj . write ( cr , uid , [ move . id ] ,
{
' product_qty ' : move . product_qty - product_qty ,
' product_uos_qty ' : move . product_qty - product_qty , #TODO: put correct uos_qty
2010-03-29 11:37:53 +00:00
2010-03-25 12:08:31 +00:00
} )
if new_picking :
move_obj . write ( cr , uid , [ c . id for c in complete ] , { ' picking_id ' : new_picking } )
for move in too_many :
2010-03-26 09:25:02 +00:00
product_qty = move_product_qty [ move . id ]
2010-03-25 12:08:31 +00:00
move_obj . write ( cr , uid , [ move . id ] ,
{
' product_qty ' : product_qty ,
' product_uos_qty ' : product_qty , #TODO: put correct uos_qty
' picking_id ' : new_picking ,
} )
else :
for move in too_many :
2010-03-26 09:25:02 +00:00
product_qty = move_product_qty [ move . id ]
2010-03-25 12:08:31 +00:00
move_obj . write ( cr , uid , [ move . id ] ,
{
' product_qty ' : product_qty ,
2010-03-26 09:25:02 +00:00
' product_uos_qty ' : product_qty #TODO: put correct uos_qty
2010-03-25 12:08:31 +00:00
} )
2010-03-29 11:37:53 +00:00
# At first we confirm the new picking (if necessary)
2010-03-25 12:08:31 +00:00
if new_picking :
wf_service . trg_validate ( uid , ' stock.picking ' , new_picking , ' button_confirm ' , cr )
# Then we finish the good picking
if new_picking :
self . write ( cr , uid , [ pick . id ] , { ' backorder_id ' : new_picking } )
self . action_move ( cr , uid , [ new_picking ] )
wf_service . trg_validate ( uid , ' stock.picking ' , new_picking , ' button_done ' , cr )
wf_service . trg_write ( uid , ' stock.picking ' , pick . id , cr )
2010-03-26 09:25:02 +00:00
delivered_pack_id = new_picking
2010-03-25 12:08:31 +00:00
else :
self . action_move ( cr , uid , [ pick . id ] )
2010-03-29 11:37:53 +00:00
wf_service . trg_validate ( uid , ' stock.picking ' , pick . id , ' button_done ' , cr )
2010-03-26 09:25:02 +00:00
delivered_pack_id = pick . id
2010-03-29 11:37:53 +00:00
delivered_pack = self . browse ( cr , uid , delivered_pack_id , context = context )
2010-03-26 09:25:02 +00:00
res [ pick . id ] = { ' delivered_picking ' : delivered_pack . id or False }
2010-07-06 11:44:51 +00:00
2010-03-25 12:08:31 +00:00
return res
2010-07-23 10:11:19 +00:00
def log_picking ( self , cr , uid , ids , context = None ) :
""" This function will create log messages for picking.
@param cr : the database cursor
@param uid : the current user ' s ID for security checks,
@param ids : List of Picking Ids
@param context : A standard dictionary for contextual values
"""
for pick in self . browse ( cr , uid , ids , context = context ) :
type_list = {
' out ' : ' Picking List ' ,
' in ' : ' Reception ' ,
' internal ' : ' Internal picking ' ,
' delivery ' : ' Delivery order '
}
message = type_list . get ( pick . type , _ ( ' Document ' ) ) + " ' " + ( pick . name or ' n/a ' ) + " ' "
state_list = {
2010-08-13 12:20:05 +00:00
' confirmed ' : " is scheduled for the ' " + datetime . strptime ( pick . min_date , ' % Y- % m- %d % H: % M: % S ' ) . strftime ( ' % Y- % m- %d ' ) + " ' . " ,
' assigned ' : ' is ready to process. ' ,
' cancel ' : ' is Cancelled. ' ,
' done ' : ' is processed. ' ,
2010-08-30 07:05:47 +00:00
' draft ' : ' is draft. ' ,
2010-07-23 10:11:19 +00:00
}
message + = state_list [ pick . state ]
self . log ( cr , uid , pick . id , message )
return True
2006-12-07 13:41:40 +00:00
stock_picking ( )
class stock_production_lot ( osv . osv ) :
2010-07-06 11:44:51 +00:00
2010-07-06 11:21:27 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2008-07-22 15:11:28 +00:00
if not ids :
return [ ]
2010-02-23 05:34:00 +00:00
reads = self . read ( cr , uid , ids , [ ' name ' , ' prefix ' , ' ref ' ] , context )
2009-08-28 09:40:49 +00:00
res = [ ]
2008-07-22 15:11:28 +00:00
for record in reads :
2009-08-28 09:40:49 +00:00
name = record [ ' name ' ]
2010-02-23 05:34:00 +00:00
prefix = record [ ' prefix ' ]
if prefix :
name = prefix + ' / ' + name
2008-07-22 15:11:28 +00:00
if record [ ' ref ' ] :
2010-02-23 05:34:00 +00:00
name = ' %s [ %s ] ' % ( name , record [ ' ref ' ] )
2008-07-22 15:11:28 +00:00
res . append ( ( record [ ' id ' ] , name ) )
return res
2008-11-03 14:40:49 +00:00
2008-07-22 15:11:28 +00:00
_name = ' stock.production.lot '
_description = ' Production lot '
2010-07-06 11:21:27 +00:00
def _get_stock ( self , cr , uid , ids , field_name , arg , context = None ) :
2010-05-26 12:59:30 +00:00
""" Gets stock of products for locations
@return : Dictionary of values
"""
2008-09-16 10:37:53 +00:00
if ' location_id ' not in context :
2009-08-28 09:40:49 +00:00
locations = self . pool . get ( ' stock.location ' ) . search ( cr , uid , [ ( ' usage ' , ' = ' , ' internal ' ) ] , context = context )
2008-09-16 10:37:53 +00:00
else :
2009-06-03 09:26:56 +00:00
locations = context [ ' location_id ' ] and [ context [ ' location_id ' ] ] or [ ]
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
2009-07-31 11:30:07 +00:00
2008-09-16 10:37:53 +00:00
res = { } . fromkeys ( ids , 0.0 )
2009-06-03 09:26:56 +00:00
if locations :
cr . execute ( ''' select
prodlot_id ,
sum ( name )
from
stock_report_prodlots
where
2010-06-10 13:34:19 +00:00
location_id IN % s and prodlot_id IN % s group by prodlot_id ''' ,(tuple(locations),tuple(ids),))
2009-06-03 09:26:56 +00:00
res . update ( dict ( cr . fetchall ( ) ) )
2010-07-06 11:44:51 +00:00
2008-09-16 10:37:53 +00:00
return res
2009-05-27 13:42:15 +00:00
2010-07-06 11:21:27 +00:00
def _stock_search ( self , cr , uid , obj , name , args , context = None ) :
2010-05-26 12:59:30 +00:00
""" Searches Ids of products
@return : Ids of locations
"""
2009-08-28 09:40:49 +00:00
locations = self . pool . get ( ' stock.location ' ) . search ( cr , uid , [ ( ' usage ' , ' = ' , ' internal ' ) ] )
2009-03-17 08:18:08 +00:00
cr . execute ( ''' select
prodlot_id ,
sum ( name )
from
stock_report_prodlots
where
2010-06-10 13:34:19 +00:00
location_id IN % s group by prodlot_id
having sum ( name ) ''' + str(args[0][1]) + str(args[0][2]),(tuple(locations),))
2009-03-17 08:18:08 +00:00
res = cr . fetchall ( )
2009-08-28 09:40:49 +00:00
ids = [ ( ' id ' , ' in ' , map ( lambda x : x [ 0 ] , res ) ) ]
2009-03-17 08:18:08 +00:00
return ids
2009-05-27 13:42:15 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
' name ' : fields . char ( ' Serial ' , size = 64 , required = True ) ,
2010-02-23 05:34:00 +00:00
' ref ' : fields . char ( ' Internal Reference ' , size = 256 ) ,
' prefix ' : fields . char ( ' Prefix ' , size = 64 ) ,
2009-08-28 09:40:49 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' , required = True ) ,
2008-09-20 19:06:25 +00:00
' date ' : fields . datetime ( ' Created Date ' , required = True ) ,
2009-03-17 08:18:08 +00:00
' stock_available ' : fields . function ( _get_stock , fnct_search = _stock_search , method = True , type = " float " , string = " Available " , select = " 2 " ) ,
2009-08-28 09:40:49 +00:00
' revisions ' : fields . one2many ( ' stock.production.lot.revision ' , ' lot_id ' , ' Revisions ' ) ,
2009-12-22 19:07:37 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-07-06 11:21:27 +00:00
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2009-08-28 09:40:49 +00:00
' name ' : lambda x , y , z , c : x . pool . get ( ' ir.sequence ' ) . get ( y , z , ' stock.lot.serial ' ) ,
' product_id ' : lambda x , y , z , c : c . get ( ' product_id ' , False ) ,
2008-07-22 15:11:28 +00:00
}
_sql_constraints = [
( ' name_ref_uniq ' , ' unique (name, ref) ' , ' The serial/ref must be unique ! ' ) ,
]
2007-09-12 13:21:07 +00:00
2006-12-07 13:41:40 +00:00
stock_production_lot ( )
class stock_production_lot_revision ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' stock.production.lot.revision '
_description = ' Production lot revisions '
2010-06-16 08:40:57 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
2009-01-27 11:15:46 +00:00
' name ' : fields . char ( ' Revision Name ' , size = 64 , required = True ) ,
2008-07-22 15:11:28 +00:00
' description ' : fields . text ( ' Description ' ) ,
2009-01-27 11:15:46 +00:00
' date ' : fields . date ( ' Revision Date ' ) ,
2008-07-22 15:11:28 +00:00
' indice ' : fields . char ( ' Revision ' , size = 16 ) ,
' author_id ' : fields . many2one ( ' res.users ' , ' Author ' ) ,
2008-09-16 10:37:53 +00:00
' lot_id ' : fields . many2one ( ' stock.production.lot ' , ' Production lot ' , select = True , ondelete = ' cascade ' ) ,
2009-12-09 09:52:38 +00:00
' company_id ' : fields . related ( ' lot_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2009-08-28 09:40:49 +00:00
' author_id ' : lambda x , y , z , c : z ,
2010-07-06 11:21:27 +00:00
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
2008-07-22 15:11:28 +00:00
}
2009-08-28 09:40:49 +00:00
2006-12-07 13:41:40 +00:00
stock_production_lot_revision ( )
2010-04-09 08:22:51 +00:00
# ----------------------------------------------------
# Move
# ----------------------------------------------------
2006-12-07 13:41:40 +00:00
#
# Fields:
2008-07-22 15:11:28 +00:00
# location_dest_id is only used for predicting futur stocks
2006-12-07 13:41:40 +00:00
#
class stock_move ( osv . osv ) :
2010-06-15 19:19:08 +00:00
2010-07-06 11:21:27 +00:00
def _getSSCC ( self , cr , uid , context = None ) :
2008-12-10 14:29:55 +00:00
cr . execute ( ' select id from stock_tracking where create_uid= %s order by id desc limit 1 ' , ( uid , ) )
2008-07-22 15:11:28 +00:00
res = cr . fetchone ( )
return ( res and res [ 0 ] ) or False
_name = " stock.move "
_description = " Stock Move "
2010-06-26 15:15:23 +00:00
_order = ' date_expected desc, id '
2010-05-19 20:02:36 +00:00
_log_create = False
2009-08-28 09:40:49 +00:00
2010-07-06 11:21:27 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2008-10-10 08:59:17 +00:00
res = [ ]
for line in self . browse ( cr , uid , ids , context ) :
res . append ( ( line . id , ( line . product_id . code or ' / ' ) + ' : ' + line . location_id . name + ' > ' + line . location_dest_id . name ) )
return res
2008-09-20 13:56:44 +00:00
def _check_tracking ( self , cr , uid , ids ) :
2010-05-26 12:59:30 +00:00
""" Checks if production lot is assigned to stock move or not.
@return : True or False
"""
2009-08-28 09:40:49 +00:00
for move in self . browse ( cr , uid , ids ) :
if not move . prodlot_id and \
( move . state == ' done ' and \
( \
( move . product_id . track_production and move . location_id . usage == ' production ' ) or \
( move . product_id . track_production and move . location_dest_id . usage == ' production ' ) or \
2009-12-08 12:58:25 +00:00
( move . product_id . track_incoming and move . location_id . usage in ( ' supplier ' , ' internal ' ) ) or \
( move . product_id . track_outgoing and move . location_dest_id . usage in ( ' customer ' , ' internal ' ) ) \
2009-08-28 09:40:49 +00:00
) ) :
return False
return True
2008-09-20 13:56:44 +00:00
def _check_product_lot ( self , cr , uid , ids ) :
2010-05-26 12:59:30 +00:00
""" Checks whether move is done or not and production lot is assigned to that move.
@return : True or False
"""
2009-08-28 09:40:49 +00:00
for move in self . browse ( cr , uid , ids ) :
2010-05-24 10:40:14 +00:00
if move . prodlot_id and move . state == ' done ' and ( move . prodlot_id . product_id . id != move . product_id . id ) :
2008-11-03 14:40:49 +00:00
return False
2009-08-28 09:40:49 +00:00
return True
2008-11-03 14:40:49 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True , select = True ) ,
2009-08-28 09:40:49 +00:00
' priority ' : fields . selection ( [ ( ' 0 ' , ' Not urgent ' ) , ( ' 1 ' , ' Urgent ' ) ] , ' Priority ' ) ,
2008-07-22 15:11:28 +00:00
2010-02-23 16:05:50 +00:00
' date ' : fields . datetime ( ' Created Date ' ) ,
2010-06-13 21:16:36 +00:00
' date_planned ' : fields . datetime ( ' Date ' , required = True , help = " Scheduled date for the movement of the products or real date if the move is done. " ) ,
2010-05-18 12:22:13 +00:00
' date_expected ' : fields . datetime ( ' Date Expected ' , readonly = True , required = True , help = " Scheduled date for the movement of the products " ) ,
2008-07-31 13:14:40 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' , required = True , select = True ) ,
2008-07-22 15:11:28 +00:00
' product_qty ' : fields . float ( ' Quantity ' , required = True ) ,
2010-05-28 05:18:58 +00:00
' product_uom ' : fields . many2one ( ' product.uom ' , ' Unit of Measure ' , required = True ) ,
2008-07-22 15:11:28 +00:00
' product_uos_qty ' : fields . float ( ' Quantity (UOS) ' ) ,
' product_uos ' : fields . many2one ( ' product.uom ' , ' Product UOS ' ) ,
2009-09-24 10:46:21 +00:00
' product_packaging ' : fields . many2one ( ' product.packaging ' , ' Packaging ' , help = " It specifies attributes of packaging like type, quantity of packaging,etc. " ) ,
2008-07-22 15:11:28 +00:00
2009-12-22 06:45:51 +00:00
' location_id ' : fields . many2one ( ' stock.location ' , ' Source Location ' , required = True , select = True , help = " Sets a location if you produce at a fixed location. This can be a partner location if you subcontract the manufacturing operations. " ) ,
2009-09-24 10:46:21 +00:00
' location_dest_id ' : fields . many2one ( ' stock.location ' , ' Dest. Location ' , required = True , select = True , help = " Location where the system will stock the finished products. " ) ,
2009-12-22 06:45:51 +00:00
' address_id ' : fields . many2one ( ' res.partner.address ' , ' Dest. Address ' , help = " Address where goods are to be delivered " ) ,
2008-07-22 15:11:28 +00:00
2009-08-28 09:40:49 +00:00
' prodlot_id ' : fields . many2one ( ' stock.production.lot ' , ' Production Lot ' , help = " Production lot is used to put a serial number on the production " ) ,
2010-06-26 15:15:23 +00:00
' tracking_id ' : fields . many2one ( ' stock.tracking ' , ' Pack ' , select = True , help = " This is the code that will be put on the logistical unit: pallet, box, pack. " ) ,
2008-07-22 15:11:28 +00:00
' auto_validate ' : fields . boolean ( ' Auto Validate ' ) ,
' move_dest_id ' : fields . many2one ( ' stock.move ' , ' Dest. Move ' ) ,
' move_history_ids ' : fields . many2many ( ' stock.move ' , ' stock_move_history_ids ' , ' parent_id ' , ' child_id ' , ' Move History ' ) ,
' move_history_ids2 ' : fields . many2many ( ' stock.move ' , ' stock_move_history_ids ' , ' child_id ' , ' parent_id ' , ' Move History ' ) ,
2009-11-13 05:41:16 +00:00
' picking_id ' : fields . many2one ( ' stock.picking ' , ' Picking List ' , select = True ) ,
2008-07-22 15:11:28 +00:00
' note ' : fields . text ( ' Notes ' ) ,
2009-11-23 13:52:38 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Draft ' ) , ( ' waiting ' , ' Waiting ' ) , ( ' confirmed ' , ' Confirmed ' ) , ( ' assigned ' , ' Available ' ) , ( ' done ' , ' Done ' ) , ( ' cancel ' , ' Cancelled ' ) ] , ' State ' , readonly = True , select = True ,
2010-07-13 16:25:09 +00:00
help = ' When the stock move is created it is in the \' Draft \' state. \n After that it is set to \' Confirmed \' state. \n If stock is available state is set to \' Available \' . \n When the picking is done the state is \' Done \' . \
2009-11-25 10:03:47 +00:00
\nThe state is \' Waiting \' if the move is waiting for another one. ' ) ,
2008-07-22 15:11:28 +00:00
' price_unit ' : fields . float ( ' Unit Price ' ,
2010-03-06 20:52:19 +00:00
digits_compute = dp . get_precision ( ' Account ' ) ) ,
2010-06-16 10:08:18 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True , select = 1 ) ,
2010-06-18 09:08:58 +00:00
' partner_id ' : fields . related ( ' picking_id ' , ' address_id ' , ' partner_id ' , type = ' many2one ' , relation = " res.partner " , string = " Partner " , store = True ) ,
2010-06-13 22:35:25 +00:00
' backorder_id ' : fields . related ( ' picking_id ' , ' backorder_id ' , type = ' many2one ' , relation = " stock.picking " , string = " Back Order " ) ,
2010-06-18 09:35:39 +00:00
' origin ' : fields . related ( ' picking_id ' , ' origin ' , type = ' char ' , size = 64 , relation = " stock.picking " , string = " Origin " , store = True ) ,
2010-08-31 06:54:27 +00:00
' scrapped ' : fields . related ( ' location_dest_id ' , ' scrap_location ' , type = ' boolean ' , relation = ' stock.location ' , string = ' Scrapped ' ) ,
2010-08-13 12:20:05 +00:00
' move_stock_return_history ' : fields . many2many ( ' stock.move ' , ' stock_move_return_history ' , ' move_id ' , ' return_move_id ' , ' Move Return History ' , readonly = True ) ,
2008-07-22 15:11:28 +00:00
}
2008-09-20 11:54:38 +00:00
_constraints = [
2008-09-20 13:56:44 +00:00
( _check_tracking ,
2008-09-20 19:06:25 +00:00
' You must assign a production lot for this product ' ,
2008-09-20 13:56:44 +00:00
[ ' prodlot_id ' ] ) ,
( _check_product_lot ,
2008-09-20 19:06:25 +00:00
' You try to assign a lot which is not from the same product ' ,
2008-09-20 11:54:38 +00:00
[ ' prodlot_id ' ] ) ]
2009-08-28 09:40:49 +00:00
2010-07-06 11:21:27 +00:00
def _default_location_destination ( self , cr , uid , context = None ) :
2010-05-26 12:59:30 +00:00
""" Gets default address of partner for destination location
@return : Address id or False
"""
2008-09-07 23:24:39 +00:00
if context . get ( ' move_line ' , [ ] ) :
2009-07-22 06:44:55 +00:00
if context [ ' move_line ' ] [ 0 ] :
2009-08-28 09:40:49 +00:00
if isinstance ( context [ ' move_line ' ] [ 0 ] , ( tuple , list ) ) :
2010-08-17 11:02:45 +00:00
return context [ ' move_line ' ] [ 0 ] [ 2 ] and context [ ' move_line ' ] [ 0 ] [ 2 ] . get ( ' location_dest_id ' , False )
2009-07-22 06:44:55 +00:00
else :
2009-08-28 09:40:49 +00:00
move_list = self . pool . get ( ' stock.move ' ) . read ( cr , uid , context [ ' move_line ' ] [ 0 ] , [ ' location_dest_id ' ] )
2009-07-22 06:44:55 +00:00
return move_list and move_list [ ' location_dest_id ' ] [ 0 ] or False
2008-09-07 23:24:39 +00:00
if context . get ( ' address_out_id ' , False ) :
2010-06-15 08:46:44 +00:00
property_out = self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , context [ ' address_out_id ' ] , context ) . partner_id . property_stock_customer
return property_out and property_out . id or False
2008-09-07 23:24:39 +00:00
return False
2010-07-06 11:21:27 +00:00
def _default_location_source ( self , cr , uid , context = None ) :
2010-05-26 12:59:30 +00:00
""" Gets default address of partner for source location
@return : Address id or False
"""
2008-09-07 23:24:39 +00:00
if context . get ( ' move_line ' , [ ] ) :
2008-09-09 12:46:37 +00:00
try :
return context [ ' move_line ' ] [ 0 ] [ 2 ] [ ' location_id ' ]
except :
pass
2008-09-07 23:24:39 +00:00
if context . get ( ' address_in_id ' , False ) :
return self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , context [ ' address_in_id ' ] , context ) . partner_id . property_stock_supplier . id
return False
2008-07-22 15:11:28 +00:00
_defaults = {
2008-09-07 23:24:39 +00:00
' location_id ' : _default_location_source ,
' location_dest_id ' : _default_location_destination ,
2010-07-06 11:21:27 +00:00
' state ' : ' draft ' ,
' priority ' : ' 1 ' ,
' product_qty ' : 1.0 ,
2010-08-31 06:54:27 +00:00
' scrapped ' : False ,
2010-07-06 11:21:27 +00:00
' date_planned ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2010-05-18 12:22:13 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' stock.move ' , context = c ) ,
2010-07-06 11:21:27 +00:00
' date_expected ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2008-07-22 15:11:28 +00:00
}
2010-02-01 12:50:00 +00:00
2010-08-31 06:39:28 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
if not context :
context = { }
group_id = self . pool . get ( ' res.groups ' ) . search ( cr , uid , [ ( ' name ' , ' in ' , ( ' Administration / Configuration ' , ' Administration / Access Rights ' ) ) ] )
user_obj = self . pool . get ( ' res.users ' )
2010-08-31 06:44:49 +00:00
group = [ ]
2010-08-31 06:39:28 +00:00
user_rec = user_obj . read ( cr , uid , uid )
for move in self . browse ( cr , uid , ids ) :
if move . state == ' done ' :
2010-08-31 06:44:49 +00:00
group = [ i for i in group_id if ( i and ( i not in user_rec [ ' groups_id ' ] ) ) ]
if group :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' Quantities, UoMs, Products and Locations cannot be modified on stock moves in Done state (except by the Administrator ' ) )
2010-08-31 06:39:28 +00:00
return super ( stock_move , self ) . write ( cr , uid , ids , vals , context = context )
2010-07-06 11:21:27 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
2010-01-07 15:39:43 +00:00
if default is None :
default = { }
default = default . copy ( )
2010-08-10 13:30:50 +00:00
default [ ' move_stock_return_history ' ] = [ ]
2010-07-06 11:21:27 +00:00
return super ( stock_move , self ) . copy ( cr , uid , id , default , context = context )
2010-02-01 12:50:00 +00:00
2010-07-06 11:21:27 +00:00
def _auto_init ( self , cursor , context = None ) :
2010-07-06 12:11:43 +00:00
res = super ( stock_move , self ) . _auto_init ( cursor , context = context )
2008-07-22 15:11:28 +00:00
cursor . execute ( ' SELECT indexname \
FROM pg_indexes \
WHERE indexname = \' stock_move_location_id_location_dest_id_product_id_state \' ' )
if not cursor . fetchone ( ) :
cursor . execute ( ' CREATE INDEX stock_move_location_id_location_dest_id_product_id_state \
ON stock_move ( location_id , location_dest_id , product_id , state ) ' )
2009-07-31 11:30:07 +00:00
return res
2008-07-22 15:11:28 +00:00
2010-06-16 08:40:57 +00:00
def onchange_lot_id ( self , cr , uid , ids , prodlot_id = False , product_qty = False ,
2010-05-26 12:59:30 +00:00
loc_id = False , product_id = False , context = None ) :
""" On change of production lot gives a warning message.
@param prodlot_id : Changed production lot id
@param product_qty : Quantity of product
@param loc_id : Location id
@param product_id : Product id
@return : Warning message
"""
2008-09-20 13:56:44 +00:00
if not prodlot_id or not loc_id :
return { }
2009-06-09 15:37:28 +00:00
ctx = context and context . copy ( ) or { }
2009-07-31 11:30:07 +00:00
ctx [ ' location_id ' ] = loc_id
2009-06-09 15:37:28 +00:00
prodlot = self . pool . get ( ' stock.production.lot ' ) . browse ( cr , uid , prodlot_id , ctx )
2009-08-28 09:40:49 +00:00
location = self . pool . get ( ' stock.location ' ) . browse ( cr , uid , loc_id )
warning = { }
2008-09-20 19:06:25 +00:00
if ( location . usage == ' internal ' ) and ( product_qty > ( prodlot . stock_available or 0.0 ) ) :
2009-08-28 09:40:49 +00:00
warning = {
2010-08-30 09:36:40 +00:00
' title ' : _ ( ' Bad Lot Assignation ! ' ) ,
' message ' : _ ( ' You are moving %.2f products but only %.2f available in this lot. ' ) % ( product_qty , prodlot . stock_available or 0.0 )
2008-09-20 19:06:25 +00:00
}
2009-08-28 09:40:49 +00:00
return { ' warning ' : warning }
2009-12-08 12:58:25 +00:00
2010-06-16 08:40:57 +00:00
def onchange_quantity ( self , cr , uid , ids , product_id , product_qty ,
2010-05-26 12:59:30 +00:00
product_uom , product_uos ) :
""" On change of product quantity finds UoM and UoS quantities
@param product_id : Product id
@param product_qty : Changed Quantity of product
@param product_uom : Unit of measure of product
2010-06-16 08:40:57 +00:00
@param product_uos : Unit of sale of product
2010-05-26 12:59:30 +00:00
@return : Dictionary of values
"""
2009-11-20 11:20:04 +00:00
result = {
' product_uos_qty ' : 0.00
}
2009-12-08 12:58:25 +00:00
2009-11-20 11:20:04 +00:00
if ( not product_id ) or ( product_qty < = 0.0 ) :
return { ' value ' : result }
2009-12-08 12:58:25 +00:00
2009-11-20 11:20:04 +00:00
product_obj = self . pool . get ( ' product.product ' )
uos_coeff = product_obj . read ( cr , uid , product_id , [ ' uos_coeff ' ] )
2009-12-08 12:58:25 +00:00
2009-11-20 11:20:04 +00:00
if product_uos and product_uom and ( product_uom != product_uos ) :
result [ ' product_uos_qty ' ] = product_qty * uos_coeff [ ' uos_coeff ' ]
else :
result [ ' product_uos_qty ' ] = product_qty
2009-12-08 12:58:25 +00:00
2009-11-20 11:20:04 +00:00
return { ' value ' : result }
2009-12-08 12:58:25 +00:00
2010-06-16 08:40:57 +00:00
def onchange_product_id ( self , cr , uid , ids , prod_id = False , loc_id = False ,
2010-05-26 12:59:30 +00:00
loc_dest_id = False , address_id = False ) :
""" On change of product id, if finds UoM, UoS, quantity and UoS quantity.
@param prod_id : Changed Product id
@param loc_id : Source location id
@param loc_id : Destination location id
2010-06-16 08:40:57 +00:00
@param address_id : Address id of partner
2010-05-26 12:59:30 +00:00
@return : Dictionary of values
"""
2008-07-22 15:11:28 +00:00
if not prod_id :
return { }
2010-02-10 11:48:08 +00:00
lang = False
if address_id :
addr_rec = self . pool . get ( ' res.partner.address ' ) . browse ( cr , uid , address_id )
if addr_rec :
lang = addr_rec . partner_id and addr_rec . partner_id . lang or False
ctx = { ' lang ' : lang }
product = self . pool . get ( ' product.product ' ) . browse ( cr , uid , [ prod_id ] , context = ctx ) [ 0 ]
2009-11-20 11:20:04 +00:00
uos_id = product . uos_id and product . uos_id . id or False
2008-07-22 15:11:28 +00:00
result = {
' product_uom ' : product . uom_id . id ,
2009-11-20 11:20:04 +00:00
' product_uos ' : uos_id ,
' product_qty ' : 1.00 ,
' product_uos_qty ' : self . pool . get ( ' stock.move ' ) . onchange_quantity ( cr , uid , ids , prod_id , 1.00 , product . uom_id . id , uos_id ) [ ' value ' ] [ ' product_uos_qty ' ]
2008-07-22 15:11:28 +00:00
}
2010-03-18 12:17:30 +00:00
if not ids :
result [ ' name ' ] = product . partner_ref
2008-07-22 15:11:28 +00:00
if loc_id :
result [ ' location_id ' ] = loc_id
if loc_dest_id :
result [ ' location_dest_id ' ] = loc_dest_id
2009-08-28 09:40:49 +00:00
return { ' value ' : result }
2008-07-22 15:11:28 +00:00
2010-07-06 11:21:27 +00:00
def _chain_compute ( self , cr , uid , moves , context = None ) :
2010-05-26 12:59:30 +00:00
""" Finds whether the location has chained location type or not.
@param moves : Stock moves
@return : Dictionary containing destination location with chained location type .
"""
2008-09-07 23:24:39 +00:00
result = { }
for m in moves :
2008-09-20 19:06:25 +00:00
dest = self . pool . get ( ' stock.location ' ) . chained_location_get (
2008-11-03 14:40:49 +00:00
cr ,
uid ,
m . location_dest_id ,
m . picking_id and m . picking_id . address_id and m . picking_id . address_id . partner_id ,
m . product_id ,
2008-09-20 19:06:25 +00:00
context
)
2008-09-07 23:24:39 +00:00
if dest :
2009-08-28 09:40:49 +00:00
if dest [ 1 ] == ' transparent ' :
2008-09-07 23:24:39 +00:00
self . write ( cr , uid , [ m . id ] , {
2010-03-01 05:05:14 +00:00
' date_planned ' : ( datetime . strptime ( m . date_planned , ' % Y- % m- %d % H: % M: % S ' ) + \
relativedelta ( days = dest [ 2 ] or 0 ) ) . strftime ( ' % Y- % m- %d ' ) ,
2008-09-07 23:24:39 +00:00
' location_dest_id ' : dest [ 0 ] . id } )
else :
result . setdefault ( m . picking_id , [ ] )
result [ m . picking_id ] . append ( ( m , dest ) )
return result
2010-07-06 11:21:27 +00:00
def action_confirm ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Confirms stock move.
@return : List of ids .
"""
2009-03-23 20:57:37 +00:00
moves = self . browse ( cr , uid , ids )
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , ids , { ' state ' : ' confirmed ' } )
i = 0
2010-07-06 11:21:27 +00:00
def create_chained_picking ( self , cr , uid , moves , context = None ) :
2009-08-28 09:40:49 +00:00
new_moves = [ ]
2010-07-28 12:10:44 +00:00
res_obj = self . pool . get ( ' res.company ' )
picking_obj = self . pool . get ( ' stock.picking ' )
move_obj = self . pool . get ( ' stock.move ' )
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
for picking , todo in self . _chain_compute ( cr , uid , moves , context = context ) . items ( ) :
2010-06-15 10:37:07 +00:00
ptype = todo [ 0 ] [ 1 ] [ 5 ] and todo [ 0 ] [ 1 ] [ 5 ] or self . pool . get ( ' stock.location ' ) . picking_type_get ( cr , uid , todo [ 0 ] [ 0 ] . location_dest_id , todo [ 0 ] [ 1 ] [ 0 ] )
2010-08-30 13:31:38 +00:00
pick_name = picking . name or ' '
2010-04-21 09:41:30 +00:00
if ptype == ' delivery ' :
pick_name = self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' stock.picking.delivery ' )
2010-08-30 13:31:38 +00:00
check_picking_ids = picking_obj . search ( cr , uid , [ ( ' name ' , ' = ' , picking . name ) , ( ' origin ' , ' = ' , str ( picking . origin or ' ' ) ) , ( ' type ' , ' = ' , ptype ) , ( ' move_type ' , ' = ' , picking . move_type ) ] )
if check_picking_ids :
pickid = check_picking_ids [ 0 ]
else :
if picking :
pickid = picking_obj . create ( cr , uid , {
' name ' : pick_name ,
' origin ' : str ( picking . origin or ' ' ) ,
' type ' : ptype ,
' note ' : picking . note ,
' move_type ' : picking . move_type ,
' auto_picking ' : todo [ 0 ] [ 1 ] [ 1 ] == ' auto ' ,
' stock_journal_id ' : todo [ 0 ] [ 1 ] [ 3 ] ,
' company_id ' : todo [ 0 ] [ 1 ] [ 4 ] or res_obj . _company_default_get ( cr , uid , ' stock.company ' , context = context ) ,
' address_id ' : picking . address_id . id ,
' invoice_state ' : ' none ' ,
' date ' : picking . date ,
' sale_id ' : ' sale_id ' in picking . _columns . keys ( ) and picking . sale_id . id or False
} )
else :
pickid = False
2010-06-15 10:37:07 +00:00
for move , ( loc , auto , delay , journal , company_id , ptype ) in todo :
2010-07-28 12:10:44 +00:00
new_id = move_obj . copy ( cr , uid , move . id , {
2009-03-18 10:36:11 +00:00
' location_id ' : move . location_dest_id . id ,
' location_dest_id ' : loc . id ,
' date_moved ' : time . strftime ( ' % Y- % m- %d ' ) ,
' picking_id ' : pickid ,
2009-08-28 09:40:49 +00:00
' state ' : ' waiting ' ,
2010-08-30 13:31:38 +00:00
' company_id ' : company_id or res_obj . _company_default_get ( cr , uid , ' stock.company ' , context = context ) ,
2009-08-28 09:40:49 +00:00
' move_history_ids ' : [ ] ,
2010-03-01 05:05:14 +00:00
' date_planned ' : ( datetime . strptime ( move . date_planned , ' % Y- % m- %d % H: % M: % S ' ) + relativedelta ( days = delay or 0 ) ) . strftime ( ' % Y- % m- %d ' ) ,
2009-08-28 09:40:49 +00:00
' move_history_ids2 ' : [ ] }
2009-03-18 10:36:11 +00:00
)
2010-07-28 12:10:44 +00:00
move_obj . write ( cr , uid , [ move . id ] , {
2009-03-18 10:36:11 +00:00
' move_dest_id ' : new_id ,
' move_history_ids ' : [ ( 4 , new_id ) ]
} )
new_moves . append ( self . browse ( cr , uid , [ new_id ] ) [ 0 ] )
2010-08-30 13:31:38 +00:00
if pickid :
wf_service = netsvc . LocalService ( " workflow " )
wf_service . trg_validate ( uid , ' stock.picking ' , pickid , ' button_confirm ' , cr )
2009-03-18 10:36:11 +00:00
if new_moves :
create_chained_picking ( self , cr , uid , new_moves , context )
create_chained_picking ( self , cr , uid , moves , context )
2008-09-07 23:24:39 +00:00
return [ ]
2008-07-22 15:11:28 +00:00
def action_assign ( self , cr , uid , ids , * args ) :
2010-05-26 12:59:30 +00:00
""" Changes state to confirmed or waiting.
@return : List of values
"""
2008-07-22 15:11:28 +00:00
todo = [ ]
for move in self . browse ( cr , uid , ids ) :
2009-08-28 09:40:49 +00:00
if move . state in ( ' confirmed ' , ' waiting ' ) :
2008-07-22 15:11:28 +00:00
todo . append ( move . id )
res = self . check_assign ( cr , uid , todo )
return res
def force_assign ( self , cr , uid , ids , context = { } ) :
2010-05-26 12:59:30 +00:00
""" Changes the state to assigned.
@return : True
"""
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , ids , { ' state ' : ' assigned ' } )
2008-07-22 15:11:28 +00:00
return True
def cancel_assign ( self , cr , uid , ids , context = { } ) :
2010-05-26 12:59:30 +00:00
""" Changes the state to confirmed.
@return : True
"""
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , ids , { ' state ' : ' confirmed ' } )
return True
#
# Duplicate stock.move
#
2010-07-06 11:21:27 +00:00
def check_assign ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Checks the product type and accordingly writes the state.
@return : No . of moves done
"""
2008-07-22 15:11:28 +00:00
done = [ ]
2009-08-28 09:40:49 +00:00
count = 0
2008-07-22 15:11:28 +00:00
pickings = { }
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
2010-06-21 18:40:58 +00:00
for move in self . browse ( cr , uid , ids , context = context ) :
2008-07-22 15:11:28 +00:00
if move . product_id . type == ' consu ' :
if move . state in ( ' confirmed ' , ' waiting ' ) :
done . append ( move . id )
pickings [ move . picking_id . id ] = 1
continue
2009-08-28 09:40:49 +00:00
if move . state in ( ' confirmed ' , ' waiting ' ) :
2008-07-22 15:11:28 +00:00
res = self . pool . get ( ' stock.location ' ) . _product_reserve ( cr , uid , [ move . location_id . id ] , move . product_id . id , move . product_qty , { ' uom ' : move . product_uom . id } )
if res :
2009-09-10 09:12:54 +00:00
#_product_available_test depends on the next status for correct functioning
#the test does not work correctly if the same product occurs multiple times
2009-11-13 05:41:16 +00:00
#in the same order. This is e.g. the case when using the button 'split in two' of
#the stock outgoing form
2009-09-10 09:12:54 +00:00
self . write ( cr , uid , move . id , { ' state ' : ' assigned ' } )
2008-07-22 15:11:28 +00:00
done . append ( move . id )
pickings [ move . picking_id . id ] = 1
r = res . pop ( 0 )
2009-08-28 09:40:49 +00:00
cr . execute ( ' update stock_move set location_id= %s , product_qty= %s where id= %s ' , ( r [ 1 ] , r [ 0 ] , move . id ) )
2008-07-22 15:11:28 +00:00
while res :
r = res . pop ( 0 )
2009-08-28 09:40:49 +00:00
move_id = self . copy ( cr , uid , move . id , { ' product_qty ' : r [ 0 ] , ' location_id ' : r [ 1 ] } )
2008-07-22 15:11:28 +00:00
done . append ( move_id )
if done :
count + = len ( done )
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , done , { ' state ' : ' assigned ' } )
2008-07-22 15:11:28 +00:00
if count :
for pick_id in pickings :
wf_service = netsvc . LocalService ( " workflow " )
wf_service . trg_write ( uid , ' stock.picking ' , pick_id , cr )
return count
2010-06-15 19:19:08 +00:00
def setlast_tracking ( self , cr , uid , ids , context = None ) :
2010-06-16 10:47:07 +00:00
tracking_obj = self . pool . get ( ' stock.tracking ' )
tracking = context . get ( ' tracking ' , False )
2010-08-30 13:31:38 +00:00
picking = self . browse ( cr , uid , ids ) [ 0 ] . picking_id
if picking :
last_track = [ line . tracking_id . id for line in picking . move_lines if line . tracking_id ]
2010-08-30 17:59:14 +00:00
if not last_track :
last_track = tracking_obj . create ( cr , uid , { } , context = context )
2010-08-30 13:31:38 +00:00
else :
last_track . sort ( )
last_track = last_track [ - 1 ]
self . write ( cr , uid , ids , { ' tracking_id ' : last_track } )
2010-06-15 19:19:08 +00:00
return True
2010-06-16 08:40:57 +00:00
2008-07-22 15:11:28 +00:00
#
# Cancel move => cancel others move and pickings
#
2010-07-06 11:21:27 +00:00
def action_cancel ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Cancels the moves and if all moves are cancelled it cancels the picking.
@return : True
"""
2008-07-22 15:11:28 +00:00
if not len ( ids ) :
return True
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
pickings = { }
for move in self . browse ( cr , uid , ids ) :
2009-08-28 09:40:49 +00:00
if move . state in ( ' confirmed ' , ' waiting ' , ' assigned ' , ' draft ' ) :
2008-07-22 15:11:28 +00:00
if move . picking_id :
pickings [ move . picking_id . id ] = True
2009-08-28 09:40:49 +00:00
if move . move_dest_id and move . move_dest_id . state == ' waiting ' :
self . write ( cr , uid , [ move . move_dest_id . id ] , { ' state ' : ' assigned ' } )
2010-01-11 13:35:13 +00:00
if context . get ( ' call_unlink ' , False ) and move . move_dest_id . picking_id :
2008-12-26 07:52:00 +00:00
wf_service = netsvc . LocalService ( " workflow " )
wf_service . trg_write ( uid , ' stock.picking ' , move . move_dest_id . picking_id . id , cr )
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , ids , { ' state ' : ' cancel ' , ' move_dest_id ' : False } )
2010-01-11 13:35:13 +00:00
if not context . get ( ' call_unlink ' , False ) :
for pick in self . pool . get ( ' stock.picking ' ) . browse ( cr , uid , pickings . keys ( ) ) :
if all ( move . state == ' cancel ' for move in pick . move_lines ) :
self . pool . get ( ' stock.picking ' ) . write ( cr , uid , [ pick . id ] , { ' state ' : ' cancel ' } )
2008-07-22 15:11:28 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for id in ids :
wf_service . trg_trigger ( uid , ' stock.move ' , id , cr )
return True
2010-06-10 12:44:52 +00:00
def _get_accounting_values ( self , cr , uid , move , context = None ) :
product_obj = self . pool . get ( ' product.product ' )
product_uom_obj = self . pool . get ( ' product.uom ' )
price_type_obj = self . pool . get ( ' product.price.type ' )
accounts = product_obj . get_product_accounts ( cr , uid , move . product_id . id , context )
acc_src = accounts [ ' stock_account_input ' ]
acc_dest = accounts [ ' stock_account_output ' ]
2010-08-06 13:46:10 +00:00
acc_variation = accounts . get ( ' property_stock_variation ' , False )
2010-06-10 12:44:52 +00:00
journal_id = accounts [ ' stock_journal ' ]
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
2010-06-10 12:44:52 +00:00
if not acc_src :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' There is no stock input account defined ' \
' for this product: " %s " (id: %d ) ' ) % \
( move . product_id . name , move . product_id . id , ) )
if not acc_dest :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' There is no stock output account defined ' \
' for this product: " %s " (id: %d ) ' ) % \
( move . product_id . name , move . product_id . id , ) )
if not journal_id :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' There is no journal defined ' \
' on the product category: " %s " (id: %d ) ' ) % \
( move . product_id . categ_id . name , move . product_id . categ_id . id , ) )
if not acc_variation :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' There is no variation account defined ' \
' on the product category: " %s " (id: %d ) ' ) % \
( move . product_id . categ_id . name , move . product_id . categ_id . id , ) )
if acc_src != acc_dest :
default_uom = move . product_id . uom_id . id
q = product_uom_obj . _compute_qty ( cr , uid , move . product_uom . id , move . product_qty , default_uom )
if move . product_id . cost_method == ' average ' and move . price_unit :
amount = q * move . price_unit
# Base computation on valuation price type
else :
company_id = move . company_id . id
context [ ' currency_id ' ] = move . company_id . currency_id . id
pricetype = price_type_obj . browse ( cr , uid , move . company_id . property_valuation_price_type . id )
amount_unit = move . product_id . price_get ( pricetype . field , context ) [ move . product_id . id ]
amount = amount_unit * q or 1.0
return journal_id , acc_src , acc_dest , acc_variation , amount
2010-07-06 11:21:27 +00:00
def action_done ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Makes the move done and if all moves are done, it will finish the picking.
2010-06-16 08:40:57 +00:00
@return :
2010-05-26 12:59:30 +00:00
"""
2009-08-28 09:40:49 +00:00
track_flag = False
2010-01-17 07:39:14 +00:00
picking_ids = [ ]
2010-05-13 06:31:20 +00:00
product_uom_obj = self . pool . get ( ' product.uom ' )
price_type_obj = self . pool . get ( ' product.price.type ' )
2010-07-06 11:21:27 +00:00
product_obj = self . pool . get ( ' product.product ' )
2010-05-13 06:31:20 +00:00
move_obj = self . pool . get ( ' account.move ' )
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
2008-07-22 15:11:28 +00:00
for move in self . browse ( cr , uid , ids ) :
2010-06-21 18:40:58 +00:00
if move . picking_id :
picking_ids . append ( move . picking_id . id )
2008-07-22 15:11:28 +00:00
if move . move_dest_id . id and ( move . state != ' done ' ) :
2008-12-10 14:29:55 +00:00
cr . execute ( ' insert into stock_move_history_ids (parent_id,child_id) values ( %s , %s ) ' , ( move . id , move . move_dest_id . id ) )
2009-08-28 09:40:49 +00:00
if move . move_dest_id . state in ( ' waiting ' , ' confirmed ' ) :
self . write ( cr , uid , [ move . move_dest_id . id ] , { ' state ' : ' assigned ' } )
2008-07-22 15:11:28 +00:00
if move . move_dest_id . picking_id :
wf_service = netsvc . LocalService ( " workflow " )
wf_service . trg_write ( uid , ' stock.picking ' , move . move_dest_id . picking_id . id , cr )
else :
pass
if move . move_dest_id . auto_validate :
self . action_done ( cr , uid , [ move . move_dest_id . id ] , context = context )
#
# Accounting Entries
#
acc_src = None
acc_dest = None
2010-06-10 12:44:52 +00:00
if move . product_id . valuation == ' real_time ' :
lines = [ ]
if ( ( move . location_id . usage == ' internal ' and move . location_dest_id . usage == ' customer ' ) or ( move . location_id . usage == ' internal ' and move . location_dest_id . usage == ' transit ' ) ) :
if move . location_id . company_id :
context . update ( { ' force_company ' : move . location_id . company_id . id } )
journal_id , acc_src , acc_dest , acc_variation , amount = self . _get_accounting_values ( cr , uid , move , context )
lines = [ ( journal_id , self . create_account_move ( cr , uid , move , acc_dest , acc_variation , amount , context ) ) ]
2010-06-16 08:40:57 +00:00
elif ( ( move . location_id . usage == ' supplier ' and move . location_dest_id . usage == ' internal ' ) or ( move . location_id . usage == ' transit ' and move . location_dest_id . usage == ' internal ' ) ) :
2010-06-10 12:44:52 +00:00
if move . location_dest_id . company_id :
context . update ( { ' force_company ' : move . location_dest_id . company_id . id } )
journal_id , acc_src , acc_dest , acc_variation , amount = self . _get_accounting_values ( cr , uid , move , context )
lines = [ ( journal_id , self . create_account_move ( cr , uid , move , acc_variation , acc_src , amount , context ) ) ]
elif ( move . location_id . usage == ' internal ' and move . location_dest_id . usage == ' internal ' and move . location_id . company_id != move . location_dest_id . company_id ) :
if move . location_id . company_id :
context . update ( { ' force_company ' : move . location_id . company_id . id } )
journal_id , acc_src , acc_dest , acc_variation , amount = self . _get_accounting_values ( cr , uid , move , context )
line1 = [ ( journal_id , self . create_account_move ( cr , uid , move , acc_dest , acc_variation , amount , context ) ) ]
if move . location_dest_id . company_id :
context . update ( { ' force_company ' : move . location_dest_id . company_id . id } )
journal_id , acc_src , acc_dest , acc_variation , amount = self . _get_accounting_values ( cr , uid , move , context )
line2 = [ ( journal_id , self . create_account_move ( cr , uid , move , acc_variation , acc_src , amount , context ) ) ]
lines = line1 + line2
for j_id , line in lines :
2010-05-13 06:31:20 +00:00
move_obj . create ( cr , uid , {
2008-10-31 14:09:59 +00:00
' name ' : move . name ,
2010-06-10 12:44:52 +00:00
' journal_id ' : j_id ,
2010-05-28 11:37:10 +00:00
' type ' : ' cont_voucher ' ,
2010-06-10 12:44:52 +00:00
' line_id ' : line ,
2010-05-28 11:37:10 +00:00
' ref ' : move . picking_id and move . picking_id . name ,
2010-08-13 12:20:05 +00:00
} )
2010-06-21 18:40:58 +00:00
self . write ( cr , uid , ids , { ' state ' : ' done ' , ' date_planned ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } )
2008-07-22 15:11:28 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for id in ids :
wf_service . trg_trigger ( uid , ' stock.move ' , id , cr )
2010-06-16 08:40:57 +00:00
2010-06-21 18:40:58 +00:00
picking_obj = self . pool . get ( ' stock.picking ' )
2010-06-25 22:34:40 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for pick_id in picking_ids :
wf_service . trg_write ( uid , ' stock.picking ' , pick_id , cr )
2010-06-21 18:58:08 +00:00
2010-07-23 10:11:19 +00:00
picking_obj . log_picking ( cr , uid , picking_ids , context = context )
2008-07-22 15:11:28 +00:00
return True
2010-06-16 08:40:57 +00:00
2010-07-06 11:21:27 +00:00
def create_account_move ( self , cr , uid , move , account_id , account_variation , amount , context = None ) :
if context is None :
context = { }
2010-05-28 11:37:10 +00:00
partner_id = move . picking_id . address_id and ( move . picking_id . address_id . partner_id and move . picking_id . address_id . partner_id . id or False ) or False
lines = [ ( 0 , 0 , {
2010-08-13 12:20:05 +00:00
' name ' : move . name ,
' quantity ' : move . product_qty ,
' product_id ' : move . product_id and move . product_id . id or False ,
' credit ' : amount ,
' account_id ' : account_id ,
' ref ' : move . picking_id and move . picking_id . name or False ,
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
' partner_id ' : partner_id ,
} ) ,
( 0 , 0 , {
' name ' : move . name ,
' product_id ' : move . product_id and move . product_id . id or False ,
' quantity ' : move . product_qty ,
' debit ' : amount ,
' account_id ' : account_variation ,
' ref ' : move . picking_id and move . picking_id . name or False ,
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
' partner_id ' : partner_id ,
} ) ]
2010-05-28 11:37:10 +00:00
return lines
2010-06-21 21:20:55 +00:00
2008-07-22 15:11:28 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2010-05-12 12:22:04 +00:00
if context is None :
2010-05-28 05:18:58 +00:00
context = { }
2008-07-22 15:11:28 +00:00
for move in self . browse ( cr , uid , ids , context = context ) :
if move . state != ' draft ' :
raise osv . except_osv ( _ ( ' UserError ' ) ,
_ ( ' You can only delete draft moves. ' ) )
return super ( stock_move , self ) . unlink (
2010-03-18 11:25:02 +00:00
cr , uid , ids , context = context )
2007-12-31 13:31:32 +00:00
2010-02-24 10:55:03 +00:00
def _create_lot ( self , cr , uid , ids , product_id , prefix = False ) :
2010-05-26 12:59:30 +00:00
""" Creates production lot
@return : Production lot id
"""
2010-02-19 10:51:45 +00:00
prodlot_obj = self . pool . get ( ' stock.production.lot ' )
2010-06-21 18:40:58 +00:00
prodlot_id = prodlot_obj . create ( cr , uid , { ' prefix ' : prefix , ' product_id ' : product_id } )
2010-02-24 10:55:03 +00:00
return prodlot_id
2010-03-18 06:31:22 +00:00
def action_scrap ( self , cr , uid , ids , quantity , location_id , context = None ) :
2010-05-26 12:59:30 +00:00
""" Move the scrap/damaged product into scrap location
@param cr : the database cursor
@param uid : the user id
2010-08-31 06:54:27 +00:00
@param ids : ids of stock move object to be scrapped
2010-05-26 12:59:30 +00:00
@param quantity : specify scrap qty
@param location_id : specify scrap location
@param context : context arguments
@return : Scraped lines
"""
2010-03-18 06:31:22 +00:00
if quantity < = 0 :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Please provide Proper Quantity ! ' ) )
2010-03-19 08:46:14 +00:00
res = [ ]
for move in self . browse ( cr , uid , ids , context = context ) :
2010-03-18 06:31:22 +00:00
move_qty = move . product_qty
uos_qty = quantity / move_qty * move . product_uos_qty
default_val = {
2010-08-13 12:20:05 +00:00
' product_qty ' : quantity ,
' product_uos_qty ' : uos_qty ,
' state ' : move . state ,
2010-08-31 06:54:27 +00:00
' scrapped ' : True ,
2010-08-30 07:37:18 +00:00
' location_dest_id ' : location_id ,
' tracking_id ' : False ,
' prodlot_id ' : False
2010-08-13 12:20:05 +00:00
}
2010-03-18 06:31:22 +00:00
new_move = self . copy ( cr , uid , move . id , default_val )
2010-03-19 08:46:14 +00:00
res + = [ new_move ]
2010-07-23 10:11:19 +00:00
product_obj = self . pool . get ( ' product.product ' )
for ( id , name ) in product_obj . name_get ( cr , uid , [ move . product_id . id ] ) :
2010-08-31 06:54:27 +00:00
message = _ ( ' Product ' ) + " ' " + name + " ' " + _ ( " is scrapped with " ) + " ' " + str ( move . product_qty ) + " ' " + _ ( " quantity. " )
2010-07-08 10:14:24 +00:00
self . log ( cr , uid , move . id , message )
2010-07-23 10:11:19 +00:00
2010-03-19 08:46:14 +00:00
self . action_done ( cr , uid , res )
2010-03-18 06:31:22 +00:00
return res
def action_split ( self , cr , uid , ids , quantity , split_by_qty = 1 , prefix = False , with_lot = True , context = None ) :
2010-05-26 12:59:30 +00:00
""" Split Stock Move lines into production lot which specified split by quantity.
@param cr : the database cursor
@param uid : the user id
@param ids : ids of stock move object to be splited
@param split_by_qty : specify split by qty
@param prefix : specify prefix of production lot
@param with_lot : if true , prodcution lot will assign for split line otherwise not .
@param context : context arguments
@return : Splited move lines
"""
2010-02-23 05:34:00 +00:00
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
2010-02-24 09:57:17 +00:00
if quantity < = 0 :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Please provide Proper Quantity ! ' ) )
2010-02-24 10:55:03 +00:00
res = [ ]
2010-02-23 05:34:00 +00:00
2010-03-18 11:25:02 +00:00
for move in self . browse ( cr , uid , ids ) :
2010-02-23 05:34:00 +00:00
if split_by_qty < = 0 or quantity == 0 :
return res
uos_qty = split_by_qty / move . product_qty * move . product_uos_qty
quantity_rest = quantity % split_by_qty
uos_qty_rest = split_by_qty / move . product_qty * move . product_uos_qty
update_val = {
' product_qty ' : split_by_qty ,
' product_uos_qty ' : uos_qty ,
2010-03-18 11:25:02 +00:00
}
for idx in range ( int ( quantity / / split_by_qty ) ) :
2010-02-24 10:55:03 +00:00
if not idx and move . product_qty < = quantity :
current_move = move . id
else :
current_move = self . copy ( cr , uid , move . id , { ' state ' : move . state } )
2010-02-24 09:57:17 +00:00
res . append ( current_move )
if with_lot :
2010-02-24 10:55:03 +00:00
update_val [ ' prodlot_id ' ] = self . _create_lot ( cr , uid , [ current_move ] , move . product_id . id )
2010-02-22 07:23:42 +00:00
2010-02-23 05:34:00 +00:00
self . write ( cr , uid , [ current_move ] , update_val )
2010-03-18 11:25:02 +00:00
if quantity_rest > 0 :
idx = int ( quantity / / split_by_qty )
2010-02-23 05:34:00 +00:00
update_val [ ' product_qty ' ] = quantity_rest
2010-03-18 11:25:02 +00:00
update_val [ ' product_uos_qty ' ] = uos_qty_rest
if not idx and move . product_qty < = quantity :
current_move = move . id
2010-02-24 10:55:03 +00:00
else :
2010-03-18 11:25:02 +00:00
current_move = self . copy ( cr , uid , move . id , { ' state ' : move . state } )
2010-02-24 09:57:17 +00:00
res . append ( current_move )
2010-03-18 11:25:02 +00:00
if with_lot :
2010-02-24 10:55:03 +00:00
update_val [ ' prodlot_id ' ] = self . _create_lot ( cr , uid , [ current_move ] , move . product_id . id )
2010-02-24 09:57:17 +00:00
2010-02-23 05:34:00 +00:00
self . write ( cr , uid , [ current_move ] , update_val )
2010-03-18 11:25:02 +00:00
return res
2010-02-22 07:23:42 +00:00
2010-03-19 08:46:14 +00:00
def action_consume ( self , cr , uid , ids , quantity , location_id = False , context = None ) :
2010-05-26 12:59:30 +00:00
""" Consumed product with specific quatity from specific source location
@param cr : the database cursor
@param uid : the user id
@param ids : ids of stock move object to be consumed
@param quantity : specify consume quantity
@param location_id : specify source location
@param context : context arguments
@return : Consumed lines
"""
2010-05-18 14:26:51 +00:00
if context is None :
2010-02-24 09:57:17 +00:00
context = { }
2010-02-23 05:34:00 +00:00
if quantity < = 0 :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( ' Please provide Proper Quantity ! ' ) )
2010-02-24 09:57:17 +00:00
2010-03-18 11:25:02 +00:00
res = [ ]
for move in self . browse ( cr , uid , ids , context = context ) :
2010-02-23 05:34:00 +00:00
move_qty = move . product_qty
quantity_rest = move . product_qty
2010-02-22 07:23:42 +00:00
2010-03-18 11:25:02 +00:00
quantity_rest - = quantity
2010-02-19 13:44:55 +00:00
uos_qty_rest = quantity_rest / move_qty * move . product_uos_qty
2010-02-22 07:23:42 +00:00
if quantity_rest < = 0 :
2010-03-18 11:25:02 +00:00
quantity_rest = 0
2010-02-24 08:17:06 +00:00
uos_qty_rest = 0
quantity = move . product_qty
2010-03-18 11:25:02 +00:00
2010-02-24 08:17:06 +00:00
uos_qty = quantity / move_qty * move . product_uos_qty
2010-02-23 05:34:00 +00:00
2010-03-18 11:25:02 +00:00
if quantity_rest > 0 :
2010-02-24 08:17:06 +00:00
default_val = {
2010-03-18 11:25:02 +00:00
' product_qty ' : quantity ,
' product_uos_qty ' : uos_qty ,
' state ' : move . state ,
2010-03-18 06:31:22 +00:00
' location_id ' : location_id
2010-02-24 08:17:06 +00:00
}
2010-02-24 09:57:17 +00:00
if move . product_id . track_production and location_id :
2010-03-18 06:31:22 +00:00
# IF product has checked track for production lot, move lines will be split by 1
res + = self . action_split ( cr , uid , [ move . id ] , quantity , split_by_qty = 1 , context = context )
2010-02-24 08:17:06 +00:00
else :
current_move = self . copy ( cr , uid , move . id , default_val )
2010-02-24 09:57:17 +00:00
res + = [ current_move ]
2010-03-18 11:25:02 +00:00
update_val = { }
2010-02-23 05:34:00 +00:00
update_val [ ' product_qty ' ] = quantity_rest
2010-03-18 11:25:02 +00:00
update_val [ ' product_uos_qty ' ] = uos_qty_rest
self . write ( cr , uid , [ move . id ] , update_val )
2010-02-24 08:17:06 +00:00
2010-03-18 11:25:02 +00:00
else :
quantity_rest = quantity
2010-02-24 10:55:03 +00:00
uos_qty_rest = uos_qty
if move . product_id . track_production and location_id :
res + = self . split_lines ( cr , uid , [ move . id ] , quantity_rest , split_by_qty = 1 , context = context )
2010-03-18 11:25:02 +00:00
else :
res + = [ move . id ]
2010-03-18 06:31:22 +00:00
update_val = {
' product_qty ' : quantity_rest ,
' product_uos_qty ' : uos_qty_rest ,
' location_id ' : location_id
2010-03-19 08:46:14 +00:00
}
2010-02-24 12:50:18 +00:00
2010-02-24 10:55:03 +00:00
self . write ( cr , uid , [ move . id ] , update_val )
2010-07-23 10:11:19 +00:00
product_obj = self . pool . get ( ' product.product ' )
2010-07-23 13:41:26 +00:00
for new_move in self . browse ( cr , uid , res , context = context ) :
for ( id , name ) in product_obj . name_get ( cr , uid , [ new_move . product_id . id ] ) :
message = _ ( ' Product ' ) + " ' " + name + " ' " + _ ( " is consumed with " ) + " ' " + str ( new_move . product_qty ) + " ' " + _ ( " quantity. " )
self . log ( cr , uid , new_move . id , message )
2010-03-29 11:37:53 +00:00
self . action_done ( cr , uid , res )
2010-07-06 11:44:51 +00:00
2010-02-24 08:17:06 +00:00
return res
2010-03-29 11:37:53 +00:00
2010-07-06 11:21:27 +00:00
def do_partial ( self , cr , uid , ids , partial_datas , context = None ) :
2010-05-26 12:59:30 +00:00
""" Makes partial pickings and moves done.
@param partial_datas : Dictionary containing details of partial picking
2010-06-16 08:40:57 +00:00
like partner_id , address_id , delivery_date , delivery
2010-05-26 12:59:30 +00:00
moves with product_id , product_qty , uom
2010-03-26 14:52:01 +00:00
"""
res = { }
picking_obj = self . pool . get ( ' stock.picking ' )
product_obj = self . pool . get ( ' product.product ' )
currency_obj = self . pool . get ( ' res.currency ' )
users_obj = self . pool . get ( ' res.users ' )
uom_obj = self . pool . get ( ' product.uom ' )
price_type_obj = self . pool . get ( ' product.price.type ' )
sequence_obj = self . pool . get ( ' ir.sequence ' )
wf_service = netsvc . LocalService ( " workflow " )
partner_id = partial_datas . get ( ' partner_id ' , False )
address_id = partial_datas . get ( ' address_id ' , False )
delivery_date = partial_datas . get ( ' delivery_date ' , False )
new_moves = [ ]
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
2010-03-26 14:52:01 +00:00
complete , too_many , too_few = [ ] , [ ] , [ ]
move_product_qty = { }
for move in self . browse ( cr , uid , ids , context = context ) :
if move . state in ( ' done ' , ' cancel ' ) :
continue
2010-03-29 11:37:53 +00:00
partial_data = partial_datas . get ( ' move %s ' % ( move . id ) , False )
2010-03-26 14:52:01 +00:00
assert partial_data , _ ( ' Do not Found Partial data of Stock Move Line : %s ' % ( move . id ) )
product_qty = partial_data . get ( ' product_qty ' , 0.0 )
move_product_qty [ move . id ] = product_qty
2010-03-29 11:37:53 +00:00
product_uom = partial_data . get ( ' product_uom ' , False )
2010-03-26 14:52:01 +00:00
product_price = partial_data . get ( ' product_price ' , 0.0 )
product_currency = partial_data . get ( ' product_currency ' , False )
if move . product_qty == product_qty :
complete . append ( move )
elif move . product_qty > product_qty :
too_few . append ( move )
else :
too_many . append ( move )
# Average price computation
if ( move . picking_id . type == ' in ' ) and ( move . product_id . cost_method == ' average ' ) :
product = product_obj . browse ( cr , uid , move . product_id . id )
2010-03-29 11:37:53 +00:00
user = users_obj . browse ( cr , uid , uid )
context [ ' currency_id ' ] = move . company_id . currency_id . id
2010-03-26 14:52:01 +00:00
qty = uom_obj . _compute_qty ( cr , uid , product_uom , product_qty , product . uom_id . id )
pricetype = False
if user . company_id . property_valuation_price_type :
pricetype = price_type_obj . browse ( cr , uid , user . company_id . property_valuation_price_type . id )
if pricetype and qty > 0 :
new_price = currency_obj . compute ( cr , uid , product_currency ,
user . company_id . currency_id . id , product_price )
new_price = uom_obj . _compute_price ( cr , uid , product_uom , new_price ,
2010-03-29 11:37:53 +00:00
product . uom_id . id )
2010-03-26 14:52:01 +00:00
if product . qty_available < = 0 :
new_std_price = new_price
else :
# Get the standard price
amount_unit = product . price_get ( pricetype . field , context ) [ product . id ]
new_std_price = ( ( amount_unit * product . qty_available ) \
+ ( new_price * qty ) ) / ( product . qty_available + qty )
2010-03-29 11:37:53 +00:00
2010-03-26 14:52:01 +00:00
# Write the field according to price type field
product_obj . write ( cr , uid , [ product . id ] ,
{ pricetype . field : new_std_price } )
self . write ( cr , uid , [ move . id ] , { ' price_unit ' : new_price } )
2010-03-29 11:37:53 +00:00
for move in too_few :
product_qty = move_product_qty [ move . id ]
2010-03-26 14:52:01 +00:00
if product_qty != 0 :
new_move = self . copy ( cr , uid , move . id ,
{
' product_qty ' : product_qty ,
' product_uos_qty ' : product_qty ,
' picking_id ' : move . picking_id . id ,
' state ' : ' assigned ' ,
' move_dest_id ' : False ,
' price_unit ' : move . price_unit ,
} )
complete . append ( self . browse ( cr , uid , new_move ) )
self . write ( cr , uid , move . id ,
{
' product_qty ' : move . product_qty - product_qty ,
' product_uos_qty ' : move . product_qty - product_qty ,
} )
2010-03-29 11:37:53 +00:00
2010-03-26 14:52:01 +00:00
for move in too_many :
self . write ( cr , uid , move . id ,
{
2010-05-05 10:27:11 +00:00
' product_qty ' : move . product_qty ,
2010-05-11 11:53:03 +00:00
' product_uos_qty ' : move . product_qty ,
2010-03-26 14:52:01 +00:00
} )
2010-03-29 11:37:53 +00:00
complete . append ( move )
2010-03-26 14:52:01 +00:00
for move in complete :
2010-07-06 11:21:27 +00:00
self . action_done ( cr , uid , [ move . id ] , context = context )
2010-06-18 09:41:42 +00:00
if move . picking_id . id :
# TOCHECK : Done picking if all moves are done
cr . execute ( """
SELECT move . id FROM stock_picking pick
RIGHT JOIN stock_move move ON move . picking_id = pick . id AND move . state = % s
WHERE pick . id = % s """ ,
( ' done ' , move . picking_id . id ) )
res = cr . fetchall ( )
if len ( res ) == len ( move . picking_id . move_lines ) :
picking_obj . action_move ( cr , uid , [ move . picking_id . id ] )
wf_service . trg_validate ( uid , ' stock.picking ' , move . picking_id . id , ' button_done ' , cr )
2010-03-29 11:37:53 +00:00
ref = { }
2010-03-26 14:52:01 +00:00
done_move_ids = [ ]
2010-03-29 11:37:53 +00:00
for move in complete :
done_move_ids . append ( move . id )
2010-03-26 14:52:01 +00:00
return done_move_ids
2007-12-31 13:31:32 +00:00
2006-12-07 13:41:40 +00:00
stock_move ( )
class stock_inventory ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.inventory "
_description = " Inventory "
_columns = {
2009-08-28 09:40:49 +00:00
' name ' : fields . char ( ' Inventory ' , size = 64 , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' date ' : fields . datetime ( ' Date create ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2008-07-22 15:11:28 +00:00
' date_done ' : fields . datetime ( ' Date done ' ) ,
2010-01-19 08:57:59 +00:00
' inventory_line_id ' : fields . one2many ( ' stock.inventory.line ' , ' inventory_id ' , ' Inventories ' , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2008-07-22 15:11:28 +00:00
' move_ids ' : fields . many2many ( ' stock.move ' , ' stock_inventory_move_rel ' , ' inventory_id ' , ' move_id ' , ' Created Moves ' ) ,
2009-11-23 13:52:38 +00:00
' state ' : fields . selection ( ( ( ' draft ' , ' Draft ' ) , ( ' done ' , ' Done ' ) , ( ' cancel ' , ' Cancelled ' ) ) , ' State ' , readonly = True ) ,
2009-12-22 19:07:37 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True , select = 1 ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
2010-07-06 11:44:51 +00:00
' date ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' state ' : ' draft ' ,
2009-12-23 17:16:39 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' stock.inventory ' , context = c )
2008-07-22 15:11:28 +00:00
}
2009-08-28 09:40:49 +00:00
2010-02-28 03:18:01 +00:00
def _inventory_line_hook ( self , cr , uid , inventory_line , move_vals ) :
2010-05-26 12:59:30 +00:00
""" Creates a stock move from an inventory line
@param inventory_line :
@param move_vals :
2010-06-16 08:40:57 +00:00
@return :
2010-05-26 12:59:30 +00:00
"""
2010-02-28 03:18:01 +00:00
return self . pool . get ( ' stock.move ' ) . create ( cr , uid , move_vals )
2009-08-28 09:40:49 +00:00
2008-07-22 15:11:28 +00:00
def action_done ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Finishes the inventory and writes its finished date
@return : True
"""
2010-07-06 11:44:51 +00:00
if context is None :
context = { }
2009-08-28 09:40:49 +00:00
for inv in self . browse ( cr , uid , ids ) :
2008-07-22 15:11:28 +00:00
move_ids = [ ]
2009-08-28 09:40:49 +00:00
move_line = [ ]
2008-07-22 15:11:28 +00:00
for line in inv . inventory_line_id :
2009-08-28 09:40:49 +00:00
pid = line . product_id . id
2010-03-18 11:25:02 +00:00
2009-08-28 09:40:49 +00:00
amount = self . pool . get ( ' stock.location ' ) . _product_get ( cr , uid , line . location_id . id , [ pid ] , { ' uom ' : line . product_uom . id } ) [ pid ]
2010-07-08 07:13:21 +00:00
#TOCHECK: Why put restriction like new inventory qty should greater available qty ?
2009-08-28 09:40:49 +00:00
change = line . product_qty - amount
2010-01-19 08:57:59 +00:00
lot_id = line . prod_lot_id . id
2008-07-22 15:11:28 +00:00
if change :
location_id = line . product_id . product_tmpl_id . property_stock_inventory . id
value = {
2009-08-28 09:40:49 +00:00
' name ' : ' INV: ' + str ( line . inventory_id . id ) + ' : ' + line . inventory_id . name ,
2008-07-22 15:11:28 +00:00
' product_id ' : line . product_id . id ,
' product_uom ' : line . product_uom . id ,
2010-02-01 12:50:00 +00:00
' prodlot_id ' : lot_id ,
2008-07-22 15:11:28 +00:00
' date ' : inv . date ,
' date_planned ' : inv . date ,
2010-06-16 11:38:52 +00:00
' state ' : ' done '
2008-07-22 15:11:28 +00:00
}
2009-08-28 09:40:49 +00:00
if change > 0 :
2008-07-22 15:11:28 +00:00
value . update ( {
' product_qty ' : change ,
' location_id ' : location_id ,
' location_dest_id ' : line . location_id . id ,
} )
else :
value . update ( {
' product_qty ' : - change ,
' location_id ' : line . location_id . id ,
' location_dest_id ' : location_id ,
} )
2010-01-19 08:57:59 +00:00
if lot_id :
value . update ( {
' prodlot_id ' : lot_id ,
2010-02-01 12:50:00 +00:00
' product_qty ' : line . product_qty
2010-01-19 08:57:59 +00:00
} )
2010-02-28 03:18:01 +00:00
move_ids . append ( self . _inventory_line_hook ( cr , uid , line , value ) )
2010-07-08 10:14:24 +00:00
message = _ ( ' Inventory ' ) + " ' " + inv . name + " ' " + _ ( " is done. " )
self . log ( cr , uid , inv . id , message )
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , [ inv . id ] , { ' state ' : ' done ' , ' date_done ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) , ' move_ids ' : [ ( 6 , 0 , move_ids ) ] } )
2008-07-22 15:11:28 +00:00
return True
2010-07-06 11:21:27 +00:00
def action_cancel ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Cancels the stock move and change inventory state to draft.
@return : True
"""
2009-08-28 09:40:49 +00:00
for inv in self . browse ( cr , uid , ids ) :
2008-07-22 15:11:28 +00:00
self . pool . get ( ' stock.move ' ) . action_cancel ( cr , uid , [ x . id for x in inv . move_ids ] , context )
2009-08-28 09:40:49 +00:00
self . write ( cr , uid , [ inv . id ] , { ' state ' : ' draft ' } )
2008-07-22 15:11:28 +00:00
return True
2009-08-28 09:40:49 +00:00
2010-07-06 11:21:27 +00:00
def action_cancel_inventary ( self , cr , uid , ids , context = None ) :
2010-05-26 12:59:30 +00:00
""" Cancels both stock move and inventory
@return : True
"""
2009-09-24 10:46:21 +00:00
for inv in self . browse ( cr , uid , ids ) :
self . pool . get ( ' stock.move ' ) . action_cancel ( cr , uid , [ x . id for x in inv . move_ids ] , context )
self . write ( cr , uid , [ inv . id ] , { ' state ' : ' cancel ' } )
return True
2006-12-07 13:41:40 +00:00
stock_inventory ( )
class stock_inventory_line ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.inventory.line "
2010-05-19 18:32:32 +00:00
_description = " Inventory Line "
2008-07-22 15:11:28 +00:00
_columns = {
2009-08-28 09:40:49 +00:00
' inventory_id ' : fields . many2one ( ' stock.inventory ' , ' Inventory ' , ondelete = ' cascade ' , select = True ) ,
' location_id ' : fields . many2one ( ' stock.location ' , ' Location ' , required = True ) ,
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' , required = True ) ,
' product_uom ' : fields . many2one ( ' product.uom ' , ' Product UOM ' , required = True ) ,
2009-11-28 09:25:22 +00:00
' product_qty ' : fields . float ( ' Quantity ' ) ,
2010-01-19 08:57:59 +00:00
' company_id ' : fields . related ( ' inventory_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True ) ,
2010-03-12 13:42:55 +00:00
' prod_lot_id ' : fields . many2one ( ' stock.production.lot ' , ' Production Lot ' , domain = " [( ' product_id ' , ' = ' ,product_id)] " ) ,
2010-03-15 13:16:40 +00:00
' state ' : fields . related ( ' inventory_id ' , ' state ' , type = ' char ' , string = ' State ' , readonly = True ) ,
2008-07-22 15:11:28 +00:00
}
2010-03-15 13:16:40 +00:00
2008-07-22 15:11:28 +00:00
def on_change_product_id ( self , cr , uid , ids , location_id , product , uom = False ) :
2010-05-26 12:59:30 +00:00
""" Changes UoM and name if product_id changes.
@param location_id : Location id
@param product : Changed product_id
2010-06-16 08:40:57 +00:00
@param uom : UoM product
2010-05-26 12:59:30 +00:00
@return : Dictionary of changed values
"""
2008-07-22 15:11:28 +00:00
if not product :
return { }
if not uom :
prod = self . pool . get ( ' product.product ' ) . browse ( cr , uid , [ product ] , { ' uom ' : uom } ) [ 0 ]
uom = prod . uom_id . id
2009-08-28 09:40:49 +00:00
amount = self . pool . get ( ' stock.location ' ) . _product_get ( cr , uid , location_id , [ product ] , { ' uom ' : uom } ) [ product ]
result = { ' product_qty ' : amount , ' product_uom ' : uom }
return { ' value ' : result }
2006-12-07 13:41:40 +00:00
stock_inventory_line ( )
#----------------------------------------------------------
# Stock Warehouse
#----------------------------------------------------------
class stock_warehouse ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " stock.warehouse "
_description = " Warehouse "
_columns = {
' name ' : fields . char ( ' Name ' , size = 60 , required = True ) ,
2009-12-22 19:07:37 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True , select = 1 ) ,
2008-07-22 15:11:28 +00:00
' partner_address_id ' : fields . many2one ( ' res.partner.address ' , ' Owner Address ' ) ,
2010-05-10 13:54:26 +00:00
' lot_input_id ' : fields . many2one ( ' stock.location ' , ' Location Input ' , required = True , domain = [ ( ' usage ' , ' <> ' , ' view ' ) ] ) ,
' lot_stock_id ' : fields . many2one ( ' stock.location ' , ' Location Stock ' , required = True , domain = [ ( ' usage ' , ' <> ' , ' view ' ) ] ) ,
' lot_output_id ' : fields . many2one ( ' stock.location ' , ' Location Output ' , required = True , domain = [ ( ' usage ' , ' <> ' , ' view ' ) ] ) ,
2008-07-22 15:11:28 +00:00
}
2009-11-28 09:25:22 +00:00
_defaults = {
2010-07-06 11:21:27 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' stock.inventory ' , context = c ) ,
2009-11-28 09:25:22 +00:00
}
2010-07-06 11:21:27 +00:00
2006-12-07 13:41:40 +00:00
stock_warehouse ( )
2008-11-03 14:40:49 +00:00
# Move wizard :
2008-07-30 06:29:44 +00:00
# get confirm or assign stock move lines of partner and put in current picking.
class stock_picking_move_wizard ( osv . osv_memory ) :
2009-08-28 09:40:49 +00:00
_name = ' stock.picking.move.wizard '
2010-07-06 11:21:27 +00:00
def _get_picking ( self , cr , uid , ctx = None ) :
if ctx is None :
ctx = { }
2009-08-28 09:40:49 +00:00
if ctx . get ( ' action_id ' , False ) :
2008-08-27 05:44:58 +00:00
return ctx [ ' action_id ' ]
2008-11-03 14:40:49 +00:00
return False
2009-08-28 09:40:49 +00:00
2010-07-06 11:21:27 +00:00
def _get_picking_address ( self , cr , uid , context = None ) :
2009-08-28 09:40:49 +00:00
picking_obj = self . pool . get ( ' stock.picking ' )
2010-07-06 11:21:27 +00:00
if context is None :
context = { }
if context . get ( ' action_id ' , False ) :
picking = picking_obj . browse ( cr , uid , [ context [ ' action_id ' ] ] ) [ 0 ]
2008-11-03 14:40:49 +00:00
return picking . address_id and picking . address_id . id or False
2008-07-30 06:29:44 +00:00
return False
2008-11-03 14:40:49 +00:00
2009-08-28 09:40:49 +00:00
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , invisible = True ) ,
2009-11-25 09:31:44 +00:00
' move_ids ' : fields . many2many ( ' stock.move ' , ' picking_move_wizard_rel ' , ' picking_move_wizard_id ' , ' move_id ' , ' Entry lines ' , required = True ) ,
2009-08-28 09:40:49 +00:00
' address_id ' : fields . many2one ( ' res.partner.address ' , ' Dest. Address ' , invisible = True ) ,
2009-11-13 05:41:16 +00:00
' picking_id ' : fields . many2one ( ' stock.picking ' , ' Picking list ' , select = True , invisible = True ) ,
2008-07-30 06:29:44 +00:00
}
2009-08-28 09:40:49 +00:00
_defaults = {
' picking_id ' : _get_picking ,
' address_id ' : _get_picking_address ,
2008-07-30 06:29:44 +00:00
}
2009-08-28 09:40:49 +00:00
def action_move ( self , cr , uid , ids , context = None ) :
move_obj = self . pool . get ( ' stock.move ' )
picking_obj = self . pool . get ( ' stock.picking ' )
2010-05-27 07:10:12 +00:00
account_move_obj = self . pool . get ( ' account.move ' )
2009-08-28 09:40:49 +00:00
for act in self . read ( cr , uid , ids ) :
move_lines = move_obj . browse ( cr , uid , act [ ' move_ids ' ] )
2008-07-30 06:29:44 +00:00
for line in move_lines :
2009-06-17 14:13:23 +00:00
if line . picking_id :
2009-08-28 09:40:49 +00:00
picking_obj . write ( cr , uid , [ line . picking_id . id ] , { ' move_lines ' : [ ( 1 , line . id , { ' picking_id ' : act [ ' picking_id ' ] } ) ] } )
picking_obj . write ( cr , uid , [ act [ ' picking_id ' ] ] , { ' move_lines ' : [ ( 1 , line . id , { ' picking_id ' : act [ ' picking_id ' ] } ) ] } )
old_picking = picking_obj . read ( cr , uid , [ line . picking_id . id ] ) [ 0 ]
2009-06-17 14:13:23 +00:00
if not len ( old_picking [ ' move_lines ' ] ) :
2009-08-28 09:40:49 +00:00
picking_obj . write ( cr , uid , [ old_picking [ ' id ' ] ] , { ' state ' : ' done ' } )
2009-06-17 14:13:23 +00:00
else :
raise osv . except_osv ( _ ( ' UserError ' ) ,
_ ( ' You can not create new moves. ' ) )
2009-08-28 09:40:49 +00:00
return { ' type ' : ' ir.actions.act_window_close ' }
2008-11-03 14:40:49 +00:00
stock_picking_move_wizard ( )
2010-02-01 12:50:00 +00:00
class report_products_to_received_planned ( osv . osv ) :
_name = " report.products.to.received.planned "
_description = " Product to Received Vs Planned "
_auto = False
_columns = {
' date ' : fields . date ( ' Date ' ) ,
' qty ' : fields . integer ( ' Actual Qty ' ) ,
' planned_qty ' : fields . integer ( ' Planned Qty ' ) ,
}
def init ( self , cr ) :
tools . drop_view_if_exists ( cr , ' report_products_to_received_planned ' )
cr . execute ( """
create or replace view report_products_to_received_planned as (
2010-03-31 14:58:35 +00:00
select stock . date , min ( stock . id ) as id , sum ( stock . product_qty ) as qty , 0 as planned_qty
2010-02-02 13:10:24 +00:00
from stock_picking picking
inner join stock_move stock
on picking . id = stock . picking_id and picking . type = ' in '
where stock . date between ( select cast ( date_trunc ( ' week ' , current_date ) as date ) ) and ( select cast ( date_trunc ( ' week ' , current_date ) as date ) + 7 )
group by stock . date
union
2010-03-31 14:58:35 +00:00
select stock . date_planned , min ( stock . id ) as id , 0 as actual_qty , sum ( stock . product_qty ) as planned_qty
2010-02-02 13:10:24 +00:00
from stock_picking picking
inner join stock_move stock
on picking . id = stock . picking_id and picking . type = ' in '
where stock . date_planned between ( select cast ( date_trunc ( ' week ' , current_date ) as date ) ) and ( select cast ( date_trunc ( ' week ' , current_date ) as date ) + 7 )
group by stock . date_planned
2010-02-01 12:50:00 +00:00
)
""" )
2010-07-06 11:21:27 +00:00
2010-02-01 12:50:00 +00:00
report_products_to_received_planned ( )
class report_delivery_products_planned ( osv . osv ) :
_name = " report.delivery.products.planned "
_description = " Number of Delivery products vs planned "
_auto = False
_columns = {
' date ' : fields . date ( ' Date ' ) ,
' qty ' : fields . integer ( ' Actual Qty ' ) ,
' planned_qty ' : fields . integer ( ' Planned Qty ' ) ,
}
def init ( self , cr ) :
tools . drop_view_if_exists ( cr , ' report_delivery_products_planned ' )
cr . execute ( """
create or replace view report_delivery_products_planned as (
2010-03-31 14:58:35 +00:00
select stock . date , min ( stock . id ) as id , sum ( stock . product_qty ) as qty , 0 as planned_qty
2010-02-02 13:10:24 +00:00
from stock_picking picking
inner join stock_move stock
on picking . id = stock . picking_id and picking . type = ' out '
where stock . date between ( select cast ( date_trunc ( ' week ' , current_date ) as date ) ) and ( select cast ( date_trunc ( ' week ' , current_date ) as date ) + 7 )
group by stock . date
union
select stock . date_planned , min ( stock . id ) , 0 as actual_qty , sum ( stock . product_qty ) as planned_qty
from stock_picking picking
inner join stock_move stock
on picking . id = stock . picking_id and picking . type = ' out '
where stock . date_planned between ( select cast ( date_trunc ( ' week ' , current_date ) as date ) ) and ( select cast ( date_trunc ( ' week ' , current_date ) as date ) + 7 )
group by stock . date_planned
2010-02-01 12:50:00 +00:00
)
""" )
2010-07-06 11:21:27 +00:00
2010-02-01 12:50:00 +00:00
report_delivery_products_planned ( )
2010-07-06 11:21:27 +00:00
2010-03-31 14:58:35 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: