Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. move_lines_to_fix = env['stock.move.line'].search([
  2. ('state', '=', 'done'),
  3. ('product_uom_qty', '!=', 0),
  4. ]).write({
  5. 'product_uom_qty': 0,
  6. })
  7.  
  8. # Use as server action to repair consistancies in quant reservation.
  9. # Usefull for errors like 'Cannot unreserve more product than you have in stock'
  10.  
  11. # Executed by support - JPM - 2019-11-12 - opw-2120582
  12.  
  13. quants = env['stock.quant'].search([])
  14. move_line_ids = []
  15. warning = ''
  16. for quant in quants:
  17. move_lines = env["stock.move.line"].search([
  18. ('product_id', '=', quant.product_id.id),
  19. ('location_id', '=', quant.location_id.id),
  20. ('lot_id', '=', quant.lot_id.id),
  21. ('package_id', '=', quant.package_id.id),
  22. ('owner_id', '=', quant.owner_id.id),
  23. ('product_qty', '!=', 0)
  24. ])
  25. move_line_ids += move_lines.ids
  26. reserved_on_move_lines = sum(move_lines.mapped('product_qty'))
  27. move_line_str = str.join(', ', [str(move_line_id) for move_line_id in move_lines.ids])
  28.  
  29. if quant.location_id.should_bypass_reservation():
  30. # If a quant is in a location that should bypass the reservation, its `reserved_quantity` field
  31. # should be 0.
  32. if quant.reserved_quantity != 0:
  33. quant.write({'reserved_quantity': 0})
  34. else:
  35. # If a quant is in a reservable location, its `reserved_quantity` should be exactly the sum
  36. # of the `product_qty` of all the partially_available / assigned move lines with the same
  37. # characteristics.
  38. if quant.reserved_quantity == 0:
  39. if move_lines:
  40. move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
  41. elif quant.reserved_quantity < 0:
  42. quant.write({'reserved_quantity': 0})
  43. if move_lines:
  44. move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
  45. else:
  46. if reserved_on_move_lines != quant.reserved_quantity:
  47. move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
  48. quant.write({'reserved_quantity': 0})
  49. else:
  50. if any(move_line.product_qty < 0 for move_line in move_lines):
  51. move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
  52. quant.write({'reserved_quantity': 0})
  53.  
  54. move_lines = env['stock.move.line'].search([
  55. ('product_id.type', '=', 'product'),
  56. ('product_qty', '!=', 0),
  57. ('id', 'not in', move_line_ids),
  58. ])
  59.  
  60. move_lines_to_unreserve = []
  61.  
  62. for move_line in move_lines:
  63. if not move_line.location_id.should_bypass_reservation():
  64. move_lines_to_unreserve.append(move_line.id)
  65.  
  66. if len(move_lines_to_unreserve) > 1:
  67. env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id in %s ;""" % (tuple(move_lines_to_unreserve), ))
  68. elif len(move_lines_to_unreserve) == 1:
  69. env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id = %s ;""" % (move_lines_to_unreserve[0]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement