<?php

use Google\Service\CloudSearch\Resource\Query;

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

class Select_model extends CI_Model
{
    // Menu Operasional Arisan Motor
    // Menampilkan semua data arisan motor
    function vd_arisanMotor()
    {
        $query = $this->db->select('*, C.id as jadwal_id');
        $query = $this->db->from('p_arisan as A');
        $query = $this->db->join('sistem_produk_koperasi as B', 'A.produk_id=B.id');
        $query = $this->db->join('p_arisan_jadwal as C', 'C.regist_arisan=A.id');
        $query = $this->db->where('A.status_arisan', 'AKTIF');
        $query = $this->db->where('A.produk_id', '22');
        $query = $this->db->where('C.status_undian !=', 'PROSES');
        $query = $this->db->where('C.status_undian !=', 'VERIFIKASI');
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data arisan motor aktif
    function vd_arisanMotorAktif()
    {
        $query = $this->db->select('*, C.id as jadwal_id');
        $query = $this->db->from('p_arisan as A');
        $query = $this->db->join('sistem_produk_koperasi as B', 'A.produk_id=B.id');
        $query = $this->db->join('p_arisan_jadwal as C', 'C.regist_arisan=A.id');
        $query = $this->db->where('A.status_arisan', 'AKTIF');
        $query = $this->db->where('A.produk_id', '22');
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data arisan Heboh aktif
    function vd_arisanHebohAktif()
    {
        $query = $this->db->select('*, C.id as jadwal_id');
        $query = $this->db->from('p_arisan as A');
        $query = $this->db->join('sistem_produk_koperasi as B', 'A.produk_id=B.id');
        $query = $this->db->join('p_arisan_jadwal as C', 'C.regist_arisan=A.id');
        $query = $this->db->where('A.status_arisan', 'AKTIF');
        $query = $this->db->where('A.produk_id', '21');
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data arisan selesai
    function vd_arisanMotorSelesai()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan as A');
        $query = $this->db->join('sistem_produk_koperasi as B', 'A.produk_id=B.id');
        $query = $this->db->where('A.status_arisan', 'SELESAI');
        $query = $this->db->where('A.produk_id', '22');
        $query = $this->db->group_by('A.id', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data anggota aktif arisan
    function getDataAnggotaAktif()
    {
        $query = $this->db->select('regist_arisan, count(A.regist_arisan) as anggota_aktif');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->where('A.status', 'AKTIF');
        $query = $this->db->group_by('A.regist_arisan');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data anggota aktif arisan Motor
    function getDataAnggotaAktifMotor()
    {
        $query = $this->db->select('regist_arisan, count(A.regist_arisan) as anggota_aktif');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->where('A.status !=', 'MUNDUR');
        $query = $this->db->group_by('A.regist_arisan');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data anggota menang arisan
    function getDataAnggotaMenang()
    {
        $query = $this->db->select('regist_arisan, count(A.regist_arisan) as anggota_menang');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->where('A.status', 'MENANG');
        $query = $this->db->group_by('A.regist_arisan');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua data anggota mundur arisan
    function getDataAnggotaMundur()
    {
        $query = $this->db->select('regist_arisan, count(A.regist_arisan) as anggota_mundur');
        $query = $this->db->from('p_arisan_anggota as A');
        $query = $this->db->where('A.status', 'MUNDUR');
        $query = $this->db->group_by('A.regist_arisan');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua anggota berdasarkan arisan
    function vd_anggotaAktifArisan($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan_anggota 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('regist_arisan', $regist_arisan);
        $query = $this->db->where('status', 'AKTIF');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua anggota berdasarkan arisan
    function vd_anggotaMundurArisan($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan_anggota 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('regist_arisan', $regist_arisan);
        $query = $this->db->where('status', 'MUNDUR');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan anggota arisan heboh Menang
    function vd_anggotaArisanMenang($regist_arisan)
    {
        $query = $this->db->select('*, A.id as kd_laporan');
        $query = $this->db->from('lap_arisan as A');
        $query = $this->db->join('lap_arisan_menang as B', 'A.id=B.laporan_id');
        $query = $this->db->join('p_arisan_anggota as D', 'B.regist_undian=D.regist_undian');
        $query = $this->db->join('anggota as E', 'D.anggota_id=E.id');
        $query = $this->db->where('A.regist_arisan', $regist_arisan);
        $query = $this->db->group_by('A.id');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan total tagihan arisan yang masuk
    function getDataRealisasiTagihan($regist_arisan)
    {
        $jadwal = $this->db->get_where('p_arisan_jadwal', ['regist_arisan' => $regist_arisan, 'status_undian' => 'BELUM'])->row_array();
        $query = $this->db->select('sum(bayar) as realisasi_bulan_ini');
        $query = $this->db->from('p_arisan_nota');
        $query = $this->db->where('jadwal_id', $jadwal['id']);
        $query = $this->db->where('status_bayar', 'LANCAR');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan saldo terakhir arisan heboh
    function getDataSaldoKelompok($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan');
        $query = $this->db->where('regist_arisan', $regist_arisan);
        $query = $this->db->order_by('jadwal_id', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data jadwal arisan sesuai dengan kode arisan
    function vd_jadwalArisan($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan_jadwal');
        $query = $this->db->where('regist_arisan', $regist_arisan);
        $query = $this->db->order_by('undian_ke', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan semua anggota berdasarkan arisan
    function vd_allDataAnggotaArisan($regist_arisan)
    {
        $query = $this->db->select('*, B.id as anggota_id');
        $query = $this->db->from('p_arisan_anggota 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('regist_arisan', $regist_arisan);
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan anggota arisan heboh Aktif
    function vd_allDataAnggotaArisanMotor($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan_anggota 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('regist_arisan', $regist_arisan);
        $query = $this->db->where('status !=', 'MUNDUR');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan Data Tagihan Anggota Yang Belum Bayar Bulan ini
    function vd_anggotaArisanMotorBelumBayar($jadwal_id, $regist_arisan)
    {
        $query = $this->db->query("Select * from p_arisan_anggota as A 
            Join anggota as B ON A.anggota_id = B.id 
            Join sistem_alamat as C ON B.id_alamat = C.id 
            Where A.regist_arisan=" . $regist_arisan . " 
            and A.status != 'MUNDUR' 
            and NOT EXISTS (Select * from p_arisan_nota as D 
            where jadwal_id=" . $jadwal_id . " 
            and D.regist_undian=A.regist_undian)");
        return $query->result();
    }
    // Menampilkan data tagihan anggota sudah bayar bulan ini
    function vd_anggotaArisanMotorSudahBayar($jadwal_id, $regist_arisan)
    {
        $query = $this->db->query("Select *, D.id as kd_nota, D.status_nota as nota_status from 
        p_arisan_anggota as A 
        Join anggota as B ON A.anggota_id = B.id 
        Join sistem_alamat as C ON B.id_alamat = C.id 
        Join p_arisan_nota as Z ON A.regist_undian = Z.regist_undian 
        join nota as D ON Z.nota_id=D.id
        Where A.regist_arisan=" . $regist_arisan . " and A.status != 'MUNDUR' and Z.jadwal_id =" . $jadwal_id . " and EXISTS (Select * from p_arisan_nota as D where jadwal_id=" . $jadwal_id . " and D.regist_undian=A.regist_undian)
        group by D.id");
        return $query->result();
    }
    // Menampilkan nomor urut invoice paling besar
    function getDataTagihanUrutMotor()
    {
        $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('produk_id', '22');
        $query = $this->db->order_by('id', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Mengambil data Invoice, Detail Invoice, Bayar Invoice, Keluarga Invoice
    function getDataInvoice($kd_invoice)
    {
        $query = $this->db->select('*, A.id as kd_invoice');
        $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->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_arisan_nota as B', 'B.nota_id=A.id');
        $query = $this->db->join('p_arisan_jadwal as C', 'B.jadwal_id=C.id');
        $query = $this->db->join('p_arisan as D', 'B.regist_arisan=D.id');
        $query = $this->db->join('p_arisan_anggota as E', 'B.regist_undian=E.regist_undian');
        $query = $this->db->where('A.id', $kd_invoice);
        $query = $this->db->get();
        return $query->result();
    }
    // Data Identitas Marketing
    function getDataMarketing($kd_invoice)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('nota as A');
        $query = $this->db->join('sistem_login as B', 'A.petugas=B.id');
        $query = $this->db->where('A.id', $kd_invoice);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Laporan Lelang Motor Bulan Kemarin
    function getDataLapLelangKemarin($regist_arisan, $jadwal_id_kemarin)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan');
        $query = $this->db->where('regist_arisan', $regist_arisan);
        $query = $this->db->where('jadwal_id', $jadwal_id_kemarin);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Mengambil 1 anggota lelang tertinggi
    function getData1LelangSekarang($jadwal_id)
    {
        $query = $this->db->select('MAX(total_lelang) as lelang_tertinggi');
        $query = $this->db->from('p_arisan_lelang_undian');
        $query = $this->db->where('jadwal_id', $jadwal_id);
        $query = $this->db->limit(1);
        $query = $this->db->order_by('total_lelang', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Mengambil 2 anggota lelang tertinggi
    function getData2LelangSekarang($jadwal_id)
    {
        $query = $this->db->select('MAX(total_lelang) as dua_lelang_tertinggi');
        $query = $this->db->from('p_arisan_lelang_undian');
        $query = $this->db->where('jadwal_id', $jadwal_id);
        $query = $this->db->limit(2);
        $query = $this->db->order_by('total_lelang', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data anggota lelang
    function getDataAnggotaLelang($jadwal_id)
    {
        $query = $this->db->select('*, A.id as lelang_id');
        $query = $this->db->from('p_arisan_lelang_undian as A');
        $query = $this->db->join('p_arisan_anggota as B', 'A.regist_undian=B.regist_undian');
        $query = $this->db->join('anggota as C', 'B.anggota_id=C.id');
        $query = $this->db->join('sistem_alamat as D', 'C.id_alamat=D.id');
        $query = $this->db->where('A.jadwal_id', $jadwal_id);
        $query = $this->db->order_by('A.total_lelang', 'DESC');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data anggota lelang
    function getDataSaldoLelang($jadwal_id)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan_lelang_undian as A');
        $query = $this->db->join('p_arisan_anggota as B', 'A.regist_undian=B.regist_undian');
        $query = $this->db->join('anggota as C', 'B.anggota_id=C.id');
        $query = $this->db->where('A.jadwal_id', $jadwal_id);
        $query = $this->db->order_by('A.total_lelang', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan Jumlah Iuran anggota lelang
    function getDataSaldoIuran($jadwal_id)
    {
        $query = $this->db->select('SUM(bayar) as jml_iuran');
        $query = $this->db->from('p_arisan_nota');
        $query = $this->db->where('jadwal_id', $jadwal_id);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan total iuran arisan
    function getTotalIuranArisan($jadwal_id)
    {
        $query = $this->db->select('sum(bayar) as ttl_iuran');
        $query = $this->db->from('p_arisan_nota');
        $query = $this->db->where('jadwal_id', $jadwal_id);
        $query = $this->db->where('status_bayar', 'LANCAR');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Rincian Laporan Arisan Motor
    function getDataRinciPemenangArisanMotorLaporan($kd_laporan_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan as A');
        $query = $this->db->join('lap_arisan_menang as B', 'A.id=B.laporan_id');
        $query = $this->db->join('p_arisan_anggota as C', 'B.regist_undian=C.regist_undian');
        $query = $this->db->join('anggota as D', 'C.anggota_id=D.id');
        $query = $this->db->where('A.id', $kd_laporan_arisan);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Menampilkan data total lelang
    function getDataTotalLelang($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan_menang as A');
        $query = $this->db->join('lap_arisan as B', 'A.laporan_id=B.id');
        $query = $this->db->where('B.regist_arisan', $regist_arisan);
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan anggota arisan Menang
    function getDataAnggotaArisan($regist_arisan)
    {
        $query = $this->db->select('*, A.id as kd_laporan');
        $query = $this->db->from('lap_arisan as A');
        $query = $this->db->join('lap_arisan_menang as B', 'A.id=B.laporan_id');
        $query = $this->db->join('p_arisan_anggota as D', 'B.regist_undian=D.regist_undian');
        $query = $this->db->join('anggota as E', 'D.anggota_id=E.id');
        $query = $this->db->where('A.regist_arisan', $regist_arisan);
        $query = $this->db->group_by('A.id');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan anggota arisan Mundur
    function getDataAnggotaArisanMundur($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('p_arisan_anggota 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('regist_arisan', $regist_arisan);
        $query = $this->db->where('status', 'MUNDUR');
        $query = $this->db->get();
        return $query->result();
    }
    // Menampilkan data laporan kemarin
    function getDataLapKemarin($regist_arisan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan');
        $query = $this->db->where('regist_arisan', $regist_arisan);
        $query = $this->db->where('status_laporan', 'VERIFIED');
        $query = $this->db->order_by('jadwal_id', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Laporan Pendapatan Arisan
    function getDataPendapatanArisanHeboh($regist_arisan)
    {
        $query = $this->db->select('SUM(administrasi)+sum(dn_kehilangan)+sum(fee_dealer) as admin');
        $query = $this->db->from('lap_arisan');
        $query = $this->db->where('regist_arisan', $regist_arisan);
        $query = $this->db->where('status_laporan', 'VERIFIED');
        $query = $this->db->get();
        return $query->row_array();
    }
    // Detail Data Laporan Arisan
    function getDataDetailLaporan($kd_laporan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan_menang as A');
        $query = $this->db->join('lap_arisan as B', 'A.laporan_id=B.id');
        $query = $this->db->join('p_arisan_anggota as C', 'A.regist_undian=C.regist_undian');
        $query = $this->db->join('anggota as D', 'C.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->where('A.laporan_id', $kd_laporan);
        $query = $this->db->get();
        return $query->row_array();
    }
    // Data Tunggakan Detail Laporan Arisan
    function getDataLaporanAnggotaTunggakan($kd_laporan)
    {
        $query = $this->db->select('*');
        $query = $this->db->from('lap_arisan_nunggak as A');
        $query = $this->db->join('lap_arisan as B', 'A.laporan_id=B.id');
        $query = $this->db->join('p_arisan_anggota as C', 'A.regist_undian=C.regist_undian');
        $query = $this->db->join('anggota as D', 'C.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->where('A.laporan_id', $kd_laporan);
        $query = $this->db->where('A.status_tunggakan', 'AKTIF');
        $query = $this->db->get();
        return $query->result();
    }
    // Data tunggakan bayar arisan
    function getDataLaporanAnggotaTunggakanBayar($kd_laporan)
    {
        $query = $this->db->select('*, F.nota_id as kd_tagihan, G.id as kd_nota');
        $query = $this->db->from('lap_arisan_nunggak as A');
        $query = $this->db->join('lap_arisan as B', 'A.laporan_id=B.id');
        $query = $this->db->join('p_arisan_anggota as C', 'A.regist_undian=C.regist_undian');
        $query = $this->db->join('anggota as D', 'C.anggota_id=D.id');
        $query = $this->db->join('sistem_alamat as E', 'D.id_alamat=E.id');
        $query = $this->db->join('p_arisan_nota as F', 'C.regist_undian=F.regist_undian');
        $query = $this->db->join('nota as G', 'G.id=F.nota_id');
        $query = $this->db->where('A.laporan_id', $kd_laporan);
        $query = $this->db->where('A.status_tunggakan', 'LUNAS');
        $query = $this->db->where('F.status_bayar', 'TUNGGAKAN');
        $query = $this->db->get();
        return $query->result();
    }
    // Data lelang nomer 2
    function vd_lelangNomer2($jadwal_id, $lelang_tertinggi)
    {
        $query = $this->db->select('MAX(total_lelang) as lelang_tertinggi_2, regist_undian');
        $query = $this->db->from('p_arisan_lelang_undian');
        $query = $this->db->where('jadwal_id', $jadwal_id);
        $query = $this->db->where('total_lelang !=', $lelang_tertinggi);
        $query = $this->db->limit(1);
        $query = $this->db->order_by('total_lelang', 'DESC');
        $query = $this->db->get();
        return $query->row_array();
    }
}
