A method for constructing optical potentials with an arbitrary distribution of gain and loss and completely real spectrum is presented. For each arbitrary distribution of gain and loss, several classes of refractive-index profiles with freely tunable parameters are obtained such that the resulting complex potentials, although being non-parity-time-symmetric in general, still feature all-real spectra for a wide range of tuning parameters. When these refractive indices are tuned below certain thresholds, phase transition can occur, where complex-conjugate pairs of eigenvalues appear in the spectrum. These non-parity-time-symmetric complex potentials generalize the concept of parity-time-symmetric potentials to allow for more flexible gain and loss distributions while still maintaining all-real spectra and the phenomenon of phase transition.