<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include("includes/top.php");
include("opendb.php");
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
      $fag10=0;
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;
  $invno_arr[]='';
  $party_arr[]='';
  $invno_prv='';
  $invno_arr2[]='';
  $invno_prv2='';

    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($spline[$x]=='INVOICE NUMBER')
            $invno_no1 = $x;
            
             if($spline[$x]=='GSTIN')
            $party_name1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
        $partyname = trim($spline[$party_name1]);
        
    if($invno1!=$invno_prv)
    {
        $query="select invno from gstrinward where invno='$invno1'  and gstin='$partyname' ";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
            $party_arr[$ctr++]=$partyname;
        }
        else
        {
          $query="select invno from gstrinward where invno='$invno1'  and gstin='$partyname'  ";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
             $party_arr[$ctr++]=$partyname;
        }  
        }
    }
        $invno_prv=$invno1;
        
    }$j++;
  }
  
  
  
  //-------------------------------------------------------------
  $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  
  $i=1;
  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($spline[$x]=='INVOICE NUMBER')
          $invno_no = $x;
         if($spline[$x]=='Invoice type')
          $invtype_no = $x;
         if($spline[$x]=='DATE')
          $invdate_no = $x;
         
            if($spline[$x]=='GSTIN')
            $vendor_no = $x;
         if($spline[$x]=='Name of Suplier')
          $cname_no = $x;
         if($spline[$x]=='PLACE OF SUPPLY')
          $state_no = $x;
        
           if($spline[$x]=='BEFORE TAX')
          $tottax_no = $x;
         
          if(trim($spline[$x])=='TOTAL INVOICE VALUE')
          $total_no = $x;
       
          if($spline[$x]=='BRANCH')
          $dlco_no = $x;
        
          if($spline[$x]=='Rate (%)')
          $tax_no = $x;
          //print $spline[$x]."--".substr($spline[$x],0,20).strlen($spline[$x])."<br>";
          if(substr($spline[$x],0,20)=='IGST')
          $igst_amount_no = $x;
          
          if($spline[$x]=='SGST')
          $sgst_amount_no = $x;
         
          if($spline[$x]=='CGST')
          $cgst_amount_no = $x;
          
          if($spline[$x]=='Cess')
          $sapinv_no = $x;
               }
    }
    if($i>1)
    {
         
          $invno = $spline[$invno_no];
          $invtype = trim($spline[$invtype_no]);
          $invdate = trim($spline[$invdate_no]);
          $invdate = str_replace('/', '-', $invdate);
          $invdate = implode('-', array_reverse(explode('-', $invdate)));
          $state = trim($spline[$state_no]);
          $cname = trim($spline[$cname_no]);
          $vendor = trim($spline[$vendor_no]);
          //$lstprice = trim($spline[$lstprice_no]);
          $tottax = trim($spline[$tottax_no]);
          $tax = trim($spline[$tax_no]);
          $tot = trim($spline[$total_no]);
          $dlco = trim($spline[$dlco_no]);
          
         
          
          $igst_amount = trim($spline[$igst_amount_no]);
          //$sgst_rate = trim($spline[$sgst_rate_no]);
          $sgst_amount = trim($spline[$sgst_amount_no]);
          //$cgst_rate = trim($spline[$cgst_rate_no]);
          $cgst_amount = trim($spline[$cgst_amount_no]);
          //$hsn = trim($spline[$hsn_no]);
      $sapinv = trim($spline[$sapinv_no]);
      
          $cname = trim($cname, '"');
          $cname=str_replace("'","", $cname);
           $lstprice = trim($lstprice,'"');
          $lstprice = str_replace(',', '', $lstprice);
          $tottax = trim($tottax,'"');
          $tottax = str_replace(',', '', $tottax);
          $tax = trim($tax,'"');
          $tax = trim($tax,'%-');
          $tax = str_replace(',', '', $tax);
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
          $dlco = str_replace(',', '', $dlco);
          $igst_amount = trim($igst_amount,'"');
          $igst_amount = str_replace(',', '', $igst_amount);
          
          $cgst_amount = trim($cgst_amount,'"');
          $cgst_amount = str_replace(',', '', $cgst_amount);
          
          $sgst_amount = trim($sgst_amount,'"');
          $sgst_amount = str_replace(',', '', $sgst_amount);

         
          if($igst_amount<='')
            $igst_amount=0;
          
          if($cgst_amount<='')
            $cgst_amount=0;
          
          if($sgst_amount<='')
            $sgst_amount=0;
            //if($tot<='')
            //$tot=0;
          $vendor_code = trim($spline[$vendor_no]);
          
          

       $cname=$cname;  
       $pq="select brman from branch where brman='$dlco' and brcode='$brcode'";
       print $pq;
       if($dlco!='')
       {
        $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 Branch...!!!');</script>";
            //return;
        }
       }
       /*print "<br> inv ".$invno_no;
       print "<br> inv ty ".$invtype_no;
       print "<br> inv dt".$invdate_no;
       print "<br> inv cname ".$cname_no;
       print "<br> inv ltp ".$tottax_no;
       print "<br> inv ttt ".$total_no;
       */
      
        if($invno_no<='' || $invtype_no<='' || $invdate_no<='' || $cname_no<='' || $tottax_no<='' )
        
        {
            print "<script>alert('You Are Selected a Wrong  File 2...!!!');</script>";
            return;
        }
      $k=$i-1;
      
      if(strlen($invno)>0 && $invdate>=$acf && $invdate<=$act )
      {
          //print "hello";
         
          $hnd=$hnd+$oth;
          if(array_search($invno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
           else
           {
                            
                                
        $sql="insert into gstrinward (invno,invtype,invdate,customer,gstin,state,aprice,tax,tot,brcode,cgstamt,sgstamt,igstamt) 
        values('$invno','$invtype','$invdate','$cname','$vendor','$state','$tottax','$tax','$tot','$brcode','$cgst_amount','$sgst_amount','$igst_amount')";
         print $sql."<br>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
         print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
      
           }
      }
      else
      {
          //print "no";
      }
      
    }
    $i++;
  }
  print "<script> alert('Successfully Uploaded....!!!');
 
  
  </script>";
// document.location='upld_gstrinward.php';
}
?>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 187);}
.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><h1>Upload GSTR INWARD</h1>
<tr ><td bgcolor='yellow'><font color='red'></font></td></tr>

<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td> <br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>