diff --git a/README.md b/README.md index 55844bf..646eed0 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/models/stock_inventory_backdate.py b/models/stock_inventory_backdate.py index ffc4868..c2800df 100755 --- a/models/stock_inventory_backdate.py +++ b/models/stock_inventory_backdate.py @@ -408,16 +408,49 @@ 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) ) - self.env.cr.execute( - "UPDATE account_move_line SET date = %s WHERE move_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) + ) _logger.info(f"Updated account_move {account_move.id} and lines to {account_date}") # Invalidate cache to ensure ORM reloads data from DB