from odoo.tests.common import TransactionCase from odoo import fields from datetime import timedelta class TestStockBackdate(TransactionCase): def setUp(self): super(TestStockBackdate, self).setUp() self.product = self.env['product.product'].create({ 'name': 'Test Product Backdate', 'type': 'product', 'categ_id': self.env.ref('product.product_category_all').id, }) # Enable automated valuation for the category self.product.categ_id.property_valuation = 'real_time' self.product.categ_id.property_cost_method = 'average' self.stock_location = self.env.ref('stock.stock_location_stock') def test_inventory_backdate(self): """Test that backdated inventory adjustment works""" backdate = fields.Datetime.now() - timedelta(days=10) # Create backdated inventory adjustment inventory = self.env['stock.inventory.backdate'].create({ 'backdate_datetime': backdate, 'location_id': self.stock_location.id, }) # Add inventory line line = self.env['stock.inventory.backdate.line'].create({ 'inventory_id': inventory.id, 'product_id': self.product.id, 'theoretical_qty': 0, 'counted_qty': 100, }) # Validate the adjustment inventory.action_validate() # Check stock move date move = self.env['stock.move'].search([ ('product_id', '=', self.product.id), ('is_inventory', '=', True), ('origin', '=', inventory.name) ], limit=1) self.assertTrue(move, "Stock move should be created") self.assertEqual(move.date, backdate, "Stock move date should be backdated") # Check account move date if exists if move.account_move_ids: # Account move date is a Date field, so compare date parts self.assertEqual(move.account_move_ids[0].date, backdate.date(), "Account move date should be backdated") # Check valuation layer date (create_date) svl = self.env['stock.valuation.layer'].search([ ('stock_move_id', '=', move.id) ], limit=1) if svl: # Both are datetime now, so we can compare directly self.assertEqual(svl.create_date, backdate, "SVL create_date should be backdated")