[FIX] (5.0 - 6.0)mrp_subproduct : Corrected action_confirm method
[odoo/odoo.git] / addons / mrp_subproduct / mrp_subproduct.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
25 class mrp_subproduct(osv.osv):
26     _name = 'mrp.subproduct'
27     _description = 'Mrp Sub Product'
28     _columns={
29         'product_id': fields.many2one('product.product', 'Product', required=True),
30         'product_qty': fields.float('Product Qty', required=True),
31         'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
32         'subproduct_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Quantity Type', required=True),
33         'bom_id': fields.many2one('mrp.bom', 'BoM'),
34     }
35     _defaults={
36         'subproduct_type': lambda *args: 'fixed'
37     }
38     def onchange_product_id(self, cr, uid, ids, product_id,context={}):
39         if product_id:
40             prod=self.pool.get('product.product').browse(cr,uid,product_id)
41             v = {'product_uom':prod.uom_id.id}
42             return {'value': v}
43         return {}
44
45 mrp_subproduct()
46
47 class mrp_bom(osv.osv):
48     _name = 'mrp.bom'
49     _description = 'Bill of Material'
50     _inherit='mrp.bom'
51     _columns={
52         'sub_products':fields.one2many('mrp.subproduct', 'bom_id', 'sub_products'),
53     }
54 mrp_bom()
55
56 class mrp_production(osv.osv):
57     _name = 'mrp.production'
58     _description = 'Production'
59     _inherit= 'mrp.production'   
60
61     def action_confirm(self, cr, uid, ids):
62         picking_id=super(mrp_production,self).action_confirm(cr, uid, ids)
63         for production in self.browse(cr, uid, ids):
64             source = production.product_id.product_tmpl_id.property_stock_production.id
65             if not production.bom_id:
66                 continue
67             for sub_product in production.bom_id.sub_products:
68                 qty1 = sub_product.product_qty
69                 qty2 = production.product_uos and production.product_uos_qty or False
70                 if sub_product.subproduct_type=='variable':
71                     if production.product_qty:
72                         qty1 *= production.product_qty / (production.bom_id.product_qty or 1.0)
73                     if production.product_uos_qty:
74                         qty2 *= production.product_uos_qty / (production.bom_id.product_uos_qty or 1.0)
75                 data = {
76                     'name':'PROD:'+production.name,
77                     'date_planned': production.date_planned,
78                     'product_id': sub_product.product_id.id,
79                     'product_qty': qty1,
80                     'product_uom': sub_product.product_uom.id,
81                     'product_uos_qty': qty2,
82                     'product_uos': production.product_uos and production.product_uos.id or False,
83                     'location_id': source,
84                     'location_dest_id': production.location_dest_id.id,
85                     'move_dest_id': production.move_prod_id.id,
86                     'state': 'waiting',
87                     'production_id':production.id
88                 }
89                 sub_prod_ids=self.pool.get('stock.move').create(cr, uid,data)
90         return picking_id
91
92 mrp_production()
93 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: