Necessary and Sufficient Conditions in the Problem of Optimal Investment with Intermediate Consumption