<?php

class AbsenModel extends CI_Model
{
    private $_table = "tb_absen";

    public function getAbsens(){
        //$this->db->order_by('status_absen', 'DESC');
        $this->db->order_by('bulan', 'ASC');
        return  $absen = $this->db->get($this->_table)->result();
    }

    public function getAbsensByTahun($tahun){
        //$this->db->order_by('status_absen', 'DESC');
        $this->db->where('tahun', $tahun);
        $this->db->order_by('bulan', 'ASC');
        return  $absen = $this->db->get($this->_table)->result();
    }

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

    public function getAbsenTahun(){
        $this->db->select('tahun');
        $this->db->distinct('tahun');
        return  $tahun = $this->db->get($this->_table)->result();
    }

    public function getCountActiveAbsen(){
        $this->db->where('status_absen', 'open');
        return  $absen = count($this->db->get($this->_table)->result());
    }

    public function getActiveAbsen(){
        $this->db->where('status_absen', 'open');
        return  $absen = $this->db->get($this->_table)->row();
    }

    public function getAbsenCountByBulanAndYear($bulan,$tahun){
        $this->db->where('tahun',$tahun);
        $this->db->where('bulan',$bulan);
        return  $absen = count($this->db->get($this->_table)->result());
    }

    public function getLastMonthAbsenCountByBulanAndYear($bulan,$tahun){
        $bulan = $bulan-1;
        $this->db->where('tahun',$tahun);
        $this->db->where('bulan',$bulan);
        return  $absen = count($this->db->get($this->_table)->result());
    }

    public function getAbsenCountByYear($tahun){
        $this->db->where('tahun',$tahun);
        return  $absen = count($this->db->get($this->_table)->result());
    }

    public function getAbsenByBulanAndYear($bulan,$tahun){
        $this->db->where('tahun',$tahun);
        $this->db->where('bulan',$bulan);
        return  $absen = $this->db->get($this->_table)->row();
    }

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

    public function getOpenAbsenCount(){
        $this->db->where('status_absen', 'open');
        return  $absen = count($this->db->get($this->_table)->result());
    }

    public function getOpenAbsen(){
        $this->db->where('status_absen', 'open');
        return  $absen = $this->db->get($this->_table)->result();
    }

    public function setCloseAbsen($id_absen)
    {
        $this->status_absen = "close";
        return $this->db->update($this->_table, $this, array('id_absen' => $id_absen));
    }

    public function setOpenAbsen($id_absen)
    {
        $this->status_absen = "open";
        return $this->db->update($this->_table, $this, array('id_absen' => $id_absen));
    }

    

    public function save($jumlahumkm,$omset,$aset,$karyawan_l,$karyawan_p)
    {
        $post = $this->input->post();
        $this->bulan = $post["bulan"];
        $this->tahun = $post["tahun"];
        $this->jumlah_umkm = $jumlahumkm;
        $this->omset = $omset;
        $this->aset = $aset;
        $this->karyawan_l = $karyawan_l;
        $this->karyawan_p = $karyawan_p;
        $this->status_absen = 'close';

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

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

}