merged with trunk
[odoo/odoo.git] / addons / stock / wizard / stock_partial_picking.py
1
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24 from tools.translate import _
25 import time
26
27 class stock_partial_picking(osv.osv_memory):
28     _name = "stock.partial.picking"
29     _description = "Partial Picking"
30     _columns = {
31         'date': fields.datetime('Date', required=True),
32         'product_moves_out' : fields.one2many('stock.move.memory.out', 'wizard_id', 'Moves'),
33         'product_moves_in' : fields.one2many('stock.move.memory.in', 'wizard_id', 'Moves'),
34      }
35     
36     def __is_in(self,cr, uid, pick_ids):
37         """
38             @return: True if one of the moves has as picking type 'in'
39         """
40         if not pick_ids:
41             return False
42        
43         pick_obj = self.pool.get('stock.picking')
44         pick_ids = pick_obj.search(cr, uid, [('id','in',pick_ids)])
45         
46        
47         for pick in pick_obj.browse(cr, uid, pick_ids):
48             for move in pick.move_lines:
49                 if pick.type == 'in' and move.product_id.cost_method == 'average':
50                     return True
51         return False
52     
53     def __get_picking_type(self, cr, uid, pick_ids):
54         if self.__is_in(cr, uid, pick_ids):
55             return "product_moves_in"
56         else:
57             return "product_moves_out"
58
59     def view_init(self, cr, uid, fields_list, context=None):
60         res = super(stock_partial_picking, self).view_init(cr, uid, fields_list, context=context)
61         return res
62
63     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False,submenu=False):
64         result = super(stock_partial_picking, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar,submenu)
65        
66         
67         picking_ids = context.get('active_ids', False)
68         picking_type = self.__get_picking_type(cr, uid, picking_ids)
69         
70         _moves_arch_lst = """<form string="%s">
71                         <field name="date" invisible="1"/>
72                         <separator colspan="4" string="%s"/>
73                         <field name="%s" colspan="4" nolabel="1" mode="tree,form" width="550" height="200" ></field>
74                         """ % (_('Process Document'), _('Products'), picking_type)
75         _moves_fields = result['fields']
76         _moves_fields.update({
77                             'product_moves_in' : {'relation': 'stock.move.memory.in', 'type' : 'one2many', 'string' : 'Product Moves'},
78                             'product_moves_out' : {'relation': 'stock.move.memory.out', 'type' : 'one2many', 'string' : 'Product Moves'}
79                             })
80             
81         _moves_arch_lst += """
82                 <separator string="" colspan="4" />
83                 <label string="" colspan="2"/>
84                 <group col="2" colspan="2">
85                 <button icon='gtk-cancel' special="cancel"
86                     string="_Cancel" />
87                 <button name="do_partial" string="_Validate"
88                     colspan="1" type="object" icon="gtk-go-forward" />
89             </group>
90         </form>"""
91         result['arch'] = _moves_arch_lst
92         result['fields'] = _moves_fields
93         return result
94
95     def __create_partial_picking_memory(self, picking, is_in):
96         move_memory = {
97             'product_id' : picking.product_id.id,
98             'quantity' : picking.product_qty,
99             'product_uom' : picking.product_uom.id,
100             'prodlot_id' : picking.prodlot_id.id,
101             'move_id' : picking.id,
102         }
103     
104         if is_in:
105             move_memory.update({
106                 'cost' : picking.product_id.standard_price,
107                 'currency' : picking.product_id.company_id.currency_id.id,
108             })
109         return move_memory
110
111     def __get_active_stock_pickings(self, cr, uid, context=None):
112         pick_obj = self.pool.get('stock.picking')
113         if not context:
114             context = {}
115                
116         res = []
117         for pick in pick_obj.browse(cr, uid, context.get('active_ids', []), context):
118             need_product_cost = (pick.type == 'in')
119             for m in pick.move_lines:
120                 if m.state in ('done', 'cancel'):
121                     continue           
122                 res.append(self.__create_partial_picking_memory(m, need_product_cost))
123             
124         return res
125     
126     _defaults = {
127         'product_moves_in' : __get_active_stock_pickings,
128         'product_moves_out' : __get_active_stock_pickings,
129         'date' : lambda *a : time.strftime('%Y-%m-%d %H:%M:%S'),
130     }
131     
132
133     def do_partial(self, cr, uid, ids, context=None):
134         """ Makes partial moves and pickings done.
135         @param self: The object pointer.
136         @param cr: A database cursor
137         @param uid: ID of the user currently logged in
138         @param fields: List of fields for which we want default values
139         @param context: A standard dictionary
140         @return: A dictionary which of fields with values.
141         """
142         pick_obj = self.pool.get('stock.picking')
143         picking_ids = context.get('active_ids', False)
144         partial = self.browse(cr, uid, ids[0], context=context)
145         partial_datas = {
146             'delivery_date' : partial.date
147         }
148
149         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
150             need_product_cost = (pick.type == 'in')
151             moves_list = need_product_cost and partial.product_moves_in  or partial.product_moves_out
152             p_moves = {}
153             for product_move in moves_list:
154                 p_moves[product_move.move_id.id] = product_move
155             
156             
157             for move in pick.move_lines:
158                 if move.state in ('done', 'cancel'):
159                     continue
160                 if not p_moves.get(move.id):
161                     continue
162                 
163                 partial_datas['move%s' % (move.id)] = {
164                     'product_id' : p_moves[move.id].id,
165                     'product_qty' : p_moves[move.id].quantity,
166                     'product_uom' :p_moves[move.id].product_uom.id,
167                     'prodlot_id' : p_moves[move.id].prodlot_id.id,
168                 }
169             
170                
171                 if (move.picking_id.type == 'in') and (move.product_id.cost_method == 'average'):
172                     partial_datas['move%s' % (move.id)].update({
173                                                     'product_price' : p_moves[move.id].cost,
174                                                     'product_currency': p_moves[move.id].currency.id,
175                                                     })
176         pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
177         return {}
178
179         
180    
181
182 stock_partial_picking()
183
184
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
186