<?php
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
ini_set('memory_limit', '1024M');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Monthly_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
		$this->load->helper(array('form', 'url'));
        $this->load->library('upload');
        $this->load->model('reports/monthly_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') == 'Get Summary Data'  || $this->input->post('submit') == 'Section Wise Report'  || $this->input->post('submit') == 'Print Summary Data' || $this->input->post('submit') == 'Pdf Summary Data' || $this->input->post('submit') == 'Excel Summary Data' || $this->input->post('submit') == 'Get Details Data' || $this->input->post('submit') == 'Print Details Data' || $this->input->post('submit') == 'Pdf Details Data' || $this->input->post('submit') == 'Excel Details Data' || $this->input->post('submit') == 'Chart Summary Data' || $this->input->post('submit') == 'Chart Details Data')) {
           //echo "<pre>"; print_r($this->input->post()); //exit;
		    $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $data_type = $this->input->post('submit');
			$archanai = $this->monthly_model->get_archanai_data($start_date,$end_date,$data_type);
			//echo '<pre>';print_r($archanai);exit;
            $abishegam = $this->monthly_model->get_abishegam_data($start_date,$end_date,$data_type);
			$homam = $this->monthly_model->get_homam_data($start_date,$end_date,$data_type);
            $festival = $this->monthly_model->get_festival_data($start_date,$end_date,$data_type);
            $ubayam = $this->monthly_model->get_ubayam_data($start_date,$end_date,$data_type);
            $mega = $this->monthly_model->get_mega_data($start_date,$end_date,$data_type);
            $other = $this->monthly_model->get_other_service_data($start_date,$end_date,$data_type);
            $gold_silver = $this->monthly_model->get_gold_silver_data($start_date,$end_date,$data_type);
            $temple = $this->monthly_model->get_temple_activity_data($start_date,$end_date,$data_type);
            $member_subscripe = $this->monthly_model->get_member_subscribe_data($start_date,$end_date,$data_type);
            $building_fund  = $this->monthly_model->get_building_fund_data($start_date,$end_date,$data_type);
            $outdoor = $this->monthly_model->get_outdoor_data($start_date,$end_date,$data_type);
            $prasadam = $this->monthly_model->get_prasadam_data($start_date,$end_date,$data_type);
            $madappalli = $this->monthly_model->get_madappalli_data($start_date,$end_date,$data_type);
            $donation = $this->monthly_model->get_donation_data($start_date,$end_date,$data_type);
            $hall_booking = $this->monthly_model->get_hall_booking_data($start_date,$end_date,$data_type);
            $wedding_hall = $this->monthly_model->get_wed_hall_booking_data($start_date,$end_date,$data_type);
            $kattalai = $this->monthly_model->get_kattalai_data($start_date,$end_date,$data_type);
            $membership = $this->monthly_model->get_membership_data($start_date,$end_date,$data_type);
            $card_topup = $this->monthly_model->get_membership_card_topup_data($start_date,$end_date,$data_type);
            $temple_shop = $this->monthly_model->get_temple_shop_data($start_date,$end_date,$data_type);
			
        }else  {
            $start_date = '';
            $end_date = '';
            $data_type = 'Get Summary Data';
            $abishegam = $this->monthly_model->get_abishegam_data($start_date = '',$end_date = '',$data_type);
			$homam = $this->monthly_model->get_homam_data($start_date = '',$end_date = '',$data_type);
            $festival = $this->monthly_model->get_festival_data($start_date = '',$end_date = '',$data_type);
            $ubayam = $this->monthly_model->get_ubayam_data($start_date = '',$end_date = '',$data_type);
            $mega = $this->monthly_model->get_mega_data($start_date = '',$end_date = '',$data_type);
            $other = $this->monthly_model->get_other_service_data($start_date = '',$end_date = '',$data_type);
            $gold_silver = $this->monthly_model->get_gold_silver_data($start_date = '',$end_date = '',$data_type);
            $temple = $this->monthly_model->get_temple_activity_data($start_date = '',$end_date = '',$data_type);
            $member_subscripe = $this->monthly_model->get_member_subscribe_data($start_date = '',$end_date = '',$data_type);
            $building_fund  = $this->monthly_model->get_building_fund_data($start_date = '',$end_date = '',$data_type);
            $outdoor = $this->monthly_model->get_outdoor_data($start_date = '',$end_date = '',$data_type);
            $prasadam = $this->monthly_model->get_prasadam_data($start_date = '',$end_date = '',$data_type);
            $madappalli = $this->monthly_model->get_madappalli_data($start_date = '',$end_date = '',$data_type);
            $donation = $this->monthly_model->get_donation_data($start_date = '',$end_date = '',$data_type);
            $hall_booking = $this->monthly_model->get_hall_booking_data($start_date = '',$end_date = '',$data_type);
            $wedding_hall = $this->monthly_model->get_wed_hall_booking_data($start_date = '',$end_date = '',$data_type);
            $archanai = $this->monthly_model->get_archanai_data($start_date = '',$end_date = '',$data_type);
            $kattalai = $this->monthly_model->get_kattalai_data($start_date = '',$end_date = '',$data_type);
            $membership = $this->monthly_model->get_membership_data($start_date = '',$end_date = '',$data_type);
            $card_topup = $this->monthly_model->get_membership_card_topup_data($start_date = '',$end_date = '',$data_type);
            $temple_shop = $this->monthly_model->get_temple_shop_data($start_date = '',$end_date = '',$data_type);
        }
       
        $data['results'] = array_merge($abishegam,$archanai,$building_fund,$donation,$festival,$gold_silver,$hall_booking,$homam,$kattalai,$madappalli,$mega,$member_subscripe,$membership,$card_topup,$outdoor,$other,$prasadam,$temple,$temple_shop,$ubayam,$wedding_hall); //--
        //echo '<pre>';print_r($data['results']);
        $data['start_date'] = $start_date;
        $data['end_date'] = $end_date;
        $data['type'] = $data_type;
        $data['mode'] = $this->monthly_model->payment_type_list();
        $actions = $this->actions();
        $data['form_action'] = 'monthly_report';
        $data['title'] = 'Monthly Summary Report';
		
		$data['pdftype'] 	= '';
		$data['exceltype']	= '';
		$data['charttype'] 	= '';
		
		if($this->input->post('submit') == 'Get Summary Data'  || $this->input->post('submit') == 'Print Summary Data'){
			$data['pdftype'] 	= 'Pdf Summary Data';
			$data['exceltype'] 	= 'Excel Summary Data';
			$data['charttype'] 	= 'Chart Summary Data';
		}
		elseif($this->input->post('submit') == 'Get Details Data' || $this->input->post('submit') == 'Print Details Data'){
			$data['pdftype'] 	= 'Pdf Details Data';	
			$data['exceltype'] 	= 'Excel Details Data';
			$data['charttype'] 	= 'Chart Details Data';
		}else{
			$data['pdftype'] 	= 'Pdf Summary Data';
			$data['exceltype'] 	= 'Excel Summary Data';
			$data['charttype'] 	= 'Chart Summary Data';
		}
        
        $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 Details 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']	= "monthly_chart_report.pdf";
			include($_SERVER['DOCUMENT_ROOT'].'/smmt/application/views/reports/monthly/chartpdf.php');
        } elseif($this->input->post('submit') == 'Print Summary Data' || $this->input->post('submit') == 'Print Details Data' ){
            $data['dataType'] = $this->input->post('submit');
            if( IS_KSK ) :
                $html = $this->load->view('reports/monthly/pdf', $data, TRUE);
                print_rec($html);
                redirect('monthly_report','refresh');
            else :
                $this->render($data, 'reports/monthly/pdf');
            endif;
        } else if ($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/monthly/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 .='Monthly Summary Report';
                $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/monthly/pdf', $data, TRUE);
               
                $htmlall .= $html;
        
                $filename = "monthly_summary_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/monthly_pdf/');
                $pdf->setFooter('{PAGENO}');
              	$pdf->WriteHTML($htmlall);
               	$pdf->Output(FCPATH.'uploads/monthly_pdf/'.$pdfFilePath, "F");
               	$_SESSION['pdf_monthly']="success";
               	$_SESSION['pdf_monthly_file']=$pdfFilePath;
                redirect("monthly_report","refresh");
                
               //exit(0);
            else :
                $this->render($data, 'reports/monthly/pdf');
            endif;
        } else if ($this->input->post('submit') == 'Section Wise Report') {
			
            $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());
			
			$data['abishegam'] 			= $abishegam;
			$data['archanai'] 			= $archanai;
			$data['building_fund'] 		= $building_fund;
			$data['donation'] 			= $donation;
			$data['festival'] 			= $festival;
			$data['gold_silver'] 		= $gold_silver;
			$data['hall_booking'] 		= $hall_booking;
			$data['homam'] 				= $homam;
			$data['kattalai'] 			= $kattalai;
			$data['madappalli'] 		= $madappalli;
			$data['mega'] 				= $mega;
			$data['member_subscripe']	= $member_subscripe;
			$data['membership']			= $membership;
			$data['card_topup']			= $card_topup;
			$data['outdoor']			= $outdoor;
			$data['other']				= $other;
			$data['prasadam']			= $prasadam;
			$data['temple']				= $temple;
			$data['temple_shop']		= $temple_shop;
			$data['ubayam']				= $ubayam;
			$data['wedding_hall']		= $wedding_hall;
			
			$user_data 					= get_session_userdata();
			$_SESSION['pdf_section']		= "success";
            $_SESSION['pdf_section_file']	= "monthly_section_report.pdf";	
			
			include($_SERVER['DOCUMENT_ROOT'].'/smmt/application/views/reports/monthly/separate_pdf.php');			
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Summary Data' || $this->input->post('submit') == 'Excel Details Data'))) {
		    $this->load->view('reports/monthly/excel', $data);
        } else {
            $this->render($data, 'reports/monthly/list');
        }
    }
	
	function uploadchartpdf(){		
		unlink(FCPATH.'uploads/monthly_pdf/monthly_chart_report.pdf');
		$filename					= 'pdf';
		$config						= array();
		$config['upload_path'] 		= FCPATH.'uploads/monthly_pdf/';
		$config['allowed_types'] 	= '*';
		$config['max_size']    		= '15000000';
		$config['file_name']        = "monthly_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";
		}
	}
	
	function generate_chart_report(){
		unlink(FCPATH.'uploads/monthly_pdf/monthly_chart_report.pdf');
		$html			= $_POST['variable'];
		$pdfFilePath 	= "monthly_chart_report.pdf";
		$this->load->library('m_pdf');
		$pdf = $this->m_pdf->load();
		$pdf->SetAutoFont(AUTOFONT_ALL);
		//                $pdf->AddPage("L");
		 $pdf->AddPage('P', // L - landscape, P - portrait
		'', '', '', '',
		5, // margin_left
		5, // margin right
		5, // margin top
		5, // margin bottom
		3, // margin header
		3);
		unlink(FCPATH.'uploads/monthly_pdf/');
		$pdf->setFooter('{PAGENO}');
		$pdf->WriteHTML($html);
		$pdf->Output(FCPATH.'uploads/monthly_pdf/'.$pdfFilePath, "F");
	}
	
	function generate_section_report(){
		//echo "<pre>"; print_r($_POST); exit;
		unlink(FCPATH.'uploads/monthly_pdf/monthly_section_report.pdf');
		$html			= $_POST['variable'];
		$pdfFilePath 	= "monthly_section_report.pdf";
		$this->load->library('m_pdf');
		$pdf = $this->m_pdf->load();
		$pdf->SetAutoFont(AUTOFONT_ALL);
		//                $pdf->AddPage("L");
		 $pdf->AddPage('P', // L - landscape, P - portrait
		'', '', '', '',
		5, // margin_left
		5, // margin right
		5, // margin top
		5, // margin bottom
		3, // margin header
		3);
		unlink(FCPATH.'uploads/monthly_pdf/');
		$pdf->setFooter('{PAGENO}');
		$pdf->WriteHTML($html);
		$pdf->Output(FCPATH.'uploads/monthly_pdf/'.$pdfFilePath, "F");
	}
   
} 
?>