[Fix]: Bug #689678 Error when manufacturing a subproduct with quantity = 0
[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     _sql_constraints = [
39         ('_check_product_qty', 'CHECK (product_qty > 0)', 'Enter valid product quantity !'),
40     ]
41
42     def onchange_product_id(self, cr, uid, ids, product_id,context={}):
43         """ Changes UoM if product_id changes.
44         @param product_id: Changed product_id
45         @return: Dictionary of changed values
46         """
47         if product_id:
48             prod = self.pool.get('product.product').browse(cr, uid, product_id)
49             v = {'product_uom': prod.uom_id.id}
50             return {'value': v}
51         return {}
52
53 mrp_subproduct()
54
55 class mrp_bom(osv.osv):
56     _name = 'mrp.bom'
57     _description = 'Bill of Material'
58     _inherit='mrp.bom'
59
60     _columns={
61         'sub_products':fields.one2many('mrp.subproduct', 'bom_id', 'sub_products'),
62     }
63 mrp_bom()
64
65 class mrp_production(osv.osv):
66     _name = 'mrp.production'
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 mrp_production()
105 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: