from odoo.tests import TransactionCase, tagged from odoo.exceptions import UserError from datetime import date, timedelta @tagged('post_install', '-at_install') class TestReproduction(TransactionCase): def setUp(self): super().setUp() self.vendor = self.env['res.partner'].create({'name': 'Test Vendor'}) self.stock_journal = self.env['account.journal'].search([('type', '=', 'general')], limit=1) # Create a product with AVCO/Automated valuation self.product_category = self.env['product.category'].create({ 'name': 'Test Category', 'property_cost_method': 'average', 'property_valuation': 'real_time', }) self.product = self.env['product.product'].create({ 'name': 'Test Product', 'type': 'product', 'categ_id': self.product_category.id, 'standard_price': 10.0, }) # Ensure accounts are set on category (simplified for test) account_type = self.env.ref('account.data_account_type_expenses') self.account_expense = self.env['account.account'].create({ 'name': 'Expense', 'code': 'EXP001', 'account_type': account_type.id, 'reconcile': True, }) self.account_valuation = self.env['account.account'].create({ 'name': 'Stock Valuation', 'code': 'STK001', 'account_type': account_type.id, 'reconcile': True, }) self.product_category.property_stock_account_input_categ_id = self.account_expense self.product_category.property_stock_account_output_categ_id = self.account_expense self.product_category.property_stock_valuation_account_id = self.account_valuation self.product_category.property_stock_journal = self.stock_journal def test_revaluation_journal_creation(self): # 1. Create PO po = self.env['purchase.order'].create({ 'partner_id': self.vendor.id, 'order_line': [(0, 0, { 'product_id': self.product.id, 'product_qty': 10.0, 'price_unit': 10.0, # $100 total })] }) po.button_confirm() # 2. Receive Goods picking = po.picking_ids picking.button_validate() # 3. Create Bill move_form = self.env['account.move'].create({ 'move_type': 'in_invoice', 'partner_id': self.vendor.id, 'invoice_date': date.today(), 'purchase_vendor_bill_id': po.id, 'line_ids': [(0, 0, { 'product_id': self.product.id, 'quantity': 10.0, 'price_unit': 10.0, 'purchase_line_id': po.order_line.id })] }) # Simulate linking causing sync? Actually just creating the line linked to PO line should be enough # But for test, we can just create the line manually linked to PO line as above bill_line = move_form.line_ids.filtered(lambda l: l.product_id == self.product) # 4. Modify Price (Trigger the issue) # Edit price to 12.0 bill_line.with_context(active_model='account.move').write({'price_unit': 12.0}) # 5. Check for Revaluation Journal # Search for account.move with 'Revaluation for' in ref revaluation_moves = self.env['account.move'].search([ ('ref', 'like', 'Revaluation for %s from Bill Edit' % self.product.name) ]) # Assert that it exists (Current bad behavior) self.assertEqual(len(revaluation_moves), 0, "Revaluation journal should NOT exist after fix") # 6. Check for Stock Valuation Layer svls = self.env['stock.valuation.layer'].search([ ('description', 'like', 'Valuation correction from Vendor Bill %') ]) self.assertEqual(len(svls), 0, "Stock Valuation Layer should NOT be created")