'Validate moves based on a production order.'

@api.multi
def move_validate(self):

    moves = self._filter_closed_moves()
    quant_obj = self.env['stock.quant']
    moves_todo = self.env['stock.move']
    moves_to_unreserve = self.env['stock.move']
    for move in moves:
        rounding = move.product_uom.rounding
        move.quantity_done = float_round(move.quantity_done, precision_rounding=rounding, rounding_method='UP')
        if (move.quantity_done <= 0):
            continue
        moves_todo |= move
        moves_todo |= move._create_extra_move()
    for move in moves_todo:
        rounding = move.product_uom.rounding
        if (float_compare(move.quantity_done, move.product_uom_qty, precision_rounding=rounding) < 0):
            qty_split = move.product_uom._compute_quantity((move.product_uom_qty - move.quantity_done), move.product_id.uom_id)
            new_move = move.split(qty_split)
            move.move_lot_ids.filtered((lambda x: ((not x.done_wo) or (x.quantity_done == 0.0)))).write({'move_id': new_move})
            self.browse(new_move).quantity_done = 0.0
        main_domain = [('qty', '>', 0)]
        preferred_domain = [('reservation_id', '=', move.id)]
        fallback_domain = [('reservation_id', '=', False)]
        fallback_domain2 = ['&', ('reservation_id', '!=', move.id), ('reservation_id', '!=', False)]
        preferred_domain_list = (([preferred_domain] + [fallback_domain]) + [fallback_domain2])
        if (move.has_tracking == 'none'):
            quants = quant_obj.quants_get_preferred_domain(move.product_qty, move, domain=main_domain, preferred_domain_list=preferred_domain_list)
            self.env['stock.quant'].quants_move(quants, move, move.location_dest_id)
        else:
            for movelot in move.move_lot_ids:
                if (float_compare(movelot.quantity_done, 0, precision_rounding=rounding) > 0):
                    if (not movelot.lot_id):
                        raise UserError(_('You    need    to    supply    a    lot/serial    number.'))
                    qty = move.product_uom._compute_quantity(movelot.quantity_done, move.product_id.uom_id)
                    quants = quant_obj.quants_get_preferred_domain(qty, move, lot_id=movelot.lot_id.id, domain=main_domain, preferred_domain_list=preferred_domain_list)
                    self.env['stock.quant'].quants_move(quants, move, move.location_dest_id, lot_id=movelot.lot_id.id)
        moves_to_unreserve |= move
        if move.move_dest_id:
            move.move_dest_id.action_assign()
    moves_to_unreserve.quants_unreserve()
    moves_todo.write({'state': 'done', 'date': fields.Datetime.now()})
    return moves_todo
