Theory of transport in inhomogeneous systems and application to magnetic multilayer systems