<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Kattalai_archanai_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
		$this->load->helper(array('form', 'url'));
        $this->load->library('upload');
        $this->load->model('reports/kattalai_archanai_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 Data'  || $this->input->post('submit') == 'Print Data')){
			$data['exceltype'] 	= 'Excel Data';
			$data['pdftype'] 	= 'PDF Data';
			$data['charttype'] 	= 'Print Chart';
		}
		elseif((!empty($this->input->post('submit'))) && ($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 Data';
			$data['pdftype'] 	= 'PDF Data';
			$data['charttype'] 	= 'Print Chart';
		}
		
		
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Data' || $this->input->post('submit') == 'Print Data' || $this->input->post('submit') == 'Excel Data' || $this->input->post('submit') == 'PDF Data' || $this->input->post('submit') == 'Chart Data')) {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $type_id = $this->input->post('type_id');
            $date_type = $this->input->post('date_type');
            $daily 		= $this->kattalai_archanai_report_model->get_daily_data($start_date,$end_date,$type_id,$date_type);
			$daily_kp 	= $this->kattalai_archanai_report_model->get_daily_prasadam_data($start_date,$end_date,$type_id,$date_type);
            #echo $this->db->last_query();die;
            $weekly = $this->kattalai_archanai_report_model->get_weekly_data($start_date,$end_date,$type_id,$date_type);
			$weekly_kp = $this->kattalai_archanai_report_model->get_weekly_prasadam_data($start_date,$end_date,$type_id,$date_type);
            $multiple = $this->kattalai_archanai_report_model->get_multiple_data($start_date,$end_date,$type_id,$date_type);
			$multiple_kp = $this->kattalai_archanai_report_model->get_multiple_prasadam_data($start_date,$end_date,$type_id,$date_type);
          
            $data['results'] = array_merge($daily,$daily_kp,$weekly,$weekly_kp,$multiple,$multiple_kp);
           
            $data['submit_type'] = 'Get Data';
        } elseif($this->input->post('submit') == 'Submit') {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('start_date');
            $type_id = '';
            $daily 		= $this->kattalai_archanai_report_model->get_daily_data($start_date,$end_date,$type_id);
			$daily_kp 	= $this->kattalai_archanai_report_model->get_daily_prasadam_data($start_date,$end_date,$type_id);
            $weekly 	= $this->kattalai_archanai_report_model->get_weekly_data($start_date,$end_date,$type_id);
			$weekly_kp = $this->kattalai_archanai_report_model->get_weekly_prasadam_data($start_date,$end_date,$type_id,$date_type);
            $multiple 	= $this->kattalai_archanai_report_model->get_multiple_data($start_date,$end_date,$type_id);
			$multiple_kp = $this->kattalai_archanai_report_model->get_multiple_prasadam_data($start_date,$end_date,$type_id,$date_type);
            
            $data['results'] = array_merge($daily,$daily_kp,$weekly,$weekly_kp,$multiple,$multiple_kp);
           
            $data['submit_type'] = 'Get Data';
        } elseif((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Details Data' || $this->input->post('submit') == 'Print Details Data' || $this->input->post('submit') == 'Excel Details Data' || $this->input->post('submit') == 'PDF Details Data' || $this->input->post('submit') == 'Chart Details Data')) {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $type_id = $this->input->post('type_id');
            $date_type = $this->input->post('date_type');
            
            $daily_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_daily($start_date,$end_date,$type,$date_type);
			$daily_deities_kp = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_prasadam_daily($start_date,$end_date,$type,$date_type);
            $weekly_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_weekly($start_date,$end_date,$type,$date_type);
			$weekly_deities_kp = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_prasadam_weekly($start_date,$end_date,$type,$date_type);
            $multiple_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_multiple($start_date,$end_date,$type,$date_type);
			$multiple_deities_kp = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_prasadam_multiple($start_date,$end_date,$type,$date_type);
            
            $deities = array_merge($daily_deities,$daily_deities_kp,$weekly_deities,$weekly_deities_kp,$multiple_deities,$multiple_deities_kp);
            
            $details_data_daily = $this->kattalai_archanai_report_model->get_details_data_by_god_daily($start_date,$end_date,$type,$date_type);
            $details_data_weekly = $this->kattalai_archanai_report_model->get_details_data_by_god_weekly($start_date,$end_date,$type,$date_type);
            $details_data_multiple = $this->kattalai_archanai_report_model->get_details_data_by_god_multiple($start_date,$end_date,$type,$date_type);
			
			$details_data_daily_kp = $this->kattalai_archanai_report_model->get_details_data_by_god_prasadam_daily($start_date,$end_date,$type,$date_type);
            $details_data_weekly_kp = $this->kattalai_archanai_report_model->get_details_data_by_god_prasadam_weekly($start_date,$end_date,$type,$date_type);
            $details_data_multiple_kp = $this->kattalai_archanai_report_model->get_details_data_by_god_prasadam_multiple($start_date,$end_date,$type,$date_type);
            
            $details_data = array_merge($details_data_daily,$details_data_daily_kp,$details_data_weekly,$details_data_weekly_kp,$details_data_multiple,$details_data_multiple_kp);
           
            foreach($deities as $k => $row) {
                $gods[$row->id] = array($row->name);
            }
            foreach($details_data as $key => $value) {
                $e = explode(',',$value->bki_god_ids);
                foreach($e as $index => $val) {
                    $gods[$e[$index]][] = array($value);
               }
            }
            
            $data['god_details'] = $gods;
            $data['submit_type'] = $this->input->post('submit');
            #echo '<pre>';print_r($data['god_details']);die;
            
        }elseif($this->input->post('print') == 'Print') {
            // Get Deity By Kattalai Archanai's God's Id
            $gods = array();
            
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('start_date');
            $type_id = '';
            $date_type = 1;
            
            /*$start_date = '';$end_date = '';$type = '';*/
            /*$daily_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_daily($start_date,$end_date,$type,$date_type);
            $weekly_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_weekly($start_date,$end_date,$type,$date_type);
            $multiple_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_multiple($start_date,$end_date,$type,$date_type);*/
            
            #$deities = $this->kattalai_archanai_report_model->get_gods_by_kattalai_items();
           /* $deities = array_merge($daily_deities,$weekly_deities,$multiple_deities);*/
		   
		    $daily_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_daily($start_date,$end_date,$type,$date_type);
			$daily_deities_kp = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_prasadam_daily($start_date,$end_date,$type,$date_type);
            $weekly_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_weekly($start_date,$end_date,$type,$date_type);
			$weekly_deities_kp = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_prasadam_weekly($start_date,$end_date,$type,$date_type);
            $multiple_deities = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_items_multiple($start_date,$end_date,$type,$date_type);
			$multiple_deities_kp = $this->kattalai_archanai_report_model->get_details_gods_by_kattalai_prasadam_multiple($start_date,$end_date,$type,$date_type);
            
            $deities = array_merge($daily_deities,$daily_deities_kp,$weekly_deities,$weekly_deities_kp,$multiple_deities,$multiple_deities_kp);
            
            foreach($deities as $k => $row) {
                $gods[$row->id] = array($row->name . ((!empty($row->tamil_name)) ? ' / <span class="linear-lbl lang-tamil"><b>' . $row->tamil_name .'</b></span>' : '') );
            } 
            
            // Get Kattalai Archanai Devotee's Details
            $details_data_daily = $this->kattalai_archanai_report_model->get_details_data_by_god_daily($start_date,$end_date,$type,$date_type);
            $details_data_weekly = $this->kattalai_archanai_report_model->get_details_data_by_god_weekly($start_date,$end_date,$type,$date_type);
            $details_data_multiple = $this->kattalai_archanai_report_model->get_details_data_by_god_multiple($start_date,$end_date,$type,$date_type);
            
			$details_data_daily_kp = $this->kattalai_archanai_report_model->get_details_data_by_god_prasadam_daily($start_date,$end_date,$type,$date_type);
            $details_data_weekly_kp = $this->kattalai_archanai_report_model->get_details_data_by_god_prasadam_weekly($start_date,$end_date,$type,$date_type);
            $details_data_multiple_kp = $this->kattalai_archanai_report_model->get_details_data_by_god_prasadam_multiple($start_date,$end_date,$type,$date_type);
            
            $today_record = array_merge($details_data_daily,$details_data_daily_kp,$details_data_weekly,$details_data_weekly_kp,$details_data_multiple,$details_data_multiple_kp);
           // $today_record = array_merge($details_data_daily,$details_data_weekly,$details_data_multiple);
            #$today_record = $this->kattalai_archanai_report_model->get_today_record_by_god();
            
            foreach($today_record as $key => $value) {
                $e = explode(',',$value->bki_god_ids);
                foreach($e as $index => $val) {
                    #echo '<pre>';print_r($value);
                    if($value->bki_day_type == 1) {
                        #$count = $value->bki_no_days;
                        #$bki_multi_date = $value->bki_dates; 
                        $multiple_date = explode(',',$value->bki_dates);
                        $start = ($start_date != '') ? $start_date : date('d-m-Y');
                        $end = ($end_date != '') ? $end_date : date('d-m-Y');
                        $count = 0;
                        $bki_date = '';
                        foreach($multiple_date as $ki=>$Row) {
                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                            if($start <= trim($Row) && $end >= trim($Row)) {
                                $count += 1;
                            }
                        }
                        $bki_multi_date = ' - ';
                    } else if ($value->bki_day_type == 2) {
                        $multiple_date = explode(',',$value->bki_dates);
                        $start = ($start_date != '') ? $start_date : date('d-m-Y');
                        $end = ($end_date != '') ? $end_date : date('d-m-Y');
                        $count = 0;
                        $bki_date = '';
                        foreach($multiple_date as $ki=>$Row) {
                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                            if($start <= trim($Row) && $end >= trim($Row)) {
                                if(date('w', strtotime($Row)) == $value->bki_week_day) {
                                    $count += 1;    
                                }
                            }
                        }
                        #$bki_multi_date = rtrim($bki_date,',');
                        $bki_multi_date = ' - ';
                    } elseif($value->bki_day_type == 3) {
                        $multiple_date = explode(',',$value->bki_dates);
                        $start = ($start_date != '') ? $start_date : date('d-m-Y');
                        $end = ($end_date != '') ? $end_date : date('d-m-Y');
                        $count = 0;
                        $bki_date = '';
                        foreach($multiple_date as $ki=>$Row) {
                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? '<span style="color:#fff;background-color: #F44336;"><b>'. $Row .'</b></span>,' : $Row . ',';
                            if($start <= trim($Row) && $end >= trim($Row)) {
                                $count += 1;
                            }
                        }
                        $bki_multi_date = rtrim($bki_date,',');
                    } 
                    if($count > 0) {
                        $gods[$e[$index]][] = array(
                            ('<div><span class="lang-tamil">' . $value->bk_tamil_name_1 . '</span>' . ((!empty($value->star_tamil)) ? ' - ' . $value->star_tamil : ''). '</div>' . ((!empty($value->bk_tamil_name_2)) ?  '<div><span class="lang-tamil">' . $value->bk_tamil_name_2 . '</span>' . ((!empty($value->star_tamil_2)) ? ' - ' . $value->star_tamil_2 : ''). '</div>' : ' - ' )),
                            ('<div>' . $value->bk_name_1 . ((!empty($value->star)) ? ' - ' . $value->star : '') . '</div>' . ((!empty($value->bk_name_2)) ?  '<div>' . $value->bk_name_2 . ((!empty($value->star2)) ? ' - ' . $value->star2 : ''). '</div>' : ' - ' ))
                        );
                    }
               }
            }
            #echo '<br/><pre>';print_r($gods);die;
            $args = array(
                'type'      => '7', // Default = 1 // Normal (Double print), '2' => Kattalai Archanai,   '3' = Hall Booking | Wedding Hall, '4' => 'Madapalli', '5' => Archanai, '6' => Common
                'rec_no'    => '',
                'title'     => 'Term Archanai Report - ' . (($start_date != '') ? date('d/m/Y',strtotime($start_date)) : date('d/m/Y')),//$main_service['service_name']. ' - '.$service['name'],
                'amount'    => array(''),
                'copy'      => FALSE,
                
                'data'      => array(
                    // Type = 7
                    'tables' => array(
                        'head'  => array('Name in Tamil', 'Name in English'),
                        'body'  => $gods,
                    ),
                ),
            );
            #echo '<pre>';print_r($args);die;
            print_rec($args);
            redirect('kiosk/index/bill','refresh');
        } else {
            $start_date = '';
            $end_date = '';
            $type_id = '';
            $daily = $this->kattalai_archanai_report_model->get_daily_data($start_date,$end_date,$type_id);
			$daily_kp 	= $this->kattalai_archanai_report_model->get_daily_prasadam_data($start_date,$end_date,$type_id);
            $weekly = $this->kattalai_archanai_report_model->get_weekly_data($start_date,$end_date,$type_id);
			$weekly_kp = $this->kattalai_archanai_report_model->get_weekly_prasadam_data($start_date,$end_date,$type_id,$date_type);
            $multiple = $this->kattalai_archanai_report_model->get_multiple_data($start_date,$end_date,$type_id);
			$multiple_kp = $this->kattalai_archanai_report_model->get_multiple_prasadam_data($start_date,$end_date,$type_id,$date_type);
            
            $data['results'] = array_merge($daily,$daily_kp,$weekly,$weekly_kp,$multiple,$multiple_kp);
            $data['submit_type'] = 'Get Data';
        }
        
        $data['start_date'] = $start_date;
        $data['end_date'] = $end_date;
        $data['type_id'] = $type_id;
        $data['date_type'] = $date_type;
        $data['types'] = $this->kattalai_archanai_report_model->list_service_type();
        $actions = $this->actions();
        $data['form_action'] = 'kattalai_archanai_report';
        $data['title'] = 'Kattalai Archanai Report';
        
        $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' || $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']	= "kattalai_chart_report.pdf";
			include($_SERVER['DOCUMENT_ROOT'].'/smmt2020/application/views/reports/kattalai_archanai/chartpdf.php');     
        } elseif($this->input->post('submit') == 'Print Data' || $this->input->post('submit') == 'Print Details Data'){
            if( IS_KSK ) :
                $html = $this->load->view('reports/kattalai_archanai/pdf', $data, TRUE);
                //echo $html; die;
                print_rec($html);
                redirect('kattalai_archanai_report','refresh');
            else :
                $this->render($data, 'reports/kattalai_archanai/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'PDF 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/kattalai_archanai/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 .='Kattalai Archanai 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/kattalai_archanai/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 = "Term_archanai_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/kattalai_archanai_pdf/');
                $pdf->setFooter('{PAGENO}');
              	$pdf->WriteHTML($htmlall);
               	$pdf->Output(FCPATH.'uploads/kattalai_archanai_pdf/'.$pdfFilePath, "F");
               	$_SESSION['pdf_kattalai_archanai']="success";
               	$_SESSION['pdf_kattalai_archanai_file']=$pdfFilePath;
                redirect("kattalai_archanai_report","refresh");
                
               //exit(0);
            else :
                $this->render($data, 'reports/kattalai_archanai/pdf');
            endif;
        } elseif(((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Excel Data' || $this->input->post('submit') == 'Excel Details Data'))) {
		    $this->load->view('reports/kattalai_archanai/excel', $data);
        } else {
            #echo '<pre>';print_r($data);die;
            $this->render($data, 'reports/kattalai_archanai/list');
        }
    }
	
	function uploadchartpdf(){
		unlink(FCPATH.'uploads/kattalai_archanai_pdf/kattalai_chart_report.pdf');
		$filename					= 'pdf';
		$config						= array();
		$config['upload_path'] 		= FCPATH.'uploads/kattalai_archanai_pdf/';
		$config['allowed_types'] 	= '*';
		$config['max_size']    		= '15000000';
		$config['file_name']        = "kattalai_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";
		}
	}
   
} 
?>