<?php

class M_Fixed_Asset_List extends CI_Model {

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

    // Function Join
    function join_all() {
        /* ===========================declar post by find================================================ */
        $Code = $this->input->post('AssetCode') ? $this->input->post('AssetCode') : '';
        $Desc = $this->input->post('ItemDesc') ? $this->input->post('ItemDesc') : '';

        $BranchCode = $this->input->post('BranchCode') ? $this->input->post('BranchCode') : '';
        /* ========================================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('
			at_inventdetail.*, am_branch.BranchName,
			am_item.ItemDesc, am_item.ItemUnit, am_item.ItemTypeID, am_itemcategory.ItemCategoryID, am_itemcategory.ItemCategoryName,
                        
                , am_fixedassettype.EstimatedLife    
                , am_fixedassettype.DepriciateRate    
                , am_fixedassettype.DepMethodID      
        ');

        $this->db->from($this->table);
        $this->db->join('am_item', 'am_item.ItemCode = at_inventdetail.ItemCode', 'inner');
        //$this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');	
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        //$this->db->join('am_fixedassettype', 'am_fixedassettype.FixedAssetTypeID = am_item.FixedAssetTypeID', 'inner');
        $this->db->join('am_branch', 'am_branch.BranchCode = at_inventdetail.BranchCode', 'inner');
        $this->db->join("am_fixedassettype", "am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName", "inner");


        if ($Code != "") {
            $this->db->like('at_inventdetail.AssetCode', $Code);
        }
        if ($Desc != "") {
            $this->db->like('am_item.ItemDesc', $ItemDesc);
        }
        // HARUS TAMBAHIN SEARCH BARU, UNTUK TAMPILAN YANG SUDAH DIJUAL, DIKEMBALIKAN, ATAU DIBUANG

        if ($BranchCode != "") {
            $this->db->where('at_inventdetail.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('at_inventdetail.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }

        // QUERY BERDASARKAN SOLD STATUS ADA DI MANA? HANYA TAMPILKAN ITEM YANG MEMPUNYAI SOLD STATUS 0
        // 0 = inventories.
        // ADDED BY PRAWIRA TANGGAL 18 SEPTEMBER 2015. KALAU SUDAH PERBAIKI, TOLONG TULIS DI KOMEN DI BAWAH INI.
        // ***** PLACE TO ADD COMMENT *****
        //
		// ***** END **********************
        $this->db->where('am_item.TransStatus', 1);

        $query = $this->db->order_by('at_inventdetail.AssetCode', 'ASC');
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        $result = $query->result();

        return $result;
    }

    function join_all_by_branch($BranchCode) {
        $this->db->select('
			at_inventories.InventoriesID, at_inventories.BranchCode, am_branch.BranchName, at_inventories.ItemCode, at_inventories.ItemQty, 
			am_item.ItemDesc, am_item.ItemUnit, am_item.ItemTypeID, am_itemtype.ItemTypeName, am_itemcategory.ItemCategoryID, am_itemcategory.ItemCategoryName	
        ');

        $this->db->from($this->table);
        $this->db->join('am_item', 'am_item.ItemCode = at_inventories.ItemCode', 'inner');
        $this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        //$this->db->join('am_fixedassettype', 'am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName', 'inner');
        $this->db->join('am_branch', 'am_branch.BranchCode = at_inventories.BranchCode', 'inner');

        $this->db->where($this->table . '.BranchCode', $BranchCode);

        $this->db->where('am_item.TransStatus', 0);
        $this->db->where_not_in('am_item.ItemTypeID', array(2));
        $this->db->order_by("at_inventories.BranchCode", "ASC");
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function cmb_aset_type() {
        $this->db->select('*');

        $this->db->from('am_fixedassettype');

        $this->db->where('am_fixedassettype.TransStatus', 1);
        $this->db->order_by("am_fixedassettype.FixedAssetTypeID", "ASC");
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function cmb_categori() {
        $this->db->select('*');

        $this->db->from('am_itemcategory');

        $this->db->where('am_itemcategory.TransStatus', 0);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function get_edit_asset_list($id) {

        $query = $this->db->join('am_item', 'am_item.ItemCode = at_inventdetail.ItemCode', 'INNER');
        $query = $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'INNER');
        //$query = $this->db->join('am_department', 'am_department.DepartmentCode = at_inventdetail.DepartmentCode', 'inner');
        $query = $this->db->join('am_fixedassettype', 'am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName', 'INNER');
        $query = $this->db->get_where($this->table, array('IventDetailID' => $id));

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

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

    //============= DHIDHIK, 23 Maret 2016 ===================================//    
    function datasend_fixedassetlist_update($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['data_fixedassetlist_update'], $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) {
        /* ===========================declar post by find================================================ */
        $Code           = $data_filter['txt_asset_code'];
        $Desc           = $data_filter['txt_item_desc'];
        $BranchCode     = $data_filter['cmb_branch'];
        /* ========================================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('
			at_inventdetail.*, am_branch.BranchName,
			am_item.ItemDesc, am_item.ItemUnit, am_item.ItemTypeID, am_itemcategory.ItemCategoryID, am_itemcategory.ItemCategoryName,
                        
                , am_fixedassettype.EstimatedLife    
                , am_fixedassettype.DepriciateRate    
                , am_fixedassettype.DepMethodID      
        ');

        $this->db->from($this->table);
        $this->db->join('am_item', 'am_item.ItemCode = at_inventdetail.ItemCode', 'inner');
        //$this->db->join('am_itemtype', 'am_itemtype.ItemTypeID = am_item.ItemTypeID', 'inner');	
        $this->db->join('am_itemcategory', 'am_itemcategory.ItemCategoryID = am_item.ItemCategoryID', 'inner');
        //$this->db->join('am_fixedassettype', 'am_fixedassettype.FixedAssetTypeID = am_item.FixedAssetTypeID', 'inner');
        $this->db->join('am_branch', 'am_branch.BranchCode = at_inventdetail.BranchCode', 'inner');
        $this->db->join("am_fixedassettype", "am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName", "inner");


        if ($Code != "") {
            $this->db->like('at_inventdetail.AssetCode', $Code);
        }
        if ($Desc != "") {
            $this->db->like('am_item.ItemDesc', $Desc);
        }
        // HARUS TAMBAHIN SEARCH BARU, UNTUK TAMPILAN YANG SUDAH DIJUAL, DIKEMBALIKAN, ATAU DIBUANG

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

        $this->db->where('am_item.TransStatus', 1);

        $query = $this->db->order_by('at_inventdetail.AssetCode', 'ASC');
        $query = $this->db->get();
        $result = $query->result();

        return $result;
    }

    //============= END DHIDHIK, 23 Maret 2016 ===============================//
}
