<body bgcolor="#ffffff"  topmargin=0>
<?php include("../../include/dbconnect.php");

$startdt = us($stdt);
$enddt = us($edt);



$linecnt=0;

$fresult=mysqli_query($conn,"select name from company where compcode = '$compcode' ");
$frow = mysqli_fetch_row($fresult);
$sname = $frow[0];
?>
<table width=750  border=0>
<thead>
<tr><td align ="center" colspan="100%"><font face="Arial, Helvetica, sans-serif" color="#000066" size="+1"><? echo($sname);?></td></tr>
<tr><td align ="center" colspan="100%"><font face="Arial, Helvetica, sans-serif" color="#006699">Stock Register from the period &nbsp;<?echo($stdt);?>&nbsp;to&nbsp;<?echo($edt);?></td></tr>
<tr><td colspan="100%"><hr width="100%"></td></tr>

	<tr bgcolor="#EDCF76">
	<td  align = "left"><font  face="Arial, Helvetica, sans-serif">Date</td>
	<td  align = "right"><font  face="Arial, Helvetica, sans-serif">Opening</td>
	<td  align = "right"><font  face="Arial, Helvetica, sans-serif">Inv No</td>
	<td align = "left"><font  face="Arial, Helvetica, sans-serif">Buyer_Name</td>	
	<td  align = "right"><font  face="Arial, Helvetica, sans-serif">In Qty</td>

	<td align = "right"><font  face="Arial, Helvetica, sans-serif">Out Qty</td>
	<td align = "right"><font  face="Arial, Helvetica, sans-serif">Conversion</td>
	<td align = "right"><font  face="Arial, Helvetica, sans-serif">Damage</td>        
	<td align = "right"><font  face="Arial, Helvetica, sans-serif">Closing</td>
	<tr><td colspan="100%"><hr width="100%"></td></tr> 
</thead>
<tbody>
<?
if($reporttype == 'Detailed')
{
  for($ind=0;$ind<count($prodcode);$ind++)
  {
	  $opening=0;
  	  $check = 'F';
	  $result3 = mysqli_query($conn,"select sum(inqty)-sum(outqty) from stktrans where  acctcode = '$prodcode[$ind]' and date < '$startdt'     order by acctcode,date,outqty,record") or die(mysqli_error());
	  $row3=mysqli_fetch_row($result3);
	  $opening=$opening+$row3[0];
		 
	  $kresult = mysqli_query($conn,"select prodname from product where  prodcode = '$prodcode[$ind]' ");
	  $krow = mysqli_fetch_row($kresult);
	  $prodname= $krow[0];
  

		 
	  ?>
	  <tr>
	  <td align = "left" colspan="100%"> <font face="Arial, Helvetica, sans-serif"  color="#0000CC"><b><?echo($prodname);?></b></td></tr>
	  <?
	  
	   $tinqty=0;
	   $toutqty=0;
	   $tconversion = 0;
	   $tdamage = 0;
	   
	 
	  $result = mysqli_query($conn,"select date,invno,acctcode,inqty,outqty,bcode,record,remarks,transact_type from stktrans where  acctcode = '$prodcode[$ind]' && date >= '$startdt' && date <= '$enddt'   order by acctcode,date,outqty");
	  while($row=mysqli_fetch_row($result))
	  {
		  $date = british($row[0]);
		  $invno = $row[1];
		  $acctcode=$row[2];
		  $type = $row[8];		  
		  $inqty=$row[3];
		  $outqty = $row[4];
		  $bcode=$row[5];
		  $remarks = $row[7];
		  $closing = $opening + $inqty -$outqty;
		  if($type == 'C')
		  {
			  $conversion = $outqty;
			  $outqty =0;	
			  $damage=0;	  
		  	  $tconversion = $tconversion +$conversion;
		  }
		  if($type == 'D')
		  {
			  $damage = $outqty;
			  $outqty = 0;
			  $conversion=0;
			  $tdamage = $tdamage + $damage;
		  }
		  $tinqty=$tinqty+$inqty;
		  $toutqty = $toutqty+$outqty;

  
		  
	  	  if($bcode != '')
		  {
			  
			$cresult = mysqli_query($conn,"select name from buy where bcode = '$bcode' ");
			$crow=mysqli_fetch_row($cresult);
			$bname = $crow[0];
		  }
		  else
		  {
			  $bname = $remarks;
		  }
	  
		  if($outqty==0)
			  {$outqty='0';}
		  if($inqty==0)
			  {$inqty='0';}
	  
		  ?>   
		  <tr  bgcolor="#F0E38C">
		  <td width =30  align = "left"><font  face="Arial, Helvetica, sans-serif"><?echo($date);?></td>
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($opening,2));?></td>
		  <td width =50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(ltrim($invno));?></td>
		  <td width=160 align = "left"><font  face="Arial, Helvetica, sans-serif"><?echo(ltrim($bname));?></td>
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($inqty);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($outqty);?></td>          
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($conversion);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($damage);?></td>          
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($closing);?></td>
		  
		  </tr>
		  
					  
		  <?  
		  $check = 'T';
		  $opening = $closing;	 
		  $conversion=0;
		  $damage =0; 
	  }
	  
if($check == 'F')
{
	if($opening >0)
	{
		$closing = $opening;
		?>
		  <tr  bgcolor="#F0E38C">
		  <td width =30  align = "left"><font  face="Arial, Helvetica, sans-serif"><?echo($date);?></td>
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($opening,2));?></td>
		  <td width =50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(ltrim($invno));?></td>
		  <td width=160 align = "left"><font  face="Arial, Helvetica, sans-serif"><?echo(ltrim('No Transactions'));?></td>
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(0);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(0);?></td>          
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($conversion);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($damage);?></td>          
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($closing);?></td>
          <?	
		  $opening=0;
		  $closing=0;	
	}
}
	  ?>
	  <tr>
	  <td align = "right" colspan="5"><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($tinqty,2));?></td>
	  <td align = "right" ><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($toutqty,2));?></td>
	  <td align = "right" ><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($tconversion,2));?></td>
	  <td align = "right" ><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($tdamage,2));?></td>            
      </tr>
  
	  <?
  
			   
  }
}
elseif($reporttype == 'Summarised' )
{
  for($ind=0;$ind<count($prodcode);$ind++)
  {
	  $opening=0;
  
	  $result3 = mysqli_query($conn,"select sum(inqty)-sum(outqty) from stktrans where  acctcode = '$prodcode[$ind]' and  date < '$startdt'     ") or die(mysqli_error());
	  $row3=mysqli_fetch_row($result3);
	  $opening=$opening+$row3[0];
		 
	  $kresult = mysqli_query($conn,"select prodname from product where  prodcode = '$prodcode[$ind]' ");
	  $krow = mysqli_fetch_row($kresult);
	  $prodname= $krow[0];
	  $result = mysqli_query($conn,"select sum(inqty),sum(outqty) from stktrans where compcode = '$compcode' && acctcode = '$prodcode[$ind]' && date >= '$startdt' && date <= '$enddt' and (transact_type != 'C' and transact_type !='D')  ");
	  $row=mysqli_fetch_row($result);
		  $inqty=$row[0];
		  $outqty = $row[1];

	  $result = mysqli_query($conn,"select sum(outqty) from stktrans where compcode = '$compcode' && acctcode = '$prodcode[$ind]' && date >= '$startdt' && date <= '$enddt' and (transact_type = 'C' )  ");
	  $row=mysqli_fetch_row($result);
		  $conversion=$row[0];


	  $result = mysqli_query($conn,"select sum(outqty) from stktrans where compcode = '$compcode' && acctcode = '$prodcode[$ind]' && date >= '$startdt' && date <= '$enddt' and (transact_type = 'D' )  ");
	  $row=mysqli_fetch_row($result);
		  $damage=$row[0];
		  
		  
		  $closing = $opening +$inqty - ($outqty+$conversion+$damage);	  

	//	if($closing>0)
	//	{		
	  ?>
	  <tr>
	  <td align = "left" colspan="100%"> <font face="Arial, Helvetica, sans-serif"  color="#0000CC"><b><?echo($prodname);?></b></td></tr>
 
		  <tr  bgcolor="#F0E38C">
		  <td width =30  align = "left"></td>
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo(number_format($opening,2));?></td>
		  <td width =50 align = "right"></td>
		  <td width=160 align = "left"></td>
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($inqty);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($outqty);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($conversion);?></td>
		  <td width=50  align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($damage);?></td>                    
		  <td width=50 align = "right"><font  face="Arial, Helvetica, sans-serif"><?echo($closing);?></td>
		  
		  </tr>
		  
		  
		  <?  
	//	}
		  $opening = 0;
		  $inqty =0;
		  $outqty =0;
		  $closing=0;	  
	  
  
  
			   
  }
}
?>
</tbody>
	</table> 



<?



?>