<?php

use Google\Service\AndroidPublisher\Timestamp;

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

class Insert_model extends CI_Model
{
    // Tambah transaksi Anggaran Dasar Koperasi
    function simpanDataTransaksiAD()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $lk_file = 'assets/invoice/kas_umum/' . $tahun . '/' . $bulan;
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/invoice/kas_umum/' . $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('bukti_transaksi')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Bukti Transaksi</div>');
        else :
            $_FILES['file']['name'] = $_FILES['bukti_transaksi']['name'];
            $_FILES['file']['type'] = $_FILES['bukti_transaksi']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['bukti_transaksi']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['bukti_transaksi']['size'];
            $uploadData = $this->upload->data();
            $data_nota = array(
                'id'                => date('ymdhis'),
                'produk_id'         => htmlspecialchars($this->input->post('produk_id')),
                'tgl_pembayaran'    => date('Y-m-d'),
                'qty_pembayaran'    => 1,
                'ttl_bayar'         => htmlspecialchars($this->input->post('total_transaksi')),
                'status_kas'        => htmlspecialchars($this->input->post('jenis_transaksi')),
                'status_nota'       => 'TERIMA',
                'petugas'           => $this->session->userdata('isloggedin'),
                'kasir'             => $this->session->userdata('isloggedin'),
                'ket_nota'          => htmlspecialchars($this->input->post('ket_transaksi')),
                'invoice_pdf'       => $uploadData['file_name']
            );
            $this->db->insert('nota', $data_nota);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Melakukan Transaksi!</div>');
        endif;
    }
    // Tambah transaksi Operasional / Umum Koperasi
    function simpanDataTransaksiOperasional()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $lk_file = 'assets/invoice/kas_umum/' . $tahun . '/' . $bulan;
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/invoice/kas_umum/' . $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('bukti_transaksi')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Bukti Transaksi</div>');
        else :
            $_FILES['file']['name'] = $_FILES['bukti_transaksi']['name'];
            $_FILES['file']['type'] = $_FILES['bukti_transaksi']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['bukti_transaksi']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['bukti_transaksi']['size'];
            $uploadData = $this->upload->data();
            $data_nota = array(
                'id'                => date('ymdhis'),
                'produk_id'         => htmlspecialchars($this->input->post('produk_id')),
                'tgl_pembayaran'    => date('Y-m-d'),
                'qty_pembayaran'    => 1,
                'ttl_bayar'         => htmlspecialchars($this->input->post('total_transaksi')),
                'status_kas'        => htmlspecialchars($this->input->post('jenis_transaksi')),
                'status_nota'       => 'TERIMA',
                'petugas'           => $this->session->userdata('isloggedin'),
                'kasir'             => $this->session->userdata('isloggedin'),
                'ket_nota'          => htmlspecialchars($this->input->post('ket_transaksi')),
                'invoice_pdf'       => $uploadData['file_name']
            );
            $this->db->insert('nota', $data_nota);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Melakukan Transaksi!</div>');
        endif;
    }
    // Tambah transaksi Arisan Heboh
    function simpanDataTransaksiArisanHeboh()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $lk_file = 'assets/invoice/kas_umum/' . $tahun . '/' . $bulan;
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/invoice/kas_umum/' . $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('bukti_transaksi')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Bukti Transaksi</div>');
        else :
            $_FILES['file']['name'] = $_FILES['bukti_transaksi']['name'];
            $_FILES['file']['type'] = $_FILES['bukti_transaksi']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['bukti_transaksi']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['bukti_transaksi']['size'];
            $uploadData = $this->upload->data();
            $data_nota = array(
                'id'                => date('ymdhis'),
                'produk_id'         => 21,
                'tgl_pembayaran'    => date('Y-m-d'),
                'qty_pembayaran'    => 1,
                'ttl_bayar'         => htmlspecialchars($this->input->post('total_transaksi')),
                'status_kas'        => htmlspecialchars($this->input->post('jenis_transaksi')),
                'status_nota'       => 'TERIMA',
                'petugas'           => $this->session->userdata('isloggedin'),
                'kasir'             => $this->session->userdata('isloggedin'),
                'ket_nota'          => 'Nomor Regist Arisan :  ' . htmlspecialchars($this->input->post('arisan_heboh')) . ' - ' . htmlspecialchars($this->input->post('ket_transaksi')),
                'invoice_pdf'       => $uploadData['file_name']
            );
            $this->db->insert('nota', $data_nota);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Melakukan Transaksi!</div>');
        endif;
    }
    // Tambah transaksi Arisan Motor
    function simpanDataTransaksiArisanMotor()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $lk_file = 'assets/invoice/kas_umum/' . $tahun . '/' . $bulan;
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/invoice/kas_umum/' . $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('bukti_transaksi')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Bukti Transaksi</div>');
        else :
            $_FILES['file']['name'] = $_FILES['bukti_transaksi']['name'];
            $_FILES['file']['type'] = $_FILES['bukti_transaksi']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['bukti_transaksi']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['bukti_transaksi']['size'];
            $uploadData = $this->upload->data();
            $data_nota = array(
                'id'                => date('ymdhis'),
                'produk_id'         => 22,
                'tgl_pembayaran'    => date('Y-m-d'),
                'qty_pembayaran'    => 1,
                'ttl_bayar'         => htmlspecialchars($this->input->post('total_transaksi')),
                'status_kas'        => htmlspecialchars($this->input->post('jenis_transaksi')),
                'status_nota'       => 'TERIMA',
                'petugas'           => $this->session->userdata('isloggedin'),
                'kasir'             => $this->session->userdata('isloggedin'),
                'ket_nota'          => 'Nomor Regist Arisan :  ' . htmlspecialchars($this->input->post('arisan_motor')) . ' - ' . htmlspecialchars($this->input->post('ket_transaksi')),
                'invoice_pdf'       => $uploadData['file_name']
            );
            $this->db->insert('nota', $data_nota);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Melakukan Transaksi!</div>');
        endif;
    }
    // Tambah Pengurus Login
    function simpanDataAnggotaPengurus($data_inputan)
    {
        $length = 20;
        $username = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 1, $length);
        $data_login = array(
            'role_id'       => 1,
            'email'         => $data_inputan['email'],
            'username'      => $username,
            'no_hp'         => $data_inputan['no_hp'],
            'nm_panggilan'  => $data_inputan['nm_anggota'],
            'password'      => $this->encrypt->encode($username),
            'status_akun'   => 'PASIF'
        );
        $this->db->insert('sistem_login', $data_login);
    }
    // Tambah Karyawan
    function simpanDataAnggotaKaryawan($data_inputan)
    {
        $length = 20;
        $username = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 1, $length);
        $data_login = array(
            'role_id'       => $data_inputan['role_id'],
            'email'         => $data_inputan['email'],
            'username'      => $username,
            'no_hp'         => $data_inputan['no_hp'],
            'nm_panggilan'  => $data_inputan['nm_anggota'],
            'password'      => $this->encrypt->encode($username),
            'status_akun'   => 'AKTIF'
        );
        $this->db->insert('sistem_login', $data_login);
    }
    // Tambah Pengurus Identitas
    function simpanIdentitasDataAnggotaPengurus($data_inputan, $alamat_id, $login_id)
    {
        $lk_file = 'assets/karyawan';
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/karyawan';
        $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_identitas = array(
                'login_id'        => $login_id,
                'nik'             => $data_inputan['nik'],
                'jk'              => $data_inputan['jk'],
                'tempat_lahir'    => $data_inputan['tempat_lahir'],
                'tgl_lahir'       => $data_inputan['tgl_lahir'],
                'alamat_id'       => $alamat_id,
                'tgl_gabung'      => date('Y-m-d')
            );
            $this->db->insert('sistem_identitas', $data_identitas);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Karyawan Berhasil ditambahkan!</div>');
        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_identitas = array(
                'login_id'        => $login_id,
                'nik'             => $data_inputan['nik'],
                'jk'              => $data_inputan['jk'],
                'tempat_lahir'    => $data_inputan['tempat_lahir'],
                'tgl_lahir'       => $data_inputan['tgl_lahir'],
                'alamat_id'       => $alamat_id,
                'foto_ktp'        => $uploadData['file_name'],
                'tgl_gabung'      => date('Y-m-d')
            );
            $this->db->insert('sistem_identitas', $data_identitas);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Karyawan Berhasil ditambahkan!</div>');
        endif;
    }
    // Tambah Karyawan Baru
    function tambahkaryawan()
    {
        $lk_file = 'assets/karyawan';
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/karyawan';
        $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')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Foto KTP</div>');
        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();
            $length = 20;
            $username = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 1, $length);
            $data_login = array(
                'id_role'       => htmlspecialchars($this->input->post('id_role')),
                'email'         => htmlspecialchars($this->input->post('email')),
                'username'      => $username,
                'nm_panggilan'  => htmlspecialchars($this->input->post('nm_anggota')),
                'password'      => $this->encrypt->encode($username),
                'status_akun'   => 'AKTIF'
            );
            $this->db->insert('sistem_login', $data_login);
            $karyawan_baru = $this->db->get_where('sistem_login', ['email' => htmlspecialchars($this->input->post('email'))])->row_array();
            $login_id = $karyawan_baru['id'];
            $data_identitas = array(
                'login_id'     => $login_id,
                'nik'          => htmlspecialchars($this->input->post('nik')),
                'nm_lengkap'   => 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')),
                'alamat'       => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'        => htmlspecialchars($this->input->post('no_hp')),
                'foto_ktp'     => $uploadData['file_name']
            );
            $this->db->insert('sistem_identitas', $data_identitas);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Karyawan Berhasil ditambahkan!</div>');
        endif;
    }
    // Tambah ANggota Arisan Baru
    function tambahAnggotaKoperasiBaru($data_inputan, $alamat_id)
    {
        $lk_file = 'assets/foto_ktp';
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $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')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload KTP, Silahkan Ulang Kembali!</div>');
        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_anggota = array(
                'nik'            => $data_inputan['nik'],
                'nm_anggota'     => $data_inputan['nm_anggota'],
                'jk'             => $data_inputan['jk'],
                'alamat_lengkap' => $data_inputan['alamat_lengkap'],
                'nm_rt'          => $data_inputan['nm_rt'],
                'nm_rw'          => $data_inputan['nm_rw'],
                'id_alamat'      => $alamat_id,
                'tempat_lahir'   => $data_inputan['tempat_lahir'],
                'tgl_lahir'      => $data_inputan['tgl_lahir'],
                'no_hp'          => $data_inputan['no_hp'],
                'pekerjaan'      => $data_inputan['pekerjaan'],
                'suami_istri'    => $data_inputan['suami_istri'],
                'foto_ktp'       => $uploadData['file_name']
            );
            $this->db->insert('anggota', $data_anggota);
            $cek_anggota = $this->db->get_where('anggota', ['nik' => $data_inputan['nik']])->row_array();
            $data = array(
                'anggota_id' => $cek_anggota['id'],
                'no_hp' => $data_inputan['no_hp'],
                'ket_cadangan' => 'Nomor ' . $cek_anggota['no_hp'] . ' di daftarkan sebagai nomor Utama Anggota Arisan Heboh ' . $buat_regist_undian['regist_undian']
            );
            $this->db->insert('anggota_hp_cadangan', $data);
        endif;
    }
    // Tambah ANggota Arisan Baru KK
    function tambahAnggotaKoperasiBaruKK($data_inputan, $alamat_id)
    {
        $lk_file = 'assets/foto_ktp';
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $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')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload KTP, Silahkan Ulang Kembali!</div>');
        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_anggota = array(
                'nik'            => $data_inputan['nik'],
                'nm_anggota'     => $data_inputan['nm_anggota'],
                'jk'             => $data_inputan['jk'],
                'alamat_lengkap' => $data_inputan['alamat_lengkap'],
                'nm_rt'          => $data_inputan['nm_rt'],
                'nm_rw'          => $data_inputan['nm_rw'],
                'id_alamat'      => $alamat_id,
                'tempat_lahir'   => $data_inputan['tempat_lahir'],
                'tgl_lahir'      => $data_inputan['tgl_lahir'],
                'no_hp'          => $data_inputan['no_hp'],
                'pekerjaan'      => $data_inputan['pekerjaan'],
                'suami_istri'    => $data_inputan['suami_istri'],
                'foto_ktp'       => $uploadData['file_name']
            );
            $this->db->insert('anggota', $data_anggota);
            $cek_anggota = $this->db->get_where('anggota', ['nik' => $data_inputan['nik']])->row_array();
            $data = array(
                'anggota_id' => $cek_anggota['id'],
                'no_hp' => $data_inputan['no_hp'],
                'ket_cadangan' => 'Nomor ' . $cek_anggota['no_hp'] . ' di daftarkan sebagai nomor Utama Anggota Arisan Heboh ' . $buat_regist_undian['regist_undian']
            );
            $this->db->insert('anggota_hp_cadangan', $data);
        endif;
    }
    // Upload KK
    function uploadKKBaru($data_inputan, $cek_anggota)
    {
        $cek_kk = $this->db->get_where('anggota_keluarga', ['nomor_kk' => $data_inputan['nomor_kk']])->row_array();
        if ($cek_kk > 0) :
            $data_detail = array(
                'kk_id' => $data_inputan['nomor_kk'],
                'anggota_id' => $cek_anggota['id'],
                'status_keluarga' => 'anak'
            );
            $this->db->insert('anggota_keluarga_detail', $data_detail);
        else :
            $data_kaka = array(
                'nomor_kk' => $data_inputan['nomor_kk'],
                'anggota_id' => $cek_anggota['id'],
                'jml_keluarga' => 1
            );
            $this->db->insert('anggota_keluarga', $data_kaka);
            $data_detail = array(
                'kk_id' => $data_inputan['nomor_kk'],
                'anggota_id' => $cek_anggota['id'],
                'status_keluarga' => 'suami_istri'
            );
            $this->db->insert('anggota_keluarga_detail', $data_detail);
        endif;
        $lk_file = 'assets/foto_kk';
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $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')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload KK, Silahkan Ulang Kembali!</div>');
        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'];
            $uploadData = $this->upload->data();
            $data_anggota = array(
                'foto_kk'        => $uploadData['file_name']
            );
            $this->db->where('id', $cek_anggota['id']);
            $this->db->update('anggota', $data_anggota);
        endif;
    }
    // Tambah Anggota Calon Koperasi
    function simpanCalonAnggotaKoperasi($data_inputan)
    {
        // Inputan 
        if (strlen($data_inputan['nik']) < 16) {
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Tidak Sesuai ( Harus 16 Karakter )!</div>');
            redirect('owner');
        }
        $cek_nomorhp = $this->db->get_where('anggota', ['no_hp' => $data_inputan['no_hp']])->row_array();
        if ($cek_nomorhp > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Terdaftar!</div>');
            redirect('owner');
        endif;
        if ($data_inputan['jk'] == 'ok') :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Jenis Kelamin Tidak Di Isi!</div>');
            redirect('owner');
        endif;
        $cari_alamat = $this->db->get_where('sistem_alamat', ['nm_rt' => $data_inputan['nm_rt'], 'nm_rw' => $data_inputan['nm_rw'], 'kd_pos' => $data_inputan['kode_pos'], 'nm_desa' => $data_inputan['nm_desa']])->row_array();
        if ($cari_alamat > 0) :
            $id_alamat = $cari_alamat['id'];
        else :
            $data_alamat = array(
                'nm_rt'        => $data_inputan['nm_rt'],
                'nm_rw'        => $data_inputan['nm_rw'],
                'nm_desa'      => $data_inputan['nm_desa'],
                'nm_kecamatan' => $data_inputan['nm_kecamatan'],
                'kd_pos'       => $data_inputan['kode_pos'],
                'kot_kab'      => $data_inputan['kot_kab'],
                'prov'         => $data_inputan['prov']
            );
            $this->db->insert('sistem_alamat', $data_alamat);
            $alamat = $this->db->get_where('sistem_alamat', ['nm_rt' => $data_inputan['nm_rt'], 'nm_rw' => $data_inputan['nm_rw'], 'kd_pos' => $data_inputan['kode_pos'], 'nm_desa' => $data_inputan['nm_desa']])->row_array();
            $id_alamat = $alamat['id'];
        endif;
        $lk_file = 'assets/foto_ktp';
        if (!is_dir($lk_file))
            mkdir($lk_file, 0777, TRUE);
        $config['upload_path']   = './assets/foto_ktp';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Foto KTP</div>');
            redirect('owner');
        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_anggota = array(
                'nik'            => $data_inputan['nik'],
                'nm_anggota'     => $data_inputan['nm_anggota'],
                'jk'             => $data_inputan['jk'],
                'alamat_lengkap' => $data_inputan['alamat_lengkap'],
                'id_alamat'      => $id_alamat,
                'tempat_lahir'   => $data_inputan['tempat_lahir'],
                'tgl_lahir'      => $data_inputan['tgl_lahir'],
                'no_hp'          => $data_inputan['no_hp'],
                'pekerjaan'      => $data_inputan['pekerjaan'],
                'suami_istri'    => $data_inputan['suami_istri'],
                'foto_ktp'       => $uploadData['file_name'],
                'status_anggota' => 'CALON_ANGGOTA'
            );
            $this->db->insert('anggota', $data_anggota);
        endif;
    }
    function tambah_detail_anggota_keluarga($data_inputan)
    {
        $data = array(
            'kk_id'           => $data_inputan['no_kk'],
            'anggota_id'      => $data_inputan['id_anggota'],
            'status_keluarga' => 'anak'
        );
        $this->db->insert('anggota_keluarga_detail', $data);
    }
    function tambah_anggota_keluarga($data_inputan)
    {
        $data_kk = array(
            'nomor_kk'     => $data_inputan['no_kk'],
            'anggota_id'   => $data_inputan['id_anggota'],
            'jml_keluarga' => 1
        );
        $this->db->insert('anggota_keluarga', $data_kk);
        $data = array(
            'kk_id'           => $data_inputan['no_kk'],
            'anggota_id'      => $data_inputan['id_anggota'],
            'status_keluarga' => 'suami_istri'
        );
        $this->db->insert('anggota_keluarga_detail', $data);
        $this->db->where('kk_id', $data_inputan['no_kk_lama']);
        $this->db->delete('anggota_keluarga_detail');
        $this->db->where('nomor_kk', $data_inputan['no_kk_lama']);
        $this->db->delete('anggota_keluarga');
    }
}