[FIX] partial picking, with push rules
[odoo/odoo.git] / addons / stock / wizard / stock_partial_move.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-TODAY OpenERP SA (<http://openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
24 import time
25
26 class stock_partial_move_line(osv.osv_memory):
27     _inherit = "stock.partial.picking.line"
28     _name = "stock.partial.move.line"
29     _columns = {
30         'wizard_id' : fields.many2one('stock.partial.move', string="Wizard", ondelete='CASCADE'),
31     }
32
33 class stock_partial_move(osv.osv_memory):
34     _name = "stock.partial.move"
35     _inherit = 'stock.partial.picking'
36     _description = "Partial Move Processing Wizard"
37     _columns = {
38         'date': fields.datetime('Date', required=True),
39         'move_ids' : fields.one2many('stock.partial.move.line', 'wizard_id', 'Moves'),
40
41         # picking_id is not used for move processing, so we remove the required attribute
42         # from the inherited column, and ignore it
43         'picking_id': fields.many2one('stock.picking', 'Picking'),
44      }
45
46     def default_get(self, cr, uid, fields, context=None):
47         if context is None: context = {}
48         # no call to super!
49         res = {}
50         move_ids = context.get('active_ids', [])
51         if not move_ids or not context.get('active_model') == 'stock.move':
52             return res
53         if 'move_ids' in fields:
54             move_ids = self.pool.get('stock.move').browse(cr, uid, move_ids, context=context)
55             moves = [self._partial_move_for(cr, uid, m) for m in move_ids if m.state not in ('done','cancel')]
56             res.update(move_ids=moves)
57         if 'date' in fields:
58             res.update(date=time.strftime(DEFAULT_SERVER_DATETIME_FORMAT))
59         return res
60
61     def do_partial(self, cr, uid, ids, context=None):
62         # no call to super!
63         assert len(ids) == 1, 'Partial move processing may only be done one form at a time'
64         partial = self.browse(cr, uid, ids[0], context=context)
65         partial_data = {
66             'delivery_date' : partial.date
67         }
68         moves_ids = []
69         for move in partial.move_ids:
70             move_id = move.move_id.id
71             partial_data['move%s' % (move_id)] = {
72                 'product_id': move.product_id.id,
73                 'product_qty': move.quantity,
74                 'product_uom': move.product_uom.id,
75                 'prodlot_id': move.prodlot_id.id,
76             }
77             moves_ids.append(move_id)
78             if (move.move_id.picking_id.type == 'in') and (move.product_id.cost_method == 'average'):
79                 partial_data['move%s' % (move_id)].update(product_price=move.cost,
80                                                           product_currency=move.currency.id)
81         self.pool.get('stock.move').do_partial(cr, uid, moves_ids, partial_data, context=context)
82         return {'type': 'ir.actions.act_window_close'}
83
84 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: