<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Kattalai_archanai_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $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');
        
        if((!empty($this->input->post('submit'))) && ($this->input->post('submit') == 'Get Data' || $this->input->post('submit') == 'Print 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);
            #echo $this->db->last_query();die;
            $weekly = $this->kattalai_archanai_report_model->get_weekly_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);
          
            $data['results'] = array_merge($daily,$weekly,$multiple);
           
            $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);
            $weekly = $this->kattalai_archanai_report_model->get_weekly_data($start_date,$end_date,$type_id);
            $multiple = $this->kattalai_archanai_report_model->get_multiple_data($start_date,$end_date,$type_id);
            
            $data['results'] = array_merge($daily,$weekly,$multiple);
           
            $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')) {
            $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);
            $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 = array_merge($daily_deities,$weekly_deities,$multiple_deities);
            
            $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 = array_merge($details_data_daily,$details_data_weekly,$details_data_multiple);
           
            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') {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('start_date');
            $type_id = $this->input->post('type_id');

            $daily = $this->kattalai_archanai_report_model->get_daily_data($start_date,$end_date,$type_id);
            $weekly = $this->kattalai_archanai_report_model->get_weekly_data($start_date,$end_date,$type_id);
            $multiple = $this->kattalai_archanai_report_model->get_multiple_data($start_date,$end_date,$type_id);
            $results = array_merge($daily,$weekly,$multiple);

            $body = array();
            $total_amt = 0;
            foreach($results as $key=>$value) {
                $total_amt += $$value->bmi_sub_total;

                if($value->bki_day_type == 1) {
                    $count = $value->bki_no_days;
                    #$bki_multi_date = $value->bki_dates;
                    $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 $k=>$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 $k=>$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($value->bki_god_ids != '') {
                    $god_id = explode(',',$value->bki_god_ids);
                    $god_name = $this->kattalai_archanai_report_model->get_deity_name($god_id);
                }

               $body[] = array(($value->bk_rec_no . ' / ' . $value->service_name), $value->bk_name_1, $value->star, $god_name->deity, date('d-m-Y',strtotime($value->bki_from_date)), date('d-m-Y',strtotime($value->bki_to_date)), $value->day_type, $count, $value->bki_no_days, $bki_multi_date, $value->bki_amount);
            }
            $args = array(
                'type'      => '6', // Default = 1 // Normal (Double print), '2' => Kattalai Archanai,   '3' = Hall Booking | Wedding Hall, '4' => 'Madapalli', '5' => Archanai, '6' => Common
                'rec_no'    => '',
                'title'     => 'Kattalai Archanai Report',//$main_service['service_name']. ' - '.$service['name'],
                'amount'    => array(''),
                'copy'      => FALSE,
                
                'data'      => array(
                    // Type = 6
                    'tables' => array(
                        'head'  => array('Receipt No / Type', 'Name', 'Star', 'Deity', 'From Date', 'To Date', 'Day Type', 'Day Count', 'Total Days', 'Multiple Dates', 'Amount'),
                        'body'  => $body,
                    ),
                ),
            );
            print_rec($args);
            redirect('kiosk/index/bill','refresh');

        }  */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);
            
            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);
            
            $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'     => 'Kattalai 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);
            $weekly = $this->kattalai_archanai_report_model->get_weekly_data($start_date,$end_date,$type_id);
            $multiple = $this->kattalai_archanai_report_model->get_multiple_data($start_date,$end_date,$type_id);
            
            $data['results'] = array_merge($daily,$weekly,$multiple);
            $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') == '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;
        } else {
            #echo '<pre>';print_r($data);die;
            $this->render($data, 'reports/kattalai_archanai/list');
        }
    }
   
} ?>

