<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Farmasi_panel extends MBELINK_Controller {
	
	public function __construct(){
		
		parent::__construct();
		date_default_timezone_set('Asia/Jakarta');	
		$this->load->helper(array('rsonline'));
				 
		// User Login Information
		$this->data['user'] = user_info();
		$this->data['instansi'] = orgInfo();
		
		// Cek Sesi
		 if(! $this->session->userdata('userid')):
			redirect(base_url('login-system.html'), 'refresh');
		endif;
		
		//load Model
		$this->load->model('farmasi/farmasi_model');		
		$this->data['depo']=$this->farmasi_model->getBangsalObat();
		
		function setTglAwal($str_tgl){
			$tglaja=strtotime($str_tgl);
			$bulan=date('m', $tglaja);
			$tahun=date('Y', $tglaja);
			$tglok=$tahun.'-'.$bulan.'-01';
			
			return $tglok;
		}
		
		function setTglAkhir($str_tgl){
			$tglaja=strtotime($str_tgl);
			$bulan=date('m', $tglaja);
			$tahun=date('Y', $tglaja);
			$tglok=$tahun.'-'.$bulan.'-31';
			
			return $tglok;
		}
		
		//inisialisasi
		$date=date_create(date('Y-m-d'));
		date_modify($date, '-1 day');
		$tglkemarin=date_format($date, 'Y-m-d');
		
		$this->data['enddate']=$tglkemarin;
		
	}
	
	public function index()
	{
		$this->data['tglawal']=$this->input->post('tglawal');
		$this->data['tglakhir']=$this->input->post('tglakhir');
		
		$this->data['result']=$this->farmasi_model->getHistoryObat($this->data['tglawal'],$this->data['tglakhir']);
		$this->load->view('farmasi/riwayat_transaksi_obat', $this->data); 
		
	}	
	
	function setStokObatTerkini()
	{
		$keys="";
		if(isset($_POST['bProses'])){
			$this->data['keyi']=$this->input->post('kode');
			$kd=explode("|",$this->data['keyi']);
			$this->data['key']=$kd[0];
			$this->data['depox']=$kd[1];
			
			if(isset($_POST['bProses']) AND $this->data['key']=='0'){
				$keys="";
			}else{
				$keys="WHERE c.`kd_bangsal`='".$this->data['key']."'";
			}
		}
		
		$this->data['result']=$this->farmasi_model->getStokObatTerkini($keys);
		
		$this->load->view('farmasi/stok_obat_terkini', $this->data); 
	}
	
	function cetakStokObatTerkini()
	{
		
			$this->data['key']=$this->input->post('kodex');
			$this->data['depo']=$this->input->post('depo');
			
			if($this->data['key']=='0'){
				$keys="";
			}else{
				$keys="WHERE c.`kd_bangsal`='".$this->data['key']."'";
			}
		
		
		$this->data['result']=$this->farmasi_model->getStokObatTerkini($keys);
		
		$this->load->view('farmasi/stok_obat_terkini_to_excel', $this->data); 
	}
}
