feat: update account move lines to use inventory variance account 521301 during backdated adjustments

This commit is contained in:
Suherdy Yacob 2026-03-31 12:16:40 +07:00
parent a4facbbb18
commit e705b821c6
2 changed files with 38 additions and 4 deletions

View File

@ -49,6 +49,7 @@ This module allows you to create backdated inventory adjustments with a specific
- Stock move line `date` field
- Stock valuation layer `create_date` field
- Account move `date` field (if real-time valuation is enabled)
- Account move `account_id` field: Overrides the interim account with `521301 Selisih Persediaan` for the non-valuation side of the entry.
### Models

View File

@ -408,12 +408,45 @@ class StockInventoryBackdateLine(models.Model):
# Refresh move to get account_move_ids
move = self.env['stock.move'].browse(move.id)
if move.account_move_ids:
# Find target account 521301 (Selisih Persediaan)
target_account = self.env['account.account'].search([
('code', '=', '521301'),
('company_id', '=', self.inventory_id.company_id.id)
], limit=1)
account_date = backdate.date()
for account_move in move.account_move_ids:
# Update move date
self.env.cr.execute(
"UPDATE account_move SET date = %s WHERE id = %s",
(account_date, account_move.id)
)
# Update lines date and account if target_account is found
if target_account:
# Get valuation account for this product's category to identify which line to update
# For inventory adjustments, we want to update the non-valuation line
valuation_account = product.categ_id.property_stock_valuation_account_id
if valuation_account:
# Update the line that is NOT the stock valuation account
self.env.cr.execute(
"""UPDATE account_move_line
SET date = %s, account_id = %s
WHERE move_id = %s AND account_id != %s""",
(account_date, target_account.id, account_move.id, valuation_account.id)
)
_logger.info(f"Updated account_move {account_move.id} lines with account 521301")
else:
# Fallback: just update dates if valuation account is not found (though it should be)
_logger.warning(f"Valuation account not found for product {product.name} (categ_id: {product.categ_id.id}). Only updating dates.")
self.env.cr.execute(
"UPDATE account_move_line SET date = %s WHERE move_id = %s",
(account_date, account_move.id)
)
else:
# No target account found, just update dates
_logger.warning("Target account 521301 not found. Only updating dates for account move lines.")
self.env.cr.execute(
"UPDATE account_move_line SET date = %s WHERE move_id = %s",
(account_date, account_move.id)