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

class ControllerArisanTunggakan 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('kabag_arisan/insert_model');
        $this->load->model('kabag_arisan/select_model');
        $this->load->model('kabag_arisan/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    // Bayar Sesuai Undian Arisan Namun Nunggak
    // Hapus
    public function tunggakan_motor($id)
    {
        $inputan_id                       = $this->encrypt->decode($id);
        $produk_id                        = substr($inputan_id, 2, 2);
        $regist_arisan                    = substr($inputan_id, 0, 7);
        $jadwal_id                        = substr($inputan_id, 10, 20);
        $regist_undian                    = substr($inputan_id, 0, 10);
        $data_anggota                     = $this->db->get_where('p_arisan_anggota', ['regist_undian' => $regist_undian])->row_array();
        $data_arisan                      = $this->db->get_where('p_arisan', ['id' => $regist_arisan])->row_array();
        $data_invoice                     = $this->select_model->getDataTagihanUrutMotor();
        $data_laporan                     = $this->db->get_where('lap_arisan', ['jadwal_id' => $jadwal_id])->row_array();
        // Membuat kode invoice
        $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'      => $data_anggota['anggota_id'],
            'tgl_pembayaran'  => date('Y-m-d'),
            'qty_pembayaran'  => 1,
            'ttl_bayar'       => $data_arisan['tagihan_iuran'] * 1,
            'status_kas'      => 'MASUK',
            'status_nota'     => 'PROSES_TUNGGAKAN',
            'ket_nota'        => 'Iuran Arisan ' . $regist_arisan . ' Nomor Undian ' . $regist_undian,
            'petugas'         => $this->session->userdata('isloggedin')
        );
        $this->db->insert('nota', $data_voice);
        // Data untuk detail tabel invoice
        $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'  => 'TUNGGAKAN',
        );
        $this->db->insert('p_arisan_nota', $data_rinci);
        // Merubah data tunggakan
        $data_tunggakan = array(
            'status_tunggakan' => 'LUNAS'
        );
        $this->db->where('regist_undian', $regist_undian);
        $this->db->where('jadwal_id', $jadwal_id);
        $this->db->update('lap_arisan_nunggak', $data_tunggakan);
        $this->_cetakTandaTerima($nomor_invoice, $produk_id);
        $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Tagihan Arisan Tunggakan!</div>');
        redirect('arisan/laporan/laporan_motor/lap_detail/' . $this->encrypt->encode($data_laporan['id']));
    }
    // Bayar Sesuai Undian Arisan Namun Nunggak Modal
    public function crud()
    {
        if (isset($_POST['tunggakanBayar'])) :
            $regist_undian       = htmlspecialchars($this->input->post('regist_undian'));
            $regist_arisan       = htmlspecialchars($this->input->post('arisan_id'));
            $produk_id           = substr($regist_undian, 2, 2);
            $jadwal_id           = htmlspecialchars($this->input->post('jadwal_id'));
            $data_laporan        = $this->db->get_where('lap_arisan', ['jadwal_id' => $jadwal_id])->row_array();
            $data_anggota        = $this->db->get_where('p_arisan_anggota', ['regist_undian' => $regist_undian])->row_array();
            $data_arisan         = $this->db->get_where('p_arisan', ['id' => $regist_arisan])->row_array();
            $data_invoice        = $this->select_model->getDataTagihanUrutMotor();
            // // Dari Tunggakan Motor
            // Membuat kode invoice
            $cek_nomor_urut         = substr($data_invoice['id'], 8, 5);
            $no_invoice_selanjutnya = $cek_nomor_urut + 1;
            $total_huruf            = strlen($no_invoice_selanjutnya);
            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'      => $data_anggota['anggota_id'],
                'tgl_pembayaran'  => date('Y-m-d'),
                'qty_pembayaran'  => 1,
                'ttl_bayar'       => $data_arisan['tagihan_iuran'] * 1,
                'status_kas'      => 'MASUK',
                'status_nota'     => 'PROSES_TUNGGAKAN',
                'ket_nota'        => 'Iuran Arisan ' . $regist_arisan . ' Nomor Undian ' . $regist_undian,
                'petugas'         => $this->session->userdata('isloggedin')
            );
            $this->db->insert('nota', $data_voice);
            // Data untuk detail tabel invoice
            $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'  => 'TUNGGAKAN',
            );
            $this->db->insert('p_arisan_nota', $data_rinci);
            // Merubah data tunggakan
            $data_tunggakan = array(
                'status_tunggakan' => 'LUNAS'
            );
            $this->db->where('regist_undian', $regist_undian);
            $this->db->where('jadwal_id', $jadwal_id);
            $this->db->update('lap_arisan_nunggak', $data_tunggakan);
            $this->_cetakTandaTerima($nomor_invoice, $produk_id);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Tagihan Arisan Tunggakan!</div>');
            redirect('arisan/laporan/laporan_motor/lap_detail/' . $this->encrypt->encode($data_laporan['id']));
        endif;
    }
    // Bagian Private Function
    // 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);
        $tahun = date('Y');
        $bulan = date('m');
        if ($produk_id == 21) :
            $lokasi_file = 'assets/invoice/arisan_heboh/' . $tahun . '/' . $bulan . '/';
            $nama_produk = 'Arisan Heboh';
        else :
            $lokasi_file = 'assets/invoice/arisan_motor/' . $tahun . '/' . $bulan . '/';
            $nama_produk = 'Arisan Motor';
        endif;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'title_pdf'           => 'Invoice Pembayaran ' . $nama_produk . '',
            '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 = 'invoice_' . $kd_invoice;
        // setting paper
        $paper = 'A5';
        //orientasi paper potrait / landscape
        $orientation = "landscape";

        // $this->load->view('nota/nota_bayar_arisan_petugas', $data);
        $html = $this->load->view('document/nota/nota_bayar_arisan_petugas', $data, true);
        // run dompdf
        $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';
        $this->_notifWABuktiSetorAsli($nama_file, $lokasi_file, $data_invoice);
    }
    // Bagian WA Notif
    // Notifikasi WA Bukti Setor Nomor Saya
    private function _notifWABuktiSetorAsli($nama_file, $lokasi_file, $data_invoice)
    {
        $no_hp               = substr(htmlspecialchars($this->input->post('no_hp')), 0, 1);
        $sett_wa             = $this->db->get_where('sistem_wa', ['id' => 2])->row_array();
        if ($no_hp == 0) :
            $cek_nomor = '62' . substr(htmlspecialchars($this->input->post('no_hp')), 1, 20);
        else :
            $cek_nomor = htmlspecialchars($this->input->post('no_hp'));
        endif;

        $caption = 'Hei, *_' . $data_invoice['nm_anggota'] . '_*,

Terimakasih sudah melakukan pembayaran iuran Arisan Motor, Ini adalah Pesan Otomatis memberitahu Bukti Setor Pembayaran.
        
*Info :* 085162800600
*_PESAN OTOMATIS ROBIN_*';

        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'media_type' => 'document',
            'caption'    => $caption,
            'footer'     => $footer,
            'url'        => base_url($lokasi_file . $nama_file)
        );
        $curl = curl_init($sett_wa['endpoint_url']);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_inputan));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_exec($curl);
        curl_close($curl);
    }
}

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