<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$cname=$_REQUEST['cname'];	
if(strlen($cname)<=0)
{
 print "<script> alert('Invalid Customer Name'); history.go(-1) ; </script>";
 return;
}
$qry="select max(code) from khadistaff";
$rs=mysqli_query($db,$qry);
$num=mysqli_num_rows($rs);
if($num>0)
{
	$rw=mysqli_fetch_row($rs);
	$code='S'.substr('00000'.(substr($rw[0],1,5)+1),-5);
}
else
{
	$code='S00001';
}
//$code=sprintf('%06d',$code);
$crdate=date('Y-m-d');


$res=mysqli_query($db,"select cname from khadistaff where cname='$cname'");
if(mysqli_num_rows($res)>0)
{
 print "<script> alert('Duplicate A/c Head'); history.go(-1) </script>";
 return;
}
$cname=strtoupper($cname)."(".$code.")";
$acname=strtoupper($cname);

	$addr1=$_REQUEST['addr1'];
	$addr2=$_REQUEST['addr2'];
	$addr3=$_REQUEST['addr3'];
	$district=$_REQUEST['district'];
	$state=$_REQUEST['state'];
	$pin1=$_REQUEST['pin1'];
	$phone1=$_REQUEST['phone1'];
	$email=$_REQUEST['email'];
	$slsysno=$_REQUEST['slsysno'];
	$crlimit=$_REQUEST['crlimit'];
        $dislimit=$_REQUEST['dislimit'];
	$op=$_REQUEST['op'];
$sql="insert into khadistaff(brcode,code,crdate,cname,acname,addr1,addr2,addr3,dist1,state1,pin1,phone1,email,crdays,slsyn,cramt,pin2) values('$brcode','$code', '$crdate','$cname','$acname','$addr1','$addr2','$addr3','$district','$state','$pin1','$phone1','$email','0','$slsysno','$crlimit','$dislimit')";

//$sql="insert into khadistaff(brcode,code,crdate,cname,acname,addr1,addr2,addr3,dist1,state1,pin1,phone1,email,crdays,slsyn,cramt) values('$brcode','$code', '$crdate','$cname','$acname','$addr1','$addr2','$addr3','$district','$state','$pin1','$phone1','$email','0','$slsysno','$crlimit')";
mysqli_query($db,$sql)or die(mysqli_error($db));
$chk=mysqli_query($db,"select * from ledger where led='$cname'");
 if(mysqli_num_rows($chk)>0)
 {
   

 }
else
{
$sql="insert into ledger(led,grp,op,drcr,code,brcode) values('$acname','Sundry Debtors','$op','Dr','$code','$brcode')";
mysqli_query($db,$sql) or die('<b>Ledger creation error:'.mysqli_error($db));
}

$cname="";
print "<script> alert('Data has been saved');history.back(1); </script>";
include("khadistaff.php");
?>
