<?php
session_start();
include 'db_connect.php';
include 'db_table_select.php';


$today_date=date("Y-m-d");
$sort_key=bookid;
$from_date = $today_date;
$to_date = $today_date;

if(!empty($_POST['repfrom']))
{
$temp_from_date = $_POST['repfrom'];
list($day, $month, $year) = explode('/', $temp_from_date);
$from_date ="$year-$month-$day";
}
if(!empty($_POST['repto']))
{
$temp_to_date = $_POST['repto'];
list($day, $month, $year) = explode('/', $temp_to_date);	

$to_date ="$year-$month-$day";

}

if($_POST['reportviewtype']=="led_det")
{
	
 $sqlbook="select  $billreg.srno as 'SR NO',DATE_FORMAT($billreg.billdate,'%d/%m/%Y') as 'BILL DATE',$billreg.billto as 'BILLED TO',$billreg.billno as 'BILL NO','SALES' as VCHTYPE,FORMAT($billreg.total,2) as 'DEBIT' ,FORMAT($billreg.paid,2) as 'CREDIT',vchreg.vchno as 'VCH NO',vchreg.bill_no as 'CHEQUE NO',DATE_FORMAT(vchreg.invdate,'%d/%m/%Y') as 'CQ. DATE', vchreg.account as ACCOUNT, $billreg.expst as 'STATUS' 
	  from $billreg  
	  INNER JOIN commst ON $billreg.billto=commst.compid 
	  INNER JOIN vchdetails ON $billreg.billno=vchdetails.billno
	  INNER JOIN vchreg ON vchreg.vchno=vchdetails.vchno
	  where 
	  billdate between '$from_date' and '$to_date' 
	  and $billreg.cmp='$_SESSION[SESS_CMPC]' 
	  and $billreg.fny = '$_SESSION[SESS_FNY]' 
	  and billto = '$_POST[billedto]' 
	  order by CONVERT(srno, SIGNED INTEGER) ";
		
		 //$output[error] = $sqlbook;
	if($resultbook = mysqli_query($con,$sqlbook))
	   {
		   
		while($sqlbook_row=mysqli_fetch_assoc($resultbook))
			{
				foreach ($sqlbook_row as $key=>$value) {	
						
					/* 
					 * start
					 * getting field name of table
					*/	
					$outputRow[$key]['value'] = $value;
					if($key == 'BILLED TO')
	               {
	              addFooterCounter($outputRow,$key);
	               }
					
					
				$totalKeyArray = array('DEBIT','CREDIT');
				if(in_array($key,$totalKeyArray))
				addFooterTotal($outputRow,$key);
				}
				$output['rows'][][] = $outputRow;	
			}
		
	}
	
	
	
 echo json_encode($output);

	
	
		
}


else if($_POST['reportviewtype']=="bill_info")
{
	 
	
 $sqlbook="select bookid as 'BOOKING ID' , billedto as 'BILLED TO', billno as 'BILLNO' ,  tamount as  'TotalAmt'  from $bookingreg where  bookid in ($_POST[billedto])   ";
		
		 //$output[error] = $sqlbook;
	if($resultbook = mysqli_query($con,$sqlbook))
	   {
		   
		while($sqlbook_row=mysqli_fetch_assoc($resultbook))
			{
				foreach ($sqlbook_row as $key=>$value) {	
						
					/* 
					 * start
					 * getting field name of table
					*/	
					$outputRow[$key]['value'] = $value;
					if($key == 'BOOKING ID')
	               {
	              addFooterCounter($outputRow,$key);
				  
				  addClassName($outputRow,$key,'links');
	              $outputRow[$key]['links'] = "carrental_entry.php?bookid=$value,870,660";
	               }
					
					
				$totalKeyArray = array('TotalAmt');
				if(in_array($key,$totalKeyArray))
				addFooterTotal($outputRow,$key);
				}
				$output['rows'][][] = $outputRow;	
			}
		
	}
	
	
	
 echo json_encode($output);

	
	
		
}



else if($_POST['reportviewtype']=="book_info")
{
	 
	
 $sqlbook="select bookid as 'BOOKING ID' , ref as 'B REF No', billedto as 'BILLED TO', billno as 'BILLNO' ,  tamount as  'TotalAmt'  from $bookingreg where  ref in ($_POST[billedto])   ";
		
		 //$output[error] = $sqlbook;
	if($resultbook = mysqli_query($con,$sqlbook))
	   {
		   
		while($sqlbook_row=mysqli_fetch_assoc($resultbook))
			{
				foreach ($sqlbook_row as $key=>$value) {	
						
					/* 
					 * start
					 * getting field name of table
					*/	
					$outputRow[$key]['value'] = $value;
					if($key == 'BOOKING ID')
	               {
	              addFooterCounter($outputRow,$key);
				  
				  addClassName($outputRow,$key,'links');
	              $outputRow[$key]['links'] = "carrental_entry.php?bookid=$value,870,660";
	               }
					
					
				$totalKeyArray = array('TotalAmt');
				if(in_array($key,$totalKeyArray))
				addFooterTotal($outputRow,$key);
				}
				$output['rows'][][] = $outputRow;	
			}
		
	}
	
	
	
 echo json_encode($output);

	
	
		
}




function addClassName(&$outputRow,$key=null,$class=null)
{
	if($class==null)
	{
		if($key!=null)
		{
			if(is_numeric(str_replace(',', '', $outputRow[$key]['value'])))
			$outputRow[$key]['tdClass'] = 'number';
		}
	}
	else
	{	
		//if(!empty($outputRow[$key]['tdClass']))
		//{
			//$class = ' '.$class;
		//}
		$className = str_replace(' ', '', $key);

		$outputRow[$key]['tdClass'] = $className;
	}
}


function addFooterCounter(&$outputRow,$key=null)
{
	if($key!=null)
	$outputRow[$key]['count'] = 'yes';
}

function addFooterTotal(&$outputRow,$key=null)
{
	//if($key!=null)
	$outputRow[$key]['total'] = 'yes';
}

function addTotalRows(&$output,$totalrows)
{
	$output['total_rows'] =$totalrows;
}


mysqli_close($con);
?>