<?php

/* 
 * Author       : Jai K
 * Purpose      : Main Controller for Expenses report
 * Created On   : 2017-02-01 15:19
 */

class Expenses_Report extends REF_Controller {
    
    function __construct(){
        parent::__construct();
        
        $this->load->model('reports/expenses_report_model');
        
    }
    
    function index(){
        
        check_permission();
        $actions = $this->actions();
        
        $data = array(
            'title'          => 'Expenses Report',
            'controller'     => $this->controller,
            'actions'        => $actions,
            
            'success'        => $this->session->flashdata('success'),
            'error'          => $this->session->flashdata('error'),
        );
        
        $whr = array();
        $post = $this->input->post();
        //echo '<pre>'; print_r( $post ); 
        if( !empty( $post['submit'] ) ) :
            $f = $post['filter'];
            if( !empty( $f['type'] ) )
                $whr['exp_type_slug'] = $f['type'];
            
            if( !empty( $f['sub'] ) )
                $whr['exp_sub_slug'] = $f['sub'];
        endif;

        /*$start_date = !empty( $f['start_date'] ) ? $f['start_date'] : date("d-m-Y", strtotime("-7 day"));
        $end_date   = !empty( $f['end_date'] ) ? $f['end_date'] : date("d-m-Y");
        $whr['DATE_FORMAT(`exp_date`, "%d-%m-%Y") >= '] = $start_date;
        $whr['DATE_FORMAT(`exp_date`, "%d-%m-%Y") <= '] = $end_date;*/
        if( !empty( $f['start_date'] ) ) :
            $e = explode('-', $f['start_date']);
            $start_date = $e[2] . '-' . $e[1]. '-' . $e[0];
        else :
            $start_date = date("Y-m-d", strtotime("-7 day"));
        endif;
        
        if( !empty( $f['end_date'] ) ) :
            $e = explode('-', $f['end_date']);
            $end_date = $e[2] . '-' . $e[1]. '-' . $e[0];
        else :
            $end_date = date("Y-m-d");
        endif;
        
        $whr['DATE(`exp_date`) >= '] = $start_date;
        $whr['DATE(`exp_date`) <= '] = $end_date;
        
        $filter = array(
            'start_date' => date("d-m-Y", strtotime( $start_date ) ),
            'end_date'   => date("d-m-Y", strtotime( $end_date ) ),
            'type'       => $f['type'],
            'sub'        => $f['sub'],   
        );
        
        $data['filter'] = $filter;
        if(!empty($this->input->post('submit'))) {
            $data['start_date'] = date("d-m-Y", strtotime( $start_date ) );
            $data['end_date'] = date("d-m-Y", strtotime( $end_date ) );
        } else {
            $data['start_date'] = '';
            $data['end_date'] = '';
        }
        $data['types']   = _get(TBL_EXPENSES, array(), array('order_by' => 'exp_type', 'select' => 'DISTINCT(exp_type), exp_type_slug'));
        $data['subjects']   = _get(TBL_EXPENSES, array(), array('order_by' => 'exp_sub', 'select' => 'DISTINCT(exp_sub), exp_sub_slug'));
        $data['results'] = _get(TBL_EXPENSES, $whr, array('order_by' => 'exp_name'));
        
        //SELECT * FROM (`thiru_expenses`) WHERE DATE_FORMAT(`exp_date`, "%d-%m-%Y") >= '25-01-2017' AND DATE_FORMAT(`exp_date`, "%d-%m-%Y") <= '01-02-2017' AND `status` = 1 ORDER BY `exp_name`
        //echo "<pre>Data: "; print_r( $data['types'] ); die;
        
        // Load Views
        //$this->render( $data, 'reports/expenses/list' );
        
        $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'){
            if( IS_KSK ) :
                $html = $this->load->view('reports/expenses/pdf', $data, TRUE);
                print_rec($html);
                redirect('expenses_report','refresh');
            else :
                $this->render($data, 'reports/expenses/pdf');
            endif;
        } else {
            $this->render($data, 'reports/expenses/list');
        }
        
    }
    
}

