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

class Payroll_model extends CI_Model
{
		
    /**
     * This function is used to insert the File details in database
     * @FileDetails array  : This will have the set of value which we need to insert into database
     * @return number $Payon : Insert row count will be the return value
     */
	function UploadFileName($FileDetails)
    {
         $this->db->insert('T_Payroll_File', $FileDetails);
     
		 $Payon = $this->db->affected_rows();

         return $Payon;
    }

    function model_sp($payon,$employee){
    
        //echo "<script>alert('mod');</script>";
        //echo "highlight_file(filename)";
        //die();
        $result=$this->db->query("call P_FINAL_PAYROLL_DATA('".$payon."','".$employee."')") or die(mysql_error());
        //$result->next_result();
		
        return $result;



    }
	/**
     * This function is used to update the File details in which is already exists in the database
     * @FileDetails array  : This will have the set of value which we need to insert into database
     * @return number boolean: success of the updation
     */
	function UpdateFileName($FileDetails, $PayOn)
    {
		//echo $EmpID;
        $this->db->where('PayOn', $PayOn);
        $this->db->update('T_Payroll_File', $FileDetails); 
        //print_r($this->db->last_query());
        return TRUE;
    }
	/**
     * This function is used to check the File details is already exists in the database or not
     * @FileDetails array  : This will have the set of value which we need to insert into database
     * @return number boolean: success of the updation
     */
	function checkFileDetailsExists($PayOn)
    {
		$this->db->select('PayOn');
        $this->db->from('T_Payroll_File');
		$this->db->where('PayOn',$PayOn);
	    
		$query = $this->db->get();
		// print_r($this->db->last_query());	
        if ($query->num_rows > 0) {
			return $query->result();
		}   
    }

    function checkFileDetailsExists2($PayOn,$EmpId)
    {
        //echo $EmpId;
        //die();
        $this->db->select('Month_Year','EmpID');
       $this->db->from('T_Monthly_Pay_Inputs');
        $this->db->where('EmpID',$EmpId);
        $this->db->where('Month_Year',$PayOn);
        
        
        $query = $this->db->get();
        // print_r($this->db->last_query());    
       if ($query->num_rows > 0) {
            //echo "success";
            //return $query->result();
            return TRUE;
        }  
        else{
            //echo "fail";
            return FALSE;
        }
    }
	 /**
     * This function is used to insert the File data in database
     * @FileDetails array  : This will have the set of value which we need to insert into database
     * @return number $Payon : Insert row count will be the return value
     */
	function UploadFileData($FileData)
    {
         $this->db->insert('T_Payroll', $FileData);
     
		 $Payon = $this->db->affected_rows();
         //print_r($this->db->last_query());
         return $Payon;
    }

    function UploadFileData2($FileData)
    {
        $this->db->insert('T_Monthly_Pay_Inputs', $FileData);
   
         $Payon = $this->db->affected_rows();
        //print_r($this->db->last_query());
        return $Payon;
    }
	
	/**
     * This function is used to update the File details in which is already exists in the database
     * @FileDetails array  : This will have the set of value which we need to insert into database
     * @return number boolean: success of the updation
     */
	function UpdateFiledata($FileData, $PayOn,$EmpId)
    {
		//echo $EmpID;
        $this->db->where('PayOn', $PayOn);
		 $this->db->where('EmpID', $EmpId);
        $this->db->update('T_Payroll', $FileData); 
		//print_r($this->db->last_query());
        //print_r($this->db->last_query());
        return TRUE;
    }
	
	/**
     * This function is used to get the data details in which is already exists in the database
     * @FileDetails array  : This will have the set of value which we need to insert into database
     * @return number boolean: success of the updation
     */
	function GetPayslipdata($PayOn='',$EmpId='')
    {
		 $this->db->select('Pay.*,payfile.TotalNoofDays as TotalNoofDays ,Emp.FirstName as FName ,Emp.LastName as Lname,Emp.DateofBirth as Dob,Emp.PANNo as Pan,Emp.BankBranchName as BankName,Emp.IFSCCode as IFSCCode,Emp.DateofJoining as DOJ,Emp.Designation as Designation,Dep.DepartmentName as DeptName');
         $this->db->from('T_Payroll Pay');
		 $this->db->join('T_Payroll_File payfile', 'Pay.PayOn = payfile.PayOn');
		  $this->db->join('T_Employee_Details Emp', 'Pay.EmpID = Emp.EmpID');
		    $this->db->join('T_DepartmentDetails Dep', 'Emp.Departmentcode = Dep.DEPCode');
		 $this->db->where('Pay.PayOn', $PayOn);
		 if($EmpId != '')
		 {
		   $this->db->where('Pay.EmpID', $EmpId);
		 }
		 
		$this->db->order_by("Pay.EmpID", "asc");
		 
        $query = $this->db->get();
        
        return $query->result();
    }
	
	/* *//**
     * This function is used to get the PayOn details from T_Payroll_File
     * @return array $result : This is result of the query
     */
	function getPayOn()
    {
        $this->db->select('DISTINCT(PayOn)');
        $this->db->from('T_Payroll_File');
        $query = $this->db->get();        
        return $query->result();
    }
	
	/* *//**
     * This function is used to get the PayOn details from T_Payroll_File
     * @return array $result : This is result of the query
     */
	function getEmployeelist($Payon ='')
    {
        $this->db->select('Pay.EmpID as EmpId,Emp.FirstName as Fname,Emp.LastName as Lname');		
        $this->db->from('T_Payroll Pay');
		$this->db->join('T_Employee_Details Emp', 'Pay.EmpID = Emp.EmpID');
		 if($Payon != '')
		 {
		   $this->db->where('Pay.PayOn', $Payon);
		 }
		 $this->db->order_by("Pay.EmpID", "asc");
		 
        $query = $this->db->get();        
        return $query;
    }


    function getEmployeelist2($Payon ='')
    {
        $this->db->select('Pay.EmpID as EmpId,Emp.FirstName as Fname,Emp.LastName as Lname');       
        $this->db->from('T_Monthly_Pay_Inputs Pay');
        $this->db->join('T_Employee_Details Emp', 'Pay.EmpID = Emp.EmpID');
         if($Payon != '')
         {
           $this->db->where('Pay.Month_Year', $Payon);
         }
         $this->db->order_by("Pay.EmpID", "asc");
         
        $query = $this->db->get();        
        return $query;
    }
	
	/* *//**
     * This function is used to get the PayOn details from T_Payroll_File
     * @return array $result : This is result of the query
     */
	function getEmpAll($Payon ='')
    {
        $this->db->select('Pay.EmpID as EmpId,Emp.FirstName as Fname,Emp.LastName as Lname');		
        $this->db->from('T_Payroll Pay');
		$this->db->join('T_Employee_Details Emp', 'Pay.EmpID = Emp.EmpID');
		 if($Payon != '')
		 {
		   $this->db->where('Pay.PayOn', $Payon);
		 }
		$this->db->order_by("Pay.EmpID", "asc");
		 
        $query = $this->db->get();      
        return $query->result_array();
    }
	
	/* *//**
     * This function is used to delete  the PayOn details from T_Payroll
     * @return array $result : This is result of the query
     */
	function deleteFileData($PayOn='')
    {
       
		$this->db->where("PayOn", $PayOn);
		 
        $this->db->delete('T_Payroll'); 
		       
    }
    
    function getEmpID($q='')
   {
       $this->db->select('EmpID,FirstName,LastName');
       $this->db->from('T_Employee_Details');
	    if($q == "addemppaydate"){
	   $this->db->where('EmpID NOT IN (SELECT EmpID from T_Emp_Pay_Data)',NULL,FALSE);
		}
       $query = $this->db->get();
       return $query->result();
   }
}
?>