|
|
domain_quant_loc, _, _, domain_move_in_loc, domain_move_out_loc = self.env['product.product']._get_domain_locations()
owner_id = self.env.context.get('owner_id')
package_id = self.env.context.get('package_id')
to_date = fields.Datetime.to_datetime(self.env.context.get('to_date'))
dates_in_the_past = to_date and to_date < fields.Datetime.now()
domain_quant = Domain([('lot_id', 'in', self.ids)]) & domain_quant_loc
if owner_id is not None:
domain_quant &= Domain([('owner_id', '=', owner_id)])
domain_move_in_loc &= Domain([('owner_id', '=', owner_id)])
domain_move_out_loc &= Domain([('owner_id', '=', owner_id)])
if package_id is not None:
domain_quant &= Domain([('package_id', '=', package_id)])
quant_qty_by_lot = dict(self.env['stock.quant']._read_group(domain_quant, ['lot_id'], ['quantity:sum']))
if not dates_in_the_past:
for lot in self:
lot.product_qty = quant_qty_by_lot.get(lot, 0.0)
else:
# If the date is in the past, we need to adjust the quantity on hand with the moves that happened after that date.
domain_lot_done = Domain([('lot_id', 'in', self.ids), ('state', '=', 'done'), ('move_id.date', '>', to_date)])
move_in_qty_by_lot = dict(self.env['stock.move.line']._read_group(domain_move_in_loc & domain_lot_done, ['lot_id'], ['quantity_product_uom:sum']))
move_out_qty_by_lot = dict(self.env['stock.move.line']._read_group(domain_move_out_loc & domain_lot_done, ['lot_id'], ['quantity_product_uom:sum']))
for lot in self:
lot.product_qty = quant_qty_by_lot.get(lot, 0.0) - move_in_qty_by_lot.get(lot, 0.0) + move_out_qty_by_lot.get(lot, 0.0)
|
|