refactory invetory query