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

class ControllerBeranda extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        check_not_login();
        if ($this->session->userdata('level') != 4) {
            redirect();
        }
        $this->set_timezone();
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('Mycustom');
        $this->load->library('pdfgenerator');
        $this->load->model('notifikasi_arisan');
        $this->load->model('bagian_arisan/insert_model');
        $this->load->model('bagian_arisan/select_model');
        $this->load->model('bagian_arisan/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        // Notifikasi
        $jml_pengajuan         = count($this->notifikasi_arisan->Notifikasi_pengajuan());
        $data_kotkab           = $this->select_model->vd_kotkab();
        // End_notifikasi 
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        // Query SQL Data atau isi data page 
        $data_kaskelompok             = $this->select_model->getDataSemuaKasKelompok(); // Menampilkan total saldo kas kelompok arisan heboh
        $data_kaslelang               = $this->select_model->getDataSemuaKasLelang(); //Menampilkan semua total saldo kas lelang kelompok arisan motor
        $data_pendapatankelompok      = $this->select_model->getDataSemuaPendapatanKelompok(); // Menampilkan semua data pendapatan kelompok arisan heboh
        $data_pendapatanlelang        = $this->select_model->getDataSemuaPendapatanLelang(); // menampilkan semua data pendatan kelompok arisan motor
        $data_laporankelompok         = $this->select_model->getDataSemuaLaporanHeboh();
        $data_laporanlelang           = $this->select_model->getDataSemuaLaporanLelang();
        $anggota_arisan_heboh         = $this->select_model->vd_anggotaArisanHeboh();
        $da_arisan_heboh              = $this->select_model->vd_detailanggotaArisanHeboh();
        $anggota_arisan_motor         = $this->select_model->vd_anggotaArisanMotor();
        $da_arisan_motor              = $this->select_model->vd_detailanggotaArisanMotor();
        $data_arisanheboh_belum       = $this->select_model->getProdukArisanHebohBelum();
        $data_arisanmotor_belum       = $this->select_model->getProdukArisanMotorBelum();
        $data_arisanheboh             = $this->select_model->getProdukArisanHeboh();
        $data_arisanmotor             = $this->select_model->getProdukArisanMotor();
        $data_tunggak                 = $this->select_model->getDataTunggakanArisan();
        $jml_tunggakan                = count($data_tunggak);
        // echo '<pre>';
        // var_dump($data_arisanheboh_belum, $data_arisanheboh);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder' => 'beranda',
            'page' => 'index',
            'judul' => 'Beranda Arisan || Bagian Arisan',
            // Isi data
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'data_kota'           => $data_kotkab,
            // Data isian konten
            'kas_kelompok'        => $data_kaskelompok,
            'kas_lelang'          => $data_kaslelang,
            'pendapatan_kelompok' => $data_pendapatankelompok,
            'pendapatan_lelang'   => $data_pendapatanlelang,
            'laporan_kelompok'    => $data_laporankelompok,
            'laporan_lelang'      => $data_laporanlelang,
            'tunggakan'           => $data_tunggak,
            'anggota_nunggak'     => $jml_tunggakan,
            'arisan_heboh_belum'  => $data_arisanheboh_belum,
            'arisan_motor_belum'  => $data_arisanmotor_belum,
            'arisan_heboh'        => $data_arisanheboh,
            'arisan_motor'        => $data_arisanmotor,
            'jml_pengajuan'       => $jml_pengajuan,
            'anggota_heboh'       => $anggota_arisan_heboh,
            'detail_produk_heboh' => $da_arisan_heboh,
            'anggota_motor'       => $anggota_arisan_motor,
            'detail_produk_motor' => $da_arisan_motor
        );
        $this->load->view('arisan/index', $data);
    }
    public function beranda()
    {
        redirect('arisan');
    }
    public function crud()
    {
        if (isset($_POST['tambahBayarArisanHeboh'])) {
            $produk_id     = 21;
            $regist_arisan = htmlspecialchars($this->input->post('arisan_heboh'));
            $data_arisan   = $this->db->get_where('p_arisan', ['id' => $regist_arisan])->row_array();
            $anggota_id    = htmlspecialchars($this->input->post('anggota_id'));
            $regist_undian = htmlspecialchars($this->input->post('regist_undian'));
            $jadwal_id     = htmlspecialchars($this->input->post('jadwal_id'));
            $kategori_id   = 21;
            // Menampilkan data terakhir invoice
            $data_invoice           = $this->select_model->getDataTagihanUrutHeboh();
            $cek_nomor_urut         = substr($data_invoice['id'], 8, 5);
            $no_invoice_selanjutnya = $cek_nomor_urut + 1;
            $total_huruf            = strlen($no_invoice_selanjutnya);
            // Membuat kode invoice
            if ($total_huruf == 1) :
                $nomor_invoice = date('ymd') . $produk_id . '000' . $no_invoice_selanjutnya;
            elseif ($total_huruf == 2) :
                $nomor_invoice = date('ymd') . $produk_id . '00' . $no_invoice_selanjutnya;
            elseif ($total_huruf == 3) :
                $nomor_invoice = date('ymd') . $produk_id . '0' . $no_invoice_selanjutnya;
            else :
                $nomor_invoice = date('ymd') . $produk_id . $no_invoice_selanjutnya;
            endif;
            // Data untuk tabel invoice
            $data_voice = array(
                'id'              => $nomor_invoice,
                'produk_id'       => $produk_id,
                'anggota_id'      => $anggota_id,
                'tgl_pembayaran'  => date('Y-m-d'),
                'qty_pembayaran'  => 1,
                'ttl_bayar'       => $data_arisan['tagihan_iuran'] * 1,
                'status_kas'      => 'MASUK',
                'status_nota'     => 'PROSES',
                'petugas'         => $this->session->userdata('isloggedin')
            );
            $this->db->insert('nota', $data_voice);
            $data_rinci = array(
                'nota_id'       => $nomor_invoice,
                'jadwal_id'     => $jadwal_id,
                'regist_arisan' => $regist_arisan,
                'regist_undian' => $regist_undian,
                'bayar'         => $data_arisan['tagihan_iuran'],
                'status_bayar'  => 'LANCAR',
            );
            $this->db->insert('p_arisan_nota', $data_rinci);
            $cek_option = $this->db->get_where('sistem_option')->row_array();
            if ($cek_option['uji_coba_pdf'] == 'N') :
                $this->_cetakTandaTerima($nomor_invoice, $produk_id);
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Tagihan Arisan!</div>');
            redirect('arisan');
        }
        if (isset($_POST['tambahBayarArisanMotor'])) {
            $produk_id     = 22;
            $regist_arisan = htmlspecialchars($this->input->post('id_arisan_motor'));
            $data_arisan   = $this->db->get_where('p_arisan', ['id' => $regist_arisan])->row_array();
            $anggota_id    = htmlspecialchars($this->input->post('anggota_id_motor'));
            $regist_undian = htmlspecialchars($this->input->post('regist_undian_motor'));
            $jadwal_id     = htmlspecialchars($this->input->post('jadwal_id_motor'));
            // Menampilkan data terakhir invoice
            $data_invoice           = $this->select_model->getDataTagihanUrutHeboh();
            $cek_nomor_urut         = substr($data_invoice['id'], 8, 5);
            $no_invoice_selanjutnya = $cek_nomor_urut + 1;
            $total_huruf            = strlen($no_invoice_selanjutnya);
            // Membuat kode invoice
            if ($total_huruf == 1) :
                $nomor_invoice = date('ymd') . $produk_id . '000' . $no_invoice_selanjutnya;
            elseif ($total_huruf == 2) :
                $nomor_invoice = date('ymd') . $produk_id . '00' . $no_invoice_selanjutnya;
            elseif ($total_huruf == 3) :
                $nomor_invoice = date('ymd') . $produk_id . '0' . $no_invoice_selanjutnya;
            else :
                $nomor_invoice = date('ymd') . $produk_id . $no_invoice_selanjutnya;
            endif;
            // Data untuk tabel invoice
            $data_voice = array(
                'id'              => $nomor_invoice,
                'produk_id'       => $produk_id,
                'anggota_id'      => $anggota_id,
                'tgl_pembayaran'  => date('Y-m-d'),
                'qty_pembayaran'  => 1,
                'ttl_bayar'       => $data_arisan['tagihan_iuran'] * 1,
                'status_kas'      => 'MASUK',
                'status_nota'     => 'PROSES',
                'petugas'         => $this->session->userdata('isloggedin')
            );
            $this->db->insert('nota', $data_voice);
            $data_rinci = array(
                'nota_id'       => $nomor_invoice,
                'jadwal_id'     => $jadwal_id,
                'regist_arisan' => $regist_arisan,
                'regist_undian' => $regist_undian,
                'bayar'         => $data_arisan['tagihan_iuran'],
                'status_bayar'  => 'LANCAR',
            );
            $this->db->insert('p_arisan_nota', $data_rinci);
            $cek_option = $this->db->get_where('sistem_option')->row_array();
            if ($cek_option['uji_coba_pdf'] == 'N') :
                $this->_cetakTandaTerima($nomor_invoice, $produk_id);
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Tagihan Arisan!</div>');
            redirect('arisan');
        }
        if (isset($_POST['simpanAnggotaArisanHeboh'])) {
            $buat_regist_undian = $this->_BuatResgistArisan();
            $alamat_id          = $this->_cekDataAlamat();
            $data_inputan       = $this->_inputanDaftarAnggotaHeboh($alamat_id);
            $this->_kapasitasAnggota($data_inputan);
            $this->_v_anggota($data_inputan, $buat_regist_undian, $alamat_id);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Menambahkan anggota arisan heboh!</div>');
            redirect('arisan');
        }
        if (isset($_POST['simpanAnggotaArisanMotor'])) {
            $buat_regist_undian = $this->_BuatResgistArisan();
            $alamat_id          = $this->_cekDataAlamatMotor();
            $data_inputan       = $this->_inputanDaftarAnggotaMotor($alamat_id);
            $this->_kapasitasAnggota($data_inputan);
            $this->_v_anggota($data_inputan, $buat_regist_undian, $alamat_id);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Menambahkan anggota arisan motor!</div>');
            redirect('arisan');
        }
    }
    public function cek_arisan()
    {
        // Cek Identitas arisan dan jadwal arisan
        $nik                 = $this->input->post('nik');
        $regist_arisan       = $this->input->post('regist_arisan');
        $data_jadwal         = $this->db->get_where('p_arisan_jadwal', ['regist_arisan' => $regist_arisan, 'status_undian' => 'BELUM'])->row_array();
        $jadwal_id           = $data_jadwal['id'];
        // Cek Inputan NIK/No urut/Nomor HP
        if (strlen($nik) > 0 && strlen($nik) <= 3) :
            // Nomor Urut Undian
            // Cari terdaftar di tabel arisan anggota atau tidak
            $data_anggota_arisan = $this->db->get_where('p_arisan_anggota', ['regist_arisan' => $regist_arisan, 'no_urut' => $nik, 'status' => 'AKTIF'])->row_array();
            if ($data_anggota_arisan > 0) :
                $regist_undian = $data_anggota_arisan['no_regist'];
                $anggota_id    = $data_anggota_arisan['anggota_id'];
            else :
                $regist_undian = 0;
                $anggota_id    = 0;
            endif;
        elseif (strlen($nik) >= 4 && strlen($nik) <= 10) :
            // Nomor Registrasi Undian
            // Cari terdaftar di tabel arisan anggota atau tidak
            $data_anggota_arisan = $this->db->get_where('p_arisan_anggota', ['regist_undian' => $nik, 'status' => 'AKTIF'])->row_array();
            if ($data_anggota_arisan > 0) :
                $regist_undian = $data_anggota_arisan['regist_undian'];
                $anggota_id    = $data_anggota_arisan['anggota_id'];
            else :
                $regist_undian = 0;
                $anggota_id    = 0;
            endif;
        elseif (strlen($nik) >= 11) :
            if (substr($nik, 0, 1) == 0) :
                // Nomor HP sebagai inputan
                $data_anggota = $this->db->get_where('anggota', ['no_hp' => $nik])->row_array();
                if ($data_anggota > 0) :
                    $anggota_id = $data_anggota['id'];
                else :
                    $anggota_id = 0;
                endif;
                $data_anggota_arisan = $this->db->get_where('p_arisan_anggota', ['anggota_id' => $anggota_id, 'regist_arisan' => $regist_arisan, 'status' => 'AKTIF'])->row_array();
                if ($data_anggota_arisan > 0) :
                    $regist_undian = $data_anggota_arisan['regist_undian'];
                    $anggota_id    = $data_anggota_arisan['anggota_id'];
                else :
                    $regist_undian = 0;
                    $anggota_id    = 0;
                endif;
            else :
                // Nomor NIK sebagai inputan
                $data_anggota = $this->db->get_where('anggota', ['nik' => $nik])->row_array();
                if ($data_anggota > 0) :
                    $anggota_id = $data_anggota['id'];
                else :
                    $anggota_id = 0;
                endif;
                $data_anggota_arisan = $this->db->get_where('p_arisan_anggota', ['anggota_id' => $anggota_id, 'regist_arisan' => $regist_arisan, 'status' => 'AKTIF'])->row_array();
                if ($data_anggota_arisan > 0) :
                    $regist_undian = $data_anggota_arisan['regist_undian'];
                    $anggota_id    = $data_anggota_arisan['anggota_id'];
                else :
                    $regist_undian = 0;
                    $anggota_id    = 0;
                endif;
            endif;
        else :
            $regist_undian = 0;
            $anggota_id    = 0;
        endif;
        $cek_akhir_anggota  = $this->db->get_where('p_arisan_anggota', ['anggota_id' => $anggota_id, 'regist_undian' => $regist_undian, 'status' => 'AKTIF'])->row_array();
        if ($cek_akhir_anggota > 0) :
            $cek_tagihan_arisan = $this->db->get_where('p_arisan_nota', ['jadwal_id' => $jadwal_id, 'regist_undian' => $regist_undian])->row_array();
            if ($cek_tagihan_arisan > 0) :
                $data_ajax = array(
                    'status_anggota'     => 'Anggota Sudah Bayar'
                );
            else :
                $data_anggota_akhir_new = $this->db->get_where('anggota', ['id' => $anggota_id])->row_array();
                $data_ajax = array(
                    'status_anggota'     => $regist_undian . ' - ' . $data_anggota_akhir_new['nm_anggota'] . ' - ' . $data_anggota_akhir_new['nik'],
                    'jadwal_id'          => $jadwal_id,
                    'anggota_id'         => $anggota_id,
                    'regist_undian'      => $regist_undian
                );
            endif;
        else :
            $data_ajax = array(
                'status_anggota'     => 'Anggota Tidak Terdaftar / Sudah Menang / Mengundurkan Diri'
            );
        endif;
        echo json_encode($data_ajax);
    }
    // Membuat kode registrasi anggota arisan
    private function _BuatResgistArisan()
    {
        $regist_arisan = htmlspecialchars($this->input->post('regist_arisan'));
        $no_undian    = $this->select_model->getDataNomorUndian($regist_arisan);
        $no_undian_selanjutnya = $no_undian['no_urut'] + 1;
        $total_huruf = strlen($no_undian_selanjutnya);
        if ($total_huruf == 1) :
            $regist_undian = $regist_arisan . '00' . $no_undian_selanjutnya;
            $no_urut = $no_undian_selanjutnya;
        elseif ($total_huruf == 2) :
            $regist_undian = $regist_arisan . '0' . $no_undian_selanjutnya;
            $no_urut = $no_undian_selanjutnya;
        else :
            $regist_undian = $regist_arisan . $no_undian_selanjutnya;
            $no_urut = $no_undian_selanjutnya;
        endif;
        $data = array(
            'no_urut' => $no_urut,
            'regist_undian' => $regist_undian
        );
        return $data;
    }
    # ======================================================================================================================================================================================================
    # ======================================================================================================================================================================================================
    # ======================================================================================================================================================================================================
    # ======================================================================================================================================================================================================
    # ======================================================================================================================================================================================================
    // Private Funtion Mulai
    // Konversi Kode Alamat ke dalam Huruf
    // Melihat alamat lengkap
    private function _KKodeAlamatKeHuruf()
    {
        // Cek Provinsi
        $provinsi               = file_get_contents('https://open-api.my.id/api/wilayah/provinces/');
        $data_provinsi          = json_decode($provinsi, true);
        foreach ($data_provinsi as $Provinsi) :
            if ($Provinsi['id'] == htmlspecialchars($this->input->post('tah_province_id'))) :
                $prov = array(
                    'id'   => $Provinsi['id'],
                    'name' => $Provinsi['name']
                );
            endif;
        endforeach;
        // Cek Kabupaten
        $kab_kota               = file_get_contents('https://open-api.my.id/api/wilayah/regencies/' . $prov['id']);
        $data_kab_kota          = json_decode($kab_kota, true);
        foreach ($data_kab_kota as $Kabupaten) :
            if ($Kabupaten['id'] == htmlspecialchars($this->input->post('tah_kot_kab'))) :
                $kab = array(
                    'id'   => $Kabupaten['id'],
                    'name' => $Kabupaten['name']
                );
            endif;
        endforeach;
        // Cek Kecamatan
        $kecamatan               = file_get_contents('https://open-api.my.id/api/wilayah/districts/' . $kab['id']);
        $data_kecamatan          = json_decode($kecamatan, true);
        foreach ($data_kecamatan as $Kec) :
            if ($Kec['id'] == htmlspecialchars($this->input->post('tah_nm_kec'))) :
                $nm_kec = array(
                    'id'   => $Kec['id'],
                    'name' => $Kec['name']
                );
            endif;
        endforeach;
        // Cek Nama Desa atau Kelurahan
        $deskel               = file_get_contents('https://open-api.my.id/api/wilayah/villages/' . $nm_kec['id']);
        $data_deskel          = json_decode($deskel, true);
        foreach ($data_deskel as $Deskel) :
            if ($Deskel['id'] == htmlspecialchars($this->input->post('tah_nm_desa'))) :
                $desa_kelurahan = array(
                    'id'   => $Deskel['id'],
                    'name' => $Deskel['name']
                );
            endif;
        endforeach;
        $data_alamat = array(
            'prov'         => $prov['name'],
            'kot_kab'      => $kab['name'],
            'nm_kecamatan' => $nm_kec['name'],
            'nm_desa'      => $desa_kelurahan['name'],
        );
        return $data_alamat;
    }
    private function _KKodeAlamatKeHurufMotor()
    {
        // Cek Provinsi
        $provinsi               = file_get_contents('https://open-api.my.id/api/wilayah/provinces/');
        $data_provinsi          = json_decode($provinsi, true);
        foreach ($data_provinsi as $Provinsi) :
            if ($Provinsi['id'] == htmlspecialchars($this->input->post('tam_province_id'))) :
                $prov = array(
                    'id'   => $Provinsi['id'],
                    'name' => $Provinsi['name']
                );
            endif;
        endforeach;
        // Cek Kabupaten
        $kab_kota               = file_get_contents('https://open-api.my.id/api/wilayah/regencies/' . $prov['id']);
        $data_kab_kota          = json_decode($kab_kota, true);
        foreach ($data_kab_kota as $Kabupaten) :
            if ($Kabupaten['id'] == htmlspecialchars($this->input->post('tam_kot_kab'))) :
                $kab = array(
                    'id'   => $Kabupaten['id'],
                    'name' => $Kabupaten['name']
                );
            endif;
        endforeach;
        // Cek Kecamatan
        $kecamatan               = file_get_contents('https://open-api.my.id/api/wilayah/districts/' . $kab['id']);
        $data_kecamatan          = json_decode($kecamatan, true);
        foreach ($data_kecamatan as $Kec) :
            if ($Kec['id'] == htmlspecialchars($this->input->post('tam_nm_kec'))) :
                $nm_kec = array(
                    'id'   => $Kec['id'],
                    'name' => $Kec['name']
                );
            endif;
        endforeach;
        // Cek Nama Desa atau Kelurahan
        $deskel               = file_get_contents('https://open-api.my.id/api/wilayah/villages/' . $nm_kec['id']);
        $data_deskel          = json_decode($deskel, true);
        foreach ($data_deskel as $Deskel) :
            if ($Deskel['id'] == htmlspecialchars($this->input->post('tam_nm_desa'))) :
                $desa_kelurahan = array(
                    'id'   => $Deskel['id'],
                    'name' => $Deskel['name']
                );
            endif;
        endforeach;
        $data_alamat = array(
            'prov'         => $prov['name'],
            'kot_kab'      => $kab['name'],
            'nm_kecamatan' => $nm_kec['name'],
            'nm_desa'      => $desa_kelurahan['name'],
        );
        return $data_alamat;
    }
    // Mencari ID alamat database
    private function _cekDataAlamat()
    {
        $cek_alamat = $this->db->get_where('sistem_alamat', ['nm_desa' => htmlspecialchars($this->input->post('tah_dk_ds')), 'nm_kecamatan' => htmlspecialchars($this->input->post('tah_kecamatan_baru')), htmlspecialchars($this->input->post('tah_kota_kabupaten'))])->row_array();
        if ($cek_alamat > 0) :
            $alamat_id = $cek_alamat['id'];
            return $alamat_id;
        else :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Tidak di Temukan, Hubungi Developer!</div>');
            redirect('arisan');
        endif;
    }
    private function _cekDataAlamatMotor()
    {
        $cek_alamat = $this->db->get_where('sistem_alamat', ['nm_desa' => htmlspecialchars($this->input->post('tam_dk_ds')), 'nm_kecamatan' => htmlspecialchars($this->input->post('tam_kecamatan_baru')), htmlspecialchars($this->input->post('tam_kota_kabupaten'))])->row_array();
        if ($cek_alamat > 0) :
            $alamat_id = $cek_alamat['id'];
            return $alamat_id;
        else :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Alamat Tidak di Temukan, Hubungi Developer!</div>');
            redirect('arisan');
        endif;
    }
    // Data Inputan Anggota Heboh
    private function _inputanDaftarAnggotaHeboh($alamat_id)
    {
        $data_inputan = array(
            'regist_arisan'  => htmlspecialchars($this->input->post('regist_arisan')),
            'nik'            => htmlspecialchars($this->input->post('nik_anggota')),
            'nm_anggota'     => htmlspecialchars($this->input->post('tah_nm_anggota')),
            'login_id'       => htmlspecialchars($this->input->post('login_id')),
            'jk'             => htmlspecialchars($this->input->post('tah_jk')),
            'alamat_lengkap' => htmlspecialchars($this->input->post('tah_alamat_lengkap')),
            'kode_pos'       => htmlspecialchars($this->input->post('tah_kode_pos')),
            'nm_rt'          => htmlspecialchars($this->input->post('tah_nm_rt')),
            'nm_rw'          => htmlspecialchars($this->input->post('tah_nm_rw')),
            'id_alamat'      => $alamat_id,
            'tempat_lahir'   => htmlspecialchars($this->input->post('tah_tempat_lahir')),
            'tgl_lahir'      => htmlspecialchars($this->input->post('tah_tgl_lahir')),
            'no_hp'          => htmlspecialchars($this->input->post('tah_no_hp')),
            'pekerjaan'      => htmlspecialchars($this->input->post('tah_pekerjaan')),
            'suami_istri'    => htmlspecialchars($this->input->post('tah_suami_istri')),
            'foto_ktp'       => htmlspecialchars($this->input->post('foto_ktp'))
        );
        return $data_inputan;
    }
    private function _inputanDaftarAnggotaMotor($alamat_id)
    {
        $data_inputan = array(
            'regist_arisan'  => htmlspecialchars($this->input->post('regist_arisan')),
            'nik'            => htmlspecialchars($this->input->post('nik_motor')),
            'nm_anggota'     => htmlspecialchars($this->input->post('tam_nm_anggota')),
            'login_id'       => htmlspecialchars($this->input->post('login_id')),
            'jk'             => htmlspecialchars($this->input->post('tam_jk')),
            'alamat_lengkap' => htmlspecialchars($this->input->post('tam_alamat_lengkap')),
            'kode_pos'       => htmlspecialchars($this->input->post('tam_kode_pos')),
            'nm_rt'          => htmlspecialchars($this->input->post('tam_nm_rt')),
            'nm_rw'          => htmlspecialchars($this->input->post('tam_nm_rw')),
            'id_alamat'      => $alamat_id,
            'tempat_lahir'   => htmlspecialchars($this->input->post('tam_tempat_lahir')),
            'tgl_lahir'      => htmlspecialchars($this->input->post('tam_tgl_lahir')),
            'no_hp'          => htmlspecialchars($this->input->post('tam_no_hp')),
            'pekerjaan'      => htmlspecialchars($this->input->post('tam_pekerjaan')),
            'suami_istri'    => htmlspecialchars($this->input->post('tam_suami_istri')),
            'foto_ktp'       => htmlspecialchars($this->input->post('foto_ktp'))
        );
        return $data_inputan;
    }
    // Validasi total anggota
    private function _kapasitasAnggota($data_inputan)
    {
        $cek_arisan = $this->db->get_where('p_arisan', ['id' => $data_inputan['regist_arisan']])->row_array();
        $cek_anggota = $this->db->get_where('p_arisan_anggota', ['regist_arisan' => $data_inputan['regist_arisan']])->result();
        $ttl_anggota = count($cek_anggota);
        if ($ttl_anggota >= $cek_arisan['target_anggota']) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Anggota Melebihi Target Kelompok, Silahkan ubah target kelompok!</div>');
            redirect('arisan');
        endif;
    }
    // validasi calon anggota koperasi daftar arisan
    private function _v_anggota($data_inputan, $buat_regist_undian, $alamat_id)
    {
        $cek_anggota = $this->db->get_where('anggota', ['nik' => $data_inputan['nik']])->row_array();
        if ($cek_anggota > 0) :
            // Daftar Arisan Langsung
            $this->_daftarAnggotaArisanHeboh($data_inputan, $cek_anggota, $buat_regist_undian);
        else :
            // Daftar ID Anggota terlebih dahulu
            $this->_daftarAnggotaKoperasi($data_inputan);
            $this->insert_model->tambahAnggotaKoperasi($data_inputan, $buat_regist_undian, $alamat_id);
        endif;
    }
    // Tambah Anggota Arisan dan Validasi nomor HP
    private function _daftarAnggotaArisanHeboh($data_inputan, $cek_anggota, $buat_regist_undian)
    {
        if ($cek_anggota['no_hp'] == $data_inputan['no_hp']) :
            $this->insert_model->daftarAnggotaArisan($data_inputan, $cek_anggota, $buat_regist_undian);
        else :
            $this->insert_model->daftarAnggotaArisan($data_inputan, $cek_anggota, $buat_regist_undian);
            $this->insert_model->masukNoHPCadangan($data_inputan, $cek_anggota, $buat_regist_undian);
        endif;
    }
    // Menambahkan data calon koperasi
    private function _daftarAnggotaKoperasi($data_inputan)
    {
        // Validasi Nomor HP
        $cek_hp = $this->db->get_where('anggota', ['no_hp' => $data_inputan['no_hp']])->row_array();
        if ($cek_hp > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP anda sudah di pakai orang lain!</div>');
            redirect('arisan' . $this->encrypt->encode($data_inputan['regist_arisan']));
        endif;
        $cek_hp2 = $this->db->get_where('anggota_hp_cadangan', ['no_hp' => $data_inputan['no_hp']])->row_array();
        $cek_hp2 = $this->db->get_where('anggota', ['no_hp' => $data_inputan['no_hp']])->row_array();
        if ($cek_hp2 > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP anda sudah di pakai orang lain!</div>');
            redirect('arisan' . $this->encrypt->encode($data_inputan['regist_arisan']));
        endif;
    }
    // Konversi Nota Tanda Terima
    private function _cetakTandaTerima($nomor_invoice, $produk_id)
    {
        $kd_invoice            = $nomor_invoice;
        $data_invoice          = $this->select_model->getDataInvoice($kd_invoice); // DONE
        $data_rinci            = $this->select_model->getDataInvoiceRinci($kd_invoice);
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        // Nama Marketing
        $data_marketing        = $this->select_model->getDataMarketing($kd_invoice);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'title_pdf'           => 'Invoice Pembayaran Arisan Heboh',
            'folder'              => 'arisan/heboh',
            'page'                => 'index',
            'judul'               => 'Kelola Bayar Arisan Heboh || Bagian Kasir',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            // Data isi konten
            'data_invoice'        => $data_invoice,
            'data_rinci'          => $data_rinci,
            'data_marketing'      => $data_marketing
        );
        // $this->load->view('nota/nota_bayar_arisan', $data_halaman);
        // filename dari pdf ketika didownload
        $file_pdf = 'bukti_setor_' . $kd_invoice . '_' . $data_marketing['petugas'];
        // setting paper
        $paper = 'A5';
        //orientasi paper potrait / landscape
        $orientation = "landscape";

        // $this->load->view('nota/nota_bayar_arisan_petugas', $data);
        $html = $this->load->view('nota/nota_bayar_arisan_petugas', $data, true);
        $tahun = date('Y');
        $bulan = date('m');
        // run dompdf
        if ($produk_id == 21) :
            $lokasi_file = 'assets/invoice/arisan_heboh/' . $tahun . '/' . $bulan . '/';
        else :
            $lokasi_file = 'assets/invoice/arisan_motor/' . $tahun . '/' . $bulan . '/';
        endif;
        $this->pdfgenerator->generate($html, $file_pdf, $paper, $lokasi_file, $orientation);
        $data_update22     = array(
            'invoice_pdf' => $file_pdf . '.pdf'
        );
        $this->db->where('id', $kd_invoice);
        $this->db->update('nota', $data_update22);
        $nama_file = $file_pdf . '.pdf';
        $cek_option = $this->db->get_where('sistem_option')->row_array();
        if ($cek_option['notif_wa_arisan'] == 1) :
            // Mode Kirim ke nomor saya
            $this->_notifWABuktiSetorPercobaan($nama_file, $lokasi_file, $cek_option);
        elseif ($cek_option['notif_wa_arisan'] == 2) :
            // Mode Kirim ke nomor asli anggota
            $this->_notifWABuktiSetorAsli($nama_file, $lokasi_file, $data_invoice, $cek_option);
        endif;
    }
    // Notifikasi WA Bukti Setor Nomor Saya
    private function _notifWABuktiSetorPercobaan($nama_file, $lokasi_file, $cek_option)
    {
        $body = array(
            "api_key" => $cek_option['api_coba'],
            "receiver" => "628158884711",
            "data" => array(
                "url" => base_url($lokasi_file . $nama_file),
                "media_type" => "file",
                "caption" => "Bukti Setor Iuran"
            )
        );

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => "https://notif.kspbinaartha.com/api/send-media",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10000,
            CURLOPT_TIMEOUT => 30000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => [
                "Accept: */*",
                "Content-Type: application/json",
            ],
        ]);
        curl_exec($curl);
        curl_error($curl);
    }
    // Notifikasi WA Bukti Setor Nomor Saya
    private function _notifWABuktiSetorAsli($nama_file, $lokasi_file, $data_invoice, $cek_option)
    {
        $cek_nomor = substr($data_invoice['no_hp'], 0, 1);
        if ($cek_nomor == 0) :
            $no_hp = '62' . substr($data_invoice['no_hp'], 1, 20);
        else :
            $no_hp = $data_invoice['no_hp'];
        endif;
        $body = array(
            "api_key" => $cek_option['api_wa_arisan'],
            "receiver" => $no_hp,
            "data" => array(
                "url" => base_url($lokasi_file . $nama_file),
                "media_type" => "file",
                "caption" => "Bukti Setor Iuran"
            )
        );

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $cek_option['arisan_api_media'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => [
                "Accept: */*",
                "Content-Type: application/json",
            ],
        ]);
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
    }
}

/* End of file ControllerBeranda.php and path \application\controllers\arisan\ControllerBeranda.php */