-- --------------------------------------------------------
-- Server OS:                    Linux
-- HeidiSQL Version:             12.8.0.6908
-- --------------------------------------------------------


USE `master`;

-- Dumping structure for function master.getRawatInap
DELIMITER //
CREATE FUNCTION `getRawatInap`(
	`PNORM` INT
) RETURNS tinyint(4)
    DETERMINISTIC
BEGIN
   DECLARE HASIL TINYINT;
   
	SELECT 1 INTO HASIL
			FROM pendaftaran.pendaftaran pd
				, pendaftaran.kunjungan k
				  LEFT JOIN layanan.pasien_pulang pp ON k.NOPEN=pp.NOPEN AND pp.`STATUS`!=0
				, `master`.ruangan r
			WHERE pd.NOMOR=k.NOPEN AND pd.`STATUS`!=0 AND k.`STATUS`!=0
			 AND k.RUANGAN=r.ID AND r.JENIS_KUNJUNGAN=3 AND k.KELUAR IS NULL
			 AND (pp.ID IS NULL OR pp.`STATUS`=0)
			 AND pd.NORM=PNORM
			LIMIT 1;
 
  RETURN HASIL;
END//
DELIMITER ;

