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

require APPPATH . '/libraries/BaseController.php';

/**
 * Class : Cost Center (Cost Center Controller)
 * User Class to control all user related operations.
 * @author : VenbaMail Mali
 * @version : 1.1
 * @since : 15 Feb 2016
 */
class monthlypay extends BaseController
{
    /**
     * This is default constructor of the class
     */
     public function __construct()
    {
        parent::__construct();
        $this->load->model('monthlypay_model');
        $this->load->library('session');
        $this->load->library('form_validation');
        // $this->load->library('pagination');
        
        $this->isLoggedIn();   
    }
    /**
     * This function used to load the first screen of the user
     */
    public function index()
    {
        $this->global['pageTitle'] = 'Siddharth : Monthly Pay Details';
        
        $this->loadviews('monthlyListing',$this->global);  
    }
    
    /**
     * This function is used to load the monthly list
     */

    function monthlyListing()
    {
       

            $this->load->model('monthlypay_model');
        
            $searchText = $this->input->post('searchText');
            $data['searchText'] = $searchText;
            
            $this->load->library('pagination');
            
            $count = $this->monthlypay_model->monthlyListingCount($searchText);

            $returns = $this->paginationCompress ( "monthlyListing", $count ,5);
            
            $data['userRecords'] = $this->monthlypay_model->monthlyListing($searchText, $returns["page"], $returns["segment"]);
            
            $this->global['pageTitle'] = 'Siddharth : Monthly Pay List';
            
            $this->loadViews("monthlyListing", $this->global, $data, NULL);   
    }

    /**
     * This function is used to load the  filtering monthly list
     */
    

    
    /**
     * This function is used to load the add new monthlypay   
     */
    function addmonthly()
    {
        
              

            //$data['emp_id'] = $this->monthlypay_model->getmonthlypay();   
            
            $this->global['pageTitle'] = 'siddharth : Add Monthly Pay';

             $this->loadviews('addMontly',$this->global, NULL);  
        
    } 
     
    function addNewmonthly()
    {
                   
           
            $this->form_validation->set_rules('Month_Year','month_year','trim|required');
            $this->form_validation->set_rules('EmpID','emp_id','trim|required');
            $this->form_validation->set_rules('LOP_days','LOP_days','trim|required');
            $this->form_validation->set_rules('OT_Hrs_Worked','OT_Hrs_Worked','trim|required');
            $this->form_validation->set_rules('Loan_Recovered','Loan_Recovered','trim|required');
            $this->form_validation->set_rules('Loan_Balance','Loan_Balance','trim|required');
            $this->form_validation->set_rules('Incentives','Incentives','trim|required');
            $this->form_validation->set_rules('Other_Deductions','Other_Deductions','trim|required');            
         
            if($this->form_validation->run() == FALSE)
            {
                $this->addmonthly();
            }
            else
            {
                $month_year = $this->input->post('month_year');
                $emp_id =     $this->input->post('emp_id');            
                $LOP_days =     $this->input->post('LOP_days');            
                $OT_Hrs_Worked =     $this->input->post('OT_Hrs_Worked');            
                $Loan_Recovered =     $this->input->post('Loan_Recovered');            
                $Loan_Balance =     $this->input->post('Loan_Balance');            
                $Incentives =     $this->input->post('Incentives');            
                $Other_Deductions =     $this->input->post('Other_Deductions');            
                
                $CreateBy =   $this->session->userdata ( 'userId' );
                    
                $dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
                
                $createddt =  $dt->format('Y-m-d H:i:s');
                
                $monthlypayExists =  $this->monthlypay_model->checkmonthlypayExists( $monthlypay);
                
                if(count($CodeExists) == 0)
                {
                 $montlypay = array('month_year'=>$month_year, 'emp_id'=>$emp_id,'LOP_days'=>$LOP_days,'OT_Hrs_Worked'=>$Loan_Recovered,'Loan_Balance'=>$Loan_Balance,'Incentives'=>$Incentives,'Other_Deductions'=>$Other_Deductions,'CreatedBy'=>$CreateBy,'createdDate'=>$createddt); 
                 $this->monthlypay_model->addNewmonthly($monthlypay);
                }
                
                
                    
                    Print_r('Successfully Created Monthly pay details');
          
            }
        
    }

    /**
     * This function is used for editing purpose and it has oldest values.    
     */
    
    function editOldmonthly($EmpID='',$month_year='')
    {
         /*if($employeeid == null)
            {
    
                redirect('monthlypay/monthlyListing');
            }*/
            
           
            
            $data['monthly'] = $this->monthlypay_model->getUserInfo($EmpID,$month_year);
            //$data['monthly_pay_inputs'] = $this->monthlylisting_model->getUserInfo($employeeid);
   
            $this->global['pageTitle'] = 'Siddharth : Edit Monthly Listing';
            
            $this->loadViews("editOldmonthly", $this->global, $data,NULL);
        
    }
   
    

    /**
     * This function is used for stored new values after editing.    
     */
    function editmonthly()
    {
        
            $this->load->library('form_validation');
            
            $EmpID= $this->input->post('EmpID');
            $month_year = $this->input->post('Month_Year');
            $this->form_validation->set_rules('Month_Year','month_year','trim|required');
            $this->form_validation->set_rules('EmpID','EmpID','trim|required');
            $this->form_validation->set_rules('LOP_Days','LOP_Days','trim|required');
            $this->form_validation->set_rules('OT_Hrs_Worked','OT_Hrs_Worked','trim|required');
            $this->form_validation->set_rules('Loan_Recovered','Loan_Recovered','trim|required');
            $this->form_validation->set_rules('Loan_Balance','Loan_Balance','trim|required');
            $this->form_validation->set_rules('Incentives','Incentives','trim|required');
            $this->form_validation->set_rules('Other_Deductions','Other_Deductions','trim|required');
            

            if($this->form_validation->run() == FALSE)
            {
    
                $this->editOldmonthly($EmpID,$month_year);
            }
            
            else
            {
                

                $month_year = $this->input->post('Month_Year');
                $EmpID = $this->input->post('EmpID');
                $LOP_Days =$this->input->post('LOP_Days');
                $OT_Hrs_Worked = $this->input->post('OT_Hrs_Worked');
                $Loan_Recovered = $this->input->post('Loan_Recovered');
                $Loan_Balance = $this->input->post('Loan_Balance');
                $Incentives = $this->input->post('Incentives');
                $Other_Deductions = $this->input->post('Other_Deductions');
                $LastModBy =   $this->session->userdata ( 'userId' );

                 $dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
                $LastModTime =  $dt->format('Y-m-d H:i:s');
                    
                
               
               $monthdata = array();
    
    
                $monthdata = array('Month_Year'=>$month_year,'EmpID'=>$EmpID,'LOP_Days'=>$LOP_Days,'OT_Hrs_Worked'=>$OT_Hrs_Worked,'Loan_Recovered'=>$Loan_Recovered,'Loan_Balance'=>$Loan_Balance,'Incentives'=>$Incentives,'Other_Deductions'=>$Other_Deductions,'Last_Mod_By'=>$LastModBy,'Last_Mod_Time'=>$LastModTime);               
     


                $result = $this->monthlypay_model->editmonth($monthdata,$EmpID);

                
                if($result == true)
                {
                    $this->session->set_flashdata('success', 'updated successfully');
                }
                else
                {
                    $this->session->set_flashdata('error', 'updation failed');
                }
                //echo success;
               
               redirect('monthlyListings');
            }    
        
    }
    

    
    
    function pageNotFound()
    {
        $this->global['pageTitle'] = 'CodeInsect : 404 - Page Not Found';
        
        $this->loadViews("404", $this->global, NULL, NULL);
    }
}

?>