<?php include('includes/fun-php.php'); ?>
<form action="" method="post">
<table width="50%" border="0">
  <tr>
    <td>From Month:<input type="text" value="<?php if(isset($_REQUEST['fromDate']))echo $_REQUEST['fromDate']?>" name="fromDate" class="datepicker"/></td>
    <td>To Month:
	<input type="text" name="toDate" value="<?php if(isset($_REQUEST['toDate'])) echo $_REQUEST['toDate']?>" class="datepicker"/>
	</td>
	<td>Party:
		<?php 
  		$sql=DB_query("select * from clientmaster",$db);		
		if(!$sql)
		{
		   die('Error.'.DB_error_msg($db));
		}
		echo "<select name='client' >";
		echo "<option value='0'>----</option>";
		while($row=DB_fetch_array($sql))
		{
		   $p='';
		   if($row['id']==$_REQUEST['client'])
		   $p='selected';
		  echo "<option value='".$row['id']."' ".$p." >".$row['c_name']."</option>";
		}
		echo "</select>";?>
	</td>
    <td><input type="submit" name="search" value="Search" /></td>
  </tr>
</table>
</form>

<?php

if($_REQUEST['search'])
{
					$fordate = $_REQUEST['fromDate'];
					$todate = $_REQUEST['toDate'];
					$fromdate = explode("/", $fordate);
					$daydate = $fromdate['0'];
					$Monthdate = $fromdate['1'];
					$yeardate = $fromdate['2'];
					 $fdate = $yeardate."-".$Monthdate."-".$daydate;
					   
					$tredate   = explode("/", $todate);
					$dayda     = $tredate['0'];
					$Monthda   = $tredate['1'];
					$yearda    = $tredate['2'];
					 $toyrdate  = $yearda."-".$Monthda."-".$dayda;	


 					$client_id=$_REQUEST['client'];
 					$no_of_months=$Monthda-$Monthdate+1;
 					$no_of_months= getMonths( str_replace('/','-', ConvertSQLDate($fordate)), str_replace('/','-', ConvertSQLDate($todate)));  
					
 ?>

<table width="100%" border="0" cellpadding="4" cellspacing="3">
<tr>
<?php 
$initial_date = strtotime ( '-1 month' , strtotime($fdate) ); 
$initial_date = date ( 'Y-m-j' , $initial_date );
for($x=0;$x<$no_of_months;$x++)
{
					$initial_date = strtotime ( '+1 month' , strtotime ($initial_date) ) ;
					$initial_date = date ( 'Y-m-j' , $initial_date );
					$initial_date_brk=explode("-",$initial_date);
					  $m=$initial_date_brk[1];
					$test1=date('F',mktime(0,0,0,$m,1));
	?>
<td valign="top">
<h1>	<strong><?php // echo $test1;  echo "&nbsp;";  echo $yeardate;?></strong></h1>

<table width="100%" border="1" cellpadding="0" cellspacing="3">
 	<tr bgcolor="#666666" style="color:#FFFFFF; font-size:13px;font-weight:800">
 	<td>Branch Name</td>
    <td>Bill No. </td>
    <td>Bill Date </td>
    <td>Due Date </td>
    <td>Bill Amt </td>
	<td>Balance Amt </td> 
  </tr>
  <?php
  				$filter="1=1";
				
				if($_REQUEST['fromDate']!='' && $_REQUEST['toDate']!='')
				{
//$filter.= " and month(invoiceDate) between '".$Monthdate."' and '".$Monthda."' and year(invoiceDate)= '".$yeardate."' and month(invoiceDate)='".$m."'";
					$filter.= " and invoiceDate between '".$fdate."' and '".$toyrdate."'";
}
				if($_REQUEST['client']!='0' )
				$filter.=" and client_id='".$_REQUEST['client']."'";
				
				 
  
  
  if($_REQUEST['client']!='0')
  $sqlquery=DB_query("SELECT * FROM clientmaster where id='".$_REQUEST['client']."'",$db);
  else
    $sqlquery=DB_query("SELECT * FROM clientmaster where 1=1",$db);
  
  
 while( $sqlquery_row=DB_fetch_array($sqlquery))
 {
 	
	
	if($_REQUEST['client']!='0')
     $query= "select * from $table_name where $filter  ";
    else
      $query= "select * from $table_name where $filter and client_id='".$sqlquery_row['id']."'";
	  
	  //echo '<br>'.$query;
	$Monthdate;
	$Monthda;
	$middle="0".$Monthdate +1;
	$result=DB_query($query,$db);
	
	$numrows = DB_num_rows($result);
	if($numrows>0){
		echo '<tr><td colspan=8><h3>'.$sqlquery_row['c_name'].'</h3></td></tr>';

?> 
	
<?php 

	while($row=DB_fetch_array($result))
	{
  	
	//echo $Monthdate==$fromdatetable[1];
	if($Monthdate!='' && $Monthda!='')
	{?>
		
    <tr    >
	<td><?php  
	   $duedate=$s->getField('clientmaster','id',$row['client_id'],'payment_terms');
  
	echo $row['invoiceNo'];?></td>
	
	<td><?php  echo $s->getField('locations','loccode', $row['branch_name'],'locationname'); ?></td>
	<td><?php  echo ConvertSQLDate($row['invoiceDate']);?></td>
	<td><?php   
	
	$newdate = strtotime ( "+$duedate day" , strtotime ( $row['invoiceDate'] ) ) ;
echo $newdate = ConvertSQLDate(date ( 'Y-m-j' , $newdate ));
	 ?></td>
	<td><?php  echo $row['grandTotal']?></td>
	<td><?php  echo $row['pending_amount']?></td>

  </tr>
  
 <?php }
 
	  $apr++;
	}
	}
}	
  ?>
 
</table></td><?php }?></tr></table>
<?php } ?>
