[FIX]: stock: when returning products if we change the price it should reflact in...
[odoo/odoo.git] / addons / stock / wizard / stock_partial_move.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from tools.translate import _
24 import time
25
26
27 class stock_partial_move_memory_out(osv.osv_memory):
28     _name = "stock.move.memory.out"
29     _rec_name = 'product_id'
30     _columns = {
31         'product_id' : fields.many2one('product.product', string="Product", required=True),
32         'quantity' : fields.float("Quantity", required=True),
33         'product_uom': fields.many2one('product.uom', 'Unit of Measure', required=True),
34         'prodlot_id' : fields.many2one('stock.production.lot', 'Production Lot'),
35         'move_id' : fields.many2one('stock.move', "Move"),
36         'wizard_id' : fields.many2one('stock.partial.move', string="Wizard"),
37         'cost' : fields.float("Cost", help="Unit Cost for this product line"),
38         'currency' : fields.many2one('res.currency', string="Currency", help="Currency in which Unit cost is expressed"),
39     }
40     
41 class stock_partial_move_memory_in(osv.osv_memory):
42     _inherit = "stock.move.memory.out"
43     _name = "stock.move.memory.in"
44     
45 class stock_partial_move(osv.osv_memory):
46     _name = "stock.partial.move"
47     _description = "Partial Move"
48     _columns = {
49         'date': fields.datetime('Date', required=True),
50         'type': fields.char("Type", size=3),
51         'product_moves_out' : fields.one2many('stock.move.memory.out', 'wizard_id', 'Moves'),
52         'product_moves_in' : fields.one2many('stock.move.memory.in', 'wizard_id', 'Moves'),
53      }
54     
55     
56     def __is_in(self,cr, uid, move_ids):
57         """
58             @return: True if one of the moves has as picking type 'in'
59         """
60         if not move_ids:
61             return False
62        
63         move_obj = self.pool.get('stock.move')
64         move_ids = move_obj.search(cr, uid, [('id','in',move_ids)])
65        
66         for move in move_obj.browse(cr, uid, move_ids):
67             if move.picking_id.type == 'in' and move.product_id.cost_method == 'average':
68                 return True
69         return False
70     
71     def __get_picking_type(self, cr, uid, move_ids):
72         if self.__is_in(cr, uid, move_ids):
73             return "product_moves_in"
74         else:
75             return "product_moves_out"
76     
77     def view_init(self, cr, uid, fields_list, context=None):
78         res = super(stock_partial_move, self).view_init(cr, uid, fields_list, context=context)
79         move_obj = self.pool.get('stock.move')
80     
81         if context is None:
82             context = {}
83         for move in move_obj.browse(cr, uid, context.get('active_ids', []), context=context):
84             if move.state in ('done', 'cancel'):
85                 raise osv.except_osv(_('Invalid action !'), _('Cannot deliver products which are already delivered !'))
86             
87         return res
88     
89     
90     def __create_partial_move_memory(self, move):
91         move_memory = {
92             'product_id' : move.product_id.id,
93             'quantity' : move.product_qty,
94             'product_uom' : move.product_uom.id,
95             'prodlot_id' : move.prodlot_id.id,
96             'move_id' : move.id,
97         }
98     
99         if move.picking_id.type == 'in':
100             move_memory.update({
101                 'cost' : move.product_id.standard_price,
102                 'currency' : move.product_id.company_id and move.product_id.company_id.currency_id and move.product_id.company_id.currency_id.id or False,
103             })
104         return move_memory
105
106     def __get_active_stock_moves(self, cr, uid, context=None):
107         move_obj = self.pool.get('stock.move')
108         if context is None:
109             context = {}
110                
111         res = []
112         for move in move_obj.browse(cr, uid, context.get('active_ids', []), context=context):
113             if move.state in ('done', 'cancel'):
114                 continue           
115             res.append(self.__create_partial_move_memory(move))
116             
117         return res
118     
119     _defaults = {
120         'product_moves_in' : __get_active_stock_moves,
121         'product_moves_out' : __get_active_stock_moves,
122         'date' : lambda *a : time.strftime('%Y-%m-%d %H:%M:%S'),
123     }
124     
125     
126     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
127         if not context:
128             context = {}
129         
130         message = {
131                 'title' : _('Deliver Products'),
132                 'info' : _('Delivery Information'),
133                 'button' : _('Deliver'),
134                 }
135         if context:            
136             if context.get('product_receive', False):
137                 message = {
138                     'title' : _('Receive Products'),
139                     'info' : _('Receive Information'),
140                     'button' : _('Receive'),
141                 }   
142          
143         move_ids = context.get('active_ids', False)    
144         message['picking_type'] = self.__get_picking_type(cr, uid, move_ids)
145         result = super(stock_partial_move, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
146         _moves_fields = result['fields']
147         _moves_fields.update({
148                             'product_moves_in' : {'relation': 'stock.move.memory.in', 'type' : 'one2many', 'string' : 'Product Moves'},
149                             'product_moves_out' : {'relation': 'stock.move.memory.out', 'type' : 'one2many', 'string' : 'Product Moves'}
150                             })
151         
152         _moves_arch_lst = """
153                 <form string="%(title)s">
154                     <separator colspan="4" string="%(info)s"/>
155                     <field name="date" colspan="2"/>
156                     <separator colspan="4" string="Move Detail"/>
157                     <field name="%(picking_type)s" colspan="4" nolabel="1" mode="tree,form" width="550" height="200" ></field>      
158                     <separator string="" colspan="4" />
159                     <label string="" colspan="2"/>
160                     <group col="2" colspan="2">
161                         <button icon='gtk-cancel' special="cancel" string="_Cancel" />
162                         <button name="do_partial" string="%(button)s"
163                             colspan="1" type="object" icon="gtk-apply" />
164                     </group>
165                 </form> """ % message
166         
167         result['arch'] = _moves_arch_lst
168         result['fields'] = _moves_fields
169         return result
170    
171     def do_partial(self, cr, uid, ids, context=None):
172         """ Makes partial moves and pickings done.
173         @param self: The object pointer.
174         @param cr: A database cursor
175         @param uid: ID of the user currently logged in
176         @param fields: List of fields for which we want default values
177         @param context: A standard dictionary
178         @return: A dictionary which of fields with values.
179         """
180     
181         if context is None:
182             context = {}
183         move_obj = self.pool.get('stock.move')
184         
185         move_ids = context.get('active_ids', False)
186         partial = self.browse(cr, uid, ids[0], context=context)
187         partial_datas = {
188             'delivery_date' : partial.date
189         }
190         
191         p_moves = {}
192         picking_type = self.__get_picking_type(cr, uid, move_ids)
193         
194         moves_list = picking_type == 'product_moves_in' and partial.product_moves_in  or partial.product_moves_out
195         for product_move in moves_list:
196             p_moves[product_move.move_id.id] = product_move
197             
198         moves_ids_final = []
199         for move in move_obj.browse(cr, uid, move_ids, context=context):
200             if move.state in ('done', 'cancel'):
201                 continue
202             if not p_moves.get(move.id):
203                 continue
204             partial_datas['move%s' % (move.id)] = {
205                 'product_id' : p_moves[move.id].id,
206                 'product_qty' : p_moves[move.id].quantity,
207                 'product_uom' :p_moves[move.id].product_uom.id,
208                 'prodlot_id' : p_moves[move.id].prodlot_id.id,
209             }
210             
211             moves_ids_final.append(move.id)
212             if (move.picking_id.type == 'in') and (move.product_id.cost_method == 'average'):
213                 partial_datas['move%s' % (move.id)].update({
214                     'product_price' : p_moves[move.id].cost,
215                     'product_currency': p_moves[move.id].currency.id,
216                 })
217                 
218             
219         move_obj.do_partial(cr, uid, moves_ids_final, partial_datas, context=context)
220         return {'type': 'ir.actions.act_window_close'}
221
222 stock_partial_move()
223 stock_partial_move_memory_out()
224 stock_partial_move_memory_in()
225 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
226