<? 
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");

$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
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[]='';
  $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]=='Claim No.')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select invno from couponinvoice where invno='$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;
  $invno_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])=='Coupon Number')
		   $jcno_no = $x;
	 	 if($spline[$x]=='Claim Date')
		  $jcdt_no = $x;
         if($spline[$x]=='Claim No.')
          $invno_no = $x;
         
            if($spline[$x]=='Internal Claim Number')
          $itemno_no = $x;
         if($spline[$x]=='Chasis Number')
          $itemdesc_no = $x;
           if($spline[$x]=='LSPSD-Claim_amt') 
          $lstprice_no = $x;
        if($spline[$x]=='IGST-Claim_amt')
          $tottax_no = $x;
         if($spline[$x]=='Total Amount-Claim_amt')
          $total_no = $x;
         if($spline[$x]=='Total Amount-Claim_amt')
          $mrp_no = $x;
          if($spline[$x]=='Dealer Code')
          $dlco_no = $x;
          //if($spline[$x]=='Claim Number')
          //$claimno_no = $x;
           if($spline[$x]=='Credit Document Number')
          $claimref_no = $x;
               }
    }
    if($i>1)
    {
          $jcno = trim($spline[$jcno_no]);	 			
		  $jcdt = trim($spline[$jcdt_no]);	
		  $jcdt = str_replace('/', '-', $jcdt);
		  $jcdt = implode('-', array_reverse(explode('-', $jcdt)));
		  $invno = trim($spline[$invno_no]);
             $itemno = trim($spline[$itemno_no]);
          $itemdesc = trim($spline[$itemdesc_no]);
         $itemdesc =mysqli_escape_string($itemdesc);
            $lstprice = trim($spline[$lstprice_no]);
          $tottax = trim($spline[$tottax_no]);
         // $tax = trim($spline[$tax_no]);
          $tot = trim($spline[$total_no]);
          $mrp = trim($spline[$mrp_no]);
          $dlco = trim($spline[$dlco_no]);
         // $claimno = trim($spline[$claimno_no]);
          $cfno = trim($spline[$claimref_no]);
   
   
         $lstprice = trim($lstprice,'"');
          $lstprice = str_replace(',', '', $lstprice);
          $tottax = trim($tottax,'"');
          $tottax = str_replace(',', '', $tottax);
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
          $mrp = trim($mrp,'"');
          $mrp = str_replace(',', '', $mrp);
          $dlco = str_replace(',', '', $dlco);
          //$claimno= str_replace(',', '', $claimno);
          
        $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($jcno_no<=''||$invno_no<=''||$total_no<='')
        {
            /*print "Coupon". $jcno_no."<br>";
            print " Claim".$invno_no."<br>";
            print "Internal".$itemno_no."<br>";
            print "tot".$total_no."<br>";*/
            print "<script>alert('You Are Selected a Wrong  File 1...!!!');</script>";
            return;
        }
      $k=$i-1;
      
           
      if(strlen($invno)>0 && $jcdt>=$acf && $jcdt<=$act)
      {
        
           
           if(array_search($invno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
            
           else
           {
		        
$sql1="insert into couponinvoice (invno,invtype,invdate,entrydate,customer,saleordno,ref,aprice,taxamt,tot,brcode,
msno,jcno,tax) values('$invno','coupon','$jcdt','$jcdt','$itemdesc','$itemno','$itemdesc','$lstprice','$tottax','$tot','$brcode','$cfno','$jcno','18.00')";
 	      //print "$sql1<br>";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
		         
           }
      }
    }
    $invno_prv1=$invno;
    $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));


  print "<script> alert('Successfully Uploaded....!!!');document.location='upld_coupongst.php';</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 Coupon Invoices GST</h3>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>Always download the Jobcard invoices from SAP with below columns in ANY ORDER (Job Card No.,Invoice Date,Claim Number,Billing Type,<br>Item Type,Customer Name,Item No.,Item Description,Quantity Billed,List Price,Net Taxable Amount,Discount Amount,Tax Rate(%),Handling,Tax Amount,Total Claim value,MRP,Dealer Code)<br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>

