<?php

use Google\Service\CloudSearch\Resource\Query;

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

class Koperasi extends CI_Model
{
    public function vdNotif2Hari($tgl_2hari)
    {
        $query = $this->db->select('*, B.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_angsuran as A');
        $query = $this->db->join('p_koperasi_kontrak as B', 'A.kontrak_id=B.id');
        $query = $this->db->join('anggota as C', 'B.anggota_id=C.id');
        $query = $this->db->join('p_koperasi_aturan as D', 'B.aturan_id=D.id');
        $query = $this->db->join('sistem_produk_koperasi as E', 'D.produk_id=E.id');
        $query = $this->db->where('A.status_angsuran', 'BELUM');
        $query = $this->db->where('A.tgl_tempo', $tgl_2hari);
        $query = $this->db->get();
        return $query->result();
    }
    public function vdNotif1Hari($tgl_1hari)
    {
        $query = $this->db->select('*, B.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_angsuran as A');
        $query = $this->db->join('p_koperasi_kontrak as B', 'A.kontrak_id=B.id');
        $query = $this->db->join('anggota as C', 'B.anggota_id=C.id');
        $query = $this->db->join('p_koperasi_aturan as D', 'B.aturan_id=D.id');
        $query = $this->db->join('sistem_produk_koperasi as E', 'D.produk_id=E.id');
        $query = $this->db->where('A.status_angsuran', 'BELUM');
        $query = $this->db->where('A.tgl_tempo', $tgl_1hari);
        $query = $this->db->get();
        return $query->result();
    }
    public function vdNotifHari($tgl_hari)
    {
        $query = $this->db->select('*, B.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_angsuran as A');
        $query = $this->db->join('p_koperasi_kontrak as B', 'A.kontrak_id=B.id');
        $query = $this->db->join('anggota as C', 'B.anggota_id=C.id');
        $query = $this->db->join('p_koperasi_aturan as D', 'B.aturan_id=D.id');
        $query = $this->db->join('sistem_produk_koperasi as E', 'D.produk_id=E.id');
        $query = $this->db->where('A.status_angsuran', 'BELUM');
        $query = $this->db->where('A.tgl_tempo', $tgl_hari);
        $query = $this->db->get();
        return $query->result();
    }
    public function vd_macet_banyak()
    {
        $query = $this->db->select('*, count(kontrak_id) as jml_macet');
        $query = $this->db->from('p_koperasi_angsuran');
        $query = $this->db->where('status_angsuran', 'MACET');
        $query = $this->db->group_by('kontrak_id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }
    function vd_marketingAktif()
    {
        $query = $this->db->select('*, A.id as marketing_id');
        $query = $this->db->from('sistem_login as A');
        $query = $this->db->join('sistem_identitas as B', 'A.id=B.login_id');
        $query = $this->db->join('sistem_alamat as C', 'B.alamat_id=C.id');
        $query = $this->db->join('sistem_role_level as D', 'A.role_id=D.id');
        $query = $this->db->where('A.status_akun', 'AKTIF');
        $query = $this->db->where('A.role_id', '7');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan jumlah Rupiah realisasi bulanan
    function vd_totalJmlRealisasi($marketing_id, $bulan, $tahun)
    {
        $query = $this->db->select('sum(A.pokok_pinjaman) jml_realisasi');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'A.id=B.kontrak_id');
        $query = $this->db->where('A.status_kontrak', 'AKTIF');
        $query = $this->db->where('A.login_id', $marketing_id);
        $query = $this->db->where('MONTH(B.tgl_realisasi)', $bulan);
        $query = $this->db->where('YEAR(B.tgl_realisasi)', $tahun);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan jumlah Rupiah realisasi bulanan devisi
    function vd_totalJmlRealisasiDevisi1($bulan, $tahun)
    {
        $query = $this->db->select('sum(A.pokok_pinjaman) as jml_realisasi');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'A.id=B.kontrak_id');
        $query = $this->db->where('A.status_kontrak', 'AKTIF');
        $query = $this->db->where('MONTH(B.tgl_realisasi)', $bulan);
        $query = $this->db->where('YEAR(B.tgl_realisasi)', $tahun);
        $query = $this->db->where('A.aturan_id', '1');
        $query = $this->db->get();
        return $query->row_array();
    }
    function vd_totalJmlRealisasiDevisi2($bulan, $tahun)
    {
        $query = $this->db->select('sum(A.pokok_pinjaman) as jml_realisasi');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'A.id=B.kontrak_id');
        $query = $this->db->where('A.status_kontrak', 'AKTIF');
        $query = $this->db->where('MONTH(B.tgl_realisasi)', $bulan);
        $query = $this->db->where('YEAR(B.tgl_realisasi)', $tahun);
        $query = $this->db->where('A.aturan_id', '2');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan jumlah Rupiah storting bulanan Devisi
    function vd_totalJmlStortingDevisi1($bulan, $tahun)
    {
        $query = $this->db->select('sum(A.bsr_bayar) as jml_storting');
        $query = $this->db->from('p_koperasi_angsuran as A');
        $query = $this->db->join('p_koperasi_kontrak as B', 'A.kontrak_id=B.id');
        $query = $this->db->where('MONTH(A.tgl_bayar)', $bulan);
        $query = $this->db->where('YEAR(A.tgl_bayar)', $tahun);
        $query = $this->db->where('B.aturan_id', '1');
        $query = $this->db->get();
        return $query->row_array();
    }
    function vd_totalJmlStortingDevisi2($bulan, $tahun)
    {
        $query = $this->db->select('sum(A.bsr_bayar) as jml_storting');
        $query = $this->db->from('p_koperasi_angsuran as A');
        $query = $this->db->join('p_koperasi_kontrak as B', 'A.kontrak_id=B.id');
        $query = $this->db->where('MONTH(A.tgl_bayar)', $bulan);
        $query = $this->db->where('YEAR(A.tgl_bayar)', $tahun);
        $query = $this->db->where('B.aturan_id', '2');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan jumlah Rupiah storting bulanan
    function vd_totalJmlStorting($marketing_id, $bulan, $tahun)
    {
        $query = $this->db->select('sum(A.bsr_bayar) jml_storting');
        $query = $this->db->from('p_koperasi_angsuran as A');
        $query = $this->db->join('p_koperasi_kontrak as B', 'A.kontrak_id=B.id');
        $query = $this->db->where('MONTH(A.tgl_bayar)', $bulan);
        $query = $this->db->where('YEAR(A.tgl_bayar)', $tahun);
        $query = $this->db->where('B.login_id', $marketing_id);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan jumlah binatang review
    function vd_bintangReviewsPelanggan($marketing_id, $bulan, $tahun)
    {
        $query = $this->db->select('sum(rating) as rate_bintang');
        $query = $this->db->from('k_rkonsumen');
        $query = $this->db->where('login_id', $marketing_id);
        $query = $this->db->where('MONTH(tgl_review)', $bulan);
        $query = $this->db->where('YEAR(tgl_review)', $tahun);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil
    function vd_ddhl_1($data_bulan_lalu)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan_lalu['id']);
        $query = $this->db->where('A.faktor_id', '1');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil
    function vd_ddhl_2($data_bulan_lalu)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan_lalu['id']);
        $query = $this->db->where('A.faktor_id', '2');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil
    function vd_ddhl_3($data_bulan_lalu)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan_lalu['id']);
        $query = $this->db->where('A.faktor_id', '3');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil
    function vd_ddhl_5($data_bulan_lalu)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan_lalu['id']);
        $query = $this->db->where('A.faktor_id', '5');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil
    function vd_ddhl_7($data_bulan_lalu)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan_lalu['id']);
        $query = $this->db->where('A.faktor_id', '7');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil
    function vd_ddhl_8($data_bulan_lalu)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan_lalu['id']);
        $query = $this->db->where('A.faktor_id', '8');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil Sekarang
    function vd_ddhi_1($data_bulan)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan['id']);
        $query = $this->db->where('A.faktor_id', '1');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil Sekarang
    function vd_ddhi_2($data_bulan)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan['id']);
        $query = $this->db->where('A.faktor_id', '2');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil Sekarang
    function vd_ddhi_3($data_bulan)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan['id']);
        $query = $this->db->where('A.faktor_id', '3');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil Sekarang
    function vd_ddhi_5($data_bulan)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan['id']);
        $query = $this->db->where('A.faktor_id', '5');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil Sekarang
    function vd_ddhi_7($data_bulan)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan['id']);
        $query = $this->db->where('A.faktor_id', '7');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data detail hasil Sekarang
    function vd_ddhi_8($data_bulan)
    {
        $query = $this->db->select('A.faktor_id as faktor_id, C.nm_komp as komp, B.nm_faktor as fact, A.target as tgt, A.realisasi as realisasi, A.persentase as persentase, A.nilai as angka');
        $query = $this->db->from('k_dhasil as A');
        $query = $this->db->join('k_faktor as B', 'A.faktor_id=B.id');
        $query = $this->db->join('k_komp as C', 'B.komp_id=C.id');
        $query = $this->db->where('A.hasil_id', $data_bulan['id']);
        $query = $this->db->where('A.faktor_id', '8');
        $query = $this->db->get();
        return $query->row_array();
    }
}