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

class ControllerSettLibur extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        check_not_login();
        if ($this->session->userdata('level') != 2) {
            redirect();
        }
        $this->set_timezone();
        $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_manager/insert_model');
        $this->load->model('bagian_manager/select_model');
        $this->load->model('bagian_manager/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $notif                 = $this->notifikasi->notif_manager();
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $tampil_tahun          = $this->select_model->getDataTahunTransaksi();
        // Data Isian Konten
        // Tampilkan Jenis Pinjaman 
        $pinjaman_aktif        = $this->select_model->vd_PinjamanAktif();
        $data_jaminan          = $this->select_model->vd_jaminanPinjaman();
        // Pinjaman Dipecah Kembali
        $pinjaman_mingguan     = $this->select_model->vd_tagihanPinjamanMingguan();
        $pinjaman_2mingguan    = $this->select_model->vd_tagihanPinjaman2Mingguan();
        $pinjaman_bulanan      = $this->select_model->vd_tagihanPinjamanBulanan();
        $pinjaman_musiman      = $this->select_model->vd_tagihanPinjamanMusiman();
        // Jumlah Pinjaman
        // echo '<pre>';
        // var_dump($pinjaman_2mingguan);
        // die;
        $jml_mingguan          = count($pinjaman_mingguan);
        $jml_2mingguan         = count($pinjaman_2mingguan);
        $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($pinjaman_musiman);
        // echo '<pre>';
        // var_dump($jml_mingguan, $jml_2mingguan);
        // die;
        $tgl                   = date('Y-m-d');
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'koperasi/sett_libur',
            'page'                => 'index',
            'judul'               => 'Daftar Tagihan || Bagian Manager',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            'tampil_tahun'        => $tampil_tahun,
            'tanggal'             => $tgl,
            // Isi dalam konten
            'pinjaman_aktif'      => $pinjaman_aktif,
            'data_jaminan'        => $data_jaminan,
            // Data Pecahan Pinjaman
            'aktif_mingguan'      => $pinjaman_mingguan,
            'aktif_2mingguan'     => $pinjaman_2mingguan,
            'aktif_bulanan'       => $pinjaman_bulanan,
            'aktif_musiman'       => $pinjaman_musiman,
            // 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('manager/index', $data);
    }
    //Merubah data angsuran
    public function crud()
    {
        if (isset($_POST['simpanDataAngsuran'])) :
            $jk         = htmlspecialchars($this->input->post('jenis_kegiatan'));
            if ($jk == 1) :
                // Mengubah Tanggal Tempo
                $data = array(
                    'tgl_tempo'        => htmlspecialchars($this->input->post('tgl_tempo')),
                    'tgl_bayar'        => null,
                    'status_angsuran'  => 'BELUM'
                );
                $this->db->where('id', htmlspecialchars($this->input->post('angs_id')));
                $this->db->update('p_koperasi_angsuran', $data);
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran Pinjaman!</div>');
            elseif ($jk == 2) :
                // Status Lunas Lancar
                $data = array(
                    'tgl_bayar'        => htmlspecialchars($this->input->post('tgl_tempo')),
                    'status_angsuran'  => 'LUNAS_LANCAR'
                );
                $this->db->where('id', htmlspecialchars($this->input->post('angs_id')));
                $this->db->update('p_koperasi_angsuran', $data);
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran Pinjaman!</div>');
            elseif ($jk == 3) :
                // Status Lunas Macet
                $data = array(
                    'tgl_bayar'        => htmlspecialchars($this->input->post('tgl_tempo')),
                    'status_angsuran'  => 'LUNAS_MACET'
                );
                $this->db->where('id', htmlspecialchars($this->input->post('angs_id')));
                $this->db->update('p_koperasi_angsuran', $data);
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran Pinjaman!</div>');
            elseif ($jk == 4) :
                // Status Macet
                $data = array(
                    'status_angsuran'  => 'MACET'
                );
                $this->db->where('id', htmlspecialchars($this->input->post('angs_id')));
                $this->db->update('p_koperasi_angsuran', $data);
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran Pinjaman!</div>');
            elseif ($jk == 5) :
                // Menghapus Angsuran
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Fitur ini tidak didukung pada menu ini!</div>');
            elseif ($jk == 6) :
                // Libur Angsuran
                $cek_angsuran = $this->db->get_where('p_koperasi_angsuran', ['id' => htmlspecialchars($this->input->post('angs_id'))])->row_array();
                $daftar_angsuran = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => $cek_angsuran['kontrak_id']])->result();
                $data_kontrak = $this->db->get_where('p_koperasi_kontrak', ['id' => $cek_angsuran['kontrak_id']])->row_array();
                if ($data_kontrak['aturan_id'] == 1) :
                    foreach ($daftar_angsuran as $Daftar_angsuran) :
                        if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
                            $data = array(
                                'tgl_tempo'     => date('Y-m-d', strtotime('+' . 1 . ' week', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                elseif ($data_kontrak['aturan_id'] == 2):
                    foreach ($daftar_angsuran as $Daftar_angsuran) :
                        if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
                            $data = array(
                                'tgl_tempo'     => date('Y-m-d', strtotime('+' . 2 . ' week', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                elseif ($data_kontrak['aturan_id'] == 3):
                    foreach ($daftar_angsuran as $Daftar_angsuran) :
                        if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
                            $data = array(
                                'tgl_tempo'     => date('Y-m-d', strtotime('+' . 2 . ' days', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                elseif ($data_kontrak['aturan_id'] == 4):
                    foreach ($daftar_angsuran as $Daftar_angsuran) :
                        if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
                            $data = array(
                                'tgl_tempo'     => date('Y-m-d', strtotime('+' . 2 . ' days', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                elseif ($data_kontrak['aturan_id'] == 5):
                    foreach ($daftar_angsuran as $Daftar_angsuran) :
                        if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
                            $data = array(
                                'tgl_tempo'     => date('Y-m-d', strtotime('+' . 2 . ' days', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                elseif ($data_kontrak['aturan_id'] == 6):
                    foreach ($daftar_angsuran as $Daftar_angsuran) :
                        if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
                            $data = array(
                                'tgl_tempo'     => date('Y-m-d', strtotime('+' . 2 . ' days', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                endif;
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran Pinjaman!</div>');
            endif;
            redirect('manager/koperasi/set_libur');
        endif;
        if (isset($_POST['resetTransaksiKasTanggal'])) :
            redirect('manager/koperasi/set_libur');
        endif;
        if (isset($_POST['filterTransaksiKasTanggal'])) :
            $this->_tanggalFilter();
        endif;
    }
    private function _tanggalFilter()
    {
        $tgl_filter            = htmlspecialchars($this->input->post('tahun_transaksi')) . '-' . htmlspecialchars($this->input->post('bulan_transaksi')) . '-' . htmlspecialchars($this->input->post('hari_transaksi'));
        if (htmlspecialchars($this->input->post('hari_transaksi')) == 'PILIH') :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Pilihan Tanggal Masih Kosong!</div>');
            redirect('manager/koperasi/set_libur');
        elseif (htmlspecialchars($this->input->post('bulan_transaksi')) == 'PILIH'):
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Pilihan Bulan Masih Kosong!</div>');
            redirect('manager/koperasi/set_libur');
        endif;
        $notif                 = $this->notifikasi->notif_manager();
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $tampil_tahun                 = $this->select_model->getDataTahunTransaksi();
        // Data Isian Konten
        // Tampilkan Jenis Pinjaman 
        $pinjaman_aktif        = $this->select_model->vd_PinjamanAktif();
        $data_jaminan          = $this->select_model->vd_jaminanPinjaman();
        // Pinjaman Dipecah Kembali
        $pinjaman_mingguan     = $this->select_model->vd_tagihanFilterPinjamanMingguan($tgl_filter);
        $pinjaman_2mingguan    = $this->select_model->vd_tagihanFilterPinjaman2Mingguan($tgl_filter);
        $pinjaman_bulanan      = $this->select_model->vd_tagihanFilterPinjamanBulanan($tgl_filter);
        $pinjaman_musiman      = $this->select_model->vd_tagihanFilterPinjamanMusiman($tgl_filter);
        // Jumlah Pinjaman
        // echo '<pre>';
        // var_dump($pinjaman_2mingguan);
        // die;
        $jml_mingguan          = count($pinjaman_mingguan);
        $jml_2mingguan         = count($pinjaman_2mingguan);
        $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($pinjaman_musiman);
        // echo '<pre>';
        // var_dump($jml_mingguan, $jml_2mingguan);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'koperasi/sett_libur',
            'page'                => 'index',
            'judul'               => 'Daftar Tagihan || Bagian Manager',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            'tampil_tahun'        => $tampil_tahun,
            'tanggal'             => $tgl_filter,
            // Isi dalam konten
            'pinjaman_aktif'      => $pinjaman_aktif,
            'data_jaminan'        => $data_jaminan,
            // Data Pecahan Pinjaman
            'aktif_mingguan'      => $pinjaman_mingguan,
            'aktif_2mingguan'     => $pinjaman_2mingguan,
            'aktif_bulanan'       => $pinjaman_bulanan,
            'aktif_musiman'       => $pinjaman_musiman,
            // 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('manager/index', $data);
    }
}

/* End of file ControllerSettLibur.php and path \application\controllers\manager\koperasi\ControllerSettLibur.php */
