<?php

class AbsensiModel extends CI_Model
{
    private $_table = "tb_absensi";


    public function getAbsensiByAbsenId($id_absen){
        $this->db->where('id_absen', $id_absen);
        return  $absensi = $this->db->get($this->_table)->result();
    }

    public function getAbsensiByAbsenIdAndUmkmId($id_umkm,$id_absen){
        $this->db->where('id_absen', $id_absen);
        $this->db->where('id_umkm', $id_umkm);
        return  $absensi = $this->db->get($this->_table)->row();
    }

    public function update($id_absensi){
        $post = $this->input->post();
        $this->aset = $post["aset"];
        $this->omset =  $post["omset"];
        $this->karyawan_l = $post["karyawan_l"];
        $this->karyawan_p = $post["karyawan_p"];
        $this->signed = 1;
        return $this->db->update($this->_table, $this, array('id_absensi' => $id_absensi));
    }

    public function getLastMonthAbsensi($bulan,$tahun){
        $bulan = $bulan-1;
        $this->db->where('bulan', $bulan);
        $this->db->where('tahun', $tahun);
        return  $absensi = $this->db->get($this->_table)->result();
    }
    
    public function getLatestAbsensi($bulan,$tahun,$id_umkm){
        $bulan = $bulan-1;
        $this->db->where('bulan', $bulan);
        $this->db->where('tahun', $tahun);
        $this->db->where('id_umkm', $id_umkm);
        return  $absensi = $this->db->get($this->_table)->row();
    }


    public function getAbsensiCountByUmkmId($id_umkm){
        $this->db->where('id_umkm', $id_umkm);
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function getAbsensiCountByAbsenId($id_absen){
        $this->db->where('id_absen', $id_absen);
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function getUnsignedAbsensiCountByAbsenId($id_absen){
        $this->db->where('signed', false);
        $this->db->where('id_absen', $id_absen);
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function getSignedAbsensiCountByAbsenId($id_absen){
        $this->db->where('signed', true);
        $this->db->where('id_absen', $id_absen);
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function getLatestSignedAbsensiCountByUmkmId($id_umkm){
        $this->db->where('signed', true);
        $this->db->where('id_umkm', $id_umkm);
        $this->db->where('tahun', date('Y'));
        $this->db->order_by('bulan', 'DESC');
        return  $absensi = count($this->db->get($this->_table)->result());
    }

    public function getLatestSignedAbsensiByUmkmId($id_umkm){
        $this->db->where('signed', true);
        $this->db->where('id_umkm', $id_umkm);
        $this->db->where('tahun', date('Y'));
        $this->db->order_by('bulan', 'DESC');
        return  $absensi = $this->db->get($this->_table)->row();
    }

    public function getSignedAbsensiCountByUmkmId($id_umkm){
        $this->db->where('signed', true);
        $this->db->where('id_umkm', $id_umkm);
        $this->db->order_by('bulan', 'DESC');
        $this->db->order_by('tahun', 'DESC');
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function getSignedAbsensiByUmkmId($id_umkm){
        $this->db->where('signed', true);
        $this->db->where('id_umkm', $id_umkm);
        $this->db->where('tahun', date('Y'));
        $this->db->order_by('bulan', 'DESC');
        $this->db->order_by('tahun', 'DESC');
        return  $user = $this->db->get($this->_table)->result();
    }

    public function getUnsignedAbsensiByAbsenId($id_absen){
        $this->db->join('tb_umkm','tb_umkm.id_umkm = tb_absensi.id_umkm');
        $this->db->where('signed', false);
        $this->db->where('id_absen', $id_absen);
        return  $absensi = $this->db->get($this->_table)->result();
    }

    public function getSignedAbsensiByAbsenId($id_absen){
        $this->db->join('tb_umkm','tb_umkm.id_umkm = tb_absensi.id_umkm');
        $this->db->where('signed', true);
        $this->db->where('id_absen', $id_absen);
        return  $absensi = $this->db->get($this->_table)->result();
    }

    public function save($id_absen,$id_umkm,$id_pemilik,$kecamatan,$kelurahan,$bulan_absen,$tahun_absen,$omset_absensi,$aset_absensi,$karyawan_absensi_l,$karyawan_absensi_p)
    {
        $this->id_absen = $id_absen;
        $this->id_umkm = $id_umkm;
        $this->id_pemilik = $id_pemilik;
        $this->kecamatan = $kecamatan;
        $this->kelurahan = $kelurahan;
        $this->bulan = $bulan_absen;
        $this->tahun = $tahun_absen;
        $this->karyawan_l = $karyawan_absensi_l;
        $this->karyawan_p = $karyawan_absensi_p;
        $this->omset = $omset_absensi;
        $this->aset = $aset_absensi;
        $this->signed = false;

        return $this->db->insert($this->_table, $this);
    }

    public function delete($id_absensi)
    {
        return $this->db->delete($this->_table, array("id_absensi" => $id_absensi));
    }


}