<? 
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<?
if($submit)
{
 if(strlen($brname)<=0)
 {
  print "<script> alert('Invalid Branch Name'); </script>";
  return;
 }
 include("../opendb.php");
 $code=substr($brname,0,3).substr(time(),7,3); 
 $code=str_replace(' ','-',$code);
 $brcode=$code;

 $res=mysqli_query($db,"select * from branch where brname='$brname'");
 if(mysqli_num_rows($res)>0)
 {
  print "<script> alert('Duplicate Entry'); history.back(1);</script>";
  return;
 }
 $sql="insert into branch values('$code','".strtoupper($brname)."', '$brman','$braddr', '$brcity','$brphone','$brpass1','$btype','$tin','$cst','$brstate','0','0','0','0','0','telcon')";
 mysqli_query($db,$sql) or die(mysqli_error($db));

 $led_code=$brname.substr(time(),7,3).date("s"); 
 mysqli_query($db,"insert into ledger values('$brname','Branches','','Dr','$led_code','$brcode')") or die('1'.mysqli_error($db));
/*
 $led_code='pet'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Petty Cash','Cash in hand','','Dr','$led_code','$code')") or die('2'.mysqli_error($db));

 $led_code='out'.substr(time(),7,3); 

 mysqli_query($db,"insert into ledger values('Output tax 12.5','Output Tax','','Cr', '$led_code','$code')") or die('3'.mysqli_error($db));


 $led_code='otc'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Output tax on credit notes 12.5','Output Tax','','Cr', '$led_code','$code')") or die('4'.mysqli_error($db));

 $led_code='dis'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Discount allowed(vehicles)','Discount Allowed','','Dr', '$led_code','$code')") or die('5'.mysqli_error($db));

 $led_code='sal'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Sales 12.5(vehicle)','Sales 12.5', '','Cr', '$led_code', '$code')") or die('6'.mysqli_error($db));

 $led_code='inp'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Input tax 12.5','Input Tax','','Dr', '$led_code', '$code')") or die('7'.mysqli_error($db));


 $led_code='pur'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Purchase 12.5(vehicle)','Purchases 12.5','','Dr', '$led_code', '$code')")or die('8'.mysqli_error($db));

 $led_code='sre'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Sales ret 12.5(vehicle)','Sales Return','','Dr', '$led_code', '$code')") or die('9'.mysqli_error($db));

 $led_code='pre'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Purchase ret 12.5(vehicle)','Purch. Returns','','Cr', '$led_code', '$code')") or die(mysqli_error($db));

 $led_code='pub'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Purchase 12.5(Body)','Purchases 12.5','','Dr', '$led_code', '$code')") or die(mysqli_error($db));

 $led_code='pun'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Purchase(Un registered)','Purchases Unreg.','','Dr', '$led_code', '$code')") or die(mysqli_error($db));

 $led_code='ots'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Output tax 12.5 (Sale Ret)','Output Tax','','Cr', '$led_code','$code')") or die(mysqli_error($db));

 $led_code='ipr'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Input tax 12.5 (Pur Ret)','Input Tax','','Dr', '$led_code', '$code')") or die(mysqli_error($db)); 

$led_code='gsc'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Goods Sent on Consig.','Stock Transfer In','','Cr', '$led_code', '$code')") or die(mysqli_error($db)); 

 $led_code='grc'.substr(time(),7,3); 
 mysqli_query($db,"insert into ledger values('Goods recvd on Consig.','Stock Transfer Out', '','Dr', '$led_code', '$code')") or die('9'.mysqli_error($db));
*/

 print "Branch and Ledgers have been Created";
}
?>

<script>
function validate()
{
var p1=document.f1.brpass1.value
var p2=document.f1.brpass2.value

 submit="true";
 if(document.f1.brname.value.length<3)
  {submit="false"; alert('Invalid branch name');}

 if(document.f1.brpass1.value.length<=0)
  {submit="false"; alert('Invalid Password');}

 if(document.f1.brpass2.value.length<=0)
  {submit="false"; alert('Invalid Password2');}
 if(p1!=p2)
  {submit="false"; alert('Passwords does not match');}

 if(submit=="false")
  return false;
}
</script>

<body bgcolor=white>

<hr bgcolor=red>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return validate()">
<table border=0  cellspacing=2 cellpadding=5 bgcolor='aliceblue'>
<th colspan=2><h3>Branch Creation</h3></font></th>
<tr><td>Branch Name</td><td><input type=text name='brname' size=40></td></tr>
<tr><td>Branch Type </td><td><select name='btype'>
<option>Telecon</option>
</select></td></tr>

<tr><td>Branch Manager</td><td><input type=text name='brman' size=40></td></tr>
<tr><td>Address</td><td><input type=text name='braddr' size=40></td></tr>
<tr><td>City</td><td><input type=text name='brcity' size=40></td></tr>
<tr><td>State</td><td>
<select name='brstate'>
<option>Kerala</option>
<option>Karnataka</option>
</select>
</td></tr>
<tr><td>Phone</td><td><input type=password name='brphone' size=40></td></tr>

<tr><td>Tin No.</td><td><input type=tex name='tin' size=40></td></tr>
<tr><td>Cst No.</td><td><input type=text name='cst' size=40></td></tr>
<!--
<tr><td>Admin Password</td><td><input type=password name='brpass1' size=40></td></tr>
<tr><td>Confirm <br>Admin Password</td><td valign=bottom><input type=password name='brpass2' size=40></td></tr>
-->
<tr bgcolor='lightsteelblue'><td colspan=2 align=center><input type=submit name='submit' value='  S A V E  '> <input type=button value='   C A N C E L    ' onClick=document.location='main.htm'></td></tr>
</table>
</form>
<?
include("listbr.php");
?>

