<?php
class Daily_summary_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->madappalli_item = TBL_BMI;
        $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->madappalli_master = TBL_MADAPPALLI;
        $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,$date='') {
        $this->db->select('SUM(pay_amount) as amount',FALSE);
        //$id = 2;
        //$date = '25-01-2017';
        $this->db->where('pay_type_id',$id);
        /*if(!empty($date)) {
           $this->db->where('created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
           $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(created_on) = "'.date('Y-m-d').'"');
        }
       $this->db->where('status',1);
       return $this->db->get($this->payments)->row(); 
       //echo "<pre>"; print_r( $this->db->get($this->payments)->row() ); die;
       
    }
    
    public function get_payment_details($type_id,$auto_id,$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($auto_id)){
            $this->db->where('p.pay_ref_id',$auto_id);
        }
        if(!empty($mem_card_id)){
            $this->db->where('p.mem_card_id',$mem_card_id);
        }        
        $this->db->where('p.status',1);
        #$this->db->group_by('pt.name');
        /*if(!empty($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/  
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }    
        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($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, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment, ab.del_reason, ser.gst as is_gst', FALSE);
		$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, ser.gst as is_gst', 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');
        //$this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id','left');
        /*if(!empty($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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 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['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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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 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['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'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst_value'] : $value['gst'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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_archanai_category($id) {
		$this->db->select('cat.category_name as type_name, cat.id as service_id, cat.grouping', FALSE);
		$this->db->where('cat.id', $id);
		return $this->db->get($this->category . ' as cat')->row_array();
	}
	
	public function get_archanai_item($id) {
		$this->db->select('item.name as item_name, item.id as item_id, item.gst', FALSE);
		$this->db->where('item.id', $id);
		return $this->db->get($this->item . ' as item')->row_array();
	}
	
	public function get_archanai_data($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->select('a.id, a.bar_rec_no, a.gst, a.gst_value, ai.bari_cat_id, ai.bari_item_id, ai.bari_qty, ai.bari_price, ai.bari_sub_total, ai.bari_rec_no, 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($date)) {
            $this->db->where('a.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('a.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(a.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(a.created_on) = "'.date('Y-m-d').'"');
        }
        $this->db->where('a.status',1);
        //$this->db->order_by('type_name', 'ASC');
        $results = $this->db->get($this->archanai . ' as a')->result_array();
      	
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			foreach($results as $key=>$value) {
				$category_data 	= $this->get_archanai_category($value['bari_cat_id']);
				$item_data 		= $this->get_archanai_item($value['bari_item_id']);
				if( $is_detail ) :
					// Submitted for "Details"
					
					// Summary
					$indx = $category_data['service_id'].'_'.$value['bari_price'].$key;
					
					// Categery Name
					if($value['is_category'] == 0) : // With Category Condition
						if( empty( $return[$indx]['cat_name'] ) ) :
							$return[$indx]['cat_name'] = $category_data['type_name'];
						endif;
					else :
						$return[$indx]['cat_name'] = 'Archanai';
					endif;
					
					// Sub Categery Name
					if( empty( $return[$indx]['name'] ) ) :
						$return[$indx]['name'] = $item_data['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 ;
					
					//GST Value in Settings
					$return[$indx]['gst'] = ($item_data['gst'] == 1) ? $value['gst'] : 0;
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = ($general->gst > 0) ? 1 : 0;
					
				else :
					/* if($value['grouping'] == 0) :
						// Summary
						$indx = $value['service_id'].'_'.$value['item_id'];
					 
						// Name
						if( empty( $return[$indx]['name'] ) ) :
							$return[$indx]['name'] = $value['type_name'] . ' - '. $value['item_name'];
						endif;
					
					else :
						// Summary
						$indx = $value['service_id'].'_'.$value['bari_price'];
					 
						// Name
						if( empty( $return[$indx]['name'] ) ) :
							$return[$indx]['name'] = $value['type_name'] . ' - '. $value['bari_price'];
						endif;
					endif; */
					
					if($category_data['grouping'] == 0) :
						// Summary
						$indx = $category_data['service_id'].'_'.$item_data['item_id'];
					 
						if($value['is_category'] == 0) : // With Category Condition
							// Name
							if( empty( $return[$indx]['name'] ) ) :
								$return[$indx]['name'] = $category_data['type_name'] . ' - '. $item_data['item_name'];
							endif;
							
						else :
						
							if( empty( $return[$indx]['name'] ) ) :
								$return[$indx]['name'] = $item_data['item_name'];
							endif;
						endif;
					
					else :
						if($value['is_category'] == 0) : // With Category Condition
							// Summary
							$indx = $category_data['service_id'].'_'.$value['bari_price'];
						 
							// Name
							if( empty( $return[$indx]['name'] ) ) :
								$return[$indx]['name'] = $category_data['type_name'] . ' - '. $value['bari_price'];
							endif;
							
						else :
							// Summary
							$indx = $category_data['service_id'].'_'.$item_data['item_id'];
						 
							// Name
							if( empty( $return[$indx]['name'] ) ) :
								$return[$indx]['name'] = $item_data['item_name'];
							endif;
							
						endif;
					endif;
					
					// Rec No
					/* $return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-(.*?)/', '', $return[$indx]['rec_no'] ) . '-'. $value['bar_rec_no'] ) : $value['bar_rec_no']; */
					
					$return[$indx]['rec_no'] = !empty( $return[$indx]['rec_no'] ) ? ( preg_replace( '/\-.+/', '', $return[$indx]['rec_no'] ) . '-'. $value['bari_rec_no'] ) :  $value['bari_rec_no'] . '-' . $value['bari_rec_no'];
					
					// Price
					$return[$indx]['price'] =  $value['bari_price'];
					
					// Qty 
					$return[$indx]['qty'] = !empty( $return[$indx]['qty'] ) ? $return[$indx]['qty']+$value['bari_qty'] : $value['bari_qty'];
					
					// Amount
					$return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? $return[$indx]['amount'] + $value['bari_sub_total'] : $value['bari_sub_total'];
					
				   # $return[$indx]['gst'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst'] : $value['gst'];
				   
					//GST Value in Settings
					$return[$indx]['gst'] = ($item_data['gst'] == 1) ? $value['gst'] : 0;
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = ($general->gst > 0) ? 1 : 0;
					
					// Auto increment id 
					$return[$indx]['id'] =  $value['id'];
					
					// Type id 
					$return[$indx]['type_id'] = 2 ;
				
				endif;
			}
		}
		
        return $return;
    }
	    
    public function get_festival_data($date='',$data_type='') {
        /* $general = $this->get_by_id();
        
        $this->db->select('ser.name as type_name, ser.prefix, f.receipt_no, f.service_id, f.amount, f.id, f.type_id, f.gst, f.gst_value', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = f.service_id');
        if(!empty($date)) {
            $this->db->where('f.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('f.created_on <=',date('Y-m-d 23:59:59',strtotime($date)));
        } else {
            $this->db->where('f.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('f.created_on <=',date('Y-m-d 23:59:59'));
        }
        $this->db->where('f.book_status',1);
        $this->db->where('f.status',1);
        $this->db->order_by('f.service_id', 'ASC');
        $results = $this->db->get($this->festival_function . ' as f')->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_value'];
                
            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'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst_value'] : $value['gst_value'];
                
            
            endif;
        }
        return $return; */
        
        $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, ser.gst as is_gst', 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($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst'] : $value['gst'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment, u.del_reason, ser.gst as is_gst', FALSE);
		$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, ser.gst as is_gst', 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');
        //$this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id','left');
        /*if(!empty($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst'] : $value['gst'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_gst'];
					
					 // Auto increment id 
					$return[$indx]['id'] = $value['id'] ;
					
					// Type id 
					$return[$indx]['type_id'] = $value['type_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_mega_data($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, ser.gst as is_gst', 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($date)) {
//            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
//            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
//        }
//        $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' || $data_type == 'Pdf 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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = os.service_id');
        /*if(!empty($date)) {
            $this->db->where('os.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('os.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(os.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(os.created_on) = "'.date('Y-m-d').'"');
        }
        #$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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = gs.service_id');
        /*if(!empty($date)) {
            $this->db->where('gs.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('gs.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(gs.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(gs.created_on) = "'.date('Y-m-d').'"');
        }
        #$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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', FALSE);
//        $this->db->join($this->services . ' as ser','ser.id = t.service_id');
//        if(!empty($date)) {
//            $this->db->where('t.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
//            $this->db->where('t.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
//        }
//        #$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' || $data_type == 'Pdf 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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', FALSE);
//        $this->db->join($this->services . ' as ser','ser.id = mem.service_id');
//        if(!empty($date)) {
//            $this->db->where('mem.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
//            $this->db->where('mem.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
//        }
//        #$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' || $data_type == 'Pdf 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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = bf.service_id');
        if(!empty($date)) {
            $this->db->where('bf.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('bf.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }
        $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' || $data_type == 'Pdf 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'];
                
                //GST Value in Settings
                $return[$indx]['gst'] = $value['gst'];
                
                //GST is enable or not in Service Master
                $return[$indx]['is_gst'] = $value['is_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'] = 'Kumbabishegam - ' . $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'];
                
                //GST Value in Settings
                $return[$indx]['gst'] = $value['gst'];
                
                //GST is enable or not in Service Master
                $return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', FALSE);
        $this->db->join($this->services . ' as ser','ser.id = od.service_id');
		$this->db->join('thiru_payments as p', 'p.pay_ref_id=od.id  AND od.type_id = p.pay_ref_type', "left");
        /*if(!empty($date)) {
            $this->db->where('od.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('od.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        } */
		
		/*if (!empty($date)) {
            $this->db->where('p.created_on >=', date('Y-m-d 00:00:00', strtotime($date)));
            $this->db->where('p.created_on <=', date('Y-m-d 23:59:59', strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $this->db->where("p.pay_amount !=", 0);
		
        #$this->db->where('od.status',1);
        $this->db->where('od.book_status',1);
		 $this->db->group_by('od.id');
        $this->db->order_by('od.receipt_no', 'ASC');
        $results = $this->db->get($this->outdoor . ' as od')->result_array();
        
        //echo 'Q:'. $this->db->last_query(); die;
        
        if((!empty($data_type)) && ($data_type == 'Get Details Data' || $data_type == 'Print Details Data' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', 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($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $this->db->where('p.status',1);
        $this->db->where('bp.bp_book_status',1);
        $this->db->group_by('p.pay_ref_id');
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'] = ($return[$indx]['service_id'] == $indx && (!empty( $return[$indx]['gst']) )) ? $return[$indx]['gst'] + $value['gst'] : $value['gst'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', 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($date)) {
//            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
//            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
//        }
//        $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' || $data_type == 'Pdf 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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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($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('+6 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, GROUP_CONCAT(DISTINCT pt.name SEPARATOR ",") as payment, d.del_reason, ser.gst as is_gst', FALSE);
		$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, ser.gst as is_gst', 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');
        //$this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id','left');
        /*if(!empty($date)) {
            $this->db->where('DATE(p.created_on) >=',date('Y-m-d',strtotime($date)));
            $this->db->where('DATE(p.created_on) <=',date('Y-m-d',strtotime($date)));
        } else {
            $this->db->where('DATE(p.created_on) >=',date('Y-m-d'));
            $this->db->where('DATE(p.created_on) <=',date('Y-m-d'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_gst'];
					
					 // Auto increment id 
					$return[$indx]['id'] = $value['id'] ;
					
					// Type id 
					$return[$indx]['type_id'] = 4 ;
					
					#$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_hall_booking_data($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, ser.gst as is_gst', 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($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
					 
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', 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($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
	
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($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, ser.gst as is_gst', 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($date)) {
            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(p.created_on) = "'.date('Y-m-d').'"');
        }
        $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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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'];
					
					//GST Value in Settings
					$return[$indx]['gst'] = $value['gst'];
					
					//GST is enable or not in Service Master
					$return[$indx]['is_gst'] = $value['is_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($date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->select('mr.*', FALSE);
        /*if(!empty($date)) {
            $this->db->where('mr.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('mr.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(mr.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(mr.created_on) = "'.date('Y-m-d').'"');
        }
        #$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' || $data_type == 'Pdf 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'] = 'Prepaid 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'] = 'Prepaid 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'] + (($value['amount'] > 0) ? '10.00' : '0.00')) * $return[$indx]['qty']) : ($return[$indx]['qty'] * (($value['amount'] > 0) ? 10 : 0 ));
                
                $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($date='',$data_type='') {
        $general = $this->get_by_id();
        
        $this->db->select('ct.*, sum(ct.topup_amount) as amount, ct.id', FALSE);
        /*if(!empty($date)) {
            $this->db->where('ct.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('ct.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(ct.created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(ct.created_on) = "'.date('Y-m-d').'"');
        }
        #$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' || $data_type == 'Pdf Details Data'))
            $is_detail = TRUE;
        
        else
            $is_detail = FALSE;
        
        $return = array();
		if($results){
			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'] = 'Prepaid 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'] ;
					
					 // Auto increment id 
					$return[$indx]['mem_card_id'] = $value['id'] ;
					
					// Type id 
					$return[$indx]['type_id'] = 12 ;
					
					// Amount
					
					$return[$indx]['amount'] = ((!empty( $return[$indx]['amount']) )) ? number_format((($return[$indx]['amount'] + $value['amount'])),2,'.','') : number_format(($value['amount']),2,'.','');
					
					#$return[$indx]['gst'] = $value['gst'];
					
					//Delete Reason
					$return[$indx]['del_reason'] = 'Delete this record';
					
				else :
					// Summary
					$indx = 1;
				 
					// Name
					if( empty( $return[$indx]['name'] ) ) :
						$return[$indx]['name'] = 'Prepaid 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'] ;
					
					 // Auto increment id 
					$return[$indx]['mem_card_id'] = $value['id'] ;
					
					// Type id 
					$return[$indx]['type_id'] = 12 ;
					
					//Delete Reason
					$return[$indx]['del_reason'] = 'Delete this record';
					
				endif;
			}
		}
        return $return;
    }
    
//    public function get_temple_shop_data($date='',$data_type='') {
//        $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, ser.gst as is_gst', 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($date)) {
//            $this->db->where('p.created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
//            $this->db->where('p.created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
//        }
//        $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' || $data_type == 'Pdf 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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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'];
//                
//                //GST Value in Settings
//                $return[$indx]['gst'] = $value['gst'];
//                
//                //GST is enable or not in Service Master
//                $return[$indx]['is_gst'] = $value['is_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($date) {
        $this->db->select('CASE WHEN amount IS NULL THEN "0.00" ELSE SUM(amount) END as refund_amount',FALSE);
        /*if(!empty($date)) {
            $this->db->where('created_on >=',date('Y-m-d 00:00:00',strtotime($date)));
            $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime($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'));
        }*/
		if (!empty($date)) {            
			$this->db->where('DATE(created_on) = "'.date('Y-m-d', strtotime($date)).'"');
        } else {            
			$this->db->where('DATE(created_on) = "'.date('Y-m-d').'"');
        }
        $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,$date) {
        if(!empty($menu_id) && !empty($service_id) && !empty($auto_id) && !empty($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($date)));
            $this->db->where('created_on <=',date('Y-m-d 23:59:59',strtotime($date)));*/
			$this->db->where('DATE(created_on) = "'.date('Y-m-d', strtotime($date)).'"');
            $this->db->where('status',1);
            return $this->db->get($this->refund)->row()->refund_amount;
        }
    }
    
    function tally($results = array(), $date){
        
        if( empty( $results ) || empty( $date ) ) :
            return '';
        endif;
        $d = explode("-", $date);
        $date = $d[2].'-'.$d[1].'-'.$d[0];
        
        $user_data = get_user_data();
        $now     = date("Y-m-d H:i:s"); 
        
        // Drop prev. Today records
        $this->db->where('DATE(`date`) = "'. $date .'"');
        $this->db->delete( TBL_TALLY );
        
        // Get Last ID
        $this->db->limit(1);
        $this->db->select('ID');
        $this->db->order_by('Transaction_id', 'DESC');
        $q = $this->db->get_where( TBL_TALLY, array() );
        $id = $q->row_array()['ID'];
        
        $id = !empty( $id ) ? intval(substr($id, 1)) : 0; 
        ++$id;
        
        $qry = 'INSERT INTO `'. TBL_TALLY .'` (`ID`, `date`, `VOUCHERTYPE`, `VOUCHERNUMBER`, `LEDGERNAME`, `LEDGERAMOUNT`, `AMTTYPE`, `NARRATION`, `created_on`, `created_by`) VALUES ';
        foreach( $results as $k => $result ) :
            $is_mode = !empty( $result['name'] ) ? FALSE : TRUE;
            $qry .= '("F'. fill_digits($id, 7) .'", "'. $date .':000", "Receipt", "B'. fill_digits($id, 3) .'", "'. ( $is_mode ? $result[0] : $result['name'] ) .'", "'. ( $is_mode ? $result[1] : $result['amount'] ) .'", "'. ( $is_mode ? 'Dr' : 'Cr' ) .'", "'. ( $is_mode ? $result[0] . ' Amount' : 'Receipt No Start From '.$result['rec_no'] ) .'", "'. $now .'", "'. $user_data['user_id'] .'"),';
        endforeach;
        
        //echo $qry; die;
        $this->db->query( substr($qry, 0, -1).';' );
        
    }

}
?>