2 # -*- coding: utf-8 -*-
3 ##############################################################################
5 # OpenERP, Open Source Management Solution
6 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
21 ##############################################################################
23 from osv import fields, osv
24 from tools.translate import _
27 class stock_partial_picking(osv.osv_memory):
28 _name = "stock.partial.picking"
29 _description = "Partial Picking"
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'),
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':
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.
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', [])
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'):
70 result.append(self.__create_partial_picking_memory(m, pick_type))
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})
77 res.update({'date': time.strftime('%Y-%m-%d %H:%M:%S')})
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)
83 pick_obj = self.pool.get('stock.picking')
84 picking_ids = context.get('active_ids', False)
87 # not called through an action (e.g. buildbot), return the default.
90 for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
91 picking_type = self.get_picking_type(cr, uid, pick, context=context)
93 _moves_arch_lst = """<form string="%s">
94 <field name="date" invisible="1"/>
95 <separator colspan="4" string="%s"/>
96 <field name="%s" colspan="4" nolabel="1" mode="tree,form" width="550" height="200" ></field>
97 """ % (_('Process Document'), _('Products'), "product_moves_" + picking_type)
98 _moves_fields = result['fields']
100 # add field related to picking type only
101 _moves_fields.update({
102 'product_moves_' + picking_type: {'relation': 'stock.move.memory.'+picking_type, 'type' : 'one2many', 'string' : 'Product Moves'},
105 _moves_arch_lst += """
106 <separator string="" colspan="4" />
107 <label string="" colspan="2"/>
108 <group col="2" colspan="2">
109 <button icon='gtk-cancel' special="cancel"
111 <button name="do_partial" string="_Validate"
112 colspan="1" type="object" icon="gtk-go-forward" />
115 result['arch'] = _moves_arch_lst
116 result['fields'] = _moves_fields
119 def __create_partial_picking_memory(self, picking, pick_type):
121 'product_id' : picking.product_id.id,
122 'quantity' : picking.product_qty,
123 'product_uom' : picking.product_uom.id,
124 'prodlot_id' : picking.prodlot_id.id,
125 'move_id' : picking.id,
126 'location_id' : picking.location_id.id,
127 'location_dest_id' : picking.location_dest_id.id,
130 if pick_type == 'in':
132 'cost' : picking.product_id.standard_price,
133 'currency' : picking.product_id.company_id and picking.product_id.company_id.currency_id and picking.product_id.company_id.currency_id.id or False,
137 def do_partial(self, cr, uid, ids, context=None):
138 """ Makes partial moves and pickings done.
139 @param self: The object pointer.
140 @param cr: A database cursor
141 @param uid: ID of the user currently logged in
142 @param fields: List of fields for which we want default values
143 @param context: A standard dictionary
144 @return: A dictionary which of fields with values.
146 pick_obj = self.pool.get('stock.picking')
147 stock_move_obj = self.pool.get('stock.move')
148 picking_ids = context.get('active_ids', False)
149 partial = self.browse(cr, uid, ids[0], context=context)
151 'delivery_date' : partial.date
153 for pick in pick_obj.browse(cr, uid, picking_ids, context=context):
154 picking_type = self.get_picking_type(cr, uid, pick, context=context)
155 moves_list = picking_type == 'in' and partial.product_moves_in or partial.product_moves_out
156 for move in moves_list:
157 if not move.move_id.id:
158 seq_obj_name = 'stock.picking.' + picking_type
159 move_id = stock_move_obj.create(cr,uid,{'name' : self.pool.get('ir.sequence').get(cr, uid, seq_obj_name),
160 'product_id': move.product_id.id,
161 'product_qty': move.quantity,
162 'product_uom': move.product_uom.id,
163 'prodlot_id': move.prodlot_id.id,
164 'location_id' : move.location_id.id,
165 'location_dest_id' : move.location_dest_id.id,
166 'picking_id': pick.id
168 stock_move_obj.action_done(cr, uid, [move_id], context)
170 move_id = move.move_id.id
172 partial_datas['move%s' % (move_id)] = {
173 'product_id': move.product_id.id,
174 'product_qty': move.quantity,
175 'product_uom': move.product_uom.id,
176 'prodlot_id': move.prodlot_id.id,
178 if (picking_type == 'in') and (move.product_id.cost_method == 'average'):
179 partial_datas['move%s' % (move.move_id.id)].update({
180 'product_price' : move.cost,
181 'product_currency': move.currency.id,
183 pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
184 return {'type': 'ir.actions.act_window_close'}
186 stock_partial_picking()
188 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: