[FIX] stock_account: "Set standard price" button
When a product with costing method = standard price and inventory valuation = perpetual (automated), the button "Set standard price" in General information in "product.template" is shown if the product template has only one variant. In the other case, the button is displayed in the variant view form. opw:668568
This commit is contained in:
@@ -103,6 +103,7 @@ class product_template(osv.osv):
|
||||
accounts.update({'stock_journal': self.categ_id.property_stock_journal or False})
|
||||
return accounts
|
||||
|
||||
# To remove in master because this function is now used on "product.product" model.
|
||||
def do_change_standard_price(self, cr, uid, ids, new_price, context=None):
|
||||
""" Changes the Standard Price of Product and creates an account move accordingly."""
|
||||
location_obj = self.pool.get('stock.location')
|
||||
@@ -164,6 +165,55 @@ class product_product(osv.osv):
|
||||
self.valuation = 'manual_periodic'
|
||||
return {}
|
||||
|
||||
def do_change_standard_price(self, cr, uid, ids, new_price, context=None):
|
||||
""" Changes the Standard Price of Product and creates an account move accordingly."""
|
||||
location_obj = self.pool.get('stock.location')
|
||||
move_obj = self.pool.get('account.move')
|
||||
if context is None:
|
||||
context = {}
|
||||
user_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
|
||||
loc_ids = location_obj.search(cr, uid, [('usage', '=', 'internal'), ('company_id', '=', user_company_id)])
|
||||
for rec_id in ids:
|
||||
for location in location_obj.browse(cr, uid, loc_ids, context=context):
|
||||
c = context.copy()
|
||||
c.update({'location': location.id, 'compute_child': False})
|
||||
product = self.browse(cr, uid, rec_id, context=c)
|
||||
datas = self.pool['product.template'].get_product_accounts(cr, uid, product.product_tmpl_id.id, context=context)
|
||||
diff = product.standard_price - new_price
|
||||
if not diff:
|
||||
raise UserError(_("No difference between standard price and new price!"))
|
||||
qty = product.qty_available
|
||||
if qty:
|
||||
# Accounting Entries
|
||||
amount_diff = abs(diff * qty)
|
||||
if diff * qty > 0:
|
||||
debit_account_id = datas['expense'].id
|
||||
credit_account_id = datas['stock_valuation'].id
|
||||
else:
|
||||
debit_account_id = datas['stock_valuation'].id
|
||||
credit_account_id = datas['expense'].id
|
||||
|
||||
lines = [(0, 0, {'name': _('Standard Price changed'),
|
||||
'account_id': debit_account_id,
|
||||
'debit': amount_diff,
|
||||
'credit': 0,
|
||||
}),
|
||||
(0, 0, {
|
||||
'name': _('Standard Price changed'),
|
||||
'account_id': credit_account_id,
|
||||
'debit': 0,
|
||||
'credit': amount_diff,
|
||||
})]
|
||||
move_vals = {
|
||||
'journal_id': datas['stock_journal'].id,
|
||||
'company_id': location.company_id.id,
|
||||
'line_ids': lines,
|
||||
}
|
||||
move_id = move_obj.create(cr, uid, move_vals, context=context)
|
||||
move_obj.post(cr, uid, [move_id], context=context)
|
||||
self.write(cr, uid, rec_id, {'standard_price': new_price})
|
||||
return True
|
||||
|
||||
class product_category(osv.osv):
|
||||
_inherit = 'product.category'
|
||||
_columns = {
|
||||
|
||||
@@ -41,14 +41,14 @@
|
||||
<div name="standard_price_uom" position="after">
|
||||
<button string="Set standard price" type="action"
|
||||
name="%(action_view_change_standard_price)d"
|
||||
attrs="{'invisible':['|',('valuation','!=', 'real_time'), ('cost_method', 'not in', ['standard'])]}"
|
||||
attrs="{'invisible':['|', '|', ('valuation','!=', 'real_time'), ('cost_method', 'not in', ['standard']), ('product_variant_count', '>', 1)]}"
|
||||
class="oe_link oe_read_only" colspan="2"
|
||||
groups="stock_account.group_inventory_valuation"/>
|
||||
</div>
|
||||
<div name="standard_price_uom" position="after">
|
||||
<button string="Compute from average price" type="action"
|
||||
name="%(action_view_change_standard_price)d"
|
||||
attrs="{'invisible':['|',('valuation','!=', 'real_time'), ('cost_method', 'not in', ['average'])]}"
|
||||
attrs="{'invisible':['|', '|', ('valuation','!=', 'real_time'), ('cost_method', 'not in', ['average']), ('product_variant_count', '>', 1)]}"
|
||||
class="oe_link oe_read_only" colspan="2"
|
||||
groups="stock_account.group_inventory_valuation"/>
|
||||
</div>
|
||||
@@ -66,5 +66,33 @@
|
||||
</data>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="product_variant_easy_edit_view_inherit" model="ir.ui.view">
|
||||
<field name="name">product.product.product.view.form.easy.inherit</field>
|
||||
<field name="model">product.product</field>
|
||||
<field name="inherit_id" ref="product.product_variant_easy_edit_view"/>
|
||||
<field name="arch" type="xml">
|
||||
<data>
|
||||
<field name="standard_price" position="after">
|
||||
<field name="valuation" invisible="1"/>
|
||||
<field name="cost_method" invisible="1"/>
|
||||
</field>
|
||||
<field name="standard_price" position="after">
|
||||
<button string="Set standard price" type="action"
|
||||
name="%(action_view_change_standard_price)d"
|
||||
attrs="{'invisible':['|',('valuation','!=', 'real_time'), ('cost_method', 'not in', ['standard'])]}"
|
||||
class="oe_link oe_read_only" colspan="2"
|
||||
groups="stock_account.group_inventory_valuation"/>
|
||||
</field>
|
||||
<field name="standard_price" position="after">
|
||||
<button string="Compute from average price" type="action"
|
||||
name="%(action_view_change_standard_price)d"
|
||||
attrs="{'invisible':['|',('valuation','!=', 'real_time'), ('cost_method', 'not in', ['average'])]}"
|
||||
class="oe_link oe_read_only" colspan="2"
|
||||
groups="stock_account.group_inventory_valuation"/>
|
||||
</field>
|
||||
</data>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
|
||||
@@ -56,12 +56,12 @@ class change_standard_price(osv.osv_memory):
|
||||
context = {}
|
||||
rec_id = context.get('active_id', False)
|
||||
assert rec_id, _('Active ID is not set in Context.')
|
||||
if context.get("active_model") == 'product.product':
|
||||
prod_obj = self.pool.get('product.product')
|
||||
rec_id = prod_obj.browse(cr, uid, rec_id, context=context).product_tmpl_id.id
|
||||
prod_obj = self.pool.get('product.template')
|
||||
|
||||
res = self.browse(cr, uid, ids, context=context)
|
||||
|
||||
prod_obj.do_change_standard_price(cr, uid, [rec_id], res[0].new_price, context)
|
||||
new_price = self.browse(cr, uid, ids, context=context)[0].new_price
|
||||
if context.get("active_model") == 'product.template':
|
||||
prod_obj = self.pool.get('product.template')
|
||||
rec_ids = prod_obj.browse(cr, uid, rec_id, context=context).product_variant_ids.mapped('id')
|
||||
else:
|
||||
rec_ids = [rec_id]
|
||||
prod_obj = self.pool.get('product.product')
|
||||
prod_obj.do_change_standard_price(cr, uid, rec_ids, new_price, context)
|
||||
return {'type': 'ir.actions.act_window_close'}
|
||||
|
||||
Reference in New Issue
Block a user