[IMP] : Added context=None on methods used for _constraints and replaced context...
[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 = '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     
39     def onchange_product_id(self, cr, uid, ids, product_id, context=None):
40         """ Changes UoM if product_id changes.
41         @param product_id: Changed product_id
42         @return: Dictionary of changed values
43         """
44         if not context:
45             context = {}
46         if product_id:
47             prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
48             v = {'product_uom': prod.uom_id.id}
49             return {'value': v}
50         return {}
51
52 mrp_subproduct()
53
54 class mrp_bom(osv.osv):
55     _name = 'mrp.bom'
56     _description = 'Bill of Material'
57     _inherit='mrp.bom'
58     
59     _columns={
60         'sub_products':fields.one2many('mrp.subproduct', 'bom_id', 'sub_products'),
61     }
62 mrp_bom()
63
64 class mrp_production(osv.osv):
65     _name = 'mrp.production'
66     _description = 'Production'
67     _inherit= 'mrp.production'   
68
69     def action_confirm(self, cr, uid, ids):
70         """ Confirms production order and calculates quantity based on subproduct_type.
71         @return: Newly generated picking Id.
72         """
73         picking_id = super(mrp_production,self).action_confirm(cr, uid, ids)
74         for production in self.browse(cr, uid, ids):
75             source = production.product_id.product_tmpl_id.property_stock_production.id
76             if not production.bom_id:
77                 continue
78             for sub_product in production.bom_id.sub_products:
79                 qty1 = sub_product.product_qty
80                 qty2 = production.product_uos and production.product_uos_qty or False
81                 if sub_product.subproduct_type == 'variable':
82                     if production.product_qty:
83                         qty1 *= production.product_qty / (production.bom_id.product_qty or 1.0)
84                     if production.product_uos_qty:
85                         qty2 *= production.product_uos_qty / (production.bom_id.product_uos_qty or 1.0)
86                 data = {
87                     'name': 'PROD:'+production.name,
88                     'date': production.date_planned,
89                     'product_id': sub_product.product_id.id,
90                     'product_qty': qty1,
91                     'product_uom': sub_product.product_uom.id,
92                     'product_uos_qty': qty2,
93                     'product_uos': production.product_uos and production.product_uos.id or False,
94                     'location_id': source,
95                     'location_dest_id': production.location_dest_id.id,
96                     'move_dest_id': production.move_prod_id.id,
97                     'state': 'waiting',
98                     'production_id': production.id
99                 }
100                 self.pool.get('stock.move').create(cr, uid, data)
101         return picking_id
102
103 mrp_production()
104 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: