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

class ControllerDataMarketing extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        check_not_login();
        if ($this->session->userdata('level') != 6) {
            redirect();
        }
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('mycustom');
        $this->load->library('Notifikasi');
        $this->load->library('pdfgenerator');
        $this->load->model('kabag_marketing/insert_model');
        $this->load->model('kabag_marketing/select_model');
        $this->load->model('kabag_marketing/update_model');
        $this->set_timezone();
    }

    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $notif                 = $this->notifikasi->notif_kabag_marketing();
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        // Data Isian Konten
        // View Data Marketing
        $data_marketing        = $this->select_model->vd_marketing(); // Menampilkasn Semua data marketing
        // echo '<pre>';
        // var_dump($data_marketing);
        // die;  
        $nilai_kpi            = $this->db->get_where('k_hasil', ['sts_hasil' => 'PROSES'])->result();
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'data/marketing',
            'page'                => 'index',
            'judul'               => 'Kelola Data Marketing || Bagian Kepala Bagian Marketing',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi dalam konten
            'data_marketing'      => $data_marketing,
            'nilai_kpi'           => $nilai_kpi
        );
        $this->load->view('kabag_marketing/index', $data);
    }
    public function crud()
    {
        // Menambahkan data anggota petugas marketing
        if (isset($_POST['simpanPetugasMarketing'])) {
            $inputan = $this->_inputanDataPetugasMarketing();
            $v_inputan = $this->_validasiDataInputan($inputan);
            $this->insert_model->simpanPetugasMarketing($inputan);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Menambah Data Petugas Marketing!</div>');
            redirect('kabag_marketing/data/marketing');
        }
        // Merubah Data Petugas Marketing
        if (isset($_POST['updatePetugasMarketing'])) {
            $inputan = $this->_inputanDataPetugasMarketingUpdate();
            $v_inputan = $this->_validasiDataInputanUpdate($inputan);
            $this->update_model->update_marketing($inputan, $v_inputan);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data Petugas Marketing!</div>');
            redirect('kabag_marketing/data/marketing');
        }
        // Blokir Petugas Marketing
        if (isset($_POST['blokirPetugasMarketing'])) {
            $data_blokir = array(
                'status_akun' => 'BLOKIR'
            );
            $this->db->where('id', htmlspecialchars($this->input->post('sistem_id')));
            $this->db->update('sistem_login', $data_blokir);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Memblokir Data Petugas Marketing!</div>');
            redirect('kabag_marketing/data/marketing');
        }
        // Blokir Petugas Marketing
        if (isset($_POST['hapusPetugasMarketing'])) {
            $data_blokir = array(
                'status_akun' => 'HAPUS'
            );
            $this->db->where('id', htmlspecialchars($this->input->post('sistem_id')));
            $this->db->update('sistem_login', $data_blokir);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Menghapus Data Petugas Marketing!</div>');
            redirect('kabag_marketing/data/marketing');
        }
    }
    // Validasi Inputan Insert Anggota Marketing
    private function _inputanDataPetugasMarketing()
    {
        $acak = $this->_randomString();
        $inputan = array(
            'email'          => htmlspecialchars($this->input->post('email')),
            'username'       => $acak,
            'nm_panggilan'   => htmlspecialchars($this->input->post('nm_anggota')),
            'nik'            => htmlspecialchars($this->input->post('nik')),
            'nm_lengkap'     => htmlspecialchars($this->input->post('nm_anggota')),
            'jk'             => htmlspecialchars($this->input->post('jk')),
            'tempat_lahir'   => htmlspecialchars($this->input->post('tempat_lahir')),
            'tgl_lahir'      => htmlspecialchars($this->input->post('tgl_lahir')),
            'alamat'         => htmlspecialchars($this->input->post('alamat_lengkap')),
            'no_hp'          => htmlspecialchars($this->input->post('no_hp'))
        );
        return $inputan;
    }
    // Validasi Isian Atau Form Insert Anggota Marketing
    private function _validasiDataInputan($inputan)
    {
        // Validasi Email
        $cek_email = $this->db->get_where('sistem_login', ['email' => $inputan['email']])->row_array();
        if ($cek_email > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Email Sudah Terdaftar!</div>');
            redirect('kabag_marketing/data/marketing');
        endif;
        // Validasi Username
        $cek_username = $this->db->get_where('sistem_login', ['username' => $inputan['username']])->row_array();
        if ($cek_username > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Username Sudah Terdaftar!</div>');
            redirect('kabag_marketing/data/marketing');
        endif;
        // Validasi NIK
        $cek_nik = $this->db->get_where('sistem_identitas', ['nik' => $inputan['nik']])->row_array();
        if ($cek_nik > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Sudah Terdaftar!</div>');
            redirect('kabag_marketing/data/marketing');
        else :
            if (strlen($inputan['nik']) != 16) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Harus 16 Karakter!</div>');
                redirect('kabag_marketing/data/marketing');
            endif;
        endif;
        // Validasi Nomor HP
        $cek_nomor = $this->db->get_where('sistem_identitas', ['no_hp' => $inputan['no_hp']])->row_array();
        if ($cek_nomor > 0) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Terdaftar!</div>');
            redirect('kabag_marketing/data/marketing');
        endif;
    }
    // Validasi Inputan Update Anggota Marketing
    private function _inputanDataPetugasMarketingUpdate()
    {
        $inputan = array(
            'id'             => htmlspecialchars($this->input->post('sistem_id')),
            'id_role'        => 9,
            'email'          => htmlspecialchars($this->input->post('email')),
            'email_lama'     => htmlspecialchars($this->input->post('email_lama')),
            'username'       => htmlspecialchars($this->input->post('username')),
            'username_lama'  => htmlspecialchars($this->input->post('username_lama')),
            'nm_panggilan'   => htmlspecialchars($this->input->post('nm_panggilan')),
            'nik'            => htmlspecialchars($this->input->post('nik')),
            'nik_lama'       => htmlspecialchars($this->input->post('nik_lama')),
            'nm_lengkap'     => htmlspecialchars($this->input->post('nm_panggilan')),
            'jk'             => htmlspecialchars($this->input->post('jk')),
            'tempat_lahir'   => htmlspecialchars($this->input->post('tempat_lahir')),
            'tgl_lahir'      => htmlspecialchars($this->input->post('tgl_lahir')),
            'alamat'         => htmlspecialchars($this->input->post('alamat_lengkap')),
            'no_hp'          => htmlspecialchars($this->input->post('no_hp')),
            'no_hp_lama'     => htmlspecialchars($this->input->post('no_hp_lama')),
            'foto_ktp_lama'  => htmlspecialchars($this->input->post('foto_ktp_lama')),
        );
        return $inputan;
    }
    // Validasi Isian atau form Update Anggota Marketing
    private function _validasiDataInputanUpdate($inputan)
    {
        // Validasi Email
        if ($inputan['email'] != $inputan['email_lama']) :
            $cek_email = $this->db->get_where('sistem_login', ['email' => $inputan['email']])->row_array();
            if ($cek_email > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Email Sudah Terdaftar!</div>');
                redirect('kabag_marketing/data/marketing');
            else :
                $email_baru = $inputan['email'];
            endif;
        else :
            $email_baru = $inputan['email_lama'];
        endif;
        // Validasi Username
        if ($inputan['username'] != $inputan['username_lama']) :
            $cek_username = $this->db->get_where('sistem_login', ['username' => $inputan['username']])->row_array();
            if ($cek_username > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Username Sudah Terdaftar!</div>');
                redirect('kabag_marketing/data/marketing');
            else :
                $username_baru = $inputan['username'];
            endif;
        else :
            $username_baru = $inputan['username_lama'];
        endif;
        // Validasi NIK
        if ($inputan['nik'] != $inputan['nik_lama']) :
            $cek_nik = $this->db->get_where('sistem_identitas', ['nik' => $inputan['nik']])->row_array();
            if ($cek_nik > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Sudah Terdaftar!</div>');
                redirect('kabag_marketing/data/marketing');
            else :
                if (strlen($inputan['nik']) != 16) :
                    $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">NIK Harus 16 Karakter!</div>');
                    redirect('kabag_marketing/data/marketing');
                else :
                    $nik_baru = $inputan['nik'];
                endif;
            endif;
        else :
            $nik_baru = $inputan['nik_lama'];
        endif;
        // Validasi Nomor HP
        if ($inputan['no_hp'] != $inputan['no_hp_lama']) :
            $cek_nomor = $this->db->get_where('sistem_identitas', ['no_hp' => $inputan['no_hp']])->row_array();
            if ($cek_nomor > 0) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Nomor HP Sudah Terdaftar!</div>');
                redirect('kabag_marketing/data/marketing');
            else :
                $no_hp_baru = $inputan['no_hp'];
            endif;
        else :
            $no_hp_baru = $inputan['no_hp_lama'];
        endif;
        $v_datainputan = array(
            'email' => $email_baru,
            'username' => $username_baru,
            'nik' => $nik_baru,
            'no_hp' => $no_hp_baru
        );
        return $v_datainputan;
    }
    private function _randomString()
    {
        $str        = "";
        $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789';
        $max        = strlen($characters) - 1;
        for ($i = 0; $i <= 5; $i++) {
            $rand = mt_rand(0, $max);
            $str .= $characters[$rand];
        }
        return $str;
    }
    // Update Nilai KPI
    public function update_kpi($id)
    {
        $marketing_id = $this->encrypt->decode($id);
        $hasil        = $this->db->get_where('k_hasil', ['login_id' => $marketing_id, 'sts_hasil' => 'PROSES'])->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) :
            $reviews_nilai = 0;
            $nilai_reviews = 0;
        else:
            $reviews_nilai = $ttl_bintang['rate_bintang'] / $jml_reviews;
            $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'   => $reviews_nilai,
            '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('kabag_marketing/marketing_aktifitas/pinjaman/' . $this->encrypt->encode($marketing_id));
    }
    public function kpi_update($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) :
            $reviews_nilai = 0;
            $nilai_reviews = 0;
        else:
            $reviews_nilai = $ttl_bintang['rate_bintang'] / $jml_reviews;
            $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'   => $reviews_nilai,
            '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('kabag_marketing/marketing_aktifitas/pinjaman/' . $this->encrypt->encode($marketing_id));
    }
    // Detail KPI Marketing
    public function detail_kpi($id)
    {
        $hasil_id = $this->encrypt->decode($id);
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $notif                 = $this->notifikasi->notif_kabag_marketing();
        // 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();
        // Data Marketing
        // marketing KPI
        // 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();
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'aktifitas/kpi_detail',
            'page'                => 'index',
            'judul'               => 'Detail KPI Marketing || Bagian Manager',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Data Nilai Komponen Faktor
            'data_kpi'            => $kpi,
            'marketing'           => $login_marketing,
            'identitas_marketing' => $identitas_marketing,
            'detail_kpi'          => $kpi_detail,
            'reviews'             => $data_review
        );
        $this->load->view('kabag_marketing/index', $data);
    }
}

/* End of file ControllerDataMarketing.php and path \application\controllers\kabag_marketing\kelola_data\ControllerDataMarketing.php */