[IMP] UoM corrections + Adapt tables in stock doc
[odoo/odoo.git] / addons / mrp / wizard / mrp_product_produce.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 import openerp.addons.decimal_precision as dp
24
25
26 class mrp_product_produce_line(osv.osv_memory):
27     _name="mrp.product.produce.line"
28     _description = "Product Produce Consume lines"
29
30     _columns = {
31         'product_id': fields.many2one('product.product', 'Product'),
32         'product_qty': fields.float('Quantity (in default UoM)'),
33         'lot_id': fields.many2one('stock.production.lot', 'Lot'),
34         'produce_id': fields.many2one('mrp.product.produce'),
35         'track_production': fields.related('product_id', 'track_production', type='boolean'),
36     }
37
38 class mrp_product_produce(osv.osv_memory):
39     _name = "mrp.product.produce"
40     _description = "Product Produce"
41
42     _columns = {
43         'product_id': fields.many2one('product.product', type='many2one'),
44         'product_qty': fields.float('Select Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
45         'mode': fields.selection([('consume_produce', 'Consume & Produce'),
46                                   ('consume', 'Consume Only')], 'Mode', required=True,
47                                   help="'Consume only' mode will only consume the products with the quantity selected.\n"
48                                         "'Consume & Produce' mode will consume as well as produce the products with the quantity selected "
49                                         "and it will finish the production order when total ordered quantities are produced."),
50         'lot_id': fields.many2one('stock.production.lot', 'Lot'), #Should only be visible when it is consume and produce mode
51         'consume_lines': fields.one2many('mrp.product.produce.line', 'produce_id', 'Products Consumed'),
52         'track_production': fields.boolean('Track production'),
53     }
54
55     def on_change_qty(self, cr, uid, ids, product_qty, consume_lines, context=None):
56         """ 
57             When changing the quantity of products to be produced it will 
58             recalculate the number of raw materials needed according
59             to the scheduled products and the already consumed/produced products
60             It will return the consume lines needed for the products to be produced
61             which the user can still adapt
62         """
63         prod_obj = self.pool.get("mrp.production")
64         uom_obj = self.pool.get("product.uom")
65         production = prod_obj.browse(cr, uid, context['active_id'], context=context)
66         consume_lines = []
67         new_consume_lines = []
68         if product_qty > 0.0:
69             product_uom_qty = uom_obj._compute_qty(cr, uid, production.product_uom.id, product_qty, production.product_id.uom_id.id)
70             consume_lines = prod_obj._calculate_qty(cr, uid, production, product_qty=product_uom_qty, context=context)
71         
72         for consume in consume_lines:
73             new_consume_lines.append([0, False, consume])
74         return {'value': {'consume_lines': new_consume_lines}}
75
76
77     def _get_product_qty(self, cr, uid, context=None):
78         """ To obtain product quantity
79         @param self: The object pointer.
80         @param cr: A database cursor
81         @param uid: ID of the user currently logged in
82         @param context: A standard dictionary
83         @return: Quantity
84         """
85         if context is None:
86             context = {}
87         prod = self.pool.get('mrp.production').browse(cr, uid,
88                                 context['active_id'], context=context)
89         done = 0.0
90         for move in prod.move_created_ids2:
91             if move.product_id == prod.product_id:
92                 if not move.scrapped:
93                     done += move.product_uom_qty # As uom of produced products and production order should correspond
94         return prod.product_qty - done
95
96     def _get_product_id(self, cr, uid, context=None):
97         """ To obtain product id
98         @return: id
99         """
100         prod=False
101         if context and context.get("active_id"):
102             prod = self.pool.get('mrp.production').browse(cr, uid,
103                                     context['active_id'], context=context)
104         return prod and prod.product_id.id or False
105     
106     def _get_track(self, cr, uid, context=None):
107         prod = self._get_product_id(cr, uid, context=context)
108         prod_obj = self.pool.get("product.product")
109         return prod and prod_obj.browse(cr, uid, prod, context=context).track_production or False
110
111     _defaults = {
112          'product_qty': _get_product_qty,
113          'mode': lambda *x: 'consume_produce',
114          'product_id': _get_product_id,
115          'track_production': _get_track, 
116     }
117
118     def do_produce(self, cr, uid, ids, context=None):
119         production_id = context.get('active_id', False)
120         assert production_id, "Production Id should be specified in context as a Active ID."
121         data = self.browse(cr, uid, ids[0], context=context)
122         self.pool.get('mrp.production').action_produce(cr, uid, production_id,
123                             data.product_qty, data.mode, data, context=context)
124         return {}
125
126
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: