stock_inventory_backdate/tests/test_stock_backdate.py
2025-12-06 19:03:29 +07:00

60 lines
2.3 KiB
Python

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 if needed,
# but for simplicity we test the move date primarily.
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 inventory adjustment backdating works"""
backdate = fields.Date.today() - timedelta(days=10)
quant = self.env['stock.quant'].create({
'product_id': self.product.id,
'location_id': self.stock_location.id,
'inventory_quantity': 100,
})
# Set forced dates
quant.force_inventory_date = backdate
quant.force_valuation_date = backdate
# Apply inventory
quant.action_apply_inventory()
# Check stock move date
move = self.env['stock.move'].search([
('product_id', '=', self.product.id),
('is_inventory', '=', True)
], limit=1)
self.assertTrue(move, "Stock move should be created")
self.assertEqual(move.date.date(), backdate, "Stock move date should be backdated")
# Check account move date if exists
if move.account_move_ids:
self.assertEqual(move.account_move_ids[0].date, backdate, "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:
# create_date is datetime, backdate is date.
# We check if the date part matches.
self.assertEqual(svl.create_date.date(), backdate, "SVL create_date should be backdated")