<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if(isset($_POST[save]))
{
$descpt=$_REQUEST['descpt'];
$tax=$_REQUEST['tax'];
$type=$_REQUEST['type'];
$purledger=$_REQUEST['purledger'];
$saleledger=$_REQUEST['saleledger'];
$code=substr($descpt,0,3).substr(time(),7,3).date("s"); 
 $code=str_replace(' ','-',$code);

$re=mysqli_query($db,"select * from taxmaster where tax='$tax'");
if(mysqli_num_rows($re)>0)
{
 print "<script> alert('Duplicate Entry'); </script>";
 return;
}

$sql="insert into taxmaster values('$tax','$descpt','$type','$purledger','$saleledger','$code')";
mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));

print "<script> document.location='taxmaster.php'</script>";
return;
}
//$re=mysqli_query($db,"select * from configmaster");
//$row=mysqli_fetch_array($re);
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:steelblue;color:white}
</style>
<script>

function valid()
{
 
 if(document.f1.tax.value.length<=0)
 {
   alert("Invalid Tax.");
   document.f1.tax.focus();
   return false;
 }
if(document.f1.purledger.selectedIndex<=0 && document.f1.saleledger.selectedIndex<=0)
{
   alert("Invalid Ledger");
   document.f1.purledger.focus();
   return false;
 }


}
</script>

<center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='white'>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=3 >
<th colspan=4><font size=3>TAX MASTER</th>
<tr> <td>Tax %</td><td><input type=text name='tax'  size=7></td><td>Description</td><td><input type=text name='descpt'  size=50></td>
</tr>
<tr><td>Type</td><td><Select name='type'  size=1>
	<option>CST</option>
	<option>Local</option>
	</select>
</td>
 <td>Purchase Ledger </td><td><select name='purledger'>
<option></option>
<?
 $purled=mysqli_query($db,"select led from ledger where grp like 'purch%'");
 while($purrow=mysqli_fetch_array($purled))
  {
   print "<option>$purrow[0]</option>";
  }
?>

</select></td>
</tr>
<tr><td></td><td></td><td>Sales Ledger </td><td colspan=4><select name='saleledger'>
<option></option>
<?
 $purled1=mysqli_query($db,"select led from ledger where grp like 'sale%'");
 while($purrow1=mysqli_fetch_array($purled1))
  {
   print "<option>$purrow1[0]</option>";
  }
?>
</select></td></tr>

<th colspan=4><input type=submit value='Save' name='save'></td></tr>
</table>
</form>
</td></tr>
</table>
<br>
<table cellspacing=1 cellpadding=5 bgcolor='orange'>
<th>No <th>Tax % <th>Description<th>Type<th>Purchase Ledger <th>Sales Ledger<th>Del<th>Edit
<?
$i=1;
$res=mysqli_query($db,"select * from taxmaster order by tax");
while($row=mysqli_fetch_array($res))
{
 print "\n<tr bgcolor='white' onMouseOver=bgColor='b8d7b4'; onMouseOut=bgColor='white'>";
 print "<td>".$i++."</td><td>$row[tax]</td><td>$row[descpt]</td><td>$row[type]</td>";
 print "<td>$row[purledger]</td><td>$row[saleledger]</td>";
 print "<td>\n<input type=button value='Del' name='del' onClick=delfun('$row[code]')></td>";
 print "<td><input type=button value='Edit'  name='ed' onClick=window.open('edittaxmaster.php?code=$row[code]',menubar=false,resizable=false)></td></tr>";
}
?>
</table>

<script>
 function delfun(x)
 {
  if (confirm('do you want to delete '+x+' ?'))
   document.location='deltaxmaster.php?code='+x;
 }
  
</script>

