A two-step procedure for the energy management in smart microgrids accounting for economical and power quality issues