<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Donation_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
		$this->load->helper(array('form', 'url'));
        $this->load->library('upload');
        $this->load->model('reports/donation_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['exceltype'] 	= '';
		$data['pdftype'] 	= '';
		$data['charttype'] 	= '';
		
		if((!empty($this->input->post('submit'))) && ($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((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Pledge Data' || $this->input->post('submit') == 'Print Pledge Data')){
			$data['exceltype']	= 'Excel Pledge Data';	
			$data['pdftype'] 	= 'PDF Pledge Data';
			$data['charttype'] 	= 'Chart Pledge Data';
		}else{
			$data['exceltype'] 	= 'Excel Summary Data';
			$data['pdftype'] 	= 'PDF Summary Data';
			$data['charttype'] 	= 'Chart Summary Data';
		}
        
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Summary Data' || $this->input->post('submit') == 'Print Summary Data' || $this->input->post('submit') == 'Excel Summary Data' || $this->input->post('submit') == 'PDF Summary Data' || $this->input->post('submit') == 'Chart Summary Data' || $this->input->post('submit') == 'Get Pledge Data' || $this->input->post('submit') == 'Print Pledge Data' || $this->input->post('submit') == 'Excel Pledge Data' || $this->input->post('submit') == 'Chart Pledge Data' || $this->input->post('submit') == 'PDF Pledge Data')) {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $type_id = $this->input->post('type_id');
            $mobile_no = $this->input->post('mobile_no');
            $pledge = $this->input->post('pledge');
            $pledge_type = $this->input->post('pledge_type');
            $data_type = $this->input->post('submit');
        } else {
            $start_date = '';
            $end_date = '';
            $type_id = '';
            $mobile_no = '';
            $pledge = '';
            $pledge_type = '';
            $data_type = 'Get Summary Data';
        }
        
        if($pledge == 2) {
            $data['results'] = $this->donation_report_model->get_data($start_date,$end_date,$type_id,$mobile_no,$pledge,$pledge_type);
        } else{
            $data['results'] = $this->donation_report_model->get_pledge_data($start_date,$end_date,$type_id,$mobile_no,$pledge,$pledge_type);
        }
        
        $data['start_date'] = $start_date;
        $data['end_date'] = $end_date;
        $data['type_id'] = $type_id;
        $data['mobile_no'] = $mobile_no;
        $data['pledge'] = $pledge;
        $data['pledge_type'] = $pledge_type;
        $data['types'] = $this->donation_report_model->list_service_type();
        $data['data_type'] = $data_type;
        $actions = $this->actions();
        $data['form_action'] = 'donation_report';
        $data['title'] = 'Donation Report';
        $data['hidden_print'] = 1;
        
        $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 Summary Data' || $this->input->post('submit') == 'Chart Pledge 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']	= "donation_chart_report.pdf";
			include($_SERVER['DOCUMENT_ROOT'].'/smmt2020/application/views/reports/donation/chartpdf.php'); 
        } else if($this->input->post('submit') == 'Print Summary Data' || $this->input->post('submit') == 'Print Pledge Data'){
            if( IS_KSK ) :
                $html = $this->load->view('reports/donation/pdf', $data, TRUE);
                print_rec($html);
                redirect('donation_report','refresh');
            else :
                $this->render($data, 'reports/donation/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'PDF Summary Data' || $this->input->post('submit') == 'PDF Pledge Data'))) {
		  	$data['dataType'] = $this->input->post('submit');
			$user_data 			= get_session_userdata();
			
            if (IS_KSK) :
                $html = $this->load->view('reports/donation/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 width="100%" align="center">';
                $htmlall .=' <span style="float:left;font-family:Arial, Helvetica, sans-serif;font-weight:bold; margin-bottom: 20px;"><span style="font-size:24px;"class="lang-tamil">';
             //   $htmlall .= $settings['tamil_name']; 
                $htmlall .='</span><br/><span style="font-size:20px;">';
                $htmlall .=$settings['temple_name'];
                $htmlall .='</span><br/><span style="font-size:14px;">';
                $htmlall .=$settings['address'];
                $htmlall .='</span><br/><span style="font-size:14px;">';
                $htmlall .='UEN : S75SS0033J | Tel:';
                $htmlall .= $settings['contact_no'];
                $htmlall .='</span><br/><span style="font-size:14px;">';
                $htmlall .=' www.arulmigumurugantemple.com </span>';
                $htmlall .=' <td width="15%">&nbsp;</td>';
             
            
                $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 align="right" width="100%" style="font-size:13px;">';               
				$htmlall .='Printed On:';
                $htmlall .=$user_data['name']."-".date("Y-m-d H:i:s");
                $htmlall .='</td>';
                $htmlall .=' </tr>';
                $htmlall .='<tr>';
               
                $htmlall .='<td align="center" width="80%" style="font-weight:bold; font-size:18px;font-family:Arial, Helvetica, sans-serif;">';
                $htmlall .='Donation Report </td>';
              
                $htmlall .=' </tr>';
                $htmlall .=' </table>'; 
           
              
                $html = $this->load->view('reports/donation/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 = "donation_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/donation_pdf/');
                $pdf->setFooter('{PAGENO}');
              	$pdf->WriteHTML($htmlall);
               	$pdf->Output(FCPATH.'uploads/donation_pdf/'.$pdfFilePath, "F");
               	$_SESSION['pdf_donation']="success";
               	$_SESSION['pdf_donation_file']=$pdfFilePath;
                redirect("donation_report","refresh");
                
               //exit(0);
            else :
                $this->render($data, 'reports/donation/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Summary Data' || $this->input->post('submit') == 'Excel Pledge Data'))) {
		    $this->load->view('reports/donation/excel', $data);
        } else {
            $this->render($data, 'reports/donation/list');
        }
    }
	
	function uploadchartpdf(){		
		unlink(FCPATH.'uploads/donation_pdf/donation_chart_report.pdf');
		$filename					= 'pdf';
		$config						= array();
		$config['upload_path'] 		= FCPATH.'uploads/donation_pdf/';
		$config['allowed_types'] 	= '*';
		$config['max_size']    		= '15000000';
		$config['file_name']        = "donation_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";
		}
	}
   
} 
?>