<?
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");
?>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
body,td{font-family:'calibri';letter-spacing:1;}
tr{background-color:rgb(95, 197, 344);}
.mnu{margin-left:5}
</style>
</head>
<body>
<center>
<br><br><br><br><br><br>
<form action='' method='post' ENCTYPE="multipart/form-data">
<table border=1 width=40%>
<tr><td>
<table width=100%>
<th colspan=2><h1>Checking Waranty Invoices</h1>
<tr><td>File <input type=file name='ff'><input type=submit value='Upload' name='upd'></td></tr>
<tr><td>(Invoice Type Desc.,Invoice date,SO No.,Customer Name,Material,<br>Material Desc.,Delivered Qty,List price Per Unit,Spares Discount,Total taxable,Handling charges,VAT Rate,VAT Amount,Grand Total,Material Type,Dealer Code) <br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>
</body>
</html>
<?

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;
  }
$tbl="waranty".$brcode.$user;
mysqli_query($db,"drop table $tbl");
mysqli_query($db,"CREATE TABLE $tbl(`invno` varchar(20) DEFAULT '0',`invtype` varchar(10) DEFAULT NULL,`invdate` date DEFAULT NULL,`entrydate` date DEFAULT NULL,`saleordno` int(10) UNSIGNED DEFAULT NULL,`customer` varchar(200) DEFAULT NULL,`addr` varchar(250) DEFAULT NULL,`tin` varchar(100) DEFAULT NULL,`partcode` varchar(100) DEFAULT NULL,`partname` varchar(200) DEFAULT NULL,`qty` double(14,2) DEFAULT NULL,`price` double(14,2) DEFAULT NULL,`disc` double(14,2) DEFAULT NULL,`aprice` double(14,2) DEFAULT NULL,`hand` double(14,2) DEFAULT NULL,`tax` double(14,2) DEFAULT NULL,`taxamt` double(14,2) DEFAULT NULL,`tot` double(14,2) DEFAULT NULL,`brcode` varchar(50) DEFAULT NULL,`paytype` varchar(30) DEFAULT '-',
  `focqty` double(14,2) DEFAULT '0.00',`warqty` double(14,2) DEFAULT '0.00',`cessp` double(14,2) DEFAULT '0.00',`cessamt` double(14,2) DEFAULT '0.00',`vatorcst` varchar(20) DEFAULT NULL,`jvno` int(11) DEFAULT '0',`msno` varchar(100) DEFAULT '-',`ref` varchar(200) DEFAULT '-',`engineer` varchar(200) DEFAULT '-',`jcno` varchar(50) DEFAULT NULL,`ac_postdt` date DEFAULT NULL,`telconpo` varchar(200) DEFAULT '0',`telconpono` varchar(200) DEFAULT NULL,`clamt` double(10,2) DEFAULT NULL,`cltax` double(10,2) DEFAULT NULL,`claimno` varchar(20) DEFAULT NULL,`itemtype` varchar(50) DEFAULT NULL)");
 
  //-------------------------------------------------------------
  $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($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]);
          
          $cname = trim($cname, '"');
          $cname = str_replace('"', '', $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 = 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($claimno)>0)
      {              
		  if($grsval<=0)
		   {
		       $tottax=$mrp*$tax/100;
$sql1="insert into $tbl (invno,invtype,invdate,entrydate,customer,partcode,partname,qty,price,aprice,hand,tax,taxamt,tot,brcode,
warqty,jcno,clamt,cltax,claimno,itemtype) values('$invno','$invtype','$jcdt','$jcdt','$cname','$itemno','$itemdesc','$qty','$disc','$mrp','$hnd','$tax','$tottax','$tot','$brcode','$qty','$jcno','$tot','$tax','$claimno','$itemtype')";
 	       mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
 	       
		   }
               
      }
    }
    $i++;
  }
  //print "<script> alert('Successfully Uploaded....!!!');document.location='upld_spareinvoice.php';</script>";
	$sql1=mysqli_query($db,"select sum(qty),sum(aprice),sum(price),sum(taxamt),sum(tot),tax,invno from $tbl where brcode='$brcode' group by invno");
   //echo "select sum(qty),sum(aprice),sum(price),sum(taxamt),sum(tot),tax,invno from $tbl where brcode='$brcode' group by invno";
	while($row1=mysqli_fetch_array($sql1))
	{
		$qry1=mysqli_query($db,"select sum(qty),sum(aprice),sum(price),sum(taxamt),sum(tot),tax,invno from warspareinvoice where invno='$row1[invno]' and brcode='$brcode' group by invno");
		while($re=mysqli_fetch_array($qry1))
		{
			if($re[0]!=$row1[0])
			{
			  print "<font color=red>Sap Qty ($row1[0]) Is Not Equal Psn Qty ($re[0])...Of INVNO: $row1[invno]</font><br>";
			  $flg=1;
			}
			if($re[1]!=$row1[1])
			{
			  print "<font color=red>Sap Aprice ($row1[1]) Is Not Equal Psn Aprice ($re[1])...Of INVNO: $row1[invno]</font><br>";
			  $flg=1;
			}
			if($re[2]!=$row1[2])
			{
			  print "<font color=red>Sap price ($row1[2]) Is Not Equal Psn price ($re[2])...Of INVNO: $row1[invno]</font><br>";	
			  $flg=1;
			}
			if($re[3]!=$row1[3])
			{
			  print "<font color=red>Sap Taxamount ($row1[3]) Is Not Equal Psn Taxamount ($re[3])...Of INVNO: $row1[invno]</font><br>";	
			  $flg=1;
			}
			if($re[4]!=$row1[4])
			{
			  print "<font color=red>Sap Total ($row1[4]) Is Not Equal Psn Total ($re[4])...Of INVNO: $row1[invno]</font><br>";	
			  $flg=1;
			}
			if($re[tax]!=$row1[tax])
			{
			  print "<font color=red>Sap Tax ($row1[tax]) Is Not Equal Psn Tax ($re[tax])...Of INVNO: $row1[invno]</font><br>";	
			  $flg=1;
			}
		}
	}
			if($flg!=1)
			{
			  print "<font color=blue>Checking Done Successfuly...!!!</font><br>";
			}
}
?>
