<?php

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

class M_department extends CI_Model {

    public $table = 'am_department';

    function get_all_department() {
        $status = array(0, 1);

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

        $CompanyCode = ($this->input->post('CompanyCode')) ? $this->input->post('CompanyCode') : '';
        $TransStatus = ($this->input->post('TransStatus')) ? $this->input->post('TransStatus') : '';
        /* ========================================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("*");
        $this->db->from($this->table);

        /*
          get data dari  combobox find di view
         */
        if ($CompanyCode != "") {
            $this->db->where('am_department.CompanyCode', $CompanyCode);
        } else {
            /*
              inisialisasi untuk loading pertama page branch info
             */
            //----------------kondisi by company-------------------------------------//
            /*
              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_department.CompanyCode', $sess_company);
            //-----------------------end-------------------------------------------------//
        }

        if ($Code != "") {
            $this->db->like('am_department.DepartmentCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_department.DepartmentName', $Name);
        }

        if ($TransStatus != '') {
            $this->db->where_in('am_department.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_department.TransStatus', $status);
        }
        $query = $this->db->order_by('am_department.DepartmentCode', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }

    function count_department_number($CompanyCode) {
        $this->db->select('*');
        $this->db->from('am_department');
        $this->db->where('CompanyCode', $CompanyCode);

        return $this->db->get()->num_rows();
    }

    function get_last_department_number($CompanyCode) {
        $qty = "SELECT MAX(DepartmentCode) as DepartmentCode FROM am_department WHERE CompanyCode='$CompanyCode'";
        return $this->db->query($qty)->result();
    }

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

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

    function get_by_id($id) {
        $query = $this->db->get_where($this->table, array('DepartmentCode' => $id));
        return $query->result();
    }

    function insert($data) {
        $this->db->insert($this->table, $data);

//        if ($this->db->trans_status() === FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }        
    }

    function update($id, $data) {
        $this->db->where('DepartmentCode', $id);
        $this->db->update($this->table, $data);
//
//        if ($this->db->trans_status() === FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }        
    }

    function delete($id) {
        $this->db->where('DepartmentCode', $id);
        $this->db->delete($this->table);

        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
    }

    function get_all_data($CompanyCode) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('am_department.CompanyCode', $CompanyCode);
        $this->db->where('am_department.TransStatus', 1);
        $query = $this->db->get();

        return $query->result();
    }

    function create_department_code() {
        //Get Branch Code, Month, dan Year
        $branch = $this->session->userdata('branch');

        //Get Last Purchase Requisition Number
        $this->db->like('DepartmentCode', $branch);
        $num = $this->db->get($this->table)->num_rows() + 1;
        $long = strlen($num);

        //Create PReq Number
        if ($long == 1) {
            $number = '00' . $num;
        } elseif ($long == 2) {
            $number = '0' . $num;
        } else {
            $number = $num;
        }

        //Return PReq Serial + PReq Number
        return $branch . '-' . $number;
    }

    //============= DHIDHIK, 21 MARET 2016 ===================================//    
    function data_send_departement_insert($dataSend) {
        $this->db->trans_start();
        $this->insert($dataSend['data_departement_insert']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

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

    function data_send_departement_suspend($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['idcode'], $dataSend['data_dept_suspend']);
        $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'];
        $Code           = $data_filter['txt_department_code'];
        $Name           = $data_filter['txt_department_name'];
        $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("*");
        $this->db->from($this->table);

        /*
          get data dari  combobox find di view
         */
        if ($CompanyCode != "") {
            $this->db->where('am_department.CompanyCode', $CompanyCode);
        } else {
            /*
              inisialisasi untuk loading pertama page branch info
             */
            //----------------kondisi by company-------------------------------------//
            /*
              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_department.CompanyCode', $sess_company);
            //-----------------------end-------------------------------------------------//
        }

        if ($Code != "") {
            $this->db->like('am_department.DepartmentCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_department.DepartmentName', $Name);
        }

        if (!empty($TransStatus)) {
            $this->db->where_in('am_department.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_department.TransStatus', $status);
        }
        $query = $this->db->order_by('am_department.DepartmentCode', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }

    //============= END DHIDHIK, 21 MARET 2016 ===============================//
}

/* End of file m_department.php */
/* Location: ./application/models/m_department.php */