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

class Quick_setup_kepala_departemen_m extends CI_Model{
  var $table = 'data_karyawan';
  public function __construct(){
  parent::__construct();

      $this->level_user           = $this->session->userdata('level_user');
      $this->id_company           = $this->session->userdata('id_company');
      $this->multi_cabang         = $this->session->userdata('multi_cabang');
      $this->id_cabang            = $this->session->userdata('id_cabang');
      $this->id_cabang_arr        = $this->session->userdata('id_cabang_arr');
      $this->multi_departemen     = $this->session->userdata('multi_departemen');
      $this->id_departemen        = $this->session->userdata('id_departemen');
      $this->id_departemen_arr    = $this->session->userdata('id_departemen_arr');
      $this->id_karyawan          = $this->session->userdata('id_karyawan');
}

  function get_datatables(){
      $this->_get_datatables_query();
      if($_POST['length'] != -1){
          $this->db->limit($_POST['length'], $_POST['start']);
      }
      $query = $this->db->get();
      return $query->result();
  }

  function count_filtered(){
      $this->_get_datatables_query();
      $query = $this->db->get();
      return $query->num_rows();
  }

  public function count_all(){
      $this->db->select("nik")->from($this->table);
      $this->db->join("master_cabang", "data_karyawan.id_cabang = master_cabang.id_cabang");
      $this->db->join("master_jabatan", "master_jabatan.id_jabatan = data_karyawan.id_jabatan");
      $this->db->where("data_karyawan.status", "2");
      $this->db->where("master_jabatan.level_user", 2);
      $this->db->where("master_cabang.id_company",$this->id_company);
      return $this->db->count_all_results();
  }
  private function _get_datatables_query(){
      $this->db->select("nik, id_karyawan, nama_lengkap, master_cabang.kode as kode_cabang, master_cabang.nama as cabang,
        master_departemen.kode as kode_departemen, master_departemen.nama as departemen")->from($this->table);
      $this->db->join("master_departemen", "master_departemen.id_departemen = data_karyawan.id_departemen");
      $this->db->join("master_cabang", "data_karyawan.id_cabang = master_cabang.id_cabang");
      $this->db->join("master_jabatan", "master_jabatan.id_jabatan = data_karyawan.id_jabatan");
      $this->db->where("data_karyawan.status", "2");
      $this->db->where("master_jabatan.level_user", 2);
      $this->db->where("master_cabang.id_company",$this->id_company);

      $column_search = array('nik','nama_lengkap','master_cabang.nama','master_departemen.nama');
      $i = 0;

      foreach ($column_search as $item){  // looping awal
          if($_POST['search']['value']){ // jika datatable mengirimkan pencarian dengan metode POST

              if($i===0){ // looping awal
                  $this->db->group_start();
                  $this->db->like($item, $_POST['search']['value']);
              }else{
                  $this->db->or_like($item, $_POST['search']['value']);
              }

              if(count($column_search) - 1 == $i){
                  $this->db->group_end();
              }
          }
          $i++;
      }

      $column_order = array('nik','nama_lengkap','master_cabang.nama','master_departemen.nama');

      if(isset($_POST['order'])){
          $this->db->order_by($column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
      }
      else{
          $order_by = array("data_karyawan.tgl_input"=>"DESC");
          $this->db->order_by(key($order_by), $order_by[key($order_by)]);
      }
  }

  function detail($id_karyawan) {
    $this->db->select('nik,nama_lengkap, data_karyawan.id_cabang, master_cabang.kode, master_cabang.nama as cabang,
      users.username, data_karyawan.id_departemen, master_departemen.kode as kode_departemen,
      master_departemen.nama as departemen');
    $this->db->join('master_cabang', 'master_cabang.id_cabang = data_karyawan.id_cabang');
    $this->db->join('master_departemen', 'master_departemen.id_departemen = data_karyawan.id_departemen');
    $this->db->join('users', 'users.id_karyawan = data_karyawan.id_karyawan');
    $this->db->where('data_karyawan.id_karyawan', $id_karyawan);
    return $this->db->get($this->table)->row();
  }

  function cek_jabatan_kepala_departemen() {
    $this->db->order_by('id_jabatan', 'asc');
    return $this->db->get_where('master_jabatan', array('level_user' => 2, 'id_company' => $this->id_company));
  }

  function create_jabatan_kepala_departemen() {
    $id_jabatan = $this->fungsi->getNextId('master_jabatan','id_jabatan',$this->id_company,3);
    $data = array(
      'id_jabatan'    => $id_jabatan,
      'kode_jabatan'  => 'K_DEP',
      'nama'          => 'Kepala Departemen',
      'level_user'    => 2,
      'id_company'    => $this->id_company
    );
    $this->db->insert('master_jabatan', $data);
    return $id_jabatan;
  }

  function update($data, $data_user, $id_karyawan) {
    $departemen_temp = $this->input->post('departemen_temp');

    $this->db->trans_start();
    $this->db->update($this->table, $data, array('id_karyawan' => $id_karyawan));
    $this->db->update('users', $data_user, array('id_karyawan' => $id_karyawan));
    if($departemen_temp != $data['id_departemen']) {
        $this->db->delete('p_kedep', array('id_karyawan' => $id_karyawan));
        # INSERT KEDEP BARU
        $id_kedep = $this->fungsi->getNextId('p_kedep', 'id', $this->id_company, 3);
        $data_kedep = array(
            "id"            => $id_kedep,
            "id_karyawan"   => $id_karyawan,
            "id_cabang"     => $data["id_cabang"],
            "id_departemen" => $data["id_departemen"],
            "flag"          => "1",
            "id_company"    => $this->id_company
        );
        //insert pengaturan kacab jika tidak kosong
        $this->db->insert('p_kedep', $data_kedep);
    }

    $this->db->trans_complete();

    if($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        return __response_update(false);
    }else{
        $this->db->trans_commit();
        return __response_update(true);
    }
  }

  function delete($id_karyawan) {
    $this->db->trans_start();

    $this->db->where(array('id_karyawan'=>$id_karyawan));
    $this->db->delete('data_karyawan');

    $this->db->where(array('id_karyawan'=>$id_karyawan));
    $this->db->delete('users');

    $this->db->delete('p_kedep', array('id_karyawan' => $id_karyawan));

    $this->db->trans_complete();

    if($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        return __response_delete(false);
    }else{
        $this->db->trans_commit();
        return __response_delete(true);
    }
  }
}
