<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Statistical_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('reports/statistical_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');
        
        $data['month_list'] = $this->month_list();
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Summary Data' || $this->input->post('submit') == 'Get Details Data' || $this->input->post('submit') == 'Print Summary Data' || $this->input->post('submit') == 'Print Details Data' || $this->input->post('submit') == 'Excel Summary Data' || $this->input->post('submit') == 'Excel Details Data' || $this->input->post('submit') == 'PDF Summary Data' || $this->input->post('submit') == 'PDF Details Data')) {
            
            $start_month = $this->input->post('start_month');
            $start_year = $this->input->post('start_year');
            $end_month = $this->input->post('end_month');
            $end_year = $this->input->post('end_year');
            $mobile = $this->input->post('mobile_no');
            $top = $this->input->post('top');
            $hidden_id = 1;
            $data_type = $this->input->post('submit');
        } else {
            $start_month = date('m');
            $start_year = date('Y');
            $end_month = date('m');
            $end_year = date('Y');
            $mobile = '';
            $top = '';
            $hidden_id = '';
            $data_type = 'Get Summary Data';
        }
        $start_date = '01-' . $start_month . '-'. $start_year;
        $end_date = '31-' . $end_month . '-'. $end_year;
        
        $data['results'] = $this->statistical_report_model->devotee_list($mobile,$top);
        
        $monthYear = array();
        if(!empty($this->input->post('month_years'))) {
            foreach($this->input->post('month_years') as $key=>$value) {
                array_push($monthYear,$this->input->post($value));
            } 
        }
        $data['monthYear'] 		= array_filter($monthYear);        
        $data['start_month'] 	= $start_month;
        $data['start_year']  	= $start_year;
        $data['end_month']   	= $end_month;
        $data['end_year']    	= $end_year;
        $data['mobile']      	= $mobile;
        $data['top']         	= $top;
        $data['hidden_id']   	= $hidden_id;
        $data['data_type']   	= $data_type;
        $data['form_action'] 	= 'statistical_report';
        $data['title'] 			= 'Statistical Report';
		
		$data['pdftype'] 		= '';
		$data['exceltype'] 		= '';
		$data['charttype'] 		= '';
		
		if($this->input->post('submit') == 'Get Summary Data'  || $this->input->post('submit') == 'Print Summary Data'){
			$data['exceltype'] 	= 'Excel Summary Data';
			$data['pdftype'] 	= 'PDF Summary Data';
			$data['charttype'] 	= 'Chart Summary Data';
		}
		elseif($this->input->post('submit') == 'Get Details Data' || $this->input->post('submit') == 'Print Details Data'){
			$data['exceltype'] 	= 'Excel Details Data';	
			$data['pdftype'] 	= 'PDF Details Data';
			$data['charttype'] 	= 'Chart Details Data';
		}else{
			$data['exceltype']  = 'Excel Summary Data';
			$data['pdftype'] 	= 'PDF Summary Data';
			$data['charttype'] 	= 'Chart Summary Data';
		}
        
        if($this->input->post('submit') == 'Chart Summary Data' || $this->input->post('submit') == 'Chart Details Data' ){
			//echo "<pre>"; print_r($data); exit;
            $data['dataType'] = $this->input->post('submit');
			$html = $this->load->view('reports/statistical/chartpdf', $data, TRUE);				
			print_rec($html, $type = "chart");
			redirect('statistical_report','refresh');
        } elseif($this->input->post('submit') == 'Print Summary Data' || $this->input->post('submit') == 'Print Details Data' ){
            #$data['data_type'] = $this->input->post('submit');
            if( IS_KSK ) :
                $html = $this->load->view('reports/statistical/pdf', $data, TRUE);
                print_rec($html);
                redirect('statistical_report','refresh');
            else :
                $this->render($data, 'reports/statistical/pdf');
            endif;
        } elseif($this->input->post('print') == 'Print') {
           redirect('kiosk/index/bill','refresh');
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'PDF Summary Data' || $this->input->post('submit') == 'PDF Details Data'))) {
		  	$data['dataType'] = $this->input->post('submit');
			$user_data 			= get_session_userdata();
			
            if (IS_KSK) :
                $html = $this->load->view('reports/statistical/pdf', $data, TRUE);
                $args = array();
                if (!is_array($args)) :
                    $is_html = TRUE;
                    $args = array('raw_html' => $args, 'copy' => FALSE);
                endif;
                $type = !empty($args['type']) ? $args['type'] : '1';
                define('IS_SHORT', ($type == '5' || $args['short'] ? TRUE : FALSE));
                define('WIDTH', IS_SHORT ? '287' : '1000'); //'562');
                // $args['is_copy'] = ($type == '1' && !isset($args['copy'])) || $args['copy'] ? TRUE : FALSE;
                $args['is_copy'] = (( $type == '12' || $type == '13' || $type == '14' || $type == '15' || $type == '16' || $type == '17') && !isset($args['copy'])) || $args['copy'] ? TRUE : FALSE;
                $args['sepr'] = '&nbsp;/&nbsp;';
                $q = $this->db->get_where(TBL_SETTINGS_BASIC);
                $args['settings'] = $q->row_array();
                $settings=$args['settings'];
                $q = $this->db->get_where(TBL_SETTINGS_DESIGN);
                $args['settings'] = array_merge($args['settings'], $q->row_array());
//                print_r($args);
               // $header = $this->load->view('templates/print/pdfheader', $args, TRUE);
                $logo_url = !empty($args['settings']['logo']) && file_exists(DIR_COMPANY_LOGO. $args['settings']['logo']) ? DIR_COMPANY_LOGO . $args['settings']['logo'] : 'assets/images/logo.png';
              // $htmlall ='<div class="clear" style="clear:both;height:9em"></div>';
				$search_date=$date;               
                $htmlall ='<table width="100%" style="font-family:Verdana, Geneva, sans-serif">';
                $htmlall .='<tr>';
                $htmlall .=' <td width="5%"><div style="min-width: 115px;">';
                $htmlall .='<img src="'. $logo_url.'" style="width:100px" />';
                $htmlall .='</div></td>';
                $htmlall .=' <td width="80%" align="center">';
                $htmlall .=' <table border="0" width="100%">';
                $htmlall .=' <tr>';
                $htmlall .=' <td align="center" style="font-size:14px;">';
                $htmlall .=$settings['temple_name'];
                $htmlall .='</tr>';
                $htmlall .='</table>';
                $htmlall .='</td>';
                $htmlall .='<tr><td colspan="3"><hr></td></tr>';
                $htmlall .='</table>';
                $htmlall .='<table width="100%" border="0" width="100%" cellpadding="5" style="font-family:Verdana, Geneva, sans-serif">';
                $htmlall .='<tr>';
                $htmlall .='<td width="25%">&nbsp;</td>';
                $htmlall .='<td align="center" width="50%" style="font-weight:bold; font-size:14px;">';
                $htmlall .='Statistical Report</td>';
                $htmlall .='<td width="25%" style="font-size:13px;">';               
				$htmlall .='Printed On:';
                $htmlall .=$user_data['name']."-".date("Y-m-d H:i:s");
                $htmlall .='</td>';
                $htmlall .=' </tr>';
                $htmlall .=' </table>';       
           
              
                $html = $this->load->view('reports/statistical/pdf', $data, TRUE);
                /*
				 if (count($data['nets_results']) > 0 || count($data['cheque_results']) > 0) {
                     $html .=$this->load->view('reports/daily_summary/pdf_nets', $data, TRUE);
                 }
               */
                $htmlall .= $html;
        
                $filename = "statistical_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/statistical_pdf/');
                $pdf->setFooter('{PAGENO}');
              	$pdf->WriteHTML($htmlall);
               	$pdf->Output(FCPATH.'uploads/statistical_pdf/'.$pdfFilePath, "F");
               	$_SESSION['pdf_statistical']="success";
               	$_SESSION['pdf_statistical_file']=$pdfFilePath;
                redirect("statistical_report","refresh");
                
               //exit(0);
            else :
                $this->render($data, 'reports/statistical/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Summary Data' || $this->input->post('submit') == 'Excel Details Data'))) {
		    $this->load->view('reports/statistical/excel', $data);
        } else {
            $this->render($data, 'reports/statistical/list');
        }
       
    }
    
    public function month_list() {
        $month_arr = array(
            '01' => 'Jan',
            '02' => 'Feb',
            '03' => 'Mar',
            '04' => 'Apr',
            '05' => 'May',
            '06' => 'Jun',
            '07' => 'Jul',
            '08' => 'Agu',
            '09' => 'Sep',
            '10' => 'Oct',
            '11' => 'Nov',
            '12' => 'Dec'
        );
        return $month_arr;
        
    }
   
} ?>

