<?php

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

class M_simrs_kemenkes_lp extends CI_Model {
    function icd(){
        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('icd.icd_verified as dxutama, tp.st_asal_masuk as asalmasuk, i.jenis_penyakit as jepenyakit
                        ,count(tp.nomr) as allicd
                        ,count(tp.nomr) FILTER (WHERE tp.status = 2 ) AS mrs
                        ,count(tp.nomr) FILTER (WHERE tp.status != 2 ) AS krs');
        $query->join('t_icd as icd',    'icd.idxdaftar = tp.idxdaftar', 'left');
        $query->join('icd as i',        'i.icd_code = icd.icd_verified', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('icd.icd_verified_order', '1');
        $query->where('icd.st_sebab_mati is null', null, true);
        $query->group_by('icd.icd_verified, tp.st_asal_masuk, i.jenis_penyakit');
        return $query->get('t_pendaftaran as tp ')->result();
    }

    function billingrajal(){
        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('icd.icd_verified as dxutama, tp.st_asal_masuk as asalmasuk
                        ,count(tb.nomr) as allbill
                        ,count(tb.nomr) FILTER (WHERE tp.status = 2 ) AS mrs
                        ,count(tb.nomr) FILTER (WHERE tp.status != 2 ) AS krs
                        ');
        $query->join('t_icd as icd',             'icd.idxdaftar = tb.idxdaftar', 'left');
        $query->join('t_pendaftaran as tp',      'tp.idxdaftar = tb.idxdaftar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('icd.icd_verified_order', '1');
        $query->where('icd.st_sebab_mati is null', null, true);
        $query->group_by('icd.icd_verified, tp.st_asal_masuk');
        return $query->get('t_billrajal as tb ')->result();
    } 

    function orderbatalbillrajal(){
        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('icd.icd_verified as dxutama, tp.st_asal_masuk as asalmasuk
                        ,count(to2.nomr) as allbill
                        ,count(to2.nomr) FILTER (WHERE tp.status = 2 ) AS mrs
                        ,count(to2.nomr) FILTER (WHERE tp.status != 2) AS krs
                        ');
        $query->join('t_icd as icd',             'icd.idxdaftar = to2.idxdaftar', 'left');                
        $query->join('t_pendaftaran as tp',      'tp.idxdaftar  = to2.idxdaftar', 'left');
        $query->join('t_billrajal   as tbrj',    'to2.idxbill   = tbrj.idxbill ', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('to2.status_orderbatal', 'SELESAI');
        $query->where('tbrj.status', 'BATAL');
        $query->where('icd.icd_verified_order', '1');
        $query->where('icd.st_sebab_mati is null', null, true);
        $query->group_by('icd.icd_verified, tp.st_asal_masuk');
        return $query->get('t_orderbatalbill as to2 ')->result();
    }

    function billrajalpenunjang(){
        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.kodetarif as kodetarif
                            ,mr.kelompok as kelompok
                            ,tr.uraian_tarif as tarif, tr.kelompok_tarif as kelompoktarif, tr.kel_smf as kelompoksmf
                            ,count(tbr.kodetarif) AS allbill
                            ,count(tbr.kodetarif) FILTER (WHERE tp.status = 2) AS mrs
                            ,count(tbr.kodetarif) FILTER (WHERE tp.status != 2) AS krs
                            ,sum(tbr.tarifrs) AS alltrkrs
                            ,sum(tbr.tarifrs) FILTER (WHERE tp.status = 2) AS trmrs
                            ,sum(tbr.tarifrs) FILTER (WHERE tp.status != 2) AS trkrs
        ');
        $query->join('t_pendaftaran as tp',     'tp.idxdaftar   = tbr.idxdaftar', 'left');
        $query->join('m_ruang as mr',           'mr.no          = tbr.kode_ruang', 'left');
        $query->join('m_tarif_rs as tr',        'tr.kode_tarif  = tbr.kodetarif', 'left');
        $query->where('tbr.tanggal >=', $awal);
        $query->where('tbr.tanggal <=', $akhir);
        $query->where('tbr.status', 'SELESAI');
        //$query->where('mr.kelompok', 'IGD');
        //$query->or_where('tr.kel_smf', 'LABORATORIUM');
        //$query->or_where('tr.kel_smf', 'RADIOLOGI');
        //$query->order_by('mr.kelompok', 'ASC');
        $query->order_by('tr.kelompok_tarif', 'ASC');
        $query->group_by('tbr.kodetarif,mr.kelompok,tr.uraian_tarif,tr.kelompok_tarif,tr.kel_smf');
        return $query->get('t_billrajal as tbr ')->result();
    }

    function icdcm(){
        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.icd_verified as icd9, tp.st_asal_masuk as asalmasuk, icdcm.keterangan as icdcmketerangan
                            ,count(icm.icd_verified) AS allicdm
                            ,count(icm.icd_verified) FILTER (WHERE tp.status = 2) AS mrs
                            ,count(icm.icd_verified) FILTER (WHERE tp.status != 2) AS krs

                            ,sum(tbr.tarifrs) AS alltr
                            ,sum(tbr.tarifrs) FILTER (WHERE tp.status = 2) AS trmrs
                            ,sum(tbr.tarifrs) FILTER (WHERE tp.status != 2) AS trkrs
        ');
        $query->join('icd_cm as icdcm',         'icdcm.kode     = icm.icd_verified', 'left');
        $query->join('t_pendaftaran as tp',     'tp.idxdaftar   = icm.idxdaftar', 'left');
        $query->join('t_billrajal as tbr',      'tbr.idxdaftar  = tp.idxdaftar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('tbr.status', 'SELESAI');
        $query->where('icm.icd_verified is NOT NULL', NULL, FALSE);
        $query->group_by('icm.icd_verified, tp.st_asal_masuk, icdcm.keterangan');
        return $query->get('t_icd_cm as icm')->result();
    }


    function pxigd(){
        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.nosep as nosep, tp.masukpoly as masukpoly, tp.keluarpoly as keluarpoly, tp.diagnosa_utama as dxutama, 
                            tp.st_asal_masuk as asalmasuk, tp.kdrujuk as rujuk, tp.idxdaftar as idxdaftar,
                            i.jenis_penyakit as jepenyakit,
                            ps.jeniskelamin as gender, ps.tgllahir as tgllahir,
                            cb.nama as carabayar,
                            skl.keterangan as statuskeluar, skl.status as kdstatuskeluar, 
                            skc.nama_kecelakaan as statuskll,
                            ddt.anamnesa as anamnesa, ddt.tekanan_darah as tekanandarah, ddt.tinggi_badan as tinggibadan, ddt.berat_badan as beratbadan, ddt.golongan_darah as golongandarah, 
                            mr.no as kodepoly, mr.nama as namapoly,
                            dms.keluarrs as keluarrs
                        ');
        $query->join('icd                   as i',      'i.icd_code             = tp.diagnosa_utama', 'left');
        $query->join('m_pasien              as ps',     'ps.nomr                = tp.nomr', 'left');
        $query->join('m_carabayar           as cb',     'cb.kode                = tp.kdcarabayar', 'left');
        $query->join('m_statuskeluar        as skl',    'skl.status             = tp.status', 'left');
        $query->join('m_status_kecelakaan   as skc',    'skc.kode_kecelakaan    = tp.status_kecelakaan', 'left');
        $query->join('t_diagnosadanterapi   as ddt',    'ddt.idxdaftar          = tp.idxdaftar', 'left');
        $query->join('m_ruang               as mr',     'mr.no                  = tp.kdpoly', 'left');
        $query->join('t_admission           as dms',    'dms.id_admission       = tp.idxdaftar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('tp.st_asal_masuk', 'RAWAT JALAN EMERGENCY');
        return $query->get('t_pendaftaran as tp ')->result();
    }

    function icdcmigd(){
        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.icd_verified as icd9, icdcm.keterangan as icdcmketerangan, icm.idxdaftar as idxdaftar');
        $query->join('icd_cm as icdcm',         'icdcm.kode     = icm.icd_verified', 'left');
        $query->join('t_pendaftaran as tp',     'tp.idxdaftar   = icm.idxdaftar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('icm.icd_verified is NOT NULL', NULL, FALSE);
        $query->order_by('icm.icd_verified_order', 'ASC');
        return $query->get('t_icd_cm as icm')->result();
    }

    function icdigd(){
        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('icd.icd_code as icd10, icd.jenis_penyakit as icdketerangan, ticd.idxdaftar as idxdaftar');
        $query->join('icd as icd',              'icd.icd_code       = ticd.icd_verified', 'left');
        $query->join('t_pendaftaran as tp',     'tp.idxdaftar       = ticd.idxdaftar', 'left');
        $query->where('tp.tglreg >=', $awal);
        $query->where('tp.tglreg <=', $akhir);
        $query->where('ticd.icd_verified is NOT NULL', NULL, FALSE);
        $query->order_by('ticd.idx', 'ASC');
        return $query->get('t_icd as ticd')->result();
    }
}
?>