<?
session_start();
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];
$scode=$_REQUEST['sapcode'];
$cname=$_REQUEST['cname'];
$acname=$_REQUEST['acname'];
$corp=$_REQUEST['corp'];
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$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'];
if(strlen($cname)<=0)
{
 print "<script> alert('Invalid Customer Name'); history.go(-1) ; </script>";
 return;
}
if(strlen($scode)<=0)
{
 
//$scode=substr($cname,0,3).date(ymds);
$scode="TEMP".date('ymds');
$scode=str_replace(' ','-',$scode);
}
$dob=$y1.'-'.$m1.'-'.$d1;
$crdate=$y.'-'.$m.'-'.$d;

$acname1=$acname.' ('.$scode.')';
$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' or code='$scode'");
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','$scode', '$crdate','$cname','$corp','$acname','$addr1', '$dist1','$state1','$pin1','$phone1','$addr2','$dist2','$state2','$pin2','$phone2','$mob','$relation', '$father','$dob','$email','$web','$tin','$cst','$kgst','0','0','0')";
//echo $sql;
mysqli_query($db,$sql)or die("customer err".mysqli_error($db));
$sql="insert into ledger values('$acname','Sundry Debtors','0','Dr','$scode','$brcode','0000-00-00')";
mysqli_query($db,$sql) or die('<b>Ledger creation error:'.mysqli_error($db));

if($cst>0)
{
$re=mysqli_query($db,"select * from partymaster where gst='$cst' ") or die(mysqli_error($db));
 if(mysqli_num_rows($re)<=0)
    {
     $sql1="insert into partymaster (brcode,code,cname,addr1,dist1,state1,pin1,phone1,phone2,email,gst,pan,ptype)                  values('$brcode','$scode','$cname','$addr1','$dist1','$state1','$pin1','$phone1','$phone2','$email','$cst','$kgst','CUS')";
 	     // print "$sql1<br>";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));

   }
   }


$cname="";
print "<font color=green> Data has been saved</font>";
include("custmaster.php");
?>
