<?php
class Temple_expenses_report extends REF_Controller {
    
    function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('upload');
        $this->load->model('reports/temple_expenses_report_model');
        $this->uri_key_index = 0;
    }
    
    function index(){
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Temple Expenses Report',
            'controller'     => $this->controller,
            'actions'        => $actions,            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        $whr = array();
        $post = $this->input->post();
        //echo '<pre>'; print_r( $post ); 
        if( !empty( $post['submit'] ) ) :
            $f = $post['filter'];
            if( !empty( $f['type'] ) )
                $whr['exp_type_id'] = $f['type'];
            
            if( !empty( $f['sub'] ) )
                $whr['expenses_for'] = $f['sub'];
        endif;

        if( !empty( $f['start_date'] ) ) :
            $e = explode('-', $f['start_date']);
            $start_date = $e[2] . '-' . $e[1]. '-' . $e[0];
        else :
            $start_date = date("Y-m-d", strtotime("-7 day"));
        endif;
        
        if( !empty( $f['end_date'] ) ) :
            $e = explode('-', $f['end_date']);
            $end_date = $e[2] . '-' . $e[1]. '-' . $e[0];
        else :
            $end_date = date("Y-m-d");
        endif;
        
        $whr['DATE(`exp_date`) >= '] = $start_date;
        $whr['DATE(`exp_date`) <= '] = $end_date;
        
        $filter = array(
            'start_date' => date("d-m-Y", strtotime( $start_date ) ),
            'end_date'   => date("d-m-Y", strtotime( $end_date ) ),
            'type'       => $f['type'],
            'sub'        => $f['sub'],   
        );
        
        $data['filter'] = $filter;
        if(!empty($this->input->post('submit'))) {
            $data['start_date'] = date("d-m-Y", strtotime( $start_date ) );
            $data['end_date'] = date("d-m-Y", strtotime( $end_date ) );
        } else {
            $data['start_date'] = '';
            $data['end_date'] = '';
        }
        $data['types']   	= _get(TBL_TEMPLE_EXPENSES, array(), array('order_by' => 'exp_type_id', 'select' => 'DISTINCT(exp_type_id), exp_type_name'));
		$data['expenses']   = _get(TBL_TEMPLE_EXPENSES, array(), array('order_by' => 'expenses_for', 'select' => 'DISTINCT(expenses_for)'));
        $data['results'] 	= _get(TBL_TEMPLE_EXPENSES, $whr, array('order_by' => 'exp_name'));
        
        $filters = array();
        if(!empty($this->input->post('filter_columns'))) {
            foreach($this->input->post('filter_columns') as $key=>$value) {
                array_push($filters,$this->input->post($value));
            } 
        }
        $data['filters'] = array_filter($filters);
        
         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']	= "temple_chart_report.pdf";
			include($_SERVER['DOCUMENT_ROOT'].'/smmt2020/application/views/reports/temple_expenses/chartpdf.php');
        } elseif($this->input->post('submit') == 'PDF Data'){            
			$filename = 'temple_expenses_report_'.time();
			$html 	  = $this->load->view('reports/temple_expenses/pdf', $data, TRUE);		  
			$header		= '';
			$footer		= '';
			//echo $header.$html.$footer; exit;
			$this->load->helper(array('My_Pdf'));   //  Load helper
			create_pdf($header, $html, $footer, $filename);
			redirect('temple_expenses_report', 'refresh');           
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Data'))) {
		    $this->load->view('reports/temple_expenses/excel', $data);
        }else {
            $this->render($data, 'reports/temple_expenses/list');
        }        
    }  
	
	function uploadchartpdf(){
		unlink(FCPATH.'uploads/temple_pdf/temple_chart_report.pdf');
		$filename					= 'pdf';
		$config						= array();
		$config['upload_path'] 		= FCPATH.'uploads/temple_pdf/';
		$config['allowed_types'] 	= '*';
		$config['max_size']    		= '15000000';
		$config['file_name']        = "temple_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";
		} 
	}
}
?>