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

class Content_m extends CI_Model
{

    function __construct() 
    {
        parent::__construct();
    }

    public function get_no_odontogram($i)
    {   
        if($i == '1'){
            $no = '18';
        }else if($i=='2'){
            $no = '17';
        }else if($i=='3'){
            $no = '16';
        }else if($i=='4'){
            $no = '15';
        }else if($i=='5'){
            $no = '14';
        }else if($i=='6'){
            $no = '13';
        }else if($i=='7'){
            $no = '12';
        }else if($i=='8'){
            $no = '11';
        }else if($i=='9'){
            $no = '21';
        }else if($i=='10'){
            $no = '22';
        }else if($i=='11'){
            $no = '23';
        }else if($i=='12'){
            $no = '24';
        }else if($i=='13'){
            $no = '25';
        }else if($i=='14'){
            $no = '26';
        }else if($i=='15'){
            $no = '27';
        }else if($i=='16'){
            $no = '28';
        }else if($i=='17'){
            $no = '55';
        }else if($i=='18'){
            $no = '54';
        }else if($i=='19'){
            $no = '53';
        }else if($i=='20'){
            $no = '52';
        }else if($i=='21'){
            $no = '51';
        }else if($i=='22'){
            $no = '61';
        }else if($i=='23'){
            $no = '62';
        }else if($i=='24'){
            $no = '63';
        }else if($i=='25'){
            $no = '64';
        }else if($i=='26'){
            $no = '65';
        }else if($i=='27'){
            $no = '85';
        }else if($i=='28'){
            $no = '84';
        }else if($i=='29'){
            $no = '83';
        }else if($i=='30'){
            $no = '82';
        }else if($i=='31'){
            $no = '81';
        }else if($i=='32'){
            $no = '71';
        }else if($i=='33'){
            $no = '72';
        }else if($i=='34'){
            $no = '73';
        }else if($i=='35'){
            $no = '74';
        }else if($i=='36'){
            $no = '75';
        }else if($i=='37'){
            $no = '48';
        }else if($i=='38'){
            $no = '47';
        }else if($i=='39'){
            $no = '46';
        }else if($i=='40'){
            $no = '45';
        }else if($i=='41'){
            $no = '44';
        }else if($i=='42'){
            $no = '43';
        }else if($i=='43'){
            $no = '42';
        }else if($i=='44'){
            $no = '41';
        }else if($i=='45'){
            $no = '31';
        }else if($i=='46'){
            $no = '32';
        }else if($i=='47'){
            $no = '33';
        }else if($i=='48'){
            $no = '34';
        }else if($i=='49'){
            $no = '35';
        }else if($i=='50'){
            $no = '36';
        }else if($i=='51'){
            $no = '37';
        }else if($i=='52'){
            $no = '38';
        }

        return "&nbsp".$no;
    }

    public function paid_status($balance=NULL)
    {
        if($balance<=0){ return '<span class="label label-success">Paid in full</span>'; }
        else{ return '<span class="label label-danger">No due date</span>'; }
    }

    function get_ai($tbl)
    {       
        $row = $this->db->query("SHOW TABLE STATUS LIKE '$tbl' ")->row();       
        return $row->Auto_increment;
    }
    public function roman($bln)
    {
        if($bln == '1' ) return 'I';
        else if($bln == '2' ) return 'II';
        else if($bln == '3' ) return 'III';
        else if($bln == '4' ) return 'IV';
        else if($bln == '5' ) return 'V';
        else if($bln == '6' ) return 'VI';
        else if($bln == '7' ) return 'VII';
        else if($bln == '8' ) return 'VIII';
        else if($bln == '9' ) return 'IX';
        else if($bln == '10' ) return 'X';
        else if($bln == '11' ) return 'XI';
        else if($bln == '12' ) return 'XII';
        
    }
    public function array_in_dinamic()
    {

        $array = array('kode','name_fix','id_cabang','balance','u_jenj_lemb','u_jenj_org','photo','id_pool','level','parent','grup','fix','id_sp','id_sms','id_tindakan','account_chartcol','added_by','date_added','modified_by','date_modified','verified_by','date_verified','validate_by','date_validate','unit','id_grup_budget','id_sinkron','deleted');

        return $array;
    }
    
    public function get_kk($a, $b, $c, $c1, $d, $d1, $e, $f, $h, $i, $j, $k, $n, $o, $p, $q){
        $this->db->where('a_kk_a',$a);
        $this->db->where('a_kk_b',$b);
        $this->db->where('a_kk_c',$c);
        $this->db->where('a_kk_c1',$c1);
        $this->db->where('a_kk_d',$d);
        $this->db->where('a_kk_d1',$d1);
        $this->db->where('a_kk_e',$e);
        $this->db->where('a_kk_f',$f);
        $this->db->where('a_kk_h',$h);
        $this->db->where('a_kk_i',$i);
        $this->db->where('a_kk_j',$j);
        $this->db->where('a_kk_k',$k);
        $this->db->where('a_kk_n',$n);
        $this->db->where('a_kk_o',$o);
        $this->db->where('a_kk_p',$p);
        $this->db->where('a_kk_q',$q);

        return $this->db->get('pddikti_re_kebutuhan_khusus');
  
    }

    public function get_kelas($id_reg_pd)
    {
        $this->db->where('id_reg_pd', $id_reg_pd);
        return $this->db->get('pddikti_tr_nilai')->num_rows();
    }

    public function get_total_drp_kehadiran($id_reg_pd)
    {
        $this->db->from('siakad_tr_history_absen ha');
        $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = ha.id_reg_pd');
        $this->db->join('pddikti_tr_mahasiswa m','m.id_pd = mpt.id_pd');
        $this->db->join('pddikti_tr_mata_kuliah mk','mk.kode_mk = ha.kode_mk');
        $this->db->join('siakad_re_hadir h','h.huruf = ha.presensi');
        $this->db->where('ha.id_reg_pd',$id_reg_pd);
        $get_detail_absen= $this->db->get();

        $ttl_drp = 0;
        $i=0; 
        foreach ($get_detail_absen->result() as $key ) { 
            $ttl_drp=$ttl_drp+$key->point_drp;
            $i++;
        }

        return $ttl_drp;
    }

    public function get_last_value($tbl, $col, $field) 
    {
        $this->db->select('*');
        $this->db->order_by($col,'desc');
        $this->db->limit(1);
        $query = $this->db->get($tbl);
        return $query->row();
    }    

    public function get_total_drp_kehadiran_new($id_reg_pd,$id_pertemuan)
    {
        $this->db->from('siakad_tr_history_absen ha');
        $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = ha.id_reg_pd');
        $this->db->join('pddikti_tr_mahasiswa m','m.id_pd = mpt.id_pd');
        $this->db->join('pddikti_tr_mata_kuliah mk','mk.kode_mk = ha.kode_mk');
        $this->db->join('siakad_re_hadir h','h.huruf = ha.presensi');
        $this->db->where('ha.id_reg_pd',$id_reg_pd);
        $this->db->where('ha.id_pertemuan',$id_pertemuan);
        $get_detail_absen= $this->db->get();

        $ttl_drp = 0;
        $i=0; 
        foreach ($get_detail_absen->result() as $key ) { 
            $ttl_drp=$ttl_drp+$key->point_drp;
            $i++;
        }

        return $ttl_drp;
    }

    public function get_total_pelanggaran($id_reg_pd, $tgl_awal=null, $tgl_akhir=null)
    {
        //cek kelas
        //jika id_reg_pd sekarang ternyata di semester 3/4 pada kelas yang di ambil maka where semester 3/4 pada pelanggaran, 

        $id_kls = $this->Content_m->get_value('pddikti_tr_nilai','id_reg_pd',$id_reg_pd,'id_kls');
        $mulai_smt = $this->Content_m->get_value('pddikti_tr_kelas_kuliah','id_kls',$id_kls,'mulai_smt');
        $id_smt = $this->Content_m->get_value('pddikti_tr_kelas_kuliah','id_kls',$id_kls,'id_smt');

        
        $cek = substr($id_smt, '0','4');
        $smtr = substr($id_smt, '4');

        if($mulai_smt != $cek){
            $w = '(semester = "3" or semester = "4")';
                $this->db->where($w);
        }
        $this->db->select('SUM(point_pelanggaran) as tot_point');
        $this->db->where('id_reg_pd', $id_reg_pd);
        $this->db->where('deleted','0');
        if($tgl_awal != null)
            $this->db->where('tgl >=',$tgl_awal);
        if($tgl_akhir != null)
            $this->db->where('tgl <=',$tgl_akhir);


        $hasil = $this->db->get('siakad_tr_pelanggaran')->row()->tot_point;


        if($mulai_smt != $cek){
            $w = '(semester = "3" or semester = "4")';
                $this->db->where($w);
        }
        $this->db->select('ak.*, mk.kode_mk');
        $this->db->from('siakad_tr_absen_kuliah ak');
        $this->db->join('pddikti_tr_mata_kuliah mk', 'mk.id_mk = ak.id_mk');
        $this->db->where('ak.id_reg_pd',$id_reg_pd);
        $this->db->where('ak.deleted','0');
        $this->db->where('ak.absen','A');
        if($tgl_awal != null)
            $this->db->where('tgl_absen >=',$tgl_awal);
        if($tgl_akhir != null)
            $this->db->where('tgl_absen <=',$tgl_akhir);

        $where = "(mk.kode_mk != 'EXTRA' OR mk.kode_mk != 'KKS')";
        $this->db->where($where);
        $this->db->group_by('ak.tgl_absen');
        
        $ttl=$this->db->get()->num_rows();



         //Alpha (A) karena ijin melebihi 8
        $this->db->select('ak.*, mk.kode_mk');
        $this->db->from('siakad_tr_absen_kuliah ak');
        $this->db->join('pddikti_tr_mata_kuliah mk', 'mk.id_mk = ak.id_mk');
        $this->db->where('ak.id_reg_pd',$id_reg_pd);
        $this->db->where('ak.deleted','0');
        $this->db->where('ak.absen','I');
        $where = "(mk.kode_mk != 'EXTRA' OR mk.kode_mk != 'KKS')";
        $this->db->where($where);
        $this->db->group_by('ak.semester');
        $for=$this->db->get();
        //echo $this->db->last_query();

        $tot=0;
        foreach ($for->result() as $keyfor ) {
            
            $this->db->select('ak.*, mk.kode_mk');
            $this->db->from('siakad_tr_absen_kuliah ak');
            $this->db->join('pddikti_tr_mata_kuliah mk', 'mk.id_mk = ak.id_mk');
            $this->db->where('ak.id_reg_pd',$id_reg_pd);
            $this->db->where('ak.deleted','0');
            $this->db->where('ak.absen','I');
            $this->db->where('ak.semester',$keyfor->semester);
            $where = "(mk.kode_mk != 'EXTRA' OR mk.kode_mk != 'KKS')";
            $this->db->where($where);

            $this->db->group_by('ak.tgl_absen');
            $q=$this->db->get();
         
            $ii=1;
            foreach ($q->result() as $key ) { 
                if($ii > 8){ 
                    $tot=$tot+1; 
                }
                $ii++; 
            }
        }


        $ttl_isin_jadi_absen = $tot;
        
        //cek ijin dari id_reg_pd, jika lebih dari 8 sisanya jadi pelanggaran
        if ( $hasil > 0 || $ttl > 0 || $ttl_isin_jadi_absen > 0)
            return $hasil + $ttl + $ttl_isin_jadi_absen;
        else
            return 0;
    }

     public function cek_absen($id_reg_pd,$tgl_sekarang,$id_kls,$absen,$id_mk,$id_absen_jam)
    {
        //$this->db->select('*');
        $this->db->from('siakad_tr_absen_kuliah');
        $this->db->where('id_reg_pd',$id_reg_pd);
        $this->db->where('tgl_absen',$tgl_sekarang);
        $this->db->where('id_kls',$id_kls);
        $this->db->where('id_mk',$id_mk);
        $this->db->where('id_absen_jam',$id_absen_jam);
        $this->db->where('absen',$absen);
        $cek = $this->db->get()->num_rows();

        if($cek == 1){
            return true;
        }else{
            return false;
        }
    }

    public function cek_absen_new($id_reg_pd,$tgl_sekarang,$id_kls,$id_mk,$id_absen_jam)
    {
        if($id_reg_pd != null){$this->db->select('id_reg_pd, absen');}
        $this->db->from('siakad_tr_absen_kuliah');
        if($id_reg_pd != null){$this->db->where('id_reg_pd',$id_reg_pd);}
        $this->db->where('tgl_absen',$tgl_sekarang);
        $this->db->where('id_kls',$id_kls);
        $this->db->where('id_mk',$id_mk);
        $this->db->where('id_absen_jam',$id_absen_jam);
        $cek = $this->db->get();

        if($id_reg_pd != null){
            if($cek->num_rows()>0)
                return $cek->row()->absen;
            else
                return '';
        }else{
            return $cek;
        }
        
    }

   

    public function get_waktu_qooco($id_kls,$id_reg_pd, $id_pertemuan)
    {
        $this->db->where('id_pertemuan',$id_pertemuan);
        $this->db->where('id_reg_pd',$id_reg_pd);
        $get = $this->select_db('siakad_tr_pertemuan_presensi','id_kls',$id_kls)->num_rows();
        
        if($get > 0){
            $this->db->where('id_pertemuan',$id_pertemuan);
            $this->db->where('id_reg_pd',$id_reg_pd);
            return $this->get_row_custom('siakad_tr_pertemuan_presensi','id_kls',$id_kls)->time_qooco;
        }
        else
            return "00:00:00";

    }

    public function get_point_qooco($id_kls,$id_reg_pd, $id_pertemuan)
    {
        $this->db->where('id_pertemuan',$id_pertemuan);
        $this->db->where('id_reg_pd',$id_reg_pd);
        $get = $this->select_db('siakad_tr_pertemuan_presensi','id_kls',$id_kls)->num_rows();
        
        if($get > 0)
            return $this->get_row_custom('siakad_tr_pertemuan_presensi','id_kls',$id_kls)->point_qooco;
        else
            return "00:00:00";

    }

    public function get_total_pelanggaran_new($id_reg_pd,$id_pertemuan)
    {
        $this->db->select('SUM(point_pelanggaran) as tot_point');
        $this->db->where('id_pertemuan',$id_pertemuan);
        $this->db->where('id_reg_pd', $id_reg_pd);
        $this->db->where('deleted','0');
        
        $hasil = $this->db->get('siakad_tr_pelanggaran')->row()->tot_point;
        if ( $hasil > 0)
            return $hasil;
        else
            return 0;
    }

    

    public function get_drp($kode_mk, $id_reg_pd, $id_pertemuan){
        $this->db->from('siakad_tr_history_absen ha');
        $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = ha.id_reg_pd');
        $this->db->join('pddikti_tr_mahasiswa m','m.id_pd = mpt.id_pd');
        $this->db->join('pddikti_tr_mata_kuliah mk','mk.kode_mk = ha.kode_mk');
        $this->db->join('siakad_re_hadir h','h.huruf = ha.presensi');
        $this->db->where('ha.id_reg_pd',$id_reg_pd);
        $this->db->where('ha.kode_mk',$kode_mk);
        $this->db->where('ha.id_pertemuan',$id_pertemuan);
        $get_detail_absen= $this->db->get();

        $ttl_drp = 0; $ttl_sk=0;
        $i=0; 
        foreach ($get_detail_absen->result() as $key ) { 
            $ttl_drp=$ttl_drp+$key->point_drp; $ttl_sk=$ttl_sk+$key->point_sk;
            $i++;
        }

        $mak_drp = ceil(($i/10*2));

        if($ttl_drp >= $mak_drp)
            return true;
        else
            return false;
        //return $ttl_drp;
    }

    public function currency($n = 0)
    {
        $cur = number_format( (float) abs($n), 2, '.', ',');
        if($n >=0){ return "<span>Rp. ".$cur."</span>";}
        else{ return "<span class='red'>- Rp. ".$cur."</span>";}
    }

    public function currency2($n = 0)
    {
        $cur = number_format( (float) abs($n), 2, '.', ',');
        if($n >=0){ return "<span>Rp. ".$cur."</span>";}
        else{ return "<span style='color:green;'>- Rp. ".$cur."</span>";}
    }

    public function currency_edit($n = 0)
    {
        $cur = number_format( (float) abs($n), 2, '.', ',');
        if($n >=0){ return "".$cur."";}
        else{ return "-".$cur."";}
    }

    public function currency_edit2($n = 0)
    {
        $cur = number_format( (float) abs($n), 0, '.', ',');
        if($n >=0){ return "".$cur."";}
        else{ return "-".$cur."";}
    }



    
    public function get_autocomplete()
    {
        $this->db->like('nm_wil',$this->input->post('queryString'));
        return $this->db->get('table', 10);    

    }
    public function select_db($tbl,$col=NULL, $col_val=NULL,$sort_col=NULL, $sort='ASC')
    {
        $this->db->where('deleted','0');
        if($col != NULL) {$this->db->where($col,$col_val); }        
        if($sort_col != NULL) {$this->db->order_by($sort_col, $sort); }
        return $this->db->get($tbl);
    }
    public function select_tbl_temp($tbl, $col=NULL, $col_val=NULL, $sort_col=NULL, $sort='ASC')
    {
        if($col != NULL) {$this->db->where($col, $col_val); }        
        if($sort_col != NULL) {$this->db->order_by($sort_col, $sort); }
        return $this->db->get($tbl);
    }

     public function select_db_custom($tbl,$col=NULL, $col_val=NULL,$sort_col=NULL, $sort='ASC')
    {
        $this->db->where('deleted','0');
        if($col != NULL) {$this->db->where($col,$col_val); }        
        if($sort_col != NULL) {$this->db->order_by($sort_col, $sort); }
        return $this->db->get($tbl);
    }

    public function get_row($tbl, $col, $val)
    {
        $this->db->where($col,$val);
        $query = $this->db->get($tbl);
        return $query->row();   
    }
    public function get_data($tbl, $id, $col)
    {
        if($id==NULL || $id==''){ return '';}
        else if(!$this->exists($tbl, 'id', $id)){ return '';  }
        $this->db->where('id',$id);
        $query = $this->db->get($tbl);
        return $query->row()->$col;         
    }

    public function msg_box($class, $msg)
    {
        
        if($class=='success'){
            $this->session->set_flashdata('msg',"<div class='alert alert-success'><strong>Berhasil !!</strong><br>$msg</div>");
        }
        else{ $this->session->set_flashdata('msg',"<div class='alert alert-warning'><strong>Terjadi Kesalahan !!</strong><br>$msg</div>"); }
    }


    public function get_values($tbl=NULL, $col=NULL, $val=NULL, $field=NULL)
    {
        if($tbl==NULL || $col==NULL || $val==NULL || $field==NULL){ return '';}
        else if(!$this->existss($tbl, $col, $val)){ return '';  }
        $this->db->where($col,$val);
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }


    public function get_value($tbl=NULL, $col=NULL, $val=NULL, $field=NULL)
    {
        if($tbl==NULL || $col==NULL || $val==NULL || $field==NULL){ return '';}
        else if(!$this->exists($tbl, $col, $val)){ return '';  }
        $this->db->where($col,$val);
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }



    
    public function get_value_raw($tbl=NULL, $col=NULL, $val=NULL, $field=NULL)
    {
        $this->db->where($col,$val);
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }


    public function get_row_custom($tbl,$where, $id)
    {
        $this->db->where($where,$id);
        $query = $this->db->get($tbl);
        return $query->row();   
    }

     public function get_row_custom2($tbl,$where, $id, $where2, $id2)
    {
        $this->db->where($where,$id);
        $this->db->where($where2,$id2);
        $query = $this->db->get($tbl);
        return $query->row();   
    }

    public function get_peserta_kelas_ifnull($id)
    {
        $this->db->select('n.*, mpt.*, m.*');
        $this->db->from('pddikti_tr_nilai n');
        $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = n.id_reg_pd','left');
        $this->db->join('pddikti_tr_mahasiswa m','m.id_pd = mpt.id_pd');
        //$this->db->join('siakad_tr_pertemuan_presensi pp', 'pp.id_reg_pd = mpt.id_reg_pd','left');
        //$this->db->join('siakad_re_list_job_training jt', 'jt.id_reg_pd = mpt.id_reg_pd','left');
        $this->db->where('n.id_kls',$id);
       // $this->db->where('pp.id_pertemuan','1');
        $this->db->where('n.deleted','0');
        $this->db->group_by('n.id_reg_pd');
        return $this->db->get();

    }

    public function get_history_ojt_by_pd ($id)
    {
         $this->db->select('*');
            $this->db->from('siakad_tr_on_the_job_training n');
            $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = n.id_reg_pd','left');
            $this->db->join('pddikti_tr_mahasiswa m','m.id_pd = mpt.id_pd');
            $this->db->where('n.id_reg_pd',$id);
            $this->db->where('n.deleted','0');
            //$this->db->group_by('n.id_reg_pd');
            return $this->db->get();

    }

    public function get_peserta_kelas_ifnot($id,$pertemuan)
    {
         $this->db->select('n.*, mpt.*, m.*');
            $this->db->from('pddikti_tr_nilai n');
            $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = n.id_reg_pd','left');
            $this->db->join('pddikti_tr_mahasiswa m','m.id_pd = mpt.id_pd');
            $this->db->join('siakad_tr_pertemuan_presensi pp', 'pp.id_reg_pd = mpt.id_reg_pd','left');
            $this->db->where('n.id_kls',$id);
            $this->db->where('pp.id_pertemuan',$pertemuan);
            $this->db->where('n.deleted','0');
            $this->db->group_by('n.id_reg_pd');
            return $this->db->get();
    }

    public function select_db_custom2($tbl, $where, $id, $where2, $id2, $sort_col=NULL, $sort='ASC')
    {
        //$this->db->where('deleted','0');
        $this->db->where($where,$id);
        $this->db->where($where2,$id2);
        if($sort_col != NULL) {$this->db->order_by($sort_col, $sort); }
        return $this->db->get($tbl);
    }
    
    public function exists($tabel,$col,$value)
    {
        $this->db->where($col,$value);
        $query = $this->db->get($tabel);        
        return ($query->num_rows()==1);
    }

    public function existss($tabel,$col,$value)
    {
        $this->db->where($col,$value);
        $query = $this->db->get($tabel);        
        return ($query->num_rows()>0);
    }

    public function format_number($n = '')
    {
        return ($n === '') ? '' : number_format( (float) $n, 0, ',', '.');
    }
    
    public function select_month()
    {
        $options = array();
        $options += array('01' => 'Januari');
        $options += array('02' => 'Februari');
        $options += array('03' => 'Maret');
        $options += array('04' => 'April');
        $options += array('05' => 'Mei');
        $options += array('06' => 'Juni');
        $options += array('07' => 'Juli');
        $options += array('08' => 'Agustus');
        $options += array('09' => 'September');
        $options += array('10' => 'Oktober');
        $options += array('11' => 'November');
        $options += array('12' => 'Desember');
        return $options;
    }
    
    public function nama_bln($bln)
    {
        if($bln == '01') return 'Januari';
        else if($bln == '02') return 'Februari';
        else if($bln == '03') return 'Maret';
        else if($bln == '04') return 'April';
        else if($bln == '05') return 'Mei';
        else if($bln == '06') return 'Juni';
        else if($bln == '07') return 'Juli';
        else if($bln == '08') return 'Agustus';
        else if($bln == '09') return 'September';
        else if($bln == '10') return 'Oktober';
        else if($bln == '11') return 'November';
        else if($bln == '12') return 'Desember';
    }

    function insert_registrasi_online($data)
    {
        return $this->db->insert('permohonan_daftar_mhs',$data);
    }

    function insert_db($tabel, $data)
    {
        if($this->db->insert($tabel,$data))
        return true;
        return false;
    }

    function update_db($tabel, $col, $val, $data)
    {
        $this->db->where($col,$val);
        if($this->db->update($tabel,$data))
        return true;
        return false;
    }

    function update_db_custom($tabel, $col, $val, $data)
    {
        $this->db->where($col,$val);
        if($this->db->update($tabel,$data))
        return true;
        return false;
    }


    function update_db2($tabel, $col, $val, $col2, $val2, $data)
    {
        $this->db->where($col,$val);
        $this->db->where($col2,$val2);
        if($this->db->update($tabel,$data))
        return true;
        return false;
    }

    function update_db3($tabel, $col, $val, $col2, $val2, $col3, $val3, $data)
    {
        $this->db->where($col,$val);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        if($this->db->update($tabel,$data))
        return true;
        return false;
    }


    function delete_db($tabel, $col, $val)
    {
        $this->db->where($col, $val);
        if($this->db->delete($tabel))
        return true;
        return false;
    }

    function delete_db_custom($tabel, $col, $val)
    {
        $this->db->where($col, $val);
        if($this->db->delete($tabel))
        return true;
        return false;
    }
   
    /*
    function get_all_admin_non_dosen()
    {
        $this->db->order_by('nm_sdm','ASC');
        $this->db->where('deleted','0');
        $this->db->where('id_cabang !=','0');
        return $this->db->get('global_employee_non_dosen');   
    }
    */

    /*
    function get_admin_non_dosen($userid)
    {
        $this->db->where('id_sdm',$userid);
        $this->db->where('deleted','0');
        $this->db->where('id_cabang !=','0');
        return $this->db->get('global_employee_non_dosen');   
    }
    */

    function get_all_dosen()
    {
        $this->db->order_by('nm_sdm','ASC');
        return $this->db->get('pddikti_tr_dosen');   
    }

    public function insert_batch_db($tabel, $data)
    {
        if($this->db->insert_batch($tabel,$data))
        return true;
        return false;
    }

    public function exists2($tabel,$col1,$val1,$col2,$val2)
    {
        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where('deleted','0');
        $query = $this->db->get($tabel);        
        return ($query->num_rows()==1);
    }

    public function get_value2($tbl=NULL, $col1=NULL, $val1=NULL, $col2=NULL, $val2=NULL , $field=NULL)
    {
        if($tbl==NULL || $col1==NULL || $val1==NULL || $field==NULL){ return '';}
        else if(!$this->exists2($tbl, $col1, $val1, $col2, $val2)){ return ''; }

        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
     
        $this->db->where('deleted','0');
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }


    
    public function exists3($tabel,$col1,$val1,$col2,$val2,$col3,$val3)
    {
        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        $this->db->where('deleted','0');
        $query = $this->db->get($tabel);        
        return ($query->num_rows()==1);
    }

    public function get_value3($tbl=NULL, $col1=NULL, $val1=NULL, $col2=NULL, $val2=NULL, $col3=NULL, $val3=NULL, $field=NULL)
    {
        if($tbl==NULL || $col1==NULL || $val1==NULL || $field==NULL){ return '';}
        else if(!$this->exists3($tbl, $col1, $val1, $col2, $val2, $col3, $val3)){ return '';  }


        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        $this->db->where('deleted','0');
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }

    public function exists4($tabel,$col1,$val1,$col2,$val2,$col3,$val3,$col4,$val4)
    {
        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        $this->db->where($col4,$val4);
        $this->db->where('deleted','0');
        $query = $this->db->get($tabel);        
        return ($query->num_rows()==1);
    }

    public function get_value4($tbl=NULL, $col1=NULL, $val1=NULL, $col2=NULL, $val2=NULL, $col3=NULL, $val3=NULL, $col4=NULL, $val4=NULL, $field=NULL)
    {
        if($tbl==NULL || $col1==NULL || $val1==NULL || $field==NULL){ return '';}
        else if(!$this->exists4($tbl, $col1, $val1, $col2, $val2, $col3, $val3, $col4, $val4)){ return ''; }

        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        $this->db->where($col4,$val4);
        $this->db->where('deleted','0');
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }

    public function exists5($tabel,$col1,$val1,$col2,$val2,$col3,$val3,$col4,$val4,$col5,$val5)
    {
        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        $this->db->where($col4,$val4);
        $this->db->where($col5,$val5);
        $this->db->where('deleted','0');
        $query = $this->db->get($tabel);        
        return ($query->num_rows()==1);
    }

    public function get_value5($tbl=NULL, $col1=NULL, $val1=NULL, $col2=NULL, $val2=NULL, $col3=NULL, $val3=NULL, $col4=NULL, $val4=NULL, $col5=NULL, $val5=NULL, $field=NULL)
    {
        if($tbl==NULL || $col1==NULL || $val1==NULL || $field==NULL){ return '';}
        else if(!$this->exists5($tbl, $col1, $val1, $col2, $val2, $col3, $val3, $col4, $val4, $col5, $val5)){ return ''; }

        $this->db->where($col1,$val1);
        $this->db->where($col2,$val2);
        $this->db->where($col3,$val3);
        $this->db->where($col4,$val4);
        $this->db->where($col5,$val5);
        $this->db->where('deleted','0');
        $query = $this->db->get($tbl);
        return $query->row()->$field;
    }

    public function get_value_penarikan($id_sdm)
    {
        $this->db->where('id_sdm',$id_sdm);
        $this->db->select('sum(jumlah) as jum');
        $this->db->from('cc_reward_take');
        return $this->db->get()->row()->jum;
    }

    public function cek_tgl_qooco($tgl)
    {

    }


    public function get_sum_day($dari_tanggal, $sampai_tanggal){
        
        $qooco_dari_tanggal = $dari_tanggal;
        $qooco_sampai_tanggal = $sampai_tanggal;

        $datetime1=date_create($qooco_dari_tanggal);
        $datetime2 = date_create($qooco_sampai_tanggal);
        $interval = date_diff($datetime1, $datetime2);
        $d= $interval->format('%a');

        return $d;
    }
    public function get_timesecond_eachday($dari_tanggal, $sampai_tanggal, $str_time)
    {

        $d = $this->get_sum_day($dari_tanggal,$sampai_tanggal);

        $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
        sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
        $time_seconds = $hours*3600 + $minutes*60 + $seconds;
        return $time_seconds/$d;

    }

    public function set_timesecond_to_time($time_second)
    {
        $t = round($time_second);
        return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
    }

    public function get_duration_of_study($dari_tanggal, $sampai_tanggal, $id_reg_pd)
    {
        $this->db->select('sum(time_second) as time_second');
        $this->db->from('siakad_tr_qooco');
        $this->db->where('id_reg_pd',$id_reg_pd);
        $this->db->where('deleted','0');
        $this->db->where('tanggal >=',$dari_tanggal);
        $this->db->where('tanggal <=',$sampai_tanggal);
        return $this->db->get()->row()->time_second;
    }

    public function get_point_qooco_new($time_second){

        if($time_second >= 21600){
            return 0;
        }else{
            return 1;
        }
        
    }

    public function akd_nilai_rata_rata($id_reg_pd)
    {
        $neout = [];
        $nft = [];
        $this->db->select('*');
        $this->db->from('siakad_tr_nilai_end_of_unit_test');
        $this->db->where('id_reg_pd',$id_reg_pd);
        $this->db->where('deleted','0');
        $q = $this->db->get();
        $i=0;
        foreach ($q->result() as $key ) {
          //echo "$i $key->nilai_end_of_unit_test<br>";
            
          $neout[$i] = ($key->nilai_end_of_unit_test/10)*7;
          $nft[$i] = ($key->nilai_final_test/10)*3;
          $i++;
        }

       

        $r = 0;
        for ($i=0; $i < count($neout); $i++) { 
            $r = $r + ($neout[$i]+$nft[$i]);
            //echo ($neout[$i]+$nft[$i])."<br>";
        }

        if(count($neout) != 0){
            $rata_rata = $r/count($neout);
        }else{
            $rata_rata = 0;
        }
        

        return round($rata_rata);
    }

    public function akd_ketidakhadiran($id_reg_pd)
    {

        
        $this->db->select('absen');
        $this->db->from('siakad_tr_absen_kuliah');
        $this->db->where('id_reg_pd',$id_reg_pd);
        $this->db->where_in('absen',array('A','I'));
        $this->db->where('deleted','0');
        $this->db->where('id_kls !=','');
        $this->db->group_by('tgl_absen');
        $q = $this->db->get();

        return $q->num_rows();


    }

    public function kks_kehadiran($id_reg_pd)
    {

        
        $this->db->select('absen');
        $this->db->from('siakad_tr_absen_kuliah');
        $this->db->where('id_reg_pd',$id_reg_pd);
        //$this->db->where_in('absen',array('A','I'));
        $this->db->where('deleted','0');
        $this->db->where('id_kls','');
        $this->db->group_by('tgl_absen');
        $q = $this->db->get();
        //echo $this->db->last_query();
        
        /*
        $this->db->select('absen');
        $this->db->from('siakad_tr_absen_kuliah');
        $this->db->where('id_reg_pd',$id_reg_pd);
        $this->db->where_not_in('absen',array('A','I'));
        $this->db->where('deleted','0');
        $this->db->where('id_kls','');
        $this->db->group_by('tgl_absen');
        $qq = $this->db->get();
        */

        $ttl = 0;
        $thadir = 0;
        foreach ($q->result() as $key ) {
            if($key->absen == 'A' || $key->absen == 'I' || $key->absen == 'S'){
                $thadir++;
            }
            $ttl++;
        }
        
        if($ttl != 0){
            $kehadiran = $ttl-$thadir;
            $total_seluruh_absen = $ttl;
            $persen_hadir = ($kehadiran/$total_seluruh_absen)*100;
            return round($persen_hadir);
        }else{
            return 0;
        }
        

        //
        //return $kehadiran;

    }

    public function peringkat_medi_cc($id_reg_pd,$id_sp)
    {
        $id_cabang = $this->get_value('pddikti_tr_mahasiswa_pt','id_reg_pd',$id_reg_pd,'id_cabang');
        $this->db->select('mpt.id_reg_pd, m.nama_lengkap, m.kupon_code, mpt.nipd , count(mpt.nipd) as jumlah_bawa');
        $this->db->from('permohonan_daftar_mhs m');
        $this->db->join('cc_generated_code cc','cc.kupon_code = m.kupon_code');
        $this->db->join('pddikti_tr_mahasiswa_pt mpt','mpt.id_reg_pd = cc.id_sdm');
        $this->db->where('m.kupon_code !=','');
        $this->db->where('mpt.id_sp',$id_sp);
        $this->db->where('mpt.id_cabang',$id_cabang);
        //$this->db->where('mpt.id_reg_pd',$id_reg_pd);
        $this->db->where_in('m.status_complete',array('4','99'));
        $this->db->group_by('mpt.nipd');
        $this->db->order_by('jumlah_bawa','desc');
        $q = $this->db->get();

        $peringkat=0;
        $jumlah_bawa = 0;
        $ada = 0;
        foreach ($q->result() as $key ) {
            if($jumlah_bawa != $key->jumlah_bawa)
            {
                $peringkat++;
            }
            $jumlah_bawa = $key->jumlah_bawa;
            if($key->id_reg_pd == $id_reg_pd){
                $ada = 1;
                break;
            }
           
        }

        if($ada == 1){
            $data['jumlah_bawa'] = $jumlah_bawa;
            $data['peringkat'] = $peringkat;
        }else{
            $data['jumlah_bawa'] = 0;
            $data['peringkat'] = 0;
        }
        

        return $data;
    }

    public function point_restasi($id_reg_pd)
    {
        
        $this->db->select('sum(point_drp) as point_drp');
        $this->db->from('siakad_tr_drp_motor_kks_prestasi_detail pd');
        $this->db->join('siakad_tr_drp_motor_kks_prestasi p','p.id_drp_motor_kks_prestasi = pd.id_drp_motor_kks_prestasi');
        $this->db->where('id_reg_pd',$id_reg_pd);
        $q = $this->db->get();

        if($q->row()->point_drp == ''){
            return '0.0';
        }else if($q->row()->point_drp > 20){
            return '20.0';
        }else{
            return $q->row()->point_drp;
        }
        

    }

    public function get_peringkat($id_reg_pd, $id_sp, $id_cabang, $mulai_smt)
    {
        $i=1;
        $this->db->select('mpt.drp_ttl_point, mpt.id_reg_pd');
        $this->db->from('pddikti_tr_mahasiswa m');
        $this->db->join('pddikti_tr_mahasiswa_pt mpt','m.id_pd = mpt.id_pd');
        $this->db->where('mpt.id_sp',$id_sp);
        $this->db->where('mpt.id_cabang',$id_cabang);
        $this->db->where('mpt.mulai_smt',$mulai_smt);
        $this->db->order_by('mpt.drp_ttl_point','desc');
        $q = $this->db->get();
        foreach ($q->result() as $key ) {

            if($key->id_reg_pd == $id_reg_pd){
                return $i;
                break;
            }
            $i++;
        }
    }

    public function get_nilai($id_reg_pd, $group=null)
    {
        /*
        $this->db->select('*');
        $this->db->from('siakad_tr_nilai_end_of_unit_test n');
        $this->db->join('pddikti_tr_mata_kuliah mk', 'n.id_mk = mk.id_mk');
        $this->db->where('n.id_reg_pd',$id_reg_pd);
        $this->db->where('n.deleted','0');
        $this->db->order_by('n.id_mk_sub');
        */


        $this->db->select('*');
        $this->db->from('siakad_tr_nilai_end_of_unit_test n');
        $this->db->join('siakad_tr_mata_kuliah_sub mk', 'n.id_mk_sub = mk.id_mata_kuliah_sub');
        $this->db->where('n.id_reg_pd',$id_reg_pd);
        $this->db->where('n.deleted','0');
        if($group != null){
            $this->db->where('group',$group);
        }
       
        $this->db->order_by('mk.urutan','asc');

        return $this->db->get();



    }

    public function get_kunjungan_pasien()
    {
        $this->db->select('*');
        $this->db->from('bbmc_tr_pasien p');
        $this->db->join('bbmc_tr_kunjungan k','k.id_re_pasien = p.id_re_pasien');
        $this->db->where('k.deleted','0');
        $this->db->where('p.deleted','0');
        //$this->db->order_by('k.tgl_kunjungan','asc');
       
        //$this->db->order_by('')
        $q = $this->db->get();

        return $q;

    }

    public function get_tipe_pasien($id_re_pasien)
    {
        $id_tipe_pasien = $this->get_value('bbmc_tr_pasien','id_re_pasien',$id_re_pasien,'id_type_pasien');
        $tipe_pasien = $this->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$id_tipe_pasien,'tipe_pasien');

        return $tipe_pasien;
    }

    public function get_tindakan_pasien($id_re_kunjungan)
    {
        $array = array();
        $this->db->where('id_re_kunjungan',$id_re_kunjungan);
        $q = $this->select_db('bbmc_tr_tindakan');
        //echo $this->db->last_query();

        foreach ($q->result() as $key ) {
            array_push($array, $key->tindakan);
        }

        $this->db->where('id_re_kunjungan',$id_re_kunjungan);
        foreach ($this->select_db('bbmc_tr_laboratorium')->result() as $key ) {
            array_push($array, $key->tindakan_lab);
        }

        $this->db->where('id_re_kunjungan',$id_re_kunjungan);
        foreach ($this->select_db('bbmc_tr_radiologi')->result() as $key ) {
            array_push($array, $key->tindakan_radiologi);
        }

        return $array;

        //obat 
        
    }

    public function get_account_tindakan($id_re_kunjungan)
    {
        $data['array_tindakan'] = $this->Content_m->get_tindakan_pasien($id_re_kunjungan);
        //var_dump($data['array_tindakan']);

        $this->db->select('*');
        $this->db->from('siak_re_account_chart ac');
        $this->db->join('bbmc_re_tindakan t','ac.id_tindakan = t.id_tindakan');

        
        if(count($data['array_tindakan']) > 0){
            $this->db->where_in('id',$data['array_tindakan']);
        }else{
            $this->db->where('t.id_tindakan','-0');
        }
        

        $this->db->where('ac.deleted','0');
        $this->db->order_by('ac.id_tindakan');
        $q = $this->db->get();

        //echo $this->db->last_query();

        return $q;
    }

    public function get_tindakan_pasien_new($id_re_kunjungan)
    {
        $this->db->select('*');
        $this->db->from('bbmc_tr_tindakan t');
        $this->db->join('siak_re_account_chart ac','ac.id = t.tindakan');
        $this->db->where('t.deleted','0');
        $this->db->where('t.id_re_kunjungan',$id_re_kunjungan);
        $q = $this->db->get();

        return $q;
    }

    public function get_tindakan_laboratorium_new($id_re_kunjungan)
    {
         $this->db->select('*');
        $this->db->from('bbmc_tr_laboratorium t');
        $this->db->join('siak_re_account_chart ac','ac.id = t.tindakan_lab');
        $this->db->where('t.deleted','0');
        $this->db->where('t.id_re_kunjungan',$id_re_kunjungan);
        $q = $this->db->get();

        return $q;
    }

    public function get_tindakan_radiologi_new($id_re_kunjungan)
    {
         $this->db->select('*');
        $this->db->from('bbmc_tr_radiologi t');
        $this->db->join('siak_re_account_chart ac','ac.id = t.tindakan_radiologi');
        $this->db->where('t.deleted','0');
        $this->db->where('t.id_re_kunjungan',$id_re_kunjungan);
        $q = $this->db->get();

        return $q;
    }


    public function get_tagihan_pasien($id_re_kunjungan, $id_tipe_pasien)
    {
        $cek=0;
        $kurangi = 0;
        $price_umum = 0;
        $price_bpjs = 0;
        $price_shhc = 0;
        $price_inhealth = 0;
        $price_mega = 0;
        $grand_ttl = 0;
        $query = $this->get_tindakan_pasien_new($id_re_kunjungan);
        foreach($query->result() as $key) {
            $price_umum = $key->price_umum;
            if($id_tipe_pasien == '1'){
                $kurangi = 0;
            }else if($id_tipe_pasien == '2'){
                $price_bpjs = $key->price_bpjs;
                $kurangi = $kurangi + $price_bpjs;
            }else if($id_tipe_pasien == '3'){
                $price_shhc = $key->price_shhc;
                $kurangi = $kurangi + $price_shhc;
            }else if($id_tipe_pasien == '4'){
                $price_inhealth = $key->price_inhealth;
                $kurangi = $kurangi + $price_inhealth;
            }else if($id_tipe_pasien == '5'){
                $price_mega = $key->price_mega;
                $kurangi = $kurangi + $price_mega;
            }
            $ttl = $key->price_umum - $kurangi;
            $grand_ttl = $grand_ttl + $ttl;
            $ttl=0; $kurangi=0;
            $cek++;
        }

        $query = $this->get_tindakan_laboratorium_new($id_re_kunjungan);
        foreach($query->result() as $key) {
            $price_umum = $key->price_umum;
            if($id_tipe_pasien == '1'){
                $kurangi = 0;
            }else if($id_tipe_pasien == '2'){
                $price_bpjs = $key->price_bpjs;
                $kurangi = $kurangi + $price_bpjs;
            }else if($id_tipe_pasien == '3'){
                $price_shhc = $key->price_shhc;
                $kurangi = $kurangi + $price_shhc;
            }else if($id_tipe_pasien == '4'){
                $price_inhealth = $key->price_inhealth;
                $kurangi = $kurangi + $price_inhealth;
            }else if($id_tipe_pasien == '5'){
                $price_mega = $key->price_mega;
                $kurangi = $kurangi + $price_mega;
            }
            $ttl = $key->price_umum - $kurangi;
            $grand_ttl = $grand_ttl + $ttl;
            $ttl=0; $kurangi=0;
            $cek++;
        }

        $query = $this->get_tindakan_radiologi_new($id_re_kunjungan);
        foreach($query->result() as $key) {
            $price_umum = $key->price_umum;
            if($id_tipe_pasien == '1'){
                $kurangi = 0;
            }else if($id_tipe_pasien == '2'){
                $price_bpjs = $key->price_bpjs;
                $kurangi = $kurangi + $price_bpjs;
            }else if($id_tipe_pasien == '3'){
                $price_shhc = $key->price_shhc;
                $kurangi = $kurangi + $price_shhc;
            }else if($id_tipe_pasien == '4'){
                $price_inhealth = $key->price_inhealth;
                $kurangi = $kurangi + $price_inhealth;
            }else if($id_tipe_pasien == '5'){
                $price_mega = $key->price_mega;
                $kurangi = $kurangi + $price_mega;
            }
            $ttl = $key->price_umum - $kurangi;
            $grand_ttl = $grand_ttl + $ttl;
            $ttl=0; $kurangi=0;
            $cek++;
        }
        





        $datas['grand_ttl'] = $grand_ttl;
        $datas['cek'] = $cek;
        return $datas;
    }

    public function get_tipe_pasien_pertindakan($id_re_kunjungan)
    {
        $id_re_pasien = $this->Content_m->get_value('bbmc_tr_kunjungan','id_re_kunjungan',$id_re_kunjungan,'id_re_pasien');
        $id_tipe_pasien = $this->Content_m->get_value('bbmc_tr_pasien','id_re_pasien',$id_re_pasien,'id_type_pasien');

        return $id_tipe_pasien;
    }


    public function get_file($tindakan_lab, $id_kunjungan)
    {
        $this->db->select('*');
        $this->db->from('bbmc_tr_laboratorium_dokumen');
        $this->db->where('tindakan_lab',$tindakan_lab);
        $this->db->where('id_re_kunjungan',$id_kunjungan);
        $this->db->where('deleted','0');
        //$this->db->where('id_reg_pd',$id_reg_pd);
        $q = $this->db->get();

        return $q;
    }

   
    
   

}
