1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 ##############################################################################
22 from osv import fields
27 class StockMove(osv.osv):
28 _inherit = 'stock.move'
31 'production_id': fields.many2one('mrp.production', 'Production', select=True),
34 def create_chained_picking(self, cr, uid, moves, context=None):
35 new_moves = super(StockMove, self).create_chained_picking(cr, uid, moves, context=context)
36 self.write(cr, uid, [x.id for x in new_moves], {'production_id': False}, context=context)
39 def _action_explode(self, cr, uid, move, context=None):
40 """ Explodes pickings.
41 @param move: Stock moves
44 bom_obj = self.pool.get('mrp.bom')
45 move_obj = self.pool.get('stock.move')
46 procurement_obj = self.pool.get('procurement.order')
47 product_obj = self.pool.get('product.product')
48 wf_service = netsvc.LocalService("workflow")
49 processed_ids = [move.id]
50 if move.product_id.supply_method == 'produce' and move.product_id.procure_method == 'make_to_order':
51 bis = bom_obj.search(cr, uid, [
52 ('product_id','=',move.product_id.id),
54 ('type','=','phantom')])
56 factor = move.product_qty
57 bom_point = bom_obj.browse(cr, uid, bis[0], context=context)
58 res = bom_obj._bom_explode(cr, uid, bom_point, factor, [])
60 if move.state == 'assigned':
64 'picking_id': move.picking_id.id,
65 'product_id': line['product_id'],
66 'product_uom': line['product_uom'],
67 'product_qty': line['product_qty'],
68 'product_uos': line['product_uos'],
69 'product_uos_qty': line['product_uos_qty'],
70 'move_dest_id': move.id,
73 'move_history_ids': [(6,0,[move.id])],
74 'move_history_ids2': [(6,0,[])],
77 mid = move_obj.copy(cr, uid, move.id, default=valdef)
78 processed_ids.append(mid)
79 prodobj = product_obj.browse(cr, uid, line['product_id'], context=context)
80 proc_id = procurement_obj.create(cr, uid, {
81 'name': (move.picking_id.origin or ''),
82 'origin': (move.picking_id.origin or ''),
83 'date_planned': move.date,
84 'product_id': line['product_id'],
85 'product_qty': line['product_qty'],
86 'product_uom': line['product_uom'],
87 'product_uos_qty': line['product_uos'] and line['product_uos_qty'] or False,
88 'product_uos': line['product_uos'],
89 'location_id': move.location_id.id,
90 'procure_method': prodobj.procure_method,
93 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
94 move_obj.write(cr, uid, [move.id], {
95 'location_dest_id': move.location_id.id, # dummy move for the kit
96 'auto_validate': True,
100 for m in procurement_obj.search(cr, uid, [('move_id','=',move.id)], context):
101 wf_service.trg_validate(uid, 'procurement.order', m, 'button_confirm', cr)
102 wf_service.trg_validate(uid, 'procurement.order', m, 'button_wait_done', cr)
105 def action_consume(self, cr, uid, ids, product_qty, location_id=False, context=None):
106 """ Consumed product with specific quatity from specific source location.
107 @param product_qty: Consumed product quantity
108 @param location_id: Source location
109 @return: Consumed lines
112 production_obj = self.pool.get('mrp.production')
113 wf_service = netsvc.LocalService("workflow")
114 for move in self.browse(cr, uid, ids):
115 move.action_confirm(context)
116 new_moves = super(StockMove, self).action_consume(cr, uid, [move.id], product_qty, location_id, context=context)
117 production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
118 for prod in production_obj.browse(cr, uid, production_ids, context=context):
119 if prod.state == 'confirmed':
120 production_obj.force_production(cr, uid, [prod.id])
121 wf_service.trg_validate(uid, 'mrp.production', prod.id, 'button_produce', cr)
122 for new_move in new_moves:
123 if new_move == move.id:
124 #This move is already there in move lines of production order
126 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
130 def action_scrap(self, cr, uid, ids, product_qty, location_id, context=None):
131 """ Move the scrap/damaged product into scrap location
132 @param product_qty: Scraped product quantity
133 @param location_id: Scrap location
134 @return: Scraped lines
137 production_obj = self.pool.get('mrp.production')
138 wf_service = netsvc.LocalService("workflow")
139 for move in self.browse(cr, uid, ids, context=context):
140 new_moves = super(StockMove, self).action_scrap(cr, uid, [move.id], product_qty, location_id, context=context)
141 #If we are not scrapping our whole move, tracking and lot references must not be removed
142 #self.write(cr, uid, [move.id], {'prodlot_id': False, 'tracking_id': False})
143 production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
144 for prod_id in production_ids:
145 wf_service.trg_validate(uid, 'mrp.production', prod_id, 'button_produce', cr)
146 for new_move in new_moves:
147 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
154 class StockPicking(osv.osv):
155 _inherit = 'stock.picking'
158 # Explode picking by replacing phantom BoMs
160 def action_explode(self, cr, uid, move_ids, *args):
161 """Explodes moves by expanding kit components"""
162 move_obj = self.pool.get('stock.move')
164 for move in move_obj.browse(cr, uid, move_ids):
165 todo.extend(move_obj._action_explode(cr, uid, move))
166 return list(set(todo))
171 class split_in_production_lot(osv.osv_memory):
172 _inherit = "stock.move.split"
174 def split(self, cr, uid, ids, move_ids, context=None):
175 """ Splits move lines into given quantities.
176 @param move_ids: Stock moves.
177 @return: List of new moves.
179 new_moves = super(split_in_production_lot, self).split(cr, uid, ids, move_ids, context=context)
180 production_obj = self.pool.get('mrp.production')
181 production_ids = production_obj.search(cr, uid, [('move_lines', 'in', move_ids)])
182 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, m) for m in new_moves]})
185 split_in_production_lot()
187 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: