<?php

use Google\Service\CloudSearch\Resource\Query;

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

class Select_model extends CI_Model
{
    // Alamat Baru
    function vd_kotkab()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('sistem_alamat');
        $query = $this->db->group_by('kot_kab', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    function vd_kecamatan($nm_kot_kab)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('sistem_alamat');
        $query = $this->db->where('kot_kab', $nm_kot_kab);
        $query = $this->db->group_by('nm_kecamatan', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    function vd_desa($nm_kecamatan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('sistem_alamat');
        $query = $this->db->where('nm_kecamatan', $nm_kecamatan);
        $query = $this->db->group_by('nm_desa', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Bagian Kelola Data
    // Menu Marketing
    function vd_marketing()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('sistem_login as A');
        $query = $this->db->join('sistem_identitas as B', 'A.id=B.login_id');
        $query = $this->db->where('A.id_role', 9);
        $query = $this->db->get();
        return $query->result();
    }
    // Bagian Pengajuan Pinjaman
    // Menampilkan data jenis pinjaman yang untuk marketing
    function vd_jenisPinjamanAktif()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('sistem_produk_koperasi');
        $query = $this->db->where('substr(id, 1, 2)=', '12');
        $query = $this->db->where('status_produk', 'AKTIF');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan nomor kontrak terakhir
    function lihatSemuaKontrak($data_inputan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_aturan as B', 'A.aturan_id=B.id');
        $query = $this->db->where('B.produk_id', $data_inputan['produk_id']);
        $query = $this->db->order_by('A.created_at', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data pengajuan pinjaman
    function vd_pengajuanPinjaman()
    {
        $query = $this->db->select('*, A.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'B.kontrak_id=A.id');
        $query = $this->db->join('anggota as C', 'A.anggota_id=C.id');
        $query = $this->db->join('sistem_alamat as D', 'C.id_alamat=D.id');
        $query = $this->db->join('anggota_keluarga_detail as E', 'A.anggota_id=E.anggota_id');
        $query = $this->db->join('p_koperasi_aturan as F', 'A.aturan_id=F.id');
        $query = $this->db->where('A.status_kontrak !=', 'TERIMA');
        $query = $this->db->where('A.status_kontrak !=', 'TOLAK');
        $query = $this->db->where('A.status_kontrak !=', 'REALISASI');
        $query = $this->db->where('A.status_kontrak !=', 'AKTIF');
        $query = $this->db->where('A.status_kontrak !=', 'LUNAS_LANCAR');
        $query = $this->db->where('A.status_kontrak !=', 'MACET');
        $query = $this->db->where('A.status_kontrak !=', 'LUNAS_MACET');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        // $query = $this->db->order_by('A.id', 'DESC');
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // View Data Pengajuan Pinjaman
    function vd_PinjamanAktif()
    {
        $query = $this->db->select('*, A.id as nomor_kontrak, C.no_hp as hp_customer');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'B.kontrak_id=A.id');
        $query = $this->db->join('anggota as C', 'A.anggota_id=C.id');
        $query = $this->db->join('sistem_alamat as D', 'C.id_alamat=D.id');
        $query = $this->db->join('anggota_keluarga_detail as E', 'A.anggota_id=E.anggota_id');
        $query = $this->db->join('p_koperasi_aturan as F', 'A.aturan_id=F.id');
        $query = $this->db->join('sistem_login as G', 'A.login_id=G.id');
        $query = $this->db->where('A.status_kontrak', 'AKTIF');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // View Data Pinjaman Macet
    function vd_PinjamanMacet()
    {
        $query = $this->db->select('*, A.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'B.kontrak_id=A.id');
        $query = $this->db->join('anggota as C', 'A.anggota_id=C.id');
        $query = $this->db->join('sistem_alamat as D', 'C.id_alamat=D.id');
        $query = $this->db->join('anggota_keluarga_detail as E', 'A.anggota_id=E.anggota_id');
        $query = $this->db->join('p_koperasi_aturan as F', 'A.aturan_id=F.id');
        $query = $this->db->join('sistem_login as G', 'A.login_id=G.id');
        $query = $this->db->where('A.status_kontrak', 'MACET');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // View Data Pinjaman Lunas
    function vd_PinjamanLunas()
    {
        $query = $this->db->select('*, A.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'B.kontrak_id=A.id');
        $query = $this->db->join('anggota as C', 'A.anggota_id=C.id');
        $query = $this->db->join('sistem_alamat as D', 'C.id_alamat=D.id');
        $query = $this->db->join('anggota_keluarga_detail as E', 'A.anggota_id=E.anggota_id');
        $query = $this->db->join('p_koperasi_aturan as F', 'A.aturan_id=F.id');
        $query = $this->db->join('sistem_login as G', 'A.login_id=G.id');
        $query = $this->db->where('A.status_kontrak !=', 'KONFIRMASI');
        $query = $this->db->where('A.status_kontrak !=', 'TERIMA');
        $query = $this->db->where('A.status_kontrak !=', 'TOLAK');
        $query = $this->db->where('A.status_kontrak !=', 'REALISASI');
        $query = $this->db->where('A.status_kontrak !=', 'AKTIF');
        $query = $this->db->where('A.status_kontrak !=', 'MACET');
        $query = $this->db->where('A.status_kontrak !=', 'PROSES');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data riwayat pengajuan pinjaman
    function vd_RiwayatPengajuanPinjaman()
    {
        $query = $this->db->select('*, A.id as nomor_kontrak');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('p_koperasi_progres as B', 'B.kontrak_id=A.id');
        $query = $this->db->join('anggota as C', 'A.anggota_id=C.id');
        $query = $this->db->join('sistem_alamat as D', 'C.id_alamat=D.id');
        $query = $this->db->join('anggota_keluarga_detail as E', 'A.anggota_id=E.anggota_id');
        $query = $this->db->join('p_koperasi_aturan as F', 'A.aturan_id=F.id');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->where('A.status_kontrak', 'TERIMA');
        $query = $this->db->or_where('A.status_kontrak', 'TOLAK');
        // $query = $this->db->order_by('A.id', 'DESC');
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data jaminan pinjaman
    function vd_jaminanPinjaman()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_koperasi_agungan');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan buku pinjaman
    function vd_angsuran($kontrak_id)
    {
        $query = $this->db->select('*, A.id as angs_id');
        $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('p_koperasi_aturan as C', 'B.aturan_id=C.id');
        $query = $this->db->where('kontrak_id', $kontrak_id);
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan buku pinjaman
    function vd_SudahBayarAngsuran($kontrak_id)
    {
        $query = $this->db->select('SUM(bsr_bayar) as sudah_bayar');
        $query = $this->db->from('p_koperasi_angsuran');
        $query = $this->db->where('kontrak_id', $kontrak_id);
        $query = $this->db->where('status_angsuran !=', 'BELUM');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan nomor nota paling besar
    // Menampilkan nomor urut invoice paling besar
    function vd_NotaTagihanPinjaman()
    {
        $date = date('Y-m-d');
        $query = $this->db->select('*, id as kd_invoice');
        $query = $this->db->from('nota');
        $query = $this->db->where('tgl_pembayaran', $date);
        $query = $this->db->where('substr(id, 1, 2)=', '12');
        $query = $this->db->where('status_kas !=', 'OMSET');
        $query = $this->db->where('status_kas !=', 'OPERASIONAL');
        $query = $this->db->order_by('id', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data invoice
    function getDataInvoice($kd_invoice)
    {
        $query = $this->db->select('*, A.id as kd_invoice, B.no_hp as hp_anggota');
        $query = $this->db->from('nota as A');
        $query = $this->db->join('anggota as B', 'A.anggota_id=B.id');
        $query = $this->db->join('sistem_alamat as C', 'B.id_alamat=C.id');
        $query = $this->db->join('sistem_login as D', 'A.petugas=D.id');
        $query = $this->db->where('A.id', $kd_invoice);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Data Invoice Rincian
    function getDataInvoiceRinci($kd_invoice)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('nota as A');
        $query = $this->db->join('p_koperasi_nota as B', 'B.nota_id=A.id');
        $query = $this->db->join('p_koperasi_angsuran as C', 'B.angsuran_id=C.id');
        $query = $this->db->join('p_koperasi_kontrak as D', 'C.kontrak_id=D.id');
        $query = $this->db->join('p_koperasi_aturan as E', 'D.aturan_id=E.id');
        $query = $this->db->where('A.id', $kd_invoice);
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan pembayaran Macet
    function getDataAngsMacet($kontrak_id)
    {
        $query = $this->db->select('count(*) as tagihan_macet');
        $query = $this->db->from('p_koperasi_angsuran');
        $query = $this->db->where('kontrak_id', $kontrak_id);
        $query = $this->db->where('status_angsuran', 'LUNAS_MACET');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan pembayaran Lancar
    function getDataAngsLancar($kontrak_id)
    {
        $query = $this->db->select('count(*) as tagihan_lancar');
        $query = $this->db->from('p_koperasi_angsuran');
        $query = $this->db->where('kontrak_id', $kontrak_id);
        $query = $this->db->where('status_angsuran', 'LUNAS_LANCAR');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan Nota
    function vd_notaBayarAngsuran($kontrak_id)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_koperasi_nota as A');
        $query = $this->db->join('nota as B', 'A.nota_id=B.id');
        $query = $this->db->join('p_koperasi_angsuran as C', 'A.angsuran_id=C.id');
        $query = $this->db->join('p_koperasi_kontrak as D', 'C.kontrak_id=D.id');
        $query = $this->db->join('p_koperasi_aturan as E', 'D.aturan_id=E.id');
        $query = $this->db->where('C.kontrak_id', $kontrak_id);
        $query = $this->db->get();
        return $query->result();
    }
    // Menu Beranda
    // Bagian Melihat data pinjaman
    function vd_tagihanHariIni()
    {
        $query = $this->db->select('*, A.id as angsuran_id');
        $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('p_koperasi_aturan as C', 'B.aturan_id=C.id');
        $query = $this->db->join('anggota as D', 'B.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->where('A.tgl_tempo', date('Y-m-d'));
        $query = $this->db->or_where('A.status_angsuran', 'MACET');
        $query = $this->db->get();
        return $query->result();
    }
    function vd_transaksiBulanan()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $query = $this->db->select('*, A.id as angsuran_id');
        $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('p_koperasi_aturan as C', 'B.aturan_id=C.id');
        $query = $this->db->join('anggota as D', 'B.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->where('B.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->where('A.status_angsuran !=', 'BELUM');
        $query = $this->db->where('A.status_angsuran !=', 'MACET');
        $query = $this->db->where('MONTH(A.tgl_bayar)', $bulan);
        $query = $this->db->where('YEAR(A.tgl_bayar)', $tahun);
        $query = $this->db->get();
        return $query->result();
    }
    function vd_jmlTagihanHariIni()
    {
        $query = $this->db->select('SUM(A.bsr_bayar) as saldo');
        $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('p_koperasi_aturan as C', 'B.aturan_id=C.id');
        $query = $this->db->join('anggota as D', 'B.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->where('B.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->where('A.tgl_tempo', date('Y-m-d'));
        $query = $this->db->or_where('A.status_angsuran', 'MACET');
        $query = $this->db->get();
        return $query->row_array();
    }
    function vd_jmlNotaHariIni()
    {
        $query = $this->db->select('SUM(A.bsr_bayar) as saldo');
        $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('B.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->where('A.tgl_bayar', date('Y-m-d'));
        $query = $this->db->where('A.status_angsuran !=', 'BELUM');
        $query = $this->db->where('A.status_angsuran !=', 'MACET');
        $query = $this->db->where('A.status_angsuran !=', 'PROSES');
        $query = $this->db->get();
        return $query->row_array();
    }
    function vd_jmlTagihanBulanan()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $query = $this->db->select('SUM(A.bsr_bayar) as saldo');
        $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('p_koperasi_aturan as C', 'B.aturan_id=C.id');
        $query = $this->db->join('anggota as D', 'B.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->where('B.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->where('MONTH(A.tgl_tempo)', $bulan);
        $query = $this->db->where('YEAR(A.tgl_tempo)', $tahun);
        $query = $this->db->get();
        return $query->row_array();
    }
    function getDataInvoiceRinciAngsuran($kd_invoice)
    {
        $query = $this->db->select('*, C.id as angsuran_id');
        $query = $this->db->from('nota as A');
        $query = $this->db->join('p_koperasi_nota as B', 'B.nota_id=A.id');
        $query = $this->db->join('p_koperasi_angsuran as C', 'B.angsuran_id=C.id');
        $query = $this->db->join('p_koperasi_kontrak as D', 'C.kontrak_id=D.id');
        $query = $this->db->join('p_koperasi_aturan as E', 'D.aturan_id=E.id');
        $query = $this->db->where('A.id', $kd_invoice);
        $query = $this->db->get();
        return $query->result();
    }
    function vd_jmlNotaBulanan()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $query = $this->db->select('A.ttl_bayar');
        $query = $this->db->from('nota as A');
        $query = $this->db->join('p_koperasi_nota as B', 'A.id=B.nota_id');
        $query = $this->db->join('p_koperasi_angsuran as C', 'B.angsuran_id=C.id');
        $query = $this->db->join('p_koperasi_kontrak as D', 'C.kontrak_id=D.id');
        $query = $this->db->where('D.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->where('substr(A.produk_id, 1, 2)=', '12');
        $query = $this->db->where('A.status_kas', 'MASUK');
        $query = $this->db->where('A.status_nota', 'TERIMA');
        $query = $this->db->where('MONTH(A.tgl_pembayaran)', $bulan);
        $query = $this->db->where('YEAR(A.tgl_pembayaran)', $tahun);
        $query = $this->db->where('A.status_nota !=', 'PROSES');
        $query = $this->db->where('A.status_nota !=', 'TOLAK');
        $query = $this->db->where('A.status_nota !=', 'PROSES_TUNGGAKAN');
        $query = $this->db->group_by('A.id');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua anggota koperasi
    function vd_anggotaKoeprasi()
    {
        $query = $this->db->select('*, A.id as anggota_id, B.id alamat_id');
        $query = $this->db->from('anggota as A');
        $query = $this->db->join('sistem_alamat as B', 'A.id_alamat=B.id');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua anggota koperasi
    function vd_anggotaKoprasi()
    {
        $query = $this->db->select('*, B.id as anggota_id, C.id alamat_id');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('anggota as B', 'A.anggota_id=B.id');
        $query = $this->db->join('sistem_alamat as C', 'B.id_alamat=C.id');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->group_by('A.anggota_id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua anggota koperasi
    function vd_anggotaKoeprasiAktif()
    {
        $query = $this->db->select('*, A.id as anggota_id, B.id alamat_id');
        $query = $this->db->from('anggota as A');
        $query = $this->db->join('sistem_alamat as B', 'A.id_alamat=B.id');
        $query = $this->db->where('A.status_anggota', 'ANGGOTA');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->get();
        return $query->result();
    }
    function vd_anggotaKoprasiAktif()
    {
        $query = $this->db->select('*, B.id as anggota_id, C.id alamat_id');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('anggota as B', 'A.anggota_id=B.id');
        $query = $this->db->join('sistem_alamat as C', 'B.id_alamat=C.id');
        $query = $this->db->where('B.status_anggota', 'ANGGOTA');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->group_by('A.anggota_id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    function updateDataKaryawanLama($data_inputan, $alamat_id)
    {
        $link_foto_lama   = $data_inputan['foto_ktp_old'];
        $config['upload_path']   = './assets/ptgs_id';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_login = array(
                'email'             => $data_inputan['email_baru'],
                'no_hp'             => $data_inputan['no_hp_baru'],
                'nm_panggilan'      => $data_inputan['nm_panggilan']
            );
            $this->db->where('id', $data_inputan['login_id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik'             => $data_inputan['nik'],
                'jk'              => $data_inputan['jk'],
                'tempat_lahir'    => $data_inputan['tempat_lahir'],
                'tgl_lahir'       => $data_inputan['tgl_lahir'],
                'alamat_id'       => $alamat_id,
                'tgl_gabung'      => $data_inputan['tgl_gabung']
            );
            $this->db->where('login_id', $data_inputan['login_id']);
            $this->db->update('sistem_identitas', $data_identitas);
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadData = $this->upload->data();
            // Inputan
            $data_login = array(
                'email'             => $data_inputan['email_baru'],
                'no_hp'             => $data_inputan['no_hp_baru'],
                'nm_panggilan'      => $data_inputan['nm_panggilan']
            );
            $this->db->where('id', $data_inputan['login_id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik'             => $data_inputan['nik'],
                'jk'              => $data_inputan['jk'],
                'tempat_lahir'    => $data_inputan['tempat_lahir'],
                'tgl_lahir'       => $data_inputan['tgl_lahir'],
                'alamat_id'       => $alamat_id,
                'foto_ktp'        => $uploadData['file_name'],
                'tgl_gabung'      => $data_inputan['tgl_gabung']
            );
            $this->db->where('login_id', $data_inputan['login_id']);
            $this->db->update('sistem_identitas', $data_identitas);
            $berkas_lama = './assets/ptgs_id/' . $link_foto_lama . '';
            unlink($berkas_lama);
        endif;
    }
    // Menampilkan semua anggota koperasi
    function vd_anggotaKoeprasiPasif()
    {
        $query = $this->db->select('*, A.id as anggota_id, B.id alamat_id');
        $query = $this->db->from('anggota as A');
        $query = $this->db->join('sistem_alamat as B', 'A.id_alamat=B.id');
        $query = $this->db->where('A.status_anggota', 'CALON_ANGGOTA');
        $query = $this->db->get();
        return $query->result();
    }
    function vd_anggotaKoprasiPasif()
    {
        $query = $this->db->select('*, B.id as anggota_id, C.id alamat_id');
        $query = $this->db->from('p_koperasi_kontrak as A');
        $query = $this->db->join('anggota as B', 'A.anggota_id=B.id');
        $query = $this->db->join('sistem_alamat as C', 'B.id_alamat=C.id');
        $query = $this->db->where('B.status_anggota', 'CALON_ANGGOTA');
        $query = $this->db->where('A.login_id', $this->session->userdata('isloggedin'));
        $query = $this->db->group_by('A.anggota_id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data identitas Pengajuan Anggota Arisan
    function getDataIdentitasAnggota($id_anggota)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('anggota as A');
        $query = $this->db->join('sistem_alamat as B', 'A.id_alamat=B.id');
        $query = $this->db->where('A.id', $id_anggota);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data produk arisan heboh aktif anggota
    function getDataProdukAHA($id_anggota)
    {
        $query = $this->db->select('*, A.status as status_arisan, B.id as arisan_id');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->join('p_arisan as B', 'A.regist_arisan=B.id');
        $query = $this->db->join('sistem_login as C', 'A.login_id=C.id');
        $query = $this->db->where('A.status', 'AKTIF');
        $query = $this->db->where('A.anggota_id', $id_anggota);
        $query = $this->db->where('B.produk_id', 21);
        $query = $this->db->get();
        return $query->result();
    }
    function getDataProdukAHP($id_anggota)
    {
        $query = $this->db->select('*, A.status as status_arisan, B.id as arisan_id');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->join('p_arisan as B', 'A.regist_arisan=B.id');
        $query = $this->db->join('sistem_login as C', 'A.login_id=C.id');
        $query = $this->db->where('A.status !=', 'AKTIF');
        $query = $this->db->where('A.anggota_id', $id_anggota);
        $query = $this->db->where('B.produk_id', 21);
        $query = $this->db->get();
        return $query->result();
    }
    function getDataProdukAMA($id_anggota)
    {
        $query = $this->db->select('*, A.status as status_arisan, B.id as arisan_id');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->join('p_arisan as B', 'A.regist_arisan=B.id');
        $query = $this->db->join('sistem_login as C', 'A.login_id=C.id');
        $query = $this->db->where('B.status_arisan', 'AKTIF');
        $query = $this->db->where('A.anggota_id', $id_anggota);
        $query = $this->db->where('B.produk_id', 22);
        $query = $this->db->get();
        return $query->result();
    }
    function getDataProdukAMP($id_anggota)
    {
        $query = $this->db->select('*, A.status as status_arisan, B.id as arisan_id');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->join('p_arisan as B', 'A.regist_arisan=B.id');
        $query = $this->db->join('sistem_login as C', 'A.login_id=C.id');
        $query = $this->db->where('B.status_arisan', 'SELESAI');
        $query = $this->db->where('A.anggota_id', $id_anggota);
        $query = $this->db->where('B.produk_id', 22);
        $query = $this->db->get();
        return $query->result();
    }
    // Bagian Ajx
    // Menampilkan data identitas anggota dengan NIK
    function getDataAnggota($nik)
    {
        $query = $this->db->select('*, A.id as id_anggota');
        $query = $this->db->from('anggota as A');
        $query = $this->db->join('sistem_alamat as B', 'A.id_alamat=B.id');
        $query = $this->db->where('A.nik', $nik);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Melihat data pinjaman anggota
    // Aktif
    function vd_anggotaPinjamanAktif($id_anggota)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_koperasi_kontrak');
        $query = $this->db->where('anggota_id', $id_anggota);
        $query = $this->db->where('status_kontrak !=', 'KONFIRMASI');
        $query = $this->db->where('status_kontrak !=', 'TERIMA');
        $query = $this->db->where('status_kontrak !=', 'TOLAK');
        $query = $this->db->where('status_kontrak !=', 'REALISASI');
        $query = $this->db->where('status_kontrak !=', 'LUNAS_LANCAR');
        $query = $this->db->where('status_kontrak !=', 'LUNAS_MACET');
        $query = $this->db->where('status_kontrak !=', 'PROSES');
        $query = $this->db->where('status_kontrak !=', 'KASIR');
        $query = $this->db->where('status_kontrak !=', 'SURVEY');
        $query = $this->db->get();
        return $query->result();
    }
    // Selesai
    function vd_anggotaPinjamanSelesai($id_anggota)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_koperasi_kontrak');
        $query = $this->db->where('anggota_id', $id_anggota);
        $query = $this->db->where('status_kontrak', 'KONFIRMASI');
        $query = $this->db->where('status_kontrak', 'TERIMA');
        $query = $this->db->where('status_kontrak', 'TOLAK');
        $query = $this->db->where('status_kontrak', 'REALISASI');
        $query = $this->db->where('status_kontrak', 'AKTIF');
        $query = $this->db->where('status_kontrak', 'MACET');
        $query = $this->db->where('status_kontrak', 'PROSES');
        $query = $this->db->where('status_kontrak', 'KASIR');
        $query = $this->db->where('status_kontrak', 'SURVEY');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data detail KPI
    function vd_detKPIMarketing($hasil_id)
    {
        $query = $this->db->select('*');
        $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', $hasil_id);
        $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) 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 KPI
    function vd_daftarHistoryKPI($marketing_id)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('k_hasil');
        $query = $this->db->where('login_id', $marketing_id);
        $query = $this->db->order_by('id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data review konsumen
    function vd_Review($marketing_id)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('k_rkonsumen');
        $query = $this->db->where('login_id', $marketing_id);
        $query = $this->db->order_by('id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }
    function vd_logsWA($tgl, $marketing_id)
    {
        $query = $this->db->select('*, A.no_hp as hp_nasabah, A.id as logs_id');
        $query = $this->db->from('logs_wa as A');
        $query = $this->db->join('sistem_login as B', 'A.login_id=B.id');
        $query = $this->db->join('sistem_identitas as C', 'B.id=C.login_id');
        $query = $this->db->where('A.tgl_kirim', $tgl);
        $query = $this->db->where('A.login_id', $marketing_id);
        $query = $this->db->order_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
}
