<?php 

class M_save_pka extends CI_Model{	

    

    function __construct() {

        parent::__construct();

    }

    

    function kode_user(){

        return $this->session->userdata("kode_user");

    }

    function insert_save($data){

        $kode_user = $this->kode_user();

        $sql = "INSERT INTO `tb_save`(`kode_save`, `nama_save`, `created_at`, `created_by`) VALUES ('$data[kode_save]','$data[nama_save]',NOW(),'$kode_user')";

        return $this->db->query($sql);

    }



    function insert_save_pka($data){

        $kode_user = $this->kode_user();

        $sql = "INSERT INTO `tb_save_pka`(

                    `kode_pka`,

                    `kode_save`,

                    `kma`, 

                    `unit_misioner`, 

                    `jenis_kegiatan`, 

                    `bidang`, 

                    `nama_kegiatan`, 

                    `nomor_strategi`, 

                    `tujuan_kegiatan`,

                    `lokasi_pp`, 

                    `pendukung_program`, 

                    `pen_jawab_program`, 

                    `w_awal_pp`, 

                    `w_akhir_pp`, 

                    `indikator_kuantitatif`, 

                    `indikator_kualitatif`, 

                    `prioritas_kegiatan`, 

                    `triwulan`, 

                    `keterangan`, 

                    `created_at`, 

                    `created_by`

                ) VALUES (

                    '$data[kode_pka]',

                    '$data[kode_save]',

                    '$data[kma]',

                    '$data[unit_misioner]',

                    '$data[jenis_kegiatan]',

                    '$data[bidang]',

                    '$data[nama_kegiatan]',

                    '$data[nomor_strategi]',

                    '$data[tujuan_kegiatan]',

                    '$data[lokasi_pp]',

                    '$data[pendukung_program]',

                    '$data[pen_jawab_program]',

                    '$data[w_awal_pp]',

                    '$data[w_akhir_pp]',

                    '$data[indikator_kuantitatif]',

                    '$data[indikator_kualitatif]',

                    '$data[prioritas_kegiatan]',

                    '$data[triwulan]',

                    '$data[keterangan]',

                    NOW(),

                    '$kode_user'

                    

                    )";

        return $this->db->query($sql);

    }

    function insert_save_pka_pengeluaran($data){

        $sql = "INSERT INTO `tb_save_pka_pengeluaran`(

                    `kode_save`,

                    `kode_pka`, 

                    `jenis_pengeluaran`, 

                    `jumlah_satuan`, 

                    `bulanan`, 

                    `tahunan`, 

                    `nilai_satuan`, 

                    `pengeluaran`

                ) VALUES (

                    '$data[kode_save]',

                    '$data[kode_pka]',

                    '$data[jenis_pengeluaran]',

                    '$data[jumlah_satuan]',

                    '$data[bulanan]',

                    '$data[tahunan]',

                    '$data[nilai_satuan]',

                    '$data[pengeluaran]'

                )";

        return $this->db->query($sql);

    }

    function insert_save_pka_penerimaan($data){

        $sql = "INSERT INTO `tb_save_pka_penerimaan`(

                    `kode_save`,

                    `kode_pka`, 

                    `jenis_penerimaan`, 

                    `jumlah_satuan`, 

                    `bulanan`, 

                    `tahunan`, 

                    `nilai_satuan`, 

                    `penerimaan`

                ) VALUES (

                    '$data[kode_save]',

                    '$data[kode_pka]',

                    '$data[jenis_pengeluaran]',

                    '$data[jumlah_satuan]',

                    '$data[bulanan]',

                    '$data[tahunan]',

                    '$data[nilai_satuan]',

                    '$data[pengeluaran]'

                )";

        return $this->db->query($sql);

    }

    function select_data_save(){

        $kode_user = $this->kode_user();

        $sql = "SELECT sp.kode_save ks,s.nama_save,count(sp.kode_save) as jumlah FROM tb_save_pka as sp

                LEFT JOIN tb_save as s ON s.kode_save = sp.kode_save 

                WHERE s.created_by = '$kode_user'

                GROUP BY sp.kode_save

                ";

        return $this->db->query($sql);

    }

    function select_data_save_pka($data){

        $sql = "SELECT a.*,b.kode kode_unit_misioner,c.kode kode_bidang, d.kode kode_jenis_kegiatan FROM  `tb_save_pka` a

                LEFT JOIN tb_unit_misioner b ON b.id_unit_misioner = a.unit_misioner

                LEFT JOIN tb_bidang c ON c.id_bidang = a.bidang

                LEFT JOIN tb_jenis_kegiatan d ON d.id_jenis_kegiatan = a.jenis_kegiatan

                WHERE a.kode_save = '$data[kode_save]' ORDER BY a.id_save_pka ASC

                ";

        return $this->db->query($sql);

    }

    function select_data_save_pka_pengeluaran($data){

        $sql = "SELECT * FROM  `tb_save_pka_pengeluaran` WHERE kode_pka = '$data[kode_pka]'

                ";

        return $this->db->query($sql);

    }

    function select_data_save_pka_penerimaan($data){

        $sql = "SELECT * FROM  `tb_save_pka_penerimaan` WHERE kode_pka = '$data[kode_pka]'

                ";

        return $this->db->query($sql);

    }

    function delete_save($data){

        

        $sql2 = "DELETE FROM `tb_save_pka` WHERE kode_save = '$data[kode_save]'

                ";

        $this->db->query($sql2);

        $sql3 = "DELETE FROM `tb_save_pka_pengeluaran` WHERE kode_save = '$data[kode_save]'

                ";

        $this->db->query($sql3);

        $sql4 = "DELETE FROM `tb_save_pka_penerimaan` WHERE kode_save = '$data[kode_save]'

                ";

        $this->db->query($sql4);

        $sql1 = "DELETE FROM `tb_save` WHERE kode_save = '$data[kode_save]'

                ";

        return $this->db->query($sql1);

    }

}