<?php include("../../include/dbconnect.php");?>
<title>Sales ReturnEntry</title>
<body background = 'blater01.gif'  topmargin=0>
<center>
<table cellpadding=0 cellspacing=4 width=603>
<tr>
<td style="padding:3px;" colspan=2 bgcolor="#93BEE2"><font ><b>Sales ReturnEntry</b></font></td>
</tr><tr valign="top"><td colspan=2>
<font >&nbsp;</font>
</td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript" src="../../include/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
var cal = new CalendarPopup();
</SCRIPT>
<script type="text/javascript" src="../../include/datetimepicker.js"></script>
<style type="text/css">
input.rightAligned {
text-align: right;
}
</style>
<?php 


foreach($_POST as $a=>$b)
{
	$_POST[$a] = trim($b);
}

$myBuyer = $_POST['Buyer'];

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

$myGroup = $_POST['Group'];


foreach($_POST as $a=>$b)
{
	$_POST[$a] = trim($b);
}

$myinvno = $_POST['invno'];


foreach($_POST as $h=>$i)
{
	$_POST[$h] = trim($i);
}

$myTax = $_POST['Tax'];


foreach($_POST as $f=>$g)
{
	$_POST[$f] = trim($g);
}

$myProd = $_POST['Prod'];

?>


<form name='myForm02' method='POST' action='<?=$_SERVER['PHP_SELF']?>'>
<table border=0 cellpadding=3 cellspacing=4 width="603" bgcolor="#E6E6FF">


<tr>

<td width="201" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Customer</b></font></td>
<td>
<?
// Pull all Customers from Customer  based on Selected Group id and populate it.
$sBuyQuery = mysqli_query($conn,"SELECT distinct bcode,name FROM   buy where buyertype!='Supplier' and active = 'Yes' order by name ");
?>
 <SELECT name='Buyer' onChange="javascript:submit();">
<?
echo "<option selected value=\" 0\"> Select Supplier";
while($row = mysqli_fetch_row($sBuyQuery))
{
	if(isset($myBuyer) && preg_match("/" . preg_quote($myBuyer, '/') . "/", $row[0]))
	{
		echo "<option selected value=\"$row[0]\">$row[1]";
	}
	else{
		echo "<option value=\"$row[0]\">$row[1]";
	}
}
?>
</SELECT></td>





</tr>




  <?php
  /*
$bankQuery=mysqli_query($conn,"select taxtype, description, tax from salestax where reserved_word != 'OUTPUT' order by description") or die(mysqli_error());
?>

  <td width="74" height="31" align="left" ><strong><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Sales Tax</font></strong></td>
<td> <SELECT name='Tax' onChange="javascript:submit();" >
<?php
    echo "<option selected value=\" 0\"> Select Tax";
    while($row = mysqli_fetch_row($bankQuery))
    {
   	$temp = $row[1].'&nbsp;' .$row[2].'%';
	if(isset($myTax) && preg_match("/" . preg_quote($myTax", '/') . "/", $row[0]))
	{
		echo "<option selected value=\"$row[0]\">$temp";
	}
	else{
		echo "<option value=\"$row[0]\">$temp";
	}
 }
?>
    </SELECT></td>

*/

if($invno == 0)
{
	?>
	<tr>
	<td width="201" height="31" align="left" ><strong><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Item Group</font></strong></td>
	<td>
	<?
	$stQuery = mysqli_query($conn," select catcode, category from prodcat  where rf = 'F' ")or die( mysqli_error() );
	?>
	  <SELECT name='Group' onChange="javascript:submit();">
		<?
	echo "<option selected value=\" 0\"> Select Group";
	while($row = mysqli_fetch_row($stQuery))
	{
		
		if(isset($myGroup) && preg_match("/" . preg_quote($myGroup, '/') . "/", $row[0]))
		{
			echo "<option selected value=\"$row[0]\">$row[1]";
		}
		else{
			echo "<option value=\"$row[0]\">$row[1]";
		}
	}
	?>
	</SELECT></td>
	<td width="201" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Item</b></font></td>	
	<td> <SELECT name='Prod' onChange="javascript:submit();">
		<?
		echo "<option selected value=\" 0\"> Item";
		$prodresult = mysqli_query($conn,"select prodcode,prodname from product   where substring(prodcode,1,2)= '$myGroup' ");
		while($prow = mysqli_fetch_row($prodresult))
		{
		if(isset($myProd) && preg_match("/" . preg_quote($myProd, '/') . "/", $prow[0]))
		{
			echo "<option selected value=\"$prow[0]\">$prow[1]";
		}
		else{
			echo "<option value=\"$prow[0]\">$prow[1]";
		}		
		}
		?>
	</SELECT> </td></tr>
	

<? 
//<input type="hidden"  name="amount" id="h3">

}
else
{

	$cq=mysqli_query($conn,"select compcode, bcode, prodcode, specification, quantity, rate, value, record from outpo where record='$invno'") or die(mysqli_error());
	$brow = mysqli_fetch_row($cq);
	?>
	<input type="hidden" name="myCompany" value="<?=$brow[0]?>">
	<input type="hidden" name="myBuyer" value="<?=$brow[1]?>">
	<input type="hidden" name="accthead" value="<?=$brow[2]?>">
	<input type="hidden" name="amount" value="<?=$brow[6]?>">
	<input type="hidden" name="rec" value="<?=$brow[7]?>">

<?
}
/*
?>

	<tr>
	
	<td width="201" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Manufacturer</b></font></td>	
	<td> <SELECT name='manufact' >
		<?
		echo "<option selected value=\" 0\"> Manufacturer";
		$prodresult = mysqli_query($conn,"select catcode,category from manufact order by category ");
		while($prow = mysqli_fetch_row($prodresult))
		{

			echo "<option value=\"$prow[0]\">$prow[1]";
				
		}
		?>
	</SELECT> </td>


*/

$prodresult = mysqli_query($conn,"select rate from product   where prodcode = '$myProd' ");
$prow = mysqli_fetch_row($prodresult);
$prate = $prow[0];
?>
</tr>



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


<td width="201" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Sales ReturnRate       </b></font></td>
<td width="259"><input type="text" onKeyPress="return event.keyCode!=13" name="rate" id="h2"  value="<?=$prate?>" ></td>

</tr>

<td width="201" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Batch No     </b></font></td>
<td width="167"><input name="batch_no"  type="text" onKeyPress="return event.keyCode!=13" id="h1"   value="" ></td>

<td width="201" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Discount % if any    </b></font></td>
<td width="167"><input name="disc_percent"  type="text" onKeyPress="return event.keyCode!=13" id="h1"   value="" ></td>
</tr>

<tr>
<td width="201" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>MRP </b></font></td>
<td width="167"><input name="mrp"  type="text" onKeyPress="return event.keyCode!=13" id="h1"   value="" ></td>

<tr>

</table>

<table cellpading=0 cellspacing=10 width= 603>
<tr><td width="284" align="left">
<tr><td width="307" align="left"><blockquote><input type =submit name="Clear" value = "Clear" ></blockquote></td><td width="288" align="right">
 <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>

</table>

<?
if($Clear == 'Clear')
{
	mysqli_query($conn,"truncate tpurch ") or die(mysqli_error());
}
if($accthead == '0' and $qty > 0)
{
?>
<script language="JavaScript">
alert("Select Product please");
</script>
<?
exit;
}
if($add == 'Add' and $qty >0)
{

	if($myTax== '0')
	{
	?>
	<script language="JavaScript">
	alert("Oh.. You forgot to select Tax....Please select Tax..");
	</script>
	<?
	exit;
	}

	if($myType== '0')
	{
	?>
	<script language="JavaScript">
	alert("Select Type of Entry Please..");
	</script>
	<?
	exit;
	}

if($batch_no == '')
{
	?>
	<script language="JavaScript">
	alert("Batch No missing..");
	</script>
	<?
	exit;	
}
	$bresult = mysqli_query($conn,"select distinct bcode from tpurch ") or die(mysqli_error());
	$brow = mysqli_fetch_row($bresult);
	if(is_array($brow))
	{
	if($brow[0] != $myBuyer)
	{
		?>
			<script language="JavaScript">
            alert("Selected Supplier is wrong");
            </script>
            <?
            exit;
	}
	}
	$aresult1=mysqli_query($conn,"select taxtype, tax,cess from salestax where taxtype='$myTax'") or die(mysqli_error());
	$arow1 = mysqli_fetch_row($aresult1);
	$tax_type = $arow1[0];
	$tax1 = $arow1[1];	
		
		//$exp_date = actdate($exp_date);
		//$exp_date = us($exp_date);

	$result = mysqli_query($conn,"insert into tpurch values ( '$myCompany','$myBuyer','$myProd','$manufact', '$exp_date', '$qty','$rate','$amount','$batch_no', '$mrp','$rec', 'PO','$disc_percent','$tax_type', '$tax1',  NULL)") or die(mysqli_error()); 

}


/*
<tr>

    <td width="74" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Discount % 
       </b></font></td>
    <td width="167"><input name="discount"  type="text"  value='<?=$discount?>' onChange="javascript:submit()"></td></td>
</tr>
*/
?>
<tr>

    <td width="71" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Packing Charges if any
       </b></font></td>
    <td width="167"><input name="packing"  type="text"  value='<?=$packing?>' onChange="javascript:submit()"></td>



</tr>

<tr>

    <td width="71" align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Scheme Discount Amount
       </b></font></td>
    <td width="167"><input name="scheme"  type="text"  value='<?=$scheme?>' onChange="javascript:submit()"></td>



</tr>
</form>



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

<script language="javascript">
function check()
{

if(myForm03.po_no.value=="")
	{
		alert("Please Enter Invoice No");
		myForm03.po_no.focus();
		return false;
	}	



}
</script>

<input type="hidden" name="bcode" value="<?=$myBuyer?>">
<input type="hidden" name="myTax" value="<?=$myTax?>">
<input type="hidden" name="accthead" value="<?=$myProd?>">
<table width="603"><tr  bgcolor="#A4A4FF">

<td width="39" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Modify</b></font></td>
<td width="110" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Product Name</b></font></td>



<td width="33" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Qty &nbsp;</b></font></td>
<td width="44" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Rate &nbsp;</b></font></td>
<td width="62" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Discount % &nbsp;</b></font></td>
<td width="44" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Value</b></font></td>
<td width="39" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Tax %</b></font></td>
<td width="45" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Tax amount</b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Amount</b></font></td>
<td width="62" height="31" align="right" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>MRP</b></font></td>

<?



$gtotal = 0;
$result = mysqli_query($conn,"select * from tpurch  order by record ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$compcode = $row[0];
	$bcode = $row[1];
	$accthead = $row[2];
	$manufact = $row[3];
	$exp_date= british($row[4]);
	$qty = $row[5];
	$rate = $row[6];
	$amount = $row[7];
	$batch_no = $row[8];
	$mrp = $row[9];

	$porec = $row[10];
	$mypo = $row[11];
	$discount_percent = $row[12];
	$tax_percent = $row[14];
	$record = $row[15];


	$value= round($qty * $rate,2);
	$taxable_value = round($value - ($value * $discount_percent /100),2);
	
	
	
	$taxamt = round($taxable_value * $tax_percent / 100,2);

	
	$amount=  $taxable_value + $taxamt+$cessamt; 
		


	$tottax = $tottax +$taxamt;
	$gtotal = $gtotal + $amount;


	$bresult  = mysqli_query($conn,"select name from buy where bcode = '$bcode' ") or die(mysqli_error());
	$brow = mysqli_fetch_row($bresult);
	$bname = $brow[0];
	


	$bresult  = mysqli_query($conn,"select prodname from product where prodcode = '$accthead' ") or die(mysqli_error());
	$brow = mysqli_fetch_row($bresult);
	$pname = $brow[0];

?>



<tr  bgcolor="#D7D7FF">
<td width="39" height="31" align="left" ><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="110" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$pname?></b></font></td>



<td width="33" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($qty >0) echo $qty;?></b></font></td>
<td width="44" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($rate >0) echo $rate;?></b></font></td>
<td width="62" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($discount_percent >0) echo $discount_percent;?></b></font></td>
<td width="44" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($value >0) echo $value;?></b></font></td>
<td width="39" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($tax_percent >0) echo $tax_percent;?></b></font></td>
<td width="45" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$taxamt?></b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($amount >0) echo $amount;?></b></font></td>
<td width="62" height="31" align="right" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$mrp?></b></font></td>
</tr>

<?
}
?>

<?

	$netamt =  $gtotal + (float)$packing - (float)$scheme;





?>


<input type="hidden" name="scheme" value="<?=$scheme?>">
<input type="hidden" name="packing" value="<?=$packing?>">
<input type="hidden" name="mypo" value="<?=$mypo?>">
<input type="hidden" name="porec" value="<?=$porec?>">




<tr  bgcolor="#D7D7FF">
<td colspan="8"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Amount</b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($gtotal,2,'.','')?></b></font></td>
<td>&nbsp;</td>
</tr>

<tr  bgcolor="#D7D7FF">
<td colspan="7"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Total Tax </b></font></td>
<td width="45" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($tottax,2,'.','')?></b></font></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?
/*
<tr  bgcolor="#D7D7FF">
<td colspan="8"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Total After Discount </b></font></td>
<td width="105" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($tot,2,'.','')?></b></font></td>
</tr>
*/
?>
<tr  bgcolor="#D7D7FF">
<td colspan="8"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Packing Charges </b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($packing>0) echo number_format($packing,2);?></b></font></td>
<td>&nbsp;</td>
</tr> 


<tr  bgcolor="#D7D7FF">
<td colspan="8"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Scheme Discount (-) </b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($scheme>0) echo number_format($scheme,2);?></b></font></td>
<td>&nbsp;</td>
</tr>

  <tr  bgcolor="#D7D7FF">
<td colspan="8"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Net Amount </b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>
<input  size="10" type="text"   class="rightAligned"name="tt" value="<?=number_format($netamt,2,'.','')?>"></b></font></td>
<td>&nbsp;</td>
</tr>


</table>


<table border=0 cellpadding=3 cellspacing=4 width="603" bgcolor="#D9E0F9">

	<td  align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>Date of Entry &nbsp;</b></font> </td>
		<td class="small_site_font_black" > 
		

		<?	$today1=strftime ("%d-%m-%Y", mktime(0,0,0,date("m"),date("d"),date("Y"))); ?>
       <input name="today1"  id="today1" value="<?=$today1?>" type="text" size="10">
<a href="javascript:NewCal('today1','DDMMYYYY')"><img
 src="../../images/cal.gif" alt="Pick a date" 
border="0"></a>    </td></tr>

<tr>
    <td width="97" align="left"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Invoice No
       </b></font></td>
    <td width="195"><input type="text" onKeyPress="return event.keyCode!=13" name="po_no" value=""></td>

<td  align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>Date &nbsp;</b></font> </td>
		<td 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.myForm03.today,'anchor1','dd/MM/yyyy'); return false;"name="anchor1" id="anchor1"><img  src="../../images/cal.gif" border="0"></a></td>
</tr>
</table>


<table width = "603">
  <tr> 
    <td width = "250" align="right" ><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> 

<tr>
<td colspan=2 >  <div align="center">
  <input type =submit name="submit" value = "Submit" >
</div></td>
</tr>

</table>

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