<? session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
$submit = $_REQUEST['submit'];
$custname = $_REQUEST['custname'];
$addr1 = $_REQUEST['addr1'];
$addr2 = $_REQUEST['addr2'];
$city = $_REQUEST['city'];
$dist = $_REQUEST['dist'];
$stat = $_REQUEST['stat'];
$pin = $_REQUEST['pin'];
$phone1 = $_REQUEST['phone1'];
$phone2 = $_REQUEST['phone2'];
$tin = $_REQUEST['tin'];
$cst = $_REQUEST['cst'];
$email = $_REQUEST['email'];
$web = $_REQUEST['web'];
$acname = $_REQUEST['acname'];
$corp = $_REQUEST['corp'];
$ofno = $_REQUEST['ofno'];
$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y=$_REQUEST['y'];

$m1=$_REQUEST['m1'];
$d1=$_REQUEST['d1'];
$y1=$_REQUEST['y1'];
$father=$_REQUEST['father'];
$addrtmp=$_REQUEST['addrtmp'];
$relation=$_REQUEST['relation'];
$tin=$_REQUEST['tin'];
$cst=$_REQUEST['cst'];
$pan=$_REQUEST['pan'];
$op=$_REQUEST['op'];
if($submit)
{
 if(strlen($custname)<=0 && strlen($ofno)<=0)
  {
     print "<script> alert('In sufficient entry'); history.back(1);  </script>";
     return;
  }
 if(strlen($acname)<=0)
  {
     print "<script> alert('In sufficient entry'); history.back(1);  </script>";
     return;
  }
$chk=mysqli_query($db,"select * from cust_master where acname='$acname' and branch='$brcode'");
 if(mysqli_num_rows($chk)>0)
 {
    print "<script> alert('Duplicate Entry Display Name'); history.back(1); </script>";
    return;
 }
 $tim=date("d-m-Y*H:i:s");
 $code=$user.'*'.$tim;
 $code=str_replace(' ','-',$code);
$dt=$y.'-'.$m.'-'.$d;
$dob=$y1.'-'.$m1.'-'.$d1;
if($dob=='--')
    $dob = '0000-00-00';
if($op<='')
    $op=0;
$acname=trim($acname);
$acname=str_replace('  ',' ',$acname);

 $sql="insert into cust_master values('$code','".strtoupper($custname)."','$addr1','$addr2', '$city', '$dist', '$stat','$pin','$phone1','$phone2','$email','$web','$brcode','$acname','$corp', '$ofno','$dt','$father','$dob','$addrtmp','$relation','$tin','$cst','$pan','$op')";
 print $sql; 
 mysqli_query($db,$sql)or die(mysqli_error());
$chk=mysqli_query($db,"select * from ledger where led='$acname' and brcode='$brcode'");
 if(mysqli_num_rows($chk)>0)
 {
    print "<script> alert('Duplicate Ledger'); history.back(1); </script>";
   // return;
}
else
{
    $code1 = substr($code,0,10);
$sql="insert into ledger values('$acname','Sundry Debtors','$op','Dr','$code1','$brcode')";
mysqli_query($db,$sql) or die('<b>Ledger creation error:'.mysqli_error());
}

 print "<font color='Green'> Data has been saved</font>";
}
?>

<script>
var ar=[];
var addr=[];
var ph=[];
var dobd=[];
var dobm=[];
var doby=[];
var pan=[];

function ch()
{
 var name=ar[document.f1.ofno.selectedIndex];
 document.f1.custname.value=name;
 document.f1.acname.value=name;
 document.f1.addr1.value=addr[document.f1.ofno.selectedIndex];
 document.f1.phone1.value=ph[document.f1.ofno.selectedIndex];
 document.f1.d1.value=dobd[document.f1.ofno.selectedIndex];
 document.f1.m1.value=dobm[document.f1.ofno.selectedIndex];
 document.f1.y1.value=doby[document.f1.ofno.selectedIndex];
document.f1.pan.value=pan[document.f1.ofno.selectedIndex];

}
function valid()
{ 
<?/* subOK="false";
 
if(document.f1.pan.value.length<10)
  {subOK="false"; alert('Invalid PAN number');
   document.f1.pan.focus();
return false; */?>
  }
 x=confirm("Do you want to save?");
 if(x)
  subOK="true";
   if(subOK=="true")
     return true;
   else
     return false;
} 
</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<CENTER>
<BR>
<FORM name='f1' ACTION='<?print $PHP_SELF?>' METHOD='POST' onSubmit="return valid()">
<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr  bgColor='c1d7bb'><td align=center>
<table cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=2 align=center bgcolor='green'><font color='ivory'><STRONG>CUSTOMER MASTER</TD></TR>
<tr bgColor='c1d7bb'><td><font color=red>Order Form No.</td>
<td><select name='ofno' onchange=ch()>
<option></option>
<?
$i=1;
/*$re=mysqli_query($db,"select formno,cust,addr,phone,dob,pan from orderform where sale_invno='-' and brcode='$brcode' and dt>='2015-04-01' order by formno desc");
while($r=mysqli_fetch_row($re))
{
 print "\n<option>$r[0]</option>";
 print "\n<script> ar[$i]='$r[1]'; ph[$i]='$r[3]'; addr[$i]='".str_replace(' ','-',$r[2])."'; pan[$i]='$r[5]';</script>";
 $dddb=explode('-',$r[4]);print '<br>dob '.$dddb[0];
 print "\n<script> dobd[$i]='$dddb[2]'; dobm[$i]='$dddb[1]'; doby[$i]='$dddb[0]'; </script>";
 $i++;
}*/
?>
</select>


Date dd  <input type=text name=d size=2 value='<?print date("d",time())?>'>
mm  <input type=text name=m size=2 value='<?print date("m",time())?>'>
yy  <input type=text name=y size=4 value='<?print date("Y",time())?>'>
</td></tr>

<tr bgColor='c1d7bb'><td><font color=red>Display Name<br><font color=blue>(A/c Head) </td><td><input type=text name='acname' size=40 autocomplete='off' onKeyDown=chkey(this);></td></tr>

<tr bgColor='c1d7bb'><td><font color=red>Name </td><td><input type=text name='custname' size=40   autocomplete='off' onKeyDown=chkey(this);></td></tr>
<tr bgColor='c1d7bb'><td>
<select name='relation'>
<option>S/o</option>
<option>D/o</option>
<option>W/o</option>
</select>

 </td><td><input type=text name='father' size=40></td></tr>

<tr bgColor='c1d7bb'><td>Date of Birth</td>
<td>
 dd  <input type=text name='d1' size=2>
mm  <input type=text name='m1' size=2>
yy  <input type=text name='y1' size=4>
</td></tr>

<tr bgColor='c1d7bb'><td>Corporate </td><td><select name='corp'><option>-</option>
<?
$re=mysqli_query($db,"select * from corp_master where branch='$brcode'  order by corpname");
while($row=mysqli_fetch_row($re))
{
 print "<option>$row[1]</option>";
}
?>
</select>
</td></tr>

<tr bgColor='c1d7bb'><td>Address1 </td><td><input type=text name='addr1' size=40></td></tr>
<tr bgColor='c1d7bb'><td>Address2 </td><td><input type=text name='addr2' size=40></td></tr>
<tr bgColor='c1d7bb'><td>City </td><td><input type=text name='city' size=40></td></tr>
<tr bgColor='c1d7bb'><td>District </td><td>
<select name='dist'>
<?
$re=mysqli_query($db,"select * from dist_master where brcode='$brcode' order by dist");
while($row=mysqli_fetch_row($re))
{
 print "<option>$row[1]</option>";
}
?>
 </select>
 
State  <select name='stat'>
<?
print "<option>KERALA</option>";
$re=mysqli_query($db,"select * from states where state !='KERALA' ");
while($row=mysqli_fetch_row($re))
{
 print "<option>$row[1]</option>";
}
?>
 </select>    
    
</td></tr>


<tr bgColor='c1d7bb'><td>Pin No </td><td><input type=text name='pin' size=40></td></tr>
<tr bgColor='c1d7bb'><td>Address(Temp) </td><td><textarea name='addrtmp' cols=39></textarea></td></tr>
<tr bgColor='c1d7bb'><td>Phone1 </td><td><input type=text name='phone1' size=15>
Phone2 <input type=text name='phone2' size=13></td></tr>
<tr bgColor='c1d7bb'><td>Email_id </td><td><input type=text name='email' size=40></td></tr>
<tr bgColor='c1d7bb'><td>Web</td><td><input type=text name='web' size=40></td></tr>
<tr bgColor='c1d7bb'><td colspan=2>GST NO <input type=text name='tin' size=10>
CST <input type=text name='cst' size=10><font color=red>
PAN <input type=text name='pan' size=10></td></tr>
<tr bgColor='c1d7bb'><td><font color=blue>Opening Balance</td> 
  <td><input type=text name='op' size=10></td></tr>

<TR bgColor='c1d7bb'><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME = 'submit' VALUE='  SAVE  '></TD></TR>

</table>
</td></tr>
</table>
</FORM>
<br>
</center>
<form name=f2 action='<?print $PHP_SELF?>' method='POST'>
<table cellspacing=1 cellpadding=1 bgcolor=red>
<tr><td bgcolor='ivory'>
<table>
<tr><td><b>Search </b>Anywhere in Cust Name </td><td><input type=text name='cname'></td>
    <td>Form No</td><td><input type=text name='fno' size=5></td>
    <td><input type=submit value='show' name='show'></td>
</tr></table>
</td></tr>
</table>
</form>
<?
if(isset($_POST['show']))
{ 
    $fno = $_REQUEST['fno'];
 $cname = $_REQUEST['cname'];
   if(strlen($fno)>0)
     $sql="select * from cust_master where branch='$brcode' and ofno='$fno' order by custname";
   else if(strlen($cname)>0)
     $sql="select * from cust_master where branch='$brcode' and acname like'%$cname%' order by acname";
}
else
  $sql="select * from cust_master where branch='$brcode' order by dt desc,ofno,custname ";
//echo $sql;
$result=mysqli_query($db,$sql)or die(mysqli_error());
print "<table cellspacing=1 cellpadding=1 border=1 width=1329>";
print "<th width=12> No.<th>Edit<th>OF NO<th>Name <th>A/c Head <th>Addr1 <th>GST NO<th>Addr 2 <th>City <th>Dist. <th>State <th>Pin <th>Phone1 <th>Phone 2";

$i=1;
while($row=mysqli_fetch_row($result))
{
    print "<tr bgColor='c1d7bb' onMouseOver=bgColor='lightsteelblue' onMouseOut=bgColor='c1d7bb'><td>".$i++."</td>";
    if($level>=2)
        print "<td align=center><a href='editcust_mast.php?code=$row[0]' title='Edit Entry' onMouseOver=window.status='';return true;><img src='../img/edit.gif' border=0></a></td>";
    else
        print "<td>&nbsp;</td>";
    print "<td>$row[15]</td><td>$row[1]</td><td>$row[13]</td><td>$row[2]</td><td>$row[21]</td><td>$row[3]</td>";
    print "<td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td>";
    print "<td>$row[9]</td>";
    print "</tr>";
}
?>
</table>
<script>
function chkey(abc)
{
 yy=abc.value
 if(yy.length>100)
 { 
   alert('Name exeeds the limit');
   return;
 }

 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }
if(yy.indexOf("&")>-1) 
  {
    alert("& is not allowed");
    abc.value='';
  }

}
</script>
