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 if move.product_id.supply_method == 'produce' and move.product_id.procure_method == 'make_to_order':
50 bis = bom_obj.search(cr, uid, [
51 ('product_id','=',move.product_id.id),
53 ('type','=','phantom')])
55 factor = move.product_qty
56 bom_point = bom_obj.browse(cr, uid, bis[0], context=context)
57 res = bom_obj._bom_explode(cr, uid, bom_point, factor, [])
58 dest = move.product_id.product_tmpl_id.property_stock_production.id
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 'location_dest_id': dest,
74 'move_history_ids': [(6,0,[move.id])],
75 'move_history_ids2': [(6,0,[])],
78 mid = move_obj.copy(cr, uid, move.id, default=valdef)
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_id': move.location_dest_id.id,
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_wait_done', cr)
104 def action_consume(self, cr, uid, ids, product_qty, location_id=False, context=None):
105 """ Consumed product with specific quatity from specific source location.
106 @param product_qty: Consumed product quantity
107 @param location_id: Source location
108 @return: Consumed lines
111 production_obj = self.pool.get('mrp.production')
112 wf_service = netsvc.LocalService("workflow")
113 for move in self.browse(cr, uid, ids):
114 new_moves = super(StockMove, self).action_consume(cr, uid, [move.id], product_qty, location_id, context=context)
115 production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
116 for prod in production_obj.browse(cr, uid, production_ids, context=context):
117 if prod.state == 'confirmed':
118 production_obj.force_production(cr, uid, [prod.id])
119 wf_service.trg_validate(uid, 'mrp.production', prod.id, 'button_produce', cr)
120 for new_move in new_moves:
121 if new_move == move.id:
122 #This move is already there in move lines of production order
124 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
128 def action_scrap(self, cr, uid, ids, product_qty, location_id, context=None):
129 """ Move the scrap/damaged product into scrap location
130 @param product_qty: Scraped product quantity
131 @param location_id: Scrap location
132 @return: Scraped lines
135 production_obj = self.pool.get('mrp.production')
136 wf_service = netsvc.LocalService("workflow")
137 for move in self.browse(cr, uid, ids, context=context):
138 new_moves = super(StockMove, self).action_scrap(cr, uid, [move.id], product_qty, location_id, context=context)
139 self.write(cr, uid, [move.id], {'prodlot_id': False, 'tracking_id': False})
140 production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
141 for prod_id in production_ids:
142 wf_service.trg_validate(uid, 'mrp.production', prod_id, 'button_produce', cr)
143 for new_move in new_moves:
144 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
151 class StockPicking(osv.osv):
152 _inherit = 'stock.picking'
155 # Explode picking by replacing phantom BoMs
157 def action_explode(self, cr, uid, picks, *args):
158 """ Explodes picking by replacing phantom BoMs
159 @param picks: Picking ids.
160 @param *args: Arguments
161 @return: Picking ids.
163 move_obj = self.pool.get('stock.move')
164 for move in move_obj.browse(cr, uid, picks):
165 move_obj._action_explode(cr, uid, move)
171 class spilt_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 production_obj = self.pool.get('mrp.production')
180 move_obj = self.pool.get('stock.move')
182 for move in move_obj.browse(cr, uid, move_ids, context=context):
183 new_moves = super(spilt_in_production_lot, self).split(cr, uid, ids, move_ids, context=context)
184 production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
185 for new_move in new_moves:
186 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
189 spilt_in_production_lot()
190 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: