<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_simrs_report_lp extends CI_Model {
    function labmikroall(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('too.tglorder AS tglorder, too.tgl_hasil AS tglhasil, too.status AS statusorder
	                        ,tom.nonota  AS nota, tom.nomr AS nomr, tom.st_rajal AS strajal
	                        ,tbl.nobill AS nobillrajal, tbl.status AS billstatus, tbl.status_verifikasi AS verstatus, tbl.kodetarif AS kodetarif 
                            ,mps.noktp AS ktp, mps.noktp_baru AS ktpbaru, mps.nama AS namapasien, mps.tgllahir AS tgllahir, mps.jeniskelamin AS jeniskelamin,mps.alamat AS alamat
                            ,pap.noktp AS ktpaps, pap.nama AS namapasienaps, pap.tgllahir AS tgllahiraps, pap.jeniskelamin AS jeniskelaminaps, pap.alamat AS alamataps
	                        ,mcl.nama AS namabayarrajal
                            ,mrl.nama AS namaruangrajal
                            ,mcn.nama AS namabayarranap
                            ,mrn.nama AS namaruangranap
	                        ,mpl.nama AS poliklinik
	                        ,mfl.jenis_kelompok AS jeniskelompok, mfl.nama_field AS namafield
                            ,tbl.aps AS tbsrajal
                            ,tbn.aps AS tbsranap
                            ');
        $query->join('t_billrajal AS tbl',      'tbl.nonota     = CAST(tom.nonota AS int8)', 'left');
        $query->join('t_billranap AS tbn',      'tbn.nonota     = CAST(tom.nonota AS int8)', 'left');
        $query->join('m_pasien AS mps',         'mps.nomr       = tom.nomr', 'left');
        $query->join('m_pasien_aps AS pap',     'pap.nomr       = tom.nomr', 'left');
        $query->join('m_carabayar AS mcl',      'mcl.kode       = tbl.carabayar', 'left');
        $query->join('m_ruang AS mrl',          'mrl.no         = tbl.kode_ruang', 'left');

        $query->join('m_carabayar AS mcn',      'mcn.kode       = tbn.carabayar', 'left');
        $query->join('m_ruang AS mrn',          'mrn.no         = tbn.kode_ruang', 'left');

        $query->join('m_poly AS mpl',           'mpl.kode       = tom.kdpoli', 'left');
        $query->join('t_orderlabmikro AS too',  'too.nolab      = tom.nolab', 'left');
        $query->join('m_form_lab AS mfl',       'mfl.form_id    = too.idxform ', 'left');

        $query->where('tom.tglorder >=', $awal);
        $query->where('tom.tglorder <=', $akhir);
        //$query->where('too.status', '1');
        //$query->where('tom.st_rajal', '1');
        return $query->get('t_orderlabmikro_main AS tom ')->result();        
    }

    function pasien(){
        $query = $this->load->database('datasim', TRUE);
        $query->select('mps.nomr, mps.noktp AS ktp, mps.noktp_baru AS ktpbaru, mps.nama AS namapasien, mps.tgllahir AS tgllahir, mps.jeniskelamin AS jeniskelamin,mps.alamat AS alamat');
        return $query->get('m_pasien AS mps')->result();    
    }

    function pasienaps(){
        $query = $this->load->database('datasim', TRUE);
        $query->select('mps.nomr, mps.noktp AS ktp, mps.nama AS namapasien, mps.tgllahir AS tgllahir, mps.jeniskelamin AS jeniskelamin,mps.alamat AS alamat');
        return $query->get('m_pasien_aps AS mps')->result(); 
    }

    function klaimmikro(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tom.tglorder as tglorder, tom.st_rajal as strajal, tom.nonota as nonota, 
                        too.tgl_hasil as tglhasil, too.idxdaftar as idxdaftar, too.no_lokal as nolokal, too.status AS statusorder,
                        trp.nobill as billranap, 
                        trl.nobill as billrajal, 
                        mps.nama as namareg, mps.nomr as nomrreg, mps.noktp as noktpreg, mps.tgllahir as tgllahirreg, mps.jeniskelamin as jkreg, mps.alamat as alamatreg, 
                        pas.nama as namaaps, pas.nomr as nomraps, pas.noktp as noktpaps, pas.tgllahir as tgllahiraps, pas.jeniskelamin as jkaps, pas.alamat as alamataps, 
                        tbl.status as statusrajal, tbl.aps as apsrajal, tbl.user_bataltindakan as batalrajal,tbl.kodetarif as kodetarifrajal, tbl.status_verifikasi AS verstatusrajal,
                        tbn.status as statusranap, tbn.aps as apsranap, tbn.user_bataltindakan as batalranap,tbn.kodetarif as kodetarifranap, tbn.status_verifikasi AS verstatusranap,
                        mfl.jenis_kelompok as jenkel,mfl.nama_field as jenpemeriksaan,
                        mcl.nama as namabayarrajal, mcp.nama as namabayarranap, 
                        mru.nama as namaruang');
        $query->join('t_billrajal AS tbl',          'tbl.nonota     = CAST(tom.nonota AS int8)', 'left');
        $query->join('t_bayarrajal AS trl',         'trl.idxbill    = tbl.idxbill', 'left');
        $query->join('m_carabayar AS mcl',          'mcl.kode       = tbl.carabayar', 'left');    
        $query->join('t_billranap AS tbn',          'tbn.nonota     = CAST(tom.nonota AS int8)', 'left');  
        $query->join('t_bayarranap AS trp',         'trp.idxbill    = tbn.idxbill', 'left');  
        $query->join('m_carabayar AS mcp',          'mcp.kode       = tbn.carabayar', 'left');  
        $query->join('m_pasien AS mps',             'mps.nomr       = tom.nomr', 'left');  
        $query->join('m_pasien_aps AS pas',         'pas.nomr       = tom.nomr', 'left');  
        $query->join('t_orderlabmikro AS too',      'too.nolab      = tom.nolab', 'left');  
        $query->join('m_form_lab AS mfl',           'mfl.form_id    = too.idxform', 'left');  
        $query->join('m_ruang AS mru',              'mru.no         = too.kdpoli', 'left');  

        $query->where('date(tom.tglorder) >=', $awal);
        $query->where('date(tom.tglorder) <=', $akhir);
        $query->where('tom.st_periksa', '1');
        
        return $query->get('t_orderlabmikro_main AS tom ')->result();        
    }

    function icd9(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('icm.nomr as nomr, icm.idxdaftar as idxdaftar, icm.icd as icdnon, icm.icd_verified as icdver, icm.icd_verified_order as icdorder, 
                        micdnon.keterangan as keticdnon, 
                        micdver.keterangan as keticdver,
                        tp.tglreg, 
                        mr.nama as klinik,
                        mp.nama as namapasien');
        $query->join('icd_cm AS micdnon',           'micdnon.kode   = icm.icd', 'left');
        $query->join('icd_cm AS micdver',           'micdver.kode   = icm.icd_verified', 'left');
        $query->join('t_pendaftaran as tp',         'tp.idxdaftar   = icm.idxdaftar', 'left');
        $query->join('m_ruang as mr',               'mr.no          = tp.kdpoly', 'left');
        $query->join('m_pasien as mp',              'mp.nomr        = tp.nomr', 'left');
        $query->where('icm.status_rajal', '1');
        $query->where('date(tp.tglreg) >=', $awal);
        $query->where('date(tp.tglreg) <=', $akhir);
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('icm.idxdaftar', 'ASC');
        $query->order_by('icm.icd_verified_order', 'ASC');
        return $query->get('t_icd_cm AS icm ')->result();      
    }

    function pasienbaru(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.tglreg as tglregistrasi, mr.nama as klinik,
                        ,count(tp.nomr) FILTER (WHERE tp.pasienbaru = 0 ) AS pasienlama
                        ,count(tp.nomr) FILTER (WHERE tp.pasienbaru = 1 ) AS pasienbaru
                        ,count(tp.nomr) as allpasien');
        $query->join('m_ruang as mr', 'mr.no          = tp.kdpoly', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->group_by('tp.tglreg, mr.nama');
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('mr.nama', 'ASC');
        return $query->get('t_pendaftaran as tp ')->result();    
    }

    function pasiendetail(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.tglreg as tglregistrasi, tp.pasienbaru as statuspasien, tp.nosep as nosep, tp.nomr as nomr, tp.idxdaftar as idxdaftar,
                        mr.nama as klinik, 
                        mp.nama as namapasien, mp.jeniskelamin as jkelamin,
                        tbl.nobill as nobill, 
                        msk.keterangan as statuskeluar,
                        mdk.kode_dpjp as kddokter, mdk.namadokter as dpjp,
                        mcr.nama as penjamin,
                        array(select ticd.icd
                            from t_icd as ticd
                            where ticd.idxdaftar = tp.idxdaftar
                            and ticd.icd_verified IS NULL
                        ) AS icd,
                        array(select ticd.icd_verified
                            from t_icd as ticd
                            where ticd.idxdaftar = tp.idxdaftar
                            and ticd.icd_verified IS NOT NULL
                        ) AS icd_verif,
                        array(select trm.diagnosa_utama
                            from t_resumemedis as trm
                            where trm.idxrajal = tp.idxdaftar
                        ) AS icd_resume,
                        ');
        $query->join('m_pasien as mp',              'mp.nomr        = tp.nomr', 'left');
        $query->join('m_ruang as mr',               'mr.no          = tp.kdpoly', 'left');
        $query->join('t_billrajal AS tbl',          'tbl.idxdaftar  = tp.idxdaftar', 'left');
        $query->join('m_statuskeluar AS msk',       'msk.status     = tp.status', 'left');
        $query->join('t_resumemedis AS trm',        'trm.idxrajal   = tp.idxdaftar', 'left');
        $query->join('m_dokter AS mdk',             'mdk.kddokter   = trm.dpjp', 'left');
        $query->join('m_carabayar AS mcr',          'mcr.kode       = tp.kdcarabayar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->group_by('tp.tglreg, mr.nama, tp.pasienbaru, tp.nosep, mp.nama, mp.jeniskelamin, tp.nomr, tbl.nobill, msk.keterangan, tp.idxdaftar, mdk.kode_dpjp, mdk.namadokter, mcr.nama');
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('mr.nama', 'ASC');
        return $query->get('t_pendaftaran as tp ')->result();    
    }

    function pasiendetailranap(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select("
                        tp.masukpoly as masukklinik, tp.keluarpoly as keluarklinik, tp.st_asal_masuk as asalmasuk,
		                mrj.nama as namapoly,
                        ta.masukrs as masukrs, ta.keluarrs as keluarrs, 
                        mp.nomr as nomr, mp.nama as namapasien, mp.jeniskelamin as jeniskelamin,
                        mr.nama as namaruang, ms.keterangan as statuspulang,
                        rm.keadaan_pulang as keadaanpulang, mcr.nama as penjamin, mdk.namadokter as dpjp,
                        array(select ticd.icd
                            from t_icd as ticd
                            where ticd.idxdaftar = ta.id_admission
                            and ticd.status_rajal = '0'
                            and ticd.icd_verified is null
                            and ticd.st_sebab_mati is null
                        ) AS icd,
                        array(select ticd.icd
                            from t_icd as ticd
                            where ticd.idxdaftar = ta.id_admission
                            and ticd.status_rajal = '0'
                            and ticd.icd_verified is null
                            and ticd.st_sebab_mati is not null
                        ) AS icdmati,
                        array(select ticd.icd_verified
                            from t_icd as ticd
                            where ticd.idxdaftar = ta.id_admission
                            and ticd.status_rajal = '0'
                            and ticd.icd_verified is not null
                            and ticd.st_sebab_mati is null
                        ) AS icdverif,
                        array(select ticd.icd_verified
                            from t_icd as ticd
                            where ticd.idxdaftar = ta.id_admission
                            and ticd.status_rajal = '0'
                            and ticd.icd_verified is not null
                            and ticd.st_sebab_mati is not null
                        ) AS icdverifmati,
                        (select tb.nobill 
                            from t_billrajal tb
                            where tb.idxdaftar = ta.id_admission
                            limit 1
		                ) as nobilrajal,
                        (select tn.nobill 
                            from t_billranap tn
                            where tn.idxdaftar = ta.id_admission
                            limit 1
                        ) as nobilranap, 
                        (select SUM(tp.jumlah_bayar)
                            from t_billranap tn
                            join t_bayarranap bran on bran.idxbill = tn.idxbill and bran.st_carabayar = 2
                            join t_piutang tp on tp.idxbill = bran.idxbill  and tp.nobilling = bran.nobill and (tp.st_piutang is null or tp.st_piutang = 'LUNAS')
                            where tn.idxdaftar = ta.id_admission
                        ) as total_biaya, 
                        ");
        $query->join('m_pasien as mp',              'mp.nomr        = ta.nomr', 'left');
        $query->join('m_ruang as mr',               'mr.no          = ta.noruang', 'left');
        $query->join('t_resumepulang as rp',        'rp.idadmission = ta.id_admission', 'left');
        $query->join('t_resumemedis as rm',         'rm.idxranap    = ta.id_admission', 'left');
        $query->join('m_statuskeluar as ms',        'ms.status      = rp.statuspulang', 'left');
        $query->join('t_pendaftaran as tp',         'tp.idxdaftar   = ta.id_admission', 'left');
        $query->join('m_ruang as mrj',              'mrj.no         = tp.kdpoly', 'left');
        $query->join('m_carabayar as mcr',          'mcr.kode       = tp.kdcarabayar', 'left');
        $query->join('m_dokter as mdk',             'mdk.kddokter   = rm.dpjp', 'left');
        $query->order_by('ta.masukrs', 'ASC');
        $query->where("ta.keluarrs BETWEEN '".$awal."' and '".$akhir."'");
        return $query->get('t_admission as ta ')->result();    
    }    

    function pasienikpk(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.tglreg as tglregistrasi, tp.pasienbaru as statuspasien, tp.nosep as nosep, tp.nomr as nomr, tp.idxdaftar as idxdaftar,
                        mr.nama as klinik, 
                        mp.nama as namapasien, mp.jeniskelamin as jkelamin,
                        tbl.nobill as nobill, 
                        msk.keterangan as statuskeluar,
                        mcr.nama as carabayar
                        ');
        $query->join('m_pasien as mp',              'mp.nomr        = tp.nomr', 'left');
        $query->join('m_ruang as mr',               'mr.no          = tp.kdpoly', 'left');
        $query->join('t_billrajal AS tbl',          'tbl.idxdaftar  = tp.idxdaftar', 'left');
        $query->join('m_statuskeluar AS msk',       'msk.status     = tp.status', 'left');
        $query->join('m_carabayar AS mcr',          'mcr.kode       = tp.kdcarabayar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->group_by('tp.tglreg, mr.nama, tp.pasienbaru, tp.nosep, mp.nama, mp.jeniskelamin, tp.nomr, tbl.nobill, msk.keterangan, tp.idxdaftar,mcr.nama');
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('mr.nama', 'ASC');
        return $query->get('t_pendaftaran as tp ')->result();    
    }

    function pasienkeuangan(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.tglreg as tglregistrasi, tp.pasienbaru as statuspasien, tp.nosep as nosep, tp.nomr as nomr, tp.idxdaftar as idxdaftar, tp.st_asal_masuk as subsistem,
                        mp.nama as namapasien, mcr.nama as penjamin,

                        tbl.nobill as nobillrajal, tbl.kodetarif as kodetarifrajal, tbl.tarifrs as tarifrsrajal, tbl.tanggal as tglbillrajal,
                        mrrj.nama as ruangrajal, 
                        mdkj.namadokter as dokterrajal, mdkj.kdsmf as smfrajal,
                        tbrj.lunas as lunasbayarrajal, tbrj.tglbayar as tglbayarrajal, tbrj.jambayar as jambayarrajal, tbrj.jmbayar as jmlbayarrajal, tbrj.status as stbayarrajal, tbrj.nip as kasirrajal,
                        mtrj.kelompok_tarif as keltarifrajal,

                        tbp.nobill as nobillranap, tbp.kodetarif as kodetarifranap, tbp.tarifrs as tarifrsranap, tbp.tanggal as tglbillranap,
                        mrrp.nama as ruangranap, 
                        mdkp.namadokter as dokterranap, mdkp.kdsmf as smfranap,
                        tbrp.lunas as lunasbayarranap, tbrp.tglbayar as tglbayarranap, tbrp.jambayar as jambayarranap, tbrp.jmbayar as jmlbayarranap, tbrp.status as stbayarranap, tbrp.nip as kasirranap,
                        mtrp.kelompok_tarif as keltarifranap,
                        '); 
        $query->join('m_pasien as mp',              'mp.nomr        = tp.nomr', 'left');
        $query->join('t_billrajal AS tbl',          'tbl.idxdaftar  = tp.idxdaftar', 'left');
        $query->join('t_bayarrajal as tbrj',        'tbrj.idxbill   = tbl.idxbill', 'left');
        $query->join('m_ruang as mrrj',             'mrrj.no        = tbl.kdpoly', 'left');
        $query->join('m_dokter as mdkj',            'mdkj.kddokter  = tbl.kddokter', 'left');
        $query->join('m_tarif_rs as mtrj',          'mtrj.kode_tarif= tbl.kodetarif', 'left');

        $query->join('t_billranap AS tbp',          'tbp.idxdaftar  = tp.idxdaftar', 'left');
        $query->join('t_bayarranap as tbrp',        'tbrp.idxbill   = tbp.idxbill', 'left');
        $query->join('m_ruang as mrrp',             'mrrp.no        = tbp.kode_ruang', 'left');
        $query->join('m_dokter as mdkp',            'mdkp.kddokter  = tbp.kddokter', 'left');
        $query->join('m_tarif_rs as mtrp',          'mtrp.kode_tarif= tbp.kodetarif', 'left');

        $query->join('m_carabayar AS mcr',          'mcr.kode       = tp.kdcarabayar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('mp.nama', 'ASC');
        return $query->get('t_pendaftaran as tp')->result();    
    }

    function pasienkeuanganrajal(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.tglreg as tglregistrasi, tp.pasienbaru as statuspasien, tp.nosep as nosep, tp.nomr as nomr, tp.idxdaftar as idxdaftar, tp.st_asal_masuk as subsistem,
                        mp.nama as namapasien, mcr.nama as penjamin, mcb.nama_cara_bayar as carabayar,

                        tbl.nobill as nobillrajal, tbl.kodetarif as kodetarifrajal, tbl.tarifrs as tarifrsrajal, tbl.tanggal as tglbillrajal, tbl.tgl_verifikasi as tglverif, tbl.user_verifikasi as userverif,
                        mrrj.nama as ruangrajal, 
                        mdkj.namadokter as dokterrajal, mdkj.kdsmf as smfrajal,
                        tbrj.lunas as lunasbayarrajal, tbrj.tglbayar as tglbayarrajal, tbrj.jambayar as jambayarrajal, tbrj.jmbayar as jmlbayarrajal, tbrj.status as stbayarrajal, tbrj.nip as kasirrajal,
                        mtrj.kelompok_tarif as keltarifrajal, mtrj.uraian_tarif as uraiantarifrajal,
                        mpj.nama_penjamin as namapenjamin,
                        mpb.keterangan as payment
                        '); 
        $query->join('m_pasien as mp',              'mp.nomr            = tp.nomr', 'left');

        $query->join('t_billrajal AS tbl',          'tbl.idxdaftar      = tp.idxdaftar', 'left');
        $query->join('t_bayarrajal as tbrj',        'tbrj.idxbill       = tbl.idxbill', 'left');
        $query->join('m_ruang as mrrj',             'mrrj.no            = tbl.kdpoly', 'left');
        $query->join('m_dokter as mdkj',            'mdkj.kddokter      = tbl.kddokter', 'left');
        $query->join('m_tarif_rs as mtrj',          'mtrj.kode_tarif    = tbl.kodetarif', 'left');
        $query->join('m_cara_bayar AS mcb',         'mcb.id_cara_bayar  = tbrj.st_carabayar', 'left');
        $query->join('m_penjamin AS mpj',           'mpj.id_penjamin    = tbrj.kd_penjamin', 'left');
        $query->join('m_carabayar AS mcr',          'mcr.kode           = tbrj.carabayar', 'left');
        $query->join('m_pembayaran_bank AS mpb',    'mpb.id             = tbrj.st_payment', 'left');

        //$query->join('m_carabayar AS mcr',          'mcr.kode       = tp.kdcarabayar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('tbl.status !=',"BATAL");
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('mp.nama', 'ASC');
        return $query->get('t_pendaftaran as tp')->result();   
    }

    function pasienkeuanganranap(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d H:i:s", strtotime($pisah[0]." 00:00:00"));
            $akhir      = date("Y-m-d H:i:s", strtotime($pisah[1]." 23:59:00"));
        }else{
            $awal       = date("Y-m-d H:i:s");
            $akhir      = date("Y-m-d H:i:s");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.tglreg as tglregistrasi, tp.pasienbaru as statuspasien, tp.nosep as nosep, tp.nomr as nomr, tp.idxdaftar as idxdaftar, tad.st_asal_masuk as subsistem, tad.masukrs as masukrs, 
                        mp.nama as namapasien, mcr.nama as penjamin, mcb.nama_cara_bayar as carabayar,
                        tbp.nobill as nobillranap, tbp.kodetarif as kodetarifranap, tbp.tarifrs as tarifrsranap, tbp.tanggal as tglbillranap, tbp.tgl_verifikasi as tglverif, tbp.user_verifikasi as userverif,
                        mrrp.nama as ruangranap, 
                        mdkp.namadokter as dokterranap, mdkp.kdsmf as smfranap,
                        tbrp.lunas as lunasbayarranap, tbrp.tglbayar as tglbayarranap, tbrp.jambayar as jambayarranap, tbrp.jmbayar as jmlbayarranap, tbrp.status as stbayarranap, tbrp.nip as kasirranap,
                        mtrp.kelompok_tarif as keltarifranap, mtrp.uraian_tarif as uraiantarifranap,
                        mpj.nama_penjamin as namapenjamin,
                        mpb.keterangan as payment
                        '); 
        $query->join('m_pasien as mp',              'mp.nomr            = tp.nomr', 'left');
        $query->join('t_billranap AS tbp',          'tbp.idxdaftar      = tp.idxdaftar', 'left');
        $query->join('t_bayarranap as tbrp',        'tbrp.idxbill       = tbp.idxbill', 'left');
        $query->join('m_ruang as mrrp',             'mrrp.no            = tbp.kode_ruang', 'left');
        $query->join('m_dokter as mdkp',            'mdkp.kddokter      = tbp.kddokter', 'left');
        $query->join('m_tarif_rs as mtrp',          'mtrp.kode_tarif    = tbp.kodetarif', 'left');
        $query->join('m_cara_bayar AS mcb',         'mcb.id_cara_bayar  = tbrp.st_carabayar', 'left');
        $query->join('m_penjamin AS mpj',           'mpj.id_penjamin    = tbrp.kd_penjamin', 'left');
        $query->join('m_carabayar AS mcr',          'mcr.kode           = tbrp.carabayar', 'left');
        $query->join('t_admission AS tad',          'tad.id_admission   = tp.idxdaftar', 'left');
        $query->join('m_pembayaran_bank AS mpb',    'mpb.id             = tbrp.st_payment', 'left');

        $query->where('tad.masukrs >=', $awal);
        $query->where('tad.masukrs <=', $akhir);
        $query->where('tbp.nobill is NOT NULL', NULL, FALSE);
        $query->where('tbp.kodetarif !=',"X");
        $query->where('tbp.status !=',"BATAL");
        $query->order_by('tad.masukrs', 'ASC');
        $query->order_by('mp.nama', 'ASC');
        return $query->get('t_pendaftaran as tp')->result();   
    }

    function pasieniurbearanap(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('
            tp.tglreg as tglregistrasi, tp.pasienbaru as statuspasien, tp.nosep as nosep, tp.nomr as nomr, tp.idxdaftar as idxdaftar,
            mp.nama as namapasien, 
            tbp.nobill as nobillranap,
            mcr.nama as penjamin,
            tiu.hak_kelas as hakkelas, tiu.naik_kelas as naikkelas, tiu.nominal_naik_kelas as nominalnaik, tiu.selisih_biaya as selisih, tiu.st_verif as verif, tiu.tgl_verifikasi as tglverif, tiu.user_verifikasi as userverif, tiu.kode_tarif as kodetarif, tiu.jumlah_bayar as jumlahbayar,
            tiu.st_bayar as statusbayar, tiu.user_bayar as petugasbayar, tiu.tgl_bayar as tglbayar,
            mdkp.namadokter as dpjp, mdkp.kdsmf as smf,
            mr.nama as namaruang,
            ta.st_asal_masuk as subsistem, 
            ');
        $query->join('m_pasien as mp',              'mp.nomr            = tp.nomr', 'left');
        $query->join('t_billranap AS tbp',          'tbp.idxdaftar      = tp.idxdaftar', 'left');
        $query->join('m_carabayar AS mcr',          'mcr.kode           = tp.kdcarabayar', 'left');
        $query->join('t_iurbea AS tiu',             'tiu.idxdaftar      = tp.idxdaftar', 'left');
        $query->join('t_admission AS ta',           'ta.id_admission    = tp.idxdaftar', 'left');
        $query->join('m_dokter as mdkp',            'mdkp.kddokter      = ta.dokter_penanggungjawab', 'left');
        $query->join('m_ruang as mr',               'mr.no         = ta.noruang', 'left');

        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('tbp.nobill is NOT NULL', NULL, FALSE);
        $query->where('tiu.iurbea_id is NOT NULL', NULL, FALSE);
        $query->where('tbp.kodetarif !=',"X");
        $query->where('tbp.status !=',"BATAL");
        $query->order_by('tp.tglreg', 'ASC');
        $query->order_by('mp.nama', 'ASC');
        $query->group_by('tp.tglreg, tp.pasienbaru, tp.nosep, tp.nomr, tp.idxdaftar,mp.nama, 
                            tbp.nobill,mcr.nama, 
                            tiu.hak_kelas, tiu.naik_kelas, tiu.nominal_naik_kelas, tiu.selisih_biaya, tiu.st_verif, tiu.tgl_verifikasi, tiu.user_verifikasi, tiu.kode_tarif, tiu.jumlah_bayar, 
                            mdkp.namadokter, mdkp.kdsmf,
                            mr.nama, ta.st_asal_masuk, 
                            tiu.st_bayar, tiu.user_bayar, tiu.tgl_bayar
                        ');
        return $query->get('t_pendaftaran as tp')->result();   
    }

    function rekamedikrl32(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $bulan      = date("m", strtotime($pisah[0]));
            $tahun      = date("Y", strtotime($pisah[0]));
            $bulansebelum =  date('m', strtotime('-1 month', strtotime( $pisah[0] )));
            $tahunsebelum =  date('Y', strtotime('-1 month', strtotime( $pisah[0] )));
        }else{
            $bulan      = date("m");
            $tahun      = date("Y");
            $bulansebelum =  date('m', strtotime(' - 1 months'));
            $tahunsebelum =  date('Y', strtotime(' - 1 months'));
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select("ms.namasmf as smf,
                            array(
                                select count(*) from t_admission ta 
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab
                                    where  md.kdsmf = ms.kdsmf
                                    and extract (year from ta.keluarrs) = ".$tahun."
                                    and extract (month from ta.keluarrs) = ".$bulan."
                            ) as jenispelayanan,
                            array(
                                select count(*) from t_admission ta 
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab
                                    where  md.kdsmf = ms.kdsmf
                                    and extract (year from ta.keluarrs) = ".$tahunsebelum."
                                    and extract (month from ta.keluarrs) = ".$bulansebelum."
                            ) as pasienawal,
                            array(
                                select count(*) from t_admission ta 
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab
                                    where  md.kdsmf = ms.kdsmf
                                    and extract (year from ta.masukrs) = ".$tahun."
                                    and extract (month from ta.masukrs) = ".$bulan."
                            ) as pasienmasuk,
                            array(
                                select count(*) from t_pelimpahan_dpjp tpd 
                                    WHERE tpd.smf_utama != tpd.smf_pengganti
                                    and tpd.smf_pengganti = ms.kdsmf
                                    and extract (year from tpd.tgl_pelimpahan) = ".$tahun."
                                    and extract (month from tpd.tgl_pelimpahan) = ".$bulan." 
                                ) as pindahan,
                            array(
                                select count(*) from t_pelimpahan_dpjp tpd 
                                    WHERE tpd.smf_utama != tpd.smf_pengganti
                                    and tpd.smf_utama = ms.kdsmf
                                    and extract (year from tpd.tgl_pelimpahan) = ".$tahun."
                                    and extract (month from tpd.tgl_pelimpahan) = ".$bulan." 
                                ) as dipindahkan,
                            array(
                                select count(*) from t_admission ta
                                    left join t_resumepulang rp on ta.id_admission = rp.idadmission
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab 
                                    left join m_statuskeluar sk on sk.status = rp.statuspulang 
                                    where  md.kdsmf = ms.kdsmf
                                    and ta.st_meninggal is null
                                    and extract (year from ta.keluarrs) = ".$tahun."
                                    and extract (month from ta.keluarrs) = ".$bulan."
                            ) as keluarhidup,
                             array(
                                select extract(epoch from (ta.keluarrs -ta.masukrs))/3600 from t_admission ta
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab 
                                    left join t_pendaftaran tp on tp.idxdaftar = ta.id_admission 
                                    left join m_pasien mp on mp.nomr = ta.nomr 
                                    where  md.kdsmf = ms.kdsmf
                                    and mp.jeniskelamin = 'L'
                                    and ta.st_meninggal = '1'
                                    and extract (year from ta.keluarrs) = ".$tahun."
                                    and extract (month from ta.keluarrs) = ".$bulan."
                            ) as lakimeningal,
                            array(
                                select extract(epoch from (ta.keluarrs -ta.masukrs))/3600 from t_admission ta
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab 
                                    left join t_pendaftaran tp on tp.idxdaftar = ta.id_admission 
                                    left join m_pasien mp on mp.nomr = ta.nomr 
                                    where  md.kdsmf = ms.kdsmf
                                    and mp.jeniskelamin = 'P'
                                    and ta.st_meninggal = '1'
                                    and extract (year from ta.keluarrs) = ".$tahun."
                                    and extract (month from ta.keluarrs) = ".$bulan."
                            ) as perempuanmeningal,
                            array(
                                select sum(extract(day from ta.keluarrs-ta.masukrs)) from t_admission ta
                                    left join m_dokter md on md.kddokter = ta.dokter_penanggungjawab 
                                    left join t_pendaftaran tp on tp.idxdaftar = ta.id_admission 
                                    where  md.kdsmf = ms.kdsmf
                                    and extract (year from ta.keluarrs) = ".$tahun."
                                    and extract (month from ta.keluarrs) = ".$bulan."
                            )as jumlahlamadirawat
        ");
        $query->where('ms.aktif','1');
        return $query->get('m_smf as ms ')->result();    
    }    

    function jmlhariperawatan(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $bulan      = date("m", strtotime($pisah[0]));
            $tahun      = date("Y", strtotime($pisah[0]));
            $tglawal    = date("Y-m-d H:i:s", strtotime($tahun."-".$bulan."-01 00:00:00"));
            $tglakhir   = date("Y-m-d H:i:s", strtotime($tahun."-".$bulan."-31 23:59:99"));
        }else{
            $bulan      = date("m");
            $tahun      = date("Y");
            $tglawal    = date("Y-m-d H:i:s",strtotime(date("Y")."-".date("m")."-01 00:00:00"));
            $tglakhir   = date("Y-m-d H:i:s",strtotime(date("Y")."-".date("m")."-31 23:59:99"));
        }

            // $query = $this->load->database('datasim', TRUE);
            // $query->db->query("select tp.nomr, 
            // to_char(ta.masukrs, 'YYYY-MM-DD') as tglmasuk, ta.masukrs,
            // to_char(ta.keluarrs, 'YYYY-MM-DD') as tglkeluar, 
            // extract(day from ta.keluarrs-ta.masukrs) as lamarawat, 
            // md.kdsmf as msf FROM t_admission as ta 
            // LEFT JOIN m_dokter AS md ON md.kddokter = ta.dokter_penanggungjawab 
            // LEFT JOIN t_pendaftaran AS tp ON tp.idxdaftar = ta.id_admission 
            // WHERE 
            // extract (year from ta.keluarrs) = 2025
            // and extract (month from ta.keluarrs) = 01 
            // ORDER BY ta.masukrs asc");
        // $query->query("select tp.nomr, 
        //     to_char(ta.masukrs, 'YYYY-MM-DD') as tglmasuk, ta.masukrs,
        //     to_char(ta.keluarrs, 'YYYY-MM-DD') as tglkeluar, 
        //     extract(day from ta.keluarrs-ta.masukrs) as lamarawat, 
        //     md.kdsmf as msf FROM t_admission as ta 
        //     LEFT JOIN m_dokter AS md ON md.kddokter = ta.dokter_penanggungjawab 
        //     LEFT JOIN t_pendaftaran AS tp ON tp.idxdaftar = ta.id_admission 
        //     WHERE 
        //     extract (year from ta.keluarrs) = 2025
        //     and extract (month from ta.keluarrs) = 01 
        //     ORDER BY ta.masukrs asc");

        //return $query->result();
    }

    function penggunareseprajal(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tpar.tgl_pesan as tgl, tpar.norm as nomr, tpar.no as noresep, md.namadokter as namadokter, mr.nama as namaklinik, mr.no as kdpoli');
        $query->join('m_ruang AS mr',   'mr.no          = tpar.kdpoli ', 'left');
        $query->join('m_login AS ml',   'ml.nip         = tpar.nip ', 'left');
        $query->join('m_dokter AS md',  'md.kddokter    = ml.kddokter ', 'left');

        $query->where('mr.kelompok', 'IRJA');
        $query->where('mr.st_aktif', '1');
        $query->where('date(tpar.tgl_pesan) >=', $awal);
        $query->where('date(tpar.tgl_pesan) <=', $akhir);
        $query->group_by('tpar.tgl_pesan, tpar.norm, tpar.no, md.namadokter, mr.nama, mr.no');
        $query->order_by('mr.nama', 'ASC');
        $query->order_by('md.namadokter', 'ASC');
        return $query->get('t_permintaan_apotek_rajal AS tpar')->result();    
    }

    function ruangklinik(){
        $query = $this->load->database('datasim', TRUE);
        $query->select('mr.no as kdpoli, mr.nama as nama, mr.kelompok as kelompok');
        $query->where('mr.kelompok', 'IRJA');
        $query->where('mr.st_aktif', '1');
        $query->order_by('mr.nama', 'ASC');
        return $query->get('m_ruang AS mr')->result(); 
    }

    function disposedokigd(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tp.idxdaftar as idxdaftar, tp.nomr as nomr, tp.tglreg as tglreg, ms.keterangan as statuskeluar, md1.namadokter as dokterawal, md2.namadokter as disposisi, md2.kdsmf as disposisismf, md3.namadokter as terimadisposisi, md3.kdsmf as terimadisposisismf, ru.nama as tujuansmf,
                        array(
                            select tpa.tgl_pelimpahan from t_pelimpahan_dpjp tpa
                                left join m_dokter md on md.kddokter = tpa.dpjp_utama
                                left join m_ruang mr on mr."no" = tpa.noruang
                                where  tpa.id_admission  = tp.idxdaftar
                                order by tpa.tgl_pelimpahan asc
                        ) as tglpelimpah,
                        array(
                            select mr.nama from t_pelimpahan_dpjp tpa
                                left join m_dokter md on md.kddokter = tpa.dpjp_utama
                                left join m_ruang mr on mr."no" = tpa.noruang
                                where  tpa.id_admission  = tp.idxdaftar
                                order by tpa.tgl_pelimpahan asc
                        ) as klinikpelimpah,
                        array(
                            select md.namadokter from t_pelimpahan_dpjp tpa
                                left join m_dokter md on md.kddokter = tpa.dpjp_utama
                                left join m_ruang mr on mr."no" = tpa.noruang
                                where  tpa.id_admission  = tp.idxdaftar
                                order by tpa.tgl_pelimpahan asc
                        ) as dpjppelimpah,
                        array(
                            select tpa.smf_utama from t_pelimpahan_dpjp tpa
                                left join m_dokter md on md.kddokter = tpa.dpjp_utama
                                left join m_ruang mr on mr."no" = tpa.noruang
                                where  tpa.id_admission  = tp.idxdaftar
                                order by tpa.tgl_pelimpahan asc
                        ) as smfpelimpah,
                        array(
                            select md.namadokter from t_pelimpahan_dpjp tpa
                                left join m_dokter md on md.kddokter = tpa.dpjp_pengganti
                                left join m_ruang mr on mr."no" = tpa.noruang
                                where  tpa.id_admission  = tp.idxdaftar
                                order by tpa.tgl_pelimpahan asc
                        ) as dpjppengganti,
                        array(
                            select tpa.smf_pengganti from t_pelimpahan_dpjp tpa
                                left join m_dokter md on md.kddokter = tpa.dpjp_pengganti
                                left join m_ruang mr on mr."no" = tpa.noruang
                                where  tpa.id_admission  = tp.idxdaftar
                                order by tpa.tgl_pelimpahan asc
                        ) as smfpengganti,
                        array(
                            select md.namadokter from t_diagnosadanterapi tdt
                                left join m_dokter md on md.kddokter = tdt.kddokter
                                where  tdt.idxdaftar  = tp.idxdaftar
                                order by tdt.created_at asc
                        ) as diagnosaterapidokter,
                        array(
                            select md.kdsmf from t_diagnosadanterapi tdt
                                left join m_dokter md on md.kddokter = tdt.kddokter
                                where  tdt.idxdaftar  = tp.idxdaftar
                                order by tdt.created_at asc
                        ) as diagnosaterapismf,
                        array(
                            select tdt.created_at from t_diagnosadanterapi tdt
                                left join m_dokter md on md.kddokter = tdt.kddokter
                                where  tdt.idxdaftar  = tp.idxdaftar
                                order by tdt.created_at asc
                        ) as diagnosaterapidate,
                        array(
                            select md.namadokter from t_ugdtriase tu
                                left join m_dokter md on md.kddokter = tu.kode_dokter
                                where  tu.idxdaftar  = tp.idxdaftar
                                order by tu.tanggal_triase asc, tu.jam_triase asc
                        ) as triagedokter,
                        array(
                            select md.kdsmf from t_ugdtriase tu
                                left join m_dokter md on md.kddokter = tu.kode_dokter
                                where  tu.idxdaftar  = tp.idxdaftar
                                order by tu.tanggal_triase asc, tu.jam_triase asc
                        ) as triagesmf,
                        array(
                            select tu.tanggal_triase from t_ugdtriase tu
                                left join m_dokter md on md.kddokter = tu.kode_dokter
                                where  tu.idxdaftar  = tp.idxdaftar
                                order by tu.tanggal_triase asc, tu.jam_triase asc
                        ) as tgltriage,
                        array(
                            select tu.jam_triase from t_ugdtriase tu
                                left join m_dokter md on md.kddokter = tu.kode_dokter
                                where  tu.idxdaftar  = tp.idxdaftar
                                order by tu.tanggal_triase asc, tu.jam_triase asc
                        ) as jamtriage,
                        array(
                            select md.namadokter from t_resumemedis trs
                                left join m_dokter md on md.kddokter = trs.dpjp
                                where  trs.idxrajal  = tp.idxdaftar
                                order by trs.tgl_entri asc
                        ) as resumedokter, 
                        array(
                            select md.kdsmf from t_resumemedis trs
                                left join m_dokter md on md.kddokter = trs.dpjp
                                where  trs.idxrajal  = tp.idxdaftar
                                order by trs.tgl_entri asc
                        ) as resumesmf,
                        array(
                            select trs.tgl_entri from t_resumemedis trs
                                left join m_dokter md on md.kddokter = trs.dpjp
                                where  trs.idxrajal  = tp.idxdaftar
                                order by trs.tgl_entri asc
                        ) as resumedate
                        ');
        $query->join('t_disposisi AS td',       'tp.idxdaftar = td.idxdaftar', 'left');
        $query->join('m_statuskeluar AS ms',    'tp.status = ms.status', 'left');
        $query->join('m_ruang AS ru',           'td.smf_dituju = ru.no', 'left');
        $query->join('m_dokter AS md1',         'md1.kddokter = tp.kddokter', 'left');
        $query->join('m_dokter AS md2',         'md2.kddokter = td.kddokter', 'left');
        $query->join('m_dokter AS md3',         'md3.kddokter = td.kddokter_disposisi', 'left');

        $query->where('tp.st_asal_masuk', 'RAWAT JALAN EMERGENCY');
        $query->where('date(tp.tglreg) >=', $awal);
        $query->where('date(tp.tglreg) <=', $akhir);
        return $query->get('t_pendaftaran AS tp')->result();    
    }

    function farmasirajal(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tbr.kode_obat as kodeobat, sum(tbr.qty) as jmlqty, md.namadokter as namadokter');
        $query->join('m_dokter AS md',    'tbr.dokter = md.kddokter ', 'left');
        $query->where('date(tbr.tanggal) >=', $awal);
        $query->where('date(tbr.tanggal) <=', $akhir);
        $query->group_by('tbr.kode_obat,md.namadokter');
        $query->order_by('jmlqty', 'DESC');
        return $query->get('t_billobat_rajal AS tbr')->result();    
    }

    function farmasiranap(){
        if(isset($_POST['tanggal'])) {
            $dateall    = $this->input->post('tanggal');
            $pisah      = explode ("-", $dateall);
            $awal       = date("Y-m-d", strtotime($pisah[0]));
            $akhir      = date("Y-m-d", strtotime($pisah[1]));
        }else{
            $awal       = date("Y-m-d");
            $akhir      = date("Y-m-d");
        }

        $query = $this->load->database('datasim', TRUE);
        $query->select('tbr.kode_obat as kodeobat, sum(tbr.qty) as jmlqty, md.namadokter as namadokter');
        $query->join('m_dokter AS md',    'tbr.dokter = md.kddokter ', 'left');
        $query->where('date(tbr.tanggal) >=', $awal);
        $query->where('date(tbr.tanggal) <=', $akhir);
        $query->group_by('tbr.kode_obat,md.namadokter');
        $query->order_by('jmlqty', 'DESC');
        return $query->get('t_billobat_ranap AS tbr')->result();    
    }

    function dataobat(){
        $query = $this->load->database('default', TRUE);
        $query->select('mo.mo_kode as mo_kode, mo.mo_nama as mo_nama, mho.mho_hpp as harga');
        $query->join('master_harga_obat AS mho',    'mho.mho_kodebarang = mo.mo_kode ', 'left');
         $query->group_by('mo.mo_kode, mo.mo_nama');
        return $query->get('master_obat as mo')->result();
    }
}
?>