<? 
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;
  $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]=='Dealer Invoice Number')
            $invno_no1 = $x;
      }
    }
    if($j>1)
    {
		 $invno1 = trim($spline[$invno_no1]);
    
    if($invno1!=$invno_prv)
    {
        $query="select invno from warspareinvoice 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])=='Job Card No.')
		   $jcno_no = $x;
	 	 if($spline[$x]=='Invoice Date')
		  $jcdt_no = $x;
         if($spline[$x]=='Dealer Invoice Number')
          $invno_no = $x;
         if($spline[$x]=='Billing Type')
          $invtype_no = $x;
         if($spline[$x]=='Item Type')
          $itemtype_no = $x;
         if($spline[$x]=='Customer Name')
          $cname_no = $x;
         if($spline[$x]=='Item No.')
          $itemno_no = $x;
         if($spline[$x]=='Item Description')
          $itemdesc_no = $x;
         if($spline[$x]=='Quantity Billed')
          $qty_no = $x;
         if($spline[$x]=='List Price') 
          $lstprice_no = $x;
         if($spline[$x]=='Net Taxable Amount')
          $grsval_no = $x;
         if($spline[$x]=='Claim Rate')
          $disc_no = $x;
         if($spline[$x]=='Tax Rate(%)')
          $tax_no = $x;
         if($spline[$x]=='Handling')
          $hnd_no = $x;
         if($spline[$x]=='Tax Amount')
          $tottax_no = $x;
         if($spline[$x]=='Total Claim value')
          $total_no = $x;
         if($spline[$x]=='Gross Value')
          $mrp_no = $x;
          if($spline[$x]=='Dealer Code')
          $dlco_no = $x;
          if($spline[$x]=='Claim Number')
          $claimno_no = $x;
          if($spline[$x]=='Customer Number')
          $cusno_no = $x;
          if($spline[$x]=='Registration No.')
          $reg_no = $x;
           if($spline[$x]=='Claim Reference No.')
          $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]);
          $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);
          $qty = trim($spline[$qty_no]);
          $itemtype = trim($spline[$itemtype_no]);
          $lstprice = trim($spline[$lstprice_no]);
          $tottax = trim($spline[$tottax_no]);
          $disc = trim($spline[$disc_no]);
          $grsval = trim($spline[$grsval_no]);
          $hnd = trim($spline[$hnd_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]);
          $regno = trim($spline[$reg_no]);
          $cusno = trim($spline[$cusno_no]);
          $cfno = trim($spline[$claimref_no]);
   
   
          $cname = trim($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 = str_replace(',', '', $disc);
          $disc = trim($disc,'"');
          $grsval = trim($grsval,'"');
          $grsval = str_replace(',', '', $grsval);
          $hnd = trim($hnd,'"');
          $hnd = str_replace(',', '', $hnd);
          $tax = trim($tax,'"');
          $tax = trim($tax,'%-');
          $tax = str_replace(',', '', $tax);
          $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<=''||$jcdt_no<=''||$invno_no<=''||$cname_no<=''||$itemno_no<=''||$qty_no<=''||$tax_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;
           print "<font color='red'>ref no.....!!!--".$claimno."<br></font>";
    print "<font color='red'>inv no.....!!!--".$invno."<br></font>";
    
      if(strlen($claimno)>0)
      {
        if($tax=='14.00')
           {
            $tax='15.00';   
           }
           
           if(array_search($invno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$invno."<br></font>";
           }
            
           else
           {
               
		        if($mrp>0)
		        {
		            print "<font color='red'>grs.....!!!--".$mrp."<br></font>";
   
		            if ($itemtype=='Labor Value' ||$itemtype=='Outside Labor')
		            {
		                $mrp=$tot/115*100;
		            }
		            else
		            {
		              $mrp=$tot/114.5*100;  
		            }
		       $tottax=$mrp*$tax/100;
$sql1="insert into warspareinvoice (invno,invtype,invdate,entrydate,customer,partcode,partname,qty,price,aprice,hand,tax,taxamt,tot,brcode,
warqty,jcno,clamt,cltax,claimno,itemtype,msno,ref,saleordno) values('$invno','$invtype','$jcdt','$jcdt','".addslashes($cname)."','$itemno','$itemdesc','$qty','$disc','$mrp','$hnd','$tax','$tottax','$tot','$brcode','$qty','$jcno','$tot','$tax','$claimno','$itemtype','$cusno','$regno','$cfno')";
 	      //print "$sql1<br>";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
		         }
           }
      }
    }
    $invno_prv1=$invno;
    $i++;
  }
  $sqldel="delete from warspareinvoice where invtype='Coupon' and brcode='$brcode'";
 	       mysqli_query($db,$sqldel) or die('del Err1:'.mysqli_error($db));


  print "<script> alert('Successfully Uploaded....!!!');</script>";
//document.location='upld_warnty.php';
}
?>
<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</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>

