<?php
	class Farmasi_model extends CI_Model {
		function __construct()
		{
			// Call the Model constructor
			parent::__construct();
			
			$this->dbhost=$this->load->database('khanza', TRUE);
					
			date_default_timezone_set('Asia/Jakarta');
		}
		
		/* public function getDataUser($user, $paswd)
		{
			$q=$this->db->get_where('karyawan1p', array('nik'=>$user, 'passwd'=> md5($paswd)));
			return $q->row();
		} */
		
		/* public function cek_login($email, $passwd)
		{
			$this->db->select("nik, tgl_lahir");
			$this->db->where('nik', $email);
			$this->db->where('passwd', md5($passwd));
			$q = $this->db->get('karyawan1p');
			
			return $q->num_rows()>0?TRUE:FALSE;
		} */
		public function getStokObatTerkini($kode)
		{
			
			$q=$this->dbhost->query("SELECT a.`kode_brng`, a.`nama_brng`, b.`stok`, c.`kd_bangsal`, c.`nm_bangsal`,a.`dasar`,a.`h_beli`,a.`jualbebas`
FROM databarang AS a
LEFT JOIN gudangbarang AS b ON a.`kode_brng`=b.`kode_brng`
LEFT JOIN bangsal AS c ON b.`kd_bangsal`=c.`kd_bangsal`  $kode ORDER BY a.`nama_brng`;");

			return $q->result();
		}
		
		public function getHistoryObat($tglawal,$tglakhir)
		{
			
			$q=$this->dbhost->query("SELECT a.`tanggal`, a.`jam`,a.`kode_brng`, c.`nama_brng`,a.masuk, a.`keluar`,a.`stok_awal`,a.`stok_akhir`, a.`posisi`,a.`kd_bangsal`, b.`nm_bangsal`, a.`petugas`, d.`nama`, a.`keterangan`
FROM riwayat_barang_medis AS a
LEFT JOIN bangsal AS b ON a.`kd_bangsal`=b.`kd_bangsal`
LEFT JOIN databarang AS c ON a.`kode_brng`=c.`kode_brng`
LEFT JOIN petugas AS d ON a.`petugas`=d.`nip`
WHERE a.`tanggal` BETWEEN '$tglawal' AND '$tglakhir' ORDER BY a.`tanggal` ;");

			return $q->result();
		}
		
		function getBangsalObat()
		{
			$were ="LEFT(a.`nm_bangsal`,1)='D'";
			$q=$this->dbhost
				->select('a.*')				
				->from('`bangsal` AS a')
				//->join('`perujuk` AS l','a.`user`=l.`nik`','left')				
				->where($were)
				->where('a.`status`', '1')
				//->order_by('a.`TanggalMasuk`', 'DESC')
				->get(); 
		
			return $q->result();
		}
		
		//dummy
		function getSaldoAwal($taun)
		{
			$q=$this->dbhost->query("SELECT b.`kd_rek`, a.saldo_awal,b.tipe, b.balance FROM rekeningtahun AS a INNER JOIN rekening AS b ON a.kd_rek=b.kd_rek WHERE a.thn LIKE '%$taun%';");
			if($q->num_rows()>0){
				foreach($q->result() as $i){
					$h[$i->kd_rek]=$i;
				}
				return $h;			
			}else{
				return $q->result();
			}
		}		
				
		function getBiayaBilling()
		{
			$q=$this->dbhost->query("SELECT a.`no_rawat`, a.`nm_perawatan`, a.`totalbiaya`, a.`status` FROM billing AS a;");
			if($q->num_rows()>0){
				foreach($q->result() as $i){
					$h[$i->no_rawat]=$i;
				}
				return $h;			
			}else{
				return $q->result();
			}
		}
	}
	
