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

class ControllerKPIMarketing extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        check_not_login();
        if ($this->session->userdata('level') != 7) {
            redirect();
        }
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('mycustom');
        $this->load->model('marketing/insert_model');
        $this->load->model('marketing/select_model');
        $this->load->model('marketing/update_model');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }

    public function index()
    {
        // NOtifikasi
        $data_login                = $this->userlogin->user_login('isloggedin');
        $data_id_login             = $this->session->userdata('isloggedin');
        // End Notifikasi
        // Data Detail KPI
        $marketing_id              = $data_id_login;
        $data_kpi                  = $this->select_model->vd_daftarHistoryKPI($marketing_id);
        // Data Reviews
        $data_review               = $this->select_model->vd_Review($marketing_id);
        // echo '<pre>';
        // var_dump($data_kpi);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'            => 'kpi',
            'page'              => 'index',
            'judul'             => 'Data Detail KPI Marketing || Bagian Marketing',
            'data_id_login'     => $data_id_login,
            'data_login'        => $data_login,
            'kpi'               => $data_kpi,
            'reviews'           => $data_review
        );
        $this->load->view('marketing/index', $data);
    }
    // Cek Identitas Anggota Koperasi
    public function detail_kpi($id)
    {
        $hasil_id = $this->encrypt->decode($id);
        // NOtifikasi
        $data_login                = $this->userlogin->user_login('isloggedin');
        $data_id_login             = $this->session->userdata('isloggedin');
        // End Notifikasi
        // Data Detail KPI
        $kpi_detail            = $this->select_model->vd_detKPIMarketing($hasil_id);
        $kpi                   = $this->db->get_where('k_hasil', ['id' => $hasil_id])->row_array();
        $login_marketing       = $this->db->get_where('sistem_login', ['id' => $kpi['login_id']])->row_array();
        $identitas_marketing   = $this->db->get_where('sistem_identitas', ['login_id' => $kpi['login_id']])->row_array();
        // Review Konsumen
        $bulan = date('m', strtotime($kpi['bln_kpi']));
        $tahun = date('Y', strtotime($kpi['bln_kpi']));
        $data_review           = $this->db->get_where('k_rkonsumen', ['login_id' => $kpi['login_id'], 'MONTH(tgl_review)' => $bulan, 'YEAR(tgl_review)' => $tahun])->result();
        // echo '<pre>';
        // var_dump($pinjaman_aktif);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'            => 'kpi/detail',
            'page'              => 'index',
            'judul'             => 'Data Detail KPI Marketing || Bagian Marketing',
            'data_id_login'     => $data_id_login,
            'data_login'        => $data_login,
            'data_kpi'            => $kpi,
            'marketing'           => $login_marketing,
            'identitas_marketing' => $identitas_marketing,
            'detail_kpi'          => $kpi_detail,
            'reviews'             => $data_review
        );
        $this->load->view('marketing/index', $data);
    }
    public function update_kpi($id)
    {
        $hasil_id     = $this->encrypt->decode($id);
        $hasil        = $this->db->get_where('k_hasil', ['id' => $hasil_id])->row_array();
        $bulan        = date('m', strtotime($hasil['bln_kpi']));
        $tahun        = date('Y', strtotime($hasil['bln_kpi']));
        // $bulan        = '09';
        // $tahun        = 2024;
        $marketing_id = $hasil['login_id'];
        // Realisasi Bulan ini Individu
        $ttl_realisasi = $this->select_model->vd_totalJmlRealisasi($marketing_id, $bulan, $tahun);
        if ($ttl_realisasi['jml_realisasi'] == null) :
            $jml_realisasi = 0;
        else:
            $jml_realisasi = $ttl_realisasi['jml_realisasi'];
        endif;
        // Realisasi Bulan ini Devisi
        $ttl_realisasi_devisi1 = $this->select_model->vd_totalJmlRealisasiDevisi1($bulan, $tahun);
        $ttl_realisasi_devisi2 = $this->select_model->vd_totalJmlRealisasiDevisi2($bulan, $tahun);
        $ttl_realisasi_devisinew = $ttl_realisasi_devisi1['jml_realisasi'] + $ttl_realisasi_devisi2['jml_realisasi'];
        if ($ttl_realisasi_devisinew == null) :
            $realisasi_devisi = 0;
        else:
            $realisasi_devisi = $ttl_realisasi_devisinew;
        endif;
        // Storting Bulan ini
        $ttl_storting = $this->select_model->vd_totalJmlStorting($marketing_id, $bulan, $tahun);
        if ($ttl_storting['jml_storting'] == null) :
            $jml_storting = 0;
        else:
            $jml_storting = $ttl_storting['jml_storting'];
        endif;
        // Storting Bulan ini Devisi
        $ttl_storting_devisi1 = $this->select_model->vd_totalJmlStortingDevisi1($bulan, $tahun);
        $ttl_storting_devisi2 = $this->select_model->vd_totalJmlStortingDevisi2($bulan, $tahun);
        $ttl_storting_devisi = $ttl_storting_devisi1['jml_storting'] + $ttl_storting_devisi2['jml_storting'];
        if ($ttl_storting_devisi == null) :
            $storting_devisi = 0;
        else:
            $storting_devisi = $ttl_storting_devisi;
        endif;
        // Target Individu bulanan
        // Data Individu Realisasi
        $di_realisasi = $this->db->get_where('k_target', ['login_id' => $marketing_id, 'sts_target' => 'AKTIF', 'faktor_id' => 1])->row_array();
        $data_faktor1 = $this->db->get_where('k_faktor', ['id' => 1])->row_array();
        $nir          = ((($jml_realisasi / $di_realisasi['tgt_kinerja']) * $data_faktor1['pers_faktor']) / 100) * 100;
        // Data Individu Storting
        $di_storting  = $this->db->get_where('k_target', ['login_id' => $marketing_id, 'sts_target' => 'AKTIF', 'faktor_id' => 2])->row_array();
        $data_faktor2 = $this->db->get_where('k_faktor', ['id' => 2])->row_array();
        $nis          = ((($jml_storting / $di_storting['tgt_kinerja']) * $data_faktor2['pers_faktor']) / 100) * 100;
        // Target Devisi bulanan
        // Data Devisi Realisasi
        $dd_realisasi = $this->db->get_where('k_target', ['login_id' => $marketing_id, 'sts_target' => 'AKTIF', 'faktor_id' => 3])->row_array();
        $data_faktor3 = $this->db->get_where('k_faktor', ['id' => 3])->row_array();
        $ndr          = ((($realisasi_devisi / $dd_realisasi['tgt_kinerja']) * $data_faktor3['pers_faktor']) / 100) * 100;
        // Data Devisi Storting
        $dd_storting  = $this->db->get_where('k_target', ['login_id' => $marketing_id, 'sts_target' => 'AKTIF', 'faktor_id' => 5])->row_array();
        $data_faktor4 = $this->db->get_where('k_faktor', ['id' => 5])->row_array();
        $nds          = ((($storting_devisi / $dd_storting['tgt_kinerja']) * $data_faktor4['pers_faktor']) / 100) * 100;
        // Target Kedisiplinan
        // Data Disiplin
        $d_disiplin   = $this->db->get_where('k_target', ['login_id' => $marketing_id, 'faktor_id' => 7, 'sts_target' => 'AKTIF'])->row_array();
        $data_faktor5 = $this->db->get_where('k_faktor', ['id' => 7])->row_array();
        $nilai_disiplin = $this->db->get_where('k_disiplin', ['login_id' => $marketing_id, 'MONTH(tgl_laporan)' => $bulan, 'YEAR(tgl_laporan)' => $tahun])->row_array();
        if ($nilai_disiplin == null) :
            $ttl_menit = '10800';
        else:
            $ttl_menit = $nilai_disiplin['absensi_menit'];
        endif;
        $nilai_disiplin = ((($ttl_menit / $d_disiplin['tgt_kinerja']) * $data_faktor5['pers_faktor']) / 100) * 100;
        // Data Review
        $d_reviews     = $this->db->get_where('k_target', ['login_id' => $marketing_id, 'faktor_id' => 8, 'sts_target' => 'AKTIF'])->row_array();
        $data_faktor6  = $this->db->get_where('k_faktor', ['id' => 8])->row_array();
        $jml_reviews   = count($this->db->get_where('k_rkonsumen', ['login_id' => $marketing_id, 'MONTH(tgl_review)' => $bulan, 'YEAR(tgl_review)' => $tahun])->result());
        $ttl_bintang   = $this->select_model->vd_bintangReviewsPelanggan($marketing_id, $bulan, $tahun);
        if ($jml_reviews == null) :
            $nilai_reviews = ((((4) / $d_reviews['tgt_kinerja']) * $data_faktor6['pers_faktor']) / 100) * 100;
        else:
            $nilai_reviews = (((($ttl_bintang['rate_bintang'] / $jml_reviews) / $d_reviews['tgt_kinerja']) * $data_faktor6['pers_faktor']) / 100) * 100;
        endif;
        $nilai_angka = round((($nir + $nis) + ($ndr + $nds) + $nilai_disiplin + $nilai_reviews), 2);
        if ($nilai_angka > 95) :
            $nilai_huruf = 'S+';
        elseif ($nilai_angka >= 91):
            $nilai_huruf = 'S';
        elseif ($nilai_angka >= 86):
            $nilai_huruf = 'A+';
        elseif ($nilai_angka >= 81):
            $nilai_huruf = 'A';
        elseif ($nilai_angka >= 76):
            $nilai_huruf = 'B+';
        elseif ($nilai_angka >= 71):
            $nilai_huruf = 'B';
        elseif ($nilai_angka >= 61):
            $nilai_huruf = 'C';
        elseif ($nilai_angka >= 51):
            $nilai_huruf = 'D';
        elseif ($nilai_angka <= 50):
            $nilai_huruf = 'E';
        endif;
        $bulan_now = date('m');
        if ($bulan == $bulan_now) :
            $sts_hasil = 'PROSES';
        else:
            $sts_hasil = 'SELESAI';
        endif;
        $data_hasil = array(
            'nilai_angka' => $nilai_angka,
            'nilai_huruf' => $nilai_huruf,
            'sts_hasil'   => $sts_hasil,
            'updated_at'  => date('Y-m-d H:i:s')
        );
        $this->db->where('id', $hasil['id']);
        $this->db->update('k_hasil', $data_hasil);
        $this->db->where('hasil_id', $hasil['id']);
        $this->db->delete('k_dhasil');
        // Detail Nilai Realisasi Individu
        $data_dhasil_1 = array(
            'hasil_id'    => $hasil['id'],
            'faktor_id'   => 1,
            'target'      => $di_realisasi['tgt_kinerja'],
            'realisasi'   => $jml_realisasi,
            'persentase'  => $data_faktor1['pers_faktor'],
            'nilai'       => $nir
        );
        $this->db->insert('k_dhasil', $data_dhasil_1);
        // Detail Nilai Storting Individu
        $data_dhasil_2 = array(
            'hasil_id'    => $hasil['id'],
            'faktor_id'   => 2,
            'target'      => $di_storting['tgt_kinerja'],
            'realisasi'   => $jml_storting,
            'persentase'  => $data_faktor2['pers_faktor'],
            'nilai'       => $nis
        );
        $this->db->insert('k_dhasil', $data_dhasil_2);
        // Detail Nilai Realisasi Devisi
        $data_dhasil_3 = array(
            'hasil_id'    => $hasil['id'],
            'faktor_id'   => 3,
            'target'      => $dd_realisasi['tgt_kinerja'],
            'realisasi'   => $realisasi_devisi,
            'persentase'  => $data_faktor3['pers_faktor'],
            'nilai'       => $ndr
        );
        $this->db->insert('k_dhasil', $data_dhasil_3);
        // Detail Nilai Storting Devisi
        $data_dhasil_4 = array(
            'hasil_id'    => $hasil['id'],
            'faktor_id'   => 5,
            'target'      => $dd_storting['tgt_kinerja'],
            'realisasi'   => $storting_devisi,
            'persentase'  => $data_faktor4['pers_faktor'],
            'nilai'       => $nds
        );
        $this->db->insert('k_dhasil', $data_dhasil_4);
        // Detail Nilai Kedisiplinan
        $data_dhasil_5 = array(
            'hasil_id'    => $hasil['id'],
            'faktor_id'   => 7,
            'target'      => $d_disiplin['tgt_kinerja'],
            'realisasi'   => $ttl_menit,
            'persentase'  => $data_faktor5['pers_faktor'],
            'nilai'       => $nilai_disiplin
        );
        $this->db->insert('k_dhasil', $data_dhasil_5);
        // Detail Nilai Review Konsumen
        $data_dhasil_6 = array(
            'hasil_id'    => $hasil['id'],
            'faktor_id'   => 8,
            'target'      => $d_reviews['tgt_kinerja'],
            'realisasi'   => $nilai_reviews,
            'persentase'  => $data_faktor6['pers_faktor'],
            'nilai'       => $nilai_reviews
        );
        $this->db->insert('k_dhasil', $data_dhasil_6);
        $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Nilai KPI Berhasil di Update!</div>');
        redirect('marketing/kpi/detail_kpi/' . $this->encrypt->encode($hasil_id));
    }
}

/* End of file ControllerKPIMarketing.php and path \application\controllers\marketing\ControllerKPIMarketing.php */