[IMP] Rounding should be done on move immediately to default UoM and quants should...
[odoo/odoo.git] / addons / mrp / product.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 openerp.osv import fields, osv
23
24
25 class product_template(osv.osv):
26     _inherit = "product.template"
27     def _bom_orders_count(self, cr, uid, ids, field_name, arg, context=None):
28         Bom = self.pool('mrp.bom')
29         res = {}
30         for product_tmpl_id in ids:
31             nb = Bom.search_count(cr, uid, [('product_tmpl_id', '=', product_tmpl_id)], context=context)
32             res[product_tmpl_id] = {
33                 'bom_count': nb,
34             }
35         return res
36
37     def _bom_orders_count_mo(self, cr, uid, ids, name, arg, context=None):
38         res = {}
39         for product_tmpl_id in self.browse(cr, uid, ids):
40             res[product_tmpl_id.id] = sum([p.mo_count for p in product_tmpl_id.product_variant_ids])
41         return res
42
43     _columns = {
44         'bom_ids': fields.one2many('mrp.bom', 'product_tmpl_id','Bill of Materials'),
45         'bom_count': fields.function(_bom_orders_count, string='# Bill of Material', type='integer', multi="_bom_order_count"),
46         'mo_count': fields.function(_bom_orders_count_mo, string='# Manufacturing Orders', type='integer'),
47         'produce_delay': fields.float('Manufacturing Lead Time', help="Average delay in days to produce this product. In the case of multi-level BOM, the manufacturing lead times of the components will be added."),
48         'track_production': fields.boolean('Track Manufacturing Lots', help="Forces to specify a Serial Number for all moves containing this product and generated by a Manufacturing Order"),
49     }
50
51     _defaults = {
52         'produce_delay': 1,
53     }
54     
55     
56     def action_view_mos(self, cr, uid, ids, context=None):
57         products = self._get_products(cr, uid, ids, context=context)
58         result = self._get_act_window_dict(cr, uid, 'mrp.act_product_mrp_production', context=context)
59         if len(ids) == 1 and len(products) == 1:
60             result['context'] = "{'default_product_id': " + str(products[0]) + ", 'search_default_product_id': " + str(products[0]) + "}"
61         else:
62             result['domain'] = "[('product_id','in',[" + ','.join(map(str, products)) + "])]"
63             result['context'] = "{}"
64         return result
65
66     
67
68 class product_product(osv.osv):
69     _inherit = "product.product"
70     def _bom_orders_count(self, cr, uid, ids, field_name, arg, context=None):
71         Production = self.pool('mrp.production')
72         res = {}
73         for product_id in ids:
74             res[product_id] = Production.search_count(cr,uid, [('product_id', '=', product_id)], context=context)
75         return res
76
77     _columns = {
78         'mo_count': fields.function(_bom_orders_count, string='# Manufacturing Orders', type='integer'),
79     }
80     
81     def action_view_bom(self, cr, uid, ids, context=None):
82         tmpl_obj = self.pool.get("product.template")
83         products = set()
84         for product in self.browse(cr, uid, ids, context=context):
85             products.add(product.product_tmpl_id.id)
86         result = tmpl_obj._get_act_window_dict(cr, uid, 'mrp.product_open_bom', context=context)
87         # bom specific to this variant or global to template
88         domain = [
89             '|',
90                 ('product_id', 'in', ids),
91                 '&',
92                     ('product_id', '=', False),
93                     ('product_tmpl_id', 'in', list(products)),
94         ]
95         result['context'] = "{}"
96         result['domain'] = str(domain)
97         return result
98
99 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: