<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Madappalli_service_report extends REF_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->model('reports/madappalli_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') == 'Submit' || $this->input->post('print') == 'Print')) {
            $start_date = $this->input->post('start_date');
            $end_date = $this->input->post('end_date');
            $period = (!empty($this->input->post('period'))) ? $this->input->post('period') : '';
            $date_type = $this->input->post('date_type');
        } else {
            $start_date = '';
            $end_date = '';
            $period = '';
            $date_type = 1;
        }
        $data['results'] = $this->madappalli_report_model->get_data($start_date,$end_date,$period,$date_type);
        if($this->input->post('print') == 'Print') {
                $date_type = 1;
                $results = $this->madappalli_report_model->get_data($this->input->post('start_date'),$this->input->post('end_date'),$this->input->post('period'),$date_type);
                $body = array();
                $total_amt = 0;
                foreach($results as $key=>$value) {
                   $total_amt += $$value->bmi_sub_total;
                   $body[] = array($value->bm_rec_no, $value->service_name, date('d-m-Y',strtotime($value->bm_fn_date)), $value->bm_dev_name, $value->bm_contact_no, $value->deity, $value->prasadam, $value->bmi_qty, number_format($value->bmi_price * $value->bmi_qty ,2,'.',''));
                }
                $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'     => 'Madappalli Services Report',//$main_service['service_name']. ' - '.$service['name'],
                    'amount'    => array(''),
                    'copy'      => FALSE,
                    
                    'data'      => array(
                        // Type = 6
                        'tables' => array(
                            'head'  => array('Receipt No', 'Type', 'Date', 'Name', 'Contact No', 'Deity', 'Naivedyam', 'Qty', 'Price'),
                            'body'  => $body,
                        ),
                    ),
                );
                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'] = 'madappalli_service_report';
        $data['title'] = 'Prasadam 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'){
            if( IS_KSK ) :
                $html = $this->load->view('reports/madappalli/pdf', $data, TRUE);
                print_rec($html);
                redirect('madappalli_service_report','refresh');
            else :
                $this->render($data, 'reports/madappalli/pdf');
            endif;
        } else {
            $this->render($data, 'reports/madappalli/list');
        }
    }
   
} ?>

