fix the revaluation bugs

This commit is contained in:
Suherdy Yacob 2026-01-08 13:08:48 +07:00
parent 9c2b160e98
commit 2ab207ccd9
3 changed files with 37 additions and 2 deletions

View File

@ -8,7 +8,7 @@
1. Find discrepancies between Vendor Bills and linked Purchase Orders within a date range. 1. Find discrepancies between Vendor Bills and linked Purchase Orders within a date range.
2. Sync selected Vendor Bills to update the Purchase Orders. 2. Sync selected Vendor Bills to update the Purchase Orders.
""", """,
'author': 'Antigravity', 'author': 'Suherdy Yacob',
'depends': ['purchase', 'account'], 'depends': ['purchase', 'account'],
'data': [ 'data': [
'security/ir.model.access.csv', 'security/ir.model.access.csv',

View File

@ -53,7 +53,6 @@ class PurchaseBillSyncWizard(models.TransientModel):
if line.product_uom_id and po_line.product_uom and line.product_uom_id != po_line.product_uom: if line.product_uom_id and po_line.product_uom and line.product_uom_id != po_line.product_uom:
bill_price_in_po_currency = line.product_uom_id._compute_price(bill_price_in_po_currency, po_line.product_uom) bill_price_in_po_currency = line.product_uom_id._compute_price(bill_price_in_po_currency, po_line.product_uom)
# Check Price
if float_compare(bill_price_in_po_currency, po_line.price_unit, precision_digits=2) != 0: if float_compare(bill_price_in_po_currency, po_line.price_unit, precision_digits=2) != 0:
discrepancies.append(f"Product {line.product_id.name}: Price {bill_price_in_po_currency:.2f} != {po_line.price_unit:.2f}") discrepancies.append(f"Product {line.product_id.name}: Price {bill_price_in_po_currency:.2f} != {po_line.price_unit:.2f}")
@ -163,6 +162,42 @@ class PurchaseBillSyncWizard(models.TransientModel):
new_date = stock_move.date + timedelta(seconds=1) new_date = stock_move.date + timedelta(seconds=1)
self._cr.execute("UPDATE stock_valuation_layer SET create_date = %s WHERE id = %s", (new_date, svl.id)) self._cr.execute("UPDATE stock_valuation_layer SET create_date = %s WHERE id = %s", (new_date, svl.id))
# AVCO/FIFO Logic: Update Standard Price and Distribute Value to Layers
product = stock_move.product_id
if product.categ_id.property_cost_method in ['average', 'fifo'] and product.quantity_svl > 0:
# 1. Update Standard Price
# We use disable_auto_svl to prevent Odoo from creating an extra SVL for this price change
# because we already created the specific adjustment SVL above.
new_std_price = product.standard_price + (diff / product.quantity_svl)
product.with_context(disable_auto_svl=True).sudo().write({'standard_price': new_std_price})
# 2. Distribute Value to Remaining Layers (Crucial for correct COGS later)
remaining_svls = self.env['stock.valuation.layer'].search([
('product_id', '=', product.id),
('remaining_qty', '>', 0),
('company_id', '=', stock_move.company_id.id),
])
if remaining_svls:
remaining_qty_total = sum(remaining_svls.mapped('remaining_qty'))
if remaining_qty_total > 0:
remaining_value_to_distribute = diff
remaining_value_unit_cost = remaining_value_to_distribute / remaining_qty_total
for layer in remaining_svls:
if float_compare(layer.remaining_qty, remaining_qty_total, precision_rounding=product.uom_id.rounding) >= 0:
taken_remaining_value = remaining_value_to_distribute
else:
taken_remaining_value = remaining_value_unit_cost * layer.remaining_qty
# Rounding
taken_remaining_value = stock_move.company_id.currency_id.round(taken_remaining_value)
layer.sudo().write({'remaining_value': layer.remaining_value + taken_remaining_value})
remaining_value_to_distribute -= taken_remaining_value
remaining_qty_total -= layer.remaining_qty
# Handle Accounting Entry if Automated # Handle Accounting Entry if Automated
if stock_move.product_id.categ_id.property_valuation == 'real_time': if stock_move.product_id.categ_id.property_valuation == 'real_time':
accounts = stock_move.product_id.product_tmpl_id.get_product_accounts() accounts = stock_move.product_id.product_tmpl_id.get_product_accounts()