<?php
	class Reportpx_model extends CI_Model {
		
		function __construct()
		{
			parent::__construct();
			
			// DETERMINE DB
			//$this->nm_db = array('1' => 'jobs', 'db_hro', '');
			//$this->user_admin = 'user_admin';
			$this->dbhost=$this->load->database('khanza', TRUE);
		}	
		
		function getRekapIGDPx($tglawal,$tglakhir)
		{
			$q=$this->dbhost->query("SELECT b.`nm_dokter`, COUNT(a.`no_rawat`) AS jml_px, COUNT(IF(a.`status_lanjut`='ranap',a.`no_rawat`,NULL)) AS jml_ranap
FROM reg_periksa AS a
LEFT JOIN dokter AS b ON a.`kd_dokter`=b.`kd_dokter`
LEFT JOIN poliklinik AS c ON a.`kd_poli`=c.`kd_poli`
WHERE a.`stts` <> 'Batal' AND a.`kd_poli` IN('POL26','POL19')
AND a.`tgl_registrasi` BETWEEN '$tglawal' AND '$tglakhir'
GROUP BY a.`kd_dokter`;");
			
			return $q->result();
		}
		 
		function getRekapPxIGD($tahun)
		{
			$q=$this->dbhost->query("SELECT a.`tgl_registrasi`, COUNT(a.`no_rawat`) AS jml, 
COUNT(IF(a.`status_lanjut` ='ranap',a.`no_rawat`,NULL)) AS jml_ranap FROM reg_periksa AS a
WHERE a.`stts` <> 'Batal' AND a.`kd_poli` IN('POL26','POL19') AND LEFT(a.`tgl_registrasi`,7) BETWEEN '$tahun-01' AND '$tahun-12' GROUP BY LEFT(a.`tgl_registrasi`,7);");
			
			return $q->result();
		}
		
		function getRekapPasienIGD($tglawal,$tglakhir)
		{
			$q=$this->dbhost->query("SELECT a.`tgl_registrasi` ,
COUNT(IF(a.`kd_pj`='BPJ',a.`no_rawat`,NULL)) AS BPJS,
COUNT(IF(a.`kd_pj`='-' OR a.`kd_pj`='1',a.`no_rawat`,NULL)) AS Umum,
COUNT(IF(a.`kd_pj`<>'BPJ' AND a.`kd_pj`<>'-' AND a.`kd_pj`<>'1',a.`no_rawat`,NULL)) AS Lainnya,
COUNT(a.`no_rawat`) AS total 
FROM reg_periksa AS a
WHERE a.`stts` <> 'Batal' AND a.`kd_poli` IN('POL26','POL19') AND a.`tgl_registrasi` BETWEEN '$tglawal' AND '$tglakhir'
GROUP BY a.`tgl_registrasi` ;
");
			
			return $q->result();
		}
		
		function getDataTransferPasienIGD($tglawal,$tglakhir)
		{
			$q=$this->dbhost->query("SELECT a.`no_rawat`, a.`no_rkm_medis`, c.`nm_pasien`,b.`tanggal_pindah`,CONCAT(a.`tgl_registrasi`,' ',a.`jam_reg`) AS tanggal_reg, a.`tgl_registrasi`,a.`jam_reg`, LEFT(b.`tanggal_pindah`,10) AS tgl_pindah,
 RIGHT(b.`tanggal_pindah`,8) AS jam_pindah, b.`asal_ruang`, b.`ruang_selanjutnya`
 FROM reg_periksa AS a
 JOIN transfer_pasien_antar_ruang AS b ON a.`no_rawat`=b.`no_rawat`
 JOIN pasien AS c ON a.`no_rkm_medis`=c.`no_rkm_medis`
 WHERE b.`asal_ruang`='igd' AND a.`tgl_registrasi` BETWEEN '$tglawal' AND '$tglakhir';");
			
			return $q->result();
		}

		
		//dummy
		function get_grupuser($nip)
        {
            $this->db->select('id_grup');
            $q = $this->db->get_where('grup_user', array('nik' => $nip));
            if($q->num_rows() > 0)
			{
				foreach($q->result() as $i)
				{
					$h[] = $i->id_grup;
				}
				return $h;
			}
        }
	}
