<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid Login') </script>";
 return;
}

include("../opendb.php");
include("../test.php");
if(isset($_POST['save']))
{
$msno=$_REQUEST['msno'];
$i=0;
foreach($msno as $a)
{
 $msno[$i]=$a; 
 $i++;
}
$custname=$_REQUEST['custname'];
$i=0;
foreach($custname as $a)
{
 $custname[$i]=$a; 
 $i++;
}
$cust =$_REQUEST['cust'];
$i=0;
foreach($cust as $a)
{
 $cust[$i]=$a; 
 $i++;
}
  for($j=0;$j<$i;$j++)
  {
    if(strlen($custname[$j])>0)
    {

       $rr=mysqli_query($db,"select * from customer where acname='$custname[$j]'");
       if(mysqli_num_rows($rr)>0)
       {
         print "Duplicate Customer  $custname[$j]";
         return;
        }
      $chre=mysqli_query($db,"select * from ledger where led='$custname[$j]' and brcode='$brcode'");
      if(mysqli_num_rows($chre)<=0)
       {
          $code=substr($custname[$j],0,3).date(ymds);
          $code=str_replace(' ','-',$code);

	  $sql="insert into ledger values('$custname[$j]','Sundry Debtors', '0','Dr', '$code', '$brcode')";
          mysqli_query($db,$sql);
        }
	else
	{
         print "Duplicate Customer  $custname[$j]";
         return;
	}
       $sql="insert into customer (brcode,code,acname) values('$brcode','$cust[$j]','$custname[$j]')";
        mysqli_query($db,$sql) or die(mysqli_error($db));
    }
  }
}
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:steelblue;color:white}
</style>
<center>
<?
$re=mysqli_query($db,"select msno,cust from minput where brcode='$brcode' and cust not in(select code from customer where brcode='$brcode')");
print "<form action='$PHP_SELF?>'><table border=0> <th> No.<th>MS NO <th>Customer";
$i=1;
while($row=mysqli_fetch_array($re))
{
 print "\n<tr><td>".$i++."</td><td><input type=text name='msno[]' size=20 value='$row[0]'></td>";
 print "<td><input type=text name='custname[]' size=40>\n";
 print "<input type=hidden name='cust[]' value='$row[cust]'></td></tr>";
}
print "\n<tr><th colspan=3 align=center><input type=submit value='SAVE' name='save'></td></tr>";
print "</table></form>";
?>
