<?php
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
ini_set('memory_limit', '2048M');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Salary_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
		$this->load->helper(array('form', 'url'));
        $this->load->library('upload');
        $this->load->model('reports/salary_report_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() {
        $data = array();        
        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');
        
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Submit' || $this->input->post('submit') == 'Print Data' || $this->input->post('submit') == 'Pdf Data' || $this->input->post('submit') == 'Excel Data' || $this->input->post('submit') == 'Chart Data')) {
           //echo "<pre>"; print_r($this->input->post()); //exit;
		    $start_date = $this->input->post('start_date');
            $end_date 	= $this->input->post('end_date');
			$archanai 	= $this->salary_report_model->get_archanai_data($start_date,$end_date);
            $abishegam 	= $this->salary_report_model->get_abishegam_data($start_date,$end_date);
            $prasadam 	= $this->salary_report_model->get_prasadam_data($start_date,$end_date);
            $kattalai 	= $this->salary_report_model->get_kattalai_data($start_date,$end_date);
        }else  {
            $start_date = '';
            $end_date = '';
			$archanai 	= $this->salary_report_model->get_archanai_data($start_date,$end_date);
            $abishegam 	= $this->salary_report_model->get_abishegam_data($start_date,$end_date);
            $prasadam 	= $this->salary_report_model->get_prasadam_data($start_date,$end_date);
            $kattalai 	= $this->salary_report_model->get_kattalai_data($start_date,$end_date);
        }
        
		$len = count($archanai);
		//echo '<pre>';print_r($archanai); //exit;
	   foreach($archanai as $k => $items){
			if($items['type_name'] != 'Abishegam Items'){
				$new_archanai[]	=  $items;
			}else{
				$key = $k;
			}
	   }
	   
	   $new_archanai[$len - 1] = $archanai[$key];
	   //print_r($new_archanai); exit;
	    /*
	    $data['archanai'] 	= $archanai;
		$data['abishegam'] 	= $abishegam;
		$data['prasadam'] 	= $prasadam;
		$data['kattalai'] 	= $kattalai;
		*/
		//$data['archanai'] = $archanai; //--
		$data['results'] = array_merge($new_archanai,$kattalai,$abishegam,$prasadam); //--
        //echo '<pre>';print_r($data['results']); exit;
        $data['start_date'] = $start_date;
        $data['end_date'] 	= $end_date;
        $data['type'] 		= $data_type;
		
		$data['pdftype'] 	= 'Pdf Data';	
		$data['exceltype'] 	= 'Excel Data';
		$data['charttype'] 	= 'Chart Data';
		
		$actions 				= $this->actions();
        $data['form_action'] 	= 'salary_report';
        $data['title']			= 'Salary Report';
        
        
        
        if($this->input->post('submit') == 'Chart Data' ){
			//echo "<pre>"; print_r($data); exit;
            $data['dataType'] = $this->input->post('submit');			
			$q 							= $this->db->get_where(TBL_SETTINGS_BASIC);
			$data['settings'] 			= $q->row_array();
			$q_des						= $this->db->get_where(TBL_SETTINGS_DESIGN);
            $data['settings'] 			= array_merge($data['settings'], $q_des->row_array());
			$user_data 					= get_session_userdata();
			$_SESSION['pdf_chart']		= "success";
            $_SESSION['pdf_chart_file']	= "salary_chart_report.pdf";
			include($_SERVER['DOCUMENT_ROOT'].'/smmt2020/application/views/reports/salary_report/chartpdf.php');
        } elseif($this->input->post('submit') == 'Print Data' ){
            $data['dataType'] = $this->input->post('submit');
            if( IS_KSK ) :
                $html = $this->load->view('reports/salary_report/print_pdf', $data, TRUE);
                print_rec($html, 'salary_report');
                redirect('salary_report','refresh');
            else :
                $this->render($data, 'reports/salary_report/pdf');
            endif;
        } else if ($this->input->post('submit') == 'Pdf Data') {
            $data['dataType']	= $this->input->post('submit');
			$user_data 			= get_session_userdata();
			
            if (IS_KSK) :
                $html = $this->load->view('reports/salary_report/pdf', $data, TRUE);        
                $filename = "salary_report_" . date("Y-m-d");
                $pdfFilePath = date('d-m-Y').".pdf";
                $this->load->library('m_pdf');
                $pdf = $this->m_pdf->load();
                $pdf->SetAutoFont(AUTOFONT_ALL);
				//                $pdf->AddPage("L");
				 $pdf->AddPage('L', // L - landscape, P - portrait
				'', '', '', '',
				5, // margin_left
				5, // margin right
				5, // margin top
				5, // margin bottom
				3, // margin header
				3);
            	unlink(FCPATH.'uploads/salary_report_pdf/');
                $pdf->setFooter('{PAGENO}');
              	$pdf->WriteHTML($html);
               	$pdf->Output(FCPATH.'uploads/salary_report_pdf/'.$pdfFilePath, "F");
               	$_SESSION['pdf_salary']="success";
               	$_SESSION['pdf_salary_file']=$pdfFilePath;
                redirect("salary_report","refresh");
                
               //exit(0);
            else :
                $this->render($data, 'reports/salary_report/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Data'))) {
		    $this->load->view('reports/salary_report/excel', $data);
        } else {
            $this->render($data, 'reports/salary_report/list');
        }
    }
	
	function uploadchartpdf(){
		unlink(FCPATH.'uploads/salary_report_pdf/salary_chart_report.pdf');
		$filename					= 'pdf';
		$config						= array();
		$config['upload_path'] 		= FCPATH.'uploads/salary_report_pdf/';
		$config['allowed_types'] 	= '*';
		$config['max_size']    		= '15000000';
		$config['file_name']        = "salary_chart_report.pdf";
		$this->load->library('upload');
	
		$this->upload->initialize($config);
	
		if(!is_dir($path)){
			@mkdir($path, 0777, true);
		}
	
		if ( ! $this->upload->do_upload($filename)) {
			return "failed";
		} else {
			return "success";
		} 
	}
   
} 
?>