Open-system theory of photodetection