<?php
include_once('../excel/details1.php');

  // output headers so that the file is downloaded rather than displayed

           // header('Content-Type: text/xls; charset=utf-8');
           // header('Content-Disposition: attachment; filename=Topup_Report.csv');

// create a file pointer connected to the output stream
            //$output = fopen('php://output', 'w');

// output the column headings
            //fputcsv($output, array('RequestID', 'Date', 'Airtime Sent', 'Recipient', 'Name', 'Group', 'Status',));

// fetch the data
            /*$qryreport = mysql_query("select * from airtime_groups where customer_id = '$session_id' order by group_id ASC ");
            $rows = mysql_query("SELECT airtime_sending_records.requestid, airtime_sending_records.date_sent, airtime_sending_records.airtime_sent, airtime_sending_records.recipient, airtime_contacts.fname, airtime_groups.name, airtime_sending_records.status 
            FROM airtime_sending_records INNER JOIN airtime_contacts ON airtime_sending_records.recipient = airtime_contacts.contact INNER JOIN airtime_groups ON airtime_contacts.group_id = airtime_groups.group_id 
            WHERE airtime_sending_records.customer_id = '$session_id' ORDER BY airtime_sending_records.date_sent DESC");*/

//$rows = mysql_query("SELECT requestid, date_sent, airtime_sent, recipient, status FROM airtime_sending_records WHERE customer_id = '$session_id' ORDER BY date_sent DESC");

// loop over the rows, outputting them
        //while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
//..........................................................................................................................................................


$fileName = "Condensed_Summary_".date('Ymd') . ".xlsx";			
	header("Content-Type: application/vnd.ms-excel");
	header("Content-Disposition: attachment; filename=\"$fileName\"");


$qryreport = mysql_query("select * from airtime_groups where customer_id = '$session_id' order by group_id ASC ");
$pp=array();
	while($reportdisp=mysql_fetch_array($qryreport)) {
	$j=$j+1;
	
		$gid = $reportdisp['group_id'];					
				$getconts = mysql_query("select * from airtime_contacts where group_id = '$gid' and customer_id = '$session_id' ");
				$numtotal = mysql_num_rows($getconts);
				$getrw=mysql_fetch_array($getconts);
				$phne = $getrw['contact'];
										
				$gettotal = mysql_query("select SUM(airtime_sent) AS value_sum from airtime_sending_records inner join airtime_contacts on airtime_sending_records.recipient = airtime_contacts.contact where airtime_contacts.group_id = '$gid' ");
				$row = mysql_fetch_array($gettotal); 
				$sum = $row['value_sum'];
				
    			$FirstName = $reportdisp['name'];
    			$LastName = $numtotal;
    			$MiddleName = $sum;
    		   
    		   $k['Group']=$FirstName;
    		   $k['Total No of Contacts']=$LastName;
    		   $k['Amount of Airtime Sent (Ksh.)']=$MiddleName;
    		  
			array_push($pp,$k);

	}
	
	
	$showColoumn = false;
	if(!empty($pp)) {
	  foreach($pp as $developerInfo) {
		if(!$showColoumn) {		 
		  echo implode("\t", array_keys($developerInfo)) . "\n";
		  $showColoumn = true;
		}
		echo implode("\t", array_values($developerInfo)) . "\n";
	  }
	}

//echo json_encode($pp);
//fputcsv($output, array('FirstName', 'LastName', 'MiddleName',));
//foreach($pp as $k){
  // while ($row = mysql_fetch_assoc($pp)) fputcsv($output, $row);
//}
//fputcsv($output, array('FirstName', 'LastName', 'MiddleName',));

//while ($row = mysql_fetch_assoc($pp)) fputcsv($output, $row);//{

//.......................................................................................................................................................




exit();

?>


<?php
/*Uob_start(); session_start(); 
include_once('../dbcon.php');
include_once('../session.php');

  require_once('OLEwriter.php');
  require_once('BIFFwriter.php');
  require_once('Worksheet.php');
  require_once('Workbook.php');
  


   function HeaderingExcel($filename) {
      header("Content-type: application/vnd.ms-excel");
      header("Content-Disposition: attachment; filename=$filename" );
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
      header("Pragma: public");
      }
	  
	  // HTTP headers
HeaderingExcel('Condensed_Summary.xls');// Creating a workbook
$workbook = new excel("-");
// Creating the first worksheet
$worksheet1 =& $workbook->add_worksheet('Topup Summary');
$worksheet1->freeze_panes(1, 0);
  $worksheet1->set_column(0, 0, 25);
  $worksheet1->set_column(1, 1, 20);
  $worksheet1->set_column(1, 2, 20);
  
   $worksheet1->write_string(0,0,"Group");
   $worksheet1->write_string(0,1,"Total No. of Contacts");
   $worksheet1->write_string(0,2,"Amount of Airtime Sent (Ksh.)");


/////////////////
	
 $qryreport = mysql_query("select * from airtime_groups where customer_id = '$session_id' order by group_id ASC ");
	
	$sqlrows=mysql_num_rows($qryreport);
	$j=0;
	
	WHILE ($reportdisp=mysql_fetch_array($qryreport)) {
	$j=$j+1;
	
		$gid = $reportdisp['group_id'];					
				$getconts = mysql_query("select * from airtime_contacts where group_id = '$gid' and customer_id = '$session_id' ");
				$numtotal = mysql_num_rows($getconts);
				$getrw=mysql_fetch_array($getconts);
				$phne = $getrw['contact'];
										
				$gettotal = mysql_query("select SUM(airtime_sent) AS value_sum from airtime_sending_records inner join airtime_contacts on airtime_sending_records.recipient = airtime_contacts.contact where airtime_contacts.group_id = '$gid' ");
				$row = mysql_fetch_array($gettotal); 
				$sum = $row['value_sum'];
				
			$FirstName = $reportdisp['name'];
			$LastName = $numtotal;
			$MiddleName = $sum;
		
			
		
			 $worksheet1->write_string($j,0,"$FirstName");
			 $worksheet1->write_string($j,1,"$LastName");
			 $worksheet1->write_string($j,2,"$MiddleName");
			 
	}
	
	
	
/////////////////
  
  

  
$workbook->close();*/
?>