<?php
	class Indikator_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('hospistar', TRUE);
		}	
		
		function getDataBor($tglawal,$tglakhir)
		{
			$q=$this->dbhost->query("SELECT a.`PendaftaranID`,a.`RI_TglMasuk`, a.`RI_TglKeluar`, SUM(IF(a.`RI_TglMasuk`=a.`RI_TglKeluar`,1,IF(a.`RI_TglKeluar` IS NULL,DATEDIFF(LEFT(NOW(),10), a.`RI_TglMasuk`),DATEDIFF(a.`RI_TglKeluar`,a.`RI_TglMasuk`)))) AS lama_rawat_hari 
FROM `rawatinap` AS a
WHERE  a.`PendaftaranID` <>'0' AND a.`RI_TglMasuk` BETWEEN '$tglawal' AND '$tglakhir'
GROUP BY a.`RI_TglMasuk`;");
			
			return $q->result();
		}
		
		function getDataBorOK($tanggal)
		{
			$q=$this->dbhost->query("SELECT DATE_ADD('$tanggal',INTERVAL a.AngkaTgl DAY) AS tgl,
get_pasien_masuk_all(DATE_ADD('$tanggal',INTERVAL a.AngkaTgl DAY)) AS jml_masuk,
COUNT(*) AS jml_keluar,
get_pasien_dirawat_all(DATE_ADD('$tanggal',INTERVAL a.AngkaTgl DAY)) AS jml_dirawat,
SUM(DATEDIFF(ri.RI_TglKeluar,ri.RI_TglMasuk)) AS lp,
ROUND(SUM(DATEDIFF(ri.RI_TglKeluar,ri.RI_TglMasuk))/(COUNT(*)-1)) AS los
FROM angka_tanggal AS a
LEFT JOIN rawatinap AS ri ON DATE_ADD('$tanggal',INTERVAL a.AngkaTgl DAY) = ri.`RI_TglKeluar`
where ri.PendaftaranID <> '0'
GROUP BY DATE_ADD('$tanggal',INTERVAL a.AngkaTgl DAY);");
			
			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;
			}
        }
	}
