<?
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");
if(isset($_POST['upd']))
{
    $ff=$_REQUEST['ff'];
    
  if($ff !="none")
  {
       $imagename=$_FILES['ff']['name'];
       
       $filepath= "util/";
       $tmp = $_FILES['ff']['tmp_name'];
       move_uploaded_file($tmp, $filepath . $imagename) or die(' err:could not copy file');
        //print "File copied :".$ff;
   }
  
     $fp=fopen($filepath.$imagename,"r") or die("Cant open file");
  $ctr=1;
  $j=1;
  $invno_arr[]='';
  $invno_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($spline[$x]=='Document Number')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select sale_invoice from crnote_gst where sale_invoice='$invno1' and brcode='$brcode'";
        $rs=mysqli_query($db,$query);
        if(mysqli_num_rows($rs)>0)
        {
            $invno_arr[$ctr++]=$invno1;
        }
    }
        $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);
     //print_r($spline);
    if($i==1)
    {
      $no_col=sizeof($spline);
      
      for($x=0;$x<=$no_col;$x++)
      {
        
         if($spline[$x]=='Document Number')
         {
            $retnum_no = $x;
         }
         if($spline[$x]=='Posting Date')
         {
            $invdate_no = $x;
         }
         if($spline[$x]=='G/L Account')
         {
            $glacnt_no = $x;
         }
        if($spline[$x]=='Customer Code')
        {
            $vendor_no = $x;
        }
        if($spline[$x]=='Amount')
        {
            $aprice_no = $x;
        }
        if($spline[$x]=='Tax Code Description')
        {
            $taxdesc_no = $x;
        }
         if($spline[$x]=='Assignment')
         {
          $itemdesc_no = $x;
         }
         if($spline[$x]=='Vendor Reference')
         {
            $invno_no = $x;
         }
         if($spline[$x]=='Line Item')
         {
            $lineitem_no = $x;
         }
         
         
               }
    }
   
    if($i>1)
    {
         
        
         //print_r($lineitem_no);
        
        
        //if($lineitem_no=='1')
        //{
        $retnum=$spline[$retnum_no];
          $invno = $spline[$invno_no];
          
          $invdate = trim($spline[$invdate_no]);
          //$invdate = str_replace('/', '-', $invdate);
          $invdate = implode('-', array_reverse(explode('-', $invdate)));
          
          
          
          $glacnt_no = trim($spline[$glacnt_no]);
          $vendor_code=trim($spline[$vendor_no]);
          $lstprice = trim($spline[$aprice_no]);
          $lstprice = trim($lstprice,'"');
          $lstprice = str_replace(',', '', $lstprice);
          $taxdesc = trim($spline[$taxdesc_no]);
          $taxdesc = trim($taxdesc,'"');
          $taxtyp=substr($taxdesc,0,4);
          if($taxtyp=='CGST')
          {
              $taxtype='INTRASTATE';
          }
          if($taxtyp=='IGST')
          {
              $taxtype='INTERSTATE';
          }
         
          $taxp=substr($taxdesc,-3);
          $taxperc = trim($taxp,'%');
          
          
          $taxperc = str_replace(',', '', $taxperc);
          if($taxperc>0)
          {
          $taxamt=$lstprice*$taxperc/100;
          $taxamt=round($taxamt,2);
          $totamt=$lstprice+$taxamt;
          }
          
          $itemdesc = trim($spline[$itemdesc_no]);
          $vendref = trim($spline[$vendref_no]);
          
          $lineitem= trim($spline[$lineitem_no]);
          
          /*
          $itemno = trim($spline[$itemno_no]);
          $itemdesc = trim($spline[$itemdesc_no]);
          $itemdesc =mysqli_escape_string($itemdesc);
          $qty = trim($spline[$qty_no]);
          $lstprice = trim($spline[$lstprice_no]);
          $tottax = trim($spline[$tottax_no]);
          $disc = trim($spline[$disc_no]);
          $hnd = trim($spline[$hnd_no]);
          $tax = trim($spline[$tax_no]);
          $vat = trim($spline[$vat_no]);
          $tot = trim($spline[$total_no]);
          $itemtype = trim($spline[$itemtype_no]);
          $dlco = trim($spline[$dlco_no]);
          
          $cname = trim($cname, '"');
          $cname=str_replace("'","", $cname);
          $cname = str_replace(';', '', $cname);
          $qty = trim($qty,'"');
          $qty = str_replace(',', '', $qty);
          $lstprice = trim($lstprice,'"');
          $lstprice = str_replace(',', '', $lstprice);
          $tottax = trim($tottax,'"');
          $tottax = str_replace(',', '', $tottax);
          $disc = trim($disc,'"');
          $disc = str_replace(',', '', $disc);
          $hnd = trim($hnd,'"');
          $hnd = str_replace(',', '', $hnd);
          $tax = trim($tax,'"');
          $tax = trim($tax,'%-');
          $tax = str_replace(',', '', $tax);
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
          $vat = trim($vat,'"');
          $vat = str_replace(',', '', $vat);
          $itemtype = str_replace(',', '', $itemtype);
          $dlco = str_replace(',', '', $dlco);
          
          $vendor_code = trim($spline[$vendor_no]);
*/
          
        if($i<=3)
        {
        if($invno<='' || $lstprice<='')
        {
            print "<script>alert('You Are Selected a Wrong  File...!!!');</script>";
            return;
        }
        }
      $k=$i-1;
      //print_r($lineitem_no);
      
      if(strlen($invno)>0 && $lstprice>0 &&$lineitem=='1')
      {
         
          if(array_search($invno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
           else
           {
               $vendor_codeval=$vendor_code;
               $custqry=mysqli_query($db,"select code from cust_master where corporate='$vendor_codeval'") or die('Insert Err:'.mysqli_error($db));
               $custrow=mysqli_fetch_array($custqry);
               $custcode=$custrow[0];
               
        $sql="insert into crnote_gst (ret_num,date,sale_invoice,mcode,netamt,brcode,tax,taxp,amt2,jvno,retfrom)
        values('$retnum','$invdate','$invno','$taxtype','$lstprice','PSN236','$taxamt','$taxperc','$totamt','0','$custcode')";
        echo $sql;
        echo "<br>";
        //exit;
        //print "<font color='blue'>Successfuly Inserted.....!!!--".$invno."<br></font>";
        mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error($db));
        
           }
      }
    
    }
    $i++;
  }
  print "<script> alert('Successfully Uploaded....!!!');document.location='upld_acpost.php';</script>";

}
?>
<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 Credit Note</h1>
<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>