<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

        class absen_model extends CI_Model {

  var $order = array('id' => 'desc'); // default order moo
            public function __construct() {
                parent::__construct();
                $this->load->database();
            }

function absensi() {
  
$menus='';
$level=$this->session->userdata('LEVEL');
$id_kar=$this->session->userdata('ID_KAR');
if($level==1 ){
$addTag="where karyawan.id_kar='".$id_kar."'";
}else{
  $addTag="";
}
            $query=$this->db->query("select absen . * , karyawan . *
                                    FROM karyawan
                                     JOIN absen ON karyawan.id_kar = absen.id_kar $addTag order by absen.tgl desc
                                  
                 ");
                   if ($query->num_rows() > 0) {
                        foreach ($query->result() as $data) {
                              $menus[]=$data;
                        }
                        return $menus;
                  }
}
function absensidpn() {
 $menus='';
            $query=$this->db->query("select absen . * , karyawan . *
                                    FROM karyawan
                                    LEFT JOIN absen ON karyawan.id_kar = absen.id_kar
                                    AND tgl = curdate( ) 
                 ");
                   if ($query->num_rows() > 0) {
                        foreach ($query->result() as $data) {
                              $menus[]=$data;
                        }
                        return $menus;
                  }
            
}
function pilih($id_ab){
  $this->db->where('id_ab', $id_ab);
  return $query=$this->db->get('absen')->row();

} 
function delete($id_ab) {
 $this->db->where('id_ab', $id_ab);
 $this->db->delete('absen');
} 
function update($id_ab,$data){
  $this->db->where('id_ab', $id_ab);
  $this->db->update('absen', $data);
}
function jammasuk($data){
  $this->db->insert('absen', $data);
}
function jammkeluar($id_kar,$tgl,$data){
  $this->db->where('tgl', $tgl);
  $this->db->where('id_kar', $id_kar);
  $this->db->update('absen', $data); 
}
function save($tgl,$id_kar,$data){
  $this->db->where('tgl', $tgl);
  $this->db->where('id_kar', $id_kar);
  $this->db->from('absen');
  $query = $this->db->get();
   $rowcount = $query->num_rows();

if ($rowcount==0) {

    $this->db->insert('absen', $data);  
  }
 else {
    $this->db->where('tgl', $tgl);
  $this->db->where('id_kar', $id_kar);
                $this->db->update('absen', $data);  
  }
        }       
}