designed a module mrp_subproduct, that adds a tab on the mrp.bom
[odoo/odoo.git] / addons / mrp_subproduct / mrp_subproduct.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 from osv import fields
32 from osv import osv
33
34 class mrp_subproduct(osv.osv):
35     _name = 'mrp.subproduct'
36     _description = 'Mrp Sub Product'
37     _columns={
38               'product_id': fields.many2one('product.product', 'Product', required=True),
39               'product_qty': fields.float('Product Qty', required=True),
40               'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
41               'bom_id': fields.many2one('mrp.bom', 'BoM'),
42               }
43     def onchange_product_id(self, cr, uid, ids, product_id,context={}):
44          if product_id:
45             prod=self.pool.get('product.product').browse(cr,uid,product_id)
46             v = {'product_uom':prod.uom_id.id}
47             return {'value': v}
48          return {}
49
50 mrp_subproduct()
51
52 class mrp_bom(osv.osv):
53     _name = 'mrp.bom'
54     _description = 'Bill of Material'
55     _inherit='mrp.bom'
56     _columns={
57               'sub_products':fields.one2many('mrp.subproduct', 'bom_id', 'sub_products'),
58               }
59
60
61 mrp_bom()
62
63 class mrp_production(osv.osv):
64     _name = 'mrp.production'
65     _description = 'Production'
66     _inherit= 'mrp.production'
67     _columns={
68               }
69
70     def action_confirm(self, cr, uid, ids):
71          picking_id=super(mrp_production,self).action_confirm(cr, uid, ids)
72          for production in self.browse(cr, uid, ids):
73              source = production.product_id.product_tmpl_id.property_stock_production.id
74              for sub_product in production.bom_id.sub_products:
75                  print "::::::::",sub_product.product_id.id,sub_product.product_uom.id,sub_product.product_qty,production.id
76
77                  data = {
78                     'name':'PROD:'+production.name,
79                     'date_planned': production.date_planned,
80                     'product_id': sub_product.product_id.id,
81                     'product_qty': sub_product.product_qty,
82                     'product_uom': sub_product.product_uom.id,
83                     'product_uos_qty': production.product_uos and production.product_uos_qty or False,
84                     'product_uos': production.product_uos and production.product_uos.id or False,
85                     'location_id': source,
86                     'location_dest_id': production.location_dest_id.id,
87                     'move_dest_id': production.move_prod_id.id,
88                     'state': 'waiting',
89                     'production_id':production.id
90                  }
91                  sub_prod_ids=self.pool.get('stock.move').create(cr, uid,data)
92          return picking_id
93
94 mrp_production()