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

class ControllerPinjaman extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        check_not_login();
        $this->set_timezone();
        if ($this->session->userdata('level') != 5) {
            redirect();
        }
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('Mycustom');
        $this->load->library('pdfgenerator');
        $this->load->library('Notifikasi');
        $this->load->model('bagian_kasir/insert_model');
        $this->load->model('bagian_kasir/select_model');
        $this->load->model('bagian_kasir/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $notif                 = $this->notifikasi->notif_kasir();
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        // Data Isian Konten
        // Tampilkan Jenis Pinjaman 
        $pinjaman_aktif        = $this->select_model->vd_PinjamanAktif();
        $data_jaminan          = $this->select_model->vd_jaminanPinjaman();
        // Jumlah Pinjaman
        $jml_mingguan          = count($this->select_model->vd_PinjamanAktifMingguan());
        $jml_2mingguan         = count($this->select_model->vd_PinjamanAktif2Mingguan());
        $jml_BulananC          = count($this->select_model->vd_PinjamanAktifBulananC());
        $jml_BulananA          = count($this->select_model->vd_PinjamanAktifBulananA());
        $jml_BulananK          = count($this->select_model->vd_PinjamanAktifBulananK());
        $jml_Musiman           = count($this->select_model->vd_PinjamanAktifMusiman());
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'pinjaman/aktif',
            'page'                => 'index',
            'judul'               => 'Daftar Pinjaman Koperasi || Bagian Kasir',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi dalam konten
            'pinjaman_aktif'      => $pinjaman_aktif,
            'data_jaminan'        => $data_jaminan,
            // Jumlah aktif
            'p_mingguan'          => $jml_mingguan,
            'p_2mingguan'         => $jml_2mingguan,
            'p_BulananC'          => $jml_BulananC,
            'p_BulananA'          => $jml_BulananA,
            'p_BulananK'          => $jml_BulananK,
            'p_musiman'           => $jml_Musiman
        );
        $this->load->view('kasir/index', $data);
    }
    public function macet()
    {
        $notif                 = $this->notifikasi->notif_kasir();
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        // Data Isian Konten
        // Tampilkan Jenis Pinjaman 
        $pinjaman_aktif        = $this->select_model->vd_PinjamanMacet();
        $data_jaminan          = $this->select_model->vd_jaminanPinjaman();
        // Jumlah Pinjaman
        $jml_mingguan          = count($this->select_model->vd_PinjamanMacetMingguan());
        $jml_2mingguan         = count($this->select_model->vd_PinjamanMacet2Mingguan());
        $jml_BulananC          = count($this->select_model->vd_PinjamanMacetBulananC());
        $jml_BulananA          = count($this->select_model->vd_PinjamanMacetBulananA());
        $jml_BulananK          = count($this->select_model->vd_PinjamanMacetBulananK());
        $jml_Musiman           = count($this->select_model->vd_PinjamanMacetMusiman());
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'pinjaman/macet',
            'page'                => 'index',
            'judul'               => 'Daftar Pinjaman Koperasi Macet || Bagian Kasir',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi dalam konten
            'pinjaman_aktif'      => $pinjaman_aktif,
            'data_jaminan'        => $data_jaminan,
            // Jumlah aktif
            'p_mingguan'          => $jml_mingguan,
            'p_2mingguan'         => $jml_2mingguan,
            'p_BulananC'          => $jml_BulananC,
            'p_BulananA'          => $jml_BulananA,
            'p_BulananK'          => $jml_BulananK,
            'p_musiman'           => $jml_Musiman
        );
        $this->load->view('kasir/index', $data);
    }
    // Detail Kontrak pinjaman
    public function kontrak($id)
    {
        $notif                 = $this->notifikasi->notif_kasir();
        // Data Login 
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $kontrak_id            = $this->encrypt->decode($id);
        // Rincian Kontrak
        $rincian_kontrak       = $this->db->get_where('p_koperasi_kontrak', ['id' => $kontrak_id])->row_array();
        $data_realisasi        = $this->db->get_where('p_koperasi_progres', ['kontrak_id' => $kontrak_id])->row_array();
        $data_jaminan          = $this->db->get_where('p_koperasi_agungan', ['kontrak_id' => $kontrak_id])->row_array();
        $data_anggota          = $this->db->get_where('anggota', ['id' => $rincian_kontrak['anggota_id']])->row_array();
        $data_angsuran         = $this->select_model->vd_angsuran($kontrak_id);
        $sudah_bayar           = $this->select_model->vd_SudahBayarAngsuran($kontrak_id);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'pinjaman/kontrak',
            'page'                => 'index',
            'judul'               => 'Detail Kontrak Pinjaman Koperasi || Bagian Kasir',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            'identitas'           => $data_anggota,
            // Isi dalam konten
            'rincian_kontrak'     => $rincian_kontrak,
            'doc'                 => $data_realisasi,
            'jaminan'             => $data_jaminan,
            'data_angsuran'       => $data_angsuran,
            'sudah_bayar'         => $sudah_bayar
        );
        $this->load->view('kasir/index', $data);
    }
    // Pembayaran Angsuran Koperasi
    // Lancar
    public function bayar_lancar($id)
    {
        $angsuran_id            = $this->encrypt->decode($id);
        $status_angsuran        = 'LUNAS_LANCAR';
        $status_nota            = 'LANCAR';
        $data_angsuran          = $this->db->get_where('p_koperasi_angsuran', ['id' => $angsuran_id])->row_array();
        $data_kontrak           = $this->db->get_where('p_koperasi_kontrak', ['id' => $data_angsuran['kontrak_id']])->row_array();
        $data_produk            = $this->db->get_where('p_koperasi_aturan', ['id' => $data_kontrak['aturan_id']])->row_array();
        $nomor_invoice          = $data_produk['produk_id'] . $data_kontrak['id'] . date('ymdhis');
        $this->insert_model->simpanBayarAngsuranMenu($nomor_invoice, $data_produk, $data_kontrak,  $data_angsuran, $status_angsuran, $status_nota);
        $kd_invoice    = $nomor_invoice;
        $data_invoice  = $this->db->get_where('nota', ['id' => $nomor_invoice])->row_array();
        $login_sistem  = $this->db->get_where('sistem_login', ['id' => $data_invoice['kasir']])->row_array();
        $tahun = date('Y', strtotime($data_invoice['tgl_pembayaran']));
        $bulan = date('m', strtotime($data_invoice['tgl_pembayaran']));
        if ($data_invoice['produk_id'] == 121) :
            $lokasi_file = 'assets/invoice/koperasi_mingguan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 122) :
            $lokasi_file = 'assets/invoice/koperasi_dua_mingguan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 123) :
            $lokasi_file = 'assets/invoice/koperasi_bulanan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 124) :
            $lokasi_file = 'assets/invoice/koperasi_musiman/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 125) :
            $lokasi_file = 'assets/invoice/anggota_bulanan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 126) :
            $lokasi_file = 'assets/invoice/karyawan_bulanan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        endif;
        $rinci_nota      = $this->select_model->getDataInvoiceRinciAngsuran($kd_invoice);
        $this->_simpananKoperasi($rinci_nota, $data_produk, $data_kontrak, $data_invoice);
        $cek_option      = $this->db->get_where('sistem_option')->row_array();
        $data_anggota    = $this->db->get_where('anggota', ['id' => $data_kontrak['anggota_id']])->row_array();
        $this->_cetakTandaTerimaKasir($nomor_invoice, $data_invoice);
        $this->_notifWABuktiSetorKasir($data_kontrak, $data_angsuran, $lokasi_file, $cek_option, $data_anggota, $login_sistem);
        $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Angsuran!</div>');
        redirect('kasir/koperasi/kontrak_pinjaman/' . $this->encrypt->encode($data_kontrak['id']));
    }
    // Macet
    public function bayar_macet($id)
    {
        $angsuran_id            = $this->encrypt->decode($id);
        $status_angsuran        = 'LUNAS_MACET';
        $status_nota            = 'TUNGGAKAN';
        $data_angsuran          = $this->db->get_where('p_koperasi_angsuran', ['id' => $angsuran_id])->row_array();
        $data_kontrak           = $this->db->get_where('p_koperasi_kontrak', ['id' => $data_angsuran['kontrak_id']])->row_array();
        $data_produk            = $this->db->get_where('p_koperasi_aturan', ['id' => $data_kontrak['aturan_id']])->row_array();
        $nomor_invoice          = $data_produk['produk_id'] . $data_kontrak['id'] . date('ymdhis');
        $this->insert_model->simpanBayarAngsuranMenu($nomor_invoice, $data_produk, $data_kontrak,  $data_angsuran, $status_angsuran, $status_nota);
        $kd_invoice    = $nomor_invoice;
        $data_invoice  = $this->db->get_where('nota', ['id' => $nomor_invoice])->row_array();
        $login_sistem  = $this->db->get_where('sistem_login', ['id' => $data_invoice['kasir']])->row_array();
        $tahun = date('Y', strtotime($data_invoice['tgl_pembayaran']));
        $bulan = date('m', strtotime($data_invoice['tgl_pembayaran']));
        if ($data_invoice['produk_id'] == 121) :
            $lokasi_file = 'assets/invoice/koperasi_mingguan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 122) :
            $lokasi_file = 'assets/invoice/koperasi_dua_mingguan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 123) :
            $lokasi_file = 'assets/invoice/koperasi_bulanan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 124) :
            $lokasi_file = 'assets/invoice/koperasi_musiman/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 125) :
            $lokasi_file = 'assets/invoice/anggota_bulanan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        elseif ($data_invoice['produk_id'] == 126) :
            $lokasi_file = 'assets/invoice/karyawan_bulanan/' . $tahun . '/' . $bulan . '/' . $data_invoice['invoice_pdf'] . '';
        endif;
        $rinci_nota      = $this->select_model->getDataInvoiceRinciAngsuran($kd_invoice);
        $this->_simpananKoperasi($rinci_nota, $data_produk, $data_kontrak, $data_invoice);
        $cek_option      = $this->db->get_where('sistem_option')->row_array();
        $data_anggota    = $this->db->get_where('anggota', ['id' => $data_kontrak['anggota_id']])->row_array();
        $this->_cetakTandaTerimaKasir($nomor_invoice, $data_invoice);
        $this->_notifWABuktiSetorKasir($data_kontrak, $data_angsuran, $lokasi_file, $cek_option, $data_anggota, $login_sistem);
        $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Angsuran!</div>');
        redirect('kasir/koperasi/kontrak_pinjaman/' . $this->encrypt->encode($data_kontrak['id']));
    }
    // Lunas
    public function bayar_lunas($id)
    {
        $kontrak_id     = $this->encrypt->decode($id);
        $data_kontrak   = $this->db->get_where('p_koperasi_kontrak', ['id' => $kontrak_id])->row_array();
        $data_produk    = $this->db->get_where('p_koperasi_aturan', ['id' => $data_kontrak['aturan_id']])->row_array();
        //    Cek Apakah kontrak masih aktif atau tidak
        $data_aturan    = $this->db->get_where('p_koperasi_aturan', ['id' => $data_kontrak['aturan_id']])->row_array();
        $data_anggota   = $this->db->get_where('anggota', ['id' => $data_kontrak['anggota_id']])->row_array();
        // Cek Apakah dia Angsuran Bulanan atau tidak
        if ($data_kontrak['aturan_id'] == 3) :
            $diskon_data = array(
                'bsr_bayar' => $data_kontrak['bsr_pokok'],
                'bsr_jasa'  => 0,
                'bsr_simpanan'  => 0
            );
            $this->db->where('kontrak_id', $kontrak_id);
            $this->db->where('status_angsuran', 'BELUM');
            $this->db->update('p_koperasi_angsuran', $diskon_data);
        elseif ($data_kontrak['aturan_id'] == 5):
            $diskon_data = array(
                'bsr_bayar' => $data_kontrak['bsr_pokok'],
                'bsr_jasa'  => 0,
                'bsr_simpanan'  => 0
            );
            $this->db->where('kontrak_id', $kontrak_id);
            $this->db->where('status_angsuran', 'BELUM');
            $this->db->update('p_koperasi_angsuran', $diskon_data);
        elseif ($data_kontrak['aturan_id'] == 6):
            $diskon_data = array(
                'bsr_bayar' => $data_kontrak['bsr_pokok'],
                'bsr_jasa'  => 0,
                'bsr_simpanan'  => 0
            );
            $this->db->where('kontrak_id', $kontrak_id);
            $this->db->where('status_angsuran', 'BELUM');
            $this->db->update('p_koperasi_angsuran', $diskon_data);
        elseif ($data_kontrak['aturan_id'] == 4):
            // Cari selisih bulan
            $data_angsuran = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => $kontrak_id])->row_array();
            $tgl_ini      = date_create();
            $tempo        = date_create($data_angsuran['tgl_tempo']);
            $selisih      = date_diff($tgl_ini, $tempo);
            $tempo        = $data_kontrak['tempo_pinjaman'];
            // Hitungan Bulanan
            $bsr_jasa     = $data_kontrak['bsr_jasa'] / $tempo;
            $bsr_simpanan = $data_kontrak['bsr_simpanan'] / $tempo;
            $bsr_admin    = $bsr_jasa + $bsr_simpanan;
            $total_jasa   = (($tempo - $selisih->m) * $bsr_admin);
            $bsr_bayar    = $total_jasa + $data_kontrak['bsr_pokok'];
            $diskon_data = array(
                'bsr_bayar'     => $bsr_bayar,
                'bsr_jasa'      => $bsr_jasa * $selisih->m,
                'bsr_simpanan'  => $bsr_simpanan * $selisih->m
            );
            $this->db->where('kontrak_id', $kontrak_id);
            $this->db->where('status_angsuran', 'BELUM');
            $this->db->update('p_koperasi_angsuran', $diskon_data);
        endif;
        // Data Angsuran & Tunggakan
        $data_angsuran   = $this->select_model->vd_DataAngsuranKontrak($kontrak_id);
        $total_angsuran  = $this->select_model->vd_TotalDataAngsuranKontrak($kontrak_id);
        $data_tunggakan  = $this->select_model->vd_DataTunggakanKontrak($kontrak_id);
        $total_tunggakan = $this->select_model->vd_TotalDataTunggakanKontrak($kontrak_id);
        // Nomor Invoice atau Nota ID
        $nomor_invoice  = $data_aturan['produk_id'] . $data_kontrak['id'] . date('ymdhis');
        // Menambahkan Nota
        $this->insert_model->simpanBayarLunasAngsuranBeranda($total_angsuran, $total_tunggakan, $nomor_invoice, $kontrak_id, $data_kontrak, $data_aturan, $data_anggota, $data_angsuran, $data_tunggakan);
        $kd_nota        = $nomor_invoice;
        $data_invoice   = $this->db->get_where('nota', ['id' => $kd_nota])->row_array();
        // Membuat Nota PDF
        $this->_cetakTandaTerimaKasirLunas($kd_nota, $data_invoice);
        $kd_invoice   = $kd_nota;
        $rinci_nota   = $this->select_model->getDataInvoiceRinciAngsuran($kd_invoice);
        $this->_simpananKoperasi($rinci_nota, $data_produk, $data_kontrak, $data_invoice);
        $cek_option = $this->db->get_where('sistem_option')->row_array();
        $this->_notifLunas($data_kontrak, $cek_option, $data_anggota);
        $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Bayar Lunas Angsuran!</div>');
        redirect('kasir/koperasi/kontrak_pinjaman/' . $this->encrypt->encode($kontrak_id));
    }
    // Kumpulan Private Function
    // Menambahkan tabungan atau simpanan
    private function _simpananKoperasi($rinci_nota, $data_produk, $data_kontrak, $data_invoice)
    {
        $no = 1;
        foreach ($rinci_nota as $Rinci_nota) :
            $data_nota_omset = array(
                'id'              => date("YmdHis") . 10 . $data_produk['produk_id'] . $no,
                'produk_id'       => 10,
                'tgl_pembayaran'  => date('Y-m-d'),
                'qty_pembayaran'  => 1,
                'ttl_bayar'       => $Rinci_nota->bsr_jasa,
                'status_kas'      => 'OMSET',
                'status_nota'     => 'TERIMA',
                'petugas'         => $data_kontrak['login_id'],
                'kasir'           => $this->session->userdata('isloggedin'),
                'ket_nota'        => 'Jasa Pinjaman ' . $Rinci_nota->nm_anggota . ' ' . $Rinci_nota->kontrak_id . ' angsuran Ke ' . $Rinci_nota->angs_ke,
                'invoice_pdf'     => $data_invoice['invoice_pdf']
            );
            $this->db->insert('nota', $data_nota_omset);
            $no++;
        endforeach;
    }
    // Membuat Dokumen Bukti Setor Angsuran
    private function _cetakTandaTerimaKasir($nomor_invoice, $data_invoice)
    {
        $kd_invoice            = $nomor_invoice;
        $data_invoice          = $this->select_model->getDataInvoice($kd_invoice); // DONE
        $data_rinci            = $this->select_model->getDataInvoiceRinciAngsuran($kd_invoice);
        // Detail data nota
        $data_marketing = $this->db->get_where('sistem_login', ['id' => $data_invoice['petugas']])->row_array();
        $data_kasir = $this->db->get_where('sistem_login', ['id' => $data_invoice['kasir']])->row_array();
        // End data nota
        $data = array(
            // Data isi konten
            'data_invoice'        => $data_invoice,
            'data_rinci'          => $data_rinci,
            'data_marketing'      => $data_marketing,
            'data_kasir'          => $data_kasir
        );
        // filename dari pdf ketika didownload
        $file_pdf = $data_invoice['invoice_pdf'];
        // 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_pinjaman_koperasi_kasir', $data, true);
        $tahun = date('Y', strtotime($data_invoice['tgl_pembayaran']));
        $bulan = date('m', strtotime($data_invoice['tgl_pembayaran']));
        // run dompdf
        if ($data_invoice['produk_id'] == 121) :
            $lokasi_file = 'assets/invoice/koperasi_mingguan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 122) :
            $lokasi_file = 'assets/invoice/koperasi_dua_mingguan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 123) :
            $lokasi_file = 'assets/invoice/koperasi_bulanan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 124) :
            $lokasi_file = 'assets/invoice/koperasi_musiman/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 125) :
            $lokasi_file = 'assets/invoice/anggota_bulanan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 126) :
            $lokasi_file = 'assets/invoice/karyawan_bulanan/' . $tahun . '/' . $bulan . '/';
        endif;
        $this->pdfgenerator->generate_kasir($html, $file_pdf, $paper, $lokasi_file, $orientation);
    }
    // Lunas
    private function _cetakTandaTerimaKasirLunas($kd_nota, $data_invoice)
    {
        $kd_invoice            = $kd_nota;
        $data_invoice          = $this->select_model->getDataInvoice($kd_invoice); // DONE
        $data_rinci            = $this->select_model->getDataInvoiceRinciAngsuran($kd_invoice);
        // Detail data nota
        $data_marketing = $this->db->get_where('sistem_login', ['id' => $data_invoice['petugas']])->row_array();
        $data_kasir = $this->db->get_where('sistem_login', ['id' => $data_invoice['kasir']])->row_array();
        // End data nota
        $data = array(
            // Data isi konten
            'data_invoice'        => $data_invoice,
            'data_rinci'          => $data_rinci,
            'data_marketing'      => $data_marketing,
            'data_kasir'          => $data_kasir
        );
        // filename dari pdf ketika didownload
        $file_pdf = $data_invoice['invoice_pdf'];
        // 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_pinjaman_koperasi_kasir_lunas', $data, true);
        $tahun = date('Y', strtotime($data_invoice['tgl_pembayaran']));
        $bulan = date('m', strtotime($data_invoice['tgl_pembayaran']));
        // run dompdf
        if ($data_invoice['produk_id'] == 121) :
            $lokasi_file = 'assets/invoice/koperasi_mingguan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 122) :
            $lokasi_file = 'assets/invoice/koperasi_dua_mingguan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 123) :
            $lokasi_file = 'assets/invoice/koperasi_bulanan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 124) :
            $lokasi_file = 'assets/invoice/koperasi_musiman/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 125) :
            $lokasi_file = 'assets/invoice/anggota_bulanan/' . $tahun . '/' . $bulan . '/';
        elseif ($data_invoice['produk_id'] == 126) :
            $lokasi_file = 'assets/invoice/karyawan_bulanan/' . $tahun . '/' . $bulan . '/';
        endif;
        $this->pdfgenerator->generate_kasir($html, $file_pdf, $paper, $lokasi_file, $orientation);
    }
    // Wa Notif Bukti Setor Kasir
    private function _notifWABuktiSetorKasir($data_kontrak, $data_angsuran, $lokasi_file, $cek_option, $data_anggota, $login_sistem)
    {
        // WA BARU
        // setting WA
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 2])->row_array();
        $cek_nomor = substr($data_anggota['no_hp'], 0, 1);
        if ($cek_nomor == 0) :
            $no_hp = '62' . substr($data_anggota['no_hp'], 1, 20);
        else :
            $no_hp = $data_anggota['no_hp'];
        endif;
        $link_file = base_url($lokasi_file);
        // Contoh URL http://localhost/binaartha/assets/slip_gaji/2025016.pdf
        $caption = 'Yth. *_pelanggan setia Koperasi Bina Artha Prima_*

*Terimakasih!*
Pembayaran Tagihan ke- ' . $data_angsuran['angs_ke'] . '/' . $data_kontrak['tempo_pinjaman'] . ' KSP Bina Artha Prima sebesar Rp ' . $data_angsuran['bsr_bayar'] . ' untuk nomor kontrak *' . $data_kontrak['id'] . '* Melalui Kasir ' . $login_sistem['nm_panggilan'] . ' sudah kami terima pada ' . date('d/m/Y') . ' Jam ' . date('H:i:s') . '.

Sempatkan waktu anda untuk memberikan kritik dan saran kepada kami :

https://kspbinaartha.com/marketing/review_konsumen/' . $this->encrypt->encode($data_kontrak['id']) . '

*Info Hub :* ' . $cek_option['cs_koperasi'] . '
*_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'        => $link_file
        );
        // Logs WhatApps Sender
        $data_invoc = $this->db->get_where('p_koperasi_kontrak', ['id' => $data_kontrak['id']])->row_array();
        $data_logs = array(
            'login_id'     => $data_invoc['login_id'],
            'tgl_kirim'    => date('Y-m-d'),
            'no_hp'        => $no_hp,
            'isi_pesan'    => $caption,
            'ket_pesan'    => 'Pembayaran Pinjaman ' . $data_kontrak['id'],
            'jml_kirim'    => '1',
            'link_pdf'     => $link_file,
            'link_reviews' => 'https://kspbinaartha.com/marketing/review_konsumen/' . $this->encrypt->encode($data_kontrak['id'])
        );
        $this->db->insert('logs_wa', $data_logs);
        // End Logs WhatApps Sender
        $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);
    }
    // Notifikasi WA Bukti Setor Nomor Saya
    private function _notifLunas($data_kontrak, $cek_option, $data_anggota)
    {
        $cek_nomor = substr($data_anggota['no_hp'], 0, 1);
        if ($cek_nomor == 0) :
            $no_hp = '62' . substr($data_anggota['no_hp'], 1, 20);
        else :
            $no_hp = $data_anggota['no_hp'];
        endif;
        $data_invoice = $this->db->get_where('anggota', ['id' => $data_kontrak['anggota_id']])->row_array();
        $pesan = 'Yth. ' . $data_invoice['nm_anggota'] . '

Terimakasih!
Pinjaman anda dengan nomor kontrak *' . $data_kontrak['id'] . '* di KSP Bina Artha Prima dinyatakan *LUNAS* pada ' . date('d-m-Y') . ' Jam ' . date('H:i:s') . '.
Silahkan jika anda ingin mengajukan kembali pinjaman pada kami, bisa menghubungi marketing kami.

Sempatkan waktu anda untuk memberikan kritik dan saran kepada kami :

https://kspbinaartha.com/marketing/review_konsumen/' . $this->encrypt->encode($data_kontrak['id']) . '

*Info Hub :* ' . $cek_option['cs_koperasi'] . '
*_PESAN OTOMATIS ROBIN_*';

        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 1])->row_array();
        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'message'    => $pesan,
            'footer'     => $footer
        );
        // Logs WhatApps Sender
        $data_invoc = $this->db->get_where('p_koperasi_kontrak', ['id' => $data_kontrak['id']])->row_array();
        $data_logs = array(
            'login_id'     => $data_invoc['login_id'],
            'tgl_kirim'    => date('Y-m-d'),
            'no_hp'        => $no_hp,
            'isi_pesan'    => $pesan,
            'ket_pesan'    => 'Pelunasan Pinjaman ' . $data_kontrak['id'],
            'jml_kirim'    => '1',
            'link_pdf'     => '0',
            'link_reviews' => 'https://kspbinaartha.com/marketing/review_konsumen/' . $this->encrypt->encode($data_kontrak['id'])
        );
        $this->db->insert('logs_wa', $data_logs);
        // End Logs WhatApps Sender
        $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 ControllerPinjaman.php and path \application\controllers\kasir\koperasi\ControllerPinjaman.php */