<?php

/* 	
 * 	Author 	: Dhidhik
 * 	Tanggal : 24-11-2015	
 * 	Revisi 	: 
 */

class M_index_fixed_asset extends CI_Model {    
    public $tbl_              = "";
    
    //get all fixed asset
    public function pre_get_all_fixed_asset_list() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $Departement       = ($this->input->post('txt_departement')!=null)?$this->input->post('txt_departement'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $Pages          = ($this->input->post('pages')!=null)?$this->input->post('pages'):'';
        $limitStart     = ($Pages['limit'] * $Pages['pages']) - $Pages['limit'];
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================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.DepartmentCode AS code");
        $this->db->from('at_inventdetail');
        $this->db->join("am_branch", "am_branch.BranchCode = at_inventdetail.BranchCode","inner");
        $this->db->join("am_item", "am_item.ItemCode = at_inventdetail.ItemCode","inner");
        $this->db->join("am_itemcategory", "am_itemcategory.ItemCategoryID = at_inventdetail.ItemCategoryID","inner");
        $this->db->join("am_fixedassettype", "am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName","inner");
        $this->db->join("at_receiveitem", "at_receiveitem.PRecNumber = at_inventdetail.ReceiptNumber","inner");
        $this->db->join("at_purchaseorder", "at_receiveitem.POrdNumber = at_receiveitem.POrdNumber","inner");
        $this->db->join("at_purchaserequisition", "at_purchaseorder.PReqNumber = at_purchaserequisition.PReqNumber","inner");
        $this->db->join("am_department", "am_department.DepartmentCode = at_purchaserequisition.DepartmentCode","inner");
        //==========================================================================================================//
     
        /* ===================================find by VendorName code========================================================= */
        if ($Departement != "" && $Departement != "All" ) {
            $this->db->like('am_department.DepartmentName', $Departement);
        } 
        /* ==================================end find by VendorName code===================================================== */
        
        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_inventdetail.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$CompCode."')+1)=CONCAT('".$CompCode."','-')", null); 
        } else {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$sess_company."')+1)=CONCAT('".$sess_company."','-')", null); 
        }
        //========end where sesuai Company========================//
        
         /* ========================Kondisi Transatus================= */
        if ($TransStatus != '') {
            $this->db->where_in('at_inventdetail.SoldStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_inventdetail.SoldStatus', array(0,1));
        }
         /* ========================END Kondisi Transatus================= */

        $this->db->group_by("at_inventdetail.DepartmentCode");
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by('at_inventdetail.ItemDesc', 'ASC');
        }else{
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */ 
        $this->db->limit($Pages['limit'], $limitStart);       
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        return $result;
    }
    public function get_all_fixed_asset_list() {
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $Departement       = ($this->input->post('txt_departement')!=null)?$this->input->post('txt_departement'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        
        $PreData        = ($this->input->post('predata')!=[null])?$this->input->post('predata'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================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.IventDetailID		AS IventDetailID_ID
                , at_inventdetail.BranchCode		AS BranchCode_ID
                , at_inventdetail.ReceiptNumber		AS ReceiptNumber_ID
                , at_inventdetail.UsageDate             AS UsageDate_ID
                , at_inventdetail.ItemCode              AS ItemCode_ID
                , at_inventdetail.ItemDesc              AS ItemDesc_ID
                , at_inventdetail.ItemCategoryID        AS ItemCategory_ID
                , at_inventdetail.ItemQty               AS ItemQty_ID
                , at_inventdetail.ItemAverage           AS ItemAverage_ID
                , at_inventdetail.Description		AS Description_ID
                , at_inventdetail.AssetCode             AS AssetCode_ID
                , at_inventdetail.SoldStatus		AS SoldStatus_ID
                
                , am_branch.BranchName                  AS BranchName_ID
                , am_item.ItemUnit                      AS ItemUnit_ID
                , am_itemcategory.ItemCategoryName      AS ItemCategoryName_ID
                , am_fixedassettype.EstimatedLife       AS EstimatedLife_ID
                , am_fixedassettype.DepriciateRate      AS DepriciateRate_ID
                , am_fixedassettype.DepMethodID         AS DepMethodID_ID
                

                , at_purchaserequisition.DepartmentCode	AS DepartmentCode_PR
                , at_purchaserequisition.TransStatus	AS TransStatus_PR
                , am_department.DepartmentName          AS DepartmentName_PR
                "
                );
        
        $this->db->from('at_inventdetail');
        $this->db->join("am_branch", "am_branch.BranchCode = at_inventdetail.BranchCode","inner");
        $this->db->join("am_item", "am_item.ItemCode = at_inventdetail.ItemCode","inner");
        $this->db->join("am_itemcategory", "am_itemcategory.ItemCategoryID = at_inventdetail.ItemCategoryID","inner");
        $this->db->join("am_fixedassettype", "am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName","inner");
        $this->db->join("at_receiveitem", "at_receiveitem.PRecNumber = at_inventdetail.ReceiptNumber","inner");
        $this->db->join("at_purchaseorder", "at_receiveitem.POrdNumber = at_receiveitem.POrdNumber","inner");
        $this->db->join("at_purchaserequisition", "at_purchaseorder.PReqNumber = at_purchaserequisition.PReqNumber","inner");
        $this->db->join("am_department", "am_department.DepartmentCode = at_purchaserequisition.DepartmentCode","inner");
        
        $this->db->where_in('at_inventdetail.DepartmentCode', $PreData);
        //==========================================================================================================//
     
        /* ===================================find by VendorName code========================================================= */
        if ($Departement != "" && $Departement != "All" ) {
            $this->db->like('am_department.DepartmentName', $Departement);
        } 
        /* ==================================end find by VendorName code===================================================== */
        
        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_inventdetail.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$CompCode."')+1)=CONCAT('".$CompCode."','-')", null); 
        } else {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$sess_company."')+1)=CONCAT('".$sess_company."','-')", null); 
        }
        //========end where sesuai Company========================//
        
         /* ========================Kondisi Transatus================= */
        if ($TransStatus != '') {
            $this->db->where_in('at_inventdetail.SoldStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_inventdetail.SoldStatus', array(0,1));
        }
         /* ========================END Kondisi Transatus================= */

        //========group by =======================================//        
        $this->db->group_by('at_inventdetail.IventDetailID');
        //========end group by ===================================/
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by('at_inventdetail.ItemDesc', 'ASC');
        }else{
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    public function get_all_fixed_asset_list_LAMA() {
        
        /* =================================post dari find di kiri==================================================== */
        //$param  = $this->input->post('param');
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $Departement       = ($this->input->post('txt_departement')!=null)?$this->input->post('txt_departement'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================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.IventDetailID		AS IventDetailID_ID
                , at_inventdetail.BranchCode		AS BranchCode_ID
                , at_inventdetail.ReceiptNumber		AS ReceiptNumber_ID
                , at_inventdetail.UsageDate             AS UsageDate_ID
                , at_inventdetail.ItemCode              AS ItemCode_ID
                , at_inventdetail.ItemDesc              AS ItemDesc_ID
                , at_inventdetail.ItemCategoryID        AS ItemCategory_ID
                , at_inventdetail.ItemQty               AS ItemQty_ID
                , at_inventdetail.ItemAverage           AS ItemAverage_ID
                , at_inventdetail.Description		AS Description_ID
                , at_inventdetail.AssetCode             AS AssetCode_ID
                , at_inventdetail.SoldStatus		AS SoldStatus_ID
                
                , am_branch.BranchName                  AS BranchName_ID
                , am_item.ItemUnit                      AS ItemUnit_ID
                , am_itemcategory.ItemCategoryName      AS ItemCategoryName_ID
                , am_fixedassettype.EstimatedLife       AS EstimatedLife_ID
                , am_fixedassettype.DepriciateRate      AS DepriciateRate_ID
                , am_fixedassettype.DepMethodID         AS DepMethodID_ID
                

                , at_purchaserequisition.DepartmentCode	AS DepartmentCode_PR
                , at_purchaserequisition.TransStatus	AS TransStatus_PR
                , am_department.DepartmentName          AS DepartmentName_PR
                "
                );
        
        $this->db->from('at_inventdetail');
        $this->db->join("am_branch", "am_branch.BranchCode = at_inventdetail.BranchCode","inner");
        $this->db->join("am_item", "am_item.ItemCode = at_inventdetail.ItemCode","inner");
        $this->db->join("am_itemcategory", "am_itemcategory.ItemCategoryID = at_inventdetail.ItemCategoryID","inner");
        $this->db->join("am_fixedassettype", "am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName","inner");
        $this->db->join("at_receiveitem", "at_receiveitem.PRecNumber = at_inventdetail.ReceiptNumber","inner");
        $this->db->join("at_purchaseorder", "at_receiveitem.POrdNumber = at_receiveitem.POrdNumber","inner");
        $this->db->join("at_purchaserequisition", "at_purchaseorder.PReqNumber = at_purchaserequisition.PReqNumber","inner");
        $this->db->join("am_department", "am_department.DepartmentCode = at_purchaserequisition.DepartmentCode","inner");
        //==========================================================================================================//
     
        /* ===================================find by VendorName code========================================================= */
        if ($Departement != "" && $Departement != "All" ) {
            $this->db->like('am_department.DepartmentName', $Departement);
        } 
        /* ==================================end find by VendorName code===================================================== */
        
        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_inventdetail.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$CompCode."')+1)=CONCAT('".$CompCode."','-')", null); 
        } else {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$sess_company."')+1)=CONCAT('".$sess_company."','-')", null); 
        }
        //========end where sesuai Company========================//
        
         /* ========================Kondisi Transatus================= */
        if ($TransStatus != '') {
            $this->db->where_in('at_inventdetail.SoldStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_inventdetail.SoldStatus', array(0,1));
        }
         /* ========================END Kondisi Transatus================= */

        //========group by =======================================//        
        $this->db->group_by('at_inventdetail.IventDetailID');
        //========end group by ===================================/
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by('at_inventdetail.ItemDesc', 'ASC');
        }else{
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    //end get all fixed asset
        
    //get all depreciation
    public function get_all_depreciation_list() {
        
        /* =================================post dari find di kiri==================================================== */
        //$param  = $this->input->post('param');
        $CompCode       = ($this->input->post('cmb_company')!=null)?$this->input->post('cmb_company'):'';
        $BranchCode     = ($this->input->post('cmb_branch')!=null)?$this->input->post('cmb_branch'):'';
        $Departement       = ($this->input->post('txt_departement')!=null)?$this->input->post('txt_departement'):'';
        $DateTo         = ($this->input->post('txt_DateTo')!=null)?$this->input->post('txt_DateTo'):'';
        $TransStatus    = ($this->input->post('chk_TransStatus')!=null)?$this->input->post('chk_TransStatus'):'';
        $txtTable       = ($this->input->post('txtTable')!=null)?$this->input->post('txtTable'):'';
        $txtField       = ($this->input->post('txtField')!=null)?$this->input->post('txtField'):'';
        $typeShorting   = ($this->input->post('typeShorting')!=null)?$this->input->post('typeShorting'):'';
        /* =================================END post dari find di kiri==================================================== */
           
        /* ==========================================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.IventDetailID		AS IventDetailID_ID
                , at_inventdetail.BranchCode		AS BranchCode_ID
                , at_inventdetail.ReceiptNumber		AS ReceiptNumber_ID
                , at_inventdetail.ReceiptDate		AS ReceiptDate_ID
                , at_inventdetail.UsageDate             AS UsageDate_ID
                , at_inventdetail.ItemCode              AS ItemCode_ID
                , at_inventdetail.ItemDesc              AS ItemDesc_ID
                , at_inventdetail.ItemCategoryID        AS ItemCategory_ID
                , at_inventdetail.ItemQty               AS ItemQty_ID
                , at_inventdetail.ItemAverage           AS ItemAverage_ID
                , at_inventdetail.Description		AS Description_ID
                , at_inventdetail.AssetCode             AS AssetCode_ID
                , at_inventdetail.SoldStatus		AS SoldStatus_ID
                
                , am_branch.BranchName                  AS BranchName_ID
                , am_item.ItemUnit                      AS ItemUnit_ID
                , am_itemprice.ItemPrice                AS ItemPrice_ID
                , am_itemcategory.ItemCategoryName      AS ItemCategoryName_ID
                , am_fixedassettype.EstimatedLife       AS EstimatedLife_ID
                , am_fixedassettype.DepriciateRate      AS DepriciateRate_ID
                , am_fixedassettype.DepMethodID         AS DepMethodID_ID                

                , at_journalvoucher.JVchDate            AS JVchDate_JV
                , at_jvchdetail.JVchDebetSaldo          AS JVchDebetSaldo_JV
                , at_jvchdetail.PeriodEnd               AS PeriodEnd_JV
                "
                );
        
        $this->db->from('at_inventdetail');
        $this->db->join("am_branch", "am_branch.BranchCode = at_inventdetail.BranchCode","inner");
        $this->db->join("am_item", "am_item.ItemCode = at_inventdetail.ItemCode","inner");
        $this->db->join("am_itemprice", "am_itemprice.ItemCode = at_inventdetail.ItemCode","inner");
        $this->db->join("am_itemcategory", "am_itemcategory.ItemCategoryID = at_inventdetail.ItemCategoryID","inner");
        $this->db->join("am_fixedassettype", "am_fixedassettype.FixedAssetType = am_itemcategory.ItemCategoryName","inner");
        $this->db->join("at_jvchdetail", "at_jvchdetail.AssetCode = at_inventdetail.AssetCode","inner");
        $this->db->join("at_journalvoucher", "at_journalvoucher.JVchNumber = at_jvchdetail.JVchNumber","inner");
        //==========================================================================================================//
     
        /* ===================================find by VendorName code========================================================= */
        if ($Departement != "" && $Departement != "All" ) {
            $this->db->like('am_department.DepartmentName', $Departement);
        } 
        /* ==================================end find by VendorName code===================================================== */
        
        /* ===================================find by branch code========================================================= */
        if ($BranchCode != "" && $BranchCode != "All" ) {
            $this->db->where('at_inventdetail.BranchCode', $BranchCode);
        } 
        /* ==================================end find by branch code===================================================== */
        
        //========where sesuai Company========================//
        if ($CompCode != "" && $CompCode != "All" ) {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$CompCode."')+1)=CONCAT('".$CompCode."','-')", null); 
        } else {
            $this->db->where("SUBSTRING(at_inventdetail.BranchCode,1,LENGTH('".$sess_company."')+1)=CONCAT('".$sess_company."','-')", null); 
        }
        //========end where sesuai Company========================//
        
         /* ========================Kondisi Transatus================= */
        if ($TransStatus != '') {
            $this->db->where_in('at_inventdetail.SoldStatus', $TransStatus);
        } else {
            /*default adalah 2,6. Finished dan Need Approval*/
            $this->db->where_in('at_inventdetail.SoldStatus', array(0,1));
        }
         /* ========================END Kondisi Transatus================= */

        //========group by =======================================//        
        $this->db->group_by('at_inventdetail.IventDetailID');
        //========end group by ===================================/
        
        /* ==============================================SHORTING TABLE========================================================== */
        if($txtTable=="" && $txtField==""){
            $this->db->order_by('at_inventdetail.ItemDesc', 'ASC');
        }else{
            $this->db->order_by($txtTable . '.'.$txtField, $typeShorting);
        }
        /* ==============================================END SHORTING TABLE========================================================== */        
        
        $query = $this->db->get();
        //echo "<hr>".$this->db->last_query()."<hr>";
        $result = $query->result();
        //$data = array('datefrom'=>$DateFrom,'dateend'=>$DateTo,"data"=>$result);
        return $result;
    }
    
}

?>
