<?php
$PageSecurity = 11;
?>
<script language="javascript" type="text/javascript">

function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
</script>
<h2 align="center"><strong>Invoice Register</strong></h2>
<form method="post" action="">
<table width="100%" border="0" cellpadding="2" cellspacing="1">
  <tr>
    <td><table width="60%" border="0" cellspacing="1" cellpadding="2" align="center">
      <tr>
        <th width="28%" ><strong>BRANCH</strong></th>
        <td width="72%" ><select name="site" id="site" style="width:107px">
            <option value="">Select Site</option>
            <?php 
			if($_SESSION['AssignedSites']!='0'){ 
		$site_sql = "SELECT prefix,loccode, locationname from locations where loccode in (".$_SESSION['AssignedSites'].")";}
		else
		{$site_sql = "SELECT prefix,loccode, locationname from locations";}
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
			$p='';
			if($site_row['prefix'] == $_POST['site'])	
			$p="selected";
		?>
            <option value="<?php echo $site_row['prefix']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
            <?php } ?>
          </select>        </td>
      </tr>
      <tr>
        <th><strong>CLIENT</strong></th>
        <td><select name="client" id="client" style="width:107px">
            <option value="">Select</option>
            <?php 
		 $client_sql = "SELECT id,client_code,c_name FROM clientmaster";
		$client_result = DB_query($client_sql,$db);
		while($client_row = DB_fetch_array($client_result))
		{
			$p='';
			if($_REQUEST['client']==$client_row['client_code'])	{
			$p="selected";}
		
		?>
            <option value="<?php echo $client_row['client_code']; ?>" <?php echo $p;?>><?php echo $client_row['c_name']; ?></option>
            <?php } ?>
          </select>        </td>
      </tr>
      <tr>
        <th><strong> DATE FROM : </strong></th>
        <td><input type="text" name="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker" size="20"></td>
      </tr>
      <tr>
        <th><strong> DATE TO : </strong></th>
        <td><input type="text" name="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker" size="20"></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td align="center"><table width="60%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr>
    <th><strong>ITEM MAIN GROUP </strong></th>
    <td>
	<select name="main_group" id="main_group" onchange="get_itemsubgroup(this.value)" style="width:107px">
	<option value="0">Select</option>
	<?php 
		$main_cat_sql=DB_query("select * from stockmaincategory where stockmaincategoryid ='FG'",$db);
		while($row1=DB_fetch_array($main_cat_sql)){
	?>  
	<option value="<?php echo $row1['stockmaincategoryid'];?>"<?php echo($_REQUEST['main_group']==$row1['stockmaincategoryid']?'selected':'')?>><?php echo $row1['stockmaincategoryname'];?></option>
	<?php }?>
	</select>	</td>
  </tr>
  <tr>
    <th><strong>ITEM SUB GROUP </strong></th>
    <td>
	<div id="sub_id">
	<select name="sub_group" id="sub_group" onchange="get_item(this.value)" style="width:107px">
	<option value="0">Select</option>
	<?php 
	$sub_cat_sql=DB_query("select categoryid,categorydescription from stockcategory where stockmaincategory='FG'",$db);
	$c2=0;
	while($row2=DB_fetch_array($sub_cat_sql)){	
	?>
	<option value="<?php echo $row2['categoryid'];?>"<?php echo($_REQUEST['sub_group']==$row2['categoryid']?'selected':'')?>><?php echo $row2['categorydescription'];?></option>
	<?php }?>
	</select></div>	</td> 
  </tr>
  <tr>
    <th><strong>ITEM</strong></th>
    <td><div id="itemid">
	<div style="overflow:auto; height:150px; width:300px; border:1px solid; " >
	<?php 
	$item_sql=DB_query("select StockID,item_name from item_master where CategoryID='FG'",$db);
	$c3=0;
	while($row3=DB_fetch_array($item_sql))
	{ 
		$c='';
		if($_REQUEST['item'][$c3]==$row3['StockID'])
		{$c="checked";}
	?>
	<input type="checkbox" name="item[<?php echo $c3;?>]" id="item_<?php echo $c3;?>" value="<?php echo $row3['StockID'];?>" <?php echo $c;?>/><?php echo $row3['StockID']." - ".$row3['item_name'];?><br />
	<?php 	
	$c3++;
	}
	?>
	</div>
		
	</div></td>
  </tr>
</table>
      <p align="center">
        <input type="submit" name="view" value="View">
	<input type="submit" name="print" value="Print">
    <input type="submit" name="cancel" value="Cancel"></p></td>
  </tr>
</table>
</form>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
    <tr>
      <th scope="col">INVOICE NO. </th>
      <th scope="col">INVOICE DATE </th>
      <th scope="col">CLIENT</th>
      <th scope="col">TOTAL QUANTITY </th>
      </tr>
<?php 
if($_POST['view'])
{
	$fordate = $_POST['from_date'];
	$todate = $_POST['to_date'];
	$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;
	$filter = array();
	
	if(isset($_REQUEST['site']) && $_REQUEST['site'] != '')
  	$filter[]="invoice.branch_name='".$_REQUEST['site']."' ";
	if(isset($_REQUEST['client']) && $_REQUEST['client'] != '')
	$filter[]="invoice.client_code='".$_REQUEST['client']."' ";
	if(isset($_REQUEST['from_date']) && $_REQUEST['from_date'] != '' && isset($_REQUEST['to_date']) && $_REQUEST['to_date'] != '')
 	$filter[]="invoice.invoice_date between '".$fdate."' and '".$toyrdate."'  ";
	
	$itemArray=array();
	if($_REQUEST['item']!=''){
	$itemArray=$_REQUEST['item'];
	$itemString="'".implode("','",$itemArray)."'"; }
 
 	if($_REQUEST['main_group'] != '0')
	{
		if($_REQUEST['sub_group'] != '0')
		{
			if($_REQUEST['item'] != '')
			$filter[]="invoice_details.ItemCode in (".$itemString.")";
			else
			$filter[]="invoice_details.ItemCode LIKE '".$_REQUEST['sub_group']."%' ";
		}
		else if($_REQUEST['item'] != '')
		{$filter[]="invoice_details.ItemCode in (".$itemString.")"; }
		else
		{$filter[]="invoice_details.ItemCode LIKE '".$_REQUEST['main_group']."%'  "; }
	}
	else 
	if($_REQUEST['sub_group'] != '0')
	{
		if($_REQUEST['item'] != '')
		$filter[]="invoice_details.ItemCode in (".$itemString.")";
		else
		$filter[]="invoice_details.ItemCode LIKE '".$_REQUEST['sub_group']."%' ";
	}
	else
	if($_REQUEST['item'] != '')
	{$filter[]="invoice_details.ItemCode in (".$itemString.")";}
	
	if($filter != '')
	{
		/*if($_REQUEST['site']!=''){$filter1 = implode(" and ",$filter)." group by invoice_details.invoiceid";}
		else
		{*/$filter1 = implode(" and ",$filter);//}
	}
	
	if(isset($_REQUEST['site'])){$sql1 = "select invoice.*, invoice_details.*, sum(Quantity)
	from invoice inner join invoice_details on(invoice.invoice_id = invoice_details.invoiceid) where $filter1"; }
	else{
	$sql1 = "select invoice.*, invoice_details.*
	from invoice inner join invoice_details on(invoice.invoice_id = invoice_details.invoiceid) where $filter1"; }
	
	echo $sql1;
	
	$result1 = DB_query($sql1,$db);
	$k=1;
	if(DB_num_rows($result1)>0){
	while($row1=DB_fetch_array($result1))
	{ ?>
	<tr bgcolor="#FFFFFF"> 
      <td><?php echo $row1['invoice_code']."-".$row1['invoice_number'];?></td>  
      <td><?php echo ConvertSQLDate($row1['invoice_date']);?></td>
      <td><?php 
	  $clientname=$s->getField('clientmaster','client_code',$row1['client_code'],'c_name');
	  echo $clientname;?></td>
      <td><?php 
	  if($_REQUEST['item']!='')
	  {echo $row1['Quantity'];}
	  else if($_REQUEST['sub_group']!='0' || $_REQUEST['main_group']!='0')
	  {echo $row1['Quantity'];}
	  else
	  {echo $row1['sum(Quantity)'];}?></td>
    </tr>
	<?php $k++;
	} }
	else
	{?>
	<tr>
	<td colspan="4"><font color="#FF0000"><strong>No Record Found</strong></font></td>
	</tr>
	<?php 
	}
} ?>
  </table>