From 6dab81a65b992e2c11b71847b1da2f5f2b5c5d5a Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 13 Apr 2016 22:41:08 +0200 Subject: [PATCH] Add possibility to make a manual update of standard_price from BOM Add script to automate the update of standard_price for phantom BOM Add user_id on price.history --- mrp_average_cost/mrp.py | 37 +++++++++++++++++++++++++++++++++++ mrp_average_cost/mrp_data.xml | 14 +++++++++++++ mrp_average_cost/mrp_view.xml | 10 ++++++++-- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/mrp_average_cost/mrp.py b/mrp_average_cost/mrp.py index 9f95e98..4311ac5 100644 --- a/mrp_average_cost/mrp.py +++ b/mrp_average_cost/mrp.py @@ -23,6 +23,7 @@ from openerp.osv import orm, fields from openerp.tools.translate import _ import openerp.addons.decimal_precision as dp +from openerp.tools import float_compare, float_is_zero import logging logger = logging.getLogger(__name__) @@ -98,6 +99,7 @@ class MrpBom(orm.Model): 'mrp.bom.labour.line', 'bom_id', 'Labour Lines'), 'total_labour_cost': fields.function( _compute_total_labour_cost, type='float', readonly=True, + digits_compute=dp.get_precision('Product Price'), string="Total Labour Cost", store={ 'labour.cost.profile': (_get_bom_from_cost_profile, [ 'hour_cost', 'company_id'], 10), @@ -114,10 +116,12 @@ class MrpBom(orm.Model): "the BOM"), 'total_components_cost': fields.function( _compute_total_cost, type='float', readonly=True, + digits_compute=dp.get_precision('Product Price'), multi='total-cost', string='Total Components Cost'), 'total_cost': fields.function( _compute_total_cost, type='float', readonly=True, multi='total-cost', string='Total Cost', + digits_compute=dp.get_precision('Product Price'), help="Total Cost = Total Components Cost + " "Total Labour Cost + Extra Cost"), 'company_currency_id': fields.related( @@ -129,6 +133,39 @@ class MrpBom(orm.Model): type='float', string='Standard Price') } + def manual_update_product_standard_price(self, cr, uid, ids, context=None): + if context is None: + context = {} + ctx = context.copy() + if 'product_price_history_origin' not in ctx: + ctx['product_price_history_origin'] = u'Manual update from BOM' + precision = self.pool['decimal.precision'].precision_get( + cr, uid, 'Product Price') + for bom in self.browse(cr, uid, ids, context=context): + if not bom.product_id: + continue + if float_compare( + bom.product_id.standard_price, bom.total_cost, + precision_digits=precision): + bom.product_id.write( + {'standard_price': bom.total_cost}, context=ctx) + logger.info( + 'Cost price updated to %s on product %s', + bom.total_cost, bom.product_id.name_get()[0][1]) + return True + + def _phantom_update_product_standard_price(self, cr, uid, context=None): + if context is None: + context = {} + ctx = context.copy() + ctx['product_price_history_origin'] = 'Automatic update of Phantom BOMs' + mbo = self.pool['mrp.bom'] + bom_ids = mbo.search( + cr, uid, [('type', '=', 'phantom')], context=context) + self.manual_update_product_standard_price( + cr, uid, bom_ids, context=ctx) + return True + class LabourCostProfile(orm.Model): _name = 'labour.cost.profile' diff --git a/mrp_average_cost/mrp_data.xml b/mrp_average_cost/mrp_data.xml index 2ca76b0..789cd4e 100644 --- a/mrp_average_cost/mrp_data.xml +++ b/mrp_average_cost/mrp_data.xml @@ -7,5 +7,19 @@ 3 + + + Update Cost Price of products with Phantom BOM + + + 1 + days + -1 + + + + + + diff --git a/mrp_average_cost/mrp_view.xml b/mrp_average_cost/mrp_view.xml index 2a52306..3bfb0af 100644 --- a/mrp_average_cost/mrp_view.xml +++ b/mrp_average_cost/mrp_view.xml @@ -20,8 +20,14 @@ options="{'currency_field': 'company_currency_id'}"/> - +