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



$res=mysqli_query($db,"select cname from customer where cname='$cname'");
if(mysqli_num_rows($res)>0)
{
 print "<script> alert('Duplicate A/c Head'); history.go(-1) </script>";
 return;
}
$cname=strtoupper($cname);
$acname=strtoupper($cname);
$addr1=$_REQUEST['addr1'];
$addr2=$_REQUEST['addr2'];
$corp=$_REQUEST['corp'];
$addr3=$_REQUEST['addr3'];
$district=$_REQUEST['district'];
$state=$_REQUEST['state'];
$pin1=$_REQUEST['pin1'];
$phone1=$_REQUEST['phone1'];
$email=$_REQUEST['email'];
$tin=$_REQUEST['tin'];
$cst=$_REQUEST['cst'];
$kgst=$_REQUEST['kgst'];
$sql="insert into customer(brcode,code,crdate,cname,corp,acname,addr1,addr2,addr3,dist1,state1,pin1,phone1,email,tin,cst,kgst,cramt,crdays) values('$brcode','$code', '$crdate','$cname($code)','$corp','$acname','$addr1','$addr2','$addr3','$district','$state','$pin1','$phone1','$email','$tin','$cst','$kgst','0','0')";
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)
 {
   // print "<script> alert('Duplicate Ledger'); history.back(1); </script>";
    //return;
 }
else
{
$op=$_REQUEST['op'];
if($op=='')
{
$op=0;
}
$sql="insert into ledger(led,grp,op,drcr,code,brcode) values('$acname($code)','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("custmaster.php");
?>
