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

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

      $acf= $acrow[0];
      $act= $acrow[1];
      $fag10=0;
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 claimno from zwty_list where claimno='$invno1' ";
        $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])=='Claim Dte')
		   $cl_date = $x;
	 	 if($spline[$x]=='Internal Claim No.')
		  $internal_claimno = $x;
         if($spline[$x]=='Claim No.')
          $claim_no = $x;
         if($spline[$x]=='Claim Typ')
          $claim_typ = $x;
         if($spline[$x]=='Claim Status')
          $claim_status = $x;
         if($spline[$x]=='Job Card Number')
          $job_no = $x;
         if($spline[$x]=='Cus. Nme.')
          $cus_name = $x;
         if($spline[$x]=='Claim Amt')
          $claim_amt = $x;
          if($spline[$x]=='Approved Amt(Warranty)')
          $appr_amt = $x;
       
         if($spline[$x]=='TDS Amt')
          $tds_amt = $x;
         if($spline[$x]=='Claim Status')
          $cl_status = $x;
         if($spline[$x]=='Auto Deduction') 
          $auto_dedu = $x;
         if($spline[$x]=='Status Date')
          $status_date = $x;
         if($spline[$x]=='Pending Days')
          $pending_days = $x;
         if($spline[$x]=='SHQ Remarks')
          $shq_rem = $x;
         if($spline[$x]=='Deduction Remarks')
          $ded_rem = $x;
         if($spline[$x]=='Accounting Document')
          $acc_doc = $x;
     
               }
    }
    if($i>1)
    {
        
        
         			
		  $cldate = trim($spline[$cl_date]);	
		  $cldate = str_replace('/', '-', $cldate);
		  $cldate = implode('-', array_reverse(explode('-', $cldate)));
		  
		  $internalclaimno = trim($spline[$internal_claimno]);
          $claimno = trim($spline[$claim_no]);
          $claimtyp = trim($spline[$claim_typ]);
          $claimstatus = trim($spline[$claim_status]);
          $jobno = trim($spline[$job_no]);
          
          $cusname= trim($spline[$cus_name]);
          
          $claimamt = trim($spline[$claim_amt]);
          $claimamt = trim($claimamt,'"');
          $claimamt = str_replace(',', '', $claimamt);
          
          $appramt =trim($spline[$appr_amt]);
          $appramt = trim($appramt,'"');
          $appramt= str_replace(',', '',$appramt);
          
          
          
          $tdsamt = trim($spline[$tds_amt]);
          $tdsamt = trim($tdsamt,'"');
          $tdsamt= str_replace(',', '',$tdsamt);
          
          $clstatus = trim($spline[$cl_status]);
          $autodedu = trim($spline[$auto_dedu]);
          
          if(strlen($statusdate>0))
          {
          $statusdate = trim($spline[$status_date]);
          $statusdate = str_replace('/', '-', $statusdate);
		  $statusdate = implode('-', array_reverse(explode('-', $statusdate)));
          }
          else
          {
              $statusdate='0000-00-00';
          }
          
          $pendingdays = trim($spline[$pending_days]);
          $shqrem = mysqli_escape_string($spline[$shq_rem]);
          $dedrem = trim($spline[$ded_rem]);
          $accdoc = trim($spline[$acc_doc]);
          
         
          
         
                 
         
         
         //print $dlco;
         

        $query=mysqli_query($db,"select brman from branch where brman='$dlco' and brcode='$brcode'");
        $nn=mysqli_num_rows($query);
        //print $nn;
        
              if($claimno<='')
        {
            print "<script>alert('You Are Selected a Wrong  File...!!!');</script>";
            return;
        }
      $k=$i-1;
      if(strlen($claimno)>0 )
      {
        //print $b=array_search($invno,$invno_arr);
        
        
           if(array_search($claimno,$invno_arr)>0)
           {
               print "<font color='red'>Already Inserted.....!!!--".$claimno."<br></font>";
           }
           else
           {
              
              
                        
                          print  $sql1="insert into zwty_list values('$cldate','$internalclaimno','$claimno','$claimtyp','$claimstatus','$jobno','$cusname','$claimamt','$appramt','$tdsamt','$clstatus','$autodedu','$statusdate','$pendingdays','$shqrem','$dedrem','$accdoc')";
                            $invno_prv1=$claimno;
                            //print "<font color='blue'>".$i.". ".$sql1."HH".$itemtype."<br></font>";
                  
                    
                
                mysqli_query($db,$sql1) or die('Insert Err1:'.mysqli_error($db));
                print "<font color='blue'>".$i.".Jobcard Inserted Successsfuly--".$invno."<br></font>";
             
           }
           
      }
      else
           {
            $flag10=1;   
           }
    }
    
    $i++;
  }
  //print "<script> alert('Successfully Uploaded....!!!');document.location='upld_jobcard.php';</script>";
  if($flag10==1)
  {
   // print "<script> alert('Some Items are not in Current Financialyear....!!!');</script>";
  
  }
print "<script> alert('Successfully Uploaded....!!!');</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, 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><h1>Upload Jobcard Invoices  GST</h1>
<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,Dealer Invoice 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,Net Amount,MRP,Dealer Code)<br>(Convert it to TAB TEXT Format and upload)
 </td></tr>
</table>
</td></tr>
</table>
</form>