<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Prasadam_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('billing/prasadam_model');
        $this->load->model('reports/prasadam_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' || $this->input->post('submit') == 'XLS Data' )) {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $type_id = $this->input->post('type_id');
            $period = $this->input->post('period');

            $date_type = $this->input->post('date_type');
        } elseif($this->input->post('submit') == 'Submit' || $this->input->post('print') == 'Print') {
            $start_date = $this->input->post('start_date');
            $type_id = $this->input->post('type_id');
            $end_date = $this->input->post('start_date');
            $date_type = 1;
        } else {
            $start_date = '';
            $end_date = '';
            $period = '';
            $type_id = '';
            $date_type = 1;
        }
        $data['results'] = $this->prasadam_report_model->get_data($start_date,$end_date,$period,$date_type,$type_id);
        if($this->input->post('print') == 'Print') {
            $start_date = $end_date = $this->input->post('start_date');
            $type_id = $this->input->post('type_id');
            $period = $this->input->post('period');
            
            //Get God Id's for all modules
            $abishegam = $this->prasadam_report_model->get_abishegam_god_prasadam($start_date,$end_date,$period);
            $festival = $this->prasadam_report_model->get_festival_god_prasadam($start_date,$end_date,$period);
            $mega = $this->prasadam_report_model->get_mega_god_prasadam($start_date,$end_date,$period);
            $ubayam = $this->prasadam_report_model->get_ubayam_god_prasadam($start_date,$end_date,$period);
            $prasadam = $this->prasadam_report_model->get_prasadam_god_prasadam($start_date,$end_date,$period);
            $madappalli = $this->prasadam_report_model->get_madappalli_god_prasadam($start_date,$end_date,$period);
            
            $array_merge = array_merge($abishegam,$festival,$mega,$ubayam,$prasadam,$madappalli);
            $result_data = array_unique($array_merge,SORT_REGULAR);
            #echo '<pre>';print_r($result_data);
            #die;
            $a = array();
            $head = array();
            $gods = array();
            $body = array();
            $head['attr'] = 'style="height: 180px; max-width: 50px; padding: 2px; '; //max-width: 80px;
            $head['sno'] = 0;
            $head[] = 'God Name' . '<style type="text/css">td { font-size: 20px !important; } td input{ font-size: 28px !important; letter-spacing: 2px; } </style>';
            
            //echo '<pre>';print_r($result_data); die;
            foreach($result_data as $key => $value) {
                
                //Get Prasadam details by God Id
                $ab_prasadam = $this->prasadam_report_model->get_ab_prasadam($value->id,$start_date,$end_date,$period);
                $ff_prasadam = $this->prasadam_report_model->get_ff_prasadam($value->id,$start_date,$end_date,$period);
                $mega_prasadam = $this->prasadam_report_model->get_mega_prasadam($value->id,$start_date,$end_date,$period);
                $ubayam_prasadam = $this->prasadam_report_model->get_ubayam_prasadam($value->id,$start_date,$end_date,$period);
                $prasadam_item = $this->prasadam_report_model->get_prasadam_item($value->id,$start_date,$end_date,$period);
                $madappalli_prasadam = $this->prasadam_report_model->get_madappalli_prasadam($value->id,$start_date,$end_date,$period);
                
                $array_item_merge = array_merge($ab_prasadam,$ff_prasadam,$mega_prasadam,$ubayam_prasadam,$prasadam_item,$madappalli_prasadam);
                
                $prasadam_qty  = array_unique($array_item_merge,SORT_REGULAR);
                
                foreach($prasadam_qty as $k => $itm_qty) {
                    $a[get_var_name($itm_qty->name).'-'.$itm_qty->prasadam_id][$value->id] = array('prasadam_id' => $itm_qty->prasadam_id, 'prasadam' => $itm_qty->name . ' / ' . tamil($itm_qty->tamil_name), 'qty' => $itm_qty->prasadam_qty);
                }
                
                $head[] = '<div style="transform: rotate(-90deg); white-space: nowrap; max-width: 37px; max-height: 60px; margin-left: 12px; margin-top: 190px;">'.$value->god_name . '<div class="lang-tamil">'. $value->god_tamil . '</div></div>';
                
                $gods[$value->id] = $value->id;
            }
            
            $wid = floor(90/(count($head)-1));
            $head['attr'] .= ($wid > 0 ? 'width : '.$wid.'%;' : '') . ' "';
            
            $head[] = '<div style="transform: rotate(-90deg); white-space: nowrap; max-width: 37px; max-height: 60px; margin-left: 12px; margin-top: 190px;">Total<div class="lang-tamil""></div>';
           
            
            
            foreach($a as $index => $items) {
                $row = array();
                $total = 0;
                foreach($gods as $god_id => $god) :            
                    $qty = !empty( $items[$god_id] ) ? $items[$god_id]['qty'] : '-';
                    if( empty( $row[0] ) ) :
                        $row[0] = $items[$god_id]['prasadam'];
                    endif;
                    $row[] = $qty;
                    $total += is_numeric($qty) ? $qty : 0;
                endforeach;
                $row[] = $total;
                $body[] = $row;
            }
            #print_r(max($b));
            
            //echo '<pre>';print_r($body); die;
            
            $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'     => 'Prasadam Consolidated Report - ' . (date('d/m/Y',strtotime($this->input->post('start_date')))) . ((!empty($this->input->post('period')) && $this->input->post('period') == 1) ? ' (Morning)' : ' (Evening)'),
                'amount'    => array(''),
                'copy'      => FALSE,
                
                'data'      => array(
                    // Type = 6
                    'tables' => array(
                        'head'  => $head,
                        'body'  => $body,
                    ),
                ),
            );
            
            
            #echo '<pre>';print_r($args);die;

            print_rec($args);            
                
        }
        $data['start_date'] = $start_date;
        $data['end_date'] = $end_date;
        $data['period'] = $period;
        $data['type_id'] = $type_id;
        $data['date_type'] = $date_type;
        $actions = $this->actions();
        $data['form_action'] = 'prasadam_report';
        $data['title'] = 'Prasadam Report';
        $data['prasadam_type'] = $this->prasadam_model->get( TBL_SERVICE, array( 'menu_name' => 15) );
        //echo "<pre>Data: "; print_r($data); "</pre>"; die;
        $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') == 'XLS Data'){
        if($this->input->post('submit') == 'Print Data'){
            if( IS_KSK ) :
                $html = $this->load->view('reports/prasadam/pdf', $data, TRUE);
                print_rec($html);
                redirect('prasadam_report','refresh');
            else :
                $this->render($data, 'reports/prasadam/pdf');
            endif;
			            }else{        
              $this->load->view('reports/prasadam/excel_data', $data); 
            }
        } elseif($this->input->post('print') == 'Print') {
           redirect('kiosk/index/bill','refresh');
        } else {
            $this->render($data, 'reports/prasadam/list');
        }
    }
   
} ?>

