2010-08-10 12:28:52 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import netsvc
import time
from osv import osv , fields
from tools . translate import _
class stock_return_picking ( osv . osv_memory ) :
_name = ' stock.return.picking '
_description = ' Return Picking '
2010-11-22 10:37:53 +00:00
def default_get ( self , cr , uid , fields , context = None ) :
2010-08-25 18:09:47 +00:00
"""
2010-08-10 12:28:52 +00:00
To get default values for the object .
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
2010-08-25 18:09:47 +00:00
@param fields : List of fields for which we want default values
@param context : A standard dictionary
@return : A dictionary with default values for all field in ` ` fields ` `
"""
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2010-08-10 12:28:52 +00:00
res = super ( stock_return_picking , self ) . default_get ( cr , uid , fields , context = context )
record_id = context and context . get ( ' active_id ' , False ) or False
pick_obj = self . pool . get ( ' stock.picking ' )
2010-11-22 10:37:53 +00:00
pick = pick_obj . browse ( cr , uid , record_id , context = context )
2010-08-25 18:09:47 +00:00
if pick :
if ' invoice_state ' in fields :
if pick . invoice_state == ' invoiced ' :
res [ ' invoice_state ' ] = ' 2binvoiced '
else :
res [ ' invoice_state ' ] = ' none '
for line in pick . move_lines :
return_id = ' return %s ' % ( line . id )
2010-12-29 13:14:20 +00:00
return_price = ' return_price %s ' % ( line . id )
2010-08-25 18:09:47 +00:00
if return_id in fields :
res [ return_id ] = line . product_qty
2010-12-29 13:14:20 +00:00
res [ return_price ] = line . price_unit
2010-08-10 12:28:52 +00:00
return res
2010-08-25 18:09:47 +00:00
2010-08-10 12:28:52 +00:00
def view_init ( self , cr , uid , fields_list , context = None ) :
2010-12-06 12:42:05 +00:00
"""
2010-08-10 12:28:52 +00:00
Creates view dynamically and adding fields at runtime .
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
2010-12-06 12:42:05 +00:00
@param context : A standard dictionary
2010-08-10 12:28:52 +00:00
@return : New arch of view with new columns .
"""
2010-11-23 07:05:05 +00:00
if context is None :
2010-11-22 10:37:53 +00:00
context = { }
2010-08-10 12:28:52 +00:00
res = super ( stock_return_picking , self ) . view_init ( cr , uid , fields_list , context = context )
2010-12-06 12:42:05 +00:00
record_id = context and context . get ( ' active_id ' , False )
2010-08-10 12:28:52 +00:00
if record_id :
pick_obj = self . pool . get ( ' stock.picking ' )
2010-11-22 10:37:53 +00:00
pick = pick_obj . browse ( cr , uid , record_id , context = context )
2010-09-28 17:25:47 +00:00
if pick . state not in [ ' done ' , ' confirmed ' , ' assigned ' ] :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( " You may only return pickings that are Confirmed, Available or Done! " ) )
return_history = { }
valid_lines = 0
2010-09-17 10:08:47 +00:00
for m in [ line for line in pick . move_lines ] :
2010-09-28 17:25:47 +00:00
if m . state == ' done ' :
return_history [ m . id ] = 0
for rec in m . move_history_ids2 :
return_history [ m . id ] + = ( rec . product_qty * rec . product_uom . factor )
2010-12-14 16:06:57 +00:00
if m . product_qty * m . product_uom . factor > = return_history [ m . id ] :
2010-09-28 17:25:47 +00:00
valid_lines + = 1
if ' return %s ' % ( m . id ) not in self . _columns :
self . _columns [ ' return %s ' % ( m . id ) ] = fields . float ( string = m . name , required = True )
2010-12-29 13:14:20 +00:00
if ' return_price %s ' % ( m . id ) not in self . _columns :
self . _columns [ ' return_price %s ' % ( m . id ) ] = fields . float ( string = m . name , required = True )
2010-09-28 17:25:47 +00:00
if ' invoice_state ' not in self . _columns :
2010-10-15 13:42:15 +00:00
self . _columns [ ' invoice_state ' ] = fields . selection ( [ ( ' 2binvoiced ' , ' To be refunded/invoiced ' ) , ( ' none ' , ' No invoicing ' ) ] , string = ' Invoicing ' , required = True )
2010-12-29 13:14:20 +00:00
2010-09-28 17:25:47 +00:00
if not valid_lines :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( " There are no products to return (only lines in Done state and not fully returned yet can be returned)! " ) )
2010-08-10 12:28:52 +00:00
return res
2010-12-06 12:42:05 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' ,
2010-08-10 12:28:52 +00:00
context = None , toolbar = False , submenu = False ) :
2010-12-06 12:42:05 +00:00
"""
2010-08-10 12:28:52 +00:00
Changes the view dynamically
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
2010-12-06 12:42:05 +00:00
@param context : A standard dictionary
2010-08-10 12:28:52 +00:00
@return : New arch of view .
"""
res = super ( stock_return_picking , self ) . fields_view_get ( cr , uid , view_id = view_id , view_type = view_type , context = context , toolbar = toolbar , submenu = False )
record_id = context and context . get ( ' active_id ' , False )
active_model = context . get ( ' active_model ' )
if active_model != ' stock.picking ' :
return res
if record_id :
pick_obj = self . pool . get ( ' stock.picking ' )
pick = pick_obj . browse ( cr , uid , record_id )
return_history = { }
res [ ' fields ' ] . clear ( )
arch_lst = [ ' <?xml version= " 1.0 " ?> ' , ' <form string= " %s " > ' % _ ( ' Return lines ' ) , ' <label string= " %s " colspan= " 4 " /> ' % _ ( ' Provide the quantities of the returned products. ' ) ]
2010-09-28 17:25:47 +00:00
for m in pick . move_lines :
return_history [ m . id ] = 0
for rec in m . move_history_ids2 :
return_history [ m . id ] + = rec . product_qty
2010-08-10 12:28:52 +00:00
quantity = m . product_qty
2010-09-28 15:30:03 +00:00
if m . state == ' done ' and quantity > return_history [ m . id ] :
2010-08-10 12:28:52 +00:00
arch_lst . append ( ' <field name= " return %s " /> \n <newline/> ' % ( m . id , ) )
2010-12-06 12:42:05 +00:00
res [ ' fields ' ] [ ' return %s ' % m . id ] = { ' string ' : m . name , ' type ' : ' float ' , ' required ' : True }
2010-12-29 13:14:20 +00:00
if m . product_id . cost_method == ' average ' and pick . type == ' in ' :
arch_lst . append ( ' <field name= " return_price %s " /> \n <newline/> ' % ( m . id , ) )
res [ ' fields ' ] [ ' return_price %s ' % m . id ] = { ' string ' : ' Price ' , ' type ' : ' float ' , ' required ' : True }
2010-08-10 12:28:52 +00:00
res . setdefault ( ' returns ' , [ ] ) . append ( m . id )
arch_lst . append ( ' <field name= " invoice_state " /> \n <newline/> ' )
2010-10-15 13:42:15 +00:00
res [ ' fields ' ] [ ' invoice_state ' ] = { ' string ' : _ ( ' Invoicing ' ) , ' type ' : ' selection ' , ' required ' : True , ' selection ' : [ ( ' 2binvoiced ' , _ ( ' To be refunded/invoiced ' ) ) , ( ' none ' , _ ( ' No invoicing ' ) ) ] }
2010-08-10 12:28:52 +00:00
arch_lst . append ( ' <group col= " 2 " colspan= " 4 " > ' )
arch_lst . append ( ' <button icon= " gtk-cancel " special= " cancel " string= " Cancel " /> ' )
2010-09-17 11:25:14 +00:00
arch_lst . append ( ' <button name= " create_returns " string= " Return " colspan= " 1 " type= " object " icon= " gtk-apply " /> ' )
2010-08-10 12:28:52 +00:00
arch_lst . append ( ' </group> ' )
arch_lst . append ( ' </form> ' )
res [ ' arch ' ] = ' \n ' . join ( arch_lst )
return res
2010-11-22 10:37:53 +00:00
def create_returns ( self , cr , uid , ids , context = None ) :
2010-12-29 13:14:20 +00:00
"""
2010-08-10 12:28:52 +00:00
Creates return picking .
@param self : The object pointer .
@param cr : A database cursor
@param uid : ID of the user currently logged in
2010-12-06 12:42:05 +00:00
@param ids : List of ids selected
@param context : A standard dictionary
@return : A dictionary which of fields with values .
"""
2010-11-23 07:05:05 +00:00
if context is None :
2010-12-29 13:14:20 +00:00
context = { }
2010-08-10 12:28:52 +00:00
record_id = context and context . get ( ' active_id ' , False ) or False
move_obj = self . pool . get ( ' stock.move ' )
pick_obj = self . pool . get ( ' stock.picking ' )
uom_obj = self . pool . get ( ' product.uom ' )
wf_service = netsvc . LocalService ( " workflow " )
2010-12-29 13:14:20 +00:00
2010-11-22 10:37:53 +00:00
pick = pick_obj . browse ( cr , uid , record_id , context = context )
2010-08-10 12:28:52 +00:00
data = self . read ( cr , uid , ids [ 0 ] )
new_picking = None
date_cur = time . strftime ( ' % Y- % m- %d % H: % M: % S ' )
2010-12-06 12:42:05 +00:00
2010-08-10 12:28:52 +00:00
set_invoice_state_to_none = True
2010-09-28 17:25:47 +00:00
returned_lines = 0
2010-11-04 12:11:48 +00:00
for move in pick . move_lines :
2010-08-10 12:28:52 +00:00
if not new_picking :
if pick . type == ' out ' :
new_type = ' in '
elif pick . type == ' in ' :
new_type = ' out '
else :
new_type = ' internal '
2010-12-06 12:42:05 +00:00
new_picking = pick_obj . copy ( cr , uid , pick . id , { ' name ' : ' %s -return ' % pick . name ,
2010-08-10 12:28:52 +00:00
' move_lines ' : [ ] , ' state ' : ' draft ' , ' type ' : new_type ,
' date ' : date_cur , ' invoice_state ' : data [ ' invoice_state ' ] , } )
new_location = move . location_dest_id . id
2010-09-22 12:50:36 +00:00
if move . state == ' done ' :
new_qty = data [ ' return %s ' % move . id ]
2010-12-29 13:14:20 +00:00
price_unit = data [ ' return_price %s ' % move . id ]
2010-09-22 12:50:36 +00:00
returned_qty = move . product_qty
2010-12-06 12:42:05 +00:00
2010-09-22 12:50:36 +00:00
for rec in move . move_history_ids2 :
returned_qty - = rec . product_qty
2010-12-06 12:42:05 +00:00
2010-09-22 12:50:36 +00:00
if returned_qty != new_qty :
set_invoice_state_to_none = False
2010-09-28 17:25:47 +00:00
if new_qty :
2010-12-06 12:42:05 +00:00
returned_lines + = 1
2010-09-28 17:25:47 +00:00
new_move = move_obj . copy ( cr , uid , move . id , {
' product_qty ' : new_qty ,
' product_uos_qty ' : uom_obj . _compute_qty ( cr , uid , move . product_uom . id ,
new_qty , move . product_uos . id ) ,
' picking_id ' : new_picking , ' state ' : ' draft ' ,
' location_id ' : new_location , ' location_dest_id ' : move . location_id . id ,
2010-12-29 13:14:20 +00:00
' date ' : date_cur ,
' price_unit ' : price_unit } )
2010-09-28 17:25:47 +00:00
move_obj . write ( cr , uid , [ move . id ] , { ' move_history_ids2 ' : [ ( 4 , new_move ) ] } )
if not returned_lines :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( " Please specify at least one non-zero quantity! " ) )
2010-08-10 12:28:52 +00:00
if set_invoice_state_to_none :
pick_obj . write ( cr , uid , [ pick . id ] , { ' invoice_state ' : ' none ' } )
2010-09-28 17:25:47 +00:00
wf_service . trg_validate ( uid , ' stock.picking ' , new_picking , ' button_confirm ' , cr )
pick_obj . force_assign ( cr , uid , [ new_picking ] , context )
return {
' domain ' : " [( ' id ' , ' in ' , [ " + str ( new_picking ) + " ])] " ,
' name ' : ' Picking List ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' tree,form ' ,
' res_model ' : ' stock.picking ' ,
' view_id ' : False ,
' type ' : ' ir.actions.act_window ' ,
' context ' : context ,
}
2010-08-10 12:28:52 +00:00
stock_return_picking ( )
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: