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

class Posting extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('dashboard_model');
        $this->load->helper('content');
    }

    public function index()  {

        $DB1 = $this->load->database('default', TRUE);
        $DB2 = $this->load->database('webrsdk', TRUE);
        $DB3 = $this->load->database('tigasatu_simrs', TRUE);
        $DB4 = $this->load->database('limasatu_gizi', TRUE);
        $DB5 = $this->load->database('limasatu_hmisdata', TRUE);

        if(!$this->input->is_cli_request()) {
            echo "This script can only be accessed via the command line" . PHP_EOL;
        return;
        }

// ===== IKT =====
// AVLOS
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_avlos');

        $data = $DB5->query("EXEC procBIOSAVLos '".bulan()."','".tahun()."'")->row()->AVLOS;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('avlos', $data);
            $this->db->insert('ikt_avlos');
            echo "update ikt avlos berhasil \r\n";
        } else { echo "update avlos gagal \r\n"; }

// BOR, masih belum bisa
        /*
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_bor');

        $data = $DB5->query("EXEC procBIOSHitungBOR '".bulan()."','".tahun()."'")->row()->Bor;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('bor', $data);
            $this->db->insert('ikt_bor');
            echo "update ikt bor berhasil \r\n";
        } else { echo "update bor gagal \r\n"; }
        */

// BTO
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_bto');

        $data = $DB5->query("EXEC procBIOSBTO '".bulan()."','".tahun()."'")->row()->BTO;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('bto', $data);
            $this->db->insert('ikt_bto');
            echo "update ikt bto berhasil \r\n";
        } else { echo "update bto gagal \r\n"; }

// NDR
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_ndr');

        $data = $DB5->query("EXEC procBIOSHitungNDR '".bulan()."','".tahun()."'")->row()->NDR;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('ndr', $data);
            $this->db->insert('ikt_ndr');
            echo "update ikt ndr berhasil \r\n";
        } else { echo "update ndr gagal \r\n"; }

// TOI
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_toi');

        $data = $DB5->query("EXEC procBIOSHitungTOI '".bulan()."','".tahun()."'")->row()->TOI;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('toi', $data);
            $this->db->insert('ikt_toi');
            echo "update ikt toi berhasil \r\n";
        } else { echo "update toi gagal \r\n"; }

// ikt - kunjungan rajal
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_kunj_irja');

        $data = $DB5->query("EXEC procBIOSKunjunganPasien 'OUT','".bulan()."','".tahun()."'")->row()->Jml;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('kunj_irja', $data);
            $this->db->insert('ikt_kunj_irja');
            echo "update ikt kunjungan irja berhasil \r\n";
        } else { echo "update ikt kunjungan irja gagal \r\n"; }

// ikt - kunjungan igd
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_kunj_igd');

        $data = $DB5->query("EXEC procBIOSKunjunganPasien 'EMG','".bulan()."','".tahun()."'")->row()->Jml;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('kunj_igd', $data);
            $this->db->insert('ikt_kunj_igd');
            echo "update ikt kunjungan igd berhasil \r\n";
        } else { echo "update ikt kunjungan igd gagal \r\n"; }

// ikt - kunjungan ranap
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_kunj_irna');

        $data = $DB5->query("EXEC procBIOSKunjunganPasien 'IN','".bulan()."','".tahun()."'")->row()->Jml;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('kunj_irna', $data);
            $this->db->insert('ikt_kunj_irna');
            echo "update ikt kunjungan irna berhasil \r\n";
        } else { echo "update ikt kunjungan irna gagal \r\n"; }

// ikt - kunjungan umum
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_kunj_umum');

        $data = $DB5->query("EXEC procBIOSKunjungangJKN '0','".bulan()."','".tahun()."'")->row()->JmlJKN;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('kunj_umum', $data);
            $this->db->insert('ikt_kunj_umum');
            echo "update ikt kunjungan umum berhasil \r\n";
        } else { echo "update ikt kunjungan umum gagal \r\n"; }

// ikt - kunjungan jkn
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_kunj_jkn');

        $data = $DB5->query("EXEC procBIOSKunjungangJKN '1','".bulan()."','".tahun()."'")->row()->JmlJKN;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('kunj_jkn', $data);
            $this->db->insert('ikt_kunj_jkn');
            echo "update ikt kunjungan jkn berhasil \r\n";
        } else { echo "update ikt kunjungan jkn gagal \r\n"; }

// ikt - kunjungan total
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_kunj_total');

        $data = $DB5->query("EXEC procBIOSKunjungangTotal '".bulan()."','".tahun()."'")->row()->JmlJKN;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('kunj_total', $data);
            $this->db->insert('ikt_kunj_total');
            echo "update ikt kunjungan total berhasil \r\n";
        } else { echo "update ikt kunjungan total gagal \r\n"; }

// ikt - pengunjung rajal
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_pengunj_irja');

        $data = $DB5->query("EXEC procBIOSPengunjungRS 'OUT','".bulan()."','".tahun()."'")->row()->Jml;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('pengunj_irja', $data);
            $this->db->insert('ikt_pengunj_irja');
            echo "update ikt pengunjung irja berhasil \r\n";
        } else { echo "update ikt pengunjung irja gagal \r\n"; }

// ikt - pengunjung igd
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_pengunj_igd');

        $data = $DB5->query("EXEC procBIOSPengunjungRS 'EMG','".bulan()."','".tahun()."'")->row()->Jml;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('pengunj_igd', $data);
            $this->db->insert('ikt_pengunj_igd');
            echo "update ikt pengunjung igd berhasil \r\n";
        } else { echo "update ikt pengunjung igd gagal \r\n"; }

// ikt - pengunjung ranap
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_pengunj_irna');

        $data = $DB5->query("EXEC procBIOSPengunjungRS 'IN','".bulan()."','".tahun()."'")->row()->Jml;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('pengunj_irna', $data);
            $this->db->insert('ikt_pengunj_irna');
            echo "update ikt pengunjung irna berhasil \r\n";
        } else { echo "update ikt pengunjung irna gagal \r\n"; }

// ikt - pengunjung umum
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_pengunj_umum');

        $data = $DB5->query("EXEC procBIOSPengunjungJKN '0','".bulan()."','".tahun()."'")->row()->JmlJKN;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('pengunj_umum', $data);
            $this->db->insert('ikt_pengunj_umum');
            echo "update ikt pengunjung umum berhasil \r\n";
        } else { echo "update ikt pengunjung umum gagal \r\n"; }

// ikt - pengunjung jkn
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_pengunj_jkn');

        $data = $DB5->query("EXEC procBIOSPengunjungJKN '1','".bulan()."','".tahun()."'")->row()->JmlJKN;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('pengunj_jkn', $data);
            $this->db->insert('ikt_pengunj_jkn');
            echo "update ikt pengunjung jkn berhasil \r\n";
        } else { echo "update ikt pengunjung jkn gagal \r\n"; }

// ikt - pengunjung total
        $DB1->where('bln', date('m'));
        $DB1->where('thn', date('Y'));
        $DB1->delete('ikt_pengunj_total');

        $data = $DB5->query("EXEC procBIOSPengunjungTotal '".bulan()."','".tahun()."'")->row()->JmlPengunjung;
        if (isset($data)) {
            $this->db->set('bln', date('m'));
            $this->db->set('thn', date('Y'));
            $this->db->set('pengunj_total', $data);
            $this->db->insert('ikt_pengunj_total');
            echo "update ikt pengunjung total berhasil \r\n";
        } else { echo "update ikt pengunjung total gagal \r\n"; }

/*
// ikt - tarif kelas 1
        $this->db->query('DELETE FROM ikt_tarif_ranap');
        $data = $DB5->query("EXEC procBIOSAkomodasi '02'")->row()->Amount;
        if (isset($data)) {
            $this->db->set('kelas', 'Kelas I');
            $this->db->set('tarif', $data);
            $this->db->insert('ikt_tarif_ranap');
            echo "update ikt tarif kelas 1 berhasil \r\n";
        } else { echo "update ikt tarif kelas 1 gagal \r\n"; }


// ikt - tarif kelas 2
        $this->db->query('DELETE FROM ikt_tarif_ranap');
        $data = $DB5->query("EXEC procBIOSAkomodasi '03'")->row()->Amount;
        if (isset($data)) {
            $this->db->set('kelas', 'Kelas II');
            $this->db->set('tarif', $data);
            $this->db->insert('ikt_tarif_ranap');
            echo "update ikt tarif kelas 2 berhasil \r\n";
        } else { echo "update ikt tarif kelas 2 gagal \r\n"; }


// ikt - tarif kelas 3
        $this->db->query('DELETE FROM ikt_tarif_ranap');
        $data = $DB5->query("EXEC procBIOSAkomodasi '04'")->row()->Amount;
        if (isset($data)) {
            $this->db->set('kelas', 'Kelas III');
            $this->db->set('tarif', $data);
            $this->db->insert('ikt_tarif_ranap');
            echo "update ikt tarif kelas 3 berhasil \r\n";
        } else { echo "update ikt tarif kelas 3 gagal \r\n"; }

// ikt - jumlah bed
        $this->db->query('DELETE FROM bed_jml');
        $data = $DB5->query("EXEC procBIOSJumlahBed")->row()->JmlTT;
        if (isset($data)) {
            $this->db->set('keterangan', 'Kapasitas Tempat Tidur');
            $this->db->set('jumlah_bed', $data);
            $this->db->insert('bed_jml');
            echo "update kapasitas tempat tidur berhasil \r\n";
        } else { echo "update kapasitas tempat tidur gagal \r\n"; }
*/

// kunjungan irja
        $this->db->like('last_update', ''.bulan().'/'.tahun().'');
        $this->db->delete('kunjungan_irja');

        //$filexml = simplexml_load_string(file_get_contents('http://183.91.84.99:8081/HmisDataSvr.asmx/GetKunjunganJKNIrja?tgl='.$tgl));
        $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('tgl',$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');
            }
            echo "update kunjungan irja success \r\n";
        } else { echo "update kunjungan irja gagal \r\n"; }

// kunjungan irna by 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');
            }
            echo "update kunjungan irna KELAS success \r\n";
        } else { echo "update kunjungan irna KELAS gagal \r\n"; }

// kunjungan irna by ruang
        $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='.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');
            }
            echo "update kunjungan irna RUANG success \r\n";
        } else { echo "update kunjungan irna RUANG gagal \r\n"; }

// 10 besar 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');
            }
            echo "update 10 besar diagnosa irja success \r\n";
        } else { echo "update 10 besar diagnosa irja gagal \r\n"; }

// 10 besar 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');
            }
            echo "update 10 besar diagnosa irna success \r\n";
        } else { echo "update 10 besar diagnosa irna gagal \r\n"; }

// 10 besar 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');
            }
            echo "update 10 besar alkes success \r\n";
        } else { echo "update 10 besar alkes gagal \r\n"; }

// 10 besar 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');
            }
            echo "update 10 besar obat success \r\n";
        } else { echo "update 10 besar obat gagal \r\n"; }

// selisih biaya INAGBs diagnosa
        $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');
            }
            echo "update selisih biaya INAGBs diagnosa IN success \r\n";
        } else { echo "update selisih biaya INAGBs IN diagnosa gagal \r\n"; }

        $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');
            }
            echo "update selisih biaya INAGBs diagnosa OUT success \r\n";
        } else { echo "update selisih biaya INAGBs diagnosa OUT gagal \r\n"; }

// selisih biaya INAGBs diagnosa
        $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');
            }
            echo "update selisih biaya INAGBs dokter IN success \r\n";
        } else { echo "update selisih biaya INAGBs IN dokter gagal \r\n"; }

        $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');
            }
            echo "update selisih biaya INAGBs dokter OUT success \r\n";
        } else { echo "update selisih biaya INAGBs OUT dokter gagal \r\n"; }

// capaian DRM rawat inap per-bulan
        $DB1->where('bulan', date('m'));
        $DB1->where('tahun', date('Y'));
        $DB1->delete('drm_ranap_bln');
        
        $data = $DB3->query("EXEC procCetakBerkasBulanan2 '".date('m')."', '".date('Y')."'")->result();
        if (isset($data)) {
            foreach ($data as $row) {
                $DB1->set('tanggal', $row->TANGGAL);
                $DB1->set('bulan', date('m'));
                $DB1->set('tahun', date('Y'));
                $DB1->set('jumlahPasien', $row->jumlahPasien); 
                $DB1->set('hariPertama', $row->hariPertama);
                $DB1->set('harikedua', $row->harikedua);
                $DB1->set('hariketiga', $row->hariketiga);
                $DB1->set('lebih3hari', $row->lebih3hari);
                $DB1->set('belumlengkap', $row->belumLengkap);
                $DB1->insert('drm_ranap_bln');
            }
            echo "update DRM rawat inap per-bulan success \r\n";
        } else { echo "update DRM rawat inap per-bulan gagal \r\n"; }

// capaian DRM rawat inap per-tahun
        $DB1->where('bulan', date('n'));
        $DB1->where('tahun', date('Y'));
        $DB1->delete('drm_ranap_thn');
        
        $data = $DB3->query("EXEC procCetakBerkasTahunan2 '".date('Y')."'")->result();
        if (isset($data)) {
            foreach ($data as $row) {
                $DB1->set('bulan', date('m'));
                $DB1->set('tahun', date('Y'));
                $DB1->set('jumlahPasien', $row->jumlahPasien); 
                $DB1->set('hariPertama', $row->hariPertama);
                $DB1->set('harikedua', $row->harikedua);
                $DB1->set('hariketiga', $row->hariketiga);
                $DB1->set('lebih3hari', $row->lebih3hari);
                $DB1->set('belumlengkap', $row->belumLengkap);
                $DB1->insert('drm_ranap_thn');
            }
            echo "update DRM rawat inap per-tahun success \r\n";
        } else { echo "update DRM rawat inap per-tahun gagal \r\n"; }

// gizi persediaan
        $DB1->where('bulan', date('n'));
        $DB1->where('tahun', date('Y'));
        $DB1->delete('gizi_persediaan');
        
        $data = $DB4->query("EXEC MM_GZ_Rekap_LaporanPersediaanBulanBerjalan")->result();
        if (isset($data)) {
            foreach ($data as $row) {
                $DB1->set('Bulan', $row->Bulan);
                $DB1->set('Tahun', $row->Tahun);
                $DB1->set('kelompok', $row->kelompok);
                $DB1->set('SaldoAwal', $row->SaldoAwal); 
                $DB1->set('MutasiMasuk', $row->MutasiMasuk);
                $DB1->set('MutasiKeluar', $row->MutasiKeluar);
                $DB1->set('Adjustment', $row->Adjustment);
                $DB1->set('SaldoAkhir', $row->SaldoAkhir);
                $DB1->insert('gizi_persediaan');
            }
            echo "update Gizi persediaan success \r\n";
        } else { echo "update Gizi persediaan gagal \r\n"; }

// gizi penerimaan
        $DB1->where('bulan', date('n'));
        $DB1->where('tahun', date('Y'));
        $DB1->delete('gizi_penerimaan');
        
        $data = $DB4->query("EXEC MM_GZ_Rekap_PenerimaanBarang '".date('n')."', '".date('Y')."'")->result();
        if (isset($data)) {
            foreach ($data as $row) {
                $DB1->set('bulan', $row->bulan);
                $DB1->set('tahun', $row->tahun);
                $DB1->set('kodekelompok', $row->kodekelompok);
                $DB1->set('namakelompok', $row->namakelompok); 
                $DB1->set('QtyPenerimaan', $row->QtyPenerimaan);
                $DB1->set('TotalHargaBeli', $row->TotalHargaBeli);
                $DB1->set('TotalPenerimaan', $row->TotalPenerimaan);
                $DB1->insert('gizi_penerimaan');
            }
            echo "update Gizi penerimaan success \r\n";
        } else { echo "update Gizi penerimaan gagal \r\n"; }

// gizi distribusi
        $DB1->where('bulan', date('n'));
        $DB1->where('tahun', date('Y'));
        $DB1->delete('gizi_distribusi');
        
        $data = $DB4->query("EXEC MM_GZ_Rekap_DistribusiBarang '".date('n')."', '".date('Y')."'")->result();
        if (isset($data)) {
            foreach ($data as $row) {
                $DB1->set('bulan', $row->bulan);
                $DB1->set('tahun', $row->tahun);
                $DB1->set('kodekelompok', $row->kodekelompok);
                $DB1->set('namakelompok', $row->namakelompok); 
                $DB1->set('QtyDistribusi', $row->QtyDistribusi);
                $DB1->set('TotalDistribusi', $row->TotalDistribusi);
                $DB1->insert('gizi_distribusi');
            }
            echo "update Gizi distribusi success \r\n";
        } else { echo "update Gizi distribusi gagal \r\n"; }

    }
}
?>