Skip to content

Instantly share code, notes, and snippets.

@rvcsdev
Created April 25, 2019 01:41
Show Gist options
  • Save rvcsdev/caf908a80e252cf14a8604614ba95404 to your computer and use it in GitHub Desktop.
Save rvcsdev/caf908a80e252cf14a8604614ba95404 to your computer and use it in GitHub Desktop.

Revisions

  1. @amoyaux amoyaux revised this gist Jun 25, 2018. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    quants = env['stock.quant'].search([])

    move_line_ids = []
    warning = ''
    for quant in quants:
    move_lines = env["stock.move.line"].search([
    @@ -10,6 +10,7 @@ for quant in quants:
    ('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])

    @@ -38,7 +39,11 @@ for quant in quants:
    move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
    quant.write({'reserved_quantity': 0})

    move_lines = env['stock.move.line'].search([('product_id.type', '=', 'product')])
    move_lines = env['stock.move.line'].search([
    ('product_id.type', '=', 'product'),
    ('product_qty', '!=', 0),
    ('id', 'not in', move_line_ids),
    ])

    move_lines_to_unreserve = []

    @@ -50,4 +55,3 @@ if len(move_lines_to_unreserve) > 1:
    env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id in %s ;""" % (tuple(move_lines_to_unreserve), ))
    elif len(move_lines_to_unreserve) == 1:
    env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id = %s ;""" % (move_lines_to_unreserve[0]))

  2. @amoyaux amoyaux revised this gist Jun 21, 2018. 1 changed file with 4 additions and 10 deletions.
    14 changes: 4 additions & 10 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -38,19 +38,13 @@ for quant in quants:
    move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
    quant.write({'reserved_quantity': 0})

    product_without_quants = env['product.product'].search([('type', '=', 'product')]) - quants.mapped('product_id')
    move_lines = env['stock.move.line'].search([('product_id.type', '=', 'product')])

    move_lines_to_unreserve = []

    for product in product_without_quants:
    move_lines = env['stock.move.line'].search([
    ('product_qty', '!=', 0),
    ('product_id', '=', product.id),
    ])

    for move_line in move_lines:
    if not move_line.location_id.should_bypass_reservation():
    move_lines_to_unreserve.append(move_line.id)
    for move_line in move_lines:
    if not move_line.location_id.should_bypass_reservation():
    move_lines_to_unreserve.append(move_line.id)

    if len(move_lines_to_unreserve) > 1:
    env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id in %s ;""" % (tuple(move_lines_to_unreserve), ))
  3. @amoyaux amoyaux revised this gist Jun 14, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,8 @@ for product in product_without_quants:
    if not move_line.location_id.should_bypass_reservation():
    move_lines_to_unreserve.append(move_line.id)

    if move_lines_to_unreserve:
    if len(move_lines_to_unreserve) > 1:
    env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id in %s ;""" % (tuple(move_lines_to_unreserve), ))
    elif len(move_lines_to_unreserve) == 1:
    env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id = %s ;""" % (move_lines_to_unreserve[0]))

  4. @amoyaux amoyaux created this gist Jun 13, 2018.
    57 changes: 57 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    quants = env['stock.quant'].search([])

    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)
    ])
    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:
    quant.write({'reserved_quantity': 0})
    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:
    move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
    elif quant.reserved_quantity < 0:
    quant.write({'reserved_quantity': 0})
    if move_lines:
    move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
    else:
    if reserved_on_move_lines != quant.reserved_quantity:
    move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
    quant.write({'reserved_quantity': 0})
    else:
    if any(move_line.product_qty < 0 for move_line in move_lines):
    move_lines.with_context(bypass_reservation_update=True).write({'product_uom_qty': 0})
    quant.write({'reserved_quantity': 0})

    product_without_quants = env['product.product'].search([('type', '=', 'product')]) - quants.mapped('product_id')

    move_lines_to_unreserve = []

    for product in product_without_quants:
    move_lines = env['stock.move.line'].search([
    ('product_qty', '!=', 0),
    ('product_id', '=', product.id),
    ])

    for move_line in move_lines:
    if not move_line.location_id.should_bypass_reservation():
    move_lines_to_unreserve.append(move_line.id)

    if move_lines_to_unreserve:
    env.cr.execute(""" UPDATE stock_move_line SET product_uom_qty = 0, product_qty = 0 WHERE id in %s ;""" % (tuple(move_lines_to_unreserve), ))