f666bf1e36738e11810356c64a75f83de401cbe6
[odoo/odoo.git] / addons / stock / wizard / stock_partial_picking.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 import time
23 from osv import fields, osv
24 from tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
25 import decimal_precision as dp
26
27 class stock_partial_picking_line(osv.TransientModel):
28     _name = "stock.partial.picking.line"
29     _rec_name = 'product_id'
30     _columns = {
31         'product_id' : fields.many2one('product.product', string="Product", required=True, ondelete='CASCADE'),
32         'quantity' : fields.float("Quantity", digits_compute=dp.get_precision('Product UoM'), required=True),
33         'product_uom': fields.many2one('product.uom', 'Unit of Measure', required=True, ondelete='CASCADE'),
34         'prodlot_id' : fields.many2one('stock.production.lot', 'Production Lot', ondelete='CASCADE'),
35         'location_id': fields.many2one('stock.location', 'Location', required=True, ondelete='CASCADE'),
36         'location_dest_id': fields.many2one('stock.location', 'Dest. Location', required=True, ondelete='CASCADE'),
37         'move_id' : fields.many2one('stock.move', "Move", ondelete='CASCADE'),
38         'wizard_id' : fields.many2one('stock.partial.picking', string="Wizard", ondelete='CASCADE'),
39         'update_cost': fields.boolean('Need cost update'),
40         'cost' : fields.float("Cost", help="Unit Cost for this product line"),
41         'currency' : fields.many2one('res.currency', string="Currency", help="Currency in which Unit cost is expressed", ondelete='CASCADE'),
42     }
43
44 class stock_partial_picking(osv.osv_memory):
45     _name = "stock.partial.picking"
46     _description = "Partial Picking Processing Wizard"
47     _columns = {
48         'date': fields.datetime('Date', required=True),
49         'move_ids' : fields.one2many('stock.partial.picking.line', 'wizard_id', 'Product Moves'),
50         'picking_id': fields.many2one('stock.picking', 'Picking', required=True, ondelete='CASCADE'),
51      }
52
53     def default_get(self, cr, uid, fields, context=None):
54         if context is None: context = {}
55         res = super(stock_partial_picking, self).default_get(cr, uid, fields, context=context)
56         picking_ids = context.get('active_ids', [])
57         if not picking_ids or (not context.get('active_model') == 'stock.picking') \
58             or len(picking_ids) != 1:
59             # Partial Picking Processing may only be done for one picking at a time
60             return res
61         picking_id, = picking_ids
62         if 'picking_id' in fields:
63             res.update(picking_id=picking_id)
64         if 'move_ids' in fields:
65             picking = self.pool.get('stock.picking').browse(cr, uid, picking_id, context=context)
66             moves = [self._partial_move_for(cr, uid, m) for m in picking.move_lines if m.state not in ('done','cancel')]
67             res.update(move_ids=moves)
68         if 'date' in fields:
69             res.update(date=time.strftime(DEFAULT_SERVER_DATETIME_FORMAT))
70         return res
71
72     def _product_cost_for_average_update(self, cr, uid, move):
73         """Returns product cost and currency ID for the given move, suited for re-computing
74            the average product cost.
75         
76            :return: map of the form::
77
78                 {'cost': 123.34,
79                  'currency': 42}
80         """
81         # Currently, the cost on the product form is supposed to be expressed in the currency
82         # of the company owning the product. If not set, we fall back to the picking's company,
83         # which should work in simple cases.
84         return {'cost': move.product_id.standard_price,
85                 'currency': move.product_id.company_id.currency_id.id \
86                                 or move.picking_id.company_id.currency_id.id \
87                                 or False}
88
89     def _partial_move_for(self, cr, uid, move):
90         partial_move = {
91             'product_id' : move.product_id.id,
92             'quantity' : move.state in ('assigned','new') and move.product_qty or 0,
93             'product_uom' : move.product_uom.id,
94             'prodlot_id' : move.prodlot_id.id,
95             'move_id' : move.id,
96             'location_id' : move.location_id.id,
97             'location_dest_id' : move.location_dest_id.id,
98         }
99         if move.picking_id.type == 'in' and move.product_id.cost_method == 'average':
100             partial_move.update(update_cost=True, **self._product_cost_for_average_update(cr, uid, move))
101         return partial_move
102
103     def do_partial(self, cr, uid, ids, context=None):
104         assert len(ids) == 1, 'Partial picking processing may only be done one at a time'
105         stock_picking = self.pool.get('stock.picking')
106         stock_move = self.pool.get('stock.move')
107         partial = self.browse(cr, uid, ids[0], context=context)
108         partial_data = {
109             'delivery_date' : partial.date
110         }
111         picking_type = partial.picking_id.type
112         for move in partial.move_ids:
113             move_id = move.move_id.id
114             if not move_id:
115                 seq_obj_name =  'stock.picking.' + picking_type
116                 move_id = stock_move.create(cr,uid,{'name' : self.pool.get('ir.sequence').get(cr, uid, seq_obj_name),
117                                                     'product_id': move.product_id.id,
118                                                     'product_qty': move.quantity,
119                                                     'product_uom': move.product_uom.id,
120                                                     'prodlot_id': move.prodlot_id.id,
121                                                     'location_id' : move.location_id.id,
122                                                     'location_dest_id' : move.location_dest_id.id,
123                                                     'picking_id': partial.picking_id.id
124                                                     },context=context)
125                 stock_move.action_confirm(cr, uid, [move_id], context)
126             partial_data['move%s' % (move_id)] = {
127                 'product_id': move.product_id.id,
128                 'product_qty': move.quantity,
129                 'product_uom': move.product_uom.id,
130                 'prodlot_id': move.prodlot_id.id,
131             }
132             if (picking_type == 'in') and (move.product_id.cost_method == 'average'):
133                 partial_data['move%s' % (move.move_id.id)].update(product_price=move.cost,
134                                                                   product_currency=move.currency.id)
135         stock_picking.do_partial(cr, uid, [partial.picking_id.id], partial_data, context=context)
136         return {'type': 'ir.actions.act_window_close'}
137
138 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: