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

class Update_model extends CI_Model
{
    // Menu Kelola Data
    // Bagian Marketing
    function update_marketing($inputan, $v_inputan)
    {
        $config['upload_path']   = './assets/ptgs_id';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_login = array(
                'email' => $v_inputan['email'],
                'username' => $v_inputan['username'],
                'status_akun' => 'AKTIF',
                'nm_panggilan' => $inputan['nm_panggilan']
            );
            $this->db->where('id', $inputan['id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik' => $v_inputan['nik'],
                'nm_lengkap' => $inputan['nm_panggilan'],
                'jk' => $inputan['jk'],
                'tempat_lahir' => $inputan['tempat_lahir'],
                'tgl_lahir' => $inputan['tgl_lahir'],
                'alamat' => $inputan['alamat'],
                'no_hp' => $v_inputan['no_hp']
            );
            $this->db->where('login_id', $inputan['id']);
            $this->db->update('sistem_identitas', $data_identitas);
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadData = $this->upload->data();
            $data_login = array(
                'email' => $v_inputan['email'],
                'username' => $v_inputan['username'],
                'status_akun' => 'AKTIF',
                'nm_panggilan' => $inputan['nm_panggilan']
            );
            $this->db->where('id', $inputan['id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik' => $v_inputan['nik'],
                'nm_lengkap' => $inputan['nm_panggilan'],
                'jk' => $inputan['jk'],
                'tempat_lahir' => $inputan['tempat_lahir'],
                'tgl_lahir' => $inputan['tgl_lahir'],
                'alamat' => $inputan['alamat'],
                'no_hp' => $v_inputan['no_hp'],
                'foto_ktp' => $uploadData['file_name']
            );
            $this->db->where('login_id', $inputan['id']);
            $this->db->update('sistem_identitas', $data_identitas);
            $berkas_lama = './assets/ptgs_id/' . $uploadData['file_name'] . '';
            unlink($berkas_lama);
        endif;
    }
    // Menambahkan Foto KK dan mengubah data anggota
    function updateDataAnggota($data_inputan, $cek_alamat)
    {
        $link_foto_lama   = $data_inputan['foto_ktp_old'];
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri'))
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadData = $this->upload->data();
            // Inputan
            $data_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'          => $uploadData['file_name']
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
            $berkas_lama = './assets/foto_ktp/' . $link_foto_lama . '';
            unlink($berkas_lama);
        endif;
    }
    function updateDataAnggotaKK($data_inputan, $cek_alamat)
    {
        $link_foto_lama   = $data_inputan['foto_kk_old'];
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $data_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri'))
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            // Inputan
            $data_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri')),
                'foto_kk'           => $uploadDataKK['file_name']
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
            $berkas_lama = './assets/foto_ktp/' . $link_foto_lama . '';
            unlink($berkas_lama);
        endif;
    }
    function menambahFotoKKAnggota($anggota_id)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_kk';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_anggota = array(
                'foto_kk'       => 'Default.png'
            );
            $this->db->where('id', $anggota_id);
            $this->db->update('anggota', $data_anggota);
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_anggota = array(
                'foto_kk'       => $uploadDataKK['file_name']
            );
            $this->db->where('id', $anggota_id);
            $this->db->update('anggota', $data_anggota);
        endif;
    }
    // Merubah progress pengajuan
    function dataUbahProgress()
    {
        $data = array(
            'tgl_pengajuan' => date('Y-m-d')
        );
        $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
        $this->db->update('p_koperasi_progres', $data);
    }
    // Merubah data identitas
    function ubahDetailIdentitas1($data_validasi)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_ubah = array(
                'nik'        => $data_validasi['nik'],
                'nm_anggota' => htmlspecialchars($this->input->post('nm_anggota')),
                'jk' => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir' => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp' => $data_validasi['no_hp'],
                'pekerjaan' => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri' => htmlspecialchars($this->input->post('suami_istri')),
                'suami_istri' => htmlspecialchars($this->input->post('suami_istri'))
            );
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadDataKTP = $this->upload->data();
            $data_ubah = array(
                'nik'          => $data_validasi['nik'],
                'nm_anggota'   => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'           => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'    => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp'        => $data_validasi['no_hp'],
                'pekerjaan'    => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'  => htmlspecialchars($this->input->post('suami_istri')),
                'suami_istri'  => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'     => $uploadDataKTP['file_name']
            );
        endif;
        $this->db->where('id', htmlspecialchars($this->input->post('anggota_id')));
        $this->db->update('anggota', $data_ubah);
        $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_ktp_old')) . '';
        unlink($berkas_lama);
    }
    function ubahDetailIdentitas1KK($data_validasi)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $data_ubah = array(
                'nomor_kk'        => $data_validasi['no_kk']
            );
            $this->db->where('anggota_id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota_keluarga', $data_ubah);
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_ubah = array(
                'nomor_kk'        => $data_validasi['no_kk']
            );
            $this->db->where('anggota_id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota_keluarga', $data_ubah);
            $data_anggota = array(
                'foto_kk' => $uploadDataKK['file_name']
            );
            $this->db->where('id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota', $data_anggota);
            $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_kk_old')) . '';
            unlink($berkas_lama);
        endif;
    }
    // Menambahkan foto jaminan 1
    function tambahFotoJaminan1()
    {
        // Upload Gambar 1
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('gambar_1')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_1']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_1']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_1']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_1']['size'];
            $uploadGambar_1 = $this->upload->data();
            $gambar01 = array(
                'foto_1'       => $uploadGambar_1['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar01);
        endif;
    }
    // Menambahkan foto jaminan 2
    function  tambahFotoJaminan2()
    {
        // Upload Gambar 2
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('gambar_2')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_2']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_2']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_2']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_2']['size'];
            $uploadGambar_2 = $this->upload->data();
            $gambar02 = array(
                'foto_2'       => $uploadGambar_2['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar02);
        endif;
    }
    // Menambahkan foto jaminan 3
    function  tambahFotoJaminan3()
    {
        // Upload Gambar 3
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('gambar_3')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_3']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_3']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_3']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_3']['size'];
            $uploadGambar_3 = $this->upload->data();
            $gambar03 = array(
                'foto_3'       => $uploadGambar_3['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar03);
        endif;
    }
    // Menambahkan foto jaminan 4
    function  tambahFotoJaminan4()
    {
        // Upload Gambar 4
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('gambar_4')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_4']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_4']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_4']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_4']['size'];
            $uploadGambar_4 = $this->upload->data();
            $gambar04 = array(
                'foto_4'       => $uploadGambar_4['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar04);
        endif;
    }
    // Merubah data angsuran
    function merubahDataAngsuran($angsuran_id)
    {
        $data_angsuran = array(
            'tgl_bayar'       => date('Y-m-d'),
            'status_angsuran' => 'PROSES',
            'update_at'       => date('Y-m-d H:i:s')
        );
        $this->db->where('id', $angsuran_id);
        $this->db->update('p_koperasi_angsuran', $data_angsuran);
    }
    // Pinjaman Selesai
    function pinjamanSelesai($data_kontrak)
    {
        $kontrak_id = $data_kontrak['id'];
        $cek_macet  = $this->select_model->getDataAngsMacet($kontrak_id);
        $cek_lancar = $this->select_model->getDataAngsLancar($kontrak_id);
        if ($cek_macet['tagihan_macet'] > $cek_lancar['tagihan_lancar']) :
            $data_kontrak = array(
                'status_kontrak' => 'LUNAS_MACET'
            );
        else :
            $data_kontrak = array(
                'status_kontrak' => 'LUNAS_LANCAR'
            );
        endif;
        $this->db->where('id', $kontrak_id);
        $this->db->update('p_koperasi_kontrak', $data_kontrak);
    }
    // Pencairan Dana Nasabah
    function pencairanDanaNasabah($isi_index)
    {
        $bulan = date('m');
        $tahun = date('Y');
        $lokasi_file = './assets/doc_kontrak/' . $tahun . '/' . $bulan . '/';
        if (!is_dir($lokasi_file))
            mkdir($lokasi_file, 0777, TRUE);
        // Membuat File Baru
        $index_file = $lokasi_file . 'index.html';
        if (!file_exists($index_file)) {
            file_put_contents($index_file, $isi_index);
        }
        $config['upload_path']   = './assets/doc_kontrak/' . $tahun . '/' . $bulan;
        $config['allowed_types'] = 'jpg|jpeg|png|pdf';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('dokumen_kontrak')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Bukti Pengakuan Pinjaman</div>');
        else :
            $_FILES['file']['name'] = $_FILES['dokumen_kontrak']['name'];
            $_FILES['file']['type'] = $_FILES['dokumen_kontrak']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['dokumen_kontrak']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['dokumen_kontrak']['size'];
            $uploadData = $this->upload->data();
            $data_doc = array(
                'tgl_realisasi'     => date('Y-m-d'),
                'doc_kontrak'       => $uploadData['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_progres', $data_doc);
        endif;
    }
    // Merubah data anggota koperasi KTP
    function updateDataAnggotaKoperasi($data_baru, $anggota_id, $alamat_id)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_ubah = array(
                'nm_anggota'     => $data_baru['nm_anggota'],
                'jk'             => $data_baru['jk'],
                'alamat_lengkap' => $data_baru['alamat_lengkap'],
                'nm_rt'          => $data_baru['nm_rt'],
                'nm_rw'          => $data_baru['nm_rw'],
                'id_alamat'      => $alamat_id,
                'tempat_lahir'   => $data_baru['tempat_lahir'],
                'tgl_lahir'      => $data_baru['tgl_lahir'],
                'pekerjaan'      => $data_baru['pekerjaan'],
                'suami_istri'    => $data_baru['suami_istri']
            );
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadDataKTP = $this->upload->data();
            $data_ubah = array(
                'nm_anggota'     => $data_baru['nm_anggota'],
                'jk'             => $data_baru['jk'],
                'alamat_lengkap' => $data_baru['alamat_lengkap'],
                'nm_rt'          => $data_baru['nm_rt'],
                'nm_rw'          => $data_baru['nm_rw'],
                'id_alamat'      => $alamat_id,
                'tempat_lahir'   => $data_baru['tempat_lahir'],
                'tgl_lahir'      => $data_baru['tgl_lahir'],
                'pekerjaan'      => $data_baru['pekerjaan'],
                'suami_istri'    => $data_baru['suami_istri'],
                'foto_ktp'       => $uploadDataKTP['file_name']
            );
            $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_ktp_old')) . '';
            unlink($berkas_lama);
        endif;
        $this->db->where('id', $anggota_id);
        $this->db->update('anggota', $data_ubah);
    }
    // Merubah data anggota koperasi KK
    function updateDataKKAnggota($data_baru, $anggota_id)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Anggota Koperasi!</div>');
            redirect('marketing/identitas/anggota/' . $this->encrypt->encode($anggota_id));
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_anggota = array(
                'foto_kk' => $uploadDataKK['file_name']
            );
            $this->db->where('id', $anggota_id);
            $this->db->update('anggota', $data_anggota);
            $berkas_lama = './assets/foto_ktp/' . $data_baru['foto_kk_old'] . '';
            unlink($berkas_lama);
        endif;
    }
    // Merubah data identitas
    function ubahDetailIdentitas11($data_input)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_ubah = array(
                'nik'        => $data_input['nik'],
                'nm_anggota' => htmlspecialchars($this->input->post('nm_anggota')),
                'jk' => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir' => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp' => $data_input['no_hp'],
                'pekerjaan' => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri' => htmlspecialchars($this->input->post('suami_istri')),
                'suami_istri' => htmlspecialchars($this->input->post('suami_istri'))
            );
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadDataKTP = $this->upload->data();
            $data_ubah = array(
                'nik'          => $data_input['nik'],
                'nm_anggota'   => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'           => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'    => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp'        => $data_input['no_hp'],
                'pekerjaan'    => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'  => htmlspecialchars($this->input->post('suami_istri')),
                'suami_istri'  => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'     => $uploadDataKTP['file_name']
            );
        endif;
        $this->db->where('id', htmlspecialchars($this->input->post('anggota_id')));
        $this->db->update('anggota', $data_ubah);
        $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_ktp_old')) . '';
        unlink($berkas_lama);
    }
    function ubahDetailIdentitas11KK($data_input)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $data_ubah = array(
                'nomor_kk'        => $data_input['no_kk']
            );
            $this->db->where('anggota_id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota_keluarga', $data_ubah);
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_ubah = array(
                'nomor_kk'        => $data_input['no_kk']
            );
            $this->db->where('anggota_id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota_keluarga', $data_ubah);
            $data_anggota = array(
                'foto_kk' => $uploadDataKK['file_name']
            );
            $this->db->where('id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota', $data_anggota);
            $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_kk_old')) . '';
            unlink($berkas_lama);
        endif;
    }
    // Merubah data identitas
    function ubahDetailIdentitas111($data_fix)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_ubah = array(
                'nik'        => $data_fix['nik'],
                'nm_anggota' => htmlspecialchars($this->input->post('nm_anggota')),
                'jk' => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir' => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp' => $data_fix['no_hp'],
                'pekerjaan' => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri' => htmlspecialchars($this->input->post('suami_istri')),
                'suami_istri' => htmlspecialchars($this->input->post('suami_istri'))
            );
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadDataKTP = $this->upload->data();
            $data_ubah = array(
                'nik'          => $data_fix['nik'],
                'nm_anggota'   => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'           => htmlspecialchars($this->input->post('jk')),
                'tempat_lahir' => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'    => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp'        => $data_fix['no_hp'],
                'pekerjaan'    => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'  => htmlspecialchars($this->input->post('suami_istri')),
                'suami_istri'  => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'     => $uploadDataKTP['file_name']
            );
        endif;
        $this->db->where('id', htmlspecialchars($this->input->post('anggota_id')));
        $this->db->update('anggota', $data_ubah);
        $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_ktp_old')) . '';
        unlink($berkas_lama);
    }
    function ubahDetailIdentitas111KK($data_fix)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $data_ubah = array(
                'nomor_kk'        => $data_fix['no_kk']
            );
            $this->db->where('anggota_id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota_keluarga', $data_ubah);
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_ubah = array(
                'nomor_kk'        => $data_fix['no_kk']
            );
            $this->db->where('anggota_id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota_keluarga', $data_ubah);
            $data_anggota = array(
                'foto_kk' => $uploadDataKK['file_name']
            );
            $this->db->where('id', htmlspecialchars($this->input->post('anggota_id')));
            $this->db->update('anggota', $data_anggota);
            $berkas_lama = './assets/foto_ktp/' . htmlspecialchars($this->input->post('foto_kk_old')) . '';
            unlink($berkas_lama);
        endif;
    }
}