1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-TODAY OpenERP SA (<http://openerp.com>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import fields, osv
24 from tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
25 import decimal_precision as dp
27 class stock_partial_picking_line(osv.TransientModel):
28 _name = "stock.partial.picking.line"
29 _rec_name = 'product_id'
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'),
44 class stock_partial_picking(osv.osv_memory):
45 _name = "stock.partial.picking"
46 _description = "Partial Picking Processing Wizard"
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'),
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
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)
69 res.update(date=time.strftime(DEFAULT_SERVER_DATETIME_FORMAT))
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.
76 :return: map of the form::
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 \
89 def _partial_move_for(self, cr, uid, 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,
96 'location_id' : move.location_id.id,
97 'location_dest_id' : move.location_dest_id.id,
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))
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)
109 'delivery_date' : partial.date
111 picking_type = partial.picking_id.type
112 for move in partial.move_ids:
113 move_id = move.move_id.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
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,
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'}
138 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: