[FIX] OPW 17161 : don't use the same relational field for two different osv_memory...
[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 import decimal_precision as dp
27
28 class stock_partial_picking(osv.osv_memory):
29     _name = "stock.partial.picking"
30     _description = "Partial Picking"
31     _columns = {
32         'date': fields.datetime('Date', required=True),
33         'product_moves_out' : fields.one2many('stock.move.memory.out', 'wizard_pick_id', 'Moves'),
34         'product_moves_in' : fields.one2many('stock.move.memory.in', 'wizard_pick_id', 'Moves'),
35      }
36
37     def get_picking_type(self, cr, uid, picking, context=None):
38         picking_type = picking.type
39         for move in picking.move_lines:
40             if picking.type == 'in' and move.product_id.cost_method == 'average':
41                 picking_type = 'in'
42                 break
43             else:
44                 picking_type = 'out'
45         return picking_type
46
47     def default_get(self, cr, uid, fields, context=None):
48         """ To get default values for the object.
49          @param self: The object pointer.
50          @param cr: A database cursor
51          @param uid: ID of the user currently logged in
52          @param fields: List of fields for which we want default values
53          @param context: A standard dictionary
54          @return: A dictionary which of fields with values.
55         """
56         if context is None:
57             context = {}
58
59         pick_obj = self.pool.get('stock.picking')
60         res = super(stock_partial_picking, self).default_get(cr, uid, fields, context=context)
61         picking_ids = context.get('active_ids', [])
62         if not picking_ids:
63             return res
64
65         result = []
66         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
67             pick_type = self.get_picking_type(cr, uid, pick, context=context)
68             for m in pick.move_lines:
69                 if m.state in ('done', 'cancel'):
70                     continue
71                 result.append(self.__create_partial_picking_memory(m, pick_type))
72
73         if 'product_moves_in' in fields:
74             res.update({'product_moves_in': result})
75         if 'product_moves_out' in fields:
76             res.update({'product_moves_out': result})
77         if 'date' in fields:
78             res.update({'date': time.strftime('%Y-%m-%d %H:%M:%S')})
79         return res
80
81     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
82         result = super(stock_partial_picking, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
83
84         pick_obj = self.pool.get('stock.picking')
85         picking_ids = context.get('active_ids', False)
86
87         if not picking_ids:
88             # not called through an action (e.g. buildbot), return the default.
89             return result
90
91         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
92             picking_type = self.get_picking_type(cr, uid, pick, context=context)
93
94         _moves_arch_lst = """<form string="%s">
95                         <field name="date" invisible="1"/>
96                         <separator colspan="4" string="%s"/>
97                         <field name="%s" colspan="4" nolabel="1" mode="tree,form" width="550" height="200" ></field>
98                         """ % (_('Process Document'), _('Products'), "product_moves_" + picking_type)
99         _moves_fields = result['fields']
100
101         # add field related to picking type only
102         _moves_fields.update({
103                             'product_moves_' + picking_type: {'relation': 'stock.move.memory.'+picking_type, 'type' : 'one2many', 'string' : 'Product Moves'},
104                             })
105
106         _moves_arch_lst += """
107                 <separator string="" colspan="4" />
108                 <label string="" colspan="2"/>
109                 <group col="2" colspan="2">
110                 <button icon='gtk-cancel' special="cancel"
111                     string="_Cancel" />
112                 <button name="do_partial" string="_Validate"
113                     colspan="1" type="object" icon="gtk-go-forward" />
114             </group>
115         </form>"""
116         result['arch'] = _moves_arch_lst
117         result['fields'] = _moves_fields
118         return result
119
120     def __create_partial_picking_memory(self, move, pick_type):
121         move_memory = {
122             'product_id' : move.product_id.id,
123             'quantity' : move.product_qty,
124             'product_uom' : move.product_uom.id,
125             'prodlot_id' : move.prodlot_id.id,
126             'move_id' : move.id,
127         }
128
129         if pick_type == 'in':
130             move_memory.update({
131                 'cost' : move.product_id.standard_price,
132                 'currency' : move.product_id.company_id and move.product_id.company_id.currency_id.id or False,
133             })
134         return move_memory
135
136     def do_partial(self, cr, uid, ids, context=None):
137         """ Makes partial moves and pickings done.
138         @param self: The object pointer.
139         @param cr: A database cursor
140         @param uid: ID of the user currently logged in
141         @param fields: List of fields for which we want default values
142         @param context: A standard dictionary
143         @return: A dictionary which of fields with values.
144         """
145         pick_obj = self.pool.get('stock.picking')
146         uom_obj = self.pool.get('product.uom')
147
148         picking_ids = context.get('active_ids', False)
149         partial = self.browse(cr, uid, ids[0], context=context)
150         partial_datas = {
151             'delivery_date' : partial.date
152         }
153
154         for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
155             picking_type = self.get_picking_type(cr, uid, pick, context=context)
156             moves_list = picking_type == 'in' and partial.product_moves_in or partial.product_moves_out
157
158             for move in moves_list:
159
160                 #Adding a check whether any line has been added with new qty
161                 if not move.move_id:
162                     raise osv.except_osv(_('Processing Error'),\
163                     _('You cannot add any new move while validating the picking, rather you can split the lines prior to validation!'))
164
165                 calc_qty = uom_obj._compute_qty(cr, uid, move.product_uom.id, \
166                                     move.quantity, move.move_id.product_uom.id)
167
168                 #Adding a check whether any move line contains exceeding qty to original moveline
169                 if calc_qty > move.move_id.product_qty:
170                     precision = '%0.' + str(dp.get_precision('Product UoM')(cr)[1] or 0) + 'f'
171                     raise osv.except_osv(_('Processing Error'),
172                     _('Processing quantity %s %s for %s is larger than the available quantity %s %s !')\
173                     % (precision % move.quantity, move.product_uom.name, move.product_id.name,\
174                        precision % move.move_id.product_qty, move.move_id.product_uom.name))
175
176                 #Adding a check whether any move line contains qty less than zero
177                 if calc_qty < 0:
178                     precision = '%0.' + str(dp.get_precision('Product UoM')(cr)[1] or 0) + 'f'
179                     raise osv.except_osv(_('Processing Error'), \
180                             _('Can not process quantity %s for Product %s !') \
181                             % (precision % move.quantity, move.product_id.name))
182
183                 partial_datas['move%s' % (move.move_id.id)] = {
184                     'product_id': move.product_id.id,
185                     'product_qty': calc_qty,
186                     'product_uom': move.move_id.product_uom.id,
187                     'prodlot_id': move.prodlot_id.id,
188                 }
189                 if (picking_type == 'in') and (move.product_id.cost_method == 'average'):
190                     partial_datas['move%s' % (move.move_id.id)].update({
191                                                     'product_price' : move.cost,
192                                                     'product_currency': move.currency.id,
193                                                     })
194         pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
195         return {'type': 'ir.actions.act_window_close'}
196
197 stock_partial_picking()
198
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: