<?php

class M_item_categories extends CI_Model {

    public $table = "am_itemcategory";

    function get_all() {
        //default status untuk keluarin di list.
        $status = array(0, 1);

        /* ===========================declar post by find================================================ */
        $Name = $this->input->post('ItemCategoryName') ? $this->input->post('ItemCategoryName') : '';
        $Desc = $this->input->post('ItemCategoryDesc') ? $this->input->post('ItemCategoryDesc') : '';

        $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("am_itemcategory.ItemCategoryID, am_itemcategory.ItemCategoryName, am_itemcategory.InitialCategory, 
							(case 
                            when am_itemcategory.TransStatus = 0 then 'Active' 
                            when am_itemcategory.TransStatus = 1 then 'Active' 
                            when am_itemcategory.TransStatus = 3 then 'Suspended'
							when am_itemcategory.TransStatus = 4 then 'Need App. Mgr.' 
							when am_itemcategory.TransStatus = 5 then 'Need App. Dir.'  end) as TransStatus,
							am_itemcategory.ItemCategoryDesc, am_itemcategory.TransStatus as Status");
        $this->db->from($this->table);

        if ($Name != "") {
            $this->db->like('am_itemcategory.ItemCategoryName', $Name);
        }
        if ($Desc != "") {
            $this->db->like('am_itemcategory.ItemCategoryDesc', $Desc);
        }

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

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

    function get_detail_row($id) {
        $query = $this->db->get_where($this->table, array('ItemCategoryID' => $id));
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

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

    function check_code_exist($id) {
        $query = $this->db->get_where($this->table, array('ItemCategoryName' => $id), 1, 0);
		$this->db->where_in('TransStatus', array(0,1,2,4,5,6));
		
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function check_code_initial_exist($id) {
        $query = $this->db->get_where($this->table, array('InitialCategory' => $id), 1, 0);
		$this->db->where_in('TransStatus', array(0,1,2,4,5,6));
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // Function Insert
    function insert($data) {
//        $this->db->trans_begin();
        $this->db->insert($this->table, $data);
    }

    function update($data, $id) {
//        $this->db->trans_begin();
        $this->db->update($this->table, $data, array('ItemCategoryID' => $id));
    }

    function update_from_aset($data, $name) {
//        $this->db->trans_begin();
        $this->db->update($this->table, $data, array('ItemCategoryName' => $name));
    }

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

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

    function suspended_by_name($name) {
//        $this->db->trans_begin();//sudah ga dipake ye,,,
        $this->db->set('TransStatus', 3);
        $this->db->where('ItemCategoryName', $name);
        $this->db->update($this->table);
    }

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

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

            return $ItemCategoryName;
        } else {
            return FALSE;
        }
    }

    function cek_status_app_dir($id) {
        $this->db->select('AppLevel2Status');
        $this->db->from('am_itemcategory');
        $this->db->where('ItemCategoryID', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel2Status = $row->AppLevel2Status;
            }
            return $AppLevel2Status;
        } else {
            return FALSE;
        }
    }

    function cek_status_app_mgr($id) {
        $this->db->select('AppLevel1Status');
        $this->db->from('am_itemcategory');
        $this->db->where('ItemCategoryID', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel1Status = $row->AppLevel1Status;
            }
            return $AppLevel1Status;
        } else {
            return FALSE;
        }
    }

    function update_trans($data, $id) {
        $this->db->update('am_itemcategory', $data, array('ItemCategoryID' => $id));
    }

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

    function data_send_itemcategory_update($dataSend) {
        $this->db->trans_start();

        foreach ($dataSend['data_asset_update'] as $key) {
            $this->m_fixed_asset_type->update_from_category($key, $dataSend['kategori_name']);
        }
        $this->m_item_categories->update($dataSend['data_itemcategory_update'], $dataSend['idcode']);

        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

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

    function data_send_itemcategory_suspend($dataSend) {
        $this->db->trans_start();

        foreach ($dataSend['data_sys_item_delete'] as $key) {
            $this->m_preference->delete_sys_item($key['idcode'], $key['SysItemCategoryID']);
            ;
        }
        foreach ($dataSend['data_sys_sales_delete'] as $key) {
            $this->m_preference->delete_sys_sales($key['idcode'], $key['SysSalesCategoryID']);
            ;
        }
        foreach ($dataSend['data_sys_purchase_delete'] as $key) {
            $this->m_preference->delete_sys_purchase($key['idcode'], $key['SysPurchaseCategoryID']);
            ;
        }
        $this->m_fixed_asset_type->suspended_by_name($dataSend['byname']);
        $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) {
        //default status untuk keluarin di list.
        /* ===========================declar post by find================================================ */        
        $Name           = $data_filter['txt_no'];
        $Desc           = $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("am_itemcategory.ItemCategoryID, am_itemcategory.ItemCategoryName, am_itemcategory.InitialCategory, 
                (case 
                    when am_itemcategory.TransStatus = 0 then 'Active' 
                    when am_itemcategory.TransStatus = 1 then 'Active' 
                    when am_itemcategory.TransStatus = 3 then 'Suspended'
                    when am_itemcategory.TransStatus = 4 then 'Need App. Mgr.' 
                    when am_itemcategory.TransStatus = 5 then 'Need App. Dir.'  end) as TransStatus,
                    am_itemcategory.ItemCategoryDesc, am_itemcategory.TransStatus as Status");
        $this->db->from($this->table);

        if ($Name != "") {
            $this->db->like('am_itemcategory.ItemCategoryName', $Name);
        }
        if ($Desc != "") {
            $this->db->like('am_itemcategory.ItemCategoryDesc', $Desc);
        }

        if (!empty($TransStatus)) {
            $this->db->where_in('am_itemcategory.TransStatus', $TransStatus);
        } else {
            $this->db->where_in('am_itemcategory.TransStatus', array(0,1));
        }

        $query = $this->db->order_by('am_itemcategory.TransStatus', 'ASC');
        $query = $this->db->order_by('am_itemcategory.ItemCategoryName', 'ASC');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

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