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
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
from openerp.osv import orm, fields
|
from openerp.osv import orm, fields
|
||||||
from openerp.tools.translate import _
|
from openerp.tools.translate import _
|
||||||
import openerp.addons.decimal_precision as dp
|
import openerp.addons.decimal_precision as dp
|
||||||
|
from openerp.tools import float_compare, float_is_zero
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -98,6 +99,7 @@ class MrpBom(orm.Model):
|
|||||||
'mrp.bom.labour.line', 'bom_id', 'Labour Lines'),
|
'mrp.bom.labour.line', 'bom_id', 'Labour Lines'),
|
||||||
'total_labour_cost': fields.function(
|
'total_labour_cost': fields.function(
|
||||||
_compute_total_labour_cost, type='float', readonly=True,
|
_compute_total_labour_cost, type='float', readonly=True,
|
||||||
|
digits_compute=dp.get_precision('Product Price'),
|
||||||
string="Total Labour Cost", store={
|
string="Total Labour Cost", store={
|
||||||
'labour.cost.profile': (_get_bom_from_cost_profile, [
|
'labour.cost.profile': (_get_bom_from_cost_profile, [
|
||||||
'hour_cost', 'company_id'], 10),
|
'hour_cost', 'company_id'], 10),
|
||||||
@@ -114,10 +116,12 @@ class MrpBom(orm.Model):
|
|||||||
"the BOM"),
|
"the BOM"),
|
||||||
'total_components_cost': fields.function(
|
'total_components_cost': fields.function(
|
||||||
_compute_total_cost, type='float', readonly=True,
|
_compute_total_cost, type='float', readonly=True,
|
||||||
|
digits_compute=dp.get_precision('Product Price'),
|
||||||
multi='total-cost', string='Total Components Cost'),
|
multi='total-cost', string='Total Components Cost'),
|
||||||
'total_cost': fields.function(
|
'total_cost': fields.function(
|
||||||
_compute_total_cost, type='float', readonly=True,
|
_compute_total_cost, type='float', readonly=True,
|
||||||
multi='total-cost', string='Total Cost',
|
multi='total-cost', string='Total Cost',
|
||||||
|
digits_compute=dp.get_precision('Product Price'),
|
||||||
help="Total Cost = Total Components Cost + "
|
help="Total Cost = Total Components Cost + "
|
||||||
"Total Labour Cost + Extra Cost"),
|
"Total Labour Cost + Extra Cost"),
|
||||||
'company_currency_id': fields.related(
|
'company_currency_id': fields.related(
|
||||||
@@ -129,6 +133,39 @@ class MrpBom(orm.Model):
|
|||||||
type='float', string='Standard Price')
|
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):
|
class LabourCostProfile(orm.Model):
|
||||||
_name = 'labour.cost.profile'
|
_name = 'labour.cost.profile'
|
||||||
|
|||||||
@@ -7,5 +7,19 @@
|
|||||||
<field name="digits">3</field>
|
<field name="digits">3</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
|
||||||
|
<record id="phantom_update_product_standard_price" model="ir.cron">
|
||||||
|
<field name="name">Update Cost Price of products with Phantom BOM</field>
|
||||||
|
<field name="active" eval="False"/>
|
||||||
|
<field name="user_id" ref="base.user_root"/>
|
||||||
|
<field name="interval_number">1</field>
|
||||||
|
<field name="interval_type">days</field>
|
||||||
|
<field name="numbercall">-1</field> <!-- don't limit the number of calls -->
|
||||||
|
<field name="doall" eval="False"/>
|
||||||
|
<field name="model" eval="'mrp.bom'"/>
|
||||||
|
<field name="function" eval="'_phantom_update_product_standard_price'"/>
|
||||||
|
<field name="args" eval="'()'"/>
|
||||||
|
</record>
|
||||||
|
|
||||||
</data>
|
</data>
|
||||||
</openerp>
|
</openerp>
|
||||||
|
|||||||
@@ -20,8 +20,14 @@
|
|||||||
options="{'currency_field': 'company_currency_id'}"/>
|
options="{'currency_field': 'company_currency_id'}"/>
|
||||||
<field name="extra_cost" widget="monetary"
|
<field name="extra_cost" widget="monetary"
|
||||||
options="{'currency_field': 'company_currency_id'}"/>
|
options="{'currency_field': 'company_currency_id'}"/>
|
||||||
<field name="total_cost" widget="monetary"
|
<label for="total_cost"/>
|
||||||
options="{'currency_field': 'company_currency_id'}"/>
|
<div>
|
||||||
|
<field name="total_cost" widget="monetary"
|
||||||
|
options="{'currency_field': 'company_currency_id'}"
|
||||||
|
class="oe_inline"/>
|
||||||
|
<button type="object" name="manual_update_product_standard_price"
|
||||||
|
string="Update Cost Price of Product" class="oe_link"/>
|
||||||
|
</div>
|
||||||
<field name="company_currency_id" invisible="1"/>
|
<field name="company_currency_id" invisible="1"/>
|
||||||
</xpath>
|
</xpath>
|
||||||
<page string="Components" position="after">
|
<page string="Components" position="after">
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ class ProductTemplate(orm.Model):
|
|||||||
'cost': value,
|
'cost': value,
|
||||||
'company_id': company_id,
|
'company_id': company_id,
|
||||||
'origin': context.get('product_price_history_origin', False),
|
'origin': context.get('product_price_history_origin', False),
|
||||||
|
'user_id': uid,
|
||||||
}, context=context)
|
}, context=context)
|
||||||
|
|
||||||
def create(self, cr, uid, vals, context=None):
|
def create(self, cr, uid, vals, context=None):
|
||||||
@@ -107,6 +108,8 @@ class ProductPriceHistory(orm.Model):
|
|||||||
digits_compute=dp.get_precision('Product Price')),
|
digits_compute=dp.get_precision('Product Price')),
|
||||||
# the 'origin' field is not in v8, it's an idea of mine !
|
# the 'origin' field is not in v8, it's an idea of mine !
|
||||||
'origin': fields.char('Origin'),
|
'origin': fields.char('Origin'),
|
||||||
|
# fields used as a remplacement of create_uid
|
||||||
|
'user_id': fields.many2one('res.users', 'Created by'),
|
||||||
}
|
}
|
||||||
|
|
||||||
def _get_default_company(self, cr, uid, context=None):
|
def _get_default_company(self, cr, uid, context=None):
|
||||||
@@ -120,4 +123,5 @@ class ProductPriceHistory(orm.Model):
|
|||||||
_defaults = {
|
_defaults = {
|
||||||
'datetime': fields.datetime.now,
|
'datetime': fields.datetime.now,
|
||||||
'company_id': _get_default_company,
|
'company_id': _get_default_company,
|
||||||
|
'user_id': lambda self, cr, uid, ctx: uid,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
<field name="datetime"/>
|
<field name="datetime"/>
|
||||||
<field name="cost"/>
|
<field name="cost"/>
|
||||||
<field name="origin"/>
|
<field name="origin"/>
|
||||||
|
<field name="user_id"/>
|
||||||
<field name="company_id" groups="base.group_multi_company"/>
|
<field name="company_id" groups="base.group_multi_company"/>
|
||||||
</group>
|
</group>
|
||||||
</form>
|
</form>
|
||||||
@@ -35,6 +36,7 @@
|
|||||||
<field name="datetime"/>
|
<field name="datetime"/>
|
||||||
<field name="cost"/>
|
<field name="cost"/>
|
||||||
<field name="origin"/>
|
<field name="origin"/>
|
||||||
|
<field name="user_id"/>
|
||||||
<field name="company_id" groups="base.group_multi_company"/>
|
<field name="company_id" groups="base.group_multi_company"/>
|
||||||
</tree>
|
</tree>
|
||||||
</field>
|
</field>
|
||||||
|
|||||||
Reference in New Issue
Block a user