[MERGE]: Merged with lp:~openerp-dev/openobject-addons/ron-dev-addons2
[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 get_picking_type(self, cr, uid, picking, context=None):
37         picking_type = picking.type
38         for move in picking.move_lines:
39             if picking.type == 'in' and move.product_id.cost_method == 'average':
40                 picking_type = 'in'
41                 break
42             else:
43                 picking_type = 'out'
44         return picking_type
45     
46     def default_get(self, cr, uid, fields, context=None):
47         """ To get default values for the object.
48          @param self: The object pointer.
49          @param cr: A database cursor
50          @param uid: ID of the user currently logged in
51          @param fields: List of fields for which we want default values
52          @param context: A standard dictionary
53          @return: A dictionary which of fields with values.
54         """
55         if context is None:
56             context = {}
57             
58         pick_obj = self.pool.get('stock.picking')
59         res = super(stock_partial_picking, self).default_get(cr, uid, fields, context=context)
60         picking_ids = context.get('active_ids', [])
61         if not picking_ids:
62             return res
63
64         result = []
65         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
66             pick_type = self.get_picking_type(cr, uid, pick, context=context)
67             for m in pick.move_lines:
68                 if m.state in ('done', 'cancel'):
69                     continue
70                 result.append(self.__create_partial_picking_memory(m, pick_type))
71
72         if 'product_moves_in' in fields:
73             res.update({'product_moves_in': result})
74         if 'product_moves_out' in fields:
75             res.update({'product_moves_out': result})
76         if 'date' in fields:
77             res.update({'date': time.strftime('%Y-%m-%d %H:%M:%S')})
78         return res
79     
80     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
81         result = super(stock_partial_picking, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
82        
83         pick_obj = self.pool.get('stock.picking')
84         picking_ids = context.get('active_ids', False)
85
86         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
87             picking_type = self.get_picking_type(cr, uid, pick, context=context)
88         
89         _moves_arch_lst = """<form string="%s">
90                         <field name="date" invisible="1"/>
91                         <separator colspan="4" string="%s"/>
92                         <field name="%s" colspan="4" nolabel="1" mode="tree,form" width="550" height="200" ></field>
93                         """ % (_('Process Document'), _('Products'), "product_moves_" + picking_type)
94         _moves_fields = result['fields']
95
96         # add field related to picking type only
97         _moves_fields.update({
98                             'product_moves_' + picking_type: {'relation': 'stock.move.memory.'+picking_type, 'type' : 'one2many', 'string' : 'Product Moves'}, 
99                             })
100
101         _moves_arch_lst += """
102                 <separator string="" colspan="4" />
103                 <label string="" colspan="2"/>
104                 <group col="2" colspan="2">
105                 <button icon='gtk-cancel' special="cancel"
106                     string="_Cancel" />
107                 <button name="do_partial" string="_Validate"
108                     colspan="1" type="object" icon="gtk-go-forward" />
109             </group>
110         </form>"""
111         result['arch'] = _moves_arch_lst
112         result['fields'] = _moves_fields
113         return result
114
115     def __create_partial_picking_memory(self, picking, pick_type):
116         move_memory = {
117             'product_id' : picking.product_id.id, 
118             'quantity' : picking.product_qty, 
119             'product_uom' : picking.product_uom.id, 
120             'prodlot_id' : picking.prodlot_id.id, 
121             'move_id' : picking.id, 
122         }
123     
124         if pick_type == 'in':
125             move_memory.update({
126                 'cost' : picking.product_id.standard_price, 
127                 'currency' : picking.product_id.company_id.currency_id.id, 
128             })
129         return move_memory
130
131     def do_partial(self, cr, uid, ids, context=None):
132         """ Makes partial moves and pickings done.
133         @param self: The object pointer.
134         @param cr: A database cursor
135         @param uid: ID of the user currently logged in
136         @param fields: List of fields for which we want default values
137         @param context: A standard dictionary
138         @return: A dictionary which of fields with values.
139         """
140         pick_obj = self.pool.get('stock.picking')
141         move_obj = self.pool.get('stock.move')
142         location_obj = self.pool.get('stock.location')
143         
144         picking_ids = context.get('active_ids', False)
145         partial = self.browse(cr, uid, ids[0], context=context)
146         partial_datas = {
147             'delivery_date' : partial.date
148         }
149
150         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
151             picking_type = self.get_picking_type(cr, uid, pick, context=context)
152             moves_list = picking_type == 'in' and partial.product_moves_in or partial.product_moves_out
153
154             for move in moves_list:
155                 partial_datas['move%s' % (move.move_id.id)] = {
156                     'product_id': move.id, 
157                     'product_qty': move.quantity, 
158                     'product_uom': move.product_uom.id, 
159                     'prodlot_id': move.prodlot_id.id, 
160                 }
161                 if (picking_type == 'in') and (move.product_id.cost_method == 'average'):
162                     partial_datas['move%s' % (move.move_id.id)].update({
163                                                     'product_price' : move.cost, 
164                                                     'product_currency': move.currency.id, 
165                                                     })
166         pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
167         return {}
168
169 stock_partial_picking()
170
171 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: