f6e18ead5e9d98ff7eb237f9b93848395a22e0c5
[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 import decimal_precision as dp
25
26 class mrp_subproduct(osv.osv):
27     _name = 'mrp.subproduct'
28     _description = 'Sub Product'
29     _columns={
30         'product_id': fields.many2one('product.product', 'Product', required=True),
31         'product_qty': fields.float('Product Qty', digits_compute=dp.get_precision('Product UoM'), 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, help="Define how the quantity of subproducts will be set on the production orders using this BoM.\
34   'Fixed' depicts a situation where the quantity of created subproduct is always equal to the quantity set on the BoM, regardless of how many are created in the production order.\
35   By opposition, 'Variable' means that the quantity will be computed as\
36     '(quantity of subproduct set on the BoM / quantity of manufactured product set on the BoM * quantity of manufactured product in the production order.)'"),
37         'bom_id': fields.many2one('mrp.bom', 'BoM'),
38     }
39     _defaults={
40         'subproduct_type': 'variable',
41     }
42
43     def onchange_product_id(self, cr, uid, ids, product_id, context=None):
44         """ Changes UoM if product_id changes.
45         @param product_id: Changed product_id
46         @return: Dictionary of changed values
47         """
48         if product_id:
49             prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
50             v = {'product_uom': prod.uom_id.id}
51             return {'value': v}
52         return {}
53
54 mrp_subproduct()
55
56 class mrp_bom(osv.osv):
57     _name = 'mrp.bom'
58     _description = 'Bill of Material'
59     _inherit='mrp.bom'
60
61     _columns={
62         'sub_products':fields.one2many('mrp.subproduct', 'bom_id', 'sub_products'),
63     }
64 mrp_bom()
65
66 class mrp_production(osv.osv):
67     _description = 'Production'
68     _inherit= 'mrp.production'
69
70     def action_confirm(self, cr, uid, ids):
71         """ Confirms production order and calculates quantity based on subproduct_type.
72         @return: Newly generated picking Id.
73         """
74         picking_id = super(mrp_production,self).action_confirm(cr, uid, ids)
75         for production in self.browse(cr, uid, ids):
76             source = production.product_id.product_tmpl_id.property_stock_production.id
77             if not production.bom_id:
78                 continue
79             for sub_product in production.bom_id.sub_products:
80                 qty1 = sub_product.product_qty
81                 qty2 = production.product_uos and production.product_uos_qty or False
82                 if sub_product.subproduct_type == 'variable':
83                     if production.product_qty:
84                         qty1 *= production.product_qty / (production.bom_id.product_qty or 1.0)
85                     if production.product_uos_qty:
86                         qty2 *= production.product_uos_qty / (production.bom_id.product_uos_qty or 1.0)
87                 data = {
88                     'name': 'PROD:'+production.name,
89                     'date': production.date_planned,
90                     'product_id': sub_product.product_id.id,
91                     'product_qty': qty1,
92                     'product_uom': sub_product.product_uom.id,
93                     'product_uos_qty': qty2,
94                     'product_uos': production.product_uos and production.product_uos.id or False,
95                     'location_id': source,
96                     'location_dest_id': production.location_dest_id.id,
97                     'move_dest_id': production.move_prod_id.id,
98                     'state': 'waiting',
99                     'production_id': production.id
100                 }
101                 self.pool.get('stock.move').create(cr, uid, data)
102         return picking_id
103
104     def _get_subproduct_factor(self, cr, uid, production_id, move_id=None, context=None):
105         """Compute the factor to compute the qty of procucts to produce for the given production_id. By default, 
106             it's always equal to the quantity encoded in the production order or the production wizard, but with 
107             the module mrp_subproduct installed it can differ for subproducts having type 'variable'.
108         :param production_id: ID of the mrp.order
109         :param move_id: ID of the stock move that needs to be produced. Identify the product to produce.
110         :return: The factor to apply to the quantity that we should produce for the given production order and stock move.
111         """
112         sub_obj = self.pool.get('mrp.subproduct')
113         move_obj = self.pool.get('stock.move')
114         production_obj = self.pool.get('mrp.production')
115         production_browse = production_obj.browse(cr, uid, production_id, context=context)
116         move_browse = move_obj.browse(cr, uid, move_id, context=context)
117         subproduct_factor = 1
118         sub_id = sub_obj.search(cr, uid,[('product_id', '=', move_browse.product_id.id),('bom_id', '=', production_browse.bom_id.id), ('subproduct_type', '=', 'variable')], context=context)
119         if sub_id:
120             subproduct_record = sub_obj.browse(cr ,uid, sub_id[0], context=context)
121             if subproduct_record.bom_id.product_qty:
122                 subproduct_factor = subproduct_record.product_qty / subproduct_record.bom_id.product_qty
123                 return subproduct_factor
124         return super(mrp_production, self)._get_subproduct_factor(cr, uid, production_id, move_id, context=context)
125
126 mrp_production()
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: