<?php

/* 
 * Author       : Jai K
 * Purpose      : Main file for Reports module related functions
 * Created On   : 2016-12-12 11:37
 */


class Reports extends DB{
    
    function __construct(){
        parent::__construct();
        
    }
    
    
    /*
     * Services Module
     */
    function summary() {
        
        switch( CMD ) :
            
            case 'DAILY' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date = (isset($psdata['date']) && $psdata['date']!='') ? $psdata['date'] : '';
                if(isset($psdata['datatype']) && $psdata['datatype']!='') {
                    if($psdata['datatype']=='details') {
                        $data_type = 'Get Details Data';
                    } else if($psdata['datatype']=='summary') {
                        $data_type = 'Get Summary Data';
                    } else {
                        throw new Exception('Invalid Data Type!');
                    }
                } else {
                    $data_type = 'Get Summary Data';
                }
                
                $abishegam = $this->get_abishegam_data($date,$data_type);
                $festival = $this->get_festival_data($date,$data_type);
                $ubayam = $this->get_ubayam_data($date,$data_type);
                $mega = $this->get_mega_data($date,$data_type);
                $other = $this->get_other_service_data($date,$data_type);
                $gold_silver = $this->get_gold_silver_data($date,$data_type);
                $temple = $this->get_temple_activity_data($date,$data_type);
                $member_subscripe = $this->get_member_subscribe_data($date,$data_type);
                $building_fund = $this->get_building_fund_data($date,$data_type);
                $outdoor = $this->get_outdoor_data($date,$data_type);
                $prasadam = $this->get_prasadam_data($date,$data_type);
                $madappalli = $this->get_madappalli_data($date,$data_type);
                $donation = $this->get_donation_data($date,$data_type);
                $hall_booking = $this->get_hall_booking_data($date,$data_type);
                $wedding_hall = $this->get_wed_hall_booking_data($date,$data_type);
                $archanai = $this->get_archanai_data($date,$data_type);
                $kattalai = $this->get_kattalai_data($date,$data_type);
                $membership = $this->get_membership_data($date,$data_type);
                $card_topup = $this->get_membership_card_topup_data($date,$data_type);
                $temple_shop = $this->get_temple_shop_data($date,$data_type);
                
                $result_arr = 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);
                
                $mode = $this->payment_type_list($date,$data_type);
                
                $result_data = array();
                $total_amt = 0;  
                if (count($result_arr)>0) {
                    $i=0; $search_date = (!empty($date)) ? $date : date('d-m-Y');
                    if($data_type == 'Get Summary Data') {
                    foreach( $result_arr as $key => $row) :
                        
                    $Refund = $this->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$search_date); 
                    
                    if(!empty($Refund)) {
                        $recentAmt = $row['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $row['amount'];
                    }

                    $total_amt += $recentAmt;
                    if(!empty($row['gst'])) {
                        $AmtwithGst = $recentAmt * ($row['gst'] / 100);
                    } else {
                        $AmtwithGst = 0;
                    }
                    
                   $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                   $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                   $paymentType = $this->get_payment_details($row['type_id'],$auto_id,$search_date,$mem_card_id);
                   
                   if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                      $payType = '-';
                   } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                      $payType = $paymentType[0]['payment'];
                   } else { $payType = $paymentType[0]['payment']; }
                    
                    $result_data[$i]['name'] = $row['name'];    
                    $result_data[$i]['receipt_no'] = $row['rec_no'];
                    $result_data[$i]['payment_type'] = $payType; 
                    $result_data[$i]['quantity'] = (string)$row['qty'];    
                    $result_data[$i]['unit_price'] = $row['price'];    
                    $result_data[$i]['before_gst'] = (!empty($recentAmt)) ? number_format($recentAmt - $AmtwithGst,2,'.','') : '0.00';    
                    $result_data[$i]['gst_amount'] = (!empty($row['gst'])) ? number_format($AmtwithGst,2,'.','') : '0.00';   
                    $result_data[$i]['total'] = number_format($recentAmt,2,'.','');    
                    $result_data[$i]['grant_total'] = '';    
                    $i++;
                    endforeach;
                    
                    $result_data[$i]['name'] = 'Total';    
                    $result_data[$i]['receipt_no'] = '';    
                    $result_data[$i]['payment_type'] = '';    
                    $result_data[$i]['quantity'] = '';    
                    $result_data[$i]['unit_price'] = '';    
                    $result_data[$i]['before_gst'] = '';    
                    $result_data[$i]['gst_amount'] = '';   
                    $result_data[$i]['total'] = '';    
                    $result_data[$i]['grant_total'] = number_format($total_amt,2,'.',''); 
                    
//                    $refundAmount = $this->get_refund_amount($search_date);
                    
//                    $result_data[$i+1]['name'] = 'Refund Amount';    
//                    $result_data[$i+1]['receipt_no'] = '';  
//                    $result_data[$i+1]['payment_type'] = ''; 
//                    $result_data[$i+1]['quantity'] = '';    
//                    $result_data[$i+1]['unit_price'] = '';    
//                    $result_data[$i+1]['before_gst'] = '';    
//                    $result_data[$i+1]['gst_amount'] = '';   
//                    $result_data[$i+1]['total'] = '';    
//                    $result_data[$i+1]['grant_total'] = $refundAmount; 
                    
                    $k = $i+1;
                    foreach($mode as $key=>$value) {
                    $paidAmount = $this->get_total_paid_amt_by_type($value['id'],$search_date);  
                    if($value['payment_mode'] == 'cash') { 
                        $refundAmt = $this->get_refund_amount($search_date);
                    } else { 
                        $refundAmt = '0.00';
                    }
                    $result_data[$k]['name'] = ucfirst($value['name']);    
                    $result_data[$k]['receipt_no'] = '';    
                    $result_data[$k]['payment_type'] = ''; 
                    $result_data[$k]['quantity'] = '';    
                    $result_data[$k]['unit_price'] = '';    
                    $result_data[$k]['before_gst'] = '';    
                    $result_data[$k]['gst_amount'] = '';   
                    $result_data[$k]['total'] = '';    
                    $result_data[$k]['grant_total'] = (!empty($paidAmount[0]['amount'])) ? (($value['payment_mode'] == 'cash') ? number_format($paidAmount[0]['amount'] - $refundAmt,2,'.','') :  $paidAmount[0]['amount'] ) : '0.00'; 
                    
                    $k++;    
                    }
                  } else if($data_type == 'Get Details Data') {
                    foreach( $result_arr as $key => $row) :
                        
                    $Refund = $this->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$search_date); 
                    
                    if(!empty($Refund)) {
                        $recentAmt = $row['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $row['amount'];
                    }

                    $total_amt += $recentAmt;
                    
                    $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                    $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                    
                    $paymentType = $this->get_payment_details($row['type_id'],$auto_id,$search_date,$mem_card_id);
                    
                    if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                        $payType = '-';
                    } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                        $payType = $paymentType[0]['payment'];
                    } else { $payType = $paymentType[0]['payment']; }
                    
                    $result_data[$i]['category_name'] = $row['cat_name'];    
                    $result_data[$i]['receipt_no'] = $row['rec_no'];    
                    $result_data[$i]['sub_category_name'] = $row['name'];    
                    $result_data[$i]['payment_type'] = $payType;    
                    $result_data[$i]['total'] = number_format($recentAmt,2,'.','');    
                    $result_data[$i]['grant_total'] = '';    
                    $i++;
                    endforeach;
                    
                    $result_data[$i]['category_name'] = 'Total';    
                    $result_data[$i]['receipt_no'] = '';    
                    $result_data[$i]['sub_category_name'] = '';    
                    $result_data[$i]['payment_type'] = '';      
                    $result_data[$i]['total'] = '';    
                    $result_data[$i]['grant_total'] = number_format($total_amt,2,'.',''); 
                    
//                    $refundAmount = $this->get_refund_amount($search_date);
//                    
//                    $result_data[$i+1]['category_name'] = 'Refund Amount';    
//                    $result_data[$i+1]['receipt_no'] = '';    
//                    $result_data[$i+1]['sub_category_name'] = '';    
//                    $result_data[$i+1]['payment_type'] = '';    
//                    $result_data[$i+1]['total'] = '';    
//                    $result_data[$i+1]['grant_total'] = $refundAmount; 
                    
                    $k = $i+1;
                    foreach($mode as $key=>$value) {
                    $paidAmount = $this->get_total_paid_amt_by_type($value['id'],$search_date);  
                    if($value['payment_mode'] == 'cash') { 
                        $refundAmt = $this->get_refund_amount($search_date);
                    } else { 
                        $refundAmt = '0.00';
                    }
                    $result_data[$k]['category_name'] = ucfirst($value['name']);    
                    $result_data[$k]['receipt_no'] = '';    
                    $result_data[$k]['sub_category_name'] = '';    
                    $result_data[$k]['payment_type'] = '';     
                    $result_data[$k]['total'] = '';    
                    $result_data[$k]['grant_total'] = (!empty($paidAmount[0]['amount'])) ? (($value['payment_mode'] == 'cash') ? number_format($paidAmount[0]['amount'] - $refundAmt,2,'.','') :  $paidAmount[0]['amount'] ) : '0.00'; 
                    
                    $k++;    
                    }
                    
                  }
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
                
                    $results['data'] = '';
                endif;
                
                return $results;

            break;

            case 'WEEKLY' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $start_date = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $end_date = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $cmd = CMD;
                if(isset($psdata['datatype']) && $psdata['datatype']!='') {
                    if($psdata['datatype']=='details') {
                        $data_type = 'Get Details Data';
                    } else if($psdata['datatype']=='summary') {
                        $data_type = 'Get Summary Data';
                    } else {
                        throw new Exception('Invalid Data Type!');
                    }
                } else {
                    $data_type = 'Get Summary Data';
                }
                
                $abishegam = $this->get_abishegam_datewise_data($start_date,$end_date,$data_type,$cmd);
                $festival = $this->get_festival_datewise_data($start_date,$end_date,$data_type,$cmd);
                $ubayam = $this->get_ubayam_datewise_data($start_date,$end_date,$data_type,$cmd);
                $mega = $this->get_mega_datewise_data($start_date,$end_date,$data_type,$cmd);
                $other = $this->get_other_service_datewise_data($start_date,$end_date,$data_type,$cmd);
                $gold_silver = $this->get_gold_silver_datewise_data($start_date,$end_date,$data_type,$cmd);
                $temple = $this->get_temple_activity_datewise_data($start_date,$end_date,$data_type,$cmd);
                $member_subscripe = $this->get_member_subscribe_datewise_data($start_date,$end_date,$data_type,$cmd);
                $building_fund = $this->get_building_fund_datewise_data($start_date,$end_date,$data_type,$cmd);
                $outdoor = $this->get_outdoor_datewise_data($start_date,$end_date,$data_type,$cmd);
                $prasadam = $this->get_prasadam_datewise_data($start_date,$end_date,$data_type,$cmd);
                $madappalli = $this->get_madappalli_datewise_data($start_date,$end_date,$data_type,$cmd);
                $donation = $this->get_donation_datewise_data($start_date,$end_date,$data_type,$cmd);
                $hall_booking = $this->get_hall_booking_datewise_data($start_date,$end_date,$data_type,$cmd);
                $wedding_hall = $this->get_wed_hall_booking_datewise_data($start_date,$end_date,$data_type,$cmd);
                $archanai = $this->get_archanai_datewise_data($start_date,$end_date,$data_type,$cmd);
                $kattalai = $this->get_kattalai_datewise_data($start_date,$end_date,$data_type,$cmd);
                $membership = $this->get_membership_datewise_data($start_date,$end_date,$data_type,$cmd);
                $card_topup = $this->get_membership_card_topup_datewise_data($start_date,$end_date,$data_type,$cmd);
                $temple_shop = $this->get_temple_shop_datewise_data($start_date,$end_date,$data_type,$cmd);
                
                $result_arr = 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);
                
                $mode = $this->payment_type_list();
                
                $result_data = array();
                $total_amt = 0;  
                if (count($result_arr)>0) {
                    $i=0;
                    if($data_type == 'Get Summary Data') {
                    foreach( $result_arr as $key => $row) :
                        
                    $Refund = $this->get_datewise_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_date); 
                    
                    if(!empty($Refund)) {
                        $recentAmt = $row['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $row['amount'];
                    }

                    $total_amt += $recentAmt;
                    if(!empty($row['gst'])) {
                        $AmtwithGst = $recentAmt * ($row['gst'] / 100);
                    } else {
                        $AmtwithGst = 0;
                    }
                    
                   $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                   $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                   $paymentType = $this->get_datewise_payment_details($row['type_id'],$auto_id,$start_date,$end_date,$mem_card_id,$cmd);
                   
                   if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                      $payType = '-';
                   } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                      $payType = $paymentType[0]['payment'];
                   } else { $payType = $paymentType[0]['payment']; }
                    
                    $result_data[$i]['name'] = $row['name'];    
                    $result_data[$i]['receipt_no'] = $row['rec_no'];
                    $result_data[$i]['payment_type'] = $payType; 
                    $result_data[$i]['quantity'] = (string)$row['qty'];    
                    $result_data[$i]['unit_price'] = $row['price'];    
                    $result_data[$i]['before_gst'] = (!empty($recentAmt)) ? number_format($recentAmt - $AmtwithGst,2,'.','') : '0.00';    
                    $result_data[$i]['gst_amount'] = (!empty($row['gst'])) ? number_format($AmtwithGst,2,'.','') : '0.00';   
                    $result_data[$i]['total'] = number_format($recentAmt,2,'.','');    
                    $result_data[$i]['grant_total'] = '';    
                    $i++;
                    endforeach;
                    
                    $result_data[$i]['name'] = 'Total';    
                    $result_data[$i]['receipt_no'] = '';    
                    $result_data[$i]['payment_type'] = '';    
                    $result_data[$i]['quantity'] = '';    
                    $result_data[$i]['unit_price'] = '';    
                    $result_data[$i]['before_gst'] = '';    
                    $result_data[$i]['gst_amount'] = '';   
                    $result_data[$i]['total'] = '';    
                    $result_data[$i]['grant_total'] = number_format($total_amt,2,'.',''); 
                    
                    $k = $i+1;
                    foreach($mode as $key=>$value) {
                    $paidAmount = $this->get_datewise_total_paid_amt_by_type($value['id'],$start_date,$end_date,$cmd);  
                    if($value['payment_mode'] == 'cash') { 
                        $refundAmt = $this->get_datewise_refund_amount($start_date,$end_date,$cmd);
                    } else { 
                        $refundAmt = '0.00';
                    }
                    $result_data[$k]['name'] = ucfirst($value['name']);    
                    $result_data[$k]['receipt_no'] = '';    
                    $result_data[$k]['payment_type'] = ''; 
                    $result_data[$k]['quantity'] = '';    
                    $result_data[$k]['unit_price'] = '';    
                    $result_data[$k]['before_gst'] = '';    
                    $result_data[$k]['gst_amount'] = '';   
                    $result_data[$k]['total'] = '';    
                    $result_data[$k]['grant_total'] = (!empty($paidAmount[0]['amount'])) ? (($value['payment_mode'] == 'cash') ? number_format($paidAmount[0]['amount'] - $refundAmt,2,'.','') :  $paidAmount[0]['amount'] ) : '0.00'; 
                    
                    $k++;    
                    }
                  } else if($data_type == 'Get Details Data') {
                    foreach( $result_arr as $key => $row) :
                    $service_id = (!empty($row['service_id'])) ? $row['service_id'] : '';    
                    $Refund = $this->get_datewise_refund_amt_by_ids($row['type_id'],$service_id,$row['id'],$start_date,$end_date);
                    
                    if(!empty($Refund)) {
                        $recentAmt = $row['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $row['amount'];
                    }

                    $total_amt += $recentAmt;
                    
                    $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                    $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                    
                    $paymentType = $this->get_datewise_payment_details($row['type_id'],$auto_id,$start_date,$end_date,$mem_card_id,$cmd);
                    
                    if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                        $payType = '-';
                    } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                        $payType = $paymentType[0]['payment'];
                    } else { $payType = $paymentType[0]['payment']; }
                    
                    $result_data[$i]['category_name'] = $row['cat_name'];    
                    $result_data[$i]['receipt_no'] = $row['rec_no'];    
                    $result_data[$i]['sub_category_name'] = $row['name'];    
                    $result_data[$i]['payment_type'] = $payType;    
                    $result_data[$i]['total'] = number_format($recentAmt,2,'.','');    
                    $result_data[$i]['grant_total'] = '';    
                    $i++;
                    endforeach;
                    
                    $result_data[$i]['category_name'] = 'Total';    
                    $result_data[$i]['receipt_no'] = '';    
                    $result_data[$i]['sub_category_name'] = '';    
                    $result_data[$i]['payment_type'] = '';      
                    $result_data[$i]['total'] = '';    
                    $result_data[$i]['grant_total'] = number_format($total_amt,2,'.',''); 
                    
                    $k = $i+1;
                    foreach($mode as $key=>$value) {
                    $paidAmount = $this->get_datewise_total_paid_amt_by_type($value['id'],$start_date,$end_date,$cmd);  
                    if($value['payment_mode'] == 'cash') { 
                        $refundAmt = $this->get_datewise_refund_amount($start_date,$end_date,$cmd);
                    } else { 
                        $refundAmt = '0.00';
                    }
                    $result_data[$k]['category_name'] = ucfirst($value['name']);    
                    $result_data[$k]['receipt_no'] = '';    
                    $result_data[$k]['sub_category_name'] = '';    
                    $result_data[$k]['payment_type'] = '';     
                    $result_data[$k]['total'] = '';    
                    $result_data[$k]['grant_total'] = (!empty($paidAmount[0]['amount'])) ? (($value['payment_mode'] == 'cash') ? number_format($paidAmount[0]['amount'] - $refundAmt,2,'.','') :  $paidAmount[0]['amount'] ) : '0.00'; 
                    
                    $k++;    
                    }
                    
                  }
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
                
                    $results['data'] = '';
                endif;
                
                return $results;

            break;  
            
            
            case 'MONTHLY' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $start_date = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $end_date = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $cmd = CMD;
                if(isset($psdata['datatype']) && $psdata['datatype']!='') {
                    if($psdata['datatype']=='details') {
                        $data_type = 'Get Details Data';
                    } else if($psdata['datatype']=='summary') {
                        $data_type = 'Get Summary Data';
                    } else {
                        throw new Exception('Invalid Data Type!');
                    }
                } else {
                    $data_type = 'Get Summary Data';
                }
                
                $abishegam = $this->get_abishegam_datewise_data($start_date,$end_date,$data_type,$cmd);
                $festival = $this->get_festival_datewise_data($start_date,$end_date,$data_type,$cmd);
                $ubayam = $this->get_ubayam_datewise_data($start_date,$end_date,$data_type,$cmd);
                $mega = $this->get_mega_datewise_data($start_date,$end_date,$data_type,$cmd);
                $other = $this->get_other_service_datewise_data($start_date,$end_date,$data_type,$cmd);
                $gold_silver = $this->get_gold_silver_datewise_data($start_date,$end_date,$data_type,$cmd);
                $temple = $this->get_temple_activity_datewise_data($start_date,$end_date,$data_type,$cmd);
                $member_subscripe = $this->get_member_subscribe_datewise_data($start_date,$end_date,$data_type,$cmd);
                $building_fund = $this->get_building_fund_datewise_data($start_date,$end_date,$data_type,$cmd);
                $outdoor = $this->get_outdoor_datewise_data($start_date,$end_date,$data_type,$cmd);
                $prasadam = $this->get_prasadam_datewise_data($start_date,$end_date,$data_type,$cmd);
                $madappalli = $this->get_madappalli_datewise_data($start_date,$end_date,$data_type,$cmd);
                $donation = $this->get_donation_datewise_data($start_date,$end_date,$data_type,$cmd);
                $hall_booking = $this->get_hall_booking_datewise_data($start_date,$end_date,$data_type,$cmd);
                $wedding_hall = $this->get_wed_hall_booking_datewise_data($start_date,$end_date,$data_type,$cmd);
                $archanai = $this->get_archanai_datewise_data($start_date,$end_date,$data_type,$cmd);
                $kattalai = $this->get_kattalai_datewise_data($start_date,$end_date,$data_type,$cmd);
                $membership = $this->get_membership_datewise_data($start_date,$end_date,$data_type,$cmd);
                $card_topup = $this->get_membership_card_topup_datewise_data($start_date,$end_date,$data_type,$cmd);
                $temple_shop = $this->get_temple_shop_datewise_data($start_date,$end_date,$data_type,$cmd);
                
                $result_arr = 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);
                
                $mode = $this->payment_type_list();
                
                $result_data = array();
                $total_amt = 0;  
                if (count($result_arr)>0) {
                    $i=0;
                    if($data_type == 'Get Summary Data') {
                    foreach( $result_arr as $key => $row) :
                        $service_id = (!empty($row['service_id'])) ? $row['service_id'] : '';
                    $Refund = $this->get_datewise_refund_amt_by_ids($row['type_id'],$service_id,$row['id'],$start_date,$end_date); 
                    
                    if(!empty($Refund)) {
                        $recentAmt = $row['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $row['amount'];
                    }

                    $total_amt += $recentAmt;
                    if(!empty($row['gst'])) {
                        $AmtwithGst = $recentAmt * ($row['gst'] / 100);
                    } else {
                        $AmtwithGst = 0;
                    }
                    
                   $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                   $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                   $paymentType = $this->get_datewise_payment_details($row['type_id'],$auto_id,$start_date,$end_date,$mem_card_id,$cmd);
                   
                   if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                      $payType = '-';
                   } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                      $payType = $paymentType[0]['payment'];
                   } else { $payType = $paymentType[0]['payment']; }
                    
                    $result_data[$i]['name'] = $row['name'];    
                    $result_data[$i]['receipt_no'] = $row['rec_no'];
                    $result_data[$i]['payment_type'] = $payType; 
                    $result_data[$i]['quantity'] = (string)$row['qty'];    
                    $result_data[$i]['unit_price'] = $row['price'];    
                    $result_data[$i]['before_gst'] = (!empty($recentAmt)) ? number_format($recentAmt - $AmtwithGst,2,'.','') : '0.00';    
                    $result_data[$i]['gst_amount'] = (!empty($row['gst'])) ? number_format($AmtwithGst,2,'.','') : '0.00';   
                    $result_data[$i]['total'] = number_format($recentAmt,2,'.','');    
                    $result_data[$i]['grant_total'] = '';    
                    $i++;
                    endforeach;
                    
                    $result_data[$i]['name'] = 'Total';    
                    $result_data[$i]['receipt_no'] = '';    
                    $result_data[$i]['payment_type'] = '';    
                    $result_data[$i]['quantity'] = '';    
                    $result_data[$i]['unit_price'] = '';    
                    $result_data[$i]['before_gst'] = '';    
                    $result_data[$i]['gst_amount'] = '';   
                    $result_data[$i]['total'] = '';    
                    $result_data[$i]['grant_total'] = number_format($total_amt,2,'.',''); 
                    
                    $k = $i+1;
                    foreach($mode as $key=>$value) {
                    $paidAmount = $this->get_datewise_total_paid_amt_by_type($value['id'],$start_date,$end_date,$cmd);  
                    if($value['payment_mode'] == 'cash') { 
                        $refundAmt = $this->get_datewise_refund_amount($start_date,$end_date,$cmd);
                    } else { 
                        $refundAmt = '0.00';
                    }
                    $result_data[$k]['name'] = ucfirst($value['name']);    
                    $result_data[$k]['receipt_no'] = '';    
                    $result_data[$k]['payment_type'] = ''; 
                    $result_data[$k]['quantity'] = '';    
                    $result_data[$k]['unit_price'] = '';    
                    $result_data[$k]['before_gst'] = '';    
                    $result_data[$k]['gst_amount'] = '';   
                    $result_data[$k]['total'] = '';    
                    $result_data[$k]['grant_total'] = (!empty($paidAmount[0]['amount'])) ? (($value['payment_mode'] == 'cash') ? number_format($paidAmount[0]['amount'] - $refundAmt,2,'.','') :  $paidAmount[0]['amount'] ) : '0.00'; 
                    
                    $k++;    
                    }
                  } else if($data_type == 'Get Details Data') {
                    foreach( $result_arr as $key => $row) :
                    $service_id = (!empty($row['service_id'])) ? $row['service_id'] : '';
                    $Refund = $this->get_datewise_refund_amt_by_ids($row['type_id'],$service_id,$row['id'],$start_date,$end_date);
                    
                    if(!empty($Refund)) {
                        $recentAmt = $row['amount'] - $Refund;
                    }
                    else {
                       $recentAmt = $row['amount'];
                    }

                    $total_amt += $recentAmt;
                    
                    $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                    $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                    
                    $paymentType = $this->get_datewise_payment_details($row['type_id'],$auto_id,$start_date,$end_date,$mem_card_id,$cmd);
                    
                    if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
                        $payType = '-';
                    } elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
                        $payType = $paymentType[0]['payment'];
                    } else { $payType = $paymentType[0]['payment']; }
                    
                    $result_data[$i]['category_name'] = $row['cat_name'];    
                    $result_data[$i]['receipt_no'] = $row['rec_no'];    
                    $result_data[$i]['sub_category_name'] = $row['name'];    
                    $result_data[$i]['payment_type'] = $payType;    
                    $result_data[$i]['total'] = number_format($recentAmt,2,'.','');    
                    $result_data[$i]['grant_total'] = '';    
                    $i++;
                    endforeach;
                    
                    $result_data[$i]['category_name'] = 'Total';    
                    $result_data[$i]['receipt_no'] = '';    
                    $result_data[$i]['sub_category_name'] = '';    
                    $result_data[$i]['payment_type'] = '';      
                    $result_data[$i]['total'] = '';    
                    $result_data[$i]['grant_total'] = number_format($total_amt,2,'.',''); 
                    
                    $k = $i+1;
                    foreach($mode as $key=>$value) {
                    $paidAmount = $this->get_datewise_total_paid_amt_by_type($value['id'],$start_date,$end_date,$cmd);  
                    if($value['payment_mode'] == 'cash') { 
                        $refundAmt = $this->get_datewise_refund_amount($start_date,$end_date,$cmd);
                    } else { 
                        $refundAmt = '0.00';
                    }
                    $result_data[$k]['category_name'] = ucfirst($value['name']);    
                    $result_data[$k]['receipt_no'] = '';    
                    $result_data[$k]['sub_category_name'] = '';    
                    $result_data[$k]['payment_type'] = '';     
                    $result_data[$k]['total'] = '';    
                    $result_data[$k]['grant_total'] = (!empty($paidAmount[0]['amount'])) ? (($value['payment_mode'] == 'cash') ? number_format($paidAmount[0]['amount'] - $refundAmt,2,'.','') :  $paidAmount[0]['amount'] ) : '0.00'; 
                    
                    $k++;    
                    }
                    
                  }
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
                
                    $results['data'] = '';
                endif;
                
                return $results;

            break;
            
        
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
                
    }
    
    
    function logdetails() {
        
        switch( CMD ) :
            
            case 'LOG' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $userid = (isset($psdata['userid']) && $psdata['userid']!='') ? $psdata['userid'] : '';
                
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
                
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
                
                $log_details = $this->get_data($startdate,$enddate,$userid);
                
                $result_data = array();
                $total_amt = 0;  
                if (count($log_details)>0) {
                    $i=0;
                    foreach( $log_details as $key => $value) :
                    
                        if($value['menu_type'] != 8) {
                    $service_data = $this->get_service_name($value['menu_type'],$value['service_menu_id']); 
                    $service = isset($service_data[0]['service_name']) ? $service_data[0]['service_name'] : ''; 
                    } else {
                    $service = 'Sync Process';    
                    }
                    
                    if($value['menu_type'] == 1) {
                        $menu = 'Master';
                    } elseif($value['menu_type'] == 3) {
                        $menu = 'Inventory';
                    } elseif($value['menu_type'] == 4){
                        $menu = 'Billing Information';
                    } elseif($value['menu_type'] == 5){
                        $menu = 'Statue and Books';
                    } elseif($value['menu_type'] == 8) {
                        $menu = '';
                    }

                    $result_data[$i]['name'] = $value['name'];    
                    $result_data[$i]['ip_address'] = $value['ip_address'];
                    $result_data[$i]['login_date_time'] = date('d-m-Y h:i:s A',strtotime($value['log_date_time'])); 
                    $result_data[$i]['service_name'] = ((!empty($menu)) ? $menu . ' - ' : '') . $service;    
                    $result_data[$i]['page_type'] = $value['page_type'];    
                    $result_data[$i]['description'] = $value['desc'];    
                    
                    $i++;
                    endforeach;
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
                
                    $results['data'] = '';
                endif;
                
                return $results;

            break; 
            
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
        
    }    
    
    function servicetypes() {
        switch( CMD ) :
            
            case 'SERVICES' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $menuid = (isset($psdata['menuid']) && $psdata['menuid']!='') ? $psdata['menuid'] : '';
                
                if($menuid =="") {
                    throw new Exception('Menu Id was empty!');
                }
                
                $services = $this->get_service_lists($menuid);
                
                $result_data = array();
                if (count($services)>0) {
                    $i=0;
                    foreach( $services as $key => $value) :

                        $result_data[$i]['id'] = $value['id'];    
                        $result_data[$i]['service_name'] = $value['name'];
                    
                        $i++;
                    endforeach;
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
                
                    $results['data'] = '';
                endif;
                
                return $results;

            break; 
            
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
        
    }
    
    
    function abishegam() {
        switch( CMD ) :
        
            case 'ABISHEGAM' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
                
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
                
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
                
                $abishgam_details = $this->abishegam_details($startdate,$enddate,$type,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($abishgam_details)>0) {
                    $i=0;
                    foreach( $abishgam_details as $key => $value) :
                        
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['ba_rec_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['function_date'] = date('d-m-Y',strtotime($value['ba_fn_from_date'])); 
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['creted_on'])); 
                        $result_data[$i]['deity'] = $value['deity'];   
                        $result_data[$i]['period'] = $value['period_str'];   
                        $result_data[$i]['time'] = date('H:i',strtotime($value['ba_fn_from_date'])) . ' - ' . date('H:i',strtotime($value['ba_fn_to_date']));
                        $result_data[$i]['name1'] = $value['ba_name_1'];    
                        $result_data[$i]['tamil_name1'] = $value['ba_tamil_name_1'];
                        $result_data[$i]['star1'] = $value['star'];                         
                        $result_data[$i]['name2'] = $value['ba_name_2'];    
                        $result_data[$i]['tamil_name2'] = $value['ba_tamil_name_2'];
                        $result_data[$i]['star2'] = $value['star2'];
                        $result_data[$i]['contact_no'] = $value['ba_contact_no'];                         
                        $result_data[$i]['email'] = $value['ba_email'];    
                        $result_data[$i]['address'] = $value['ba_address'];
                        $result_data[$i]['desc'] = $value['ba_desc'];    
                        $result_data[$i]['amount'] = $value['ba_amount'];
                        $i++;
                    endforeach;
                    
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
                
                    $results['data'] = '';
                endif;
                
                return $results;

            break;            
        
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function building() {
       switch( CMD ) :
       
           case 'BUILDING' :
               // List all services
               $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
               $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
               $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
               if(($startdate!="") && ($enddate=='')) {
                   throw new Exception('End Date was empty!');
               }
               
               if(($startdate=="") && ($enddate!='')) {
                   throw new Exception('Start Date was empty!');
               }
               
               $building_details = $this->building_details($startdate,$enddate,$type);
               $general = $this->get_general_settings_data();
               
               $result_data = array();
               if (count($building_details)>0) {
                   $i=0;
                   foreach( $building_details as $key => $value) :
                       
                       $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                       $result_data[$i]['type'] = $value['service_name'];
                       $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                       $result_data[$i]['name'] = $value['name'];
                       $result_data[$i]['tamil_name'] = $value['tamil_name'];   
                       $result_data[$i]['contact_no'] = $value['contact_no'];   
                       $result_data[$i]['email'] = $value['email'];    
                       $result_data[$i]['address'] = $value['address'];    
                       $result_data[$i]['desc'] = $value['desc'];
                       $result_data[$i]['amount'] = $value['amount'];
                       $i++;
                   endforeach;
                   
               }
              
               if (count($result_data) > 0) :
                   /**
                    * Success.
                    */
                   $results['header'] = array(
                       'status' => 'S',
                       'message' => ucfirst( CMD ) . ' is executed successfully',
                   );

                   // Set Results
                   $results['data'] = $result_data;
               else:
                   /**
                    * Error
                    */
                   $results['header'] = array(
                       'status' => 'N',
                       'message' => 'No records found.',
                   );
               
                   $results['data'] = '';
               endif;
               
               return $results;

           break;            
       
           default : 
               throw new Exception('Given command is not found!');
           break;


        endswitch;
    }
   
    function festival() {
        switch( CMD ) :
       
            case 'FESTIVAL' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $festival_details = $this->festival_details($startdate,$enddate,$type,$date_type);
                $general = $this->get_general_settings_data();
               
                $result_data = array();
                if (count($festival_details)>0) {
                    $i=0;
                    foreach( $festival_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['function_date'] = date('d-m-Y',strtotime($value['date'])); 
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['deity'] = $value['deity'];   
                        $result_data[$i]['period'] = $value['period_str'];   
                        $result_data[$i]['time'] = date('H:i',strtotime($value['from_time'])) . ' - ' . date('H:i',strtotime($value['to_time']));
                        $result_data[$i]['name1'] = $value['devotee_name1'];    
                        $result_data[$i]['tamil_name1'] = $value['tamil_name1']; 
                        $result_data[$i]['star1'] = $value['star1']; 
                        $result_data[$i]['name2'] = $value['devotee_name2']; 
                        $result_data[$i]['tamil_name2'] = $value['tamil_name2']; 
                        $result_data[$i]['star2'] = $value['star2']; 
                        $result_data[$i]['contact_no'] = $value['contact_no'];
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['address'] = $value['address'];
                        $result_data[$i]['desc'] = $value['desc'];
                        $result_data[$i]['amount'] = $value['amount']; 
                       
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                   /**
                    * Success.
                    */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                    * Error
                    */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function mega() {
        switch( CMD ) :
       
            case 'MEGA' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $mega_details = $this->mega_details($startdate,$enddate,$type,$date_type);
                $general = $this->get_general_settings_data();
               
                $result_data = array();
                if (count($mega_details)>0) {
                    $i=0;
                    foreach( $mega_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['function_date'] = date('d-m-Y',strtotime($value['date'])); 
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['deity'] = $value['deity'];   
                        $result_data[$i]['period'] = $value['period_str'];   
                        $result_data[$i]['time'] = date('H:i',strtotime($value['from_time'])) . ' - ' . date('H:i',strtotime($value['to_time']));
                        $result_data[$i]['name1'] = $value['devotee_name1'];    
                        $result_data[$i]['tamil_name1'] = $value['tamil_name1']; 
                        $result_data[$i]['star1'] = $value['star']; 
                        $result_data[$i]['name2'] = $value['devotee_name2']; 
                        $result_data[$i]['tamil_name2'] = $value['tamil_name2']; 
                        $result_data[$i]['star2'] = $value['star2']; 
                        $result_data[$i]['contact_no'] = $value['contact_no'];
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['address'] = $value['address'];
                        $result_data[$i]['desc'] = $value['desc'];
                        $result_data[$i]['amount'] = $value['amount'];    
                       
                        $i++;
                    endforeach;
                   
                }
               
                if (count($result_data) > 0) :
                    /**
                     * Success.
                    */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                   /**
                    * Error
                    */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function ubayam() {
        switch( CMD ) :
       
            case 'UBAYAM' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $ubayam_details = $this->ubayam_details($startdate,$enddate,$type,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($ubayam_details)>0) {
                    $i=0;
                    foreach( $ubayam_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['function_date'] = date('d-m-Y',strtotime($value['date'])); 
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['booked_date'])); 
                        $result_data[$i]['deity'] = $value['deity'];   
                        $result_data[$i]['period'] = $value['period_str'];   
                        $result_data[$i]['time'] = date('H:i',strtotime($value['from_time'])) . ' - ' . date('H:i',strtotime($value['to_time']));
                        $result_data[$i]['name1'] = $value['devotee_name1'];    
                        $result_data[$i]['tamil_name1'] = $value['tamil_name1'];
                        $result_data[$i]['star1'] = $value['star'];                         
                        $result_data[$i]['name2'] = $value['devotee_name2'];    
                        $result_data[$i]['tamil_name2'] = $value['tamil_name2'];
                        $result_data[$i]['star2'] = $value['star1'];
                        $result_data[$i]['contact_no'] = $value['contact_no'];                         
                        $result_data[$i]['email'] = $value['email'];    
                        $result_data[$i]['address'] = $value['address'];
                        $result_data[$i]['desc'] = $value['desc'];
                        $result_data[$i]['amount'] = $value['amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    
    function prasadam() {
        switch( CMD ) :
       
            case 'PRASADAM' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $period = (isset($psdata['period']) && $psdata['period']!='') ? $psdata['period'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $prasadam_details = $this->prasadam_details($startdate,$enddate,$period,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($prasadam_details)>0) {
                    $i=0;
                    foreach( $prasadam_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['bp_rec_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['function_date'] = date('d-m-Y h:i:s A',strtotime($value['bp_fn_date'])); 
                        $result_data[$i]['booked_date'] = date('d-m-Y h:i:s A',strtotime($value['created_on'])); 
                        $result_data[$i]['period'] = $value['period_str'];  
                        $result_data[$i]['name1'] = $value['bp_dev_name_1'];    
                        $result_data[$i]['tamil_name1'] = $value['bp_tamil_name_1'];
                        $result_data[$i]['star1'] = $value['star'];                         
                        $result_data[$i]['name2'] = $value['bp_dev_name_2'];    
                        $result_data[$i]['tamil_name2'] = $value['bp_tamil_name_2'];
                        $result_data[$i]['star2'] = $value['star2'];
                        $result_data[$i]['contact_no'] = $value['bp_contact_no'];                         
                        $result_data[$i]['email'] = $value['bp_email'];    
                        $result_data[$i]['address'] = $value['bp_address'];
                        $result_data[$i]['particulars'] = $value['bp_desc'];    
                        $result_data[$i]['amount'] = $value['bp_amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                        */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function other() {
        switch( CMD ) :
       
            case 'OTHER' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                   throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $other_details = $this->other_details($startdate,$enddate,$type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($other_details)>0) {
                    $i=0;
                    foreach( $other_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['period'] = ($value['period'] == 1) ? 'Morning' : 'Evening';   
                        $result_data[$i]['time'] = date('H:i',strtotime($value['from_time'])) . ' - ' . date('H:i',strtotime($value['to_time']));
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['name'] = $value['devotee_name'];  
                        $result_data[$i]['tamil_name'] = $value['tamil_name'];
                        $result_data[$i]['star'] = $value['star'];	
                        $result_data[$i]['contact_no'] = $value['contact_no'];   
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['desc'] = $value['desc'];	
                        $result_data[$i]['address'] = $value['address'];    
                        $result_data[$i]['amount'] = $value['amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function offering() {
        switch( CMD ) :
       
            case 'OFFERING' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $offering_details = $this->offering_details($startdate,$enddate,$type);
                $general = $this->get_general_settings_data();
               
                $result_data = array();
                if (count($offering_details)>0) {
                    $i=0;
                    foreach( $offering_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['name'] = $value['name'];  
                        $result_data[$i]['tamil_name'] = $value['tamil_name'];
                        $result_data[$i]['contact_no'] = $value['contact_no'];   
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['desc'] = $value['desc'];	
                        $result_data[$i]['amount'] = $value['amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function prasadam_sale() {
        switch( CMD ) :
       
            case 'PRASADAM_SALE' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $prasadam_sale_details = $this->prasadam_sale_details($startdate,$enddate);
                $result_data = array();
                if (count($prasadam_sale_details)>0) {
                    $i=0;
                    foreach( $prasadam_sale_details as $key => $value) :
                       
                        $result_data[$i]['function_date'] = date('d-m-Y',strtotime($value['bp_fn_date'])); 
                        $result_data[$i]['prasadam'] = $value['prasadam'];
                        $result_data[$i]['quantity'] = $value['qty']; 
                        $result_data[$i]['amount'] = $value['total'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function madapalli() {
        switch( CMD ) :
       
            case 'MADAPALLI' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $madapalli_details = $this->madapalli_details($startdate,$enddate,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($madapalli_details)>0) {
                    $i=0;
                    foreach( $madapalli_details as $key => $value) :
                       $result_data[$i]['receipt_no'] = $value['prefix'].$value['bm_rec_no'];    
                       $result_data[$i]['type'] = $value['service_name'];   
                       $result_data[$i]['amount'] = $value['bm_amount'];
                       $result_data[$i]['function_date'] = date('d-m-Y',strtotime($value['bm_fn_date'])); 
                       $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['bm_booked_date'])); 
                       $result_data[$i]['period'] = $value['period_str']; 
                       $result_data[$i]['name'] = $value['bm_dev_name'];    
                       $result_data[$i]['tamil_name'] = $value['bm_tamil_name'];
                       $result_data[$i]['contact_no'] = $value['bm_contact_no'];
                       $result_data[$i]['email'] = $value['bm_email'];
                       $result_data[$i]['description'] = $value['bm_desc'];
                       $result_data[$i]['deity'] = $value['deity'];
                       $result_data[$i]['prasadam_name'] = $value['prasadam'];
                       $result_data[$i]['quantity'] = $value['bmi_qty'];
                       $result_data[$i]['price'] = number_format($value['bmi_price'] * $value['bmi_qty'] ,2,'.','');
                       $i++;
                   endforeach;
                   
               }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function kattalai() {
        switch( CMD ) :
       
            case 'KATTALAI' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(isset($psdata['datatype']) && $psdata['datatype']!='') {
                    if($psdata['datatype']=='details') {
                        $data_type = 'Get Details Data';
                    } else if($psdata['datatype']=='getdata') {
                        $data_type = 'Get Data';
                    } else {
                        throw new Exception('Invalid Data Type!');
                    }
                } else {
                    $data_type = 'Get Data';
                }
                
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
                
                $general = $this->get_general_settings_data();
                
                if($data_type == 'Get Data') {
                    $daily = $this->get_daily_data($startdate,$enddate,$type,$date_type);
                    $weekly = $this->get_weekly_data($startdate,$enddate,$type,$date_type);
                    $multiple = $this->get_multiple_data($startdate,$enddate,$type,$date_type);
              
                    $kattalai_data = array_merge($daily,$weekly,$multiple);
                    $result_data = array();
                    if (count($kattalai_data)>0) {
                        
                        $i = 0;
                        foreach( $kattalai_data as $key => $value) :
                            if($value['bki_day_type'] == 1) {
                                #$count = $value['bki_no_days'];
                                #$bki_multi_date = $value['bki_dates']; 
                                $multiple_date = explode(',',$value['bki_dates']);
                                $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                $count = 0;
                                $bki_date = '';
                                foreach($multiple_date as $k=>$row) {
                                    $bki_date .= ($start <= trim($row) && $end >= trim($row)) ? $row . ',' : $row . ',';
                                    if($start <= trim($row) && $end >= trim($row)) {
                                        $count += 1;
                                    }
                                }
                                $bki_multi_date = ' - ';
                            }else if ($value['bki_day_type'] == 2) {
                                $multiple_date = explode(',',$value['bki_dates']);
                                $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                $count = 0;
                                $bki_date = '';
                                foreach($multiple_date as $k=>$row) {
                                    $bki_date .= ($start <= trim($row) && $end >= trim($row)) ? $row .',' : $row . ',';
                                    if($start <= trim($row) && $end >= trim($row)) {
                                        if(date('w', strtotime($row)) == $value['bki_week_day']) {
                                            $count += 1;    
                                        }
                                    }
                                }
                                #$bki_multi_date = rtrim($bki_date,',');
                                $bki_multi_date = ' - ';
                            } elseif($value['bki_day_type'] == 3) {
                                $multiple_date = explode(',',$value['bki_dates']);
                                $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                $count = 0;
                                $bki_date = '';
                                foreach($multiple_date as $k=>$row) {
                                    $bki_date .= ($start <= trim($row) && $end >= trim($row)) ? $row .',' : $row . ',';
                                    if($start <= trim($row) && $end >= trim($row)) {
                                        $count += 1;
                                    }
                                }
                                $bki_multi_date = rtrim($bki_date,',');
                            }
                            
                         
                            if($count > 0) {
                                $godName = '';
                                if($value['bki_god_ids'] != '') {
                                    #$god_id = explode(',',$value['bki_god_ids']);
                                    $god_name = $this->get_deity_name($value['bki_god_ids']);
                                    $godName =  $god_name[0]['deity'];     
                                } 
                                
                                $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) .  $value['bk_rec_no'];    
                                $result_data[$i]['type'] = $value['service_name'];
                                $result_data[$i]['name(s)'] = $value['bk_name_1'] . ( !empty( $value['bk_name_2'] ) ? ' / '. $value['bk_name_2'] : '' );
                                $result_data[$i]['tamil_name(s)'] = $value['bk_tamil_name_1'] . ( !empty( $value['bk_tamil_name_2'] ) ? ' / '. $value['bk_tamil_name_2'] : '' );
                                $result_data[$i]['star(s)'] = $value['star'] . ( !empty( $value['star2'] ) ? ' / '.$value['star2'] : '' );    
                                $result_data[$i]['deity'] =  $godName;
                                $result_data[$i]['from_date'] = date('d-m-Y',strtotime($value['bki_from_date']));    
                                $result_data[$i]['to_date'] = date('d-m-Y',strtotime($value['bki_to_date']));   
                                $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on']));   
                                $result_data[$i]['day_type'] = $value['day_type'];
                                $result_data[$i]['day_count'] = $count;
                                $result_data[$i]['total_days'] = $value['bki_no_days'];
                                $result_data[$i]['multiple_dates'] = $bki_multi_date;
                                $result_data[$i]['desc'] = $value['bk_desc'];
                                $result_data[$i]['amount'] = $value['bki_amount'];
                               $i++;
                            }                            
                            
                        endforeach;
                        
                    }    
                } elseif($data_type == 'Get Details Data') {
                    $result_data = array();
                    $daily_deities = $this->get_details_gods_by_kattalai_items_daily($startdate,$enddate,$type,$date_type);
                    $weekly_deities = $this->get_details_gods_by_kattalai_items_weekly($startdate,$enddate,$type,$date_type);
                    $multiple_deities = $this->get_details_gods_by_kattalai_items_multiple($startdate,$enddate,$type,$date_type);
                    
                    $deities = array_merge($daily_deities,$weekly_deities,$multiple_deities);
                    
                    $details_data_daily = $this->get_details_data_by_god_daily($startdate,$enddate,$type,$date_type);
                    $details_data_weekly = $this->get_details_data_by_god_weekly($startdate,$enddate,$type,$date_type);
                    $details_data_multiple = $this->get_details_data_by_god_multiple($startdate,$enddate,$type,$date_type);
                   
                    $details_data = array_merge($details_data_daily,$details_data_weekly,$details_data_multiple);
                    
                    foreach($deities as $k => $row) {
                        $gods[$row['id']] = array($row['name']);
                    }
                    foreach($details_data as $key => $value) {
                        $e = explode(',',$value['bki_god_ids']);
                        foreach($e as $index => $val) {
                            $gods[$e[$index]][] = array($value);
                       }
                    }
                    
                    $god_details = $gods;
                    if(count($god_details) > 0) {  
                        $ii = 0;
                        foreach($god_details as $k => $row) {
                            $count = 0;
                            foreach($row as $i => $td) { 
                                foreach($td as $index => $val) {
                                    if($val['bki_day_type'] == 1) {
                                        #$count = $val['bki_no_days'];
                                        #$bki_multi_date = $value['bki_dates']; 
                                        $multiple_date = explode(',',$val['bki_dates']);
                                        $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                        $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                        //$count = 0;
                                        $bki_date = '';
                                        foreach($multiple_date as $ki=>$Row) {
                                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? $Row . ',' : $Row . ',';
                                            if($start <= trim($Row) && $end >= trim($Row)) {
                                                $count += 1;
                                            }
                                        }
                                        $bki_multi_date = ' - ';
                                    } else if ($val['bki_day_type'] == 2) {
                                        $multiple_date = explode(',',$val['bki_dates']);
                                        $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                        $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                        //$count = 0;
                                        $bki_date = '';
                                        foreach($multiple_date as $ki=>$Row) {
                                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? $Row . ',' : $Row . ',';
                                            if($start <= trim($Row) && $end >= trim($Row)) {
                                                if(date('w', strtotime($Row)) == $val['bki_week_day']) {
                                                    $count += 1;    
                                                }
                                            }
                                        }
                                        #$bki_multi_date = rtrim($bki_date,',');
                                        $bki_multi_date = ' - ';
                                    } elseif($val['bki_day_type'] == 3) {
                                        $multiple_date = explode(',',$val['bki_dates']);
                                        $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                        $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                       // $count = 0;
                                        $bki_date = '';
                                        foreach($multiple_date as $ki=>$Row) {
                                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? $Row . ',' : $Row . ',';
                                            if($start <= trim($Row) && $end >= trim($Row)) {
                                                $count += 1;
                                            }
                                        }
                                        $bki_multi_date = rtrim($bki_date,',');
                                    }
                                }
                            }
                            
                            if($count > 0) {
                                $result_data[$ii]['deity'] = $row[0];
                            } else {
                                $result_data[$ii]['deity'] = '';
                            }
                            
                            unset($row[0]) ;
                            $kk = 0;
                            foreach($row as $i => $td) {   
                                foreach($td as $index => $val) {
                                    if($val['bki_day_type'] == 1) {
                                        #$count = $val['bki_no_days'];
                                        #$bki_multi_date = $value['bki_dates']; 
                                        $multiple_date = explode(',',$val['bki_dates']);
                                        $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                        $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                        $count = 0;
                                        $bki_date = '';
                                        foreach($multiple_date as $ki=>$Row) {
                                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? $Row . ',' : $Row . ',';
                                            if($start <= trim($Row) && $end >= trim($Row)) {
                                                $count += 1;
                                            }
                                        }
                                        $bki_multi_date = ' - ';
                                    } else if ($val['bki_day_type'] == 2) {
                                        $multiple_date = explode(',',$val['bki_dates']);
                                        $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                        $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                        $count = 0;
                                        $bki_date = '';
                                        foreach($multiple_date as $ki=>$Row) {
                                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? $Row . ',' : $Row . ',';
                                            if($start <= trim($Row) && $end >= trim($Row)) {
                                                if(date('w', strtotime($Row)) == $val['bki_week_day']) {
                                                    $count += 1;    
                                                }
                                            }
                                        }
                                        #$bki_multi_date = rtrim($bki_date,',');
                                        $bki_multi_date = ' - ';
                                    } elseif($val['bki_day_type'] == 3) {
                                        $multiple_date = explode(',',$val['bki_dates']);
                                        $start = ($startdate != '') ?  $startdate : date('d-m-Y');
                                        $end = ($enddate != '') ?  $enddate : date('d-m-Y');
                                        $count = 0;
                                        $bki_date = '';
                                        foreach($multiple_date as $ki=>$Row) {
                                            $bki_date .= ($start <= trim($Row) && $end >= trim($Row)) ? $Row . ',' : $Row . ',';
                                            if($start <= trim($Row) && $end >= trim($Row)) {
                                                $count += 1;
                                            }
                                        }
                                        $bki_multi_date = rtrim($bki_date,',');
                                    }
                                    
                                    $god_names = $this->get_deity_name($val['bki_god_ids']);
                                    
                                    if($count > 0) {
                                        if($val['bki_day_type'] == 1) { $dayType =  'Daily'; } elseif($val['bki_day_type'] == 2) { $dayType = 'Weekly'; } elseif( $val['bki_day_type'] == 3 ) { $dayType = 'Multiple Date(s)'; }
                                        
                                        $bki_week_day = '';
                                        if($val['bki_day_type'] == 2) {
                                            if($val['bki_week_day'] == 0) { $bki_week_day = 'Sunday'; } 
                                            elseif($val['bki_week_day'] == 1) { $bki_week_day = 'Monday'; }
                                            elseif($val['bki_week_day'] == 2) { $bki_week_day = 'Tuesday'; }
                                            elseif($val['bki_week_day'] == 3) { $bki_week_day = 'Wednesday'; }
                                            elseif($val['bki_week_day'] == 4) { $bki_week_day = 'Thursday'; }
                                            elseif($val['bki_week_day'] == 5) { $bki_week_day = 'Friday'; }
                                            elseif($val['bki_week_day'] == 6) { $bki_week_day = 'Saturday'; }
                                        }
                                        
                                        $result_data[$ii]['devotee_details'][$kk]['receipt_no'] = (!empty($general[0]['prefix']) ?  $val['prefix'] : '' ) .  $val['bk_rec_no'];    
                                        $result_data[$ii]['devotee_details'][$kk]['god_name'] = $god_names[0]['deity'];
                                        $result_data[$ii]['devotee_details'][$kk]['name'] = ($val['bk_name_1']  . ' - '. $val['star'] . ((!empty($val['bk_name_2'])) ? '<br/>' .$val['bk_name_2'] . ((!empty($val['bk_star_2'])) ? ' - ' . $val['star2'] : ''): '') );
                                        $result_data[$ii]['devotee_details'][$kk]['tamil_name(s)'] = $val['bk_tamil_name_1'] . ( !empty( $val['bk_tamil_name_2'] ) ? ' / '. $val['bk_tamil_name_2'] : '' );
                                        $result_data[$ii]['devotee_details'][$kk]['booked_date'] = date('d-m-Y',strtotime($val['created_on'])); 
                                        $result_data[$ii]['devotee_details'][$kk]['from_date'] = date('d-m-Y',strtotime($val['bki_from_date']));    
                                        $result_data[$ii]['devotee_details'][$kk]['to_date'] = date('d-m-Y',strtotime($val['bki_to_date']));    
                                        $result_data[$ii]['devotee_details'][$kk]['type'] = $dayType;
                                        $result_data[$ii]['devotee_details'][$kk]['day'] = $bki_week_day;
                                        $result_data[$ii]['devotee_details'][$kk]['total_weeks'] = $val['bki_week_day'];
                                        $result_data[$ii]['devotee_details'][$kk]['total_days'] = $val['bki_no_days'];
                                        $result_data[$ii]['devotee_details'][$kk]['amount'] = $val['bki_amount'];
                                    }
                                }
                                $kk++;
                            }
                            $ii++;
                        }
                    }
                }
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
              
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function hall_booking() {
        switch( CMD ) :
       
            case 'HALL_BOOKING' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $hall_booking_details = $this->hall_booking_details($startdate,$enddate,$type,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($hall_booking_details)>0) {
                    $i=0;
                    foreach( $hall_booking_details as $key => $value) :
                    
                        $part_pay_rec_no = $this->get_part_pay_rec_no($value['id']);
                        $rec_no = explode(',',$part_pay_rec_no[0]['receipt_no']);  $Receipt = '';
                        foreach($rec_no as $no) {
                            if(!empty($no)) {
                                $Receipt .= ((($general[0]['prefix']== 1) ? $value['prefix'] : '') . trim($no));
                                $Receipt .= ',';
                            }
                        }
                        $Rec = rtrim($Receipt,',');
                        $bh_rec_no = ((!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['bh_rec_no']);
                        $result_data[$i]['receipt_no'] =  $bh_rec_no . ((!empty($Rec)) ? '(' . $Rec . ')' : '');    
                        $result_data[$i]['type'] = $value['service_name'];  
                        $result_data[$i]['event_date'] = date('d-m-Y',strtotime($value['bh_from_date']));
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['period'] = $value['period_str']; 
                        $result_data[$i]['time'] = date('H:i',strtotime($value['bh_from_date'])). ' - ' . date('H:i',strtotime($value['bh_to_date'])); 
                        $result_data[$i]['event_type'] = $value['bh_evt_type'];
                        $result_data[$i]['name'] = $value['bh_name']; 
                        $result_data[$i]['tamil_name'] = $value['bh_tamil_name']; 
                        $result_data[$i]['contact_no'] = $value['bh_contact_no'];
                        $result_data[$i]['email'] = $value['bh_email'];
                        $result_data[$i]['address'] = $value['bh_address'];
                        $result_data[$i]['desc'] = $value['bh_desc'];
                        $result_data[$i]['amount'] = $value['bh_paid_amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function wedding_hall() {
        switch( CMD ) :
       
            case 'WEDDING_HALL' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
                $period = (isset($psdata['period']) && $psdata['period']!='') ? $psdata['period'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $wed_hall_booking_details = $this->wedding_hall_booking_details($startdate,$enddate,$type,$period,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($wed_hall_booking_details)>0) {
                    $i=0;
                    foreach( $wed_hall_booking_details as $key => $value) :
                    
                        $part_pay_rec_no = $this->get_part_pay_bwh_rec_no($value['id']);
                        $rec_no = explode(',',$part_pay_rec_no[0]['receipt_no']);  $Receipt = '';
                        foreach($rec_no as $no) {
                            if(!empty($no)) {
                                $Receipt .= ((($general[0]['prefix']== 1) ? $value['prefix'] : '') . trim($no));
                                $Receipt .= ',';
                            }
                        }
                        $Rec = rtrim($Receipt,',');
                        $bwh_rec_no = ((!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['bwh_rec_no']);
                        
                        $result_data[$i]['receipt_no'] = $bwh_rec_no . ((!empty($Rec)) ? '(' . $Rec . ')' : '');    
                        $result_data[$i]['type'] = $value['service_name'];  
                        $result_data[$i]['wedding_date'] = date('d-m-Y',strtotime($value['bwh_from_date'])); 
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['period'] = $value['period_str'];
                        $result_data[$i]['time'] = date('H:i',strtotime($value['bwh_from_date'])). ' - ' . date('H:i',strtotime($value['bwh_to_date'])); 
                        $result_data[$i]['groom_name'] = $value['bwh_groom_name'];
                        $result_data[$i]['bride_name'] = $value['bwh_bride_name']; 
                        $result_data[$i]['contact_person'] = $value['bwh_contact_name'];
                        $result_data[$i]['contact_no'] = $value['bwh_contact_no']; 
                        $result_data[$i]['email'] = $value['bwh_email'];
                        $result_data[$i]['address'] = $value['bwh_address']; 
                        $result_data[$i]['desc'] = $value['bwh_desc'];
                        $result_data[$i]['amount'] = $value['bwh_paid_amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
   
    function cashlog() {
        switch( CMD ) :
       
            case 'CASHLOG' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
                
                $payment_types = $this->list_payment_type();
                $cashlog_details = $this->cashlog_details($startdate,$enddate);
                
                $result_data = array();
                if (count($cashlog_details)>0) {
                    $i=0;
                    foreach( $cashlog_details as $key => $value) :
                        $result_data[$i]['cash_date'] = date('d-m-Y',strtotime($value['created_on']));
                        foreach($payment_types as $index => $row) { 
                           $pay_details = $this->get_pay_amount_by_type($value['pay_ref_type'],$value['pay_ref_id'],$startdate,$enddate); 
                           $paid_amt = 0; 
                            foreach($pay_details as $kk => $pay) {
                                if($row['id'] == $pay['pay_type_id']) {
                                    $paid_amt += $pay['pay_amount'];
                                }
                            }
                            $result_data[$i][$row['payment_name']] = number_format($paid_amt,2,'.','');
                        }
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function temple() {
        switch( CMD ) :
       
            case 'TEMPLE' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
                
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
                
                $temple_details = $this->temple_details($startdate,$enddate,$type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($temple_details)>0) {
                    $i=0;
                    foreach( $temple_details as $key => $value) :
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];  
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on']));
                        $result_data[$i]['term'] = $value['term_str'];
                        $result_data[$i]['name'] = $value['devotee_name'];
                        $result_data[$i]['tamil_name'] = $value['tamil_name']; 
                        $result_data[$i]['sex'] = ($value['sex'] == 1) ? 'Male' : 'Female';
                        $result_data[$i]['contact_no'] = $value['contact_no']; 
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['class_type'] = $value['class_type_str']; 
                        $result_data[$i]['class'] = $value['class_str'];
                        $result_data[$i]['transport_facility'] = ($value['transport_facility'] == 1) ? 'Yes' : 'No';
                        $result_data[$i]['bus_no'] = $value['bus_no'];
                        $result_data[$i]['fee'] = $value['fee']; 
                        $result_data[$i]['address'] = $value['address'];
                        $result_data[$i]['desc'] = $value['desc']; 
                        $result_data[$i]['amount'] = $value['amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function donation() {
        switch( CMD ) :
       
            case 'DONATION' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
                $contactno = (isset($psdata['contactno']) && $psdata['contactno']!='') ? $psdata['contactno'] : '';
                $ispledge = (isset($psdata['ispledge']) && $psdata['ispledge']!='') ? $psdata['ispledge'] : '';
                $pledgetype = (isset($psdata['pledgetype']) && $psdata['pledgetype']!='') ? $psdata['pledgetype'] : '';
                
                if(isset($psdata['datatype']) && $psdata['datatype']!='') {
                    if($psdata['datatype']=='pledge' && $ispledge == '1') {
                        $data_type = 'Get Pledge Data';
                    } else if($psdata['datatype']=='summary') {
                        $data_type = 'Get Summary Data';
                    } else {
                        throw new Exception('Invalid Data Type!');
                    }
                } else {
                    $data_type = 'Get Summary Data';
                }
                
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
                
                if($ispledge == 2) {
                    $donation_details = $this->donation_details($startdate,$enddate,$type,$contactno,$ispledge,$pledgetype);
                } else {
                    $donation_details = $this->get_pledge_details($startdate,$enddate,$type,$contactno,$ispledge,$pledgetype);
                }
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($donation_details)>0) {
                    $i=0;
                    if($data_type == 'Get Summary Data') {
                        foreach( $donation_details as $key => $value) :
                        
                            if($value['amount_type'] == 1) { $amounttype = 'Amount'; } elseif($value['amount_type'] == 2) { $amounttype = 'Item'; } elseif($value['amount_type'] == 3) { $amounttype = 'Ongoing';}
                            
                            /* $Amount = ((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? 'Amount (grams)' : 'Amount';
                            $PaidAmount = ((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? 'Paid Amount (grams)' : 'Paid Amount'; */
                            
                            $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                            $result_data[$i]['type'] = $value['service_name'] . (((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? ' (Item)' : '');  
                            $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on']));
                            $result_data[$i]['name'] = (!empty($value['name'])) ? $value['name'] : '';
                            $result_data[$i]['tamil_name'] = (!empty($value['tamil_name'])) ? $value['tamil_name'] : '';
                            $result_data[$i]['contact_no'] = (!empty($value['contact_no'])) ? $value['contact_no'] : ''; 
                            $result_data[$i]['email'] = (!empty($value['email'])) ? $value['email'] : ''; 
                            $result_data[$i]['address'] = (!empty($value['address'])) ? $value['address'] : '';
                            $result_data[$i]['desc'] = (!empty($value['desc'])) ? $value['desc'] : ''; 
                            $result_data[$i]['amount_type'] = $amounttype;
                            $result_data[$i]['amount'] = $value['total_amount']; 
                            $result_data[$i]['paid_amount'] = ($ispledge == 1) ? number_format($value['paid_amount'],2,'.','') : '0.00';
                            $i++;
                        endforeach;
                    } elseif($data_type == 'Get Pledge Data') {
                        foreach( $donation_details as $key => $value) :
                            
                            /* Sub Table*/
                            $donation_trans = $this->get_donation_pledge_transaction($value['service_id'],$value['name'],$value['contact_no'],$value['amount_type'],$startdate,$enddate,$value['id']);
                            
                            if($value['amount_type'] == 1) { $amounttype = 'Amount'; } elseif($value['amount_type'] == 2) { $amounttype = 'Item'; } elseif($value['amount_type'] == 3) { $amounttype = 'Ongoing';}
                            
                            /* $pledge_amount = ((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? 'Pledge Amount (grams)' : 'Pledge Amount';
                            $PaidAmount = ((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? 'Paid Amount (grams)' : 'Paid Amount';
                            $BalanceAmount = ((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? 'Balance Amount (grams)' : 'Balance Amount';
                            
                            $Amount = ((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? 'Amount (grams)' : 'Amount'; */                            
                           
                            $result_data[$i]['date'] = date('d-m-Y',strtotime($value['created_on']));    
                            $result_data[$i]['type'] = $value['service_name'] . (((!empty($ispledge)) && $ispledge == 1 && $pledgetype == 2) ? ' (Item)' : '');  
                            $result_data[$i]['devotee_name'] = (!empty($value['name'])) ? $value['name'] : '';
                            $result_data[$i]['contact_no'] = (!empty($value['contact_no'])) ? $value['contact_no'] : '';
                            $result_data[$i]['pledge_amount'] = $value['pledge_amount'];
                            $result_data[$i]['paid_amount'] = number_format($value['amount'],2,'.',''); 
                            $result_data[$i]['balance_amount'] = number_format(($value['pledge_amount'] - $value['amount']),2,'.',''); 
                            $k = 0;
                            foreach($donation_trans as $index => $row) {
                                $result_data[$i]['transaction_details'][$k]['receipt_no'] = $row['receipt_no'];
                                $result_data[$i]['transaction_details'][$k]['date'] = date('d-m-Y',strtotime($row['pledge_date']));
                                $result_data[$i]['transaction_details'][$k]['amount'] = number_format($row['amount'],2,'.','');
                                $k++;
                            }
                            $i++;
                        endforeach;
                    }
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function outdoor() {
        switch( CMD ) :
       
            case 'OUTDOOR' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $date_type = (isset($psdata['datetype']) && $psdata['datetype']!='') ? $psdata['datetype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $outdoor_details = $this->outdoor_details($startdate,$enddate,$type,$date_type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($outdoor_details)>0) {
                    $i=0;
                    foreach( $outdoor_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) .  $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['date'])); 
                        $result_data[$i]['period'] = $value['period_str'];
                        $result_data[$i]['time'] = date('H:i', strtotime($value['from_time'])) . ' - ' . date('H:i',strtotime($value['to_time']));	
                        $result_data[$i]['name'] = $value['devotee_name'];   
                        $result_data[$i]['tamil_name'] = $value['tamil_name'];  
                        $result_data[$i]['contact_no1'] = $value['contact_no1'];
                        $result_data[$i]['contact_no2'] = $value['contact_no2'];
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['address'] = $value['address'];    
                        $result_data[$i]['postal_code'] = $value['postal_code'];    
                        $result_data[$i]['priest'] = (!empty($value['priest'])) ? $value['priest'] : '';
                        $result_data[$i]['melam'] = (!empty($value['melam'])) ?  $value['melam'] : '';
                        $result_data[$i]['nathaswaram'] = (!empty($value['nathaswaram'])) ? $value['nathaswaram'] : '';
                        $result_data[$i]['desc'] = $value['desc'];
                        $result_data[$i]['amount'] = $value['amount'];
                       $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function subscription() {
        switch( CMD ) :
       
            case 'SUBSCRIPTION' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $subscription_details = $this->subscription_details($startdate,$enddate,$type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($subscription_details)>0) {
                    $i=0;
                    foreach( $subscription_details as $key => $value) :

                        if($value['maritial_status'] == 1) { 
                            $maritial_status = 'Single'; 
                        } elseif($value['maritial_status'] == 2) { 
                            $maritial_status = 'Married';
                        }
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['date_of_joined'] = date('d-m-Y',strtotime($value['date_of_join']));
                        $result_data[$i]['member_no'] = $value['member_no'];	
                        $result_data[$i]['name'] = $value['member_name'];
                        $result_data[$i]['tamil_name'] = $value['tamil_name'];
                        $result_data[$i]['dob'] = date('d-m-Y',strtotime($value['dob']));
                        $result_data[$i]['sex'] = ($value['sex'] == 1) ? 'Male' : 'Female'; 
                        $result_data[$i]['maritial_status'] = $maritial_status;
                        $result_data[$i]['contact_no'] = $value['contact_no'];   
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['address'] = $value['address'];
                        $result_data[$i]['postal_code'] = $value['postal_code'];
                        $result_data[$i]['desc'] = $value['desc'];
                        $result_data[$i]['entry_fee_amount'] = $value['entry_fee_amount'];	
                        $result_data[$i]['amount'] = $value['amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function card() {
        switch( CMD ) :
       
            case 'CARD' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $card_no = (isset($psdata['cardno']) && $psdata['cardno']!='') ? $psdata['cardno'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
              
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $card_details = $this->card_details($startdate,$enddate,$card_no);
               
                $result_data = array();
                if (count($card_details)>0) {
                    $i=0;
                    foreach( $card_details as $key => $value) :
                       
                        $result_data[$i]['card_no'] = $value['card_no'];    
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['name'] = $value['name'];   
                        $result_data[$i]['tamil_name'] = (!empty($value['tamil_name'])) ? $value['tamil_name'] : '';  
                        $result_data[$i]['star'] = $value['star'];
                        $result_data[$i]['contact_no'] = $value['contact_no'];
                        $result_data[$i]['email'] = (!empty($value['email'])) ? $value['email'] : '';
                        $result_data[$i]['address'] = (!empty($value['address'])) ? $value['address'] : '';    
                        $result_data[$i]['amount'] = $value['topup_amount'];

                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function temple_shop_sale() {
        switch( CMD ) :
       
            case 'TEMPLE_SHOP_SALE' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
                $type = (isset($psdata['type']) && $psdata['type']!='') ? $psdata['type'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                   throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $temple_shop_sale_details = $this->temple_shop_sale_details($startdate,$enddate,$type);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($temple_shop_sale_details)>0) {
                    $i=0;
                    foreach( $temple_shop_sale_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['booked_date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['name'] = $value['devotee_name1'];
                        $result_data[$i]['contact_no'] = $value['contact_no'];   
                        $result_data[$i]['email'] = $value['email'];
                        $result_data[$i]['desc'] = $value['desc'];	
                        $result_data[$i]['address'] = $value['address'];    
                        $result_data[$i]['amount'] = $value['amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function statue_category() {
        switch( CMD ) :
       
            case 'STATUE_CATEGORY' :
                // List all services
               
                $statue_category_details = $this->statue_category_details();
                
                $result_data = array();
                if (count($statue_category_details)>0) {
                    $i=0;
                    foreach( $statue_category_details as $key => $value) :
                       
                        $result_data[$i]['id'] = $value['id'];   
                        $result_data[$i]['category_code'] = $value['category_code'];
                        $result_data[$i]['category_name'] = $value['category_name'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function statue_item() {
        switch( CMD ) :
       
            case 'STATUE_ITEM' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $category_id = (isset($psdata['category_id']) && $psdata['category_id']!='') ? $psdata['category_id'] : '';
                $statue_item_details = $this->statue_item_details($category_id);
                
                $result_data = array();
                if (count($statue_item_details)>0) {
                    $i=0;
                    foreach( $statue_item_details as $key => $value) :
                       
                        $result_data[$i]['id'] = $value['id'];   
                        $result_data[$i]['item_code'] = $value['item_code'];
                        $result_data[$i]['item_name'] = $value['item_name'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               ///echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function statuebook_purchase() {
        switch( CMD ) :
       
            case 'STATUEBOOK_PURCHASE' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $cat_id = (isset($psdata['category_id']) && $psdata['category_id']!='') ? $psdata['category_id'] : '';
                $item_id = (isset($psdata['item_id']) && $psdata['item_id']!='') ? $psdata['item_id'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $purcahse_details = $this->statuebook_purchase_details($startdate,$enddate,$cat_id,$item_id);
                
                $result_data = array();
                if (count($purcahse_details)>0) {
                    $i=0;
                    foreach( $purcahse_details as $key => $value) :
                       
                        $result_data[$i]['ref_no'] = $value['code'];    
                        $result_data[$i]['category_name'] = $value['cat_name'];
                        $result_data[$i]['item_name'] = $value['item_name'];
                        $result_data[$i]['invoice_no'] = $value['invoice_no'];
                        $result_data[$i]['invoice_date'] = ($value['invoice_date'] != '1970-00-00 00:00:00') ? date('d-m-Y',strtotime($value['invoice_date'])) : ''; 
                        $result_data[$i]['vendor_name'] = $value['vendor'];
                        $result_data[$i]['ship_advice_no'] = $value['ship_advice_no'];   
                        $result_data[$i]['received_date'] = ($value['received_date'] != '1970-00-00 00:00:00') ? date('d-m-Y',strtotime($value['received_date'])) : ''; 
                        $result_data[$i]['box_no'] = $value['box_no'];	
                        $result_data[$i]['description'] = $value['description'];
                        $result_data[$i]['quantity'] = $value['quantity'];   
                        $result_data[$i]['cost_per_item'] = $value['cost_per_item'];
                        $result_data[$i]['amount'] = $value['total_amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function statuebook_sale() {
        switch( CMD ) :
       
            case 'STATUEBOOK_SALE' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $cat_id = (isset($psdata['category_id']) && $psdata['category_id']!='') ? $psdata['category_id'] : '';
                $item_id = (isset($psdata['item_id']) && $psdata['item_id']!='') ? $psdata['item_id'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $sale_details = $this->statuebook_sale_details($startdate,$enddate,$cat_id,$item_id);
                $general = $this->get_general_settings_data();
                
                $result_data = array();
                if (count($sale_details)>0) {
                    $i=0;
                    foreach( $sale_details as $key => $value) :
                       
                        $result_data[$i]['receipt_no'] = (!empty($general[0]['prefix']) ?  $value['prefix'] : '' ) . $value['receipt_no'];    
                        $result_data[$i]['type'] = $value['service_name'];
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['category_name'] = $value['category_name'];
                        $result_data[$i]['item_name'] = $value['item_name'];
                        $result_data[$i]['quantity'] = $value['quantity'];
                        $result_data[$i]['amount'] = $value['sub_total'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function statuebook_stock() {
        switch( CMD ) :
       
            case 'STATUEBOOK_STOCK' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $cat_id = (isset($psdata['category_id']) && $psdata['category_id']!='') ? $psdata['category_id'] : '';
                $item_id = (isset($psdata['item_id']) && $psdata['item_id']!='') ? $psdata['item_id'] : '';
               
                $stock_details = $this->statuebook_stock_details($cat_id,$item_id);
                
                $result_data = array();
                if (count($stock_details)>0) {
                    $i=0;
                    foreach( $stock_details as $key => $value) :
                       
                        $result_data[$i]['category_name'] = $value['cat_name'];    
                        $result_data[$i]['item_name'] = $value['item_name'];
                        $result_data[$i]['purchased_quantity'] = (!empty($value['purchased_qty'])) ? $value['purchased_qty'] : '0';
                        $result_data[$i]['sales_quantity'] = (!empty($value['sales_qty'])) ? $value['sales_qty'] : '0';                        
                        $result_data[$i]['balance_quantity'] = (!empty($value['total_quantity'])) ? $value['total_quantity'] : '0';
                        $result_data[$i]['unit_price'] = $value['selling_price'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function expense_types() {
        switch( CMD ) :
       
            case 'EXPENSE_TYPES' :
                // List all services
               
                $expenses_type_list = $this->expenses_type_list();
                
                $result_data = array();
                if (count($expenses_type_list)>0) {
                    $i=0;
                    foreach( $expenses_type_list as $key => $value) :
                        
                        $result_data[$i]['expense_type'] = $value['exp_type'];
                        $result_data[$i]['expense_type_slug'] = $value['exp_type_slug'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function expense_subjects() {
        switch( CMD ) :
       
            case 'EXPENSE_SUBJECTS' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $expense_type_slug = (isset($psdata['expense_type_slug']) && $psdata['expense_type_slug']!='') ? $psdata['expense_type_slug'] : '';
                $expense_subjects_list = $this->expense_subjects_list($expense_type_slug);
                
                $result_data = array();
                if (count($expense_subjects_list)>0) {
                    $i=0;
                    foreach( $expense_subjects_list as $key => $value) :
                    
                        $result_data[$i]['expense_subject'] = $value['exp_sub'];
                        $result_data[$i]['expense_subject_slug'] = $value['exp_sub_slug'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               ///echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    
    function expenses() {
        switch( CMD ) :
       
            case 'EXPENSES' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $type_slug = (isset($psdata['type_slug']) && $psdata['type_slug']!='') ? $psdata['type_slug'] : '';
                $subject_slug = (isset($psdata['subject_slug']) && $psdata['subject_slug']!='') ? $psdata['subject_slug'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                
                $expenses_details = $this->expenses_details($type_slug,$subject_slug,$startdate,$enddate);
                
                $result_data = array();
                if (count($expenses_details)>0) {
                    $i=0;
                    foreach( $expenses_details as $key => $value) :
                       
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['exp_date']));    
                        $result_data[$i]['ref_no'] = $value['exp_rec_no'];
                        $result_data[$i]['type'] = $value['exp_type'];
                        $result_data[$i]['subject'] = $value['exp_sub'];                        
                        $result_data[$i]['name'] = $value['exp_name'];
                        $result_data[$i]['desc'] = $value['exp_desc'];
                        $result_data[$i]['amount'] = $value['exp_amount'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function inventory_item() {
        switch( CMD ) :
       
            case 'INVENTORY_ITEM' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                
                $inv_item_details = $this->inventory_item_details();
                
                $result_data = array();
                if (count($inv_item_details)>0) {
                    $i=0;
                    foreach( $inv_item_details as $key => $value) :
                       
                        $result_data[$i]['id'] = $value['id'];
                        $result_data[$i]['item_name'] = $value['item_name'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               ///echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function inventory_sale() {
        switch( CMD ) :
       
            case 'INVENTORY_SALE' :
                // List all Inventory Sale
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $item_id = (isset($psdata['item_id']) && $psdata['item_id']!='') ? $psdata['item_id'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $sale_details = $this->inventory_sale_details($startdate,$enddate,$item_id);
                
                $result_data = array();
                if (count($sale_details)>0) {
                    $i=0;
                    foreach( $sale_details as $key => $value) :
                       
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['item_name'] = $value['item_name'];
                        $result_data[$i]['usage_type'] = $value['usage_str'];
                        $result_data[$i]['total_quantity'] = $value['inv_qty'];
                        $result_data[$i]['issued_quantity'] = $value['total_issued_qty'];
                        $result_data[$i]['balance_quantity'] = $value['bal_issued_qty'];
                        $result_data[$i]['cost_price'] = $value['cost_price'];
                        $result_data[$i]['sell_price'] = $value['sell_price'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function archanai_item() {
        switch( CMD ) :
       
            case 'ARCHANAI_ITEM' :
                // List all services
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                
                $archanai_item_details = $this->archanai_item_details();
                
                $result_data = array();
                if (count($archanai_item_details)>0) {
                    $i=0;
                    foreach( $archanai_item_details as $key => $value) :
                       
                        $result_data[$i]['id'] = $value['id'];
                        $result_data[$i]['item_name'] = $value['name'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
               ///echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function archanai_sale() {
        switch( CMD ) :
       
            case 'ARCHANAI_SALE' :
                // List all Inventory Sale
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $item_id = (isset($psdata['item_id']) && $psdata['item_id']!='') ? $psdata['item_id'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $sale_details = $this->archanai_sale_details($startdate,$enddate,$item_id);
                
                $result_data = array();
                if (count($sale_details)>0) {
                    $i=0;
                    foreach( $sale_details as $key => $value) :
                       $old_issued_qty = $value['old_issued_qty'];
                       $balance_qty = $value['total_issued_qty'] - ($value['old_issued_qty'] + $value['issued_qty']);
                       $now_counter_total_qty = $value['total_issued_qty'] - $value['old_issued_qty'];
                                       
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['item_name'] = $value['item'];
                        $result_data[$i]['total_quantity'] = $now_counter_total_qty;
                        $result_data[$i]['issued_quantity'] = $value['issued_qty'];
                        $result_data[$i]['balance_quantity'] = $balance_qty;
                        $result_data[$i]['price'] = $value['price'];
                        $i++;
                    endforeach;
                   
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    function inv_gold_silver() {
        switch( CMD ) :
       
            case 'INVENTORYGOLDSILVER' :
                // List all Inventory Sale
                $psdata = (isset($this->post['data'])) ? (array)json_decode($this->post['data']) : array();
                $item_type = (isset($psdata['itemtype']) && $psdata['itemtype']!='') ? $psdata['itemtype'] : '';
                $startdate = (isset($psdata['startdate']) && $psdata['startdate']!='') ? $psdata['startdate'] : '';
                $enddate = (isset($psdata['enddate']) && $psdata['enddate']!='') ? $psdata['enddate'] : '';
               
                if(($startdate!="") && ($enddate=='')) {
                    throw new Exception('End Date was empty!');
                }
               
                if(($startdate=="") && ($enddate!='')) {
                    throw new Exception('Start Date was empty!');
                }
               
                $inv_gold_details = $this->inventory_gold_silver_details($startdate,$enddate,$item_type);
                
                $result_data = array();
                if (count($inv_gold_details)>0) {
                    $i=0;
                    foreach( $inv_gold_details as $key => $value) :
                        $result_data[$i]['date'] = date('d-m-Y',strtotime($value['created_on'])); 
                        $result_data[$i]['item_type'] = $value['item_str'];
                        $result_data[$i]['type'] = $value['type_name'];
                        $result_data[$i]['weight_in_grams'] = $value['weight'];
                        $result_data[$i]['desc'] = $value['description'];
                        $i++;
                    endforeach;
                }
              
                if (count($result_data) > 0) :
                    /**
                     * Success.
                     */
                    $results['header'] = array(
                        'status' => 'S',
                        'message' => ucfirst( CMD ) . ' is executed successfully',
                    );

                    // Set Results
                    $results['data'] = $result_data;
                else:
                    /**
                     * Error
                     */
                    $results['header'] = array(
                        'status' => 'N',
                        'message' => 'No records found.',
                    );
               
                    $results['data'] = '';
                endif;
                //echo '<pre>'; print_r($results); exit;
                return $results;

            break;            
       
            default : 
                throw new Exception('Given command is not found!');
            break;


        endswitch;
    }
    
    
    function get_data($start_date='',$end_date='',$userid=''){
        
        $query = 'SELECT l.ip_address, l.log_date_time, l.desc, l.service_menu_id, l.menu_type, u.name, CASE l.page_type_id WHEN 2 THEN "Edit" WHEN 4 THEN "Delete" WHEN 8 THEN "Sync" END as page_type FROM ' . TBL_LOGIN. ' as l';
        $query .= ' LEFT JOIN ' . TBL_USERS . ' as u ON u.id = l.user_id';
        if(!empty($start_date) && !empty($end_date)) {
        $query .= ' WHERE ( l.log_date_time >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND l.log_date_time <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
        $query .= ' WHERE ( l.log_date_time >= "'.date('Y-m-d 00:00:00').'" AND l.log_date_time <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        
        if(!empty($userid)) {
        $query .= ' AND l.user_id='.$userid;    
        }
        
        $query .= ' AND l.status=1';
        $query .= ' ORDER BY l.id DESC';
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }
    
    function get_service_name($menu_type,$service_id) {
        
        if($menu_type == 1) {
            $tbl_name = TBL_SETTINGS_MASTER;
            $service_name = 'master_name as service_name';
        } else if($menu_type == 3) {
            $tbl_name = TBL_SETTINGS_INVENTORY;
            $service_name = 'inventory_name as service_name';
        } elseif($menu_type == 4) {
            $tbl_name = TBL_SERVICE_MENU;
            $service_name = 'service_name as service_name';
        } elseif($menu_type == 5) {
            $tbl_name = TBL_SETTINGS_STATUE;
            $service_name = 'menu_name as service_name';
        }
        
        if($menu_type != 8) {
        $query = 'SELECT id,controller_name,'.$service_name.' FROM ' . $tbl_name;
        $query .= ' WHERE id='.$service_id;
        $query .= ' AND status=1';
        $refund_data = $this->get_array_result($query);
        return $refund_data;
        } else {
        return FALSE;    
        }
    }
    
    function payment_type_list() {
        $p_query = "SELECT id,name, slug as payment_mode FROM " . TBL_PAY_TYPE . " WHERE status = 1";
        $mode = $this->get_array_result($p_query);
        return $mode;
    }
    
    function get_refund_amount($date) {
        $return = '';
        
        $query = 'SELECT CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount FROM ' . TBL_REFUND;
        if(!empty($date)) {
        $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00').'" AND created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND status=1';
        $refund_data = $this->get_array_result($query);
        $return = $refund_data[0]['refund_amount'];
        
        return $return;
    }
    
    function get_refund_amt_by_ids($menu_id,$service_id,$auto_id,$date) {
        $return = '';
        if(!empty($menu_id) && !empty($service_id) && !empty($auto_id) && !empty($date)) {
        $query = 'SELECT CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount FROM ' . TBL_REFUND;
        $query .= ' WHERE menu_id='.$menu_id;
        $query .= ' AND service_id='.$service_id;
        $query .= ' AND auto_id='.$auto_id;
        $query .= ' AND ( created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        $query .= ' AND status=1';
        $refund_data = $this->get_array_result($query);
        $return = $refund_data[0]['refund_amount'];
        }
        return $return;
    }
    
    function get_total_paid_amt_by_type($id,$date=''){
        
        $query = 'SELECT SUM(pay_amount) as amount FROM ' . TBL_PAY;
        if(!empty($date)) {
        $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00').'" AND created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND pay_type_id='.$id;
        $query .= ' AND status=1';
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }
    
    function get_payment_details($type_id,$service_id,$date='',$mem_card_id){
        
        $query = 'SELECT *, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_PAY_TYPE . ' as pt ON pt.id = p.pay_type_id AND pt.status != 2';
        $query .= ' WHERE p.pay_ref_type='.$type_id;
        if(!empty($service_id)){
        $query .= ' AND p.pay_ref_id='.$service_id;
        }
        if(!empty($mem_card_id)){
        $query .= ' AND p.mem_card_id='.$mem_card_id;
        }
        
        $query .= ' AND p.status=1';
        
        if(!empty($date)) {
        $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }
    
    function get_abishegam_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, ab.ba_rec_no, ab.service_id, ab.ba_amount, ab.id, ab.gst, ab.gst_value, ser.menu_name, ab.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BA . ' as ab ON ab.id = p.pay_ref_id AND p.pay_ref_type = 1';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = ab.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND ab.ba_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY ab.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Abishegam';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Abishegam - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_festival_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, f.receipt_no, f.service_id, f.id, f.type_id, f.gst, f.gst_value, f.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_FESTIVAL_FUNCTION . ' as f ON f.id = p.pay_ref_id AND f.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = f.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND f.book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY f.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Festival and Function';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Festival and Function - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
               
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];                
            
            endif;
        }
        
        return $return;
    }
    
    function get_ubayam_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, u.receipt_no, u.service_id, u.id, u.type_id, u.gst, u.gst_value, u.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_UBAYAM . ' as u ON u.id = p.pay_ref_id AND u.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = u.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND u.book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY u.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Ubayam';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Ubayam - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_mega_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, m.receipt_no, m.service_id, m.id, m.type_id, m.gst, m.gst_value, m.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_MEGA_FUNCTION . ' as m ON m.id = p.pay_ref_id AND m.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = m.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND m.book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY m.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Mega Function';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Mega Function - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_other_service_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, os.receipt_no, os.service_id, os.amount, os.id, os.type_id, os.gst, os.gst_value, os.del_reason FROM ' . TBL_BILL_OTHERS . ' as os';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = os.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND os.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00').'" AND os.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' ORDER BY os.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Other Services';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Other Services - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_gold_silver_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, gs.receipt_no, gs.service_id, gs.amount, gs.id, gs.type_id, gs.gst, gs.gst_value, gs.del_reason FROM ' . TBL_BILL_GOLD_SILVER_OFFERING . ' as gs';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = gs.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND gs.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00').'" AND gs.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
       
        $query .= ' ORDER BY gs.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Gold Silver';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Gold Silver - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_temple_activity_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, t.receipt_no, t.service_id, t.amount, t.id, t.type_id, t.gst, t.gst_value, t.del_reason FROM ' . TBL_BILL_TEMPLE . ' as t';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = t.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND t.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00').'" AND t.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        
        $query .= ' ORDER BY t.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Temple Activities';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Temple Activities - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_member_subscribe_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, mem.receipt_no, mem.service_id, mem.amount, mem.id, mem.type_id, mem.gst, mem.gst_value, mem.del_reason FROM ' . TBL_BILL_MEMBER_SUBSCRIPE . ' as mem';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = mem.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( mem.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND mem.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( mem.created_on >= "'.date('Y-m-d 00:00:00').'" AND mem.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        
        $query .= ' ORDER BY mem.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Member Subscription';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Member Subscription - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_building_fund_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, bf.receipt_no, bf.service_id, bf.amount, bf.id, bf.type_id, bf.gst, bf.gst_value, bf.del_reason FROM ' . TBL_BILL_BUILDING_FUND . ' as bf';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = bf.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND bf.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00').'" AND bf.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        
        $query .= ' ORDER BY bf.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Building Fund';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Building Fund - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_outdoor_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, od.receipt_no, od.service_id, od.amount, od.id, od.type_id, od.gst, od.gst_value, od.del_reason FROM ' . TBL_BILL_OUTDOOR_SERVICES . ' as od';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = od.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( od.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND od.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( od.created_on >= "'.date('Y-m-d 00:00:00').'" AND od.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= 'AND od.book_status=1';
        $query .= ' ORDER BY od.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Outdoor Service';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Outdoor Service - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_prasadam_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, bp.bp_rec_no, bp.service_id, bp.bp_amount, bp.id, bp.gst, bp.gst_value, ser.menu_name, bp.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BP . ' as bp ON bp.id = p.pay_ref_id AND p.pay_ref_type = 15';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = bp.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND bp.bp_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY bp.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Prasadam';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Prasadam - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_madappalli_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, bm.bm_rec_no, bm.service_id, bm.bm_amount, bm.id, bm.gst, bm.gst_value, ser.menu_name, bm.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BM . ' as bm ON bm.id = p.pay_ref_id AND p.pay_ref_type = 9';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = bm.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND bm.bm_book_status = 1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY bm.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Madappalli Service';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Madappalli Service - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_donation_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, d.receipt_no, d.service_id, d.id, d.amount_type, d.gst, d.gst_value, d.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_DONATION . ' as d ON d.id = p.pay_ref_id AND d.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = d.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY d.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Donation';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 4 ;
                
                if($value['amount_type'] != 2) {
                    // Amount
                    $return[$indx]['service_id'] = $value['service_id'];
                    $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                } else {
                    $return[$indx]['service_id'] = $value['service_id'];
                    $return[$indx]['amount'] = 0;
                }
                
                $return[$indx]['amount_type'] = $value['amount_type'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Donation - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                if($value['amount_type'] != 2) {
                    // Amount
                    $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                }/*  else {
                    $return[$indx]['amount'] = 0;
                } */
                
                $return[$indx]['amount_type'] = $value['amount_type'];
                
                $return[$indx]['gst'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst'] : $value['gst'];
                
                  // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 4 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_hall_booking_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, hb.bh_rec_no, hb.service_id, SUM(p.pay_amount) as amount, hb.id, hb.gst, hb.gst_value, hb.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' JOIN ' . TBL_BH . ' as hb ON hb.id = p.pay_ref_id';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = hb.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND p.pay_ref_type=7';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY p.pay_ref_id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Hall Booking';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 7 ;
                
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
               
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Hall Booking - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                 
                $return[$indx]['gst'] = $value['gst'];
            
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 7 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_wed_hall_booking_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT p.*,ser.name as type_name, ser.prefix, whb.bwh_rec_no, whb.service_id, SUM(p.pay_amount) as amount, whb.id, whb.gst, whb.gst_value, whb.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' JOIN ' . TBL_BWH . ' as whb ON whb.id = p.pay_ref_id';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = whb.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND p.pay_ref_type=18';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY p.pay_ref_id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Wedding Hall Booking';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 18 ;
                
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
               
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Wedding Hall Booking - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];

                $return[$indx]['gst'] = $value['gst'];
            
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 18 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_archanai_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT a.id, a.bar_rec_no, ai.bari_qty, ai.bari_price, ai.bari_sub_total, ai.bari_rec_no, cat.category_name as type_name, cat.id as service_id, item.name as item_name, a.gst, a.gst_value, item.id as item_id, cat.grouping, ai.is_category FROM ' . TBL_BAR . ' as a';
        $query .= ' LEFT JOIN ' . TBL_BARI . ' as ai ON a.id = ai.bar_id';
        $query .= ' LEFT JOIN ' . TBL_CATEGORY . ' as cat ON cat.id = ai.bari_cat_id';
        $query .= ' LEFT JOIN ' . TBL_ITEM . ' as item ON item.id = ai.bari_item_id';
        if(!empty($date)) {
        $query .= ' WHERE ( a.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND a.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( a.created_on >= "'.date('Y-m-d 00:00:00').'" AND a.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND a.status=1';
        $query .= ' ORDER BY type_name ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                // Summary
                $indx = $value['service_id'].'_'.$value['bari_price'].$key;
                
                // Categery Name
                if($value['is_category'] == 0) : // No Category Condition
                    if( empty( $return[$indx]['cat_name'] ) ) :
                        $return[$indx]['cat_name'] = $value['type_name'];
                    endif;
                else :
                    $return[$indx]['cat_name'] = 'Archanai';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['item_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = $value['bari_rec_no'];
                
                // Price
                $return[$indx]['price'] = $value['bari_price'];
                
                // Qty 
                $return[$indx]['qty'] = $value['bari_qty'];
                
                // Amount
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['bari_sub_total'] : $value['bari_sub_total'];
                
                // Auto increment id 
                $return[$indx]['id'] =  $value['id'];
                
                // Type id 
                $return[$indx]['type_id'] = 2 ;
                
                $return[$indx]['gst'] = $value['gst'];
                
            else :
                if($value['grouping'] == 0) :
                    // Summary
                    $indx = $value['service_id'].'_'.$value['item_id'];
                 
                    if($value['is_category'] == 0) : // No Category Condition
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['type_name'] . ' - '. $value['item_name'];
                        endif;
                        
                    else :
                    
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['item_name'];
                        endif;
                    endif;
                
                else :
                    if($value['is_category'] == 0) : // No Category Condition
                        // Summary
                        $indx = $value['service_id'].'_'.$value['bari_price'];
                     
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['type_name'] . ' - '. $value['bari_price'];
                        endif;
                        
                    else :
                        // Summary
                        $indx = $value['service_id'].'_'.$value['item_id'];
                     
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['item_name'];
                        endif;
                        
                    endif;
                endif;
                
                // Rec No
                /* $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-(.*?)/', '', $return[$indx]['rec_no'] ) . '-'. $value['bar_rec_no'] ) : $value['bar_rec_no']; */
                
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. $value['bari_rec_no'] ) :  $value['bari_rec_no'] . '-' . $value['bari_rec_no'];
                
                // Price
                $return[$indx]['price'] =  $value['bari_price'];
                
                // Qty 
                $return[$indx]['qty'] = !empty( $return[$indx]['qty'] ) ? $return[$indx]['qty']+$value['bari_qty'] : $value['bari_qty'];
                
                // Amount
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['bari_sub_total'] : $value['bari_sub_total'];
                
               # $return[$indx]['gst'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst'] : $value['gst'];
               
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] =  $value['id'];
                
                // Type id 
                $return[$indx]['type_id'] = 2 ;
                
            endif;
        }
        
        return $return;
    }
    
    function get_kattalai_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, k.bk_rec_no, k.service_id, k.bk_amount, k.bk_paid_amount, k.id, k.gst, k.gst_value, ser.menu_name, k.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BK . ' as k ON k.id = p.pay_ref_id AND k.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = k.service_id';
        if(!empty($date)) {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND p.status=1 AND k.bk_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY k.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Kattalai Archanai';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Kattalai - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_membership_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT mr.* FROM ' . TBL_BILL_MEMBER_CARD . ' as mr';
        if(!empty($date)) {
        $query .= ' WHERE ( mr.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND mr.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( mr.created_on >= "'.date('Y-m-d 00:00:00').'" AND mr.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = '1'.$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Membership Card';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'User Registration';
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + (($value['amount'] > 0) ? '10.00' : '0.00') : (($value['amount'] > 0) ? '10.00' : '0.00');
                
                //$return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = 1;
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Membership Card - User Registration';
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['id'];
                $return[$indx]['static_id'] = 1;
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['static_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
               
               # $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($value['amount'] > 0) ? $return[$indx]['amount'] + $value['actual_amount'] : $return[$indx]['amount'];
                
                //$return[$indx]['gst'] = $value['gst'];
                
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_membership_card_topup_data($date='',$data_type=''){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ct.*, sum(ct.topup_amount) as amount, ct.id FROM ' . TBL_BILL_CARD_TOPUP . ' as ct';
        if(!empty($date)) {
        $query .= ' WHERE ( ct.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND ct.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
        $query .= ' WHERE ( ct.created_on >= "'.date('Y-m-d 00:00:00').'" AND ct.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .= ' AND ct.mem_first_data != 1';
        $query .= ' GROUP BY ct.mem_id';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = '1'.$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Membership Card';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Card Topup';
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                 // Auto increment id 
                $return[$indx]['mem_card_id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                // Amount
                
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? number_format((($return[$indx]['amount'] + $value['amount'])),2,'.','') : number_format(($value['amount']),2,'.','');
                
                //$return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = 'Delete this record';
                
            else :
                // Summary
                $indx = 1;
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Membership Card - Card Topup';
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['id'];
                $return[$indx]['static_id'] = 1;
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['static_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                /* $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? number_format((($return[$indx]['amount'] + $value['amount'])),2,'.','') : number_format(($value['amount']),2,'.',''); */
                
                $return[$indx]['amount'] = ($return[$indx]['static_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                //$return[$indx]['gst'] = $value['gst'];
                
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                 // Auto increment id 
                $return[$indx]['mem_card_id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = 'Delete this record';
                
            endif;
        }
        
        return $return;
    }
    
    
    public function get_temple_shop_data($date='',$data_type='') {
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, ts.receipt_no, ts.service_id, ts.amount, ts.id, ts.gst';
        $query .= ', ts.gst_value, ser.menu_name, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment, ts.del_reason FROM ' . TBL_PAY .' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_TEMPLESHOP_SALES . ' as ts ON ts.id = p.pay_ref_id AND p.pay_ref_type = 19';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = ts.service_id';
        $query .= ' LEFT JOIN ' . TBL_PAY_TYPE . ' as pt ON pt.id = p.pay_type_id';
        $query .= ' WHERE p.status = 1';
        
        if(!empty($date)) {
            $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($date)).'" )';
        } else {
            $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        $query .=' GROUP BY p.pay_ref_id ORDER BY ts.receipt_no DESC';
        
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Temple Shop Sale';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Temple Shop Sale - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                #$return[$indx]['gst'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst_value'] : $value['gst'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                #$return[$indx]['paid_type_name'] = !empty($return[$indx]['paid_type_name']) ? $return[$indx]['paid_type_name'] . ',' . $value['payment'] : $value['payment'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        return $return;
        
    }

    /*
     * Service Menu module
     */
    function services_menu(){
        
                
        switch( CMD ) :

            case 'UPDATE_IT' :
                // Modify the Row
                $args = array(
                    'status'    => 2
                );

                $this->db->update( TBL_SERVICE, $args );



            break;    
        
            default : return 'NOCMD'; break;

        endswitch;
                
            
    }
    
    /*
     * Weekly and Monthly Report Module
     */
    function get_general_settings_data() {
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        return $this->get_array_result($g_query);
    }
    
    function get_datewise_refund_amt_by_ids($menu_id,$service_id,$auto_id,$start_date,$end_date) {
        $return = '';
        if(!empty($menu_id) && !empty($service_id) && !empty($auto_id) && !empty($date)) {
        $query = 'SELECT CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount FROM ' . TBL_REFUND;
        $query .= ' WHERE menu_id='.$menu_id;
        $query .= ' AND service_id='.$service_id;
        $query .= ' AND auto_id='.$auto_id;
        $query .= ' AND ( created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        $query .= ' AND status=1';
        $refund_data = $this->get_array_result($query);
        $return = $refund_data[0]['refund_amount'];
        }
        return $return;
    }
    
    function get_datewise_payment_details($type_id,$service_id,$start_date='',$end_date='',$mem_card_id,$cmd){
        
        $query = 'SELECT *, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_PAY_TYPE . ' as pt ON pt.id = p.pay_type_id AND pt.status != 2';
        $query .= ' WHERE p.pay_ref_type='.$type_id;
        if(!empty($service_id)){
            $query .= ' AND p.pay_ref_id='.$service_id;
        }
        if(!empty($mem_card_id)){
            $query .= ' AND p.mem_card_id='.$mem_card_id;
        }
        
        $query .= ' AND p.status=1';
        
        if((!empty($start_date)) && (!empty($end_date))) {
            $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }                
        }
        
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }
    
    public function get_datewise_total_paid_amt_by_type($id,$start_date='',$end_date='',$cmd) {
        $query = 'SELECT SUM(pay_amount) as amount FROM ' . TBL_PAY;
        if((!empty($start_date)) && (!empty($end_date))) {
            $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }            
        }
        $query .= ' AND pay_type_id='.$id;
        $query .= ' AND status=1';
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }
    
    function get_datewise_refund_amount($start_date,$end_date,$cmd) {
        $return = '';
        
        $query = 'SELECT CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount FROM ' . TBL_REFUND;
        if((!empty($start_date)) && (!empty($end_date))) {
            $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == "WEEKLY") {
                $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND created_on <= "'.date('Y-m-d 23:59:59').'" )';    
            } elseif($cmd == "MONTHLY") {
                $query .= ' WHERE ( created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }
        }
        $query .= ' AND status=1';
        $refund_data = $this->get_array_result($query);
        $return = $refund_data[0]['refund_amount'];
        
        return $return;
    }
    
    function get_abishegam_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, ab.ba_rec_no, ab.service_id, ab.ba_amount, ab.id, ab.gst, ab.gst_value, ser.menu_name, ab.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BA . ' as ab ON ab.id = p.pay_ref_id AND p.pay_ref_type = 1';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = ab.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';  
            } elseif($cmd == 'MONTHLY'){
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }          
        }
        $query .= ' AND p.status=1 AND ab.ba_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY ab.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Abishegam';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Abishegam - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_festival_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, f.receipt_no, f.service_id, f.id, f.type_id, f.gst, f.gst_value, f.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_FESTIVAL_FUNCTION . ' as f ON f.id = p.pay_ref_id AND f.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = f.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }           
        }
        $query .= ' AND p.status=1 AND f.book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY f.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Festival and Function';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Festival and Function - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
               
                $return[$indx]['gst'] = $value['gst'];
               
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];                
            
            endif;
        }
        
        return $return;
    }
    
    function get_ubayam_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, u.receipt_no, u.service_id, u.id, u.type_id, u.gst, u.gst_value, u.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_UBAYAM . ' as u ON u.id = p.pay_ref_id AND u.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = u.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }
        }
        $query .= ' AND p.status=1 AND u.book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY u.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Ubayam';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Ubayam - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    
    function get_mega_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, m.receipt_no, m.service_id, m.id, m.type_id, m.gst, m.gst_value, m.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_MEGA_FUNCTION . ' as m ON m.id = p.pay_ref_id AND m.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = m.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }           
        }
        $query .= ' AND p.status=1 AND m.book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY m.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Mega Function';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Mega Function - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_other_service_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, os.receipt_no, os.service_id, os.amount, os.id, os.type_id, os.gst, os.gst_value, os.del_reason FROM ' . TBL_BILL_OTHERS . ' as os';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = os.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND os.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND os.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND os.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }           
        }
        $query .= ' ORDER BY os.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Other Services';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Other Services - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_gold_silver_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, gs.receipt_no, gs.service_id, gs.amount, gs.id, gs.type_id, gs.gst, gs.gst_value, gs.del_reason FROM ' . TBL_BILL_GOLD_SILVER_OFFERING . ' as gs';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = gs.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND gs.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND gs.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND gs.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }            
        }
        $query .= ' ORDER BY gs.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Gold Silver';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Gold Silver - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    
    function get_temple_activity_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, t.receipt_no, t.service_id, t.amount, t.id, t.type_id, t.gst, t.gst_value, t.del_reason FROM ' . TBL_BILL_TEMPLE . ' as t';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = t.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND t.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND t.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND t.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }          
        }
        $query .= ' ORDER BY t.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Temple Activities';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Temple Activities - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_member_subscribe_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, mem.receipt_no, mem.service_id, mem.amount, mem.id, mem.type_id, mem.gst, mem.gst_value, mem.del_reason FROM ' . TBL_BILL_MEMBER_SUBSCRIPE . ' as mem';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = mem.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( mem.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND mem.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( mem.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND mem.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( mem.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND mem.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }                
        }
        $query .= ' ORDER BY mem.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Member Subscription';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Member Subscription - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_building_fund_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, bf.receipt_no, bf.service_id, bf.amount, bf.id, bf.type_id, bf.gst, bf.gst_value, bf.del_reason FROM ' . TBL_BILL_BUILDING_FUND . ' as bf';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = bf.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND bf.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND bf.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND bf.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }                
        }
        $query .= ' ORDER BY bf.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Building Fund';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Building Fund - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    
    function get_outdoor_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, od.receipt_no, od.service_id, od.amount, od.id, od.type_id, od.gst, od.gst_value, od.del_reason FROM ' . TBL_BILL_OUTDOOR_SERVICES . ' as od';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = od.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( od.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND od.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( od.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND od.created_on <= "'.date('Y-m-d 23:59:59').'" )';  
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( od.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND od.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }           
        }
        $query .= ' AND od.book_status=1';
        $query .= ' ORDER BY od.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Outdoor Service';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Outdoor Service - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
            
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['type_id'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_prasadam_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, bp.bp_rec_no, bp.service_id, bp.bp_amount, bp.id, bp.gst, bp.gst_value, ser.menu_name, bp.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BP . ' as bp ON bp.id = p.pay_ref_id AND p.pay_ref_type = 15';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = bp.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';  
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }                 
        }
        $query .= ' AND p.status=1 AND bp.bp_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY bp.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Prasadam';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Prasadam - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_madappalli_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, bm.bm_rec_no, bm.service_id, bm.bm_amount, bm.id, bm.gst, bm.gst_value, ser.menu_name, bm.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BM . ' as bm ON bm.id = p.pay_ref_id AND p.pay_ref_type = 9';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = bm.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }            
        }
        $query .= ' AND p.status=1 AND bm.bm_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY bm.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Madappalli Service';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Madappalli Service - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_donation_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, d.receipt_no, d.service_id, d.id, d.amount_type, d.gst, d.gst_value, d.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_DONATION . ' as d ON d.id = p.pay_ref_id AND d.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = d.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }           
        }
        $query .= ' AND p.status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY d.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Donation';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 4 ;
                
                if($value['amount_type'] != 2) {
                    // Amount
                    $return[$indx]['service_id'] = $value['service_id'];
                    $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                } else {
                    $return[$indx]['service_id'] = $value['service_id'];
                    $return[$indx]['amount'] = 0;
                }
                
                $return[$indx]['amount_type'] = $value['amount_type'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Donation - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                if($value['amount_type'] != 2) {
                    // Amount
                    $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                }/*  else {
                    $return[$indx]['amount'] = 0;
                } */
                
                $return[$indx]['amount_type'] = $value['amount_type'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                  // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 4 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
            
            endif;
        }
        
        return $return;
    }
    
    function get_hall_booking_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ser.name as type_name, ser.prefix, hb.bh_rec_no, hb.service_id, SUM(p.pay_amount) as amount, hb.id, hb.gst, hb.gst_value, hb.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' JOIN ' . TBL_BH . ' as hb ON hb.id = p.pay_ref_id';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = hb.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        $query .= ' AND p.status=1 AND p.pay_ref_type=7';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY p.pay_ref_id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Hall Booking';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 7 ;
                
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
               
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Hall Booking - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                 
                $return[$indx]['gst'] = $value['gst'];
            
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 7 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_wed_hall_booking_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT p.*,ser.name as type_name, ser.prefix, whb.bwh_rec_no, whb.service_id, SUM(p.pay_amount) as amount, whb.id, whb.gst, whb.gst_value, whb.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' JOIN ' . TBL_BWH . ' as whb ON whb.id = p.pay_ref_id';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = whb.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }                
        }
        $query .= ' AND p.status=1 AND p.pay_ref_type=18';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY p.pay_ref_id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Wedding Hall Booking';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 18 ;
                
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
               
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Wedding Hall Booking - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];

                $return[$indx]['gst'] = $value['gst'];
            
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 18 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_archanai_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT a.id, a.bar_rec_no, ai.bari_qty, ai.bari_price, ai.bari_sub_total, ai.bari_rec_no, cat.category_name as type_name, cat.id as service_id, item.name as item_name, a.gst, a.gst_value, item.id as item_id, cat.grouping, ai.is_category FROM ' . TBL_BAR . ' as a';
        $query .= ' LEFT JOIN ' . TBL_BARI . ' as ai ON a.id = ai.bar_id';
        $query .= ' LEFT JOIN ' . TBL_CATEGORY . ' as cat ON cat.id = ai.bari_cat_id';
        $query .= ' LEFT JOIN ' . TBL_ITEM . ' as item ON item.id = ai.bari_item_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( a.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND a.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( a.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND a.created_on <= "'.date('Y-m-d 23:59:59').'" )';  
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( a.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND a.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }                
        }
        $query .= ' AND a.status=1';
        $query .= ' ORDER BY type_name ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                // Summary
                $indx = $value['service_id'].'_'.$value['bari_price'].$key;
                
                // Categery Name
                if($value['is_category'] == 0) : // No Category Condition
                    if( empty( $return[$indx]['cat_name'] ) ) :
                        $return[$indx]['cat_name'] = $value['type_name'];
                    endif;
                else :
                    $return[$indx]['cat_name'] = 'Archanai';
                endif;
                
                 // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['item_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = $value['bari_rec_no'];
                
                // Price
                $return[$indx]['price'] = $value['bari_price'];
                
                // Qty 
                $return[$indx]['qty'] = $value['bari_qty'];
                
                // Amount
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['bari_sub_total'] : $value['bari_sub_total'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 2 ;
                
                // Service id 
                $return[$indx]['service_id'] = '' ;
                
                $return[$indx]['gst'] = $value['gst'];
                
            else :
                if($value['grouping'] == 0) :
                    // Summary
                    $indx = $value['service_id'].'_'.$value['item_id'];
                 
                    if($value['is_category'] == 0) : // No Category Condition
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['type_name'] . ' - '. $value['item_name'];
                        endif;
                        
                    else :
                    
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['item_name'];
                        endif;
                    endif;
                
                else :
                    if($value['is_category'] == 0) : // No Category Condition
                        // Summary
                        $indx = $value['service_id'].'_'.$value['bari_price'];
                     
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['type_name'] . ' - '. $value['bari_price'];
                        endif;
                        
                    else :
                        // Summary
                        $indx = $value['service_id'].'_'.$value['item_id'];
                     
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                            $return[$indx]['name'] = $value['item_name'];
                        endif;
                        
                    endif;
                endif;
                
                // Rec No
                /* $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-(.*?)/', '', $return[$indx]['rec_no'] ) . '-'. $value['bar_rec_no'] ) : $value['bar_rec_no']; */
                
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. $value['bari_rec_no'] ) :  $value['bari_rec_no'] . '-' . $value['bari_rec_no'];
                
                // Price
                $return[$indx]['price'] = $value['bari_price'];
                
                // Qty 
                $return[$indx]['qty'] = !empty( $return[$indx]['qty'] ) ? $return[$indx]['qty']+$value['bari_qty'] : $value['bari_qty'];
                
                // Amount
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['bari_sub_total'] : $value['bari_sub_total'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 2 ;
                
                // Service id 
                $return[$indx]['service_id'] = '' ;
                
            endif;
        }
        
        return $return;
    }
    
    function get_kattalai_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, k.bk_rec_no, k.service_id, k.bk_amount, k.bk_paid_amount, k.id, k.gst, k.gst_value, ser.menu_name, k.del_reason FROM ' . TBL_PAY . ' as p';
        $query .= ' LEFT JOIN ' . TBL_BK . ' as k ON k.id = p.pay_ref_id AND k.type_id = p.pay_ref_type';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = k.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )'; 
            }           
        }
        $query .= ' AND p.status=1 AND k.bk_book_status=1';
        $query .= ' GROUP BY p.pay_ref_id ORDER BY k.id ASC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Kattalai Archanai';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Kattalai - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_membership_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT mr.* FROM ' . TBL_BILL_MEMBER_CARD . ' as mr';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( mr.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND mr.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( mr.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND mr.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( mr.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND mr.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }                
        }
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = '1'.$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Membership Card';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'User Registration';
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                // Amounts
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + (($value['amount'] > 0) ? '10.00' : '0.00') : (($value['amount'] > 0) ? '10.00' : '0.00');
                
                //$return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = 1;
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Membership Card - User Registration';
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['id'];
                $return[$indx]['static_id'] = 1;
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['static_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
               
               # $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($value['amount'] > 0) ? ((!empty($return[$indx]['amount'])) ? $return[$indx]['amount'] + $value['actual_amount'] : $value['actual_amount']) : ((!empty($return[$indx]['amount'])) ? $return[$indx]['amount'] : 0);
                
                //$return[$indx]['gst'] = $value['gst'];
                
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        
        return $return;
    }
    
    function get_membership_card_topup_datewise_data($start_date='',$end_date='',$data_type,$cmd){
        
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT ct.*, sum(ct.topup_amount) as amount, ct.id FROM ' . TBL_BILL_CARD_TOPUP . ' as ct';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( ct.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ct.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' WHERE ( ct.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND ct.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' WHERE ( ct.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND ct.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }           
        }
        $query .= ' AND ct.mem_first_data != 1';
        $query .= ' GROUP BY ct.mem_id';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = '1'.$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Membership Card';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Card Topup';
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                 // Auto increment id 
                $return[$indx]['mem_card_id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                // Amount
                
                $return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? number_format((($return[$indx]['amount'] + $value['amount'])),2,'.','') : number_format(($value['amount']),2,'.','');
                
                //$return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = 'Delete this record';
                
            else :
                // Summary
                $indx = 1;
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Membership Card - Card Topup';
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general[0]['prefix'] == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['id'];
                $return[$indx]['static_id'] = 1;
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['static_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                /* $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? number_format((($return[$indx]['amount'] + $value['amount'])),2,'.','') : number_format(($value['amount']),2,'.',''); */
                
                $return[$indx]['amount'] = ($return[$indx]['static_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                //$return[$indx]['gst'] = $value['gst'];
                
                 // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                 // Auto increment id 
                $return[$indx]['mem_card_id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = 'Delete this record';
                
            endif;
        }
        
        return $return;
    }
    
    public function get_temple_shop_datewise_data($start_date='',$end_date='',$data_type='',$cmd) {
        $g_query = "SELECT * FROM " . TBL_SETTINGS_GENERAL . " ORDER BY id DESC";
        $general = $this->get_array_result($g_query);
        
        $query = 'SELECT SUM(p.pay_amount) as amount, ser.name as type_name, ser.prefix, ts.receipt_no, ts.service_id, ts.amount, ts.id, ts.gst';
        $query .= ', ts.gst_value, ser.menu_name, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment, ts.del_reason FROM ' . TBL_PAY .' as p';
        $query .= ' LEFT JOIN ' . TBL_BILL_TEMPLESHOP_SALES . ' as ts ON ts.id = p.pay_ref_id AND p.pay_ref_type = 19';
        $query .= ' JOIN ' . TBL_SERVICE . ' as ser ON ser.id = ts.service_id';
        $query .= ' LEFT JOIN ' . TBL_PAY_TYPE . ' as pt ON pt.id = p.pay_type_id';
        $query .= ' WHERE p.status = 1';
        
        if((!empty($start_date)) && (!empty($end_date))) {
            $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            if($cmd == 'WEEKLY') {
                $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-6 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            } elseif($cmd == 'MONTHLY') {
                $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime('-27 days')).'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        $query .= ' GROUP BY p.pay_ref_id ORDER BY ts.receipt_no DESC';
        $results = $this->get_array_result($query);
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
        foreach($results as $key=>$value) {
            
            if( $is_detail ) :
                // Submitted for "Details"
                
                $indx = $value['service_id'].$key;
             
                // Categery Name
                if( empty( $return[$indx]['cat_name'] ) ) :
                    $return[$indx]['cat_name'] = 'Temple Shop Sale';
                endif;
                
                // Sub Categery Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = $value['type_name'];
                endif;
                
               // Rec No
                $return[$indx]['rec_no'] = (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'] ;
                
                // Amount
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            else :
                // Summary
                $indx = $value['service_id'];
             
                // Name
                if( empty( $return[$indx]['name'] ) ) :
                    $return[$indx]['name'] = 'Temple Shop Sale - ' . $value['type_name'];
                endif;
                
                // Rec No
                $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'] . '-' . (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Price
                $return[$indx]['price'] = '-';
                
                // Qty
                $return[$indx]['service_id'] = $value['service_id'];
                $return[$indx]['qty'] = ((!empty($return[$indx]['qty'])) && $return[$indx]['service_id'] == $indx ) ? (++$return[$indx]['qty']) : 1;
                
                // Amount
                $return[$indx]['amount'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['amount'] : $value['amount'];
                
                #$return[$indx]['gst'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst_value'] : $value['gst'];
                
                $return[$indx]['gst'] = $value['gst'];
                
                // Type id 
                $return[$indx]['type_id'] = $value['menu_name'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['id'] ;
                
                #$return[$indx]['paid_type_name'] = !empty($return[$indx]['paid_type_name']) ? $return[$indx]['paid_type_name'] . ',' . $value['payment'] : $value['payment'];
                
                //Delete Reason
                $return[$indx]['del_reason'] = $value['del_reason'];
                
            endif;
        }
        return $return;
        
    }
    
    /*
     * Weekly and Monthly Report End
     */
    
    /*
     * List Services
     */
    
    function get_service_lists($menu_id){
        $query = 'SELECT id, name FROM ' . TBL_SERVICE . ' WHERE menu_name = ' . $menu_id . ' AND status = 1';
        return $this->get_array_result($query);
    }
    
    /*
     * Abishegam Report Module
     */
     
    function abishegam_details($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, star.name as star, star2.name as star2';
        $query .= ', d.name as deity, CASE a.ba_period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, a.* FROM ' . TBL_BA . ' as a';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as s ON s.id = a.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = a.star_id_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = a.star_id_2';
        $query .= ' LEFT JOIN ' . TBL_GOD_NAME . ' as d ON d.id = a.god_id';
        $query .= ' WHERE a.ba_book_status = 1';
         
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( a.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND a.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( a.created_on >= "'.date('Y-m-d 00:00:00').'" AND a.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( a.ba_fn_from_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND a.ba_fn_from_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( a.ba_fn_from_date >= "'.date('Y-m-d 00:00:00').'" AND a.ba_fn_from_date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        if(!empty($type)){
            $query .= ' AND a.service_id='.$type;
        }
        
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }
    
    
    /*
     * Building Report Module
     */
   
    function building_details($start_date='',$end_date='',$type=''){
       $query = 'SELECT s.name as service_name, s.prefix, bf.* FROM ' . TBL_BILL_BUILDING_FUND . ' as bf';
       $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = bf.service_id';
       
       if(!empty($start_date) && !empty($end_date)) {
           $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND bf.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
       } else {
           $query .= ' WHERE ( bf.created_on >= "'.date('Y-m-d 00:00:00').'" AND bf.created_on <= "'.date('Y-m-d 23:59:59').'" )';
       }
     
      if(!empty($type)){
          $query .= ' AND bf.service_id='.$type;
      }
      $resultdt = $this->get_array_result($query);
      
      return $resultdt;
    } 
    
    /*
     * Festival and Function Report Module
     */
    function festival_details($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, d.name as deity, star.name as star';
        $query .= ', star2.name as star2, CASE f.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, f.* FROM ' . TBL_BILL_FESTIVAL_FUNCTION . ' as f';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = f.service_id';
        $query .= ' JOIN ' . TBL_GOD_NAME . ' as d ON d.id = f.deity_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = f.star1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = f.star2';
        $query .= ' WHERE f.book_status = 1';
       
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( f.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND f.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( f.created_on >= "'.date('Y-m-d 00:00:00').'" AND f.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( f.date >= "'.date('Y-m-d',strtotime($start_date)).'" AND f.date <= "'.date('Y-m-d',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( f.date >= "'.date('Y-m-d').'" AND f.date <= "'.date('Y-m-d').'" )';
            }
        }
        if(!empty($type)){
            $query .= ' AND f.service_id='.$type;
        }

        $resultdt = $this->get_array_result($query);
      
       return $resultdt;
    } 
    
    /*
     * Mega Report Module
     */
   
    function mega_details($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, d.name as deity';
        $query .= ', CASE mf.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, st.name as star, st2.name as star2, mf.* FROM ' . TBL_BILL_MEGA_FUNCTION . ' as mf';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = mf.service_id';
        $query .= ' JOIN ' . TBL_GOD_NAME . ' as d ON d.id = mf.deity_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as st ON st.id = mf.star1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as st2 ON st2.id = mf.star2';
        $query .= ' WHERE mf.book_status = 1';
       
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( mf.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND mf.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( mf.created_on >= "'.date('Y-m-d 00:00:00').'" AND mf.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( mf.date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND mf.date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( mf.date >= "'.date('Y-m-d 00:00:00').'" AND mf.date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        if(!empty($type)){
            $query .= ' AND mf.service_id='.$type;
        }
      
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    }
    
    
    /*
     *  Ubayam Report Module
     */
   
    function ubayam_details($start_date='',$end_date='',$type='',$date_type){
        $query  = 'SELECT s.name as service_name, s.prefix, star.name as star, star2.name as star2';
        $query .= ', d.name as deity, CASE u.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, u.* FROM ' . TBL_BILL_UBAYAM . ' as u';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as s ON s.id = u.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = u.star1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = u.star2';
        $query .= ' LEFT JOIN ' . TBL_GOD_NAME . ' as d ON d.id = u.deity_id';
        $query .= ' WHERE u.book_status = 1';
        
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( u.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND u.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( u.created_on >= "'.date('Y-m-d 00:00:00').'" AND u.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( u.date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND u.date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
               $query .= ' AND ( u.date >= "'.date('Y-m-d 00:00:00').'" AND u.date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        if(!empty($type)){
            $query .= ' AND u.service_id='.$type;
        }
       
        $resultdt = $this->get_array_result($query);
       
        return $resultdt;
    }
    
    /*
     *  Prasadam Report Module
     */
   
    function prasadam_details($start_date='',$end_date='',$period='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, star.name as star, star2.name as star2';
        $query .= ', CASE p.bp_period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, p.* FROM ' . TBL_BP . ' as p';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as s ON s.id = p.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = p.star_id_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = p.star_id_2';
        $query .= ' WHERE p.bp_book_status = 1';
        
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( p.bp_fn_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.bp_fn_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
               $query .= ' AND ( p.bp_fn_date >= "'.date('Y-m-d 00:00:00').'" AND p.bp_fn_date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        if(!empty($period)){
            $query .= ' AND p.bp_period='.$period;
        }
        
        $resultdt = $this->get_array_result($query);
       
        return $resultdt;
    }
    
    /*
     * Other Services Report Module
     */
   
    function other_details($start_date='',$end_date='',$type=''){
        $query = 'SELECT s.name as service_name, s.prefix, star.name as star, os.* FROM ' . TBL_BILL_OTHERS . ' as os';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = os.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = os.star_id';
       
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND os.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00').'" AND os.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
     
        if(!empty($type)){
            $query .= ' AND os.service_id='.$type;
        }
      
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    } 
    
    /*
     * Gold Silver Offering Report Module
     */
   
    function offering_details($start_date='',$end_date='',$type=''){
        $query = 'SELECT s.name as service_name, s.prefix, gs.* FROM ' . TBL_BILL_GOLD_SILVER_OFFERING . ' as gs';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = gs.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND gs.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' WHERE ( gs.created_on >= "'.date('Y-m-d 00:00:00').'" AND gs.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
     
        if(!empty($type)){
            $query .= ' AND gs.service_id='.$type;
        }
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    } 
    
    /*
     *  Prasadam Sale Report Module
     */
   
    function prasadam_sale_details($start_date='',$end_date=''){
        $query = 'SELECT p.bp_fn_date, SUM(pi.bpi_qty) as qty, SUM(pi.bpi_sub_total) as total, pl.name as prasadam';
        $query .= ', pi.* FROM ' . TBL_BPI . ' as pi';
        $query .= ' LEFT JOIN ' . TBL_BP . ' as p ON p.id = pi.bp_id';
        $query .= ' LEFT JOIN ' . TBL_PRASADAM . ' as pl ON pl.id = pi.prasadam_id';
        $query .= ' WHERE p.status = 1 AND pi.status = 1';
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( p.bp_fn_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.bp_fn_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( p.bp_fn_date >= "'.date('Y-m-d 00:00:00').'" AND p.bp_fn_date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        $query .= ' GROUP BY pi.prasadam_id';
        $resultdt = $this->get_array_result($query);
       
        return $resultdt;
    }
    
    /*
     *  Madapalli Report Module
     */
   
    function madapalli_details($start_date='',$end_date='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, d.name as deity, mada.prasadam_name as prasadam, mi.bmi_qty, mi.bmi_price, mi.bmi_sub_total';
        $query .= ',m.*, CASE m.bm_period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str FROM ' . TBL_BM . ' as m';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as s ON s.id = m.service_id';
        $query .= ' LEFT JOIN ' . TBL_BMI . ' as mi ON mi.bm_id = m.id AND mi.status = 1';
        $query .= ' LEFT JOIN ' . TBL_GOD_NAME . ' as d ON d.id = mi.god_id';
        $query .= ' LEFT JOIN ' . TBL_MADAPPALLI . ' as mada ON mada.id = mi.prasadam_id';
        $query .= ' WHERE m.bm_book_status = 1';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( m.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND m.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            }  else {
                $query .= ' AND ( m.created_on >= "'.date('Y-m-d 00:00:00').'" AND m.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( m.bm_fn_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND m.bm_fn_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( m.bm_fn_date >= "'.date('Y-m-d 00:00:00').'" AND m.bm_fn_date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }

        $resultdt = $this->get_array_result($query);
       
        return $resultdt;
    }
    
    /*
     * Kattalai Archanai Report Module
     */
     
    function get_daily_data($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2, k.bk_tamil_name_1, k.bk_tamil_name_2, star2.name as star2';
        $query .= ', ki.bki_from_date, ki.bki_to_date, ki.bki_amount, ki.bki_day_type, ki.bki_dates, ki.bki_god_ids, ki.bki_no_days';
        $query .= ', ki.bki_week_day, "Daily" as day_type, ki.created_on, k.bk_desc FROM ' . TBL_BK . ' as k';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = k.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = k.bk_star_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = k.bk_star_2';
        $query .= ' JOIN ' . TBL_BKI . ' as ki ON ki.bk_id = k.id AND ki.status = 1 AND ki.bki_day_type = 1';
        $query .= ' WHERE k.bk_book_status = 1';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_weekly_data($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2, k.bk_tamil_name_1, k.bk_tamil_name_2,star2.name as star2';
        $query .= ', ki.bki_from_date, ki.bki_to_date, ki.bki_amount, ki.bki_day_type, ki.bki_dates, ki.bki_god_ids, ki.bki_no_days';
        $query .= ', ki.bki_week_day, "Weekly" as day_type, ki.created_on, k.bk_desc FROM ' . TBL_BK . ' as k';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = k.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = k.bk_star_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = k.bk_star_2';
        $query .= ' JOIN ' . TBL_BKI . ' as ki ON ki.bk_id = k.id AND ki.status = 1 AND ki.bki_day_type = 2';
        $query .= ' WHERE k.bk_book_status = 1';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
   function get_multiple_data($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, k.bk_rec_no, k.bk_name_1, star.name as star, k.bk_name_2,  k.bk_tamil_name_1, k.bk_tamil_name_2, star2.name as star2';
        $query .= ', ki.bki_from_date, ki.bki_to_date, ki.bki_amount, ki.bki_day_type, ki.bki_dates, ki.bki_god_ids, ki.bki_no_days';
        $query .= ', ki.bki_week_day, "Weekly" as day_type, ki.created_on, k.bk_desc FROM ' . TBL_BK . ' as k';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = k.service_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = k.bk_star_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star2 ON star2.id = k.bk_star_2';
        $query .= ' JOIN ' . TBL_BKI . ' as ki ON ki.bk_id = k.id AND ki.status = 1 AND ki.bki_day_type = 3';
        $query .= ' WHERE k.bk_book_status = 1';
        
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_deity_name($ids) {
        $query = 'SELECT GROUP_CONCAT(name SEPARATOR ",") as deity FROM ' . TBL_GOD_NAME . ' WHERE id IN (' . $ids . ')';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_details_gods_by_kattalai_items_daily($start_date='',$end_date='',$type='',$date_type) {
        $query  = 'SELECT d.name , d.tamil_name, d.id, ki.bki_day_type, ki.bki_dates, ki.bki_week_day, ki.created_on FROM ' . TBL_BKI . ' as ki';
        $query .= ' JOIN ' . TBL_GOD_NAME . ' as d ON FIND_IN_SET(d.id , ki.bki_god_ids)';
        $query .= ' LEFT JOIN ' . TBL_BK . ' as k ON k.id = ki.bk_id';
        $query .= ' WHERE k.bk_book_status = 1 AND ki.status = 1 AND ki.bki_day_type = 1';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $query .= ' GROUP BY d.id';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_details_gods_by_kattalai_items_weekly($start_date='',$end_date='',$type='',$date_type) {
        $query  = 'SELECT d.name , d.tamil_name, d.id, ki.bki_day_type, ki.bki_dates, ki.bki_week_day, ki.created_on FROM ' . TBL_BKI . ' as ki';
        $query .= ' JOIN ' . TBL_GOD_NAME . ' as d ON FIND_IN_SET(d.id , ki.bki_god_ids)';
        $query .= ' LEFT JOIN ' . TBL_BK . ' as k ON k.id = ki.bk_id';
        $query .= ' WHERE k.bk_book_status = 1 AND ki.status = 1 AND ki.bki_day_type = 2';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $query .= ' GROUP BY d.id';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_details_gods_by_kattalai_items_multiple($start_date='',$end_date='',$type='',$date_type) {
        $query  = 'SELECT d.name , d.tamil_name, d.id, ki.bki_day_type, ki.bki_dates, ki.bki_week_day, ki.created_on FROM ' . TBL_BKI . ' as ki';
        $query .= ' JOIN ' . TBL_GOD_NAME . ' as d ON FIND_IN_SET(d.id , ki.bki_god_ids)';
        $query .= ' LEFT JOIN ' . TBL_BK . ' as k ON k.id = ki.bk_id';
        $query .= ' WHERE k.bk_book_status = 1 AND ki.status = 1 AND ki.bki_day_type = 3';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $query .= ' GROUP BY d.id';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_details_data_by_god_daily($start_date='',$end_date='',$type='',$date_type) {
        $query  = 'SELECT k.*, ki.*, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2';
        $query .= ', ser.prefix, ki.created_on FROM ' . TBL_BKI . ' as ki';
        $query .= ' JOIN ' . TBL_BK . ' as k ON k.id = ki.bk_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as s ON s.id = k.bk_star_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as s2 ON s2.id = k.bk_star_2';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as ser ON ser.id = k.service_id';
        $query .= ' WHERE k.bk_book_status = 1 AND ki.status = 1 AND ki.bki_day_type = 1';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_details_data_by_god_weekly($start_date='',$end_date='',$type='',$date_type) {
        $query  = 'SELECT k.*, ki.*, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2';
        $query .= ', ser.prefix, ki.created_on FROM ' . TBL_BKI . ' as ki';
        $query .= ' JOIN ' . TBL_BK . ' as k ON k.id = ki.bk_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as s ON s.id = k.bk_star_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as s2 ON s2.id = k.bk_star_2';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as ser ON ser.id = k.service_id';
        $query .= ' WHERE k.bk_book_status = 1 AND ki.status = 1 AND ki.bki_day_type = 2';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_details_data_by_god_multiple($start_date='',$end_date='',$type='',$date_type) {
        $query  = 'SELECT k.*, ki.*, s.name as star, s.tamil_name as star_tamil,s2.name as star2, s2.tamil_name as star_tamil_2';
        $query .= ', ser.prefix, ki.created_on FROM ' . TBL_BKI . ' as ki';
        $query .= ' JOIN ' . TBL_BK . ' as k ON k.id = ki.bk_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as s ON s.id = k.bk_star_1';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as s2 ON s2.id = k.bk_star_2';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as ser ON ser.id = k.service_id';
        $query .= ' WHERE k.bk_book_status = 1 AND ki.status = 1 AND ki.bki_day_type = 3';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ki.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' AND ( ki.created_on >= "'.date('Y-m-d 00:00:00').'" AND ki.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_from_date <= "' . date('Y-m-d',strtotime($end_date)) . '")) and ((ki.bki_to_date >= "' . date('Y-m-d',strtotime($start_date)) . '") or (ki.bki_to_date <= "' . date('Y-m-d',strtotime($end_date)) . '"))';
            } else {
                $query .= ' AND ((ki.bki_from_date >= "' . date('Y-m-d') . '") or (ki.bki_from_date <= "' . date('Y-m-d') . '")) and ((ki.bki_to_date >= "' . date('Y-m-d') . '") or (ki.bki_to_date <= "' . date('Y-m-d') . '"))';
            }
        }
        if(!empty($type)) {
            $query .= ' AND k.service_id = ' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    /*
     * Hall Booking Report Module
     */
   
    function hall_booking_details($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix';
        $query .= ', CASE hp.bh_period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, hp.* FROM ' . TBL_BH . ' as hp';
        $query .= ' JOIN ' . TBL_BH . ' as h ON h.self_id = hp.id';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = hp.service_id';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' WHERE ( hp.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND hp.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
               $query .= ' WHERE ( hp.created_on >= "'.date('Y-m-d 00:00:00').'" AND hp.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' WHERE ( hp.bh_from_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND hp.bh_from_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
               $query .= ' WHERE ( hp.bh_from_date >= "'.date('Y-m-d 00:00:00').'" AND hp.bh_from_date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        if(!empty($type)){
            $query .= ' AND hp.service_id ='.$type;
        }
        $query .= ' GROUP BY h.self_id';
        $resultdt = $this->get_array_result($query);
       
        return $resultdt;
    }
    
    function get_part_pay_rec_no($id) {
        $query = 'SELECT *, GROUP_CONCAT(DISTINCT bh_rec_no SEPARATOR ",") as receipt_no FROM ' . TBL_BH;
        $query .= ' WHERE self_id = ' .$id;
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    /*
     * Wedding Hall Booking Report Module
     */
    
    function wedding_hall_booking_details($start_date='',$end_date='',$type='',$period='',$date_type){
        $query  = 'SELECT s.name as service_name, s.prefix, CASE whp.bwh_period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, whp.*';
        $query .= ' FROM ' . TBL_BWH . ' as whp JOIN ' . TBL_SERVICE . ' as s ON s.id = whp.service_id';
        
        if($date_type == 2) {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' WHERE ( whp.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND whp.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
               $query .= ' WHERE ( whp.created_on >= "'.date('Y-m-d 00:00:00').'" AND whp.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' WHERE ( whp.bwh_from_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND whp.bwh_from_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
               $query .= ' WHERE ( whp.bwh_from_date >= "'.date('Y-m-d 00:00:00').'" AND whp.bwh_from_date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }        
        if(!empty($type)){
            $query .= ' AND whp.service_id ='.$type;
        }
        if(!empty($period)){
            $query .= ' AND whp.bwh_period ='.$period;
        }
        
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_part_pay_bwh_rec_no($id) {
        $query = 'SELECT *, GROUP_CONCAT(DISTINCT bwh_rec_no SEPARATOR ",") as receipt_no FROM ' . TBL_BWH;
        $query .= ' WHERE self_id = ' .$id;
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    /*
     * Cashlog Report Module
     */
     
    function list_payment_type(){
        $query = 'SELECT id,name as payment_name, slug FROM ' . TBL_PAY_TYPE . ' WHERE status = 1';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function cashlog_details($start_date='',$end_date=''){
        $query  = 'SELECT p.id, p.pay_type_id, p.pay_amount, p.created_on, p.pay_ref_id, p.pay_ref_type FROM ' . TBL_PAY . ' as p';
        $query .= ' WHERE p.status = 1'; 
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
           $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
        $query .= ' GROUP BY p.pay_ref_type,p.pay_ref_id';
        $query .= ' ORDER BY p.created_on DESC';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_pay_amount_by_type($pay_ref_type,$pay_ref_id,$start_date='',$end_date='') {
        $query  = 'SELECT p.pay_amount, p.pay_type_id, pt.name as PayName, pt.slug, p.created_on FROM ' . TBL_PAY . ' as p';
        $query .= ' JOIN ' . TBL_PAY_TYPE . ' as pt ON pt.id = p.pay_type_id';
        $query .= ' WHERE p.pay_ref_type = ' . $pay_ref_type;
        $query .= ' AND p.pay_ref_id = ' . $pay_ref_id;
        $query .= ' AND p.status = 1';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND p.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
           $query .= ' AND ( p.created_on >= "'.date('Y-m-d 00:00:00').'" AND p.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
        
        $resultdt = $this->get_array_result($query);
        return $resultdt;
        
    }
    
    /*
     * Temple Institution Report Module
     */
    
    function temple_details($start_date='',$end_date='',$type=''){
        $query  = 'SELECT s.name as service_name, s.prefix, CASE t.class WHEN 1 THEN "Morning" ELSE "Evening" END as class_str';
        $query .= ', CASE t.class_type WHEN 1 THEN "Individual in Home" WHEN 2 THEN "Individual in Temple" WHEN 3 THEN "Group in Home" WHEN 4 THEN "Group in Temple" END as class_type_str';
        $query .= ', CASE t.term WHEN 1 THEN "Yearly" WHEN 2 THEN "Monthly" WHEN 3 THEN "Weekly" WHEN 4 THEN "Daily" END term_str, t.* FROM ' . TBL_BILL_TEMPLE . ' as t';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = t.service_id';
        
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND t.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
           $query .= ' WHERE ( t.created_on >= "'.date('Y-m-d 00:00:00').'" AND t.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
            
        if(!empty($type)) {
            $query .= ' AND t.service_id =' . $type;
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    /*
     * Donation Report Module
     */
     
    function donation_details($start_date='',$end_date='',$type='',$mobile='',$pledge='',$pledge_type=''){
        $query  = 'SELECT s.name as service_name, s.prefix, d.*, d.amount as total_amount FROM ' . TBL_BILL_DONATION .' as d';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = d.service_id';
         
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( d.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND d.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
           $query .= ' WHERE ( d.created_on >= "'.date('Y-m-d 00:00:00').'" AND d.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
        if(!empty($type)) {
            $query .= ' AND d.service_id =' . $type;
        }
        if(!empty($mobile)) {
            $query .= ' AND d.contact_no = ' . $mobile;
        }
        if(!empty($pledge) && $pledge == 2) {
            $query .= ' AND d.amount_type = 3';
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_pledge_details($start_date='',$end_date='',$type='',$mobile='',$pledge='',$pledge_type='') {
        $query = 'SELECT s.name as service_name, s.prefix, dp.*, dp.pledge_amount as total_amount, dp.amount as paid_amoun FROM ' . TBL_BILL_DONATION_PLEDGE .' as dp';
        $query .= ' JOIN ' . TBL_SERVICE . ' as  s ON s.id = dp.service_id';
        $query .= ' WHERE dp.status = 1';
        
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( dp.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND dp.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
           $query .= ' AND ( dp.created_on >= "'.date('Y-m-d 00:00:00').'" AND dp.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
        if(!empty($type)) {
            $query .= ' AND dp.service_id = ' . $type;
        }
        if(!empty($mobile)) {
            $query .= ' AND dp.contact_no = ' . $mobile;
        }
        if(!empty($pledge)) {
            $query .= ' AND dp.amount_type = ' . $pledge_type ;
        } else {
            $query .= ' AND dp.amount_type = 1';
        }
        
        $query .= ' ORDER BY dp.id DESC';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    function get_donation_pledge_transaction($service_id='',$name='',$contact_no='',$amount_type='',$start_date='',$end_date='',$pledge_id='') {
        $query = ' SELECT dpt.* FROM ' . TBL_BILL_DONATION_TRANSACTION . ' as dpt WHERE dpt.status = 1';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( dpt.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND dpt.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
           $query .= ' AND ( dpt.created_on >= "'.date('Y-m-d 00:00:00').'" AND dpt.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
        if(!empty($service_id)) {
            $query .= ' AND dpt.service_id = ' . $service_id;
        }
        if(!empty($name)) {
            $query .= ' AND dpt.name = "' . $name .'"';
        }
        if(!empty($mobile)) {
            $query .= ' AND dpt.contact_no = "' . $contact_no . '"';
        }
        if(!empty($amount_type)) {
            $query .= ' AND dpt.amount_type = ' . $amount_type;
        }
        if(!empty($pledge_id)) {
            $query .= ' AND dpt.pledge_id = ' . $pledge_id;
        }
        $query .= ' ORDER BY dpt.id DESC';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    
    
    /*
     * Outdoor Services Report Module
     */
   
    function outdoor_details($start_date='',$end_date='',$type='',$date_type){
        $query = 'SELECT s.name as service_name, s.prefix, p.designation as priest, n.designation as nathaswaram';
        $query .= ', m.designation as melam, CASE os.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, os.* FROM ' . TBL_BILL_OUTDOOR_SERVICES . ' as os';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = os.service_id';
        $query .= ' LEFT JOIN ' . TBL_PRIEST . ' as p ON p.id = os.priest_id';
        $query .= ' LEFT JOIN ' . TBL_PRIEST . ' as n ON n.id = os.nathaswaram_id';
        $query .= ' LEFT JOIN ' . TBL_PRIEST . ' as m ON m.id = os.melam_id';
 
        if($date_type == 2){
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND os.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' WHERE ( os.created_on >= "'.date('Y-m-d 00:00:00').'" AND os.created_on <= "'.date('Y-m-d 23:59:59').'" )';
            }
        } else {
            if(!empty($start_date) && !empty($end_date)) {
                $query .= ' WHERE ( os.date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND os.date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
            } else {
                $query .= ' WHERE ( os.date >= "'.date('Y-m-d 00:00:00').'" AND os.date <= "'.date('Y-m-d 23:59:59').'" )';
            }
        }
        if(!empty($type)){
            $query .= ' AND os.service_id='.$type;
        }
     
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }  

    /*
     * Member Subscription Report Module
     */
   
    function subscription_details($start_date='',$end_date='',$type=''){
        $query = 'SELECT s.name as service_name, s.prefix, ms.* FROM ' . TBL_BILL_MEMBER_SUBSCRIPE . ' as ms';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = ms.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( ms.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ms.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' WHERE ( ms.created_on >= "'.date('Y-m-d 00:00:00').'" AND ms.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
     
        if(!empty($type)){
           $query .= ' AND ms.service_id='.$type;
        }
        $resultdt = $this->get_array_result($query);      
        return $resultdt;
    }

    /*
     * Member Card Report Module
     */
   
    function card_details($start_date='',$end_date='',$card_no=''){
        $query = 'SELECT mc.card_no, mc.name, mc.contact_no, star.name as star, ct.* FROM ' . TBL_BILL_CARD_TOPUP . ' as ct';
        $query .= ' JOIN ' . TBL_BILL_MEMBER_CARD . ' as mc ON mc.id = ct.mem_id';
        $query .= ' LEFT JOIN ' . TBL_STAR . ' as star ON star.id = mc.star';
        $query .= ' WHERE ct.status = 1';

        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( ct.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ct.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' AND ( ct.created_on >= "'.date('Y-m-d 00:00:00').'" AND ct.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
     
        if(!empty($card_no)){
            $query .= ' AND mc.card_no= "'.$card_no . '"';
        }
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }     
    
    
    /*
     * Temple Shop Sale Report Module
     */
   
    function temple_shop_sale_details($start_date='',$end_date='',$type=''){
        $query = 'SELECT s.name as service_name, s.prefix, ts.* FROM ' . TBL_BILL_TEMPLESHOP_SALES . ' as ts';
        $query .= ' JOIN ' . TBL_SERVICE . ' as s ON s.id = ts.service_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( ts.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND ts.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' WHERE ( ts.created_on >= "'.date('Y-m-d 00:00:00').'" AND ts.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
     
        if(!empty($type)){
            $query .= ' AND ts.service_id='.$type;
        }       
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    }
    
    /*
     * Statue Category Report Module
     */
    
    function statue_category_details() {
        $query = 'SELECT id,code as category_code,name as category_name FROM ' . TBL_INV_CATEGORY;
        $query .= ' WHERE is_statue_cat=1';
        $query .= ' AND status=1';
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;        
    }
    
    /*
     * Statue Item Report Module
     */
     
    function statue_item_details($category_id) {
        $query = 'SELECT id,code as item_code,item_name FROM ' . TBL_INV_ITEM;
        $query .= ' WHERE is_statue_item=1';
        $query .= ' AND status=1';
        if(!empty($category_id)){
            $query .= ' AND category_id='.$category_id;
        }
        $resultdt = $this->get_array_result($query);
      
        return $resultdt; 
    }
    
    /*
     * Statue and Book Purchase Report Module
     */
     
    function statuebook_purchase_details($start_date='',$end_date='',$cat_id='',$item_id='') {
        $query = 'SELECT sb.*, c.name as cat_name, i.item_name, v.name as vendor FROM ' . TBL_STATUE_BOOK . ' as sb';
        $query .= ' LEFT JOIN ' . TBL_INV_CATEGORY . ' as c ON c.id = sb.cat_id AND c.is_statue_cat = 1';
        $query .= ' LEFT JOIN ' . TBL_INV_ITEM . ' as i ON i.id = sb.item_id AND i.is_statue_item = 1' ;
        $query .= ' LEFT JOIN ' . TBL_VENDOR . ' as v ON v.id = sb.vendor_id';
        $query .= ' WHERE sb.status != 2';
        
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( sb.received_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND sb.received_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' AND ( sb.received_date >= "'.date('Y-m-d 00:00:00').'" AND sb.received_date <= "'.date('Y-m-d 23:59:59').'" )';
        }
        
        if(!empty($cat_id)) {
            $query .= ' AND sb.cat_id = ' .$cat_id;
        }
        if(!empty($item_id)) {
            $query .= ' AND sb.item_id = ' .$item_id;
        }
        
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    }
    
    /*
     * Statue and Book Sales Report Module
     */
     
    function statuebook_sale_details($start_date='',$end_date='',$cat_id='',$item_id='') {        
        $query = 'SELECT cat.code as category_code, cat.name as category_name, inv.code as item_code, inv.item_name, tmp.created_on';
        $query .= ', tmp.receipt_no, serv.name as service_name, serv.prefix, tp.* FROM ' . TBL_BILL_TEMPLESHOP_ITEM . ' as tp';
        $query .= ' JOIN ' . TBL_BILL_TEMPLESHOP_SALES . ' as tmp ON tmp.id = tp.temple_shop_id';
        $query .= ' JOIN ' . TBL_INV_ITEM . ' as inv ON inv.id = tp.inventory_item_id AND inv.is_statue_item=1 AND inv.status=1';
        $query .= ' JOIN ' . TBL_INV_CATEGORY . ' as cat ON cat.id = inv.category_id AND cat.is_statue_cat=1 AND cat.status=1';
        $query .= ' LEFT JOIN ' . TBL_SERVICE . ' as serv ON serv.id = tmp.service_id';
        $query .= ' WHERE tp.status != 2';
        
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( tmp.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND tmp.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' AND ( tmp.created_on >= "'.date('Y-m-d 00:00:00').'" AND tmp.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }

        if(!empty($cat_id)) {
            $query .= ' AND inv.category_id = ' . $cat_id;
        }
        
        if(!empty($item_id)) {
            $query .= ' AND tp.inventory_item_id = ' . $item_id;
        }
       
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    }
     
    /*
     * Statue and Book Stock Report Module
     */
     
    function statuebook_stock_details($cat_id='',$item_id='') {
        $sale_qty = ', (SELECT CASE WHEN ISNULL(ti.quantity) THEN "0" ELSE SUM(ti.quantity) END as ti_quantity FROM ' . TBL_BILL_TEMPLESHOP_ITEM . ' as ti WHERE ti.inventory_item_id = si.item_id AND ti.status != 2 GROUP BY ti.inventory_item_id) as sales_qty';
        
        $purchase_qty = ', (SELECT CASE WHEN ISNULL(sb.quantity) THEN "0" ELSE SUM(sb.quantity) END as sb_quantity FROM ' . TBL_STATUE_BOOK . ' as sb WHERE sb.item_id = si.item_id AND sb.status != 2  GROUP BY sb.item_id) as purchased_qty';
        
        $query = ' SELECT si.*, c.name as cat_name, i.item_name, i.selling_price' . $sale_qty . $purchase_qty . ' FROM ' . TBL_SB_ITEM . ' as si';
        $query .= ' JOIN ' . TBL_INV_ITEM . ' as i ON i.id = si.item_id AND is_statue_item = 1';
        $query .= ' JOIN ' . TBL_INV_CATEGORY . ' as c ON c.id = i.category_id AND is_statue_cat = 1';
        $query .= ' WHERE c.status != 2 AND i.status != 2';
        
        if(!empty($cat_id)) {
            $query .= ' AND c.id = ' . $cat_id;
        }
        if(!empty($item_id)) {
            $query .= ' AND si.item_id = ' . $item_id;
        }
        
        $query .= ' GROUP BY si.item_id ORDER BY id DESC';
        $resultdt = $this->get_array_result($query);
      
        return $resultdt;
    }
    
    /*
     * Expenses Types
     */
     
    function expenses_type_list() {
        $query = 'SELECT DISTINCT(exp_type), exp_type_slug FROM ' . TBL_EXPENSES;
        $query .= ' WHERE status = 1 ORDER BY exp_type';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    /*
     * Expenses Subjects
     */
     
    function expense_subjects_list($exp_type_slug='') {
        $query = 'SELECT DISTINCT(exp_sub), exp_sub_slug FROM ' . TBL_EXPENSES;
        $query .= ' WHERE status = 1';
        if(!empty($exp_type_slug)) {
            $query .= ' AND exp_type_slug = "' . $exp_type_slug . '"';
        }
        $query .= ' ORDER BY exp_sub';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
    
    /*
     * Expenses Report Module
     */
     
    function expenses_details($type_slug='',$subj_slug='',$start_date='',$end_date='')  {
        $query = 'SELECT exp_rec_no, exp_type, exp_sub, exp_name, exp_date, exp_amount, exp_desc FROM ' . TBL_EXPENSES;
        $query .= ' WHERE status = 1';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( exp_date >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND exp_date <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' AND ( exp_date >= "'.date('Y-m-d 00:00:00').'" AND exp_date <= "'.date('Y-m-d 23:59:59').'" )';
        } 
        
        if(!empty($type_slug)) {
            $query .= ' AND exp_type_slug = "' . $type_slug . '"';
        }
        if(!empty($subj_slug)) {
            $query .= ' AND exp_sub_slug = "' . $subj_slug . '"';
        } 
        $query .= ' ORDER BY exp_name';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    } 

    /*
     * Inventory Sale Report
     */
     
    function inventory_item_details() {
        $query = 'SELECT ii.id, ii.item_name FROM ' . TBL_INV_LOG . ' as il';
        $query .= ' JOIN ' . TBL_INV_ITEM . ' as ii ON ii.id = il.inv_item_id';
        $query .= ' WHERE il.status = 1 GROUP BY il.inv_item_id';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }

    function inventory_sale_details($start_date='',$end_date='',$item_id=''){
        
        $query = 'SELECT il.*, ii.item_name FROM ' . TBL_INV_LOG. ' as il';
        $query .= ' LEFT JOIN ' . TBL_INV_ITEM . ' as ii ON ii.id = il.inv_item_id';
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' WHERE ( il.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND il.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $query .= ' WHERE ( il.created_on >= "'.date('Y-m-d 00:00:00').'" AND il.created_on <= "'.date('Y-m-d 23:59:59').'" )';    
        }
        
        if(!empty($item_id)) {
            $query .= ' AND il.inv_item_id='.$item_id;    
        }
        
        $query .= ' AND il.status=1';
        $resultdt = $this->get_array_result($query);
        
        return $resultdt;
    }

    /*
     * Archanai Sale Report
     */

    function archanai_item_details() {
        $query = 'SELECT i.* FROM ' . TBL_BARI . ' as ai JOIN ' . TBL_ITEM . ' as i ON i.id = ai.bari_item_id';
        $query .= ' WHERE i.status != 2 AND i.is_inventory = 1 GROUP BY ai.bari_item_id';
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    } 
    
    function archanai_sale_details($start_date,$end_date,$item_id) {
        /* $total_qty = ', (SELECT SUM(iss.issue_qty) as totalqty FROM ' . TBL_INV_ISSUED . ' as iss WHERE iss.inv_item_id = ii.id AND iss.usage_type = 1 AND iss.status != 2) as total_quantity'; */    
            
        if(!empty($start_date) && !empty($end_date)) {
            $where = ' AND ( bari.created_on >= "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" AND bari.created_on <= "'.date('Y-m-d 23:59:59',strtotime($end_date)).'" )';
        } else {
            $where = ' AND ( bari.created_on >= "'.date('Y-m-d 00:00:00').'" AND bari.created_on <= "'.date('Y-m-d 23:59:59').'" )';
        }
        
        $issued_qty = ', (SELECT SUM(bari.bari_qty) as qty FROM ' . TBL_BARI .' as bari WHERE bari.bari_item_id = ai.bari_item_id AND bari.bari_is_inventory = 1' . $where . ') as issued_qty';
        
        if(!empty($start_date) && !empty($end_date)) {
            $o_where = ' AND ( obari.created_on < "'.date('Y-m-d 00:00:00',strtotime($start_date)).'" )';
        } else {
            $o_where = ' AND ( obari.created_on < "'.date('Y-m-d 00:00:00').'" )';
        }
        
        $old_issued_qty = ', (SELECT CASE WHEN ISNULL(SUM(obari.bari_qty))THEN "0" ELSE (SUM(obari.bari_qty)) END as qty FROM ' . TBL_BARI .' as obari WHERE obari.bari_item_id = ai.bari_item_id AND obari.bari_is_inventory = 1' . $o_where . ') as old_issued_qty';
        
        $total_issued_qty = ', (SELECT CASE WHEN ISNULL(SUM(issq.total_issued_qty))THEN "" ELSE (SUM(issq.total_issued_qty)) END as iss_qty FROM ' . TBL_INV_ISSUED.' as issq WHERE issq.inv_item_id = ii.id AND issq.usage_type = 1) as total_issued_qty';
        
        $query = 'SELECT ii.id as iv_item, i.name as item, i.price, ai.created_on, iq.total_issued_qty as total_issued';
        $query .= ', iq.total_sold_qty, iq.total_avail_qty' . $issued_qty . $old_issued_qty . $total_issued_qty . ' FROM ' . TBL_BARI . ' as ai';
        $query .= ' JOIN ' . TBL_ITEM . ' as i ON i.id = ai.bari_item_id';
        $query .= ' JOIN ' . TBL_INV_ITEM . ' as ii ON ii.id = i.inv_item_id';
        $query .= ' JOIN ' . TBL_INV_ISSUED . ' as iq ON iq.inv_item_id = ii.id AND iq.usage_type = 1';
        $query .= ' WHERE i.status != 2 AND i.is_inventory = 1 AND ai.bari_is_inventory = 1';
        
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( ai.created_on >= "' . date('Y-m-d 00:00:00',strtotime($start_date)). '" AND ai.created_on <= "' . date('Y-m-d 23:59:59',strtotime($end_date)) . '")';        
        } else {
            $query .= ' AND ( ai.created_on >= "' . date('Y-m-d 00:00:00') . '" AND ai.created_on <= "' . date('Y-m-d 23:59:59') . '")';
        }
        
        if(!empty($item_id)) {
            $query .= ' AND i.id =' . $item_id; 
        }
        $query .= ' GROUP BY ai.bari_item_id';
        $resultdt = $this->get_array_result($query);
        return $resultdt;        
    }
    
    function inventory_gold_silver_details($start_date='',$end_date='',$item_type=''){
        $query = 'SELECT t.type as type_name, gs.*, CASE gs.item WHEN 1 THEN "Gold" WHEN 2 THEN "silver" END as item_str';
        $query .= ' FROM ' . TBL_INV_GOLD . ' as gs JOIN ' . TBL_INV_GOLD_TYPE . ' as t ON t.id = gs.type';
        $query .= ' WHERE gs.status = 1';
        
        if(!empty($start_date) && !empty($end_date)) {
            $query .= ' AND ( gs.created_on >= "' . date('Y-m-d 00:00:00',strtotime($start_date)). '" AND gs.created_on <= "' . date('Y-m-d 23:59:59',strtotime($end_date)) . '")';        
        } else {
            $query .= ' AND ( gs.created_on >= "' . date('Y-m-d 00:00:00') . '" AND gs.created_on <= "' . date('Y-m-d 23:59:59') . '")';
        }
       
        if(!empty($item_type)) {
            $this->db->where('gs.item',$item_type);
            $query .= 'AND gs.item = "' . $item_type . '"';
        }
        
        $resultdt = $this->get_array_result($query);
        return $resultdt;
    }
}