feat: Update author and enhance account move sequence generation to correct date mismatches in inventory revaluation.

This commit is contained in:
Suherdy Yacob 2025-12-29 09:44:59 +07:00
parent f2490ffcb8
commit e296df45d9
2 changed files with 56 additions and 3 deletions

View File

@ -8,8 +8,7 @@
Inventory Revaluation Odoo App provides a solution for businesses to reassess and recalculate the value of their stock. Inventory Revaluation Odoo App provides a solution for businesses to reassess and recalculate the value of their stock.
It allows creating backdate inventory adjustment and revaluation. It allows creating backdate inventory adjustment and revaluation.
""", """,
'author': "Antigravity", 'author': "Suherdy Yacob",
'website': "https://www.mapan.co.id",
'category': 'Inventory/Inventory', 'category': 'Inventory/Inventory',
'version': '17.0.1.0.0', 'version': '17.0.1.0.0',
'depends': ['stock_account'], 'depends': ['stock_account'],

View File

@ -1,6 +1,8 @@
from odoo import models, fields, api, _ from odoo import models, fields, api, _
import logging
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tools import float_compare, float_is_zero from odoo.tools import float_compare, float_is_zero
from datetime import timedelta
class StockInventoryRevaluation(models.Model): class StockInventoryRevaluation(models.Model):
_name = 'stock.inventory.revaluation' _name = 'stock.inventory.revaluation'
@ -59,7 +61,59 @@ class StockInventoryRevaluation(models.Model):
# Create Accounting Entry # Create Accounting Entry
move_vals = self._prepare_account_move_vals() move_vals = self._prepare_account_move_vals()
move = self.env['account.move'].create(move_vals) move = self.env['account.move'].with_context(default_date=self.date.date()).create(move_vals)
# Check and fix sequence date mismatch
if move.name == '/' and not move.posted_before:
move._set_next_sequence()
if move.name and move.date:
move_date = move.date
expected_prefix = move_date.strftime('%Y/%m')
# If the sequence doesn't contain the expected Year/Month (e.g. 2025/11)
# We strictly enforce that 2025/11 is in the name if date is Nov 2025
if expected_prefix not in move.name:
journal_id = move.journal_id.id
date_start = move_date.replace(day=1)
# Calculate end of month
next_month = move_date.replace(day=28) + timedelta(days=4)
date_end = next_month - timedelta(days=next_month.day)
# correct period query
last_move = self.env['account.move'].search([
('journal_id', '=', journal_id),
('name', '!=', '/'),
('date', '>=', date_start),
('date', '<=', date_end),
('company_id', '=', move.company_id.id),
('name', 'like', f"%{expected_prefix}%")
], order='sequence_number desc', limit=1)
new_seq = 1
prefix = ""
if last_move and last_move.name:
# Try to parse the sequence number from the end
parts = last_move.name.split('/')
if len(parts) >= 2 and parts[-1].isdigit():
new_seq = int(parts[-1]) + 1
prefix = "/".join(parts[:-1]) + "/"
else:
# Construct prefix from the current (wrong) name but replacing the date part
# Assuming format PREFIX/YEAR/MONTH/SEQ
parts = move.name.split('/')
if len(parts) >= 3:
# Attempt to reconstruct: STJ/2025/12/XXXX -> STJ/2025/11/
# We know move_date.year and move_date.month
# Let's try to preserve the prefix (index 0)
prefix_code = parts[0]
prefix = f"{prefix_code}/{move_date.year}/{move_date.month:02d}/"
if prefix:
new_name = f"{prefix}{new_seq:04d}"
move.write({'name': new_name})
move.action_post() move.action_post()
# Create Stock Valuation Layer # Create Stock Valuation Layer