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

class ControllerDataAngsuran extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        //validasi jika user belum login
        check_not_login();
        if ($this->session->userdata('level') != 1) {
            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_owner/insert_model');
        $this->load->model('bagian_owner/select_model');
        $this->load->model('bagian_owner/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $notif                 = $this->notifikasi->notif_owner();
        // Data Login
        $data_login            = $this->userlogin->user_login(); // Menampilkan siapa yang login
        $data_id_login         = $this->session->userdata('isloggedin'); // Menampilkan data session
        // Data Isian Konten
        // Query SQL Data atau isi data page
        // Menampilkan saldo pertama koperasi 
        $data_table          = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => 0])->result();
        $jml_data            = count($data_table);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/koperasi/angsuran',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Koperasi Angsuran || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table
        );
        $this->load->view('owner/index', $data);
    }
    // Crud
    public function crud()
    {
        // Cek Filter
        if (isset($_POST['cari_nota'])) :
            $this->_dataNomorNota();
        endif;
        // Tambah Angsuran
        if (isset($_POST['tambah_angsuran'])) :
            $data_angsuran = $this->_cekAngsuranData();
            $this->db->insert('p_koperasi_angsuran', $data_angsuran);
            $data_kontrak = array(
                'tempo_pinjaman' => $data_angsuran['angs_ke']
            );
            $this->db->where('id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_kontrak', $data_kontrak);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Tambah Data Angsuran!</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
        // Sinkronisasi Angsuran
        if (isset($_POST['sinkronAngsuran'])) :
            $kontrak_id = htmlspecialchars($this->input->post('kontrak_id'));
            $data_kontrak  = $this->db->get_where('p_koperasi_kontrak', ['id' => $kontrak_id])->row_array();
            $data_progress = $this->db->get_where('p_koperasi_progres', ['kontrak_id' => $kontrak_id])->row_array();
            $aturan_id = $data_kontrak['aturan_id'];
            if ($aturan_id == 1) :
                $this->_sinkronDataMingguan($data_kontrak, $data_progress);
            elseif ($aturan_id == 2) :
                $this->_sinkronDataDuaMingguan($data_kontrak, $data_progress);
            elseif ($aturan_id == 3) :
                $this->_sinkronDataBulanan($data_kontrak, $data_progress);
            elseif ($aturan_id == 4) :
                $this->_sinkronDataMusiman($data_kontrak, $data_progress);
            elseif ($aturan_id == 5) :
                $this->_sinkronDataBulananAnggota($data_kontrak, $data_progress);
            elseif ($aturan_id == 6) :
                $this->_sinkronDataBulanKaryawan($data_kontrak, $data_progress);
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Sinkron Data Angsuran!</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
        // Ubah data angsuran
        if (isset($_POST['updateDatabaseTerbaru'])) :
            $kontrak_id = htmlspecialchars($this->input->post('kontrak_id'));
            $data_kontrak  = $this->db->get_where('p_koperasi_kontrak', ['id' => $kontrak_id])->row_array();
            $data_angsuran = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => $kontrak_id, 'id' => htmlspecialchars($this->input->post('angsuran_id'))])->row_array();
            $status_angsuran = htmlspecialchars($this->input->post('status_angsuran'));
            if ($status_angsuran == 'BELUM') :
                $data = array(
                    'tgl_bayar'       => NULL,
                    'status_angsuran' => 'BELUM',
                    'invoice_pdf'     => NULL,
                    'update_at'       => date('Y-m-d H:i:s')
                );
                $tahun = date('Y', strtotime(htmlspecialchars($this->input->post('tgl_bayar'))));
                $bulan = date('m', strtotime(htmlspecialchars($this->input->post('tgl_bayar'))));
                // Melihat Produk ID
                if ($data_kontrak['aturan_id'] == 1) :
                    $lokasi_file = 'assets/invoice/koperasi_mingguan/' . $tahun . '/' . $bulan . '/' . $data_angsuran['invoice_pdf'] . '';
                elseif ($data_kontrak['aturan_id'] == 2) :
                    $lokasi_file = 'assets/invoice/koperasi_dua_mingguan/' . $tahun . '/' . $bulan . '/' . $data_angsuran['invoice_pdf'] . '';
                elseif ($data_kontrak['aturan_id'] == 3) :
                    $lokasi_file = 'assets/invoice/koperasi_bulanan/' . $tahun . '/' . $bulan . '/' . $data_angsuran['invoice_pdf'] . '';
                elseif ($data_kontrak['aturan_id'] == 4) :
                    $lokasi_file = 'assets/invoice/koperasi_musiman/' . $tahun . '/' . $bulan . '/' . $data_angsuran['invoice_pdf'] . '';
                elseif ($data_kontrak['aturan_id'] == 5) :
                    $lokasi_file = 'assets/invoice/anggota_bulanan/' . $tahun . '/' . $bulan . '/' . $data_angsuran['invoice_pdf'] . '';
                elseif ($data_kontrak['aturan_id'] == 6) :
                    $lokasi_file = 'assets/invoice/karyawan_bulanan/' . $tahun . '/' . $bulan . '/' . $data_angsuran['invoice_pdf'] . '';
                endif;
                unlink($lokasi_file);
                $this->db->where('id', htmlspecialchars($this->input->post('angsuran_id')));
                $this->db->update('p_koperasi_angsuran', $data);
            else:
                $data = array(
                    'angs_ke'          => htmlspecialchars($this->input->post('angs_ke')),
                    'tgl_tempo'        => htmlspecialchars($this->input->post('tgl_tempo')),
                    'tgl_bayar'        => htmlspecialchars($this->input->post('tgl_bayar')),
                    'bsr_bayar'        => htmlspecialchars($this->input->post('bsr_bayar')),
                    'bsr_pokok'        => htmlspecialchars($this->input->post('bsr_pokok')),
                    'bsr_simpanan'     => htmlspecialchars($this->input->post('bsr_simpanan')),
                    'bsr_jasa'         => htmlspecialchars($this->input->post('bsr_jasa')),
                    'status_angsuran'  => htmlspecialchars($this->input->post('status_angsuran')),
                    'update_at'        => date('Y-m-d H:i:s')
                );
                $this->db->where('id', htmlspecialchars($this->input->post('angsuran_id')));
                $this->db->update('p_koperasi_angsuran', $data);
            endif;
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Angsuran!</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
        if (isset($_POST['deleteDatabaseTerbaru'])) :
            $this->_cekDetailKoperasi();
            $this->db->where('id', htmlspecialchars($this->input->post('angsuran_id')));
            $this->db->delete('p_koperasi_angsuran');
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Hapus Data Angsuran!</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
    }
    // Melihat data Nota berdasarkan Nomor Nota
    private function _dataNomorNota()
    {
        $kontrak_id            = htmlspecialchars($this->input->post('kontrak_id'));
        $notif                 = $this->notifikasi->notif_owner();
        // Data Login
        $data_login            = $this->userlogin->user_login(); // Menampilkan siapa yang login
        $data_id_login         = $this->session->userdata('isloggedin'); // Menampilkan data session
        // Data Isian Konten
        // Query SQL Data atau isi data page
        // Menampilkan saldo pertama koperasi 
        $data_table          = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => $kontrak_id])->result();
        $jml_data            = count($data_table);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/koperasi/angsuran',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Koperasi Angsuran || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table
        );
        $this->load->view('owner/index', $data);
    }
    // Cek Data Angsuran kontrak
    private function _cekAngsuranData()
    {
        $kontrak_id = htmlspecialchars($this->input->post('kontrak_id'));
        // Cek apakah ada nomor kontrak tersebut
        $cekKontrakPinjaman = $this->db->get_where('p_koperasi_kontrak', ['id' => $kontrak_id])->row_array();
        if ($cekKontrakPinjaman == NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Tidak Ada Data!</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
        // Cek Angsuran
        $cekAngsuranKontrak = $this->select_model->vdAngsuranKontrak($kontrak_id);
        if ($cekAngsuranKontrak == NULL) :
            $angs_ke = 0;
        else:
            $angs_ke = $cekAngsuranKontrak['angs_ke'];
        endif;
        if ($cekAngsuranKontrak['tgl_tempo'] > htmlspecialchars($this->input->post('tgl_tempo'))) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Tanggal Tempo Terlalu Kecil!</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
        $data_angsuran_baru = array(
            'kontrak_id'      => $kontrak_id,
            'angs_ke'         => $angs_ke + 1,
            'tgl_tempo'       => htmlspecialchars($this->input->post('tgl_tempo')),
            'bsr_bayar'       => $cekKontrakPinjaman['bsr_angsuran'],
            'bsr_pokok'       => $cekKontrakPinjaman['bsr_pokok'],
            'bsr_jasa'        => $cekKontrakPinjaman['bsr_jasa'],
            'bsr_simpanan'    => $cekKontrakPinjaman['bsr_simpanan'],
            'status_angsuran' => 'BELUM'
        );
        return $data_angsuran_baru;
    }
    // Sinkron data angsuran minggugan
    private function _sinkronDataMingguan($data_kontrak, $data_progress)
    {
        $a = 1;
        while ($a <= $data_kontrak['tempo_pinjaman']) :
            $data = array(
                'tgl_tempo'  => date('Y-m-d', strtotime('+' . $a . ' week', strtotime($data_progress['tgl_realisasi']))),
                'bsr_bayar'  => $data_kontrak['bsr_angsuran'],
                'bsr_pokok'  => $data_kontrak['bsr_pokok'],
                'bsr_jasa'   => $data_kontrak['bsr_jasa'],
                'bsr_simpanan' => $data_kontrak['bsr_simpanan']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->where('angs_ke', $a);
            $this->db->update('p_koperasi_angsuran', $data);
            $a++;
        endwhile;
    }
    // Sinkron Dua Mingguan
    private function _sinkronDataDuaMingguan($data_kontrak, $data_progress)
    {
        $a = 1;
        while ($a <= $data_kontrak['tempo_pinjaman']) :
            $data = array(
                'tgl_tempo'  => date('Y-m-d', strtotime('+' . ($a * 2) . ' week', strtotime($data_progress['tgl_realisasi']))),
                'bsr_bayar'  => $data_kontrak['bsr_angsuran'],
                'bsr_pokok'  => $data_kontrak['bsr_pokok'],
                'bsr_jasa'   => $data_kontrak['bsr_jasa'],
                'bsr_simpanan' => $data_kontrak['bsr_simpanan']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->where('angs_ke', $a);
            $this->db->update('p_koperasi_angsuran', $data);
            $a++;
        endwhile;
    }
    // Sinkron data angsuran Bulanan
    private function _sinkronDataBulanan($data_kontrak, $data_progress)
    {
        $a = 1;
        while ($a <= $data_kontrak['tempo_pinjaman']) :
            $data = array(
                'tgl_tempo'  => date('Y-m-d', strtotime('+' . $a . ' month', strtotime($data_progress['tgl_realisasi']))),
                'bsr_bayar'  => $data_kontrak['bsr_angsuran'],
                'bsr_pokok'  => $data_kontrak['bsr_pokok'],
                'bsr_jasa'   => $data_kontrak['bsr_jasa'],
                'bsr_simpanan' => $data_kontrak['bsr_simpanan']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->where('angs_ke', $a);
            $this->db->update('p_koperasi_angsuran', $data);
            $a++;
        endwhile;
    }
    // Sinkron data angsuran Bulanan
    private function _sinkronDataMusiman($data_kontrak, $data_progress)
    {
        $a = 1;
        while ($a <= $data_kontrak['tempo_pinjaman']) :
            $data = array(
                'tgl_tempo'  => date('Y-m-d', strtotime('+' . $data_kontrak['tempo_pinjaman'] . ' month', strtotime($data_progress['tgl_realisasi']))),
                'bsr_bayar'  => $data_kontrak['bsr_angsuran'],
                'bsr_pokok'  => $data_kontrak['bsr_pokok'],
                'bsr_jasa'   => $data_kontrak['bsr_jasa'],
                'bsr_simpanan' => $data_kontrak['bsr_simpanan']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->where('angs_ke', $a);
            $this->db->update('p_koperasi_angsuran', $data);
            $a++;
        endwhile;
    }
    // Sinkron data angsuran Bulanan
    private function _sinkronDataBulananAnggota($data_kontrak, $data_progress)
    {
        $a = 1;
        while ($a <= $data_kontrak['tempo_pinjaman']) :
            $data = array(
                'tgl_tempo'  => date('Y-m-d', strtotime('+' . $a . ' month', strtotime($data_progress['tgl_realisasi']))),
                'bsr_bayar'  => $data_kontrak['bsr_angsuran'],
                'bsr_pokok'  => $data_kontrak['bsr_pokok'],
                'bsr_jasa'   => $data_kontrak['bsr_jasa'],
                'bsr_simpanan' => $data_kontrak['bsr_simpanan']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->where('angs_ke', $a);
            $this->db->update('p_koperasi_angsuran', $data);
            $a++;
        endwhile;
    }
    // Sinkron data angsuran Bulanan
    private function _sinkronDataBulanKaryawan($data_kontrak, $data_progress)
    {
        $a = 1;
        while ($a <= $data_kontrak['tempo_pinjaman']) :
            $data = array(
                'tgl_tempo'  => date('Y-m-d', strtotime('+' . $a . ' month', strtotime($data_progress['tgl_realisasi']))),
                'bsr_bayar'  => $data_kontrak['bsr_angsuran'],
                'bsr_pokok'  => $data_kontrak['bsr_pokok'],
                'bsr_jasa'   => $data_kontrak['bsr_jasa'],
                'bsr_simpanan' => $data_kontrak['bsr_simpanan']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->where('angs_ke', $a);
            $this->db->update('p_koperasi_angsuran', $data);
            $a++;
        endwhile;
    }
    // Cek Angsuran Nota
    private function _cekDetailKoperasi()
    {
        $angsuran_id = htmlspecialchars($this->input->post('angsuran_id'));
        $cek_detail = $this->db->get_where('p_koperasi_nota', ['angsuran_id' => $angsuran_id])->row_array();
        if ($cek_detail != NULL) :
            # code...
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Masih ada terikat nota ' . $cek_detail['nota_id'] . '</div>');
            redirect('owner/database/koperasi/angsuran');
        endif;
    }
}

/* End of file ControllerDataAngsuran.php and path \application\controllers\owner\database\koperasi\ControllerDataAngsuran.php */