diff --git a/addons/mrp_account/tests/test_mrp_account.py b/addons/mrp_account/tests/test_mrp_account.py index 0936dd9bb79d32099a74909852a5001272e7694c..c2ee573bcd7e63e99731f9f72dea98495f4f53d6 100644 --- a/addons/mrp_account/tests/test_mrp_account.py +++ b/addons/mrp_account/tests/test_mrp_account.py @@ -259,7 +259,7 @@ class TestMrpAccountMove(TestAccountMove): "name": "Product B", "type": "product", "default_code": "prda", - "categ_id": cls.env.ref("product.product_category_all").id, + "categ_id": cls.auto_categ.id, "taxes_id": [(5, 0, 0)], "supplier_taxes_id": [(5, 0, 0)], "lst_price": 100.0, diff --git a/addons/stock_account/__init__.py b/addons/stock_account/__init__.py index 99d699ac408b3ee73aa7918f68076670e07326ca..e63a5e638fe50463efb5d79d585d6d713eb7c6e3 100644 --- a/addons/stock_account/__init__.py +++ b/addons/stock_account/__init__.py @@ -20,6 +20,7 @@ def _configure_journals(cr, registry): 'property_stock_valuation_account_id', ] # Property Stock Accounts + categ_values = {category.id: False for category in env['product.category'].search([])} for company_id in company_ids: # Check if property exists for stock account journal exists field = env['ir.model.fields']._get("product.category", "property_stock_journal") @@ -57,10 +58,4 @@ def _configure_journals(cr, registry): account, company_id, ) - for name in todo_list: - env['ir.property']._set_multi( - name, - 'product.category', - {category.id: False for category in env['product.category'].search([])}, - True - ) + env['ir.property'].with_company(company_id.id)._set_multi(name, 'product.category', categ_values, True) diff --git a/addons/stock_account/models/account_chart_template.py b/addons/stock_account/models/account_chart_template.py index 8f6105d09bb72959cf57fa448b69ac9859b6a813..5057b742796193e02ed5127b5ccbed2360a74bf9 100644 --- a/addons/stock_account/models/account_chart_template.py +++ b/addons/stock_account/models/account_chart_template.py @@ -27,9 +27,11 @@ class AccountChartTemplate(models.Model): 'property_stock_account_output_categ_id', 'property_stock_valuation_account_id', ] + categ_values = {category.id: False for category in self.env['product.category'].search([])} for field in todo_list: account = self[field] value = acc_template_ref[account.id] if account else False PropertyObj._set_default(field, "product.category", value, company) + PropertyObj._set_multi(field, "product.category", categ_values, True) return res diff --git a/addons/stock_account/tests/test_account_move.py b/addons/stock_account/tests/test_account_move.py index 7b2607004c5d051787e4e778dcda9115697a8446..ef5a027df1856324a7cc7d6b7f76265b7bab5a50 100644 --- a/addons/stock_account/tests/test_account_move.py +++ b/addons/stock_account/tests/test_account_move.py @@ -4,6 +4,7 @@ from odoo.addons.account.tests.common import AccountTestInvoicingCommon from odoo.addons.stock_account.tests.test_stockvaluation import _create_accounting_data from odoo.tests.common import tagged, Form +from odoo import fields @tagged("post_install", "-at_install") @@ -20,12 +21,25 @@ class TestAccountMove(AccountTestInvoicingCommon): cls.stock_journal, ) = _create_accounting_data(cls.env) + # `all_categ` should not be altered, so we can test the `post_init` hook of `stock_account` + cls.all_categ = cls.env.ref('product.product_category_all') + + cls.auto_categ = cls.env['product.category'].create({ + 'name': 'child_category', + 'parent_id': cls.all_categ.id, + "property_stock_account_input_categ_id": cls.stock_input_account.id, + "property_stock_account_output_categ_id": cls.stock_output_account.id, + "property_stock_valuation_account_id": cls.stock_valuation_account.id, + "property_stock_journal": cls.stock_journal.id, + "property_valuation": "real_time", + "property_cost_method": "standard", + }) cls.product_A = cls.env["product.product"].create( { "name": "Product A", "type": "product", "default_code": "prda", - "categ_id": cls.env.ref("product.product_category_all").id, + "categ_id": cls.auto_categ.id, "taxes_id": [(5, 0, 0)], "supplier_taxes_id": [(5, 0, 0)], "lst_price": 100.0, @@ -34,16 +48,6 @@ class TestAccountMove(AccountTestInvoicingCommon): "property_account_expense_id": cls.company_data["default_account_expense"].id, } ) - cls.product_A.categ_id.write( - { - "property_stock_account_input_categ_id": cls.stock_input_account.id, - "property_stock_account_output_categ_id": cls.stock_output_account.id, - "property_stock_valuation_account_id": cls.stock_valuation_account.id, - "property_stock_journal": cls.stock_journal.id, - "property_valuation": "real_time", - "property_cost_method": "standard", - } - ) def test_standard_perpetual_01_mc_01(self): rate = self.currency_data["rates"].sorted()[0].rate @@ -118,3 +122,33 @@ class TestAccountMove(AccountTestInvoicingCommon): self.assertEqual(len(invoice.mapped("line_ids")), 4) self.assertEqual(len(invoice.mapped("line_ids").filtered("is_anglo_saxon_line")), 2) self.assertEqual(len(invoice.mapped("line_ids.currency_id")), 2) + + def test_basic_bill(self): + """ + When billing a storable product with a basic category (manual + valuation), the account used should be the expenses one. This test + checks the flow with two companies: + - One that existed before the installation of `stock_account` (to test + the post-install hook) + - One created after the module installation + """ + first_company = self.env['res.company'].browse(1) + self.env.user.company_ids |= first_company + basic_product = self.env['product.product'].create({ + 'name': 'SuperProduct', + 'type': 'product', + 'categ_id': self.all_categ.id, + }) + + for company in (self.env.company | first_company): + bill_form = Form(self.env['account.move'].with_company(company.id).with_context(default_move_type='in_invoice')) + bill_form.partner_id = self.partner_a + bill_form.invoice_date = fields.Date.today() + with bill_form.invoice_line_ids.new() as line: + line.product_id = basic_product + line.price_unit = 100 + bill = bill_form.save() + bill.action_post() + + product_accounts = basic_product.product_tmpl_id.with_company(company.id).get_product_accounts() + self.assertEqual(bill.invoice_line_ids.account_id, product_accounts['expense']) diff --git a/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py b/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py index f2f377967f89ff9c3bcb0a0c6dc5b934270ab1ff..4a3bce7872f41d43c753f2b7d321cf153099ac3d 100644 --- a/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py +++ b/addons/stock_landed_costs/tests/test_stock_landed_costs_purchase.py @@ -69,6 +69,8 @@ class TestLandedCosts(TestStockLandedCostsCommon): # 3.transportation 250 By Weight # 4.packaging 20 By Volume + self.landed_cost.categ_id.property_valuation = 'real_time' + # Process incoming shipment income_ship = self._process_incoming_shipment() # Create landed costs @@ -181,6 +183,8 @@ class TestLandedCosts(TestStockLandedCostsCommon): # 3.transportation -50 By Weight # 4.packaging -5 By Volume + self.landed_cost.categ_id.property_valuation = 'real_time' + # Process incoming shipment income_ship = self._process_incoming_shipment() # Refrigerator outgoing shipment.