modifs
[odoo/odoo.git] / addons / mrp_subproduct / mrp_subproduct.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields
24 from osv import osv
25
26 class mrp_subproduct(osv.osv):
27     _name = 'mrp.subproduct'
28     _description = 'Mrp Sub Product'
29     _columns={
30         'product_id': fields.many2one('product.product', 'Product', required=True),
31         'product_qty': fields.float('Product Qty', required=True),
32         'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
33         'subproduct_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Quantity Type', required=True),
34         'bom_id': fields.many2one('mrp.bom', 'BoM'),
35     }
36     _defaults={
37         'subproduct_type': lambda *args: 'fixed'
38     }
39     def onchange_product_id(self, cr, uid, ids, product_id,context={}):
40         if product_id:
41             prod=self.pool.get('product.product').browse(cr,uid,product_id)
42             v = {'product_uom':prod.uom_id.id}
43             return {'value': v}
44         return {}
45
46 mrp_subproduct()
47
48 class mrp_bom(osv.osv):
49     _name = 'mrp.bom'
50     _description = 'Bill of Material'
51     _inherit='mrp.bom'
52     _columns={
53         'sub_products':fields.one2many('mrp.subproduct', 'bom_id', 'sub_products'),
54     }
55 mrp_bom()
56
57 class mrp_production(osv.osv):
58     _name = 'mrp.production'
59     _description = 'Production'
60     _inherit= 'mrp.production'   
61
62     def action_confirm(self, cr, uid, ids):
63          picking_id=super(mrp_production,self).action_confirm(cr, uid, ids)
64          for production in self.browse(cr, uid, ids):
65              source = production.product_id.product_tmpl_id.property_stock_production.id
66              for sub_product in production.bom_id.sub_products:
67                  qty1 = sub_product.product_qty
68                  qty2 = production.product_uos and production.product_uos_qty or False
69                  if sub_product.subproduct_type=='variable':
70                     if production.product_qty:
71                         qty1 *= production.product_qty / (sub_product.product_qty or 1.0)
72                     if production.product_uos_qty:
73                         qty2 *= production.product_uos_qty / (sub_product.product_qty or 1.0)
74                  data = {
75                     'name':'PROD:'+production.name,
76                     'date_planned': production.date_planned,
77                     'product_id': sub_product.product_id.id,
78                     'product_qty': qty1,
79                     'product_uom': sub_product.product_uom.id,
80                     'product_uos_qty': qty2,
81                     'product_uos': production.product_uos and production.product_uos.id or False,
82                     'location_id': source,
83                     'location_dest_id': production.location_dest_id.id,
84                     'move_dest_id': production.move_prod_id.id,
85                     'state': 'waiting',
86                     'production_id':production.id
87                  }
88                  sub_prod_ids=self.pool.get('stock.move').create(cr, uid,data)
89          return picking_id
90
91 mrp_production()