<?php

use Google\Service\AndroidPublisher\Timestamp;

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

class Insert_model extends CI_Model
{
    // Ubah data karyawan
    function tambahDataIdentitasKaryawan($data_inputan, $alamat_id)
    {
        $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'             => $data_inputan['email_baru'],
                'no_hp'             => $data_inputan['no_hp_baru'],
                'nm_panggilan'      => $data_inputan['nm_panggilan']
            );
            $this->db->where('id', $data_inputan['login_id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'login_id'        => $data_inputan['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'      => $data_inputan['tgl_gabung']
            );
            $this->db->insert('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();
            // Inputan
            $data_login = array(
                'email'             => $data_inputan['email_baru'],
                'no_hp'             => $data_inputan['no_hp_baru'],
                'nm_panggilan'      => $data_inputan['nm_panggilan']
            );
            $this->db->where('id', $data_inputan['login_id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'login_id'        => $data_inputan['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'      => $data_inputan['tgl_gabung']
            );
            $this->db->insert('sistem_identitas', $data_identitas);
        endif;
    }
    // Tambah transaksi Anggaran Dasar Koperasi
    function simpanDataTransaksiAD()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $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);
            if (htmlspecialchars($this->input->post('jenis_transaksi')) == 'KELUAR') :
                if (htmlspecialchars($this->input->post('penggunaan_dana')) == 'KAS') :
                    $data_nota = array(
                        'id'                => date('ymdhis') . '10',
                        'produk_id'         => 10,
                        'tgl_pembayaran'    => date('Y-m-d'),
                        'qty_pembayaran'    => 1,
                        'ttl_bayar'         => htmlspecialchars($this->input->post('total_transaksi')),
                        'status_kas'        => 'MASUK',
                        '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);
                endif;
            endif;
            $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');
        $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);
            if (htmlspecialchars($this->input->post('jenis_transaksi')) == 'KELUAR') :
                if (htmlspecialchars($this->input->post('penggunaan_dana')) == 'OMSET') :
                    $data_nota = array(
                        'id'                => date('ymdhis') . '99',
                        'produk_id'         => 99,
                        'tgl_pembayaran'    => date('Y-m-d'),
                        'qty_pembayaran'    => 1,
                        'ttl_bayar'         => htmlspecialchars($this->input->post('total_transaksi')),
                        'status_kas'        => 'MASUK',
                        '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);
                endif;
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Melakukan Transaksi!</div>');
        endif;
    }
    function simpanDataTransaksiOperasionalGaji($data_selesai, $identitas, $data_gaji)
    {
        if (date('m', strtotime($data_gaji['tgl_gaji'])) == '01') :
            $periode = 'Januari ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '02'):
            $periode = 'Febuari ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '03'):
            $periode = 'Maret ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '04'):
            $periode = 'April ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '05'):
            $periode = 'Mei ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '06'):
            $periode = 'Juni ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '07'):
            $periode = 'Juli ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '08'):
            $periode = 'Agustus ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '09'):
            $periode = 'September ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '10'):
            $periode = 'Oktober ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '11'):
            $periode = 'November ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        elseif (date('m', strtotime($data_gaji['tgl_gaji'])) == '12'):
            $periode = 'Desember ' . date('Y', strtotime($data_gaji['tgl_gaji']));
        endif;
        $data_nota = array(
            'id'                => date('ymdhis'),
            'produk_id'         => 10,
            'tgl_pembayaran'    => date('Y-m-d'),
            'qty_pembayaran'    => 1,
            'ttl_bayar'         => $data_selesai['ttl_gaji'],
            'status_kas'        => 'KELUAR',
            'status_nota'       => 'TERIMA',
            'petugas'           => $this->session->userdata('isloggedin'),
            'kasir'             => $this->session->userdata('isloggedin'),
            'ket_nota'          => 'Pembayaran Gaji Karyawan ' . $identitas['nm_panggilan'] . ' Periode ' . $periode,
            'invoice_pdf'       => date('Y', strtotime($data_gaji['tgl_gaji'])) . date('m', strtotime($data_gaji['tgl_gaji'])) . $data_gaji['login_id']
        );
        $this->db->insert('nota', $data_nota);
    }
    // Tambah transaksi Arisan Heboh
    function simpanDataTransaksiArisanHeboh()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $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');
        $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 Karyawan Baru
    function tambahkaryawan()
    {
        $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)
    {
        $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)
    {
        $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;
        $config['upload_path']   = './assets/foto_kk';
        $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_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;
    }
    function tambahAnggotaKoperasi($data_inputan, $alamat_id)
    {
        $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_pengajuan = array(
                'anggota_id'    => $cek_anggota['id'],
                'regist_arisan' => $data_inputan['regist_arisan'],
                'rekom_id'      => $this->session->userdata('isloggedin')
            );
            $this->db->insert('p_arisan_pengajuan', $data_pengajuan);
            $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;
    }
    // Menambahkan data anggota arisan heboh
    function daftarAnggotaArisan($data_inputan, $cek_anggota)
    {
        $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_pengajuan = array(
                'anggota_id'    => $cek_anggota['id'],
                'regist_arisan' => $data_inputan['regist_arisan'],
                'rekom_id'      => $this->session->userdata('isloggedin')
            );
            $this->db->insert('p_arisan_pengajuan', $data_pengajuan);
        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(
                'foto_ktp' => $uploadData['file_name']
            );
            $this->db->where('nik', $data_inputan['nik']);
            $this->db->update('anggota', $data_anggota);
            $data_pengajuan = array(
                'anggota_id'    => $cek_anggota['id'],
                'regist_arisan' => $data_inputan['regist_arisan'],
                'rekom_id'      => $this->session->userdata('isloggedin')
            );
            $this->db->insert('p_arisan_pengajuan', $data_pengajuan);
        endif;
    }
    // Menambahkan Nomor HP Cadangan
    function masukNoHPCadangan($data_inputan, $cek_anggota)
    {
        $data = array(
            'anggota_id' => $cek_anggota['id'],
            'no_hp' => $data_inputan['no_hp'],
            'ket_cadangan' => 'Nomor ' . $cek_anggota['no_hp'] . ' di daftarkan sebagai nomor cadangan Anggota Arisan Heboh ' . $buat_regist_undian['regist_undian']
        );
        $this->db->insert('anggota_hp_cadangan', $data);
    }
    // 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;
        $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;
    }
    // Tambah Anggota Calon Koperasi
    function simpanAnggotaKoperasi()
    {
        // Inputan 
        $nik            = htmlspecialchars($this->input->post('nik'));
        $nm_anggota     = htmlspecialchars($this->input->post('nm_anggota'));
        $jk             = htmlspecialchars($this->input->post('jk'));
        $alamat_lengkap = htmlspecialchars($this->input->post('alamat_lengkap'));
        $kode_pos       = htmlspecialchars($this->input->post('kode_pos'));
        $nm_rt          = htmlspecialchars($this->input->post('nm_rt'));
        $nm_rw          = htmlspecialchars($this->input->post('nm_rw'));
        $tempat_lahir   = htmlspecialchars($this->input->post('tempat_lahir'));
        $tgl_lahir      = htmlspecialchars($this->input->post('tgl_lahir'));
        $no_hp          = htmlspecialchars($this->input->post('no_hp'));
        $pekerjaan      = htmlspecialchars($this->input->post('pekerjaan'));
        $suami_istri    = htmlspecialchars($this->input->post('suami_istri'));
        $cek_nik = $this->db->get_where('tbl_anggota', ['nik' => $nik])->row_array();
        if ($cek_nik > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Sudah Terdaftar!</div>');
            redirect('manager/sistem/anggota/tambah');
        endif;
        if (strlen($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('manager/sistem/anggota/tambah');
        }
        $cek_nomorhp = $this->db->get_where('tbl_anggota', ['no_hp' => $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('manager/sistem/anggota/tambah');
        endif;
        if ($jk == '3') :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Jenis Kelamin Tidak Di Isi!</div>');
            redirect('manager/sistem/anggota/tambah');
        endif;
        $cari_alamat = $this->db->get_where('sistem_alamat', ['nm_rt' => $nm_rt, 'nm_rw' => $nm_rw, 'kd_pos' => $kode_pos])->row_array();
        if ($cari_alamat > 0) :
            $id_alamat = $cari_alamat['id'];
        else :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Tidak Di Temukan!</div>');
            redirect('manager/sistem/anggota/tambah');
        endif;
        $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('manager/sistem/anggota/tambah');
        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'            => $nik,
                'nm_anggota'     => $nm_anggota,
                'jk'             => $jk,
                'alamat_lengkap' => $alamat_lengkap,
                'id_alamat'      => $id_alamat,
                'tempat_lahir'   => $tempat_lahir,
                'tgl_lahir'      => $tgl_lahir,
                'no_hp'          => $no_hp,
                'pekerjaan'      => $pekerjaan,
                'suami_istri'    => $suami_istri,
                'foto_ktp'       => $uploadData['file_name'],
                'status_anggota' => 'DAFTAR'
            );
            $this->db->insert('tbl_anggota', $data_anggota);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Tambah Anggota Koperasi!</div>');
            redirect('manager/sistem/anggota');
        endif;
    }
    // Pengajuan anggota arisan heboh
    function pengajuanAnggotaArisanHebohBeranda($alamat_id, $id_arisan)
    {
        $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 Foto KTP</div>');
            redirect('arisan');
        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(
                'nik'               => htmlspecialchars($this->input->post('nik')),
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'id_alamat'         => $alamat_id,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'          => $uploadData['file_name'],
                'status_anggota'    => 'CALON'
            );
            $this->db->insert('tbl_anggota', $data_anggota_koperasi);
            $cek_anggota = $this->db->get_where('tbl_anggota', ['nik' => htmlspecialchars($this->input->post('nik'))])->row_array();
            $data_tambah_arisan = array(
                'anggota_id'            => $cek_anggota['id'],
                'regist_arisan'         => $id_arisan,
                'login_id'              => $this->session->userdata('isloggedin')
            );
            $this->db->insert('tbl_arisan_pengajuan', $data_tambah_arisan);
        endif;
    }
    // Pengajuan anggota arisan Motor
    function pengajuanAnggotaArisanMotorBeranda($alamat_id, $id_arisan)
    {
        $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 Foto KTP</div>');
            redirect('arisan');
        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(
                'nik'               => htmlspecialchars($this->input->post('nik')),
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'id_alamat'         => $alamat_id,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'          => $uploadData['file_name'],
                'status_anggota'    => 'CALON'
            );
            $this->db->insert('tbl_anggota', $data_anggota_koperasi);
            $cek_anggota = $this->db->get_where('tbl_anggota', ['nik' => htmlspecialchars($this->input->post('nik'))])->row_array();
            $data_tambah_arisan = array(
                'anggota_id'            => $cek_anggota['id'],
                'regist_arisan'         => $id_arisan,
                'login_id'              => $this->session->userdata('isloggedin')
            );
            $this->db->insert('tbl_arisan_pengajuan', $data_tambah_arisan);
        endif;
    }
}