<?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 showItems(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?partName="+str,true);
xmlhttp.send();
}



</script>
<h2 align="center"><strong>Trading Sales Register</strong></h2>
<form method="post" action="">
<table width="65%" border="0" cellpadding="2" cellspacing="1" align="center">
  <tr>
    <td align="center"><table width="100%" border="0" cellspacing="1" cellpadding="2" >
      <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 mst_companyid='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";}
		else
		{
		$site_sql = "SELECT prefix,loccode, locationname from locations  where mst_companyid='".$_SESSION['ACGCompanyId']."'";
		}
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
		?>
            <option value="<?php echo $site_row['prefix']; ?>" <?php echo ($_REQUEST['site']==$site_row['prefix']?'selected':'')?>><?php echo $site_row['locationname']; ?></option>
            <?php } ?>
          </select>        </td>
      </tr>
      <tr>
        <th><strong>Party</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['id'])	{
			$p="selected";}
		
		?>
            <option value="<?php echo $client_row['id']; ?>" <?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>
      <tr>
    <th><strong>Enter Part Name Of Item</strong></th>
    <td>
    <input type="text" name="itemName" value="" onkeyup="showItems(this.value)" />
	</td>
  </tr>
    </table></td>
  </tr>
  <tr>
    <td align="center"><table width="70%" border="0" cellspacing="1" cellpadding="2" align="center">
  
  
  <tr>
    <th><strong>Items</strong></th>
    <td><div id="itemid">
	<div style="overflow:auto; height:150px; width:450px; 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['id'];?>" <?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>
<p align="right"><a href="saleregisterexport.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>">Export</a></p>
<div style="overflow: auto; width: 700px;">
	<table width="600px;" border="0" cellspacing="0" cellpadding="0"> 
  
  <tr>
    <td><table width="100%" border="0" cellspacing="1" cellpadding="0" style="border:1px solid #ccc; background-color:#FFF">
    <tr>
    	      <td><table width="100%" border="0" cellspacing="0" cellpadding="5">
    	        <tr>
                <?php
                if($_POST['view'])
{
	$_SESSION['client']=$_REQUEST['client'];
	
	$_SESSION['item']=$_REQUEST['item'];
	
    ?>
    	          <?php if($_REQUEST['client']!=''){?><td width="10%"><strong>Party Name</strong></td>
    	          <td width="22%"><?php $p=DB_query("Select c_name from clientmaster where id='".$_REQUEST['client']."'",$db);
    	          	$pRes=DB_fetch_array($p); echo $pRes['c_name'];
    	          	?></td><?php } ?>
    	          	<?php if($_REQUEST['item']!='' ){?><?php $itemArray=$_REQUEST['item'];
	                    $itemString1="'".implode("','",$itemArray)."'"; 
					$itemstr=explode(',',$itemString1);
					
					if(count($itemstr)<=1) {?><td width="10%"><strong>Item Name</strong></td><td><?php 
    	        	     $itemArray=$_REQUEST['item'];
	                    $itemString1="'".implode("','",$itemArray)."'"; 
					$itemstr=explode(',',$itemString1);
					
					if(count($itemstr)<=1) {
					  	$pp=DB_query("select item_name from item_master where StockID=$itemString1",$db);
    	          	$pResp=DB_fetch_array($pp); echo $pResp['item_name'];
						
					}
    	          	?>
    	          	</td><?php } }?>
                   <?php } ?>
  	          </tr>
  	        </table></td>
   	      </tr>
    	    <tr>
    	      <td><table width="100%" border="0" cellspacing="1" cellpadding="1">
    	        <tr>
	  <th scope="col">Invoice No. </th>
	  <th scope="col">Invoice Date</th>
	  <th scope="col">Party Name</th>
	  <th scope="col">Basic Amt</th>
	  <th scope="col">Discount</th>
	  <th scope="col">Pkg Charges<br /></th>
	  <th scope="col">Excise Duty Amt </th>
	  <th scope="col">E-Cess</th>
	  <th scope="col">SAH- E Cess</th>
	  <th scope="col">Tax Amt</th>
	  <th scope="col">Freight Charges</th>
	  <th scope="col">Other Amt</th>
	  <th scope="col">Gross Amt</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();	
	$itemArray=array();
	if($_REQUEST['item']!=''){
	$itemArray=$_REQUEST['item'];
	$itemString="'".implode("','",$itemArray)."'"; }
	if(isset($_REQUEST['site']) && $_REQUEST['site'] != '')
  	$filter[]="invoice.branch_name='".$_REQUEST['site']."' ";
	if(isset($_REQUEST['client']) && $_REQUEST['client'] != '')
	$filter[]="invoice.client_id='".$_REQUEST['client']."' ";
	if($_REQUEST['from_date'] != '' && $_REQUEST['to_date'] != '')
 	$filter[]="invoice.invoiceDate between '".$fdate."' and '".$toyrdate."'  ";
	if($_REQUEST['item']!='')
	$filter[]="invoice_details.ItemCode in (".$itemString.")";	
	if($filter != '')
	{
		$filter1 = implode(" and ",$filter);
		
		if($_REQUEST['site']!='' || $_REQUEST['client']!='' || $_REQUEST['item']!='')
		{
		      $sql1 = "select invoice.*, invoice_details.*
			from invoice inner join invoice_details on(invoice.invoice_id = invoice_details.invoiceid) where $filter1 group by invoice.invoice_id";
			$_SESSION['data1']=$sql1;
		}
		else
		{
			$sql1 = "select invoice.*, invoice_details.*
			from invoice inner join invoice_details on(invoice.invoice_id = invoice_details.invoiceid) "; 
			$_SESSION['data1']=$sql1;
		}
	}
	//echo $sql1;
	}
   else {$sql1 = "select invoice.*, invoice_details.*
			from invoice inner join invoice_details on(invoice.invoice_id = invoice_details.invoiceid) group by invoice.invoice_id";
   $_SESSION['data1']=$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['invoiceNo'];?></td>  
	  <td><?php echo $row1['invoiceDate'];?></td>
	  <td><?php 
	  echo $s->getField('clientmaster','id',$row1['client_id'],'c_name');
	  //echo $row1['client_id'];?></td>
	  <td><?php 
	  if($_REQUEST['item']!=''){echo $row1['Amount'];}else
	  {echo $row1['basicAmount'];}?></td>
	  <td><?php 
	  if($_REQUEST['item']!=''){echo $row1['discount'];}else
	  {echo $row1['basicAmount']-$row1['totalDiscountedPrice'];}?></td>
	  <td><?php echo $row1['packagingCharges'];?></td>
	  <td><?php echo $row1['exciseValue'];?></td>
	  <td><?php echo $row1['ECessValue'];?></td>
	  <td><?php echo $row1['SAHCessValue'];?></td>
	  <td><?php echo $row1['VatCstValue'];?></td>
	  <td><?php echo $row1['freightValue'];?></td>
	  <td><?php echo $row1['additionalCharges'];?></td>
	  <td><?php echo $row1['grandTotal'];?></td>
   </tr> <?php $k++; $t++;
	}
?>
               <?php         
  	          	$t=0;
			   $totalQty=0;
			   $totalWeight=0;
			   $totalPacketNo=0;
			   $totalPieces=0;
			   $totalPieces1=0;$totalPieces2=0;$totalPieces3=0;$totalPieces4=0;$totalPieces5=0;$totalPieces6=0;
			   $result12=DB_query($sql1,$db);
			   while($row1=DB_fetch_array($result12))
                 {
                if($_REQUEST['item']!=''){ 
  	          	$totalQty=$totalQty+$row1['Amount'];
				}
				else {
						$totalQty=$totalQty+$row1['basicAmount'];
				}
  	          	$totalWeight=$totalWeight+$row1['discount'];
				$totalPacketNo=$totalPacketNo+$row1['packagingCharges'];
				$totalPieces=$totalPieces+$row1['exciseValue'];
				$totalPieces1=$totalPieces1+$row1['ECessValue'];
				$totalPieces2=$totalPieces2+$row1['SAHCessValue'];
				$totalPieces3=$totalPieces3+$row1['VatCstValue'];
				$totalPieces4=$totalPieces4+$row1['freightValue'];
				$totalPieces5=$totalPieces5+$row1['additionalCharges'];
				$totalPieces6=$totalPieces6+$row1['grandTotal'];
  	          	 } ?>
    	        <tr  bgcolor="#f2f2f2">
    	          <td>&nbsp;</td>
    	          <td>&nbsp;</td>
    	          
    	          <td align="right" style="text-align:right;"><strong>Total:</strong></td>
    	          <td><strong><?php echo round($totalQty,2);?></strong></td>
    	          <td><strong><?php echo round($totalWeight,2);?></strong></td>
    	          <td><strong><?php echo round($totalPacketNo,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces1,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces2,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces3,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces4,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces5,2);?></strong></td>
    	          <td><strong><?php echo round($totalPieces6,2);?></strong></td>
  	          </tr>
<?php } 
	else
	{?>
  	        <tr><td colspan="13"><font color="#FF0000"><strong>No Record Found</strong></font></td></tr>  <?php 
	} 
?>
  	        </table></td>
</tr><//td><//tr><//table><//td><//tr></table></td></tr>
    
</table>
	
	
</div>