bacdeb2c67546ca8d11fa87d61da33c5ab327117
[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 = 'Byproduct'
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 Unit of Measure'), required=True),
32         'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', 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
65 mrp_bom()
66
67 class mrp_production(osv.osv):
68     _description = 'Production'
69     _inherit= 'mrp.production'
70
71
72     def action_confirm(self, cr, uid, ids):
73         """ Confirms production order and calculates quantity based on subproduct_type.
74         @return: Newly generated picking Id.
75         """
76         picking_id = super(mrp_production,self).action_confirm(cr, uid, ids)
77         for production in self.browse(cr, uid, ids):
78             source = production.product_id.product_tmpl_id.property_stock_production.id
79             if not production.bom_id:
80                 continue
81             for sub_product in production.bom_id.sub_products:
82                 qty1 = sub_product.product_qty
83                 qty2 = production.product_uos and production.product_uos_qty or False
84                 if sub_product.subproduct_type == 'variable':
85                     if production.product_qty:
86                         qty1 *= production.product_qty / (production.bom_id.product_qty or 1.0)
87                     if production.product_uos_qty:
88                         qty2 *= production.product_uos_qty / (production.bom_id.product_uos_qty or 1.0)
89                 data = {
90                     'name': 'PROD:'+production.name,
91                     'date': production.date_planned,
92                     'product_id': sub_product.product_id.id,
93                     'product_qty': qty1,
94                     'product_uom': sub_product.product_uom.id,
95                     'product_uos_qty': qty2,
96                     'product_uos': production.product_uos and production.product_uos.id or False,
97                     'location_id': source,
98                     'location_dest_id': production.location_dest_id.id,
99                     'move_dest_id': production.move_prod_id.id,
100                     'state': 'waiting',
101                     'production_id': production.id
102                 }
103                 self.pool.get('stock.move').create(cr, uid, data)
104         return picking_id
105
106     def _get_subproduct_factor(self, cr, uid, production_id, move_id=None, context=None):
107         """Compute the factor to compute the qty of procucts to produce for the given production_id. By default, 
108             it's always equal to the quantity encoded in the production order or the production wizard, but with 
109             the module mrp_subproduct installed it can differ for subproducts having type 'variable'.
110         :param production_id: ID of the mrp.order
111         :param move_id: ID of the stock move that needs to be produced. Identify the product to produce.
112         :return: The factor to apply to the quantity that we should produce for the given production order and stock move.
113         """
114         sub_obj = self.pool.get('mrp.subproduct')
115         move_obj = self.pool.get('stock.move')
116         production_obj = self.pool.get('mrp.production')
117         production_browse = production_obj.browse(cr, uid, production_id, context=context)
118         move_browse = move_obj.browse(cr, uid, move_id, context=context)
119         subproduct_factor = 1
120         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)
121         if sub_id:
122             subproduct_record = sub_obj.browse(cr ,uid, sub_id[0], context=context)
123             if subproduct_record.bom_id.product_qty:
124                 subproduct_factor = subproduct_record.product_qty / subproduct_record.bom_id.product_qty
125                 return subproduct_factor
126         return super(mrp_production, self)._get_subproduct_factor(cr, uid, production_id, move_id, context=context)
127
128 mrp_production()
129
130 class change_production_qty(osv.osv_memory):
131     _inherit = 'change.production.qty'
132
133     def _update_product_to_produce(self, cr, uid, prod, qty, context=None):
134         bom_obj = self.pool.get('mrp.bom')
135         move_lines_obj = self.pool.get('stock.move')
136         prod_obj = self.pool.get('mrp.production')
137         for m in prod.move_created_ids:
138             if m.product_id.id == prod.product_id.id:
139                 move_lines_obj.write(cr, uid, [m.id], {'product_qty': qty})
140             else:
141                 for sub_product_line in prod.bom_id.sub_products:
142                     if sub_product_line.product_id.id == m.product_id.id:
143                         factor = prod_obj._get_subproduct_factor(cr, uid, prod.id, m.id, context=context)
144                         subproduct_qty = sub_product_line.subproduct_type == 'variable' and qty * factor or sub_product_line.product_qty
145                         move_lines_obj.write(cr, uid, [m.id], {'product_qty': subproduct_qty})
146
147 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: