<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class costcenter_model extends CI_Model
{
   
	/**
     * This function is used to get the user listing count
     * @param string $searchText : This is optional search text
     * @param number $page : This is pagination offset
     * @param number $segment : This is pagination limit
     * @return array $result : This is result
     */
    function CostListing()
    {
        $this->db->select('Cost.CostCenterCode as code,Cost.CostCenterName as CostCenterName , Cost.ApprovedBy as ApprovedBy,Emp.FirstName as FirstName,Emp.Designation as  Designation ');
        $this->db->from('T_CostCenter_Master as Cost');
        $this->db->join('T_Employee_Details as Emp', 'Emp.EmpID=Cost.ApprovedBy');
       
     
         $query = $this->db->get();       
        $result = $query->result();  
       
         return $result;
    }
	/**
     * This function used to get user information by id
     * @param number $userId : This is user id
     * @return array $result : This is user information
     */
   
function addNewcost($Cost)
    {
        $this->db->trans_start();
        $this->db->insert('T_CostCenter_Master', $Cost);
        
        $insert_id = $this->db->affected_rows(); 
        
        $this->db->trans_complete();      
      	   
	    return $insert_id;
    }
    
	
	function addCostCenterDepartment($Dept)
    {
        $this->db->trans_start();
        $this->db->insert('T_CostCenter_Departments', $Dept);
        
        $insert_id = $this->db->affected_rows(); 
        
        $this->db->trans_complete();      
      	//  print_r( $this->db->last_query()); 
	    return $insert_id;
    }
	
	
	function addBudget($Budget)
    {
        $this->db->trans_start();
        $this->db->insert('T_CostCenter_Budget', $Budget);
        
        $insert_id = $this->db->affected_rows(); 
        
        $this->db->trans_complete();      
      	//  print_r( $this->db->last_query()); 
	    return $insert_id;
    }
	
	function getApproverName()
    {
		$this->db->select('EmpID,FirstName,LastName,Designation');
        $this->db->from('T_Employee_Details');
	    $this->db->where('Departmentcode ', 'DEP10');
	   
		$query = $this->db->get();
        
        return $query->result();
    }
	
    /**
     * This function used to get user information by id
     * @param number $userId : This is user id
     * @return array $result : This is user information
     */
	 function getuserInfo($CostCenterID)
    {
        $this->db->select('CostCentreID, CostName,Description,CreatedDate');
        $this->db->from('T_CostCentre');
        
        $this->db->where('CostCentreID', $CostCenterID);
        $query = $this->db->get();
        
        return $query->result();
    }
	 /**
     * This function used to get Cost center Master information by Cost center ID
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetCostCenterMaster($CostCenterCode)
    {
        $this->db->select('Cost.CostCenterCode,Cost.CostCenterName,Cost.ApprovedBy,emp.FirstName,emp.Designation,max(BudgetYear) as BudYear' );
        $this->db->from('T_CostCenter_Master Cost');
		 $this->db->join('T_Employee_Details emp', 'emp.EmpID = Cost.ApprovedBy');
         $this->db->join('T_CostCenter_Budget Bud', ' Bud.CostCenterCode = Cost.CostCenterCode');
        $this->db->where('Cost.CostCenterCode', $CostCenterCode);
        $query = $this->db->get();
        
        return $query->result();
    }
	
	/**
     * This function used to get Cost center Department information by Cost center ID
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetCostCenterDepartment($CostCenterCode)
    {
        $this->db->select('Dept.DEPCode,Det.DepartmentName');
        $this->db->from('T_CostCenter_Departments Dept');
		 $this->db->join('T_DepartmentDetails Det', 'Det.DEPCode = Dept.DEPCode');
        
        $this->db->where('Dept.CostCenterCode', $CostCenterCode);
        $query = $this->db->get();
        
        return $query->result();
    }
	/**
     * This function used to get Cost center Budget information by Cost center ID
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetBudgetYear($CostCenterCode)
    {
		
		$subQuery = 'select BudgetYear from T_CostCenter_Budget where  CostCenterCode=? ';   
       
   
        $query = $this->db->query($subQuery, array($CostCenterCode));
        
        return $query->result();
    }
	/**
     * This function used to get Cost center Budget information by Cost center ID
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetCostCenterBudget($CostCenterCode)
    {
		
		$subQuery = 'select BudgetType,BudgetYear,BudgetAmount,Remarks from T_CostCenter_Budget where  CostCenterCode=? and 
					BudgetYear=(select max(BudgetYear) from T_CostCenter_Budget  where CostCenterCode=?)';   
       
   
        $query = $this->db->query($subQuery, array($CostCenterCode,$CostCenterCode));
        
        return $query->result();
    }
	/**
     * This function used to get Cost center Budget information by Cost center ID
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetCostBudgetTypeByYear($CostCenterCode,$ConfigID,$Year)
    {	
			
			$subQuery = 'select ConfigValue  from T_ConfigDetails Bud  where ConfigValue not in
						(select BudgetType from T_CostCenter_Budget where CostCenterCode=? and BudgetYear=?) and Config_ID=? ';
	
			$query = $this->db->query($subQuery, array($CostCenterCode,$Year,$ConfigID));
     
       
		   return $query->result_array();
  
    }
	/**
     * This function used to get Cost center Budget information by Cost center ID
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetCostCenterBudgetByYear($CostCenterCode,$Year)
    {
		
		$subQuery = 'select BudgetType,BudgetYear,BudgetAmount,Remarks from T_CostCenter_Budget where  CostCenterCode=? and 
					budgetyear=?';   
       
   
        $query = $this->db->query($subQuery, array($CostCenterCode,$Year));
        
       return $query->result_array();
    }
	
	/**
     * This function used to Department list which is not available in the Costcenter master
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetDepartmentNotinCostCenter($CostCenterCode)
    {
			
			$subQuery = 'select Dept.DEPCode,Dept.DepartmentName from T_DepartmentDetails Dept
		where Dept.DEPCode not in 
			(select DEPCode from T_CostCenter_Departments where CostCenterCode=?) order by  Dept.DEPCode asc';
	
        $query = $this->db->query($subQuery, array($CostCenterCode));
     
       
        return $query->result();
    }
	
	
	/**
     * This function used to Department list which is not available in the Costcenter master
     * @param number $CostCenterCode : CostCenterCode as Parameter
     * @return array $result : This is user information
     */
	function GetBudgetTypeNotinCostCenter($CostCenterCode,$ConfigID)
    {
			
			$subQuery = 'select ConfigValue  from T_ConfigDetails Bud  where ConfigValue not in
						(select BudgetType from T_CostCenter_Budget where CostCenterCode=? and BudgetYear=(select max(BudgetYear) from T_CostCenter_Budget)) and Config_ID=?';
	
			$query = $this->db->query($subQuery, array($CostCenterCode,$ConfigID));
             
			// print_r($this->db->last_query());
       
		return $query->result();
    }
	
	function EditCost($Cost,$CostCenterCode)
    {
         $this->db->where('CostCenterCode', $CostCenterCode);
		
        $this->db->update('T_CostCenter_Master', $Cost); 
	
        return TRUE;
    }
	
	
	function EditBudget($Budget)
    {
        $this->db->trans_start();
        $this->db->insert('T_CostCenter_Budget', $Budget);
        
        $insert_id = $this->db->affected_rows(); 
        
        $this->db->trans_complete();      
      	//  print_r( $this->db->last_query()); 
	    return $insert_id;
    }
	
	function DeleteBudget($BudgetType='',$BudgetYear='',$CostCenterCode ='')
    {
       $this->db->where("BudgetType", $BudgetType);
	    $this->db->where("BudgetYear", $BudgetYear);
		$this->db->where("CostCenterCode", $CostCenterCode);			
        $this->db->delete('T_CostCenter_Budget'); 
		//print_r( $this->db->last_query());
    }
	
	function DeleteDepartment($DepCode='',$CostCode='')
    {
       $this->db->where("DEPCode", $DepCode);
	    $this->db->where("CostCenterCode", $CostCode);		 
        $this->db->delete('T_CostCenter_Departments'); 
		//print_r( $this->db->last_query());
    }
	
    function checkDeptCostExists($DepCode='',$CostCode='')
    {
		$this->db->select('CostCenterCode,DEPCode');
        $this->db->from('T_CostCenter_Departments');
		$this->db->where('DEPCode',$DepCode);
		$this->db->where('CostCenterCode',$CostCode);
	    
		$query = $this->db->get();
		
        if ($query->num_rows > 0) {
			return $query->result_array();
		}   
    }
	function checkCostDetailsExists($Code='',$CostName='')
    {
		$this->db->select('CostCenterCode');
        $this->db->from('T_CostCenter_Master');
		$this->db->where('CostCenterCode',$Code);
	    if ($CostName != '')
		{
			$this->db->where('CostCenterName  !=',$CostName);
		}
		$query = $this->db->get();
		
        if ($query->num_rows > 0) {
			return $query->result_array();
		}   
    }
	/* *//**
     * This function is used to get the Department details from T_DepartmentDetails
     * @return array $result : This is result of the query
     */
	function getDepartment()
    {
        $this->db->select('DEPCode, DepartmentName');
        $this->db->from('T_DepartmentDetails');
        $this->db->where('IsActive =', 1);
        $query = $this->db->get();
        
        return $query->result();
    }
	/**
     * This function is used to get the Config value from configuration table
     * @return array $result : This is result of the query
     */
	function getConfigValue($ConfigID )
    {
		
        $this->db->select('ConfigValue');
        $this->db->from('T_ConfigDetails');
		 $this->db->where('Config_id ',$ConfigID );
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function export_excel(){
		
		
		
		$this->db->select('Mas.*,bud.*,part.DepartmentName,emp.firstname,emp1.firstname as Updated_By');
		$this->db->select('sum(if(budgettype="CAPITAL",budgetamount,0)) as capital',FALSE);
		$this->db->select('sum(if(budgettype="REVENUE",budgetamount,0)) as revenue',FALSE);
		$this->db->select('sum(if(budgettype="SERVICE",budgetamount,0)) as service',FALSE);
		$this->db->select('sum(if(budgettype="IMPORT",budgetamount,0)) as import',FALSE);
		
		$this->db->join('T_CostCenter_Budget as Mas',' Mas.CostCenterCode=bud.CostCenterCode','left');
		$this->db->join('T_CostCenter_Departments as dep',' dep.CostCenterCode=Mas.CostCenterCode','left');
		$this->db->join('T_DepartmentDetails as part','part.DEPCode=dep.DEPCode','left');
		$this->db->group_by('bud.CostCenterCode');
		$this->db->join('tbl_users as tbl',' bud.createdby = tbl.userid','left');
		$this->db->join('T_Employee_Details as emp', 'emp.empid=tbl.empid','left');
		$this->db->join('tbl_users as tbl1',' bud.UpdatedBy = tbl1.userid','left');
		$this->db->join('T_Employee_Details as emp1', 'emp1.empid=tbl1.empid','left');
        return $this->db->get('T_CostCenter_Master as bud')->result_array();
	
	}
}
?>