diff --git a/__manifest__.py b/__manifest__.py index 93ed414..35bb78e 100755 --- a/__manifest__.py +++ b/__manifest__.py @@ -8,8 +8,7 @@ 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. """, - 'author': "Antigravity", - 'website': "https://www.mapan.co.id", + 'author': "Suherdy Yacob", 'category': 'Inventory/Inventory', 'version': '17.0.1.0.0', 'depends': ['stock_account'], diff --git a/models/stock_inventory_revaluation.py b/models/stock_inventory_revaluation.py index 32be72a..6f98f9f 100755 --- a/models/stock_inventory_revaluation.py +++ b/models/stock_inventory_revaluation.py @@ -1,6 +1,8 @@ from odoo import models, fields, api, _ +import logging from odoo.exceptions import UserError from odoo.tools import float_compare, float_is_zero +from datetime import timedelta class StockInventoryRevaluation(models.Model): _name = 'stock.inventory.revaluation' @@ -59,7 +61,59 @@ class StockInventoryRevaluation(models.Model): # Create Accounting Entry 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() # Create Stock Valuation Layer