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

class ControllerAktif 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');
        // 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_PinjamanAktifMingguan();
        $pinjaman_2mingguan    = $this->select_model->vd_PinjamanAktif2Mingguan();
        $pinjaman_bulanan      = $this->select_model->vd_PinjamanAktifBulanan();
        $pinjaman_musiman      = $this->select_model->vd_PinjamanAktifMusiman();
        // Jumlah Pinjaman
        $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/aktif',
            'page'                => 'index',
            'judul'               => 'Pinjaman Aktif || Bagian Manager',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // 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);
    }
    // Detail Kontrak pinjaman
    public function kontrak($id)
    {
        $notif                 = $this->notifikasi->notif_manager();
        // 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);
        // echo '<pre>';
        // var_dump($data_angsuran); 
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'koperasi/kontrak',
            'page'                => 'index',
            'judul'               => 'Detail Kontrak Pinjaman || Bagian Manager',
            '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('manager/index', $data);
    }
    //Merubah data angsuran
    public function crud()
    {
        if (isset($_POST['simpanDataAngsuran'])) {
            $kontrak_id = htmlspecialchars($this->input->post('kontrak_id'));
            $jk         = htmlspecialchars($this->input->post('jk'));
            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);
            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);
            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);
            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);
            elseif ($jk == 5) :
                // Menghapus Angsuran
                $this->db->where('id', htmlspecialchars($this->input->post('angs_id')));
                $this->db->delete('p_koperasi_angsuran');
            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('+' . 1 . ' month', 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('+' . $data_kontrak['tempo_pinjaman'] . ' month', 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('+' . 1 . ' month', 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('+' . 1 . ' month', strtotime($Daftar_angsuran->tgl_tempo)))
                            );
                            $this->db->where('id', $Daftar_angsuran->id);
                            $this->db->update('p_koperasi_angsuran', $data);
                        endif;
                    endforeach;
                endif;
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran Pinjaman!</div>');
            redirect('manager/marketing/pinjaman/kontrak/' . $this->encrypt->encode($kontrak_id));
        }
    }
}

/* End of file ControllerAktif.php and path \application\controllers\kabag_marketing\operasional\ControllerAktif.php */