<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
include("opendb.php");
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];
$cname=$_REQUEST['cname'];
$acname=$_REQUEST['acname'];
$corp=$_REQUEST['corp'];
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$addr1=$_REQUEST['addr1'];
$dist1=$_REQUEST['dist2'];
$dist2=$_REQUEST['dist1'];
$state1=$_REQUEST['state1'];
$pin1=$_REQUEST['pin1'];
$phone1=$_REQUEST['phone1'];
$phone2=$_REQUEST['phone2'];
$addr2=$_REQUEST['addr2'];
$state2=$_REQUEST['state2'];
$pin2=$_REQUEST['pin2'];
$mob=$_REQUEST['mob'];
$relation=$_REQUEST['relation'];
$father=$_REQUEST['father'];
$dob=$_REQUEST['dob'];
$email=$_REQUEST['email'];
$web=$_REQUEST['web'];
$tin=$_REQUEST['tin'];
$cst=$_REQUEST['cst'];
$kgst=$_REQUEST['kgst'];
$gst=$_REQUEST['gst'];
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$companycode=$_REQUEST['companycode'];
$tcsapply=$_REQUEST['tcsapply'];
if($state1=='Select State' || $state2=='Select State')
{
         print "<script>alert('Select A State...!!!'); history.go(-1) ; </script>";
         return;
}

if(strlen($cname)<=0)
{
 print "<script> alert('Invalid Customer Name'); history.go(-1) ; </script>";
 return;
}
$code=substr($cname,0,3).date('ymds');
$code=substr(str_replace(' ','-',$code),0,10);

$dob=$y1.'-'.$m1.'-'.$d1;
$crdate=$y.'-'.$m.'-'.$d;
$chk=mysqli_query($db,"select * from ledger where led='$acname'");
 if(mysqli_num_rows($chk)>0)
 {
 //   print "<script> alert('Duplicate Ledger'); history.back(1); </script>";
 //   return;
 }

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

$sql="insert into customer values('$brcode','$code', '$crdate','$cname','$corp','$acname','$addr1', '$dist1','$state1','$pin1','$phone1','$addr2','$dist2','$state2','$pin2','$phone2','$mob','$relation', '$father','$dob','$email','$tcsapply','0','0','0','0','0','$gst')";

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

$cname="";
print "<font color=green> Data has been saved</font>";
print "<script>document.location='custmaster_serv.php'</script>";
?>
