[MERGE] forward port of branch 7.0 up to e5533d0
[odoo/odoo.git] / addons / mrp / mrp.py
index e8e1887..bdcf0ab 100644 (file)
@@ -27,8 +27,7 @@ from openerp.osv import fields, osv, orm
 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP
 from openerp.tools import float_compare
 from openerp.tools.translate import _
-from openerp import netsvc
-from openerp import tools
+from openerp import tools, SUPERUSER_ID
 from openerp import SUPERUSER_ID
 from openerp.addons.product import _common
 
@@ -73,7 +72,6 @@ class mrp_workcenter(osv.osv):
             value = {'costs_hour': cost.standard_price}
         return {'value': value}
 
-mrp_workcenter()
 
 
 class mrp_routing(osv.osv):
@@ -101,7 +99,6 @@ class mrp_routing(osv.osv):
         'active': lambda *a: 1,
         'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'mrp.routing', context=context)
     }
-mrp_routing()
 
 class mrp_routing_workcenter(osv.osv):
     """
@@ -127,7 +124,6 @@ class mrp_routing_workcenter(osv.osv):
         'cycle_nbr': lambda *a: 1.0,
         'hour_nbr': lambda *a: 0.0,
     }
-mrp_routing_workcenter()
 
 class mrp_bom(osv.osv):
     """
@@ -430,6 +426,18 @@ class mrp_production(osv.osv):
             location_id = False
         return location_id
 
+    def _get_progress(self, cr, uid, ids, name, arg, context=None):
+        """ Return product quantity percentage """
+        result = dict.fromkeys(ids, 100)
+        for mrp_production in self.browse(cr, uid, ids, context=context):
+            if mrp_production.product_qty:
+                done = 0.0
+                for move in mrp_production.move_created_ids2:
+                    if not move.scrapped and move.product_id == mrp_production.product_id:
+                        done += move.product_qty
+                result[mrp_production.id] = done / mrp_production.product_qty * 100
+        return result
+
     _columns = {
         'name': fields.char('Reference', size=64, required=True, readonly=True, states={'draft': [('readonly', False)]}),
         'origin': fields.char('Source Document', size=64, readonly=True, states={'draft': [('readonly', False)]},
@@ -442,6 +450,8 @@ class mrp_production(osv.osv):
         'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', required=True, readonly=True, states={'draft': [('readonly', False)]}),
         'product_uos_qty': fields.float('Product UoS Quantity', readonly=True, states={'draft': [('readonly', False)]}),
         'product_uos': fields.many2one('product.uom', 'Product UoS', readonly=True, states={'draft': [('readonly', False)]}),
+        'progress': fields.function(_get_progress, type='float',
+            string='Production progress'),
 
         'location_src_id': fields.many2one('stock.location', 'Raw Materials Location', required=True,
             readonly=True, states={'draft':[('readonly',False)]},
@@ -735,10 +745,9 @@ class mrp_production(osv.osv):
         stock_mov_obj = self.pool.get('stock.move')
         production = self.browse(cr, uid, production_id, context=context)
 
-        wf_service = netsvc.LocalService("workflow")
         if not production.move_lines and production.state == 'ready':
             # trigger workflow if not products to consume (eg: services)
-            wf_service.trg_validate(uid, 'mrp.production', production_id, 'button_produce', cr)
+            self.signal_button_produce(cr, uid, [production_id])
 
         produced_qty = 0
         for produced_product in production.move_created_ids2:
@@ -816,8 +825,8 @@ class mrp_production(osv.osv):
                     new_parent_ids.append(final_product.id)
             for new_parent_id in new_parent_ids:
                 stock_mov_obj.write(cr, uid, [raw_product.id], {'move_history_ids': [(4,new_parent_id)]})
-
-        wf_service.trg_validate(uid, 'mrp.production', production_id, 'button_produce_done', cr)
+        self.message_post(cr, uid, production_id, body=_("%s produced") % self._description, context=context)
+        self.signal_button_produce_done(cr, uid, [production_id])
         return True
 
     def _costs_generate(self, cr, uid, production):
@@ -835,7 +844,10 @@ class mrp_production(osv.osv):
                 account = wc.costs_hour_account_id.id
                 if value and account:
                     amount += value
-                    analytic_line_obj.create(cr, uid, {
+                    # we user SUPERUSER_ID as we do not garantee an mrp user
+                    # has access to account analytic lines but still should be
+                    # able to produce orders
+                    analytic_line_obj.create(cr, SUPERUSER_ID, {
                         'name': wc_line.name + ' (H)',
                         'amount': value,
                         'account_id': account,
@@ -851,7 +863,7 @@ class mrp_production(osv.osv):
                 account = wc.costs_cycle_account_id.id
                 if value and account:
                     amount += value
-                    analytic_line_obj.create(cr, uid, {
+                    analytic_line_obj.create(cr, SUPERUSER_ID, {
                         'name': wc_line.name+' (C)',
                         'amount': value,
                         'account_id': account,
@@ -891,7 +903,6 @@ class mrp_production(osv.osv):
         return True
 
     def _make_production_line_procurement(self, cr, uid, production_line, shipment_move_id, context=None):
-        wf_service = netsvc.LocalService("workflow")
         procurement_order = self.pool.get('procurement.order')
         production = production_line.production_id
         location_id = production.location_src_id.id
@@ -911,8 +922,7 @@ class mrp_production(osv.osv):
                     'move_id': shipment_move_id,
                     'company_id': production.company_id.id,
                 })
-        self._hook_create_post_procurement(cr, uid, production, procurement_id, context=context)
-        wf_service.trg_validate(uid, procurement_order._name, procurement_id, 'button_confirm', cr)
+        procurement_order.signal_button_confirm(cr, uid, [procurement_id])
         return procurement_id
 
     def _make_production_internal_shipment_line(self, cr, uid, production_line, shipment_id, parent_move_id, destination_location_id=False, context=None):
@@ -1028,7 +1038,6 @@ class mrp_production(osv.osv):
         @return: Newly generated Shipment Id.
         """
         shipment_id = False
-        wf_service = netsvc.LocalService("workflow")
         uncompute_ids = filter(lambda x:x, [not x.product_lines and x.id or False for x in self.browse(cr, uid, ids, context=context)])
         self.action_compute(cr, uid, uncompute_ids, context=context)
         for production in self.browse(cr, uid, ids, context=context):
@@ -1048,7 +1057,7 @@ class mrp_production(osv.osv):
                     self._make_production_line_procurement(cr, uid, line, shipment_move_id, context=context)
 
             if shipment_id:
-                wf_service.trg_validate(uid, 'stock.picking', shipment_id, 'button_confirm', cr)
+                self.pool.get('stock.picking').signal_button_confirm(cr, uid, [shipment_id])
             production.write({'state':'confirmed'}, context=context)
         return shipment_id