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 __is_in(self,cr, uid, pick_ids):
38 @return: True if one of the moves has as picking type 'in'
43 pick_obj = self.pool.get('stock.picking')
44 pick_ids = pick_obj.search(cr, uid, [('id','in',pick_ids)])
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':
53 def __get_picking_type(self, cr, uid, pick_ids):
54 if self.__is_in(cr, uid, pick_ids):
55 return "product_moves_in"
57 return "product_moves_out"
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)
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)
67 picking_ids = context.get('active_ids', False)
68 picking_type = self.__get_picking_type(cr, uid, picking_ids)
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'}
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"
87 <button name="do_partial" string="_Validate"
88 colspan="1" type="object" icon="gtk-go-forward" />
91 result['arch'] = _moves_arch_lst
92 result['fields'] = _moves_fields
95 def __create_partial_picking_memory(self, picking, is_in):
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,
106 'cost' : picking.product_id.standard_price,
107 '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,
111 def __get_active_stock_pickings(self, cr, uid, context=None):
112 pick_obj = self.pool.get('stock.picking')
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'):
122 res.append(self.__create_partial_picking_memory(m, need_product_cost))
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'),
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.
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)
146 'delivery_date' : partial.date
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
153 for product_move in moves_list:
154 p_moves[product_move.move_id.id] = product_move
157 for move in pick.move_lines:
158 if move.state in ('done', 'cancel'):
160 if not p_moves.get(move.id):
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,
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,
176 pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
177 return {'type': 'ir.actions.act_window_close'}
182 stock_partial_picking()
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: