From b9e0e55764dcdebbae555365c3cf7df2d516281d Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 10 Mar 2016 12:21:05 +0100 Subject: [PATCH] Add mrp.bom.labour.line, to be able to have several labor cost profile on a single BOM --- mrp_average_cost/mrp.py | 70 ++++++++++++++++--- mrp_average_cost/mrp_view.xml | 49 +++++++++++-- mrp_average_cost/security/ir.model.access.csv | 6 ++ 3 files changed, 108 insertions(+), 17 deletions(-) diff --git a/mrp_average_cost/mrp.py b/mrp_average_cost/mrp.py index 6f807a2..c0ca00b 100644 --- a/mrp_average_cost/mrp.py +++ b/mrp_average_cost/mrp.py @@ -28,19 +28,74 @@ import logging logger = logging.getLogger(__name__) +class MrpBomLabourLine(orm.Model): + _name = 'mrp.bom.labour.line' + _description = 'Labour lines on BOM' + + _columns = { + 'bom_id': fields.many2one( + 'mrp.bom', 'Labour Lines', ondelete='cascade'), + 'labour_time': fields.float( + 'Labour Time', + digits_compute=dp.get_precision('Labour Hours'), + help="Average labour time for the production of " + "items of the BOM, in hours."), + 'labour_cost_profile_id': fields.many2one( + 'labour.cost.profile', 'Labour Cost Profile', required=True), + 'note': fields.text('Note'), + } + + _sql_constraints = [( + 'labour_time_positive', + 'CHECK (labour_time >= 0)', + "The value of the field 'Labour Time' must be positive or 0.")] + + class MrpBom(orm.Model): _inherit = 'mrp.bom' + def _compute_total_labour_cost( + self, cr, uid, ids, name, arg, context=None): + res = {} + for bom in self.browse(cr, uid, ids, context=context): + cost = 0.0 + for lline in bom.labour_line_ids: + cost += lline.labour_time * lline.labour_cost_profile_id.hour_cost + res[bom.id] = cost + return res + + def _get_bom_from_labour_lines(self, cr, uid, ids, context=None): + return self.pool['mrp.bom'].search( + cr, uid, [('labour_line_ids', 'in', ids)], context=context) + + def _get_bom_from_cost_profile(self, cr, uid, ids, context=None): + labour_line_ids = self.pool['mrp.bom.labour.line'].search( + cr, uid, [('labour_cost_profile_id', 'in', ids)], context=context) + return self.pool['mrp.bom'].search( + cr, uid, [('labour_line_ids', 'in', labour_line_ids)], context=context) + _columns = { + # TODO: delete this field once migration is done 'labour_time': fields.float( 'Labour Time', digits_compute=dp.get_precision('Labour Hours'), help="Average labour time for the production of the quantity of " "items of the BOM, in hours."), + # TODO: delete this field once migration is done 'labour_cost_profile_id': fields.many2one( 'labour.cost.profile', 'Labour Cost Profile'), + 'total_labour_cost': fields.function( + _compute_total_labour_cost, type='float', readonly=True, + string="Total Labour Cost", store={ + 'labour.cost.profile': (_get_bom_from_cost_profile, [ + 'hour_cost', 'company_id'], 10), + 'mrp.bom.labour.line': (_get_bom_from_labour_lines, [ + 'bom_id', 'labour_time', 'labour_cost_profile_id'], 20), + }), + 'labour_line_ids': fields.one2many( + 'mrp.bom.labour.line', 'bom_id', 'Labour Lines'), 'extra_cost': fields.float( - 'Extra Cost', + 'Extra Cost', track_visibility='onchange', digits_compute=dp.get_precision('Product Price'), help="Extra cost for the production of the quantity of " "items of the BOM, in company currency. " @@ -131,15 +186,10 @@ class MrpProduction(orm.Model): mo_total_price += raw_material_cost if order.bom_id: bom = order.bom_id - if not bom.labour_cost_profile_id: + if not bom.total_labour_cost: raise orm.except_orm( _('Error:'), - _("Labor Cost Profile is not set on bill of " - "material '%s'.") % bom.name) - if not bom.labour_time: - raise orm.except_orm( - _('Error:'), - _("Labour Time is not set on bill of material '%s'.") + _("Total Labor Cost is 0 on bill of material '%s'.") % bom.name) if not bom.product_qty: raise orm.except_orm( @@ -150,9 +200,7 @@ class MrpProduction(orm.Model): cr, uid, bom.product_uom, bom.product_qty, bom.product_id.uom_id, context=context) assert bom_qty_product_uom > 0, 'BoM qty should be positive' - labor_cost_per_unit = ( - bom.labour_time * bom.labour_cost_profile_id.hour_cost) /\ - bom_qty_product_uom + labor_cost_per_unit = bom.total_labour_cost / bom_qty_product_uom extra_cost_per_unit = bom.extra_cost / bom_qty_product_uom # mo_standard_price and labor_cost_per_unit are # in the UoM of the product (not of the MO/BOM) diff --git a/mrp_average_cost/mrp_view.xml b/mrp_average_cost/mrp_view.xml index 0db31a6..fe0bb36 100644 --- a/mrp_average_cost/mrp_view.xml +++ b/mrp_average_cost/mrp_view.xml @@ -14,19 +14,56 @@ + - + + + + + + + + + mrp_bom_labour_line.tree + mrp.bom.labour.line + + + + + + + + + + + + mrp_bom_labour_line.form + mrp.bom.labour.line + +
+ + + +
+
+
+ + + labour_cost_profile_form labour.cost.profile diff --git a/mrp_average_cost/security/ir.model.access.csv b/mrp_average_cost/security/ir.model.access.csv index 6db0da6..4512aae 100644 --- a/mrp_average_cost/security/ir.model.access.csv +++ b/mrp_average_cost/security/ir.model.access.csv @@ -1,3 +1,9 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_labour_cost_profile_read,Read access on labour.cost.profile to MRP user,model_labour_cost_profile,mrp.group_mrp_user,1,0,0,0 +access_labour_cost_profile_read_sale,Read access on labour.cost.profile to Sale user,model_labour_cost_profile,base.group_sale_salesman,1,0,0,0 +access_labour_cost_profile_read_stock,Read access on labour.cost.profile to Stock user,model_labour_cost_profile,stock.group_stock_user,1,0,0,0 access_labour_cost_profile_full,Full access on labour.cost.profile to MRP manager,model_labour_cost_profile,mrp.group_mrp_manager,1,1,1,1 +access_mrp_bom_labour_line_read,Read access on mrp.bom.labour.line to MRP user,model_mrp_bom_labour_line,mrp.group_mrp_user,1,0,0,0 +access_mrp_bom_labour_line_read_sale,Read access on mrp.bom.labour.line to Sale user,model_mrp_bom_labour_line,base.group_sale_salesman,1,0,0,0 +access_mrp_bom_labour_line_read_stock,Read access on mrp.bom.labour.line to Stock user,model_mrp_bom_labour_line,stock.group_stock_user,1,0,0,0 +access_mrp_bom_labour_line_full,Full access on mrp.bom.labour.line to MRP manager,model_mrp_bom_labour_line,mrp.group_mrp_manager,1,1,1,1