Diagrammatic theory of linear and nonlinear optics for composite systems