<?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('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')) {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $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');
            $period = $this->input->post('period');
            $end_date = $this->input->post('start_date');
            $date_type = 1;
        } else {
            $start_date = '';
            $end_date = '';
            $period = '';
            $date_type = 1;
        }
        $data['results'] = $this->prasadam_report_model->get_data($start_date, $end_date, $period, $date_type);
        if ($this->input->post('print') == 'Print') {
            $start_date = $end_date = $this->input->post('start_date');
            $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();
            $aq = array();
            $asq = array();
            $head = array();
            $gods = array();
            $body = array();
            $god = 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;
            //$data['results'] = array_merge($result_data,$data['results']);
            $qtyal = array();
            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);

                $ab_prasadam_qty = $this->prasadam_report_model->get_ab_prasadam_qty($value->id, $start_date, $end_date, $period);

                $ff_prasadam_qty = $this->prasadam_report_model->get_ff_prasadam_qty($value->id, $start_date, $end_date, $period);

                $mega_prasadam_qty = $this->prasadam_report_model->get_mega_prasadam_qty($value->id, $start_date, $end_date, $period);

                $ubayam_prasadam_qty = $this->prasadam_report_model->get_ubayam_prasadam_qty($value->id, $start_date, $end_date, $period);

                $prasadam_item_qty = $this->prasadam_report_model->get_prasadam_item_qty($value->id, $start_date, $end_date, $period);
//                print_r($prasadam_item_qty);
                $madappalli_prasadam_qty = $this->prasadam_report_model->get_madappalli_prasadam_qty($value->id, $start_date, $end_date, $period);
                $array_qty_merge_qty = array_merge($ab_prasadam_qty, $ff_prasadam_qty, $mega_prasadam_qty, $ubayam_prasadam_qty, $prasadam_item_qty, $madappalli_prasadam_qty);

                $prasadam_qty = array_unique($array_item_merge, SORT_REGULAR);


                //echo "<pre>";print_r($prasadam_qty); die();
                // 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);
                //}


                $quantity = array();
                $i = 1;

                foreach ($array_item_merge as $k => $itm_qty) {
                    $quantity[get_var_name($itm_qty->name) . '_' . $i] = $itm_qty->prasadam_qty;
                    if (array_key_exists(get_var_name($itm_qty->name) . '-' . $itm_qty->prasadam_id, $a)) {
                        $quant = (($a[get_var_name($itm_qty->name) . '-' . $itm_qty->prasadam_id][$value->id]['qty']) + $itm_qty->prasadam_qty);
                    } else {
                        $quant = $itm_qty->prasadam_qty;
                    }
                    $qty = $itm_qty->prasadam_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' => $quant);

                    $i++;
                }
                $quantity_qty = array();
                $j = 0;

                foreach ($array_qty_merge_qty as $k => $itm_qty) {
                    $quantity[get_var_name($itm_qty->name) . '_' . $i] = $itm_qty->prasadam_qty;
                    if (array_key_exists($itm_qty->god_id, $aq)) {
                        $god[] = $itm_qty->god_id;
                        $quant = (($aq[$itm_qty->god_id]['qty']) + $itm_qty->prasadam_qty);
                    } else {
                        $god[] = $itm_qty->god_id;
                        $quant = $itm_qty->prasadam_qty;
                    }

                    $qty = $itm_qty->prasadam_qty;

                    $aq[$itm_qty->god_id] = array(
                        'qty' => $quant);
                    $asq[] = $aq;
                    $i++;
                }













                // echo "<pre>"; print_r($a); die();
                $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>';


            // Distribution Prasadam

            $dprasadam = $this->prasadam_report_model->get_dis_prasadam_god_prasadam($start_date, $end_date, $period);

            $d = array();
            $dq = array();
            $names = array();
            $dhead = array();
            $dbody = array();
            $dhead['attr'] = 'style="height: 180px; max-width: 50px; padding: 2px;'; //max-width: 80px;
            $dhead['sno'] = 0;
            $dhead[] = 'Item Name' . '<style type="text/css">td { font-size: 20px !important; } td input{ font-size: 28px !important; letter-spacing: 2px; } </style>';

            foreach ($dprasadam as $key => $value) {
                //Get Prasadam details by Id
                $dprasadam_item = $this->prasadam_report_model->get_dis_prasadam_name($value->prasadam_id, $start_date, $end_date, $period);
                $dprasadamqty = $this->prasadam_report_model->get_group_total_id($value->prasadam_id, $start_date, $end_date, $period);

                $dprasadam_qty = array_unique($dprasadam_item, SORT_REGULAR);
//                print_r($dprasadam_qty);
                foreach ($dprasadam_qty as $k => $itm_qty) {
                    $d[get_var_name($itm_qty->dev_name) . '-' . $itm_qty->id][$value->prasadam_id] = array('prasadam_id' => $itm_qty->prasadam_id, 'name' => $itm_qty->dev_name . ((!empty($itm_qty->dev_tamil_name)) ? ' / ' . tamil($itm_qty->dev_tamil_name) : ''), 'qty' => $itm_qty->prasadam_qty);
                }
                foreach ($dprasadamqty as $k => $itm_qty) {
                    $dq[] = array('prasadam_id' => $itm_qty->prasadam_id, 'name' => $itm_qty->dev_name . ((!empty($itm_qty->dev_tamil_name)) ? ' / ' . tamil($itm_qty->dev_tamil_name) : ''), 'qty' => $itm_qty->prasadam_qty);
                }

                $dhead[] = '<div style="transform: rotate(-90deg); white-space: nowrap; max-width: 37px; max-height: 60px; margin-left: 12px; margin-top: 190px;">' . $value->name . '<div class="lang-tamil">' . $value->tamil_name . '</div></div>';

                $names[$value->prasadam_id] = $value->prasadam_id;
            }
            $dwid = floor(90 / (count($dhead) - 1));
            $dhead['attr'] .= ($dwid > 0 ? 'width : ' . $dwid . '%;' : '') . ' "';

            $dhead[] = '<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>';
            $all_tot = 0;
//            echo '<pre>';
            $god_val = array_values(array_unique($god));
//            print_r($god_val);
//            for ($gg = 0; $gg < count($god_val); $gg++) {
//                $finaltotal[] = $god_val[$gg];
//            }
//            echo '</pre>';
//            die;
            foreach ($a as $index => $items) {
                $row = array();
                $total = 0;
                $gc = count($gods);
                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;
                $all_tot += $total;
                $row[] = $total;
                $body[] = $row;
            }

            $row[0] = "Total";



            for ($cc = 1; $cc <= $gc; $cc++) {
                $godsval = $cc - 1;
                $row[$cc] = $aq[$god_val[$godsval]]['qty'];
            }

            $row[$gc + 1] = $all_tot;
            $body[] = $row;

            #print_r(max($b));
            //echo '<pre>';print_r($body); die;
            $pall_tot = 0;
            foreach ($d as $index => $ditems) {

                $drow = array();
                $dtotal = 0;
                $nc = count($names);
                foreach ($names as $name_id => $name) :

                    $dqty = !empty($ditems[$name_id]) ? $ditems[$name_id]['qty'] : '-';
                    if (empty($drow[0])) :
                        $drow[0] = $ditems[$name_id]['name'];
                    endif;
                    $drow[] = $dqty;
                    $dtotal += is_numeric($dqty) ? $dqty : 0;
                endforeach;
                $pall_tot += $dtotal;
                $drow[] = $dtotal;
                $dbody[] = $drow;
            }
            $allqty = array();
            foreach ($dq as $key => $value):
                $allqty[] = $value['qty'];
            endforeach;
//            print_r($allqty);
//            die;
            $drow[0] = "Total";

            for ($cc = 1; $cc <= $nc; $cc++) {
                $drow[$cc] = $allqty[$cc - 1];
            }
            $drow[$nc + 1] = $pall_tot;
            $dbody[] = $drow;

            //((!empty($this->input->post('period')) && $this->input->post('period') == 1) ? ' (Morning)' : ' (Evening)');

            if ($this->input->post('period') == "1") {
                $p = " ( Morning )";
            } elseif ($this->input->post('period') == "2") {
                $p = " ( Evening)";
            } else if ($this->input->post('period') == "3") {
                $p = " (Afternoon)";
            } else {
                $p = "";
            }

            $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')))) . ' - ' . (date('l', strtotime($this->input->post('start_date')))) . $p,
                'amount' => array(''),
                'copy' => FALSE,
                'isheader' => TRUE,
                'data' => array(
                    // Type = 6
                    'tables' => array(
                        'head' => $head,
                        'body' => $body,
                    ),
                    'tablesub' => array(
                        'head' => $dhead,
                        'body' => $dbody,
                    ),
                ),
            );


            #echo '<pre>';print_r($args);die;

            print_rec($args);
        }
        $data['start_date'] = $start_date;
        $data['end_date'] = $end_date;
        $data['period'] = $period;
        $data['date_type'] = $date_type;
        $actions = $this->actions();
        $data['form_action'] = 'prasadam_report';
        $data['title'] = 'Prasadam Report';

        $filters = array();
        if (!empty($this->input->post('filter_columns'))) {
            foreach ($this->input->post('filte r_column s') as $key => $value) {
                array_push($filters, $this->input->post($value));
            }
        }
        $data['filters'] = array_filter($filters);

        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;
        } elseif ($this->input->post('print') == 'Print') {
            redirect('kiosk/index/bill', 'refresh');
        } else {

            //echo "<pre>";print_r($data); die();

            $this->render($data, 'reports/prasadam/list');
            // $this->render($data, 'reports/prasadam/pdf-list');
        }
    }

}
?>

