<?php

class M_Items extends CI_Model {

    // deklarasi nama table
    public $table = "am_item";

    // Function View	
    function view_all() {
        $this->db->select('*');
        $this->db->from($this->table);
        $query = $this->db->get();
        return $query;
    }

    function get_all_print_LAMA() {
        $this->db->select('am_item.ItemCode, am_item.ItemDesc, am_item.ItemCategoryID,
			am_item.ItemTypeID, am_item.ItemUnit as unit, am_item.TransStatus, am_itemcategory.ItemCategoryName, 
			am_itemtype.ItemTypeName');
        $this->db->from($this->table);
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        $this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');

        $query = $this->db->get();

        return $query->result();
    }

    function get_all_to_print($data_filter) { //default status untuk keluarin di list.
        $status = array(0, 1);
        /* ===========================declar post by find================================================ */
        $Code           = $data_filter['txt_IDNumber'];
        $Desc           = $data_filter['txt_Description'];
        $ItemCategoryID = $data_filter['cmb_category'];
        $ItemTypeID     = $data_filter['chk_ItemType'];
        $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_item.ItemCode, am_item.ItemDesc, am_item.ItemCategoryID,
            am_item.ItemTypeID, am_item.ItemUnit as unit, am_itemcategory.ItemCategoryName, 
            am_itemtype.ItemTypeName,
            (case 
                when am_item.TransStatus = 1 then 'On Progress'
                when am_item.TransStatus = 2 then 'Finished'
                when am_item.TransStatus = 3 then 'Suspend'
                when am_item.TransStatus = 4 then 'Need App. Mgr.'
                when am_item.TransStatus = 5 then 'Need App. Dir.'
                when am_item.TransStatus = 6 then 'Need App. Dir. & Mgr.'
                when am_item.TransStatus = 9 then 'Canceled by Dir.'
                else 'Unknown' end) as TransStatus"
        );
        $this->db->from($this->table);
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        $this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');

        if ($Code != "") {
            $this->db->like('am_item.ItemCode', $Code);
        }
        if ($Desc != "") {
            $this->db->like('am_item.ItemDesc', $Desc);
        }

        if ($ItemCategoryID != "") {
            if ($ItemCategoryID == 'All') {
                
            } else {
                $this->db->like('am_item.ItemCategoryID', $ItemCategoryID);
            }
        }

        if ($ItemTypeID != '') {
            $this->db->where_in('am_itemtype.ItemTypeID', $ItemTypeID);
        }

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

        $query = $this->db->ORDER_BY('TransStatus', 'ASC');
        $query = $this->db->ORDER_BY('ItemCode', 'ASC');
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function get_by_id($id) {
//        $this->db->select('
//			am_item.ItemCode, am_item.ItemDesc, am_item.ItemTypeID as Type, am_item.ItemUnit, am_item.FixedAssetTypeID, am_item.TransStatus, 
//			am_itemcategory.ItemCategoryID, am_itemcategory.ItemCategoryName, am_warehousetype.WHTypeID, am_warehousetype.WHTypeName, 
//			am_itemdetail.ItemMerk, am_itemdetail.ItemDesign, am_itemdetail.ItemColour, am_itemdetail.ItemSize, am_itemdetail.ItemQuality, am_itemdetail.ItemType, am_itemdetail.ItemShading, am_itemdetail.ItemGroup
//		');

        $this->db->select('am_itemcategory.ItemCategoryID, 
							am_itemcategory.ItemCategoryName, 
							am_item.TransStatus,
							am_item.ItemCode,
							am_item.ItemDesc,
							am_item.ItemUnit,
							am_itemtype.ItemTypeID,
							am_item.ItemPPhType,
							am_item.AppLevel1Status,
							am_item.AppLevel2Status,
							am_itemdetail.*');
        $this->db->from($this->table);
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        $this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');
        $this->db->join('am_itemdetail', 'am_itemdetail.ItemCode = am_item.ItemCode', 'LEFT');
        $this->db->where('am_item.ItemCode', $id);
        $query = $this->db->get();
        return $query->result();
    }

    function cek_status_app_dir($id) {
        $this->db->select('AppLevel2Status');
        $this->db->from('am_item');
        $this->db->where('ItemCode', $id);
        $this->db->where('AppLevel2Status', 1);
        $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_item');
        $this->db->where('ItemCode', $id);
        $this->db->where('AppLevel1Status', 1);
        $query = $this->db->get();

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

    function get_by_id_by_me($id) {
        $this->db->select('TransStatus');
        $query = $this->db->get_where($this->table, array($this->table . '.ItemCode' => $id));
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    function get_menulink($id) {
        $this->db->select('MenuLink');
        $this->db->from('tbl_menu');
        $this->db->where('MenuID', $id);
        $query = $this->db->get();

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

            return $MenuLink;
        } else {
            return FALSE;
        }
    }

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

        /* ===========================declar post by find================================================ */
        $param = $this->input->post('param');
        $Code = $this->input->post('ItemCode') ? $this->input->post('ItemCode') : '';
        $Desc = $this->input->post('ItemDesc') ? $this->input->post('ItemDesc') : '';

        $TransStatus = $this->input->post('TransStatus') ? $this->input->post('TransStatus') : '';
        $ItemCategoryID = $this->input->post('ItemCategoryName') ? $this->input->post('ItemCategoryName') : '';
        $ItemTypeID = $this->input->post('ItemTypeID') ? $this->input->post('ItemTypeID') : '';

        /* ========================================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_item.ItemCode, am_item.ItemDesc, am_item.ItemCategoryID,
			am_item.ItemTypeID, am_item.ItemUnit as unit, am_item.TransStatus, am_itemcategory.ItemCategoryName, 
			am_itemtype.ItemTypeName
		');
        $this->db->from($this->table);
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        $this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');

        if ($Code != "") {
            $this->db->like('am_item.ItemCode', $Code);
        }
        if ($Desc != "") {
            $this->db->like('am_item.ItemDesc', $Desc);
        }

        if ($ItemCategoryID != "") {
            if ($ItemCategoryID == 'All') {
                
            } else {
                $this->db->like('am_item.ItemCategoryID', $ItemCategoryID);
            }
        }

        if ($ItemTypeID != '') {
            $this->db->where_in('am_itemtype.ItemTypeID', $ItemTypeID);
        }

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

        $query = $this->db->ORDER_BY('TransStatus', 'ASC');
        $query = $this->db->ORDER_BY('ItemCode', 'ASC');
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

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

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

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

    // Function Update
    function update($data, $detail, $id) {
        $this->db->update($this->table, $data, array('ItemCode' => $id));
        if ($detail != "") {
            $this->db->update('am_itemdetail', $detail, array('ItemCode' => $id));
        }
    }

    // Function Delete	
    function delete($id) {
        $this->db->delete($this->table, array('ItemCode' => $id));
        $this->db->delete('am_itemdetail', array('ItemCode' => $id));
    }

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

    function check_item_service($id) {
        $this->db->select('ItemTypeID');
        $this->db->from($this->table);
        $this->db->where('ItemCode', $id);
        $query = $this->db->get();

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

            return $ItemTypeID;
        } else {
            return 0;
        }
    }

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

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

            return $ItemCategoryID;
        } else {
            return FALSE;
        }
    }

    /* =========================get item by  yang aktif dari kategori yg diberikan============================= */

    function get_item_by_categori($id) {
        $this->db->select($this->table . '.TransStatus');
        $this->db->from($this->table);
        $this->db->where($this->table . '.ItemCategoryID', $id);
        $this->db->where_in($this->table . '.TransStatus', array(0, 1));

        $query = $this->db->get();

        // 1 berarti ada data, 0 = berarti ga ada data.
        if ($query->num_rows() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /* ======================================================================================================================= */

    /* =========================get item by  yang aktif dari kategori yg diberikan============================= */

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

        // 1 berarti ada data, 0 = berarti ga ada data.
        if ($query->num_rows() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

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

        // 1 berarti ada data, 0 = berarti ga ada data.
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    /* ======================================================================================================================= */

    function get_all_item_on_transaction($id) {
        $this->db->select('av_item_trans_app.item_code, av_item_trans_app.no_transaksi, av_item_trans_app.trans_type, av_item_trans_app.company_code');
        $this->db->from('av_item_trans_app');
        $this->db->where('item_code', $id);
        $query = $this->db->get();

        // 1 berarti ada data, 0 = berarti ga ada data.
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    function get_all_item_on_transaction_by_categories($id, $company_code) {
        $this->db->select('av_item_trans_app.item_code, av_item_trans_app.no_transaksi, av_item_trans_app.trans_type, av_item_trans_app.company_code');
        $this->db->from('av_item_trans_app');
        $this->db->join('am_company', 'am_company.CompanyName = av_item_trans_app.company_code', 'inner');

        $this->db->where('av_item_trans_app.item_category', $id);
        $this->db->where('am_company.CompanyCode', $company_code);
        $query = $this->db->get();

        // 1 berarti ada data, 0 = berarti ga ada data.
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    /* =========================get item suspen yang aktif dari kategori yg diberikan============================= */

    function get_item_suspend_by_categori($id) {
        $this->db->select($this->table . '.TransStatus');
        $this->db->select_sum('at_inventories.ItemQty');
        $this->db->from($this->table);
        $this->db->join('at_inventories', 'at_inventories.ItemCode =' . $this->table . '.ItemCode', 'inner');

        $this->db->where($this->table . '.ItemCategoryID', $id);
        $this->db->group_by($this->table . '.ItemCode');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

    /* ======================================================================================================================= */

    /* =====================get initial kategori for item product=============================================== */

    function create_initial_code($item_kategori) {
        //Get Last Purchase Requisition Number
        $this->db->where($this->table . '.ItemCategoryID', $item_kategori);
        $num = $this->db->get($this->table)->num_rows() + 1;
        $long = strlen($num);

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

        //Return PReq Serial + PReq Number
        return $number;
    }

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

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

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

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

    //========== DHIDHIK, 22 Maret 2016 ======================================//
    /* ============================================end======================================================================= */
}
