From 5a2b38918ad7e6832c3829d46c4ad2f2b0f1ca4d Mon Sep 17 00:00:00 2001 From: Pratima Gupta Date: Mon, 12 Nov 2018 13:29:14 +0000 Subject: [PATCH] [IMP]stock_account: Prevent user to set same valuation account and IN/OUT accounts. In this commit use the python constraint to check the accounts if they are duplicate. This change is because before this configuration there will be an entry cancelling another one in the same account, resulting in a not impacted valuation account. This commit is related to task #1908065. closes odoo/odoo#28604 --- addons/point_of_sale/tests/test_anglo_saxon.py | 3 ++- addons/sale_mrp/tests/test_sale_mrp_flow.py | 3 ++- addons/stock_account/models/product.py | 11 ++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/addons/point_of_sale/tests/test_anglo_saxon.py b/addons/point_of_sale/tests/test_anglo_saxon.py index 8ddc0110063..c4b37211008 100644 --- a/addons/point_of_sale/tests/test_anglo_saxon.py +++ b/addons/point_of_sale/tests/test_anglo_saxon.py @@ -24,12 +24,13 @@ class TestAngloSaxonCommon(common.TransactionCase): self.account = self.env['account.account'].create({'name': 'Receivable', 'code': 'RCV00' , 'user_type_id': account_type_rcv.id, 'reconcile': True}) account_expense = self.env['account.account'].create({'name': 'Expense', 'code': 'EXP00' , 'user_type_id': account_type_oth.id, 'reconcile': True}) account_output = self.env['account.account'].create({'name': 'Output', 'code': 'OUT00' , 'user_type_id': account_type_oth.id, 'reconcile': True}) + account_valuation = self.env['account.account'].create({'name': 'Valuation', 'code': 'STV00', 'user_type_id': account_type_oth.id, 'reconcile': True}) self.partner.property_account_receivable_id = self.account self.category.property_account_income_categ_id = self.account self.category.property_account_expense_categ_id = account_expense self.category.property_stock_account_input_categ_id = self.account self.category.property_stock_account_output_categ_id = account_output - self.category.property_stock_valuation_account_id = self.account + self.category.property_stock_valuation_account_id = account_valuation self.category.property_stock_journal = self.env['account.journal'].create({'name': 'Stock journal', 'type': 'sale', 'code': 'STK00'}) self.pos_config = self.env.ref('point_of_sale.pos_config_main') self.pos_config = self.pos_config.copy({'name': 'New POS config'}) diff --git a/addons/sale_mrp/tests/test_sale_mrp_flow.py b/addons/sale_mrp/tests/test_sale_mrp_flow.py index 5e77a99ab45..51c18ca7283 100644 --- a/addons/sale_mrp/tests/test_sale_mrp_flow.py +++ b/addons/sale_mrp/tests/test_sale_mrp_flow.py @@ -405,12 +405,13 @@ class TestSaleMrpFlow(common.TransactionCase): self.account_receiv = self.env['account.account'].create({'name': 'Receivable', 'code': 'RCV00' , 'user_type_id': account_type.id, 'reconcile': True}) account_expense = self.env['account.account'].create({'name': 'Expense', 'code': 'EXP00' , 'user_type_id': account_type.id, 'reconcile': True}) account_output = self.env['account.account'].create({'name': 'Output', 'code': 'OUT00' , 'user_type_id': account_type.id, 'reconcile': True}) + account_valuation = self.env['account.account'].create({'name': 'Valuation', 'code': 'STV00' , 'user_type_id': account_type.id, 'reconcile': True}) self.partner.property_account_receivable_id = self.account_receiv self.category.property_account_income_categ_id = self.account_receiv self.category.property_account_expense_categ_id = account_expense self.category.property_stock_account_input_categ_id = self.account_receiv self.category.property_stock_account_output_categ_id = account_output - self.category.property_stock_valuation_account_id = self.account_receiv + self.category.property_stock_valuation_account_id = account_valuation self.category.property_stock_journal = self.env['account.journal'].create({'name': 'Stock journal', 'type': 'sale', 'code': 'STK00'}) Product = self.env['product.product'] diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index c8817b777ef..81fa093ccd5 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -5,7 +5,7 @@ from odoo import api, fields, models, tools, _ from odoo.exceptions import UserError from odoo.tools import float_is_zero, pycompat from odoo.addons import decimal_precision as dp - +from odoo.exceptions import ValidationError class ProductTemplate(models.Model): @@ -394,6 +394,15 @@ class ProductCategory(models.Model): domain=[('deprecated', '=', False)], help="When real-time inventory valuation is enabled on a product, this account will hold the current value of the products.",) + @api.constrains('property_stock_valuation_account_id', 'property_stock_account_output_categ_id', 'property_stock_account_input_categ_id') + def _check_valuation_accouts(self): + # Prevent to set the valuation account as the input or output account. + for category in self: + valuation_account = category.property_stock_valuation_account_id + input_and_output_accounts = category.property_stock_account_input_categ_id | category.property_stock_account_output_categ_id + if valuation_account and valuation_account in input_and_output_accounts: + raise ValidationError(_('The Stock Input and/or Output accounts cannot be the same than the Stock Valuation account.')) + @api.onchange('property_cost_method') def onchange_property_valuation(self): if not self._origin: