Last active
March 6, 2024 17:23
-
-
Save amoyaux/279aee13eaddacbddb435dafbc0a6295 to your computer and use it in GitHub Desktop.
quant_check_health.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
quants = env['stock.quant'].search([]) | |
move_line_ids = [] | |
warning = '' | |
for quant in quants: | |
move_lines = env["stock.move.line"].search([ | |
('product_id', '=', quant.product_id.id), | |
('location_id', '=', quant.location_id.id), | |
('lot_id', '=', quant.lot_id.id), | |
('package_id', '=', quant.package_id.id), | |
('owner_id', '=', quant.owner_id.id), | |
('product_qty', '!=', 0) | |
]) | |
move_line_ids += move_lines.ids | |
reserved_on_move_lines = sum(move_lines.mapped('product_qty')) | |
move_line_str = str.join(', ', [str(move_line_id) for move_line_id in move_lines.ids]) | |
if quant.location_id.should_bypass_reservation(): | |
# If a quant is in a location that should bypass the reservation, its `reserved_quantity` field | |
# should be 0. | |
if quant.reserved_quantity != 0: | |
warning += "Problematic quant found: %s (quantity: %s, reserved_quantity: %s)\n" % (quant.id, quant.quantity, quant.reserved_quantity) | |
warning += "its `reserved_quantity` field is not 0 while its location should bypass the reservation\n" | |
if move_lines: | |
warning += "These move lines are reserved on it: %s (sum of the reservation: %s)\n" % (move_line_str, reserved_on_move_lines) | |
else: | |
warning += "no move lines are reserved on it, you can safely reset its `reserved_quantity` to 0\n" | |
warning += '******************\n' | |
else: | |
# If a quant is in a reservable location, its `reserved_quantity` should be exactly the sum | |
# of the `product_qty` of all the partially_available / assigned move lines with the same | |
# characteristics. | |
if quant.reserved_quantity == 0: | |
if move_lines: | |
warning += "Problematic quant found: %s (quantity: %s, reserved_quantity: %s)\n" % (quant.id, quant.quantity, quant.reserved_quantity) | |
warning += "its `reserved_quantity` field is 0 while these move lines are reserved on it: %s (sum of the reservation: %s)\n" % (move_line_str, reserved_on_move_lines) | |
warning += '******************\n' | |
elif quant.reserved_quantity < 0: | |
warning += "Problematic quant found: %s (quantity: %s, reserved_quantity: %s)\n" % (quant.id, quant.quantity, quant.reserved_quantity) | |
warning += "its `reserved_quantity` field is negative while it should not happen\n" | |
if move_lines: | |
warning += "These move lines are reserved on it: %s (sum of the reservation: %s)\n" % (move_line_str, reserved_on_move_lines) | |
warning += '******************\n' | |
else: | |
if reserved_on_move_lines != quant.reserved_quantity: | |
warning += "Problematic quant found: %s (quantity: %s, reserved_quantity: %s)\n" % (quant.id, quant.quantity, quant.reserved_quantity) | |
warning += "its `reserved_quantity` does not reflect the move lines reservation\n" | |
warning += "These move lines are reserved on it: %s (sum of the reservation: %s)\n" % (move_line_str, reserved_on_move_lines) | |
warning += '******************\n' | |
else: | |
if any(move_line.product_qty < 0 for move_line in move_lines): | |
warning += "Problematic quant found: %s (quantity: %s, reserved_quantity: %s)\n" % (quant.id, quant.quantity, quant.reserved_quantity) | |
warning += "its `reserved_quantity` correctly reflects the move lines reservation but some are negatives\n" | |
warning += "These move lines are reserved on it: %s (sum of the reservation: %s)\n" % (move_line_str, reserved_on_move_lines) | |
warning += '******************\n' | |
move_lines = env['stock.move.line'].search([('product_id.type', '=', 'product'), ('product_qty', '!=', 0), ('id', 'not in', move_line_ids)]) | |
for move_line in move_lines: | |
if not move_line.location_id.should_bypass_reservation(): | |
warning += "Problematic move line found: %s (reserved_quantity: %s)\n" % (move_line.id, move_line.product_qty) | |
warning += "There is no exiting quants despite its `reserved_quantity`\n" | |
warning += '******************\n' | |
if warning == '': | |
warning = 'nothing seems wrong' | |
raise Warning(warning) |
where is the actions server code to fix the bug?
You have to create it, check https://www.odoo.com/forum/help-1/question/it-is-not-possible-to-unreserve-more-products-of-than-you-have-in-stock-138783
@nehmiascr why there is steel no fix in odoo ?
Just FYI, here is a variant that will update the stock.quant
records reserved_quantity
to match all move lines: https://gist.github.com/ryanc-me/632fd59639a8a68e041c876abe87168f
I think this method is safer than what's advised on the Odoo help forum. Updating stock.move.line
records outside the Python context can be a bit dangerous, and Odoo doesn't always update those 0-quantity lines when you click Check Availability
.
(I also include some instructions on creating the server action.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is the actions server code to fix the bug?