<?php
	class RM_model extends CI_Model {
		function __construct()
		{
			parent::__construct();
			
			// DETERMINE DB
			$this->dbhost=$this->load->database('hospistar', TRUE);
			//$this->nm_db = array('1' => 'jobs', 'db_hro', '');
			//$this->user_admin = 'user_admin';
		}	
		public function getPintuMasuk()
		{
			/* $stat = array('1','2','3','4','5');
			//$this->db->where_in('NurseStation', $stat);
			
			
			$q=$this->db
				->select('b.`NurseStation` AS nama_ns, a.*')
				->from('`kamar` AS a')
				->join('nursestation AS b','a.`NurseStation`=b.`ID`','left')
				->where_in('a.NurseStation', $stat)
				//->order_by('a.`id_poli`')
				->get(); */
			$q=$this->dbhost->get('pintumasuk');	
			return $q->result();
		}
		
		function getDataRegistrasiRMAllNow()
		{
			
			$q=$this->dbhost->query("SELECT  b.`NoRM`, b.`NIK`,b.`Nama`,b.`NoHP`,f.`Item`, b.`TglLhr`,c.`DQueue`,d.`Dokter` AS nm_dokter, b.`Alamat`,e.`nmkecamatan`,g.`NamaPerujuk`,
IF(a.`PershID`='0','Umum',IF(a.`PershID`='1','BPJS','Asuransi')) AS penjamin, j.`PintuMasuk` AS poli,a.*
FROM pendaftaran AS a
LEFT JOIN datapasien AS b ON a.`PasienID`=b.`ID`
LEFT JOIN pendaftaran_poli AS c ON a.`ID`=c.`RegID`
LEFT JOIN dokter AS d ON a.`Dokter`=d.`ID`
LEFT JOIN kecamatan AS e ON b.`Kecamatan`=e.`ID`
LEFT JOIN ket_nama AS f ON b.`ketNamaID`=f.`ID`
LEFT JOIN perujuk AS g ON a.`Perujuk`=g.`ID`
LEFT JOIN pintumasuk AS j ON a.`PintuMasuk`=j.`ID`
WHERE  a.`TanggalMasuk`=LEFT(NOW(),10)
GROUP BY a.`ID` ORDER BY a.`JamMasuk` DESC;");
			return $q->result();
		}
		
		function getDataRegistrasiRM($tglawal,$poli)
		{
			
			$q=$this->dbhost->query("SELECT  b.`NoRM`, b.`NIK`,b.`Nama`,b.`NoHP`,f.`Item`, b.`TglLhr`,c.`DQueue`,d.`Dokter` AS nm_dokter, b.`Alamat`,e.`nmkecamatan`,g.`NamaPerujuk`,
IF(a.`PershID`='0','Umum',IF(a.`PershID`='1','BPJS','Asuransi')) AS penjamin, j.`PintuMasuk` AS poli,a.*
FROM pendaftaran AS a
LEFT JOIN datapasien AS b ON a.`PasienID`=b.`ID`
LEFT JOIN pendaftaran_poli AS c ON a.`ID`=c.`RegID`
LEFT JOIN dokter AS d ON a.`Dokter`=d.`ID`
LEFT JOIN kecamatan AS e ON b.`Kecamatan`=e.`ID`
LEFT JOIN ket_nama AS f ON b.`ketNamaID`=f.`ID`
LEFT JOIN perujuk AS g ON a.`Perujuk`=g.`ID`
LEFT JOIN pintumasuk AS j ON a.`PintuMasuk`=j.`ID`
WHERE  a.`TanggalMasuk`='$tglawal' $poli
GROUP BY a.`ID` ORDER BY a.`JamMasuk` DESC;");
		
			return $q->result();
		}
		
		function getDataRegistrasiRMAllNowMobi()
		{
			
			$q=$this->dbhost->query("SELECT  b.`NoRM`, b.`NIK`,b.`Nama`,b.`NoHP`,f.`Item`, b.`TglLhr`,d.`Dokter` AS nm_dokter, b.`Alamat`,e.`nmkecamatan`,g.`NamaPerujuk`,
IF(a.`PershID`='0','Umum',IF(a.`PershID`='1','BPJS','Asuransi')) AS penjamin, j.`PintuMasuk` AS poli,a.*
FROM pendaftaran_mobi AS a
LEFT JOIN datapasien AS b ON a.`NoRM`=b.`NoRM`
#LEFT JOIN pendaftaran_poli AS c ON a.`ID`=c.`RegID`
LEFT JOIN dokter AS d ON a.`Dokter`=d.`ID`
LEFT JOIN kecamatan AS e ON b.`Kecamatan`=e.`ID`
LEFT JOIN ket_nama AS f ON b.`ketNamaID`=f.`ID`
LEFT JOIN perujuk AS g ON a.`Perujuk`=g.`ID`
LEFT JOIN pintumasuk AS j ON a.`PintuMasuk`=j.`ID`
WHERE  a.`TanggalMasuk`=LEFT(NOW(),10) 
GROUP BY a.`NoRM` ORDER BY a.`JamMasuk` DESC;");
			return $q->result();
		}
		
		function getDataRegistrasiRMMobi($tglawal,$poli,$status)
		{
			
			$q=$this->dbhost->query("SELECT  b.`NoRM`, b.`NIK`,b.`Nama`,b.`NoHP`,f.`Item`, b.`TglLhr`,d.`Dokter` AS nm_dokter, b.`Alamat`,e.`nmkecamatan`,g.`NamaPerujuk`,
IF(a.`PershID`='0','Umum',IF(a.`PershID`='1','BPJS','Asuransi')) AS penjamin, j.`PintuMasuk` AS poli,a.*
FROM pendaftaran_mobi AS a
LEFT JOIN datapasien AS b ON a.`NoRM`=b.`NoRM`
LEFT JOIN dokter AS d ON a.`Dokter`=d.`ID`
LEFT JOIN kecamatan AS e ON b.`Kecamatan`=e.`ID`
LEFT JOIN ket_nama AS f ON b.`ketNamaID`=f.`ID`
LEFT JOIN perujuk AS g ON a.`Perujuk`=g.`ID`
LEFT JOIN pintumasuk AS j ON a.`PintuMasuk`=j.`ID`
WHERE  a.`TanggalMasuk`='$tglawal' $poli $status
GROUP BY a.`NoRM` ORDER BY a.`JamMasuk` DESC;");
		
			return $q->result();
		}
		
		//Json
		function getKamar($idkamar)
		{
			//$this->db->select('ID,NamaKamar,Kapasitas,terisi');
			$q=$this->db->get_where('kamar', array('ID' => $idkamar));
			
			if($q->num_rows() > 0)
			{
				return $q->result();
			}
		}			
		
		function getPasienRMByID($regid)
		{
			$q=$this->dbhost->query("SELECT a.`ID`,a.`TanggalMasuk`,b.`Nama`,b.`NoRM`,c.`PintuMasuk` AS poli,IF(a.`PershID`='0','Umum',IF(a.`PershID`='1','BPJS','Asuransi')) AS penjamin
FROM pendaftaran AS a
LEFT JOIN datapasien AS b ON a.`PasienID`=b.`ID`
LEFT JOIN pintumasuk AS c ON a.`PintuMasuk`=c.`ID`
WHERE a.`ID`='$regid';");

			return $q->result();
		}
	}
