<body background="../forms/i.jpg">
  
<script language="javascript">
function check()
{
	if(myform2.compcode.value=="")
	{
		alert("Please select Company");
		myform2.compcode.focus();
		return false;
	}	

	if(myform2.bcode.value=="0")
	{
		alert("Please Select Customer");
		myform2.bcode.focus();
		return false;
	}



	
}
</script>



<?php include("../../include/dbconnect.php");

foreach($_POST as $q=>$r)
{
	$_POST[$q] = trim($r);
}

$myCompany = $_POST['Company'];

// Get all _POST VALUES HERE..

foreach($_POST as $p=>$t)
{
	$_POST[$p] = trim($t);
}

$myCat = $_POST['Cat'];


?>
<SCRIPT LANGUAGE="JavaScript" src="../../include/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
var cal = new CalendarPopup();
</SCRIPT>
<title>Receipt</title>
<body background="images/blater01.gif" topmargin=0>
<center> 
<table border=0 cellpadding=3 cellspacing=4 width="677" bgcolor="#E6E6FF">
  <tr> 
    <td  colspan="100%"  bgcolor="#93BEE2"><font face="Verdana, Arial, Helvetica, sans-serif"><b><b>Item Issues / Receipts</b></font></td>
  </tr>



  <?

// Pull all the data from Cat master & populate it

$result= mysqli_query($conn," select distinct compcode,name from company  order by name")or die( mysqli_error() );


?>
  <form name='myform' method='POST' action='<?=$_SERVER['PHP_SELF']?>'>
    <tr > 
      <td  width="59" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Company 
        &nbsp;</b></font></td>
      <td width="148" > <select name='Company' onChange="javascript:submit();">
          <?php 
	 //echo "<option selected value=\" 0\"> Select Company";	  
      while ( $row1= mysqli_fetch_row($result))
      {
	  	if(isset($myCompany) && preg_match("/" . preg_quote($myCompany, '/') . "/", $row1[0]))
		{
			echo "<option selected value=\"$row1[0]\">$row1[1]";
	  	}
	  	else
	  	{
			echo "<option value=\"$row1[0]\">$row1[1]";
	  	}
	}
?>
      </select> </td>
    </tr>
    <tr> 
      <td width="59" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Category&nbsp;</b></font></td>
      <td> 
        <?
$stQuery = mysqli_query($conn,"select catcode,category from prodcat order by category")or die( mysqli_error() );



?>
        <select name='Cat' onChange="javascript:submit();">
          <?
echo "<option selected value=\" 0\"> Select Cat";
while($row = mysqli_fetch_row($stQuery))
{
	
	if(isset($myCat) && preg_match("/" . preg_quote($myCat, '/') . "/", $row[0]))
	{
		echo "<option selected value=\"$row[0]\">$row[1]";
	}
	else{
		echo "<option value=\"$row[0]\">$row[1]";
	}
}
?>
        </select></td>
      <?
	//$prodresult = mysqli_query($conn,"select distinct tpurreg.prodcode,product.prodname,tpurreg.record,product.packing from tpurreg,product where tpurreg.balance > 0 and compcode = '$myCompany' and product.groupcode = '$myCat' and tpurreg.prodcode = product.prodcode  order by product.prodname") or die(mysqli_error());
	
	$query= "select distinct prodcode,prodname from product where catcode =  '$myCat' ";
	$prodresult = mysqli_query($conn,$query) or die(mysqli_error());	
	
?>
      <td width="43" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Product 
        &nbsp;</b></font></td>
      <td colspan="3" ><select name='prodcode' >
        <?

	echo "<option selected value=\" 0\"> Product & Stock";
	while($prodrow = mysqli_fetch_row($prodresult))
	{
		
		$netqty = $srow[0] - $trow[0];
		$temp= $prodrow[1];
		echo "<option value=\"$prodrow[0]\">$temp";		
	}
	?>
      </select></td>
    <tr> 

      <td width="59" align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Qty 
        </b></font></td>
      <td width="148"><input type="text" onKeyPress="return event.keyCode!=13" name="qty" value=""></td>


		<td width="99" height="31" align="right" colspan="1" ><div align="right">
		  <font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Mode</font></b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">&nbsp;</font></b></font></div></td>
		<td width="125" > 
		
		<select name="purmode">
		<option value="Issue">Production(Issue)</option>
		<option  selected  value="Receipt">Receipt</option>
        		<option value="Conversion">Conversion(issue)</option>
		<option value="Damage">Damage(Issue)</option>                
		</select>		</td>



    </tr>
	<tr>
      <td align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Description        </b></font></td>
      <td colspan="100%"> 
      <input type="text" size="75"   onKeyPress="return event.keyCode!=13" name="remarks" value=""></td></tr>
</table>
<table>
</table>
 <table cellpading=0 cellspacing=10 width= 676>
<tr><td width="307" align="left"><blockquote><input type =submit name="Clear" value = "Clear" ></blockquote></td><td>
 <blockquote>  <input type =submit name="refresh" value = "Refresh" ></blockquote></td><td width="307" align="left"><blockquote><input type =submit name="add" value = "Add" ></blockquote></td>
</tr>
</table>

<?

if($Clear == 'Clear')
{
	mysqli_query($conn,"truncate purch ") or die(mysqli_error());
}

if($prodcode == '0' and $qty > 0)
{
?>
<script language="JavaScript">
alert("Select Product plase");
</script>
<?
exit;
}
if($qty >0 and $add == 'Add')
{
		if($purmode =='Issue')
		{
			$taxamt =1;
		}
		elseif($purmode == 'Receipt')
		{
			$taxamt =2;
		}
		if($purmode == 'Conversion')
		{
			$taxamt = 3;
		}
		if($purmode == 'Damage')
		{
			$taxamt = 4;
		}
		

	$result = mysqli_query($conn,"insert into purch values ( '$myCompany','$myBuyer','$prodcode','$qty','$taxamt','','$netamt','$remarks',NULL)") or die(mysqli_error());
}

?>


  </form><tr >
  


<form name='myform2' method='POST' action='entry.php' onSubmit="return check()" >

<input type="hidden" name="bcode" value="<?=$myBuyer?>">
<input type="hidden" name="compcode" value="<?=$myCompany?>">
<input type="hidden" name="taxtype" value="<?=$myTax?>">


<table width="677">
  <tr  bgcolor="#A4A4FF">

<td width="48" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Modify&nbsp;</b></font></td>
<td width="259" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Product &nbsp;</b></font></td>
<td width="71" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Qty &nbsp;</b></font></td>
<td width="73" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Mode</b></font></td>
<td width="104" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Description </b></font></td>
<?
$gtotal = 0;
$ttotal =0;
$result = mysqli_query($conn,"select * from purch ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$compcode = $row[0];
	$bcode = $row[1];
	$prodcode = $row[2];
	$qty = $row[3];
	$taxamt = $row[4];
	$remarks = $row[7];
	$record = $row[8];
		if($taxamt ==1)
		{
			$mode = 'Issue';
				if($remarks =='')
				$remarks = 'Issued for Production';			
		}
		if($taxamt == 2)
		{
			$mode = 'Receipt';
					if($remarks =='')
				$remarks = 'Receipt from Production';
		}
		if($taxamt ==3)
		{
			$mode = 'Conversion';
							if($remarks =='')
				$remarks = 'Sent for Conversion';
		}
		if($taxamt == 4)
		{
			$remarks = 'Damage';
							if($remarks =='')
				$remarks = 'Damage';
		}
			

	$prodname = mysqli_fetch_array(mysqli_query($conn,"select prodname from product where prodcode = '$prodcode'")) or die(mysqli_error());
?>
<tr  bgcolor="#D7D7FF">
<td width="48" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><a href = "peditview.php?record=<?echo($record);?>"><?echo($record);?></a></b></font></td>
<td width="259" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$prodname['prodname']?></b></font></td>

<td width="71" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$qty?></b></font></td>
<td width="73" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$mode;?></b></font></td>
<td width="104" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$remarks?></b></font></td>
</tr>

<?
}


?>
</table>
<table width="682">

		<?
			$aresult = mysqli_query($conn,"select account_year from branches ") or die(mysqli_error());
			$arow = mysqli_fetch_row($aresult);
			$account_year = $arow[0];
			
			$aresult = mysqli_query($conn,"select max(invno) from stktrans where compcode = '$myCompany' and account_year = '$account_year' and transact_type != 'S' and transact_type !='P' and transact_type != 'SR' ") or die(mysqli_error());
			$arow = mysqli_fetch_row($aresult);
			$bno = $arow[0]+1;			
			

		?>

		<tr > 
		<td width="99" height="31" align="right" ><div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Issue/Receipt ID No
		  &nbsp;</font></b></font></div></td>
		<td width="125" > <input type="text"  size="10" onKeyPress="return event.keyCode!=13" name="bill_no" value= '<?=$bno?>' >		</td>

	<td width="61"  align="left"><div align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b><font color="#003366">Bill 
	  Date &nbsp;</font></b></font> </div></td>
		<td width="159" class="small_site_font_black" > 
		<?
		
		$today=strftime ("%d/%m/%Y", mktime(0,0,0,date("m"),date("d"),date("Y"))); ?>
		<input name="today" type="text" onKeyPress="return event.keyCode!=13" class="text_box_button" value="<?=$today?>" size="10"   > 
		<a href="#" onClick="cal.select(document.myform2.today,'anchor1','dd/MM/yyyy'); return false;"name="anchor1" id="anchor1"><img  src="../../images/cal.gif" border="0"></a>		</td>
</tr>

  <tr> 
    <td align="right" colspan="3" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Confirm</b></font></td>
    <td> <input type="radio"    onKeyPress="return event.keyCode!=13"   name="cnf" value = "No" checked> <font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>No 
    </b></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio"    onKeyPress="return event.keyCode!=13"   name="cnf" value = "Yes"> <font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Yes
      </b></font>&nbsp;</td>
  </tr> 
  <table cellpading=0 cellspacing=10 width= 50>
    <tr> 
      <td> <blockquote> <input type =submit name="submit" value = "Submit"></blockquote></td>
  </table>
</table>

<font size=1> 
<br><br><font size=1><center><a href= "../../index.html">Return to index page</center></font>
</font> 
</form>