<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Daily_summary_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('reports/daily_summary_model', '', TRUE);
        $this->load->model('general_settings_model', '', TRUE);
        $this->uri_key_index = 0;
    }
    
    public function index() { #echo '<pre>';print_r($_POST);die;
        $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') == 'Print Summary Data' || $this->input->post('submit') == 'Get Details Data' || $this->input->post('submit') == 'Print Details Data' || $this->input->post('submit') == 'Submit')) {
            $date = $this->input->post('search_date');
            $data_type = ($this->input->post('submit') != 'Submit') ? $this->input->post('submit') : 'Get Summary Data';
            $abishegam = $this->daily_summary_model->get_abishegam_data($date,$data_type);
            $festival = $this->daily_summary_model->get_festival_data($date,$data_type);
            $ubayam = $this->daily_summary_model->get_ubayam_data($date,$data_type);
            $mega = $this->daily_summary_model->get_mega_data($date,$data_type);
            $other = $this->daily_summary_model->get_other_service_data($date,$data_type);
            $gold_silver = $this->daily_summary_model->get_gold_silver_data($date,$data_type);
            $temple = $this->daily_summary_model->get_temple_activity_data($date,$data_type);
            $member_subscripe = $this->daily_summary_model->get_member_subscribe_data($date,$data_type);
            $building_fund  = $this->daily_summary_model->get_building_fund_data($date,$data_type);
            $outdoor = $this->daily_summary_model->get_outdoor_data($date,$data_type);
            $prasadam = $this->daily_summary_model->get_prasadam_data($date,$data_type);
            $madappalli = $this->daily_summary_model->get_madappalli_data($date,$data_type);
            $donation = $this->daily_summary_model->get_donation_data($date,$data_type);
            $hall_booking = $this->daily_summary_model->get_hall_booking_data($date,$data_type);
            $wedding_hall = $this->daily_summary_model->get_wed_hall_booking_data($date,$data_type);
            $archanai = $this->daily_summary_model->get_archanai_data($date,$data_type);
            $kattalai = $this->daily_summary_model->get_kattalai_data($date,$data_type);
            $membership = $this->daily_summary_model->get_membership_data($date,$data_type);
            $card_topup = $this->daily_summary_model->get_membership_card_topup_data($date,$data_type);
            $temple_shop = $this->daily_summary_model->get_temple_shop_data($date,$data_type);
			$building_fund_daily = $this->daily_summary_model->get_building_fund_daily($date,$data_type);
            
        } elseif($this->input->post('print') == 'Print') {
                $date = $this->input->post('search_date');
                $data_type = 'Get Summary Data';
                $abishegam = $this->daily_summary_model->get_abishegam_data($date,$data_type);
                $festival = $this->daily_summary_model->get_festival_data($date,$data_type);
                $ubayam = $this->daily_summary_model->get_ubayam_data($date,$data_type);
                $mega = $this->daily_summary_model->get_mega_data($date,$data_type);
                $other = $this->daily_summary_model->get_other_service_data($date,$data_type);
                $gold_silver = $this->daily_summary_model->get_gold_silver_data($date,$data_type);
                $temple = $this->daily_summary_model->get_temple_activity_data($date,$data_type);
                $member_subscripe = $this->daily_summary_model->get_member_subscribe_data($date,$data_type);
                $building_fund  = $this->daily_summary_model->get_building_fund_data($date,$data_type);
                $outdoor = $this->daily_summary_model->get_outdoor_data($date,$data_type);
                $prasadam = $this->daily_summary_model->get_prasadam_data($date,$data_type);
                $madappalli = $this->daily_summary_model->get_madappalli_data($date,$data_type);
                $donation = $this->daily_summary_model->get_donation_data($date,$data_type);
                $hall_booking = $this->daily_summary_model->get_hall_booking_data($date,$data_type);
                $wedding_hall = $this->daily_summary_model->get_wed_hall_booking_data($date,$data_type);
                $archanai = $this->daily_summary_model->get_archanai_data($date,$data_type);
                $kattalai = $this->daily_summary_model->get_kattalai_data($date,$data_type);
                $membership = $this->daily_summary_model->get_membership_data($date,$data_type);
                $card_topup = $this->daily_summary_model->get_membership_card_topup_data($date,$data_type);
                $temple_shop = $this->daily_summary_model->get_temple_shop_data($date,$data_type);

                $results = array_merge($abishegam,$archanai,$building_fund,$donation,$festival,$gold_silver,$hall_booking,$kattalai,$madappalli,$mega,$member_subscripe,$membership,$card_topup,$outdoor,$other,$prasadam,$temple,$temple_shop,$ubayam,$wedding_hall);
               
                $general = $this->general_settings_model->get_by_id();
                
                $body = array();
                $total_amt = 0;
                
                foreach($results as $key=>$value) {
                   #$total_amt += $value['amount'];
                   
                   //Refund Amount
                    $Refund = $this->daily_summary_model->get_refund_amt_by_ids($value['type_id'],$value['service_id'],$value['id'],$date);
                  
                    if(!empty($Refund)) {
                        $recentAmt = $value['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $value['amount'];
                    }
                    
                    $total_amt += $recentAmt;
                    if(!empty($value['gst'])) {
                        $AmtwithGst = $recentAmt * ($value['gst'] / 100);
                    } else {
                        $AmtwithGst = 0;
                    }
                                                
                   $gst = (!empty($value['amount'])) ? number_format($recentAmt - $AmtwithGst,2,'.','') : '0.00';
                   $gst_amt = (!empty($value['gst'])) ? number_format($AmtwithGst,2,'.','') : '0.00';
                   //$body[] = array($value['rec_no'], $value['name'], $value['qty'], $value['price'], (($general->gst > 0) ? $gst,$gst_amt,number_format($recentAmt,2,'.','') : number_format($recentAmt,2,'.','')) );
                    if($general->gst > 0) {
                        $body[] = array($value['rec_no'], $value['name'], $value['qty'], $value['price'], '<span style="float: right;">' . $gst . '</span>', '<span style="float: right;">' . $gst_amt . '</span>','<span style="float: right;">' . number_format($recentAmt,2,'.','') . '</span>');
                    } else {
                        $body[] = array($value['rec_no'], $value['name'], $value['qty'], $value['price'], '<span style="float: right;">' . number_format($recentAmt,2,'.','') . '</span>');  
                    }
                   
                }
                
                
                $sub_body = $this->get_sub_body( $date );
                $sub_body[] = array('Total ' .(($general->gst > 0) ? '(with GST)' : '' ),  number_format(($total_amt),2,'.','')); 
				
				/*$sub_body = array();
				$sub_body[] = array('Total ' .(($general->gst > 0) ? '(with GST)' : '' ),  number_format(($total_amt),2,'.',''));
				$pay_detls = $this->get_sub_body( $date );
				$sub_body = array_merge($sub_body,$pay_detls);*/
                
                $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'     => 'Daily Summary Report - ' . (date('d/m/Y',strtotime($date))) ,//$main_service['service_name']. ' - '.$service['name'],
                    'amount'    => array(''),
                    'copy'      => FALSE,
                    
                    'data'      => array(
                        // Type = 6
                        'tables' => array(
                            #'head'  => array('Receipt No', 'Name', 'Quantity', 'Unit Price',(($general->gst > 0) ? ('Before GST', 'GST Amount', 'Total') : 'Total')),
                            'head'  => array('Receipt No', 'Name', 'Quantity', 'Unit Price',(($general->gst > 0) ? 'Before GST' : ''), (($general->gst > 0) ? 'GST Amount' : ''), 'Total'),
                            'body'  => $body,
                        ),
                   
                        'sub_tables' => array(
                            'head'  => array(''),
                            'body'  => $sub_body,
                        ),
                    ),
                );
                
                #echo '<pre>';print_r($args);die;
                print_rec($args); 
        } else  {
            $date = '';
            $data_type = 'Get Summary Data';
            $abishegam = $this->daily_summary_model->get_abishegam_data($date = '',$data_type);
            $festival = $this->daily_summary_model->get_festival_data($date = '',$data_type);
            //error_log($this->db->last_query());
            $ubayam = $this->daily_summary_model->get_ubayam_data($date = '',$data_type);
            $mega = $this->daily_summary_model->get_mega_data($date = '',$data_type);
            $other = $this->daily_summary_model->get_other_service_data($date = '',$data_type);
            $gold_silver = $this->daily_summary_model->get_gold_silver_data($date = '',$data_type);
            $temple = $this->daily_summary_model->get_temple_activity_data($date = '',$data_type);
            $member_subscripe = $this->daily_summary_model->get_member_subscribe_data($date = '',$data_type);
            $building_fund  = $this->daily_summary_model->get_building_fund_data($date = '',$data_type);
            $outdoor = $this->daily_summary_model->get_outdoor_data($date = '',$data_type);
            $prasadam = $this->daily_summary_model->get_prasadam_data($date = '',$data_type);
            $madappalli = $this->daily_summary_model->get_madappalli_data($date = '',$data_type);
            $donation = $this->daily_summary_model->get_donation_data($date = '',$data_type);
            $hall_booking = $this->daily_summary_model->get_hall_booking_data($date = '',$data_type);
            $wedding_hall = $this->daily_summary_model->get_wed_hall_booking_data($date= '',$data_type);
            $archanai = $this->daily_summary_model->get_archanai_data($date= '',$data_type);
            $kattalai = $this->daily_summary_model->get_kattalai_data($date= '',$data_type);
            $membership = $this->daily_summary_model->get_membership_data($date= '',$data_type);
            $card_topup = $this->daily_summary_model->get_membership_card_topup_data($date= '',$data_type);
            $temple_shop = $this->daily_summary_model->get_temple_shop_data($date= '',$data_type);
        }
        
        $data['results'] 		= array_merge($abishegam,$archanai,$building_fund,$donation,$festival,$gold_silver,$hall_booking,$kattalai,$madappalli,$mega,$member_subscripe,$membership,$card_topup,$outdoor,$other,$prasadam,$temple,$temple_shop,$ubayam,$wedding_hall);
		
		$data['building_fund'] 	= $building_fund_daily;
        #echo '<pre>';print_r($data['results']);die;
        /*
         * Tally Operation
         */
        if( ($this->input->post('submit') == 'Print Summary Data' || $this->input->post('print') == 'Print') && !empty( $data['results'] ) ) :
            if( $this->input->post('submit') == 'Print Summary Data' ) :
                 $sub_body = $this->get_sub_body( $date );
            else :
                // Remove Total
                unset($sub_body[count($sub_body)-1]);
            endif;
            
            $tally_results = array_merge( $data['results'], $sub_body );
            //$this->daily_summary_model->tally( $tally_results, $date );
        endif;
        
        //error_log(print_r($data['results'],1));
        $data['search_date'] = $date;
        $data['type'] = $data_type;
        $data['mode'] = $this->daily_summary_model->payment_type_list();
        $actions = $this->actions();
        $data['form_action'] = 'daily_summary_report';
        $data['title'] = 'Daily Summary 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 Summary Data' || $this->input->post('submit') == 'Print Details Data' ){
            $data['dataType'] = $this->input->post('submit');
            if( IS_KSK ) :
                $html = $this->load->view('reports/daily_summary/pdf', $data, TRUE);
                print_rec($html);
                redirect('daily_summary_report','refresh');
            else :
                $this->render($data, 'reports/daily_summary/pdf');
            endif;
        } elseif($this->input->post('print') == 'Print') {
           redirect('kiosk/index/bill','refresh');
        }else {
            $this->render($data, 'reports/daily_summary/list');
        }
    }
    
    function get_sub_body( $date ) {
        
        $sub_body = array();
        $mode = $this->daily_summary_model->payment_type_list();
        
        $refundAmount = $this->daily_summary_model->get_refund_amount($date);
        //$sub_body[] = array('Refund Amount',  number_format(($refundAmount),2,'.',''));

        foreach($mode as $key=>$value) {
            $paidAmount = $this->daily_summary_model->get_total_paid_amt_by_type($value->id,$date);
            if($value->payment_mode == 'cash') { 
                $refundAmt = $this->daily_summary_model->get_refund_amount($date);
            } else { 
                $refundAmt = '0.00';
            }

            $Amount = (!empty($paidAmount->amount)) ? (($value->payment_mode == 'cash') ? number_format($paidAmount->amount - $refundAmt,2,'.','') :  $paidAmount->amount ) : '0.00';

            $sub_body[] = array(ucfirst($value->name), $Amount);
        }
        
        return $sub_body;
    }
   
} ?>

