<?php
class Monthly_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->services = TBL_SERVICE;
        $this->prasadam = TBL_PRASADAM;
        $this->payments = TBL_PAY;
        $this->payment_type = TBL_PAY_TYPE;
        $this->abishegam = TBL_BA;
        $this->ab_prasadam = TBL_BAI;
        $this->festival_function = TBL_BILL_FESTIVAL_FUNCTION;
        $this->f_prasadam = TBL_BILL_FESTIVAL_PRASADAM;
        $this->ubayam = TBL_BILL_UBAYAM;
        $this->u_prasadam = TBL_BILL_UBAYAM_PRASADAM;
        $this->mega_function = TBL_BILL_MEGA_FUNCTION;
        $this->m_prasadam = TBL_BILL_MEGA_PRASADAM;
        $this->donation = TBL_BILL_DONATION;
        #$this->d_prasadam = TBL_BILL_DONATION;
        $this->other_services = TBL_BILL_OTHERS;
        $this->gold_silver = TBL_BILL_GOLD_SILVER_OFFERING;
        $this->temple = TBL_BILL_TEMPLE;
        $this->member_subscripe = TBL_BILL_MEMBER_SUBSCRIPE;
        $this->building_fund = TBL_BILL_BUILDING_FUND;
        $this->outdoor  = TBL_BILL_OUTDOOR_SERVICES;
        $this->bill_prasadam = TBL_BP;
        $this->madappalli = TBL_BM;
        $this->hall_booking = TBL_BH;
        $this->wedding_hall_booking = TBL_BWH;
        $this->settings = TBL_SETTINGS_GENERAL;
        $this->archanai = TBL_BAR;
        $this->archanai_item = TBL_BARI;
        $this->category = TBL_CATEGORY;
        $this->item = TBL_ITEM;
        $this->kattalai = TBL_BK;
        $this->kattalai_item = TBL_BKI;
        $this->refund = TBL_REFUND;
        $this->member_register = TBL_BILL_MEMBER_CARD;
        $this->card_topup = TBL_BILL_CARD_TOPUP;
        $this->temple_shop = TBL_BILL_TEMPLESHOP_SALES;
    }
    
    public function payment_type_list() {
        $this->db->select('id,name, slug as payment_mode');
        $this->db->where('status',1);
        return $this->db->get($this->payment_type)->result();
    }
    
    public function get_total_paid_amt_by_type($id,$start_date='',$end_date='') {
        $this->db->select('SUM(pay_amount) as amount',FALSE);
        $this->db->where('pay_type_id',$id);
        if((!empty($start_date)) && (!empty($end_date))) {
           $this->db->where('created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
           $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
           $this->db->where('created_on >=',date('Y-m-d 00:00:00'));
           $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
       $this->db->where('status',1);
       return $this->db->get($this->payments)->row(); 
    }
    
    public function get_payment_details($type_id,$service_id,$start_date= '',$end_date='',$mem_card_id) {
        $this->db->select('*, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment',FALSE);
        $this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id AND pt.status != 2','left');
        $this->db->where('p.pay_ref_type',$type_id);
        if(!empty($service_id)){
            $this->db->where('p.pay_ref_id',$service_id);
        }
        if(!empty($mem_card_id)){
            $this->db->where('p.mem_card_id',$mem_card_id);
        }
        $this->db->where('p.status',1);
        if((!empty($start_date)) && (!empty($end_date))) {
           $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
           $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
           $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
           $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        return $this->db->get($this->payments . ' as p')->result_array();
    }
    
    public function get_by_id() {
        $resource = $this->db->get($this->settings);
        return $resource->row();
    }
    
    public function get_abishegam_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->abishegam . ' as ab','ab.id = p.pay_ref_id AND p.pay_ref_type = 1','left');
        $this->db->join($this->services . ' as ser','ser.id = ab.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.status',1);
        $this->db->where('ab.ba_book_status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('ab.ba_rec_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['ba_rec_no'] . '-' . (($general->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'];
                
                // 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;
    }
    
    public function get_archanai_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        /*$this->db->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', FALSE);
        $this->db->join($this->archanai_item . ' as ai','a.id = ai.bar_id'); //,'left'
        $this->db->join($this->category . ' as cat','cat.id = ai.bari_cat_id'); //,'left'
        $this->db->join($this->item . ' as item','item.id = ai.bari_item_id'); //,'left'
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('a.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('a.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('a.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('a.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('a.status',1);
        $this->db->order_by('type_name', 'ASC');
        $results = $this->db->get($this->archanai . ' as a')->result_array(); */
		
		
		
		$this->db->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', FALSE);
        $this->db->join($this->item . ' as item','item.id = ai.bari_item_id','left');
       $this->db->join($this->archanai . ' as a','a.id = ai.bar_id','left');
       $this->db->join($this->category . ' as cat','cat.id = ai.bari_cat_id','left');
       
       if((!empty($start_date)) && (!empty($end_date))) {
           $this->db->where('a.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
           $this->db->where('a.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
       } else {
           $this->db->where('a.created_on >=',date('Y-m-d 00:00:00'));
           $this->db->where('a.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
       }
       $this->db->where('a.status',1);
       $this->db->order_by('type_name', 'ASC');
       $results = $this->db->get($this->archanai_item . ' as ai')->result_array();		
        
        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'];
						$indx = $value['service_id'];
                     
                        // Name
                        if( empty( $return[$indx]['name'] ) ) :
                           // $return[$indx]['name'] = $value['type_name'] . ' - '. $value['bari_price'];						   
						   $return[$indx]['name'] = $value['type_name'];
                        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'];
                
				if($value['grouping'] == 0) :
					// Price
					$return[$indx]['price'] =  $value['bari_price'];
				else :
					// Price
					$return[$indx]['price'] =  "-";
				endif;
                
                // 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;
    }
    
    public function get_festival_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->festival_function . ' as f','f.id = p.pay_ref_id AND f.type_id = p.pay_ref_type','left');
        $this->db->join($this->services . ' as ser','ser.id = f.service_id');
        if((!empty($start_date))&& (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('f.book_status',1);
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('f.receipt_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_ubayam_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->ubayam . ' as u','u.id = p.pay_ref_id AND u.type_id = p.pay_ref_type','left');
        $this->db->join($this->services . ' as ser','ser.id = u.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('u.book_status',1);
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('u.receipt_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_mega_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->mega_function . ' as m','m.id = p.pay_ref_id AND m.type_id = p.pay_ref_type','left');
        $this->db->join($this->services . ' as ser','ser.id = m.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('m.book_status',1);
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('m.receipt_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_other_service_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = os.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('os.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('os.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('os.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('os.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('os.status',1);
        $this->db->order_by('os.id', 'ASC');
        $results = $this->db->get($this->other_services . ' as os')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_gold_silver_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = gs.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('gs.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('gs.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('gs.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('gs.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('gs.status',1);
        $this->db->order_by('gs.id', 'ASC');
        $results = $this->db->get($this->gold_silver . ' as gs')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_temple_activity_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = t.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('t.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('t.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('t.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('t.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('t.status',1);
        $this->db->order_by('t.id', 'ASC');
        $results = $this->db->get($this->temple . ' as t')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_member_subscribe_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = mem.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('mem.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('mem.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('mem.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('mem.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('mem.status',1);
        $this->db->order_by('mem.id', 'ASC');
        $results = $this->db->get($this->member_subscripe . ' as mem')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_building_fund_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = bf.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('bf.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('bf.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('bf.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('bf.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('bf.status',1);
        $this->db->order_by('bf.id', 'ASC');
        $results = $this->db->get($this->building_fund . ' as bf')->result_array();
        
        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->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->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'] = $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;
    }
    
    public function get_outdoor_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        /* $this->db->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', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = od.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('od.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('od.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('od.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('od.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('od.status',1);
        $this->db->where('od.book_status',1);
        $this->db->order_by('od.receipt_no', 'ASC');
        $results = $this->db->get($this->outdoor . ' as od')->result_array(); */
        
        
        $this->db->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', FALSE);
        $this->db->join($this->outdoor . ' as od','od.id = p.pay_ref_id AND od.type_id = p.pay_ref_type','left');
        $this->db->join($this->services . ' as ser','ser.id = od.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('od.book_status',1);
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('od.receipt_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->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'] = $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;
    }
    
    
    public function get_prasadam_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->bill_prasadam . ' as bp','bp.id = p.pay_ref_id AND p.pay_ref_type = 15','left');
        $this->db->join($this->services . ' as ser','ser.id = bp.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->where('bp.bp_book_status',1);
        $this->db->order_by('bp.bp_rec_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bp_rec_no'] . '-' . (($general->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;
    }
    
    public function get_madappalli_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->madappalli . ' as bm','bm.id = p.pay_ref_id AND p.pay_ref_type = 9','left');
        $this->db->join($this->services . ' as ser','ser.id = bm.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.status',1);
        $this->db->where('bm.bm_book_status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('bm.bm_rec_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bm_rec_no'] . '-' . (($general->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;
    }
    
    public function get_donation_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        /* $this->db->select('ser.name as type_name, ser.prefix, d.receipt_no, d.service_id, p.pay_amount as amount, d.id, d.amount_type, d.gst, d.gst_value', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = d.service_id');
        $this->db->join($this->payments . ' as p','p.pay_ref_id = d.id AND p.pay_ref_type = d.type_id AND p.status = 1','left');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('d.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('d.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('d.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('d.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('d.status',1);
        $this->db->order_by('d.id', 'ASC');
        $results = $this->db->get($this->donation . ' as d')->result_array(); */
        
        $this->db->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', FALSE);
        $this->db->join($this->donation . ' as d','d.id = p.pay_ref_id AND d.type_id = p.pay_ref_type','left');
        $this->db->join($this->services . ' as ser','ser.id = d.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('d.id', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->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;
                
                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;
    }
    
    public function get_hall_booking_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->hall_booking . ' as hb','hb.id = p.pay_ref_id');
        $this->db->join($this->services . ' as ser','ser.id = hb.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.pay_ref_type',7);
        $this->db->where('p.status',1);
        $this->db->order_by('p.pay_ref_id', 'ASC');
        $this->db->group_by('p.pay_ref_id');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bh_rec_no'] . '-' . (($general->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;
    
    }
    
    public function get_wed_hall_booking_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->wedding_hall_booking . ' as whb','whb.id = p.pay_ref_id');
        $this->db->join($this->services . ' as ser','ser.id = whb.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.pay_ref_type',18);
        $this->db->where('p.status',1);
        $this->db->order_by('p.pay_ref_id', 'ASC');
        $this->db->group_by('p.pay_ref_id');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bwh_rec_no'] . '-' . (($general->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;
    
    }
    
    public function get_kattalai_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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', FALSE);
        $this->db->join($this->kattalai . ' as k','k.id = p.pay_ref_id AND k.type_id = p.pay_ref_type','left');
        $this->db->join($this->services . ' as ser','ser.id = k.service_id');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.status',1);
        $this->db->where('k.bk_book_status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('k.bk_rec_no', 'ASC');
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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->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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'] ) : (($general->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['bk_rec_no'] . '-' . (($general->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;
    }
    
    public function get_membership_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->select('mr.*', FALSE);
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('mr.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('mr.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('mr.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('mr.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('mr.status',1);
        $results = $this->db->get($this->member_register . ' as mr')->result_array();
        
        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 ;
                
                // Amount
                
                $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->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['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'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['amount']) )) ? (($return[$indx]['amount'] + '10.00') * $return[$indx]['qty']) : ($return[$indx]['qty'] * 10);
                
                $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;
    }
    
    public function get_membership_card_topup_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->select('ct.*, sum(ct.topup_amount) as amount, ct.id', FALSE);
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('ct.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('ct.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('ct.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('ct.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        #$this->db->where('ct.status',1);
        $this->db->where('ct.mem_first_data !=',1);
        $this->db->group_by('ct.mem_id');
        $results = $this->db->get($this->card_topup . ' as ct')->result_array();
        
        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->prefix == 1 && (!empty($value['prefix']))) ? $value['prefix'] : '') . $value['receipt_no'];
                
                // Auto increment id 
                $return[$indx]['id'] = $value['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 the 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->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['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'] ;
                
                $return[$indx]['mem_card_id'] = $value['id'] ;
                
                // Type id 
                $return[$indx]['type_id'] = 12 ;
                
                //Delete Reason
                $return[$indx]['del_reason'] = 'Delete the record';
                
            endif;
        }
        return $return;
    }
    
    public function get_temple_shop_data($start_date='',$end_date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->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, ts.gst_value, ser.menu_name, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment, ts.del_reason', FALSE);
        $this->db->join($this->temple_shop . ' as ts','ts.id = p.pay_ref_id AND p.pay_ref_type = 19','left');
        $this->db->join($this->services . ' as ser','ser.id = ts.service_id');
        $this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id','left');
        if((!empty($start_date)) && (!empty($end_date))) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('p.status',1);
        $this->db->group_by('p.pay_ref_id');
        $this->db->order_by('ts.receipt_no', 'ASC');        
        $results = $this->db->get($this->payments . ' as p')->result_array();
        
        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;
        
    }
    
    public function get_refund_amount($start_date,$end_date) {
        $this->db->select('CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount',FALSE);
        if((!empty($start_date)) && (!empty($end_date))) {
           $this->db->where('created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
           $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
           $this->db->where('created_on >=',date('Y-m-d 00:00:00'));
           $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime('+27 days')));
        }
        $this->db->where('status',1);
        return $this->db->get($this->refund)->row()->refund_amount;
    }
    
    public function get_refund_amt_by_ids($menu_id,$service_id,$auto_id,$start_date,$end_date) {
        if(!empty($menu_id) && !empty($service_id) && !empty($auto_id) && !empty($start_date) && !empty($end_date)) {
            $this->db->select('CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount',FALSE);
            $this->db->where('menu_id',$menu_id);
            $this->db->where('service_id',$service_id);
            $this->db->where('auto_id',$auto_id);
            $this->db->where('created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
            $this->db->where('status',1);
            return $this->db->get($this->refund)->row()->refund_amount;
        }
    }
}
?>


