<?php

/* Author : Ferry / Tanggal : 02-02-2015 */

class M_employee extends CI_Model {

    public $table = "am_employee";

    function get_all_data() {
        $this->db->select('*');
        $this->db->from($this->table);
        $query = $this->db->get();
        return $query->result();
    }

    // 11-11-2014, Ferry, Inner Join Company To Branch
    function join_all() {
        //default status untuk keluarin di list.
        $status = array(0, 1);

        /* ===========================declar post by find================================================ */
        $Code = $this->input->post('EmployeeCode') ? $this->input->post('EmployeeCode') : '';
        $Name = $this->input->post('EmployeeName') ? $this->input->post('EmployeeName') : '';

        $TransStatus = $this->input->post('TransStatus') ? $this->input->post('TransStatus') : '';
        $BranchCode = $this->input->post('BranchCode') ? $this->input->post('BranchCode') : '';
        $CompanyCode = $this->input->post('CompanyCode') ? $this->input->post('CompanyCode') : '';
        /* ========================================end================================================== */

        /* ======================================scrol view 1-150======================================= */
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================================================================== */

        /* ==========================================by session========================================== */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =============================================================================================== */

        $this->db->select("(case when am_employee.TransStatus = 0 then 'Active'
                                 when am_employee.TransStatus = 1 then 'Active'
                                 when am_employee.TransStatus = 3 then 'Resign' end) as Status, 
                            am_employee.EmployeeCode, am_employee.EmployeeName, am_employee.EmployeeAddress, 
                            am_employee.EmployeeCity, am_employee.EmployeeState, am_employee.EmployeeCountry, 
                            am_employee.TransStatus, am_employee.EmployeeCountry, am_employee.BranchCode, 
                            am_employee.DepartmentCode, am_employee.JobTitleCode, am_employee.EmployeeCity, 
                            am_employee.EmployeeState, am_city.CityName, am_state.StateName, 
                            am_country.CountryName,  am_branch.BranchName, am_department.DepartmentName, 
                            am_jobtitle.JobTitleName, am_company.CompanyName,");
        $this->db->from($this->table);
        $this->db->join('am_city', 'am_city.CityCode = am_employee.EmployeeCity', 'left');
        $this->db->join('am_state', 'am_state.StateCode = am_employee.EmployeeState', 'left');
        $this->db->join('am_country', 'am_country.CountryCode = am_employee.EmployeeCountry', 'left');
        $this->db->join('am_branch', 'am_branch.BranchCode = am_employee.BranchCode', 'inner');
        $this->db->join('am_department', 'am_department.DepartmentCode = am_employee.DepartmentCode', 'inner');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'inner');
        $this->db->join('am_jobtitle', 'am_jobtitle.JobTitleCode = am_employee.JobTitleCode', 'inner');

        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where('am_employee.BranchCode', $BranchCode);
        } else {
            /*
              inisialisasi untuk loading pertama page branch info
             */
            //----------------kondisi 1 untuk branch-------------------------------------//
            /*
              branch status 0 = HO
              branch status 1 = depo/branch
              jangan lupa sesion data di atas untuk setiap kondisi
              jangan lupa tambahin case when trans status
             */
            $this->db->where('am_employee.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }

        if ($Code != "") {
            $this->db->like('am_employee.EmployeeCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_employee.EmployeeName', $Name);
        }

        if ($TransStatus != '') {
            $this->db->where_in('am_employee.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_employee.TransStatus', $status);
        }

        $query = $this->db->order_by('am_employee.EmployeeCode', 'ASC');
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function get_all() {
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function get_detail($id) {
        $this->db->select('em.*, ct.CityName, st.StateName, cy.CountryName');
        $this->db->from($this->table . ' as em');
        $this->db->join('am_branch as br', 'br.BranchCode = em.BranchCode', 'INNER');
        $this->db->join('am_department as dp', 'dp.DepartmentCode = em.DepartmentCode', 'INNER');
        $this->db->join('am_jobtitle as jt', 'jt.JobTitleCode = em.JobTitleCode', 'INNER');
        $this->db->join('am_city as ct', 'ct.CityCode = em.EmployeeCity', 'INNER');
        $this->db->join('am_state as st', 'st.StateCode = em.EmployeeState', 'INNER');
        $this->db->join('am_country as cy', 'cy.CountryCode = em.EmployeeCountry', 'INNER');
        $this->db->where('em.EmployeeCode', $id);
        $query = $this->db->get();

        return $query->row();
    }

    function check_code_exist($id) {
        $query = $this->db->get_where($this->table, array('EmployeeCode' => $id), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function check_resign_date($employee_code) {
        $this->db->select('ResignDate');
        $this->db->from($this->table);
        $this->db->where('EmployeeCode', $employee_code);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $ResignDate = $row->ResignDate;
            }

            return $ResignDate;
        } else {
            return FALSE;
        }
    }

    function insert($data) {
        $this->db->insert($this->table, $data);
//		
//        if ($this->db->trans_status() === FALSE) {ga dipakai karena pakai array
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function update($data, $id) {
        $this->db->update($this->table, $data, array('EmployeeCode' => $id));
//		
//        if ($this->db->trans_status() === FALSE) {//ga dipakai karena sudah pakai array
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function delete($id) {
        $this->db->delete($this->table, array('EmployeeCode' => $id));

//        if ($this->db->trans_status() === FALSE) {//ga dipakai karena sudah pakai array
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function suspended($id) {
        $this->db->set('TransStatus', 3);
        $this->db->where('EmployeeCode', $id);
        $this->db->update($this->table);

//        if ($this->db->trans_status() === FALSE) {//ga dipakai karena pakai array
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function get_name_by_id($id) {
        $this->db->select('EmployeeName');
        $this->db->from($this->table);
        $this->db->where('EmployeeCode', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $EmployeeName = $row->EmployeeName;
            }

            return $EmployeeName;
        } else {
            return FALSE;
        }
    }

    //============= DHIDHIK, 28 Maret 2016 ===================================//
    function datasend_employee_insert($dataSend) {
        $this->db->trans_start();
        $this->insert($dataSend['data_employee']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function datasend_employee_update($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['data_employee'], $dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function datasend_employee_delete($dataSend) {
        $this->db->trans_start();
        $this->delete($dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function datasend_employee_suspend($dataSend) {
        $this->db->trans_start();
        $this->suspended($dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function get_all_to_print($data_filter) {
        $status = array(0, 1);
        /* ===========================declar post by find================================================ */
        $CompanyCode    = $data_filter['cmb_company'];
        $BranchCode     = $data_filter['cmb_branch'];
        $Code           = $data_filter['txt_no'];
        $Name           = $data_filter['txt_desc'];
        $TransStatus    = $data_filter['chk_TransStatus'];
        /* ========================================end================================================== */

        /* ==========================================by session========================================== */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =============================================================================================== */

        $this->db->select("(case when am_employee.TransStatus = 0 then 'Active'
                when am_employee.TransStatus = 1 then 'Active'
                when am_employee.TransStatus = 3 then 'Resign' end) as Status, 
           am_employee.EmployeeCode, am_employee.EmployeeName, am_employee.EmployeeAddress, 
           am_employee.EmployeeCity, am_employee.EmployeeState, am_employee.EmployeeCountry, 
           am_employee.TransStatus, am_employee.EmployeeCountry, am_employee.BranchCode, 
           am_employee.DepartmentCode, am_employee.JobTitleCode, am_employee.EmployeeCity, 
           am_employee.EmployeeState, am_city.CityName, am_state.StateName, 
           am_country.CountryName,  am_branch.BranchName, am_department.DepartmentName, 
           am_jobtitle.JobTitleName, am_company.CompanyName,");
        $this->db->from($this->table);
        $this->db->join('am_city', 'am_city.CityCode = am_employee.EmployeeCity', 'left');
        $this->db->join('am_state', 'am_state.StateCode = am_employee.EmployeeState', 'left');
        $this->db->join('am_country', 'am_country.CountryCode = am_employee.EmployeeCountry', 'left');
        $this->db->join('am_branch', 'am_branch.BranchCode = am_employee.BranchCode', 'inner');
        $this->db->join('am_department', 'am_department.DepartmentCode = am_employee.DepartmentCode', 'inner');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'inner');
        $this->db->join('am_jobtitle', 'am_jobtitle.JobTitleCode = am_employee.JobTitleCode', 'inner');

        if ($BranchCode != "") {
            $this->db->where('am_employee.BranchCode', $BranchCode);
        } else {
            $this->db->where('am_employee.BranchCode', $sess_branch);
        }

        if ($Code != "") {
            $this->db->like('am_employee.EmployeeCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_employee.EmployeeName', $Name);
        }

        if ($TransStatus != '') {
            $this->db->where_in('am_employee.TransStatus', $TransStatus);
        } else {
            $this->db->where_in('am_employee.TransStatus', $status);
        }

        $query = $this->db->order_by('am_employee.EmployeeCode', 'ASC');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    //============= End DHIDHIK, 28 Maret 2016 ===============================//
}
