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


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

$mySalesexp = $_POST['Salesexp'];



?>
<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 Sales Entries</b></font></td>
  </tr>

<form  name='myform' method='POST' action='<?=$_SERVER['PHP_SELF']?>?compcode=<?=$compcode?>&mypono=<?=$mypono?>&account_year=<?=$account_year?>&dis=F'>



<tr>
<?
$aresult = mysqli_query($conn,"select distinct account_year,compcode from branches ");
$arow = mysqli_fetch_row($aresult);
$account_year = $arow[0];
$compcode = $arow[1];
$result=mysqli_query($conn,"select  salesreg8b.packing  from salesreg8b where compcode ='$compcode' and salesreg8b.trans_id ='$mypono' and account_year = '$account_year'  and prodcode = 'TOTAL' ") or die(mysqli_error());
$row=mysqli_fetch_row($result);
if($dis == 'T')
{
	$pack = $row[0];
}
else
{
	$pack = $pack;
}

$bankQuery=mysqli_query($conn,"select acctcode,description from acctmast where reserved_word = 'SALESEXP' order by description") or die(mysqli_error());
?>


<td> <SELECT name='Salesexp' onChange="javascript:submit();" >
<?php
    while($row = mysqli_fetch_row($bankQuery))
    {
   	$temp = $row[1];
	if(isset($mySalesexp) && preg_match("/" . preg_quote($mySalesexp, '/') . "/", $row[0]))
	{
		echo "<option selected value=\"$row[0]\">$temp";
	}
	else{
		echo "<option value=\"$row[0]\">$temp";
	}
 }
?>
    </SELECT></td>
<td width="291"><input type="text"   name="pack" value="<?=$pack?>" onChange="javascript:submit();"> </td>

</tr>

</form>



<form name="myform2" method="POST"  action="update.php" onsubmit="return check()">
<script language="javascript">
function check()
{

if(myform2.mySalesexp.value=="")
	{
		alert("Please Select Sales Expense GL or Re enter the sales expense. Put 0 if no sales expense.");
		myform2.mySalesexp.focus();
		return false;
	}	



}
</script>


<?  

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



$result=mysqli_query($conn,"select salesreg8b.bcode, salesreg8b.inv_no, salesreg8b.inv_date,  salesreg8b.packing, salesreg8b.trans_id, salesreg8b.bcode, salesreg8b.compcode, salesreg8b.specification,invoice_type,vehicle_no  from salesreg8b where compcode ='$compcode' and salesreg8b.trans_id ='$mypono' and account_year = '$account_year'  and prodcode = 'TOTAL' ") or die(mysqli_error());
$row=mysqli_fetch_row($result);


$bcode=$row[0];
$inv_no=$row[1];
$inv_date=$row[2];
$packing = $row[3];
$trans_id=$row[4];
$bcode=$row[5];
$compcode=$row[6];
$specification=$row[7];
$inv_type = $row[8];
$vehicle_no = $row[9];


	if($bcode != '0')
	{
		$result = mysqli_query($conn,"select  name,address1,address2,city,state,pin,cst,tin,std,telno from buy where bcode ='$bcode' ") or die(mysqli_error());
		$row=mysqli_fetch_row($result);
		$bname=$row[0];
	}
	else
	{
		$result = mysqli_query($conn,"select  name,address1,address2,city,state,pin,phone from bill_patient where inv_no='$inv_no'   and account_year ='$account_year' ") or die(mysqli_error());
		$row=mysqli_fetch_row($result);
		$bname=$row[0];

	}	








$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="mySalesexp" value="<?=$mySalesexp?>" >
<input type="hidden" name="trans_id" value="<?=$mypono?>" >
<input type="hidden" name="account_year" value="<?=$account_year?>" >


<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>Customer Name&nbsp;</b></font></div></td>
<td width="291"><? echo($row[0]); ?></td>
</tr>






</table>


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

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



<td width="35" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Qty &nbsp;</b></font></td>
<td width="47" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Rate &nbsp;</b></font></td>
<td width="67" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Discount % &nbsp;</b></font></td>
<td width="95" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Value</b></font></td>
<td width="95" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Value after Discount</b></font></td>
<td width="47" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Tax %</b></font></td>
<td width="60" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Tax amount</b></font></td>
<td width="118" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Amount</b></font></td>


<?

if($dis == 'T')
{
	$result1=mysqli_query($conn,"select compcode,bcode, prodcode,quantity,rate,batch_no,specification,discount,taxtype,vat_percent,trans_id,cess_percent from salesreg8b where salesreg8b.inv_no ='$inv_no' and salesreg8b.prodcode != 'TOTAL' and account_year = '$account_year' ") or die(mysqli_error());
	while($row1 = mysqli_fetch_row($result1))
	{
		$compcode = $row1[0];
		$bcode = $row1[1];
		$prodcode = $row1[2];
		$qty = $row1[3];
		$rate = $row1[4];
		$batch_no = $row1[5];
		$specification = $row1[6];
		$discount = $row1[7];
		$taxtype = $row1[8];
		$vat_percent = $row1[9];
		$trans_id = $row1[10];
		$cess_percent = $row1[11];
		
		
		$amount = ($rate *$qty);
		$result = mysqli_query($conn,"insert into spurch values ( '$compcode','$bcode','$prodcode', '$qty','$rate','$batch_no', '$amount','$bags', '$discount','$taxtype','$vat_percent','$cess_percent','$specification','$stainer_rate',  NULL)") or die(mysqli_error());
	}
	$dis='F';
}


$result = mysqli_query($conn,"select taxtype from buy where bcode = (select distinct bcode from spurch) ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$buyertype = $row[0];



$gtotal = 0;
$result = mysqli_query($conn,"select * from spurch  order by record ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{
	$compcode = $row[0];
	$bcode = $row[1];
	$accthead = $row[2];
	$qty = $row[3];
	$rate = $row[4];
	$batch_no = $row[5];	
	$amount = $row[6];
	$units = $row[7];
	$discount_percent = $row[8];
	$taxtype = $row[9];
	$tax_percent = $row[10];
	$cess_percent = $row[11];
	$specification = $row[12];
	$stainer_rate = $row[13];
	$record = $row[14];




	$value= round($qty * $rate,2);
	$taxable_value = round($value ,2);
	//$disc_amt = round($value * $discount_percent /100,2);
	
	
	$taxamt = round($taxable_value * $tax_percent / 100,2);
	$cess_amt = round($taxable_value * $cess_percent/100,2);
	$amount=  $taxable_value + $taxamt; 
	$tottax = $tottax +$taxamt;
	$gtotal = $gtotal + $amount;

	$valuetot = $valuetot + $value;
	$cesstotal = $cesstotal +$cess_amt;			
		
		$disctotal = $disctotal + $disc_amt;


	$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];
	if($specification != "")
	{
		$pname = $pname.'&nbsp;('.$specification.')';
	}
	

?>



<tr  bgcolor="#D7D7FF">
<td width="42" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><a href = "peditview1.php?trans_id=<?echo($mypono);?>&record=<?=$record?>&inv_no=<?=$inv_no?>&inv_date=<?=$inv_date?>"><?echo($record);?></a></b></font></td>
<td width="119" height="31" align="left" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$pname?></b></font></td>



<td width="35" 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="47" 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="67" 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="95" 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="95" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($taxable_value >0) echo $taxable_value;?></b></font></td>
<td width="47" 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="60" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=$taxamt?></b></font></td>
<td width="118" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><? if($amount >0) echo $amount;?></b></font></td>

</tr>

<?
}












$bamt =  $gtotal+(float)$pack +(float)$cesstotal;

$billamt = round($bamt);
$diff = $billamt- $bamt;


?>

<input type="hidden" name="packing" value="<?=$pack?>">




<tr  bgcolor="#D7D7FF">
<td colspan="5"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Total </b></font></td>
<td width="60" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($valuetot,2,'.','')?></b></font></td>
<td width="60" height="31" align="right" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($disctotal,2,'.','')?>(D)</b></font></td>
<td width="60" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b></b></font></td>

<td width="60" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($tottax,2,'.','')?></b></font></td>
<td width="118" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($gtotal,2,'.','')?></b></font></td>
</tr>
<?
	$taxresult = mysqli_query($conn,"select taxtype,round(sum(rate*qty*tax_rate/100),2),round(sum((rate*qty)),2) from spurch group by taxtype") or die(mysqli_error());
		while($trow = mysqli_fetch_row($taxresult))
	{
		$stresult = mysqli_query($conn,"select sgst_acctcode, cgst_acctcode, igst_acctcode, sp_acctcode,tax,cess,cess_acctcode 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];
		//$cess_percent = $srow[5];
		$cess_acctcode = $srow[6];
		
		$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')
		{
			//$cess_amt = round($taxamt * $cess_percent/100,2);
			$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;
			$cess_amt=0;
		}
		if($sgst_amt >0)
		{		
?>
<tr  bgcolor="#D7D7FF">
<td colspan="8"   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>
</tr>

<?
		}
		if($cgst_amt >0)
		{		
?>
<tr  bgcolor="#D7D7FF">
<td colspan="8"   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>
</tr>

<?
		}		

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


</tr>

<?
		}	
				
		if($igst_amt >0)
		{		
?>
<tr  bgcolor="#D7D7FF">
<td colspan="8"   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>
</tr>

<?
		}

}// end of WHile Loop.

?>


<tr  bgcolor="#D7D7FF">
<td colspan="9"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Other  Charges </b></font></td>
<td width="118" 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>
</tr> 






  <tr  bgcolor="#D7D7FF">
<td colspan="9"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Round off</b></font></td>
<td width="118" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($diff,2,'.','')?></b></font></td>
</tr>

  <tr  bgcolor="#D7D7FF">
<td colspan="9"   align="right"><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Net Amount </b></font></td>
<td width="118" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b><?=number_format($billamt,2,'.','')?></b></font></td>
</tr>

</table>

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


		<tr > 
		<td width="97" height="31" align="left" ><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>Invoice  No
		&nbsp;</b></font></td>
		<td width="195" > <input type="text" onKeyPress="return event.keyCode!=13" name="inv_no" value= '<?=$inv_no?>' readonly >		</td>
	<td  align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>Invoice Date &nbsp;</b></font> </td>
		<td class="small_site_font_black" > 
		<?
				
		//$today2=strftime ("%d/%m/%Y", mktime(0,0,0,date("m"),date("d"),date("Y"))); ?>

		<input name="today2" type="text" onKeyPress="return event.keyCode!=13" class="text_box_button" value="<?=british($inv_date)?>" size="10"  readonly=""  > 
		<a href="#" onClick="cal.select(document.myform2.today2,'anchor1','dd/MM/yyyy'); return false;"name="anchor1" id="anchor1"><img  src="../../images/cal.gif" border="0"></a>		</td>
		</tr>



        

<tr > 
    <td width="139" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Documents Through&nbsp;</b></font></td> <td width="145" > <input type="text" name="form20" value= "">
  </td>
  
    <td width="139" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Type of Sale 
    &nbsp;</b></font></td><td>
	<SELECT name='saletype' >
	
    <option value="GST">GST

	
      </SELECT> </td>
      
</tr>


<tr >   <td width="139" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Order Ref No 
    &nbsp;</b></font></td>
  <td width="145" > <input type="text" name="pur_no" value= ""> 
  </td>
  <td width="139" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Order Ref Date 
    &nbsp;</b></font></td>
  <td width="145" > <input type="text" name="pur_date" value= ""> 
  </td>

</tr>
  

<tr >   <td width="139" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Mode of Transport
    &nbsp;</b></font></td>
  <td width="145" > <input type="text" name="vehicle_no" value= "<?=$vehicle_no?>"> 
  </td>
  <td width="139" height="31" align="right" ><font color="#003366" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>
    Invoice Type</b></font></td>
  <td width="145" ><input type="text" name="inv_type" value= "<?=$inv_type?>" readonly>   </td>

</tr>  
</table>

<table width = "667">
  <tr> 
    <td width = "284" align="right" ><font color="#FF0000" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Confirm</b></font></td>
    <td width="371"> <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 align="center" colspan="2"> <blockquote> <input type =submit name="submit" value = "Submit"></blockquote></td>
  </table>


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