<style type="text/css">
input.rightAligned {
text-align: right;
}
</style>
<?php 
include("../../include/dbconnect.php");



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

$myTax = $_POST['Tax'];

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

$myCTax = $_POST['CTax'];



?>
<SCRIPT LANGUAGE="JavaScript" src="../../include/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
var cal = new CalendarPopup();
</SCRIPT>

<title> Transaction  details edit</title>
<body bgcolor="#ffffff"  topmargin=0>
<center>

<table border=0 cellpadding=3 cellspacing=4 width="597" bgcolor="#E6E6FF">
  <tr> 
    <td  colspan="100%"  bgcolor="#93BEE2"><font face="Arial, Helvetica, sans-serif"><b>Edit Purchase Entry</b></font></td>
  </tr>

<form  name='myform' method='POST' action='<?=$_SERVER['PHP_SELF']?>?compcode=<?=$compcode?>&mypono=<?=$mypono?>&trans_id=<?=$trans_id?>&account_year=<?=$account_year?>'>
<?
$sresult=mysqli_query($conn,"select purchasereg.packing,purchasereg.manufact,purchasereg.tcs  from purchasereg, buy where compcode ='$compcode' and purchasereg.trans_id = '$trans_id' and account_year = '$account_year'  and purchasereg.bcode=buy.bcode and prodcode = 'TOTAL' ") or die(mysqli_error());
$srow=mysqli_fetch_row($sresult);
if($dis == 'T')
{
	$pack = $srow[0];
	$sch = $srow[1];
	$tcs = $srow[2];
}
else
{
	$pack = $pack;
	$sch = $sch;
	$tcs = $tcs;
}

?>
<tr>


<td  width="100" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Packing Charges  &nbsp;</b></font></td>
<td width="291"><input type="text"   name="pack" value="<?=$pack?>" onChange="javascript:submit();"> </td>



    <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="sch"  type="text"  value='<?=$sch?>' onChange="javascript:submit()"></td>

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


</tr>
</form>



<form name="myform3" method="POST"  action="update.php?mypono=<?=$mypono?>&account_year=<?=$account_year?>&trans_id=<?=$trans_id?>" onsubmit="return check()">
<script language="javascript">
function check()
{

}
</script>


<?  
$aresult = mysqli_query($conn,"select distinct account_year from branches ");
$arow = mysqli_fetch_row($aresult);
$account_year = $arow[0];



$result=mysqli_query($conn,"select  buy.name, purchasereg.poinv_no, purchasereg.poinv_date,  purchasereg.packing, purchasereg.trans_id, purchasereg.bcode, purchasereg.compcode, purchasereg.remarks,purchasereg.manufact  from purchasereg, buy where compcode ='$compcode' and purchasereg.trans_id = '$trans_id' and account_year = '$account_year'  and purchasereg.bcode=buy.bcode and prodcode = 'TOTAL' ") or die(mysqli_error());
$row=mysqli_fetch_row($result);


$bname=$row[0];
$mypono=$row[1];
$inv_date=$row[2];
$trans_id=$row[4];
$bcode=$row[5];
$compcode=$row[6];
$remarks=$row[7];



$result9= mysqli_query($conn," select distinct compcode,name from company  where compcode='$compcode'")or die( mysqli_error() );
$arow4 = mysqli_fetch_row($result9);
$comname = $arow4[1];

?>


<input type="hidden" name="trans_id" value="<?=$trans_id?>" >

<tr > 
<td width="100" height="31" align="right" ><div align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Company &nbsp;</b></font></div></td>
<td width="192"><? echo($comname); ?></td>

<td width="100" height="31" align="right" ><div align="left"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Supplier&nbsp;</b></font></div></td>
<td width="291"><? echo($row[0]); ?></td>
</tr>






</table>


<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>


<?
if($pk == 'T')
{
	$result1=mysqli_query($conn,"select compcode,bcode, manufact,batch_no,exp_date,quantity, rate, mrp, discount,taxtype,vat_percent,prodcode,po_no,po_date,trans_id from purchasereg where purchasereg.trans_id = '$trans_id' and purchasereg.prodcode != 'TOTAL' and account_year = '$account_year' and bcode = '$bcode' ") or die(mysqli_error());
	while($row1 = mysqli_fetch_row($result1))
	{
		$compcode = $row1[0];
		$bcode = $row1[1];
		$manufact = $row1[2];
		$batch_no = $row1[3];
		$exp_date = $row1[4];
		$qty = $row1[5];
		$rate = $row1[6];
		$mrp = $row1[7];
		$discount = $row1[8];
		$taxtype = $row1[9];
		$tax_percent = $row1[10];
		$accthead = $row1[11];
		$po_no = $row1[12];
		$po_date = $row1[13];
		$trans_id = $row1[14];
		$result = mysqli_query($conn,"insert into tpurch values ( '$compcode','$bcode','$accthead','$manufact', '$exp_date', '$qty','$rate','$amount','$batch_no', '$mrp','$rec', '$po','$discount','$taxtype', '$tax_percent',  NULL)") or die(mysqli_error()); 
	}
	$pk = 'F';
}	

$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 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 = "peditview1.php?record=<?echo($record);?>&mypono=<?=$mypono?>&account_year=<?=$account_year?>&trans_id=<?=$trans_id?>&myPurgl=<?=$myPurgl?>"><?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)$pack - (float)$sch+ (float)$tcs;





?>



<input type="hidden" name="pack" value="<?=$pack?>">
<input type="hidden" name="sch" value="<?=$sch?>">
<input type="hidden" name="tcs" value="<?=$tcs?>">
<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>

<?
	$taxresult = mysqli_query($conn,"select tax_type,round(sum(((rate*qty)- (rate*qty*discount_percent/100))*tax_percent/100),2),round(sum((rate*qty)- (rate*qty*discount_percent/100)),2) from tpurch group by tax_type") or die(mysqli_error());
	while($trow = mysqli_fetch_row($taxresult))
	{
		$stresult = mysqli_query($conn,"select sgst_acctcode, cgst_acctcode, igst_acctcode, sp_acctcode,tax from salestax where taxtype = '$trow[0]' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($stresult);	
		$sgst_acctcode = $srow[0];	
		$cgst_acctcode = $srow[1];
		$igst_acctcode = $srow[2];
		$myPurgl = $srow[3];
		$tax_rate = $srow[4];
		
		$bresult = mysqli_query($conn,"select taxtype from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow = mysqli_fetch_row($bresult);
		$buyertype =  $brow[0];
		$taxamt=round($trow[1],2);
		$pvalue = round($trow[2],2);		
		if($buyertype == 'STATE')
		{
			$sgst_amt = round($taxamt/2,2);
			$cgst_amt = $sgst_amt;
			$igst_amt =0;
			$tax_rate = $tax_rate/2;
		}
		if($buyertype == 'INTERSTATE')
		{
			$igst_amt = round($taxamt,2);
			$sgst_amt=0;
			$cgst_amt=0;
		}
		if($sgst_amt >0)
		{		
?>
<tr  bgcolor="#D7D7FF">
<td colspan="7"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>SGST &nbsp;<?=$tax_rate?>&nbsp;% </b></font></td>
<td width="45" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($sgst_amt,2,'.','')?></b></font></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<?
		}
		if($cgst_amt >0)
		{		
?>
<tr  bgcolor="#D7D7FF">
<td colspan="7"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>CGST &nbsp;<?=$tax_rate?>&nbsp;% </b></font></td>
<td width="45" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($cgst_amt,2,'.','')?></b></font></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<?
		}		
		if($igst_amt >0)
		{		
?>
<tr  bgcolor="#D7D7FF">
<td colspan="7"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>IGST &nbsp;<?=$tax_rate?>&nbsp;% </b></font></td>
<td width="45" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($igst_amt,2,'.','')?></b></font></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<?
		}

}// end of WHile Loop.
?>
<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($pack>0) echo number_format($pack,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($sch>0) echo number_format($sch,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>TCS (+) </b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($tcs>0) echo number_format($tcs,2);?></b></font></td>
<td>&nbsp;</td>
</tr>



<?
$diff = round($netamt,0) - $netamt;
$netamt = round($netamt,0);
?>

<tr  bgcolor="#D7D7FF">
<td colspan="8"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Round off</b></font></td>
<td width="81" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? echo number_format($diff,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,'.','')?>" readonly></b></font></td>
<td>&nbsp;</td>
</tr>
</table>






<table border=0 cellpadding=3 cellspacing=4 width="603" bgcolor="#D9E0F9">
<tr><td align=center><blockquote>


   <input  type =submit name=submit value = "Update"></blockquote></td><td><blockquote><input type =submit name=submit value = "Delete"></blockquote></td> </tr></table>

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