Skip to content

Instantly share code, notes, and snippets.

@sle-odoo
Created December 2, 2019 12:44
Show Gist options
  • Save sle-odoo/6f8f1704eaad4b99101098643dc838ba to your computer and use it in GitHub Desktop.
Save sle-odoo/6f8f1704eaad4b99101098643dc838ba to your computer and use it in GitHub Desktop.
_get_returnable_quantities on stock.move
def _get_returnable_quantities(self, lot=None, package=None):
"""Return the `stock.move.line` of `self` still returnable.
When computing the quantities to return, this method handles the existing returns, returns
of returns, and so on.
:param lot: `stock.production.lot` record to filter candidate move lines
:param package: `stock.quant.package` record to filter candidate move lines
:return: a dict of the quantities of `stock_move` stille returnable, indexed by
`stock.production.lot` and `stock.quant.package`
:rtype: dict
"""
self.ensure_one()
qty_per_lot_and_package = defaultdict(lambda: 0)
if self.state != 'done' or self.scrapped:
return qty_per_lot_and_package
moves_of_chain = self.browse()
if self.move_dest_ids:
moves_of_chain = self.browse()
tmp = self
while tmp.move_dest_ids:
moves_of_chain |= tmp.move_dest_ids.filtered(lambda m: m.state == 'done' and not m.scrapped)
tmp = tmp.move_dest_ids
moves_of_chain |= self
mls = moves_of_chain.move_line_ids
if lot is not None:
mls = mls.filtered(lambda ml: ml.lot_id == lot)
in_mls = self.env['stock.move.line']
out_mls = self.env['stock.move.line']
for ml in mls:
# FIXME: chilof not ==
if ml.location_id == self.location_id and ml.location_dest_id == self.location_dest_id:
# origianl mls
out_mls |= ml
elif ml.location_id == self.location_dest_id and ml.location_dest_id != self.location_id:
# mls taken by our sibling moves
in_mls |= ml
elif ml.location_id != self.location_dest_id and ml.location_dest_id == self.location_dest_id:
# returned to my siblings
out_mls |= ml
elif ml.location_id == self.location_dest_id and ml.location_dest_id == self.location_id:
# returned to me
in_mls |= ml
for out_ml in out_mls:
qty = out_ml.product_uom_id._compute_quantity(out_ml.qty_done, out_ml.product_id.uom_id)
qty_per_lot_and_package[(out_ml.lot_id, out_ml.package_id)] += qty
for in_ml in in_mls:
qty = in_ml.product_uom_id._compute_quantity(in_ml.qty_done, in_ml.product_id.uom_id)
qty_per_lot_and_package[(in_ml.lot_id, in_ml.package_id)] -= qty
return qty_per_lot_and_package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment