[FIX] Hr_payroll: Inherit wizard of payslip runs to pass journl id on context which...
[odoo/odoo.git] / addons / mrp / stock.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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
23 from osv import osv
24 import netsvc
25
26
27 class StockMove(osv.osv):
28     _inherit = 'stock.move'
29     
30     _columns = {
31         'production_id': fields.many2one('mrp.production', 'Production', select=True),
32     }
33
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)
37         return new_moves
38     
39     def _action_explode(self, cr, uid, move, context=None):
40         """ Explodes pickings.
41         @param move: Stock moves
42         @return: True
43         """
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),
52                 ('bom_id','=',False),
53                 ('type','=','phantom')])
54             if bis:
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
59                 state = 'confirmed'
60                 if move.state == 'assigned':
61                     state = 'assigned'
62                 for line in res[0]:                    
63                     valdef = {
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,
71                         'state': state,
72                         'name': line['name'],
73                         'location_dest_id': dest,
74                         'move_history_ids': [(6,0,[move.id])],
75                         'move_history_ids2': [(6,0,[])],
76                         'procurements': [],
77                     }
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,
91                         'move_id': mid,
92                     })
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,
97                     'picking_id': False,
98                     'state': 'confirmed'
99                 })
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)
103         return True
104     
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
110         """       
111         res = []
112         production_obj = self.pool.get('mrp.production')
113         wf_service = netsvc.LocalService("workflow")
114         for move in self.browse(cr, uid, ids):
115             new_moves = super(StockMove, self).action_consume(cr, uid, [move.id], product_qty, location_id, context=context)
116             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
117             for prod in production_obj.browse(cr, uid, production_ids, context=context):
118                 if prod.state == 'confirmed':
119                     production_obj.force_production(cr, uid, [prod.id])
120                 wf_service.trg_validate(uid, 'mrp.production', prod.id, 'button_produce', cr)
121             for new_move in new_moves:
122                 if new_move == move.id:
123                     #This move is already there in move lines of production order
124                     continue
125                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
126                 res.append(new_move)
127         return res
128     
129     def action_scrap(self, cr, uid, ids, product_qty, location_id, context=None):
130         """ Move the scrap/damaged product into scrap location
131         @param product_qty: Scraped product quantity
132         @param location_id: Scrap location
133         @return: Scraped lines
134         """  
135         res = []
136         production_obj = self.pool.get('mrp.production')
137         wf_service = netsvc.LocalService("workflow")
138         for move in self.browse(cr, uid, ids, context=context):
139             new_moves = super(StockMove, self).action_scrap(cr, uid, [move.id], product_qty, location_id, context=context)
140             self.write(cr, uid, [move.id], {'prodlot_id': False, 'tracking_id': False})
141             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
142             for prod_id in production_ids:
143                 wf_service.trg_validate(uid, 'mrp.production', prod_id, 'button_produce', cr)
144             for new_move in new_moves:
145                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})
146                 res.append(new_move)
147         return {}
148
149 StockMove()
150
151
152 class StockPicking(osv.osv):
153     _inherit = 'stock.picking'
154
155     #
156     # Explode picking by replacing phantom BoMs
157     #
158     def action_explode(self, cr, uid, picks, *args):
159         """ Explodes picking by replacing phantom BoMs
160         @param picks: Picking ids. 
161         @param *args: Arguments
162         @return: Picking ids.
163         """  
164         move_obj = self.pool.get('stock.move')
165         for move in move_obj.browse(cr, uid, picks):
166             move_obj._action_explode(cr, uid, move)
167         return picks
168
169 StockPicking()
170
171
172 class spilt_in_production_lot(osv.osv_memory):
173     _inherit = "stock.move.split"
174     
175     def split(self, cr, uid, ids, move_ids, context=None):
176         """ Splits move lines into given quantities.
177         @param move_ids: Stock moves.
178         @return: List of new moves.
179         """  
180         production_obj = self.pool.get('mrp.production')
181         move_obj = self.pool.get('stock.move')  
182         res = []
183         for move in move_obj.browse(cr, uid, move_ids, context=context):
184             new_moves = super(spilt_in_production_lot, self).split(cr, uid, ids, move_ids, context=context)
185             production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
186             for new_move in new_moves:
187                 production_obj.write(cr, uid, production_ids, {'move_lines': [(4, new_move)]})                
188         return res
189     
190 spilt_in_production_lot()
191 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: