<?php

class M_Fixed_Asset_Type extends CI_Model {

    public $table = "am_fixedassettype";

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

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

        $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("(case 
                                when am_fixedassettype.DepMethodID=1 then 'Straight Line Method'
                                when am_fixedassettype.DepMethodID=2 then 'Double Declining Method'
                                else 'Non Depreciable Method' end) as depreciate_method,	
                            am_fixedassettype.FixedAssetTypeID, am_fixedassettype.FixedAssetType, am_fixedassettype.FixedAssetDesc, 
                            am_fixedassettype.DepMethodID, am_fixedassettype.EstimatedLife, am_fixedassettype.DepriciateRate, am_fixedassettype.TransStatus");
        $this->db->from($this->table);

        if ($Code != "") {
            $this->db->like('am_fixedassettype.FixedAssetType', $Code);
        }
        if ($Desc != "") {
            $this->db->like('am_fixedassettype.FixedAssetDesc', $Desc);
        }

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

        $query = $this->db->order_by('am_fixedassettype.TransStatus', 'ASC');
        $query = $this->db->order_by('am_fixedassettype.FixedAssetType', 'ASC');
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        $result = $query->result();
        return $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_code'];
        $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("(case 
                when am_fixedassettype.DepMethodID=1 then 'Straight Line Method'
                when am_fixedassettype.DepMethodID=2 then 'Double Declining Method'
                else 'Non Depreciable Method' end) as depreciate_method,	
            am_fixedassettype.FixedAssetTypeID, am_fixedassettype.FixedAssetType, am_fixedassettype.FixedAssetDesc, 
            am_fixedassettype.DepMethodID, am_fixedassettype.EstimatedLife, am_fixedassettype.DepriciateRate, 
            (case 
                when am_fixedassettype.TransStatus = 0 then 'Waiting' 
                when am_fixedassettype.TransStatus = 1 then 'On Progress' 
                when am_fixedassettype.TransStatus = 2 then 'Active' 
                when am_fixedassettype.TransStatus = 3 then 'Suspended' 
                when am_fixedassettype.TransStatus = 4 then 'Need Mgr. App.' 
                when am_fixedassettype.TransStatus = 5 then 'Need Dir. App.' 
                when am_fixedassettype.TransStatus = 6 then 'Need Dir & Mgr App.' end) as Status
            ");
        $this->db->from($this->table);

        if ($Code != "") {
            $this->db->like('am_fixedassettype.FixedAssetType', $Code);
        }
        if ($Desc != "") {
            $this->db->like('am_fixedassettype.FixedAssetDesc', $Desc);
        }

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

        $query = $this->db->order_by('am_fixedassettype.TransStatus', 'ASC');
        $query = $this->db->order_by('am_fixedassettype.FixedAssetType', 'ASC');
        $query = $this->db->get();
//        echo $this->db->last_query();
        $result = $query->result();
        return $result;
    }

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

    function get_by_id_for_suspend($id) {
        $query = $this->db->get_where($this->table, array('FixedAssetTypeID' => $id));
        return $query->row();
    }

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

    function get_by_name($name) {
        $query = $this->db->get_where($this->table, array('FixedAssetType' => $name));
        return $query->result();
    }

    function check_code_exist($id) {
        $this->db->select('FixedAssetType');
        $this->db->from($this->table);
        $this->db->where('FixedAssetType', $id);
		$this->db->where_in('TransStatus', array(0,1,2,4,5,6));
        $query = $this->db->get();

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

    function check_code_initial_exist($id) {
         $this->db->select('InitialCategory');
        $this->db->from($this->table);
        $this->db->where('InitialCategory', $id);
		$this->db->where_in('TransStatus', array(0,1,2,4,5,6));
        $query = $this->db->get();

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

    function insert($data) {
//        $this->db->trans_begin();//ga di pakai lagi ye,,
        $this->db->insert($this->table, $data);
    }

    function update($data, $id) {
//        $this->db->trans_begin();//sudah ga dipake ye,,
        $this->db->update($this->table, $data, array('FixedAssetTypeID' => $id));
    }

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

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

    function suspended($id) {
//        $this->db->trans_begin();//sudah ga dipakae ye
        $this->db->set('TransStatus', 3);
        $this->db->where('FixedAssetTypeID', $id);
        $this->db->update($this->table);
    }

    function suspended_by_name($name) {
//        $this->db->trans_begin();//sudah ga dipakai karena pakai sistem array
        $this->db->set('TransStatus', 3);
        $this->db->where('FixedAssetType', $name);
        $this->db->update($this->table);
    }

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

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

            return $FixedAssetType;
        } else {
            return FALSE;
        }
    }

    function check_as_assets($name) {
        $query = $this->db->get_where($this->table, array('FixedAssetType' => $name), 1, 0);

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

    function get_inven_detail($id, $BranchCode) {
        $this->db->select('PaymentAmount,PaymentPeriod, ItemAverage');
        $this->db->from('at_inventdetail');
        //$this->db->join('am_item','am_item.ItemCategoryID=at_inventdetail.ItemCategoryID','INNER');

        $this->db->where('ItemCategoryID', $id);
        $this->db->where('BranchCode', $BranchCode);
        //$this->db->where('(PaymentAmount - PaymentPeriod)', 0);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
	
	/*
		Created By Fachri
	*/
	function get_fix_asset_type_journal($ItemCategoryID) {
        $this->db->select('am_fixedassettype.AccumAccountNumber');
        $this->db->from('am_fixedassettype');
		$this->db->join('am_itemcategory', "am_itemcategory.ItemCategoryName=am_fixedassettype.FixedAssetType", "INNER");
		
        $this->db->where('am_itemcategory.ItemCategoryID', $ItemCategoryID);
        $this->db->where('am_fixedassettype.TransStatus', 1);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
	/*========================END=========================*/
	
    function get_akun_penyusutan() {
        $branch = $this->session->userdata("branch");
        $company = $this->session->userdata("company");

        $this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName');
        $this->db->from('am_accountgl');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber', 'INNER');
        $this->db->join('am_branch', "am_branch.BranchCode=at_compaccountgl.BranchCode", "INNER");
        $this->db->join('am_company', "am_company.CompanyCode=am_branch.CompanyCode", "INNER");
        $this->db->where('(case when am_branch.BranchStatus = 0 then at_compaccountgl.PLStatus in (0, 1) else at_compaccountgl.PLStatus = 1 end)');
        $this->db->where('am_branch.BranchCode', $branch);
        $this->db->where('am_company.CompanyCode', $company);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('am_accountgl.AccountSubTypeID', 3);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);
        $query = $this->db->get();

        $result = $query->result();
		return ($result);
    }

    function get_biaya_penyusutan() {
        $branch = $this->session->userdata("branch");
        $company = $this->session->userdata("company");

        $this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName');
        $this->db->from('am_accountgl');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber', 'INNER');
        $this->db->join('am_branch', "am_branch.BranchCode=at_compaccountgl.BranchCode", "INNER");
        $this->db->join('am_company', "am_company.CompanyCode=am_branch.CompanyCode", "INNER");
        $this->db->where('(case when am_branch.BranchStatus = 0 then at_compaccountgl.PLStatus in (0, 1) else at_compaccountgl.PLStatus = 1 end)');
        $this->db->where('am_branch.BranchCode', $branch);
        $this->db->where('am_company.CompanyCode', $company);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->like('am_accountgl.AccountName', 'Biaya Penyusutan');
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);
        $query = $this->db->get();

        $result = $query->result();
        return ($result);
    }

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

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

        $this->m_item_categories->insert($dataSend['data_category']);
        $this->m_fixed_asset_type->update($dataSend['data_fixedassettype_app'], $dataSend['idcode']);

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

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

        foreach ($dataSend['data_category_update'] as $key) {
            $data_update = array(
                'ItemCategoryName' => $key['ItemCategoryName'],
                'ItemCategoryDesc' => $key['ItemCategoryDesc'],
                'InitialCategory' => $key['InitialCategory'],
                'LastUser' => $key['LastUser']
            );
            $this->m_item_categories->update_from_aset($data_update, $key['idcode']);
        }
        $this->m_fixed_asset_type->update($dataSend['data_fixedassettype_update'], $dataSend['idcode']);

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

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

        $this->m_item_categories->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;
    }

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