<? 
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]=='Invoice/ Debit Note/ Credit note/Supplem')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select invno from warspareinvoicepass 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])=='Sales Order No')
		   $jcno_no = $x;
	 	 if($spline[$x]=='Posting date')
		  $jcdt_no = $x;
         if($spline[$x]=='Invoice/ Debit Note/ Credit note/Supplem')
          $invno_no = $x;
         if($spline[$x]=='Claim Type')
          $invtype_no = $x;
         if($spline[$x]=='HSN for goods/ Service Accounting Code f')
          $itemtype_no = $x;
         if($spline[$x]=='Bill to Customer Name')
          $cname_no = $x;
         if($spline[$x]=='Material Number')
          $itemno_no = $x;
         if($spline[$x]=='Description of item')
          $itemdesc_no = $x;
         if($spline[$x]=='Quantity Billed')
          $qty_no = $x;
        if($spline[$x]=='Claim No')
          $claimno_no = $x;
         if($spline[$x]=='IGST Base Value')
          $grsval_no = $x;
         
         if($spline[$x]=='Invoice value')
          $total_no = $x;
        
          if($spline[$x]=='Plant Code')
          $dlco_no = $x;
          
          if($spline[$x]=='IGST Rate')
            $igstp_no = $x;
       
       if($spline[$x]=='IGST Amt')
          $tottax_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]);
          $invtype = trim($spline[$invtype_no]);
          $cname = trim($spline[$cname_no]);
          $itemno = trim($spline[$itemno_no]);
          $itemdesc = trim($spline[$itemdesc_no]);
          $itemdesc =mysqli_escape_string($itemdesc);
            $itemtype = trim($spline[$itemtype_no]);
          $lstprice = trim($spline[$lstprice_no]);
          $tottax = trim($spline[$tottax_no]);
           $grsval = trim($spline[$grsval_no]);
            $tot = trim($spline[$total_no]);
          $mrp = trim($spline[$mrp_no]);
          $dlco = trim($spline[$dlco_no]);
          $claimno = trim($spline[$claimno_no]);
          
   
          $cname = trim($cname, '"');
          $cname = str_replace(',', '', $cname);
          $qty = trim($qty,'"');
          $qty = str_replace(',', '', $qty);
            $tottax = trim($tottax,'"');
          $tottax = str_replace(',', '', $tottax);
          $grsval = trim($grsval,'"');
          $grsval = str_replace(',', '', $grsval);
          $tot = trim($tot,'"');
          $tot = str_replace(',', '', $tot);
          $mrp = trim($mrp,'"');
          $mrp = str_replace(',', '', $mrp);
          $dlco = str_replace(',', '', $dlco);
          $claimno= str_replace(',', '', $claimno);
          
          
          $igstp = trim($spline[$igstp_no]);
       
       
        
          $igstp = trim($igstp,'"');
          $igstp = trim($igstp,'%-');
          $igstp = str_replace(',', '', $igstp);          
          
          if($igstp<='')
          $igstp=0;
          
          if($igstp>0)
          {
               $gstp = $igstp;
            // $gstamt = $tottax;             
          }         
             
             
          
        $query=mysqli_query($db,"select brman from branch where brman='$dlco' and brcode='$brcode'");
        if(mysqli_num_rows($query)<=0 && $dlco!='')
        {
            print "<script>alert('You Are Selected a Wrong File');</script>";
            return;
        }
        if($jcno_no<=''||$jcdt_no<=''||$invno_no<=''||$cname_no<=''||$itemno_no<=''||$claimno_no<=''||$total_no<='')
        {
            print "<script>alert('You Are Selected a Wrong  File...!!!');</script>";
            return;
        }
      $k=$i-1;
      if(strlen($cfno)<=0)
           {
            $cfno=$claimno;   
           }
           
           $btaxa=0;
           $taxamt=0;
           
      if(strlen($claimno)>0 && $jcdt>=$acf && $jcdt<=$act)
      {
        
           
           if(array_search($invno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
            
           else
           {
		        if($grsval>=0)
		        {
		            if ($itemtype=='Labor Value' ||$itemtype=='Outside Labor' || $itemtype=='Misc. Labor')
		            {
		                $mrp=($tot/(100+$gstp))*100;
		            }
		            else
		            {
		              $mrp=($tot/(100+$gstp))*100;  
		            }
		       $tottax=$mrp*$gstp/100;
		       $gstamt = $tottax;
$sql1="insert into warspareinvoicepass (invno,invtype,invdate,entrydate,customer,partcode,partname,aprice,tot,brcode,
jcno,claimno,itemtype,saleordno,gstp,gstamt) values('$invno','$invtype','$jcdt','$jcdt','".addslashes($cname)."','$itemno','$itemdesc','$grsval','$tot','$brcode','$jcno','$claimno','$itemtype','$invno','$gstp','$gstamt')";
 	      print "$sql1<br>";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
 	       
 	       $sql2="update warspareinvoice set telconpo='1',telconpono='$invno',claimpdate='$jcdt',claimpamt='$grsval',claimptot='$tot',claimptax='$gstamt' where claimno='$claimno' and brcode='$brcode' and  partcode='$itemno' and partname='$itemdesc' ";
 	      print "$sql2<br>";
 	       //mysqli_query($db,$sql2) or die('update warr Err1:'.mysqli_error($db));
 	       
$sql3="update couponinvoice set telconpo='1',telconpono='$invno',claimpdate='$jcdt',claimpamt='$grsval',claimptot='$tot',claimptax='$gstamt' where saleordno='$claimno' and brcode='$brcode' ";
 	      print "$sql3<br>";
 	       //mysqli_query($db,$sql3) or die('update coup Err1:'.mysqli_error($db));


		         }
           }
      }
    }
    $invno_prv1=$invno;
    $i++;
  }
  

  print "<script> alert('Successfully Uploaded....!!!');document.location='upld_warantygstpass.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 Waranty Invoices GST Aproved</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>

