feat: update account move lines to use inventory variance account 521301 during backdated adjustments
This commit is contained in:
parent
a4facbbb18
commit
e705b821c6
@ -49,6 +49,7 @@ This module allows you to create backdated inventory adjustments with a specific
|
|||||||
- Stock move line `date` field
|
- Stock move line `date` field
|
||||||
- Stock valuation layer `create_date` field
|
- Stock valuation layer `create_date` field
|
||||||
- Account move `date` field (if real-time valuation is enabled)
|
- 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
|
### Models
|
||||||
|
|
||||||
|
|||||||
@ -408,16 +408,49 @@ class StockInventoryBackdateLine(models.Model):
|
|||||||
# Refresh move to get account_move_ids
|
# Refresh move to get account_move_ids
|
||||||
move = self.env['stock.move'].browse(move.id)
|
move = self.env['stock.move'].browse(move.id)
|
||||||
if move.account_move_ids:
|
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()
|
account_date = backdate.date()
|
||||||
for account_move in move.account_move_ids:
|
for account_move in move.account_move_ids:
|
||||||
|
# Update move date
|
||||||
self.env.cr.execute(
|
self.env.cr.execute(
|
||||||
"UPDATE account_move SET date = %s WHERE id = %s",
|
"UPDATE account_move SET date = %s WHERE id = %s",
|
||||||
(account_date, account_move.id)
|
(account_date, account_move.id)
|
||||||
)
|
)
|
||||||
self.env.cr.execute(
|
|
||||||
"UPDATE account_move_line SET date = %s WHERE move_id = %s",
|
# Update lines date and account if target_account is found
|
||||||
(account_date, account_move.id)
|
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)
|
||||||
|
)
|
||||||
_logger.info(f"Updated account_move {account_move.id} and lines to {account_date}")
|
_logger.info(f"Updated account_move {account_move.id} and lines to {account_date}")
|
||||||
|
|
||||||
# Invalidate cache to ensure ORM reloads data from DB
|
# Invalidate cache to ensure ORM reloads data from DB
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user