<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

ini_set('max_execution_time', 0); 
//ini_set('memory_limit','2048M');

class Config extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('dashboard_model');
        $this->load->helper('text');
        $this->load->helper('content');

        if ($this->user_model->is_logged_in() == FALSE)
            redirect(base_url(), 'refresh');
        if ($this->user_model->is_superadmin() == FALSE)
            redirect('admin/profil');

    }

    function index() {
        $username = $this->session->userdata('username');
        $view['session'] = $username;      
        $view['superadmin'] = $this->user_model->superadminmenu()->result();
        $view['admin'] = $this->user_model->adminmenu()->result();
        $view['list_user'] = $this->user_model->list_user()->result();
        $view['list_ip'] = $this->user_model->list_ip()->result();
        $view['list_menu'] = $this->user_model->list_menu()->result();
        $view['content'] = 'admin/welcome';
        
        $this->load->view('admin/index', $view);
    }

/* ===== USER ===== */
    function input_user() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('realname', 'Nama Lengkap', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == true) {
            $this->user_model->insert_user();
            $this->session->set_flashdata('message', 'User berhasil di buat');
            redirect('admin/config');
        }
        else {
            $this->session->set_flashdata('error', ''.validation_errors().'');
            redirect('admin/config');
        }
    }

    function edit_user($id) {
        if ((empty($id)) || ($this->user_model->view_user($id)->num_rows() == 0 ))
            redirect('admin/config');

        if ($this->input->post('submit')) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('realname', 'Nama Lengkap', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');

            if ($this->form_validation->run() == true) {
                $this->user_model->update_user($id);
                $this->session->set_flashdata('message', 'User berhasil di ubah');
                redirect('admin/config');
            }
            else {
                $this->session->set_flashdata('error', ''.validation_errors().'');
                redirect('admin/config');
            }
        }

        $view['data'] = $this->user_model->view_user($id)->result();

        $view['superadmin'] = $this->user_model->superadminmenu()->result();
        $view['admin'] = $this->user_model->adminmenu()->result();
        $view['content'] = 'admin/configuseredit';
        $view['superadmin_menu'] = TRUE;
        $this->load->view('admin/index', $view);
    }

    function delete_user($id) {
        if ((empty($id)) || ($this->user_model->view_user($id)->num_rows() == 0 ))
            redirect('admin/config');

        $this->user_model->delete_user($id);
        $this->session->set_flashdata('message', 'User berhasil dihapus');
        redirect('admin/config');
    }

/* ===== IP ===== */
    function input_ip() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nomorip', 'Nomor IP', 'required');
        $this->form_validation->set_rules('port', 'Port', 'required');
        $this->form_validation->set_rules('keterangan', 'Keterangan', 'required');

        if ($this->form_validation->run() == true) {
            $this->user_model->insert_ip();
            $this->session->set_flashdata('message', 'User berhasil di buat');
            redirect('admin/config');
        }
        else {
            $this->session->set_flashdata('error', ''.validation_errors().'');
            redirect('admin/config');
        }
    }

    function edit_ip($id) {
        if ((empty($id)) || ($this->user_model->view_ip($id)->num_rows() == 0 ))
            redirect('admin/config');

        if ($this->input->post('submit')) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('nomorip', 'Nomor IP', 'required');
            $this->form_validation->set_rules('port', 'Port', 'required');
            $this->form_validation->set_rules('keterangan', 'Keterangan', 'required');

            if ($this->form_validation->run() == true) {
                $this->user_model->update_ip($id);
                $this->session->set_flashdata('message', 'User berhasil di ubah');
                redirect('admin/config');
            }
            else {
                $this->session->set_flashdata('error', ''.validation_errors().'');
                redirect('admin/config');
            }
        }

        $view['data'] = $this->user_model->view_ip($id)->result();

        $view['superadmin'] = $this->user_model->superadminmenu()->result();
        $view['admin'] = $this->user_model->adminmenu()->result();
        $view['content'] = 'admin/configipedit';
        $view['superadmin_menu'] = TRUE;
        $this->load->view('admin/index', $view);
    }

    function delete_ip($id) {
        if ((empty($id)) || ($this->user_model->view_ip($id)->num_rows() == 0 ))
            redirect('admin/config');

        $this->user_model->delete_ip($id);
        $this->session->set_flashdata('message', 'User berhasil dihapus');
        redirect('admin/config');
    }
    
    function aktifkan_ip($id) {
        $this->user_model->aktifkan($id);
        $this->session->set_flashdata('message', 'IP telah diaktifkan');
        redirect('admin/config');
    }
    
    function nonaktifkan_ip($id) {
        $this->user_model->nonaktifkan($id);
        $this->session->set_flashdata('message', 'IP telah dinonaktifkan');
        redirect('admin/config');
    }
    
/* ===== MENU ===== */
    function input_menu() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nama', 'Nama', 'required');
        $this->form_validation->set_rules('url', 'URL', 'required');
        $this->form_validation->set_rules('superadmin', 'Superadmin');
        $this->form_validation->set_rules('admin', 'Admin');
        $this->form_validation->set_rules('limit', 'Limit');

        if ($this->form_validation->run() == true) {
            $this->user_model->insert_menu();
            $this->session->set_flashdata('message', 'Menu berhasil di buat');
            redirect('admin/config');
        }
        else {
            $this->session->set_flashdata('error', ''.validation_errors().'');
            redirect('admin/config');
        }
    }

    function edit_menu($id) {
        if ((empty($id)) || ($this->user_model->view_ip($id)->num_rows() == 0 ))
            redirect('admin/config');

        if ($this->input->post('submit')) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('nomorip', 'Nomor IP', 'required');
            $this->form_validation->set_rules('port', 'Port', 'required');
            $this->form_validation->set_rules('keterangan', 'Keterangan', 'required');

            if ($this->form_validation->run() == true) {
                $this->user_model->update_ip($id);
                $this->session->set_flashdata('message', 'User berhasil di ubah');
                redirect('admin/config');
            }
            else {
                $this->session->set_flashdata('error', ''.validation_errors().'');
                redirect('admin/config');
            }
        }

        $view['data'] = $this->user_model->view_ip($id)->result();

        $view['superadmin'] = $this->user_model->superadminmenu()->result();
        $view['admin'] = $this->user_model->adminmenu()->result();
        $view['content'] = 'admin/configipedit';
        $view['superadmin_menu'] = TRUE;
        $this->load->view('admin/index', $view);
    }

    function delete_menu($id) {
        if ((empty($id)) || ($this->user_model->view_ip($id)->num_rows() == 0 ))
            redirect('admin/config');

        $this->user_model->delete_ip($id);
        $this->session->set_flashdata('message', 'User berhasil dihapus');
        redirect('admin/config');
    }

/* ===== INFORMASI BED ===== */
    public function bed() {
        
        $nip = $this->dashboard_model->get_ip_local2()->result();
        foreach ($nip as $content) 
        {
            $id = $content->id;
            $noip = $content->nomorip;
            $port = $content->port;
        }
        $nomorip = $noip;
        $nomorport = $port;

        $this->db->query('DELETE FROM bed');
        $filexml = simplexml_load_string(file_get_contents('http://'.$nomorip.':'.$nomorport.'/services/bedinforsdk.aspx'));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('kelas',$content->Kelas);
                $this->db->set('jmlbed',$content->JmlBed);
                $this->db->set('jmlantrian',$content->JmlAntrian);
                $this->db->set('tglupdate',$content->TglUpdate);
                $this->db->insert('bed');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== 10 PENYAKIT IRJA ===== */
    public function penyakit_irja() {
        $this->db->query('DELETE FROM 10besarirj');
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/Get10BesarIrj?tgl='.sekarang()));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('id_diag',$content->ID_DIAG);
                $this->db->set('jumlah_kasus',$content->JUMLAH_KASUS);
                $this->db->set('desc_diag',$content->DESC_DIAG);
                $this->db->insert('10besarirj');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== 10 PENYAKIT IRNA ===== */
    public function penyakit_irna() {
        $this->db->query('DELETE FROM 10besariri');
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/Get10BesarIri?tgl='.sekarang()));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                
                $this->db->set('id_diag',$content->ID_DIAG);
                $this->db->set('jumlah_kasus',$content->JUMLAH_KASUS);
                $this->db->set('desc_diag',$content->DESC_DIAG);
                $this->db->insert('10besariri');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== 10 KUNJUNGAN IRJA ===== */
    public function kunjungan_irja() {
        //$this->db->like('last_update', '08/2016');
        $this->db->like('last_update', ''.bulan().'/'.tahun().'');
        $this->db->delete('kunjungan_irja');
        
        //$filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetKunjunganJKNIrja?tgl=2016-08-01'));
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetKunjunganJKNIrja?tgl='.sekarang()));

        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('last_update',$content->TGL);
                $this->db->set('polycode',$content->PolyCode);
                $this->db->set('polyname',$content->PolyName);
                $this->db->set('jkn_pbi',$content->JKN_PBI);
                $this->db->set('jkn_non_pbi',$content->JKN_NON_PBI);
                $this->db->set('jamkesda',$content->JAMKESDA);
                $this->db->set('umum',$content->UMUM);
                $this->db->set('jml',$content->JML);
                $this->db->insert('kunjungan_irja');
            }
        }

        redirect('admin/config?succsess=1','refresh');
    }
/*
    public function kunjungan_irja($tgl = '2016-08-12') {
        $nip = $this->dashboard_model->get_ip_local()->result();
            foreach ($nip as $content) 
            {
                $id = $content->id;
                $noip = $content->nomorip;
                $port = $content->port;
            }
        $nomorip = $noip;
        $nomorport = $port;

        $this->db->query('DELETE FROM kunjungan_irja');
        
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetKunjunganRajalXML?tgl='.sekarang()));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $data = array(
                    'NoCm' => $content->NoCm,
                    'JenisKelamin' => $content->JenisKelamin,
                    'TglLahir' => $content->TglLahir,
                    'AlamatKota' => $content->AlamatKota,
                    'TglKunjungan' => $content->TglKunjungan,
                    'KodePoli' => $content->KodePoli,
                    'NamaPoli' => $content->NamaPoli,
                    'StatusKunjungan' => $content->StatusKunjungan,
                    'DiagnosaPrimer' => $content->DiagnosaPrimer,
                    'JenisPembayaran' => $content->JenisPembayaran,
                );
                
                $this->db->set('NoCm',$content->NoCm);
                $this->db->set('JenisKelamin',$content->JenisKelamin);
                $this->db->set('TglLahir',$content->TglLahir);
                $this->db->set('AlamatKota',$content->AlamatKota);
                $this->db->set('TglKunjungan',$content->TglKunjungan);
                $this->db->set('KodePoli',$content->KodePoli);
                $this->db->set('NamaPoli',$content->NamaPoli);
                $this->db->set('StatusKunjungan',$content->StatusKunjungan);
                $this->db->set('DiagnosaPrimer',$content->DiagnosaPrimer);
                $this->db->set('JenisPembayaran',$content->JenisPembayaran);
                $this->db->insert('kunjungan_irj');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }
*/
/* ===== 10 KUNJUNGAN IRNA ===== */

/* BY KELAS */
    public function kunjungan_irna_kelas() {
        $nip = $this->dashboard_model->get_ip_local2()->result();
        foreach ($nip as $content) 
        {
            $id = $content->id;
            $noip = $content->nomorip;
            $port = $content->port;
        }
        $nomorip = $noip;
        $nomorport = $port;

        $this->db->query('DELETE FROM kunjungan_iri');
        $filexml = simplexml_load_string(file_get_contents('http://'.$nomorip.':'.$nomorport.'/dashboard_kemkes/kunjungan/iri/'));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('kelas',$content->CONTENT);
                $this->db->set('jumlah',$content->JLH);
                $this->db->insert('kunjungan_iri');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* BY RUANG */
    public function kunjungan_irna_ruang() {
        $this->db->like('last_update', '08/2016');
        //$this->db->like('last_update', ''.bulan().'/'.tahun().'');
        $this->db->delete('kunjungan_irna');
        
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetKunjunganJKNIrna?tgl=2016-08-01'));
        //$filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetKunjunganJKNIrna?tgl='.sekarang()));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('last_update',$content->TGL);
                $this->db->set('polycode',$content->PolyCode);
                $this->db->set('polyname',$content->PolyName);
                $this->db->set('jkn_pbi',$content->JKN_PBI);
                $this->db->set('jkn_non_pbi',$content->JKN_NON_PBI);
                $this->db->set('jamkesda',$content->JAMKESDA);
                $this->db->set('umum',$content->UMUM);
                $this->db->set('jml',$content->JML);
                $this->db->insert('kunjungan_irna');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== 10 PENGGUNAAN ALAT KESEHATAN ===== */
    public function alkes() {
        $this->db->query('DELETE FROM alkes');
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetTopTenAlkesUsage?tgl_a='.awal().'&tgl_z='.sekarang()));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('item_code',$content->item_code);
                $this->db->set('item_code_desc',$content->item_code_desc);
                $this->db->set('Qty',$content->Qty);
                $this->db->insert('alkes');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== 10 OBAT ===== */
    public function obat() {
        $this->db->query('DELETE FROM obat');
        $filexml = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/GetTopTenDrugUsage?tgl_a='.awal().'&tgl_z='.sekarang()));
        if (isset($filexml)) {
            foreach ($filexml as $element => $content) 
            {
                $this->db->set('item_code',$content->item_code);
                $this->db->set('item_code_desc',$content->item_code_desc);
                $this->db->set('Qty',$content->Qty);
                $this->db->insert('obat');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== SELISIH DIAGNOSA ===== */
    public function selisihdiagnosa() {
        $this->db->query('DELETE FROM selisihbiaya_diagnosa');
        $in = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/SelisihBiayaDiagnosa?tgl_a='.awal().'&tgl_z='.sekarang().'&typ=in'));
        if (isset($in)) {
            foreach ($in as $element => $content) 
            {
                $this->db->set('IcdUtama',$content->IcdUtama);
                $this->db->set('ICDDesc',$content->ICDDesc);
                $this->db->set('TotalBiayaRS',$content->TotalBiayaRS);
                $this->db->set('TotalInaCbg',$content->TotalInaCbg);
                $this->db->set('Selisih',$content->Selisih);
                $this->db->set('Type','IN');
                $this->db->insert('selisihbiaya_diagnosa');
            }
        }
        $out = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/SelisihBiayaDiagnosa?tgl_a='.awal().'&tgl_z='.sekarang().'&typ=out'));
        if (isset($out)) {
            foreach ($out as $element => $content) 
            {
                $this->db->set('IcdUtama',$content->IcdUtama);
                $this->db->set('ICDDesc',$content->ICDDesc);
                $this->db->set('TotalBiayaRS',$content->TotalBiayaRS);
                $this->db->set('TotalInaCbg',$content->TotalInaCbg);
                $this->db->set('Selisih',$content->Selisih);
                $this->db->set('Type','OUT');
                $this->db->insert('selisihbiaya_diagnosa');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }

/* ===== SELISIH DOKTER ===== */
    public function selisihdokter() {
        $this->db->query('DELETE FROM selisihbiaya_dokter');
        $in = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/SelisihBiayaDokter?tgl_a='.awal().'&tgl_z='.sekarang().'&typ=in'));
        if (isset($in)) {
            foreach ($in as $element => $content) 
            {
                $this->db->set('DoctCode',$content->DoctCode);
                $this->db->set('Name',$content->Name);
                $this->db->set('TotalBiayaRS',$content->TotalBiayaRS);
                $this->db->set('TotalInaCbg',$content->TotalInaCbg);
                $this->db->set('Selisih',$content->Selisih);
                $this->db->set('Type','IN');
                $this->db->insert('selisihbiaya_dokter');
            }
        }
        $out = simplexml_load_string(file_get_contents('http://'.ip().'/HmisDataSvr.asmx/SelisihBiayaDokter?tgl_a='.awal().'&tgl_z='.sekarang().'&typ=out'));
        if (isset($out)) {
            foreach ($out as $element => $content) 
            {
                $this->db->set('DoctCode',$content->DoctCode);
                $this->db->set('Name',$content->Name);
                $this->db->set('TotalBiayaRS',$content->TotalBiayaRS);
                $this->db->set('TotalInaCbg',$content->TotalInaCbg);
                $this->db->set('Selisih',$content->Selisih);
                $this->db->set('Type','OUT');
                $this->db->insert('selisihbiaya_dokter');
            }
        }
        redirect('admin/config?succsess=1','refresh');
    }


}
?>