<? 
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("includes/top.php");
include("opendb.php");
if(isset($_POST['upd']))
{
    $ff=$_REQUEST['ff'];
  if($ff !="none")
  {
       $imagename=$_FILES['ff']['name'];
       $filepath= "";
       $tmp = $_FILES['ff']['tmp_name'];
       move_uploaded_file($tmp, $filepath . $imagename) or die(' err:could not copy file');
       // print "File copied :".$fl;
   }
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $ctr=1;
  $j=1;
  $gstno_arr[]='';
  $gstno_prv='';
    while(!feof($fp))
  {
     $line=fgets($fp,1024);
     $spline=explode("\t",$line);
    if($j==1)
    {
      $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      { 
           if(trim($spline[$x])=='GSTIN')
            $gstno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $gstno1 = trim($spline[$gstno_no1]);
    
    if($gstno1!=$gstno_prv)
    {
        $query="select gst from partymaster where gst='$gstno1' ";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $gstno_arr[$ctr++]=$gstno1;
        }
    }
        $gstno_prv=$gstno1;
        
    }$j++;
  }
  
  
  
  //-------------------------------------------------------------
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $i=1;
  $gstno_prv1='';
  while(!feof($fp))
  {
     $line=fgets($fp,1024);
     $spline=explode("\t",$line);
              if($i==1)
    {
      $no_col=sizeof($spline);
      for($x=0;$x<=$no_col;$x++)
      {
         if(trim($spline[$x])=='PARTY')
		   $party_no = $x;
	 	 if($spline[$x]=='ADDR')
		  $adr_no = $x;
         if($spline[$x]=='DIST')
          $dist_no = $x;
          if($spline[$x]=='STATE')
          $stat_no = $x;
         if($spline[$x]=='PIN')
          $pin_no = $x;
           if($spline[$x]=='PHONE1') 
          $phone1_no = $x;
        if($spline[$x]=='PHONE2') 
          $phone2_no = $x;
       if($spline[$x]=='MOB') 
          $mob_no = $x;
          if($spline[$x]=='EMAIL')
          $email_no = $x;
          if(trim($spline[$x])=='PAN')
          $pan_no = $x;
             if(trim($spline[$x])=='GSTIN')
          $gstn_no = $x;
          if(trim($spline[$x])=='PTYPE')
          $ptype_no = $x;
     
               }
    }
    if($i>1)
    {
          $partyno = trim($spline[$party_no]);	 			
		 	  $adrno = trim($spline[$adr_no]);
             $distno = trim($spline[$dist_no]);
          $statno = trim($spline[$stat_no]);
         //$itemdesc =mysqli_escape_string($itemdesc);
            $pin = trim($spline[$pin_no]);
          $phone1 = trim($spline[$phone1_no]);
           $phone2 = trim($spline[$phone2_no]);
          // $tax = trim($spline[$tax_no]);
          $mob = trim($spline[$mob_no]);
          $email = trim($spline[$email_no]);
          $pan = trim($spline[$pan_no]);
         // $claimno = trim($spline[$claimno_no]);
          $gstin = trim($spline[$gstn_no]);
          $ptype = trim($spline[$ptype_no]);
  // print $gstin;
   
         $partyno = trim($partyno,'"');
            $adrno = trim($adrno,'"');
            $distno = trim($distno,'"');
           $statno = trim($statno,'"');
            $pin = trim($pin,'"');
            $phone1 = trim($phone1,'"');
            $phone2 = trim($phone2,'"');
            $mob = trim($mob,'"');
           $email = trim($email,'"');
            $pan = trim($pan,'"');
            $gstin = trim($gstin,'"');
            $ptype = trim($ptype,'"');
       //print $gstin;
        /*$query=mysqli_query($db,"select brman from branch where brman='$dlco' and brcode='$brcode'");
        if(mysqli_num_rows($query)<0)
        {
            print "<script>alert('You Are Selected a Wrong File');</script>";
            return;
        }*/
        if($gstin=='')
        {
           // print $gstin;
            //print $partyno;
            //print $statno;
            print "<script>alert('You Are Selected a Wrong  File 1...!!!');</script>";
           return;
        }
      $k=$i-1;
      $le=strlen($gstin);
      //print $gstin;
      //print $le;
      if(strlen($gstin)>0)
      {
          
           if(array_search($gstin,$gstno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$gstin."<br></font>";
           }
            
           else
           {
		        
$sql1="insert into partymaster (brcode,code,cname,addr1,dist1,state1,pin1,phone1,phone2,mob,email,gst,pan,ptype) 
                 values('$brcode','$user','$partyno','$adrno','$distno','$statno','$pin','$phone1','$phone2','$mob','$email','$gstin','$pan','$ptype')";
 	      print "$sql1<br>";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
		         
           }
      }
    }
    $gstno_prv1=$gstin;
    $i++;
  }
  //$sqlupd="update warspareinvoice set tax='15.00' where tax='14.00' and brcode='$brcode'";
 	      // mysqli_query($db,$sqlupd) or die('update Err1:'.mysqli_error($db));

//document.location='upld_gst.php';
  print "<script> alert('Successfully Uploaded....!!!');</script>";

}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 17);}
.mnu{margin-left:5}
</style>
<center>
<br><br><br><br><br><br>
<form action='' method='post' ENCTYPE="multipart/form-data">
<table border=1 width=60%>
<tr><td>
<table width=100%>
<th colspan=2><h3>Upload GST Master</h3>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>
 </td></tr>
</table>
</td></tr>
</table>
</form>

