Skip to Content
Menu
This question has been flagged

I have set Costing method as average cost and Inventory Valuation as Automated, I created a landed cost the cost changes and I changed the price manually and caused a Stock Valuation Layer and Journal entries has been created,

I want to delete these entries using code how?

Avatar
Discard
Author

i created a button in landed cost to cancel i tried something like this so when i change the product price 2 entries are created one 'stock.valuation.layer' and one Journal entree

stock valuation contatins a description saying Product value manually modified from 1000 to 200

i tried it using like this

if product.cost_method == 'average':
original_price = product.standard_price
new_price = product.standard_price - line.additional_landed_cost
product.write({'standard_price': new_price})
stock_valuation_layer = self.env['stock.valuation.layer'] \
.search([('product_id', '=', product.id),
('description', '=', f'Product value manually '
f'modified (from {original_price} to {new_price})')],
limit=1)
if stock_valuation_layer:
stock_valuation_layer.account_move_id.button_draft()
stock_valuation_layer.account_move_id.sudo().unlink()
stock_valuation_layer.sudo().unlink()

 
This works but  i want to search that stock.valuation.layer instead of seaching using description ,any other way? for this 

self.env['stock.valuation.layer'] \
.search([('product_id', '=', product.id),
('description', '=', f'Product value manually '
f'modified (from {
original_price} to {new_price})')],
limit
=1) or should i keep it like this

ANY IDEAS  

Best Answer

you can achieve it using code. Here's an example of how you can delete these entries programmatically:

# Import necessary models
from odoo import api, models

class StockValuationEntryDeletion(models.Model):
_inherit = 'stock.valuation.layer'

@api.model
def delete_entries(self, product_id):
# Retrieve the Stock Valuation Layer entries for the given product
valuation_entries = self.search([('product_id', '=', product_id)])

# Delete the Stock Valuation Layer entries
valuation_entries.unlink()

class JournalEntryDeletion(models.Model):
_inherit = 'account.move'

@api.model
def delete_entries(self, product_id):
# Retrieve the Journal entries for the given product
journal_entries = self.env['account.move'].search([('stock_valuation_layer_ids.product_id', '=', product_id)])

# Delete the Journal entries
journal_entries.unlink()


You can then use these methods to delete the entries. For example:

product_id = 123 # Replace with the actual product ID
StockValuationEntryDeletion.delete_entries(product_id)
JournalEntryDeletion.delete_entries(product_id)

Make sure you run this code with the necessary access rights and proper validation to ensure the correct entries are deleted. It's recommended to test this code in a development environment first and take proper backups before running it in a production environment.


Avatar
Discard
Related Posts Replies Views Activity
1
Sep 23
2229
1
Apr 24
2529
0
Oct 23
2026
2
Sep 23
3109
0
Jul 23
1907