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